| 1 |
/* |
| 2 |
* pdfopen.c. |
| 3 |
* |
| 4 |
* Heavily modified by Jim Diamond ([email protected]) 2010/04/11 |
| 5 |
* to work with AR 9, xpdf and evince. |
| 6 |
* (Also some mods by Peter Breitenlohner <[email protected]> from 2009 |
| 7 |
* and suggestions from 2010, and some suggestions from Karl Berry |
| 8 |
* (2011).) |
| 9 |
* |
| 10 |
* Note 1: the calling sequence was changed considerably to match what |
| 11 |
* the program actually seemed to do as well as what made sense to me. |
| 12 |
* |
| 13 |
* Note 2: if there are two instances of acroread displaying that file, |
| 14 |
* only one is reloaded. |
| 15 |
* |
| 16 |
* Note 3: Unlike xpdf and AR[5,7,8,9], okular will not accept X events |
| 17 |
* from this program unless the okular window has focus. On my system |
| 18 |
* (Slackware64 13.0, using fvwm2), the functions I added to sendx.c to try |
| 19 |
* to change the focus to the okular window do not work. (See also the |
| 20 |
* comments in sendx.c.) If anyone cares to contribute working code, |
| 21 |
* please send it to me for inclusion. |
| 22 |
|
| 23 |
* Further modified 2010/05/03 to work with ar5, and to include some code |
| 24 |
* that does *not* work with okular (see note below). |
| 25 |
* |
| 26 |
* Modified 2014/05/16 (V 0.84) to add capability to open a pdf file |
| 27 |
* at a specified page (currently working with AR9, xpdf and evince). |
| 28 |
* |
| 29 |
* Modified 2014/05/22 (V 0.85) to allow '--file' option, for |
| 30 |
* "compatibility" with windows pdfopen/pdfclose calling sequence. |
| 31 |
* Drive by: allow '--viewer' for consistency's sake. |
| 32 |
* Also allow '-V', '-?' and '--page' for compatibility. |
| 33 |
* Added --goto/-g option... this works in (at least) xpdf 3.03, |
| 34 |
* evince 3.12.1. |
| 35 |
* |
| 36 |
* Modified 2014/05/24 (V 0.86) so that -g works in AR 9.5.5 and |
| 37 |
* possibly other AR9 versions. |
| 38 |
*/ |
| 39 |
|
| 40 |
#include <stdio.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <sys/types.h> |
| 45 |
#include <sys/wait.h> |
| 46 |
|
| 47 |
#include "sendx.h" |
| 48 |
#include "xpdfopen.h" |
| 49 |
#include "utils.h" |
| 50 |
|
| 51 |
#define DEFINE_EXTERNS |
| 52 |
#include "externs.h" |
| 53 |
|
| 54 |
#define MAX_RELOAD_CMDS 10 |
| 55 |
#define MAX_PRE_PAGE_OPTIONS 2 /* See struct defn below */ |
| 56 |
#define MAX_POST_PAGE_OPTIONS 1 /* See struct defn below */ |
| 57 |
#define MAX_PRE_DEST_OPTIONS 2 /* See struct defn below */ |
| 58 |
#define MAX_POST_DEST_OPTIONS 1 /* See struct defn below */ |
| 59 |
/* Overkill ... */ |
| 60 |
#define MAX_OPTIONS (10 + MAX_PRE_PAGE_OPTIONS \ |
| 61 |
+ MAX_POST_PAGE_OPTIONS + MAX_PRE_DEST_OPTIONS \ |
| 62 |
+ MAX_POST_DEST_OPTIONS) |
| 63 |
|
| 64 |
/* A string unlikely to be used as a real command by a PDF viewer */ |
| 65 |
#define FORK_EXEC_CMD "*FORK EXEC*" |
| 66 |
|
| 67 |
|
| 68 |
/* Used in the pdf_viewer_t type... see comment below */ |
| 69 |
typedef enum |
| 70 |
{ |
| 71 |
USE_SENDX, USE_FORK_EXEC |
| 72 |
} reload_method_t; |
| 73 |
|
| 74 |
|
| 75 |
/* |
| 76 |
* This struct contains the information needed for both |
| 77 |
* (a) initially starting up a PDF viewer, and |
| 78 |
* (b) telling the viewer to reload the document. |
| 79 |
* Of course, the viewer has to support that functionality. |
| 80 |
* Two possible ways are supported. |
| 81 |
* (a) Send the viewer's window (a) simulated keypress(ss) |
| 82 |
* (b) fork()/exec() a second instance of the program which somehow signals |
| 83 |
* the running instance. Note that this is problematical if the |
| 84 |
* arguments for the "reload" case are different than the options |
| 85 |
* for the "start up the viewer" case, because the command-line |
| 86 |
* (and environment variable) syntax have to be extended. |
| 87 |
* Arguably, if both the start-up and reload can be done with shell |
| 88 |
* commands, the user should just write a shell script and be done with |
| 89 |
* it. |
| 90 |
* These could be extended with this as well: |
| 91 |
* (c) send a Unix signal to the currently running viewer. |
| 92 |
* This would require augmenting reload_method_t and pdf_viewer_t. |
| 93 |
*/ |
| 94 |
|
| 95 |
typedef struct |
| 96 |
{ |
| 97 |
const char * short_name; /* cmd line option name */ |
| 98 |
const char * program_name; /* program to exec() */ |
| 99 |
reload_method_t method; /* method to induce reload */ |
| 100 |
const char * window_name; /* when no file is viewed */ |
| 101 |
const char * window_name_fmt; /* fmt string for file viewed */ |
| 102 |
const char * exec_pre_options[MAX_OPTIONS]; /* before filename in cmd */ |
| 103 |
const char * exec_post_options[MAX_OPTIONS];/* after filename in cmd */ |
| 104 |
const char * reload_cmds[MAX_RELOAD_CMDS]; /* cmds to reload a file */ |
| 105 |
/* |
| 106 |
* If the page number goes before the filename, use these options. |
| 107 |
* The last one must be a printf format string with a %d and |
| 108 |
* no other conversions specs. |
| 109 |
* End list with a NULL. |
| 110 |
*/ |
| 111 |
const char * initial_page_pre_options[MAX_PRE_PAGE_OPTIONS + 1]; /* */ |
| 112 |
/* |
| 113 |
* If the page number goes after the filename, use these options. |
| 114 |
* The last one must be a printf format string with a %d and |
| 115 |
* no other conversions specs. |
| 116 |
* End list with a NULL. |
| 117 |
*/ |
| 118 |
const char * initial_page_post_options[MAX_POST_PAGE_OPTIONS + 1]; /* */ |
| 119 |
/* Ditto for named destinations */ |
| 120 |
const char * initial_dest_pre_options[MAX_PRE_DEST_OPTIONS + 1]; /* */ |
| 121 |
const char * initial_dest_post_options[MAX_POST_DEST_OPTIONS + 1]; /* */ |
| 122 |
} pdf_viewer_t; |
| 123 |
|
| 124 |
|
| 125 |
/* Reload commands (or prefixes thereof) */ |
| 126 |
#define CTRL "Ctrl-" |
| 127 |
#define ALT "Alt-" |
| 128 |
#define CTRL_ALT "Ctrl-Alt-" |
| 129 |
#define FN "Fn-" |
| 130 |
#define FOCUS_IN "FOCUS-IN" |
| 131 |
#define FOCUS_OUT "FOCUS-OUT" |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
/* |
| 136 |
* The first struct represents the default viewer with the default options. |
| 137 |
*/ |
| 138 |
static |
| 139 |
pdf_viewer_t pdf_viewers[] = |
| 140 |
{ |
| 141 |
{"ar9", "acroread", USE_SENDX, |
| 142 |
AR9_WIN_NAME, AR9_WIN_NAME_FMT, |
| 143 |
{"-openInNewInstance", NULL}, |
| 144 |
{NULL}, |
| 145 |
{"Ctrl-R", NULL}, |
| 146 |
{"/a", "page=%d", NULL}, |
| 147 |
{NULL}, |
| 148 |
{"/a", "nameddest=%s", NULL}, |
| 149 |
{NULL} |
| 150 |
}, |
| 151 |
{"ar9-tab", "acroread", USE_SENDX, |
| 152 |
AR9_WIN_NAME, AR9_WIN_NAME_FMT, |
| 153 |
{NULL}, |
| 154 |
{NULL}, |
| 155 |
{"Ctrl-R", NULL}, |
| 156 |
{"/a", "page=%d", NULL}, |
| 157 |
{NULL}, |
| 158 |
{"/a", "nameddest='%s'", NULL}, |
| 159 |
{NULL} |
| 160 |
}, |
| 161 |
{"ar8", "acroread", USE_SENDX, |
| 162 |
AR8_WIN_NAME, AR8_WIN_NAME_FMT, |
| 163 |
{NULL}, |
| 164 |
{NULL}, |
| 165 |
{"Ctrl-W", FORK_EXEC_CMD, NULL}, |
| 166 |
{NULL}, |
| 167 |
{NULL}, |
| 168 |
{NULL}, |
| 169 |
{NULL} |
| 170 |
}, |
| 171 |
{"ar7", "acroread", USE_SENDX, |
| 172 |
AR7_WIN_NAME, AR7_WIN_NAME_FMT, |
| 173 |
{NULL}, |
| 174 |
{NULL}, |
| 175 |
{"Ctrl-W", "Alt-Left", NULL}, |
| 176 |
{NULL}, |
| 177 |
{NULL}, |
| 178 |
{NULL}, |
| 179 |
{NULL} |
| 180 |
}, |
| 181 |
{"ar5", "acroread", USE_SENDX, |
| 182 |
AR5_WIN_NAME, AR5_WIN_NAME_FMT, |
| 183 |
{NULL}, |
| 184 |
{NULL}, |
| 185 |
{"Ctrl-W", "Alt-Left", NULL}, |
| 186 |
{NULL}, |
| 187 |
{NULL}, |
| 188 |
{NULL}, |
| 189 |
{NULL} |
| 190 |
}, |
| 191 |
/* |
| 192 |
* xpdf also allows 'xpdf -remote tex -reload', but if that xpdf server |
| 193 |
* is not running, rather than a failure return, a blank window is |
| 194 |
* opened. This is not much good to us. |
| 195 |
* Using "tex-server" as the server name is an arbitrary choice. |
| 196 |
*/ |
| 197 |
{"xpdf", "xpdf", USE_SENDX, |
| 198 |
XPDF_WIN_NAME, XPDF_WIN_NAME_FMT, |
| 199 |
{"-remote", "tex-server", NULL}, |
| 200 |
{NULL}, |
| 201 |
{"r", NULL}, |
| 202 |
{NULL}, |
| 203 |
{"%d", NULL}, |
| 204 |
{NULL}, |
| 205 |
{"+%s", NULL} |
| 206 |
}, |
| 207 |
{"evince", "evince", USE_SENDX, |
| 208 |
EVINCE_WIN_NAME, EVINCE_WIN_NAME_FMT, |
| 209 |
{NULL}, |
| 210 |
{NULL}, |
| 211 |
{"Ctrl-R", NULL}, |
| 212 |
{"-i", "%d", NULL}, |
| 213 |
{NULL}, |
| 214 |
{"-n", "%s", NULL}, |
| 215 |
{NULL} |
| 216 |
}, |
| 217 |
#ifdef TRY_OCULAR |
| 218 |
{"okular", "okular", USE_SENDX, |
| 219 |
OKULAR_WIN_NAME, OKULAR_WIN_NAME_FMT, |
| 220 |
{NULL}, |
| 221 |
{NULL}, |
| 222 |
{FOCUS_IN, "Fn-F5", FOCUS_OUT, NULL}, |
| 223 |
{NULL}, |
| 224 |
{NULL}, |
| 225 |
{NULL}, |
| 226 |
{NULL} |
| 227 |
}, |
| 228 |
#endif |
| 229 |
}; |
| 230 |
|
| 231 |
#define DEFAULT_VIEWER (pdf_viewers[0].short_name) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
static void |
| 236 |
usage(void) |
| 237 |
{ |
| 238 |
int i; |
| 239 |
int viewers = sizeof(pdf_viewers) / sizeof(pdf_viewers[0]); |
| 240 |
|
| 241 |
fprintf(stderr, "This is version %s of %s.\n", VERSION, progname); |
| 242 |
fprintf(stderr, "Usage:\n %s [-h|--help|-?]\n" |
| 243 |
" Show this help and exit.\n\n", progname); |
| 244 |
fprintf(stderr, " %s [-v|--version]\n" |
| 245 |
" Show the version number and exit.\n\n", progname); |
| 246 |
fprintf(stderr, " %s [-r|--reset_focus] [[-]-viewer <prog>]\n" |
| 247 |
"\t\t[-p|--page <n>] [-g|--goto <named-dest>] " |
| 248 |
"[--file] <file.pdf>\n", progname); |
| 249 |
fprintf(stderr, " If the PDF viewer <prog> is displaying <file.pdf>, " |
| 250 |
"reload that file;\n" |
| 251 |
" Otherwise call <prog> to display <file.pdf>.\n" |
| 252 |
" If '-r' or '--reset_focus' is used, attempt to " |
| 253 |
"reset the input focus\n" |
| 254 |
" after calling the viewer program.\n" |
| 255 |
" If the viewer allows it and is not already " |
| 256 |
"running, the argument\n of -p/--page specifies the " |
| 257 |
"first page to display.\n" |
| 258 |
" If the viewer allows it and is not already " |
| 259 |
"running, the argument\n of -g/--goto specifies the " |
| 260 |
"named destination to view.\n NOTE: -g overrides -p\n\n"); |
| 261 |
fprintf(stderr, "The default viewer program is %s.\n", DEFAULT_VIEWER); |
| 262 |
fprintf(stderr, "Implemented viewers:"); |
| 263 |
for (i = 0; i < viewers - 1; i++) |
| 264 |
fprintf(stderr, " '%s'", pdf_viewers[i].short_name); |
| 265 |
fprintf(stderr, " and '%s'.\n", pdf_viewers[viewers - 1].short_name); |
| 266 |
fprintf(stderr, "Note: ar9 uses acroread's -openInNewInstance argument," |
| 267 |
" ar9-tab does not.\n"); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
/* |
| 273 |
* Send one "command" to the given window. |
| 274 |
* The command can be of these forms (for X an upper case letter or arrow |
| 275 |
* key, such as "Left", and D one or more digits; see sendx.c): |
| 276 |
* - Ctrl-X |
| 277 |
* - Alt-X |
| 278 |
* - Ctrl-Atl-X |
| 279 |
* - Fn-FD |
| 280 |
* - FOCUS-[IN|OUT] |
| 281 |
* - <string> (not starting with any of the above prefixes) |
| 282 |
* Allow other variations on the cases of "Ctrl" and "Alt". |
| 283 |
* |
| 284 |
* Return 0 on success, non-0 otherwise. |
| 285 |
*/ |
| 286 |
|
| 287 |
static int |
| 288 |
send_command(const char * window_name, const char * reload_cmd) |
| 289 |
{ |
| 290 |
int alt_len, ctrl_len, ctrl_alt_len, fn_len, focus_len; |
| 291 |
|
| 292 |
if (!strcmp(reload_cmd, FORK_EXEC_CMD)) |
| 293 |
return 999; |
| 294 |
|
| 295 |
ctrl_alt_len = strlen(CTRL_ALT); |
| 296 |
if (!strncasecmp(CTRL_ALT, reload_cmd, ctrl_alt_len)) |
| 297 |
return sendx_controlalt_token(reload_cmd + ctrl_alt_len, window_name); |
| 298 |
|
| 299 |
ctrl_len = strlen(CTRL); |
| 300 |
if (!strncasecmp(CTRL, reload_cmd, ctrl_len)) |
| 301 |
return sendx_control_token(reload_cmd + ctrl_len, window_name); |
| 302 |
|
| 303 |
alt_len = strlen(ALT); |
| 304 |
if (!strncasecmp(ALT, reload_cmd, alt_len)) |
| 305 |
return sendx_alt_token(reload_cmd + alt_len, window_name); |
| 306 |
|
| 307 |
fn_len = strlen(FN); |
| 308 |
if (!strncasecmp(FN, reload_cmd, fn_len)) |
| 309 |
return sendx_token(reload_cmd + fn_len, window_name); |
| 310 |
|
| 311 |
focus_len = strlen(FOCUS_IN); |
| 312 |
if (!strncasecmp(FOCUS_IN, reload_cmd, focus_len)) |
| 313 |
return set_focus(window_name); |
| 314 |
|
| 315 |
focus_len = strlen(FOCUS_OUT); |
| 316 |
if (!strncasecmp(FOCUS_OUT, reload_cmd, focus_len)) |
| 317 |
return reset_focus(); |
| 318 |
|
| 319 |
return sendx_string(reload_cmd, window_name); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
/* |
| 325 |
* reload_file(): Try to reload the given file. |
| 326 |
* Return 0 on success, non-0 otherwise. |
| 327 |
* |
| 328 |
* If reset_focus is true, (attempt to) reset the input focus to the |
| 329 |
* window which had focus when this program was called. This is |
| 330 |
* currently only implemented for the USE_SENDX case. |
| 331 |
*/ |
| 332 |
|
| 333 |
static int |
| 334 |
reload_file(const char * filename, int viewer_index, int focus_reset) |
| 335 |
{ |
| 336 |
int success = 0; |
| 337 |
pdf_viewer_t pdf_viewer = pdf_viewers[viewer_index]; |
| 338 |
const char ** reload_cmds = pdf_viewer.reload_cmds; |
| 339 |
char * window_name; |
| 340 |
|
| 341 |
switch (pdf_viewers[viewer_index].method) |
| 342 |
{ |
| 343 |
case USE_SENDX: |
| 344 |
window_name = make_window_name(pdf_viewer.window_name_fmt, filename); |
| 345 |
if (window_name == NULL) |
| 346 |
return 99; |
| 347 |
|
| 348 |
if (focus_reset) |
| 349 |
(void)set_focus(window_name); |
| 350 |
|
| 351 |
while (*reload_cmds) |
| 352 |
{ |
| 353 |
/* |
| 354 |
* Note: there is a race condition in the sendx.c code which |
| 355 |
* can cause the program to crash and burn with a 'BadWindow' |
| 356 |
* fault. This has been observed for AR8 (but not AR7, oddly) |
| 357 |
* after the ^W causes AR8 to change the window name. It seems |
| 358 |
* that Window_With_Name() tries to access a BadWindow within |
| 359 |
* its recursive call. |
| 360 |
* If this continues to be a problem, a usleep() may make the |
| 361 |
* problem go away. |
| 362 |
*/ |
| 363 |
success = ! send_command(window_name, *reload_cmds); |
| 364 |
if (! success && reload_cmds != pdf_viewer.reload_cmds) |
| 365 |
{ |
| 366 |
/* |
| 367 |
* In some cases the window name might be changed because |
| 368 |
* of the first command. |
| 369 |
* For example, in AR7 the first command (^W) "closes" |
| 370 |
* the document, and the window title changes to reflect |
| 371 |
* the fact there is no document. So try that command with |
| 372 |
* the "plain" window name. |
| 373 |
*/ |
| 374 |
success = ! send_command(pdf_viewer.window_name, *reload_cmds); |
| 375 |
} |
| 376 |
if (! success) |
| 377 |
break; |
| 378 |
reload_cmds++; |
| 379 |
} |
| 380 |
if (focus_reset) |
| 381 |
(void)reset_focus(); |
| 382 |
break; |
| 383 |
|
| 384 |
case USE_FORK_EXEC: |
| 385 |
return 2; /* TODO: someone who cares can put some code here */ |
| 386 |
break; |
| 387 |
|
| 388 |
default: |
| 389 |
fprintf(stderr, "%s: internal error: invalid reload_method. Sorry.\n", |
| 390 |
progname); |
| 391 |
return 99; |
| 392 |
break; |
| 393 |
} |
| 394 |
|
| 395 |
if (success) |
| 396 |
return 0; |
| 397 |
|
| 398 |
return 1; |
| 399 |
} |
| 400 |
|
| 401 |
/* |
| 402 |
* view_file(): (Attempt to) reload or view the given filename. |
| 403 |
* |
| 404 |
* First call reload_file() in case the given viewer is already |
| 405 |
* displaying the given file. |
| 406 |
* |
| 407 |
* If that is unsuccessful, try to start up a new instance of the |
| 408 |
* given viewer with the given file. |
| 409 |
* |
| 410 |
* If dest != NULL, and the viewer allows it, on initial opening of |
| 411 |
* the file go to that named destination. |
| 412 |
* As of V 0.85, this is implemented for xpdf, evince, and, according |
| 413 |
* to the man page for acroread 9.5.5, for ar9. |
| 414 |
* Otherwise, if the viewer allows it, open the document at the given |
| 415 |
* page. |
| 416 |
*/ |
| 417 |
|
| 418 |
static int |
| 419 |
view_file(const char * filename, int viewer_index, int focus_reset, |
| 420 |
int initial_page, char * dest) |
| 421 |
{ |
| 422 |
pid_t pid; |
| 423 |
pdf_viewer_t pdf_viewer = pdf_viewers[viewer_index]; |
| 424 |
const char * argv_list[2 * MAX_OPTIONS + 3]; |
| 425 |
const char ** p; |
| 426 |
int i = 0; |
| 427 |
|
| 428 |
/* First try to reload the file */ |
| 429 |
if (reload_file(filename, viewer_index, focus_reset) == 0) |
| 430 |
{ |
| 431 |
/* Success! */ |
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
/* |
| 436 |
* No luck re-loading the file. |
| 437 |
* (Try to) start a new instance of the viewer program. |
| 438 |
* Start by forking into the background. |
| 439 |
*/ |
| 440 |
pid = fork(); |
| 441 |
if (pid < 0) |
| 442 |
{ |
| 443 |
fprintf(stderr, "%s: fork() failed!?\n", progname); |
| 444 |
return 10; |
| 445 |
} |
| 446 |
if (pid > 0) |
| 447 |
return 0; |
| 448 |
|
| 449 |
/* Child code: */ |
| 450 |
argv_list[i++] = pdf_viewer.program_name; |
| 451 |
for (p = pdf_viewer.exec_pre_options; *p;) |
| 452 |
argv_list[i++] = *p++; |
| 453 |
|
| 454 |
if (pdf_viewer.initial_page_pre_options[0] != NULL && dest == NULL) |
| 455 |
{ |
| 456 |
int j = 0; |
| 457 |
char * buf; |
| 458 |
|
| 459 |
while (pdf_viewer.initial_page_pre_options[j + 1] != NULL) |
| 460 |
argv_list[i++] = pdf_viewer.initial_page_pre_options[j++]; |
| 461 |
|
| 462 |
/* 10 is more than enough for the page number */ |
| 463 |
buf = malloc(strlen(pdf_viewer.initial_page_pre_options[j]) + 10); |
| 464 |
if (buf != NULL) |
| 465 |
{ |
| 466 |
/* 1e8 or more pages? I doubt it. */ |
| 467 |
sprintf(buf, pdf_viewer.initial_page_pre_options[j], |
| 468 |
initial_page > 99999999 ? 1 : initial_page); |
| 469 |
argv_list[i++] = buf; |
| 470 |
} |
| 471 |
else |
| 472 |
fprintf(stderr, "%s: unable to allocate memory for page " |
| 473 |
"specification!!\n" |
| 474 |
"Crossing fingers and continuing.", progname); |
| 475 |
} |
| 476 |
|
| 477 |
if (pdf_viewer.initial_dest_pre_options[0] != NULL && dest != NULL) |
| 478 |
{ |
| 479 |
int j = 0; |
| 480 |
char * buf; |
| 481 |
|
| 482 |
while (pdf_viewer.initial_dest_pre_options[j + 1] != NULL) |
| 483 |
argv_list[i++] = pdf_viewer.initial_dest_pre_options[j++]; |
| 484 |
|
| 485 |
buf = malloc(strlen(pdf_viewer.initial_dest_pre_options[j]) |
| 486 |
+ strlen(dest) + 1); |
| 487 |
if (buf != NULL) |
| 488 |
{ |
| 489 |
sprintf(buf, pdf_viewer.initial_dest_pre_options[j], dest); |
| 490 |
argv_list[i++] = buf; |
| 491 |
} |
| 492 |
else |
| 493 |
fprintf(stderr, "%s: unable to allocate memory for named-dest " |
| 494 |
"specification!!\n" |
| 495 |
"Crossing fingers and continuing.", progname); |
| 496 |
} |
| 497 |
|
| 498 |
argv_list[i++] = filename; |
| 499 |
|
| 500 |
if (pdf_viewer.initial_page_post_options[0] != NULL && dest == NULL) |
| 501 |
{ |
| 502 |
int j = 0; |
| 503 |
char * buf; |
| 504 |
|
| 505 |
while (pdf_viewer.initial_page_post_options[j + 1] != NULL) |
| 506 |
argv_list[i++] = pdf_viewer.initial_page_post_options[j++]; |
| 507 |
|
| 508 |
buf = malloc(strlen(pdf_viewer.initial_page_post_options[j]) + 10); |
| 509 |
if (buf != NULL) |
| 510 |
{ |
| 511 |
sprintf(buf, pdf_viewer.initial_page_post_options[j], |
| 512 |
initial_page > 99999999 ? 1 : initial_page); |
| 513 |
argv_list[i++] = buf; |
| 514 |
} |
| 515 |
else |
| 516 |
fprintf(stderr, "%s: unable to allocate memory for page " |
| 517 |
"specification!!\n" |
| 518 |
"Crossing fingers and continuing.", progname); |
| 519 |
} |
| 520 |
|
| 521 |
if (pdf_viewer.initial_dest_post_options[0] != NULL && dest != NULL) |
| 522 |
{ |
| 523 |
int j = 0; |
| 524 |
char * buf; |
| 525 |
|
| 526 |
while (pdf_viewer.initial_dest_post_options[j + 1] != NULL) |
| 527 |
argv_list[i++] = pdf_viewer.initial_dest_post_options[j++]; |
| 528 |
|
| 529 |
buf = malloc(strlen(pdf_viewer.initial_dest_post_options[j]) |
| 530 |
+ strlen(dest) + 1); |
| 531 |
if (buf != NULL) |
| 532 |
{ |
| 533 |
sprintf(buf, pdf_viewer.initial_dest_post_options[j], dest); |
| 534 |
argv_list[i++] = buf; |
| 535 |
} |
| 536 |
else |
| 537 |
fprintf(stderr, "%s: unable to allocate memory for named-dest " |
| 538 |
"specification!!\n" |
| 539 |
"Crossing fingers and continuing.", progname); |
| 540 |
} |
| 541 |
|
| 542 |
argv_list[i++] = NULL; |
| 543 |
#ifdef DEBUG |
| 544 |
{ |
| 545 |
int k = 0; |
| 546 |
if (dest == NULL) |
| 547 |
fprintf(stderr, "dest == NULL\n"); |
| 548 |
else |
| 549 |
fprintf(stderr, "dest == '%s'\n", dest); |
| 550 |
for (; k < i-1; k++) |
| 551 |
fprintf(stderr, "%s\n", argv_list[k]); |
| 552 |
} |
| 553 |
#endif |
| 554 |
execvp(pdf_viewer.program_name, (char **)argv_list); |
| 555 |
|
| 556 |
/* If we are here, Something Bad happened. */ |
| 557 |
fprintf(stderr, "%s: %s startup failed\n", progname, |
| 558 |
pdf_viewer.program_name); |
| 559 |
return 5; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
int |
| 565 |
main(int argc, char * argv[]) |
| 566 |
{ |
| 567 |
const char * viewer = pdf_viewers[0].short_name; |
| 568 |
char * env_viewer; |
| 569 |
char * file = NULL; |
| 570 |
char * dest = NULL; |
| 571 |
int i; |
| 572 |
int initial_page = 1; |
| 573 |
int viewer_index = -1; |
| 574 |
int focus_reset = 0; |
| 575 |
|
| 576 |
progname = argv[0]; |
| 577 |
argc--; |
| 578 |
argv++; |
| 579 |
|
| 580 |
env_viewer = getenv("PDF_VIEWER"); |
| 581 |
if (env_viewer != NULL) |
| 582 |
viewer = env_viewer; |
| 583 |
|
| 584 |
if (argc < 1 || !strcmp(argv[0], "-h") || !strcmp(argv[0], "--help") |
| 585 |
|| !strcmp(argv[0], "-?")) |
| 586 |
{ |
| 587 |
usage(); |
| 588 |
return argc < 1 ? EXIT_FAILURE : EXIT_SUCCESS; |
| 589 |
} |
| 590 |
|
| 591 |
if (!strcmp(argv[0], "-v") || !strcmp(argv[0], "-V") |
| 592 |
|| !strcmp(argv[0], "--version")) |
| 593 |
{ |
| 594 |
printf("This is pdfopen version %s\n", VERSION); |
| 595 |
return EXIT_SUCCESS; |
| 596 |
} |
| 597 |
|
| 598 |
while (argc > 0) |
| 599 |
{ |
| 600 |
if (!strcmp(argv[0], "--reset_focus") || !strcmp(argv[0], "-r")) |
| 601 |
{ |
| 602 |
argc--; |
| 603 |
argv++; |
| 604 |
focus_reset = 1; |
| 605 |
continue; |
| 606 |
} |
| 607 |
|
| 608 |
if (argc >= 2 |
| 609 |
&& (!strcmp(argv[0], "-viewer") || !strcmp(argv[0], "--viewer"))) |
| 610 |
{ |
| 611 |
viewer = argv[1]; |
| 612 |
argc -= 2; |
| 613 |
argv += 2; |
| 614 |
continue; |
| 615 |
} |
| 616 |
|
| 617 |
if (argc >= 2 |
| 618 |
&& (!strcmp(argv[0], "-g") || !strcmp(argv[0], "--goto"))) |
| 619 |
{ |
| 620 |
dest = argv[1]; |
| 621 |
argc -= 2; |
| 622 |
argv += 2; |
| 623 |
continue; |
| 624 |
} |
| 625 |
|
| 626 |
if (argc >= 2 && !strcmp(argv[0], "--file")) |
| 627 |
{ |
| 628 |
file = argv[1]; |
| 629 |
argc -= 2; |
| 630 |
argv += 2; |
| 631 |
continue; |
| 632 |
} |
| 633 |
|
| 634 |
if (argc >= 2 |
| 635 |
&& (!strcmp(argv[0], "-p") || !strcmp(argv[0], "--page"))) |
| 636 |
{ |
| 637 |
initial_page = atoi(argv[1]); |
| 638 |
argc -= 2; |
| 639 |
argv += 2; |
| 640 |
continue; |
| 641 |
} |
| 642 |
|
| 643 |
break; |
| 644 |
} |
| 645 |
|
| 646 |
/* |
| 647 |
* Should either have no args left (--file seen above) or |
| 648 |
* --file was not seen and there is one arg left. |
| 649 |
*/ |
| 650 |
if ( ! ((argc == 0 && file != NULL) || (argc == 1 && file == NULL)) ) |
| 651 |
{ |
| 652 |
fprintf(stderr, "%s: invalid argument(s)\n", progname); |
| 653 |
usage(); |
| 654 |
return EXIT_FAILURE; |
| 655 |
} |
| 656 |
|
| 657 |
if (argc == 1) |
| 658 |
file = argv[0]; |
| 659 |
|
| 660 |
for (i = 0; i < sizeof(pdf_viewers) / sizeof(pdf_viewers[0]); i++) |
| 661 |
{ |
| 662 |
if (!strcmp(pdf_viewers[i].short_name, viewer)) |
| 663 |
{ |
| 664 |
viewer_index = i; |
| 665 |
break; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
if (viewer_index < 0) |
| 670 |
{ |
| 671 |
fprintf(stderr, "%s: Unknown PDF viewer '%s'\n", progname, viewer); |
| 672 |
usage(); |
| 673 |
|
| 674 |
return EXIT_FAILURE; |
| 675 |
} |
| 676 |
|
| 677 |
if (view_file(file, viewer_index, focus_reset, initial_page, dest)) |
| 678 |
return EXIT_FAILURE; |
| 679 |
|
| 680 |
return EXIT_SUCCESS; |
| 681 |
} |