]> git.lyx.org Git - lyx.git/blob - src/lyx_main.C
5aa42b2e9198848bf798a948186742cb42fa197e
[lyx.git] / src / lyx_main.C
1 /**
2  * \file lyx_main.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16 #include <version.h>
17
18 #include "lyx_main.h"
19
20 #include "buffer.h"
21 #include "buffer_funcs.h"
22 #include "bufferlist.h"
23 #include "converter.h"
24 #include "debug.h"
25 #include "encoding.h"
26 #include "errorlist.h"
27 #include "format.h"
28 #include "gettext.h"
29 #include "kbmap.h"
30 #include "language.h"
31 #include "lastfiles.h"
32 #include "lyxfunc.h"
33 #include "lyxlex.h"
34 #include "lyxrc.h"
35 #include "lyxtextclasslist.h"
36 #include "lyxserver.h"
37 #include "MenuBackend.h"
38 #include "ToolbarBackend.h"
39
40 #include "frontends/Alert.h"
41 #include "frontends/lyx_gui.h"
42
43 #include "support/FileInfo.h"
44 #include "support/filetools.h"
45 #include "support/lyxlib.h"
46 #include "support/os.h"
47 #include "support/path.h"
48 #include "support/path_defines.h"
49
50 #include <boost/bind.hpp>
51
52 #include <iostream>
53
54 using namespace lyx::support;
55
56 using std::vector;
57 using std::endl;
58
59 #ifndef CXX_GLOBAL_CSTD
60 using std::exit;
61 using std::signal;
62 using std::system;
63 #endif
64
65 extern void QuitLyX();
66
67 extern LyXServer * lyxserver;
68
69 DebugStream lyxerr;
70
71 boost::scoped_ptr<LastFiles> lastfiles;
72
73 // This is the global bufferlist object
74 BufferList bufferlist;
75
76 // convenient to have it here.
77 boost::scoped_ptr<kb_keymap> toplevel_keymap;
78
79 namespace {
80
81 void showFileError(string const & error)
82 {
83         Alert::warning(_("Could not read configuration file"),
84                    bformat(_("Error while reading the configuration file\n%1$s.\n"
85                      "Please check your installation."), error));
86         exit(EXIT_FAILURE);
87 }
88
89 }
90
91 LyX::LyX(int & argc, char * argv[])
92 {
93         // Here we need to parse the command line. At least
94         // we need to parse for "-dbg" and "-help"
95         bool const want_gui = easyParse(argc, argv);
96
97         // set the DisplayTranslator only once; should that be done here??
98         // if this should not be in this file, please also remove
99         // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
100         lyx::graphics::setDisplayTranslator();
101
102         if (want_gui)
103                 lyx_gui::parse_init(argc, argv);
104
105         // check for any spurious extra arguments
106         // other than documents
107         for (int argi = 1; argi < argc ; ++argi) {
108                 if (argv[argi][0] == '-') {
109                         lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
110                                 argv[argi]) << endl;
111                         exit(1);
112                 }
113         }
114
115         // Initialization of LyX (reads lyxrc and more)
116         lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
117         init(want_gui);
118         lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
119
120         if (want_gui) 
121                 lyx_gui::parse_lyxrc();
122
123         vector<string> files;
124
125         for (int argi = argc - 1; argi >= 1; --argi) 
126                 files.push_back(argv[argi]);
127
128         if (first_start)
129                 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
130
131         // Execute batch commands if available
132         if (!batch_command.empty()) {
133
134                 lyxerr[Debug::INIT] << "About to handle -x '"
135                        << batch_command << '\'' << endl;
136
137                 Buffer * last_loaded = 0;
138
139                 vector<string>::const_iterator it = files.begin();
140                 vector<string>::const_iterator end = files.end();
141
142                 for (; it != end; ++it) {
143                         // get absolute path of file and add ".lyx" to
144                         // the filename if necessary
145                         string s = FileSearch(string(), *it, "lyx");
146                         if (s.empty()) {
147                                 last_loaded = newFile(*it, string(), true);
148                         } else {
149                                 Buffer * buf = bufferlist.newBuffer(s, false);
150                                 buf->error.connect(boost::bind(&LyX::printError, this, _1));
151                                 if (loadLyXFile(buf, s)) 
152                                         last_loaded = buf;
153                                 else
154                                         bufferlist.release(buf);
155                         }
156                 }
157
158                 // try to dispatch to last loaded buffer first
159                 if (last_loaded) {
160                         bool success = false;
161                         if (last_loaded->dispatch(batch_command, &success)) {
162                                 QuitLyX();
163                                 exit(!success);
164                         }
165                 } 
166                 files.clear(); // the files are already loaded
167         }
168
169         lyx_gui::start(batch_command, files);
170 }
171
172
173 extern "C" {
174
175 static void error_handler(int err_sig)
176 {
177         switch (err_sig) {
178         case SIGHUP:
179                 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
180                 break;
181         case SIGINT:
182                 // no comments
183                 break;
184         case SIGFPE:
185                 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
186                 break;
187         case SIGSEGV:
188                 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
189                 lyxerr <<
190                         "Sorry, you have found a bug in LyX. "
191                         "Please read the bug-reporting instructions "
192                         "in Help->Introduction and send us a bug report, "
193                         "if necessary. Thanks !" << endl;
194                 break;
195         case SIGTERM:
196                 // no comments
197                 break;
198         }
199
200         // Deinstall the signal handlers
201         signal(SIGHUP, SIG_DFL);
202         signal(SIGINT, SIG_DFL);
203         signal(SIGFPE, SIG_DFL);
204         signal(SIGSEGV, SIG_DFL);
205         signal(SIGTERM, SIG_DFL);
206
207         LyX::emergencyCleanup();
208
209         lyxerr << "Bye." << endl;
210         if (err_sig!= SIGHUP &&
211            (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
212                 lyx::support::abort();
213         exit(0);
214 }
215
216 }
217
218
219 void LyX::printError(ErrorItem const & ei)
220 {
221         std::cerr << _("LyX: ") << ei.error
222                   << ':' << ei.description << std::endl;
223
224 }
225
226
227 void LyX::init(bool gui)
228 {
229         signal(SIGHUP, error_handler);
230         signal(SIGFPE, error_handler);
231         signal(SIGSEGV, error_handler);
232         signal(SIGINT, error_handler);
233         signal(SIGTERM, error_handler);
234
235         bool const explicit_userdir = setLyxPaths();
236
237         // Check that user LyX directory is ok. We don't do that if
238         // running in batch mode.
239         if (gui) {
240                 queryUserLyXDir(explicit_userdir);
241         } else {
242                 first_start = false;
243         }
244
245         // Disable gui when easyparse says so
246         lyx_gui::use_gui = gui;
247
248         if (lyxrc.template_path.empty()) {
249                 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
250         }
251
252         if (lyxrc.lastfiles.empty()) {
253                 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
254         }
255
256         if (lyxrc.roman_font_name.empty())
257                 lyxrc.roman_font_name = lyx_gui::roman_font_name();
258         if (lyxrc.sans_font_name.empty())
259                 lyxrc.sans_font_name = lyx_gui::sans_font_name();
260         if (lyxrc.typewriter_font_name.empty())
261                 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
262
263         //
264         // Read configuration files
265         //
266
267         readRcFile("lyxrc.defaults");
268         system_lyxrc = lyxrc;
269         system_formats = formats;
270         system_converters = converters;
271         system_lcolor = lcolor;
272
273         string prefsfile = "preferences";
274         // back compatibility to lyxs < 1.1.6
275         if (LibFileSearch(string(), prefsfile).empty())
276                 prefsfile = "lyxrc";
277         if (!LibFileSearch(string(), prefsfile).empty())
278                 readRcFile(prefsfile);
279
280         readEncodingsFile("encodings");
281         readLanguagesFile("languages");
282
283         // Load the layouts
284         lyxerr[Debug::INIT] << "Reading layouts..." << endl;
285         LyXSetStyle();
286
287         if (gui) {
288                 // Set up bindings
289                 toplevel_keymap.reset(new kb_keymap);
290                 defaultKeyBindings(toplevel_keymap.get());
291                 toplevel_keymap->read(lyxrc.bind_file);
292
293                 // Read menus
294                 readUIFile(lyxrc.ui_file);
295         }
296
297         if (lyxerr.debugging(Debug::LYXRC))
298                 lyxrc.print();
299
300         os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
301         if (lyxerr.debugging(Debug::INIT)) {
302                 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
303         }
304
305         lyxerr[Debug::INIT] << "Reading lastfiles `"
306                             << lyxrc.lastfiles << "'..." << endl;
307         lastfiles.reset(new LastFiles(lyxrc.lastfiles,
308                                       lyxrc.check_lastfiles,
309                                       lyxrc.num_lastfiles));
310 }
311
312
313 void LyX::defaultKeyBindings(kb_keymap  * kbmap)
314 {
315         kbmap->bind("Right", LFUN_RIGHT);
316         kbmap->bind("Left", LFUN_LEFT);
317         kbmap->bind("Up", LFUN_UP);
318         kbmap->bind("Down", LFUN_DOWN);
319
320         kbmap->bind("Tab", LFUN_CELL_FORWARD);
321         kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
322
323         kbmap->bind("Home", LFUN_HOME);
324         kbmap->bind("End", LFUN_END);
325         kbmap->bind("Prior", LFUN_PRIOR);
326         kbmap->bind("Next", LFUN_NEXT);
327
328         kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
329         //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
330
331         kbmap->bind("Delete", LFUN_DELETE);
332         kbmap->bind("BackSpace", LFUN_BACKSPACE);
333
334         // sub- and superscript -MV
335         kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
336         kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
337
338         // kbmap->bindings to enable the use of the numeric keypad
339         // e.g. Num Lock set
340         //kbmap->bind("KP_0", LFUN_SELFINSERT);
341         //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
342         kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
343         //kbmap->bind("KP_1", LFUN_SELFINSERT);
344         //kbmap->bind("KP_2", LFUN_SELFINSERT);
345         //kbmap->bind("KP_3", LFUN_SELFINSERT);
346         //kbmap->bind("KP_4", LFUN_SELFINSERT);
347         //kbmap->bind("KP_5", LFUN_SELFINSERT);
348         //kbmap->bind("KP_6", LFUN_SELFINSERT);
349         //kbmap->bind("KP_Add", LFUN_SELFINSERT);
350         //kbmap->bind("KP_7", LFUN_SELFINSERT);
351         //kbmap->bind("KP_8", LFUN_SELFINSERT);
352         //kbmap->bind("KP_9", LFUN_SELFINSERT);
353         //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
354         //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
355         //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
356         kbmap->bind("KP_Right", LFUN_RIGHT);
357         kbmap->bind("KP_Left", LFUN_LEFT);
358         kbmap->bind("KP_Up", LFUN_UP);
359         kbmap->bind("KP_Down", LFUN_DOWN);
360         kbmap->bind("KP_Home", LFUN_HOME);
361         kbmap->bind("KP_End", LFUN_END);
362         kbmap->bind("KP_Prior", LFUN_PRIOR);
363         kbmap->bind("KP_Next", LFUN_NEXT);
364
365         kbmap->bind("C-Tab", LFUN_CELL_SPLIT);  // ale970515
366         kbmap->bind("S-Tab", LFUN_CELL_BACKWARD);  // jug20000522
367         kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
368 }
369
370
371 void LyX::emergencyCleanup()
372 {
373         // what to do about tmpfiles is non-obvious. we would
374         // like to delete any we find, but our lyxdir might
375         // contain documents etc. which might be helpful on
376         // a crash
377
378         bufferlist.emergencyWriteAll();
379         if (lyxserver)
380                 lyxserver->emergencyCleanup();
381 }
382
383
384 void LyX::deadKeyBindings(kb_keymap * kbmap)
385 {
386         // bindKeyings for transparent handling of deadkeys
387         // The keysyms are gotten from XFree86 X11R6
388         kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
389         kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
390         kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
391         kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
392         kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
393         kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
394         kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
395         kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
396         kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
397         kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
398         // nothing with this name
399         // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
400         kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
401         kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
402         // nothing with this name either...
403         //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
404         kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
405         kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
406         kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
407 }
408
409
410 void LyX::queryUserLyXDir(bool explicit_userdir)
411 {
412         string const configure_script = AddName(system_lyxdir(), "configure");
413
414         // Does user directory exist?
415         FileInfo fileInfo(user_lyxdir());
416         if (fileInfo.isOK() && fileInfo.isDir()) {
417                 first_start = false;
418                 FileInfo script(configure_script);
419                 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
420                 if (defaults.isOK() && script.isOK()
421                     && defaults.getModificationTime() < script.getModificationTime()) {
422                         lyxerr << _("LyX: reconfiguring user directory")
423                                << endl;
424                         Path p(user_lyxdir());
425                         ::system(configure_script.c_str());
426                         lyxerr << "LyX: " << _("Done!") << endl;
427                 }
428                 return;
429         }
430
431         first_start = !explicit_userdir;
432
433         lyxerr << bformat(_("LyX: Creating directory %1$s"
434                                   " and running configure..."), user_lyxdir()) << endl;
435
436         if (!createDirectory(user_lyxdir(), 0755)) {
437                 // Failed, let's use $HOME instead.
438                 user_lyxdir(GetEnvPath("HOME"));
439                 lyxerr << bformat(_("Failed. Will use %1$s instead."),
440                         user_lyxdir()) << endl;
441                 return;
442         }
443
444         // Run configure in user lyx directory
445         Path p(user_lyxdir());
446         ::system(configure_script.c_str());
447         lyxerr << "LyX: " << _("Done!") << endl;
448 }
449
450
451 void LyX::readRcFile(string const & name)
452 {
453         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
454
455         string const lyxrc_path = LibFileSearch(string(), name);
456         if (!lyxrc_path.empty()) {
457
458                 lyxerr[Debug::INIT] << "Found " << name
459                                     << " in " << lyxrc_path << endl;
460
461                 if (lyxrc.read(lyxrc_path) >= 0)
462                         return;
463         }
464
465         showFileError(name);
466 }
467
468
469 // Read the ui file `name'
470 void LyX::readUIFile(string const & name)
471 {
472         enum Uitags {
473                 ui_menuset = 1,
474                 ui_toolbar,
475                 ui_toolbars,
476                 ui_include,
477                 ui_last
478         };
479
480         struct keyword_item uitags[ui_last - 1] = {
481                 { "include", ui_include },
482                 { "menuset", ui_menuset },
483                 { "toolbar", ui_toolbar },
484                 { "toolbars", ui_toolbars }
485         };
486
487         // Ensure that a file is read only once (prevents include loops)
488         static std::list<string> uifiles;
489         std::list<string>::const_iterator it  = uifiles.begin();
490         std::list<string>::const_iterator end = uifiles.end();
491         it = std::find(it, end, name);
492         if (it != end) {
493                 lyxerr[Debug::INIT] << "UI file '" << name
494                                     << "' has been read already. "
495                                     << "Is this an include loop?"
496                                     << endl;
497                 return;
498         }
499
500         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
501
502         string const ui_path = LibFileSearch("ui", name, "ui");
503
504         if (ui_path.empty()) {
505                 lyxerr[Debug::INIT] << "Could not find " << name << endl;
506                 showFileError(name);
507                 return;
508         }
509         uifiles.push_back(name);
510
511         lyxerr[Debug::INIT] << "Found " << name
512                             << " in " << ui_path << endl;
513         LyXLex lex(uitags, ui_last - 1);
514         lex.setFile(ui_path);
515         if (!lex.isOK()) {
516                 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
517                        << endl;
518         }
519
520         if (lyxerr.debugging(Debug::PARSER))
521                 lex.printTable(lyxerr);
522
523         while (lex.isOK()) {
524                 switch (lex.lex()) {
525                 case ui_include: {
526                         lex.next(true);
527                         string const file = lex.getString();
528                         readUIFile(file);
529                         break;
530                 }
531                 case ui_menuset:
532                         menubackend.read(lex);
533                         break;
534
535                 case ui_toolbar:
536                         toolbarbackend.read(lex);
537                         break;
538
539                 case ui_toolbars:
540                         toolbarbackend.readToolbars(lex);
541                         break;
542
543                 default:
544                         if (!rtrim(lex.getString()).empty())
545                                 lex.printError("LyX::ReadUIFile: "
546                                                "Unknown menu tag: `$$Token'");
547                         break;
548                 }
549         }
550 }
551
552
553 // Read the languages file `name'
554 void LyX::readLanguagesFile(string const & name)
555 {
556         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
557
558         string const lang_path = LibFileSearch(string(), name);
559         if (lang_path.empty()) {
560                 showFileError(name);
561                 return;
562         }
563         languages.read(lang_path);
564 }
565
566
567 // Read the encodings file `name'
568 void LyX::readEncodingsFile(string const & name)
569 {
570         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
571
572         string const enc_path = LibFileSearch(string(), name);
573         if (enc_path.empty()) {
574                 showFileError(name);
575                 return;
576         }
577         encodings.read(enc_path);
578 }
579
580
581 namespace {
582
583 bool is_gui = true;
584 string batch;
585
586 /// return the the number of arguments consumed
587 typedef boost::function<int(string const &, string const &)> cmd_helper;
588
589 int parse_dbg(string const & arg, string const &)
590 {
591         if (arg.empty()) {
592                 lyxerr << _("List of supported debug flags:") << endl;
593                 Debug::showTags(lyxerr);
594                 exit(0);
595         }
596         lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
597
598         lyxerr.level(Debug::value(arg));
599         Debug::showLevel(lyxerr, lyxerr.level());
600         return 1;
601 }
602
603
604 int parse_help(string const &, string const &)
605 {
606         lyxerr <<
607                 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
608                   "Command line switches (case sensitive):\n"
609                   "\t-help              summarize LyX usage\n"
610                   "\t-userdir dir       try to set user directory to dir\n"
611                   "\t-sysdir dir        try to set system directory to dir\n"
612                   "\t-geometry WxH+X+Y  set geometry of the main window\n"
613                   "\t-dbg feature[,feature]...\n"
614                   "                  select the features to debug.\n"
615                   "                  Type `lyx -dbg' to see the list of features\n"
616                   "\t-x [--execute] command\n"
617                   "                  where command is a lyx command.\n"
618                   "\t-e [--export] fmt\n"
619                   "                  where fmt is the export format of choice.\n"
620                   "\t-i [--import] fmt file.xxx\n"
621                   "                  where fmt is the import format of choice\n"
622                   "                  and file.xxx is the file to be imported.\n"
623                   "\t-version        summarize version and build info\n"
624                   "Check the LyX man page for more details.") << endl;
625         exit(0);
626         return 0;
627 }
628
629 int parse_version(string const &, string const &)
630 {
631         lyxerr << "LyX " << lyx_version
632                << " of " << lyx_release_date << endl;
633         lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
634
635         lyxerr << lyx_version_info << endl;
636         exit(0);
637         return 0;
638 }
639
640 int parse_sysdir(string const & arg, string const &)
641 {
642         if (arg.empty()) {
643                 lyxerr << _("Missing directory for -sysdir switch") << endl;
644                 exit(1);
645         }
646         system_lyxdir(arg);
647         return 1;
648 }
649
650 int parse_userdir(string const & arg, string const &)
651 {
652         if (arg.empty()) {
653                 lyxerr << _("Missing directory for -userdir switch") << endl;
654                 exit(1);
655         }
656         user_lyxdir(arg);
657         return 1;
658 }
659
660 int parse_execute(string const & arg, string const &)
661 {
662         if (arg.empty()) {
663                 lyxerr << _("Missing command string after --execute switch") << endl;
664                 exit(1);
665         }
666         batch = arg;
667         // Argh. Setting gui to false segfaults..
668         // FIXME: when ? how ?
669         // is_gui = false;
670         return 1;
671 }
672
673 int parse_export(string const & type, string const &)
674 {
675         if (type.empty()) {
676                 lyxerr << _("Missing file type [eg latex, ps...] after "
677                         "--export switch") << endl;
678                 exit(1);
679         }
680         batch = "buffer-export " + type;
681         is_gui = false;
682         return 1;
683 }
684
685 int parse_import(string const & type, string const & file)
686 {
687         if (type.empty()) {
688                 lyxerr << _("Missing file type [eg latex, ps...] after "
689                         "--import switch") << endl;
690                 exit(1);
691         }
692         if (file.empty()) {
693                 lyxerr << _("Missing filename for --import") << endl;
694                 exit(1);
695         }
696
697         batch = "buffer-import " + type + ' ' + file;
698         return 2;
699 }
700
701 } // namespace anon
702
703
704 bool LyX::easyParse(int & argc, char * argv[])
705 {
706         std::map<string, cmd_helper> cmdmap;
707
708         cmdmap["-dbg"] = parse_dbg;
709         cmdmap["-help"] = parse_help;
710         cmdmap["--help"] = parse_help;
711         cmdmap["-version"] = parse_version;
712         cmdmap["--version"] = parse_version;
713         cmdmap["-sysdir"] = parse_sysdir;
714         cmdmap["-userdir"] = parse_userdir;
715         cmdmap["-x"] = parse_execute;
716         cmdmap["--execute"] = parse_execute;
717         cmdmap["-e"] = parse_export;
718         cmdmap["--export"] = parse_export;
719         cmdmap["-i"] = parse_import;
720         cmdmap["--import"] = parse_import;
721
722         for (int i = 1; i < argc; ++i) {
723                 std::map<string, cmd_helper>::const_iterator it
724                         = cmdmap.find(argv[i]);
725
726                 // don't complain if not found - may be parsed later
727                 if (it == cmdmap.end())
728                         continue;
729
730                 string arg((i + 1 < argc) ? argv[i + 1] : "");
731                 string arg2((i + 2 < argc) ? argv[i + 2] : "");
732
733                 int const remove = 1 + it->second(arg, arg2);
734
735                 // Now, remove used arguments by shifting
736                 // the following ones remove places down.
737                 argc -= remove;
738                 for (int j = i; j < argc; ++j)
739                         argv[j] = argv[j + remove];
740                 --i;
741         }
742
743         batch_command = batch;
744
745         return is_gui;
746 }