3 b855148c 2004-05-16 devnull * postreverse - reverse the page order in certain PostScript files.
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,
10 b855148c 2004-05-16 devnull * 1) Marks the end of the prologue with an %%EndProlog comment.
12 b855148c 2004-05-16 devnull * 2) Starts each page with a %%Page: comment.
14 b855148c 2004-05-16 devnull * 3) Marks the end of all the pages %%Trailer comment.
16 b855148c 2004-05-16 devnull * 4) Obeys page independence (ie. pages can be arbitrarily rearranged).
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,
25 b855148c 2004-05-16 devnull * 1) Marks the end of the prologue (only definitions) with %%EndProlog.
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.
30 b855148c 2004-05-16 devnull * 3) Starts each page with a %%Page: comment.
32 b855148c 2004-05-16 devnull * 4) Marks the end of all the pages with a %%Trailer comment.
34 b855148c 2004-05-16 devnull * 5) Obeys page independence.
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.
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.
51 b855148c 2004-05-16 devnull * So a document that will reverse properly with postreverse,
53 b855148c 2004-05-16 devnull * 1) Marks the end of the prologue with %%EndProlog.
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.
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.
61 b855148c 2004-05-16 devnull * 4) Marks the end of all the pages with a %%Trailer comment.
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.
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.
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>
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 */
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 */
87 b855148c 2004-05-16 devnull char *temp_dir = TEMPDIR; /* temp directory for copying stdin */
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 */
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 */
99 b855148c 2004-05-16 devnull char buf[2048]; /* line buffer for input file */
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 */
104 b855148c 2004-05-16 devnull /*****************************************************************************/
106 b855148c 2004-05-16 devnull main(agc, agv)
108 b855148c 2004-05-16 devnull int agc;
109 b855148c 2004-05-16 devnull char *agv[];
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.
123 b855148c 2004-05-16 devnull argc = agc; /* other routines may want them */
124 b855148c 2004-05-16 devnull argv = agv;
126 b855148c 2004-05-16 devnull prog_name = argv[0]; /* just for error messages */
128 b855148c 2004-05-16 devnull fp_in = stdin;
129 b855148c 2004-05-16 devnull fp_out = stdout;
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 */
136 b855148c 2004-05-16 devnull exit(x_stat); /* not much could be wrong */
138 b855148c 2004-05-16 devnull } /* End of main */
140 b855148c 2004-05-16 devnull /*****************************************************************************/
142 b855148c 2004-05-16 devnull init_signals()
148 b855148c 2004-05-16 devnull * Makes sure we handle interrupts properly.
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 */
161 b855148c 2004-05-16 devnull signal(SIGTERM, interrupt);
163 b855148c 2004-05-16 devnull } /* End of init_signals */
165 b855148c 2004-05-16 devnull /*****************************************************************************/
167 b855148c 2004-05-16 devnull options()
171 b855148c 2004-05-16 devnull int ch; /* return value from getopt() */
172 b855148c 2004-05-16 devnull char *optnames = "n:o:rvT:DI";
174 b855148c 2004-05-16 devnull extern char *optarg; /* used by getopt() */
175 b855148c 2004-05-16 devnull extern int optind;
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.
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);
194 b855148c 2004-05-16 devnull case 'o': /* output page list */
195 b855148c 2004-05-16 devnull out_list(optarg);
198 b855148c 2004-05-16 devnull case 'r': /* don't reverse the pages */
199 b855148c 2004-05-16 devnull noreverse = TRUE;
202 b855148c 2004-05-16 devnull case 'v': /* ignore possible forms.ps problems */
203 b855148c 2004-05-16 devnull ignoreversion = TRUE;
206 b855148c 2004-05-16 devnull case 'T': /* temporary file directory */
207 b855148c 2004-05-16 devnull temp_dir = optarg;
210 b855148c 2004-05-16 devnull case 'D': /* debug flag */
211 b855148c 2004-05-16 devnull debug = ON;
214 b855148c 2004-05-16 devnull case 'I': /* ignore FATAL errors */
215 b855148c 2004-05-16 devnull ignore = ON;
218 b855148c 2004-05-16 devnull case '?': /* don't understand the option */
219 b855148c 2004-05-16 devnull error(FATAL, "");
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);
225 b855148c 2004-05-16 devnull } /* End switch */
226 b855148c 2004-05-16 devnull } /* End while */
228 b855148c 2004-05-16 devnull argc -= optind; /* get ready for non-option args */
229 b855148c 2004-05-16 devnull argv += optind;
231 b855148c 2004-05-16 devnull } /* End of options */
233 b855148c 2004-05-16 devnull /*****************************************************************************/
235 b855148c 2004-05-16 devnull arguments()
239 b855148c 2004-05-16 devnull char *name; /* name of the input file */
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.
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");
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;
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);
259 b855148c 2004-05-16 devnull reverse();
261 b855148c 2004-05-16 devnull } /* End of arguments */
263 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.
276 b855148c 2004-05-16 devnull if ( temp_file != NULL )
277 b855148c 2004-05-16 devnull unlink(temp_file);
279 b855148c 2004-05-16 devnull } /* End of done */
281 b855148c 2004-05-16 devnull /*****************************************************************************/
283 b855148c 2004-05-16 devnull char *copystdin()
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[] */
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
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");
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);
307 b855148c 2004-05-16 devnull fd_in = fileno(stdin);
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);
313 b855148c 2004-05-16 devnull close(fd_out);
315 b855148c 2004-05-16 devnull return(temp_file);
317 b855148c 2004-05-16 devnull } /* End of copystdin */
319 b855148c 2004-05-16 devnull /*****************************************************************************/
321 b855148c 2004-05-16 devnull reverse()
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
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 */
347 b855148c 2004-05-16 devnull } /* End of reverse */
349 b855148c 2004-05-16 devnull /*****************************************************************************/
351 b855148c 2004-05-16 devnull moreprolog(str)
353 b855148c 2004-05-16 devnull char *str; /* copy everything up to this string */
357 b855148c 2004-05-16 devnull int len; /* length of FORMSPERPAGE string */
358 b855148c 2004-05-16 devnull int vlen; /* length of VERSION string */
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.
370 b855148c 2004-05-16 devnull len = strlen(FORMSPERPAGE);
371 b855148c 2004-05-16 devnull vlen = strlen(VERSION);
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 */
383 b855148c 2004-05-16 devnull return(FALSE);
385 b855148c 2004-05-16 devnull } /* End of moreprolog */
387 b855148c 2004-05-16 devnull /*****************************************************************************/
389 b855148c 2004-05-16 devnull readpages()
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 */
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.
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.
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;
414 b855148c 2004-05-16 devnull endpagelen = strlen(ENDPAGE);
415 b855148c 2004-05-16 devnull pagelen = strlen(PAGE);
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);
448 b855148c 2004-05-16 devnull } /* End if */
450 b855148c 2004-05-16 devnull } /* End of readpages */
452 b855148c 2004-05-16 devnull /*****************************************************************************/
454 b855148c 2004-05-16 devnull writepages()
458 b855148c 2004-05-16 devnull int i, j, k; /* loop indices */
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.
470 b855148c 2004-05-16 devnull fprintf(fp_out, "%s", endprolog);
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 */
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);
493 b855148c 2004-05-16 devnull } /* End of writepages */
495 b855148c 2004-05-16 devnull /*****************************************************************************/
497 b855148c 2004-05-16 devnull copypage(start, stop)
499 b855148c 2004-05-16 devnull long start; /* starting from this offset */
500 b855148c 2004-05-16 devnull long stop; /* and ending here */
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.
512 b855148c 2004-05-16 devnull fseek(fp_in, start, 0);
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);
519 b855148c 2004-05-16 devnull } /* End of copypage */
521 b855148c 2004-05-16 devnull /*****************************************************************************/
523 b855148c 2004-05-16 devnull trailer()
529 b855148c 2004-05-16 devnull * Makes sure everything from the TRAILER string to EOF is copied to the output
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 */
541 b855148c 2004-05-16 devnull } /* End of trailer */
543 b855148c 2004-05-16 devnull /*****************************************************************************/