]> www.ginac.de Git - ginac.git/blob - cint/ginaccint.bin.cpp
38d3daee07949c848bf46c780c2a1340bf8d881d
[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 bool is_whitespace_char(char c)
53 {
54     return ((c==' ') || (c=='\t') || (c=='\n') || (c=='\r')); 
55 }
56
57 char first_non_whitespace_char(char const * s)
58 {
59     int l = strlen(s);
60     int pos = 0;
61     while ((pos<l)&&is_whitespace_char(s[pos]))
62         pos++;
63     return s[pos];
64 }    
65
66 char last_non_whitespace_char(char const * s)
67 {
68     int pos = strlen(s)-1;
69     while ((pos>=0) && is_whitespace_char(s[pos]))
70         pos--;
71     return s[pos];
72 }    
73
74 string strip_whitespace(string const & s)
75 {
76     string s2;
77     int l = s.length();
78     for (int pos=0; pos<l; ++pos) {
79         if (!is_whitespace_char(s[pos])) s2 += s[pos];
80     }
81     return s2;
82 }
83
84 G__value exec_tempfile(string const & command)
85 {
86     G__value retval;
87     char *tmpfilename = tempnam(NULL,"ginac");
88     ofstream fout;
89     fout.open(tmpfilename);
90     fout << "{" << endl << command << endl << "}" << endl;
91     fout.close();
92     G__store_undo_position();
93     retval = G__exec_tempfile(tmpfilename);
94     G__security_recover(stdout);
95     remove(tmpfilename);
96     free(tmpfilename);
97     return retval;
98 }
99
100 char * process_permanentfile(string const & command)
101 {
102     char *tmpfilename = tempnam(NULL,"ginac");
103     cout << "creating file " << tmpfilename << endl;
104     ofstream fout;
105     fout.open(tmpfilename);
106     fout << command << endl;
107     fout.close();
108     G__store_undo_position();
109     G__loadfile(tmpfilename);
110     G__security_recover(stdout);
111     return tmpfilename;
112 }
113
114 void process_tempfile(string const & command)
115 {
116 #ifdef OBSCURE_CINT_HACK
117     static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
118     static G__value ref_constant = exec_tempfile("constant ginac_cint_internal_constant; ginac_cint_internal_constant;");
119     static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
120     static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
121     static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
122     static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
123     static bool basic_type_warning_already_displayed=false;
124 #endif // def OBSCURE_CINT_HACK
125
126     G__value retval = exec_tempfile(command);
127
128 #ifdef OBSCURE_CINT_HACK
129
130     #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
131     
132     static unsigned out_count = 0;
133     if (TYPES_EQUAL(retval,ref_ex)) {
134         string varname = "Out"+ToString(++out_count);
135         if (retval.obj.i!=ex::last_created_or_assigned_exp) {
136             // an ex was returned, but this is not the ex which was created last
137             // => this is not a temporary ex, but one that resides safely in memory
138             
139             // cout << "warning: using ex from retval (experimental)" << endl;
140             ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
141             exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
142         } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
143             //string varfill;
144             //for (int i=4-int(log10(out_count)); i>0; --i)
145             //    varfill += ' ';
146             exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
147         } else {
148             cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
149         }
150         exec_tempfile(string()+"LLLAST=LLAST;\n"
151                       +"LLAST=LAST;\n"
152                       +"LAST="+varname+";\n"
153                       +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
154     } else if (TYPES_EQUAL(retval,ref_symbol)||
155                TYPES_EQUAL(retval,ref_constant)||
156                TYPES_EQUAL(retval,ref_function)||
157                TYPES_EQUAL(retval,ref_power)||
158                TYPES_EQUAL(retval,ref_numeric)) {
159         if (!basic_type_warning_already_displayed) {
160             cout << endl
161                  <<"WARNING: The return value of the last expression you entered was a symbol," << endl
162                  << "constant, function, power or numeric, which cannot be safely displayed." << endl
163                  << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl
164                  << "for example (assume 'x' is a symbol):" << endl
165                  << PROMPT "ex(x);" << endl
166                  << "OutX = x" << endl << endl
167                  << PROMPT "cout << x << endl;" << endl
168                  << "x" << endl << endl
169                  << "This warning will not be shown again." << endl;
170             basic_type_warning_already_displayed=true;
171         }
172     }
173 #endif // def OBSCURE_CINT_HACK
174 }
175
176 void greeting(void)
177 {
178     cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
179     cout << "This software is provided \"as is\" without any warranty.  Copyright of Cint is" << endl
180          << "owned by Agilent Technologies Japan and Masaharu Goto.  Registration is" << endl
181          << "  __,  _______  requested, at this moment, for commercial use.  Send e-mail to" << endl
182          << " (__) *       | <MXJ02154@niftyserve.or.jp>.  The registration is free." << endl
183          << "  ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
184          << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
185          << endl;
186 }
187
188 int main(void) 
189 {
190     char *line;
191     char prompt[G__ONELINE];
192
193     if (isatty(0))
194         greeting();
195
196     atexit(cleanup);
197     signal(SIGTERM,sigterm_handler);
198     
199     G__init_cint("cint");    /* initialize cint */
200
201     // no longer needed as of cint 5.14.31
202     // exec_tempfile("#include <string>\n");
203
204     exec_tempfile("ex LAST,LLAST,LLLAST;\n");
205     
206     bool quit = false;
207     bool next_command_is_function=false;    
208     while (!quit) {
209         strcpy(prompt,PROMPT);
210         int open_braces = 0;
211         bool end_of_command = false;
212         string command;
213         while (!end_of_command) {
214             line = G__input(prompt);
215             
216             int pos = 0;
217             bool double_quote = false;
218             bool single_quote = false;
219             while(line[pos]!='\0') {
220                 switch(line[pos]) {
221                 case '"':
222                     if (!single_quote) double_quote = !double_quote;
223                     break;
224                 case '\'':
225                     if (!double_quote) single_quote = !single_quote;
226                     break;
227                 case '{':
228                     if ((!single_quote) && (!double_quote)) open_braces++;
229                     break;
230                 case '}':
231                     if ((!single_quote) && (!double_quote)) open_braces--;
232                     break;
233                 }
234                 pos++;
235             }
236             command += line;
237             command += '\n';
238             if (open_braces==0) {
239                 if ((first_non_whitespace_char(command.c_str())=='#')||
240                     (first_non_whitespace_char(command.c_str())=='.')||
241                     (last_non_whitespace_char(command.c_str())==';')||
242                     (last_non_whitespace_char(command.c_str())=='}')) {
243                     end_of_command=true;
244                 }
245             }
246             strcpy(prompt,"     > ");
247         }
248         string stripped_command=strip_whitespace(command);
249         if ((stripped_command=="quit;")||
250             (stripped_command=="exit;")||
251             (stripped_command=="bye;")||
252             (stripped_command==".q")||
253             (stripped_command==".quit")||
254             (stripped_command==".exit")||
255             (stripped_command==".bye")) {
256             quit = true;
257         } else if (stripped_command==".function") {
258             cout << "next expression can be a function definition" << endl;
259             next_command_is_function=true;
260         } else if (stripped_command==".cint") {
261             cout << endl << "switching to cint interactive mode" << endl;
262             cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
263             G__pause();
264             cout << "back from cint" << endl;
265         } else if (command[0]=='.') {
266             cout << "special command (TBD): " << command << endl;
267         } else {
268             // cout << "now processing: " << command << endl;
269             if (next_command_is_function) {
270                 next_command_is_function = false;
271                 filenames.push_back(process_permanentfile(command));
272             } else {
273                 process_tempfile(command);
274             }
275         }
276     }
277
278     return 0;
279 }
280
281
282
283
284
285
286