From: Alexander Frink Date: Wed, 10 May 2000 23:03:35 +0000 (+0000) Subject: enhanced ginaccint.bin.cpp (help, read from file, save commands) X-Git-Tag: release_0-6-0~7 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=a1b23792e63360bbe82337e2d11d9bd07ffcbb53 enhanced ginaccint.bin.cpp (help, read from file, save commands) removed necessity for dummies.h for cint >= 5.14.37 some modifications for future cint-versions which will support namespaces (hopefully) --- diff --git a/cint/dummies.h b/cint/dummies.h index e594a0b4..6eb17652 100644 --- a/cint/dummies.h +++ b/cint/dummies.h @@ -5,6 +5,9 @@ * Please do not modify it directly, edit the perl script instead! */ +#if (!defined(G_CINTVERSION)) || (G_CINTVERSION < 501437) +// this dummy file seems to be no longer needed for cint >= 5.14.37 + // fixes for function sin(x) inline function sin(symbol const & x) { return sin(ex(x)); } inline function sin(function const & x) { return sin(ex(x)); } @@ -833,3 +836,5 @@ inline simp_lor lor_g(basic const & x, basic const & y) { inline simp_lor lor_vec(const string & x, const basic & y) { return lor_vec((x),ex(y)); } + +#endif // (!defined(G_CINTVERSION)) || (G_CINTVERSION < 501437) diff --git a/cint/dummies.pl b/cint/dummies.pl index 890bafe8..0bb46962 100644 --- a/cint/dummies.pl +++ b/cint/dummies.pl @@ -44,6 +44,9 @@ $opening=<= 5.14.37 + END_OF_OPENING print OUT $opening; @@ -201,6 +204,8 @@ inline_function_1p('bool','is_zero'); inline_function_2p('simp_lor','lor_g'); inline_single_function_2p('simp_lor','lor_vec','const string &','','const basic &','ex'); +print OUT "\n#endif // (!defined(G_CINTVERSION)) || (G_CINTVERSION < 501437)\n"; + close OUT; # Create a file containing stubs that may be necessary because Cint always diff --git a/cint/ginaccint.bin.cpp b/cint/ginaccint.bin.cpp index 9c4cd8d3..083ad5d3 100644 --- a/cint/ginaccint.bin.cpp +++ b/cint/ginaccint.bin.cpp @@ -8,10 +8,15 @@ #include "config.h" #include +#ifndef NO_NAMESPACE_GINAC +using namespace GiNaC; +#endif // ndef NO_NAMESPACE_GINAC + extern "C" G__value G__exec_tempfile G__P((char *file)); extern "C" void G__store_undo_position(void); -#define PROMPT "GiNaC> " +#define PROMPT1 "GiNaC> " +#define PROMPT2 " > " #ifdef OBSCURE_CINT_HACK @@ -31,24 +36,23 @@ long ex::last_created_or_assigned_exp=0; #endif // def OBSCURE_CINT_HACK -typedef list cplist; +G__value exec_tempfile(string const & command); +char * process_permanentfile(string const & command); +void process_tempfile(string const & command); +void greeting(void); +void helpmessage(void); +string preprocess(char const * const line, bool & comment, bool & single_quote, + bool & double_quote, unsigned & open_braces); +void cleanup(void); +void sigterm_handler(int n); +void initialize(void); +bool readlines(istream * is, string & allcommands); +bool readfile(string const & filename, string & allcommands); +void savefile(string const & filename, string const & allcommands); +typedef list cplist; cplist filenames; -void cleanup(void) -{ - for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) { - cout << "removing file " << *it << endl; - remove(*it); - free(*it); - } -} - -void sigterm_handler(int n) -{ - exit(1); -} - G__value exec_tempfile(string const & command) { G__value retval; @@ -130,9 +134,9 @@ void process_tempfile(string const & command) << "constant, function, power or numeric, which cannot be safely displayed." << endl << "To force the output, cast it explicitly to type 'ex' or use 'cout'," << endl << "for example (assume 'x' is a symbol):" << endl - << PROMPT "ex(x);" << endl + << PROMPT1 "ex(x);" << endl << "OutX = x" << endl << endl - << PROMPT "cout << x << endl;" << endl + << PROMPT1 "cout << x << endl;" << endl << "x" << endl << endl << "This warning will not be shown again." << endl; basic_type_warning_already_displayed=true; @@ -150,10 +154,27 @@ void greeting(void) << " (__) * | . The registration is free." << endl << " ._) i N a C | The GiNaC framework is Copyright by Johannes Gutenberg Univ.," << endl << "<-------------' Germany and licensed under the terms and conditions of the GPL." << endl + << "Type .help for help." << endl << endl; } -string preprocess(char const * line, bool & comment, bool & single_quote, +void helpmessage(void) +{ + cout << "GiNaC-cint recognizes some special commands which start with a dot:" << endl << endl + << " .q, .quit, .exit, .bye quit GiNaC-cint" << endl + << " .help the text you are currently reading" << endl + << " .function define the body of a function (necessary due to a" << endl + << " cint limitation)" << endl + << " .cint switch to cint interactive mode (see cint" << endl + << " documentation for further details)" << endl + << " .read filename read a file from disk and execute it in GiNaC-cint" << endl + << " (recursive call is possible)" << endl + << " .save filename save the commands you have entered so far in a file" << endl << endl + << "Additionally you can exit GiNaC-cint with quit; exit; or bye;" << endl + << endl; +} + +string preprocess(char const * const line, bool & comment, bool & single_quote, bool & double_quote, unsigned & open_braces) { // "preprocess" the line entered to be able to decide if the command shall be @@ -189,6 +210,7 @@ string preprocess(char const * line, bool & comment, bool & single_quote, comment=true; } else { preprocessed += '/'; + preprocessed += line[pos]; } } else if (asterisk) { // last character was a *, test if / @@ -253,12 +275,23 @@ string preprocess(char const * line, bool & comment, bool & single_quote, return preprocessed; } - -int main(void) + +void cleanup(void) { - char *line; - char prompt[G__ONELINE]; + for (cplist::iterator it=filenames.begin(); it!=filenames.end(); ++it) { + cout << "removing file " << *it << endl; + remove(*it); + free(*it); + } +} + +void sigterm_handler(int n) +{ + exit(1); +} +void initialize(void) +{ if (isatty(0)) greeting(); @@ -270,22 +303,39 @@ int main(void) // no longer needed as of cint 5.14.31 // exec_tempfile("#include \n"); +#ifndef NO_NAMESPACE_GINAC + exec_tempfile("using namespace GiNaC;"); +#endif // ndef NO_NAMESPACE_GINAC + exec_tempfile("ex LAST,LLAST,LLLAST;\n"); +} + +bool readlines(istream * is, string & allcommands) +{ + char const * line; + char prompt[G__ONELINE]; + string linebuffer; bool quit = false; + bool eof = false; bool next_command_is_function=false; bool single_quote=false; bool double_quote=false; bool comment=false; unsigned open_braces=0; - while (!quit) { - strcpy(prompt,PROMPT); + while ((!quit)&&(!eof)) { + strcpy(prompt,PROMPT1); bool end_of_command = false; string command; string preprocessed; while (!end_of_command) { - line = G__input(prompt); + if (is==NULL) { + line = G__input(prompt); + } else { + getline(*is,linebuffer); + line=linebuffer.c_str(); + } command += line; command += "\n"; preprocessed += preprocess(line,comment,single_quote,double_quote,open_braces); @@ -299,7 +349,7 @@ int main(void) end_of_command=true; } } - strcpy(prompt," > "); + strcpy(prompt,PROMPT2); } if ((preprocessed=="quit;")|| (preprocessed=="exit;")|| @@ -317,6 +367,13 @@ int main(void) cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << endl; G__pause(); cout << "back from cint" << endl; + } else if (preprocessed==".help") { + helpmessage(); + } else if (preprocessed.substr(0,5)==".read") { + quit=readfile(preprocessed.substr(5),allcommands); + } else if (preprocessed.substr(0,5)==".save") { + command = "// "+command; // we do not want the .save command itself in saved files + savefile(preprocessed.substr(5),allcommands); /* test for more special commands } else if (preprocessed==".xyz") { cout << "special command (TBD): " << command << endl; @@ -330,6 +387,75 @@ int main(void) process_tempfile(command); } } + if (is!=NULL) { + // test for end of file if reading from a stream + eof=is->eof(); + } else { + // save commands only when reading from keyboard + allcommands += command; + } + + } + return quit; +} + +bool readfile(string const & filename, string & allcommands) +{ + cout << "Reading commands from file " << filename << "." << endl; + bool quit=false; + ifstream fin; + fin.open(filename.c_str()); + if (fin.good()) { + quit=readlines(&fin,allcommands); + } else { + cout << "Cannot open " << filename << " for reading." << endl; + } + fin.close(); + return quit; +} + +void savefile(string const & filename, string const & allcommands) +{ + cout << "Saving commands to file " << filename << "." << endl; + ofstream fout; + fout.open(filename.c_str()); + if (fout.good()) { + fout << allcommands; + if (!fout.good()) { + cout << "Cannot save commands to " << filename << "." << endl; + } + } else { + cout << "Cannot open " << filename << " for writing." << endl; + } + fout.close(); +} + +int main(int argc, char ** argv) +{ + string allcommands; + initialize(); + + bool quit=false; + bool argsexist=argc>1; + + if (argsexist) { + allcommands="/* Files given as command line arguments:\n"; + } + + argc--; argv++; + while (argc && !quit) { + allcommands += *argv; + allcommands += "\n"; + quit=readfile(*argv,allcommands); + argc--; argv++; + } + + if (argsexist) { + allcommands += "*/\n"; + } + + if (!quit) { + readlines(NULL,allcommands); } return 0;