postreverse.c 16 KB

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