1 /* dt2dv - convert human-readable "DTL" file to DVI format
2 - this is intended to invert dv2dt version 0.6.0
4 This file is public domain.
5 Originally written 1995, Geoffrey Tobin.
6 The author has expressed the hope that any modification will retain enough content to remain useful. He would also appreciate being acknowledged as the original author in the documentation.
7 This declaration added 2008/11/14 by Clea F. Rees with the permission of Geoffrey Tobin.
10 - Geoffrey Tobin G.Tobin@ee.latrobe.edu.au
11 - fixes: Michal Tomczak-Jaegermann ntomczak@vm.ucs.ualberta.ca
12 Nelson H. F. Beebe beebe@math.utah.edu
13 Angus Leeming leeming@lyx.org: Enable dt2dv to handle .dvi files
14 containing strings longer than 1024 chars.
15 Enrico Forestieri forenr@lyx.org: handle non-ASCII characters.
16 Juergen Spitzmueller spitz@lyx.org: fix compiler warnings
17 - Reference: "The DVI Driver Standard, Level 0",
18 by The TUG DVI Driver Standards Committee.
19 Appendix A, "Device-Independent File Format".
22 /* unix version; read from stdin, write to stdout, by default. */
33 /* by default, read and write regular files */
37 /* maximum number of characters in a DTL input line */
43 COUNT num; /* current line number */
44 size_t max; /* capacity of buf */
45 U4 wrote; /* number of characters written into buf */
46 size_t read; /* position in buf of next character to read from buf */
47 char * buf; /* line buffer */
50 char linebuf[MAXLINE+1];
52 Line dtl_line = {0, 0, 0, MAXLINE, linebuf};
54 /* a DTL token either is:
55 a quoted string (admitting an escape character),
56 or BCOM (if that is a nonempty string),
57 or ECOM (if that is a nonempty string),
58 or a string _not_ including ECOM_CHAR or space.
61 /* maximum expected length of a DTL token */
63 typedef char Token[MAXTOKLEN+1];
65 typedef unsigned char Byte;
71 /* command prefixes */
77 Byte first_suffix, last_suffix;
80 CmdPrefix cmd_prefixes [] =
82 {0, SETCHAR, true, 0, 127},
83 {128, SET, true, 1, 4},
84 {132, SETRULE, false, 0, 0},
85 {133, PUT, true, 1, 4},
86 {137, PUTRULE, false, 0, 0},
87 {138, NOP, false, 0, 0},
88 {139, BOP, false, 0, 0},
89 {140, EOP, false, 0, 0},
90 {141, PUSH, false, 0, 0},
91 {142, POP, false, 0, 0},
92 {143, RIGHT, true, 1, 4},
95 {157, DOWN, true, 1, 4},
98 {171, FONTNUM, true, 0, 63},
99 {235, FONT, true, 1, 4},
100 {239, SPECIAL, true, 1, 4},
101 {243, FONTDEF, true, 1, 4},
102 {247, PRE, false, 0, 0},
103 {248, POST, false, 0, 0},
104 {249, POSTPOST, false, 0, 0},
105 {250, OPCODE, true, 250, 255}
109 /* Number of DVI commands, including those officially undefined */
112 /* table of command name (string) pointers */
113 typedef char * CmdTable [NCMDS];
115 /* initially all command name pointers are NULL */
118 /* operation's opcode, name, number of args, string of arguments. */
127 /* name of table, first opcode, last opcode, pointer to opcode info. */
136 /* Table for opcodes 128 to 170 inclusive. */
138 op_info op_info_128_170 [] =
143 {131, SET4, 1, "-4"},
144 {132, SETRULE, 2, "-4 -4"},
148 {136, PUT4, 1, "-4"},
149 {137, PUTRULE, 2, "-4 -4"},
151 /* bop: not counting last argument, a signed address: */
152 {139, BOP, 10, "-4 -4 -4 -4 -4 -4 -4 -4 -4 -4"},
156 {143, RIGHT1, 1, "-1"},
157 {144, RIGHT2, 1, "-2"},
158 {145, RIGHT3, 1, "-3"},
159 {146, RIGHT4, 1, "-4"},
170 {157, DOWN1, 1, "-1"},
171 {158, DOWN2, 1, "-2"},
172 {159, DOWN3, 1, "-3"},
173 {160, DOWN4, 1, "-4"},
185 /* op_info op_info_128_170 [] */
187 op_table op_128_170 = {"op_128_170", 128, 170, op_info_128_170};
189 /* Table for fnt1 to fnt4 (opcodes 235 to 238) inclusive. */
193 {235, FONT1, 1, "1"},
194 {236, FONT2, 1, "2"},
195 {237, FONT3, 1, "3"},
196 {238, FONT4, 1, "-4"}
198 /* op_info fnt_n [] */
200 op_table fnt = {FONT, 235, 238, fnt_n};
203 /* Function prototypes */
205 Void mem_viol ARGS((int sig));
206 Void give_help (VOID);
207 int parse ARGS((char * s));
208 Void process ARGS((char * s));
211 Void dtl_stdin (VOID);
212 Void dvi_stdout (VOID);
214 int open_dtl ARGS((char * dtl_file, FILE ** pdtl));
215 int open_dvi ARGS((char * dvi_file, FILE ** pdvi));
217 int dt2dv ARGS((FILE * dtl, FILE * dvi));
219 Void * gmalloc ARGS((long int size));
222 Void dexit ARGS((int n));
224 int cons_cmds ARGS((int nprefixes, CmdPrefix * prefix, CmdTable cmds));
225 Void free_cmds ARGS((CmdTable cmd_table));
227 int get_line ARGS((FILE * fp, Line * line, int max));
228 int read_line_char ARGS((FILE * fp, int * ch));
229 int read_char ARGS((FILE * fp, int * ch));
230 int unread_char (VOID);
231 int read_string_char ARGS((FILE * fp, int * ch));
233 COUNT read_variety ARGS((FILE * dtl));
234 COUNT read_token ARGS((FILE * dtl, char * token));
235 COUNT skip_space ARGS((FILE * fp, int * ch));
236 COUNT read_misc ARGS((FILE * fp, Token token));
237 COUNT read_mes ARGS((FILE * fp, char * token));
239 int find_command ARGS((char * command, int * opcode));
240 int xfer_args ARGS((FILE * dtl, FILE * dvi, int opcode));
242 int set_seq ARGS((FILE * dtl, FILE * dvi));
244 int check_byte ARGS((int byte));
245 int put_byte ARGS((int onebyte, FILE * dvi));
247 U4 xfer_hex ARGS((int n, FILE * dtl, FILE * dvi));
248 U4 xfer_oct ARGS((int n, FILE * dtl, FILE * dvi));
249 U4 xfer_unsigned ARGS((int n, FILE * dtl, FILE * dvi));
250 S4 xfer_signed ARGS((int n, FILE * dtl, FILE * dvi));
252 int check_bmes ARGS((FILE * dtl));
253 int check_emes ARGS((FILE * dtl));
255 Void init_Lstring ARGS((Lstring * lsp, long int n));
256 Void de_init_Lstring ARGS((Lstring * lsp));
257 Lstring * alloc_Lstring ARGS((long int n));
258 Void free_Lstring ARGS((Lstring * lstr));
259 Void ls_putb ARGS((int ch, Lstring * lstr));
261 S4 get_Lstring ARGS((FILE * dtl, Lstring * lstr));
262 Void put_Lstring ARGS((const Lstring * lstr, FILE * dvi));
263 U4 xfer_len_string ARGS((int n, FILE * dtl, FILE * dvi));
265 U4 get_unsigned ARGS((FILE * dtl));
266 S4 get_signed ARGS((FILE * dtl));
268 int put_unsigned ARGS((int n, U4 unum, FILE * dvi));
269 int put_signed ARGS((int n, S4 snum, FILE * dvi));
271 S4 xfer_bop_address ARGS((FILE * dtl, FILE * dvi));
272 S4 xfer_postamble_address ARGS((FILE * dtl, FILE * dvi));
274 int put_table ARGS((op_table table, int opcode, FILE * dtl, FILE * dvi));
276 U4 special ARGS((FILE * dtl, FILE * dvi, int n));
277 int fontdef ARGS((FILE * dtl, FILE * dvi, int n));
279 U4 preamble ARGS((FILE * dtl, FILE * dvi));
280 int postamble ARGS((FILE * dtl, FILE * dvi));
281 int post_post ARGS((FILE * dtl, FILE * dvi));
286 char * keyword; /* command line option keyword */
287 int * p_var; /* pointer to option variable */
288 char * desc; /* description of keyword and value */
289 Void (* p_fn) (VOID); /* pointer to function called when option is set */
294 {"-debug", &debug, "detailed debugging", no_op},
295 {"-group", &group, "each DTL command is in parentheses", no_op},
296 {"-si", &rd_stdin, "read all DTL commands from standard input", dtl_stdin},
297 {"-so", &wr_stdout, "write all DVI commands to standard output", dvi_stdout},
298 {NULL, NULL, NULL, NULL}
302 char * progname = ""; /* intended for name of this program */
303 int nfile = 0; /* number of filename arguments on the command line */
305 #define PRINT_PROGNAME fprintf (stderr, "%s ", progname)
307 /* Harbison & Steele (1991) warn that some C implementations */
308 /* of free() do not treat NULL pointers correctly. */
309 #define gfree(p) {if (p) free (p);}
312 FILE * dtl_fp = NULL;
313 FILE * dvi_fp = NULL;
315 char * dtl_filename = "";
316 char * dvi_filename = "";
322 (int argc, char * argv[])
329 Void (*handler) ARGS((int)); /* Previous signal handler */
332 progname = argv[0]; /* name of this program */
334 /* memory violation signal handler */
336 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
349 #define C_LEVEL "non-"
350 #endif /* NOT STDC */
352 /* message about program and compiler */
353 /* NB: LTU EE's Sun/OS library is BSD, even though gcc 2.2.2 is ANSI */
355 fprintf (stderr, "\n");
357 "Program \"%s\" version %s compiled %s %s in %sstandard C.\n",
358 progname, VERSION, __DATE__, __TIME__, C_LEVEL);
360 /* interpret command line arguments */
363 dtl_fp = dvi_fp = NULL;
364 dtl_filename = dvi_filename = "";
366 for (i=1; i < argc; i++)
368 /* parse options, followed by any explicit filenames */
372 if (nfile != 2) /* not exactly two files specified, so give help */
376 dt2dv (dtl_fp, dvi_fp);
392 Void (* handler) ARGS((int));
393 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
398 "(mem_viol) : called with wrong signal!\n");
401 fprintf (stderr, "(mem_viol) : RUNTIME MEMORY ERROR : memory violation, ");
402 fprintf (stderr, "dtl line >= ");
403 fprintf (stderr, WF, dtl_line.num);
404 fprintf (stderr, "\n");
415 fprintf (stderr, "usage: ");
417 fprintf (stderr, "[options] dtl_file dvi_file");
418 fprintf (stderr, "\n");
419 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
421 fprintf (stderr, " ");
422 fprintf (stderr, "[%s]", keyword);
423 fprintf (stderr, " ");
424 fprintf (stderr, "%s", opts[i].desc);
425 fprintf (stderr, "\n");
427 fprintf (stderr, "Messages, like this one, go to stderr.\n");
437 Void dtl_stdin (VOID)
439 extern FILE * dtl_fp;
443 dtl_filename = "Standard Input";
447 Void dvi_stdout (VOID)
449 extern FILE * dvi_fp;
452 /* ! Perilous to monitors! */
454 dvi_filename = "Standard Output";
467 /* parse one command-line argument, `s' */
471 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
473 if (strncmp (s, keyword, strlen (keyword)) == 0)
476 (*(opts[i].p_var)) = 1; /* turn option on */
477 if ((pfn = opts[i].p_fn) != NULL)
478 (*pfn) (); /* call option function */
482 /* reached here, so not an option: assume it's a filename */
492 (char * dtl_file, FILE ** pdtl)
498 /* I: dtl_file; I: pdtl; O: *pdtl. */
500 extern char * dtl_filename;
502 dtl_filename = dtl_file;
504 if (dtl_filename == NULL)
508 "(open_dtl) : INTERNAL ERROR : dtl file's name is NULL.\n");
516 "(open_dtl) : INTERNAL ERROR : address of dtl variable is NULL.\n");
520 *pdtl = fopen (dtl_file, "r");
526 "(open_dtl) : DTL FILE ERROR : Cannot open \"%s\" for text reading.\n",
539 (char * dvi_file, FILE ** pdvi)
545 /* I: dvi_file; I: pdvi; O: *pdvi. */
547 extern char * dvi_filename;
549 dvi_filename = dvi_file;
551 if (dvi_filename == NULL)
555 "(open_dvi) : INTERNAL ERROR : dvi file's name is NULL.\n");
563 "(open_dvi) : INTERNAL ERROR : address of dvi variable is NULL.\n");
567 *pdvi = fopen (dvi_file, "wb");
573 "(open_dvi) : DVI FILE ERROR : Cannot open \"%s\" for binary writing.\n",
592 extern FILE * dtl_fp, * dvi_fp;
594 if (dtl_fp == NULL) /* first filename assumed to be DTL input */
596 open_dtl (s, &dtl_fp);
598 else if (dvi_fp == NULL) /* second filename assumed to be DVI output */
600 open_dvi (s, &dvi_fp);
605 fprintf (stderr, "(process) : at most two filenames allowed.\n");
613 COUNT dtl_read = 0; /* bytes read from dtl file */
614 COUNT dvi_written = 0; /* bytes written to dvi file */
615 word_t last_bop_address = -1; /* byte address of last bop; first bop uses -1 */
616 word_t postamble_address = -1; /* byte address of postamble */
617 COUNT ncom = 0; /* commands successfully read and interpreted from dtl file */
618 COUNT com_read = 0; /* bytes read in current (command and arguments), */
619 /* since and including the opening BCOM_CHAR, if any */
625 (int byte, FILE * dvi)
631 /* write byte into dvi file */
634 /* if (fprintf (dvi, "%c", byte) != 1) */
635 if (fprintf (dvi, "%c", byte) < 0)
639 "(put_byte) : DVI FILE ERROR (%s) : cannot write to dvi file.\n",
652 (FILE * dtl, FILE * dvi)
659 int nprefixes = 0; /* number of prefixes in cmd_prefixes[] list. */
660 static Token dtl_cmd = ""; /* DTL command name */
661 COUNT nread = 0; /* number of bytes read by a function from dtl file. */
663 nprefixes = sizeof (cmd_prefixes) / sizeof (CmdPrefix);
665 /* Construct array of all NCMDS == 256 DTL commands */
667 (Void) cons_cmds (nprefixes, cmd_prefixes, cmd_table);
669 /* DTL commands have the form "[ ]*command arg ... arg[ ]*", */
670 /* possibly enclosed in a BCOM, ECOM pair, */
671 /* and are separated by optional whitespace, typically newlines. */
672 /* That is, each command and its arguments are parenthesised, */
673 /* with optional spaces after the BCOM and before the ECOM, if any. */
675 /* dt2dv is now at the very start of the DTL file */
680 /* The very first thing should be the "variety" signature */
682 nread = read_variety (dtl);
684 /* while not end of dtl file or reading error, */
685 /* read, interpret, and write commands */
696 static Token token = ""; /* DTL token */
697 nread = read_token (dtl, token);
698 /* test for end of input, or reading error */
699 if (strlen (token) == 0)
704 fprintf (stderr, "(dt2dv) : end of input, or reading error.\n");
708 /* test whether this command begins correctly */
709 else if (strcmp (token, BCOM) != 0)
712 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
713 fprintf (stderr, "command must begin with \"%s\", ", BCOM);
714 fprintf (stderr, "not `%c' (char %d).\n", token[0], token[0]);
720 /* read the command name */
721 nread = read_token (dtl, dtl_cmd);
722 /* test for end of input, or reading error */
723 if (strlen (dtl_cmd) == 0)
729 "(dt2dv) : end of input, or reading error.\n");
738 fprintf (stderr, "(dt2dv) : command ");
739 fprintf (stderr, WF, ncom);
740 fprintf (stderr, " = \"%s\".\n", dtl_cmd);
743 /* find opcode for this command */
744 if (find_command (dtl_cmd, &opcode) == 1)
746 /* write the opcode, if we can */
747 put_byte (opcode, dvi);
749 /* treat the arguments, if any */
750 xfer_args (dtl, dvi, opcode);
752 else if (dtl_cmd[0] == BSEQ_CHAR)
754 /* sequence of font characters for SETCHAR */
761 "(dt2dv) : DTL FILE ERROR (%s) : unknown command \"%s\".\n",
762 dtl_filename, dtl_cmd);
769 /* seek ECOM after command's last argument and optional whitespace */
770 static Token token = ""; /* DTL token */
771 nread = read_token (dtl, token);
772 /* test for end of input, or reading error */
773 if (strlen (token) == 0)
779 "(dt2dv) : end of input, or reading error.\n");
783 if (strcmp (token, ECOM) != 0)
786 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
787 fprintf (stderr, "ECOM (\"%s\") expected, not `%c' (char %d).\n",
788 ECOM, token[0], token[0]);
794 ++ ncom; /* one more command successfully read and interpreted */
799 fprintf (stderr, "(dt2dv) :\n");
800 fprintf (stderr, "Read (from file \"%s\") ", dtl_filename);
801 fprintf (stderr, WF, dtl_read);
802 fprintf (stderr, " DTL bytes (");
803 fprintf (stderr, UF4, dtl_line.num);
804 fprintf (stderr, " lines);\n");
805 fprintf (stderr, "wrote (to file \"%s\") ", dvi_filename);
806 fprintf (stderr, WF, dvi_written);
807 fprintf (stderr, " DVI bytes;\n");
808 fprintf (stderr, "completely interpreted ");
809 fprintf (stderr, WF, ncom);
810 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
811 fprintf (stderr, "\n");
813 (Void) free_cmds (cmd_table);
833 fprintf (stderr, "(gmalloc) : INTERNAL ERROR : ");
835 "unreasonable request to malloc %ld bytes\n",
839 p = (Void *) malloc ((size_t) size);
843 fprintf (stderr, "(gmalloc) : MEMORY ALLOCATION ERROR : ");
845 "operating system failed to malloc %ld bytes\n",
858 fprintf (stderr, "(dinfo) : ");
859 fprintf (stderr, "Current DTL input line ");
860 fprintf (stderr, UF4, dtl_line.num);
861 fprintf (stderr, " :\n");
862 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
863 fprintf (stderr, "Read ");
864 fprintf (stderr, WF, dtl_read);
865 fprintf (stderr, " DTL bytes (");
866 fprintf (stderr, WF, com_read);
867 fprintf (stderr, " in current command), wrote ");
868 fprintf (stderr, WF, dvi_written);
869 fprintf (stderr, " DVI bytes.\n");
870 fprintf (stderr, "Successfully interpreted ");
871 fprintf (stderr, WF, ncom);
872 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
888 fprintf (stderr, "(dexit) : exiting with status %d.\n", n);
897 (int nprefixes, CmdPrefix * prefix, CmdTable cmds)
899 (nprefixes, prefix, cmds)
905 int code; /* first opcode for a given command prefix */
906 int opcode; /* command's opcode */
907 int nsuffixes; /* number of commands with a given prefix */
908 int isuffix; /**** integer suffix, of at most three digits ****/
909 String suffix; /* suffix string generated from integer suffix */
910 size_t plen = 0; /* prefix length */
911 size_t slen; /* suffix length */
912 size_t clen; /* whole command name length */
913 int i, j; /* loop indices */
915 for (i=0; i < nprefixes; prefix++, i++)
917 code = prefix->first_code;
918 if (code < 0 || code > 255)
921 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
923 "prefix listed internally with code = %d, must be 0 to 255\n",
927 if (prefix->has_suffix)
929 plen = strlen (prefix->name);
930 /**** Suffixes in DTL are Integers, in Sequence */
931 if (prefix->last_suffix < prefix->first_suffix)
934 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
936 "prefix's last suffix %d < first suffix (%d)\n",
937 prefix->last_suffix, prefix->first_suffix);
940 nsuffixes = prefix->last_suffix - prefix->first_suffix + 1;
941 opcode = prefix->first_code;
942 for (j=0; j < nsuffixes; j++, opcode++)
944 isuffix = prefix->first_suffix + j;
945 if (0 <= code && code <= 127) /* SETCHAR */
947 /* SETCHAR's suffix is written in uppercase hexadecimal */
948 sprintf (suffix, "%02X", isuffix);
950 else /* 128 <= code && code <= 255 */ /* other DTL commands */
952 /* other commands' suffices are written in decimal */
953 sprintf (suffix, "%d", isuffix);
955 slen = strlen (suffix);
957 cmds[opcode] = (char *) gmalloc (clen+1);
958 strcpy (cmds[opcode], prefix->name);
959 strcat (cmds[opcode], suffix);
962 else /* command name = prefix */
964 plen = strlen (prefix->name);
966 opcode = prefix->first_code;
967 cmds[opcode] = (char *) gmalloc (clen+1);
968 strcpy (cmds[opcode], prefix->name);
987 for (i=0; i < NCMDS; i++)
988 gfree (cmd_table[i]);
996 (FILE * fp, Line * line, int max)
1003 /* read a (Line *) line from fp, return length */
1004 /* adapted from K&R (second, alias ANSI C, edition, 1988), page 165 */
1006 if (fgets (line->buf, max, fp) == NULL)
1011 line->wrote = strlen (line->buf);
1022 (FILE * fp, int * ch)
1028 /* read one character from dtl_line if possible, */
1029 /* otherwise read another dtl_line from fp */
1030 /* return 1 if a character is read, 0 if at end of fp file */
1032 extern Line dtl_line;
1033 if (dtl_line.wrote == 0 || dtl_line.read >= dtl_line.wrote)
1036 /* refill line buffer */
1037 line_status = get_line (fp, &dtl_line, MAXLINE);
1038 if (line_status == 0)
1040 /* at end of DTL file */
1044 fprintf (stderr, "(read_line_char) : end of DTL file\n");
1051 /* new DTL line was read */
1055 fprintf (stderr, "(read_line_char) : new DTL input line:\n");
1056 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
1060 *ch = dtl_line.buf [dtl_line.read ++];
1062 ++ com_read; /* count another DTL command character */
1065 /* read_line_char */
1071 (FILE * fp, int * ch)
1077 /* Read next character, if any, from file fp. */
1078 /* Write it into *ch. */
1079 /* If no character is read, then *ch value < 0. */
1080 /* Return 1 if OK, 0 if EOF or error. */
1083 int c; /* in case ch points awry, we still have something in c. */
1086 if (read_line_char (fp, &c) == 0)
1088 /* end of fp file, or error reading it */
1097 "(read_char) : character %d not in range 0 to 255\n",
1102 else if ( ! isprint (c & 0x7f) && ! isspace (c))
1106 "(read_char) : character %d %s.\n",
1108 "not printable and not white space");
1128 /* read and check DTL variety signature */
1129 /* return number of DTL bytes written */
1130 /* DTL variety is _NEVER_ grouped by BCOM and ECOM. */
1131 /* Uniformity here enables the program easily to modify its behavior. */
1133 COUNT vread = 0; /* number of DTL bytes read by read_variety */
1134 COUNT nread = 0; /* number of DTL bytes read by read_token */
1135 static Token token = "";
1137 /* read the DTL VARIETY keyword */
1138 nread = read_token (dtl, token);
1140 /* test whether signature begins correctly */
1141 if (strcmp (token, "variety") != 0)
1144 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1145 fprintf (stderr, "DTL signature must begin with \"%s\", not \"%s\".\n",
1150 /* read the DTL variety */
1151 nread = read_token (dtl, token);
1153 /* test whether variety is correct */
1154 if (strcmp (token, VARIETY) != 0)
1157 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1158 fprintf (stderr, "DTL variety must be \"%s\", not \"%s\".\n",
1164 fprintf (stderr, "(read_variety) : DTL variety %s is OK.\n", VARIETY);
1166 return vread; /* OK */
1174 (FILE * fp, int * ch)
1180 /* Skip whitespace characters in file fp. */
1181 /* Write in *ch the last character read from fp, */
1182 /* or < 0 if fp could not be read. */
1183 /* Return number of characters read from fp. */
1185 int c; /* character read (if any) */
1186 COUNT count; /* number (0 or more) of whitespace characters read */
1187 int nchar; /* number (0 or 1) of characters read by read_char */
1189 /* loop ends at: end of fp file, or reading error, or not a white space */
1190 for (count=0; ((nchar = read_char (fp, &c)) == 1 && isspace (c)); ++count)
1192 /* otherwise, more white spaces to skip */
1195 /* report when each DTL end of line is reached */
1199 fprintf (stderr, "(skip_space) : ");
1200 fprintf (stderr, "end of DTL line (at least) ");
1201 fprintf (stderr, WF, dtl_line.num);
1202 fprintf (stderr, "\n");
1212 *ch = c; /* c will be < 0 if read_char could not read fp */
1213 return (count + nchar);
1221 (FILE * dtl, char * token)
1227 /* read next token from dtl file. */
1228 /* return number of DTL bytes read. */
1229 /* A token is one of:
1230 a string from BMES_CHAR to the next unescaped EMES_CHAR, inclusive;
1231 BCOM or ECOM, unless these are empty strings;
1233 any other sequence of non-whitespace characters.
1236 COUNT nread; /* number of DTL bytes read by read_token */
1237 int ch; /* most recent character read */
1241 /* obtain first non-space character */
1242 /* add to nread the number of characters read from dtl by skip_space */
1243 nread += skip_space (dtl, &ch);
1247 /* end of dtl file */
1248 /* write an empty token */
1253 fprintf (stderr, "(read_token) : end of dtl file.\n");
1256 else if (group && ch == BCOM_CHAR)
1258 strcpy (token, BCOM);
1260 else if (group && ch == ECOM_CHAR)
1262 strcpy (token, ECOM);
1268 if (ch == BMES_CHAR) /* string token; read until unescaped EMES_CHAR */
1270 nread += read_mes (dtl, token+1);
1272 else if (ch == BSEQ_CHAR || ch == ESEQ_CHAR)
1274 /* token is complete */
1276 else /* any other string not containing (ECOM_CHAR or) whitespace */
1278 nread += read_misc (dtl, token+1);
1285 fprintf (stderr, "(read_token) : token = \"%s\"\n", token);
1288 return (nread); /* number of bytes read from dtl file */
1295 #define CHAR_EOS (-1)
1300 (FILE * fp, int * ch)
1307 int status = CHAR_OK; /* OK so far */
1310 if (read_char (fp, &c) == 0)
1311 status = CHAR_FAIL; /* fail */
1313 if (c == EMES_CHAR) /* end-of-string char */
1315 status = CHAR_EOS; /* end of string */
1317 else if (c == ESC_CHAR) /* escape character */
1319 /* accept the next character literally, even ESC_CHAR and EMES_CHAR */
1320 if (read_char (fp, &c) == 0)
1321 status = CHAR_FAIL; /* fail */
1327 /* read_string_char */
1333 (FILE * fp, Token token)
1342 /* loop ends at: end of fp file, or reading error, or a space */
1343 for (count=0; count <= MAXTOKLEN; ++count)
1345 if (read_char (fp, &c) == 0 || isspace (c))
1347 if (group && c == ECOM_CHAR)
1349 (Void) unread_char ();
1355 token[count] = '\0';
1364 (FILE * fp, char * token)
1370 /* called **AFTER** a BMES_CHAR has been read */
1371 /* read file fp for characters up to next unescaped EMES_CHAR */
1372 /* this is called a "string token" */
1373 /* write string, including EMES_CHAR, into token[] */
1374 /* return number of characters read from fp */
1376 COUNT dtl_count; /* number of DTL characters read by read_mes from fp */
1377 int more; /* flag more == 0 to terminate loop */
1378 int escape; /* flag escape == 1 if previous character was ESC_CHAR */
1379 int ch; /* current DTL character */
1386 if (read_char (fp, &ch) == 0)
1388 /* end of fp file, or reading error */
1391 else /* error checking passed */
1394 if (ch == EMES_CHAR && escape == 0) /* end of string */
1396 /* include final EMES_CHAR */
1400 else if (ch == ESC_CHAR && escape == 0)
1402 /* next character is not end of string */
1407 /* store any other character, */
1408 /* including escaped EMES_CHAR and ESC_CHAR*/
1422 /* wind input back, to allow rereading of one character */
1423 /* return 1 if this works, 0 on error */
1425 extern Line dtl_line;
1427 if (dtl_line.read > 0)
1429 -- dtl_line.read; /* back up one character in dtl_line */
1430 -- dtl_read; /* correct the count of DTL characters */
1431 -- com_read; /* count another DTL command character */
1432 status = 1; /* OK */
1434 else /* current DTL line is empty */
1436 status = 0; /* error */
1446 (char * command, int * opcode)
1457 for (i=0; i < NCMDS; i++)
1459 if ((cmd_table[i] != 0) && (strcmp (command, cmd_table[i]) == 0))
1482 if (byte < 0 || byte > 255)
1485 fprintf (stderr, "(check_byte) : INTERNAL ERROR : ");
1486 fprintf (stderr, "byte %d not in the range of 0 to 255.\n", byte);
1497 (FILE * dtl, FILE * dvi, int opcode)
1507 if (opcode >= 0 && opcode <= 127)
1508 ; /* SETCHAR uses no data */
1509 else if (opcode >= 128 && opcode <= 170)
1511 word_t this_bop_address = last_bop_address;
1513 if (opcode == 139) /* BOP */
1515 this_bop_address = dvi_written - 1;
1517 put_table (op_128_170, opcode, dtl, dvi);
1518 if (opcode == 139) /* BOP */
1520 xfer_bop_address (dtl, dvi);
1521 last_bop_address = this_bop_address;
1524 else if (opcode >= 171 && opcode <= 234)
1525 ; /* FONTNUM uses no data */
1526 else if (opcode >= 235 && opcode <= 238)
1527 put_table (fnt, opcode, dtl, dvi);
1528 else if (opcode >= 239 && opcode <= 242)
1531 special (dtl, dvi, n);
1533 else if (opcode >= 243 && opcode <= 246)
1536 fontdef (dtl, dvi, n);
1538 else if (opcode == 247)
1539 preamble (dtl, dvi);
1540 else if (opcode == 248)
1541 postamble (dtl, dvi);
1542 else if (opcode == 249)
1543 post_post (dtl, dvi);
1544 else if (opcode >= 250 && opcode <= 255)
1545 ; /* these, undefined, opcodes use no data */
1550 "(xfer_args) : opcode %d not handled.\n",
1562 (FILE * dtl, FILE * dvi)
1568 /* Called _after_ a BSEQ_CHAR command */
1569 /* Read bytes from dtl file, */
1570 /* writing corresponding SETCHAR or SET1 commands to DVI file, */
1571 /* _until_ unescaped ESEQ_CHAR is found */
1572 /* Return 1 if OK, 0 on error */
1573 /**** dt2dv assumes 8 bit characters, ****/
1574 /**** but some day one might change that. ****/
1576 int status = 1; /* status = 1 if OK, 0 if error */
1577 int more; /* sequence of font characters continuing? */
1578 int escape = 0; /* flag set if previous character was an escape */
1579 int ch; /* character read from DTL file */
1583 /* ignore read_char status, to allow unprintable characters */
1584 (Void) read_char (dtl, &ch);
1585 /* but check for end of dtl file, or serious file reading error */
1589 fprintf (stderr, "(set_seq) : ");
1590 fprintf (stderr, "end of dtl file, ");
1591 fprintf (stderr, "or serious dtl file reading error\n");
1594 status = 0; /* bad news */
1596 else /* read dtl file, okay */
1598 if (ch == ESC_CHAR && escape == 0) /* escape next character */
1604 if (ch == ESEQ_CHAR && escape == 0) /* end of sequence */
1608 else if (ch <= 127) /* can use SETCHAR */
1612 else if (ch <= 255) /* can use SET1 */
1614 put_byte (128, dvi); /* SET1 opcode */
1615 put_unsigned (1, (U4) ch, dvi);
1620 fprintf (stderr, "(set_seq) : ");
1622 "ERROR : DTL character %d is not in range 0 to 255\n",
1626 status = 0; /* Error, because dt2dv assumes 8 bit characters */
1628 escape = 0; /* current ch is not an escape character */
1640 (int n, FILE * dtl, FILE * dvi)
1647 /* translate unsigned n-byte hexadecimal number from dtl to dvi file. */
1648 /* return value of hexadecimal number */
1650 U4 unum = 0; /* at most this space needed */
1651 COUNT nread = 0; /* number of DTL bytes read by read_token */
1652 int nconv = 0; /* number of arguments converted by sscanf */
1653 static Token token = ""; /* DTL token */
1659 "(xfer_hex) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1664 nread = read_token (dtl, token);
1666 nconv = sscanf (token, XF4, &unum);
1671 fprintf (stderr, "(xfer_hex) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1672 dtl_filename, "hexadecimal number expected, not", token);
1676 put_unsigned (n, unum, dvi);
1686 (int n, FILE * dtl, FILE * dvi)
1693 /* translate unsigned n-byte octal number from dtl to dvi file. */
1694 /* return value of octal number */
1696 U4 unum = 0; /* at most this space needed */
1697 COUNT nread = 0; /* number of DTL bytes read by read_token */
1698 int nconv = 0; /* number of arguments converted by sscanf */
1699 static Token token = ""; /* DTL token */
1705 "(xfer_oct) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1710 nread = read_token (dtl, token);
1712 nconv = sscanf (token, OF4, &unum);
1717 fprintf (stderr, "(xfer_oct) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1718 dtl_filename, "octal number expected, not", token);
1722 put_unsigned (n, unum, dvi);
1732 (int n, FILE * dtl, FILE * dvi)
1739 /* translate unsigned n-byte number from dtl to dvi file. */
1740 /* return value of unsigned number */
1742 U4 unum = 0; /* at most this space needed */
1744 unum = get_unsigned (dtl);
1745 put_unsigned (n, unum, dvi);
1755 (int n, FILE * dtl, FILE * dvi)
1762 /* translate signed n-byte number from dtl to dvi file. */
1763 /* return value of signed number */
1767 snum = get_signed (dtl);
1768 put_signed (n, snum, dvi);
1783 /* read unsigned number from dtl file. */
1784 /* return value of unsigned number */
1786 U4 unum = 0; /* at most this space needed */
1787 COUNT nread = 0; /* number of DTL bytes read by read_token */
1788 int nconv = 0; /* number of arguments converted by sscanf */
1789 static Token token = ""; /* DTL token */
1791 nread = read_token (dtl, token);
1793 nconv = sscanf (token, UF4, &unum);
1798 fprintf (stderr, "(get_unsigned) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1799 dtl_filename, "unsigned number expected, not", token);
1816 /* read signed number from dtl file. */
1817 /* return value of signed number */
1820 COUNT nread = 0; /* number of DTL bytes read by read_token */
1821 int nconv = 0; /* number of sscanf arguments converted and assigned */
1822 static Token token = "";
1824 nread = read_token (dtl, token);
1826 nconv = sscanf (token, SF4, &snum);
1831 fprintf (stderr, "(get_signed) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1832 dtl_filename, "signed number expected, not", token);
1844 (int n, U4 unum, FILE * dvi)
1851 /* put unsigned in-byte integer to dvi file */
1852 /* DVI format uses Big-endian storage of numbers: */
1853 /* most significant byte is first. */
1854 /* return number of bytes written. */
1856 Byte ubyte[4]; /* at most 4 bytes needed in DVI format */
1863 "(put_unsigned) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1868 /* Big-endian storage. */
1869 for (i = 0; i < n; i++)
1871 ubyte[i] = (Byte) (unum % 256);
1874 /* Reverse order for big-endian representation. */
1875 for (i = n-1; i >= 0; i--)
1877 put_byte ((int) ubyte[i], dvi);
1888 (int n, S4 snum, FILE * dvi)
1895 /* put signed in-byte integer to dvi file */
1896 /* DVI format uses 2's complement Big-endian storage of signed numbers: */
1897 /* most significant byte is first. */
1898 /* return number of bytes written. */
1900 /* Will this deal properly with the sign? */
1906 "(put_signed) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1911 /* How do we ensure 2's complement representation? */
1912 /* Here's a trick that I hope is portable, given ANSI C. */
1913 /* See K&R (2nd edition), Appendix A6.2 "Integral Conversions". */
1915 /* Convert snum to U4 data type */
1916 put_unsigned (n, (U4) snum, dvi);
1931 /* check that a BMES_CHAR is the next non-whitespace character in dtl */
1933 int ch; /* next non-whitespace character in dtl */
1935 /* `(Void)' because we ignore the number of spaces skipped */
1936 (Void) skip_space (dtl, &ch);
1941 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1943 fprintf (stderr, "end of dtl file, or reading error\n");
1947 if (ch != BMES_CHAR)
1950 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1952 fprintf (stderr, "BMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1953 BMES_CHAR, "expected before string", ch, ch);
1970 /* check that an EMES_CHAR is the next character in dtl */
1972 int ch; /* dtl character */
1974 if (read_char (dtl, &ch) == 0)
1977 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1979 fprintf (stderr, "end of dtl file, or reading error\n");
1983 if (ch != EMES_CHAR)
1986 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1988 fprintf (stderr, "EMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1989 EMES_CHAR, "expected to follow string", ch, ch);
1998 /* Size typically used in this program for Lstring variables */
2005 (Lstring * lsp, long int n)
2014 lsp->s = (char *) gmalloc (n);
2031 lsp->s = NULL; /* to be sure */
2033 /* de_init_Lstring */
2046 lsp = (Lstring *) gmalloc (sizeof (Lstring));
2047 init_Lstring (lsp, n);
2071 (int ch, Lstring * lstr)
2077 /* write byte ch into Lstring *lstr */
2079 if (lstr->l >= lstr->m)
2082 fprintf (stderr, "(ls_putb) : ERROR : No more room in Lstring.\n");
2087 lstr->s [(lstr->l)++] = ch;
2096 (FILE * dtl, Lstring * lstr)
2102 /* get a string from dtl file, store as an Lstring in *lstr. */
2103 /* lstr must already be allocated and initialised. */
2104 /* return length of Lstring *lstr */
2107 int char_status = CHAR_OK; /* OK so far */
2112 fprintf (stderr, "(get_Lstring) : entering get_Lstring.\n");
2121 "(get_Lstring) : string is: \"");
2124 for (nch=0; ; nch++)
2128 char_status = read_string_char (dtl, &ch);
2130 if (char_status == CHAR_FAIL)
2132 /* end of dtl file, or reading error */
2133 fprintf (stderr, "\n");
2135 fprintf (stderr, "(get_Lstring) : DTL FILE ERROR (%s) : ",
2137 fprintf (stderr, "cannot read string[");
2138 fprintf (stderr, UF4, nch);
2139 fprintf (stderr, "] from dtl file.\n");
2145 fprintf (stderr, "%c", ch);
2148 if (char_status == CHAR_EOS)
2150 if (ch != EMES_CHAR)
2153 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2154 fprintf (stderr, "char_status = CHAR_FAIL,\n");
2156 "but ch = %c (char %d) is not EMES_CHAR = %c (char %d)\n",
2157 ch, ch, EMES_CHAR, EMES_CHAR);
2160 (Void) unread_char ();
2161 break; /* end of string */
2163 else if (char_status == CHAR_OK)
2170 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2171 fprintf (stderr, "char_status = %d is unfamiliar!\n", char_status);
2179 fprintf (stderr, "\".\n");
2187 fprintf (stderr, "(get_Lstring) : leaving get_Lstring.\n");
2198 (const Lstring * lstr, FILE * dvi)
2206 fwret = fwrite ((lstr->s), sizeof (char), (size_t) (lstr->l), dvi);
2208 dvi_written += fwret;
2210 if ((long int) fwret < lstr->l)
2214 "(put_Lstring) : DVI File ERROR : not all bytes written ");
2215 fprintf (stderr, "(%ld of %ld).\n",
2216 (long int) fwret, (long int) (lstr->l));
2226 (int n, FILE * dtl, FILE * dvi)
2233 /* transfer (length and) quoted string from dtl to dvi file, */
2234 /* return number of bytes written to dvi file. */
2236 U4 k, k2, lstr_maxsize;
2242 fprintf (stderr, "(xfer_len_string) : entering xfer_len_string.\n");
2245 /* k[n] : length of special string */
2247 k = get_unsigned (dtl);
2249 lstr_maxsize = (k > LSIZE) ? k : LSIZE;
2250 init_Lstring (&lstr, lstr_maxsize);
2255 fprintf (stderr, "(xfer_len_string) : string's nominal length k = ");
2256 fprintf (stderr, UF4, k);
2257 fprintf (stderr, " characters.\n");
2260 k2 = get_Lstring (dtl, &lstr);
2265 fprintf (stderr, "(xfer_len_string) : WARNING : string length (");
2266 fprintf (stderr, UF4, k);
2267 fprintf (stderr, ") in DTL file is wrong\n");
2268 fprintf (stderr, "Writing correct value (");
2269 fprintf (stderr, UF4, k2);
2270 fprintf (stderr, ") to DVI file\n");
2273 put_unsigned (n, k2, dvi);
2275 put_Lstring (&lstr, dvi);
2280 fprintf (stderr, "(xfer_len_string) : leaving xfer_len_string.\n");
2283 de_init_Lstring (&lstr);
2287 /* xfer_len_string */
2293 (FILE * dtl, FILE * dvi)
2299 /* translate signed 4-byte bop address from dtl to dvi file. */
2300 /* return value of bop address written to DVI file */
2302 S4 snum = 0; /* at most this space needed for byte address */
2303 COUNT nread = 0; /* number of DTL bytes read by read_token */
2304 int nconv = 0; /* number of arguments converted by sscanf */
2305 static Token token = ""; /* DTL token */
2307 nread += read_token (dtl, token);
2309 nconv = sscanf (token, SF4, &snum);
2314 fprintf (stderr, "(xfer_bop_address) : DTL FILE ERROR (%s) : ",
2316 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2320 if (snum != last_bop_address)
2323 fprintf (stderr, "(xfer_bop_address) : WARNING : byte address (");
2324 fprintf (stderr, WF, snum);
2325 fprintf (stderr, ")\n");
2326 fprintf (stderr, "for previous bop in DTL file is wrong\n");
2327 fprintf (stderr, "Writing correct value (");
2328 fprintf (stderr, WF, last_bop_address);
2329 fprintf (stderr, ") to DVI file\n");
2332 put_signed (4, last_bop_address, dvi);
2334 return last_bop_address;
2336 /* xfer_bop_address */
2340 xfer_postamble_address
2342 (FILE * dtl, FILE * dvi)
2348 /* translate signed 4-byte postamble address from dtl to dvi file. */
2349 /* return value of postamble address written to DVI file */
2351 S4 snum = 0; /* at most this space needed for byte address */
2352 COUNT nread = 0; /* number of DTL bytes read by read_token */
2353 int nconv = 0; /* number of arguments converted by sscanf */
2354 static Token token = ""; /* DTL token */
2356 nread += read_token (dtl, token);
2358 nconv = sscanf (token, SF4, &snum);
2363 fprintf (stderr, "(xfer_postamble_address) : DTL FILE ERROR (%s) : ",
2365 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2369 if (snum != postamble_address)
2372 fprintf (stderr, "(xfer_postamble_address) : WARNING : byte address (");
2373 fprintf (stderr, WF, snum);
2374 fprintf (stderr, ")\n");
2375 fprintf (stderr, "for postamble in DTL file is wrong\n");
2376 fprintf (stderr, "Writing correct value (");
2377 fprintf (stderr, WF, postamble_address);
2378 fprintf (stderr, ") to DVI file\n");
2381 put_signed (4, postamble_address, dvi);
2383 return postamble_address;
2385 /* xfer_postamble_address */
2391 (op_table table, int opcode, FILE * dtl, FILE * dvi)
2393 (table, opcode, dtl, dvi)
2400 /* table: {char * name; int first, last; op_info * list; }; */
2401 /* op_info: {int code; char * name; int nargs; char * args; }; */
2403 op_info op; /* entry in table */
2405 int pos; /* current position in string being scanned */
2406 static String args = "";
2408 /* Defensive programming. */
2409 if (opcode < table.first || opcode > table.last)
2413 "(put_table) : DTL FILE (OR INTERNAL) ERROR : opcode %d ", opcode);
2414 fprintf (stderr, "is outside table %s [ %d to %d ] !\n",
2415 table.name, table.first, table.last);
2419 op = table.list [ opcode - table.first ];
2421 /* More defensive programming. */
2422 if (opcode != op.code)
2426 "(put_table) : INTERNAL ERROR : opcode %d for command \"%s\"",
2428 fprintf (stderr, " faulty in table \"%s\".\n", table.name);
2432 /* process all the arguments, according to size and sign */
2434 strncpy (args, op.args, MAXSTRLEN);
2437 for (i=0; i < op.nargs; i++)
2440 int nscan = 0; /* number of bytes read by sscanf */
2441 int nconv = 0; /* number of sscanf arguments converted & assigned */
2443 /* sscanf() does NOT advance over its input: */
2444 /* C strings lack internal state information, which C files have. */
2445 /* On Sun/OS, sscanf calls ungetc on the string it reads, */
2446 /* which therefore has to be writable. */
2448 nconv = sscanf (args + pos, "%d%n", &argtype, &nscan);
2450 if (nconv < 1 || nscan < 1)
2454 "(put_table) : INTERNAL ERROR : internal read of table %s failed!\n",
2462 xfer_signed (-argtype, dtl, dvi);
2464 xfer_unsigned (argtype, dtl, dvi);
2473 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
2479 (FILE * dtl, FILE * dvi, int n)
2486 /* read special (1 <= n <= 4 byte) data from dtl, and write in dvi */
2487 /* return number of bytes written */
2494 fprintf (stderr, "(special) : entering special.\n");
2500 fprintf (stderr, "(special) : DTL FILE ERROR (%s) : special %d, ",
2502 fprintf (stderr, "range is 1 to 4.\n");
2506 /* k[n] : length of special string */
2507 /* x[k] : special string */
2509 nk = xfer_len_string (n, dtl, dvi);
2514 fprintf (stderr, "(special) : leaving special.\n");
2525 (FILE * dtl, FILE * dvi, int suffix)
2532 /* read fontdef fnt_def1 .. fnt_def4 from dtl, and write in dvi */
2533 /* suffix is the fontdef suffix : 1 to 4 */
2534 /* return number of bytes written */
2538 Lstring lstr1, lstr2;
2543 fprintf (stderr, "(fontdef) : entering fontdef.\n");
2546 if (suffix < 1 || suffix > 4)
2549 fprintf (stderr, "(fontdef) : DTL FILE ERROR (%s) : ",
2551 fprintf (stderr, "font def %d, but range is 1 to 4.\n", suffix);
2555 init_Lstring (&lstr1, LSIZE);
2556 init_Lstring (&lstr2, LSIZE);
2561 fprintf (stderr, "(fontdef) : about to read font number.\n");
2564 /* k[suffix] : font number */
2566 k = xfer_signed (suffix, dtl, dvi);
2568 k = xfer_unsigned (suffix, dtl, dvi);
2573 fprintf (stderr, "(fontdef) : font ");
2574 fprintf (stderr, UF4, k);
2575 fprintf (stderr, ".\n");
2579 /* c[4] : (hexadecimal) checksum : I (gt) would prefer this */
2580 xfer_hex (4, dtl, dvi);
2581 #else /*NOT HEX_CHECKSUM */
2582 /* c[4] : checksum (octal, for comparison with tftopl's .pl file) */
2583 xfer_oct (4, dtl, dvi);
2587 xfer_unsigned (4, dtl, dvi);
2590 xfer_unsigned (4, dtl, dvi);
2592 /* If DTL file's edited, a and l may be wrong. */
2594 /* a[1] : length of font `area' (directory) portion of pathname string */
2595 a = get_unsigned (dtl);
2597 /* l[1] : length of font portion of pathname string */
2598 l = get_unsigned (dtl);
2600 /* n[a+l] : font pathname string <= area + font */
2602 a2 = get_Lstring (dtl, &lstr1);
2607 fprintf (stderr, "(fontdef) : WARNING : font area string's length (");
2608 fprintf (stderr, UF4, a);
2609 fprintf (stderr, ") in DTL file is wrong\n");
2610 fprintf (stderr, "Writing correct value (");
2611 fprintf (stderr, UF4, a2);
2612 fprintf (stderr, ") to DVI file\n");
2615 put_unsigned (1, a2, dvi);
2617 l2 = get_Lstring (dtl, &lstr2);
2622 fprintf (stderr, "(fontdef) : WARNING : font string's length (");
2623 fprintf (stderr, UF4, l);
2624 fprintf (stderr, ") in DTL file is wrong\n");
2625 fprintf (stderr, "Writing correct value (");
2626 fprintf (stderr, UF4, l2);
2627 fprintf (stderr, ") to DVI file\n");
2630 put_unsigned (1, l2, dvi);
2632 put_Lstring (&lstr1, dvi);
2633 put_Lstring (&lstr2, dvi);
2635 de_init_Lstring (&lstr2);
2636 de_init_Lstring (&lstr1);
2641 fprintf (stderr, "(fontdef) : leaving fontdef.\n");
2644 return (suffix + 4*4 + 2*1 + a2 + l2);
2652 (FILE * dtl, FILE * dvi)
2658 /* read preamble from dtl, and write in dvi */
2659 /* return number of bytes written */
2666 fprintf (stderr, "(preamble) : entering preamble.\n");
2670 xfer_unsigned (1, dtl, dvi);
2673 xfer_unsigned (4, dtl, dvi);
2676 xfer_unsigned (4, dtl, dvi);
2679 xfer_unsigned (4, dtl, dvi);
2681 /* k[1] : length of comment */
2682 /* x[k] : comment string */
2684 k1 = xfer_len_string (1, dtl, dvi);
2689 fprintf (stderr, "(preamble) : leaving preamble.\n");
2692 return (1 + 3*4 + k1);
2700 (FILE * dtl, FILE * dvi)
2706 /* read postamble from dtl, and write in dvi */
2707 /* return number of bytes written */
2709 postamble_address = dvi_written - 1;
2711 /* p[4] : DVI address of previous bop command */
2712 /* --- unsigned? --- or signed, as I assume? */
2713 /* For, surely p should be -1 if the DVI file has NO bop? */
2714 xfer_bop_address (dtl, dvi);
2717 xfer_unsigned (4, dtl, dvi);
2720 xfer_unsigned (4, dtl, dvi);
2723 xfer_unsigned (4, dtl, dvi);
2726 xfer_unsigned (4, dtl, dvi);
2729 xfer_unsigned (4, dtl, dvi);
2732 xfer_unsigned (2, dtl, dvi);
2735 xfer_unsigned (2, dtl, dvi);
2745 (FILE * dtl, FILE * dvi)
2751 /* read post_post from dtl, and write in dvi */
2752 /* return number of bytes written */
2754 /* hope I'm writing the "223" bytes in an 8-bit clean way */
2755 int n223 = 0; /* number of "223" bytes in final padding */
2757 /* q[4] : DVI address of post command */
2758 /* --- unsigned? --- or signed, as I assume? */
2759 /* what happens if there is NO postamble command? */
2760 /* shouldn't q be -1 then? */
2762 xfer_postamble_address (dtl, dvi);
2764 /* i[1] : DVI identification byte = 2 */
2765 xfer_unsigned (1, dtl, dvi);
2767 for (n223 = 0; true; n223++)
2769 COUNT nread = 0; /* number of DTL bytes read by read_token */
2774 nread = read_token (dtl, token);
2776 /* check whether end of dtl file */
2781 /* dtl file shouldn't end before an ECOM */
2783 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2785 fprintf (stderr, "premature end of DTL file!\n");
2787 "%d complete iterations of \"padding byte\" loop;\n", n223);
2788 fprintf (stderr, "troublesome token = \"%s\"\n", token);
2791 /* leave the "223" loop */
2794 else if (strcmp (token, "223") == 0)
2796 /* token is a "223" padding byte */
2801 /* read a non-empty token that wasn't "223" */
2802 (Void) unread_char ();
2805 if (strcmp (token, ECOM) == 0)
2807 /* end of DTL's post_post command */
2811 /* error : expected end of post_post */
2813 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2815 fprintf (stderr, "token \"%s\" should be ECOM (\"%s\")\n",
2820 /* leave the "223" loop */
2829 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : \n",
2831 fprintf (stderr, "fewer than four `223' padding bytes.\n");
2832 fprintf (stderr, "Will write at least four `223' padding bytes.\n");
2835 /* check whether the DVI file size is a multiple of 4 bytes */
2836 if ((dvi_written + n223) % 4 != 0)
2839 fprintf (stderr, "(post_post) : WARNING : \n");
2840 fprintf (stderr, "DVI size ");
2841 fprintf (stderr, WF, dvi_written);
2842 fprintf (stderr, " (bytes) wouldn't be a multiple of 4 !\n");
2844 "Will write (at least four) `223' padding bytes until it is.\n");
2847 /* final padding of DVI file by "223" bytes to a multiple of 4 bytes, */
2848 /* with at least 4 bytes */
2850 for (n223 = 0; (n223 < 4) || (dvi_written % 4 != 0); n223++)
2852 /* add a "223" padding byte */
2853 put_byte (223, dvi);
2856 return (4 + 1 + n223);
2861 /* end of dt2dv.c */