3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
13 #pragma implementation
18 #include "support/filetools.h"
19 #include "support/lyxlib.h"
20 #include "support/os.h"
21 #include "support/FileInfo.h"
22 #include "support/path.h"
27 #include "graphics/GraphicsTypes.h"
29 #include "bufferlist.h"
31 #include "lyxserver.h"
34 #include "ToolbarDefaults.h"
35 #include "MenuBackend.h"
37 #include "lastfiles.h"
39 #include "converter.h"
40 #include "lyxtextclasslist.h"
42 #include "frontends/Alert.h"
43 #include "frontends/lyx_gui.h"
45 #include "BoostFormat.h"
46 #include <boost/function.hpp>
54 #ifndef CXX_GLOBAL_CSTD
60 extern void LoadLyXFile(string const &);
61 extern void QuitLyX();
63 extern LyXServer * lyxserver;
67 string system_tempdir;
72 boost::scoped_ptr<LastFiles> lastfiles;
74 // This is the global bufferlist object
75 BufferList bufferlist;
77 // convenient to have it here.
78 boost::scoped_ptr<kb_keymap> toplevel_keymap;
81 LyX::LyX(int & argc, char * argv[])
83 // Here we need to parse the command line. At least
84 // we need to parse for "-dbg" and "-help"
85 bool const want_gui = easyParse(argc, argv);
87 // Global bindings (this must be done as early as possible.) (Lgb)
88 toplevel_keymap.reset(new kb_keymap);
89 defaultKeyBindings(toplevel_keymap.get());
91 // set the DisplayTranslator only once; should that be done here??
92 // if this should not be in this file, please also remove
93 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
94 grfx::setDisplayTranslator();
97 lyx_gui::parse_init(argc, argv);
100 // check for any spurious extra arguments
101 // other than documents
102 for (int argi = 1; argi < argc ; ++argi) {
103 if (argv[argi][0] == '-') {
104 lyxerr << boost::format(_("Wrong command line option `%1$s'. Exiting.")) % argv[argi] << endl;
109 // Initialization of LyX (reads lyxrc and more)
110 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
112 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
115 lyx_gui::parse_lyxrc();
118 vector<string> files;
120 for (int argi = argc - 1; argi >= 1; --argi) {
121 files.push_back(argv[argi]);
125 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
128 // Execute batch commands if available
129 if (!batch_command.empty()) {
130 lyxerr[Debug::INIT] << "About to handle -x '"
131 << batch_command << "'" << endl;
133 Buffer * last_loaded = 0;
135 vector<string>::iterator it = files.begin();
136 vector<string>::iterator end = files.end();
137 for (; it != end; ++it) {
138 last_loaded = bufferlist.loadLyXFile(*it);
143 // no buffer loaded, create one
145 last_loaded = bufferlist.newFile("tmpfile", string());
147 bool success = false;
149 // try to dispatch to last loaded buffer first
150 bool const dispatched = last_loaded->dispatch(batch_command, &success);
158 lyx_gui::start(batch_command, files);
164 static void error_handler(int err_sig)
168 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
174 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
177 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
179 "Sorry, you have found a bug in LyX. "
180 "Please read the bug-reporting instructions "
181 "in Help->Introduction and send us a bug report, "
182 "if necessary. Thanks !" << endl;
189 // Deinstall the signal handlers
190 signal(SIGHUP, SIG_DFL);
191 signal(SIGINT, SIG_DFL);
192 signal(SIGFPE, SIG_DFL);
193 signal(SIGSEGV, SIG_DFL);
194 signal(SIGTERM, SIG_DFL);
196 LyX::emergencyCleanup();
198 lyxerr << "Bye." << endl;
199 if (err_sig!= SIGHUP &&
200 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
208 void LyX::init(bool gui)
210 signal(SIGHUP, error_handler);
211 signal(SIGFPE, error_handler);
212 signal(SIGSEGV, error_handler);
213 signal(SIGINT, error_handler);
214 signal(SIGTERM, error_handler);
217 // Determine path of binary
220 string binpath = os::binpath();
221 string binname = os::binname();
222 string fullbinname = MakeAbsPath(binname, binpath);
224 if (binpath.empty()) {
225 lyxerr << _("Warning: could not determine path of binary.")
227 << _("If you have problems, try starting LyX with an absolute path.")
230 lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
231 lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
234 // Determine system directory.
237 // Directories are searched in this order:
238 // 1) -sysdir command line parameter
239 // 2) LYX_DIR_13x environment variable
240 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
241 // 4) <path of binary>/../share/<name of binary>/
242 // 4a) repeat 4 after following the Symlink if <path of
243 // binary> is a symbolic link.
244 // 5) hardcoded lyx_dir
245 // The directory is checked for the presence of the file
246 // "chkconfig.ltx", and if that is present, the directory
247 // is accepted as the system directory.
248 // If no chkconfig.ltx file is found, a warning is given,
249 // and the hardcoded lyx_dir is used.
251 // If we had a command line switch, system_lyxdir is already set
253 if (!system_lyxdir.empty())
254 searchpath = MakeAbsPath(system_lyxdir) + ';';
256 string const lyxdir = GetEnvPath("LYX_DIR_13x");
258 if (!lyxdir.empty()) {
259 lyxerr[Debug::INIT] << "LYX_DIR_13x: " << lyxdir << endl;
260 searchpath += lyxdir + ';';
263 string fullbinpath = binpath;
264 FileInfo file(fullbinname, true);
266 lyxerr[Debug::INIT] << "binary is a link" << endl;
268 if (LyXReadLink(fullbinname, link, true)) {
269 // Path of binary/../share/name of binary/
270 searchpath += NormalizePath(AddPath(binpath,
272 + OnlyFilename(binname));
275 binpath = MakeAbsPath(OnlyPath(fullbinpath));
281 // Path of binary/../share/name of binary/
282 searchpath += NormalizePath(AddPath(binpath, "../share/") +
283 OnlyFilename(binname)) + ';';
286 FileInfo file(fullbinpath, true);
287 followlink = file.isLink();
289 lyxerr << " directory " << fullbinpath
290 << " is a link" << endl;
292 if (LyXReadLink(fullbinpath, link, true)) {
294 binpath = MakeAbsPath(OnlyPath(fullbinpath));
300 } while (followlink);
302 // <path of binary>/TOP_SRCDIR/lib
303 build_lyxdir = MakeAbsPath("../lib", binpath);
304 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
305 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
307 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
308 "place... yes" << endl;
311 << "Checking whether LyX is run in place... no"
313 build_lyxdir.erase();
317 searchpath += LYX_DIR;
319 lyxerr[Debug::INIT] << "System directory search path: "
320 << searchpath << endl;
322 string const filename = "chkconfig.ltx";
323 string const temp = FileOpenSearch(searchpath, filename, string());
324 system_lyxdir = OnlyPath(temp);
326 // Reduce "path/../path" stuff out of system directory
327 system_lyxdir = NormalizePath(system_lyxdir);
329 bool path_shown = false;
331 // Warn if environment variable is set, but unusable
332 if (!lyxdir.empty()) {
333 if (system_lyxdir != NormalizePath(lyxdir)) {
334 lyxerr <<_("LYX_DIR_13x environment variable no good.")
336 << _("System directory set to: ")
337 << system_lyxdir << endl;
342 // Warn the user if we couldn't find "chkconfig.ltx"
343 if (system_lyxdir == "./") {
344 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
345 <<_("Try the '-sysdir' command line parameter or ")
346 <<_("set the environment variable LYX_DIR_13x to the "
347 "LyX system directory ")
348 << _("containing the file `chkconfig.ltx'.") << endl;
350 FileInfo fi(LYX_DIR);
352 lyxerr << "Couldn't even find the default LYX_DIR." << endl
353 << "Giving up." << endl;
356 lyxerr << boost::format(_("Using built-in default %1$s but expect problems.")) % LYX_DIR
359 lyxerr << _("Expect problems.") << endl;
361 system_lyxdir = LYX_DIR;
366 lyxerr[Debug::INIT] << "System directory: '"
367 << system_lyxdir << '\'' << endl;
370 // Determine user lyx-dir
373 // Directories are searched in this order:
374 // 1) -userdir command line parameter
375 // 2) LYX_USERDIR_13x environment variable
376 // 3) $HOME/.<name of binary>
378 // If we had a command line switch, user_lyxdir is already set
379 bool explicit_userdir = true;
380 if (user_lyxdir.empty()) {
382 // LYX_USERDIR_13x environment variable
383 user_lyxdir = GetEnvPath("LYX_USERDIR_13x");
386 if (user_lyxdir.empty())
387 user_lyxdir = AddPath(GetEnvPath("HOME"),
388 string(".") + PACKAGE);
389 explicit_userdir = false;
392 lyxerr[Debug::INIT] << "User LyX directory: '"
393 << user_lyxdir << '\'' << endl;
395 // Check that user LyX directory is ok. We don't do that if
396 // running in batch mode.
398 queryUserLyXDir(explicit_userdir);
404 // Shine up lyxrc defaults
407 // Default template path: system_dir/templates
408 if (lyxrc.template_path.empty()) {
409 lyxrc.template_path = AddPath(system_lyxdir, "templates");
412 // Default lastfiles file: $HOME/.lyx/lastfiles
413 if (lyxrc.lastfiles.empty()) {
414 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
417 // Disable gui when either lyxrc or easyparse says so
419 lyxrc.use_gui = false;
422 // Read configuration files
425 readRcFile("lyxrc.defaults");
426 system_lyxrc = lyxrc;
427 system_formats = formats;
428 system_converters = converters;
429 system_lcolor = lcolor;
431 // If there is a preferences file we read that instead
432 // of the old lyxrc file.
433 if (!readRcFile("preferences"))
436 readEncodingsFile("encodings");
437 readLanguagesFile("languages");
440 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
443 // Ensure that we have really read a bind file, so that LyX is
445 lyxrc.readBindFileIfNeeded();
448 readUIFile(lyxrc.ui_file);
450 // Bind the X dead keys to the corresponding LyX functions if
452 if (lyxrc.override_x_deadkeys)
453 deadKeyBindings(toplevel_keymap.get());
455 if (lyxerr.debugging(Debug::LYXRC)) {
459 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
460 system_tempdir = os::getTmpDir();
461 if (lyxerr.debugging(Debug::INIT)) {
462 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
465 lyxerr[Debug::INIT] << "Reading lastfiles `"
466 << lyxrc.lastfiles << "'..." << endl;
467 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
468 lyxrc.check_lastfiles,
469 lyxrc.num_lastfiles));
473 void LyX::defaultKeyBindings(kb_keymap * kbmap)
475 kbmap->bind("Right", LFUN_RIGHT);
476 kbmap->bind("Left", LFUN_LEFT);
477 kbmap->bind("Up", LFUN_UP);
478 kbmap->bind("Down", LFUN_DOWN);
480 kbmap->bind("Tab", LFUN_TAB);
481 kbmap->bind("ISO_Left_Tab", LFUN_TAB); // jbl 2001-23-02
483 kbmap->bind("Home", LFUN_HOME);
484 kbmap->bind("End", LFUN_END);
485 kbmap->bind("Prior", LFUN_PRIOR);
486 kbmap->bind("Next", LFUN_NEXT);
488 kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
489 //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
491 kbmap->bind("Delete", LFUN_DELETE);
492 kbmap->bind("BackSpace", LFUN_BACKSPACE);
494 // sub- and superscript -MV
495 kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
496 kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
498 // kbmap->bindings to enable the use of the numeric keypad
500 //kbmap->bind("KP_0", LFUN_SELFINSERT);
501 //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
502 kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
503 //kbmap->bind("KP_1", LFUN_SELFINSERT);
504 //kbmap->bind("KP_2", LFUN_SELFINSERT);
505 //kbmap->bind("KP_3", LFUN_SELFINSERT);
506 //kbmap->bind("KP_4", LFUN_SELFINSERT);
507 //kbmap->bind("KP_5", LFUN_SELFINSERT);
508 //kbmap->bind("KP_6", LFUN_SELFINSERT);
509 //kbmap->bind("KP_Add", LFUN_SELFINSERT);
510 //kbmap->bind("KP_7", LFUN_SELFINSERT);
511 //kbmap->bind("KP_8", LFUN_SELFINSERT);
512 //kbmap->bind("KP_9", LFUN_SELFINSERT);
513 //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
514 //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
515 //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
516 kbmap->bind("KP_Right", LFUN_RIGHT);
517 kbmap->bind("KP_Left", LFUN_LEFT);
518 kbmap->bind("KP_Up", LFUN_UP);
519 kbmap->bind("KP_Down", LFUN_DOWN);
520 kbmap->bind("KP_Home", LFUN_HOME);
521 kbmap->bind("KP_End", LFUN_END);
522 kbmap->bind("KP_Prior", LFUN_PRIOR);
523 kbmap->bind("KP_Next", LFUN_NEXT);
525 kbmap->bind("C-Tab", LFUN_TABINSERT); // ale970515
526 kbmap->bind("S-Tab", LFUN_SHIFT_TAB); // jug20000522
527 kbmap->bind("S-ISO_Left_Tab", LFUN_SHIFT_TAB); // jbl 2001-23-02
531 void LyX::emergencyCleanup()
533 // what to do about tmpfiles is non-obvious. we would
534 // like to delete any we find, but our lyxdir might
535 // contain documents etc. which might be helpful on
538 bufferlist.emergencyWriteAll();
540 lyxserver->emergencyCleanup();
544 void LyX::deadKeyBindings(kb_keymap * kbmap)
546 // bindKeyings for transparent handling of deadkeys
547 // The keysyms are gotten from XFree86 X11R6
548 kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
549 kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
550 kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
551 kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
552 kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
553 kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
554 kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
555 kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
556 kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
557 kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
558 // nothing with this name
559 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
560 kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
561 kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
562 // nothing with this name either...
563 //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
564 kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
565 kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
566 kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
570 void LyX::queryUserLyXDir(bool explicit_userdir)
572 string const configure_script = AddName(system_lyxdir, "configure");
574 // Does user directory exist?
575 FileInfo fileInfo(user_lyxdir);
576 if (fileInfo.isOK() && fileInfo.isDir()) {
578 FileInfo script(configure_script);
579 FileInfo defaults(AddName(user_lyxdir, "lyxrc.defaults"));
580 if (defaults.isOK() && script.isOK()
581 && defaults.getModificationTime() < script.getModificationTime()) {
582 lyxerr << _("LyX: reconfiguring user directory")
585 ::system(configure_script.c_str());
586 lyxerr << "LyX: " << _("Done!") << endl;
591 first_start = !explicit_userdir;
593 // If the user specified explicitly a directory, ask whether
594 // to create it (otherwise, always create it)
595 if (explicit_userdir &&
596 !Alert::askQuestion(_("You have specified an invalid LyX directory."),
597 _("It is needed to keep your own configuration."),
598 _("Should I try to set it up for you (recommended)?"))) {
599 lyxerr << _("Running without personal LyX directory.") << endl;
600 // No, let's use $HOME instead.
601 user_lyxdir = GetEnvPath("HOME");
605 lyxerr << boost::format(_("LyX: Creating directory %1$s and running configure...")) % user_lyxdir << endl;
607 if (!createDirectory(user_lyxdir, 0755)) {
608 // Failed, let's use $HOME instead.
609 user_lyxdir = GetEnvPath("HOME");
610 lyxerr << boost::format(_("Failed. Will use %1$s instead.")) % user_lyxdir
615 // Run configure in user lyx directory
617 ::system(configure_script.c_str());
618 lyxerr << "LyX: " << _("Done!") << endl;
622 bool LyX::readRcFile(string const & name)
624 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
626 string const lyxrc_path = LibFileSearch(string(), name);
627 if (!lyxrc_path.empty()) {
628 lyxerr[Debug::INIT] << "Found " << name
629 << " in " << lyxrc_path << endl;
630 if (lyxrc.read(lyxrc_path) < 0) {
631 Alert::alert(_("LyX Warning!"),
632 boost::io::str(boost::format(_("Error while reading %1$s.")) % lyxrc_path),
633 _("Using built-in defaults."));
638 lyxerr[Debug::INIT] << "Could not find " << name << endl;
645 // Read the ui file `name'
646 void LyX::readUIFile(string const & name)
654 struct keyword_item uitags[ui_last - 1] = {
655 { "menuset", ui_menuset },
656 { "toolbar", ui_toolbar }
659 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
661 string const ui_path = LibFileSearch("ui", name, "ui");
663 if (ui_path.empty()) {
664 lyxerr[Debug::INIT] << "Could not find " << name << endl;
665 menubackend.defaults();
669 lyxerr[Debug::INIT] << "Found " << name
670 << " in " << ui_path << endl;
671 LyXLex lex(uitags, ui_last - 1);
672 lex.setFile(ui_path);
674 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
678 if (lyxerr.debugging(Debug::PARSER))
679 lex.printTable(lyxerr);
684 menubackend.read(lex);
688 toolbardefaults.read(lex);
692 if (!rtrim(lex.getString()).empty())
693 lex.printError("LyX::ReadUIFile: "
694 "Unknown menu tag: `$$Token'");
701 // Read the languages file `name'
702 void LyX::readLanguagesFile(string const & name)
704 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
706 string const lang_path = LibFileSearch(string(), name);
707 if (lang_path.empty()) {
708 lyxerr[Debug::INIT] << "Could not find " << name << endl;
709 languages.setDefaults();
712 languages.read(lang_path);
716 // Read the encodings file `name'
717 void LyX::readEncodingsFile(string const & name)
719 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
721 string const enc_path = LibFileSearch(string(), name);
722 if (enc_path.empty()) {
723 lyxerr[Debug::INIT] << "Could not find " << name << endl;
726 encodings.read(enc_path);
735 /// return the the number of arguments consumed
736 typedef boost::function<int(string const &, string const &)> cmd_helper;
738 int parse_dbg(string const & arg, string const &)
741 lyxerr << _("List of supported debug flags:") << endl;
742 Debug::showTags(lyxerr);
745 lyxerr << boost::format(_("Setting debug level to %1$s")) % arg << endl;
746 lyxerr.level(Debug::value(arg));
747 Debug::showLevel(lyxerr, lyxerr.level());
751 int parse_help(string const &, string const &)
754 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
755 "Command line switches (case sensitive):\n"
756 "\t-help summarize LyX usage\n"
757 "\t-userdir dir try to set user directory to dir\n"
758 "\t-sysdir dir try to set system directory to dir\n"
759 "\t-geometry WxH+X+Y set geometry of the main window\n"
760 "\t-dbg feature[,feature]...\n"
761 " select the features to debug.\n"
762 " Type `lyx -dbg' to see the list of features\n"
763 "\t-x [--execute] command\n"
764 " where command is a lyx command.\n"
765 "\t-e [--export] fmt\n"
766 " where fmt is the export format of choice.\n"
767 "\t-i [--import] fmt file.xxx\n"
768 " where fmt is the import format of choice\n"
769 " and file.xxx is the file to be imported.\n"
770 "\t-version summarize version and build info\n"
771 "Check the LyX man page for more details.") << endl;
776 int parse_version(string const &, string const &)
778 lyxerr << "LyX " << lyx_version
779 << " of " << lyx_release_date << endl;
780 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
782 lyxerr << lyx_version_info << endl;
787 int parse_sysdir(string const & arg, string const &)
790 lyxerr << _("Missing directory for -sysdir switch") << endl;
797 int parse_userdir(string const & arg, string const &)
800 lyxerr << _("Missing directory for -userdir switch") << endl;
807 int parse_execute(string const & arg, string const &)
810 lyxerr << _("Missing command string after --execute switch") << endl;
814 // Argh. Setting gui to false segfaults..
815 // FIXME: when ? how ?
820 int parse_export(string const & type, string const &)
823 lyxerr << _("Missing file type [eg latex, ps...] after "
824 "--export switch") << endl;
827 batch = "buffer-export " + type;
832 int parse_import(string const & type, string const & file)
835 lyxerr << _("Missing file type [eg latex, ps...] after "
836 "--import switch") << endl;
840 lyxerr << _("Missing filename for --import") << endl;
844 batch = "buffer-import " + type + " " + file;
851 bool LyX::easyParse(int & argc, char * argv[])
853 std::map<string, cmd_helper> cmdmap;
855 cmdmap["-dbg"] = parse_dbg;
856 cmdmap["-help"] = parse_help;
857 cmdmap["--help"] = parse_help;
858 cmdmap["-version"] = parse_version;
859 cmdmap["--version"] = parse_version;
860 cmdmap["-sysdir"] = parse_sysdir;
861 cmdmap["-userdir"] = parse_userdir;
862 cmdmap["-x"] = parse_execute;
863 cmdmap["--execute"] = parse_execute;
864 cmdmap["-e"] = parse_export;
865 cmdmap["--export"] = parse_export;
866 cmdmap["-i"] = parse_import;
867 cmdmap["--import"] = parse_import;
869 for (int i = 1; i < argc; ++i) {
870 std::map<string, cmd_helper>::const_iterator it
871 = cmdmap.find(argv[i]);
873 // don't complain if not found - may be parsed later
874 if (it == cmdmap.end())
877 string arg((i + 1 < argc) ? argv[i + 1] : "");
878 string arg2((i + 2 < argc) ? argv[i + 2] : "");
880 int const remove = 1 + it->second(arg, arg2);
882 // Now, remove used arguments by shifting
883 // the following ones remove places down.
885 for (int j = i; j < argc; ++j)
886 argv[j] = argv[j + remove];
890 batch_command = batch;