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 extern void LoadLyXFile(string const &);
63 extern void QuitLyX();
65 extern LyXServer * lyxserver;
73 boost::scoped_ptr<LastFiles> lastfiles;
75 // This is the global bufferlist object
76 BufferList bufferlist;
78 // convenient to have it here.
79 boost::scoped_ptr<kb_keymap> toplevel_keymap;
83 void showFileError(string const & error)
85 Alert::warning(_("Could not read configuration file"),
86 bformat(_("Error while reading the configuration file\n%1$s.\n"
87 "Please check your installation."), error));
93 LyX::LyX(int & argc, char * argv[])
95 // Here we need to parse the command line. At least
96 // we need to parse for "-dbg" and "-help"
97 bool const want_gui = easyParse(argc, argv);
99 // set the DisplayTranslator only once; should that be done here??
100 // if this should not be in this file, please also remove
101 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
102 lyx::graphics::setDisplayTranslator();
105 lyx_gui::parse_init(argc, argv);
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."),
118 // Initialization of LyX (reads lyxrc and more)
119 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
121 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
124 lyx_gui::parse_lyxrc();
127 vector<string> files;
129 for (int argi = argc - 1; argi >= 1; --argi) {
130 files.push_back(argv[argi]);
134 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
136 // Execute batch commands if available
137 if (!batch_command.empty()) {
139 lyxerr[Debug::INIT] << "About to handle -x '"
140 << batch_command << '\'' << endl;
142 Buffer * last_loaded = 0;
144 vector<string>::const_iterator it = files.begin();
145 vector<string>::const_iterator end = files.end();
147 for (; it != end; ++it) {
148 // get absolute path of file and add ".lyx" to
149 // the filename if necessary
150 string s = FileSearch(string(), *it, "lyx");
152 last_loaded = newFile(*it, "");
154 last_loaded = bufferlist.newBuffer(s, false);
155 last_loaded->error.connect(boost::bind(&LyX::printError, this, _1));
156 if (!loadLyXFile(last_loaded, s)) {
157 bufferlist.release(last_loaded);
158 last_loaded = newFile(*it, string());
163 // try to dispatch to last loaded buffer first
165 bool success = false;
166 if (last_loaded->dispatch(batch_command, &success)) {
171 files.clear(); // the files are already loaded
174 lyx_gui::start(batch_command, files);
180 static void error_handler(int err_sig)
184 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
190 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
193 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
195 "Sorry, you have found a bug in LyX. "
196 "Please read the bug-reporting instructions "
197 "in Help->Introduction and send us a bug report, "
198 "if necessary. Thanks !" << endl;
205 // Deinstall the signal handlers
206 signal(SIGHUP, SIG_DFL);
207 signal(SIGINT, SIG_DFL);
208 signal(SIGFPE, SIG_DFL);
209 signal(SIGSEGV, SIG_DFL);
210 signal(SIGTERM, SIG_DFL);
212 LyX::emergencyCleanup();
214 lyxerr << "Bye." << endl;
215 if (err_sig!= SIGHUP &&
216 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
217 lyx::support::abort();
224 void LyX::printError(ErrorItem const & ei)
226 std::cerr << _("LyX: ") << ei.error
227 << ':' << ei.description << std::endl;
232 void LyX::init(bool gui)
234 signal(SIGHUP, error_handler);
235 signal(SIGFPE, error_handler);
236 signal(SIGSEGV, error_handler);
237 signal(SIGINT, error_handler);
238 signal(SIGTERM, error_handler);
241 // Determine path of binary
244 string binpath = os::binpath();
245 string binname = os::binname();
246 string fullbinname = MakeAbsPath(binname, binpath);
248 if (binpath.empty()) {
249 lyxerr << _("Warning: could not determine path of binary.")
251 << _("If you have problems, try starting LyX with an absolute path.")
254 lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
255 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
258 // Determine system directory.
261 // Directories are searched in this order:
262 // 1) -sysdir command line parameter
263 // 2) LYX_DIR_14x environment variable
264 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
265 // 4) <path of binary>/../share/<name of binary>/
266 // 4a) repeat 4 after following the Symlink if <path of
267 // binary> is a symbolic link.
268 // 5) hardcoded lyx_dir
269 // The directory is checked for the presence of the file
270 // "chkconfig.ltx", and if that is present, the directory
271 // is accepted as the system directory.
272 // If no chkconfig.ltx file is found, a warning is given,
273 // and the hardcoded lyx_dir is used.
275 // If we had a command line switch, system_lyxdir is already set
277 if (!system_lyxdir.empty())
278 searchpath = MakeAbsPath(system_lyxdir) + ';';
280 string const lyxdir = GetEnvPath("LYX_DIR_14x");
282 if (!lyxdir.empty()) {
283 lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
284 searchpath += lyxdir + ';';
287 string fullbinpath = binpath;
288 FileInfo file(fullbinname, true);
290 lyxerr[Debug::INIT] << "binary is a link" << endl;
292 if (LyXReadLink(fullbinname, link, true)) {
293 // Path of binary/../share/name of binary/
294 searchpath += NormalizePath(AddPath(binpath,
296 + OnlyFilename(binname));
299 binpath = MakeAbsPath(OnlyPath(fullbinpath));
305 // Path of binary/../share/name of binary/
306 searchpath += NormalizePath(AddPath(binpath, "../share/") +
307 OnlyFilename(binname)) + ';';
310 FileInfo file(fullbinpath, true);
311 followlink = file.isLink();
313 lyxerr[Debug::INIT] << " directory " << fullbinpath
314 << " is a link" << endl;
316 if (LyXReadLink(fullbinpath, link, true)) {
318 binpath = MakeAbsPath(OnlyPath(fullbinpath));
324 } while (followlink);
326 // <path of binary>/TOP_SRCDIR/lib
327 build_lyxdir = MakeAbsPath("../lib", binpath);
328 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
329 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
331 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
332 "place... yes" << endl;
335 << "Checking whether LyX is run in place... no"
337 build_lyxdir.erase();
341 searchpath += LYX_DIR;
343 lyxerr[Debug::INIT] << "System directory search path: "
344 << searchpath << endl;
346 string const filename = "chkconfig.ltx";
347 string const temp = FileOpenSearch(searchpath, filename, string());
348 system_lyxdir = OnlyPath(temp);
350 // Reduce "path/../path" stuff out of system directory
351 system_lyxdir = NormalizePath(system_lyxdir);
353 bool path_shown = false;
355 // Warn if environment variable is set, but unusable
356 if (!lyxdir.empty()) {
357 if (system_lyxdir != NormalizePath(lyxdir)) {
358 lyxerr <<_("LYX_DIR_14x environment variable no good.")
360 << _("System directory set to: ")
361 << system_lyxdir << endl;
366 // Warn the user if we couldn't find "chkconfig.ltx"
367 if (system_lyxdir == "./") {
368 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
369 <<_("Try the '-sysdir' command line parameter or ")
370 <<_("set the environment variable LYX_DIR_14x to the "
371 "LyX system directory ")
372 << _("containing the file `chkconfig.ltx'.") << endl;
374 FileInfo fi(LYX_DIR);
376 lyxerr << "Couldn't even find the default LYX_DIR." << endl
377 << "Giving up." << endl;
380 lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
383 lyxerr << _("Expect problems.") << endl;
385 system_lyxdir = LYX_DIR;
390 lyxerr[Debug::INIT] << "System directory: '"
391 << system_lyxdir << '\'' << endl;
394 // Determine user lyx-dir
397 // Directories are searched in this order:
398 // 1) -userdir command line parameter
399 // 2) LYX_USERDIR_14x environment variable
400 // 3) $HOME/.<name of binary>
402 // If we had a command line switch, user_lyxdir is already set
403 bool explicit_userdir = true;
404 if (user_lyxdir.empty()) {
406 // LYX_USERDIR_14x environment variable
407 user_lyxdir = GetEnvPath("LYX_USERDIR_14x");
410 if (user_lyxdir.empty())
411 user_lyxdir = AddPath(GetEnvPath("HOME"),
412 string(".") + PACKAGE);
413 explicit_userdir = false;
416 lyxerr[Debug::INIT] << "User LyX directory: '"
417 << user_lyxdir << '\'' << endl;
419 // Check that user LyX directory is ok. We don't do that if
420 // running in batch mode.
422 queryUserLyXDir(explicit_userdir);
427 // Disable gui when easyparse says so
428 lyx_gui::use_gui = gui;
430 if (lyxrc.template_path.empty()) {
431 lyxrc.template_path = AddPath(system_lyxdir, "templates");
434 if (lyxrc.lastfiles.empty()) {
435 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
438 if (lyxrc.roman_font_name.empty())
439 lyxrc.roman_font_name = lyx_gui::roman_font_name();
440 if (lyxrc.sans_font_name.empty())
441 lyxrc.sans_font_name = lyx_gui::sans_font_name();
442 if (lyxrc.typewriter_font_name.empty())
443 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
446 // Read configuration files
449 readRcFile("lyxrc.defaults");
450 system_lyxrc = lyxrc;
451 system_formats = formats;
452 system_converters = converters;
453 system_lcolor = lcolor;
455 string prefsfile = "preferences";
456 // back compatibility to lyxs < 1.1.6
457 if (LibFileSearch(string(), prefsfile).empty())
459 if (!LibFileSearch(string(), prefsfile).empty())
460 readRcFile(prefsfile);
462 readEncodingsFile("encodings");
463 readLanguagesFile("languages");
466 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
471 toplevel_keymap.reset(new kb_keymap);
472 defaultKeyBindings(toplevel_keymap.get());
473 toplevel_keymap->read(lyxrc.bind_file);
476 readUIFile(lyxrc.ui_file);
479 if (lyxerr.debugging(Debug::LYXRC))
482 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
483 if (lyxerr.debugging(Debug::INIT)) {
484 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
487 lyxerr[Debug::INIT] << "Reading lastfiles `"
488 << lyxrc.lastfiles << "'..." << endl;
489 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
490 lyxrc.check_lastfiles,
491 lyxrc.num_lastfiles));
495 void LyX::defaultKeyBindings(kb_keymap * kbmap)
497 kbmap->bind("Right", LFUN_RIGHT);
498 kbmap->bind("Left", LFUN_LEFT);
499 kbmap->bind("Up", LFUN_UP);
500 kbmap->bind("Down", LFUN_DOWN);
502 kbmap->bind("Tab", LFUN_CELL_FORWARD);
503 kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
505 kbmap->bind("Home", LFUN_HOME);
506 kbmap->bind("End", LFUN_END);
507 kbmap->bind("Prior", LFUN_PRIOR);
508 kbmap->bind("Next", LFUN_NEXT);
510 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
511 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
513 kbmap->bind("Delete", LFUN_DELETE);
514 kbmap->bind("BackSpace", LFUN_BACKSPACE);
516 // sub- and superscript -MV
517 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
518 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
520 // kbmap->bindings to enable the use of the numeric keypad
522 //kbmap->bind("KP_0", LFUN_SELFINSERT);
523 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
524 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
525 //kbmap->bind("KP_1", LFUN_SELFINSERT);
526 //kbmap->bind("KP_2", LFUN_SELFINSERT);
527 //kbmap->bind("KP_3", LFUN_SELFINSERT);
528 //kbmap->bind("KP_4", LFUN_SELFINSERT);
529 //kbmap->bind("KP_5", LFUN_SELFINSERT);
530 //kbmap->bind("KP_6", LFUN_SELFINSERT);
531 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
532 //kbmap->bind("KP_7", LFUN_SELFINSERT);
533 //kbmap->bind("KP_8", LFUN_SELFINSERT);
534 //kbmap->bind("KP_9", LFUN_SELFINSERT);
535 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
536 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
537 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
538 kbmap->bind("KP_Right", LFUN_RIGHT);
539 kbmap->bind("KP_Left", LFUN_LEFT);
540 kbmap->bind("KP_Up", LFUN_UP);
541 kbmap->bind("KP_Down", LFUN_DOWN);
542 kbmap->bind("KP_Home", LFUN_HOME);
543 kbmap->bind("KP_End", LFUN_END);
544 kbmap->bind("KP_Prior", LFUN_PRIOR);
545 kbmap->bind("KP_Next", LFUN_NEXT);
547 kbmap->bind("C-Tab", LFUN_CELL_SPLIT); // ale970515
548 kbmap->bind("S-Tab", LFUN_CELL_BACKWARD); // jug20000522
549 kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
553 void LyX::emergencyCleanup()
555 // what to do about tmpfiles is non-obvious. we would
556 // like to delete any we find, but our lyxdir might
557 // contain documents etc. which might be helpful on
560 bufferlist.emergencyWriteAll();
562 lyxserver->emergencyCleanup();
566 void LyX::deadKeyBindings(kb_keymap * kbmap)
568 // bindKeyings for transparent handling of deadkeys
569 // The keysyms are gotten from XFree86 X11R6
570 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
571 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
572 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
573 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
574 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
575 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
576 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
577 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
578 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
579 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
580 // nothing with this name
581 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
582 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
583 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
584 // nothing with this name either...
585 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
586 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
587 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
588 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
592 void LyX::queryUserLyXDir(bool explicit_userdir)
594 string const configure_script = AddName(system_lyxdir, "configure");
596 // Does user directory exist?
597 FileInfo fileInfo(user_lyxdir);
598 if (fileInfo.isOK() && fileInfo.isDir()) {
600 FileInfo script(configure_script);
601 FileInfo defaults(AddName(user_lyxdir, "lyxrc.defaults"));
602 if (defaults.isOK() && script.isOK()
603 && defaults.getModificationTime() < script.getModificationTime()) {
604 lyxerr << _("LyX: reconfiguring user directory")
607 ::system(configure_script.c_str());
608 lyxerr << "LyX: " << _("Done!") << endl;
613 first_start = !explicit_userdir;
615 lyxerr << bformat(_("LyX: Creating directory %1$s"
616 " and running configure..."), user_lyxdir) << endl;
618 if (!createDirectory(user_lyxdir, 0755)) {
619 // Failed, let's use $HOME instead.
620 user_lyxdir = GetEnvPath("HOME");
621 lyxerr << bformat(_("Failed. Will use %1$s instead."),
622 user_lyxdir) << endl;
626 // Run configure in user lyx directory
628 ::system(configure_script.c_str());
629 lyxerr << "LyX: " << _("Done!") << endl;
633 void LyX::readRcFile(string const & name)
635 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
637 string const lyxrc_path = LibFileSearch(string(), name);
638 if (!lyxrc_path.empty()) {
640 lyxerr[Debug::INIT] << "Found " << name
641 << " in " << lyxrc_path << endl;
643 if (lyxrc.read(lyxrc_path) >= 0)
651 // Read the ui file `name'
652 void LyX::readUIFile(string const & name)
662 struct keyword_item uitags[ui_last - 1] = {
663 { "include", ui_include },
664 { "menuset", ui_menuset },
665 { "toolbar", ui_toolbar },
666 { "toolbars", ui_toolbars }
669 // Ensure that a file is read only once (prevents include loops)
670 static std::list<string> uifiles;
671 std::list<string>::const_iterator it = uifiles.begin();
672 std::list<string>::const_iterator end = uifiles.end();
673 it = std::find(it, end, name);
675 lyxerr[Debug::INIT] << "UI file '" << name
676 << "' has been read already. "
677 << "Is this an include loop?"
682 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
684 string const ui_path = LibFileSearch("ui", name, "ui");
686 if (ui_path.empty()) {
687 lyxerr[Debug::INIT] << "Could not find " << name << endl;
691 uifiles.push_back(name);
693 lyxerr[Debug::INIT] << "Found " << name
694 << " in " << ui_path << endl;
695 LyXLex lex(uitags, ui_last - 1);
696 lex.setFile(ui_path);
698 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
702 if (lyxerr.debugging(Debug::PARSER))
703 lex.printTable(lyxerr);
709 string const file = lex.getString();
714 menubackend.read(lex);
718 toolbarbackend.read(lex);
722 toolbarbackend.readToolbars(lex);
726 if (!rtrim(lex.getString()).empty())
727 lex.printError("LyX::ReadUIFile: "
728 "Unknown menu tag: `$$Token'");
735 // Read the languages file `name'
736 void LyX::readLanguagesFile(string const & name)
738 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
740 string const lang_path = LibFileSearch(string(), name);
741 if (lang_path.empty()) {
745 languages.read(lang_path);
749 // Read the encodings file `name'
750 void LyX::readEncodingsFile(string const & name)
752 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
754 string const enc_path = LibFileSearch(string(), name);
755 if (enc_path.empty()) {
759 encodings.read(enc_path);
768 /// return the the number of arguments consumed
769 typedef boost::function<int(string const &, string const &)> cmd_helper;
771 int parse_dbg(string const & arg, string const &)
774 lyxerr << _("List of supported debug flags:") << endl;
775 Debug::showTags(lyxerr);
778 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
780 lyxerr.level(Debug::value(arg));
781 Debug::showLevel(lyxerr, lyxerr.level());
786 int parse_help(string const &, string const &)
789 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
790 "Command line switches (case sensitive):\n"
791 "\t-help summarize LyX usage\n"
792 "\t-userdir dir try to set user directory to dir\n"
793 "\t-sysdir dir try to set system directory to dir\n"
794 "\t-geometry WxH+X+Y set geometry of the main window\n"
795 "\t-dbg feature[,feature]...\n"
796 " select the features to debug.\n"
797 " Type `lyx -dbg' to see the list of features\n"
798 "\t-x [--execute] command\n"
799 " where command is a lyx command.\n"
800 "\t-e [--export] fmt\n"
801 " where fmt is the export format of choice.\n"
802 "\t-i [--import] fmt file.xxx\n"
803 " where fmt is the import format of choice\n"
804 " and file.xxx is the file to be imported.\n"
805 "\t-version summarize version and build info\n"
806 "Check the LyX man page for more details.") << endl;
811 int parse_version(string const &, string const &)
813 lyxerr << "LyX " << lyx_version
814 << " of " << lyx_release_date << endl;
815 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
817 lyxerr << lyx_version_info << endl;
822 int parse_sysdir(string const & arg, string const &)
825 lyxerr << _("Missing directory for -sysdir switch") << endl;
832 int parse_userdir(string const & arg, string const &)
835 lyxerr << _("Missing directory for -userdir switch") << endl;
842 int parse_execute(string const & arg, string const &)
845 lyxerr << _("Missing command string after --execute switch") << endl;
849 // Argh. Setting gui to false segfaults..
850 // FIXME: when ? how ?
855 int parse_export(string const & type, string const &)
858 lyxerr << _("Missing file type [eg latex, ps...] after "
859 "--export switch") << endl;
862 batch = "buffer-export " + type;
867 int parse_import(string const & type, string const & file)
870 lyxerr << _("Missing file type [eg latex, ps...] after "
871 "--import switch") << endl;
875 lyxerr << _("Missing filename for --import") << endl;
879 batch = "buffer-import " + type + ' ' + file;
886 bool LyX::easyParse(int & argc, char * argv[])
888 std::map<string, cmd_helper> cmdmap;
890 cmdmap["-dbg"] = parse_dbg;
891 cmdmap["-help"] = parse_help;
892 cmdmap["--help"] = parse_help;
893 cmdmap["-version"] = parse_version;
894 cmdmap["--version"] = parse_version;
895 cmdmap["-sysdir"] = parse_sysdir;
896 cmdmap["-userdir"] = parse_userdir;
897 cmdmap["-x"] = parse_execute;
898 cmdmap["--execute"] = parse_execute;
899 cmdmap["-e"] = parse_export;
900 cmdmap["--export"] = parse_export;
901 cmdmap["-i"] = parse_import;
902 cmdmap["--import"] = parse_import;
904 for (int i = 1; i < argc; ++i) {
905 std::map<string, cmd_helper>::const_iterator it
906 = cmdmap.find(argv[i]);
908 // don't complain if not found - may be parsed later
909 if (it == cmdmap.end())
912 string arg((i + 1 < argc) ? argv[i + 1] : "");
913 string arg2((i + 2 < argc) ? argv[i + 2] : "");
915 int const remove = 1 + it->second(arg, arg2);
917 // Now, remove used arguments by shifting
918 // the following ones remove places down.
920 for (int j = i; j < argc; ++j)
921 argv[j] = argv[j + remove];
925 batch_command = batch;