This patch reduces the number of calls of ::exit() and hands the return
value of main() trough the various exec functions instead. The difference
to the one I sent earlier is that it does not remove the QApplication hack
in qt4, because that caused a crash on windows.
This will go in this evening.

Georg

Log:
Reduce calls of ::exit()
        * src/main.C
        (main): return LyX::exec()

        * src/frontends/{gtk,qt3,qt4}/lyx_gui.C
        (lyx_gui::exec): Change return value to int
        (lyx_gui::start): ditto

        * src/frontends/qt[3,4]/lyx_gui.C
        (lyx_gui::exit): Call QApplication::exit instead of ::exit, this
        hack is not needed anymore

        * src/frontends/lyx_gui.h
        (lyx_gui::exec): Change return value to int
        (lyx_gui::start): ditto

        * src/lyxtextclasslist.[Ch]
        (LyXSetStyle): SChange return value to bool and don't call ::exit

        * src/lyx_main.C
        (showFileError): Don't exit anymore

        * src/lyx_main.[Ch]
        (LyX::exec): Change return value to int
        (LyX::priv_exec): ditto
        (LyX::exec2): ditto
        (LyX::init): Change return value to bool
        (LyX::readRcFile): ditto
        (LyX::readUIFile): ditto
Index: src/main.C
===================================================================
--- src/main.C	(Revision 14337)
+++ src/main.C	(Arbeitskopie)
@@ -44,6 +44,5 @@ int main(int argc, char * argv[])
 	// initialize for internationalized version *EK*
 	locale_init();
 
-	LyX::exec(argc, argv);
-	return 0;
+	return LyX::exec(argc, argv);
 }
Index: src/frontends/gtk/lyx_gui.C
===================================================================
--- src/frontends/gtk/lyx_gui.C	(Revision 14337)
+++ src/frontends/gtk/lyx_gui.C	(Arbeitskopie)
@@ -118,7 +118,7 @@ private:
 
 Application * theApp;
 
-void lyx_gui::exec(int & argc, char * argv[])
+int lyx_gui::exec(int & argc, char * argv[])
 {
 	theApp = new Application(argc, argv);
 
@@ -131,7 +131,7 @@ void lyx_gui::exec(int & argc, char * ar
 	// must do this /before/ lyxrc gets read
 	lyxrc.dpi = getDPI();
 
-	LyX::ref().exec2(argc, argv);
+	return LyX::ref().exec2(argc, argv);
 }
 
 
@@ -140,8 +140,9 @@ void lyx_gui::parse_lyxrc()
 }
 
 
