-
Notifications
You must be signed in to change notification settings - Fork 1
/
browser.c
374 lines (338 loc) · 11.2 KB
/
browser.c
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* Copyright (c) 2002 Berliner Speicherring-Gesellschaft fuer Synchrotron-
* Strahlung mbH (BESSY).
* Copyright (c) 2002 Southeastern Universities Research Association, as
* Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/* browser.c */
/************************DESCRIPTION***********************************
Invokes Netscape browser
Original Author : Kenneth Evans, Jr.
**********************************************************************/
/* Note that there are separate WIN32 and UNIX versions */
#define DEBUG 0
#ifndef WIN32
/*************************************************************************/
/*************************************************************************/
/* Netscape UNIX Version */
/*************************************************************************/
/*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/wait.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xmu/WinUtil.h>
#ifndef NETSCAPEPATH
#define NETSCAPEPATH "netscape"
#endif
/* Function prototypes */
extern int kill(pid_t, int); /* May not be defined for strict ANSI */
int callBrowser(char *url);
static Window checkNetscapeWindow(Window w);
static int execute(char *s);
static Window findNetscapeWindow(void);
static int ignoreXError(Display *display, XErrorEvent *xev);
/* Global variables */
extern Display *display;
/**************************** callBrowser ********************************/
int callBrowser(char *url)
/* Returns non-zero on success, 0 on failure */
/* url is the URL that the browser is to display */
/* or "quit" to terminate the browser */
{
int (*oldhandler)(Display *, XErrorEvent *);
static Window netscapew=(Window)0;
static pid_t pid=0;
int status;
char command[BUFSIZ];
char *envstring;
/* Handle quit */
if(!strcmp(url,"quit")) {
if (pid) {
kill(pid,SIGTERM);
pid=0;
}
return 3;
}
/* Set handler to ignore possible BadWindow error */
/* (Would prefer a routine that tells if the window is defined) */
oldhandler=XSetErrorHandler(ignoreXError);
/* Check if the stored window value is valid */
netscapew=checkNetscapeWindow(netscapew);
/* Reset error handler */
XSetErrorHandler(oldhandler);
/* If stored window is not valid, look for a valid one */
if(!netscapew) {
netscapew=findNetscapeWindow();
/* If no window found, exec Netscape */
if(!netscapew) {
envstring=getenv("BROWSER");
if(!envstring) envstring=getenv("NETSCAPEPATH");
if(!envstring) {
sprintf(command,"%s -install '%s' &",NETSCAPEPATH,url);
}
else {
sprintf(command,"%s -install '%s' &",envstring,url);
}
#if DEBUG
printf("execute(before): cmd=%s\n",command);
#endif
status=execute(command);
#if DEBUG
printf("execute(after): cmd=%s status=%d\n",command,status);
#endif
return 1;
}
}
/* Netscape window is valid, send url via -remote */
/* (Use -id for speed) */
envstring=getenv("BROWSER");
if(!envstring) envstring=getenv("NETSCAPEPATH");
if(!envstring) {
sprintf(command,"%s -id 0x%x -remote 'openURL(%s)' &",
NETSCAPEPATH,(unsigned int)netscapew,url);
}
else {
sprintf(command,"%s -id 0x%x -remote 'openURL(%s)' &",
envstring,(unsigned int)netscapew,url);
}
#if DEBUG
printf("execute(before): cmd=%s\n",command);
#endif
status=execute(command);
#if DEBUG
printf("execute(after): cmd=%s status=%d\n",command,status);
#endif
return 2;
}
/**************************** checkNetscapeWindow ************************/
static Window checkNetscapeWindow(Window w)
/* Checks if this window is the Netscape window and returns the window
* if it is or 0 otherwise */
{
Window wfound=(Window)0;
static Atom typeatom,versionatom=(Atom)0;
unsigned long nitems,bytesafter;
int format,status;
unsigned char *version=NULL;
/* If window is NULL, return it */
if(!w) return w;
/* Get the atom for the version property (once) */
if(!versionatom) versionatom=XInternAtom(display,"_MOZILLA_VERSION",False);
/* Get the version property for this window if it exists */
status=XGetWindowProperty(display,w,versionatom,0,
(65536/sizeof(long)),False,AnyPropertyType,
&typeatom,&format,&nitems,&bytesafter,&version);
/* If the version property exists, it is the Netscape window */
if(version && status == Success) wfound=w;
#if DEBUG
printf("XGetWindowProperty: status=%d version=%d w=%x wfound=%x\n",
status,version,w,wfound);
#endif
/* Free space and return */
if(version) XFree((void *)version);
return wfound;
}
/**************************** execute ************************************/
static int execute(char *s)
/* From O'Reilly, Vol. 1, p. 438 */
{
int status,pid,w;
register void (*istat)(),(*qstat)();
if((pid=fork()) == 0) {
signal(SIGINT,SIG_DFL);
signal(SIGQUIT,SIG_DFL);
signal(SIGHUP,SIG_DFL);
execl("/bin/sh","sh","-c",s,(char *)0);
_exit(127);
}
istat=signal(SIGINT,SIG_IGN);
qstat=signal(SIGQUIT,SIG_IGN);
while((w=wait(&status)) != pid && w != -1) ;
if(w == -1) status=-1;
signal(SIGINT,istat);
signal(SIGQUIT,qstat);
return(status);
}
/**************************** findNetscapeWindow *************************/
static Window findNetscapeWindow(void)
{
int screen=DefaultScreen(display);
Window rootwindow=RootWindow(display,screen);
Window *children,dummy,w,wfound=(Window)0;
unsigned int nchildren;
int i;
/* Get the root window tree */
if(!XQueryTree(display,rootwindow,&dummy,&dummy,&children,&nchildren))
return (Window)0;
/* Look at the children from the top of the stacking order */
for(i=nchildren-1; i >= 0; i--) {
w=XmuClientWindow(display,children[i]);
/* Check if this is the Netscape window */
#if DEBUG
printf("Child %d ",i);
#endif
wfound=checkNetscapeWindow(w);
if(wfound) break;
}
if(children) XFree((void *)children);
return wfound;
}
/**************************** ignoreXError *******************************/
static int ignoreXError(Display *display, XErrorEvent *xev)
{
#if DEBUG
printf("In ignoreXError\n");
#endif
return 0;
}
#else /*ifndef WIN32 */
/*************************************************************************/
/*************************************************************************/
/* WIN32 Version */
/*************************************************************************/
/*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <string.h>
#include <errno.h>
/* void medmPrintf(int priority, char *format, ...); */
int callBrowser(char *url);
/**************************** callBrowser (WIN32) ************************/
int callBrowser(char *url)
/* Returns non-zero on success, 0 on failure */
/* Should use the default browser */
/* Does nothing with "quit" */
{
static int first=1;
static char *ComSpec;
char command[BUFSIZ];
intptr_t status;
/* Handle quit */
if(!strcmp(url,"quit")) {
/* For compatibility, but do nothing */
return(3);
}
/* Get ComSpec for the command shell (should be defined) */
if (first) {
first=0;
ComSpec = getenv("ComSpec");
}
if (!ComSpec) return(0); /* Abort with no message like the UNIX version*/
/* Spawn the process that handles a url */
#if 0
/* Works, command window that goes away */
sprintf(command,"start \"%s\"",url);
status = _spawnl(_P_WAIT, ComSpec, ComSpec, "/C", command, NULL);
/* Works, command window that goes away */
sprintf(command,"start \"%s\"",url);
status = _spawnl(_P_DETACH, ComSpec, ComSpec, "/C", command, NULL);
/* Works, command window that goes away */
sprintf(command,"\"%s\"",url);
status = _spawnl(_P_NOWAIT, "c:\\windows\\command\\start.exe",
"c:\\windows\\command\\start.exe", command, NULL);
/* Works, command window that goes away */
sprintf(command,"\"%s\"",url);
status = _spawnl(_P_WAIT, ComSpec, "start", command, NULL);
/* Works, command window that goes away */
sprintf(command,"start \"%s\"",url);
status = _spawnl(_P_NOWAIT, ComSpec, ComSpec, "/C", command, NULL);
/* Doesn't work on 95 (No such file or directory), works on NT */
sprintf(command,"start \"%s\"",url);
status = _spawnl(_P_NOWAIT, ComSpec, "/C", command, NULL);
/* Works on 95, not NT, no command window
* No start.exe for NT */
sprintf(command,"\"%s\"",url);
status = _spawnlp(_P_DETACH, "start", "start", command, NULL);
/* Doesn't work on 95 */
sprintf(command,"\"start %s\"",url);
status = _spawnl(_P_DETACH, ComSpec, ComSpec, "/C", command, NULL);
#else
/* This seems to work on 95 and NT, with a command box on 95
* It may have trouble if the URL has spaces */
sprintf(command,"start %s",url);
status = _spawnl(_P_DETACH, ComSpec, ComSpec, "/C", command, NULL);
#endif
if(status == -1) {
char *errstring=strerror(errno);
printf("\ncallBrowser: Cannot start browser:\n"
"%s %s\n"
" %s\n",ComSpec,command,errstring);
/* perror("callBrowser:"); */
return(0);
}
return(1);
}
#endif /* #ifndef WIN32 */
#if 0
/*************************************************************************/
/*************************************************************************/
/* Mosaic Version */
/*************************************************************************/
/*************************************************************************/
#ifndef MOSAICPATH
/* #define MOSAICPATH "/usr/bin/X11/mosaic" */
/* #define MOSAICPATH "/opt/local/bin/mosaic" */
#define MOSAICPATH "mosaic"
#endif
/**************************** callBrowser ********************************/
int callBrowser(char *url)
/* Returns non-zero on success, 0 on failure */
/* url is the URL that Mosaic is to display */
/* or "quit" to terminate Mosaic */
{
static pid_t pid=0;
char filename[32];
FILE *file;
char path[BUFSIZ];
char *envstring;
signal(SIGCHLD,SIG_IGN);
/* Handle quit */
if(!strcmp(url,"quit")) {
if (pid) {
sprintf(filename,"/tmp/Mosaic.%d",pid);
unlink(filename);
kill(pid,SIGTERM);
pid=0;
}
return 3;
}
/* If Mosaic is not up, exec it */
if ((!pid) || kill(pid,0)) {
if (!(pid=fork())) {
envstring=getenv("MOSAICPATH");
if(!envstring) {
sprintf(path,"%s",MOSAICPATH);
}
else {
sprintf(path,"%s",envstring);
}
execlp(path,path,url,(char *)0);
perror(path);
_exit(127);
}
return 1;
}
/* Mosaic is up, send message through file */
sprintf(filename,"/tmp/Mosaic.%d",pid);
if (!(file=fopen(filename,"w"))) return 0;
fprintf(file,"goto\n%s\n",url);
fclose(file);
kill(pid,SIGUSR1);
return 2;
}
#endif