]> www.ginac.de Git - ginac.git/blob - cint/ginaccint.bin.cpp
4c134b4e7a81dcfc7eb28f48ffe96f78e7b1313f
[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 << endl;
160 }
161
162 int main(void) 
163 {
164     char *line;
165     char prompt[G__ONELINE];
166
167     greeting();
168
169     atexit(cleanup);
170     signal(SIGTERM,sigterm_handler);
171     
172     G__init_cint("cint");    /* initialize cint */
173
174     exec_tempfile("#include <string>\n");
175     exec_tempfile("ex LAST,LLAST,LLLAST;\n");
176     
177     bool quit = false;
178     bool next_command_is_function=false;    
179     while (!quit) {
180         strcpy(prompt,"GiNaC> ");
181         int open_braces = 0;
182         bool end_of_command=false;
183         string command;
184         while (!end_of_command) {
185             line = G__input(prompt);
186         
187             int pos = 0;
188             bool double_quote=false;
189             bool single_quote=false;
190             while(line[pos]!='\0') {
191                 switch(line[pos]) {
192                 case '"':
193                     if (!single_quote) double_quote = !double_quote;
194                     break;
195                 case '\'':
196                     if (!double_quote) single_quote = !single_quote;
197                     break;
198                 case '{':
199                     if ((!single_quote) && (!double_quote)) open_braces++;
200                     break;
201                 case '}':
202                     if ((!single_quote) && (!double_quote)) open_braces--;
203                     break;
204                 }
205                 pos++;
206             }
207             command += line;
208             command += '\n';
209             if (open_braces==0) {
210                 if ((first_non_whitespace_char(command.c_str())=='#')||
211                     (first_non_whitespace_char(command.c_str())=='.')||
212                     (last_non_whitespace_char(command.c_str())==';')||
213                     (last_non_whitespace_char(command.c_str())=='}')) {
214                     end_of_command=true;
215                 }
216             }
217             strcpy(prompt,"     > ");
218         }
219         string stripped_command=strip_whitespace(command);
220         if ((stripped_command=="quit;")||
221             (stripped_command=="exit;")||
222             (stripped_command=="bye;")||
223             (stripped_command==".q")||
224             (stripped_command==".quit")||
225             (stripped_command==".exit")||
226             (stripped_command==".bye")) {
227             quit = true;
228         } else if (stripped_command==".function") {
229             cout << "next expression can be a function definition" << endl;
230             next_command_is_function=true;
231         } else if (stripped_command==".cint") {
232             cout << endl << "switching to cint interactive mode" << endl;
233             cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
234             G__pause();
235             cout << "back from cint" << endl;
236         } else if (command[0]=='.') {
237             cout << "special command (TBD): " << command << endl;
238         } else {
239             // cout << "now processing: " << command << endl;
240             if (next_command_is_function) {
241                 next_command_is_function = false;
242                 filenames.push_back(process_permanentfile(command));
243             } else {
244                 process_tempfile(command);
245             }
246         }
247     }
248
249     return 0;
250 }
251
252
253
254
255
256
257