1 #include "G__ci.h" /* G__atpause is defined in G__ci.h */
7 #include <ginac/ginac.h>
10 extern "C" G__value G__exec_tempfile G__P((char *file));
11 extern "C" void G__store_undo_position(void);
13 #ifdef OBSCURE_CINT_HACK
18 string ToString(T const & t)
21 ostrstream(buf,sizeof(buf)) << t << ends;
25 basic * ex::last_created_or_assigned_bp=0;
27 #endif // def OBSCURE_CINT_HACK
29 typedef list<char *> cplist;
35 for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
36 cout << "removing file " << *it << endl;
42 void sigterm_handler(int n)
47 bool is_whitespace_char(char c)
49 return ((c==' ')||(c=='\t')||(c=='\n')||(c=='\r'));
52 char first_non_whitespace_char(char const * s)
56 while ((pos<l)&&is_whitespace_char(s[pos])) pos++;
60 char last_non_whitespace_char(char const * s)
63 while ((pos>=0)&&is_whitespace_char(s[pos])) pos--;
67 string strip_whitespace(string const & s)
71 for (int pos=0; pos<l; ++pos) {
72 if (!is_whitespace_char(s[pos])) s2 += s[pos];
77 G__value exec_tempfile(string const & command)
80 char *tmpfilename=tempnam(NULL,"ginac");
82 fout.open(tmpfilename);
83 fout << "{" << endl << command << endl << "}" << endl;
85 G__store_undo_position();
86 retval=G__exec_tempfile(tmpfilename);
87 G__security_recover(stdout);
93 char * process_permanentfile(string const & command)
95 char *tmpfilename=tempnam(NULL,"ginac");
96 cout << "creating file " << tmpfilename << endl;
98 fout.open(tmpfilename);
99 fout << command << endl;
101 G__store_undo_position();
102 G__loadfile(tmpfilename);
103 G__security_recover(stdout);
107 void process_tempfile(string const & command)
109 #ifdef OBSCURE_CINT_HACK
110 static G__value ref_symbol=exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
111 static G__value ref_ex=exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
112 static G__value ref_function=exec_tempfile("sin(ginac_cint_internal_symbol);");
113 static G__value ref_power=exec_tempfile("power(ginac_cint_internal_symbol,ginac_cint_internal_symbol);");
114 #endif // def OBSCURE_CINT_HACK
116 G__value retval=exec_tempfile(command);
118 #ifdef OBSCURE_CINT_HACK
120 #define TYPES_EQUAL(A,B) (((A).type==(B).type)&&((A).tagnum==(B).tagnum))
122 static unsigned out_count=0;
123 if (TYPES_EQUAL(retval,ref_ex)) {
124 if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
125 string varname="Out"+ToString(++out_count);
126 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);\n"
129 +"LAST="+varname+";\n"
130 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
132 cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
135 #endif // def OBSCURE_CINT_HACK
140 cout << "Welcome to GiNaCcint V0.2" << endl << endl;
141 cout << "To quit, type 'quit;', 'exit;', 'bye;', '.q', '.quit', '.exit' or '.bye'" << endl;
147 char prompt[G__ONELINE];
152 signal(SIGTERM,sigterm_handler);
154 G__init_cint("cint"); /* initialize cint */
156 exec_tempfile("#include <string>\n");
157 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
160 bool next_command_is_function=false;
162 strcpy(prompt,"GiNaCcint> ");
164 bool end_of_command=false;
166 while (!end_of_command) {
167 line=G__input(prompt);
170 bool double_quote=false;
171 bool single_quote=false;
172 while(line[pos]!='\0') {
175 if (!single_quote) double_quote = !double_quote;
178 if (!double_quote) single_quote = !single_quote;
181 if ((!single_quote)&&(!double_quote)) open_braces++;
184 if ((!single_quote)&&(!double_quote)) open_braces--;
191 if (open_braces==0) {
192 if ((first_non_whitespace_char(command.c_str())=='#')||
193 (first_non_whitespace_char(command.c_str())=='.')||
194 (last_non_whitespace_char(command.c_str())==';')||
195 (last_non_whitespace_char(command.c_str())=='}')) {
199 strcpy(prompt," (more) > ");
201 string stripped_command=strip_whitespace(command);
202 if ((stripped_command=="quit;")||
203 (stripped_command=="exit;")||
204 (stripped_command=="bye;")||
205 (stripped_command==".q")||
206 (stripped_command==".quit")||
207 (stripped_command==".exit")||
208 (stripped_command==".bye")) {
210 } else if (stripped_command==".function") {
211 cout << "next expression can be a function definition" << endl;
212 next_command_is_function=true;
213 } else if (stripped_command==".cint") {
214 cout << endl << "switching to cint interactive mode" << endl;
215 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
217 cout << "back from cint" << endl;
218 } else if (command[0]=='.') {
219 cout << "special command (TBD): " << command << endl;
221 // cout << "now processing: " << command << endl;
222 if (next_command_is_function) {
223 next_command_is_function=false;
224 filenames.push_back(process_permanentfile(command));
226 process_tempfile(command);