picpack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. *
  3. * picpack - picture packing pre-processor
  4. *
  5. * A trivial troff pre-processor that copies files to stdout, expanding picture
  6. * requests into an in-line format that's passed transparently through troff and
  7. * handled by dpost. The program is an attempt to address requirements, expressed
  8. * by several organizations, of being able to store a document as a single file
  9. * (usually troff input) that can then be sent through dpost and ultimately to
  10. * a PostScript printer.
  11. *
  12. * The program looks for strings listed in the keys[] array at the start of each
  13. * line. When a picture request (as listed in keys[]) is found the second string
  14. * on the line is taken to be a picture file pathname that's added (in transparent
  15. * mode) to the output file. In addition each in-line picture file is preceeded by
  16. * device control command (again passed through in transparent mode) that looks
  17. * like,
  18. *
  19. * x X InlinePicture filename bytes
  20. *
  21. * where bytes is the size of the picture file (which begins on the next line)
  22. * and filename is the pathname of the picture file. dpost uses both arguments to
  23. * manage in-line pictures (in a big temp file). To handle pictures in diversions
  24. * picpack reads each input file twice. The first pass looks for picture inclusion
  25. * requests and copies each picture file transparently to the output file, while
  26. * second pass just copies the input file to the output file. Things could still
  27. * break, but the two pass method should handle most jobs.
  28. *
  29. * The recognized in-line picture requests are saved in keys[] and by default only
  30. * expand .BP and .PI macro calls. The -k option changes the recognized strings,
  31. * and may be needed if you've built your own picture inclusion macros on top of
  32. * .BP or .PI or decided to list each picture file at the start of your input file
  33. * using a dummy macro. For example you could require every in-line picture be
  34. * named by a dummy macro (say .iP), then the command line,
  35. *
  36. * picpack -k.iP file > file.pack
  37. *
  38. * hits on lines that begin with .iP (rather than .BP or .PI), and the only files
  39. * pulled in would be ones named as the second argument to the new .iP macro. The
  40. * -k option accepts a space or comma separated list of up to 10 different key
  41. * strings. picpack imposes no contraints on key strings, other than not allowing
  42. * spaces or commas. A key string can begin with \" and in that case it would be
  43. * troff comment.
  44. *
  45. * Although the program will help some users, there are obvious disadvantages.
  46. * Perhaps the most important is that troff output files (with in-line pictures
  47. * included) don't fit the device independent language accepted by important post
  48. * processors like proof, and that means you won't be able to reliably preview a
  49. * packed file on your 5620 or whatever. Another potential problem is that picture
  50. * files can be large. Packing everything together in a single file at an early
  51. * stage has a better chance of exceeding your system's ulimit.
  52. *
  53. */
  54. #include <stdio.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include <string.h>
  58. #include "gen.h" /* general purpose definitions */
  59. #include "ext.h" /* external variable definitions */
  60. #include "path.h" /* just for TEMPDIR definition */
  61. char *keys[11] = {".BP", ".PI", NULL};
  62. int quiet = FALSE;
  63. FILE *fp_in = stdin; /* input */
  64. FILE *fp_out = stdout; /* and output files */
  65. /*****************************************************************************/
  66. main(agc, agv)
  67. int agc;
  68. char *agv[];
  69. {
  70. /*
  71. *
  72. * A picture packing pre-processor that copies input files to stdout, expanding
  73. * picture requests (as listed in keys[]) to an in-line format that can be passed
  74. * through troff (using transparent mode) and handled later by dpost.
  75. *
  76. */
  77. argc = agc; /* global so everyone can use them */
  78. argv = agv;
  79. prog_name = argv[0]; /* just for error messages */
  80. options(); /* command line options */
  81. arguments(); /* translate all the input files */
  82. done(); /* clean things up */
  83. exit(x_stat); /* everything probably went OK */
  84. } /* End of main */
  85. /*****************************************************************************/
  86. options()
  87. {
  88. int ch; /* name returned by getopt() */
  89. extern char *optarg; /* option argument set by getopt() */
  90. extern int optind;
  91. /*
  92. *
  93. * Handles the command line options.
  94. *
  95. */
  96. while ( (ch = getopt(argc, argv, "k:qDI")) != EOF ) {
  97. switch ( ch ) {
  98. case 'k': /* new expansion key strings */
  99. newkeys(optarg);
  100. break;
  101. case 'q': /* disables "missing picture" messages */
  102. quiet = TRUE;
  103. break;
  104. case 'D': /* debug flag */
  105. debug = ON;
  106. break;
  107. case 'I': /* ignore FATAL errors */
  108. ignore = ON;
  109. break;
  110. case '?': /* don't know the option */
  111. error(FATAL, "");
  112. break;
  113. default:
  114. error(FATAL, "missing case for option %c", ch);
  115. break;
  116. } /* End switch */
  117. } /* End while */
  118. argc -= optind; /* get ready for non-options args */
  119. argv += optind;
  120. } /* End of options */
  121. /*****************************************************************************/
  122. newkeys(list)
  123. char *list; /* comma or space separated key strings */
  124. {
  125. char *p; /* next key string from *list */
  126. int i; /* goes in keys[i] */
  127. int n; /* last key string slot in keys[] */
  128. /*
  129. *
  130. * Separates *list into space or comma separated strings and adds each to the
  131. * keys[] array. The strings in keys[] are used to locate the picture inclusion
  132. * requests that are translated to the in-line format. The keys array must end
  133. * with a NULL pointer and by default only expands .BP and .PI macro calls.
  134. *
  135. */
  136. n = (sizeof(keys) / sizeof(char *)) - 1;
  137. for ( i = 0, p = strtok(list, " ,"); p != NULL; i++, p = strtok(NULL, " ,") )
  138. if ( i >= n )
  139. error(FATAL, "too many key strings");
  140. else keys[i] = p;
  141. keys[i] = NULL;
  142. } /* End of newkeys */
  143. /*****************************************************************************/
  144. arguments()
  145. {
  146. FILE *copystdin();
  147. /*
  148. *
  149. * Makes sure all the non-option command line arguments are processed. If we get
  150. * here and there aren't any arguments left, or if '-' is one of the input files
  151. * we process stdin, after copying it to a temporary file.
  152. *
  153. */
  154. if ( argc < 1 ) {
  155. fp_in = copystdin();
  156. picpack();
  157. } else
  158. while ( argc > 0 ) {
  159. if ( strcmp(*argv, "-") == 0 )
  160. fp_in = copystdin();
  161. else if ( (fp_in = fopen(*argv, "r")) == NULL )
  162. error(FATAL, "can't open %s", *argv);
  163. picpack();
  164. fclose(fp_in);
  165. argc--;
  166. argv++;
  167. } /* End while */
  168. } /* End of arguments */
  169. /*****************************************************************************/
  170. FILE *copystdin()
  171. {
  172. char *tfile; /* temporary file name */
  173. int fd_out; /* and its file descriptor */
  174. FILE *fp; /* return value - the new input file */
  175. /*
  176. *
  177. * Copies stdin to a temp file, unlinks the file, and returns the file pointer
  178. * for the new temporary file to the caller. Needed because we read each input
  179. * file twice in an attempt to handle pictures in diversions.
  180. *
  181. */
  182. if ( (tfile = tempnam(TEMPDIR, "post")) == NULL )
  183. error(FATAL, "can't generate temp file name");
  184. if ( (fd_out = creat(tfile, 0660)) == -1 )
  185. error(FATAL, "can't create %s", tfile);
  186. copyfile(fileno(stdin), fd_out);
  187. close(fd_out);
  188. if ( (fp = fopen(tfile, "r")) == NULL )
  189. error(FATAL, "can't open %s", tfile);
  190. unlink(tfile);
  191. return(fp);
  192. } /* End of copystdin */
  193. /*****************************************************************************/
  194. copyfile(fd_in, fd_out)
  195. int fd_in; /* input */
  196. int fd_out; /* and output files */
  197. {
  198. char buf[512]; /* internal buffer for reads and writes */
  199. int count; /* number of bytes put in buf[] */
  200. /*
  201. *
  202. * Copies file fd_in to fd_out. Handles the second pass for each input file and
  203. * also used to copy stdin to a temporary file.
  204. *
  205. */
  206. while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )
  207. if ( write(fd_out, buf, count) != count )
  208. error(FATAL, "write error");
  209. } /* End of copyfile */
  210. /*****************************************************************************/
  211. done()
  212. {
  213. /*
  214. *
  215. * Finished with all the input files - unlink the temporary file that was used
  216. * to record the in-line picture file pathnames.
  217. *
  218. */
  219. if ( temp_file != NULL )
  220. unlink(temp_file);
  221. } /* End of done */
  222. /*****************************************************************************/
  223. picpack()
  224. {
  225. char line[512]; /* next input line */
  226. char name[100]; /* picture file names - from BP or PI */
  227. int i; /* for looking through keys[] */
  228. /*
  229. *
  230. * Handles the two passes over the next input file. First pass compares the start
  231. * of each line in *fp_in with the key strings saved in the keys[] array. If a
  232. * match is found inline() is called to copy the picture file (ie. the file named
  233. * as the second string in line[]) to stdout, provided the file hasn't previously
  234. * been copied. The second pass goes back to the start of fp_in and copies it all
  235. * to the output file.
  236. *
  237. */
  238. while ( fgets(line, sizeof(line), fp_in) != NULL ) {
  239. for ( i = 0; keys[i] != NULL; i++ )
  240. if ( strncmp(line, keys[i], strlen(keys[i])) == 0 ) {
  241. if ( sscanf(line, "%*s %s", name) == 1 ) {
  242. strtok(name, "(");
  243. if ( gotpicfile(name) == FALSE )
  244. inline(name);
  245. } /* End if */
  246. } /* End if */
  247. } /* End while */
  248. fflush(fp_out); /* second pass - copy fp_in to fp_out */
  249. fseek(fp_in, 0L, 0);
  250. copyfile(fileno(fp_in), fileno(fp_out));
  251. } /* End of picpack */
  252. /*****************************************************************************/
  253. inline(name)
  254. char *name; /* name of the in-line picture file */
  255. {
  256. long size; /* size in bytes - from fstat */
  257. FILE *fp; /* for reading *name */
  258. int ch; /* next character from picture file */
  259. int lastch = '\n'; /* so we know when to put out \! */
  260. struct stat sbuf; /* for the picture file size */
  261. /*
  262. *
  263. * Copies the picture file *name to the output file in an in-line format that can
  264. * be passed through troff and recovered later by dpost. Transparent mode is used
  265. * so each line starts with \! and all \ characters must be escaped. The in-line
  266. * picture sequence begins with an "x X InlinePicture" device control command that
  267. * names the picture file and gives its size (in bytes).
  268. *
  269. */
  270. if ( (fp = fopen(name, "r")) != NULL ) {
  271. fstat(fileno(fp), &sbuf);
  272. if ( (size = sbuf.st_size) > 0 ) {
  273. fprintf(fp_out, "\\!x X InlinePicture %s %ld\n", name, size);
  274. while ( (ch = getc(fp)) != EOF ) {
  275. if ( lastch == '\n' )
  276. fprintf(fp_out, "\\!");
  277. if ( ch == '\\' )
  278. putc('\\', fp_out);
  279. putc(lastch = ch, fp_out);
  280. } /* End while */
  281. if ( lastch != '\n' )
  282. putc('\n', fp_out);
  283. } /* End if */
  284. fclose(fp);
  285. addpicfile(name);
  286. } else if ( quiet == FALSE )
  287. error(NON_FATAL, "can't read picture file %s", name);
  288. } /* End of inline */
  289. /*****************************************************************************/
  290. gotpicfile(name)
  291. char *name;
  292. {
  293. char buf[100];
  294. FILE *fp_pic;
  295. /*
  296. *
  297. * Checks the list of previously added picture files in *temp_file and returns
  298. * FALSE if it's a new file and TRUE otherwise. Probably should open the temp
  299. * file once for update and leave it open, rather than opening and closing it
  300. * every time.
  301. *
  302. */
  303. if ( temp_file != NULL )
  304. if ( (fp_pic = fopen(temp_file, "r")) != NULL ) {
  305. while ( fscanf(fp_pic, "%s", buf) != EOF )
  306. if ( strcmp(buf, name) == 0 ) {
  307. fclose(fp_pic);
  308. return(TRUE);
  309. } /* End if */
  310. fclose(fp_pic);
  311. } /* End if */
  312. return(FALSE);
  313. } /* End of gotpicfile */
  314. /*****************************************************************************/
  315. addpicfile(name)
  316. char *name;
  317. {
  318. FILE *fp_pic;
  319. /*
  320. *
  321. * Adds string *name to the list of in-line picture files that's maintained in
  322. * *temp_file. Should undoubtedly open the file once for update and use fseek()
  323. * to move around in the file!
  324. *
  325. */
  326. if ( temp_file == NULL )
  327. if ( (temp_file = tempnam(TEMPDIR, "picpac")) == NULL )
  328. return;
  329. if ( (fp_pic = fopen(temp_file, "a")) != NULL ) {
  330. fprintf(fp_pic, "%s\n", name);
  331. fclose(fp_pic);
  332. } /* End if */
  333. } /* End of addpicfile */
  334. /*****************************************************************************/