Debian Codegraph @

texk/psutils/psutils-src/pstops.c

raw
1 /* pstops.c
2 * Rearrange pages in conforming PS file
3 *
4 * (c) Reuben Thomas 2012-2014
5 * (c) Angus J. C. Duggan 1991-1997
6 * See file LICENSE for details.
7 */
8
9 #include "config.h"
10
11 #include <unistd.h>
12 #include <string.h>
13 #include "progname.h"
14 #include "binary-io.h"
15
16 #include "psutil.h"
17 #include "psspec.h"
18
19 const char *syntax = "[-q] [-b] [-wWIDTH] [-hHEIGHT] [-dLWIDTH] [-pPAPER] PAGESPECS [INFILE [OUTFILE]]\n";
20
21 const char *argerr_message = "%page specification error:\n"
22 " pagespecs = [modulo:]spec\n"
23 " spec = [-]pageno[@scale][L|R|U|H|V][(xoff,yoff)][,spec|+spec]\n"
24 " modulo >= 1, 0 <= pageno < modulo\n";
25
26 static int modulo = 1;
27 static int pagesperspec = 1;
28
29 static PageSpec *parsespecs(char *str)
30 {
31 PageSpec *head, *tail;
32 unsigned long spec_count = 0;
33 long num = -1;
34
35 head = tail = newspec();
36 while (*str) {
37 if (isdigit((unsigned char)*str)) {
38 num = parseint(&str);
39 } else {
40 switch (*str++) {
41 case ':':
42 if (spec_count || head != tail || num < 1) argerror();
43 modulo = num;
44 num = -1;
45 break;
46 case '-':
47 tail->flags ^= REVERSED;
48 break;
49 case '@':
50 tail->scale *= parsedouble(&str);
51 tail->flags |= SCALE;
52 break;
53 case 'l': case 'L':
54 tail->rotate += 90;
55 tail->flags |= ROTATE;
56 break;
57 case 'r': case 'R':
58 tail->rotate -= 90;
59 tail->flags |= ROTATE;
60 break;
61 case 'u': case 'U':
62 tail->rotate += 180;
63 tail->flags |= ROTATE;
64 break;
65 case 'h': case 'H':
66 tail->flags ^= HFLIP;
67 break;
68 case 'v': case 'V':
69 tail->flags ^= VFLIP;
70 break;
71 case '(':
72 tail->xoff += parsedimen(&str);
73 if (*str++ != ',') argerror();
74 tail->yoff += parsedimen(&str);
75 if (*str++ != ')') argerror();
76 tail->flags |= OFFSET;
77 break;
78 case '+':
79 tail->flags |= ADD_NEXT;
80 case ',':
81 if (num < 0 || num >= modulo) argerror();
82 if ((tail->flags & ADD_NEXT) == 0)
83 pagesperspec++;
84 tail->pageno = num;
85 tail->next = newspec();
86 tail = tail->next;
87 num = -1;
88 break;
89 default:
90 argerror();
91 }
92 spec_count++;
93 }
94 }
95 if (num >= modulo)
96 argerror();
97 else if (num >= 0)
98 tail->pageno = num;
99 return (head);
100 }
101
102 int
103 main(int argc, char *argv[])
104 {
105 PageSpec *specs = NULL;
106 int nobinding = 0;
107 double draw = 0;
108 int opt;
109
110 set_program_name (argv[0]);
111
112 if (!paper_size(NULL, &width, &height))
113 die("could not get default paper size");
114
115 verbose = 1;
116
117 while((opt = getopt(argc, argv, "qd::bw:h:p:v0123456789")) != EOF) {
118 switch(opt) {
119 case 'q': /* quiet */
120 verbose = 0;
121 break;
122 case 'd': /* draw borders */
123 if(optarg)
124 draw = singledimen(optarg);
125 else
126 draw = 1;
127 break;
128 case 'b': /* no bind operator */
129 nobinding = 1;
130 break;
131 case 'w': /* page width */
132 width = singledimen(optarg);
133 break;
134 case 'h': /* page height */
135 height = singledimen(optarg);
136 break;
137 case 'p': /* paper type */
138 if (!paper_size(optarg, &width, &height))
139 die("paper size '%s' not recognised", optarg);
140 break;
141 case 'v': /* version */
142 usage();
143 case '0':
144 case '1':
145 case '2':
146 case '3':
147 case '4':
148 case '5':
149 case '6':
150 case '7':
151 case '8':
152 case '9':
153 if (specs == NULL) {
154 char *spec_txt = malloc((optarg ? strlen(optarg) : 0) + 3);
155 if(!spec_txt) die("no memory for spec allocation");
156 spec_txt[0] = '-';
157 spec_txt[1] = opt;
158 spec_txt[2] = 0;
159 if (optarg) strcat(spec_txt, optarg);
160 specs = parsespecs(spec_txt);
161 free(spec_txt);
162 } else {
163 usage();
164 }
165 break;
166 default:
167 usage();
168 break;
169 }
170 }
171
172 if (specs == NULL) {
173 if(optind == argc) usage();
174 specs = parsespecs(argv[optind]);
175 optind++;
176 }
177
178 infile = stdin;
179 outfile = stdout;
180
181 /* Be defensive */
182 if((argc - optind) < 0 || (argc - optind) > 2) usage();
183
184 if (optind != argc) {
185 /* User specified an input file */
186 if ((infile = fopen(argv[optind], "rb")) == NULL)
187 die("can't open input file %s", argv[optind]);
188 optind++;
189 }
190
191 if (optind != argc) {
192 /* User specified an output file */
193 if ((outfile = fopen(argv[optind], "wb")) == NULL)
194 die("can't open output file %s", argv[optind]);
195 optind++;
196 }
197
198 if (optind != argc || specs == NULL) usage();
199
200 if (infile == stdin && set_binary_mode(fileno(stdin), O_BINARY) < 0)
201 die("can't reset stdin to binary mode");
202 if (outfile == stdout && set_binary_mode(fileno(stdout), O_BINARY) < 0)
203 die("can't reset stdout to binary mode");
204
205 if ((infile=seekable(infile))==NULL)
206 die("can't seek input");
207
208 pstops(modulo, pagesperspec, nobinding, specs, draw);
209
210 return 0;
211 }