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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "sendx.h"
static void usage (void) {
puts ("pdfopen 0.51: you are mistaking me for an actual program.\n");
puts(" pdfopen [--file filename.pdf]");
puts ("\nusing no arguments tells the Reader to 'go back'.\n");
}
#define READERFIVE "Acrobat Reader"
#define READERNAME "Adobe Reader"
#define READERWINPREFIX "Adobe Reader - "
#define SUCCESS(a) (!a)
int main (int argc, char **argv){
char *newargv[3];
char *winname;
char *filename;
char *basefile;
pid_t reader;
if (argc == 5 &&
(strcmp(argv[3],"--page") == 0
&& strcmp(argv[1],"--file") == 0 )) {
argc = 3;
}
if (argc == 3 && strcmp(argv[1],"--file") == 0) {
filename = malloc(strlen(argv[2])+1);
basefile = malloc(strlen(argv[2])+1);
if (filename == NULL || basefile == NULL) {
puts ("out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(filename,argv[2]);
strcpy(basefile,filename);
if (strrchr(basefile,'/'))
basefile = strrchr(basefile,'/')+1;
winname = malloc(strlen(argv[2])+1+strlen(READERWINPREFIX));
if (winname == NULL ) {
puts ("out of memory\n");
exit(EXIT_FAILURE);
}
strcpy (winname,READERWINPREFIX);
strcpy ((winname+strlen(READERWINPREFIX)), basefile);
if (SUCCESS(sendx_control_token("W",winname))) {
sendx_alt_token("Left",READERNAME); /* that's seven */
sendx_control_token("Left",READERNAME);
/* that's seven, with artificial five keymaps */
} else if (SUCCESS(sendx_control_token("W",filename))) {
sendx_control_token("Left",READERFIVE); /* that's five */
} else if (SUCCESS(sendx_control_token("W",basefile))) {
sendx_control_token("Left",READERFIVE); /* that's five */
} else {
if((reader = fork()) >= 0) {
if (reader) {
waitpid(reader,NULL,WNOHANG);
} else {
newargv[0] = malloc(strlen("acroread")+1);
if (newargv[0] == NULL) {
puts ("out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(newargv[0], "acroread");
newargv[1] = filename;
newargv[2] = NULL;
if(execvp("acroread",newargv)) {
puts ("acroread startup failed\n");
exit(EXIT_FAILURE);
}
}
} else {
puts ("fork failed\n");
exit(EXIT_FAILURE);
}
}
} else if (argc != 1) {
usage();
} else {
sendx_alt_token("Left",READERNAME);
sendx_control_token("Left",READERNAME); /* that's seven */
sendx_control_token("Left",READERFIVE);
}
exit(EXIT_SUCCESS);
}
|