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