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;
28 #endif // def OBSCURE_CINT_HACK
30 typedef list<char *> cplist;
36 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
37 cout << "removing file " << *it << endl;
43 void sigterm_handler(int n)
48 bool is_whitespace_char(char c)
50 return ((c==' ') || (c=='\t') || (c=='\n') || (c=='\r'));
53 char first_non_whitespace_char(char const * s)
57 while ((pos<l)&&is_whitespace_char(s[pos])) pos++;
61 char last_non_whitespace_char(char const * s)
63 int pos = strlen(s)-1;
64 while ((pos>=0) && is_whitespace_char(s[pos])) pos--;
68 string strip_whitespace(string const & s)
72 for (int pos=0; pos<l; ++pos) {
73 if (!is_whitespace_char(s[pos])) s2 += s[pos];
78 G__value exec_tempfile(string const & command)
81 char *tmpfilename = tempnam(NULL,"ginac");
83 fout.open(tmpfilename);
84 fout << "{" << endl << command << endl << "}" << endl;
86 G__store_undo_position();
87 retval=G__exec_tempfile(tmpfilename);
88 G__security_recover(stdout);
94 char * process_permanentfile(string const & command)
96 char *tmpfilename = tempnam(NULL,"ginac");
97 cout << "creating file " << tmpfilename << endl;
99 fout.open(tmpfilename);
100 fout << command << endl;
102 G__store_undo_position();
103 G__loadfile(tmpfilename);
104 G__security_recover(stdout);
108 void process_tempfile(string const & command)
110 #ifdef OBSCURE_CINT_HACK
111 static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
112 static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
113 static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
114 static G__value ref_power = exec_tempfile("power(ginac_cint_internal_symbol,ginac_cint_internal_symbol);");
115 #endif // def OBSCURE_CINT_HACK
117 G__value retval = exec_tempfile(command);
119 #ifdef OBSCURE_CINT_HACK
121 #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
123 static unsigned out_count = 0;
124 if (TYPES_EQUAL(retval,ref_ex)) {
125 if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
126 string varname = "Out"+ToString(++out_count);
128 //for (int i=4-int(log10(out_count)); i>0; --i)
130 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);\n"
133 +"LAST="+varname+";\n"
134 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
136 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
139 #endif // def OBSCURE_CINT_HACK
144 cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
145 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
146 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
147 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
148 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
149 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
150 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl << endl;
156 char prompt[G__ONELINE];
161 signal(SIGTERM,sigterm_handler);
163 G__init_cint("cint"); /* initialize cint */
165 exec_tempfile("#include <string>\n");
166 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
169 bool next_command_is_function=false;
171 strcpy(prompt,"GiNaC> ");
173 bool end_of_command=false;
175 while (!end_of_command) {
176 line = G__input(prompt);
179 bool double_quote=false;
180 bool single_quote=false;
181 while(line[pos]!='\0') {
184 if (!single_quote) double_quote = !double_quote;
187 if (!double_quote) single_quote = !single_quote;
190 if ((!single_quote) && (!double_quote)) open_braces++;
193 if ((!single_quote) && (!double_quote)) open_braces--;
200 if (open_braces==0) {
201 if ((first_non_whitespace_char(command.c_str())=='#')||
202 (first_non_whitespace_char(command.c_str())=='.')||
203 (last_non_whitespace_char(command.c_str())==';')||
204 (last_non_whitespace_char(command.c_str())=='}')) {
208 strcpy(prompt," > ");
210 string stripped_command=strip_whitespace(command);
211 if ((stripped_command=="quit;")||
212 (stripped_command=="exit;")||
213 (stripped_command=="bye;")||
214 (stripped_command==".q")||
215 (stripped_command==".quit")||
216 (stripped_command==".exit")||
217 (stripped_command==".bye")) {
219 } else if (stripped_command==".function") {
220 cout << "next expression can be a function definition" << endl;
221 next_command_is_function=true;
222 } else if (stripped_command==".cint") {
223 cout << endl << "switching to cint interactive mode" << endl;
224 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
226 cout << "back from cint" << endl;
227 } else if (command[0]=='.') {
228 cout << "special command (TBD): " << command << endl;
230 // cout << "now processing: " << command << endl;
231 if (next_command_is_function) {
232 next_command_is_function = false;
233 filenames.push_back(process_permanentfile(command));
235 process_tempfile(command);