3 * picpack - picture packing pre-processor
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.
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
19 * x X InlinePicture filename bytes
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.
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,
36 * picpack -k.iP file > file.pack
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
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.
56 #include <sys/types.h>
60 #include "gen.h" /* general purpose definitions */
61 #include "ext.h" /* external variable definitions */
62 #include "path.h" /* just for TEMPDIR definition */
64 char *keys[11] = {".BP", ".PI", NULL};
67 FILE *fp_in = stdin; /* input */
68 FILE *fp_out = stdout; /* and output files */
70 /*****************************************************************************/
81 * A picture packing pre-processor that copies input files to stdout, expanding
82 * picture requests (as listed in keys[]) to an in-line format that can be passed
83 * through troff (using transparent mode) and handled later by dpost.
87 argc = agc; /* global so everyone can use them */
90 prog_name = argv[0]; /* just for error messages */
92 options(); /* command line options */
93 arguments(); /* translate all the input files */
94 done(); /* clean things up */
96 exit(x_stat); /* everything probably went OK */
100 /*****************************************************************************/
106 int ch; /* name returned by getopt() */
108 extern char *optarg; /* option argument set by getopt() */
113 * Handles the command line options.
117 while ( (ch = getopt(argc, argv, "k:qDI")) != EOF ) {
119 case 'k': /* new expansion key strings */
123 case 'q': /* disables "missing picture" messages */
127 case 'D': /* debug flag */
131 case 'I': /* ignore FATAL errors */
135 case '?': /* don't know the option */
140 error(FATAL, "missing case for option %c", ch);
145 argc -= optind; /* get ready for non-options args */
148 } /* End of options */
150 /*****************************************************************************/
154 char *list; /* comma or space separated key strings */
158 char *p; /* next key string from *list */
159 int i; /* goes in keys[i] */
160 int n; /* last key string slot in keys[] */
164 * Separates *list into space or comma separated strings and adds each to the
165 * keys[] array. The strings in keys[] are used to locate the picture inclusion
166 * requests that are translated to the in-line format. The keys array must end
167 * with a NULL pointer and by default only expands .BP and .PI macro calls.
171 n = (sizeof(keys) / sizeof(char *)) - 1;
173 for ( i = 0, p = strtok(list, " ,"); p != NULL; i++, p = strtok(NULL, " ,") )
175 error(FATAL, "too many key strings");
180 } /* End of newkeys */
182 /*****************************************************************************/
192 * Makes sure all the non-option command line arguments are processed. If we get
193 * here and there aren't any arguments left, or if '-' is one of the input files
194 * we process stdin, after copying it to a temporary file.
203 if ( strcmp(*argv, "-") == 0 )
205 else if ( (fp_in = fopen(*argv, "r")) == NULL )
206 error(FATAL, "can't open %s", *argv);
213 } /* End of arguments */
215 /*****************************************************************************/
221 char *tfile; /* temporary file name */
222 int fd_out; /* and its file descriptor */
223 FILE *fp; /* return value - the new input file */
227 * Copies stdin to a temp file, unlinks the file, and returns the file pointer
228 * for the new temporary file to the caller. Needed because we read each input
229 * file twice in an attempt to handle pictures in diversions.
233 if ( (tfile = tempnam(TEMPDIR, "post")) == NULL )
234 error(FATAL, "can't generate temp file name");
236 if ( (fd_out = creat(tfile, 0660)) == -1 )
237 error(FATAL, "can't create %s", tfile);
239 copyfile(fileno(stdin), fd_out);
242 if ( (fp = fopen(tfile, "r")) == NULL )
243 error(FATAL, "can't open %s", tfile);
248 } /* End of copystdin */
250 /*****************************************************************************/
252 copyfile(fd_in, fd_out)
254 int fd_in; /* input */
255 int fd_out; /* and output files */
259 char buf[512]; /* internal buffer for reads and writes */
260 int count; /* number of bytes put in buf[] */
264 * Copies file fd_in to fd_out. Handles the second pass for each input file and
265 * also used to copy stdin to a temporary file.
269 while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )
270 if ( write(fd_out, buf, count) != count )
271 error(FATAL, "write error");
273 } /* End of copyfile */
275 /*****************************************************************************/
283 * Finished with all the input files - unlink the temporary file that was used
284 * to record the in-line picture file pathnames.
288 if ( temp_file != NULL )
293 /*****************************************************************************/
299 char line[512]; /* next input line */
300 char name[100]; /* picture file names - from BP or PI */
301 int i; /* for looking through keys[] */
305 * Handles the two passes over the next input file. First pass compares the start
306 * of each line in *fp_in with the key strings saved in the keys[] array. If a
307 * match is found inline() is called to copy the picture file (ie. the file named
308 * as the second string in line[]) to stdout, provided the file hasn't previously
309 * been copied. The second pass goes back to the start of fp_in and copies it all
310 * to the output file.
314 while ( fgets(line, sizeof(line), fp_in) != NULL ) {
315 for ( i = 0; keys[i] != NULL; i++ )
316 if ( strncmp(line, keys[i], strlen(keys[i])) == 0 ) {
317 if ( sscanf(line, "%*s %s", name) == 1 ) {
319 if ( gotpicfile(name) == FALSE )
325 fflush(fp_out); /* second pass - copy fp_in to fp_out */
327 copyfile(fileno(fp_in), fileno(fp_out));
329 } /* End of picpack */
331 /*****************************************************************************/
335 char *name; /* name of the in-line picture file */
339 long size; /* size in bytes - from fstat */
340 FILE *fp; /* for reading *name */
341 int ch; /* next character from picture file */
342 int lastch = '\n'; /* so we know when to put out \! */
344 struct stat sbuf; /* for the picture file size */
348 * Copies the picture file *name to the output file in an in-line format that can
349 * be passed through troff and recovered later by dpost. Transparent mode is used
350 * so each line starts with \! and all \ characters must be escaped. The in-line
351 * picture sequence begins with an "x X InlinePicture" device control command that
352 * names the picture file and gives its size (in bytes).
356 if ( (fp = fopen(name, "r")) != NULL ) {
357 fstat(fileno(fp), &sbuf);
358 if ( (size = sbuf.st_size) > 0 ) {
359 fprintf(fp_out, "\\!x X InlinePicture %s %ld\n", name, size);
360 while ( (ch = getc(fp)) != EOF ) {
361 if ( lastch == '\n' )
362 fprintf(fp_out, "\\!");
365 putc(lastch = ch, fp_out);
367 if ( lastch != '\n' )
372 } else if ( quiet == FALSE )
373 error(NON_FATAL, "can't read picture file %s", name);
375 } /* End of inline */
377 /*****************************************************************************/
390 * Checks the list of previously added picture files in *temp_file and returns
391 * FALSE if it's a new file and TRUE otherwise. Probably should open the temp
392 * file once for update and leave it open, rather than opening and closing it
397 if ( temp_file != NULL )
398 if ( (fp_pic = fopen(temp_file, "r")) != NULL ) {
399 while ( fscanf(fp_pic, "%s", buf) != EOF )
400 if ( strcmp(buf, name) == 0 ) {
409 } /* End of gotpicfile */
411 /*****************************************************************************/
423 * Adds string *name to the list of in-line picture files that's maintained in
424 * *temp_file. Should undoubtedly open the file once for update and use fseek()
425 * to move around in the file!
429 if ( temp_file == NULL )
430 if ( (temp_file = tempnam(TEMPDIR, "picpac")) == NULL )
433 if ( (fp_pic = fopen(temp_file, "a")) != NULL ) {
434 fprintf(fp_pic, "%s\n", name);
438 } /* End of addpicfile */
440 /*****************************************************************************/