]> www.ginac.de Git - ginac.git/blob - cint/ginaccint.bin.cpp
109583378447345e9247b41d69225df0152218b8
[ginac.git] / cint / ginaccint.bin.cpp
1 #include "G__ci.h"   /* G__atpause is defined in G__ci.h */
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "ginac/ginac.h"
8 #include "config.h"
9 #include <list>
10
11 extern "C" G__value G__exec_tempfile G__P((char *file));
12 extern "C" void G__store_undo_position(void);
13
14 #ifdef OBSCURE_CINT_HACK
15
16 #include <strstream>
17
18 template<class T>
19 string ToString(const T & t)
20 {
21     char buf[256];
22     ostrstream(buf,sizeof(buf)) << t << ends;
23     return buf;
24 }
25
26 basic * ex::last_created_or_assigned_bp=0;
27 basic * ex::dummy_bp=0;
28 long ex::last_created_or_assigned_exp=0;
29
30 #endif // def OBSCURE_CINT_HACK
31
32 typedef list<char *> cplist;
33
34 cplist filenames;
35
36 void cleanup(void)
37 {
38     for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) {
39         cout << "removing file " << *it << endl;
40         remove(*it);
41         free(*it);
42     }
43 }
44
45 void sigterm_handler(int n)
46 {
47     exit(1);
48 }
49
50 bool is_whitespace_char(char c)
51 {
52     return ((c==' ') || (c=='\t') || (c=='\n') || (c=='\r')); 
53 }
54
55 char first_non_whitespace_char(char const * s)
56 {
57     int l = strlen(s);
58     int pos = 0;
59     while ((pos<l)&&is_whitespace_char(s[pos])) pos++;
60     return s[pos];
61 }    
62
63 char last_non_whitespace_char(char const * s)
64 {
65     int pos = strlen(s)-1;
66     while ((pos>=0) && is_whitespace_char(s[pos])) pos--;
67     return s[pos];
68 }    
69
70 string strip_whitespace(string const & s)
71 {
72     string s2;
73     int l = s.length();
74     for (int pos=0; pos<l; ++pos) {
75         if (!is_whitespace_char(s[pos])) s2 += s[pos];
76     }
77     return s2;
78 }
79
80 G__value exec_tempfile(string const & command)
81 {
82     G__value retval;
83     char *tmpfilename = tempnam(NULL,"ginac");
84     ofstream fout;
85     fout.open(tmpfilename);
86     fout << "{" << endl << command << endl << "}" << endl;
87     fout.close();
88     G__store_undo_position();
89     retval=G__exec_tempfile(tmpfilename);
90     G__security_recover(stdout);
91     remove(tmpfilename);
92     free(tmpfilename);
93     return retval;
94 }
95
96 char * process_permanentfile(string const & command)
97 {
98     char *tmpfilename = tempnam(NULL,"ginac");
99     cout << "creating file " << tmpfilename << endl;
100     ofstream fout;
101     fout.open(tmpfilename);
102     fout << command << endl;
103     fout.close();
104     G__store_undo_position();
105     G__loadfile(tmpfilename);
106     G__security_recover(stdout);
107     return tmpfilename;
108 }
109
110 void process_tempfile(string const & command)
111 {
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
118
119     G__value retval = exec_tempfile(command);
120
121 #ifdef OBSCURE_CINT_HACK
122
123     #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
124     
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
131             
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()) {
136             //string varfill;
137             //for (int i=4-int(log10(out_count)); i>0; --i)
138             //    varfill += ' ';
139             exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
140         } else {
141             cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
142         }
143         exec_tempfile(string()+"LLLAST=LLAST;\n"
144                       +"LLAST=LAST;\n"
145                       +"LAST="+varname+";\n"
146                       +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
147     }
148 #endif // def OBSCURE_CINT_HACK
149 }
150
151 void greeting(void)
152 {
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
160          << endl;
161 }
162
163 int main(void) 
164 {
165     char *line;
166     char prompt[G__ONELINE];
167
168     greeting();
169
170     atexit(cleanup);
171     signal(SIGTERM,sigterm_handler);
172     
173     G__init_cint("cint");    /* initialize cint */
174
175     exec_tempfile("#include <string>\n");
176     exec_tempfile("ex LAST,LLAST,LLLAST;\n");
177     
178     bool quit = false;
179     bool next_command_is_function=false;    
180     while (!quit) {
181         strcpy(prompt,"GiNaC> ");
182         int open_braces = 0;
183         bool end_of_command=false;
184         string command;
185         while (!end_of_command) {
186             line = G__input(prompt);
187             
188             int pos = 0;
189             bool double_quote=false;
190             bool single_quote=false;
191             while(line[pos]!='\0') {
192                 switch(line[pos]) {
193                 case '"':
194                     if (!single_quote) double_quote = !double_quote;
195                     break;
196                 case '\'':
197                     if (!double_quote) single_quote = !single_quote;
198                     break;
199                 case '{':
200                     if ((!single_quote) && (!double_quote)) open_braces++;
201                     break;
202                 case '}':
203                     if ((!single_quote) && (!double_quote)) open_braces--;
204                     break;
205                 }
206                 pos++;
207             }
208             command += line;
209             command += '\n';
210             if (open_braces==0) {
211                 if ((first_non_whitespace_char(command.c_str())=='#')||
212                     (first_non_whitespace_char(command.c_str())=='.')||
213                     (last_non_whitespace_char(command.c_str())==';')||
214                     (last_non_whitespace_char(command.c_str())=='}')) {
215                     end_of_command=true;
216                 }
217             }
218             strcpy(prompt,"     > ");
219         }
220         string stripped_command=strip_whitespace(command);
221         if ((stripped_command=="quit;")||
222             (stripped_command=="exit;")||
223             (stripped_command=="bye;")||
224             (stripped_command==".q")||
225             (stripped_command==".quit")||
226             (stripped_command==".exit")||
227             (stripped_command==".bye")) {
228             quit = true;
229         } else if (stripped_command==".function") {
230             cout << "next expression can be a function definition" << endl;
231             next_command_is_function=true;
232         } else if (stripped_command==".cint") {
233             cout << endl << "switching to cint interactive mode" << endl;
234             cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
235             G__pause();
236             cout << "back from cint" << endl;
237         } else if (command[0]=='.') {
238             cout << "special command (TBD): " << command << endl;
239         } else {
240             // cout << "now processing: " << command << endl;
241             if (next_command_is_function) {
242                 next_command_is_function = false;
243                 filenames.push_back(process_permanentfile(command));
244             } else {
245                 process_tempfile(command);
246             }
247         }
248     }
249
250     return 0;
251 }
252
253
254
255
256
257
258