3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
21 #include "buffer_funcs.h"
22 #include "bufferlist.h"
23 #include "converter.h"
26 #include "errorlist.h"
31 #include "lastfiles.h"
36 #include "lyxtextclasslist.h"
37 #include "lyxserver.h"
38 #include "MenuBackend.h"
40 #include "ToolbarBackend.h"
42 #include "mathed/math_inset.h"
44 #include "frontends/Alert.h"
45 #include "frontends/lyx_gui.h"
46 #include "frontends/LyXView.h"
48 #include "support/FileInfo.h"
49 #include "support/filetools.h"
50 #include "support/lyxlib.h"
51 #include "support/os.h"
52 #include "support/package.h"
53 #include "support/path.h"
55 #include <boost/bind.hpp>
60 using lyx::support::AddName;
61 using lyx::support::AddPath;
62 using lyx::support::bformat;
63 using lyx::support::createDirectory;
64 using lyx::support::createLyXTmpDir;
65 using lyx::support::FileInfo;
66 using lyx::support::FileSearch;
67 using lyx::support::GetEnv;
68 using lyx::support::i18nLibFileSearch;
69 using lyx::support::LibFileSearch;
70 using lyx::support::package;
71 using lyx::support::Path;
72 using lyx::support::QuoteName;
73 using lyx::support::rtrim;
75 namespace os = lyx::support::os;
81 #ifndef CXX_GLOBAL_CSTD
88 extern void QuitLyX();
90 extern LyXServer * lyxserver;
92 // This is the global bufferlist object
93 BufferList bufferlist;
95 // convenient to have it here.
96 boost::scoped_ptr<kb_keymap> toplevel_keymap;
100 // Filled with the command line arguments "foo" of "-sysdir foo" or
102 string cl_system_support;
103 string cl_user_support;
106 void showFileError(string const & error)
108 Alert::warning(_("Could not read configuration file"),
109 bformat(_("Error while reading the configuration file\n%1$s.\n"
110 "Please check your installation."), error));
117 boost::scoped_ptr<LyX> LyX::singleton_;
119 void LyX::exec(int & argc, char * argv[])
121 BOOST_ASSERT(!singleton_.get());
122 // We must return from this before launching the gui so that
123 // other parts of the code can access singleton_ through
124 // LyX::ref and LyX::cref.
125 singleton_.reset(new LyX);
126 // Start the real execution loop.
127 singleton_->priv_exec(argc, argv);
133 BOOST_ASSERT(singleton_.get());
134 return *singleton_.get();
138 LyX const & LyX::cref()
140 BOOST_ASSERT(singleton_.get());
141 return *singleton_.get();
150 LastFiles & LyX::lastfiles()
152 BOOST_ASSERT(lastfiles_.get());
153 return *lastfiles_.get();
157 LastFiles const & LyX::lastfiles() const
159 BOOST_ASSERT(lastfiles_.get());
160 return *lastfiles_.get();
164 void LyX::addLyXView(boost::shared_ptr<LyXView> const & lyxview)
166 views_.push_back(lyxview);
170 Buffer const * const LyX::updateInset(InsetBase const * inset) const
175 Buffer const * buffer_ptr = 0;
176 ViewList::const_iterator it = views_.begin();
177 ViewList::const_iterator const end = views_.end();
178 for (; it != end; ++it) {
179 Buffer const * ptr = (*it)->updateInset(inset);
187 void LyX::priv_exec(int & argc, char * argv[])
189 // Here we need to parse the command line. At least
190 // we need to parse for "-dbg" and "-help"
191 bool const want_gui = easyParse(argc, argv);
193 lyx::support::init_package(argv[0], cl_system_support, cl_user_support);
196 lyx_gui::parse_init(argc, argv);
198 // check for any spurious extra arguments
199 // other than documents
200 for (int argi = 1; argi < argc ; ++argi) {
201 if (argv[argi][0] == '-') {
202 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
208 // Initialization of LyX (reads lyxrc and more)
209 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
211 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
214 lyx_gui::parse_lyxrc();
218 vector<string> files;
220 for (int argi = argc - 1; argi >= 1; --argi)
221 files.push_back(argv[argi]);
224 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
226 // Execute batch commands if available
227 if (!batch_command.empty()) {
229 lyxerr[Debug::INIT] << "About to handle -x '"
230 << batch_command << '\'' << endl;
232 Buffer * last_loaded = 0;
234 vector<string>::const_iterator it = files.begin();
235 vector<string>::const_iterator end = files.end();
237 for (; it != end; ++it) {
238 // get absolute path of file and add ".lyx" to
239 // the filename if necessary
240 string s = FileSearch(string(), *it, "lyx");
242 last_loaded = newFile(*it, string(), true);
244 Buffer * buf = bufferlist.newBuffer(s, false);
245 buf->error.connect(boost::bind(&LyX::printError, this, _1));
246 if (loadLyXFile(buf, s))
249 bufferlist.release(buf);
253 // try to dispatch to last loaded buffer first
255 bool success = false;
256 if (last_loaded->dispatch(batch_command, &success)) {
261 files.clear(); // the files are already loaded
264 lyx_gui::start(batch_command, files);
270 static void error_handler(int err_sig)
272 // Throw away any signals other than the first one received.
273 static sig_atomic_t handling_error = false;
276 handling_error = true;
278 // We have received a signal indicating a fatal error, so
279 // try and save the data ASAP.
280 LyX::cref().emergencyCleanup();
282 // These lyxerr calls may or may not work:
284 // Signals are asynchronous, so the main program may be in a very
285 // fragile state when a signal is processed and thus while a signal
286 // handler function executes.
287 // In general, therefore, we should avoid performing any
288 // I/O operations or calling most library and system functions from
291 // This shouldn't matter here, however, as we've already invoked
295 lyxerr << "\nlyx: SIGHUP signal caught\nBye." << endl;
298 lyxerr << "\nlyx: SIGFPE signal caught\nBye." << endl;
301 lyxerr << "\nlyx: SIGSEGV signal caught\n"
302 "Sorry, you have found a bug in LyX. "
303 "Please read the bug-reporting instructions "
304 "in Help->Introduction and send us a bug report, "
305 "if necessary. Thanks !\nBye." << endl;
313 // Deinstall the signal handlers
314 signal(SIGHUP, SIG_DFL);
315 signal(SIGINT, SIG_DFL);
316 signal(SIGFPE, SIG_DFL);
317 signal(SIGSEGV, SIG_DFL);
318 signal(SIGTERM, SIG_DFL);
320 if (err_sig == SIGSEGV ||
321 (err_sig != SIGHUP && !GetEnv("LYXDEBUG").empty()))
322 lyx::support::abort();
329 void LyX::printError(ErrorItem const & ei)
331 std::cerr << _("LyX: ") << ei.error
332 << ':' << ei.description << std::endl;
337 void LyX::init(bool gui)
339 signal(SIGHUP, error_handler);
340 signal(SIGFPE, error_handler);
341 signal(SIGSEGV, error_handler);
342 signal(SIGINT, error_handler);
343 signal(SIGTERM, error_handler);
344 // SIGPIPE can be safely ignored.
346 #if defined (USE_MACOSX_PACKAGING)
347 // Set PATH for LyX/Mac
349 // LyX/Mac is a relocatable application bundle; here we add to
350 // the PATH so it can find binaries like reLyX inside its own
351 // application bundle, and also append PATH elements that it
352 // needs to run latex, previewers, etc.
353 string oldpath = GetEnv("PATH");
354 string newpath = "PATH=" + oldpath + ":" + package().binary_dir() + ":";
355 newpath += "/sw/bin:/usr/local/bin:"
356 "/usr/local/teTeX/bin/powerpc-apple-darwin-current";
358 lyxerr[Debug::INIT] << "Running from LyX/Mac bundle. "
359 "Setting PATH to: " << GetEnv("PATH") << endl;
362 // Set the locale_dir.
363 string const & locale_dir = package().locale_dir();
364 FileInfo fi(locale_dir);
365 if (fi.isOK() && fi.isDir()) {
367 << "Setting locale directory to "
368 << locale_dir << endl;
369 //gettext_init(locale_dir);
372 // Check that user LyX directory is ok. We don't do that if
373 // running in batch mode.
375 queryUserLyXDir(package().explicit_user_support());
380 // Disable gui when easyparse says so
381 lyx_gui::use_gui = gui;
383 lyxrc.tempdir_path = package().temp_dir();
384 lyxrc.document_path = package().document_dir();
386 if (lyxrc.template_path.empty()) {
387 lyxrc.template_path = AddPath(package().system_support(),
391 if (lyxrc.lastfiles.empty()) {
392 lyxrc.lastfiles = AddName(package().user_support(), "lastfiles");
395 if (lyxrc.roman_font_name.empty())
396 lyxrc.roman_font_name = lyx_gui::roman_font_name();
397 if (lyxrc.sans_font_name.empty())
398 lyxrc.sans_font_name = lyx_gui::sans_font_name();
399 if (lyxrc.typewriter_font_name.empty())
400 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
403 // Read configuration files
406 readRcFile("lyxrc.defaults");
407 system_lyxrc = lyxrc;
408 system_formats = formats;
409 system_converters = converters;
410 system_movers = movers;
411 system_lcolor = lcolor;
413 string prefsfile = "preferences";
414 // back compatibility to lyxs < 1.1.6
415 if (LibFileSearch(string(), prefsfile).empty())
417 if (!LibFileSearch(string(), prefsfile).empty())
418 readRcFile(prefsfile);
420 readEncodingsFile("encodings");
421 readLanguagesFile("languages");
424 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
429 toplevel_keymap.reset(new kb_keymap);
430 defaultKeyBindings(toplevel_keymap.get());
431 toplevel_keymap->read(lyxrc.bind_file);
434 readUIFile(lyxrc.ui_file);
437 if (lyxerr.debugging(Debug::LYXRC))
440 package().document_dir() = lyxrc.document_path;
442 package().temp_dir() = createLyXTmpDir(lyxrc.tempdir_path);
443 if (package().temp_dir().empty()) {
444 Alert::error(_("Could not create temporary directory"),
445 bformat(_("Could not create a temporary directory in\n"
446 "%1$s. Make sure that this\n"
447 "path exists and is writable and try again."),
448 lyxrc.tempdir_path));
449 // createLyXTmpDir() tries sufficiently hard to create a
450 // usable temp dir, so the probability to come here is
451 // close to zero. We therefore don't try to overcome this
452 // problem with e.g. asking the user for a new path and
453 // trying again but simply exit.
457 if (lyxerr.debugging(Debug::INIT)) {
458 lyxerr << "LyX tmp dir: `" << package().temp_dir() << '\'' << endl;
461 lyxerr[Debug::INIT] << "Reading lastfiles `"
462 << lyxrc.lastfiles << "'..." << endl;
463 lastfiles_.reset(new LastFiles(lyxrc.lastfiles,
464 lyxrc.check_lastfiles,
465 lyxrc.num_lastfiles));
469 void LyX::defaultKeyBindings(kb_keymap * kbmap)
471 kbmap->bind("Right", FuncRequest(LFUN_RIGHT));
472 kbmap->bind("Left", FuncRequest(LFUN_LEFT));
473 kbmap->bind("Up", FuncRequest(LFUN_UP));
474 kbmap->bind("Down", FuncRequest(LFUN_DOWN));
476 kbmap->bind("Tab", FuncRequest(LFUN_CELL_FORWARD));
477 kbmap->bind("ISO_Left_Tab", FuncRequest(LFUN_CELL_FORWARD));
479 kbmap->bind("Home", FuncRequest(LFUN_HOME));
480 kbmap->bind("End", FuncRequest(LFUN_END));
481 kbmap->bind("Prior", FuncRequest(LFUN_PRIOR));
482 kbmap->bind("Next", FuncRequest(LFUN_NEXT));
484 kbmap->bind("Return", FuncRequest(LFUN_BREAKPARAGRAPH));
485 //kbmap->bind("~C-~S-~M-nobreakspace", FuncRequest(LFUN_PROTECTEDSPACE));
487 kbmap->bind("Delete", FuncRequest(LFUN_DELETE));
488 kbmap->bind("BackSpace", FuncRequest(LFUN_BACKSPACE));
490 // kbmap->bindings to enable the use of the numeric keypad
492 //kbmap->bind("KP_0", FuncRequest(LFUN_SELFINSERT));
493 //kbmap->bind("KP_Decimal", FuncRequest(LFUN_SELFINSERT));
494 kbmap->bind("KP_Enter", FuncRequest(LFUN_BREAKPARAGRAPH));
495 //kbmap->bind("KP_1", FuncRequest(LFUN_SELFINSERT));
496 //kbmap->bind("KP_2", FuncRequest(LFUN_SELFINSERT));
497 //kbmap->bind("KP_3", FuncRequest(LFUN_SELFINSERT));
498 //kbmap->bind("KP_4", FuncRequest(LFUN_SELFINSERT));
499 //kbmap->bind("KP_5", FuncRequest(LFUN_SELFINSERT));
500 //kbmap->bind("KP_6", FuncRequest(LFUN_SELFINSERT));
501 //kbmap->bind("KP_Add", FuncRequest(LFUN_SELFINSERT));
502 //kbmap->bind("KP_7", FuncRequest(LFUN_SELFINSERT));
503 //kbmap->bind("KP_8", FuncRequest(LFUN_SELFINSERT));
504 //kbmap->bind("KP_9", FuncRequest(LFUN_SELFINSERT));
505 //kbmap->bind("KP_Divide", FuncRequest(LFUN_SELFINSERT));
506 //kbmap->bind("KP_Multiply", FuncRequest(LFUN_SELFINSERT));
507 //kbmap->bind("KP_Subtract", FuncRequest(LFUN_SELFINSERT));
508 kbmap->bind("KP_Right", FuncRequest(LFUN_RIGHT));
509 kbmap->bind("KP_Left", FuncRequest(LFUN_LEFT));
510 kbmap->bind("KP_Up", FuncRequest(LFUN_UP));
511 kbmap->bind("KP_Down", FuncRequest(LFUN_DOWN));
512 kbmap->bind("KP_Home", FuncRequest(LFUN_HOME));
513 kbmap->bind("KP_End", FuncRequest(LFUN_END));
514 kbmap->bind("KP_Prior", FuncRequest(LFUN_PRIOR));
515 kbmap->bind("KP_Next", FuncRequest(LFUN_NEXT));
517 kbmap->bind("C-Tab", FuncRequest(LFUN_CELL_SPLIT));
518 kbmap->bind("S-Tab", FuncRequest(LFUN_CELL_BACKWARD));
519 kbmap->bind("S-ISO_Left_Tab", FuncRequest(LFUN_CELL_BACKWARD));
523 void LyX::emergencyCleanup() const
525 // what to do about tmpfiles is non-obvious. we would
526 // like to delete any we find, but our lyxdir might
527 // contain documents etc. which might be helpful on
530 bufferlist.emergencyWriteAll();
532 lyxserver->emergencyCleanup();
536 void LyX::deadKeyBindings(kb_keymap * kbmap)
538 // bindKeyings for transparent handling of deadkeys
539 // The keysyms are gotten from XFree86 X11R6
540 kbmap->bind("~C-~S-~M-dead_acute", FuncRequest(LFUN_ACUTE));
541 kbmap->bind("~C-~S-~M-dead_breve", FuncRequest(LFUN_BREVE));
542 kbmap->bind("~C-~S-~M-dead_caron", FuncRequest(LFUN_CARON));
543 kbmap->bind("~C-~S-~M-dead_cedilla", FuncRequest(LFUN_CEDILLA));
544 kbmap->bind("~C-~S-~M-dead_abovering", FuncRequest(LFUN_CIRCLE));
545 kbmap->bind("~C-~S-~M-dead_circumflex", FuncRequest(LFUN_CIRCUMFLEX));
546 kbmap->bind("~C-~S-~M-dead_abovedot", FuncRequest(LFUN_DOT));
547 kbmap->bind("~C-~S-~M-dead_grave", FuncRequest(LFUN_GRAVE));
548 kbmap->bind("~C-~S-~M-dead_doubleacute", FuncRequest(LFUN_HUNG_UMLAUT));
549 kbmap->bind("~C-~S-~M-dead_macron", FuncRequest(LFUN_MACRON));
550 // nothing with this name
551 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
552 kbmap->bind("~C-~S-~M-dead_tilde", FuncRequest(LFUN_TILDE));
553 kbmap->bind("~C-~S-~M-dead_diaeresis", FuncRequest(LFUN_UMLAUT));
554 // nothing with this name either...
555 //kbmap->bind("~C-~S-~M-dead_underbar", FuncRequest(LFUN_UNDERBAR));
556 kbmap->bind("~C-~S-~M-dead_belowdot", FuncRequest(LFUN_UNDERDOT));
557 kbmap->bind("~C-~S-~M-dead_tie", FuncRequest(LFUN_TIE));
558 kbmap->bind("~C-~S-~M-dead_ogonek",FuncRequest(LFUN_OGONEK));
562 void LyX::queryUserLyXDir(bool explicit_userdir)
564 string const configure_script = AddName(package().system_support(), "configure");
565 string const configure_command = "sh " + QuoteName(configure_script);
567 // Does user directory exist?
568 FileInfo fileInfo(package().user_support());
569 if (fileInfo.isOK() && fileInfo.isDir()) {
571 FileInfo script(configure_script);
572 FileInfo defaults(AddName(package().user_support(), "lyxrc.defaults"));
573 if (defaults.isOK() && script.isOK()
574 && defaults.getModificationTime() < script.getModificationTime()) {
575 lyxerr << _("LyX: reconfiguring user directory")
577 Path p(package().user_support());
578 ::system(configure_command.c_str());
579 lyxerr << "LyX: " << _("Done!") << endl;
584 first_start = !explicit_userdir;
586 // If the user specified explicitly a directory, ask whether
587 // to create it. If the user says "no", then exit.
588 if (explicit_userdir &&
590 _("Missing LyX support directory"),
591 bformat(_("You have specified a non-existent user "
592 "LyX directory, %1$s.\n"
593 "It is needed to keep your own configuration."),
594 package().user_support()),
596 _("&Create directory."),
598 lyxerr << _("No user LyX directory. Exiting.") << endl;
602 lyxerr << bformat(_("LyX: Creating directory %1$s"
603 " and running configure..."), package().user_support()) << endl;
605 if (!createDirectory(package().user_support(), 0755)) {
606 // Failed, so let's exit.
607 lyxerr << _("Failed to create directory. Exiting.")
612 // Run configure in user lyx directory
613 Path p(package().user_support());
614 ::system(configure_command.c_str());
615 lyxerr << "LyX: " << _("Done!") << endl;
619 void LyX::readRcFile(string const & name)
621 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
623 string const lyxrc_path = LibFileSearch(string(), name);
624 if (!lyxrc_path.empty()) {
626 lyxerr[Debug::INIT] << "Found " << name
627 << " in " << lyxrc_path << endl;
629 if (lyxrc.read(lyxrc_path) >= 0)
637 // Read the ui file `name'
638 void LyX::readUIFile(string const & name)
648 struct keyword_item uitags[ui_last - 1] = {
649 { "include", ui_include },
650 { "menuset", ui_menuset },
651 { "toolbar", ui_toolbar },
652 { "toolbars", ui_toolbars }
655 // Ensure that a file is read only once (prevents include loops)
656 static std::list<string> uifiles;
657 std::list<string>::const_iterator it = uifiles.begin();
658 std::list<string>::const_iterator end = uifiles.end();
659 it = std::find(it, end, name);
661 lyxerr[Debug::INIT] << "UI file '" << name
662 << "' has been read already. "
663 << "Is this an include loop?"
668 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
670 string const ui_path = LibFileSearch("ui", name, "ui");
672 if (ui_path.empty()) {
673 lyxerr[Debug::INIT] << "Could not find " << name << endl;
677 uifiles.push_back(name);
679 lyxerr[Debug::INIT] << "Found " << name
680 << " in " << ui_path << endl;
681 LyXLex lex(uitags, ui_last - 1);
682 lex.setFile(ui_path);
684 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
688 if (lyxerr.debugging(Debug::PARSER))
689 lex.printTable(lyxerr);
695 string const file = lex.getString();
700 menubackend.read(lex);
704 toolbarbackend.read(lex);
708 toolbarbackend.readToolbars(lex);
712 if (!rtrim(lex.getString()).empty())
713 lex.printError("LyX::ReadUIFile: "
714 "Unknown menu tag: `$$Token'");
721 // Read the languages file `name'
722 void LyX::readLanguagesFile(string const & name)
724 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
726 string const lang_path = LibFileSearch(string(), name);
727 if (lang_path.empty()) {
731 languages.read(lang_path);
735 // Read the encodings file `name'
736 void LyX::readEncodingsFile(string const & name)
738 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
740 string const enc_path = LibFileSearch(string(), name);
741 if (enc_path.empty()) {
745 encodings.read(enc_path);
754 /// return the the number of arguments consumed
755 typedef boost::function<int(string const &, string const &)> cmd_helper;
757 int parse_dbg(string const & arg, string const &)
760 lyxerr << _("List of supported debug flags:") << endl;
761 Debug::showTags(lyxerr);
764 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
766 lyxerr.level(Debug::value(arg));
767 Debug::showLevel(lyxerr, lyxerr.level());
772 int parse_help(string const &, string const &)
775 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
776 "Command line switches (case sensitive):\n"
777 "\t-help summarize LyX usage\n"
778 "\t-userdir dir try to set user directory to dir\n"
779 "\t-sysdir dir try to set system directory to dir\n"
780 "\t-geometry WxH+X+Y set geometry of the main window\n"
781 "\t-dbg feature[,feature]...\n"
782 " select the features to debug.\n"
783 " Type `lyx -dbg' to see the list of features\n"
784 "\t-x [--execute] command\n"
785 " where command is a lyx command.\n"
786 "\t-e [--export] fmt\n"
787 " where fmt is the export format of choice.\n"
788 "\t-i [--import] fmt file.xxx\n"
789 " where fmt is the import format of choice\n"
790 " and file.xxx is the file to be imported.\n"
791 "\t-version summarize version and build info\n"
792 "Check the LyX man page for more details.") << endl;
797 int parse_version(string const &, string const &)
799 lyxerr << "LyX " << lyx_version
800 << " of " << lyx_release_date << endl;
801 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
803 lyxerr << lyx_version_info << endl;
808 int parse_sysdir(string const & arg, string const &)
811 lyxerr << _("Missing directory for -sysdir switch") << endl;
814 cl_system_support = arg;
818 int parse_userdir(string const & arg, string const &)
821 lyxerr << _("Missing directory for -userdir switch") << endl;
824 cl_user_support = arg;
828 int parse_execute(string const & arg, string const &)
831 lyxerr << _("Missing command string after --execute switch") << endl;
835 // Argh. Setting gui to false segfaults..
836 // FIXME: when ? how ?
841 int parse_export(string const & type, string const &)
844 lyxerr << _("Missing file type [eg latex, ps...] after "
845 "--export switch") << endl;
848 batch = "buffer-export " + type;
853 int parse_import(string const & type, string const & file)
856 lyxerr << _("Missing file type [eg latex, ps...] after "
857 "--import switch") << endl;
861 lyxerr << _("Missing filename for --import") << endl;
865 batch = "buffer-import " + type + ' ' + file;
872 bool LyX::easyParse(int & argc, char * argv[])
874 std::map<string, cmd_helper> cmdmap;
876 cmdmap["-dbg"] = parse_dbg;
877 cmdmap["-help"] = parse_help;
878 cmdmap["--help"] = parse_help;
879 cmdmap["-version"] = parse_version;
880 cmdmap["--version"] = parse_version;
881 cmdmap["-sysdir"] = parse_sysdir;
882 cmdmap["-userdir"] = parse_userdir;
883 cmdmap["-x"] = parse_execute;
884 cmdmap["--execute"] = parse_execute;
885 cmdmap["-e"] = parse_export;
886 cmdmap["--export"] = parse_export;
887 cmdmap["-i"] = parse_import;
888 cmdmap["--import"] = parse_import;
890 for (int i = 1; i < argc; ++i) {
891 std::map<string, cmd_helper>::const_iterator it
892 = cmdmap.find(argv[i]);
894 // don't complain if not found - may be parsed later
895 if (it == cmdmap.end())
898 string arg((i + 1 < argc) ? argv[i + 1] : "");
899 string arg2((i + 2 < argc) ? argv[i + 2] : "");
901 int const remove = 1 + it->second(arg, arg2);
903 // Now, remove used arguments by shifting
904 // the following ones remove places down.
906 for (int j = i; j < argc; ++j)
907 argv[j] = argv[j + remove];
911 batch_command = batch;