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 #define PROMPT "GiNaC> "
16 #ifdef OBSCURE_CINT_HACK
21 string ToString(const T & t)
24 ostrstream(buf,sizeof(buf)) << t << ends;
28 basic * ex::last_created_or_assigned_bp=0;
29 basic * ex::dummy_bp=0;
30 long ex::last_created_or_assigned_exp=0;
32 #endif // def OBSCURE_CINT_HACK
34 typedef list<char *> cplist;
40 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
41 cout << "removing file " << *it << endl;
47 void sigterm_handler(int n)
52 bool is_whitespace_char(char c)
54 return ((c==' ') || (c=='\t') || (c=='\n') || (c=='\r'));
57 char first_non_whitespace_char(char const * s)
61 while ((pos<l)&&is_whitespace_char(s[pos]))
66 char last_non_whitespace_char(char const * s)
68 int pos = strlen(s)-1;
69 while ((pos>=0) && is_whitespace_char(s[pos]))
74 string strip_whitespace(string const & s)
78 for (int pos=0; pos<l; ++pos) {
79 if (!is_whitespace_char(s[pos])) s2 += s[pos];
84 G__value exec_tempfile(string const & command)
87 char *tmpfilename = tempnam(NULL,"ginac");
89 fout.open(tmpfilename);
90 fout << "{" << endl << command << endl << "}" << endl;
92 G__store_undo_position();
93 retval = G__exec_tempfile(tmpfilename);
94 G__security_recover(stdout);
100 char * process_permanentfile(string const & command)
102 char *tmpfilename = tempnam(NULL,"ginac");
103 cout << "creating file " << tmpfilename << endl;
105 fout.open(tmpfilename);
106 fout << command << endl;
108 G__store_undo_position();
109 G__loadfile(tmpfilename);
110 G__security_recover(stdout);
114 void process_tempfile(string const & command)
116 #ifdef OBSCURE_CINT_HACK
117 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
118 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
119 static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
120 static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
121 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
122 static bool basic_type_warning_already_displayed=false;
123 #endif // def OBSCURE_CINT_HACK
125 G__value retval = exec_tempfile(command);
127 #ifdef OBSCURE_CINT_HACK
129 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
131 static unsigned out_count = 0;
132 if (TYPES_EQUAL(retval,ref_ex)) {
133 string varname = "Out"+ToString(++out_count);
134 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
135 // an ex was returned, but this is not the ex which was created last
136 // => this is not a temporary ex, but one that resides safely in memory
138 // cout << "warning: using ex from retval (experimental)" << endl;
139 ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
140 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
141 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
143 //for (int i=4-int(log10(out_count)); i>0; --i)
145 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
147 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
149 exec_tempfile(string()+"LLLAST=LLAST;\n"
151 +"LAST="+varname+";\n"
152 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
153 } else if (TYPES_EQUAL(retval,ref_symbol)||
154 TYPES_EQUAL(retval,ref_function)||
155 TYPES_EQUAL(retval,ref_power)||
156 TYPES_EQUAL(retval,ref_numeric)) {
157 if (!basic_type_warning_already_displayed) {
158 cout << "WARNING: The return value of the last expression you entered was a symbol," << endl
159 << "function, power or numeric, which cannot be safely displayed." << endl
160 << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl
161 << "for example (assume 'x' is a symbol):" << endl
162 << PROMPT "ex(x);" << endl
163 << "OutX = x" << endl << endl
164 << PROMPT "cout << x << endl;" << endl
165 << "x" << endl << endl
166 << "This warning will not be shown again." << endl;
167 basic_type_warning_already_displayed=true;
170 #endif // def OBSCURE_CINT_HACK
175 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
176 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
177 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
178 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
179 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
180 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
181 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
188 char prompt[G__ONELINE];
194 signal(SIGTERM,sigterm_handler);
196 G__init_cint("cint"); /* initialize cint */
198 // no longer needed as of cint 5.14.31
199 // exec_tempfile("#include <string>\n");
201 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
204 bool next_command_is_function=false;
206 strcpy(prompt,PROMPT);
208 bool end_of_command = false;
210 while (!end_of_command) {
211 line = G__input(prompt);
214 bool double_quote = false;
215 bool single_quote = false;
216 while(line[pos]!='\0') {
219 if (!single_quote) double_quote = !double_quote;
222 if (!double_quote) single_quote = !single_quote;
225 if ((!single_quote) && (!double_quote)) open_braces++;
228 if ((!single_quote) && (!double_quote)) open_braces--;
235 if (open_braces==0) {
236 if ((first_non_whitespace_char(command.c_str())=='#')||
237 (first_non_whitespace_char(command.c_str())=='.')||
238 (last_non_whitespace_char(command.c_str())==';')||
239 (last_non_whitespace_char(command.c_str())=='}')) {
243 strcpy(prompt," > ");
245 string stripped_command=strip_whitespace(command);
246 if ((stripped_command=="quit;")||
247 (stripped_command=="exit;")||
248 (stripped_command=="bye;")||
249 (stripped_command==".q")||
250 (stripped_command==".quit")||
251 (stripped_command==".exit")||
252 (stripped_command==".bye")) {
254 } else if (stripped_command==".function") {
255 cout << "next expression can be a function definition" << endl;
256 next_command_is_function=true;
257 } else if (stripped_command==".cint") {
258 cout << endl << "switching to cint interactive mode" << endl;
259 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
261 cout << "back from cint" << endl;
262 } else if (command[0]=='.') {
263 cout << "special command (TBD): " << command << endl;
265 // cout << "now processing: " << command << endl;
266 if (next_command_is_function) {
267 next_command_is_function = false;
268 filenames.push_back(process_permanentfile(command));
270 process_tempfile(command);