1 #include "G__ci.h" /* G__atpause is defined in G__ci.h */
7 #include "ginac/ginac.h"
11 #ifndef NO_NAMESPACE_GINAC
12 using namespace GiNaC;
13 #endif // ndef NO_NAMESPACE_GINAC
15 extern "C" G__value G__exec_tempfile G__P((char *file));
16 extern "C" void G__store_undo_position(void);
18 #define PROMPT1 "GiNaC> "
21 #ifdef OBSCURE_CINT_HACK
26 string ToString(const T & t)
29 ostrstream(buf,sizeof(buf)) << t << ends;
33 basic * ex::last_created_or_assigned_bp=0;
34 basic * ex::dummy_bp=0;
35 long ex::last_created_or_assigned_exp=0;
37 #endif // def OBSCURE_CINT_HACK
39 G__value exec_tempfile(string const & command);
40 char * process_permanentfile(string const & command);
41 void process_tempfile(string const & command);
43 void helpmessage(void);
44 string preprocess(char const * const line, bool & comment, bool & single_quote,
45 bool & double_quote, unsigned & open_braces);
47 void sigterm_handler(int n);
48 void initialize(void);
49 bool readlines(istream * is, string & allcommands);
50 bool readfile(string const & filename, string & allcommands);
51 void savefile(string const & filename, string const & allcommands);
53 typedef list<char *> cplist;
56 G__value exec_tempfile(string const & command)
59 char *tmpfilename = tempnam(NULL,"ginac");
61 fout.open(tmpfilename);
62 fout << "{" << endl << command << endl << "}" << endl;
64 G__store_undo_position();
65 retval = G__exec_tempfile(tmpfilename);
66 G__security_recover(stdout);
72 char * process_permanentfile(string const & command)
74 char *tmpfilename = tempnam(NULL,"ginac");
75 cout << "creating file " << tmpfilename << endl;
77 fout.open(tmpfilename);
78 fout << command << endl;
80 G__store_undo_position();
81 G__loadfile(tmpfilename);
82 G__security_recover(stdout);
86 void process_tempfile(string const & command)
88 #ifdef OBSCURE_CINT_HACK
89 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
90 static G__value ref_constant = exec_tempfile("constant ginac_cint_internal_constant; ginac_cint_internal_constant;");
91 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
92 static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
93 static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
94 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
95 static bool basic_type_warning_already_displayed=false;
96 #endif // def OBSCURE_CINT_HACK
98 G__value retval = exec_tempfile(command);
100 #ifdef OBSCURE_CINT_HACK
102 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
104 static unsigned out_count = 0;
105 if (TYPES_EQUAL(retval,ref_ex)) {
106 string varname = "Out"+ToString(++out_count);
107 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
108 // an ex was returned, but this is not the ex which was created last
109 // => this is not a temporary ex, but one that resides safely in memory
111 // cout << "warning: using ex from retval (experimental)" << endl;
112 ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
113 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
114 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
116 //for (int i=4-int(log10(out_count)); i>0; --i)
118 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
120 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
122 exec_tempfile(string()+"LLLAST=LLAST;\n"
124 +"LAST="+varname+";\n"
125 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
126 } else if (TYPES_EQUAL(retval,ref_symbol)||
127 TYPES_EQUAL(retval,ref_constant)||
128 TYPES_EQUAL(retval,ref_function)||
129 TYPES_EQUAL(retval,ref_power)||
130 TYPES_EQUAL(retval,ref_numeric)) {
131 if (!basic_type_warning_already_displayed) {
133 <<"WARNING: The return value of the last expression you entered was a symbol," << endl
134 << "constant, function, power or numeric, which cannot be safely displayed." << endl
135 << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl
136 << "for example (assume 'x' is a symbol):" << endl
137 << PROMPT1 "ex(x);" << endl
138 << "OutX = x" << endl << endl
139 << PROMPT1 "cout << x << endl;" << endl
140 << "x" << endl << endl
141 << "This warning will not be shown again." << endl;
142 basic_type_warning_already_displayed=true;
145 #endif // def OBSCURE_CINT_HACK
150 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
151 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
152 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
153 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
154 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
155 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
156 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
157 << "Type .help for help." << endl
161 void helpmessage(void)
163 cout << "GiNaC-cint recognizes some special commands which start with a dot:" << endl << endl
164 << " .q, .quit, .exit, .bye quit GiNaC-cint" << endl
165 << " .help the text you are currently reading" << endl
166 << " .function define the body of a function (necessary due to a" << endl
167 << " cint limitation)" << endl
168 << " .cint switch to cint interactive mode (see cint" << endl
169 << " documentation for further details)" << endl
170 << " .read filename read a file from disk and execute it in GiNaC-cint" << endl
171 << " (recursive call is possible)" << endl
172 << " .save filename save the commands you have entered so far in a file" << endl << endl
173 << "Additionally you can exit GiNaC-cint with quit; exit; or bye;" << endl
177 string preprocess(char const * const line, bool & comment, bool & single_quote,
178 bool & double_quote, unsigned & open_braces)
180 // "preprocess" the line entered to be able to decide if the command shall be
181 // executed directly or more input is needed or this is a special command
183 // all whitespace will be removed
184 // all comments (/* */ and //) will be removed
185 // open and close braces ( { and } ) outside strings will be counted
188 cout << "line=" << line << endl;
189 cout << "comment=" << comment << ", single_quote=" << single_quote
190 << ",double_quote=" << double_quote << ", open_braces=" << open_braces
200 while ((line[pos]!='\0')&&!end) {
202 // last character was a \, ignore this one
205 // last character was a /, test if * or /
207 if (line[pos]=='/') {
209 } else if (line[pos]=='*') {
213 preprocessed += line[pos];
215 } else if (asterisk) {
216 // last character was a *, test if /
218 if (line[pos]=='/') {
220 } else if (line[pos]=='*') {
230 // whitespace: ignore
233 // escape character, ignore next
237 if ((!single_quote)&&(!comment)) {
238 double_quote = !double_quote;
242 if ((!double_quote)&&(!comment)) {
243 single_quote = !single_quote;
247 if ((!single_quote)&&(!double_quote)&&(!comment)) {
252 if ((!single_quote)&&(!double_quote)&&(!comment)&&(open_braces>0)) {
263 preprocessed += line[pos];
270 cout << "preprocessed=" << preprocessed << endl;
271 cout << "comment=" << comment << ", single_quote=" << single_quote
272 << ",double_quote=" << double_quote << ", open_braces=" << open_braces
281 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
282 cout << "removing file " << *it << endl;
288 void sigterm_handler(int n)
293 void initialize(void)
299 signal(SIGTERM,sigterm_handler);
301 G__init_cint("cint"); /* initialize cint */
303 // no longer needed as of cint 5.14.31
304 // exec_tempfile("#include <string>\n");
306 #ifndef NO_NAMESPACE_GINAC
307 exec_tempfile("using namespace GiNaC;");
308 #endif // ndef NO_NAMESPACE_GINAC
310 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
313 bool readlines(istream * is, string & allcommands)
316 char prompt[G__ONELINE];
321 bool next_command_is_function=false;
322 bool single_quote=false;
323 bool double_quote=false;
325 unsigned open_braces=0;
327 while ((!quit)&&(!eof)) {
328 strcpy(prompt,PROMPT1);
329 bool end_of_command = false;
332 while (!end_of_command) {
334 line = G__input(prompt);
336 getline(*is,linebuffer);
337 line=linebuffer.c_str();
341 preprocessed += preprocess(line,comment,single_quote,double_quote,open_braces);
342 if ((open_braces==0)&&(!single_quote)&&(!double_quote)&&(!comment)) {
343 unsigned l=preprocessed.length();
345 (preprocessed[0]=='#')||
346 (preprocessed[0]=='.')||
347 (preprocessed[l-1]==';')||
348 (preprocessed[l-1]=='}')) {
352 strcpy(prompt,PROMPT2);
354 if ((preprocessed=="quit;")||
355 (preprocessed=="exit;")||
356 (preprocessed=="bye;")||
357 (preprocessed==".q")||
358 (preprocessed==".quit")||
359 (preprocessed==".exit")||
360 (preprocessed==".bye")) {
362 } else if (preprocessed==".function") {
363 cout << "next expression can be a function definition" << endl;
364 next_command_is_function=true;
365 } else if (preprocessed==".cint") {
366 cout << endl << "switching to cint interactive mode" << endl;
367 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
369 cout << "back from cint" << endl;
370 } else if (preprocessed==".help") {
372 } else if (preprocessed.substr(0,5)==".read") {
373 quit=readfile(preprocessed.substr(5),allcommands);
374 } else if (preprocessed.substr(0,5)==".save") {
375 command = "// "+command; // we do not want the .save command itself in saved files
376 savefile(preprocessed.substr(5),allcommands);
377 /* test for more special commands
378 } else if (preprocessed==".xyz") {
379 cout << "special command (TBD): " << command << endl;
382 // cout << "now processing: " << command << endl;
383 if (next_command_is_function) {
384 next_command_is_function = false;
385 filenames.push_back(process_permanentfile(command));
387 process_tempfile(command);
391 // test for end of file if reading from a stream
394 // save commands only when reading from keyboard
395 allcommands += command;
402 bool readfile(string const & filename, string & allcommands)
404 cout << "Reading commands from file " << filename << "." << endl;
407 fin.open(filename.c_str());
409 quit=readlines(&fin,allcommands);
411 cout << "Cannot open " << filename << " for reading." << endl;
417 void savefile(string const & filename, string const & allcommands)
419 cout << "Saving commands to file " << filename << "." << endl;
421 fout.open(filename.c_str());
425 cout << "Cannot save commands to " << filename << "." << endl;
428 cout << "Cannot open " << filename << " for writing." << endl;
433 int main(int argc, char ** argv)
439 bool argsexist=argc>1;
442 allcommands="/* Files given as command line arguments:\n";
446 while (argc && !quit) {
447 allcommands += *argv;
449 quit=readfile(*argv,allcommands);
454 allcommands += "*/\n";
458 readlines(NULL,allcommands);