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 #ifdef OBSCURE_CINT_HACK
19 string ToString(const T & t)
22 ostrstream(buf,sizeof(buf)) << t << ends;
26 basic * ex::last_created_or_assigned_bp=0;
27 basic * ex::dummy_bp=0;
28 long ex::last_created_or_assigned_exp=0;
30 #endif // def OBSCURE_CINT_HACK
32 typedef list<char *> cplist;
38 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
39 cout << "removing file " << *it << endl;
45 void sigterm_handler(int n)
50 bool is_whitespace_char(char c)
52 return ((c==' ') || (c=='\t') || (c=='\n') || (c=='\r'));
55 char first_non_whitespace_char(char const * s)
59 while ((pos<l)&&is_whitespace_char(s[pos]))
64 char last_non_whitespace_char(char const * s)
66 int pos = strlen(s)-1;
67 while ((pos>=0) && is_whitespace_char(s[pos]))
72 string strip_whitespace(string const & s)
76 for (int pos=0; pos<l; ++pos) {
77 if (!is_whitespace_char(s[pos])) s2 += s[pos];
82 G__value exec_tempfile(string const & command)
85 char *tmpfilename = tempnam(NULL,"ginac");
87 fout.open(tmpfilename);
88 fout << "{" << endl << command << endl << "}" << endl;
90 G__store_undo_position();
91 retval = G__exec_tempfile(tmpfilename);
92 G__security_recover(stdout);
98 char * process_permanentfile(string const & command)
100 char *tmpfilename = tempnam(NULL,"ginac");
101 cout << "creating file " << tmpfilename << endl;
103 fout.open(tmpfilename);
104 fout << command << endl;
106 G__store_undo_position();
107 G__loadfile(tmpfilename);
108 G__security_recover(stdout);
112 void process_tempfile(string const & command)
114 #ifdef OBSCURE_CINT_HACK
115 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
116 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
117 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
118 static G__value ref_power = exec_tempfile("power(ginac_cint_internal_symbol,ginac_cint_internal_symbol);");
119 #endif // def OBSCURE_CINT_HACK
121 G__value retval = exec_tempfile(command);
123 #ifdef OBSCURE_CINT_HACK
125 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
127 static unsigned out_count = 0;
128 if (TYPES_EQUAL(retval,ref_ex)) {
129 string varname = "Out"+ToString(++out_count);
130 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
131 // an ex was returned, but this is not the ex which was created last
132 // => this is not a temporary ex, but one that resides safely in memory
134 // cout << "warning: using ex from retval (experimental)" << endl;
135 ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
136 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
137 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
139 //for (int i=4-int(log10(out_count)); i>0; --i)
141 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
143 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
145 exec_tempfile(string()+"LLLAST=LLAST;\n"
147 +"LAST="+varname+";\n"
148 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
150 #endif // def OBSCURE_CINT_HACK
155 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
156 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
157 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
158 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
159 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
160 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
161 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
168 char prompt[G__ONELINE];
174 signal(SIGTERM,sigterm_handler);
176 G__init_cint("cint"); /* initialize cint */
178 exec_tempfile("#include <string>\n");
179 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
182 bool next_command_is_function=false;
184 strcpy(prompt,"GiNaC> ");
186 bool end_of_command = false;
188 while (!end_of_command) {
189 line = G__input(prompt);
192 bool double_quote = false;
193 bool single_quote = false;
194 while(line[pos]!='\0') {
197 if (!single_quote) double_quote = !double_quote;
200 if (!double_quote) single_quote = !single_quote;
203 if ((!single_quote) && (!double_quote)) open_braces++;
206 if ((!single_quote) && (!double_quote)) open_braces--;
213 if (open_braces==0) {
214 if ((first_non_whitespace_char(command.c_str())=='#')||
215 (first_non_whitespace_char(command.c_str())=='.')||
216 (last_non_whitespace_char(command.c_str())==';')||
217 (last_non_whitespace_char(command.c_str())=='}')) {
221 strcpy(prompt," > ");
223 string stripped_command=strip_whitespace(command);
224 if ((stripped_command=="quit;")||
225 (stripped_command=="exit;")||
226 (stripped_command=="bye;")||
227 (stripped_command==".q")||
228 (stripped_command==".quit")||
229 (stripped_command==".exit")||
230 (stripped_command==".bye")) {
232 } else if (stripped_command==".function") {
233 cout << "next expression can be a function definition" << endl;
234 next_command_is_function=true;
235 } else if (stripped_command==".cint") {
236 cout << endl << "switching to cint interactive mode" << endl;
237 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
239 cout << "back from cint" << endl;
240 } else if (command[0]=='.') {
241 cout << "special command (TBD): " << command << endl;
243 // cout << "now processing: " << command << endl;
244 if (next_command_is_function) {
245 next_command_is_function = false;
246 filenames.push_back(process_permanentfile(command));
248 process_tempfile(command);