Blame


1 b855148c 2004-05-16 devnull /*
2 b855148c 2004-05-16 devnull *
3 b855148c 2004-05-16 devnull * postreverse - reverse the page order in certain PostScript files.
4 b855148c 2004-05-16 devnull *
5 b855148c 2004-05-16 devnull * Page reversal relies on being able to locate sections of a document using file
6 b855148c 2004-05-16 devnull * structuring comments defined by Adobe (ie. the 1.0 and now 2.0 conventions) and
7 b855148c 2004-05-16 devnull * a few I've added. Among other things a minimally conforming document, according
8 b855148c 2004-05-16 devnull * to the 1.0 conventions,
9 b855148c 2004-05-16 devnull *
10 b855148c 2004-05-16 devnull * 1) Marks the end of the prologue with an %%EndProlog comment.
11 b855148c 2004-05-16 devnull *
12 b855148c 2004-05-16 devnull * 2) Starts each page with a %%Page: comment.
13 b855148c 2004-05-16 devnull *
14 b855148c 2004-05-16 devnull * 3) Marks the end of all the pages %%Trailer comment.
15 b855148c 2004-05-16 devnull *
16 b855148c 2004-05-16 devnull * 4) Obeys page independence (ie. pages can be arbitrarily rearranged).
17 b855148c 2004-05-16 devnull *
18 b855148c 2004-05-16 devnull * The most important change (at least for this program) that Adobe made in going
19 b855148c 2004-05-16 devnull * from the 1.0 to the 2.0 structuring conventions was in the prologue. They now
20 b855148c 2004-05-16 devnull * say the prologue should only define things, and the global initialization that
21 b855148c 2004-05-16 devnull * was in the prologue (1.0 conventions) should now come after the %%EndProlog
22 b855148c 2004-05-16 devnull * comment but before the first %%Page: comment and be bracketed by %%BeginSetup
23 b855148c 2004-05-16 devnull * and %%EndSetup comments. So a document that conforms to Adobe's 2.0 conventions,
24 b855148c 2004-05-16 devnull *
25 b855148c 2004-05-16 devnull * 1) Marks the end of the prologue (only definitions) with %%EndProlog.
26 b855148c 2004-05-16 devnull *
27 b855148c 2004-05-16 devnull * 2) Brackets global initialization with %%BeginSetup and %%EndSetup comments
28 b855148c 2004-05-16 devnull * which come after the prologue but before the first %Page: comment.
29 b855148c 2004-05-16 devnull *
30 b855148c 2004-05-16 devnull * 3) Starts each page with a %%Page: comment.
31 b855148c 2004-05-16 devnull *
32 b855148c 2004-05-16 devnull * 4) Marks the end of all the pages with a %%Trailer comment.
33 b855148c 2004-05-16 devnull *
34 b855148c 2004-05-16 devnull * 5) Obeys page independence.
35 b855148c 2004-05-16 devnull *
36 b855148c 2004-05-16 devnull * postreverse can handle documents that follow the 1.0 or 2.0 conventions, but has
37 b855148c 2004-05-16 devnull * also been extended slightly so it works properly with the translators (primarily
38 b855148c 2004-05-16 devnull * dpost) supplied with this package. The page independence requirement has been
39 b855148c 2004-05-16 devnull * relaxed some. In particular definitions exported to the global environment from
40 b855148c 2004-05-16 devnull * within a page should be bracketed by %%BeginGlobal and %%EndGlobal comments.
41 b855148c 2004-05-16 devnull * postreverse pulls them out of each page and inserts them in the setup section
42 b855148c 2004-05-16 devnull * of the document, immediately before it writes the %%EndProlog (for version 1.0)
43 b855148c 2004-05-16 devnull * or %%EndSetup (for version 2.0) comments.
44 b855148c 2004-05-16 devnull *
45 b855148c 2004-05-16 devnull * In addition postreverse accepts documents that choose to mark the end of each
46 b855148c 2004-05-16 devnull * page with a %%EndPage: comment, which from a translator's point of view is often
47 b855148c 2004-05-16 devnull * a more natural approach. Both page boundary comments (ie. Page: and %%EndPage:)
48 b855148c 2004-05-16 devnull * are also accepted, but be warned that everything between consecutive %%EndPage:
49 b855148c 2004-05-16 devnull * and %%Page: comments will be ignored.
50 b855148c 2004-05-16 devnull *
51 b855148c 2004-05-16 devnull * So a document that will reverse properly with postreverse,
52 b855148c 2004-05-16 devnull *
53 b855148c 2004-05-16 devnull * 1) Marks the end of the prologue with %%EndProlog.
54 b855148c 2004-05-16 devnull *
55 b855148c 2004-05-16 devnull * 2) May have a %%BeginSetup/%%EndSetup comment pair before the first %%Page:
56 b855148c 2004-05-16 devnull * comment that brackets any global initialization.
57 b855148c 2004-05-16 devnull *
58 b855148c 2004-05-16 devnull * 3) Marks the start of each page with a %%Page: comment, or the end of each
59 b855148c 2004-05-16 devnull * page with a %%EndPage: comment. Both page boundary comments are allowed.
60 b855148c 2004-05-16 devnull *
61 b855148c 2004-05-16 devnull * 4) Marks the end of all the pages with a %%Trailer comment.
62 b855148c 2004-05-16 devnull *
63 b855148c 2004-05-16 devnull * 5) Obeys page independence or violates it to a rather limited extent and
64 b855148c 2004-05-16 devnull * marks the violations with %%BeginGlobal and %%EndGlobal comments.
65 b855148c 2004-05-16 devnull *
66 b855148c 2004-05-16 devnull * If no file arguments are given postreverse copies stdin to a temporary file and
67 b855148c 2004-05-16 devnull * then processes that file. That means the input is read three times (rather than
68 b855148c 2004-05-16 devnull * two) whenever we handle stdin. That's expensive, and shouldn't be too difficult
69 b855148c 2004-05-16 devnull * to fix, but I haven't gotten around to it yet.
70 b855148c 2004-05-16 devnull *
71 b855148c 2004-05-16 devnull */
72 b855148c 2004-05-16 devnull
73 b855148c 2004-05-16 devnull #include <stdio.h>
74 b855148c 2004-05-16 devnull #include <signal.h>
75 b855148c 2004-05-16 devnull #include <sys/types.h>
76 b855148c 2004-05-16 devnull #include <fcntl.h>
77 b855148c 2004-05-16 devnull
78 b855148c 2004-05-16 devnull #include "comments.h" /* PostScript file structuring comments */
79 b855148c 2004-05-16 devnull #include "gen.h" /* general purpose definitions */
80 b855148c 2004-05-16 devnull #include "path.h" /* for temporary directory */
81 b855148c 2004-05-16 devnull #include "ext.h" /* external variable declarations */
82 b855148c 2004-05-16 devnull #include "postreverse.h" /* a few special definitions */
83 b855148c 2004-05-16 devnull
84 b855148c 2004-05-16 devnull int page = 1; /* current page number */
85 b855148c 2004-05-16 devnull int forms = 1; /* forms per page in the input file */
86 b855148c 2004-05-16 devnull
87 b855148c 2004-05-16 devnull char *temp_dir = TEMPDIR; /* temp directory for copying stdin */
88 b855148c 2004-05-16 devnull
89 b855148c 2004-05-16 devnull Pages pages[1000]; /* byte offsets for all pages */
90 b855148c 2004-05-16 devnull int next_page = 0; /* next page goes here */
91 b855148c 2004-05-16 devnull long start; /* starting offset for next page */
92 b855148c 2004-05-16 devnull long endoff = -1; /* offset where TRAILER was found */
93 b855148c 2004-05-16 devnull int noreverse = FALSE; /* don't reverse pages if TRUE */
94 b855148c 2004-05-16 devnull char *endprolog = ENDPROLOG; /* occasionally changed to ENDSETUP */
95 b855148c 2004-05-16 devnull
96 b855148c 2004-05-16 devnull double version = 3.3; /* of the input file */
97 b855148c 2004-05-16 devnull int ignoreversion = FALSE; /* ignore possible forms.ps problems */
98 b855148c 2004-05-16 devnull
99 b855148c 2004-05-16 devnull char buf[2048]; /* line buffer for input file */
100 b855148c 2004-05-16 devnull
101 b855148c 2004-05-16 devnull FILE *fp_in; /* stuff is read from this file */
102 b855148c 2004-05-16 devnull FILE *fp_out; /* and written here */
103 b855148c 2004-05-16 devnull
104 b855148c 2004-05-16 devnull /*****************************************************************************/
105 b855148c 2004-05-16 devnull
106 b855148c 2004-05-16 devnull main(agc, agv)
107 b855148c 2004-05-16 devnull
108 b855148c 2004-05-16 devnull int agc;
109 b855148c 2004-05-16 devnull char *agv[];
110 b855148c 2004-05-16 devnull
111 b855148c 2004-05-16 devnull {
112 b855148c 2004-05-16 devnull
113 b855148c 2004-05-16 devnull /*
114 b855148c 2004-05-16 devnull *
115 b855148c 2004-05-16 devnull * A simple program that reverses the pages in specially formatted PostScript
116 b855148c 2004-05-16 devnull * files. Will work with all the translators in this package, and should handle
117 b855148c 2004-05-16 devnull * any document that conforms to Adobe's version 1.0 or 2.0 file structuring
118 b855148c 2004-05-16 devnull * conventions. Only one input file is allowed, and it can either be a named (on
119 b855148c 2004-05-16 devnull * the command line) file or stdin.
120 b855148c 2004-05-16 devnull *
121 b855148c 2004-05-16 devnull */
122 b855148c 2004-05-16 devnull
123 b855148c 2004-05-16 devnull argc = agc; /* other routines may want them */
124 b855148c 2004-05-16 devnull argv = agv;
125 b855148c 2004-05-16 devnull
126 b855148c 2004-05-16 devnull prog_name = argv[0]; /* just for error messages */
127 b855148c 2004-05-16 devnull
128 b855148c 2004-05-16 devnull fp_in = stdin;
129 b855148c 2004-05-16 devnull fp_out = stdout;
130 b855148c 2004-05-16 devnull
131 b855148c 2004-05-16 devnull init_signals(); /* sets up interrupt handling */
132 b855148c 2004-05-16 devnull options(); /* first get command line options */
133 b855148c 2004-05-16 devnull arguments(); /* then process non-option arguments */
134 b855148c 2004-05-16 devnull done(); /* and clean things up */
135 b855148c 2004-05-16 devnull
136 b855148c 2004-05-16 devnull exit(x_stat); /* not much could be wrong */
137 b855148c 2004-05-16 devnull
138 b855148c 2004-05-16 devnull } /* End of main */
139 b855148c 2004-05-16 devnull
140 b855148c 2004-05-16 devnull /*****************************************************************************/
141 b855148c 2004-05-16 devnull
142 b855148c 2004-05-16 devnull init_signals()
143 b855148c 2004-05-16 devnull
144 b855148c 2004-05-16 devnull {
145 b855148c 2004-05-16 devnull
146 b855148c 2004-05-16 devnull /*
147 b855148c 2004-05-16 devnull *
148 b855148c 2004-05-16 devnull * Makes sure we handle interrupts properly.
149 b855148c 2004-05-16 devnull *
150 b855148c 2004-05-16 devnull */
151 b855148c 2004-05-16 devnull
152 b855148c 2004-05-16 devnull if ( signal(SIGINT, interrupt) == SIG_IGN ) {
153 b855148c 2004-05-16 devnull signal(SIGINT, SIG_IGN);
154 b855148c 2004-05-16 devnull signal(SIGQUIT, SIG_IGN);
155 b855148c 2004-05-16 devnull signal(SIGHUP, SIG_IGN);
156 b855148c 2004-05-16 devnull } else {
157 b855148c 2004-05-16 devnull signal(SIGHUP, interrupt);
158 b855148c 2004-05-16 devnull signal(SIGQUIT, interrupt);
159 b855148c 2004-05-16 devnull } /* End else */
160 b855148c 2004-05-16 devnull
161 b855148c 2004-05-16 devnull signal(SIGTERM, interrupt);
162 b855148c 2004-05-16 devnull
163 b855148c 2004-05-16 devnull } /* End of init_signals */
164 b855148c 2004-05-16 devnull
165 b855148c 2004-05-16 devnull /*****************************************************************************/
166 b855148c 2004-05-16 devnull
167 b855148c 2004-05-16 devnull options()
168 b855148c 2004-05-16 devnull
169 b855148c 2004-05-16 devnull {
170 b855148c 2004-05-16 devnull
171 b855148c 2004-05-16 devnull int ch; /* return value from getopt() */
172 b855148c 2004-05-16 devnull char *optnames = "n:o:rvT:DI";
173 b855148c 2004-05-16 devnull
174 b855148c 2004-05-16 devnull extern char *optarg; /* used by getopt() */
175 b855148c 2004-05-16 devnull extern int optind;
176 b855148c 2004-05-16 devnull
177 b855148c 2004-05-16 devnull /*
178 b855148c 2004-05-16 devnull *
179 b855148c 2004-05-16 devnull * Reads and processes the command line options. The -r option (ie. the one that
180 b855148c 2004-05-16 devnull * turns page reversal off) is really only useful if you want to take dpost output
181 b855148c 2004-05-16 devnull * and produce a page independent output file. In that case global definitions
182 b855148c 2004-05-16 devnull * made within pages and bracketed by %%BeginGlobal/%%EndGlobal comments will be
183 b855148c 2004-05-16 devnull * moved into the prologue or setup section of the document.
184 b855148c 2004-05-16 devnull *
185 b855148c 2004-05-16 devnull */
186 b855148c 2004-05-16 devnull
187 b855148c 2004-05-16 devnull while ( (ch = getopt(argc, argv, optnames)) != EOF ) {
188 b855148c 2004-05-16 devnull switch ( ch ) {
189 b855148c 2004-05-16 devnull case 'n': /* forms per page */
190 b855148c 2004-05-16 devnull if ( (forms = atoi(optarg)) <= 0 )
191 b855148c 2004-05-16 devnull error(FATAL, "illegal forms request %s", optarg);
192 b855148c 2004-05-16 devnull break;
193 b855148c 2004-05-16 devnull
194 b855148c 2004-05-16 devnull case 'o': /* output page list */
195 b855148c 2004-05-16 devnull out_list(optarg);
196 b855148c 2004-05-16 devnull break;
197 b855148c 2004-05-16 devnull
198 b855148c 2004-05-16 devnull case 'r': /* don't reverse the pages */
199 b855148c 2004-05-16 devnull noreverse = TRUE;
200 b855148c 2004-05-16 devnull break;
201 b855148c 2004-05-16 devnull
202 b855148c 2004-05-16 devnull case 'v': /* ignore possible forms.ps problems */
203 b855148c 2004-05-16 devnull ignoreversion = TRUE;
204 b855148c 2004-05-16 devnull break;
205 b855148c 2004-05-16 devnull
206 b855148c 2004-05-16 devnull case 'T': /* temporary file directory */
207 b855148c 2004-05-16 devnull temp_dir = optarg;
208 b855148c 2004-05-16 devnull break;
209 b855148c 2004-05-16 devnull
210 b855148c 2004-05-16 devnull case 'D': /* debug flag */
211 b855148c 2004-05-16 devnull debug = ON;
212 b855148c 2004-05-16 devnull break;
213 b855148c 2004-05-16 devnull
214 b855148c 2004-05-16 devnull case 'I': /* ignore FATAL errors */
215 b855148c 2004-05-16 devnull ignore = ON;
216 b855148c 2004-05-16 devnull break;
217 b855148c 2004-05-16 devnull
218 b855148c 2004-05-16 devnull case '?': /* don't understand the option */
219 b855148c 2004-05-16 devnull error(FATAL, "");
220 b855148c 2004-05-16 devnull break;
221 b855148c 2004-05-16 devnull
222 b855148c 2004-05-16 devnull default: /* don't know what to do for ch */
223 b855148c 2004-05-16 devnull error(FATAL, "missing case for option %c\n", ch);
224 b855148c 2004-05-16 devnull break;
225 b855148c 2004-05-16 devnull } /* End switch */
226 b855148c 2004-05-16 devnull } /* End while */
227 b855148c 2004-05-16 devnull
228 b855148c 2004-05-16 devnull argc -= optind; /* get ready for non-option args */
229 b855148c 2004-05-16 devnull argv += optind;
230 b855148c 2004-05-16 devnull
231 b855148c 2004-05-16 devnull } /* End of options */
232 b855148c 2004-05-16 devnull
233 b855148c 2004-05-16 devnull /*****************************************************************************/
234 b855148c 2004-05-16 devnull
235 b855148c 2004-05-16 devnull arguments()
236 b855148c 2004-05-16 devnull
237 b855148c 2004-05-16 devnull {
238 b855148c 2004-05-16 devnull
239 b855148c 2004-05-16 devnull char *name; /* name of the input file */
240 b855148c 2004-05-16 devnull
241 b855148c 2004-05-16 devnull /*
242 b855148c 2004-05-16 devnull *
243 b855148c 2004-05-16 devnull * postreverse only handles one input file at a time, so if there's more than one
244 b855148c 2004-05-16 devnull * argument left when we get here we'll quit. If none remain we copy stdin to a
245 b855148c 2004-05-16 devnull * temporary file and process that file.
246 b855148c 2004-05-16 devnull *
247 b855148c 2004-05-16 devnull */
248 b855148c 2004-05-16 devnull
249 b855148c 2004-05-16 devnull if ( argc > 1 ) /* can't handle more than one file */
250 b855148c 2004-05-16 devnull error(FATAL, "too many arguments");
251 b855148c 2004-05-16 devnull
252 b855148c 2004-05-16 devnull if ( argc == 0 ) /* copy stdin to a temporary file */
253 b855148c 2004-05-16 devnull name = copystdin();
254 b855148c 2004-05-16 devnull else name = *argv;
255 b855148c 2004-05-16 devnull
256 b855148c 2004-05-16 devnull if ( (fp_in = fopen(name, "r")) == NULL )
257 b855148c 2004-05-16 devnull error(FATAL, "can't open %s", name);
258 b855148c 2004-05-16 devnull
259 b855148c 2004-05-16 devnull reverse();
260 b855148c 2004-05-16 devnull
261 b855148c 2004-05-16 devnull } /* End of arguments */
262 b855148c 2004-05-16 devnull
263 b855148c 2004-05-16 devnull /*****************************************************************************/
264 b855148c 2004-05-16 devnull
265 b855148c 2004-05-16 devnull done()
266 b855148c 2004-05-16 devnull
267 b855148c 2004-05-16 devnull {
268 b855148c 2004-05-16 devnull
269 b855148c 2004-05-16 devnull /*
270 b855148c 2004-05-16 devnull *
271 b855148c 2004-05-16 devnull * Cleans things up after we've finished reversing the pages in the input file.
272 b855148c 2004-05-16 devnull * All that's really left to do is remove the temp file, provided we used one.
273 b855148c 2004-05-16 devnull *
274 b855148c 2004-05-16 devnull */
275 b855148c 2004-05-16 devnull
276 b855148c 2004-05-16 devnull if ( temp_file != NULL )
277 b855148c 2004-05-16 devnull unlink(temp_file);
278 b855148c 2004-05-16 devnull
279 b855148c 2004-05-16 devnull } /* End of done */
280 b855148c 2004-05-16 devnull
281 b855148c 2004-05-16 devnull /*****************************************************************************/
282 b855148c 2004-05-16 devnull
283 b855148c 2004-05-16 devnull char *copystdin()
284 b855148c 2004-05-16 devnull
285 b855148c 2004-05-16 devnull {
286 b855148c 2004-05-16 devnull
287 b855148c 2004-05-16 devnull int fd_out; /* for the temporary file */
288 b855148c 2004-05-16 devnull int fd_in; /* for stdin */
289 b855148c 2004-05-16 devnull int count; /* number of bytes put in buf[] */
290 b855148c 2004-05-16 devnull
291 b855148c 2004-05-16 devnull /*
292 b855148c 2004-05-16 devnull *
293 b855148c 2004-05-16 devnull * Copies stdin to a temporary file and returns the pathname of that file to the
294 b855148c 2004-05-16 devnull * caller. It's an expensive way of doing things, because it means we end up
295 b855148c 2004-05-16 devnull * reading the input file three times - rather than just twice. Could probably be
296 b855148c 2004-05-16 devnull * fixed by creating the temporary file on the fly as we read the file the first
297 b855148c 2004-05-16 devnull * time.
298 b855148c 2004-05-16 devnull *
299 b855148c 2004-05-16 devnull */
300 b855148c 2004-05-16 devnull
301 b855148c 2004-05-16 devnull if ( (temp_file = tempnam(temp_dir, "post")) == NULL )
302 b855148c 2004-05-16 devnull error(FATAL, "can't generate temp file name");
303 b855148c 2004-05-16 devnull
304 b855148c 2004-05-16 devnull if ( (fd_out = creat(temp_file, 0660)) == -1 )
305 b855148c 2004-05-16 devnull error(FATAL, "can't open %s", temp_file);
306 b855148c 2004-05-16 devnull
307 b855148c 2004-05-16 devnull fd_in = fileno(stdin);
308 b855148c 2004-05-16 devnull
309 b855148c 2004-05-16 devnull while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )
310 b855148c 2004-05-16 devnull if ( write(fd_out, buf, count) != count )
311 b855148c 2004-05-16 devnull error(FATAL, "error writing to %s", temp_file);
312 b855148c 2004-05-16 devnull
313 b855148c 2004-05-16 devnull close(fd_out);
314 b855148c 2004-05-16 devnull
315 b855148c 2004-05-16 devnull return(temp_file);
316 b855148c 2004-05-16 devnull
317 b855148c 2004-05-16 devnull } /* End of copystdin */
318 b855148c 2004-05-16 devnull
319 b855148c 2004-05-16 devnull /*****************************************************************************/
320 b855148c 2004-05-16 devnull
321 b855148c 2004-05-16 devnull reverse()
322 b855148c 2004-05-16 devnull
323 b855148c 2004-05-16 devnull {
324 b855148c 2004-05-16 devnull
325 b855148c 2004-05-16 devnull /*
326 b855148c 2004-05-16 devnull *
327 b855148c 2004-05-16 devnull * Begins by looking for the ENDPROLOG comment in the input file. Everything up to
328 b855148c 2004-05-16 devnull * that comment is copied to the output file. If the comment isn't found the entire
329 b855148c 2004-05-16 devnull * input file is copied and moreprolog() returns FALSE. Otherwise readpages() reads
330 b855148c 2004-05-16 devnull * the rest of the input file and remembers (in pages[]) where each page starts and
331 b855148c 2004-05-16 devnull * ends. In addition everything bracketed by %%BeginGlobal and %%EndGlobal comments
332 b855148c 2004-05-16 devnull * is immediately added to the new prologue (or setup section) and ends up being
333 b855148c 2004-05-16 devnull * removed from the individual pages. When readpages() finds the TRAILER comment
334 b855148c 2004-05-16 devnull * or gets to the end of the input file we go back to the pages[] array and use
335 b855148c 2004-05-16 devnull * the saved offsets to write the pages out in reverse order. Finally everything
336 b855148c 2004-05-16 devnull * from the TRAILER comment to the end of the input file is copied to the output
337 b855148c 2004-05-16 devnull * file.
338 b855148c 2004-05-16 devnull *
339 b855148c 2004-05-16 devnull */
340 b855148c 2004-05-16 devnull
341 b855148c 2004-05-16 devnull if ( moreprolog(ENDPROLOG) == TRUE ) {
342 b855148c 2004-05-16 devnull readpages();
343 b855148c 2004-05-16 devnull writepages();
344 b855148c 2004-05-16 devnull trailer();
345 b855148c 2004-05-16 devnull } /* End if */
346 b855148c 2004-05-16 devnull
347 b855148c 2004-05-16 devnull } /* End of reverse */
348 b855148c 2004-05-16 devnull
349 b855148c 2004-05-16 devnull /*****************************************************************************/
350 b855148c 2004-05-16 devnull
351 b855148c 2004-05-16 devnull moreprolog(str)
352 b855148c 2004-05-16 devnull
353 b855148c 2004-05-16 devnull char *str; /* copy everything up to this string */
354 b855148c 2004-05-16 devnull
355 b855148c 2004-05-16 devnull {
356 b855148c 2004-05-16 devnull
357 b855148c 2004-05-16 devnull int len; /* length of FORMSPERPAGE string */
358 b855148c 2004-05-16 devnull int vlen; /* length of VERSION string */
359 b855148c 2004-05-16 devnull
360 b855148c 2004-05-16 devnull /*
361 b855148c 2004-05-16 devnull *
362 b855148c 2004-05-16 devnull * Looks for string *str at the start of a line and copies everything up to that
363 b855148c 2004-05-16 devnull * string to the output file. If *str isn't found the entire input file will end
364 b855148c 2004-05-16 devnull * up being copied to the output file and FALSE will be returned to the caller.
365 b855148c 2004-05-16 devnull * The first call (made from reverse()) looks for ENDPROLOG. Any other call comes
366 b855148c 2004-05-16 devnull * from readpages() and will be looking for the ENDSETUP comment.
367 fa325e9b 2020-01-10 cross *
368 b855148c 2004-05-16 devnull */
369 b855148c 2004-05-16 devnull
370 b855148c 2004-05-16 devnull len = strlen(FORMSPERPAGE);
371 b855148c 2004-05-16 devnull vlen = strlen(VERSION);
372 b855148c 2004-05-16 devnull
373 b855148c 2004-05-16 devnull while ( fgets(buf, sizeof(buf), fp_in) != NULL ) {
374 b855148c 2004-05-16 devnull if ( strcmp(buf, str) == 0 )
375 b855148c 2004-05-16 devnull return(TRUE);
376 b855148c 2004-05-16 devnull else if ( strncmp(buf, FORMSPERPAGE, len) == 0 )
377 b855148c 2004-05-16 devnull forms = atoi(&buf[len+1]);
378 b855148c 2004-05-16 devnull else if ( strncmp(buf, VERSION, vlen) == 0 )
379 b855148c 2004-05-16 devnull version = atof(&buf[vlen+1]);
380 b855148c 2004-05-16 devnull fprintf(fp_out, "%s", buf);
381 b855148c 2004-05-16 devnull } /* End while */
382 b855148c 2004-05-16 devnull
383 b855148c 2004-05-16 devnull return(FALSE);
384 b855148c 2004-05-16 devnull
385 b855148c 2004-05-16 devnull } /* End of moreprolog */
386 b855148c 2004-05-16 devnull
387 b855148c 2004-05-16 devnull /*****************************************************************************/
388 b855148c 2004-05-16 devnull
389 b855148c 2004-05-16 devnull readpages()
390 b855148c 2004-05-16 devnull
391 b855148c 2004-05-16 devnull {
392 b855148c 2004-05-16 devnull
393 b855148c 2004-05-16 devnull int endpagelen; /* length of ENDPAGE */
394 b855148c 2004-05-16 devnull int pagelen; /* and PAGE strings */
395 b855148c 2004-05-16 devnull int sawendpage = TRUE; /* ENDPAGE equivalent marked last page */
396 b855148c 2004-05-16 devnull int gotpage = FALSE; /* TRUE disables BEGINSETUP stuff */
397 b855148c 2004-05-16 devnull
398 b855148c 2004-05-16 devnull /*
399 b855148c 2004-05-16 devnull *
400 b855148c 2004-05-16 devnull * Records starting and ending positions of the requested pages (usually all of
401 b855148c 2004-05-16 devnull * them), puts global definitions in the prologue, and remembers where the TRAILER
402 b855148c 2004-05-16 devnull * was found.
403 b855148c 2004-05-16 devnull *
404 b855148c 2004-05-16 devnull * Page boundaries are marked by the strings PAGE, ENDPAGE, or perhaps both.
405 b855148c 2004-05-16 devnull * Application programs will normally find one or the other more convenient, so
406 b855148c 2004-05-16 devnull * in most cases only one kind of page delimiter will be found in a particular
407 b855148c 2004-05-16 devnull * document.
408 b855148c 2004-05-16 devnull *
409 b855148c 2004-05-16 devnull */
410 b855148c 2004-05-16 devnull
411 b855148c 2004-05-16 devnull pages[0].start = ftell(fp_in); /* first page starts after ENDPROLOG */
412 b855148c 2004-05-16 devnull endprolog = ENDPROLOG;
413 b855148c 2004-05-16 devnull
414 b855148c 2004-05-16 devnull endpagelen = strlen(ENDPAGE);
415 b855148c 2004-05-16 devnull pagelen = strlen(PAGE);
416 b855148c 2004-05-16 devnull
417 b855148c 2004-05-16 devnull while ( fgets(buf, sizeof(buf), fp_in) != NULL )
418 b855148c 2004-05-16 devnull if ( buf[0] != '%' )
419 b855148c 2004-05-16 devnull continue;
420 b855148c 2004-05-16 devnull else if ( strncmp(buf, ENDPAGE, endpagelen) == 0 ) {
421 b855148c 2004-05-16 devnull if ( in_olist(page++) == ON ) {
422 b855148c 2004-05-16 devnull pages[next_page].empty = FALSE;
423 b855148c 2004-05-16 devnull pages[next_page++].stop = ftell(fp_in);
424 b855148c 2004-05-16 devnull } /* End if */
425 b855148c 2004-05-16 devnull pages[next_page].start = ftell(fp_in);
426 b855148c 2004-05-16 devnull sawendpage = TRUE;
427 b855148c 2004-05-16 devnull gotpage = TRUE;
428 b855148c 2004-05-16 devnull } else if ( strncmp(buf, PAGE, pagelen) == 0 ) {
429 b855148c 2004-05-16 devnull if ( sawendpage == FALSE && in_olist(page++) == ON ) {
430 b855148c 2004-05-16 devnull pages[next_page].empty = FALSE;
431 b855148c 2004-05-16 devnull pages[next_page++].stop = ftell(fp_in) - strlen(buf);
432 b855148c 2004-05-16 devnull } /* End if */
433 b855148c 2004-05-16 devnull pages[next_page].start = ftell(fp_in) - strlen(buf);
434 b855148c 2004-05-16 devnull sawendpage = FALSE;
435 b855148c 2004-05-16 devnull gotpage = TRUE;
436 b855148c 2004-05-16 devnull } else if ( gotpage == FALSE && strcmp(buf, BEGINSETUP) == 0 ) {
437 b855148c 2004-05-16 devnull fprintf(fp_out, "%s", endprolog);
438 b855148c 2004-05-16 devnull fprintf(fp_out, "%s", BEGINSETUP);
439 b855148c 2004-05-16 devnull moreprolog(ENDSETUP);
440 b855148c 2004-05-16 devnull endprolog = ENDSETUP;
441 b855148c 2004-05-16 devnull } else if ( strcmp(buf, BEGINGLOBAL) == 0 ) {
442 b855148c 2004-05-16 devnull moreprolog(ENDGLOBAL);
443 b855148c 2004-05-16 devnull } else if ( strcmp(buf, TRAILER) == 0 ) {
444 b855148c 2004-05-16 devnull if ( sawendpage == FALSE )
445 b855148c 2004-05-16 devnull pages[next_page++].stop = ftell(fp_in) - strlen(buf);
446 b855148c 2004-05-16 devnull endoff = ftell(fp_in);
447 b855148c 2004-05-16 devnull break;
448 b855148c 2004-05-16 devnull } /* End if */
449 b855148c 2004-05-16 devnull
450 b855148c 2004-05-16 devnull } /* End of readpages */
451 b855148c 2004-05-16 devnull
452 b855148c 2004-05-16 devnull /*****************************************************************************/
453 b855148c 2004-05-16 devnull
454 b855148c 2004-05-16 devnull writepages()
455 b855148c 2004-05-16 devnull
456 b855148c 2004-05-16 devnull {
457 b855148c 2004-05-16 devnull
458 b855148c 2004-05-16 devnull int i, j, k; /* loop indices */
459 b855148c 2004-05-16 devnull
460 b855148c 2004-05-16 devnull /*
461 b855148c 2004-05-16 devnull *
462 b855148c 2004-05-16 devnull * Goes through the pages[] array, usually from the bottom up, and writes out all
463 b855148c 2004-05-16 devnull * the pages. Documents that print more than one form per page cause things to get
464 b855148c 2004-05-16 devnull * a little more complicated. Each physical page has to have its subpages printed
465 b855148c 2004-05-16 devnull * in the correct order, and we have to build a few dummy subpages for the last
466 b855148c 2004-05-16 devnull * (and now first) sheet of paper, otherwise things will only occasionally work.
467 b855148c 2004-05-16 devnull *
468 b855148c 2004-05-16 devnull */
469 b855148c 2004-05-16 devnull
470 b855148c 2004-05-16 devnull fprintf(fp_out, "%s", endprolog);
471 b855148c 2004-05-16 devnull
472 b855148c 2004-05-16 devnull if ( noreverse == FALSE ) /* fill out the first page */
473 b855148c 2004-05-16 devnull for ( i = (forms - next_page % forms) % forms; i > 0; i--, next_page++ )
474 b855148c 2004-05-16 devnull pages[next_page].empty = TRUE;
475 b855148c 2004-05-16 devnull else forms = next_page; /* turns reversal off in next loop */
476 b855148c 2004-05-16 devnull
477 b855148c 2004-05-16 devnull for ( i = next_page - forms; i >= 0; i -= forms )
478 b855148c 2004-05-16 devnull for ( j = i, k = 0; k < forms; j++, k++ )
479 b855148c 2004-05-16 devnull if ( pages[j].empty == TRUE ) {
480 b855148c 2004-05-16 devnull if ( ignoreversion == TRUE || version > 3.1 ) {
481 b855148c 2004-05-16 devnull fprintf(fp_out, "%s 0 0\n", PAGE);
482 b855148c 2004-05-16 devnull fprintf(fp_out, "/saveobj save def\n");
483 b855148c 2004-05-16 devnull fprintf(fp_out, "showpage\n");
484 b855148c 2004-05-16 devnull fprintf(fp_out, "saveobj restore\n");
485 b855148c 2004-05-16 devnull fprintf(fp_out, "%s 0 0\n", ENDPAGE);
486 b855148c 2004-05-16 devnull } else {
487 b855148c 2004-05-16 devnull fprintf(fp_out, "%s 0 0\n", PAGE);
488 b855148c 2004-05-16 devnull fprintf(fp_out, "save showpage restore\n");
489 b855148c 2004-05-16 devnull fprintf(fp_out, "%s 0 0\n", ENDPAGE);
490 b855148c 2004-05-16 devnull } /* End else */
491 b855148c 2004-05-16 devnull } else copypage(pages[j].start, pages[j].stop);
492 b855148c 2004-05-16 devnull
493 b855148c 2004-05-16 devnull } /* End of writepages */
494 b855148c 2004-05-16 devnull
495 b855148c 2004-05-16 devnull /*****************************************************************************/
496 b855148c 2004-05-16 devnull
497 b855148c 2004-05-16 devnull copypage(start, stop)
498 b855148c 2004-05-16 devnull
499 b855148c 2004-05-16 devnull long start; /* starting from this offset */
500 b855148c 2004-05-16 devnull long stop; /* and ending here */
501 b855148c 2004-05-16 devnull
502 b855148c 2004-05-16 devnull {
503 b855148c 2004-05-16 devnull
504 b855148c 2004-05-16 devnull /*
505 b855148c 2004-05-16 devnull *
506 b855148c 2004-05-16 devnull * Copies the page beginning at offset start and ending at stop to the output
507 b855148c 2004-05-16 devnull * file. Global definitions are skipped since they've already been added to the
508 b855148c 2004-05-16 devnull * prologue.
509 b855148c 2004-05-16 devnull *
510 b855148c 2004-05-16 devnull */
511 b855148c 2004-05-16 devnull
512 b855148c 2004-05-16 devnull fseek(fp_in, start, 0);
513 b855148c 2004-05-16 devnull
514 b855148c 2004-05-16 devnull while ( ftell(fp_in) < stop && fgets(buf, sizeof(buf), fp_in) != NULL )
515 b855148c 2004-05-16 devnull if ( buf[0] == '%' && strcmp(buf, BEGINGLOBAL) == 0 )
516 b855148c 2004-05-16 devnull while ( fgets(buf, sizeof(buf), fp_in) != NULL && strcmp(buf, ENDGLOBAL) != 0 ) ;
517 b855148c 2004-05-16 devnull else fprintf(fp_out, "%s", buf);
518 b855148c 2004-05-16 devnull
519 b855148c 2004-05-16 devnull } /* End of copypage */
520 b855148c 2004-05-16 devnull
521 b855148c 2004-05-16 devnull /*****************************************************************************/
522 b855148c 2004-05-16 devnull
523 b855148c 2004-05-16 devnull trailer()
524 b855148c 2004-05-16 devnull
525 b855148c 2004-05-16 devnull {
526 b855148c 2004-05-16 devnull
527 b855148c 2004-05-16 devnull /*
528 b855148c 2004-05-16 devnull *
529 b855148c 2004-05-16 devnull * Makes sure everything from the TRAILER string to EOF is copied to the output
530 b855148c 2004-05-16 devnull * file.
531 b855148c 2004-05-16 devnull *
532 b855148c 2004-05-16 devnull */
533 b855148c 2004-05-16 devnull
534 b855148c 2004-05-16 devnull if ( endoff > 0 ) {
535 b855148c 2004-05-16 devnull fprintf(fp_out, "%s", TRAILER);
536 b855148c 2004-05-16 devnull fseek(fp_in, endoff, 0);
537 b855148c 2004-05-16 devnull while ( fgets(buf, sizeof(buf), fp_in) != NULL )
538 b855148c 2004-05-16 devnull fprintf(fp_out, "%s", buf);
539 b855148c 2004-05-16 devnull } /* End if */
540 b855148c 2004-05-16 devnull
541 b855148c 2004-05-16 devnull } /* End of trailer */
542 b855148c 2004-05-16 devnull
543 b855148c 2004-05-16 devnull /*****************************************************************************/