Debian Codegraph @

texk/seetexk/dviselect.c

raw
1 /* Copyright (c) 1987, 1989, 2012 University of Maryland Department of
2 Computer Science.
3
4 Permission is hereby granted, free of charge, to any person obtaining
5 a copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation, the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions: The above copyright notice, this permission
11 notice and the disclaimer statement shall be included in all copies
12 or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 /*
24 * DVI page selection program
25 *
26 * Reads DVI version 2 files and selects pages, writing a new DVI
27 * file. The new DVI file is technically correct, though we do not
28 * adjust the tallest and widest page values, nor the DVI stack size.
29 * This is all right since the values will never become too small,
30 * but it might be nice to fix them up. Perhaps someday . . . .
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #ifdef KPATHSEA
38 #include <kpathsea/config.h>
39 #include <kpathsea/c-fopen.h>
40 #include <kpathsea/getopt.h>
41 #if defined(WIN32)
42 #include <kpathsea/variable.h>
43 #endif
44 #else
45 #define FOPEN_RBIN_MODE "rb"
46 #define FOPEN_WBIN_MODE "wb"
47 #define SET_BINARY(x) (void)0
48 extern char *optarg;
49 extern int optind;
50 #endif
51
52 #include "types.h"
53 #include "dviclass.h"
54 #include "dvicodes.h"
55 #include "error.h"
56 #include "fio.h"
57 #include "gripes.h"
58 #include "search.h"
59 #include <stdio.h>
60 #include <ctype.h>
61 #include "common.h"
62
63 #define white(x) ((x) == ' ' || (x) == '\t' || (x) == ',')
64
65 char *ProgName;
66 /* Globals */
67 char serrbuf[BUFSIZ]; /* buffer for stderr */
68
69 /*
70 * We will try to keep output lines shorter than MAXCOL characters.
71 */
72 #define MAXCOL 75
73
74 /*
75 * We use the following structure to keep track of fonts we have seen.
76 * The final DVI file lists only the fonts it uses.
77 */
78 struct fontinfo {
79 i32 fi_newindex; /* font number in output file */
80 int fi_reallyused; /* true => used on a page we copied */
81 i32 fi_checksum; /* the checksum */
82 i32 fi_mag; /* the magnification */
83 i32 fi_designsize; /* the design size */
84 short fi_n1; /* the name header length */
85 short fi_n2; /* the name body length */
86 char *fi_name; /* the name itself */
87 };
88
89 /*
90 * We need to remember which pages the user would like. We build a linked
91 * list that allows us to decide (for any given page) whether it should
92 * be included in the output file. Each page has ten \count variables
93 * associated with it. We put a bound on the values allowed for each, and
94 * keep a linked list of alternatives should any be outside the allowed
95 * range. For example, `dviselect *.3,10-15' would generate a two-element
96 * page list, with the first allowing any value for \count0 (and \counts 2 to
97 * 9) but \count1 restricted to the range 3-3, and the second restricting
98 * \count0 to the range 10-15 but leaving \counts 1 to 9 unrestrained.
99 *
100 * We remember whether a bound is specified, so that we need not fix
101 * some `large' number as a maximum value.
102 *
103 * We also allow `absolute' page references, where the first page is
104 * page 1, the second 2, and so forth. These are specified with an
105 * equal sign: `dviselect =4:10' picks up the fourth through tenth
106 * sequential pages, irrespective of \count values.
107 *
108 * There is also an even/odd switch (which can be applied to absolute
109 * or relative pages): dviselect =even, even.3, etc.
110 */
111 enum pstype {
112 pt_lu, /* have both lower and upper bounds */
113 pt_l, /* lower bound only */
114 pt_u, /* upper bound only */
115 pt_unbounded, /* any value at all */
116 pt_even, /* even values only */
117 pt_odd /* odd values only */
118 };
119 struct pagesel {
120 enum pstype ps_type; /* type of selector */
121 i32 ps_low; /* lower bound */
122 i32 ps_high; /* upper bound */
123 };
124 struct pagelist {
125 struct pagelist *pl_alt; /* next in a series of alternates */
126 int pl_len; /* number of ranges to check */
127 int pl_abs; /* true iff absolute page ref */
128 struct pagesel pl_pages[10]; /* one for each \count variable */
129 };
130
131 int SFlag; /* true => -s, silent operation */
132
133 struct search *FontFinder; /* maps from input indicies to fontinfo */
134 i32 NextOutputFontIndex; /* generates output indicies */
135 i32 CurrentFontIndex; /* current (old) index in input */
136 i32 OutputFontIndex; /* current (new) index in ouput */
137
138 struct pagelist *PageList; /* the list of allowed pages */
139
140 const char *DVIFileName; /* name of input DVI file */
141 FILE *inf; /* the input file itself */
142 const char *outname; /* name of output DVI file */
143 FILE *outf; /* the output DVI file */
144
145 long StartOfLastPage; /* The file position just before we started
146 the last page (this is later written to
147 the output file as the previous page
148 pointer). */
149 long CurrentPosition; /* The current position of the file */
150
151 int UseThisPage; /* true => current page is selected */
152 #ifdef ASCIIPTEX
153 int ptexdvi; /* true => dvi format is extended (TATEKUMI) */
154 #endif /* ASCIIPTEX */
155
156 i32 InputPageNumber; /* current absolute page in old DVI file */
157 int NumberOfOutputPages; /* number of pages in new DVI file */
158
159 i32 Numerator; /* numerator from DVI file */
160 i32 Denominator; /* denominator from DVI file */
161 i32 DVIMag; /* magnification from DVI file */
162
163 i32 Count[10]; /* the 10 \count variables */
164
165 /* save some string space: we use this a lot */
166 char writeerr[] = "error writing DVI file";
167
168 #include <string.h>
169 static void WriteFont(struct fontinfo *);
170 static int evenodd(char *);
171 static int ParsePages(char *);
172 static void HandleDVIFile(void);
173 static void PutFontSelector(i32);
174 static void WritePreAmble(void);
175
176 #ifndef KPATHSEA
177 char *malloc(), *realloc();
178 #endif /* not KPATHSEA */
179
180 /*
181 * You may get lint warnings about sprintf's return value.
182 * Older versions of 4BSD have `char *sprintf()'. ANSI and
183 * SysV use `int sprintf()'; so ignore the warnings.
184 */
185
186 /*
187 * Lint gets somewhat confused over putc.
188 */
189 #ifdef lint
190 #undef putc
191 #ifdef ultrix /* grr */
192 #define putc(c, f) fputc((char)(c), f)
193 #else
194 #define putc(c, f) fputc((int)(c), f)
195 #endif
196 #endif
197
198 /*
199 * Return true iff the 10 \counts are one of the desired output pages.
200 */
201 static int
202 DesiredPageP(void)
203 {
204 register struct pagelist *pl;
205
206 for (pl = PageList; pl != NULL; pl = pl->pl_alt) {
207 register struct pagesel *ps = pl->pl_pages;
208 register int i;
209 register i32 *pagep;
210
211 pagep = pl->pl_abs ? &InputPageNumber : &Count[0];
212 for (i = 0; i < pl->pl_len; i++, ps++, pagep++) {
213 switch (ps->ps_type) {
214
215 case pt_lu: /* check upper, fall through */
216 if (*pagep > ps->ps_high)
217 goto no_good;
218 /* FALLTHROUGH */
219
220 case pt_l: /* only if not too low */
221 if (*pagep < ps->ps_low)
222 goto no_good;
223 break;
224
225 case pt_u: /* only if not too high */
226 if (*pagep > ps->ps_high)
227 goto no_good;
228 break;
229
230 case pt_unbounded:
231 break;
232
233 case pt_even: /* only if even */
234 if (*pagep & 1)
235 goto no_good;
236 break;
237
238 case pt_odd: /* only if odd */
239 if ((*pagep & 1) == 0)
240 goto no_good;
241 break;
242 }
243 }
244 return (1); /* success */
245 no_good:
246 continue;
247 }
248 return (0);
249 }
250
251 /*
252 * Print a message to stderr, with an optional leading space, and handling
253 * long line wraps.
254 */
255 static void
256 message(int space, const char *str, int len)
257 {
258 static int beenhere;
259 static int col;
260
261 if (!beenhere)
262 space = 0, beenhere++;
263 if (len == 0)
264 len = strlen(str);
265 col += len;
266 if (space) {
267 if (col >= MAXCOL)
268 (void) putc('\n', stderr), col = len;
269 else
270 (void) putc(' ', stderr), col++;
271 }
272 while (--len >= 0)
273 (void) putc(*str++, stderr);
274 (void) fflush(stderr);
275 }
276
277 /*
278 * Start a page (process a DVI_BOP).
279 */
280 static void
281 BeginPage(void)
282 {
283 register i32 *i;
284
285 OutputFontIndex = -1; /* new page requires respecifying font */
286 InputPageNumber++; /* count it */
287 for (i = Count; i < &Count[10]; i++)
288 fGetLong(inf, *i);
289 (void) GetLong(inf); /* previous page pointer */
290
291 if ((UseThisPage = DesiredPageP()) == 0)
292 return;
293
294 if (NumberOfOutputPages == 0)
295 WritePreAmble();
296
297 putbyte(outf, DVI_BOP);
298 for (i = Count; i < &Count[10]; i++)
299 PutLong(outf, *i);
300 PutLong(outf, StartOfLastPage);
301 if (ferror(outf))
302 error(1, -1, writeerr);
303
304 StartOfLastPage = CurrentPosition;
305 CurrentPosition += 45; /* we just wrote this much */
306
307 if (!SFlag) { /* write nice page usage messages */
308 register int z = 0;
309 register int mlen = 0;
310 char msg[80];
311
312 (void) sprintf(msg, "[%ld", (long)Count[0]);
313 mlen = strlen(msg);
314 for (i = &Count[1]; i < &Count[10]; i++) {
315 if (*i == 0) {
316 z++;
317 continue;
318 }
319 while (--z >= 0)
320 msg[mlen++] = '.', msg[mlen++] = '0';
321 z = 0;
322 (void) sprintf(msg + mlen, ".%ld", (long)*i);
323 mlen += strlen(msg + mlen);
324 }
325 message(1, msg, mlen);
326 }
327 }
328
329 /*
330 * End a page (process a DVI_EOP).
331 */
332 static void
333 EndPage(void)
334 {
335
336 if (!UseThisPage)
337 return;
338 if (!SFlag)
339 message(0, "]", 1);
340 putbyte(outf, DVI_EOP);
341 if (ferror(outf))
342 error(1, -1, writeerr);
343 CurrentPosition++;
344 NumberOfOutputPages++;
345 }
346
347 /*
348 * For each of the fonts used in the new DVI file, write out a definition.
349 */
350 /* ARGSUSED */
351 static void
352 PostAmbleFontEnumerator(char *addr, i32 key)
353 {
354
355 if (((struct fontinfo *)addr)->fi_reallyused)
356 WriteFont((struct fontinfo *)addr);
357 }
358
359 static void
360 HandlePostAmble(void)
361 {
362 register i32 c;
363
364 if (NumberOfOutputPages == 0)
365 return;
366
367 (void) GetLong(inf); /* previous page pointer */
368 if (GetLong(inf) != Numerator)
369 GripeMismatchedValue("numerator");
370 if (GetLong(inf) != Denominator)
371 GripeMismatchedValue("denominator");
372 if (GetLong(inf) != DVIMag)
373 GripeMismatchedValue("\\magnification");
374
375 putbyte(outf, DVI_POST);
376 PutLong(outf, StartOfLastPage);
377 PutLong(outf, Numerator);
378 PutLong(outf, Denominator);
379 PutLong(outf, DVIMag);
380 c = GetLong(inf);
381 PutLong(outf, c); /* tallest page height */
382 c = GetLong(inf);
383 PutLong(outf, c); /* widest page width */
384 c = GetWord(inf);
385 PutWord(outf, c); /* DVI stack size */
386 PutWord(outf, NumberOfOutputPages);
387 StartOfLastPage = CurrentPosition; /* point at post */
388 CurrentPosition += 29; /* count all those `put's */
389 #ifdef notdef
390 (void) GetWord(inf); /* skip original number of pages */
391 #endif
392
393 /*
394 * just ignore all the incoming font definitions; we are done with
395 * input file
396 */
397
398 /*
399 * run through the FontFinder table and dump definitions for the
400 * fonts we have used.
401 */
402 SEnumerate(FontFinder, PostAmbleFontEnumerator);
403
404 putbyte(outf, DVI_POSTPOST);
405 PutLong(outf, StartOfLastPage); /* actually start of postamble */
406 #ifdef ASCIIPTEX
407 if (ptexdvi)
408 putbyte(outf, DVI_PTEXVERSION);
409 else
410 #endif /* ASCIIPTEX */
411 putbyte(outf, DVI_VERSION);
412 putbyte(outf, DVI_FILLER);
413 putbyte(outf, DVI_FILLER);
414 putbyte(outf, DVI_FILLER);
415 putbyte(outf, DVI_FILLER);
416 CurrentPosition += 10;
417 while (CurrentPosition & 3) {
418 putbyte(outf, DVI_FILLER);
419 CurrentPosition++;
420 }
421 if (ferror(outf))
422 error(1, -1, writeerr);
423 }
424
425 /*
426 * Write a font definition to the output file
427 */
428 static void
429 WriteFont(struct fontinfo *fi)
430 {
431 register int l;
432 register char *s;
433
434 if (fi->fi_newindex < 256) {
435 putbyte(outf, DVI_FNTDEF1);
436 putbyte(outf, fi->fi_newindex);
437 CurrentPosition += 2;
438 } else if (fi->fi_newindex < 65536) {
439 putbyte(outf, DVI_FNTDEF2);
440 PutWord(outf, fi->fi_newindex);
441 CurrentPosition += 3;
442 } else if (fi->fi_newindex < 16777216) {
443 putbyte(outf, DVI_FNTDEF3);
444 Put3Byte(outf, fi->fi_newindex);
445 CurrentPosition += 4;
446 } else {
447 putbyte(outf, DVI_FNTDEF4);
448 PutLong(outf, fi->fi_newindex);
449 CurrentPosition += 5;
450 }
451 PutLong(outf, fi->fi_checksum);
452 PutLong(outf, fi->fi_mag);
453 PutLong(outf, fi->fi_designsize);
454 putbyte(outf, fi->fi_n1);
455 putbyte(outf, fi->fi_n2);
456 l = fi->fi_n1 + fi->fi_n2;
457 CurrentPosition += 14 + l;
458 s = fi->fi_name;
459 while (--l >= 0)
460 putbyte(outf, *s++);
461 }
462
463 /*
464 * Array to hold the preamble comment.
465 */
466 static char PreComment[256];
467
468 /*
469 * Read the preamble. Someday we should update the comment field.
470 */
471 static void
472 ReadPreAmble(void)
473 {
474 register int n, c;
475 char *comment = PreComment;
476
477 c = getc(inf);
478 if (c == EOF)
479 GripeUnexpectedDVIEOF();
480 if (c != DVI_PRE)
481 GripeMissingOp("PRE");
482 if (getc(inf) != DVI_VERSION)
483 error(1, 0, "%s is not a DVI version %d file",
484 DVIFileName, DVI_VERSION);
485 Numerator = GetLong(inf);
486 Denominator = GetLong(inf);
487 DVIMag = GetLong(inf);
488 c = GetByte(inf);
489 n = UnSign8(c);
490 *comment++ = (char)c;
491 while (--n >= 0) {
492 c = GetByte(inf);
493 *comment++ = (char)c;
494 }
495 }
496
497 /*
498 * Open the output and write the preamble.
499 */
500 static void
501 WritePreAmble(void)
502 {
503 register int n, c;
504 char *comment = PreComment;
505
506 if (outname == NULL) {
507 outf = stdout;
508 if (!isatty(fileno(outf)))
509 SET_BINARY(fileno(outf));
510 } else if ((outf = fopen(outname, FOPEN_WBIN_MODE)) == 0)
511 error(1, -1, "cannot write %s", outname);
512
513 putbyte(outf, DVI_PRE);
514 putbyte(outf, DVI_VERSION);
515 PutLong(outf, Numerator);
516 PutLong(outf, Denominator);
517 PutLong(outf, DVIMag);
518
519 c = *comment++;
520 n = UnSign8(c);
521 CurrentPosition = 15 + n; /* well, almost */
522 putbyte(outf, n);
523 while (--n >= 0) {
524 c = *comment++;
525 putbyte(outf, c);
526 }
527 }
528
529 int
530 main(int argc, char **argv)
531 {
532 register int c;
533 register char *s;
534 #if defined(WIN32) && defined(KPATHSEA)
535 int ac;
536 char **av, *enc;
537
538 if (argc>1) {
539 kpse_set_program_name(argv[0], "dviselect");
540 enc = kpse_var_value("command_line_encoding");
541 if (get_command_line_args_utf8(enc, &ac, &av)) {
542 argc = ac;
543 argv = av;
544 }
545 }
546 #endif
547
548 ProgName = *argv;
549 setbuf(stderr, serrbuf);
550
551 while ((c = getopt(argc, argv, "i:o:s")) != EOF) {
552 switch (c) {
553
554 case 's': /* silent */
555 SFlag++;
556 break;
557
558 case 'i':
559 if (DVIFileName != NULL)
560 goto usage;
561 DVIFileName = optarg;
562 break;
563
564 case 'o':
565 if (outname != NULL)
566 goto usage;
567 outname = optarg;
568 break;
569
570 case '?':
571 usage:
572 (void) fprintf(stderr,
573 "dviselect in SeeTeX Ver.%s (%s)\n", VERSION, TL_VERSION);
574 (void) fprintf(stderr, "\
575 Usage: %s [-s] [-i infile] [-o outfile] pages [...] [infile [outfile]]\n",
576 ProgName);
577 (void) fprintf(stderr,
578 "\nEmail bug reports to %s.\n", BUG_ADDRESS);
579 (void) fflush(stderr);
580 exit(1);
581 }
582 }
583
584 while (optind < argc) {
585 s = argv[optind++];
586 c = *s;
587 if (!(isalpha(c) ||
588 c == '/' ||
589 (c == '.' && (s[1] == '/' || (s[1] == '.' && s[2] == '/')))) ||
590 ((c == 'e' || c == 'o') && evenodd(s))) {
591 if (ParsePages(s))
592 goto usage;
593 } else if (DVIFileName == NULL)
594 DVIFileName = s;
595 else if (outname == NULL)
596 outname = s;
597 else
598 goto usage;
599 }
600 if (PageList == NULL)
601 goto usage;
602 if (DVIFileName == NULL) {
603 DVIFileName = "`stdin'";
604 inf = stdin;
605 if (!isatty(fileno(inf)))
606 SET_BINARY(fileno(inf));
607 else
608 goto usage;
609 } else if ((inf = fopen(DVIFileName, FOPEN_RBIN_MODE)) == 0)
610 error(1, -1, "cannot read %s", DVIFileName);
611
612 if ((FontFinder = SCreate(sizeof(struct fontinfo))) == 0)
613 error(1, 0, "cannot create font finder (out of memory?)");
614
615 StartOfLastPage = -1;
616 ReadPreAmble();
617 #ifdef ASCIIPTEX
618 ptexdvi = 0;
619 #endif /* ASCIIPTEX */
620 HandleDVIFile();
621 HandlePostAmble();
622 if (NumberOfOutputPages > 0) {
623 if (!SFlag)
624 (void) fprintf(stderr, "\nWrote %d page%s, %ld bytes\n",
625 NumberOfOutputPages, NumberOfOutputPages == 1 ? "" : "s",
626 (long)CurrentPosition);
627 return 0;
628 } else {
629 if (!SFlag)
630 (void) fprintf(stderr, "Specified page may be out of range\n");
631 return 1;
632 }
633 }
634
635 static struct pagelist *
636 InstallPL(struct pagesel *ps, int n, int absolute)
637 {
638 register struct pagelist *pl;
639
640 pl = (struct pagelist *)malloc(sizeof *pl);
641 if (pl == NULL)
642 GripeOutOfMemory(sizeof *pl, "page list");
643 pl->pl_alt = PageList;
644 PageList = pl;
645 pl->pl_len = n;
646 while (--n >= 0)
647 pl->pl_pages[n] = ps[n];
648 pl->pl_abs = absolute;
649 return pl;
650 }
651
652 /*
653 * Return true iff the string `s' is the word `even' or `odd',
654 * followed by `.' or `white' characters.
655 */
656 static int
657 evenodd(char *s)
658 {
659 register int c;
660
661 if (strncmp(s, "even", 4) == 0)
662 c = s[4];
663 else if (strncmp(s, "odd", 3) == 0)
664 c = s[3];
665 else
666 return (0);
667 if (c == 0 || c == '.' || white(c))
668 return (1);
669 return (0);
670 }
671
672 /*
673 * Parse a string representing a list of pages. Return 0 iff ok. As a
674 * side effect, the page selection(s) is (are) prepended to PageList.
675 */
676 static int
677 ParsePages(char *s)
678 {
679 register struct pagesel *ps;
680 register int c; /* current character */
681 register i32 n; /* current numeric value */
682 register int innumber; /* true => gathering a number */
683 int i; /* next index in page select list */
684 int range; /* true => saw a range indicator */
685 int negative; /* true => number being built is negative */
686 int absolute; /* true => absolute, not \count */
687 int lb, ub, even, odd; /* flags for lower,upper,even,odd */
688 struct pagesel pagesel[10];
689
690 range = 0;
691 n = 0;
692 innumber = 0;
693 negative = 0;
694 absolute = 0;
695 i = 0;
696 ps = pagesel;
697 lb = ub = even = odd = 0;
698 /*
699 * Talk about ad hoc! (Not to mention convoluted.)
700 */
701 for (;;) {
702 c = *s++;
703 if (i == 0 && !innumber && !range) {
704 /* nothing special going on */
705 if (c == 0)
706 return 0;
707 if (white(c))
708 continue;
709 }
710 if ((c == 'e' || c == 'o') && evenodd(s - 1)) {
711 if (innumber || range)
712 return (-1);
713 if (c == 'e') {
714 s += 3;
715 even = 1;
716 } else {
717 s += 2;
718 odd = 1;
719 }
720 c = *s++;
721 goto finishnum;
722 }
723 if (c == '_') {
724 /* kludge: should be '-' for negatives */
725 if (innumber || absolute)
726 return (-1);
727 innumber = 1;
728 negative = 1;
729 n = 0;
730 continue;
731 }
732 if (c == '=') {
733 /* absolute page */
734 if (innumber || range || i > 0)
735 return (-1);
736 absolute = 1;
737 negative = 0;
738 n = 0;
739 continue;
740 }
741 if (isdigit(c)) {
742 /* accumulate numeric value */
743 if (!innumber) {
744 innumber = 1;
745 negative = 0;
746 n = c - '0';
747 continue;
748 }
749 n *= 10;
750 n += negative ? '0' - c : c - '0';
751 continue;
752 }
753 if (c == '-' || c == ':') {
754 /* here is a range */
755 if (range)
756 return (-1);
757 if (innumber) { /* have a lower bound */
758 ps->ps_low = n;
759 lb = 1;
760 }
761 range = 1;
762 innumber = 0;
763 continue;
764 }
765 if (c == '*') {
766 /* no lower bound, no upper bound */
767 c = *s++;
768 if (innumber || range || (c && c != '.' && !white(c)))
769 return (-1);
770 goto finishnum;
771 }
772 if (c == 0 || c == '.' || white(c)) {
773 /* end of this range */
774 if (innumber) { /* have an upper bound */
775 ps->ps_high = n;
776 ub = 1;
777 if (!range) {
778 /* no range => lower bound == upper */
779 ps->ps_low = ps->ps_high;
780 lb = 1;
781 }
782 }
783 finishnum:
784 if (even)
785 ps->ps_type = pt_even;
786 else if (odd)
787 ps->ps_type = pt_odd;
788 else if (lb)
789 ps->ps_type = ub ? pt_lu : pt_l;
790 else
791 ps->ps_type = ub ? pt_u : pt_unbounded;
792 if (c == '.') {
793 if (absolute)
794 return (-1);
795 if (++i >= 10) /* too many specifiers */
796 return (-1);
797 ps++;
798 } else {
799 InstallPL(pagesel, i + 1, absolute);
800 ps = pagesel;
801 i = 0;
802 absolute = 0;
803 }
804 if (c == 0)
805 return (0);
806 range = 0;
807 innumber = 0;
808 lb = ub = even = odd = 0;
809 continue;
810 }
811 /* illegal character */
812 return (-1);
813 }
814 }
815
816 /*
817 * Handle a font definition.
818 */
819 static void
820 HandleFontDef(i32 index)
821 {
822 register struct fontinfo *fi;
823 register int i;
824 register char *s;
825 int def = S_CREATE | S_EXCL;
826
827 if ((fi = (struct fontinfo *)SSearch(FontFinder, index, &def)) == 0) {
828 if (def & S_COLL)
829 error(1, 0, "font %ld already defined", (long)index);
830 else
831 error(1, 0, "cannot stash font %ld (out of memory?)",
832 (long)index);
833 }
834 fi->fi_reallyused = 0;
835 fi->fi_checksum = GetLong(inf);
836 fi->fi_mag = GetLong(inf);
837 fi->fi_designsize = GetLong(inf);
838 fi->fi_n1 = UnSign8(GetByte(inf));
839 fi->fi_n2 = UnSign8(GetByte(inf));
840 i = fi->fi_n1 + fi->fi_n2;
841 if ((s = malloc((unsigned)i)) == 0)
842 GripeOutOfMemory(i, "font name");
843 fi->fi_name = s;
844 while (--i >= 0)
845 *s++ = GetByte(inf);
846 }
847
848 /*
849 * Handle a \special.
850 */
851 static void
852 HandleSpecial(int c, int l, i32 p)
853 {
854 register int i;
855
856 if (UseThisPage) {
857 putbyte(outf, c);
858 switch (l) {
859
860 case DPL_UNS1:
861 putbyte(outf, p);
862 CurrentPosition += 2;
863 break;
864
865 case DPL_UNS2:
866 PutWord(outf, p);
867 CurrentPosition += 3;
868 break;
869
870 case DPL_UNS3:
871 Put3Byte(outf, p);
872 CurrentPosition += 4;
873 break;
874
875 case DPL_SGN4:
876 PutLong(outf, p);
877 CurrentPosition += 5;
878 break;
879
880 default:
881 panic("HandleSpecial l=%d", l);
882 /* NOTREACHED */
883 }
884 CurrentPosition += p;
885 while (--p >= 0) {
886 i = getc(inf);
887 putbyte(outf, i);
888 }
889 if (feof(inf))
890 GripeUnexpectedDVIEOF();
891 if (ferror(outf))
892 error(1, -1, writeerr);
893 } else
894 while (--p >= 0)
895 (void) getc(inf);
896 }
897
898 static void
899 ReallyUseFont(void)
900 {
901 register struct fontinfo *fi;
902 int look = S_LOOKUP;
903
904 fi = (struct fontinfo *)SSearch(FontFinder, CurrentFontIndex, &look);
905 if (fi == NULL)
906 error(1, 0, "DVI file requested font %ld without defining it",
907 (long)CurrentFontIndex);
908 if (fi->fi_reallyused == 0) {
909 fi->fi_reallyused++;
910 fi->fi_newindex = NextOutputFontIndex++;
911 WriteFont(fi);
912 }
913 if (fi->fi_newindex != OutputFontIndex) {
914 PutFontSelector(fi->fi_newindex);
915 OutputFontIndex = fi->fi_newindex;
916 }
917 }
918
919 /*
920 * Write a font selection command to the output file
921 */
922 static void
923 PutFontSelector(i32 index)
924 {
925
926 if (index < 64) {
927 putbyte(outf, index + DVI_FNTNUM0);
928 CurrentPosition++;
929 } else if (index < 256) {
930 putbyte(outf, DVI_FNT1);
931 putbyte(outf, index);
932 CurrentPosition += 2;
933 } else if (index < 65536) {
934 putbyte(outf, DVI_FNT2);
935 PutWord(outf, index);
936 CurrentPosition += 3;
937 } else if (index < 16777216) {
938 putbyte(outf, DVI_FNT3);
939 Put3Byte(outf, index);
940 CurrentPosition += 4;
941 } else {
942 putbyte(outf, DVI_FNT4);
943 PutLong(outf, index);
944 CurrentPosition += 5;
945 }
946 }
947
948 /*
949 * The following table describes the length (in bytes) of each of the DVI
950 * commands that we can simply copy, starting with DVI_SET1 (128).
951 */
952 char oplen[128] = {
953 0, 0, 0, 0, /* DVI_SET1 .. DVI_SET4 */
954 9, /* DVI_SETRULE */
955 0, 0, 0, 0, /* DVI_PUT1 .. DVI_PUT4 */
956 9, /* DVI_PUTRULE */
957 1, /* DVI_NOP */
958 0, /* DVI_BOP */
959 0, /* DVI_EOP */
960 1, /* DVI_PUSH */
961 1, /* DVI_POP */
962 2, 3, 4, 5, /* DVI_RIGHT1 .. DVI_RIGHT4 */
963 1, /* DVI_W0 */
964 2, 3, 4, 5, /* DVI_W1 .. DVI_W4 */
965 1, /* DVI_X0 */
966 2, 3, 4, 5, /* DVI_X1 .. DVI_X4 */
967 2, 3, 4, 5, /* DVI_DOWN1 .. DVI_DOWN4 */
968 1, /* DVI_Y0 */
969 2, 3, 4, 5, /* DVI_Y1 .. DVI_Y4 */
970 1, /* DVI_Z0 */
971 2, 3, 4, 5, /* DVI_Z1 .. DVI_Z4 */
972 0, /* DVI_FNTNUM0 (171) */
973 0, 0, 0, 0, 0, 0, 0, 0, /* 172 .. 179 */
974 0, 0, 0, 0, 0, 0, 0, 0, /* 180 .. 187 */
975 0, 0, 0, 0, 0, 0, 0, 0, /* 188 .. 195 */
976 0, 0, 0, 0, 0, 0, 0, 0, /* 196 .. 203 */
977 0, 0, 0, 0, 0, 0, 0, 0, /* 204 .. 211 */
978 0, 0, 0, 0, 0, 0, 0, 0, /* 212 .. 219 */
979 0, 0, 0, 0, 0, 0, 0, 0, /* 220 .. 227 */
980 0, 0, 0, 0, 0, 0, 0, /* 228 .. 234 */
981 0, 0, 0, 0, /* DVI_FNT1 .. DVI_FNT4 */
982 0, 0, 0, 0, /* DVI_XXX1 .. DVI_XXX4 */
983 0, 0, 0, 0, /* DVI_FNTDEF1 .. DVI_FNTDEF4 */
984 0, /* DVI_PRE */
985 0, /* DVI_POST */
986 0, /* DVI_POSTPOST */
987 #ifdef ASCIIPTEX
988 0, 0, 0, 0, 0, /* 250 .. 254 */
989 0, /* DVI_DIR */
990 #else /* !ASCIIPTEX */
991 0, 0, 0, 0, 0, 0, /* 250 .. 255 */
992 #endif /* !ASCIIPTEX */
993 };
994
995 /*
996 * Here we read the input DVI file and write relevant pages to the
997 * output DVI file. We also keep track of font changes, handle font
998 * definitions, and perform some other housekeeping.
999 */
1000 static void
1001 HandleDVIFile(void)
1002 {
1003 register int c, l;
1004 register i32 p = 0; /* avoid uninitialized warning */
1005 register int CurrentFontOK = 0;
1006 int doingpage = 0;
1007
1008 /* Only way out is via "return" statement */
1009 for (;;) {
1010 c = getc(inf); /* getc() returns unsigned values */
1011 if (DVI_IsChar(c)) {
1012 /*
1013 * Copy chars, note font usage, but ignore if
1014 * page is not interesting.
1015 */
1016 if (!UseThisPage)
1017 continue;
1018 if (!CurrentFontOK) {
1019 ReallyUseFont();
1020 CurrentFontOK++;
1021 }
1022 putbyte(outf, c);
1023 CurrentPosition++;
1024 continue;
1025 }
1026 if (DVI_IsFont(c)) { /* note font change */
1027 CurrentFontIndex = c - DVI_FNTNUM0;
1028 CurrentFontOK = 0;
1029 continue;
1030 }
1031 if (c == EOF)
1032 GripeUnexpectedDVIEOF();
1033 if ((l = (oplen - 128)[c]) != 0) { /* simple copy */
1034 if (!UseThisPage) {
1035 while (--l > 0)
1036 (void) getc(inf);
1037 continue;
1038 }
1039 CurrentPosition += l;
1040 putbyte(outf, c);
1041 while (--l > 0) {
1042 c = getc(inf);
1043 putbyte(outf, c);
1044 }
1045 if (ferror(outf))
1046 error(1, -1, writeerr);
1047 continue;
1048 }
1049 if ((l = DVI_OpLen(c)) != 0) {
1050 /*
1051 * Handle other generics.
1052 * N.B.: there should only be unsigned parameters
1053 * here (save SGN4), for commands with negative
1054 * parameters have been taken care of above.
1055 */
1056 switch (l) {
1057
1058 case DPL_UNS1:
1059 p = getc(inf);
1060 break;
1061
1062 case DPL_UNS2:
1063 fGetWord(inf, p);
1064 break;
1065
1066 case DPL_UNS3:
1067 fGet3Byte(inf, p);
1068 break;
1069
1070 case DPL_SGN4:
1071 fGetLong(inf, p);
1072 break;
1073
1074 default:
1075 panic("HandleDVIFile l=%d", l);
1076 }
1077
1078 /*
1079 * Now that we have the parameter, perform the
1080 * command.
1081 */
1082 switch (DVI_DT(c)) {
1083
1084 case DT_SET:
1085 case DT_PUT:
1086 if (!UseThisPage)
1087 continue;
1088 if (!CurrentFontOK) {
1089 ReallyUseFont();
1090 CurrentFontOK++;
1091 }
1092 putbyte(outf, c);
1093 switch (l) {
1094
1095 case DPL_UNS1:
1096 putbyte(outf, p);
1097 CurrentPosition += 2;
1098 continue;
1099
1100 case DPL_UNS2:
1101 PutWord(outf, p);
1102 CurrentPosition += 3;
1103 continue;
1104
1105 case DPL_UNS3:
1106 Put3Byte(outf, p);
1107 CurrentPosition += 4;
1108 continue;
1109
1110 case DPL_SGN4:
1111 PutLong(outf, p);
1112 CurrentPosition += 5;
1113 continue;
1114 }
1115
1116 case DT_FNT:
1117 CurrentFontIndex = p;
1118 CurrentFontOK = 0;
1119 continue;
1120
1121 case DT_XXX:
1122 HandleSpecial(c, l, p);
1123 continue;
1124
1125 case DT_FNTDEF:
1126 HandleFontDef(p);
1127 continue;
1128
1129 #ifdef ASCIIPTEX
1130 case DT_DIR:
1131 if (!UseThisPage)
1132 continue;
1133 ptexdvi = 1;
1134
1135 putbyte(outf, c);
1136 putbyte(outf, p);
1137 CurrentPosition += 2;
1138 continue;
1139 #endif /* ASCIIPTEX */
1140
1141 default:
1142 panic("HandleDVIFile DVI_DT(%d)=%d",
1143 c, DVI_DT(c));
1144 }
1145 continue;
1146 }
1147
1148 switch (c) { /* handle the few remaining cases */
1149
1150 case DVI_BOP:
1151 if (doingpage)
1152 GripeUnexpectedOp("BOP (during page)");
1153 BeginPage();
1154 doingpage = 1;
1155 CurrentFontOK = 0;
1156 break;
1157
1158 case DVI_EOP:
1159 if (!doingpage)
1160 GripeUnexpectedOp("EOP (outside page)");
1161 EndPage();
1162 doingpage = 0;
1163 break;
1164
1165 case DVI_PRE:
1166 GripeUnexpectedOp("PRE");
1167 /* NOTREACHED */
1168
1169 case DVI_POST:
1170 if (doingpage)
1171 GripeUnexpectedOp("POST (inside page)");
1172 return;
1173
1174 case DVI_POSTPOST:
1175 GripeUnexpectedOp("POSTPOST");
1176 /* NOTREACHED */
1177
1178 default:
1179 GripeUndefinedOp(c);
1180 /* NOTREACHED */
1181 }
1182 }
1183 }