1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
//-----------------------------------------------------------------------------
//
// KDE Help main
//
// (c) Martin R. Jones 1996
//
#include <qfileinf.h>
#include "helpwin.h"
#include <signal.h>
#include <sys/stat.h>
#include <qmsgbox.h>
#include <kapp.h>
#include <drag.h>
#include <klocale.h>
#include <kwm.h>
#include <time.h>
#include "error.h"
#include "khelp.h"
#include "mainwidget.h"
#include <X11/Xatom.h>
#ifdef __FreeBSD__
#include <floatingpoint.h>
#endif
static void cleanup( int );
void catchSignals();
static int msgqid = -1;
static QString pidFile;
//-----------------------------------------------------------------------------
// timer used to monitor msg queue for request for new window
class Timer : public QObject
{
public:
Timer()
{
startTimer( 100 );
}
protected:
virtual void timerEvent( QTimerEvent * );
};
void Timer::timerEvent( QTimerEvent * )
{
KHelpMsg buf, retbuf;
KHelpMain *helpWin;
if ( buf.recv( msgqid, 1L ) != -1 )
{
printf( "got request: %s\n", buf.getMsg() );
retbuf.setType( 2L );
retbuf.send( msgqid );
helpWin = new KHelpMain;
QString tmp = buf.getMsg();
QString url = strtok( tmp.data(), " " );
QString props = strtok( NULL, " " );
if ( !strchr( url, ':' ) )
{
url = "file:";
url += buf.getMsg();
}
if ( helpWin->openURL( url ) )
delete helpWin;
else
{
if ( !props.isEmpty() )
KWM::setProperties( helpWin->winId(), props );
helpWin->show();
}
}
}
void errorHandler( int type, char *msg )
{
QApplication::setOverrideCursor( arrowCursor );
QMessageBox::message( klocale->translate("Error"),
msg,
klocale->translate("Ok") );
QApplication::restoreOverrideCursor();
if ( type == ERR_FATAL )
{
if (msgqid >= 0)
msgctl( msgqid, IPC_RMID, 0 );
remove( pidFile );
exit(1);
}
}
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
int i;
QString url, initDoc;
FILE *fp;
for ( i = 1; i < argc; i++ )
{
if ( argv[i][0] == '-' )
{
// skip caption
if ( strcasecmp( argv[i], "-caption" ) == 0 )
i++;
if ( strcasecmp( argv[i], "-icon" ) == 0 )
i++;
if ( strcasecmp( argv[i], "-miniicon" ) == 0 )
i++;
continue;
}
initDoc = argv[i];
}
if ( initDoc.isEmpty() )
{
initDoc = "file:";
initDoc += kapp->kde_htmldir().copy();
initDoc += "/default/kdehelp/main.html";
}
url = initDoc;
if ( !strchr( url, ':' ) )
{
if ( initDoc[0] == '.' || initDoc[0] != '/' )
{
QFileInfo fi( initDoc );
initDoc = fi.absFilePath();
}
url = "file:";
url += initDoc;
}
// create data directory if necessary
QString p = getenv( "HOME" );
QString rcDir = p + "/.kde/share/apps";
if ( access( rcDir, F_OK ) )
mkdir( rcDir, 0740 );
rcDir += "/kdehelp";
if ( access( rcDir, F_OK ) )
mkdir( rcDir, 0740 );
pidFile = rcDir + "/kdehelp.pid";
// if there is a pidFile then this is not the first instance of kdehelp
if ( ( fp = fopen( pidFile, "r" ) ) != NULL )
{
KHelpMsg buf;
int pid;
QString msg = url;
buf.setType( 1L );
buf.setMsg( msg );
fscanf( fp, "%d %d", &pid, &msgqid );
// if this fails I assume that the pid file is left over from bad exit
// and continue on
//
if ( buf.send( msgqid ) != -1)
{
// if we don't receive a reply within 3secs assume previous
// instance of kdehelp died an unnatural death.
// How should this stuff be handled properly?
//
time_t start = time(NULL);
while ( time(NULL) - start < 3 )
{
if (buf.recv( msgqid, 2L, IPC_NOWAIT ) != -1)
{
fclose( fp );
exit(0);
}
}
msgctl( msgqid, IPC_RMID, NULL );
}
fclose( fp );
}
// This is the first instance so create a pid/msgqid file
key_t key = ftok( getenv( "HOME" ), (char)rand() );
msgqid = msgget( key, IPC_CREAT | 0600 );
fp = fopen( pidFile, "w" );
fprintf( fp, "%ld %d\n", (long)getpid(), msgqid );
fclose( fp );
// so that everything is cleaned up
catchSignals();
Timer *timer = new Timer;
// error handler for info and man stuff
Error.SetHandler( (void (*)(int, const char *))errorHandler );
KApplication a( argc, argv, "kdehelp" );
KHelpMain *helpWin;
if ( a.isRestored() )
{
int n = 1;
while ( KTopLevelWidget::canBeRestored( n ) )
{
helpWin = new KHelpMain;
helpWin->restore( n );
n++;
}
a.exec();
}
else
{
helpWin = new KHelpMain;
if ( !helpWin->openURL( url ) )
{
helpWin->show();
a.exec();
}
}
delete timer;
msgctl( msgqid, IPC_RMID, 0 );
remove( pidFile );
}
// make sure the pid file is cleaned up when exiting unexpectedly.
//
void catchSignals()
{
// signal(SIGHUP, cleanup); /* Hangup */
signal(SIGINT, cleanup); /* Interrupt */
signal(SIGTERM, cleanup); /* Terminate */
// signal(SIGCHLD, cleanup);
signal(SIGABRT, cleanup);
signal(SIGALRM, cleanup);
signal(SIGFPE, cleanup);
signal(SIGILL, cleanup);
signal(SIGPIPE, cleanup);
signal(SIGQUIT, cleanup);
// signal(SIGSEGV, cleanup);
#ifdef SIGBUS
signal(SIGBUS, cleanup);
#endif
#ifdef SIGPOLL
signal(SIGPOLL, cleanup);
#endif
#ifdef SIGSYS
signal(SIGSYS, cleanup);
#endif
#ifdef SIGTRAP
signal(SIGTRAP, cleanup);
#endif
#ifdef SIGVTALRM
signal(SIGVTALRM, cleanup);
#endif
#ifdef SIGXCPU
signal(SIGXCPU, cleanup);
#endif
#ifdef SIGXFSZ
signal(SIGXFSZ, cleanup);
#endif
}
// remove pid file
static void cleanup( int sig )
{
printf( "cleanup: signal = %d\n", sig );
if (msgqid >= 0)
msgctl( msgqid, IPC_RMID, 0 );
remove( pidFile );
exit(0);
}
|