1 #include "G__ci.h" /* G__atpause is defined in G__ci.h */
7 #include "ginac/ginac.h"
11 extern "C" G__value G__exec_tempfile G__P((char *file));
12 extern "C" void G__store_undo_position(void);
14 #define PROMPT "GiNaC> "
16 #ifdef OBSCURE_CINT_HACK
21 string ToString(const T & t)
24 ostrstream(buf,sizeof(buf)) << t << ends;
28 basic * ex::last_created_or_assigned_bp=0;
29 basic * ex::dummy_bp=0;
30 long ex::last_created_or_assigned_exp=0;
32 #endif // def OBSCURE_CINT_HACK
34 typedef list<char *> cplist;
40 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
41 cout << "removing file " << *it << endl;
47 void sigterm_handler(int n)
52 G__value exec_tempfile(string const & command)
55 char *tmpfilename = tempnam(NULL,"ginac");
57 fout.open(tmpfilename);
58 fout << "{" << endl << command << endl << "}" << endl;
60 G__store_undo_position();
61 retval = G__exec_tempfile(tmpfilename);
62 G__security_recover(stdout);
68 char * process_permanentfile(string const & command)
70 char *tmpfilename = tempnam(NULL,"ginac");
71 cout << "creating file " << tmpfilename << endl;
73 fout.open(tmpfilename);
74 fout << command << endl;
76 G__store_undo_position();
77 G__loadfile(tmpfilename);
78 G__security_recover(stdout);
82 void process_tempfile(string const & command)
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
94 G__value retval = exec_tempfile(command);
96 #ifdef OBSCURE_CINT_HACK
98 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
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
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()) {
112 //for (int i=4-int(log10(out_count)); i>0; --i)
114 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
116 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
118 exec_tempfile(string()+"LLLAST=LLAST;\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) {
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;
141 #endif // def OBSCURE_CINT_HACK
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
156 string preprocess(char const * line, bool & comment, bool & single_quote,
157 bool & double_quote, unsigned & open_braces)
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
162 // all whitespace will be removed
163 // all comments (/* */ and //) will be removed
164 // open and close braces ( { and } ) outside strings will be counted
167 cout << "line=" << line << endl;
168 cout << "comment=" << comment << ", single_quote=" << single_quote
169 << ",double_quote=" << double_quote << ", open_braces=" << open_braces
179 while ((line[pos]!='\0')&&!end) {
181 // last character was a \, ignore this one
184 // last character was a /, test if * or /
186 if (line[pos]=='/') {
188 } else if (line[pos]=='*') {
193 } else if (asterisk) {
194 // last character was a *, test if /
196 if (line[pos]=='/') {
198 } else if (line[pos]=='*') {
208 // whitespace: ignore
211 // escape character, ignore next
215 if ((!single_quote)&&(!comment)) {
216 double_quote = !double_quote;
220 if ((!double_quote)&&(!comment)) {
221 single_quote = !single_quote;
225 if ((!single_quote)&&(!double_quote)&&(!comment)) {
230 if ((!single_quote)&&(!double_quote)&&(!comment)&&(open_braces>0)) {
241 preprocessed += line[pos];
248 cout << "preprocessed=" << preprocessed << endl;
249 cout << "comment=" << comment << ", single_quote=" << single_quote
250 << ",double_quote=" << double_quote << ", open_braces=" << open_braces
260 char prompt[G__ONELINE];
266 signal(SIGTERM,sigterm_handler);
268 G__init_cint("cint"); /* initialize cint */
270 // no longer needed as of cint 5.14.31
271 // exec_tempfile("#include <string>\n");
273 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
276 bool next_command_is_function=false;
277 bool single_quote=false;
278 bool double_quote=false;
280 unsigned open_braces=0;
283 strcpy(prompt,PROMPT);
284 bool end_of_command = false;
287 while (!end_of_command) {
288 line = G__input(prompt);
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();
295 (preprocessed[0]=='#')||
296 (preprocessed[0]=='.')||
297 (preprocessed[l-1]==';')||
298 (preprocessed[l-1]=='}')) {
302 strcpy(prompt," > ");
304 if ((preprocessed=="quit;")||
305 (preprocessed=="exit;")||
306 (preprocessed=="bye;")||
307 (preprocessed==".q")||
308 (preprocessed==".quit")||
309 (preprocessed==".exit")||
310 (preprocessed==".bye")) {
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;
319 cout << "back from cint" << endl;
320 /* test for more special commands
321 } else if (preprocessed==".xyz") {
322 cout << "special command (TBD): " << command << endl;
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));
330 process_tempfile(command);