-
Notifications
You must be signed in to change notification settings - Fork 6
/
browserHelp.c
347 lines (312 loc) · 10.8 KB
/
browserHelp.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
/*************************************************************************\
* Copyright (c) 1994-2004 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 1997-2003 Southeastern Universities Research Association,
* as Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 1997-2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/*****************************************************************************
*
* 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
#ifdef VMS
#include <ssdef.h>
#include <lib$routines.h>
#include <ctype.h>
#include <descrip.h>
#include <clidef.h>
#endif
/* Function prototypes */
extern int kill(pid_t, int); /* May not be defined for strict ANSI */
int callBrowser(Display *dpy, char *url, char *bookmark);
static Window checkNetscapeWindow(Window w);
static int execute(char *s);
static Window findNetscapeWindow(void);
static int ignoreXError(Display *display, XErrorEvent *xev);
/* Global variables */
Display *display;
/**************************** callBrowser ********************************/
int callBrowser(Display *dpy, char *url, char *bookmark)
/* 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;
/* Set the global display variable */
display=dpy;
/* 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("NETSCAPEPATH");
if(!envstring) {
#ifndef VMS
sprintf(command,"%s -install '%s%s' &",NETSCAPEPATH,url,bookmark);
#else
sprintf(command,"%s -install \"%s%s\"",NETSCAPEPATH,url,bookmark);
#endif
}
else {
sprintf(command,"%s -install '%s%s' &",envstring,url,bookmark);
}
#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("NETSCAPEPATH");
if(!envstring) {
#ifndef VMS
sprintf(command,"%s -id 0x%x -remote 'openURL(%s%s)' &",
NETSCAPEPATH,(unsigned int)netscapew,url,bookmark);
#else
sprintf(command,"%s -id 0x%x -remote \"openURL(%s%s)\"",
NETSCAPEPATH,netscapew,url,bookmark);
#endif
}
else {
#ifndef VMS
sprintf(command,"%s -id 0x%x -remote 'openURL(%s%s)' &",
envstring,(unsigned int)netscapew,url,bookmark);
#else
sprintf(command,"%s -id 0x%x -remote \"openURL(%s%s)\" &",
envstring,netscapew,url,bookmark);
#endif
}
#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
/* Raise the window */
XMapRaised(display,netscapew);
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 */
{
#ifndef VMS
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);
#else
int status,spawn_sts;
int spawnFlags=CLI$M_NOWAIT;
struct dsc$descriptor cmdDesc;
cmdDesc.dsc$w_length = strlen(s);
cmdDesc.dsc$b_dtype = DSC$K_DTYPE_T;
cmdDesc.dsc$b_class = DSC$K_CLASS_S;
cmdDesc.dsc$a_pointer = s;
spawn_sts = lib$spawn(&cmdDesc,0,0,&spawnFlags,0,0, &status,0,0,0,0,0);
if(spawn_sts != 1)
printf("statuss %d %d\n",spawn_sts, status);
#endif
}
/**************************** 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>
int callBrowser(char *url, char *bookmark);
/**************************** callBrowser (WIN32) ************************/
int callBrowser(char *url, char *bookmark)
/* 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];
int 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%s\"",url,bookmark);
status = _spawnl(_P_WAIT, ComSpec, ComSpec, "/C", command, NULL);
/* Works, command window that goes away */
sprintf(command,"start \"%s%s\"",url,bookmark);
status = _spawnl(_P_DETACH, ComSpec, ComSpec, "/C", command, NULL);
/* Works, command window that goes away */
sprintf(command,"\"%s%s\"",url,bookmark);
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%s\"",url,bookmark);
status = _spawnl(_P_WAIT, ComSpec, "start", command, NULL);
/* Works, command window that goes away */
sprintf(command,"start \"%s%s\"",url,bookmark);
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%s\"",url,bookmark);
status = _spawnl(_P_NOWAIT, ComSpec, "/C", command, NULL);
/* Works on 95, not NT, no command window
* No start.exe for NT */
sprintf(command,"\"%s%s\"",url,bookmark);
status = _spawnlp(_P_DETACH, "start", "start", command, NULL);
/* Doesn't work on 95 */
sprintf(command,"\"start %s%s\"",url,bookmark);
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%s",url,bookmark);
status = _spawnl(_P_DETACH, ComSpec, ComSpec, "/C", command, NULL);
#endif
if(status == -1) {
char *errstring=strerror(errno);
fprintf(stderr,"\ncallBrowser: Cannot start browser:\n"
"%s %s\n"
" %s\n",ComSpec,command,errstring);
/* perror("callBrowser:"); */
return(0);
}
return(1);
}
#endif /* #ifndef WIN32 */