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