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