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