]> www.ginac.de Git - ginac.git/blob - cint/ginaccint.bin.cpp
- ginac.h includes version.h (created by configure script), which contains
[ginac.git] / cint / ginaccint.bin.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 "config.h"
9 #include <list>
10
11 extern "C" G__value G__exec_tempfile G__P((char *file));
12 extern "C" void G__store_undo_position(void);
13
14 #define PROMPT "GiNaC> "
15
16 #ifdef OBSCURE_CINT_HACK
17
18 #include <strstream>
19
20 template<class T>
21 string ToString(const T & t)
22 {
23     char buf[256];
24     ostrstream(buf,sizeof(buf)) << t << ends;
25     return buf;
26 }
27
28 basic * ex::last_created_or_assigned_bp=0;
29 basic * ex::dummy_bp=0;
30 long ex::last_created_or_assigned_exp=0;
31
32 #endif // def OBSCURE_CINT_HACK
33
34 typedef list<char *> cplist;
35
36 cplist filenames;
37
38 void cleanup(void)
39 {
40     for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
41         cout << "removing file " << *it << endl;
42         remove(*it);
43         free(*it);
44     }
45 }
46
47 void sigterm_handler(int n)
48 {
49     exit(1);
50 }
51
52 G__value exec_tempfile(string const & command)
53 {
54     G__value retval;
55     char *tmpfilename = tempnam(NULL,"ginac");
56     ofstream fout;
57     fout.open(tmpfilename);
58     fout << "{" << endl << command << endl << "}" << endl;
59     fout.close();
60     G__store_undo_position();
61     retval = G__exec_tempfile(tmpfilename);
62     G__security_recover(stdout);
63     remove(tmpfilename);
64     free(tmpfilename);
65     return retval;
66 }
67
68 char * process_permanentfile(string const & command)
69 {
70     char *tmpfilename = tempnam(NULL,"ginac");
71     cout << "creating file " << tmpfilename << endl;
72     ofstream fout;
73     fout.open(tmpfilename);
74     fout << command << endl;
75     fout.close();
76     G__store_undo_position();
77     G__loadfile(tmpfilename);
78     G__security_recover(stdout);
79     return tmpfilename;
80 }
81
82 void process_tempfile(string const & command)
83 {
84 #ifdef OBSCURE_CINT_HACK
85     static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
86     static G__value ref_constant = exec_tempfile("constant ginac_cint_internal_constant; ginac_cint_internal_constant;");
87     static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
88     static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
89     static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
90     static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
91     static bool basic_type_warning_already_displayed=false;
92 #endif // def OBSCURE_CINT_HACK
93
94     G__value retval = exec_tempfile(command);
95
96 #ifdef OBSCURE_CINT_HACK
97
98     #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
99     
100     static unsigned out_count = 0;
101     if (TYPES_EQUAL(retval,ref_ex)) {
102         string varname = "Out"+ToString(++out_count);
103         if (retval.obj.i!=ex::last_created_or_assigned_exp) {
104             // an ex was returned, but this is not the ex which was created last
105             // => this is not a temporary ex, but one that resides safely in memory
106             
107             // cout << "warning: using ex from retval (experimental)" << endl;
108             ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
109             exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
110         } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
111             //string varfill;
112             //for (int i=4-int(log10(out_count)); i>0; --i)
113             //    varfill += ' ';
114             exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
115         } else {
116             cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
117         }
118         exec_tempfile(string()+"LLLAST=LLAST;\n"
119                       +"LLAST=LAST;\n"
120                       +"LAST="+varname+";\n"
121                       +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
122     } else if (TYPES_EQUAL(retval,ref_symbol)||
123                TYPES_EQUAL(retval,ref_constant)||
124                TYPES_EQUAL(retval,ref_function)||
125                TYPES_EQUAL(retval,ref_power)||
126                TYPES_EQUAL(retval,ref_numeric)) {
127         if (!basic_type_warning_already_displayed) {
128             cout << endl
129                  <<"WARNING: The return value of the last expression you entered was a symbol," << endl
130                  << "constant, function, power or numeric, which cannot be safely displayed." << endl
131                  << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl
132                  << "for example (assume 'x' is a symbol):" << endl
133                  << PROMPT "ex(x);" << endl
134                  << "OutX = x" << endl << endl
135                  << PROMPT "cout << x << endl;" << endl
136                  << "x" << endl << endl
137                  << "This warning will not be shown again." << endl;
138             basic_type_warning_already_displayed=true;
139         }
140     }
141 #endif // def OBSCURE_CINT_HACK
142 }
143
144 void greeting(void)
145 {
146     cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
147     cout << "This software is provided \"as is\" without any warranty.  Copyright of Cint is" << endl
148          << "owned by Agilent Technologies Japan and Masaharu Goto.  Registration is" << endl
149          << "  __,  _______  requested, at this moment, for commercial use.  Send e-mail to" << endl
150          << " (__) *       | <MXJ02154@niftyserve.or.jp>.  The registration is free." << endl
151          << "  ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
152          << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
153          << endl;
154 }
155
156 string preprocess(char const * line, bool & comment, bool & single_quote,
157                   bool & double_quote, unsigned & open_braces)
158 {
159     // "preprocess" the line entered to be able to decide if the command shall be
160     // executed directly or more input is needed or this is a special command
161
162     // all whitespace will be removed
163     // all comments (/* */ and //) will be removed
164     // open and close braces ( { and } ) outside strings will be counted 
165
166     /*
167     cout << "line=" << line << endl;
168     cout << "comment=" << comment << ", single_quote=" << single_quote
169          << ",double_quote=" << double_quote << ", open_braces=" << open_braces
170          << endl;
171     */
172     
173     string preprocessed;
174     int pos=0;
175     bool end=false;
176     bool escape=false;
177     bool slash=false;
178     bool asterisk=false;
179     while ((line[pos]!='\0')&&!end) {
180         if (escape) {
181             // last character was a \, ignore this one
182             escape=false;
183         } else if (slash) {
184             // last character was a /, test if * or /
185             slash=false;
186             if (line[pos]=='/') {
187                 end=true;
188             } else if (line[pos]=='*') {
189                 comment=true;
190             } else {
191                 preprocessed += '/';
192             }
193         } else if (asterisk) {
194             // last character was a *, test if /
195             asterisk=false;
196             if (line[pos]=='/') {
197                 comment=false;
198             } else if (line[pos]=='*') {
199                 preprocessed += '*';
200                 asterisk=true;
201             }
202         } else {
203             switch (line[pos]) {
204             case ' ':
205             case '\t':
206             case '\n':
207             case '\r':
208                 // whitespace: ignore
209                 break;
210             case '\\':
211                 // escape character, ignore next
212                 escape=true;
213                 break;
214             case '"':
215                 if ((!single_quote)&&(!comment)) {
216                     double_quote = !double_quote;
217                 }
218                 break;
219                 case '\'':
220                     if ((!double_quote)&&(!comment)) {
221                         single_quote = !single_quote;
222                     }
223                     break;
224             case '{':
225                 if ((!single_quote)&&(!double_quote)&&(!comment)) {
226                     open_braces++;
227                 }
228                 break;
229             case '}':
230                 if ((!single_quote)&&(!double_quote)&&(!comment)&&(open_braces>0)) {
231                     open_braces--;
232                 }
233                 break;
234             case '/':
235                 slash=true;
236                 break;
237             case '*':
238                 asterisk=true;
239                 break;
240             default:
241                 preprocessed += line[pos];
242             }
243         }
244         pos++;
245     }
246
247     /*
248     cout << "preprocessed=" << preprocessed << endl;
249     cout << "comment=" << comment << ", single_quote=" << single_quote
250          << ",double_quote=" << double_quote << ", open_braces=" << open_braces
251          << endl;
252     */
253     
254     return preprocessed;
255 }
256                 
257 int main(void) 
258 {
259     char *line;
260     char prompt[G__ONELINE];
261
262     if (isatty(0))
263         greeting();
264
265     atexit(cleanup);
266     signal(SIGTERM,sigterm_handler);
267     
268     G__init_cint("cint");    /* initialize cint */
269
270     // no longer needed as of cint 5.14.31
271     // exec_tempfile("#include <string>\n");
272
273     exec_tempfile("ex LAST,LLAST,LLLAST;\n");
274     
275     bool quit = false;
276     bool next_command_is_function=false;
277     bool single_quote=false;
278     bool double_quote=false;
279     bool comment=false;
280     unsigned open_braces=0;
281
282     while (!quit) {
283         strcpy(prompt,PROMPT);
284         bool end_of_command = false;
285         string command;
286         string preprocessed;
287         while (!end_of_command) {
288             line = G__input(prompt);
289             command += line;
290             command += "\n";
291             preprocessed += preprocess(line,comment,single_quote,double_quote,open_braces);
292             if ((open_braces==0)&&(!single_quote)&&(!double_quote)&&(!comment)) {
293                 unsigned l=preprocessed.length();
294                 if ((l==0)||
295                     (preprocessed[0]=='#')||
296                     (preprocessed[0]=='.')||
297                     (preprocessed[l-1]==';')||
298                     (preprocessed[l-1]=='}')) {
299                     end_of_command=true;
300                 }
301             }
302             strcpy(prompt,"     > ");
303         }
304         if ((preprocessed=="quit;")||
305             (preprocessed=="exit;")||
306             (preprocessed=="bye;")||
307             (preprocessed==".q")||
308             (preprocessed==".quit")||
309             (preprocessed==".exit")||
310             (preprocessed==".bye")) {
311             quit = true;
312         } else if (preprocessed==".function") {
313             cout << "next expression can be a function definition" << endl;
314             next_command_is_function=true;
315         } else if (preprocessed==".cint") {
316             cout << endl << "switching to cint interactive mode" << endl;
317             cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
318             G__pause();
319             cout << "back from cint" << endl;
320         /* test for more special commands
321         } else if (preprocessed==".xyz") {
322             cout << "special command (TBD): " << command << endl;
323         */
324         } else {
325             // cout << "now processing: " << command << endl;
326             if (next_command_is_function) {
327                 next_command_is_function = false;
328                 filenames.push_back(process_permanentfile(command));
329             } else {
330                 process_tempfile(command);
331             }
332         }
333     }
334
335     return 0;
336 }
337
338
339
340
341
342
343