| 1 |
/* routines for launching a browser to retrieve remote documents. |
| 2 |
* Copyright(C) 2002-2004 the xdvik development team. |
| 3 |
*/ |
| 4 |
/* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
* of this software and associated documentation files (the "Software"), to |
| 7 |
* deal in the Software without restriction, including without limitation the |
| 8 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 |
* sell copies of the Software, and to permit persons to whom the Software is |
| 10 |
* furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 |
* IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE |
| 19 |
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 |
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "xdvi-config.h" |
| 25 |
#include "xdvi.h" |
| 26 |
|
| 27 |
#include <ctype.h> |
| 28 |
#include "kpathsea/c-fopen.h" |
| 29 |
#include "kpathsea/c-stat.h" |
| 30 |
|
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/wait.h> /* for waitpid(), WEXITSTATUS */ |
| 33 |
|
| 34 |
#include "util.h" |
| 35 |
#include "message-window.h" |
| 36 |
#include "events.h" |
| 37 |
#include "browser.h" |
| 38 |
#include "string-utils.h" |
| 39 |
#include "statusline.h" |
| 40 |
|
| 41 |
static const char *const default_browser_cmd = |
| 42 |
"xdg-open %s" |
| 43 |
":htmlview %s" |
| 44 |
":firefox -remote \"openURL(%s,new-window)\"" |
| 45 |
":mozilla -remote \"openURL(%s,new-window)\"" |
| 46 |
":netscape -remote \"openURL(%s,new-window)\"" |
| 47 |
":xterm -e w3m %s" |
| 48 |
":xterm -e lynx %s" |
| 49 |
":xterm -e wget %s"; |
| 50 |
|
| 51 |
static Boolean |
| 52 |
do_fork_browser(char *argv[]) |
| 53 |
{ |
| 54 |
pid_t pid; |
| 55 |
switch(pid = vfork()) { |
| 56 |
case -1: /* forking error */ |
| 57 |
perror("fork"); |
| 58 |
return False; |
| 59 |
case 0: /* child */ |
| 60 |
execvp(argv[0], argv); |
| 61 |
/* arrive here only if execvp failed */ |
| 62 |
XDVI_ERROR((stderr, "Execution of %s failed: %s", argv[0], strerror(errno))); |
| 63 |
_exit(EXIT_FAILURE); |
| 64 |
return False; /* notreached - make compiler happy */ |
| 65 |
default: /* parent */ |
| 66 |
{ |
| 67 |
int timeout; |
| 68 |
for (timeout = 0; timeout < 15; timeout++) { |
| 69 |
int status; |
| 70 |
if (waitpid(pid, &status, WNOHANG)) { |
| 71 |
TRACE_HTEX((stderr, "waiting for %d: %d", (int)pid, status)); |
| 72 |
if (WIFEXITED(status)) { |
| 73 |
if (WEXITSTATUS(status) != 0) { |
| 74 |
fprintf(stderr, "Command `%s' exited with error status %d\n", |
| 75 |
argv[0], WEXITSTATUS(status)); |
| 76 |
return False; |
| 77 |
} |
| 78 |
else { |
| 79 |
TRACE_HTEX((stderr, "Child exited OK.")); |
| 80 |
return True; |
| 81 |
} |
| 82 |
} |
| 83 |
else { /* when do we arrive here?? */ |
| 84 |
sleep(1); |
| 85 |
} |
| 86 |
} |
| 87 |
else { /* waiting for child to finish */ |
| 88 |
sleep(1); |
| 89 |
} |
| 90 |
} |
| 91 |
return True; /* child not finished in time */ |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
static Boolean |
| 97 |
fork_browser(const char *browser, const char *url) |
| 98 |
{ |
| 99 |
char *cmd, *orig_cmd, *last_arg; |
| 100 |
char **argv = NULL; |
| 101 |
int argv_len = 0; |
| 102 |
int i = 0, j = 0; |
| 103 |
int match = 0; |
| 104 |
int retval; |
| 105 |
|
| 106 |
|
| 107 |
cmd = xmalloc(strlen(browser) + strlen(url) + 1); |
| 108 |
orig_cmd = cmd; /* for freeing it later */ |
| 109 |
last_arg = cmd; |
| 110 |
|
| 111 |
/* skip over leading space */ |
| 112 |
while (isspace((int)browser[i])) |
| 113 |
i++; |
| 114 |
|
| 115 |
while (browser[i] != '\0') { |
| 116 |
while (j >= argv_len) { |
| 117 |
argv_len += 10; |
| 118 |
argv = xrealloc(argv, argv_len * sizeof *argv); |
| 119 |
} |
| 120 |
/* chop into separate arguments at spaces */ |
| 121 |
if (isspace((int)browser[i])) { |
| 122 |
*cmd++ = '\0'; |
| 123 |
argv[j++] = format_arg(last_arg, url, &match); |
| 124 |
last_arg = cmd; |
| 125 |
/* skip over multiple spaces */ |
| 126 |
while (isspace((int)browser[i])) |
| 127 |
i++; |
| 128 |
} |
| 129 |
/* remove quotes around arguments containing spaces */ |
| 130 |
else if (browser[i] == '\'' || browser[i] == '"') { |
| 131 |
int len = 0; |
| 132 |
/* normalize %% and replace %s by URL */ |
| 133 |
argv[j++] = unquote_arg(browser+i, url, &match, &len); |
| 134 |
if (len == 0) { /* excess quote at end of arg; try to recover: */ |
| 135 |
j--; |
| 136 |
break; |
| 137 |
} |
| 138 |
i += len + 1; |
| 139 |
} |
| 140 |
else { |
| 141 |
*cmd++ = browser[i++]; |
| 142 |
} |
| 143 |
} |
| 144 |
*cmd = browser[i]; /* null-teminate */ |
| 145 |
/* append last, unless it contained only skipped spaces */ |
| 146 |
if (strlen(last_arg) > 0) { |
| 147 |
argv[j++] = format_arg(last_arg, url, &match); |
| 148 |
} |
| 149 |
|
| 150 |
if (match == 0) |
| 151 |
argv[j++] = xstrdup(url); |
| 152 |
argv[j++] = NULL; |
| 153 |
|
| 154 |
for (i = 0; argv[i] != NULL; i++) { |
| 155 |
TRACE_HTEX((stderr, "arg[%d]: |%s|", i, argv[i])); |
| 156 |
} |
| 157 |
|
| 158 |
/* This will wait for child exits: */ |
| 159 |
retval = do_fork_browser(argv); |
| 160 |
for (i = 0; argv[i] != NULL; i++) |
| 161 |
free(argv[i]); |
| 162 |
free(orig_cmd); |
| 163 |
free(argv); |
| 164 |
return retval; |
| 165 |
} |
| 166 |
|
| 167 |
void |
| 168 |
launch_browser(const char *filename) |
| 169 |
{ |
| 170 |
const char *browser; |
| 171 |
int pid; |
| 172 |
struct xchild *my_child; |
| 173 |
struct xio *my_io; |
| 174 |
int err_pipe[2]; |
| 175 |
|
| 176 |
/* try to set it from possible resources: First command-line argument |
| 177 |
or X resource, then environment variables, then default_browser_cmd |
| 178 |
*/ |
| 179 |
for (;;) { |
| 180 |
if ((browser = resource.browser) != NULL) |
| 181 |
break; |
| 182 |
if ((browser = getenv("BROWSER")) != NULL) |
| 183 |
break; |
| 184 |
if ((browser = getenv("WWWBROWSER")) != NULL) |
| 185 |
break; |
| 186 |
XDVI_INFO((stderr, "Browser not set (xdvi.wwwBrowser, -browser or $BROWSER environment variable).")); |
| 187 |
XDVI_INFO((stderr, "Using built-in default: `%s'", |
| 188 |
default_browser_cmd)); |
| 189 |
browser = default_browser_cmd; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
/* fork first time so that we can wait for children, without |
| 194 |
freezing the GUI. FIXME: this copies stuff from |
| 195 |
fork_process for the inter-process communication stuff - |
| 196 |
it would be better to have this in one function. */ |
| 197 |
my_child = xmalloc(sizeof *my_child); |
| 198 |
my_io = xmalloc(sizeof *my_io); |
| 199 |
statusline_info(STATUS_MEDIUM, "Trying to launch browser ..."); |
| 200 |
/* flush output buffers to avoid double buffering (i.e. data |
| 201 |
waiting in the output buffer being written twice, by the parent |
| 202 |
and the child) */ |
| 203 |
fflush(stdout); |
| 204 |
fflush(stderr); |
| 205 |
|
| 206 |
if (pipe(err_pipe) < 0) { |
| 207 |
perror("pipe"); |
| 208 |
_exit(-1); |
| 209 |
} |
| 210 |
|
| 211 |
switch (pid = fork()) { |
| 212 |
case -1: /* forking error */ |
| 213 |
perror("fork"); |
| 214 |
close(err_pipe[0]); |
| 215 |
close(err_pipe[1]); |
| 216 |
return; |
| 217 |
case 0: /* child */ |
| 218 |
{ |
| 219 |
char *tmp_browser = xstrdup(browser); |
| 220 |
|
| 221 |
close(err_pipe[0]); /* no reading from stderr */ |
| 222 |
|
| 223 |
/* make stderr of child go to err_pipe[1] */ |
| 224 |
if (dup2(err_pipe[1], STDERR_FILENO) != STDERR_FILENO) { |
| 225 |
perror("dup2 for stderr"); |
| 226 |
_exit(EXIT_FAILURE); |
| 227 |
return; /* make compiler happy */ |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
BROWSER is a colon-separated list of commands, in decreasing preference; |
| 232 |
use the first that can be forked successfully. Note that the return |
| 233 |
value of the command isn't used at all (with GUI programs, xdvi shouldn't |
| 234 |
hang until they terminate!) |
| 235 |
*/ |
| 236 |
while (tmp_browser != NULL) { |
| 237 |
char *next = strchr(tmp_browser, ':'); |
| 238 |
if (next != NULL) { |
| 239 |
*next++ = '\0'; |
| 240 |
} |
| 241 |
TRACE_HTEX((stderr, "trying browser |%s|", tmp_browser)); |
| 242 |
/* fork a second time to start the browser */ |
| 243 |
if (fork_browser(tmp_browser, filename)) { /* foking worked */ |
| 244 |
_exit(EXIT_SUCCESS); |
| 245 |
} |
| 246 |
tmp_browser = next; |
| 247 |
} |
| 248 |
/* child arrives here only if none of the commands worked */ |
| 249 |
XDVI_WARNING((stderr, "None of the browser commands in the `browser' resource (%s) worked\n", |
| 250 |
browser)); |
| 251 |
free(tmp_browser); |
| 252 |
_exit(EXIT_FAILURE); |
| 253 |
} |
| 254 |
default: /* parent */ |
| 255 |
close(err_pipe[1]); /* no writing to stderr */ |
| 256 |
|
| 257 |
my_io->next = NULL; |
| 258 |
my_io->fd = err_pipe[0]; |
| 259 |
my_io->xio_events = XIO_IN; |
| 260 |
#if HAVE_POLL |
| 261 |
my_io->pfd = NULL; |
| 262 |
#endif |
| 263 |
my_io->read_proc = read_child_error; |
| 264 |
my_io->write_proc = NULL; |
| 265 |
my_io->data = NULL; |
| 266 |
|
| 267 |
my_child->next = NULL; |
| 268 |
my_child->pid = pid; |
| 269 |
my_child->name = NULL; |
| 270 |
my_child->proc = handle_child_exit; |
| 271 |
my_child->data = NULL; |
| 272 |
my_child->io = my_io; |
| 273 |
|
| 274 |
set_chld(my_child); |
| 275 |
statusline_info(STATUS_MEDIUM, "Trying to launch browser ... done."); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|