1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8 						main.cpp  -  description
9 							-------------------
10 	begin                : Fre Apr  6 21:47:55 CEST 2001
11 	copyright            : (C) 2001 by Franz Schmid
12 	email                : Franz.Schmid@altmuehlnet.de
13 	copyright            : (C) 2004 by Alessandro Rimoldi
14 	email                : http://ideale.ch/contact
15 	copyright            : (C) 2005 by Craig Bradney
16 	email                : cbradney@zip.com.au
17 ***************************************************************************/
18 
19 /***************************************************************************
20 *                                                                         *
21 *   This program is free software; you can redistribute it and/or modify  *
22 *   it under the terms of the GNU General Public License as published by  *
23 *   the Free Software Foundation; either version 2 of the License, or     *
24 *   (at your option) any later version.                                   *
25 *                                                                         *
26 ***************************************************************************/
27 
28 #include <iostream>
29 #include <csignal>
30 
31 #include <QApplication>
32 #include <QMessageBox>
33 
34 #include "scribusapp.h"
35 #include "scribuscore.h"
36 #include "scribus.h"
37 #include "scimagecachemanager.h"
38 
39 #include "scconfig.h"
40 
41 int mainApp(int argc, char **argv);
42 void initCrashHandler();
43 static void defaultCrashHandler(int sig);
44 
45 ScribusCore SCRIBUS_API *ScCore;
46 ScribusQApp SCRIBUS_API *ScQApp;
47 bool emergencyActivated;
48 
main(int argc,char * argv[])49 int main(int argc, char *argv[])
50 {
51 	return mainApp(argc, argv);
52 }
53 
54 /*!
55 \author Franz Schmid
56 \author Alessandro Rimoldi
57 \author Craig Bradney
58 \date Mon Feb  9 14:07:46 CET 2004
59 \brief Launches the Gui
60 \param argc Number of arguments passed to Scribus
61 \param argv *argv list of the arguments passed to Scribus
62 \retval int Error code from the execution of Scribus
63 */
mainApp(int argc,char ** argv)64 int mainApp(int argc, char **argv)
65 {
66 	emergencyActivated = false;
67 
68 	ScribusQApp::setAttribute(Qt::AA_EnableHighDpiScaling);
69 	ScribusQApp app(argc, argv);
70 	initCrashHandler();
71 	app.parseCommandLine();
72 	int appRetVal=app.init();
73 	if (appRetVal==EXIT_FAILURE)
74 		return(EXIT_FAILURE);
75 	if (app.useGUI)
76 		return app.exec();
77 	return EXIT_SUCCESS;
78 }
79 
initCrashHandler()80 void initCrashHandler()
81 {
82 	typedef void (*HandlerType)(int);
83 	HandlerType handler	= nullptr;
84 	handler = defaultCrashHandler;
85 	if (!handler)
86 		handler = SIG_DFL;
87 	sigset_t mask;
88 	sigemptyset(&mask);
89 #ifdef SIGSEGV
90 	signal (SIGSEGV, handler);
91 	sigaddset(&mask, SIGSEGV);
92 #endif
93 #ifdef SIGFPE
94 	signal (SIGFPE, handler);
95 	sigaddset(&mask, SIGFPE);
96 #endif
97 #ifdef SIGILL
98 	signal (SIGILL, handler);
99 	sigaddset(&mask, SIGILL);
100 #endif
101 #ifdef SIGABRT
102 	signal (SIGABRT, handler);
103 	sigaddset(&mask, SIGABRT);
104 #endif
105 	sigprocmask(SIG_UNBLOCK, &mask, nullptr);
106 }
107 
defaultCrashHandler(int sig)108 void defaultCrashHandler(int sig)
109 {
110 	static int crashRecursionCounter = 0;
111 	crashRecursionCounter++;
112 	signal(SIGALRM, SIG_DFL);
113 	if (crashRecursionCounter < 2)
114 	{
115 		emergencyActivated=true;
116 		crashRecursionCounter++;
117 		QString sigHdr=QObject::tr("Scribus Crash");
118 		QString sigLine="-------------";
119 		QString sigMsg=QObject::tr("Scribus crashes due to Signal #%1").arg(sig);
120 		std::cout << sigHdr.toStdString() << std::endl;
121 		std::cout << sigLine.toStdString() << std::endl;
122 		std::cout << sigMsg.toStdString() << std::endl;
123 		ScImageCacheManager::instance().removeMasterLock();
124 		if (ScribusQApp::useGUI)
125 		{
126 			ScCore->closeSplash();
127 			ScribusMainWindow* mainWin = ScCore->primaryMainWindow();
128 			if (mainWin)
129 			{
130 				ScMessageBox::critical(mainWin, sigHdr, sigMsg);
131 				mainWin->emergencySave();
132 				mainWin->close();
133 			}
134 		}
135 		alarm(300);
136 	}
137 	exit(255);
138 }
139