1 /* ginaccint.bin.cpp: Binary depends on CINTSYSDIR, better don't call it
2 * directly. Use the launcher (compiled from ginaccint.cpp) instead. */
4 #include "G__ci.h" /* G__atpause is defined in G__ci.h */
6 #if (!defined(G__CINTVERSION)) || (G__CINTVERSION < 501439)
7 #error You need at least cint 5.14.39 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
8 #endif // (!defined(G__CINTVERSION)) || (G__CINTVERSION < 501438)
15 #include "ginac/ginac.h"
19 #ifndef NO_NAMESPACE_GINAC
20 using namespace GiNaC;
21 #endif // ndef NO_NAMESPACE_GINAC
23 extern "C" G__value G__exec_tempfile G__P((char *file));
24 extern "C" void G__store_undo_position(void);
26 #define PROMPT1 "GiNaC> "
29 #ifdef OBSCURE_CINT_HACK
34 string ToString(const T & t)
37 ostrstream(buf,sizeof(buf)) << t << ends;
41 basic * ex::last_created_or_assigned_bp = 0;
42 basic * ex::dummy_bp = 0;
43 long ex::last_created_or_assigned_exp = 0;
45 #endif // def OBSCURE_CINT_HACK
47 G__value exec_tempfile(string const & command);
48 char * process_permanentfile(string const & command);
49 void process_tempfile(string const & command);
51 void helpmessage(void);
52 string preprocess(char const * const line, bool & comment, bool & single_quote,
53 bool & double_quote, unsigned & open_braces);
55 void sigterm_handler(int n);
56 void initialize(void);
57 void initialize_cint(void);
59 bool is_command(string const & command, string & preprocessed,
60 string const & comparevalue, bool substr=false);
61 bool readlines(istream * is, string & allcommands);
62 bool readfile(string const & filename, string & allcommands, bool shutup=false);
63 void savefile(string const & filename, string const & allcommands);
65 typedef list<char *> cplist;
67 bool redirect_output = false;
70 G__value exec_tempfile(string const & command)
73 char *tmpfilename = tempnam(NULL,"ginac");
75 fout.open(tmpfilename);
76 fout << "{" << endl << command << endl << "}" << endl;
78 G__store_undo_position();
79 retval = G__exec_tempfile(tmpfilename);
80 G__security_recover(stdout);
86 char * process_permanentfile(string const & command)
88 char *tmpfilename = tempnam(NULL,"ginac");
90 cout << "creating file " << tmpfilename << endl;
92 fout.open(tmpfilename);
93 fout << command << endl;
95 G__store_undo_position();
96 G__loadfile(tmpfilename);
97 G__security_recover(stdout);
101 void process_tempfile(string const & command)
103 #ifdef OBSCURE_CINT_HACK
104 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
105 static G__value ref_constant = exec_tempfile("constant ginac_cint_internal_constant; ginac_cint_internal_constant;");
106 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
107 static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
108 static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
109 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
110 static bool basic_type_warning_already_displayed = false;
111 #endif // def OBSCURE_CINT_HACK
113 G__value retval = exec_tempfile(command);
115 #ifdef OBSCURE_CINT_HACK
117 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
119 static unsigned out_count = 0;
120 if (TYPES_EQUAL(retval,ref_ex)) {
121 string varname = "Out"+ToString(++out_count);
122 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
123 // an ex was returned, but this is not the ex which was created last
124 // => this is not a temporary ex, but one that resides safely in memory
126 // cout << "warning: using ex from retval (experimental)" << endl;
127 ex::dummy_bp = ((ex *)(void *)(retval.obj.i))->bp;
128 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
129 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
130 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
132 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
134 exec_tempfile(string()+"LLLAST=LLAST;\n"
136 +"LAST="+varname+";\n"
137 +"if (ginac_cint_internal_redirect_output&&"
138 +" ginac_cint_internal_fout.good()) {"
139 +" ginac_cint_internal_fout << \""+varname+" = \" << "+varname+" << endl << endl;"
141 +" cout << \""+varname+" = \" << "+varname+" << endl << endl;"
143 } else if (TYPES_EQUAL(retval,ref_symbol)||
144 TYPES_EQUAL(retval,ref_constant)||
145 TYPES_EQUAL(retval,ref_function)||
146 TYPES_EQUAL(retval,ref_power)||
147 TYPES_EQUAL(retval,ref_numeric)) {
148 if (!basic_type_warning_already_displayed) {
150 <<"WARNING: The return value of the last expression you entered was a symbol," << endl
151 << "constant, function, power or numeric, which cannot be safely displayed." << endl
152 << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl
153 << "for example (assume 'x' is a symbol):" << endl
154 << PROMPT1 "ex(x);" << endl
155 << "OutX = x" << endl << endl
156 << PROMPT1 "cout << x << endl;" << endl
157 << "x" << endl << endl
158 << "This warning will not be shown again." << endl;
159 basic_type_warning_already_displayed = true;
162 #endif // def OBSCURE_CINT_HACK
168 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION
169 << ", Cint V" << G__CINTVERSION << ")\n";
170 cout << " __, _______ GiNaC: (C) 1999-2000 Johannes Gutenberg University Mainz,\n"
171 << " (__) * | Germany. Cint C/C++ interpreter: (C) 1995-2000 Masaharu\n"
172 << " ._) i N a C | Goto and Agilent Technologies, Japan. This is free software\n"
173 << "<-------------' with ABSOLUTELY NO WARRANTY. For details, type `.warranty'\n"
174 << "Type `.help' for help.\n\n";
178 void helpmessage(void)
180 cout << "GiNaC-cint recognizes some special commands which start with a dot:\n\n"
181 << " .cint switch to cint interactive mode (see cint\n"
182 << " documentation for further details)\n"
183 << " .function define the body of a function (necessary due to a\n"
184 << " cint limitation)\n"
185 << " .help the text you are currently reading\n"
186 << " .q, .quit, .exit, .bye quit GiNaC-cint\n"
187 << " .read filename read a file from disk and execute it in GiNaC-cint\n"
188 << " (recursive call is possible)\n"
189 << " .redirect [filename] redirect 'OutXY = ...' output to a file\n"
190 << " (.redirect alone redirects output back to console)\n"
191 << " .restart restart GiNaC-cint (does not re-read command line\n"
193 << " .save filename save the commands you have entered so far in a file\n"
194 << " .silent suppress 'OutXY = ...' output (variables are still\n"
196 << " .warranty information on redistribution and warranty\n"
197 << " .> [filename] same as .redirect [filename]\n\n"
198 << "Instead of '.cmd' you can also write '//GiNaC-cint.cmd' to be compatible with\n"
199 << "programs that will be compiled later.\n"
200 << "Additionally you can exit GiNaC-cint with quit; exit; or bye;\n\n";
204 void warrantymessage(void)
206 cout << "GiNaC is free software; you can redistribute it and/or modify it under the\n"
207 << "the terms of the GNU General Public License as published by the Free Software\n"
208 << "Foundation; either version 2 of the License, or (at your option) any later\n"
210 << "This program is distributed in the hope that it will be useful, but WITHOUT\n"
211 << "ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n"
212 << "FOR A PARTICULAR PURPOSE. See the GNU General Public License for more\n"
214 << "You should have received a copy of the GNU General Public License along with\n"
215 << "this program. If not, write to the Free Software Foundation, 675 Mass Ave,\n"
216 << "Cambridge, MA 02139, USA.\n\n";
217 cout << "Cint and associated tools are copyright by Agilent Technologies Japan Company\n"
218 << "and Masaharu Goto <MXJ02154@niftyserve.or.jp>.\n"
219 << "Source code, binary executable or library of Cint and associated tools can be\n"
220 << "used, modified and distributed with no royalty for any purpose provided that\n"
221 << "the copyright notice appear in all copies and that both that copyright notice\n"
222 << "and this permission notice appear in supporting documentation.\n"
223 << "Agilent Technologies Japan and the author make no representations about the\n"
224 << "suitability of this software for any purpose. It is provided \"AS IS\"\n"
225 << "without express or implied warranty.\n";
229 /** "preprocess" the line entered to be able to decide if the command shall be
230 * executed directly or more input is needed or this is a special command.
231 * All whitespace will be removed. All comments will be removed. Open and
232 * close braces ( { and } ) outside strings will be counted. */
233 string preprocess(char const * const line, bool & comment, bool & single_quote,
234 bool & double_quote, unsigned & open_braces)
241 bool asterisk = false;
242 while ((line[pos]!='\0')&&!end) {
244 // last character was a \, ignore this one
247 // last character was a /, test if * or /
249 if (line[pos]=='/') {
251 } else if (line[pos]=='*') {
255 preprocessed += line[pos];
257 } else if (asterisk) {
258 // last character was a *, test if /
260 if (line[pos]=='/') {
262 } else if (line[pos]=='*') {
272 // whitespace: ignore
275 // escape character, ignore next
279 if ((!single_quote)&&(!comment)) {
280 double_quote = !double_quote;
284 if ((!double_quote)&&(!comment)) {
285 single_quote = !single_quote;
289 if ((!single_quote)&&(!double_quote)&&(!comment)) {
294 if ((!single_quote)&&(!double_quote)&&(!comment)&&(open_braces>0)) {
305 preprocessed += line[pos];
316 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
318 cout << "removing file " << *it << endl;
324 void sigterm_handler(int n)
329 void initialize(void)
332 signal(SIGTERM,sigterm_handler);
336 void initialize_cint(void)
338 G__init_cint("cint"); /* initialize cint */
340 #ifndef NO_NAMESPACE_GINAC
341 exec_tempfile("using namespace GiNaC;");
342 #endif // ndef NO_NAMESPACE_GINAC
344 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
345 exec_tempfile("bool ginac_cint_internal_redirect_output=false;\n");
346 exec_tempfile("ofstream ginac_cint_internal_fout;\n");
351 cout << "Restarting GiNaC-cint." << endl;
356 void redirect(string const & filename,
361 cout << "Redirecting output back to console..." << endl;
362 exec_tempfile( string()
363 +"ginac_cint_internal_redirect_output=false;\n"
364 +"ginac_cint_internal_fout.close();");
367 cout << "Redirecting output to " << filename << "..." << endl;
368 exec_tempfile( string()
369 +"ginac_cint_internal_redirect_output=true;\n"
370 +"ginac_cint_internal_fout.open(\""+filename+"\");\n");
374 /** Sort out command line options and evaluate them. Returns true if it
375 * succeeds and false otherwise. */
376 bool evaloption(const string & option)
378 if (option=="--version") {
379 cout << "GiNaC-cint (" << PACKAGE << " V" << VERSION
380 << ", Cint V" << G__CINTVERSION << ")\n";
383 if (option=="--help") {
384 cout << "GiNaC-cint (" << PACKAGE << " V" << VERSION
385 << ", Cint V" << G__CINTVERSION << ")\n";
386 cout << "usage: ginaccint [option] [file ...]\n";
387 cout << " --help print this help message and exit\n"
388 << " --silent invoke ginaccint in silent mode\n"
389 << " --version print GiNaC version and Cint version and exit\n";
392 if (option=="--silent") {
393 redirect("/dev/null",true);
400 bool is_command(string const & command,
401 string & preprocessed,
402 string const & comparevalue,
405 bool single_quote = false;
406 bool double_quote = false;
407 bool comment = false;
408 unsigned open_braces = 0;
409 if ((preprocessed=="."+comparevalue)||
410 substr&&(preprocessed.substr(0,comparevalue.length()+1)==
414 if ((command=="//GiNaC-cint."+comparevalue+"\n")||
415 substr&&(command.substr(0,comparevalue.length()+13)==
416 "//GiNaC-cint."+comparevalue)) {
417 preprocessed = preprocess(command.substr(12).c_str(),comment,
418 single_quote,double_quote,open_braces);
424 bool readlines(istream * is,
425 string & allcommands)
428 char prompt[G__ONELINE];
433 bool next_command_is_function = false;
434 bool single_quote = false;
435 bool double_quote = false;
436 bool comment = false;
437 unsigned open_braces = 0;
439 while ((!quit)&&(!eof)) {
440 strcpy(prompt,PROMPT1);
441 bool end_of_command = false;
444 while (!end_of_command) {
446 line = G__input(prompt);
448 getline(*is,linebuffer);
449 line = linebuffer.c_str();
453 preprocessed += preprocess(line,comment,single_quote,double_quote,open_braces);
454 if ((open_braces==0)&&(!single_quote)&&(!double_quote)&&(!comment)) {
455 unsigned l = preprocessed.length();
457 (preprocessed[0]=='#')||
458 (preprocessed[0]=='.')||
459 (preprocessed[l-1]==';')||
460 (preprocessed[l-1]=='}')) {
461 end_of_command = true;
464 strcpy(prompt,PROMPT2);
466 if ((preprocessed=="quit;")||
467 (preprocessed=="exit;")||
468 (preprocessed=="bye;")||
469 (is_command(command,preprocessed,"quit"))||
470 (is_command(command,preprocessed,"exit"))||
471 (is_command(command,preprocessed,"bye"))||
472 (is_command(command,preprocessed,"q"))) {
474 } else if (is_command(command,preprocessed,"function")) {
476 cout << "next expression can be a function definition" << endl;
477 next_command_is_function = true;
478 } else if (is_command(command,preprocessed,"cint")) {
479 cout << endl << "switching to cint interactive mode" << endl;
480 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
482 cout << "back from cint" << endl;
483 } else if (is_command(command,preprocessed,"help")) {
485 } else if (is_command(command,preprocessed,"read",true)) {
486 quit = readfile(preprocessed.substr(5),allcommands);
487 } else if (is_command(command,preprocessed,"save",true)) {
488 command = "/* "+command+" */"; // we do not want the .save command itself in saved files
489 savefile(preprocessed.substr(5),allcommands);
490 } else if (is_command(command,preprocessed,"restart")) {
492 } else if (is_command(command,preprocessed,"redirect",true)) {
493 redirect(preprocessed.substr(9));
494 } else if (is_command(command,preprocessed,">",true)) {
495 redirect(preprocessed.substr(2));
496 } else if (is_command(command,preprocessed,"silent")) {
498 redirect("/dev/null");
504 } else if (is_command(command,preprocessed,"warranty")) {
506 /* test for more special commands
507 } else if (preprocessed==".xyz") {
508 cout << "special command (TBD): " << command << endl;
510 } else if (command.substr(0,2)=="#!") {
511 // ignore lines which indicate that this file is executed as a script
513 // cout << "now processing: " << command << endl;
514 if (next_command_is_function) {
515 next_command_is_function = false;
516 filenames.push_back(process_permanentfile(command));
518 process_tempfile(command);
522 // test for end of file if reading from a stream
525 // save commands only when reading from keyboard
526 allcommands += command;
533 bool readfile(string const & filename,
534 string & allcommands,
538 cout << "Reading commands from file " << filename << "." << endl;
541 fin.open(filename.c_str());
543 quit = readlines(&fin,allcommands);
545 cout << "Cannot open " << filename << " for reading." << endl;
550 void savefile(string const & filename, string const & allcommands)
552 cout << "Saving commands to file " << filename << "." << endl;
554 fout.open(filename.c_str());
558 cout << "Cannot save commands to " << filename << "." << endl;
561 cout << "Cannot open " << filename << " for writing." << endl;
566 int main(int argc, char * *argv)
572 // sort out and evaluate recognized options from the argument list
573 for (int i=1; i<argc; ++i)
574 if (evaloption(argv[i])) {
575 for (int j=i; j<argc-1; ++j)
579 bool argsexist = argc>1;
581 // greet the user if it makes sense
582 if (isatty(0) && !silent)
585 // evaluate files given as command line arguments
587 allcommands = "/* Files given as command line arguments:\n";
589 while (argc && !quit) {
590 allcommands += string(argv[argc])+'\n';
591 quit = readfile(argv[argc], allcommands, silent);
594 allcommands += "*/\n";
597 // evaluate input from command line or script
599 readlines(NULL, allcommands);