From: John Levon Date: Thu, 30 May 2002 19:49:00 +0000 (+0000) Subject: command line import fixups X-Git-Tag: 1.6.10~19153 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=ad9c1fa3aa8cde8aef91c8ccad6bb03e702c3a18;p=features.git command line import fixups git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4303 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/ChangeLog b/src/ChangeLog index f4d8ac514a..718f5e0ad8 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2002-05-30 John Levon + + * buffer.h: + * buffer.C: + * lyx_main.C: fix a crash on bad command line, + and give a useful exit status on error + + * lyxfunc.C (doImport): allow -i lyx to work + 2002-03-30 André Pönitz * lyxfunc.C: mathed font changes diff --git a/src/buffer.C b/src/buffer.C index 0bf0910091..481ca4990a 100644 --- a/src/buffer.C +++ b/src/buffer.C @@ -3893,24 +3893,28 @@ void Buffer::markDepClean(string const & name) } -bool Buffer::dispatch(string const & command) +bool Buffer::dispatch(string const & command, bool * result) { // Split command string into command and argument string cmd; string line = frontStrip(command); string const arg = strip(frontStrip(split(line, cmd, ' '))); - return dispatch(lyxaction.LookupFunc(cmd), arg); + return dispatch(lyxaction.LookupFunc(cmd), arg, result); } -bool Buffer::dispatch(int action, string const & argument) +bool Buffer::dispatch(int action, string const & argument, bool * result) { bool dispatched = true; + switch (action) { - case LFUN_EXPORT: - Exporter::Export(this, argument, false); + case LFUN_EXPORT: { + bool const tmp = Exporter::Export(this, argument, false); + if (result) + *result = tmp; break; + } default: dispatched = false; diff --git a/src/buffer.h b/src/buffer.h index 1aacc136ba..4da4b29a3a 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -76,10 +76,10 @@ public: /** High-level interface to buffer functionality. This function parses a command string and executes it */ - bool dispatch(string const & command); + bool dispatch(string const & command, bool * result = 0); /// Maybe we know the function already by number... - bool dispatch(int ac, string const & argument); + bool dispatch(int ac, string const & argument, bool * result = 0); /// void resizeInsets(BufferView *); diff --git a/src/lyx_main.C b/src/lyx_main.C index bc3da055e9..9502dd15f3 100644 --- a/src/lyx_main.C +++ b/src/lyx_main.C @@ -102,7 +102,7 @@ LyX::LyX(int * argc, char * argv[]) lyxerr << _("Wrong command line option `") << argv[argi] << _("'. Exiting.") << endl; - exit(0); + exit(1); } } @@ -154,15 +154,16 @@ LyX::LyX(int * argc, char * argv[]) if (!last_loaded) last_loaded = bufferlist.newFile("tmpfile", string()); + bool success = false; + // try to dispatch to last loaded buffer first - bool dispatched = last_loaded->dispatch(batch_command); + bool dispatched = last_loaded->dispatch(batch_command, &success); // if this was successful, return. // Maybe we could do something more clever than aborting... if (dispatched) { - lyxerr << "We are done!" << endl; QuitLyX(); - return; + exit(!success); } // otherwise, let the GUI handle the batch command @@ -835,7 +836,7 @@ bool LyX::easyParse(int * argc, char * argv[]) lyxerr << _("List of supported debug flags:") << endl; Debug::showTags(lyxerr); - exit(0); + exit(1); } } // Check for "-sysdir" @@ -846,7 +847,7 @@ bool LyX::easyParse(int * argc, char * argv[]) } else { lyxerr << _("Missing directory for -sysdir switch!") << endl; - exit(0); + exit(1); } } // Check for "-userdir" @@ -857,7 +858,7 @@ bool LyX::easyParse(int * argc, char * argv[]) } else { lyxerr << _("Missing directory for -userdir switch!") << endl; - exit(0); + exit(1); } } // Check for --help or -help @@ -870,6 +871,7 @@ bool LyX::easyParse(int * argc, char * argv[]) commandLineVersionInfo(); exit(0); } + // FIXME: why is this commented out ? // Check for "-nw": No XWindows as for emacs this should // give a LyX that could be used in a terminal window. //else if (arg == "-nw") { @@ -886,7 +888,8 @@ bool LyX::easyParse(int * argc, char * argv[]) lyxerr << _("Missing command string after -x switch!") << endl; // Argh. Setting gui to false segfaults.. - //gui = false; + // FIXME: when ? how ? + // gui = false; } else if (arg == "-e" || arg == "--export") { @@ -895,25 +898,34 @@ bool LyX::easyParse(int * argc, char * argv[]) removeargs = 2; batch_command = "buffer-export " + type; gui = false; - } else + } else { lyxerr << _("Missing file type [eg latex, " "ps...] after ") << arg << _(" switch!") << endl; + exit(1); + } } else if (arg == "-i" || arg == "--import") { if (i + 1 < *argc) { - string const type(argv[i+1]); + if (!argv[i+2]) { + lyxerr << _("Missing filename for --import") << endl; + exit(1); + } + string const file(argv[i+2]); + string const type(argv[i+1]); removeargs = 3; - + batch_command = "buffer-import " + type + " " + file; lyxerr << "batch_command: " << batch_command << endl; - } else + } else { lyxerr << _("Missing type [eg latex, " "ps...] after ") << arg << _(" switch!") << endl; + exit(1); + } } if (removeargs > 0) { diff --git a/src/lyxfunc.C b/src/lyxfunc.C index c5dfc10413..088828537d 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -1912,15 +1912,16 @@ void LyXFunc::open(string const & fname) } -// checks for running without gui are missing. void LyXFunc::doImport(string const & argument) { string format; string filename = split(argument, format, ' '); + lyxerr[Debug::INFO] << "LyXFunc::doImport: " << format << " file: " << filename << endl; - - if (filename.empty()) { // need user interaction + + // need user interaction + if (filename.empty()) { string initpath = lyxrc.document_path; if (owner->view()->available()) { @@ -1957,7 +1958,6 @@ void LyXFunc::doImport(string const & argument) owner->message(_("Canceled.")); } - // still no filename? abort if (filename.empty()) return; @@ -1967,35 +1967,35 @@ void LyXFunc::doImport(string const & argument) string const lyxfile = ChangeExtension(filename, ".lyx"); // Check if the document already is open - if (bufferlist.exists(lyxfile)) { + if (lyxrc.use_gui && bufferlist.exists(lyxfile)) { switch (Alert::askConfirmation(_("Document is already open:"), MakeDisplayPath(lyxfile, 50), _("Do you want to close that document now?\n" "('No' will just switch to the open version)"))) { - case 1: // Yes: close the document - if (!bufferlist.close(bufferlist.getBuffer(lyxfile))) + case 1: // If close is canceled, we cancel here too. + if (!bufferlist.close(bufferlist.getBuffer(lyxfile))) return; break; - case 2: // No: switch to the open document + case 2: owner->view()->buffer(bufferlist.getBuffer(lyxfile)); return; - case 3: // Cancel: Do nothing + case 3: owner->message(_("Canceled.")); return; } } - // Check if a LyX document by the same root exists in filesystem - FileInfo const f(lyxfile, true); - if (f.exist() && !Alert::askQuestion(_("A document by the name"), - MakeDisplayPath(lyxfile), - _("already exists. Overwrite?"))) { - owner->message(_("Canceled")); - return; + // if the file exists already, and we didn't do + // -i lyx thefile.lyx, warn + if (FileInfo(lyxfile, true).exist() && filename != lyxfile) { + if (!Alert::askQuestion(_("A document by the name"), + MakeDisplayPath(lyxfile), _("already exists. Overwrite?"))) { + owner->message(_("Canceled")); + return; + } } - // filename should be valid now Importer::Import(owner, filename, format); }