Debian Codegraph @

texk/seetexk/dviconcat.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 concatenation program.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #ifdef KPATHSEA
32 #include <kpathsea/config.h>
33 #include <kpathsea/c-fopen.h>
34 #include <kpathsea/getopt.h>
35 #if defined(WIN32)
36 #include <kpathsea/variable.h>
37 #endif
38 #else
39 #define FOPEN_RBIN_MODE "rb"
40 #define FOPEN_WBIN_MODE "wb"
41 #define SET_BINARY(x) (void)0
42 extern char *optarg;
43 extern int optind;
44 #endif
45 #if defined(WIN32)
46 #include <sys/stat.h>
47 #endif
48
49 #include "types.h"
50 #include "dviclass.h"
51 #include "dvicodes.h"
52 #include "error.h"
53 #include "fio.h"
54 #include "gripes.h"
55 #include "search.h"
56 #include <stdio.h>
57 #include <ctype.h>
58 #include "common.h"
59
60 char *ProgName;
61
62 /*
63 * We use the following structure to keep track of fonts we have seen.
64 * The final DVI file lists all the fonts used in each of the input
65 * files.
66 */
67 struct fontinfo {
68 struct fontinfo *fi_next;/* next in list */
69 i32 fi_index; /* font number in output file */
70 i32 fi_checksum; /* the checksum */
71 i32 fi_mag; /* the magnification */
72 i32 fi_designsize; /* the design size */
73 short fi_n1; /* the name header length */
74 short fi_n2; /* the name body length */
75 char *fi_name; /* the name itself */
76 } *fonts;
77
78 struct search *FontFinder; /* input indicies to ptr-to-fontinfo */
79 i32 NextOutputFontIndex; /* generates output indicies */
80 i32 OutputFontIndex; /* current (new) index in ouput */
81
82 const char *DVIFileName; /* the current input file name */
83 FILE *inf; /* the current input DVI file */
84 FILE *outf; /* the output DVI file */
85
86 int errs; /* counts non-fatal errors */
87
88 long StartOfLastPage; /* The file position just before we started
89 the last page (this is later written to
90 the output file as the previous page
91 pointer). */
92 long CurrentPosition; /* The current position of the file */
93
94 int NumberOfOutputPages; /* number of pages in new DVI file */
95 #ifdef ASCIIPTEX
96 int ptexdvi; /* true => dvi format is extended (TATEKUMI) */
97 #endif /* ASCIIPTEX */
98
99 i32 Numerator; /* numerator from current DVI file */
100 i32 Denominator; /* denominator from current DVI file */
101 i32 DVIMag; /* magnification from current DVI file */
102
103 i32 OutputNumerator; /* numerator from first DVI file */
104 i32 OutputDenominator; /* denominator from first DVI file */
105 i32 OutputMag; /* magnification from first DVI file or arg */
106
107 i32 TallestPageHeight; /* max of all tallest-page-height values */
108 i32 WidestPageWidth; /* max of all widest-page-widths */
109 i16 DVIStackSize; /* max of all stack sizes */
110
111 /* save some string space: we use this a lot */
112 char writeerr[] = "error writing DVI file";
113
114 static void HandleDVIFile(void);
115
116 #ifndef KPATHSEA
117 char *malloc(), *realloc();
118 #endif
119
120 /* extern int getopt(int, char **, char*); */
121
122 /*
123 * You may get lint warnings about sprintf's return value.
124 * Older versions of 4BSD have `char *sprintf()'. ANSI and
125 * SysV use `int sprintf()'; so ignore the warnings.
126 */
127
128 /*
129 * Lint gets somewhat confused over putc.
130 */
131 #ifdef lint
132 #define putc(c, f) fputc((int) c, f)
133 #endif
134
135 /*
136 * Start a page (process a DVI_BOP).
137 */
138 static void
139 BeginPage(void)
140 {
141 register i32 t;
142 register int i;
143
144 OutputFontIndex = -1; /* new page requires respecifying font */
145 putbyte(outf, DVI_BOP);
146 /* copy the count registers */
147 for (i = 10; --i >= 0;) {
148 fGetLong(inf, t);
149 PutLong(outf, t);
150 }
151 (void) GetLong(inf); /* previous page pointer */
152 PutLong(outf, StartOfLastPage);
153 if (ferror(outf))
154 error(1, -1, writeerr);
155
156 StartOfLastPage = CurrentPosition;
157 CurrentPosition += 45; /* we just wrote this much */
158 }
159
160 /*
161 * End a page (process a DVI_EOP).
162 */
163 static void
164 EndPage(void)
165 {
166
167 putbyte(outf, DVI_EOP);
168 if (ferror(outf))
169 error(1, -1, writeerr);
170 CurrentPosition++;
171 NumberOfOutputPages++;
172 }
173
174 /*
175 * Write a font definition to the output file.
176 */
177 static void
178 WriteFont(register struct fontinfo *fi)
179 {
180 register int l;
181 register char *s;
182
183 if (fi->fi_index < 256) {
184 putbyte(outf, DVI_FNTDEF1);
185 putbyte(outf, fi->fi_index);
186 CurrentPosition += 2;
187 } else if (fi->fi_index < 65536) {
188 putbyte(outf, DVI_FNTDEF2);
189 PutWord(outf, fi->fi_index);
190 CurrentPosition += 3;
191 } else if (fi->fi_index < 16777216) {
192 putbyte(outf, DVI_FNTDEF3);
193 Put3Byte(outf, fi->fi_index);
194 CurrentPosition += 4;
195 } else {
196 putbyte(outf, DVI_FNTDEF4);
197 PutLong(outf, fi->fi_index);
198 CurrentPosition += 5;
199 }
200 PutLong(outf, fi->fi_checksum);
201 PutLong(outf, fi->fi_mag);
202 PutLong(outf, fi->fi_designsize);
203 putbyte(outf, fi->fi_n1);
204 putbyte(outf, fi->fi_n2);
205 l = fi->fi_n1 + fi->fi_n2;
206 CurrentPosition += 14 + l;
207 s = fi->fi_name;
208 while (--l >= 0)
209 putbyte(outf, *s++);
210 }
211
212 /*
213 * Write the postamble for the concatenation of all the input files.
214 */
215 static void
216 WritePostAmble(void)
217 {
218 register struct fontinfo *fi;
219 register i32 postpos = CurrentPosition; /* remember for later */
220
221 /* POST p n d m h w s pages */
222 putbyte(outf, DVI_POST);
223 PutLong(outf, StartOfLastPage);
224 PutLong(outf, OutputNumerator);
225 PutLong(outf, OutputDenominator);
226 PutLong(outf, OutputMag);
227 PutLong(outf, TallestPageHeight);
228 PutLong(outf, WidestPageWidth);
229 PutWord(outf, DVIStackSize);
230 PutWord(outf, NumberOfOutputPages);
231 CurrentPosition += 29; /* count all those `put's */
232
233 for (fi = fonts; fi != NULL; fi = fi->fi_next)
234 WriteFont(fi);
235
236 putbyte(outf, DVI_POSTPOST);
237 PutLong(outf, postpos);
238 #ifdef ASCIIPTEX
239 if (ptexdvi)
240 putbyte(outf, DVI_PTEXVERSION);
241 else
242 #endif /* ASCIIPTEX */
243 putbyte(outf, DVI_VERSION);
244 putbyte(outf, DVI_FILLER);
245 putbyte(outf, DVI_FILLER);
246 putbyte(outf, DVI_FILLER);
247 putbyte(outf, DVI_FILLER);
248 CurrentPosition += 10;
249 while (CurrentPosition & 3) {
250 putbyte(outf, DVI_FILLER);
251 CurrentPosition++;
252 }
253 if (ferror(outf))
254 error(1, -1, writeerr);
255 }
256
257 /*
258 * Read the information we need from the postamble for the current DVI file.
259 */
260 static void
261 HandlePostAmble(void)
262 {
263 register i32 t;
264 register i16 w;
265
266 (void) GetLong(inf); /* previous page pointer */
267 if (GetLong(inf) != Numerator) {
268 error(0, 0,
269 "%s: postamble's numerator does not match preamble's",
270 DVIFileName);
271 errs++;
272 }
273 if (GetLong(inf) != Denominator) {
274 error(0, 0,
275 "%s: postamble's denominator does not match preamble's",
276 DVIFileName);
277 errs++;
278 }
279 if (GetLong(inf) != DVIMag) {
280 error(0, 0,
281 "%s: postamble's magnification does not match preamble's",
282 DVIFileName);
283 errs++;
284 }
285
286 /*
287 * Find maximum of tallest page height, widest page width, and
288 * stack size.
289 */
290 t = GetLong(inf);
291 if (t > TallestPageHeight)
292 TallestPageHeight = t;
293 t = GetLong(inf);
294 if (t > WidestPageWidth)
295 WidestPageWidth = t;
296 w = GetWord(inf);
297 if (w > DVIStackSize)
298 DVIStackSize = w;
299
300 /*
301 * The remainder of the file---number of pages, list of fonts,
302 * postpost, pointer, version, and filler---we simply ignore.
303 */
304 }
305
306 /*
307 * Handle a preamble.
308 * `firstone' is true if this is the first input file.
309 * Return true iff something is wrong with the file.
310 */
311 static int
312 HandlePreAmble(int firstone)
313 {
314 register int n, c;
315 static char warn1[] = "%s: Warning: preamble %s of %ld";
316 static char warn2[] = "does not match first file's value of %ld";
317
318 if (getc(inf) != DVI_PRE) {
319 error(0, 0, "%s does not begin with a preamble", DVIFileName);
320 error(0, 0, "(are you sure it is a DVI file?)");
321 errs++;
322 return (1);
323 }
324 if (getc(inf) != DVI_VERSION) {
325 error(0, 0, "%s is not a DVI version %d file", DVIFileName,
326 DVI_VERSION);
327 errs++;
328 return (1);
329 }
330
331 /* committed to DVI file: now most errors are fatal */
332 if (firstone) {
333 OutputNumerator = Numerator = GetLong(inf);
334 OutputDenominator = Denominator = GetLong(inf);
335 } else {
336 Numerator = GetLong(inf);
337 if (Numerator != OutputNumerator) {
338 error(0, 0, warn1, DVIFileName, "numerator",
339 (long)Numerator);
340 error(0, 0, warn2, (long)OutputNumerator);
341 errs++;
342 }
343 Denominator = GetLong(inf);
344 if (Denominator != OutputDenominator) {
345 error(0, 0, warn1, DVIFileName, "denominator",
346 (long)Denominator);
347 error(0, 0, warn2, (long)OutputDenominator);
348 errs++;
349 }
350 }
351 DVIMag = GetLong(inf);
352 if (OutputMag == 0)
353 OutputMag = DVIMag;
354 else if (DVIMag != OutputMag) {
355 error(0, 0,
356 "%s: Warning: magnification of %ld changed to %ld",
357 DVIFileName, (long)DVIMag, (long)OutputMag);
358 errs++;
359 }
360 n = UnSign8(GetByte(inf)); /* comment length */
361 if (firstone) {
362 putbyte(outf, DVI_PRE);
363 putbyte(outf, DVI_VERSION);
364 PutLong(outf, Numerator);
365 PutLong(outf, Denominator);
366 PutLong(outf, OutputMag);
367
368 CurrentPosition = 15 + n; /* well, almost */
369 putbyte(outf, n);
370 while (--n >= 0) {
371 c = GetByte(inf);
372 putbyte(outf, c);
373 }
374 } else {
375 while (--n >= 0)
376 (void) GetByte(inf);
377 }
378 return (0);
379 }
380
381 /*
382 * Read one DVI file, concatenating it with the previous one (if any)
383 * or starting up the output file (otherwise).
384 */
385 static void
386 doit(const char *name, FILE *fp)
387 {
388 static int started;
389
390 DVIFileName = name;
391 inf = fp;
392 if (HandlePreAmble(started ? 0 : 1))
393 return;
394 SClear(FontFinder);
395 HandleDVIFile();
396 HandlePostAmble();
397 started = 1;
398 }
399
400 int
401 main(int argc, char **argv)
402 {
403 register int c;
404 register char *s;
405 FILE *f;
406 #if defined(WIN32) && defined(KPATHSEA)
407 int ac;
408 char **av, *enc;
409
410 if (argc>1) {
411 kpse_set_program_name(argv[0], "dviconcat");
412 enc = kpse_var_value("command_line_encoding");
413 if (get_command_line_args_utf8(enc, &ac, &av)) {
414 argc = ac;
415 argv = av;
416 }
417 }
418 #endif
419
420 ProgName = *argv;
421
422 /*
423 * Handle arguments.
424 */
425 while ((c = getopt(argc, argv, "m:o:")) != EOF) {
426 switch (c) {
427
428 case 'm':
429 if (OutputMag)
430 goto usage;
431 OutputMag = atoi(optarg);
432 break;
433
434 case 'o':
435 if (outf != NULL)
436 goto usage;
437 if ((outf = fopen(optarg, FOPEN_WBIN_MODE)) == NULL)
438 error(1, -1, "cannot write %s", optarg);
439 break;
440
441 case '?':
442 usage:
443 (void) fprintf(stderr,
444 "dviconcat in SeeTeX Ver.%s (%s)\n", VERSION, TL_VERSION);
445 (void) fprintf(stderr,
446 "Usage: %s [-m mag] [-o outfile] [files]\n",
447 ProgName);
448 (void) fprintf(stderr,
449 "\nEmail bug reports to %s.\n", BUG_ADDRESS);
450 (void) fflush(stderr);
451 exit(1);
452 }
453 }
454
455 /* setup */
456 if (outf == NULL) {
457 outf = stdout;
458 if (!isatty(fileno(outf)))
459 SET_BINARY(fileno(outf));
460 }
461 if ((FontFinder = SCreate(sizeof(struct fontinfo *))) == 0)
462 error(1, 0, "cannot create font finder (out of memory?)");
463 StartOfLastPage = -1;
464 #ifdef ASCIIPTEX
465 ptexdvi = 0;
466 #endif
467
468 /*
469 * Concatenate the named input file(s).
470 * We write a preamble based on the first input file.
471 */
472 if (optind >= argc) {
473 #if defined(WIN32)
474 if (outf == stdout) {
475 struct stat sti, sto;
476 int fdi = fileno(stdin), fdo = fileno(stdout);
477 if (isatty(fdi) == 0 && fstat(fdi, &sti) == 0 && S_ISFIFO(sti.st_mode)
478 && isatty(fdo) == 0 && fstat(fdo, &sto) == 0 && S_ISFIFO(sto.st_mode)) {
479 goto usage;
480 }
481 }
482 #endif
483 if (!isatty(fileno(stdin)))
484 SET_BINARY(fileno(stdin));
485 else
486 goto usage;
487 doit("`stdin'", stdin);
488 }
489 else {
490 for (c = optind; c < argc; c++) {
491 s = argv[c];
492 if (*s == '-' && s[1] == 0) {
493 if (!isatty(fileno(stdin)))
494 SET_BINARY(fileno(stdin));
495 else
496 goto usage;
497 doit("`stdin'", stdin);
498 }
499 else if ((f = fopen(s, FOPEN_RBIN_MODE)) == NULL) {
500 error(0, -1, "cannot read %s", s);
501 errs++;
502 } else {
503 doit(s, f);
504 (void) fclose(f);
505 }
506 }
507 }
508 if (CurrentPosition)
509 WritePostAmble();
510
511 (void) fprintf(stderr, "Wrote %d page%s, %ld bytes\n",
512 NumberOfOutputPages, NumberOfOutputPages == 1 ? "" : "s",
513 (long)CurrentPosition);
514 return errs ? 2 : 0;
515 }
516
517 /*
518 * Handle a font definition.
519 */
520 static void
521 HandleFontDef(i32 index)
522 {
523 register struct fontinfo *fi;
524 register int i;
525 register char *s;
526 register i32 checksum, mag, designsize;
527 register int n1, n2;
528 struct fontinfo **p;
529 char *name;
530 int d = S_CREATE | S_EXCL;
531
532 if ((p = (struct fontinfo **)SSearch(FontFinder, index, &d)) == NULL) {
533 if (d & S_COLL)
534 error(1, 0, "font %ld already defined", (long)index);
535 else
536 error(1, 0, "cannot stash font %ld (out of memory?)",
537 (long)index);
538
539 }
540 /* collect the font information */
541 checksum = GetLong(inf);
542 mag = GetLong(inf);
543 designsize = GetLong(inf);
544 n1 = UnSign8(GetByte(inf));
545 n2 = UnSign8(GetByte(inf));
546 i = n1 + n2;
547 if ((s = malloc((unsigned)i)) == NULL)
548 GripeOutOfMemory(i, "font name");
549 for (name = s; --i >= 0;)
550 *s++ = GetByte(inf);
551 s = name;
552
553 /*
554 * We have not seen the font before in this input file,
555 * but we may have seen it in a previous file, so we
556 * must search.
557 */
558 i = n1 + n2;
559 for (fi = fonts; fi != NULL; fi = fi->fi_next) {
560 if (fi->fi_designsize == designsize &&
561 fi->fi_mag == mag &&
562 fi->fi_n1 == n1 && fi->fi_n2 == n2 &&
563 memcmp(fi->fi_name, s, i) == 0) {
564 if (fi->fi_checksum == 0)
565 fi->fi_checksum = checksum;
566 else if (checksum && fi->fi_checksum != checksum) {
567 error(0, 0, "\
568 %s: Warning: font checksum mismatch for %.*s detected",
569 DVIFileName, i, s);
570 errs++;
571 }
572 *p = fi;
573 return;
574 }
575 }
576 /* it is really new; add it to the list */
577 if ((fi = (struct fontinfo *)malloc(sizeof *fi)) == NULL)
578 GripeOutOfMemory(sizeof *fi, "font information");
579 fi->fi_next = fonts;
580 fi->fi_index = NextOutputFontIndex++;
581 fi->fi_checksum = checksum;
582 fi->fi_mag = mag;
583 fi->fi_designsize = designsize;
584 fi->fi_n1 = n1;
585 fi->fi_n2 = n2;
586 fi->fi_name = s;
587 fonts = fi;
588 WriteFont(fi);
589 *p = fi;
590 }
591
592 /*
593 * Handle a \special.
594 */
595 static void
596 HandleSpecial(int c, int l, i32 p)
597 {
598 register int i;
599
600 putbyte(outf, c);
601 switch (l) {
602
603 case DPL_UNS1:
604 putbyte(outf, p);
605 CurrentPosition += 2;
606 break;
607
608 case DPL_UNS2:
609 PutWord(outf, p);
610 CurrentPosition += 3;
611 break;
612
613 case DPL_UNS3:
614 Put3Byte(outf, p);
615 CurrentPosition += 4;
616 break;
617
618 case DPL_SGN4:
619 PutLong(outf, p);
620 CurrentPosition += 5;
621 break;
622
623 default:
624 panic("HandleSpecial l=%d", l);
625 /* NOTREACHED */
626 }
627 CurrentPosition += p;
628 while (--p >= 0) {
629 i = getc(inf);
630 putbyte(outf, i);
631 }
632 if (feof(inf))
633 GripeUnexpectedDVIEOF();
634 if (ferror(outf))
635 error(1, -1, writeerr);
636 }
637
638 static void
639 UseFont(i32 index)
640 {
641 struct fontinfo *fi, **p;
642 int look = S_LOOKUP;
643
644 p = (struct fontinfo **)SSearch(FontFinder, index, &look);
645 if (p == NULL)
646 error(1, 0, "%s requested font %ld without defining it",
647 (long)index);
648 if ((fi = *p) == NULL)
649 panic("null entry in FontFinder for %ld", (long)index);
650 index = fi->fi_index;
651 if (index < 64) {
652 putbyte(outf, index + DVI_FNTNUM0);
653 CurrentPosition++;
654 } else if (index < 256) {
655 putbyte(outf, DVI_FNT1);
656 putbyte(outf, index);
657 CurrentPosition += 2;
658 } else if (index < 65536) {
659 putbyte(outf, DVI_FNT2);
660 PutWord(outf, index);
661 CurrentPosition += 3;
662 } else if (index < 16777216) {
663 putbyte(outf, DVI_FNT3);
664 Put3Byte(outf, index);
665 CurrentPosition += 4;
666 } else {
667 putbyte(outf, DVI_FNT4);
668 PutLong(outf, index);
669 CurrentPosition += 5;
670 }
671 }
672
673 /*
674 * The following table describes the length (in bytes) of each of the DVI
675 * commands that we can simply copy, starting with DVI_SET1 (128).
676 */
677 char oplen[128] = {
678 2, 3, 4, 5, /* DVI_SET1 .. DVI_SET4 */
679 9, /* DVI_SETRULE */
680 2, 3, 4, 5, /* DVI_PUT1 .. DVI_PUT4 */
681 9, /* DVI_PUTRULE */
682 1, /* DVI_NOP */
683 0, /* DVI_BOP */
684 0, /* DVI_EOP */
685 1, /* DVI_PUSH */
686 1, /* DVI_POP */
687 2, 3, 4, 5, /* DVI_RIGHT1 .. DVI_RIGHT4 */
688 1, /* DVI_W0 */
689 2, 3, 4, 5, /* DVI_W1 .. DVI_W4 */
690 1, /* DVI_X0 */
691 2, 3, 4, 5, /* DVI_X1 .. DVI_X4 */
692 2, 3, 4, 5, /* DVI_DOWN1 .. DVI_DOWN4 */
693 1, /* DVI_Y0 */
694 2, 3, 4, 5, /* DVI_Y1 .. DVI_Y4 */
695 1, /* DVI_Z0 */
696 2, 3, 4, 5, /* DVI_Z1 .. DVI_Z4 */
697 0, /* DVI_FNTNUM0 (171) */
698 0, 0, 0, 0, 0, 0, 0, 0, /* 172 .. 179 */
699 0, 0, 0, 0, 0, 0, 0, 0, /* 180 .. 187 */
700 0, 0, 0, 0, 0, 0, 0, 0, /* 188 .. 195 */
701 0, 0, 0, 0, 0, 0, 0, 0, /* 196 .. 203 */
702 0, 0, 0, 0, 0, 0, 0, 0, /* 204 .. 211 */
703 0, 0, 0, 0, 0, 0, 0, 0, /* 212 .. 219 */
704 0, 0, 0, 0, 0, 0, 0, 0, /* 220 .. 227 */
705 0, 0, 0, 0, 0, 0, 0, /* 228 .. 234 */
706 0, 0, 0, 0, /* DVI_FNT1 .. DVI_FNT4 */
707 0, 0, 0, 0, /* DVI_XXX1 .. DVI_XXX4 */
708 0, 0, 0, 0, /* DVI_FNTDEF1 .. DVI_FNTDEF4 */
709 0, /* DVI_PRE */
710 0, /* DVI_POST */
711 0, /* DVI_POSTPOST */
712 #ifdef ASCIIPTEX
713 0, 0, 0, 0, 0, /* 250 .. 254 */
714 0, /* DVI_DIR */
715 #else /* !ASCIIPTEX */
716 0, 0, 0, 0, 0, 0, /* 250 .. 255 */
717 #endif /* !ASCIIPTEX */
718 };
719
720 /*
721 * Here we read the input DVI file and copy the pages to the output
722 * DVI file, renumbering fonts. We also keep track of font changes,
723 * handle font definitions, and perform some other housekeeping.
724 */
725 static void
726 HandleDVIFile(void)
727 {
728 register int c, l;
729 register i32 p = 0; /* avoid uninitialized warning */
730 int doingpage = 0;
731
732 /* Only way out is via "return" statement */
733 for (;;) {
734 c = getc(inf); /* getc() returns unsigned values */
735 if (DVI_IsChar(c)) {
736 putbyte(outf, c);
737 CurrentPosition++;
738 continue;
739 }
740 if (DVI_IsFont(c)) { /* note font change */
741 UseFont((i32)(c - DVI_FNTNUM0));
742 continue;
743 }
744 if (c == EOF)
745 GripeUnexpectedDVIEOF();
746 if ((l = (oplen - 128)[c]) != 0) { /* simple copy */
747 CurrentPosition += l;
748 putbyte(outf, c);
749 while (--l > 0) {
750 c = getc(inf);
751 putbyte(outf, c);
752 }
753 if (ferror(outf))
754 error(1, -1, writeerr);
755 continue;
756 }
757 if ((l = DVI_OpLen(c)) != 0) {
758 /*
759 * Handle other generics.
760 * N.B.: there should only be unsigned parameters
761 * here (save SGN4), for commands with negative
762 * parameters have been taken care of above.
763 */
764 switch (l) {
765
766 case DPL_UNS1:
767 p = getc(inf);
768 break;
769
770 case DPL_UNS2:
771 fGetWord(inf, p);
772 break;
773
774 case DPL_UNS3:
775 fGet3Byte(inf, p);
776 break;
777
778 case DPL_SGN4:
779 fGetLong(inf, p);
780 break;
781
782 default:
783 panic("HandleDVIFile l=%d", l);
784 }
785
786 /*
787 * Now that we have the parameter, perform the
788 * command.
789 */
790 switch (DVI_DT(c)) {
791
792 case DT_FNT:
793 UseFont(p);
794 continue;
795
796 case DT_XXX:
797 HandleSpecial(c, l, p);
798 continue;
799
800 case DT_FNTDEF:
801 HandleFontDef(p);
802 continue;
803
804 #ifdef ASCIIPTEX
805 case DT_DIR:
806 ptexdvi = 1;
807
808 putbyte(outf, c);
809 putbyte(outf, p);
810 CurrentPosition += 2;
811 continue;
812 #endif /* ASCIIPTEX */
813
814 default:
815 panic("HandleDVIFile DVI_DT(%d)=%d",
816 c, DVI_DT(c));
817 }
818 continue;
819 }
820
821 switch (c) { /* handle the few remaining cases */
822
823 case DVI_BOP:
824 if (doingpage)
825 GripeUnexpectedOp("BOP (during page)");
826 BeginPage();
827 doingpage = 1;
828 break;
829
830 case DVI_EOP:
831 if (!doingpage)
832 GripeUnexpectedOp("EOP (outside page)");
833 EndPage();
834 doingpage = 0;
835 break;
836
837 case DVI_PRE:
838 GripeUnexpectedOp("PRE");
839 /* NOTREACHED */
840
841 case DVI_POST:
842 if (doingpage)
843 GripeUnexpectedOp("POST (inside page)");
844 return;
845
846 case DVI_POSTPOST:
847 GripeUnexpectedOp("POSTPOST");
848 /* NOTREACHED */
849
850 default:
851 GripeUndefinedOp(c);
852 /* NOTREACHED */
853 }
854 }
855 }