+bool is_command(std::string const & command,
+ std::string & preprocessed,
+ std::string const & comparevalue,
+ bool substr)
+{
+ bool single_quote = false;
+ bool double_quote = false;
+ bool comment = false;
+ unsigned open_braces = 0;
+ if ((preprocessed=="."+comparevalue)||
+ substr&&(preprocessed.substr(0,comparevalue.length()+1)==
+ "."+comparevalue)) {
+ return true;
+ }
+ if ((command=="//GiNaC-cint."+comparevalue+"\n") ||
+ substr &&
+ (command.substr(0,comparevalue.length()+13)=="//GiNaC-cint."+comparevalue)) {
+ preprocessed = preprocess(command.substr(12).c_str(),comment,
+ single_quote,double_quote,open_braces);
+ return true;
+ }
+ return false;
+}
+
+bool readlines(istream * is,
+ std::string & allcommands)
+{
+ char const * line;
+ char prompt[G__ONELINE];
+ std::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)&&(!eof)) {
+ strcpy(prompt,PROMPT1);
+ bool end_of_command = false;
+ std::string command;
+ std::string preprocessed;
+ while (!end_of_command) {
+ 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);
+ if ((open_braces==0)&&(!single_quote)&&(!double_quote)&&(!comment)) {
+ unsigned l = preprocessed.length();
+ if ((l==0)||
+ (preprocessed[0]=='#')||
+ (preprocessed[0]=='.')||
+ (preprocessed[l-1]==';')||
+ (preprocessed[l-1]=='}')) {
+ end_of_command = true;
+ }
+ }
+ strcpy(prompt,PROMPT2);
+ }
+ if ((preprocessed=="quit;")||
+ (preprocessed=="exit;")||
+ (preprocessed=="bye;")||
+ (is_command(command,preprocessed,"quit"))||
+ (is_command(command,preprocessed,"exit"))||
+ (is_command(command,preprocessed,"bye"))||
+ (is_command(command,preprocessed,"q"))) {
+ quit = true;
+ } else if (is_command(command,preprocessed,"function")) {
+ if (!silent)
+ std::cout << "next expression can be a function definition" << std::endl;
+ next_command_is_function = true;
+ } else if (is_command(command,preprocessed,"cint")) {
+ std::cout << std::endl << "switching to cint interactive mode" << std::endl;
+ std::cout << "'h' for help, 'q' to quit, '{ statements }' or 'p [expression]' to evaluate" << std::endl;
+ G__pause();
+ std::cout << "back from cint" << std::endl;
+ } else if (is_command(command,preprocessed,"help")) {
+ helpmessage();
+ } else if (is_command(command,preprocessed,"read",true)) {
+ quit = readfile(preprocessed.substr(5),allcommands);
+ } else if (is_command(command,preprocessed,"save",true)) {
+ command = "/* "+command+" */"; // we do not want the .save command itself in saved files
+ savefile(preprocessed.substr(5),allcommands);
+ } else if (is_command(command,preprocessed,"restart")) {
+ restart();
+ } else if (is_command(command,preprocessed,"redirect",true)) {
+ redirect(preprocessed.substr(9));
+ } else if (is_command(command,preprocessed,">",true)) {
+ redirect(preprocessed.substr(2));
+ } else if (is_command(command,preprocessed,"silent")) {
+ if (!silent) {
+ redirect("/dev/null");
+ silent = true;
+ } else {
+ redirect("");
+ silent = false;
+ }
+ } else if (is_command(command,preprocessed,"warranty")) {
+ warrantymessage();
+ /* insert tests for more special commands here */
+ } else if (command.substr(0,2)=="#!") {
+ // ignore lines which indicate that this file is executed as a script
+ } else {
+ // std::cout << "now processing: " << command << std::endl;
+ if (next_command_is_function) {
+ next_command_is_function = false;
+ filenames.push_back(process_permanentfile(command));
+ } else {
+ 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(std::string const & filename,
+ std::string & allcommands,
+ bool shutup = false)
+{
+ if (!shutup)
+ std::cout << "Reading commands from file " << filename << "." << std::endl;
+ bool quit = false;
+ std::ifstream fin;
+ fin.open(filename.c_str());
+ if (fin.good())
+ quit = readlines(&fin,allcommands);
+ else
+ std::cout << "Cannot open " << filename << " for reading." << std::endl;
+ fin.close();
+ return quit;
+}
+
+void savefile(std::string const & filename, std::string const & allcommands)
+{
+ std::cout << "Saving commands to file " << filename << "." << std::endl;
+ std::ofstream fout;
+ fout.open(filename.c_str());
+ if (fout.good()) {
+ fout << allcommands;
+ if (!fout.good()) {
+ std::cout << "Cannot save commands to " << filename << "." << std::endl;
+ }
+ } else {
+ std::cout << "Cannot open " << filename << " for writing." << std::endl;
+ }
+ fout.close();
+}
+
+int main(int argc, char * *argv)
+{
+ std::string allcommands;
+ initialize();
+
+ bool quit = false;
+ // sort out and evaluate recognized options from the argument list
+ for (int i=1; i<argc; ++i)
+ if (evaloption(argv[i])) {
+ for (int j=i; j<argc-1; ++j)
+ argv[j] = argv[j+1];
+ --argc;
+ }
+ bool argsexist = argc>1;
+
+ // greet the user if it makes sense
+ if (isatty(0) && !silent)
+ greeting();
+
+ // evaluate files given as command line arguments
+ if (argsexist) {
+ allcommands = "/* Files given as command line arguments:\n";
+ --argc;
+ while (argc && !quit) {
+ allcommands += std::string(argv[argc])+'\n';
+ quit = readfile(argv[argc], allcommands, silent);
+ --argc;
+ }
+ allcommands += "*/\n";
+ }
+
+ // evaluate input from command line or script
+ if (!quit)
+ readlines(NULL, allcommands);
+
+ return 0;
+}