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_constant = exec_tempfile("constant ginac_cint_internal_constant; ginac_cint_internal_constant;");
119 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
120 static G__value ref_power = exec_tempfile("power(ex(ginac_cint_internal_symbol),ex(ginac_cint_internal_symbol));");
121 static G__value ref_numeric = exec_tempfile("numeric ginac_cint_internal_numeric; ginac_cint_internal_numeric;");
122 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
123 static bool basic_type_warning_already_displayed=false;
124 #endif // def OBSCURE_CINT_HACK
126 G__value retval = exec_tempfile(command);
128 #ifdef OBSCURE_CINT_HACK
130 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
132 static unsigned out_count = 0;
133 if (TYPES_EQUAL(retval,ref_ex)) {
134 string varname = "Out"+ToString(++out_count);
135 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
136 // an ex was returned, but this is not the ex which was created last
137 // => this is not a temporary ex, but one that resides safely in memory
139 // cout << "warning: using ex from retval (experimental)" << endl;
140 ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
141 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
142 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
144 //for (int i=4-int(log10(out_count)); i>0; --i)
146 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
148 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
150 exec_tempfile(string()+"LLLAST=LLAST;\n"
152 +"LAST="+varname+";\n"
153 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
154 } else if (TYPES_EQUAL(retval,ref_symbol)||
155 TYPES_EQUAL(retval,ref_constant)||
156 TYPES_EQUAL(retval,ref_function)||
157 TYPES_EQUAL(retval,ref_power)||
158 TYPES_EQUAL(retval,ref_numeric)) {
159 if (!basic_type_warning_already_displayed) {
161 <<"WARNING: The return value of the last expression you entered was a symbol," << endl
162 << "constant, function, power or numeric, which cannot be safely displayed." << endl
163 << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl
164 << "for example (assume 'x' is a symbol):" << endl
165 << PROMPT "ex(x);" << endl
166 << "OutX = x" << endl << endl
167 << PROMPT "cout << x << endl;" << endl
168 << "x" << endl << endl
169 << "This warning will not be shown again." << endl;
170 basic_type_warning_already_displayed=true;
173 #endif // def OBSCURE_CINT_HACK
178 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
179 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
180 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
181 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
182 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
183 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
184 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
191 char prompt[G__ONELINE];
197 signal(SIGTERM,sigterm_handler);
199 G__init_cint("cint"); /* initialize cint */
201 // no longer needed as of cint 5.14.31
202 // exec_tempfile("#include <string>\n");
204 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
207 bool next_command_is_function=false;
209 strcpy(prompt,PROMPT);
211 bool end_of_command = false;
213 while (!end_of_command) {
214 line = G__input(prompt);
217 bool double_quote = false;
218 bool single_quote = false;
219 while(line[pos]!='\0') {
222 if (!single_quote) double_quote = !double_quote;
225 if (!double_quote) single_quote = !single_quote;
228 if ((!single_quote) && (!double_quote)) open_braces++;
231 if ((!single_quote) && (!double_quote)) open_braces--;
238 if (open_braces==0) {
239 if ((first_non_whitespace_char(command.c_str())=='#')||
240 (first_non_whitespace_char(command.c_str())=='.')||
241 (last_non_whitespace_char(command.c_str())==';')||
242 (last_non_whitespace_char(command.c_str())=='}')) {
246 strcpy(prompt," > ");
248 string stripped_command=strip_whitespace(command);
249 if ((stripped_command=="quit;")||
250 (stripped_command=="exit;")||
251 (stripped_command=="bye;")||
252 (stripped_command==".q")||
253 (stripped_command==".quit")||
254 (stripped_command==".exit")||
255 (stripped_command==".bye")) {
257 } else if (stripped_command==".function") {
258 cout << "next expression can be a function definition" << endl;
259 next_command_is_function=true;
260 } else if (stripped_command==".cint") {
261 cout << endl << "switching to cint interactive mode" << endl;
262 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
264 cout << "back from cint" << endl;
265 } else if (command[0]=='.') {
266 cout << "special command (TBD): " << command << endl;
268 // cout << "now processing: " << command << endl;
269 if (next_command_is_function) {
270 next_command_is_function = false;
271 filenames.push_back(process_permanentfile(command));
273 process_tempfile(command);