3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
6 * \author Lars Gullik Bjønnes
14 #include "support/filetools.h"
15 #include "support/lyxlib.h"
16 #include "support/os.h"
17 #include "support/FileInfo.h"
18 #include "support/path.h"
23 #include "graphics/GraphicsTypes.h"
25 #include "bufferlist.h"
27 #include "buffer_funcs.h"
28 #include "lyxserver.h"
31 #include "ToolbarBackend.h"
32 #include "MenuBackend.h"
34 #include "lastfiles.h"
36 #include "converter.h"
38 #include "lyxtextclasslist.h"
40 #include "frontends/Alert.h"
41 #include "frontends/lyx_gui.h"
43 #include <boost/function.hpp>
44 #include <boost/bind.hpp>
45 #include <boost/signals/signal1.hpp>
51 using namespace lyx::support;
56 #ifndef CXX_GLOBAL_CSTD
62 namespace grfx = lyx::graphics;
64 extern void LoadLyXFile(string const &);
65 extern void QuitLyX();
67 extern LyXServer * lyxserver;
71 string system_tempdir;
76 boost::scoped_ptr<LastFiles> lastfiles;
78 // This is the global bufferlist object
79 BufferList bufferlist;
81 // convenient to have it here.
82 boost::scoped_ptr<kb_keymap> toplevel_keymap;
86 void showFileError(string const & error)
88 Alert::warning(_("Could not read configuration file"),
89 bformat(_("Error while reading the configuration file\n%1$s.\n"
90 "Please check your installation."), error));
96 LyX::LyX(int & argc, char * argv[])
98 // Here we need to parse the command line. At least
99 // we need to parse for "-dbg" and "-help"
100 bool const want_gui = easyParse(argc, argv);
102 // Global bindings (this must be done as early as possible.) (Lgb)
103 toplevel_keymap.reset(new kb_keymap);
104 defaultKeyBindings(toplevel_keymap.get());
106 // set the DisplayTranslator only once; should that be done here??
107 // if this should not be in this file, please also remove
108 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
109 grfx::setDisplayTranslator();
112 lyx_gui::parse_init(argc, argv);
115 // check for any spurious extra arguments
116 // other than documents
117 for (int argi = 1; argi < argc ; ++argi) {
118 if (argv[argi][0] == '-') {
119 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
125 // Initialization of LyX (reads lyxrc and more)
126 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
128 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
131 lyx_gui::parse_lyxrc();
134 vector<string> files;
136 for (int argi = argc - 1; argi >= 1; --argi) {
137 files.push_back(argv[argi]);
141 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
143 // Execute batch commands if available
144 if (!batch_command.empty()) {
146 lyxerr[Debug::INIT] << "About to handle -x '"
147 << batch_command << '\'' << endl;
149 Buffer * last_loaded = 0;
151 vector<string>::const_iterator it = files.begin();
152 vector<string>::const_iterator end = files.end();
154 for (; it != end; ++it) {
155 // get absolute path of file and add ".lyx" to
156 // the filename if necessary
157 string s = FileSearch(string(), *it, "lyx");
159 last_loaded = newFile(*it, "");
161 last_loaded = bufferlist.newBuffer(s, false);
162 last_loaded->error.connect(boost::bind(&LyX::printError, this, _1));
163 if (!loadLyXFile(last_loaded, s)) {
164 bufferlist.release(last_loaded);
165 last_loaded = newFile(*it, string());
170 // try to dispatch to last loaded buffer first
172 bool success = false;
173 if (last_loaded->dispatch(batch_command, &success)) {
178 files.clear(); // the files are already loaded
181 lyx_gui::start(batch_command, files);
187 static void error_handler(int err_sig)
191 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
197 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
200 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
202 "Sorry, you have found a bug in LyX. "
203 "Please read the bug-reporting instructions "
204 "in Help->Introduction and send us a bug report, "
205 "if necessary. Thanks !" << endl;
212 // Deinstall the signal handlers
213 signal(SIGHUP, SIG_DFL);
214 signal(SIGINT, SIG_DFL);
215 signal(SIGFPE, SIG_DFL);
216 signal(SIGSEGV, SIG_DFL);
217 signal(SIGTERM, SIG_DFL);
219 LyX::emergencyCleanup();
221 lyxerr << "Bye." << endl;
222 if (err_sig!= SIGHUP &&
223 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
224 lyx::support::abort();
231 void LyX::printError(ErrorItem const & ei)
233 std::cerr << _("LyX: ") << ei.error
234 << ':' << ei.description << std::endl;
239 void LyX::init(bool gui)
241 signal(SIGHUP, error_handler);
242 signal(SIGFPE, error_handler);
243 signal(SIGSEGV, error_handler);
244 signal(SIGINT, error_handler);
245 signal(SIGTERM, error_handler);
248 // Determine path of binary
251 string binpath = os::binpath();
252 string binname = os::binname();
253 string fullbinname = MakeAbsPath(binname, binpath);
255 if (binpath.empty()) {
256 lyxerr << _("Warning: could not determine path of binary.")
258 << _("If you have problems, try starting LyX with an absolute path.")
261 lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
262 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
265 // Determine system directory.
268 // Directories are searched in this order:
269 // 1) -sysdir command line parameter
270 // 2) LYX_DIR_14x environment variable
271 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
272 // 4) <path of binary>/../share/<name of binary>/
273 // 4a) repeat 4 after following the Symlink if <path of
274 // binary> is a symbolic link.
275 // 5) hardcoded lyx_dir
276 // The directory is checked for the presence of the file
277 // "chkconfig.ltx", and if that is present, the directory
278 // is accepted as the system directory.
279 // If no chkconfig.ltx file is found, a warning is given,
280 // and the hardcoded lyx_dir is used.
282 // If we had a command line switch, system_lyxdir is already set
284 if (!system_lyxdir.empty())
285 searchpath = MakeAbsPath(system_lyxdir) + ';';
287 string const lyxdir = GetEnvPath("LYX_DIR_14x");
289 if (!lyxdir.empty()) {
290 lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
291 searchpath += lyxdir + ';';
294 string fullbinpath = binpath;
295 FileInfo file(fullbinname, true);
297 lyxerr[Debug::INIT] << "binary is a link" << endl;
299 if (LyXReadLink(fullbinname, link, true)) {
300 // Path of binary/../share/name of binary/
301 searchpath += NormalizePath(AddPath(binpath,
303 + OnlyFilename(binname));
306 binpath = MakeAbsPath(OnlyPath(fullbinpath));
312 // Path of binary/../share/name of binary/
313 searchpath += NormalizePath(AddPath(binpath, "../share/") +
314 OnlyFilename(binname)) + ';';
317 FileInfo file(fullbinpath, true);
318 followlink = file.isLink();
320 lyxerr[Debug::INIT] << " directory " << fullbinpath
321 << " is a link" << endl;
323 if (LyXReadLink(fullbinpath, link, true)) {
325 binpath = MakeAbsPath(OnlyPath(fullbinpath));
331 } while (followlink);
333 // <path of binary>/TOP_SRCDIR/lib
334 build_lyxdir = MakeAbsPath("../lib", binpath);
335 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
336 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
338 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
339 "place... yes" << endl;
342 << "Checking whether LyX is run in place... no"
344 build_lyxdir.erase();
348 searchpath += LYX_DIR;
350 lyxerr[Debug::INIT] << "System directory search path: "
351 << searchpath << endl;
353 string const filename = "chkconfig.ltx";
354 string const temp = FileOpenSearch(searchpath, filename, string());
355 system_lyxdir = OnlyPath(temp);
357 // Reduce "path/../path" stuff out of system directory
358 system_lyxdir = NormalizePath(system_lyxdir);
360 bool path_shown = false;
362 // Warn if environment variable is set, but unusable
363 if (!lyxdir.empty()) {
364 if (system_lyxdir != NormalizePath(lyxdir)) {
365 lyxerr <<_("LYX_DIR_14x environment variable no good.")
367 << _("System directory set to: ")
368 << system_lyxdir << endl;
373 // Warn the user if we couldn't find "chkconfig.ltx"
374 if (system_lyxdir == "./") {
375 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
376 <<_("Try the '-sysdir' command line parameter or ")
377 <<_("set the environment variable LYX_DIR_14x to the "
378 "LyX system directory ")
379 << _("containing the file `chkconfig.ltx'.") << endl;
381 FileInfo fi(LYX_DIR);
383 lyxerr << "Couldn't even find the default LYX_DIR." << endl
384 << "Giving up." << endl;
387 lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
390 lyxerr << _("Expect problems.") << endl;
392 system_lyxdir = LYX_DIR;
397 lyxerr[Debug::INIT] << "System directory: '"
398 << system_lyxdir << '\'' << endl;
401 // Determine user lyx-dir
404 // Directories are searched in this order:
405 // 1) -userdir command line parameter
406 // 2) LYX_USERDIR_14x environment variable
407 // 3) $HOME/.<name of binary>
409 // If we had a command line switch, user_lyxdir is already set
410 bool explicit_userdir = true;
411 if (user_lyxdir.empty()) {
413 // LYX_USERDIR_14x environment variable
414 user_lyxdir = GetEnvPath("LYX_USERDIR_14x");
417 if (user_lyxdir.empty())
418 user_lyxdir = AddPath(GetEnvPath("HOME"),
419 string(".") + PACKAGE);
420 explicit_userdir = false;
423 lyxerr[Debug::INIT] << "User LyX directory: '"
424 << user_lyxdir << '\'' << endl;
426 // Check that user LyX directory is ok. We don't do that if
427 // running in batch mode.
429 queryUserLyXDir(explicit_userdir);
434 // Disable gui when easyparse says so
435 lyx_gui::use_gui = gui;
437 if (lyxrc.template_path.empty()) {
438 lyxrc.template_path = AddPath(system_lyxdir, "templates");
441 if (lyxrc.lastfiles.empty()) {
442 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
445 if (lyxrc.roman_font_name.empty())
446 lyxrc.roman_font_name = lyx_gui::roman_font_name();
447 if (lyxrc.sans_font_name.empty())
448 lyxrc.sans_font_name = lyx_gui::sans_font_name();
449 if (lyxrc.typewriter_font_name.empty())
450 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
453 // Read configuration files
456 readRcFile("lyxrc.defaults");
457 system_lyxrc = lyxrc;
458 system_formats = formats;
459 system_converters = converters;
460 system_lcolor = lcolor;
462 string prefsfile = "preferences";
463 // back compatibility to lyxs < 1.1.6
464 if (LibFileSearch(string(), prefsfile).empty())
466 if (!LibFileSearch(string(), prefsfile).empty())
467 readRcFile(prefsfile);
469 readEncodingsFile("encodings");
470 readLanguagesFile("languages");
473 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
476 // Ensure that we have really read a bind file, so that LyX is
478 lyxrc.readBindFileIfNeeded();
481 readUIFile(lyxrc.ui_file);
483 if (lyxerr.debugging(Debug::LYXRC))
486 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
487 system_tempdir = os::getTmpDir();
488 if (lyxerr.debugging(Debug::INIT)) {
489 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
492 lyxerr[Debug::INIT] << "Reading lastfiles `"
493 << lyxrc.lastfiles << "'..." << endl;
494 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
495 lyxrc.check_lastfiles,
496 lyxrc.num_lastfiles));
500 void LyX::defaultKeyBindings(kb_keymap * kbmap)
502 kbmap->bind("Right", LFUN_RIGHT);
503 kbmap->bind("Left", LFUN_LEFT);
504 kbmap->bind("Up", LFUN_UP);
505 kbmap->bind("Down", LFUN_DOWN);
507 kbmap->bind("Tab", LFUN_CELL_FORWARD);
508 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
510 kbmap->bind("Home", LFUN_HOME);
511 kbmap->bind("End", LFUN_END);
512 kbmap->bind("Prior", LFUN_PRIOR);
513 kbmap->bind("Next", LFUN_NEXT);
515 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
516 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
518 kbmap->bind("Delete", LFUN_DELETE);
519 kbmap->bind("BackSpace", LFUN_BACKSPACE);
521 // sub- and superscript -MV
522 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
523 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
525 // kbmap->bindings to enable the use of the numeric keypad
527 //kbmap->bind("KP_0", LFUN_SELFINSERT);
528 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
529 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
530 //kbmap->bind("KP_1", LFUN_SELFINSERT);
531 //kbmap->bind("KP_2", LFUN_SELFINSERT);
532 //kbmap->bind("KP_3", LFUN_SELFINSERT);
533 //kbmap->bind("KP_4", LFUN_SELFINSERT);
534 //kbmap->bind("KP_5", LFUN_SELFINSERT);
535 //kbmap->bind("KP_6", LFUN_SELFINSERT);
536 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
537 //kbmap->bind("KP_7", LFUN_SELFINSERT);
538 //kbmap->bind("KP_8", LFUN_SELFINSERT);
539 //kbmap->bind("KP_9", LFUN_SELFINSERT);
540 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
541 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
542 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
543 kbmap->bind("KP_Right", LFUN_RIGHT);
544 kbmap->bind("KP_Left", LFUN_LEFT);
545 kbmap->bind("KP_Up", LFUN_UP);
546 kbmap->bind("KP_Down", LFUN_DOWN);
547 kbmap->bind("KP_Home", LFUN_HOME);
548 kbmap->bind("KP_End", LFUN_END);
549 kbmap->bind("KP_Prior", LFUN_PRIOR);
550 kbmap->bind("KP_Next", LFUN_NEXT);
552 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
553 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
554 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
558 void LyX::emergencyCleanup()
560 // what to do about tmpfiles is non-obvious. we would
561 // like to delete any we find, but our lyxdir might
562 // contain documents etc. which might be helpful on
565 bufferlist.emergencyWriteAll();
567 lyxserver->emergencyCleanup();
571 void LyX::deadKeyBindings(kb_keymap * kbmap)
573 // bindKeyings for transparent handling of deadkeys
574 // The keysyms are gotten from XFree86 X11R6
575 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
576 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
577 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
578 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
579 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
580 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
581 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
582 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
583 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
584 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
585 // nothing with this name
586 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
587 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
588 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
589 // nothing with this name either...
590 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
591 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
592 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
593 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
597 void LyX::queryUserLyXDir(bool explicit_userdir)
599 string const configure_script = AddName(system_lyxdir, "configure");
601 // Does user directory exist?
602 FileInfo fileInfo(user_lyxdir);
603 if (fileInfo.isOK() && fileInfo.isDir()) {
605 FileInfo script(configure_script);
606 FileInfo defaults(AddName(user_lyxdir, "lyxrc.defaults"));
607 if (defaults.isOK() && script.isOK()
608 && defaults.getModificationTime() < script.getModificationTime()) {
609 lyxerr << _("LyX: reconfiguring user directory")
612 ::system(configure_script.c_str());
613 lyxerr << "LyX: " << _("Done!") << endl;
618 first_start = !explicit_userdir;
620 lyxerr << bformat(_("LyX: Creating directory %1$s"
621 " and running configure..."), user_lyxdir) << endl;
623 if (!createDirectory(user_lyxdir, 0755)) {
624 // Failed, let's use $HOME instead.
625 user_lyxdir = GetEnvPath("HOME");
626 lyxerr << bformat(_("Failed. Will use %1$s instead."),
627 user_lyxdir) << endl;
631 // Run configure in user lyx directory
633 ::system(configure_script.c_str());
634 lyxerr << "LyX: " << _("Done!") << endl;
638 void LyX::readRcFile(string const & name)
640 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
642 string const lyxrc_path = LibFileSearch(string(), name);
643 if (!lyxrc_path.empty()) {
645 lyxerr[Debug::INIT] << "Found " << name
646 << " in " << lyxrc_path << endl;
648 if (lyxrc.read(lyxrc_path) >= 0)
656 // Read the ui file `name'
657 void LyX::readUIFile(string const & name)
667 struct keyword_item uitags[ui_last - 1] = {
668 { "include", ui_include },
669 { "menuset", ui_menuset },
670 { "toolbar", ui_toolbar },
671 { "toolbars", ui_toolbars }
674 // Ensure that a file is read only once (prevents include loops)
675 static std::list<string> uifiles;
676 std::list<string>::const_iterator it = uifiles.begin();
677 std::list<string>::const_iterator end = uifiles.end();
678 it = std::find(it, end, name);
680 lyxerr[Debug::INIT] << "UI file '" << name
681 << "' has been read already. "
682 << "Is this an include loop?"
687 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
689 string const ui_path = LibFileSearch("ui", name, "ui");
691 if (ui_path.empty()) {
692 lyxerr[Debug::INIT] << "Could not find " << name << endl;
696 uifiles.push_back(name);
698 lyxerr[Debug::INIT] << "Found " << name
699 << " in " << ui_path << endl;
700 LyXLex lex(uitags, ui_last - 1);
701 lex.setFile(ui_path);
703 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
707 if (lyxerr.debugging(Debug::PARSER))
708 lex.printTable(lyxerr);
714 string const file = lex.getString();
719 menubackend.read(lex);
723 toolbarbackend.read(lex);
727 toolbarbackend.readToolbars(lex);
731 if (!rtrim(lex.getString()).empty())
732 lex.printError("LyX::ReadUIFile: "
733 "Unknown menu tag: `$$Token'");
740 // Read the languages file `name'
741 void LyX::readLanguagesFile(string const & name)
743 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
745 string const lang_path = LibFileSearch(string(), name);
746 if (lang_path.empty()) {
750 languages.read(lang_path);
754 // Read the encodings file `name'
755 void LyX::readEncodingsFile(string const & name)
757 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
759 string const enc_path = LibFileSearch(string(), name);
760 if (enc_path.empty()) {
764 encodings.read(enc_path);
773 /// return the the number of arguments consumed
774 typedef boost::function<int(string const &, string const &)> cmd_helper;
776 int parse_dbg(string const & arg, string const &)
779 lyxerr << _("List of supported debug flags:") << endl;
780 Debug::showTags(lyxerr);
783 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
785 lyxerr.level(Debug::value(arg));
786 Debug::showLevel(lyxerr, lyxerr.level());
791 int parse_help(string const &, string const &)
794 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
795 "Command line switches (case sensitive):\n"
796 "\t-help summarize LyX usage\n"
797 "\t-userdir dir try to set user directory to dir\n"
798 "\t-sysdir dir try to set system directory to dir\n"
799 "\t-geometry WxH+X+Y set geometry of the main window\n"
800 "\t-dbg feature[,feature]...\n"
801 " select the features to debug.\n"
802 " Type `lyx -dbg' to see the list of features\n"
803 "\t-x [--execute] command\n"
804 " where command is a lyx command.\n"
805 "\t-e [--export] fmt\n"
806 " where fmt is the export format of choice.\n"
807 "\t-i [--import] fmt file.xxx\n"
808 " where fmt is the import format of choice\n"
809 " and file.xxx is the file to be imported.\n"
810 "\t-version summarize version and build info\n"
811 "Check the LyX man page for more details.") << endl;
816 int parse_version(string const &, string const &)
818 lyxerr << "LyX " << lyx_version
819 << " of " << lyx_release_date << endl;
820 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
822 lyxerr << lyx_version_info << endl;
827 int parse_sysdir(string const & arg, string const &)
830 lyxerr << _("Missing directory for -sysdir switch") << endl;
837 int parse_userdir(string const & arg, string const &)
840 lyxerr << _("Missing directory for -userdir switch") << endl;
847 int parse_execute(string const & arg, string const &)
850 lyxerr << _("Missing command string after --execute switch") << endl;
854 // Argh. Setting gui to false segfaults..
855 // FIXME: when ? how ?
860 int parse_export(string const & type, string const &)
863 lyxerr << _("Missing file type [eg latex, ps...] after "
864 "--export switch") << endl;
867 batch = "buffer-export " + type;
872 int parse_import(string const & type, string const & file)
875 lyxerr << _("Missing file type [eg latex, ps...] after "
876 "--import switch") << endl;
880 lyxerr << _("Missing filename for --import") << endl;
884 batch = "buffer-import " + type + ' ' + file;
891 bool LyX::easyParse(int & argc, char * argv[])
893 std::map<string, cmd_helper> cmdmap;
895 cmdmap["-dbg"] = parse_dbg;
896 cmdmap["-help"] = parse_help;
897 cmdmap["--help"] = parse_help;
898 cmdmap["-version"] = parse_version;
899 cmdmap["--version"] = parse_version;
900 cmdmap["-sysdir"] = parse_sysdir;
901 cmdmap["-userdir"] = parse_userdir;
902 cmdmap["-x"] = parse_execute;
903 cmdmap["--execute"] = parse_execute;
904 cmdmap["-e"] = parse_export;
905 cmdmap["--export"] = parse_export;
906 cmdmap["-i"] = parse_import;
907 cmdmap["--import"] = parse_import;
909 for (int i = 1; i < argc; ++i) {
910 std::map<string, cmd_helper>::const_iterator it
911 = cmdmap.find(argv[i]);
913 // don't complain if not found - may be parsed later
914 if (it == cmdmap.end())
917 string arg((i + 1 < argc) ? argv[i + 1] : "");
918 string arg2((i + 2 < argc) ? argv[i + 2] : "");
920 int const remove = 1 + it->second(arg, arg2);
922 // Now, remove used arguments by shifting
923 // the following ones remove places down.
925 for (int j = i; j < argc; ++j)
926 argv[j] = argv[j + remove];
930 batch_command = batch;