0000777: Shared libraries call exit

This commit is contained in:
wmayer 2012-07-05 15:24:28 +02:00
parent d9971311ed
commit 857454aad5
11 changed files with 132 additions and 110 deletions

View File

@ -1188,13 +1188,13 @@ void Application::processCmdLineFiles(void)
Application::_pcSingleton->openDocument(File.filePath().c_str()); Application::_pcSingleton->openDocument(File.filePath().c_str());
} }
else if (File.hasExtension("fcscript")||File.hasExtension("fcmacro")) { else if (File.hasExtension("fcscript")||File.hasExtension("fcmacro")) {
Base::Interpreter().runFile(File.filePath().c_str(), false); Base::Interpreter().runFile(File.filePath().c_str(), true);
} }
else if (File.hasExtension("py")) { else if (File.hasExtension("py")) {
//FIXME: Does this make any sense? I think we should do the ame as for //FIXME: Does this make any sense? I think we should do the ame as for
// fcmacro or fcscript. // fcmacro or fcscript.
//Base::Interpreter().loadModule(File.fileNamePure().c_str()); //Base::Interpreter().loadModule(File.fileNamePure().c_str());
Base::Interpreter().runFile(File.filePath().c_str(), false); Base::Interpreter().runFile(File.filePath().c_str(), true);
} }
else { else {
std::vector<std::string> mods = App::GetApplication().getImportModules(Ext.c_str()); std::vector<std::string> mods = App::GetApplication().getImportModules(Ext.c_str());
@ -1210,8 +1210,7 @@ void Application::processCmdLineFiles(void)
} }
} }
catch (const Base::SystemExitException&) { catch (const Base::SystemExitException&) {
Base::PyGILStateLocker locker; throw; // re-throw to main() function
Base::Interpreter().systemExit();
} }
catch (const Base::Exception& e) { catch (const Base::Exception& e) {
Console().Error("Exception while processing file: %s [%s]\n", File.filePath().c_str(), e.what()); Console().Error("Exception while processing file: %s [%s]\n", File.filePath().c_str(), e.what());
@ -1523,20 +1522,23 @@ void Application::ParseOptions(int ac, char ** av)
notify(vm); notify(vm);
} }
catch (const std::exception& e) { catch (const std::exception& e) {
cerr << e.what() << endl << endl << visible << endl; std::stringstream str;
exit(1); str << e.what() << endl << endl << visible << endl;
throw UnknownProgramOption(str.str());
} }
catch (...) { catch (...) {
cerr << "Wrong or unknown option, bailing out!" << endl << endl << visible << endl; std::stringstream str;
exit(1); str << "Wrong or unknown option, bailing out!" << endl << endl << visible << endl;
throw UnknownProgramOption(str.str());
} }
if (vm.count("help")) { if (vm.count("help")) {
cout << mConfig["ExeName"] << endl << endl; std::stringstream str;
cout << "For detailed descripton see http://free-cad.sf.net" << endl<<endl; str << mConfig["ExeName"] << endl << endl;
cout << "Usage: " << mConfig["ExeName"] << " [options] File1 File2 ..." << endl << endl; str << "For detailed descripton see http://free-cad.sf.net" << endl<<endl;
cout << visible << endl; str << "Usage: " << mConfig["ExeName"] << " [options] File1 File2 ..." << endl << endl;
exit(0); str << visible << endl;
throw Base::ProgramInformation(str.str());
} }
if (vm.count("response-file")) { if (vm.count("response-file")) {
@ -1544,9 +1546,10 @@ void Application::ParseOptions(int ac, char ** av)
std::ifstream ifs(vm["response-file"].as<string>().c_str()); std::ifstream ifs(vm["response-file"].as<string>().c_str());
if (!ifs) { if (!ifs) {
Base::Console().Error("Could no open the response file\n"); Base::Console().Error("Could no open the response file\n");
cerr << "Could no open the response file: '" std::stringstream str;
<< vm["response-file"].as<string>() << "'" << endl; str << "Could no open the response file: '"
exit(1); << vm["response-file"].as<string>() << "'" << endl;
throw Base::UnknownProgramOption(str.str());
} }
// Read the whole file into a string // Read the whole file into a string
stringstream ss; stringstream ss;
@ -1562,9 +1565,10 @@ void Application::ParseOptions(int ac, char ** av)
} }
if (vm.count("version")) { if (vm.count("version")) {
std::cout << mConfig["ExeName"] << " " << mConfig["ExeVersion"] std::stringstream str;
<< " Revision: " << mConfig["BuildRevision"] << std::endl; str << mConfig["ExeName"] << " " << mConfig["ExeVersion"]
exit(0); << " Revision: " << mConfig["BuildRevision"] << std::endl;
throw Base::ProgramInformation(str.str());
} }
if (vm.count("console")) { if (vm.count("console")) {

View File

@ -202,6 +202,40 @@ AbnormalProgramTermination::AbnormalProgramTermination(const AbnormalProgramTerm
// --------------------------------------------------------- // ---------------------------------------------------------
UnknownProgramOption::UnknownProgramOption(const char * sMessage)
: Exception(sMessage)
{
}
UnknownProgramOption::UnknownProgramOption(const std::string& sMessage)
: Exception(sMessage)
{
}
UnknownProgramOption::UnknownProgramOption(const UnknownProgramOption &inst)
: Exception(inst)
{
}
// ---------------------------------------------------------
ProgramInformation::ProgramInformation(const char * sMessage)
: Exception(sMessage)
{
}
ProgramInformation::ProgramInformation(const std::string& sMessage)
: Exception(sMessage)
{
}
ProgramInformation::ProgramInformation(const ProgramInformation &inst)
: Exception(inst)
{
}
// ---------------------------------------------------------
#if defined(__GNUC__) && defined (FC_OS_LINUX) #if defined(__GNUC__) && defined (FC_OS_LINUX)
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>

View File

@ -174,6 +174,38 @@ public:
virtual ~AbnormalProgramTermination() throw() {} virtual ~AbnormalProgramTermination() throw() {}
}; };
/**
* The UnknownProgramOption can be used to indicate an unknown program option.
* @author Werner Mayer
*/
class BaseExport UnknownProgramOption : public Exception
{
public:
/// Construction
UnknownProgramOption(const char * sMessage);
UnknownProgramOption(const std::string& sMessage);
/// Construction
UnknownProgramOption(const UnknownProgramOption &inst);
/// Destruction
virtual ~UnknownProgramOption() throw() {}
};
/**
* The ProgramInformation can be used to show information about the program.
* @author Werner Mayer
*/
class BaseExport ProgramInformation : public Exception
{
public:
/// Construction
ProgramInformation(const char * sMessage);
ProgramInformation(const std::string& sMessage);
/// Construction
ProgramInformation(const ProgramInformation &inst);
/// Destruction
virtual ~ProgramInformation() throw() {}
};
inline void Exception::setMessage(const char * sMessage) inline void Exception::setMessage(const char * sMessage)
{ {

View File

@ -190,7 +190,6 @@ void InterpreterSingleton::runInteractiveString(const char *sCmd)
presult = PyRun_String(sCmd, Py_single_input, dict, dict); /* eval direct */ presult = PyRun_String(sCmd, Py_single_input, dict, dict); /* eval direct */
if (!presult) { if (!presult) {
if (PyErr_ExceptionMatches(PyExc_SystemExit)) { if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
//systemExit();
throw SystemExitException(); throw SystemExitException();
} }
/* get latest python exception information */ /* get latest python exception information */

View File

@ -26,6 +26,7 @@
#ifndef _PreComp_ #ifndef _PreComp_
# include <assert.h> # include <assert.h>
# include <stdio.h> # include <stdio.h>
# include <QApplication>
# include <QFile> # include <QFile>
# include <QTextStream> # include <QTextStream>
#endif #endif
@ -232,9 +233,7 @@ void MacroManager::run(MacroType eType,const char *sName)
Base::Interpreter().runFile(sName, this->localEnv); Base::Interpreter().runFile(sName, this->localEnv);
} }
catch (const Base::SystemExitException&) { catch (const Base::SystemExitException&) {
Base::PyGILStateLocker lock; qApp->quit();
PyErr_Clear();
Base::Interpreter().systemExit();
} }
catch (const Base::PyException& e) { catch (const Base::PyException& e) {
Base::Console().Error("%s%s: %s\n", Base::Console().Error("%s%s: %s\n",

View File

@ -52,6 +52,7 @@
#include <Base/Parameter.h> #include <Base/Parameter.h>
#include <Base/Exception.h> #include <Base/Exception.h>
#include <Base/FileInfo.h> #include <Base/FileInfo.h>
#include <Base/Interpreter.h>
#include <Base/Persistence.h> #include <Base/Persistence.h>
#include <Base/Stream.h> #include <Base/Stream.h>
#include <Base/Reader.h> #include <Base/Reader.h>
@ -1152,7 +1153,13 @@ void MainWindow::showMainWindow()
void MainWindow::delayedStartup() void MainWindow::delayedStartup()
{ {
// processing all command line files // processing all command line files
App::Application::processCmdLineFiles(); try {
App::Application::processCmdLineFiles();
}
catch (const Base::SystemExitException&) {
QApplication::quit();
return;
}
const std::map<std::string,std::string>& cfg = App::Application::Config(); const std::map<std::string,std::string>& cfg = App::Application::Config();
std::map<std::string,std::string>::const_iterator it = cfg.find("StartHidden"); std::map<std::string,std::string>::const_iterator it = cfg.find("StartHidden");

View File

@ -23,6 +23,7 @@
#include "PreCompiled.h" #include "PreCompiled.h"
#ifndef _PreComp_ #ifndef _PreComp_
# include <QApplication>
# include <QClipboard> # include <QClipboard>
# include <QDockWidget> # include <QDockWidget>
# include <QGridLayout> # include <QGridLayout>
@ -700,12 +701,13 @@ void PythonConsole::runSource(const QString& line)
catch (const Base::SystemExitException&) { catch (const Base::SystemExitException&) {
ParameterGrp::handle hPrefGrp = getWindowParameter(); ParameterGrp::handle hPrefGrp = getWindowParameter();
bool check = hPrefGrp->GetBool("CheckSystemExit",true); bool check = hPrefGrp->GetBool("CheckSystemExit",true);
if (!check) Base::Interpreter().systemExit(); if (!check) qApp->quit();
int ret = QMessageBox::question(this, tr("System exit"), tr("The application is still running.\nDo you want to exit without saving your data?"), int ret = QMessageBox::question(this, tr("System exit"), tr("The application is still running.\nDo you want to exit without saving your data?"),
QMessageBox::Yes, QMessageBox::No|QMessageBox::Escape|QMessageBox::Default); QMessageBox::Yes, QMessageBox::No|QMessageBox::Escape|QMessageBox::Default);
if (ret == QMessageBox::Yes) { if (ret == QMessageBox::Yes) {
Base::Interpreter().systemExit(); qApp->quit();
} else { }
else {
PyErr_Clear(); PyErr_Clear();
} }
} }

View File

@ -311,7 +311,7 @@ void LightManip(SoSeparator * root)
in.setBuffer((void *)scenegraph, std::strlen(scenegraph)); in.setBuffer((void *)scenegraph, std::strlen(scenegraph));
SoSeparator * _root = SoDB::readAll( &in ); SoSeparator * _root = SoDB::readAll( &in );
root->addChild(_root); root->addChild(_root);
if ( root == NULL ) exit( 1 ); // Shouldn't happen. if ( root == NULL ) return; // Shouldn't happen.
root->ref(); root->ref();
const char * pointlightnames[3] = { "RedLight", "GreenLight", "BlueLight" }; const char * pointlightnames[3] = { "RedLight", "GreenLight", "BlueLight" };
@ -323,7 +323,7 @@ void LightManip(SoSeparator * root)
sa.setSearchingAll( FALSE ); sa.setSearchingAll( FALSE );
sa.apply( root ); sa.apply( root );
SoPath * path = sa.getPath(); SoPath * path = sa.getPath();
if ( path == NULL) exit( 1 ); // Shouldn't happen. if ( path == NULL) return; // Shouldn't happen.
SoPointLightManip * manip = new SoPointLightManip; SoPointLightManip * manip = new SoPointLightManip;
manip->replaceNode( path ); manip->replaceNode( path );
@ -446,7 +446,7 @@ void AnimationTexture(SoSeparator * root)
texturetimer->schedule(); texturetimer->schedule();
// Scene graph // Scene graph
if ( root == NULL ) exit( 1 ); // Shouldn't happen. if ( root == NULL ) return; // Shouldn't happen.
root->ref(); // prevent from being deleted because of the still running timer sensor root->ref(); // prevent from being deleted because of the still running timer sensor
// SoSeparator * root = new SoSeparator; // SoSeparator * root = new SoSeparator;
// root->ref(); // root->ref();

View File

@ -37,6 +37,7 @@
#include <stdio.h> #include <stdio.h>
#include <sstream> #include <sstream>
#include <iostream>
// FreeCAD Base header // FreeCAD Base header
#include <Base/Console.h> #include <Base/Console.h>
@ -89,6 +90,14 @@ int main( int argc, char ** argv )
// Inits the Application // Inits the Application
App::Application::init(argc,argv); App::Application::init(argc,argv);
} }
catch (const Base::UnknownProgramOption& e) {
std::cerr << e.what();
exit(1);
}
catch (const Base::ProgramInformation& e) {
std::cout << e.what();
exit(0);
}
catch (const Base::Exception& e) { catch (const Base::Exception& e) {
std::string appName = App::Application::Config()["ExeName"]; std::string appName = App::Application::Config()["ExeName"];
std::stringstream msg; std::stringstream msg;

View File

@ -161,83 +161,6 @@ private:
QDomDocument domDocument; QDomDocument domDocument;
}; };
class ProgramOptions
{
public:
ProgramOptions()
{
newcout = new ProgramOptionsStream(out);
oldcout = std::cout.rdbuf(newcout);
out.reserve(80);
newcerr = new ProgramOptionsStream(err);
oldcerr = std::cerr.rdbuf(newcerr);
err.reserve(80);
error = true;
::atexit(ProgramOptions::failure);
}
~ProgramOptions()
{
std::cout.rdbuf(oldcout);
delete newcout;
std::cerr.rdbuf(oldcerr);
delete newcerr;
error = false;
}
static void failure()
{
if (error) {
int argc=0;
QApplication app(argc,0);
QString appName = QString::fromAscii(App::Application::Config()["ExeName"].c_str());
if (!err.empty()) {
QString msg = QString::fromAscii(err.c_str());
QString s = QLatin1String("<pre>") + msg + QLatin1String("</pre>");
QMessageBox::critical(0, appName, s);
}
else if (!out.empty()) {
QString msg = QString::fromAscii(out.c_str());
QString s = QLatin1String("<pre>") + msg + QLatin1String("</pre>");
QMessageBox::information(0, appName, s);
}
}
}
private:
class ProgramOptionsStream : public std::streambuf
{
public:
ProgramOptionsStream(std::string& s) : buf(s)
{
}
int overflow(int c = EOF)
{
if (c != EOF)
buf.push_back((char)c);
return c;
}
int sync()
{
return 0;
}
private:
std::string& buf;
};
private:
friend class ProgramOptionsStream;
std::streambuf* oldcout;
std::streambuf* newcout;
std::streambuf* oldcerr;
std::streambuf* newcerr;
static std::string out, err;
static bool error;
};
bool ProgramOptions::error = false;
std::string ProgramOptions::out;
std::string ProgramOptions::err;
#if defined (FC_OS_LINUX) || defined(FC_OS_BSD) #if defined (FC_OS_LINUX) || defined(FC_OS_BSD)
QString myDecoderFunc(const QByteArray &localFileName) QString myDecoderFunc(const QByteArray &localFileName)
{ {
@ -300,10 +223,25 @@ int main( int argc, char ** argv )
App::Application::Config()["RunMode"] = "Gui"; App::Application::Config()["RunMode"] = "Gui";
// Inits the Application // Inits the Application
ProgramOptions po;
App::Application::init(argc,argv); App::Application::init(argc,argv);
Gui::Application::initApplication(); Gui::Application::initApplication();
} }
catch (const Base::UnknownProgramOption& e) {
QApplication app(argc,argv);
QString appName = QString::fromAscii(App::Application::Config()["ExeName"].c_str());
QString msg = QString::fromAscii(e.what());
QString s = QLatin1String("<pre>") + msg + QLatin1String("</pre>");
QMessageBox::critical(0, appName, s);
exit(1);
}
catch (const Base::ProgramInformation& e) {
QApplication app(argc,argv);
QString appName = QString::fromAscii(App::Application::Config()["ExeName"].c_str());
QString msg = QString::fromAscii(e.what());
QString s = QLatin1String("<pre>") + msg + QLatin1String("</pre>");
QMessageBox::information(0, appName, s);
exit(0);
}
catch (const Base::Exception& e) { catch (const Base::Exception& e) {
// Popup an own dialog box instead of that one of Windows // Popup an own dialog box instead of that one of Windows
QApplication app(argc,argv); QApplication app(argc,argv);

View File

@ -102,8 +102,6 @@ void CmdRaytracingWriteCamera::activated(int iMsg)
SoInput in; SoInput in;
in.setBuffer((void*)ppReturn,std::strlen(ppReturn)); in.setBuffer((void*)ppReturn,std::strlen(ppReturn));
//if (!in.openFile(filename)) { exit(1); }
SoNode* rootNode; SoNode* rootNode;
SoDB::read(&in,rootNode); SoDB::read(&in,rootNode);