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