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