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 < 501460)
7 #error You need at least cint 5.14.60 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 using namespace GiNaC;
21 extern "C" G__value G__exec_tempfile G__P((char *file));
22 extern "C" void G__store_undo_position(void);
24 #define PROMPT1 "GiNaC> "
27 #ifdef OBSCURE_CINT_HACK
32 std::string ToString(const T & t)
35 ostrstream(buf,sizeof(buf)) << t << ends;
39 basic * ex::last_created_or_assigned_bp = 0;
40 basic * ex::dummy_bp = 0;
41 long ex::last_created_or_assigned_exp = 0;
43 #endif // def OBSCURE_CINT_HACK
45 G__value exec_tempfile(std::string const & command);
46 char * process_permanentfile(std::string const & command);
47 void process_tempfile(std::string const & command);
48 void printversionmessage(std::ostream & os);
50 void helpmessage(void);
51 std::string preprocess(char const * const line, bool & comment, bool & single_quote,
52 bool & double_quote, unsigned & open_braces);
54 void sigterm_handler(int n);
55 void initialize(void);
56 void initialize_cint(void);
58 bool is_command(std::string const & command, std::string & preprocessed,
59 std::string const & comparevalue, bool substr=false);
60 bool readlines(istream * is, std::string & allcommands);
61 bool readfile(std::string const & filename, std::string & allcommands, bool shutup=false);
62 void savefile(std::string const & filename, std::string const & allcommands);
64 typedef list<char *> cplist;
66 bool redirect_output = false;
69 G__value exec_tempfile(std::string const & command)
72 char *tmpfilename = tempnam(NULL,"ginac");
74 fout.open(tmpfilename);
75 fout << "{" << std::endl << command << std::endl << "}" << std::endl;
77 G__store_undo_position();
78 retval = G__exec_tempfile(tmpfilename);
79 G__security_recover(stdout);
85 char * process_permanentfile(std::string const & command)
87 char *tmpfilename = tempnam(NULL,"ginac");
89 std::cout << "creating file " << tmpfilename << std::endl;
91 fout.open(tmpfilename);
92 fout << command << std::endl;
94 G__store_undo_position();
95 G__loadfile(tmpfilename);
96 G__security_recover(stdout);
100 void process_tempfile(std::string const & command)
102 #ifdef OBSCURE_CINT_HACK
103 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
104 static G__value ref_constant = exec_tempfile("constant ginac_cint_internal_constant; ginac_cint_internal_constant;");
105 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
106 static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
107 static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
108 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
109 static bool basic_type_warning_already_displayed = false;
110 #endif // def OBSCURE_CINT_HACK
112 G__value retval = exec_tempfile(command);
114 #ifdef OBSCURE_CINT_HACK
116 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
118 static unsigned out_count = 0;
119 if (TYPES_EQUAL(retval,ref_ex)) {
120 std::string varname = "Out"+ToString(++out_count);
121 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
122 // an ex was returned, but this is not the ex which was created last
123 // => this is not a temporary ex, but one that resides safely in memory
125 // std::cout << "warning: using ex from retval (experimental)" << std::endl;
126 ex::dummy_bp = ((ex *)(void *)(retval.obj.i))->bp;
127 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
128 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
129 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
131 std::cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << std::endl;
133 exec_tempfile(std::string()+"LLLAST=LLAST;\n"
135 +"LAST="+varname+";\n"
136 +"if (ginac_cint_internal_redirect_output&&"
137 +" ginac_cint_internal_fout.good()) {"
138 +" ginac_cint_internal_fout << \""+varname+" = \" << "+varname+" << endl << endl;"
140 +" std::cout << \""+varname+" = \" << "+varname+" << endl << endl;"
142 } else if (TYPES_EQUAL(retval,ref_symbol)||
143 TYPES_EQUAL(retval,ref_constant)||
144 TYPES_EQUAL(retval,ref_function)||
145 TYPES_EQUAL(retval,ref_power)||
146 TYPES_EQUAL(retval,ref_numeric)) {
147 if (!basic_type_warning_already_displayed) {
148 std::cout << std::endl
149 <<"WARNING: The return value of the last expression you entered was a symbol," << std::endl
150 << "constant, function, power or numeric, which cannot be safely displayed." << std::endl
151 << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << std::endl
152 << "for example (assume 'x' is a symbol):" << std::endl
153 << PROMPT1 "ex(x);" << std::endl
154 << "OutX = x" << std::endl << std::endl
155 << PROMPT1 "cout << x << endl;" << std::endl
156 << "x" << std::endl << std::endl
157 << "This warning will not be shown again." << std::endl;
158 basic_type_warning_already_displayed = true;
161 #endif // def OBSCURE_CINT_HACK
165 void printversionmessage(std::ostream & os)
167 os << "GiNaC-cint (" << PACKAGE << " V" << VERSION
168 << ", Cint V" << G__CINTVERSION/1000000
169 << '.' << G__CINTVERSION/1000%1000
170 << '.' << G__CINTVERSION%1000 << ')' << endl;
176 std::cout << "Welcome to ";
177 printversionmessage(std::cout);
178 std::cout << " __, _______ GiNaC: (C) 1999-2001 Johannes Gutenberg University Mainz,\n"
179 << " (__) * | Germany. Cint C/C++ interpreter: (C) 1995-2001 Masaharu\n"
180 << " ._) i N a C | Goto and Agilent Technologies, Japan. This is free software\n"
181 << "<-------------' with ABSOLUTELY NO WARRANTY. For details, type `.warranty'\n"
182 << "Type `.help' for help.\n\n";
186 void helpmessage(void)
188 std::cout << "GiNaC-cint recognizes some special commands which start with a dot:\n\n"
189 << " .cint switch to cint interactive mode (see cint\n"
190 << " documentation for further details)\n"
191 << " .function define the body of a function (necessary due to a\n"
192 << " cint limitation)\n"
193 << " .help the text you are currently reading\n"
194 << " .q, .quit, .exit, .bye quit GiNaC-cint\n"
195 << " .read filename read a file from disk and execute it in GiNaC-cint\n"
196 << " (recursive call is possible)\n"
197 << " .redirect [filename] redirect 'OutXY = ...' output to a file\n"
198 << " (.redirect alone redirects output back to console)\n"
199 << " .restart restart GiNaC-cint (does not re-read command line\n"
201 << " .save filename save the commands you have entered so far in a file\n"
202 << " .silent suppress 'OutXY = ...' output (variables are still\n"
204 << " .warranty information on redistribution and warranty\n"
205 << " .> [filename] same as .redirect [filename]\n\n"
206 << "Instead of '.cmd' you can also write '//GiNaC-cint.cmd' to be compatible with\n"
207 << "programs that will be compiled later.\n"
208 << "Additionally you can exit GiNaC-cint with quit; exit; or bye;\n\n";
212 void warrantymessage(void)
214 std::cout << "GiNaC is free software; you can redistribute it and/or modify it under the\n"
215 << "the terms of the GNU General Public License as published by the Free Software\n"
216 << "Foundation; either version 2 of the License, or (at your option) any later\n"
218 << "This program is distributed in the hope that it will be useful, but WITHOUT\n"
219 << "ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\n"
220 << "FOR A PARTICULAR PURPOSE. See the GNU General Public License for more\n"
222 << "You should have received a copy of the GNU General Public License along with\n"
223 << "this program. If not, write to the Free Software Foundation, 675 Mass Ave,\n"
224 << "Cambridge, MA 02139, USA.\n\n";
225 std::cout << "Cint and associated tools are copyright by Agilent Technologies Japan Company\n"
226 << "and Masaharu Goto <MXJ02154@niftyserve.or.jp>.\n"
227 << "Source code, binary executable or library of Cint and associated tools can be\n"
228 << "used, modified and distributed with no royalty for any purpose provided that\n"
229 << "the copyright notice appear in all copies and that both that copyright notice\n"
230 << "and this permission notice appear in supporting documentation.\n"
231 << "Agilent Technologies Japan and the author make no representations about the\n"
232 << "suitability of this software for any purpose. It is provided \"AS IS\"\n"
233 << "without express or implied warranty.\n";
237 /** "preprocess" the line entered to be able to decide if the command shall be
238 * executed directly or more input is needed or this is a special command.
239 * All whitespace will be removed. All comments will be removed. Open and
240 * close braces ( { and } ) outside strings will be counted. */
241 std::string preprocess(char const * const line, bool & comment, bool & single_quote,
242 bool & double_quote, unsigned & open_braces)
244 std::string preprocessed;
249 bool asterisk = false;
250 while ((line[pos]!='\0')&&!end) {
252 // last character was a \, ignore this one
255 // last character was a /, test if * or /
257 if (line[pos]=='/') {
259 } else if (line[pos]=='*') {
263 preprocessed += line[pos];
265 } else if (asterisk) {
266 // last character was a *, test if /
268 if (line[pos]=='/') {
270 } else if (line[pos]=='*') {
280 // whitespace: ignore
283 // escape character, ignore next
287 if ((!single_quote)&&(!comment)) {
288 double_quote = !double_quote;
292 if ((!double_quote)&&(!comment)) {
293 single_quote = !single_quote;
297 if ((!single_quote)&&(!double_quote)&&(!comment)) {
302 if ((!single_quote)&&(!double_quote)&&(!comment)&&(open_braces>0)) {
313 preprocessed += line[pos];
324 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
326 std::cout << "removing file " << *it << std::endl;
332 void sigterm_handler(int n)
338 void initialize(void)
341 signal(SIGTERM, sigterm_handler);
345 void initialize_cint(void)
347 G__init_cint("cint"); /* initialize cint */
349 exec_tempfile("using namespace GiNaC;");
350 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
351 exec_tempfile("bool ginac_cint_internal_redirect_output = false;\n");
352 exec_tempfile("ofstream ginac_cint_internal_fout;\n");
357 std::cout << "Restarting GiNaC-cint." << std::endl;
362 void redirect(std::string const & filename,
367 std::cout << "Redirecting output back to console..." << std::endl;
368 exec_tempfile(std::string()
369 +"ginac_cint_internal_redirect_output=false;\n"
370 +"ginac_cint_internal_fout.close();");
373 std::cout << "Redirecting output to " << filename << "..." << std::endl;
374 exec_tempfile(std::string()
375 +"ginac_cint_internal_redirect_output=true;\n"
376 +"ginac_cint_internal_fout.open(\""+filename+"\");\n");
380 /** Sort out command line options and evaluate them. Returns true if it
381 * succeeds and false otherwise. */
382 bool evaloption(const std::string & option)
384 if (option=="--version") {
385 printversionmessage(std::cout);
388 if (option=="--help") {
389 printversionmessage(std::cout);
390 std::cout << "usage: ginaccint [option] [file ...]\n";
391 std::cout << " --help print this help message and exit\n"
392 << " --silent invoke ginaccint in silent mode\n"
393 << " --version print GiNaC version and Cint version and exit\n";
396 if (option=="--silent") {
397 redirect("/dev/null",true);
404 bool is_command(std::string const & command,
405 std::string & preprocessed,
406 std::string const & comparevalue,
409 bool single_quote = false;
410 bool double_quote = false;
411 bool comment = false;
412 unsigned open_braces = 0;
413 if ((preprocessed=="."+comparevalue)||
414 substr&&(preprocessed.substr(0,comparevalue.length()+1)==
418 if ((command=="//GiNaC-cint."+comparevalue+"\n") ||
420 (command.substr(0,comparevalue.length()+13)=="//GiNaC-cint."+comparevalue)) {
421 preprocessed = preprocess(command.substr(12).c_str(),comment,
422 single_quote,double_quote,open_braces);
428 bool readlines(istream * is,
429 std::string & allcommands)
432 char prompt[G__ONELINE];
433 std::string linebuffer;
437 bool next_command_is_function = false;
438 bool single_quote = false;
439 bool double_quote = false;
440 bool comment = false;
441 unsigned open_braces = 0;
443 while ((!quit)&&(!eof)) {
444 strcpy(prompt,PROMPT1);
445 bool end_of_command = false;
447 std::string preprocessed;
448 while (!end_of_command) {
450 line = G__input(prompt);
452 getline(*is,linebuffer);
453 line = linebuffer.c_str();
457 preprocessed += preprocess(line,comment,single_quote,double_quote,open_braces);
458 if ((open_braces==0)&&(!single_quote)&&(!double_quote)&&(!comment)) {
459 unsigned l = preprocessed.length();
461 (preprocessed[0]=='#')||
462 (preprocessed[0]=='.')||
463 (preprocessed[l-1]==';')||
464 (preprocessed[l-1]=='}')) {
465 end_of_command = true;
468 strcpy(prompt,PROMPT2);
470 if ((preprocessed=="quit;")||
471 (preprocessed=="exit;")||
472 (preprocessed=="bye;")||
473 (is_command(command,preprocessed,"quit"))||
474 (is_command(command,preprocessed,"exit"))||
475 (is_command(command,preprocessed,"bye"))||
476 (is_command(command,preprocessed,"q"))) {
478 } else if (is_command(command,preprocessed,"function")) {
480 std::cout << "next expression can be a function definition" << std::endl;
481 next_command_is_function = true;
482 } else if (is_command(command,preprocessed,"cint")) {
483 std::cout << std::endl << "switching to cint interactive mode" << std::endl;
484 std::cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << std::endl;
486 std::cout << "back from cint" << std::endl;
487 } else if (is_command(command,preprocessed,"help")) {
489 } else if (is_command(command,preprocessed,"read",true)) {
490 quit = readfile(preprocessed.substr(5),allcommands);
491 } else if (is_command(command,preprocessed,"save",true)) {
492 command = "/* "+command+" */"; // we do not want the .save command itself in saved files
493 savefile(preprocessed.substr(5),allcommands);
494 } else if (is_command(command,preprocessed,"restart")) {
496 } else if (is_command(command,preprocessed,"redirect",true)) {
497 redirect(preprocessed.substr(9));
498 } else if (is_command(command,preprocessed,">",true)) {
499 redirect(preprocessed.substr(2));
500 } else if (is_command(command,preprocessed,"silent")) {
502 redirect("/dev/null");
508 } else if (is_command(command,preprocessed,"warranty")) {
510 /* insert tests for more special commands here */
511 } else if (command.substr(0,2)=="#!") {
512 // ignore lines which indicate that this file is executed as a script
514 // std::cout << "now processing: " << command << std::endl;
515 if (next_command_is_function) {
516 next_command_is_function = false;
517 filenames.push_back(process_permanentfile(command));
519 process_tempfile(command);
523 // test for end of file if reading from a stream
526 // save commands only when reading from keyboard
527 allcommands += command;
534 bool readfile(std::string const & filename,
535 std::string & allcommands,
539 std::cout << "Reading commands from file " << filename << "." << std::endl;
542 fin.open(filename.c_str());
544 quit = readlines(&fin,allcommands);
546 std::cout << "Cannot open " << filename << " for reading." << std::endl;
551 void savefile(std::string const & filename, std::string const & allcommands)
553 std::cout << "Saving commands to file " << filename << "." << std::endl;
555 fout.open(filename.c_str());
559 std::cout << "Cannot save commands to " << filename << "." << std::endl;
562 std::cout << "Cannot open " << filename << " for writing." << std::endl;
567 int main(int argc, char * *argv)
569 std::string allcommands;
573 // sort out and evaluate recognized options from the argument list
574 for (int i=1; i<argc; ++i)
575 if (evaloption(argv[i])) {
576 for (int j=i; j<argc-1; ++j)
580 bool argsexist = argc>1;
582 // greet the user if it makes sense
583 if (isatty(0) && !silent)
586 // evaluate files given as command line arguments
588 allcommands = "/* Files given as command line arguments:\n";
590 while (argc && !quit) {
591 allcommands += std::string(argv[argc])+'\n';
592 quit = readfile(argv[argc], allcommands, silent);
595 allcommands += "*/\n";
598 // evaluate input from command line or script
600 readlines(NULL, allcommands);