]> www.ginac.de Git - ginac.git/blob - cint/ginaccint.cpp
- updated for class series->class pseries
[ginac.git] / cint / ginaccint.cpp
1 #include "G__ci.h"   /* G__atpause is defined in G__ci.h */
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <ginac/ginac.h>
8 #include <list>
9
10 extern "C" G__value G__exec_tempfile G__P((char *file));
11 extern "C" void G__store_undo_position(void);
12
13 #ifdef OBSCURE_CINT_HACK
14
15 #include <strstream>
16
17 template<class T>
18 string ToString(T const & t)
19 {
20     char buf[256];
21     ostrstream(buf,sizeof(buf)) << t << ends;
22     return buf;
23 }
24
25 basic * ex::last_created_or_assigned_bp=0;
26
27 #endif // def OBSCURE_CINT_HACK
28
29 typedef list<char *> cplist;
30
31 cplist filenames;
32
33 void cleanup(void)
34 {
35     for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
36         cout << "removing file " << *it << endl;
37         remove(*it);
38         free(*it);
39     }
40 }
41
42 void sigterm_handler(int n)
43 {
44     exit(1);
45 }
46
47 bool is_whitespace_char(char c)
48 {
49     return ((c==' ')||(c=='\t')||(c=='\n')||(c=='\r')); 
50 }
51
52 char first_non_whitespace_char(char const * s)
53 {
54     int l=strlen(s);
55     int pos=0;
56     while ((pos<l)&&is_whitespace_char(s[pos])) pos++;
57     return s[pos];
58 }    
59
60 char last_non_whitespace_char(char const * s)
61 {
62     int pos=strlen(s)-1;
63     while ((pos>=0)&&is_whitespace_char(s[pos])) pos--;
64     return s[pos];
65 }    
66
67 string strip_whitespace(string const & s)
68 {
69     string s2;
70     int l=s.length();
71     for (int pos=0; pos<l; ++pos) {
72         if (!is_whitespace_char(s[pos])) s2 += s[pos];
73     }
74     return s2;
75 }
76
77 G__value exec_tempfile(string const & command)
78 {
79     G__value retval;
80     char *tmpfilename=tempnam(NULL,"ginac");
81     ofstream fout;
82     fout.open(tmpfilename);
83     fout << "{" << endl << command << endl << "}" << endl;
84     fout.close();
85     G__store_undo_position();
86     retval=G__exec_tempfile(tmpfilename);
87     G__security_recover(stdout);
88     remove(tmpfilename);
89     free(tmpfilename);
90     return retval;
91 }
92
93 char * process_permanentfile(string const & command)
94 {
95     char *tmpfilename=tempnam(NULL,"ginac");
96     cout << "creating file " << tmpfilename << endl;
97     ofstream fout;
98     fout.open(tmpfilename);
99     fout << command << endl;
100     fout.close();
101     G__store_undo_position();
102     G__loadfile(tmpfilename);
103     G__security_recover(stdout);
104     return tmpfilename;
105 }
106
107 void process_tempfile(string const & command)
108 {
109 #ifdef OBSCURE_CINT_HACK
110     static G__value ref_symbol=exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
111     static G__value ref_ex=exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
112     static G__value ref_function=exec_tempfile("sin(ginac_cint_internal_symbol);");
113     static G__value ref_power=exec_tempfile("power(ginac_cint_internal_symbol,ginac_cint_internal_symbol);");
114 #endif // def OBSCURE_CINT_HACK
115
116     G__value retval=exec_tempfile(command);
117
118 #ifdef OBSCURE_CINT_HACK
119
120     #define TYPES_EQUAL(A,B) (((A).type==(B).type)&&((A).tagnum==(B).tagnum))
121     
122     static unsigned out_count=0;
123     if (TYPES_EQUAL(retval,ref_ex)) {
124         if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
125             string varname="Out"+ToString(++out_count);
126             exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);\n"
127                           +"LLLAST=LLAST;\n"
128                           +"LLAST=LAST;\n"
129                           +"LAST="+varname+";\n"
130                           +"cout << \""+varname+" = \" << "+varname+" << endl << endl;"); 
131         } else {
132             cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
133         }
134     }
135 #endif // def OBSCURE_CINT_HACK
136 }
137
138 void greeting(void)
139 {
140     cout << "Welcome to GiNaCcint V0.2" << endl << endl;
141     cout << "To quit, type 'quit;', 'exit;', 'bye;', '.q', '.quit', '.exit' or '.bye'" << endl;
142 }
143
144 int main(void) 
145 {
146     char *line;
147     char prompt[G__ONELINE];
148
149     greeting();
150
151     atexit(cleanup);
152     signal(SIGTERM,sigterm_handler);
153     
154     G__init_cint("cint");    /* initialize cint */
155
156     exec_tempfile("#include <string>\n");
157     exec_tempfile("ex LAST,LLAST,LLLAST;\n");
158     
159     bool quit=false;
160     bool next_command_is_function=false;    
161     while (!quit) {
162         strcpy(prompt,"GiNaCcint> ");
163         int open_braces=0;
164         bool end_of_command=false;
165         string command;
166         while (!end_of_command) {
167             line=G__input(prompt);
168         
169             int pos = 0;
170             bool double_quote=false;
171             bool single_quote=false;
172             while(line[pos]!='\0') {
173                 switch(line[pos]) {
174                 case '"':
175                     if (!single_quote) double_quote = !double_quote;
176                     break;
177                 case '\'':
178                     if (!double_quote) single_quote = !single_quote;
179                     break;
180                 case '{':
181                     if ((!single_quote)&&(!double_quote)) open_braces++;
182                     break;
183                 case '}':
184                     if ((!single_quote)&&(!double_quote)) open_braces--;
185                     break;
186                 }
187                 pos++;
188             }
189             command += line;
190             command += '\n';
191             if (open_braces==0) {
192                 if ((first_non_whitespace_char(command.c_str())=='#')||
193                     (first_non_whitespace_char(command.c_str())=='.')||
194                     (last_non_whitespace_char(command.c_str())==';')||
195                     (last_non_whitespace_char(command.c_str())=='}')) {
196                     end_of_command=true;
197                 }
198             }
199             strcpy(prompt,"  (more) > ");
200         }
201         string stripped_command=strip_whitespace(command);
202         if ((stripped_command=="quit;")||
203             (stripped_command=="exit;")||
204             (stripped_command=="bye;")||
205             (stripped_command==".q")||
206             (stripped_command==".quit")||
207             (stripped_command==".exit")||
208             (stripped_command==".bye")) {
209             quit=true;
210         } else if (stripped_command==".function") {
211             cout << "next expression can be a function definition" << endl;
212             next_command_is_function=true;
213         } else if (stripped_command==".cint") {
214             cout << endl << "switching to cint interactive mode" << endl;
215             cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
216             G__pause();
217             cout << "back from cint" << endl;
218         } else if (command[0]=='.') {
219             cout << "special command (TBD): " << command << endl;
220         } else {
221             // cout << "now processing: " << command << endl;
222             if (next_command_is_function) {
223                 next_command_is_function=false;
224                 filenames.push_back(process_permanentfile(command));
225             } else {
226                 process_tempfile(command);
227             }
228         }
229     }
230
231     return 0;
232 }
233
234
235
236
237
238
239