1 #include "G__ci.h" /* G__atpause is defined in G__ci.h */
3 #if (!defined(G__CINTVERSION)) || (G__CINTVERSION < 501438)
4 #error You need at least cint 5.14.38 to compile GiNaC-cint. Download it via http from root.cern.ch/root/Cint.html or via ftp from ftpthep.physik.uni-mainz.de/pub/cint
5 #endif // (!defined(G__CINTVERSION)) || (G__CINTVERSION < 501438)
12 #include "ginac/ginac.h"
16 #ifndef NO_NAMESPACE_GINAC
17 using namespace GiNaC;
18 #endif // ndef NO_NAMESPACE_GINAC
20 extern "C" G__value G__exec_tempfile G__P((char *file));
21 extern "C" void G__store_undo_position(void);
23 #define PROMPT1 "GiNaC> "
26 #ifdef OBSCURE_CINT_HACK
31 string ToString(const T & t)
34 ostrstream(buf,sizeof(buf)) << t << ends;
38 basic * ex::last_created_or_assigned_bp=0;
39 basic * ex::dummy_bp=0;
40 long ex::last_created_or_assigned_exp=0;
42 #endif // def OBSCURE_CINT_HACK
44 G__value exec_tempfile(string const & command);
45 char * process_permanentfile(string const & command);
46 void process_tempfile(string const & command);
48 void helpmessage(void);
49 string preprocess(char const * const line, bool & comment, bool & single_quote,
50 bool & double_quote, unsigned & open_braces);
52 void sigterm_handler(int n);
53 void initialize(void);
54 void initialize_cint(void);
56 bool readlines(istream * is, string & allcommands);
57 bool readfile(string const & filename, string & allcommands);
58 void savefile(string const & filename, string const & allcommands);
60 typedef list<char *> cplist;
62 bool redirect_output=false;
64 G__value exec_tempfile(string const & command)
67 char *tmpfilename = tempnam(NULL,"ginac");
69 fout.open(tmpfilename);
70 fout << "{" << endl << command << endl << "}" << endl;
72 G__store_undo_position();
73 retval = G__exec_tempfile(tmpfilename);
74 G__security_recover(stdout);
80 char * process_permanentfile(string const & command)
82 char *tmpfilename = tempnam(NULL,"ginac");
83 cout << "creating file " << tmpfilename << endl;
85 fout.open(tmpfilename);
86 fout << command << endl;
88 G__store_undo_position();
89 G__loadfile(tmpfilename);
90 G__security_recover(stdout);
94 void process_tempfile(string const & command)
96 #ifdef OBSCURE_CINT_HACK
97 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
98 static G__value ref_constant = exec_tempfile("constant ginac_cint_internal_constant; ginac_cint_internal_constant;");
99 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
100 static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
101 static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
102 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
103 static bool basic_type_warning_already_displayed=false;
104 #endif // def OBSCURE_CINT_HACK
106 G__value retval = exec_tempfile(command);
108 #ifdef OBSCURE_CINT_HACK
110 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
112 static unsigned out_count = 0;
113 if (TYPES_EQUAL(retval,ref_ex)) {
114 string varname = "Out"+ToString(++out_count);
115 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
116 // an ex was returned, but this is not the ex which was created last
117 // => this is not a temporary ex, but one that resides safely in memory
119 // cout << "warning: using ex from retval (experimental)" << endl;
120 ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
121 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
122 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
124 //for (int i=4-int(log10(out_count)); i>0; --i)
126 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
128 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
130 exec_tempfile(string()+"LLLAST=LLAST;\n"
132 +"LAST="+varname+";\n"
133 +"if (ginac_cint_internal_redirect_output&&"
134 +" ginac_cint_internal_fout.good()) {"
135 +" ginac_cint_internal_fout << \""+varname+" = \" << "+varname+" << endl << endl;"
137 +" cout << \""+varname+" = \" << "+varname+" << endl << endl;"
139 } else if (TYPES_EQUAL(retval,ref_symbol)||
140 TYPES_EQUAL(retval,ref_constant)||
141 TYPES_EQUAL(retval,ref_function)||
142 TYPES_EQUAL(retval,ref_power)||
143 TYPES_EQUAL(retval,ref_numeric)) {
144 if (!basic_type_warning_already_displayed) {
146 <<"WARNING: The return value of the last expression you entered was a symbol," << endl
147 << "constant, function, power or numeric, which cannot be safely displayed." << endl
148 << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl
149 << "for example (assume 'x' is a symbol):" << endl
150 << PROMPT1 "ex(x);" << endl
151 << "OutX = x" << endl << endl
152 << PROMPT1 "cout << x << endl;" << endl
153 << "x" << endl << endl
154 << "This warning will not be shown again." << endl;
155 basic_type_warning_already_displayed=true;
158 #endif // def OBSCURE_CINT_HACK
163 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
164 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
165 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
166 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
167 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
168 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
169 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
170 << "Type .help for help." << endl
174 void helpmessage(void)
176 cout << "GiNaC-cint recognizes some special commands which start with a dot:" << endl << endl
177 << " .cint switch to cint interactive mode (see cint" << endl
178 << " documentation for further details)" << endl
179 << " .function define the body of a function (necessary due to a" << endl
180 << " cint limitation)" << endl
181 << " .help the text you are currently reading" << endl
182 << " .q, .quit, .exit, .bye quit GiNaC-cint" << endl
183 << " .read filename read a file from disk and execute it in GiNaC-cint" << endl
184 << " (recursive call is possible)" << endl
185 << " .redirect [filename] redirect 'OutXY = ...' output to a file" << endl
186 << " (.redirect alone redirects output back to console)" << endl
187 << " .restart restart GiNaC-cint (does not re-read command line" << endl
189 << " .save filename save the commands you have entered so far in a file" << endl
190 << " .silent suppress 'OutXY = ...' output (variables are still" << endl
191 << " accessible)" << endl
192 << " .> [filename] same as .redirect [filename]" << endl << endl
193 << "Additionally you can exit GiNaC-cint with quit; exit; or bye;" << endl
197 string preprocess(char const * const line, bool & comment, bool & single_quote,
198 bool & double_quote, unsigned & open_braces)
200 // "preprocess" the line entered to be able to decide if the command shall be
201 // executed directly or more input is needed or this is a special command
203 // ALL whitespace will be removed
204 // all comments (/* */ and //) will be removed
205 // open and close braces ( { and } ) outside strings will be counted
208 cout << "line=" << line << endl;
209 cout << "comment=" << comment << ", single_quote=" << single_quote
210 << ",double_quote=" << double_quote << ", open_braces=" << open_braces
220 while ((line[pos]!='\0')&&!end) {
222 // last character was a \, ignore this one
225 // last character was a /, test if * or /
227 if (line[pos]=='/') {
229 } else if (line[pos]=='*') {
233 preprocessed += line[pos];
235 } else if (asterisk) {
236 // last character was a *, test if /
238 if (line[pos]=='/') {
240 } else if (line[pos]=='*') {
250 // whitespace: ignore
253 // escape character, ignore next
257 if ((!single_quote)&&(!comment)) {
258 double_quote = !double_quote;
262 if ((!double_quote)&&(!comment)) {
263 single_quote = !single_quote;
267 if ((!single_quote)&&(!double_quote)&&(!comment)) {
272 if ((!single_quote)&&(!double_quote)&&(!comment)&&(open_braces>0)) {
283 preprocessed += line[pos];
290 cout << "preprocessed=" << preprocessed << endl;
291 cout << "comment=" << comment << ", single_quote=" << single_quote
292 << ",double_quote=" << double_quote << ", open_braces=" << open_braces
301 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
302 cout << "removing file " << *it << endl;
308 void sigterm_handler(int n)
313 void initialize(void)
319 signal(SIGTERM,sigterm_handler);
323 void initialize_cint(void)
325 G__init_cint("cint"); /* initialize cint */
327 // no longer needed as of cint 5.14.31
328 // exec_tempfile("#include <string>\n");
330 #ifndef NO_NAMESPACE_GINAC
331 exec_tempfile("using namespace GiNaC;");
332 #endif // ndef NO_NAMESPACE_GINAC
334 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
335 exec_tempfile("bool ginac_cint_internal_redirect_output=false;\n");
336 exec_tempfile("ofstream ginac_cint_internal_fout;\n");
341 cout << "Restarting GiNaC-cint." << endl;
346 void redirect(string const & filename)
349 cout << "Redirecting output back to console..." << endl;
350 exec_tempfile( string()
351 +"ginac_cint_internal_redirect_output=false;\n"
352 +"ginac_cint_internal_fout.close();");
354 cout << "Redirecting output to " << filename << "..." << endl;
355 exec_tempfile( string()
356 +"ginac_cint_internal_redirect_output=true;\n"
357 +"ginac_cint_internal_fout.open(\""+filename+"\");\n");
361 bool readlines(istream * is, string & allcommands)
364 char prompt[G__ONELINE];
369 bool next_command_is_function=false;
370 bool single_quote=false;
371 bool double_quote=false;
373 unsigned open_braces=0;
375 while ((!quit)&&(!eof)) {
376 strcpy(prompt,PROMPT1);
377 bool end_of_command = false;
380 while (!end_of_command) {
382 line = G__input(prompt);
384 getline(*is,linebuffer);
385 line=linebuffer.c_str();
389 preprocessed += preprocess(line,comment,single_quote,double_quote,open_braces);
390 if ((open_braces==0)&&(!single_quote)&&(!double_quote)&&(!comment)) {
391 unsigned l=preprocessed.length();
393 (preprocessed[0]=='#')||
394 (preprocessed[0]=='.')||
395 (preprocessed[l-1]==';')||
396 (preprocessed[l-1]=='}')) {
400 strcpy(prompt,PROMPT2);
402 if ((preprocessed=="quit;")||
403 (preprocessed=="exit;")||
404 (preprocessed=="bye;")||
405 (preprocessed==".q")||
406 (preprocessed==".quit")||
407 (preprocessed==".exit")||
408 (preprocessed==".bye")) {
410 } else if (preprocessed==".function") {
411 cout << "next expression can be a function definition" << endl;
412 next_command_is_function=true;
413 } else if (preprocessed==".cint") {
414 cout << endl << "switching to cint interactive mode" << endl;
415 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
417 cout << "back from cint" << endl;
418 } else if (preprocessed==".help") {
420 } else if (preprocessed.substr(0,5)==".read") {
421 quit=readfile(preprocessed.substr(5),allcommands);
422 } else if (preprocessed.substr(0,5)==".save") {
423 command = "// "+command; // we do not want the .save command itself in saved files
424 savefile(preprocessed.substr(5),allcommands);
425 } else if (preprocessed==".restart") {
427 } else if (preprocessed.substr(0,9)==".redirect") {
428 redirect(preprocessed.substr(9));
429 } else if (preprocessed.substr(0,2)==".>") {
430 redirect(preprocessed.substr(2));
431 } else if (preprocessed==".silent") {
432 redirect("/dev/null");
433 /* test for more special commands
434 } else if (preprocessed==".xyz") {
435 cout << "special command (TBD): " << command << endl;
438 // cout << "now processing: " << command << endl;
439 if (next_command_is_function) {
440 next_command_is_function = false;
441 filenames.push_back(process_permanentfile(command));
443 process_tempfile(command);
447 // test for end of file if reading from a stream
450 // save commands only when reading from keyboard
451 allcommands += command;
458 bool readfile(string const & filename, string & allcommands)
460 cout << "Reading commands from file " << filename << "." << endl;
463 fin.open(filename.c_str());
465 quit=readlines(&fin,allcommands);
467 cout << "Cannot open " << filename << " for reading." << endl;
473 void savefile(string const & filename, string const & allcommands)
475 cout << "Saving commands to file " << filename << "." << endl;
477 fout.open(filename.c_str());
481 cout << "Cannot save commands to " << filename << "." << endl;
484 cout << "Cannot open " << filename << " for writing." << endl;
489 int main(int argc, char ** argv)
495 bool argsexist=argc>1;
498 allcommands="/* Files given as command line arguments:\n";
502 while (argc && !quit) {
503 allcommands += *argv;
505 quit=readfile(*argv,allcommands);
510 allcommands += "*/\n";
514 readlines(NULL,allcommands);