00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <getopt.h>
00014 #include <basix/string.hpp>
00015 #include <basix/list.hpp>
00016 #include <basix/system.hpp>
00017 #include <basix/document.hpp>
00018 #include <mmxlight/base_evaluator.hpp>
00019 #include <mmxlight/shell.hpp>
00020
00021 namespace mmx {
00022 #define DATA_BEGIN ((char) 2)
00023 #define DATA_END ((char) 5)
00024
00025 bool script_flag = false;
00026 bool quiet_flag = false;
00027 bool debug_flag = false;
00028 bool type_flag = false;
00029 bool time_flag = false;
00030 nat history_len = 100;
00031
00032 bool completion_flag = true;
00033 bool batch_flag = true;
00034 bool replay_flag = false;
00035
00036 bool disable_boot = false;
00037
00038
00039 list<string> identifiers_list =
00040 list<string> ("case", "continue", "else", "error") *
00041 list<string> ("exists", "for", "generate", "in") *
00042 list<string> ("operator", "postfix", "prefix", "step") *
00043 list<string> ("until", "while");
00044
00045 static void
00046 help(string progname) {
00047 mmerr << "Usage: " << progname << " [-c on|off] [-q] [--version] [--debug] [--texmacs] [--help] [filelist]\n";
00048 mmerr << " -c on|off\t" << "enable|disable the completion mode; default is on.\n";
00049 mmerr << " -q on|off\t" << "quiet mode: do not display prompts and output; default is off.\n";
00050 mmerr << " --noboot\t" << "do not load boot file.\n";
00051 mmerr << " --replay\t" << "replay previous session.\n";
00052 mmerr << " --version\t" << "display version number.\n";
00053 mmerr << " --debug\t" << "enable debug mode.\n";
00054 mmerr << " --texmacs\t" << "enable texmacs mode; should be enabled by texmacs only.\n";
00055 mmerr << " --help\t" << "display this message.\n";
00056 }
00057
00058 static list<string>
00059 handle_cmdline_options(int argc, char** argv) {
00060 int c;
00061 int option_index = 0;
00062 string arg, tmp;
00063 list<string> to_load;
00064 string progname = string(argv[0]);
00065
00066 opterr=0;
00067 struct option long_options[] =
00068 {
00069 {"version", 0, 0, 0},
00070 {"noboot", 0, 0, 0},
00071 {"replay", 0, 0, 0},
00072 {"debug", 0, 0, 0},
00073 {"texmacs", 0, 0, 0},
00074 {"help", 0, 0, 0},
00075 {0, 0, 0, 0}
00076 };
00077
00078 while (1) {
00079 option_index = 0;
00080
00081 c = getopt_long (argc, argv, "c:q:",
00082 long_options, &option_index);
00083 if (c == -1)
00084 break;
00085
00086 if (optarg)
00087 arg = string(optarg);
00088
00089 switch (c)
00090 {
00091 case 0:
00092 tmp = string((char*)long_options[option_index].name);
00093 if (tmp=="texmacs") {
00094 texmacs_mode=true;
00095 math_mode=true;
00096 break;
00097 }
00098 if (tmp=="debug") {
00099 debug_flag=true;
00100 break;
00101 }
00102 if (tmp=="noboot") {
00103 disable_boot=true;
00104 break;
00105 }
00106 if (tmp=="version") {
00107 mmout << MMXLIGHT_VERSION << "\n";
00108 exit(0);
00109 }
00110 if (tmp=="replay") {
00111 replay_flag = true;
00112 break;
00113 }
00114 if (tmp=="help") {
00115 help(progname);
00116 exit(0);
00117 }
00118
00119 mmerr << progname << " error in command line\n";
00120 help(progname);
00121 exit(-1);
00122
00123 case 'c':
00124 tmp = "-c " * arg;
00125 if (arg=="off") {
00126 completion_flag=false;
00127 break;
00128 }
00129 if (arg=="on") {
00130 completion_flag=true;
00131 break;
00132 }
00133 mmerr << progname << " error in command line\n";
00134 help(progname);
00135 exit(-1);
00136
00137 case 'q':
00138 tmp = "-q";
00139 if (arg=="off") {
00140 quiet_flag=false;
00141 break;
00142 }
00143 if (arg=="on") {
00144 quiet_flag=true;
00145 break;
00146 }
00147 mmerr << progname << " error in command line\n";
00148 help(progname);
00149 exit(-1);
00150
00151 case '?':
00152 mmerr << progname << " : bad command line\n";
00153 help(progname);
00154 exit(-1);
00155
00156 default:
00157 mmerr << progname << " : error in command line\n";
00158 }
00159 }
00160
00161 if (optind < argc) {
00162 while (optind < argc)
00163 to_load = cons(string(argv[optind++]), to_load);
00164 }
00165 return reverse (to_load);
00166 }
00167
00168 static void
00169 output_banner () {
00170 if (texmacs_mode) {
00171 string title= "Welcome to Mathemagix-light " * string (MMXLIGHT_VERSION);
00172 string license= "This software falls under the GNU General Public License";
00173 string warning= "It comes without any warranty whatsoever";
00174 string copyright0= "www.mathemagix.org";
00175 string copyright1= "(c) 2001-2010";
00176 mmout << DATA_BEGIN << "scheme:(document";
00177 mmout << "(tformat (cwith 1 5 1 1 \"cell-halign\" \"c\") (table ";
00178 mmout << "(row (with \"color\" \"blue\" (with \"font-base-size\" 14 \""
00179 * title * "\"))) ";
00180 mmout << "(row (with \"color\" \"blue\" \"" * license * "\")) ";
00181 mmout << "(row (with \"color\" \"orange\" \"" * warning * "\"))";
00182 mmout << "(row \"" << copyright0 << "\")";
00183 mmout << "(row \"" << copyright1 << "\")";
00184 mmout << ")))";
00185 mmout << DATA_END;
00186 }
00187 else {
00188 mmout << "--------------------------------------------------------------\n";
00189 mmout << "|:*) Welcome to Mathemagix-light " << MMXLIGHT_VERSION;
00190 for (nat i=0; i<16-N(string(MMXLIGHT_VERSION)); i++) mmout << " ";
00191 mmout << "(*:|\n";
00192 mmout << "|------------------------------------------------------------|\n";
00193 mmout << "| This software falls under the GNU General Public License |\n";
00194 mmout << "| It comes without any warranty whatsoever |\n";
00195 mmout << "| www.mathemagix.org |\n";
00196 mmout << "| (c) 2001--2010 |\n";
00197 mmout << "--------------------------------------------------------------\n";
00198 }
00199 }
00200
00201 list<string>
00202 itf_initialize (int argc, char** argv) {
00203 string first= (argc <= 1? string (""): string (argv[1]));
00204 if (argc >= 2 && file_is_script (argv[1])) {
00205 script_flag= true;
00206 return list<string> (string (argv[1]));
00207 }
00208 else {
00209 list<string> to_load;
00210 to_load = handle_cmdline_options(argc, argv);
00211 if (!quiet_flag && is_nil (to_load)) output_banner();
00212 if (texmacs_mode)
00213 itf_texmacs_initialize();
00214 return to_load;
00215 }
00216 }
00217
00218 void
00219 itf_exit (const int& i) {
00220 mmerr << flush_now;
00221 mmout << flush_now;
00222 if (!batch_flag) {
00223 itf_save_history ();
00224 itf_save_session ();
00225 }
00226 restore_evaluator ();
00227 exit (i);
00228 }
00229
00230 void
00231 itf_quit () {
00232 itf_exit (0);
00233 }
00234
00235 vector<generic>
00236 itf_history () {
00237 vector<generic> v;
00238 for (nat i=0; i<get_interactive_number(); i++)
00239 v << as<generic> (get_interactive_source (i));
00240 return v;
00241 }
00242
00243 }