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;
26 bool ex::last_created_or_assigned_bp_modified=false;
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)
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_ex=exec_tempfile("ex ginac_cint_internal; ginac_cint_internal;");
113 ex::last_created_or_assigned_bp_modified=false;
114 #endif // def OBSCURE_CINT_HACK
116 G__value retval=exec_tempfile(command);
118 #ifdef OBSCURE_CINT_HACK
119 static unsigned out_count=0;
120 if (!ex::last_created_or_assigned_bp_modified) {
121 if ((ref_ex.type==retval.type)&&(ref_ex.tagnum==retval.tagnum)) {
122 ex * ep=(ex *)(retval.obj.i);
126 if (ex::last_created_or_assigned_bp_modified) {
127 if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
128 string varname="Out"+ToString(++out_count);
129 exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);\n"
132 +"LAST="+varname+";\n"
133 +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
135 cout << "warning: last_created_or_assigned_bp modified but 0 or not evaluated or not dynallocated" << endl;
138 #endif // def OBSCURE_CINT_HACK
143 cout << "Welcome to GiNaCcint V0.2" << endl << endl;
144 cout << "To quit, type 'quit;', 'exit;', 'bye;', '.q', '.quit', '.exit' or '.bye'" << endl;
150 char prompt[G__ONELINE];
155 signal(SIGTERM,sigterm_handler);
157 G__init_cint("cint"); /* initialize cint */
159 exec_tempfile("#include <string>\n");
160 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
163 bool next_command_is_function=false;
165 strcpy(prompt,"GiNaCcint> ");
167 bool end_of_command=false;
169 while (!end_of_command) {
170 line=G__input(prompt);
173 bool double_quote=false;
174 bool single_quote=false;
175 while(line[pos]!='\0') {
178 if (!single_quote) double_quote = !double_quote;
181 if (!double_quote) single_quote = !single_quote;
184 if ((!single_quote)&&(!double_quote)) open_braces++;
187 if ((!single_quote)&&(!double_quote)) open_braces--;
194 if (open_braces==0) {
195 if ((first_non_whitespace_char(command.c_str())=='#')||
196 (first_non_whitespace_char(command.c_str())=='.')||
197 (last_non_whitespace_char(command.c_str())==';')||
198 (last_non_whitespace_char(command.c_str())=='}')) {
202 strcpy(prompt," (more) > ");
204 string stripped_command=strip_whitespace(command);
205 if ((stripped_command=="quit;")||
206 (stripped_command=="exit;")||
207 (stripped_command=="bye;")||
208 (stripped_command==".q")||
209 (stripped_command==".quit")||
210 (stripped_command==".exit")||
211 (stripped_command==".bye")) {
213 } else if (stripped_command==".function") {
214 cout << "next expression can be a function definition" << endl;
215 next_command_is_function=true;
216 } else if (stripped_command==".cint") {
217 cout << endl << "switching to cint interactive mode" << endl;
218 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
220 cout << "back from cint" << endl;
221 } else if (command[0]=='.') {
222 cout << "special command (TBD): " << command << endl;
224 // cout << "now processing: " << command << endl;
225 if (next_command_is_function) {
226 next_command_is_function=false;
227 filenames.push_back(process_permanentfile(command));
229 process_tempfile(command);