1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1995 Matthias Ettrich
7 * Copyright (C) 1995-1998 The LyX Team.
9 *======================================================*/
13 // $Id: lyx_main.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $
15 #if !defined(lint) && !defined(WITH_WARNINGS)
16 static char vcid[] = "$Id: lyx_main.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $";
25 #include "lyx_gui_misc.h"
27 #include "pathstack.h"
28 #include "filetools.h"
29 #include "bufferlist.h"
32 #include "lastfiles.h"
34 #include "lyxserver.h"
38 extern void LoadLyXFile(LString const &);
40 LString system_lyxdir;
42 LString system_tempdir;
43 LString user_lyxdir; // Default $HOME/.lyx
45 // Should this be kept global? Asger says Yes.
51 // This is the global bufferlist object
52 BufferList bufferlist;
54 LyXServer *lyxserver = 0;
55 // this should be static, but I need it in buffer.C
56 bool finished = false; // flag, that we are quitting the program
58 // convenient to have it here.
59 kb_keymap *toplevel_keymap;
61 // from spellchecker.C
63 extern void sigchldhandler(int sig);
66 LyX::LyX(int *argc, char *argv[])
68 // Prevent crash with --help
72 // Here we need to parse the command line. At least
73 // we need to parse for "-dbg" and "-help"
74 bool gui = easyParse(argc, argv);
76 // Global bindings (this must be done as early as possible.) (Lgb)
77 toplevel_keymap = new kb_keymap;
79 lyxerr.debug("Initializing lyxrc");
82 // Make the GUI object, and let it take care of the
83 // command line arguments that concerns it.
84 lyxerr.debug("Initializing LyXGUI...");
85 lyxGUI = new LyXGUI(this, argc, argv, gui);
86 lyxerr.debug("Initializing LyXGUI...done");
88 // Initialization of LyX (reads lyxrc and more)
89 lyxerr.debug("Initializing LyX::init...");
91 lyxerr.debug("Initializing LyX::init...done");
95 // Load the files specified in the command line.
96 // Now the GUI and LyX have taken care of their arguments, so
97 // the only thing left on the command line should be
100 lyxerr.debug("Opening document...");
102 lyxerr.debug("Opening documents...");
104 Buffer *last_loaded = NULL;
106 for (int argi = (*argc) - 1; argi >= 1; argi--) {
107 Buffer * loadb = bufferlist.loadLyXFile(argv[argi]);
114 LString splash = i18nLibFileSearch("examples", "splash.lyx");
115 lyxerr.debug("Opening splash document "+splash+"...");
116 Buffer * loadb = bufferlist.loadLyXFile(splash);
122 if (last_loaded != NULL) {
123 lyxerr.debug("Yes we loaded some files.");
124 lyxGUI->regBuf(last_loaded);
127 // Let the ball begin...
132 // A destructor is always necessary (asierra-970604)
143 extern "C" void error_handler(int err_sig);
145 void LyX::init(int */*argc*/, char **argv)
147 // Install the signal handlers
148 signal(SIGHUP, error_handler);
149 signal(SIGFPE, error_handler);
150 signal(SIGSEGV, error_handler);
151 signal(SIGINT, error_handler);
152 signal(SIGTERM, error_handler);
155 // Install the SIGCHLD handler
156 act_.sa_handler = sigchldhandler;
157 //act_.sa_mask = SIGCHLD;
159 //act_.sa_flags = SA_RESTART; //perhaps
160 sigaction(SIGCHLD, &act_, NULL);
163 // Determine path of binary
166 LString fullbinpath, binpath = argv[0];
167 binpath.subst('\\','/');
168 LString binname = OnlyFilename(argv[0]);
169 // Sorry for system specific code. (SMiyata)
170 if (binname.suffixIs(".exe")) binname.substring(0,binname.length()-5);
172 binpath = ExpandPath(binpath); // This expands ./ and ~/
174 if (!AbsolutePath(binpath)) {
175 LString binsearchpath = getEnvPath("PATH");
176 binsearchpath += ";."; // This will make "src/lyx" work always :-)
177 binpath = FileOpenSearch(binsearchpath, argv[0]);
180 fullbinpath = binpath;
181 binpath = MakeAbsPath(OnlyPath(binpath));
183 if (binpath.empty()) {
184 lyxerr.print(_("Warning: could not determine path of binary."));
185 lyxerr.print(_("If you have problems, try starting LyX with an absolute path."));
187 lyxerr.debug("Path of binary: " + binpath);
190 // Determine system directory.
193 // Directories are searched in this order:
194 // 1) -sysdir command line parameter
195 // 2) LYX_DIR_10x environment variable
196 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
197 // 4) <path of binary>/../share/<name of binary>/
198 // 4a) repeat 4 after following the Symlink if <path of
199 // binary> is a symbolic link.
200 // 5) hardcoded lyx_dir
201 // The directory is checked for the presence of the file
202 // "chkconfig.ltx", and if that is present, the directory
203 // is accepted as the system directory.
204 // If no chkconfig.ltx file is found, a warning is given,
205 // and the hardcoded lyx_dir is used.
207 // If we had a command line switch, system_lyxdir is already set
208 LString searchpath = MakeAbsPath(system_lyxdir) + ';';
210 // LYX_DIR_10x environment variable
211 LString lyxdir = getEnvPath("LYX_DIR_10x");
213 if (!lyxdir.empty()) {
214 lyxerr.debug("LYX_DIR_10x: " + lyxdir, Error::INIT);
215 searchpath += lyxdir + ';';
218 // <path of binary>/TOP_SRCDIR/lib
219 build_lyxdir = MakeAbsPath("../lib", binpath);
220 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
221 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
223 lyxerr.debug("Checking whether LyX is run in "
224 "place... yes", Error::INIT);
226 lyxerr.debug("Checking whether LyX is run in place... no",
228 build_lyxdir.clean();
234 // Path of binary/../share/name of binary/
235 searchpath += NormalizePath(AddPath(binpath, "../share/") +
236 OnlyFilename(binname)) + ';';
239 FileInfo file(fullbinpath,true);
240 FollowLink = file.isLink();
243 if (LyXReadLink(fullbinpath,Link)) {
245 binpath = MakeAbsPath(OnlyPath(fullbinpath));
251 } while (FollowLink);
254 searchpath += LYX_DIR;
256 // If debugging, show complete search path
257 lyxerr.debug("System directory search path: "+searchpath, Error::INIT);
259 LString const filename = "chkconfig.ltx";
260 LString temp = FileOpenSearch(searchpath, filename, LString());
261 system_lyxdir = OnlyPath(temp);
263 // Reduce "path/../path" stuff out of system directory
264 system_lyxdir = NormalizePath(system_lyxdir);
266 bool path_shown = false;
268 // Warn if environment variable is set, but unusable
269 if (!lyxdir.empty()) {
270 if (system_lyxdir != NormalizePath(lyxdir)) {
271 lyxerr.print(_("LYX_DIR_10x environment variable no good."));
272 lyxerr.print(_("System directory set to: ")
278 // Warn the user if we couldn't find "chkconfig.ltx"
279 if (system_lyxdir.empty()) {
280 lyxerr.print(_("LyX Warning! Couldn't determine system directory."));
281 lyxerr.print(_("Try the '-sysdir' command line parameter or"));
282 lyxerr.print(_("set the environment variable LYX_DIR_10x to the "
283 "LyX system directory"));
284 lyxerr.print(_("containing the file `chkconfig.ltx'."));
286 lyxerr.print(_("Using built-in default ")
287 + LString(LYX_DIR) + _(" but expect problems."));
289 lyxerr.print(_("Expect problems."));
290 system_lyxdir = LYX_DIR;
294 // Report the system directory if debugging is on
296 lyxerr.debug("System directory: '" + system_lyxdir + '\'',
300 // Determine user lyx-dir
303 user_lyxdir = AddPath(getEnvPath("HOME"), LString('.')+LYX_NAME);
304 lyxerr.debug("User LyX directory: '"
305 + user_lyxdir + '\'', Error::INIT);
307 // Check that user LyX directory is ok.
311 // Load the layouts first
314 lyxerr.debug("Reading layouts...", Error::INIT);
318 // Shine up lyxrc defaults
321 // Default template path: system_dir/templates
322 if (lyxrc->template_path.empty()){
323 lyxrc->template_path = AddPath(system_lyxdir, "templates");
326 // Default lastfiles file: $HOME/.lyx/lastfiles
327 if (lyxrc->lastfiles.empty()){
328 lyxrc->lastfiles = AddName(user_lyxdir, "lastfiles");
331 // Calculate screen dpi as average of x-DPI and y-DPI:
332 Screen * scr=(DefaultScreenOfDisplay(fl_get_display()));
333 lyxrc->dpi = ((HeightOfScreen(scr)* 25.4 / HeightMMOfScreen(scr)) +
334 (WidthOfScreen(scr)* 25.4 / WidthMMOfScreen(scr))) / 2;
335 lyxerr.debug(LString("DPI setting detected to be ") +
336 int(lyxrc->dpi+0.5));
339 // Read configuration files
342 ReadRcFile("lyxrc.defaults");
345 // Ensure that we have really read a bind file, so that LyX is
347 if (!lyxrc->hasBindFile)
348 lyxrc->ReadBindFile();
350 if (lyxerr.debugging(Error::LYXRC)) {
354 // Create temp directory
355 system_tempdir = CreateLyXTmpDir(lyxrc->tempdir_path);
356 if (lyxerr.debugging(Error::INIT)) {
357 lyxerr.print("LyX tmp dir: `" + system_tempdir + '\'');
360 // load the lastfiles mini-database
361 lyxerr.debug("Reading lastfiles `" + lyxrc->lastfiles + "'...",
363 lastfiles = new LastFiles(lyxrc->lastfiles,
364 lyxrc->check_lastfiles,
365 lyxrc->num_lastfiles);
367 // start up the lyxserver. (is this a bit early?) (Lgb)
368 // 0.12 this will be way to early, we need the GUI to be initialized
369 // first, so move it for now.
370 // lyxserver = new LyXServer;
374 // This one is not allowed to use anything on the main form, since that
375 // one does not exist yet. (Asger)
376 void LyX::queryUserLyXDir()
378 // Does user directory exist?
379 FileInfo fileInfo(user_lyxdir);
380 if (fileInfo.isOK() && fileInfo.isDir()) {
388 if (!AskQuestion(_("You don't have a personal LyX directory."),
389 _("It is needed to keep your own configuration."),
390 _("Should I try to set it up for you (recommended)?"))) {
391 lyxerr.print(_("Running without personal LyX directory."));
392 // No, let's use $HOME instead.
393 user_lyxdir = getEnvPath("HOME");
397 // Tell the user what is going on
398 lyxerr.print(_("LyX: Creating directory ") + user_lyxdir + _(" and running configure..."));
400 // Create directory structure
401 if (!createDirectory(user_lyxdir, 0755)) {
402 // Failed, let's use $HOME instead.
403 user_lyxdir = getEnvPath("HOME");
404 lyxerr.print(_("Failed. Will use ") + user_lyxdir + _(" instead."));
408 // Run configure in user lyx directory
409 PathPush(user_lyxdir);
410 system(AddName(system_lyxdir,"configure").c_str());
412 lyxerr.print(LString("LyX: ") + _("Done!"));
416 // Read the rc file `name'
417 void LyX::ReadRcFile(LString const & name)
419 lyxerr.debug("About to read "+name+"...", Error::INIT);
421 LString lyxrc_path = LibFileSearch(LString(), name);
422 if (!lyxrc_path.empty()){
423 lyxerr.debug("Found "+name+" in " + lyxrc_path, Error::INIT);
424 if (lyxrc->Read(lyxrc_path) < 0) {
425 WriteAlert(_("LyX Warning!"),
426 _("Error while reading ")+lyxrc_path+".",
427 _("Using built-in defaults."));
430 lyxerr.debug("Could not find "+name, Error::INIT);
434 // Set debugging level and report result to user
435 void setDebuggingLevel(int dbgLevel)
437 lyxerr.print(LString(_("Setting debug level to ")) + dbgLevel);
438 lyxerr.setDebugLevel(dbgLevel);
439 lyxerr.debug(LString("Debugging INFO #") + int(Error::INFO),
441 lyxerr.debug(LString("Debugging INIT #") + int(Error::INIT),
443 lyxerr.debug(LString("Debugging KEY #") + int(Error::KEY),
445 lyxerr.debug(LString("Debugging TOOLBAR #") + int(Error::TOOLBAR),
447 lyxerr.debug(LString("Debugging LEX and PARSER #") +
448 int(Error::LEX_PARSER),
450 lyxerr.debug(LString("Debugging LYXRC #") + int(Error::LYXRC),
452 lyxerr.debug(LString("Debugging KBMAP #") + int(Error::KBMAP),
454 lyxerr.debug(LString("Debugging LATEX #") + int(Error::LATEX),
456 lyxerr.debug(LString("Debugging MATHED #") + int(Error::MATHED),
458 lyxerr.debug(LString("Debugging FONT #") + int(Error::FONT),
460 lyxerr.debug(LString("Debugging TCLASS #") + int(Error::TCLASS),
462 lyxerr.debug(LString("Debugging LYXVC #") + int(Error::LYXVC),
464 lyxerr.debug(LString("Debugging LYXSERVER #") + int(Error::LYXSERVER),
469 // Give command line help
470 void commandLineHelp()
472 lyxerr.print(_("LyX " LYX_VERSION " of " LYX_RELEASE ".\n"));
473 lyxerr.print(_("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"));
474 lyxerr.print(_("Command line switches (case sensitive):"));
475 lyxerr.print(_(" -help summarize LyX usage"));
476 lyxerr.print(_(" -sysdir x try to set system directory to x"));
477 lyxerr.print(_(" -width x set the width of the main window"));
478 lyxerr.print(_(" -height y set the height of the main window"));
479 lyxerr.print(_(" -xpos x set the x position of the main window"));
480 lyxerr.print(_(" -ypos y set the y position of the main window"));
481 lyxerr.print(_(" -dbg n where n is a sum of debugging options. Try -dbg 65535 -help"));
482 lyxerr.print(_(" -Reverse swaps foreground & background colors"));
483 lyxerr.print(_(" -Mono runs LyX in black and white mode"));
484 lyxerr.print(_(" -FastSelection use a fast routine for drawing selections\n"));
485 lyxerr.print(_("Check the LyX man page for more options."));
489 bool LyX::easyParse(int *argc, char *argv[])
492 for(int i=1; i < *argc; i++) {
493 LString arg = argv[i];
494 // Check for -dbg int
498 sscanf(argv[i+1],"%d", &erri);
500 setDebuggingLevel(erri);
502 // Now, remove these two arguments by shifting
503 // the following two places down.
505 for (int j=i; j < (*argc); j++)
507 i--; // After shift, check this number again.
509 lyxerr.print(_("Missing number for -dbg switch!"));
511 // Check for "-sysdir"
512 else if (arg == "-sysdir") {
514 system_lyxdir = argv[i+1];
516 // Now, remove these two arguments by shifting
517 // the following two places down.
519 for (int j=i; j < (*argc); j++)
521 i--; // After shift, check this number again.
523 lyxerr.print(_("Missing directory for -sysdir switch!"));
524 // Check for --help or -help
525 } else if (arg == "--help" || arg == "-help") {
529 // Check for "-nw": No window
530 else if (arg == "-nw") {
538 void error_handler(int err_sig)
542 fprintf(stderr, "\nlyx: SIGHUP signal caught\n");
548 fprintf(stderr, "\nlyx: SIGFPE signal caught\n");
551 fprintf(stderr, "\nlyx: SIGSEGV signal caught\n");
553 "Sorry, you have found a bug in LyX."
554 " If possible, please read 'Known bugs'\n"
555 "under the Help menu and then send us "
556 "a full bug report. Thanks!\n");
563 // Deinstall the signal handlers
564 signal(SIGHUP, SIG_DFL);
565 signal(SIGINT, SIG_DFL);
566 signal(SIGFPE, SIG_DFL);
567 signal(SIGSEGV, SIG_DFL);
568 signal(SIGTERM, SIG_DFL);
570 bufferlist.emergencyWriteAll();
572 lyxerr.print("Bye.");
573 if(err_sig!=SIGHUP && (getenv("LYXDEBUG") || err_sig == SIGSEGV))