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])) pos++;
63 char last_non_whitespace_char(char const * s)
65 int pos = strlen(s)-1;
66 while ((pos>=0) && is_whitespace_char(s[pos])) pos--;
70 string strip_whitespace(string const & s)
74 for (int pos=0; pos<l; ++pos) {
75 if (!is_whitespace_char(s[pos])) s2 += s[pos];
80 G__value exec_tempfile(string const & command)
83 char *tmpfilename = tempnam(NULL,"ginac");
85 fout.open(tmpfilename);
86 fout << "{" << endl << command << endl << "}" << endl;
88 G__store_undo_position();
89 retval=G__exec_tempfile(tmpfilename);
90 G__security_recover(stdout);
96 char * process_permanentfile(string const & command)
98 char *tmpfilename = tempnam(NULL,"ginac");
99 cout << "creating file " << tmpfilename << endl;
101 fout.open(tmpfilename);
102 fout << command << endl;
104 G__store_undo_position();
105 G__loadfile(tmpfilename);
106 G__security_recover(stdout);
110 void process_tempfile(string const & command)
112 #ifdef OBSCURE_CINT_HACK
113 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
114 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
115 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
116 static G__value ref_power = exec_tempfile("power(ginac_cint_internal_symbol,ginac_cint_internal_symbol);");
117 #endif // def OBSCURE_CINT_HACK
119 G__value retval = exec_tempfile(command);
121 #ifdef OBSCURE_CINT_HACK
123 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
125 static unsigned out_count = 0;
126 if (TYPES_EQUAL(retval,ref_ex)) {
127 string varname = "Out"+ToString(++out_count);
128 if (retval.obj.i!=ex::last_created_or_assigned_exp) {
129 // an ex was returned, but this is not the ex which was created last
130 // => this is not a temporary ex, but one that resides safely in memory
132 // cout << "warning: using ex from retval (experimental)" << endl;
133 ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
134 exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
135 } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
137 //for (int i=4-int(log10(out_count)); i>0; --i)
139 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
141 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
143 exec_tempfile(string()+"LLLAST=LLAST;\n"
145 +"LAST="+varname+";\n"
146 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
148 #endif // def OBSCURE_CINT_HACK
153 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
154 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
155 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
156 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
157 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
158 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
159 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl << endl;
165 char prompt[G__ONELINE];
170 signal(SIGTERM,sigterm_handler);
172 G__init_cint("cint"); /* initialize cint */
174 exec_tempfile("#include <string>\n");
175 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
178 bool next_command_is_function=false;
180 strcpy(prompt,"GiNaC> ");
182 bool end_of_command=false;
184 while (!end_of_command) {
185 line = G__input(prompt);
188 bool double_quote=false;
189 bool single_quote=false;
190 while(line[pos]!='\0') {
193 if (!single_quote) double_quote = !double_quote;
196 if (!double_quote) single_quote = !single_quote;
199 if ((!single_quote) && (!double_quote)) open_braces++;
202 if ((!single_quote) && (!double_quote)) open_braces--;
209 if (open_braces==0) {
210 if ((first_non_whitespace_char(command.c_str())=='#')||
211 (first_non_whitespace_char(command.c_str())=='.')||
212 (last_non_whitespace_char(command.c_str())==';')||
213 (last_non_whitespace_char(command.c_str())=='}')) {
217 strcpy(prompt," > ");
219 string stripped_command=strip_whitespace(command);
220 if ((stripped_command=="quit;")||
221 (stripped_command=="exit;")||
222 (stripped_command=="bye;")||
223 (stripped_command==".q")||
224 (stripped_command==".quit")||
225 (stripped_command==".exit")||
226 (stripped_command==".bye")) {
228 } else if (stripped_command==".function") {
229 cout << "next expression can be a function definition" << endl;
230 next_command_is_function=true;
231 } else if (stripped_command==".cint") {
232 cout << endl << "switching to cint interactive mode" << endl;
233 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
235 cout << "back from cint" << endl;
236 } else if (command[0]=='.') {
237 cout << "special command (TBD): " << command << endl;
239 // cout << "now processing: " << command << endl;
240 if (next_command_is_function) {
241 next_command_is_function = false;
242 filenames.push_back(process_permanentfile(command));
244 process_tempfile(command);