]> www.ginac.de Git - ginac.git/blob - cint/ginaccint.bin.cpp
- fixed broken target clean in Makefiles.
[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]))
60         pos++;
61     return s[pos];
62 }    
63
64 char last_non_whitespace_char(char const * s)
65 {
66     int pos = strlen(s)-1;
67     while ((pos>=0) && is_whitespace_char(s[pos]))
68         pos--;
69     return s[pos];
70 }    
71
72 string strip_whitespace(string const & s)
73 {
74     string s2;
75     int l = s.length();
76     for (int pos=0; pos<l; ++pos) {
77         if (!is_whitespace_char(s[pos])) s2 += s[pos];
78     }
79     return s2;
80 }
81
82 G__value exec_tempfile(string const & command)
83 {
84     G__value retval;
85     char *tmpfilename = tempnam(NULL,"ginac");
86     ofstream fout;
87     fout.open(tmpfilename);
88     fout << "{" << endl << command << endl << "}" << endl;
89     fout.close();
90     G__store_undo_position();
91     retval = G__exec_tempfile(tmpfilename);
92     G__security_recover(stdout);
93     remove(tmpfilename);
94     free(tmpfilename);
95     return retval;
96 }
97
98 char * process_permanentfile(string const & command)
99 {
100     char *tmpfilename = tempnam(NULL,"ginac");
101     cout << "creating file " << tmpfilename << endl;
102     ofstream fout;
103     fout.open(tmpfilename);
104     fout << command << endl;
105     fout.close();
106     G__store_undo_position();
107     G__loadfile(tmpfilename);
108     G__security_recover(stdout);
109     return tmpfilename;
110 }
111
112 void process_tempfile(string const & command)
113 {
114 #ifdef OBSCURE_CINT_HACK
115     static G__value ref_symbol = exec_tempfile("symbol ginac_cint_internal_symbol; ginac_cint_internal_symbol;");
116     static G__value ref_ex = exec_tempfile("ex ginac_cint_internal_ex; ginac_cint_internal_ex;");
117     static G__value ref_function = exec_tempfile("sin(ginac_cint_internal_symbol);");
118     static G__value ref_power = exec_tempfile("power(ginac_cint_internal_symbol,ginac_cint_internal_symbol);");
119 #endif // def OBSCURE_CINT_HACK
120
121     G__value retval = exec_tempfile(command);
122
123 #ifdef OBSCURE_CINT_HACK
124
125     #define TYPES_EQUAL(A,B) (((A).type==(B).type) && ((A).tagnum==(B).tagnum))
126     
127     static unsigned out_count = 0;
128     if (TYPES_EQUAL(retval,ref_ex)) {
129         string varname = "Out"+ToString(++out_count);
130         if (retval.obj.i!=ex::last_created_or_assigned_exp) {
131             // an ex was returned, but this is not the ex which was created last
132             // => this is not a temporary ex, but one that resides safely in memory
133             
134             // cout << "warning: using ex from retval (experimental)" << endl;
135             ex::dummy_bp=((ex *)(void *)(retval.obj.i))->bp;
136             exec_tempfile("ex "+varname+"(*ex::dummy_bp);");
137         } else if (ex::last_created_or_assigned_bp_can_be_converted_to_ex()) {
138             //string varfill;
139             //for (int i=4-int(log10(out_count)); i>0; --i)
140             //    varfill += ' ';
141             exec_tempfile("ex "+varname+"(*ex::last_created_or_assigned_bp);");
142         } else {
143             cout << "warning: last_created_or_assigned_bp modified 0 or not evaluated or not dynallocated" << endl;
144         }
145         exec_tempfile(string()+"LLLAST=LLAST;\n"
146                       +"LLAST=LAST;\n"
147                       +"LAST="+varname+";\n"
148                       +"cout << \""+varname+" = \" << "+varname+" << endl << endl;");
149     }
150 #endif // def OBSCURE_CINT_HACK
151 }
152
153 void greeting(void)
154 {
155     cout << "Welcome to GiNaC-cint (" << PACKAGE << " V" << VERSION << ")" << endl;
156     cout << "This software is provided \"as is\" without any warranty.  Copyright of Cint is" << endl
157          << "owned by Agilent Technologies Japan and Masaharu Goto.  Registration is" << endl
158          << "  __,  _______  requested, at this moment, for commercial use.  Send e-mail to" << endl
159          << " (__) *       | <MXJ02154@niftyserve.or.jp>.  The registration is free." << endl
160          << "  ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl
161          << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl
162          << endl;
163 }
164
165 int main(void) 
166 {
167     char *line;
168     char prompt[G__ONELINE];
169
170     if (isatty(0))
171         greeting();
172
173     atexit(cleanup);
174     signal(SIGTERM,sigterm_handler);
175     
176     G__init_cint("cint");    /* initialize cint */
177
178     exec_tempfile("#include <string>\n");
179     exec_tempfile("ex LAST,LLAST,LLLAST;\n");
180     
181     bool quit = false;
182     bool next_command_is_function=false;    
183     while (!quit) {
184         strcpy(prompt,"GiNaC> ");
185         int open_braces = 0;
186         bool end_of_command = false;
187         string command;
188         while (!end_of_command) {
189             line = G__input(prompt);
190             
191             int pos = 0;
192             bool double_quote = false;
193             bool single_quote = false;
194             while(line[pos]!='\0') {
195                 switch(line[pos]) {
196                 case '"':
197                     if (!single_quote) double_quote = !double_quote;
198                     break;
199                 case '\'':
200                     if (!double_quote) single_quote = !single_quote;
201                     break;
202                 case '{':
203                     if ((!single_quote) && (!double_quote)) open_braces++;
204                     break;
205                 case '}':
206                     if ((!single_quote) && (!double_quote)) open_braces--;
207                     break;
208                 }
209                 pos++;
210             }
211             command += line;
212             command += '\n';
213             if (open_braces==0) {
214                 if ((first_non_whitespace_char(command.c_str())=='#')||
215                     (first_non_whitespace_char(command.c_str())=='.')||
216                     (last_non_whitespace_char(command.c_str())==';')||
217                     (last_non_whitespace_char(command.c_str())=='}')) {
218                     end_of_command=true;
219                 }
220             }
221             strcpy(prompt,"     > ");
222         }
223         string stripped_command=strip_whitespace(command);
224         if ((stripped_command=="quit;")||
225             (stripped_command=="exit;")||
226             (stripped_command=="bye;")||
227             (stripped_command==".q")||
228             (stripped_command==".quit")||
229             (stripped_command==".exit")||
230             (stripped_command==".bye")) {
231             quit = true;
232         } else if (stripped_command==".function") {
233             cout << "next expression can be a function definition" << endl;
234             next_command_is_function=true;
235         } else if (stripped_command==".cint") {
236             cout << endl << "switching to cint interactive mode" << endl;
237             cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl;
238             G__pause();
239             cout << "back from cint" << endl;
240         } else if (command[0]=='.') {
241             cout << "special command (TBD): " << command << endl;
242         } else {
243             // cout << "now processing: " << command << endl;
244             if (next_command_is_function) {
245                 next_command_is_function = false;
246                 filenames.push_back(process_permanentfile(command));
247             } else {
248                 process_tempfile(command);
249             }
250         }
251     }
252
253     return 0;
254 }
255
256
257
258
259
260
261