-void lyx_gui::start(string const & batch, std::vector<string> const & files,
-		    unsigned int width, unsigned int height, int posx, int posy, bool)
+int lyx_gui::start(string const & batch, std::vector<string> const & files,
+                   unsigned int width, unsigned int height, int posx,
+                   int posy, bool)
 {
 	int view_id = theApp->gui().newView(width, height);
 	GView & view = static_cast<GView &> (theApp->gui().view(view_id));
@@ -171,6 +172,7 @@ void lyx_gui::start(string const & batch
 	// FIXME: breaks emergencyCleanup
 	delete lyxsocket;
 	delete lyxserver;
+	return EXIT_SUCCESS;
 }
 
 
Index: src/frontends/qt3/lyx_gui.C
===================================================================
--- src/frontends/qt3/lyx_gui.C	(Revision 14337)
+++ src/frontends/qt3/lyx_gui.C	(Arbeitskopie)
@@ -172,7 +172,7 @@ namespace lyx_gui {
 bool use_gui = true;
 
 
-void exec(int & argc, char * argv[])
+int exec(int & argc, char * argv[])
 {
 	// Force adding of font path _before_ QApplication is initialized
 	FontLoader::initFontPath();
@@ -225,7 +225,7 @@ void exec(int & argc, char * argv[])
 
 	LoaderQueue::setPriority(10,100);
 
-	LyX::ref().exec2(argc, argv);
+	return LyX::ref().exec2(argc, argv);
 }
 
 
@@ -233,8 +233,8 @@ void parse_lyxrc()
 {}
 
 
-void start(string const & batch, vector<string> const & files,
-	   unsigned int width, unsigned int height, int posx, int posy, bool maximize)
+int start(string const & batch, vector<string> const & files,
+          unsigned int width, unsigned int height, int posx, int posy, bool maximize)
 {
 	// this can't be done before because it needs the Languages object
 	initEncodings();
@@ -272,10 +272,11 @@ void start(string const & batch, vector<
 		view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
 	}
 
-	qApp->exec();
+	int const status = qApp->exec();
 
 	// FIXME
 	cleanup();
+	return status;
 }
 
 
@@ -294,13 +295,7 @@ void sync_events()
 void exit(int status)
 {
 	cleanup();
-
-	// we cannot call QApplication::exit(status) - that could return us
-	// into a static dialog return in the lyx code (for example,
-	// load autosave file QMessageBox. We have to just get the hell
-	// out.
-
-	::exit(status);
+	QApplication::exit(status);
 }
 
 
Index: src/frontends/qt4/lyx_gui.C
===================================================================
--- src/frontends/qt4/lyx_gui.C	(Revision 14337)
+++ src/frontends/qt4/lyx_gui.C	(Arbeitskopie)
@@ -108,7 +108,7 @@ namespace lyx_gui {
 
 bool use_gui = true;
 
-void exec(int & argc, char * argv[])
+int exec(int & argc, char * argv[])
 {	
 	/*
 	FIXME : Abdel 29/05/2006 ([EMAIL PROTECTED])
@@ -185,7 +185,7 @@ void exec(int & argc, char * argv[])
 
 	LoaderQueue::setPriority(10,100);
 
-	LyX::ref().exec2(argc, argv);
+	return LyX::ref().exec2(argc, argv);
 }
 
 
@@ -193,8 +193,9 @@ void parse_lyxrc()
 {}
 
 
-void start(string const & batch, vector<string> const & files,
-	   unsigned int width, unsigned int height, int posx, int posy, bool maximize)
+int start(string const & batch, vector<string> const & files,
+          unsigned int width, unsigned int height, int posx, int posy,
+          bool maximize)
 {
 	// this can't be done before because it needs the Languages object
 	initEncodings();
@@ -245,10 +246,11 @@ void start(string const & batch, vector<
 		view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
 	}
 
-	qApp->exec();
+	int const status = qApp->exec();
 
 	// FIXME
 	cleanup();
+	return status;
 }
 
 
@@ -265,13 +267,7 @@ void sync_events()
 void exit(int status)
 {
 	cleanup();
-
-	// we cannot call QApplication::exit(status) - that could return us
-	// into a static dialog return in the lyx code (for example,
-	// load autosave file QMessageBox. We have to just get the hell
-	// out.
-
-	::exit(status);
+	QApplication::exit(status);
 }
 
 
Index: src/frontends/lyx_gui.h
===================================================================
--- src/frontends/lyx_gui.h	(Revision 14337)
+++ src/frontends/lyx_gui.h	(Arbeitskopie)
@@ -56,13 +56,14 @@ void parse_lyxrc();
  * Start the main event loop, after executing the given
  * batch commands, and loading the given documents
  */
-void start(std::string const & batch, std::vector<std::string> const & files,
-           unsigned int width, unsigned int height, int posx, int posy, bool maximize);
+int start(std::string const & batch, std::vector<std::string> const & files,
+          unsigned int width, unsigned int height, int posx, int posy,
+          bool maximize);
 
 /**
  * Enter the main event loop (\sa LyX::exec2)
  */
-void exec(int & argc, char * argv[]);
+int exec(int & argc, char * argv[]);
 
 /**
  * Synchronise all pending events.
@@ -70,7 +71,8 @@ void exec(int & argc, char * argv[]);
 void sync_events();
 
 /**
- * quit running LyX
+ * Quit running LyX. This may either quit directly or record the exit status
+ * and only stop the event loop.
  */
 void exit(int);
 
Index: src/lyxtextclasslist.C
===================================================================
--- src/lyxtextclasslist.C	(Revision 14337)
+++ src/lyxtextclasslist.C	(Arbeitskopie)
@@ -217,15 +217,16 @@ LyXTextClassList textclasslist;
 
 
 // Reads the style files
-void LyXSetStyle()
+bool LyXSetStyle()
 {
 	lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
 
 	if (!textclasslist.read()) {
 		lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
 			"during parsing.\n             Exiting." << endl;
-		exit(1);
+		return false;
 	}
 
 	lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
+	return true;
 }
Index: src/lyxtextclasslist.h
===================================================================
--- src/lyxtextclasslist.h	(Revision 14337)
+++ src/lyxtextclasslist.h	(Arbeitskopie)
@@ -24,7 +24,7 @@
 class LyXLayout;
 
 /// Reads the style files
-extern void LyXSetStyle();
+extern bool LyXSetStyle();
 
 ///
 class LyXTextClassList : boost::noncopyable {
Index: src/lyx_main.C
===================================================================
--- src/lyx_main.C	(Revision 14337)
+++ src/lyx_main.C	(Arbeitskopie)
@@ -113,6 +113,7 @@ void lyx_exit(int status)
 	// guarantees a return to the system, no application cleanup.
 	// This may cause troubles with not executed destructors.
 	if (lyx_gui::use_gui)
+		// lyx_gui::exit may return and only schedule the exit
 		lyx_gui::exit(status);
 	exit(status);
 }
@@ -123,7 +124,6 @@ void showFileError(string const & error)
 	Alert::warning(_("Could not read configuration file"),
 		   bformat(_("Error while reading the configuration file\n%1$s.\n"
 		     "Please check your installation."), error));
-	lyx_exit(EXIT_FAILURE);
 }
 
 
@@ -143,7 +143,7 @@ void reconfigureUserLyXDir()
 
 boost::scoped_ptr<LyX> LyX::singleton_;
 
-void LyX::exec(int & argc, char * argv[])
+int LyX::exec(int & argc, char * argv[])
 {
 	BOOST_ASSERT(!singleton_.get());
 	// We must return from this before launching the gui so that
@@ -151,7 +151,7 @@ void LyX::exec(int & argc, char * argv[]
 	// LyX::ref and LyX::cref.
 	singleton_.reset(new LyX);
 	// Start the real execution loop.
-	singleton_->priv_exec(argc, argv);
+	return singleton_->priv_exec(argc, argv);
 }
 
 
@@ -211,7 +211,7 @@ Buffer const * const LyX::updateInset(In
 }
 
 
-void LyX::priv_exec(int & argc, char * argv[])
+int LyX::priv_exec(int & argc, char * argv[])
 {
 	// Here we need to parse the command line. At least
 	// we need to parse for "-dbg" and "-help"
@@ -222,13 +222,13 @@ void LyX::priv_exec(int & argc, char * a
 
 	// Start the real execution loop.
 	if (lyx_gui::use_gui)
-		lyx_gui::exec(argc, argv);
+		return lyx_gui::exec(argc, argv);
 	else
-		exec2(argc, argv);
+		return exec2(argc, argv);
 }
 
 
-void LyX::exec2(int & argc, char * argv[])
+int LyX::exec2(int & argc, char * argv[])
 {
 	// check for any spurious extra arguments
 	// other than documents
@@ -236,14 +236,16 @@ void LyX::exec2(int & argc, char * argv[
 		if (argv[argi][0] == '-') {
 			lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
 				argv[argi]) << endl;
-			exit(1);
+			return EXIT_FAILURE;
 		}
 	}
 
 	// Initialization of LyX (reads lyxrc and more)
 	lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
-	init();
+	bool const success = init();
 	lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
+	if (!success)
+		return EXIT_FAILURE;
 
 	if (lyx_gui::use_gui)
 		lyx_gui::parse_lyxrc();
@@ -296,7 +298,7 @@ void LyX::exec2(int & argc, char * argv[
 			bool success = false;
 			if (last_loaded->dispatch(batch_command, &success)) {
 				quitLyX(false);
-				lyx_exit(!success);
+				return !success;
 			}
 		}
 		files.clear(); // the files are already loaded
@@ -341,12 +343,12 @@ void LyX::exec2(int & argc, char * argv[
 			height = 0;
 		}
 
-		lyx_gui::start(batch_command, files, width, height, posx, posy, maximize);
+		return lyx_gui::start(batch_command, files, width, height, posx, posy, maximize);
 
 	} else {
 		// Something went wrong above
 		quitLyX(false);
-		lyx_exit(EXIT_FAILURE);
+		return EXIT_FAILURE;
 	}
 }
 
@@ -458,7 +460,7 @@ void LyX::printError(ErrorItem const & e
 }
 
 
-void LyX::init()
+bool LyX::init()
 {
 #ifdef SIGHUP
 	signal(SIGHUP, error_handler);
@@ -489,7 +491,8 @@ void LyX::init()
 	//
 
 	// This one may have been distributed along with LyX.
-	readRcFile("lyxrc.dist");
+	if (!readRcFile("lyxrc.dist"))
+		return false;
 
 	// Set the PATH correctly.
 #if !defined (USE_POSIX_PACKAGING)
@@ -511,7 +514,8 @@ void LyX::init()
 	}
 
 	// This one is generated in user_support directory by lib/configure.py.
-	readRcFile("lyxrc.defaults");
+	if (!readRcFile("lyxrc.defaults"))
+		return false;
 
 	// Query the OS to know what formats are viewed natively
 	formats.setAutoOpen();
@@ -523,14 +527,18 @@ void LyX::init()
 	system_lcolor = lcolor;
 
 	// This one is edited through the preferences dialog.
-	readRcFile("preferences");
+	if (!readRcFile("preferences"))
+		return false;
 
-	readEncodingsFile("encodings");
-	readLanguagesFile("languages");
+	if (!readEncodingsFile("encodings"))
+		return false;
+	if (!readLanguagesFile("languages"))
+		return false;
 
 	// Load the layouts
 	lyxerr[Debug::INIT] << "Reading layouts..." << endl;
-	LyXSetStyle();
+	if (!LyXSetStyle())
+		return false;
 
 	if (lyx_gui::use_gui) {
 		// Set up bindings
@@ -539,7 +547,8 @@ void LyX::init()
 		toplevel_keymap->read(lyxrc.bind_file);
 
 		// Read menus
-		readUIFile(lyxrc.ui_file);
+		if (!readUIFile(lyxrc.ui_file))
+			return false;
 	}
 
 	if (lyxerr.debugging(Debug::LYXRC))
@@ -565,7 +574,7 @@ void LyX::init()
 		// close to zero. We therefore don't try to overcome this
 		// problem with e.g. asking the user for a new path and
 		// trying again but simply exit.
-		lyx_exit(EXIT_FAILURE);
+		return false;
 	}
 
 	if (lyxerr.debugging(Debug::INIT)) {
@@ -574,6 +583,7 @@ void LyX::init()
 
 	lyxerr[Debug::INIT] << "Reading session information '.lyx/session'..." << endl;
 	session_.reset(new lyx::Session(lyxrc.num_lastfiles));
+	return true;
 }
 
 
@@ -731,7 +741,7 @@ bool LyX::queryUserLyXDir(bool explicit_
 }
 
 
-void LyX::readRcFile(string const & name)
+bool LyX::readRcFile(string const & name)
 {
 	lyxerr[Debug::INIT] << "About to read " << name << "... ";
 
@@ -740,16 +750,19 @@ void LyX::readRcFile(string const & name
 
 		lyxerr[Debug::INIT] << "Found in " << lyxrc_path << endl;
 
-		if (lyxrc.read(lyxrc_path) < 0)
+		if (lyxrc.read(lyxrc_path) < 0) {
 			showFileError(name);
+			return false;
+		}
 	} else
 		lyxerr[Debug::INIT] << "Not found." << lyxrc_path << endl;
+	return true;
 
 }
 
 
 // Read the ui file `name'
-void LyX::readUIFile(string const & name)
+bool LyX::readUIFile(string const & name)
 {
 	enum Uitags {
 		ui_menuset = 1,
@@ -776,7 +789,7 @@ void LyX::readUIFile(string const & name
 				    << "' has been read already. "
 				    << "Is this an include loop?"
 				    << endl;
-		return;
+		return false;
 	}
 
 	lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
@@ -786,7 +799,7 @@ void LyX::readUIFile(string const & name
 	if (ui_path.empty()) {
 		lyxerr[Debug::INIT] << "Could not find " << name << endl;
 		showFileError(name);
-		return;
+		return false;
 	}
 	uifiles.push_back(name);
 
@@ -807,7 +820,8 @@ void LyX::readUIFile(string const & name
 		case ui_include: {
 			lex.next(true);
 			string const file = lex.getString();
-			readUIFile(file);
+			if (!readUIFile(file))
+				return false;
 			break;
 		}
 		case ui_menuset:
@@ -829,34 +843,37 @@ void LyX::readUIFile(string const & name
 			break;
 		}
 	}
+	return true;
 }
 
 
 // Read the languages file `name'
-void LyX::readLanguagesFile(string const & name)
+bool LyX::readLanguagesFile(string const & name)
 {
 	lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
 
 	string const lang_path = libFileSearch(string(), name);
 	if (lang_path.empty()) {
 		showFileError(name);
-		return;
+		return false;
 	}
 	languages.read(lang_path);
+	return true;
 }
 
 
 // Read the encodings file `name'
-void LyX::readEncodingsFile(string const & name)
+bool LyX::readEncodingsFile(string const & name)
 {
 	lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
 
 	string const enc_path = libFileSearch(string(), name);
 	if (enc_path.empty()) {
 		showFileError(name);
-		return;
+		return false;
 	}
 	encodings.read(enc_path);
+	return true;
 }
 
 
Index: src/lyx_main.h
===================================================================
--- src/lyx_main.h	(Revision 14337)
+++ src/lyx_main.h	(Arbeitskopie)
@@ -46,9 +46,9 @@ public:
 	 * objects lead either to harmless error messages on exit
 	 * ("Mutex destroy failure") or crashes (OS X).
 	 */
-	static void exec(int & argc, char * argv[]);
+	static int exec(int & argc, char * argv[]);
 	/// Execute LyX (inner execution loop, \sa exec)
-	void exec2(int & argc, char * argv[]);
+	int exec2(int & argc, char * argv[]);
 	static LyX & ref();
 	static LyX const & cref();
 
@@ -69,10 +69,10 @@ private:
 	static boost::scoped_ptr<LyX> singleton_;
 
 	LyX();
-	void priv_exec(int & argc, char * argv[]);
+	int priv_exec(int & argc, char * argv[]);
 
 	/// initial LyX set up
-	void init();
+	bool init();
 	/// set up the default key bindings
 	void defaultKeyBindings(kb_keymap * kbmap);
 	/// set up the default dead key bindings if requested
@@ -85,13 +85,13 @@ private:
 	 */
 	bool queryUserLyXDir(bool explicit_userdir);
 	/// read lyxrc/preferences
-	void readRcFile(std::string const & name);
+	bool readRcFile(std::string const & name);
 	/// read the given ui (menu/toolbar) file
-	void readUIFile(std::string const & name);
+	bool readUIFile(std::string const & name);
 	/// read the given languages file
-	void readLanguagesFile(std::string const & name);
+	bool readLanguagesFile(std::string const & name);
 	/// read the given encodings file
-	void readEncodingsFile(std::string const & name);
+	bool readEncodingsFile(std::string const & name);
 	/// parsing of non-gui LyX options. Returns true if gui
 	bool easyParse(int & argc, char * argv[]);
 	/// shows up a parsing error on screen

Reply via email to