1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * ======================================================*/
19 #include "lyx_gui_misc.h"
21 #include "support/path.h"
22 #include "support/filetools.h"
23 #include "bufferlist.h"
25 #include "support/FileInfo.h"
26 #include "lastfiles.h"
28 #include "lyxserver.h"
32 extern void LoadLyXFile(string const &);
36 string system_tempdir;
37 string user_lyxdir; // Default $HOME/.lyx
39 // Should this be kept global? Asger says Yes.
45 // This is the global bufferlist object
46 BufferList bufferlist;
48 LyXServer *lyxserver = 0;
49 // this should be static, but I need it in buffer.C
50 bool finished = false; // flag, that we are quitting the program
52 // convenient to have it here.
53 kb_keymap *toplevel_keymap;
55 // from spellchecker.C
57 extern void sigchldhandler(int sig);
60 LyX::LyX(int *argc, char *argv[])
62 // Prevent crash with --help
66 // Here we need to parse the command line. At least
67 // we need to parse for "-dbg" and "-help"
68 bool gui = easyParse(argc, argv);
70 // Global bindings (this must be done as early as possible.) (Lgb)
71 toplevel_keymap = new kb_keymap;
73 lyxerr.debug() << "Initializing lyxrc" << endl;
76 // Make the GUI object, and let it take care of the
77 // command line arguments that concerns it.
78 lyxerr.debug() << "Initializing LyXGUI..." << endl;
79 lyxGUI = new LyXGUI(this, argc, argv, gui);
80 lyxerr.debug() << "Initializing LyXGUI...done" << endl;
82 // Initialization of LyX (reads lyxrc and more)
83 lyxerr.debug() << "Initializing LyX::init..." << endl;
85 lyxerr.debug() << "Initializing LyX::init...done" << endl;
89 // Load the files specified in the command line.
90 // Now the GUI and LyX have taken care of their arguments, so
91 // the only thing left on the command line should be
94 lyxerr.debug() << "Opening document..." << endl;
96 lyxerr.debug() << "Opening documents..." << endl;
98 Buffer *last_loaded = 0;
100 for (int argi = (*argc) - 1; argi >= 1; argi--) {
101 Buffer * loadb = bufferlist.loadLyXFile(argv[argi]);
108 string splash = i18nLibFileSearch("examples", "splash.lyx");
109 lyxerr.debug() << "Opening splash document "
110 << splash << "..." << endl;
111 Buffer * loadb = bufferlist.loadLyXFile(splash);
117 if (last_loaded != 0) {
118 lyxerr.debug() << "Yes we loaded some files." << endl;
119 lyxGUI->regBuf(last_loaded);
122 // Let the ball begin...
127 // A destructor is always necessary (asierra-970604)
138 extern "C" void error_handler(int err_sig);
140 void LyX::init(int */*argc*/, char **argv)
142 // Install the signal handlers
143 signal(SIGHUP, error_handler);
144 signal(SIGFPE, error_handler);
145 signal(SIGSEGV, error_handler);
146 signal(SIGINT, error_handler);
147 signal(SIGTERM, error_handler);
150 // Install the SIGCHLD handler
151 act_.sa_handler = sigchldhandler;
152 //act_.sa_mask = SIGCHLD;
154 //act_.sa_flags = SA_RESTART; //perhaps
155 sigaction(SIGCHLD, &act_, 0);
158 // Determine path of binary
161 string fullbinpath, binpath = argv[0];
162 subst(binpath, '\\', '/');
163 string binname = OnlyFilename(argv[0]);
164 // Sorry for system specific code. (SMiyata)
165 if (suffixIs(binname, ".exe")) binname.erase(binname.length()-4, string::npos);
167 binpath = ExpandPath(binpath); // This expands ./ and ~/
169 if (!AbsolutePath(binpath)) {
170 string binsearchpath = GetEnvPath("PATH");
171 binsearchpath += ";."; // This will make "src/lyx" work always :-)
172 binpath = FileOpenSearch(binsearchpath, argv[0]);
175 fullbinpath = binpath;
176 binpath = MakeAbsPath(OnlyPath(binpath));
178 if (binpath.empty()) {
179 lyxerr << _("Warning: could not determine path of binary.")
181 << _("If you have problems, try starting LyX with an absolute path.")
184 lyxerr.debug() << "Path of binary: " << binpath << endl;
187 // Determine system directory.
190 // Directories are searched in this order:
191 // 1) -sysdir command line parameter
192 // 2) LYX_DIR_10x environment variable
193 // 3) Maybe <path of binary>/TOP_SRCDIR/lib
194 // 4) <path of binary>/../share/<name of binary>/
195 // 4a) repeat 4 after following the Symlink if <path of
196 // binary> is a symbolic link.
197 // 5) hardcoded lyx_dir
198 // The directory is checked for the presence of the file
199 // "chkconfig.ltx", and if that is present, the directory
200 // is accepted as the system directory.
201 // If no chkconfig.ltx file is found, a warning is given,
202 // and the hardcoded lyx_dir is used.
204 // If we had a command line switch, system_lyxdir is already set
206 if (!system_lyxdir.empty())
207 searchpath=MakeAbsPath(system_lyxdir) + ';';
209 // LYX_DIR_10x environment variable
210 string lyxdir = GetEnvPath("LYX_DIR_10x");
212 if (!lyxdir.empty()) {
213 lyxerr[Debug::INIT] << "LYX_DIR_10x: " << lyxdir << endl;
214 searchpath += lyxdir + ';';
217 // <path of binary>/TOP_SRCDIR/lib
218 build_lyxdir = MakeAbsPath("../lib", binpath);
219 if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
220 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
222 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
223 "place... yes" << endl;
226 << "Checking whether LyX is run in place... no"
228 build_lyxdir.clear();
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::INIT] << "System directory search path: "
258 << searchpath << endl;
260 string const filename = "chkconfig.ltx";
261 string temp = FileOpenSearch(searchpath, filename, string());
262 system_lyxdir = OnlyPath(temp);
264 // Reduce "path/../path" stuff out of system directory
265 system_lyxdir = NormalizePath(system_lyxdir);
267 bool path_shown = false;
269 // Warn if environment variable is set, but unusable
270 if (!lyxdir.empty()) {
271 if (system_lyxdir != NormalizePath(lyxdir)) {
272 lyxerr <<_("LYX_DIR_10x environment variable no good.")
274 << _("System directory set to: ")
275 << system_lyxdir << endl;
280 // Warn the user if we couldn't find "chkconfig.ltx"
281 if (system_lyxdir.empty()) {
282 lyxerr <<_("LyX Warning! Couldn't determine system directory.")
283 <<_("Try the '-sysdir' command line parameter or")
284 <<_("set the environment variable LYX_DIR_10x to the "
285 "LyX system directory")
286 << _("containing the file `chkconfig.ltx'.") << endl;
288 lyxerr << _("Using built-in default ")
289 << LYX_DIR << _(" but expect problems.")
292 lyxerr << _("Expect problems.") << endl;
293 system_lyxdir = LYX_DIR;
297 // Report the system directory if debugging is on
299 lyxerr[Debug::INIT] << "System directory: '"
300 << system_lyxdir << '\'' << endl;
303 // Determine user lyx-dir
306 user_lyxdir = AddPath(GetEnvPath("HOME"), string(".") + LYX_NAME);
307 lyxerr[Debug::INIT] << "User LyX directory: '"
308 << user_lyxdir << '\'' << endl;
310 // Check that user LyX directory is ok.
314 // Load the layouts first
317 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
321 // Shine up lyxrc defaults
324 // Default template path: system_dir/templates
325 if (lyxrc->template_path.empty()){
326 lyxrc->template_path = AddPath(system_lyxdir, "templates");
329 // Default lastfiles file: $HOME/.lyx/lastfiles
330 if (lyxrc->lastfiles.empty()){
331 lyxrc->lastfiles = AddName(user_lyxdir, "lastfiles");
334 // Calculate screen dpi as average of x-DPI and y-DPI:
335 Screen * scr=(DefaultScreenOfDisplay(fl_get_display()));
336 lyxrc->dpi = ((HeightOfScreen(scr)* 25.4 / HeightMMOfScreen(scr)) +
337 (WidthOfScreen(scr)* 25.4 / WidthMMOfScreen(scr))) / 2;
338 lyxerr.debug() << "DPI setting detected to be "
339 << lyxrc->dpi+0.5 << endl;
342 // Read configuration files
345 ReadRcFile("lyxrc.defaults");
348 // Ensure that we have really read a bind file, so that LyX is
350 if (!lyxrc->hasBindFile)
351 lyxrc->ReadBindFile();
353 if (lyxerr.debugging(Debug::LYXRC)) {
357 // Create temp directory
358 system_tempdir = CreateLyXTmpDir(lyxrc->tempdir_path);
359 if (lyxerr.debugging(Debug::INIT)) {
360 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
363 // load the lastfiles mini-database
364 lyxerr[Debug::INIT] << "Reading lastfiles `"
365 << lyxrc->lastfiles << "'..." << endl;
366 lastfiles = new LastFiles(lyxrc->lastfiles,
367 lyxrc->check_lastfiles,
368 lyxrc->num_lastfiles);
370 // start up the lyxserver. (is this a bit early?) (Lgb)
371 // 0.12 this will be way to early, we need the GUI to be initialized
372 // first, so move it for now.
373 // lyxserver = new LyXServer;
377 // This one is not allowed to use anything on the main form, since that
378 // one does not exist yet. (Asger)
379 void LyX::queryUserLyXDir()
381 // Does user directory exist?
382 FileInfo fileInfo(user_lyxdir);
383 if (fileInfo.isOK() && fileInfo.isDir()) {
391 if (!AskQuestion(_("You don't have a personal LyX directory."),
392 _("It is needed to keep your own configuration."),
393 _("Should I try to set it up for you (recommended)?"))) {
394 lyxerr << _("Running without personal LyX directory.") << endl;
395 // No, let's use $HOME instead.
396 user_lyxdir = GetEnvPath("HOME");
400 // Tell the user what is going on
401 lyxerr << _("LyX: Creating directory ") << user_lyxdir
402 << _(" and running configure...") << endl;
404 // Create directory structure
405 if (!createDirectory(user_lyxdir, 0755)) {
406 // Failed, let's use $HOME instead.
407 user_lyxdir = GetEnvPath("HOME");
408 lyxerr << _("Failed. Will use ") << user_lyxdir
409 << _(" instead.") << endl;
413 // Run configure in user lyx directory
415 system(AddName(system_lyxdir,"configure").c_str());
416 lyxerr << "LyX: " << _("Done!") << endl;
420 // Read the rc file `name'
421 void LyX::ReadRcFile(string const & name)
423 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
425 string lyxrc_path = LibFileSearch(string(), name);
426 if (!lyxrc_path.empty()){
427 lyxerr[Debug::INIT] << "Found " << name
428 << " in " << lyxrc_path << endl;
429 if (lyxrc->Read(lyxrc_path) < 0) {
430 WriteAlert(_("LyX Warning!"),
431 _("Error while reading ")+lyxrc_path+".",
432 _("Using built-in defaults."));
435 lyxerr[Debug::INIT] << "Could not find " << name << endl;
439 // Set debugging level and report result to user
440 void setDebuggingLevel(int dbgLevel)
442 lyxerr << _("Setting debug level to ") << dbgLevel << endl;
443 lyxerr.level(Debug::type(dbgLevel));
444 lyxerr[Debug::INFO] << "Debugging INFO #" << Debug::INFO << endl;
445 lyxerr[Debug::INIT] << "Debugging INIT #" << Debug::INIT << endl;
446 lyxerr[Debug::KEY] << "Debugging KEY #" << Debug::KEY << endl;
447 lyxerr[Debug::TOOLBAR] << "Debugging TOOLBAR #" << Debug::TOOLBAR << endl;
448 lyxerr[Debug::PARSER] << "Debugging LEX and PARSER #" << Debug::PARSER << endl;
449 lyxerr[Debug::LYXRC] << "Debugging LYXRC #" << Debug::LYXRC << endl;
450 lyxerr[Debug::KBMAP] << "Debugging KBMAP #" << Debug::KBMAP << endl;
451 lyxerr[Debug::LATEX] << "Debugging LATEX #" << Debug::LATEX << endl;
452 lyxerr[Debug::MATHED] << "Debugging MATHED #" << Debug::MATHED << endl;
453 lyxerr[Debug::FONT] << "Debugging FONT #" << Debug::FONT << endl;
454 lyxerr[Debug::TCLASS] << "Debugging TCLASS #" << Debug::TCLASS << endl;
455 lyxerr[Debug::LYXVC] << "Debugging LYXVC #" << Debug::LYXVC << endl;
456 lyxerr[Debug::LYXSERVER] << "Debugging LYXSERVER #" << Debug::LYXSERVER << endl;
460 // Give command line help
461 void commandLineHelp()
463 lyxerr << "LyX " LYX_VERSION << " of " LYX_RELEASE << endl;
465 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
466 "Command line switches (case sensitive):\n"
467 "\t-help summarize LyX usage\n"
468 "\t-sysdir x try to set system directory to x\n"
469 "\t-width x set the width of the main window\n"
470 "\t-height y set the height of the main window\n"
471 "\t-xpos x set the x position of the main window\n"
472 "\t-ypos y set the y position of the main window\n"
473 "\t-dbg n where n is a sum of debugging options. Try -dbg 65535 -help\n"
474 "\t-Reverse swaps foreground & background colors\n"
475 "\t-Mono runs LyX in black and white mode\n"
476 "\t-FastSelection use a fast routine for drawing selections\n\n"
477 "Check the LyX man page for more options.") << endl;
481 bool LyX::easyParse(int *argc, char *argv[])
484 for(int i=1; i < *argc; i++) {
485 string arg = argv[i];
486 // Check for -dbg int
490 sscanf(argv[i+1],"%d", &erri);
492 setDebuggingLevel(erri);
494 // Now, remove these two arguments by shifting
495 // the following two places down.
497 for (int j=i; j < (*argc); j++)
499 i--; // After shift, check this number again.
501 lyxerr << _("Missing number for -dbg switch!")
504 // Check for "-sysdir"
505 else if (arg == "-sysdir") {
507 system_lyxdir = argv[i+1];
509 // Now, remove these two arguments by shifting
510 // the following two places down.
512 for (int j=i; j < (*argc); j++)
514 i--; // After shift, check this number again.
516 lyxerr << _("Missing directory for -sysdir switch!")
518 // Check for --help or -help
519 } else if (arg == "--help" || arg == "-help") {
523 // Check for "-nw": No window
524 else if (arg == "-nw") {
532 void error_handler(int err_sig)
536 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
542 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
545 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
547 "Sorry, you have found a bug in LyX."
548 " If possible, please read 'Known bugs'\n"
549 "under the Help menu and then send us "
550 "a full bug report. Thanks!" << endl;
557 // Deinstall the signal handlers
558 signal(SIGHUP, SIG_DFL);
559 signal(SIGINT, SIG_DFL);
560 signal(SIGFPE, SIG_DFL);
561 signal(SIGSEGV, SIG_DFL);
562 signal(SIGTERM, SIG_DFL);
564 bufferlist.emergencyWriteAll();
566 lyxerr << "Bye." << endl;
567 if(err_sig!=SIGHUP && (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))