Debian Codegraph @

texk/dtl/dt2dv.c

raw
1 /* dt2dv - convert human-readable "DTL" file to DVI format
2 - this is intended to invert dv2dt version 0.6.0
3
4 This file is public domain.
5 Originally written 1995, Geoffrey Tobin.
6 The author has expressed the hope that any modification will retain
7 enough content to remain useful. He would also appreciate being
8 acknowledged as the original author in the documentation.
9 This declaration added 2008/11/14 by Clea F. Rees with the permission
10 of Geoffrey Tobin.
11
12 - version 0.6.1 - 14:38 GMT +11 Thu 9 March 1995
13 - Geoffrey Tobin [email protected]
14 - fixes: Michal Tomczak-Jaegermann [email protected]
15 Nelson H. F. Beebe [email protected]
16 - Reference: "The DVI Driver Standard, Level 0",
17 by The TUG DVI Driver Standards Committee.
18 Appendix A, "Device-Independent File Format".
19 */
20
21 /* unix version; read from stdin, write to stdout, by default. */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #ifdef KPATHSEA
39 #include <kpathsea/config.h>
40 #include <kpathsea/c-fopen.h>
41 #include <kpathsea/progname.h>
42 #endif
43
44 #include "dtl.h"
45
46 /* by default, read and write regular files */
47 int rd_stdin = 0;
48 int wr_stdout = 0;
49
50 /* maximum number of characters in a DTL input line */
51 #define MAXLINE 8192
52
53 /* input line */
54 typedef struct
55 {
56 COUNT num; /* current line number */
57 size_t max; /* capacity of buf */
58 S4 wrote; /* number of characters written into buf */
59 size_t read; /* position in buf of next character to read from buf */
60 char * buf; /* line buffer */
61 } Line;
62
63 char linebuf[MAXLINE+1];
64
65 Line dtl_line = {0, 0, 0, MAXLINE, linebuf};
66
67 /* a DTL token either is:
68 a quoted string (admitting an escape character),
69 or BCOM (if that is a nonempty string),
70 or ECOM (if that is a nonempty string),
71 or a string _not_ including ECOM_CHAR or space.
72 */
73
74 /* maximum expected length of a DTL token */
75 #define MAXTOKLEN 255
76 typedef char Token[MAXTOKLEN+2];
77
78 typedef unsigned char Byte;
79 typedef char Boolean;
80
81 #ifndef true
82 #define true 1
83 #endif
84 #ifndef false
85 #define false 0
86 #endif
87
88 /* command prefixes */
89 typedef struct
90 {
91 Byte first_code;
92 const char * name;
93 Boolean has_suffix;
94 Byte first_suffix, last_suffix;
95 } CmdPrefix;
96
97 CmdPrefix cmd_prefixes [] =
98 {
99 {0, SETCHAR, true, 0, 127},
100 {128, SET, true, 1, 4},
101 {132, SETRULE, false, 0, 0},
102 {133, PUT, true, 1, 4},
103 {137, PUTRULE, false, 0, 0},
104 {138, NOP, false, 0, 0},
105 {139, BOP, false, 0, 0},
106 {140, EOP, false, 0, 0},
107 {141, PUSH, false, 0, 0},
108 {142, POP, false, 0, 0},
109 {143, RIGHT, true, 1, 4},
110 {147, W, true, 0, 4},
111 {152, X, true, 0, 4},
112 {157, DOWN, true, 1, 4},
113 {161, Y, true, 0, 4},
114 {166, Z, true, 0, 4},
115 {171, FONTNUM, true, 0, 63},
116 {235, FONT, true, 1, 4},
117 {239, SPECIAL, true, 1, 4},
118 {243, FONTDEF, true, 1, 4},
119 {247, PRE, false, 0, 0},
120 {248, POST, false, 0, 0},
121 {249, POSTPOST, false, 0, 0},
122 {250, OPCODE, true, 250, 255}
123 };
124 /* cmd_prefixes[] */
125
126 /* Number of DVI commands, including those officially undefined */
127 #define NCMDS 256
128
129 /* table of command name (string) pointers */
130 typedef char * CmdTable [NCMDS];
131
132 /* initially all command name pointers are NULL */
133 CmdTable cmd_table;
134
135 /* operation's opcode, name, number of args, string of arguments. */
136 typedef struct
137 {
138 int code;
139 const char * name;
140 int nargs;
141 const char * args;
142 } op_info;
143
144 /* name of table, first opcode, last opcode, pointer to opcode info. */
145 typedef struct
146 {
147 const char * name;
148 int first;
149 int last;
150 op_info * list;
151 } op_table;
152
153 /* Table for opcodes 128 to 170 inclusive. */
154
155 op_info op_info_128_170 [] =
156 {
157 {128, SET1, 1, "1"},
158 {129, SET2, 1, "2"},
159 {130, SET3, 1, "3"},
160 {131, SET4, 1, "-4"},
161 {132, SETRULE, 2, "-4 -4"},
162 {133, PUT1, 1, "1"},
163 {134, PUT2, 1, "2"},
164 {135, PUT3, 1, "3"},
165 {136, PUT4, 1, "-4"},
166 {137, PUTRULE, 2, "-4 -4"},
167 {138, NOP, 0, ""},
168 /* bop: not counting last argument, a signed address: */
169 {139, BOP, 10, "-4 -4 -4 -4 -4 -4 -4 -4 -4 -4"},
170 {140, EOP, 0, ""},
171 {141, PUSH, 0, ""},
172 {142, POP, 0, ""},
173 {143, RIGHT1, 1, "-1"},
174 {144, RIGHT2, 1, "-2"},
175 {145, RIGHT3, 1, "-3"},
176 {146, RIGHT4, 1, "-4"},
177 {147, W0, 0, ""},
178 {148, W1, 1, "-1"},
179 {149, W2, 1, "-2"},
180 {150, W3, 1, "-3"},
181 {151, W4, 1, "-4"},
182 {152, X0, 0, ""},
183 {153, X1, 1, "-1"},
184 {154, X2, 1, "-2"},
185 {155, X3, 1, "-3"},
186 {156, X4, 1, "-4"},
187 {157, DOWN1, 1, "-1"},
188 {158, DOWN2, 1, "-2"},
189 {159, DOWN3, 1, "-3"},
190 {160, DOWN4, 1, "-4"},
191 {161, Y0, 0, ""},
192 {162, Y1, 1, "-1"},
193 {163, Y2, 1, "-2"},
194 {164, Y3, 1, "-3"},
195 {165, Y4, 1, "-4"},
196 {166, Z0, 0, ""},
197 {167, Z1, 1, "-1"},
198 {168, Z2, 1, "-2"},
199 {169, Z3, 1, "-3"},
200 {170, Z4, 1, "-4"}
201 };
202 /* op_info op_info_128_170 [] */
203
204 op_table op_128_170 = {"op_128_170", 128, 170, op_info_128_170};
205
206 /* Table for fnt1 to fnt4 (opcodes 235 to 238) inclusive. */
207
208 op_info fnt_n [] =
209 {
210 {235, FONT1, 1, "1"},
211 {236, FONT2, 1, "2"},
212 {237, FONT3, 1, "3"},
213 {238, FONT4, 1, "-4"}
214 };
215 /* op_info fnt_n [] */
216
217 op_table fnt = {FONT, 235, 238, fnt_n};
218
219
220 /* Function prototypes */
221
222 Void mem_viol ARGS((int sig));
223 Void give_help (VOID);
224 int parse ARGS((const char * s));
225 Void process ARGS((const char * s));
226
227 Void no_op (VOID);
228 Void dtl_stdin (VOID);
229 Void dvi_stdout (VOID);
230
231 int open_dtl ARGS((const char * dtl_file, FILE ** pdtl));
232 int open_dvi ARGS((const char * dvi_file, FILE ** pdvi));
233
234 int dt2dv ARGS((FILE * dtl, FILE * dvi));
235
236 Void * gmalloc ARGS((long int size));
237
238 Void dinfo (VOID);
239 Void dexit ARGS((int n));
240
241 int cons_cmds ARGS((int nprefixes, CmdPrefix * prefix, CmdTable cmds));
242 Void free_cmds ARGS((CmdTable cmds));
243
244 int get_line ARGS((FILE * fp, Line * line, int max));
245 int read_line_char ARGS((FILE * fp, int * ch));
246 int read_char ARGS((FILE * fp, int * ch));
247 int unread_char (VOID);
248 int read_string_char ARGS((FILE * fp, int * ch));
249
250 COUNT read_variety ARGS((FILE * dtl));
251 COUNT read_token ARGS((FILE * dtl, char * token));
252 COUNT skip_space ARGS((FILE * fp, int * ch));
253 COUNT read_misc ARGS((FILE * fp, Token token));
254 COUNT read_mes ARGS((FILE * fp, char * token));
255
256 int find_command ARGS((char * command, int * opcode));
257 int xfer_args ARGS((FILE * dtl, FILE * dvi, int opcode));
258
259 int set_seq ARGS((FILE * dtl, FILE * dvi));
260
261 int check_byte ARGS((int byte));
262 int put_byte ARGS((int onebyte, FILE * dvi));
263
264 U4 xfer_hex ARGS((int n, FILE * dtl, FILE * dvi));
265 U4 xfer_oct ARGS((int n, FILE * dtl, FILE * dvi));
266 U4 xfer_unsigned ARGS((int n, FILE * dtl, FILE * dvi));
267 S4 xfer_signed ARGS((int n, FILE * dtl, FILE * dvi));
268
269 int check_bmes ARGS((FILE * dtl));
270 int check_emes ARGS((FILE * dtl));
271
272 Void init_Lstring ARGS((Lstring * lsp, long int n));
273 Void de_init_Lstring ARGS((Lstring * lsp));
274 Lstring * alloc_Lstring ARGS((long int n));
275 Void free_Lstring ARGS((Lstring * lstr));
276 Void ls_putb ARGS((int ch, Lstring * lstr));
277
278 S4 get_Lstring ARGS((FILE * dtl, Lstring * lstr));
279 Void put_Lstring ARGS((const Lstring * lstr, FILE * dvi));
280 U4 xfer_len_string ARGS((int n, FILE * dtl, FILE * dvi));
281
282 U4 get_unsigned ARGS((FILE * dtl));
283 S4 get_signed ARGS((FILE * dtl));
284
285 int put_unsigned ARGS((int n, U4 unum, FILE * dvi));
286 int put_signed ARGS((int n, S4 snum, FILE * dvi));
287
288 S4 xfer_bop_address ARGS((FILE * dtl, FILE * dvi));
289 S4 xfer_postamble_address ARGS((FILE * dtl, FILE * dvi));
290
291 int put_table ARGS((op_table table, int opcode, FILE * dtl, FILE * dvi));
292
293 U4 special ARGS((FILE * dtl, FILE * dvi, int n));
294 int fontdef ARGS((FILE * dtl, FILE * dvi, int n));
295
296 U4 preamble ARGS((FILE * dtl, FILE * dvi));
297 int postamble ARGS((FILE * dtl, FILE * dvi));
298 int post_post ARGS((FILE * dtl, FILE * dvi));
299
300
301 typedef struct
302 {
303 const char * keyword; /* command line option keyword */
304 int * p_var; /* pointer to option variable */
305 const char * desc; /* description of keyword and value */
306 Void (* p_fn) (VOID); /* pointer to function called when option is set */
307 } Options;
308
309 Options opts[] =
310 {
311 {"-debug", &debug, "detailed debugging", no_op},
312 {"-group", &group, "each DTL command is in parentheses", no_op},
313 {"-si", &rd_stdin, "read all DTL commands from standard input", dtl_stdin},
314 {"-so", &wr_stdout, "write all DVI commands to standard output", dvi_stdout},
315 {NULL, NULL, NULL, NULL}
316 };
317 /* opts[] */
318
319 const char * progname; /* intended for name of this program */
320 int nfile; /* number of filename arguments on the command line */
321
322 #define PRINT_PROGNAME fprintf (stderr, "%s ", progname)
323
324 /* Harbison & Steele (1991) warn that some C implementations */
325 /* of free() do not treat NULL pointers correctly. */
326 #define gfree(p) {if (p) free (p);}
327
328
329 FILE * dtl_fp;
330 FILE * dvi_fp;
331
332 const char * dtl_filename;
333 const char * dvi_filename;
334
335
336 int
337 main
338 #ifdef STDC
339 (int argc, char * argv[])
340 #else
341 (argc, argv)
342 int argc;
343 char * argv[];
344 #endif
345 {
346 int i;
347
348 #ifdef KPATHSEA
349 kpse_set_program_name(argv[0], "dt2dv");
350 progname = kpse_program_name;
351 #else
352 progname = argv[0]; /* name of this program */
353 #endif
354
355 /* memory violation signal handler */
356 /* Not supported under Win32 */
357 #ifndef WIN32
358 signal (SIGSEGV, mem_viol);
359 #endif
360
361 #ifndef __DATE__
362 #define __DATE__ ""
363 #endif
364
365 #ifndef __TIME__
366 #define __TIME__ ""
367 #endif
368
369 #if STDC
370 #define C_LEVEL ""
371 #else /* NOT STDC */
372 #define C_LEVEL "non-"
373 #endif /* NOT STDC */
374
375 /* message about program and compiler */
376 /* NB: LTU EE's Sun/OS library is BSD, even though gcc 2.2.2 is ANSI */
377
378 fprintf (stderr, "\n");
379 fprintf (stderr,
380 "Program \"%s\" version %s (%s) compiled in %sstandard C.\n",
381 progname, VERSION, TL_VERSION, C_LEVEL);
382
383 /* interpret command line arguments */
384
385 nfile = 0;
386 dtl_fp = dvi_fp = NULL;
387 dtl_filename = dvi_filename = "";
388
389 for (i=1; i < argc; i++)
390 {
391 /* parse options, followed by any explicit filenames */
392 parse (argv[i]);
393 }
394
395 if (nfile != 2) /* not exactly two files specified, so give help */
396 give_help();
397 else
398 /* the real works */
399 dt2dv (dtl_fp, dvi_fp);
400
401 return 0; /* OK */
402 }
403 /* end main */
404
405 #ifndef WIN32
406 Void
407 mem_viol
408 #ifdef STDC
409 (int sig)
410 #else
411 (sig)
412 int sig;
413 #endif
414 {
415 signal (SIGSEGV, mem_viol);
416 if (sig != SIGSEGV)
417 {
418 PRINT_PROGNAME;
419 fprintf (stderr,
420 "(mem_viol) : called with wrong signal!\n");
421 }
422 PRINT_PROGNAME;
423 fprintf (stderr, "(mem_viol) : RUNTIME MEMORY ERROR : memory violation, ");
424 fprintf (stderr, "dtl line >= ");
425 fprintf (stderr, WF, dtl_line.num);
426 fprintf (stderr, "\n");
427 dexit (1);
428 }
429 /* mem_viol */
430 #endif
431
432 Void
433 give_help (VOID)
434 {
435 int i;
436 const char * keyword;
437 fprintf (stderr, "usage: ");
438 PRINT_PROGNAME;
439 fprintf (stderr, "[options] dtl_file dvi_file");
440 fprintf (stderr, "\n");
441 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
442 {
443 fprintf (stderr, " ");
444 fprintf (stderr, "[%s]", keyword);
445 fprintf (stderr, " ");
446 fprintf (stderr, "%s", opts[i].desc);
447 fprintf (stderr, "\n");
448 }
449 fprintf (stderr, "Messages, like this one, go to stderr.\n");
450 fprintf (stderr, "\nEmail bug reports to %s.\n", PACKAGE_BUGREPORT);
451 }
452 /* give_help */
453
454
455 Void no_op (VOID)
456 /* do nothing */
457 {
458 }
459
460 Void dtl_stdin (VOID)
461 {
462 dtl_fp = stdin;
463 dtl_filename = "Standard Input";
464 ++ nfile;
465 }
466
467 Void dvi_stdout (VOID)
468 {
469 /* ! Perilous to monitors! */
470 dvi_fp = stdout;
471 dvi_filename = "Standard Output";
472 ++ nfile;
473 if (!isatty(fileno(dvi_fp)))
474 SET_BINARY(fileno(dvi_fp));
475 }
476
477
478 int
479 parse
480 #ifdef STDC
481 (const char * s)
482 #else
483 (s)
484 const char * s;
485 #endif
486 /* parse one command-line argument, `s' */
487 {
488 int i;
489 const char * keyword;
490 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
491 {
492 if (strncmp (s, keyword, strlen (keyword)) == 0)
493 {
494 Void (*pfn) (VOID);
495 (*(opts[i].p_var)) = 1; /* turn option on */
496 if ((pfn = opts[i].p_fn) != NULL)
497 (*pfn) (); /* call option function */
498 return i;
499 }
500 }
501 /* reached here, so not an option: assume it's a filename */
502 process (s);
503 return i;
504 }
505 /* parse */
506
507
508 int
509 open_dtl
510 #ifdef STDC
511 (const char * dtl_file, FILE ** pdtl)
512 #else
513 (dtl_file, pdtl)
514 const char * dtl_file;
515 FILE ** pdtl;
516 #endif
517 /* I: dtl_file; I: pdtl; O: *pdtl. */
518 {
519 dtl_filename = dtl_file;
520
521 if (dtl_filename == NULL)
522 {
523 PRINT_PROGNAME;
524 fprintf (stderr,
525 "(open_dtl) : INTERNAL ERROR : dtl file's name is NULL.\n");
526 dexit (1);
527 }
528
529 if (pdtl == NULL)
530 {
531 PRINT_PROGNAME;
532 fprintf (stderr,
533 "(open_dtl) : INTERNAL ERROR : address of dtl variable is NULL.\n");
534 dexit (1);
535 }
536
537 *pdtl = fopen (dtl_file, "r");
538
539 if (*pdtl == NULL)
540 {
541 PRINT_PROGNAME;
542 fprintf (stderr,
543 "(open_dtl) : DTL FILE ERROR : Cannot open \"%s\" for text reading.\n",
544 dtl_file);
545 dexit (1);
546 }
547
548 return 1; /* OK */
549 }
550 /* open_dtl */
551
552
553 int
554 open_dvi
555 #ifdef STDC
556 (const char * dvi_file, FILE ** pdvi)
557 #else
558 (dvi_file, pdvi)
559 const char * dvi_file;
560 FILE ** pdvi;
561 #endif
562 /* I: dvi_file; I: pdvi; O: *pdvi. */
563 {
564 dvi_filename = dvi_file;
565
566 if (dvi_filename == NULL)
567 {
568 PRINT_PROGNAME;
569 fprintf (stderr,
570 "(open_dvi) : INTERNAL ERROR : dvi file's name is NULL.\n");
571 dexit (1);
572 }
573
574 if (pdvi == NULL)
575 {
576 PRINT_PROGNAME;
577 fprintf (stderr,
578 "(open_dvi) : INTERNAL ERROR : address of dvi variable is NULL.\n");
579 dexit (1);
580 }
581
582 *pdvi = fopen (dvi_file, "wb");
583
584 if (*pdvi == NULL)
585 {
586 PRINT_PROGNAME;
587 fprintf (stderr,
588 "(open_dvi) : DVI FILE ERROR : Cannot open \"%s\" for binary writing.\n",
589 dvi_file);
590 dexit (1);
591 }
592
593 return 1; /* OK */
594 }
595 /* open_dvi */
596
597
598 Void
599 process
600 #ifdef STDC
601 (const char * s)
602 #else
603 (s)
604 const char * s;
605 #endif
606 {
607 if (dtl_fp == NULL) /* first filename assumed to be DTL input */
608 {
609 open_dtl (s, &dtl_fp);
610 }
611 else if (dvi_fp == NULL) /* second filename assumed to be DVI output */
612 {
613 open_dvi (s, &dvi_fp);
614 }
615 else
616 {
617 PRINT_PROGNAME;
618 fprintf (stderr, "(process) : at most two filenames allowed.\n");
619 exit (1);
620 }
621 ++ nfile;
622 }
623 /* process */
624
625
626 COUNT dtl_read = 0; /* bytes read from dtl file */
627 COUNT dvi_written = 0; /* bytes written to dvi file */
628 word_t last_bop_address = -1; /* byte address of last bop; first bop uses -1 */
629 word_t postamble_address = -1; /* byte address of postamble */
630 COUNT ncom = 0; /* commands successfully read and interpreted from dtl file */
631 COUNT com_read = 0; /* bytes read in current (command and arguments), */
632 /* since and including the opening BCOM_CHAR, if any */
633
634
635 int
636 put_byte
637 #ifdef STDC
638 (int byte, FILE * dvi)
639 #else
640 (byte, dvi)
641 int byte;
642 FILE * dvi;
643 #endif
644 /* write byte into dvi file */
645 {
646 check_byte (byte);
647 /* if (fprintf (dvi, "%c", byte) != 1) */
648 if (fprintf (dvi, "%c", byte) < 0)
649 {
650 PRINT_PROGNAME;
651 fprintf (stderr,
652 "(put_byte) : DVI FILE ERROR (%s) : cannot write to dvi file.\n",
653 dtl_filename);
654 dexit (1);
655 }
656 ++ dvi_written;
657 return 1; /* OK */
658 }
659 /* put_byte */
660
661
662 int
663 dt2dv
664 #ifdef STDC
665 (FILE * dtl, FILE * dvi)
666 #else
667 (dtl, dvi)
668 FILE * dtl;
669 FILE * dvi;
670 #endif
671 {
672 int nprefixes = 0; /* number of prefixes in cmd_prefixes[] list. */
673 static Token dtl_cmd = ""; /* DTL command name */
674
675 nprefixes = sizeof (cmd_prefixes) / sizeof (CmdPrefix);
676
677 /* Construct array of all NCMDS == 256 DTL commands */
678
679 (Void) cons_cmds (nprefixes, cmd_prefixes, cmd_table);
680
681 /* DTL commands have the form "[ ]*command arg ... arg[ ]*", */
682 /* possibly enclosed in a BCOM, ECOM pair, */
683 /* and are separated by optional whitespace, typically newlines. */
684 /* That is, each command and its arguments are parenthesised, */
685 /* with optional spaces after the BCOM and before the ECOM, if any. */
686
687 /* dt2dv is now at the very start of the DTL file */
688
689 dtl_line.num = 0;
690 dtl_read = 0;
691
692 /* The very first thing should be the "variety" signature */
693
694 read_variety (dtl);
695
696 /* while not end of dtl file or reading error, */
697 /* read, interpret, and write commands */
698
699 while (!feof (dtl))
700 {
701 int opcode;
702
703 com_read = 0;
704
705 if (group)
706 {
707 /* BCOM check */
708 static Token token = ""; /* DTL token */
709 read_token (dtl, token);
710 /* test for end of input, or reading error */
711 if (strlen (token) == 0)
712 {
713 if (debug)
714 {
715 PRINT_PROGNAME;
716 fprintf (stderr, "(dt2dv) : end of input, or reading error.\n");
717 }
718 break;
719 }
720 /* test whether this command begins correctly */
721 else if (strcmp (token, BCOM) != 0)
722 {
723 PRINT_PROGNAME;
724 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
725 fprintf (stderr, "command must begin with \"%s\", ", BCOM);
726 fprintf (stderr, "not `%c' (char %d).\n", token[0], token[0]);
727 dexit (1);
728 }
729 /* end BCOM check */
730 }
731
732 /* read the command name */
733 read_token (dtl, dtl_cmd);
734 /* test for end of input, or reading error */
735 if (strlen (dtl_cmd) == 0)
736 {
737 if (debug)
738 {
739 PRINT_PROGNAME;
740 fprintf (stderr,
741 "(dt2dv) : end of input, or reading error.\n");
742 }
743 break;
744 }
745 else
746 {
747 if (debug)
748 {
749 PRINT_PROGNAME;
750 fprintf (stderr, "(dt2dv) : command ");
751 fprintf (stderr, WF, ncom);
752 fprintf (stderr, " = \"%s\".\n", dtl_cmd);
753 }
754
755 /* find opcode for this command */
756 if (find_command (dtl_cmd, &opcode) == 1)
757 {
758 /* write the opcode, if we can */
759 put_byte (opcode, dvi);
760
761 /* treat the arguments, if any */
762 xfer_args (dtl, dvi, opcode);
763 }
764 else if (dtl_cmd[0] == BSEQ_CHAR)
765 {
766 /* sequence of font characters for SETCHAR */
767 set_seq (dtl, dvi);
768 }
769 else
770 {
771 PRINT_PROGNAME;
772 fprintf (stderr,
773 "(dt2dv) : DTL FILE ERROR (%s) : unknown command \"%s\".\n",
774 dtl_filename, dtl_cmd);
775 dexit (1);
776 }
777 }
778
779 if (group)
780 {
781 /* seek ECOM after command's last argument and optional whitespace */
782 static Token token = ""; /* DTL token */
783 read_token (dtl, token);
784 /* test for end of input, or reading error */
785 if (strlen (token) == 0)
786 {
787 if (debug)
788 {
789 PRINT_PROGNAME;
790 fprintf (stderr,
791 "(dt2dv) : end of input, or reading error.\n");
792 }
793 break;
794 }
795 if (strcmp (token, ECOM) != 0)
796 {
797 PRINT_PROGNAME;
798 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
799 fprintf (stderr, "ECOM (\"%s\") expected, not `%c' (char %d).\n",
800 ECOM, token[0], token[0]);
801 dexit (1);
802 }
803 /* end ECOM check */
804 }
805
806 ++ ncom; /* one more command successfully read and interpreted */
807 }
808 /* end while */
809
810 PRINT_PROGNAME;
811 fprintf (stderr, "(dt2dv) :\n");
812 fprintf (stderr, "Read (from file \"%s\") ", dtl_filename);
813 fprintf (stderr, WF, dtl_read);
814 fprintf (stderr, " DTL bytes (");
815 fprintf (stderr, UF4, dtl_line.num);
816 fprintf (stderr, " lines);\n");
817 fprintf (stderr, "wrote (to file \"%s\") ", dvi_filename);
818 fprintf (stderr, WF, dvi_written);
819 fprintf (stderr, " DVI bytes;\n");
820 fprintf (stderr, "completely interpreted ");
821 fprintf (stderr, WF, ncom);
822 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
823 fprintf (stderr, "\n");
824
825 (Void) free_cmds (cmd_table);
826
827 return 1; /* OK */
828 }
829 /* dt2dv */
830
831
832 Void *
833 gmalloc
834 #ifdef STDC
835 (long int size)
836 #else
837 (size)
838 long int size;
839 #endif
840 {
841 Void * p = NULL;
842 if (size < 1)
843 {
844 PRINT_PROGNAME;
845 fprintf (stderr, "(gmalloc) : INTERNAL ERROR : ");
846 fprintf (stderr,
847 "unreasonable request to malloc %ld bytes\n",
848 size);
849 dexit (1);
850 }
851 p = (Void *) malloc ((size_t) size);
852 if (p == NULL)
853 {
854 PRINT_PROGNAME;
855 fprintf (stderr, "(gmalloc) : MEMORY ALLOCATION ERROR : ");
856 fprintf (stderr,
857 "operating system failed to malloc %ld bytes\n",
858 size);
859 dexit (1);
860 }
861 return (p);
862 }
863 /* gmalloc */
864
865
866 Void
867 dinfo (VOID)
868 {
869 PRINT_PROGNAME;
870 fprintf (stderr, "(dinfo) : ");
871 fprintf (stderr, "Current DTL input line ");
872 fprintf (stderr, UF4, dtl_line.num);
873 fprintf (stderr, " :\n");
874 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
875 fprintf (stderr, "Read ");
876 fprintf (stderr, WF, dtl_read);
877 fprintf (stderr, " DTL bytes (");
878 fprintf (stderr, WF, com_read);
879 fprintf (stderr, " in current command), wrote ");
880 fprintf (stderr, WF, dvi_written);
881 fprintf (stderr, " DVI bytes.\n");
882 fprintf (stderr, "Successfully interpreted ");
883 fprintf (stderr, WF, ncom);
884 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
885 }
886 /* dinfo */
887
888
889 Void
890 dexit
891 #ifdef STDC
892 (int n)
893 #else
894 (n)
895 int n;
896 #endif
897 {
898 dinfo();
899 PRINT_PROGNAME;
900 fprintf (stderr, "(dexit) : exiting with status %d.\n", n);
901 exit (n);
902 }
903 /* dexit */
904
905
906 int
907 cons_cmds
908 #ifdef STDC
909 (int nprefixes, CmdPrefix * prefix, CmdTable cmds)
910 #else
911 (nprefixes, prefix, cmds)
912 int nprefixes;
913 CmdPrefix * prefix;
914 CmdTable cmds;
915 #endif
916 {
917 int code; /* first opcode for a given command prefix */
918 int opcode; /* command's opcode */
919 int nsuffixes; /* number of commands with a given prefix */
920 int isuffix; /**** integer suffix, of at most three digits ****/
921 String suffix; /* suffix string generated from integer suffix */
922 size_t plen = 0; /* prefix length */
923 size_t slen; /* suffix length */
924 size_t clen; /* whole command name length */
925 int i, j; /* loop indices */
926
927 for (i=0; i < nprefixes; prefix++, i++)
928 {
929 code = prefix->first_code;
930 if (code < 0 || code > 255)
931 {
932 PRINT_PROGNAME;
933 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
934 fprintf (stderr,
935 "prefix listed internally with code = %d, must be 0 to 255\n",
936 code);
937 dexit (1);
938 }
939 if (prefix->has_suffix)
940 {
941 plen = strlen (prefix->name);
942 /**** Suffixes in DTL are Integers, in Sequence */
943 if (prefix->last_suffix < prefix->first_suffix)
944 {
945 PRINT_PROGNAME;
946 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
947 fprintf (stderr,
948 "prefix's last suffix %d < first suffix (%d)\n",
949 prefix->last_suffix, prefix->first_suffix);
950 dexit (1);
951 }
952 nsuffixes = prefix->last_suffix - prefix->first_suffix + 1;
953 opcode = prefix->first_code;
954 for (j=0; j < nsuffixes; j++, opcode++)
955 {
956 isuffix = prefix->first_suffix + j;
957 if (0 <= code && code <= 127) /* SETCHAR */
958 {
959 /* SETCHAR's suffix is written in uppercase hexadecimal */
960 sprintf (suffix, "%02X", isuffix);
961 }
962 else /* 128 <= code && code <= 255 */ /* other DTL commands */
963 {
964 /* other commands' suffices are written in decimal */
965 sprintf (suffix, "%d", isuffix);
966 }
967 slen = strlen (suffix);
968 clen = plen + slen;
969 cmds[opcode] = (char *) gmalloc (clen+1);
970 strcpy (cmds[opcode], prefix->name);
971 strcat (cmds[opcode], suffix);
972 }
973 }
974 else /* command name = prefix */
975 {
976 plen = strlen (prefix->name);
977 clen = plen;
978 opcode = prefix->first_code;
979 cmds[opcode] = (char *) gmalloc (clen+1);
980 strcpy (cmds[opcode], prefix->name);
981 }
982 }
983
984 return 1; /* OK */
985 }
986 /* cons_cmds */
987
988
989 Void
990 free_cmds
991 #ifdef STDC
992 (CmdTable cmds)
993 #else
994 (cmds)
995 CmdTable cmds;
996 #endif
997 {
998 int i;
999 for (i=0; i < NCMDS; i++)
1000 gfree (cmds[i]);
1001 }
1002 /* free_cmds */
1003
1004
1005 int
1006 get_line
1007 #ifdef STDC
1008 (FILE * fp, Line * line, int max)
1009 #else
1010 (fp, line, max)
1011 FILE * fp;
1012 Line * line;
1013 int max;
1014 #endif
1015 /* read a (Line *) line from fp, return length */
1016 /* adapted from K&R (second, alias ANSI C, edition, 1988), page 165 */
1017 {
1018 if (fgets (line->buf, max, fp) == NULL)
1019 return 0;
1020 else
1021 {
1022 ++ line->num;
1023 line->wrote = strlen (line->buf);
1024 line->read = 0;
1025 return 1;
1026 }
1027 }
1028 /* get_line */
1029
1030
1031 int
1032 read_line_char
1033 #ifdef STDC
1034 (FILE * fp, int * ch)
1035 #else
1036 (fp, ch)
1037 FILE * fp;
1038 int * ch;
1039 #endif
1040 /* read one character from dtl_line if possible, */
1041 /* otherwise read another dtl_line from fp */
1042 /* return 1 if a character is read, 0 if at end of fp file */
1043 {
1044 if (dtl_line.wrote == 0 || dtl_line.read >= dtl_line.wrote)
1045 {
1046 int line_status;
1047 /* refill line buffer */
1048 line_status = get_line (fp, &dtl_line, MAXLINE);
1049 if (line_status == 0)
1050 {
1051 /* at end of DTL file */
1052 if (debug)
1053 {
1054 PRINT_PROGNAME;
1055 fprintf (stderr, "(read_line_char) : end of DTL file\n");
1056 dinfo();
1057 }
1058 return 0;
1059 }
1060 else
1061 {
1062 /* new DTL line was read */
1063 if (debug)
1064 {
1065 PRINT_PROGNAME;
1066 fprintf (stderr, "(read_line_char) : new DTL input line:\n");
1067 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
1068 }
1069 }
1070 }
1071 *ch = dtl_line.buf [dtl_line.read ++];
1072 ++ dtl_read;
1073 ++ com_read; /* count another DTL command character */
1074 return 1;
1075 }
1076 /* read_line_char */
1077
1078
1079 int
1080 read_char
1081 #ifdef STDC
1082 (FILE * fp, int * ch)
1083 #else
1084 (fp, ch)
1085 FILE * fp;
1086 int * ch;
1087 #endif
1088 /* Read next character, if any, from file fp. */
1089 /* Write it into *ch. */
1090 /* If no character is read, then *ch value < 0. */
1091 /* Return 1 if OK, 0 if EOF or error. */
1092 {
1093 int status = 1;
1094 int c; /* in case ch points awry, we still have something in c. */
1095
1096 c = EOF;
1097 if (read_line_char (fp, &c) == 0)
1098 {
1099 /* end of fp file, or error reading it */
1100 status = 0;
1101 }
1102 else
1103 {
1104 if (c > 255)
1105 {
1106 PRINT_PROGNAME;
1107 fprintf (stderr,
1108 "(read_char) : character %d not in range 0 to 255\n",
1109 c);
1110 dinfo();
1111 status = 0;
1112 }
1113 else if ( ! isprint (c) && ! isspace (c)
1114 #ifdef WIN32
1115 && ! isknj (c) && ! isknj2 (c)
1116 #endif
1117 )
1118 {
1119 PRINT_PROGNAME;
1120 fprintf (stderr,
1121 "(read_char) : character %d not printable and not white space.\n",
1122 c);
1123 dinfo();
1124 status = 0;
1125 }
1126 }
1127 *ch = c;
1128
1129 return status;
1130 }
1131 /* read_char */
1132
1133
1134 COUNT
1135 read_variety
1136 #ifdef STDC
1137 (FILE * dtl)
1138 #else
1139 (dtl)
1140 FILE * dtl;
1141 #endif
1142 /* read and check DTL variety signature */
1143 /* return number of DTL bytes written */
1144 /* DTL variety is _NEVER_ grouped by BCOM and ECOM. */
1145 /* Uniformity here enables the program easily to modify its behavior. */
1146 {
1147 COUNT vread = 0; /* number of DTL bytes read by read_variety */
1148 static Token token = "";
1149
1150 /* read the DTL VARIETY keyword */
1151 vread += read_token (dtl, token);
1152 /* test whether signature begins correctly */
1153 if (strcmp (token, "variety") != 0)
1154 {
1155 PRINT_PROGNAME;
1156 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1157 fprintf (stderr, "DTL signature must begin with \"variety\", not \"%s\".\n",
1158 token);
1159 dexit (1);
1160 }
1161
1162 /* read the DTL variety */
1163 vread += read_token (dtl, token);
1164 /* test whether variety is correct */
1165 if (strcmp (token, VARIETY) != 0)
1166 {
1167 PRINT_PROGNAME;
1168 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1169 fprintf (stderr, "DTL variety must be \"%s\", not \"%s\".\n",
1170 VARIETY, token);
1171 dexit (1);
1172 }
1173
1174 PRINT_PROGNAME;
1175 fprintf (stderr, "(read_variety) : DTL variety %s is OK.\n", VARIETY);
1176
1177 return vread; /* OK */
1178 }
1179 /* read_variety */
1180
1181
1182 COUNT
1183 skip_space
1184 #ifdef STDC
1185 (FILE * fp, int * ch)
1186 #else
1187 (fp, ch)
1188 FILE * fp;
1189 int * ch;
1190 #endif
1191 /* Skip whitespace characters in file fp. */
1192 /* Write in *ch the last character read from fp, */
1193 /* or < 0 if fp could not be read. */
1194 /* Return number of characters read from fp. */
1195 {
1196 int c; /* character read (if any) */
1197 COUNT count; /* number (0 or more) of whitespace characters read */
1198 int nchar; /* number (0 or 1) of characters read by read_char */
1199
1200 /* loop ends at: end of fp file, or reading error, or not a white space */
1201 for (count=0; ((nchar = read_char (fp, &c)) == 1 && isspace (c)); ++count)
1202 {
1203 /* otherwise, more white spaces to skip */
1204 if (debug)
1205 {
1206 /* report when each DTL end of line is reached */
1207 if (c == '\n')
1208 {
1209 PRINT_PROGNAME;
1210 fprintf (stderr, "(skip_space) : ");
1211 fprintf (stderr, "end of DTL line (at least) ");
1212 fprintf (stderr, WF, dtl_line.num);
1213 fprintf (stderr, "\n");
1214 }
1215 }
1216 }
1217
1218 if (nchar == 0)
1219 {
1220 c = -1;
1221 }
1222
1223 *ch = c; /* c will be < 0 if read_char could not read fp */
1224 return (count + nchar);
1225 }
1226 /* skip_space */
1227
1228
1229 COUNT
1230 read_token
1231 #ifdef STDC
1232 (FILE * dtl, char * token)
1233 #else
1234 (dtl, token)
1235 FILE * dtl;
1236 char * token;
1237 #endif
1238 /* read next token from dtl file. */
1239 /* return number of DTL bytes read. */
1240 /* A token is one of:
1241 a string from BMES_CHAR to the next unescaped EMES_CHAR, inclusive;
1242 BCOM or ECOM, unless these are empty strings;
1243 BSEQ or ESEQ;
1244 any other sequence of non-whitespace characters.
1245 */
1246 {
1247 COUNT nread; /* number of DTL bytes read by read_token */
1248 int ch; /* most recent character read */
1249
1250 nread = 0;
1251
1252 /* obtain first non-space character */
1253 /* add to nread the number of characters read from dtl by skip_space */
1254 nread += skip_space (dtl, &ch);
1255
1256 if (ch < 0)
1257 {
1258 /* end of dtl file */
1259 /* write an empty token */
1260 strcpy (token, "");
1261 if (debug)
1262 {
1263 PRINT_PROGNAME;
1264 fprintf (stderr, "(read_token) : end of dtl file.\n");
1265 }
1266 }
1267 else if (group && ch == BCOM_CHAR)
1268 {
1269 strcpy (token, BCOM);
1270 }
1271 else if (group && ch == ECOM_CHAR)
1272 {
1273 strcpy (token, ECOM);
1274 }
1275 else
1276 {
1277 token[0] = ch;
1278 token[1] = '\0';
1279 if (ch == BMES_CHAR) /* string token; read until unescaped EMES_CHAR */
1280 {
1281 nread += read_mes (dtl, token+1);
1282 }
1283 else if (ch == BSEQ_CHAR || ch == ESEQ_CHAR)
1284 {
1285 /* token is complete */
1286 }
1287 else /* any other string not containing (ECOM_CHAR or) whitespace */
1288 {
1289 nread += read_misc (dtl, token+1);
1290 }
1291 }
1292
1293 if (debug)
1294 {
1295 PRINT_PROGNAME;
1296 fprintf (stderr, "(read_token) : token = \"%s\"\n", token);
1297 }
1298
1299 return (nread); /* number of bytes read from dtl file */
1300 }
1301 /* read_token */
1302
1303
1304 #define CHAR_OK 1
1305 #define CHAR_FAIL 0
1306 #define CHAR_EOS (-1)
1307
1308 int
1309 read_string_char
1310 #ifdef STDC
1311 (FILE * fp, int * ch)
1312 #else
1313 (fp, ch)
1314 FILE * fp;
1315 int * ch;
1316 #endif
1317 {
1318 int status = CHAR_OK; /* OK so far */
1319 int c;
1320
1321 if (read_char (fp, &c) == 0)
1322 status = CHAR_FAIL; /* fail */
1323
1324 if (c == EMES_CHAR) /* end-of-string char */
1325 {
1326 status = CHAR_EOS; /* end of string */
1327 }
1328 else if (c == ESC_CHAR) /* escape character */
1329 {
1330 /* accept the next character literally, even ESC_CHAR and EMES_CHAR */
1331 if (read_char (fp, &c) == 0)
1332 status = CHAR_FAIL; /* fail */
1333 }
1334
1335 *ch = c;
1336 return status;
1337 }
1338 /* read_string_char */
1339
1340
1341 COUNT
1342 read_misc
1343 #ifdef STDC
1344 (FILE * fp, Token token)
1345 #else
1346 (fp, token)
1347 FILE * fp;
1348 Token token;
1349 #endif
1350 {
1351 int c;
1352 int count;
1353 /* loop ends at: end of fp file, or reading error, or a space */
1354 for (count=0; count <= MAXTOKLEN; ++count)
1355 {
1356 if (read_char (fp, &c) == 0 || isspace (c))
1357 break;
1358 if (group && c == ECOM_CHAR)
1359 {
1360 (Void) unread_char ();
1361 break;
1362 }
1363
1364 token[count] = c;
1365 }
1366 token[count] = '\0';
1367 return count;
1368 }
1369 /* read_misc */
1370
1371
1372 COUNT
1373 read_mes
1374 #ifdef STDC
1375 (FILE * fp, char * token)
1376 #else
1377 (fp, token)
1378 FILE * fp;
1379 char * token;
1380 #endif
1381 /* called **AFTER** a BMES_CHAR has been read */
1382 /* read file fp for characters up to next unescaped EMES_CHAR */
1383 /* this is called a "string token" */
1384 /* write string, including EMES_CHAR, into token[] */
1385 /* return number of characters read from fp */
1386 {
1387 COUNT dtl_count; /* number of DTL characters read by read_mes from fp */
1388 int more; /* flag more == 0 to terminate loop */
1389 int escape; /* flag escape == 1 if previous character was ESC_CHAR */
1390 int ch; /* current DTL character */
1391
1392 escape = 0;
1393 more = 1;
1394 dtl_count = 0;
1395 while (more)
1396 {
1397 if (read_char (fp, &ch) == 0)
1398 {
1399 /* end of fp file, or reading error */
1400 more = 0;
1401 }
1402 else /* error checking passed */
1403 {
1404 ++ dtl_count;
1405 if (ch == EMES_CHAR && escape == 0) /* end of string */
1406 {
1407 /* include final EMES_CHAR */
1408 * token ++ = ch;
1409 more = 0;
1410 }
1411 else if (ch == ESC_CHAR && escape == 0)
1412 {
1413 /* next character is not end of string */
1414 escape = 1;
1415 }
1416 else
1417 {
1418 /* store any other character, */
1419 /* including escaped EMES_CHAR and ESC_CHAR*/
1420 * token ++ = ch;
1421 escape = 0;
1422 }
1423 }
1424 }
1425 * token = '\0';
1426 return dtl_count;
1427 }
1428 /* read_mes */
1429
1430
1431 int
1432 unread_char (VOID)
1433 /* wind input back, to allow rereading of one character */
1434 /* return 1 if this works, 0 on error */
1435 {
1436 int status;
1437 if (dtl_line.read > 0)
1438 {
1439 -- dtl_line.read; /* back up one character in dtl_line */
1440 -- dtl_read; /* correct the count of DTL characters */
1441 -- com_read; /* count another DTL command character */
1442 status = 1; /* OK */
1443 }
1444 else /* current DTL line is empty */
1445 {
1446 status = 0; /* error */
1447 }
1448 return status;
1449 }
1450 /* unread_char */
1451
1452
1453 int
1454 find_command
1455 #ifdef STDC
1456 (char * command, int * opcode)
1457 #else
1458 (command, opcode)
1459 char * command;
1460 int * opcode;
1461 #endif
1462 {
1463 int found;
1464 int i;
1465
1466 found = 0;
1467 for (i=0; i < NCMDS; i++)
1468 {
1469 if ((cmd_table[i] != 0) && (strcmp (command, cmd_table[i]) == 0))
1470 {
1471 found = 1;
1472 break;
1473 }
1474 }
1475
1476 *opcode = i;
1477
1478 return found;
1479 }
1480 /* find_command */
1481
1482
1483 int
1484 check_byte
1485 #ifdef STDC
1486 (int byte)
1487 #else
1488 (byte)
1489 int byte;
1490 #endif
1491 {
1492 if (byte < 0 || byte > 255)
1493 {
1494 PRINT_PROGNAME;
1495 fprintf (stderr, "(check_byte) : INTERNAL ERROR : ");
1496 fprintf (stderr, "byte %d not in the range of 0 to 255.\n", byte);
1497 dexit (1);
1498 }
1499 return 1; /* OK */
1500 }
1501 /* check_byte */
1502
1503
1504 int
1505 xfer_args
1506 #ifdef STDC
1507 (FILE * dtl, FILE * dvi, int opcode)
1508 #else
1509 (dtl, dvi, opcode)
1510 FILE * dtl;
1511 FILE * dvi;
1512 int opcode;
1513 #endif
1514 {
1515 int n;
1516
1517 if (opcode >= 0 && opcode <= 127)
1518 ; /* SETCHAR uses no data */
1519 else if (opcode >= 128 && opcode <= 170)
1520 {
1521 word_t this_bop_address = last_bop_address;
1522
1523 if (opcode == 139) /* BOP */
1524 {
1525 this_bop_address = dvi_written - 1;
1526 }
1527 put_table (op_128_170, opcode, dtl, dvi);
1528 if (opcode == 139) /* BOP */
1529 {
1530 xfer_bop_address (dtl, dvi);
1531 last_bop_address = this_bop_address;
1532 }
1533 }
1534 else if (opcode >= 171 && opcode <= 234)
1535 ; /* FONTNUM uses no data */
1536 else if (opcode >= 235 && opcode <= 238)
1537 put_table (fnt, opcode, dtl, dvi);
1538 else if (opcode >= 239 && opcode <= 242)
1539 {
1540 n = opcode - 238;
1541 special (dtl, dvi, n);
1542 }
1543 else if (opcode >= 243 && opcode <= 246)
1544 {
1545 n = opcode - 242;
1546 fontdef (dtl, dvi, n);
1547 }
1548 else if (opcode == 247)
1549 preamble (dtl, dvi);
1550 else if (opcode == 248)
1551 postamble (dtl, dvi);
1552 else if (opcode == 249)
1553 post_post (dtl, dvi);
1554 else if (opcode >= 250 && opcode <= 255)
1555 ; /* these, undefined, opcodes use no data */
1556 else
1557 {
1558 PRINT_PROGNAME;
1559 fprintf (stderr,
1560 "(xfer_args) : opcode %d not handled.\n",
1561 opcode);
1562 }
1563
1564 return 1; /* OK */
1565 }
1566 /* xfer_args */
1567
1568
1569 int
1570 set_seq
1571 #ifdef STDC
1572 (FILE * dtl, FILE * dvi)
1573 #else
1574 (dtl, dvi)
1575 FILE * dtl;
1576 FILE * dvi;
1577 #endif
1578 /* Called _after_ a BSEQ_CHAR command */
1579 /* Read bytes from dtl file, */
1580 /* writing corresponding SETCHAR or SET1 commands to DVI file, */
1581 /* _until_ unescaped ESEQ_CHAR is found */
1582 /* Return 1 if OK, 0 on error */
1583 /**** dt2dv assumes 8 bit characters, ****/
1584 /**** but some day one might change that. ****/
1585 {
1586 int status = 1; /* status = 1 if OK, 0 if error */
1587 int more; /* sequence of font characters continuing? */
1588 int escape = 0; /* flag set if previous character was an escape */
1589 int ch; /* character read from DTL file */
1590 more = 1;
1591 while (more)
1592 {
1593 /* ignore read_char status, to allow unprintable characters */
1594 (Void) read_char (dtl, &ch);
1595 /* but check for end of dtl file, or serious file reading error */
1596 if (ch < 0)
1597 {
1598 PRINT_PROGNAME;
1599 fprintf (stderr, "(set_seq) : ");
1600 fprintf (stderr, "end of dtl file, ");
1601 fprintf (stderr, "or serious dtl file reading error\n");
1602 dinfo();
1603 more = 0;
1604 status = 0; /* bad news */
1605 }
1606 else /* read dtl file, okay */
1607 {
1608 if (ch == ESC_CHAR && escape == 0) /* escape next character */
1609 {
1610 escape = 1;
1611 }
1612 else
1613 {
1614 if (ch == ESEQ_CHAR && escape == 0) /* end of sequence */
1615 {
1616 more = 0;
1617 }
1618 else if (ch <= 127) /* can use SETCHAR */
1619 {
1620 put_byte (ch, dvi);
1621 }
1622 else if (ch <= 255) /* can use SET1 */
1623 {
1624 put_byte (128, dvi); /* SET1 opcode */
1625 put_unsigned (1, (U4) ch, dvi);
1626 }
1627 else
1628 {
1629 PRINT_PROGNAME;
1630 fprintf (stderr, "(set_seq) : ");
1631 fprintf (stderr,
1632 "ERROR : DTL character %d is not in range 0 to 255\n",
1633 ch);
1634 dexit (1);
1635 more = 0;
1636 status = 0; /* Error, because dt2dv assumes 8 bit characters */
1637 }
1638 escape = 0; /* current ch is not an escape character */
1639 }
1640 }
1641 }
1642 return status;
1643 }
1644 /* set_seq */
1645
1646
1647 U4
1648 xfer_hex
1649 #ifdef STDC
1650 (int n, FILE * dtl, FILE * dvi)
1651 #else
1652 (n, dtl, dvi)
1653 int n;
1654 FILE * dtl;
1655 FILE * dvi;
1656 #endif
1657 /* translate unsigned n-byte hexadecimal number from dtl to dvi file. */
1658 /* return value of hexadecimal number */
1659 {
1660 U4 unum = 0; /* at most this space needed */
1661 int nconv = 0; /* number of arguments converted by sscanf */
1662 static Token token = ""; /* DTL token */
1663
1664 if (n < 1 || n > 4)
1665 {
1666 PRINT_PROGNAME;
1667 fprintf (stderr,
1668 "(xfer_hex) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1669 n);
1670 dexit (1);
1671 }
1672
1673 read_token (dtl, token);
1674
1675 nconv = sscanf (token, XF4, &unum);
1676
1677 if (nconv < 1)
1678 {
1679 PRINT_PROGNAME;
1680 fprintf (stderr, "(xfer_hex) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1681 dtl_filename, "hexadecimal number expected, not", token);
1682 dexit (1);
1683 }
1684
1685 put_unsigned (n, unum, dvi);
1686
1687 return unum;
1688 }
1689 /* xfer_hex */
1690
1691
1692 U4
1693 xfer_oct
1694 #ifdef STDC
1695 (int n, FILE * dtl, FILE * dvi)
1696 #else
1697 (n, dtl, dvi)
1698 int n;
1699 FILE * dtl;
1700 FILE * dvi;
1701 #endif
1702 /* translate unsigned n-byte octal number from dtl to dvi file. */
1703 /* return value of octal number */
1704 {
1705 U4 unum = 0; /* at most this space needed */
1706 int nconv = 0; /* number of arguments converted by sscanf */
1707 static Token token = ""; /* DTL token */
1708
1709 if (n < 1 || n > 4)
1710 {
1711 PRINT_PROGNAME;
1712 fprintf (stderr,
1713 "(xfer_oct) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1714 n);
1715 dexit (1);
1716 }
1717
1718 read_token (dtl, token);
1719
1720 nconv = sscanf (token, OF4, &unum);
1721
1722 if (nconv < 1)
1723 {
1724 PRINT_PROGNAME;
1725 fprintf (stderr, "(xfer_oct) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1726 dtl_filename, "octal number expected, not", token);
1727 dexit (1);
1728 }
1729
1730 put_unsigned (n, unum, dvi);
1731
1732 return unum;
1733 }
1734 /* xfer_oct */
1735
1736
1737 U4
1738 xfer_unsigned
1739 #ifdef STDC
1740 (int n, FILE * dtl, FILE * dvi)
1741 #else
1742 (n, dtl, dvi)
1743 int n;
1744 FILE * dtl;
1745 FILE * dvi;
1746 #endif
1747 /* translate unsigned n-byte number from dtl to dvi file. */
1748 /* return value of unsigned number */
1749 {
1750 U4 unum = 0; /* at most this space needed */
1751
1752 unum = get_unsigned (dtl);
1753 put_unsigned (n, unum, dvi);
1754
1755 return unum;
1756 }
1757 /* xfer_unsigned */
1758
1759
1760 S4
1761 xfer_signed
1762 #ifdef STDC
1763 (int n, FILE * dtl, FILE * dvi)
1764 #else
1765 (n, dtl, dvi)
1766 int n;
1767 FILE * dtl;
1768 FILE * dvi;
1769 #endif
1770 /* translate signed n-byte number from dtl to dvi file. */
1771 /* return value of signed number */
1772 {
1773 S4 snum = 0;
1774
1775 snum = get_signed (dtl);
1776 put_signed (n, snum, dvi);
1777
1778 return snum;
1779 }
1780 /* xfer_signed */
1781
1782
1783 U4
1784 get_unsigned
1785 #ifdef STDC
1786 (FILE * dtl)
1787 #else
1788 (dtl)
1789 FILE * dtl;
1790 #endif
1791 /* read unsigned number from dtl file. */
1792 /* return value of unsigned number */
1793 {
1794 U4 unum = 0; /* at most this space needed */
1795 int nconv = 0; /* number of arguments converted by sscanf */
1796 static Token token = ""; /* DTL token */
1797
1798 read_token (dtl, token);
1799
1800 nconv = sscanf (token, UF4, &unum);
1801
1802 if (nconv < 1)
1803 {
1804 PRINT_PROGNAME;
1805 fprintf (stderr, "(get_unsigned) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1806 dtl_filename, "unsigned number expected, not", token);
1807 dexit (1);
1808 }
1809
1810 return unum;
1811 }
1812 /* get_unsigned */
1813
1814
1815 S4
1816 get_signed
1817 #ifdef STDC
1818 (FILE * dtl)
1819 #else
1820 (dtl)
1821 FILE * dtl;
1822 #endif
1823 /* read signed number from dtl file. */
1824 /* return value of signed number */
1825 {
1826 S4 snum = 0;
1827 int nconv = 0; /* number of sscanf arguments converted and assigned */
1828 static Token token = "";
1829
1830 read_token (dtl, token);
1831
1832 nconv = sscanf (token, SF4, &snum);
1833
1834 if (nconv < 1)
1835 {
1836 PRINT_PROGNAME;
1837 fprintf (stderr, "(get_signed) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1838 dtl_filename, "signed number expected, not", token);
1839 dexit (1);
1840 }
1841
1842 return snum;
1843 }
1844 /* get_signed */
1845
1846
1847 int
1848 put_unsigned
1849 #ifdef STDC
1850 (int n, U4 unum, FILE * dvi)
1851 #else
1852 (n, unum, dvi)
1853 int n;
1854 U4 unum;
1855 FILE * dvi;
1856 #endif
1857 /* put unsigned in-byte integer to dvi file */
1858 /* DVI format uses Big-endian storage of numbers: */
1859 /* most significant byte is first. */
1860 /* return number of bytes written. */
1861 {
1862 Byte ubyte[4]; /* at most 4 bytes needed in DVI format */
1863 int i;
1864
1865 if (n < 1 || n > 4)
1866 {
1867 PRINT_PROGNAME;
1868 fprintf (stderr,
1869 "(put_unsigned) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1870 n);
1871 dexit (1);
1872 }
1873
1874 /* Big-endian storage. */
1875 for (i = 0; i < n; i++)
1876 {
1877 ubyte[i] = (Byte) (unum % 256);
1878 unum /= 256;
1879 }
1880 /* Reverse order for big-endian representation. */
1881 for (i = n-1; i >= 0; i--)
1882 {
1883 put_byte ((int) ubyte[i], dvi);
1884 }
1885
1886 return n;
1887 }
1888 /* put_unsigned */
1889
1890
1891 int
1892 put_signed
1893 #ifdef STDC
1894 (int n, S4 snum, FILE * dvi)
1895 #else
1896 (n, snum, dvi)
1897 int n;
1898 S4 snum;
1899 FILE * dvi;
1900 #endif
1901 /* put signed in-byte integer to dvi file */
1902 /* DVI format uses 2's complement Big-endian storage of signed numbers: */
1903 /* most significant byte is first. */
1904 /* return number of bytes written. */
1905 {
1906 /* Will this deal properly with the sign? */
1907
1908 if (n < 1 || n > 4)
1909 {
1910 PRINT_PROGNAME;
1911 fprintf (stderr,
1912 "(put_signed) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1913 n);
1914 dexit (1);
1915 }
1916
1917 /* How do we ensure 2's complement representation? */
1918 /* Here's a trick that I hope is portable, given ANSI C. */
1919 /* See K&R (2nd edition), Appendix A6.2 "Integral Conversions". */
1920
1921 /* Convert snum to U4 data type */
1922 put_unsigned (n, (U4) snum, dvi);
1923
1924 return n;
1925 }
1926 /* put_signed */
1927
1928
1929 int
1930 check_bmes
1931 #ifdef STDC
1932 (FILE * dtl)
1933 #else
1934 (dtl)
1935 FILE * dtl;
1936 #endif
1937 /* check that a BMES_CHAR is the next non-whitespace character in dtl */
1938 {
1939 int ch; /* next non-whitespace character in dtl */
1940
1941 /* `(Void)' because we ignore the number of spaces skipped */
1942 (Void) skip_space (dtl, &ch);
1943
1944 if (ch < 0)
1945 {
1946 PRINT_PROGNAME;
1947 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1948 dtl_filename);
1949 fprintf (stderr, "end of dtl file, or reading error\n");
1950 dexit (1);
1951 }
1952
1953 if (ch != BMES_CHAR)
1954 {
1955 PRINT_PROGNAME;
1956 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1957 dtl_filename);
1958 fprintf (stderr, "BMES_CHAR (`%c') expected before string, not `%c' (char %d).\n",
1959 BMES_CHAR, ch, ch);
1960 dexit (1);
1961 }
1962
1963 return 1; /* OK */
1964 }
1965 /* check_bmes */
1966
1967
1968 int
1969 check_emes
1970 #ifdef STDC
1971 (FILE * dtl)
1972 #else
1973 (dtl)
1974 FILE * dtl;
1975 #endif
1976 /* check that an EMES_CHAR is the next character in dtl */
1977 {
1978 int ch; /* dtl character */
1979
1980 if (read_char (dtl, &ch) == 0)
1981 {
1982 PRINT_PROGNAME;
1983 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1984 dtl_filename);
1985 fprintf (stderr, "end of dtl file, or reading error\n");
1986 dexit (1);
1987 }
1988
1989 if (ch != EMES_CHAR)
1990 {
1991 PRINT_PROGNAME;
1992 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1993 dtl_filename);
1994 fprintf (stderr, "EMES_CHAR (`%c') expected to follow string, not `%c' (char %d).\n",
1995 EMES_CHAR, ch, ch);
1996 dexit (1);
1997 }
1998
1999 return 1; /* OK */
2000 }
2001 /* check_emes */
2002
2003
2004 /* Size typically used in this program for Lstring variables */
2005 #define LSIZE 16384
2006
2007
2008 Void
2009 init_Lstring
2010 #ifdef STDC
2011 (Lstring * lsp, long int n)
2012 #else
2013 (lsp, n)
2014 Lstring * lsp;
2015 long int n;
2016 #endif
2017 {
2018 lsp->l = 0;
2019 lsp->m = n;
2020 lsp->s = (char *) gmalloc (n);
2021 }
2022 /* init_Lstring */
2023
2024
2025 Void
2026 de_init_Lstring
2027 #ifdef STDC
2028 (Lstring * lsp)
2029 #else
2030 (lsp)
2031 Lstring * lsp;
2032 #endif
2033 {
2034 lsp->l = 0;
2035 lsp->m = 0;
2036 free (lsp->s);
2037 lsp->s = NULL; /* to be sure */
2038 }
2039 /* de_init_Lstring */
2040
2041
2042 Lstring *
2043 alloc_Lstring
2044 #ifdef STDC
2045 (long int n)
2046 #else
2047 (n)
2048 long int n;
2049 #endif
2050 {
2051 Lstring * lsp;
2052 lsp = (Lstring *) gmalloc (sizeof (Lstring));
2053 init_Lstring (lsp, n);
2054 return (lsp);
2055 }
2056 /* alloc_Lstring */
2057
2058
2059 Void
2060 free_Lstring
2061 #ifdef STDC
2062 (Lstring * lstr)
2063 #else
2064 (lstr)
2065 Lstring * lstr;
2066 #endif
2067 {
2068 free (lstr->s);
2069 free (lstr);
2070 }
2071 /* free_Lstring */
2072
2073
2074 Void
2075 ls_putb
2076 #ifdef STDC
2077 (int ch, Lstring * lstr)
2078 #else
2079 (ch, lstr)
2080 int ch;
2081 Lstring * lstr;
2082 #endif
2083 /* write byte ch into Lstring *lstr */
2084 {
2085 if (lstr->l >= lstr->m)
2086 {
2087 PRINT_PROGNAME;
2088 fprintf (stderr, "(ls_putb) : ERROR : No more room in Lstring.\n");
2089 dexit (1);
2090 }
2091 else
2092 {
2093 lstr->s [(lstr->l)++] = ch;
2094 }
2095 }
2096 /* ls_putb */
2097
2098
2099 long int
2100 get_Lstring
2101 #ifdef STDC
2102 (FILE * dtl, Lstring * lstr)
2103 #else
2104 (dtl, lstr)
2105 FILE * dtl;
2106 Lstring * lstr;
2107 #endif
2108 /* get a string from dtl file, store as an Lstring in *lstr. */
2109 /* lstr must already be allocated and initialised. */
2110 /* return length of Lstring *lstr */
2111 {
2112 U4 nch;
2113 int char_status = CHAR_OK; /* OK so far */
2114
2115 if (debug)
2116 {
2117 PRINT_PROGNAME;
2118 fprintf (stderr, "(get_Lstring) : entering get_Lstring.\n");
2119 }
2120
2121 check_bmes (dtl);
2122
2123 if (debug)
2124 {
2125 PRINT_PROGNAME;
2126 fprintf (stderr,
2127 "(get_Lstring) : string is: \"");
2128 }
2129
2130 for (nch=0; ; nch++)
2131 {
2132 int ch;
2133
2134 char_status = read_string_char (dtl, &ch);
2135
2136 if (char_status == CHAR_FAIL)
2137 {
2138 /* end of dtl file, or reading error */
2139 fprintf (stderr, "\n");
2140 PRINT_PROGNAME;
2141 fprintf (stderr, "(get_Lstring) : DTL FILE ERROR (%s) : ",
2142 dtl_filename);
2143 fprintf (stderr, "cannot read string[");
2144 fprintf (stderr, UF4, nch);
2145 fprintf (stderr, "] from dtl file.\n");
2146 dexit (1);
2147 }
2148
2149 if (debug)
2150 {
2151 fprintf (stderr, "%c", ch);
2152 }
2153
2154 if (char_status == CHAR_EOS)
2155 {
2156 if (ch != EMES_CHAR)
2157 {
2158 PRINT_PROGNAME;
2159 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2160 fprintf (stderr, "char_status = CHAR_FAIL,\n");
2161 fprintf (stderr,
2162 "but ch = %c (char %d) is not EMES_CHAR = %c (char %d)\n",
2163 ch, ch, EMES_CHAR, EMES_CHAR);
2164 dexit (1);
2165 }
2166 (Void) unread_char ();
2167 break; /* end of string */
2168 }
2169 else if (char_status == CHAR_OK)
2170 {
2171 ls_putb (ch, lstr);
2172 }
2173 else
2174 {
2175 PRINT_PROGNAME;
2176 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2177 fprintf (stderr, "char_status = %d is unfamiliar!\n", char_status);
2178 dexit (1);
2179 }
2180 }
2181 /* end for */
2182
2183 if (debug)
2184 {
2185 fprintf (stderr, "\".\n");
2186 }
2187
2188 check_emes (dtl);
2189
2190 if (debug)
2191 {
2192 PRINT_PROGNAME;
2193 fprintf (stderr, "(get_Lstring) : leaving get_Lstring.\n");
2194 }
2195
2196 return (lstr->l);
2197 }
2198 /* get_Lstring */
2199
2200
2201 Void
2202 put_Lstring
2203 #ifdef STDC
2204 (const Lstring * lstr, FILE * dvi)
2205 #else
2206 (str, dvi)
2207 Lstring lstr;
2208 FILE * dvi;
2209 #endif
2210 {
2211 size_t fwret;
2212 fwret = fwrite ((lstr->s), sizeof (char), (size_t) (lstr->l), dvi);
2213
2214 dvi_written += fwret;
2215
2216 if (fwret < lstr->l)
2217 {
2218 PRINT_PROGNAME;
2219 fprintf (stderr,
2220 "(put_Lstring) : DVI File ERROR : not all bytes written ");
2221 fprintf (stderr, "(%ld of %ld).\n",
2222 (long int) fwret, (long int) (lstr->l));
2223 dexit (1);
2224 }
2225 }
2226 /* put_Lstring */
2227
2228
2229 U4
2230 xfer_len_string
2231 #ifdef STDC
2232 (int n, FILE * dtl, FILE * dvi)
2233 #else
2234 (n, dtl, dvi)
2235 int n;
2236 FILE * dtl;
2237 FILE * dvi;
2238 #endif
2239 /* transfer (length and) quoted string from dtl to dvi file, */
2240 /* return number of bytes written to dvi file. */
2241 {
2242 U4 k, k2;
2243 Lstring lstr;
2244
2245 if (debug)
2246 {
2247 PRINT_PROGNAME;
2248 fprintf (stderr, "(xfer_len_string) : entering xfer_len_string.\n");
2249 }
2250
2251 init_Lstring (&lstr, LSIZE);
2252
2253 /* k[n] : length of special string */
2254
2255 k = get_unsigned (dtl);
2256
2257 if (debug)
2258 {
2259 PRINT_PROGNAME;
2260 fprintf (stderr, "(xfer_len_string) : string's nominal length k = ");
2261 fprintf (stderr, UF4, k);
2262 fprintf (stderr, " characters.\n");
2263 }
2264
2265 k2 = get_Lstring (dtl, &lstr);
2266
2267 if (k2 != k)
2268 {
2269 PRINT_PROGNAME;
2270 fprintf (stderr, "(xfer_len_string) : WARNING : string length (");
2271 fprintf (stderr, UF4, k);
2272 fprintf (stderr, ") in DTL file is wrong\n");
2273 fprintf (stderr, "Writing correct value (");
2274 fprintf (stderr, UF4, k2);
2275 fprintf (stderr, ") to DVI file\n");
2276 }
2277
2278 put_unsigned (n, k2, dvi);
2279
2280 put_Lstring (&lstr, dvi);
2281
2282 if (debug)
2283 {
2284 PRINT_PROGNAME;
2285 fprintf (stderr, "(xfer_len_string) : leaving xfer_len_string.\n");
2286 }
2287
2288 de_init_Lstring (&lstr);
2289
2290 return (n + k2);
2291 }
2292 /* xfer_len_string */
2293
2294
2295 S4
2296 xfer_bop_address
2297 #ifdef STDC
2298 (FILE * dtl, FILE * dvi)
2299 #else
2300 (dtl, dvi)
2301 FILE * dtl;
2302 FILE * dvi;
2303 #endif
2304 /* translate signed 4-byte bop address from dtl to dvi file. */
2305 /* return value of bop address written to DVI file */
2306 {
2307 S4 snum = 0; /* at most this space needed for byte address */
2308 int nconv = 0; /* number of arguments converted by sscanf */
2309 static Token token = ""; /* DTL token */
2310
2311 read_token (dtl, token);
2312
2313 nconv = sscanf (token, SF4, &snum);
2314
2315 if (nconv != 1)
2316 {
2317 PRINT_PROGNAME;
2318 fprintf (stderr, "(xfer_bop_address) : DTL FILE ERROR (%s) : ",
2319 dtl_filename);
2320 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2321 dexit (1);
2322 }
2323
2324 if (snum != last_bop_address)
2325 {
2326 PRINT_PROGNAME;
2327 fprintf (stderr, "(xfer_bop_address) : WARNING : byte address (");
2328 fprintf (stderr, WF, snum);
2329 fprintf (stderr, ")\n");
2330 fprintf (stderr, "for previous bop in DTL file is wrong\n");
2331 fprintf (stderr, "Writing correct value (");
2332 fprintf (stderr, WF, last_bop_address);
2333 fprintf (stderr, ") to DVI file\n");
2334 }
2335
2336 put_signed (4, last_bop_address, dvi);
2337
2338 return last_bop_address;
2339 }
2340 /* xfer_bop_address */
2341
2342
2343 S4
2344 xfer_postamble_address
2345 #ifdef STDC
2346 (FILE * dtl, FILE * dvi)
2347 #else
2348 (dtl, dvi)
2349 FILE * dtl;
2350 FILE * dvi;
2351 #endif
2352 /* translate signed 4-byte postamble address from dtl to dvi file. */
2353 /* return value of postamble address written to DVI file */
2354 {
2355 S4 snum = 0; /* at most this space needed for byte address */
2356 int nconv = 0; /* number of arguments converted by sscanf */
2357 static Token token = ""; /* DTL token */
2358
2359 read_token (dtl, token);
2360
2361 nconv = sscanf (token, SF4, &snum);
2362
2363 if (nconv != 1)
2364 {
2365 PRINT_PROGNAME;
2366 fprintf (stderr, "(xfer_postamble_address) : DTL FILE ERROR (%s) : ",
2367 dtl_filename);
2368 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2369 dexit (1);
2370 }
2371
2372 if (snum != postamble_address)
2373 {
2374 PRINT_PROGNAME;
2375 fprintf (stderr, "(xfer_postamble_address) : WARNING : byte address (");
2376 fprintf (stderr, WF, snum);
2377 fprintf (stderr, ")\n");
2378 fprintf (stderr, "for postamble in DTL file is wrong\n");
2379 fprintf (stderr, "Writing correct value (");
2380 fprintf (stderr, WF, postamble_address);
2381 fprintf (stderr, ") to DVI file\n");
2382 }
2383
2384 put_signed (4, postamble_address, dvi);
2385
2386 return postamble_address;
2387 }
2388 /* xfer_postamble_address */
2389
2390
2391 int
2392 put_table
2393 #ifdef STDC
2394 (op_table table, int opcode, FILE * dtl, FILE * dvi)
2395 #else
2396 (table, opcode, dtl, dvi)
2397 op_table table;
2398 int opcode;
2399 FILE * dtl;
2400 FILE * dvi;
2401 #endif
2402 {
2403 /* table: {char * name; int first, last; op_info * list; }; */
2404 /* op_info: {int code; char * name; int nargs; char * args; }; */
2405
2406 op_info op; /* entry in table */
2407 int i;
2408 int pos; /* current position in string being scanned */
2409 static String args = "";
2410
2411 /* Defensive programming. */
2412 if (opcode < table.first || opcode > table.last)
2413 {
2414 PRINT_PROGNAME;
2415 fprintf (stderr,
2416 "(put_table) : DTL FILE (OR INTERNAL) ERROR : opcode %d ", opcode);
2417 fprintf (stderr, "is outside table %s [ %d to %d ] !\n",
2418 table.name, table.first, table.last);
2419 dexit (1);
2420 }
2421
2422 op = table.list [ opcode - table.first ];
2423
2424 /* More defensive programming. */
2425 if (opcode != op.code)
2426 {
2427 PRINT_PROGNAME;
2428 fprintf (stderr,
2429 "(put_table) : INTERNAL ERROR : opcode %d for command \"%s\"",
2430 opcode, op.name);
2431 fprintf (stderr, " faulty in table \"%s\".\n", table.name);
2432 dexit (1);
2433 }
2434
2435 /* process all the arguments, according to size and sign */
2436
2437 strncpy (args, op.args, MAXSTRLEN);
2438
2439 pos = 0;
2440 for (i=0; i < op.nargs; i++)
2441 {
2442 int argtype = 0;
2443 int nscan = 0; /* number of bytes read by sscanf */
2444 int nconv = 0; /* number of sscanf arguments converted & assigned */
2445
2446 /* sscanf() does NOT advance over its input: */
2447 /* C strings lack internal state information, which C files have. */
2448 /* On Sun/OS, sscanf calls ungetc on the string it reads, */
2449 /* which therefore has to be writable. */
2450
2451 nconv = sscanf (args + pos, "%d%n", &argtype, &nscan);
2452
2453 if (nconv < 1 || nscan < 1)
2454 {
2455 PRINT_PROGNAME;
2456 fprintf (stderr,
2457 "(put_table) : INTERNAL ERROR : internal read of table %s failed!\n",
2458 table.name);
2459 dexit (1);
2460 }
2461
2462 pos += nscan;
2463
2464 if (argtype < 0)
2465 xfer_signed (-argtype, dtl, dvi);
2466 else
2467 xfer_unsigned (argtype, dtl, dvi);
2468 }
2469 /* end for */
2470
2471 return 1; /* OK */
2472 }
2473 /* put_table */
2474
2475
2476 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
2477
2478
2479 U4
2480 special
2481 #ifdef STDC
2482 (FILE * dtl, FILE * dvi, int n)
2483 #else
2484 (dtl, dvi, n)
2485 FILE * dtl;
2486 FILE * dvi;
2487 int n;
2488 #endif
2489 /* read special (1 <= n <= 4 byte) data from dtl, and write in dvi */
2490 /* return number of bytes written */
2491 {
2492 U4 nk;
2493
2494 if (debug)
2495 {
2496 PRINT_PROGNAME;
2497 fprintf (stderr, "(special) : entering special.\n");
2498 }
2499
2500 if (n < 1 || n > 4)
2501 {
2502 PRINT_PROGNAME;
2503 fprintf (stderr, "(special) : DTL FILE ERROR (%s) : special %d, ",
2504 dtl_filename, n);
2505 fprintf (stderr, "range is 1 to 4.\n");
2506 dexit (1);
2507 }
2508
2509 /* k[n] : length of special string */
2510 /* x[k] : special string */
2511 /* nk = n + k */
2512 nk = xfer_len_string (n, dtl, dvi);
2513
2514 if (debug)
2515 {
2516 PRINT_PROGNAME;
2517 fprintf (stderr, "(special) : leaving special.\n");
2518 }
2519
2520 return (nk);
2521 }
2522 /* special */
2523
2524
2525 int
2526 fontdef
2527 #ifdef STDC
2528 (FILE * dtl, FILE * dvi, int suffix)
2529 #else
2530 (dtl, dvi, suffix)
2531 FILE * dtl;
2532 FILE * dvi;
2533 int suffix;
2534 #endif
2535 /* read fontdef fnt_def1 .. fnt_def4 from dtl, and write in dvi */
2536 /* suffix is the fontdef suffix : 1 to 4 */
2537 /* return number of bytes written */
2538 {
2539 U4 a, l, a2, l2;
2540 U4 k;
2541 Lstring lstr1, lstr2;
2542
2543 if (debug)
2544 {
2545 PRINT_PROGNAME;
2546 fprintf (stderr, "(fontdef) : entering fontdef.\n");
2547 }
2548
2549 if (suffix < 1 || suffix > 4)
2550 {
2551 PRINT_PROGNAME;
2552 fprintf (stderr, "(fontdef) : DTL FILE ERROR (%s) : ",
2553 dtl_filename);
2554 fprintf (stderr, "font def %d, but range is 1 to 4.\n", suffix);
2555 dexit (1);
2556 }
2557
2558 init_Lstring (&lstr1, LSIZE);
2559 init_Lstring (&lstr2, LSIZE);
2560
2561 if (debug)
2562 {
2563 PRINT_PROGNAME;
2564 fprintf (stderr, "(fontdef) : about to read font number.\n");
2565 }
2566
2567 /* k[suffix] : font number */
2568 if (suffix == 4)
2569 k = xfer_signed (suffix, dtl, dvi);
2570 else
2571 k = xfer_unsigned (suffix, dtl, dvi);
2572
2573 if (debug)
2574 {
2575 PRINT_PROGNAME;
2576 fprintf (stderr, "(fontdef) : font ");
2577 fprintf (stderr, UF4, k);
2578 fprintf (stderr, ".\n");
2579 }
2580
2581 #ifdef HEX_CHECKSUM
2582 /* c[4] : (hexadecimal) checksum : I (gt) would prefer this */
2583 xfer_hex (4, dtl, dvi);
2584 #else /* NOT HEX_CHECKSUM */
2585 /* c[4] : checksum (octal, for comparison with tftopl's .pl file) */
2586 xfer_oct (4, dtl, dvi);
2587 #endif
2588
2589 /* s[4] */
2590 xfer_unsigned (4, dtl, dvi);
2591
2592 /* d[4] */
2593 xfer_unsigned (4, dtl, dvi);
2594
2595 /* If DTL file's edited, a and l may be wrong. */
2596
2597 /* a[1] : length of font `area' (directory) portion of pathname string */
2598 a = get_unsigned (dtl);
2599
2600 /* l[1] : length of font portion of pathname string */
2601 l = get_unsigned (dtl);
2602
2603 /* n[a+l] : font pathname string <= area + font */
2604
2605 a2 = get_Lstring (dtl, &lstr1);
2606
2607 if (a2 != a)
2608 {
2609 PRINT_PROGNAME;
2610 fprintf (stderr, "(fontdef) : WARNING : font area string's length (");
2611 fprintf (stderr, UF4, a);
2612 fprintf (stderr, ") in DTL file is wrong\n");
2613 fprintf (stderr, "Writing correct value (");
2614 fprintf (stderr, UF4, a2);
2615 fprintf (stderr, ") to DVI file\n");
2616 }
2617
2618 put_unsigned (1, a2, dvi);
2619
2620 l2 = get_Lstring (dtl, &lstr2);
2621
2622 if (l2 != l)
2623 {
2624 PRINT_PROGNAME;
2625 fprintf (stderr, "(fontdef) : WARNING : font string's length (");
2626 fprintf (stderr, UF4, l);
2627 fprintf (stderr, ") in DTL file is wrong\n");
2628 fprintf (stderr, "Writing correct value (");
2629 fprintf (stderr, UF4, l2);
2630 fprintf (stderr, ") to DVI file\n");
2631 }
2632
2633 put_unsigned (1, l2, dvi);
2634
2635 put_Lstring (&lstr1, dvi);
2636 put_Lstring (&lstr2, dvi);
2637
2638 de_init_Lstring (&lstr2);
2639 de_init_Lstring (&lstr1);
2640
2641 if (debug)
2642 {
2643 PRINT_PROGNAME;
2644 fprintf (stderr, "(fontdef) : leaving fontdef.\n");
2645 }
2646
2647 return (suffix + 4*4 + 2*1 + a2 + l2);
2648 }
2649 /* fontdef */
2650
2651
2652 U4
2653 preamble
2654 #ifdef STDC
2655 (FILE * dtl, FILE * dvi)
2656 #else
2657 (dtl, dvi)
2658 FILE * dtl;
2659 FILE * dvi;
2660 #endif
2661 /* read preamble from dtl, and write in dvi */
2662 /* return number of bytes written */
2663 {
2664 U4 k1;
2665
2666 if (debug)
2667 {
2668 PRINT_PROGNAME;
2669 fprintf (stderr, "(preamble) : entering preamble.\n");
2670 }
2671
2672 /* i[1] */
2673 xfer_unsigned (1, dtl, dvi);
2674
2675 /* num[4] */
2676 xfer_unsigned (4, dtl, dvi);
2677
2678 /* den[4] */
2679 xfer_unsigned (4, dtl, dvi);
2680
2681 /* mag[4] */
2682 xfer_unsigned (4, dtl, dvi);
2683
2684 /* k[1] : length of comment */
2685 /* x[k] : comment string */
2686 /* k1 = 1 + k */
2687 k1 = xfer_len_string (1, dtl, dvi);
2688
2689 if (debug)
2690 {
2691 PRINT_PROGNAME;
2692 fprintf (stderr, "(preamble) : leaving preamble.\n");
2693 }
2694
2695 return (1 + 3*4 + k1);
2696 }
2697 /* preamble */
2698
2699
2700 int
2701 postamble
2702 #ifdef STDC
2703 (FILE * dtl, FILE * dvi)
2704 #else
2705 (dtl, dvi)
2706 FILE * dtl;
2707 FILE * dvi;
2708 #endif
2709 /* read postamble from dtl, and write in dvi */
2710 /* return number of bytes written */
2711 {
2712 postamble_address = dvi_written - 1;
2713
2714 /* p[4] : DVI address of previous bop command */
2715 /* --- unsigned? --- or signed, as I assume? */
2716 /* For, surely p should be -1 if the DVI file has NO bop? */
2717 xfer_bop_address (dtl, dvi);
2718
2719 /* num[4] */
2720 xfer_unsigned (4, dtl, dvi);
2721
2722 /* den[4] */
2723 xfer_unsigned (4, dtl, dvi);
2724
2725 /* mag[4] */
2726 xfer_unsigned (4, dtl, dvi);
2727
2728 /* l[4] */
2729 xfer_unsigned (4, dtl, dvi);
2730
2731 /* u[4] */
2732 xfer_unsigned (4, dtl, dvi);
2733
2734 /* s[2] */
2735 xfer_unsigned (2, dtl, dvi);
2736
2737 /* t[2] */
2738 xfer_unsigned (2, dtl, dvi);
2739
2740 return (6*4 + 2*2);
2741 }
2742 /* postamble */
2743
2744
2745 int
2746 post_post
2747 #ifdef STDC
2748 (FILE * dtl, FILE * dvi)
2749 #else
2750 (dtl, dvi)
2751 FILE * dtl;
2752 FILE * dvi;
2753 #endif
2754 /* read post_post from dtl, and write in dvi */
2755 /* return number of bytes written */
2756 {
2757 /* hope I'm writing the "223" bytes in an 8-bit clean way */
2758 int n223 = 0; /* number of "223" bytes in final padding */
2759
2760 /* q[4] : DVI address of post command */
2761 /* --- unsigned? --- or signed, as I assume? */
2762 /* what happens if there is NO postamble command? */
2763 /* shouldn't q be -1 then? */
2764
2765 xfer_postamble_address (dtl, dvi);
2766
2767 /* i[1] : DVI identification byte = 2 */
2768 xfer_unsigned (1, dtl, dvi);
2769
2770 for (n223 = 0; true; n223++)
2771 {
2772 COUNT nread = 0; /* number of DTL bytes read by read_token */
2773 static Token token;
2774
2775 strcpy (token, "");
2776
2777 nread = read_token (dtl, token);
2778
2779 /* check whether end of dtl file */
2780 if (nread == 0)
2781 {
2782 if (group)
2783 {
2784 /* dtl file shouldn't end before an ECOM */
2785 PRINT_PROGNAME;
2786 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2787 dtl_filename);
2788 fprintf (stderr, "premature end of DTL file!\n");
2789 fprintf (stderr,
2790 "%d complete iterations of \"padding byte\" loop;\n", n223);
2791 fprintf (stderr, "troublesome token = \"%s\"\n", token);
2792 dexit (1);
2793 }
2794 /* leave the "223" loop */
2795 break;
2796 }
2797 else if (strcmp (token, "223") == 0)
2798 {
2799 /* token is a "223" padding byte */
2800 /* loop again */
2801 }
2802 else
2803 {
2804 /* read a non-empty token that wasn't "223" */
2805 (Void) unread_char ();
2806 if (group)
2807 {
2808 if (strcmp (token, ECOM) == 0)
2809 {
2810 /* end of DTL's post_post command */
2811 }
2812 else
2813 {
2814 /* error : expected end of post_post */
2815 PRINT_PROGNAME;
2816 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2817 dtl_filename);
2818 fprintf (stderr, "token \"%s\" should be ECOM (\"%s\")\n",
2819 token, ECOM);
2820 dexit (1);
2821 }
2822 }
2823 /* leave the "223" loop */
2824 break;
2825 }
2826 }
2827 /* end for */
2828
2829 if (n223 < 4)
2830 {
2831 PRINT_PROGNAME;
2832 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : \n",
2833 dtl_filename);
2834 fprintf (stderr, "fewer than four `223' padding bytes.\n");
2835 fprintf (stderr, "Will write at least four `223' padding bytes.\n");
2836 }
2837
2838 /* check whether the DVI file size is a multiple of 4 bytes */
2839 if ((dvi_written + n223) % 4 != 0)
2840 {
2841 PRINT_PROGNAME;
2842 fprintf (stderr, "(post_post) : WARNING : \n");
2843 fprintf (stderr, "DVI size ");
2844 fprintf (stderr, WF, dvi_written);
2845 fprintf (stderr, " (bytes) wouldn't be a multiple of 4 !\n");
2846 fprintf (stderr,
2847 "Will write (at least four) `223' padding bytes until it is.\n");
2848 }
2849
2850 /* final padding of DVI file by "223" bytes to a multiple of 4 bytes, */
2851 /* with at least 4 bytes */
2852
2853 for (n223 = 0; (n223 < 4) || (dvi_written % 4 != 0); n223++)
2854 {
2855 /* add a "223" padding byte */
2856 put_byte (223, dvi);
2857 }
2858
2859 return (4 + 1 + n223);
2860 }
2861 /* post_post */
2862
2863
2864 /* end of dt2dv.c */