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 GiNaC-cint V" << VERSION << endl;
141 cout << "This software is provided \"as is\" without any warranty. Copyright of Cint is" << endl
142 << "owned by Agilent Technologies Japan and Masaharu Goto. Registration is" << endl
143 << " __, _______ requested, at this moment, for commercial use. Send e-mail to" << endl
144 << " (__) * | <MXJ02154@niftyserve.or.jp>. The registration is free." << endl
145 << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ." << endl
146 << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl << endl;
147 cout << "To quit, type 'quit;', 'exit;', 'bye;', '.q', '.quit', '.exit' or '.bye'" << endl;
153 char prompt[G__ONELINE];
158 signal(SIGTERM,sigterm_handler);
160 G__init_cint("cint"); /* initialize cint */
162 exec_tempfile("#include <string>\n");
163 exec_tempfile("ex LAST,LLAST,LLLAST;\n");
166 bool next_command_is_function=false;
168 strcpy(prompt,"GiNaC> ");
170 bool end_of_command=false;
172 while (!end_of_command) {
173 line=G__input(prompt);
176 bool double_quote=false;
177 bool single_quote=false;
178 while(line[pos]!='\0') {
181 if (!single_quote) double_quote = !double_quote;
184 if (!double_quote) single_quote = !single_quote;
187 if ((!single_quote)&&(!double_quote)) open_braces++;
190 if ((!single_quote)&&(!double_quote)) open_braces--;
197 if (open_braces==0) {
198 if ((first_non_whitespace_char(command.c_str())=='#')||
199 (first_non_whitespace_char(command.c_str())=='.')||
200 (last_non_whitespace_char(command.c_str())==';')||
201 (last_non_whitespace_char(command.c_str())=='}')) {
205 strcpy(prompt," > ");
207 string stripped_command=strip_whitespace(command);
208 if ((stripped_command=="quit;")||
209 (stripped_command=="exit;")||
210 (stripped_command=="bye;")||
211 (stripped_command==".q")||
212 (stripped_command==".quit")||
213 (stripped_command==".exit")||
214 (stripped_command==".bye")) {
216 } else if (stripped_command==".function") {
217 cout << "next expression can be a function definition" << endl;
218 next_command_is_function=true;
219 } else if (stripped_command==".cint") {
220 cout << endl << "switching to cint interactive mode" << endl;
221 cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
223 cout << "back from cint" << endl;
224 } else if (command[0]=='.') {
225 cout << "special command (TBD): " << command << endl;
227 // cout << "now processing: " << command << endl;
228 if (next_command_is_function) {
229 next_command_is_function=false;
230 filenames.push_back(process_permanentfile(command));
232 process_tempfile(command);