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
|
/*
* file: pspages.c
*
* (c) Peter Kleiweg 1998
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
*/
#ifdef __MSDOS__
# ifndef __SMALL__
# error Memory model SMALL required
# endif
# include <dir.h>
# include <io.h>
#else
# include <unistd.h>
#endif
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 4096
typedef enum { BOTH, EVEN, ODD } PAGES;
char
buffer [BUFSIZE + 1],
*programname;
void
get_programname (char const *argv0),
errit (char const *format, ...),
syntax (void);
char
*get_arg (int argc, char *argv [], int *index);
int main (int argc, char *argv [])
{
PAGES
pages = BOTH;
int
first = 0,
last = 0,
i;
FILE
*fp;
get_programname (argv [0]);
for (i = 1; i < argc; i++)
if (argv [i][0] == '-') {
switch (argv [i][1]) {
case 'f':
first = atoi (get_arg (argc, argv, &i));
if (first < 1)
errit ("Illegal value for first page: %i", first);
break;
case 'l':
last = atoi (get_arg (argc, argv, &i));
if (last < 1)
errit ("Illegal value for last page: %i", last);
break;
case 'p':
first = last = atoi (get_arg (argc, argv, &i));
if (first < 1)
errit ("Illegal value for page: %i", first);
break;
case 'e':
pages = EVEN;
break;
case 'o':
pages = ODD;
break;
default:
errit ("Illegal option '%s'", *argv [i]);
}
} else
break;
if ((! first) && (! last) && (pages == BOTH))
syntax ();
if (first && last && (first > last))
errit ("First page (%i) greater than last page (%i)", first, last);
if (first && last && (first == last))
if (first % 2) {
if (pages == EVEN)
errit ("Page %i is not an even page", first);
} else {
if (pages == ODD)
errit ("Page %i is not an odd page", first);
}
switch (argc - i) {
case 0:
if (isatty (fileno (stdin)))
syntax ();
fp = stdin;
break;
case 1:
fp = fopen (argv [i], "r");
if (! fp)
errit ("Opening file \"%s\": %s", argv [i], strerror (errno));
break;
default:
syntax ();
}
fputs (
"%!PS\n"
"\n"
"<<\n"
" /EndPage {\n"
" dup 0 eq {\n"
" pop\n"
"\n"
" 1 add\n"
" true\n",
stdout
);
if (pages == ODD) fputs (
" 1 index 2 mod 1 ne { pop false } if\n",
stdout
);
if (pages == EVEN) fputs (
" 1 index 2 mod 0 ne { pop false } if\n",
stdout
);
if (first) printf (
" 1 index %i lt { pop false } if\n",
first
);
if (last) printf (
" 1 index %i gt { pop false } if\n",
last
);
fputs (
" dup not { erasepage } if\n"
"\n"
" } {\n"
" 1 eq\n"
" } ifelse\n"
" exch pop\n"
" }\n"
">> setpagedevice\n"
"\n",
stdout
);
while (fgets (buffer, BUFSIZE, fp))
if (memcmp (buffer, "%%Page", 6) && memcmp (buffer, "%!PS", 4))
fputs (buffer, stdout);
if (fp != stdin)
fclose (fp);
return 0;
}
char *get_arg (int argc, char *argv [], int *index)
{
if (argv [*index][2])
return argv [*index] + 2;
if (*index == argc - 1)
errit ("Missing argument for '%s'", argv [*index]);
return argv [++*index];
}
void errit (char const *format, ...)
{
va_list
list;
fprintf (stderr, "\nError %s: ", programname);
va_start (list, format);
vfprintf (stderr, format, list);
fprintf (stderr, "\n\n");
exit (1);
}
void get_programname (char const *argv0)
{
#ifdef __MSDOS__
char
name [MAXFILE];
fnsplit (argv0, NULL, NULL, name, NULL);
programname = strdup (name);
#else /* unix */
char
*p;
p = strrchr (argv0, '/');
if (p)
programname = strdup (p + 1);
else
programname = strdup (argv0);
#endif
}
void syntax ()
{
fprintf (
stderr,
"\nPrinting selected pages from a PostScript document\n\n"
"Useful for documents without page number comments\n"
"Requires PostScript Level 2 (should understand setpagedevice operator)\n\n"
"Usage: %s options [psfile]\n\n"
"Options:\n"
" -p # page number to print\n"
" -f # first page number to print\n"
" -l # last page number to print\n"
" -e print even pages\n"
" -o print odd pages\n\n"
"(c) P. Kleiweg 1998\n\n",
programname
);
exit (1);
}
|