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.
9 - version 0.6.1 - 14:38 GMT +11 Thu 9 March 1995
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 - Reference: "The DVI Driver Standard, Level 0",
16 by The TUG DVI Driver Standards Committee.
17 Appendix A, "Device-Independent File Format".
20 /* unix version; read from stdin, write to stdout, by default. */
31 /* by default, read and write regular files */
35 /* maximum number of characters in a DTL input line */
41 COUNT num; /* current line number */
42 size_t max; /* capacity of buf */
43 S4 wrote; /* number of characters written into buf */
44 size_t read; /* position in buf of next character to read from buf */
45 char * buf; /* line buffer */
48 char linebuf[MAXLINE+1];
50 Line dtl_line = {0, 0, 0, MAXLINE, linebuf};
52 /* a DTL token either is:
53 a quoted string (admitting an escape character),
54 or BCOM (if that is a nonempty string),
55 or ECOM (if that is a nonempty string),
56 or a string _not_ including ECOM_CHAR or space.
59 /* maximum expected length of a DTL token */
61 typedef char Token[MAXTOKLEN+1];
63 typedef unsigned char Byte;
69 /* command prefixes */
75 Byte first_suffix, last_suffix;
78 CmdPrefix cmd_prefixes [] =
80 {0, SETCHAR, true, 0, 127},
81 {128, SET, true, 1, 4},
82 {132, SETRULE, false, 0, 0},
83 {133, PUT, true, 1, 4},
84 {137, PUTRULE, false, 0, 0},
85 {138, NOP, false, 0, 0},
86 {139, BOP, false, 0, 0},
87 {140, EOP, false, 0, 0},
88 {141, PUSH, false, 0, 0},
89 {142, POP, false, 0, 0},
90 {143, RIGHT, true, 1, 4},
93 {157, DOWN, true, 1, 4},
96 {171, FONTNUM, true, 0, 63},
97 {235, FONT, true, 1, 4},
98 {239, SPECIAL, true, 1, 4},
99 {243, FONTDEF, true, 1, 4},
100 {247, PRE, false, 0, 0},
101 {248, POST, false, 0, 0},
102 {249, POSTPOST, false, 0, 0},
103 {250, OPCODE, true, 250, 255}
107 /* Number of DVI commands, including those officially undefined */
110 /* table of command name (string) pointers */
111 typedef char * CmdTable [NCMDS];
113 /* initially all command name pointers are NULL */
116 /* operation's opcode, name, number of args, string of arguments. */
125 /* name of table, first opcode, last opcode, pointer to opcode info. */
134 /* Table for opcodes 128 to 170 inclusive. */
136 op_info op_info_128_170 [] =
141 {131, SET4, 1, "-4"},
142 {132, SETRULE, 2, "-4 -4"},
146 {136, PUT4, 1, "-4"},
147 {137, PUTRULE, 2, "-4 -4"},
149 /* bop: not counting last argument, a signed address: */
150 {139, BOP, 10, "-4 -4 -4 -4 -4 -4 -4 -4 -4 -4"},
154 {143, RIGHT1, 1, "-1"},
155 {144, RIGHT2, 1, "-2"},
156 {145, RIGHT3, 1, "-3"},
157 {146, RIGHT4, 1, "-4"},
168 {157, DOWN1, 1, "-1"},
169 {158, DOWN2, 1, "-2"},
170 {159, DOWN3, 1, "-3"},
171 {160, DOWN4, 1, "-4"},
183 /* op_info op_info_128_170 [] */
185 op_table op_128_170 = {"op_128_170", 128, 170, op_info_128_170};
187 /* Table for fnt1 to fnt4 (opcodes 235 to 238) inclusive. */
191 {235, FONT1, 1, "1"},
192 {236, FONT2, 1, "2"},
193 {237, FONT3, 1, "3"},
194 {238, FONT4, 1, "-4"}
196 /* op_info fnt_n [] */
198 op_table fnt = {FONT, 235, 238, fnt_n};
201 /* Function prototypes */
203 Void mem_viol ARGS((int sig));
204 Void give_help (VOID);
205 int parse ARGS((char * s));
206 Void process ARGS((char * s));
209 Void dtl_stdin (VOID);
210 Void dvi_stdout (VOID);
212 int open_dtl ARGS((char * dtl_file, FILE ** pdtl));
213 int open_dvi ARGS((char * dvi_file, FILE ** pdvi));
215 int dt2dv ARGS((FILE * dtl, FILE * dvi));
217 Void * gmalloc ARGS((long int size));
220 Void dexit ARGS((int n));
222 int cons_cmds ARGS((int nprefixes, CmdPrefix * prefix, CmdTable cmds));
223 Void free_cmds ARGS((CmdTable cmd_table));
225 int get_line ARGS((FILE * fp, Line * line, int max));
226 int read_line_char ARGS((FILE * fp, int * ch));
227 int read_char ARGS((FILE * fp, int * ch));
228 int unread_char (VOID);
229 int read_string_char ARGS((FILE * fp, int * ch));
231 COUNT read_variety ARGS((FILE * dtl));
232 COUNT read_token ARGS((FILE * dtl, char * token));
233 COUNT skip_space ARGS((FILE * fp, int * ch));
234 COUNT read_misc ARGS((FILE * fp, Token token));
235 COUNT read_mes ARGS((FILE * fp, char * token));
237 int find_command ARGS((char * command, int * opcode));
238 int xfer_args ARGS((FILE * dtl, FILE * dvi, int opcode));
240 int set_seq ARGS((FILE * dtl, FILE * dvi));
242 int check_byte ARGS((int byte));
243 int put_byte ARGS((int onebyte, FILE * dvi));
245 U4 xfer_hex ARGS((int n, FILE * dtl, FILE * dvi));
246 U4 xfer_oct ARGS((int n, FILE * dtl, FILE * dvi));
247 U4 xfer_unsigned ARGS((int n, FILE * dtl, FILE * dvi));
248 S4 xfer_signed ARGS((int n, FILE * dtl, FILE * dvi));
250 int check_bmes ARGS((FILE * dtl));
251 int check_emes ARGS((FILE * dtl));
253 Void init_Lstring ARGS((Lstring * lsp, long int n));
254 Void de_init_Lstring ARGS((Lstring * lsp));
255 Lstring * alloc_Lstring ARGS((long int n));
256 Void free_Lstring ARGS((Lstring * lstr));
257 Void ls_putb ARGS((int ch, Lstring * lstr));
259 S4 get_Lstring ARGS((FILE * dtl, Lstring * lstr));
260 Void put_Lstring ARGS((const Lstring * lstr, FILE * dvi));
261 U4 xfer_len_string ARGS((int n, FILE * dtl, FILE * dvi));
263 U4 get_unsigned ARGS((FILE * dtl));
264 S4 get_signed ARGS((FILE * dtl));
266 int put_unsigned ARGS((int n, U4 unum, FILE * dvi));
267 int put_signed ARGS((int n, S4 snum, FILE * dvi));
269 S4 xfer_bop_address ARGS((FILE * dtl, FILE * dvi));
270 S4 xfer_postamble_address ARGS((FILE * dtl, FILE * dvi));
272 int put_table ARGS((op_table table, int opcode, FILE * dtl, FILE * dvi));
274 U4 special ARGS((FILE * dtl, FILE * dvi, int n));
275 int fontdef ARGS((FILE * dtl, FILE * dvi, int n));
277 U4 preamble ARGS((FILE * dtl, FILE * dvi));
278 int postamble ARGS((FILE * dtl, FILE * dvi));
279 int post_post ARGS((FILE * dtl, FILE * dvi));
284 char * keyword; /* command line option keyword */
285 int * p_var; /* pointer to option variable */
286 char * desc; /* description of keyword and value */
287 Void (* p_fn) (VOID); /* pointer to function called when option is set */
292 {"-debug", &debug, "detailed debugging", no_op},
293 {"-group", &group, "each DTL command is in parentheses", no_op},
294 {"-si", &rd_stdin, "read all DTL commands from standard input", dtl_stdin},
295 {"-so", &wr_stdout, "write all DVI commands to standard output", dvi_stdout},
296 {NULL, NULL, NULL, NULL}
300 char * progname = ""; /* intended for name of this program */
301 int nfile = 0; /* number of filename arguments on the command line */
303 #define PRINT_PROGNAME fprintf (stderr, "%s ", progname)
305 /* Harbison & Steele (1991) warn that some C implementations */
306 /* of free() do not treat NULL pointers correctly. */
307 #define gfree(p) {if (p) free (p);}
310 FILE * dtl_fp = NULL;
311 FILE * dvi_fp = NULL;
313 char * dtl_filename = "";
314 char * dvi_filename = "";
320 (int argc, char * argv[])
327 Void (*handler) ARGS((int)); /* Previous signal handler */
330 progname = argv[0]; /* name of this program */
332 /* memory violation signal handler */
334 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
347 #define C_LEVEL "non-"
348 #endif /* NOT STDC */
350 /* message about program and compiler */
351 /* NB: LTU EE's Sun/OS library is BSD, even though gcc 2.2.2 is ANSI */
353 fprintf (stderr, "\n");
355 "Program \"%s\" version %s compiled %s %s in %sstandard C.\n",
356 progname, VERSION, __DATE__, __TIME__, C_LEVEL);
358 /* interpret command line arguments */
361 dtl_fp = dvi_fp = NULL;
362 dtl_filename = dvi_filename = "";
364 for (i=1; i < argc; i++)
366 /* parse options, followed by any explicit filenames */
370 if (nfile != 2) /* not exactly two files specified, so give help */
374 dt2dv (dtl_fp, dvi_fp);
390 Void (* handler) ARGS((int));
391 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
396 "(mem_viol) : called with wrong signal!\n");
399 fprintf (stderr, "(mem_viol) : RUNTIME MEMORY ERROR : memory violation, ");
400 fprintf (stderr, "dtl line >= ");
401 fprintf (stderr, WF, dtl_line.num);
402 fprintf (stderr, "\n");
413 fprintf (stderr, "usage: ");
415 fprintf (stderr, "[options] dtl_file dvi_file");
416 fprintf (stderr, "\n");
417 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
419 fprintf (stderr, " ");
420 fprintf (stderr, "[%s]", keyword);
421 fprintf (stderr, " ");
422 fprintf (stderr, "%s", opts[i].desc);
423 fprintf (stderr, "\n");
425 fprintf (stderr, "Messages, like this one, go to stderr.\n");
435 Void dtl_stdin (VOID)
437 extern FILE * dtl_fp;
441 dtl_filename = "Standard Input";
445 Void dvi_stdout (VOID)
447 extern FILE * dvi_fp;
450 /* ! Perilous to monitors! */
452 dvi_filename = "Standard Output";
465 /* parse one command-line argument, `s' */
469 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
471 if (strncmp (s, keyword, strlen (keyword)) == 0)
474 (*(opts[i].p_var)) = 1; /* turn option on */
475 if ((pfn = opts[i].p_fn) != NULL)
476 (*pfn) (); /* call option function */
480 /* reached here, so not an option: assume it's a filename */
490 (char * dtl_file, FILE ** pdtl)
496 /* I: dtl_file; I: pdtl; O: *pdtl. */
498 extern char * dtl_filename;
500 dtl_filename = dtl_file;
502 if (dtl_filename == NULL)
506 "(open_dtl) : INTERNAL ERROR : dtl file's name is NULL.\n");
514 "(open_dtl) : INTERNAL ERROR : address of dtl variable is NULL.\n");
518 *pdtl = fopen (dtl_file, "r");
524 "(open_dtl) : DTL FILE ERROR : Cannot open \"%s\" for text reading.\n",
537 (char * dvi_file, FILE ** pdvi)
543 /* I: dvi_file; I: pdvi; O: *pdvi. */
545 extern char * dvi_filename;
547 dvi_filename = dvi_file;
549 if (dvi_filename == NULL)
553 "(open_dvi) : INTERNAL ERROR : dvi file's name is NULL.\n");
561 "(open_dvi) : INTERNAL ERROR : address of dvi variable is NULL.\n");
565 *pdvi = fopen (dvi_file, "wb");
571 "(open_dvi) : DVI FILE ERROR : Cannot open \"%s\" for binary writing.\n",
590 extern FILE * dtl_fp, * dvi_fp;
592 if (dtl_fp == NULL) /* first filename assumed to be DTL input */
594 open_dtl (s, &dtl_fp);
596 else if (dvi_fp == NULL) /* second filename assumed to be DVI output */
598 open_dvi (s, &dvi_fp);
603 fprintf (stderr, "(process) : at most two filenames allowed.\n");
611 COUNT dtl_read = 0; /* bytes read from dtl file */
612 COUNT dvi_written = 0; /* bytes written to dvi file */
613 word_t last_bop_address = -1; /* byte address of last bop; first bop uses -1 */
614 word_t postamble_address = -1; /* byte address of postamble */
615 COUNT ncom = 0; /* commands successfully read and interpreted from dtl file */
616 COUNT com_read = 0; /* bytes read in current (command and arguments), */
617 /* since and including the opening BCOM_CHAR, if any */
623 (int byte, FILE * dvi)
629 /* write byte into dvi file */
632 /* if (fprintf (dvi, "%c", byte) != 1) */
633 if (fprintf (dvi, "%c", byte) < 0)
637 "(put_byte) : DVI FILE ERROR (%s) : cannot write to dvi file.\n",
650 (FILE * dtl, FILE * dvi)
657 int nprefixes = 0; /* number of prefixes in cmd_prefixes[] list. */
658 static Token dtl_cmd = ""; /* DTL command name */
659 COUNT nread = 0; /* number of bytes read by a function from dtl file. */
661 nprefixes = sizeof (cmd_prefixes) / sizeof (CmdPrefix);
663 /* Construct array of all NCMDS == 256 DTL commands */
665 (Void) cons_cmds (nprefixes, cmd_prefixes, cmd_table);
667 /* DTL commands have the form "[ ]*command arg ... arg[ ]*", */
668 /* possibly enclosed in a BCOM, ECOM pair, */
669 /* and are separated by optional whitespace, typically newlines. */
670 /* That is, each command and its arguments are parenthesised, */
671 /* with optional spaces after the BCOM and before the ECOM, if any. */
673 /* dt2dv is now at the very start of the DTL file */
678 /* The very first thing should be the "variety" signature */
680 nread = read_variety (dtl);
682 /* while not end of dtl file or reading error, */
683 /* read, interpret, and write commands */
694 static Token token = ""; /* DTL token */
695 nread = read_token (dtl, token);
696 /* test for end of input, or reading error */
697 if (strlen (token) == 0)
702 fprintf (stderr, "(dt2dv) : end of input, or reading error.\n");
706 /* test whether this command begins correctly */
707 else if (strcmp (token, BCOM) != 0)
710 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
711 fprintf (stderr, "command must begin with \"%s\", ", BCOM);
712 fprintf (stderr, "not `%c' (char %d).\n", token[0], token[0]);
718 /* read the command name */
719 nread = read_token (dtl, dtl_cmd);
720 /* test for end of input, or reading error */
721 if (strlen (dtl_cmd) == 0)
727 "(dt2dv) : end of input, or reading error.\n");
736 fprintf (stderr, "(dt2dv) : command ");
737 fprintf (stderr, WF, ncom);
738 fprintf (stderr, " = \"%s\".\n", dtl_cmd);
741 /* find opcode for this command */
742 if (find_command (dtl_cmd, &opcode) == 1)
744 /* write the opcode, if we can */
745 put_byte (opcode, dvi);
747 /* treat the arguments, if any */
748 xfer_args (dtl, dvi, opcode);
750 else if (dtl_cmd[0] == BSEQ_CHAR)
752 /* sequence of font characters for SETCHAR */
759 "(dt2dv) : DTL FILE ERROR (%s) : unknown command \"%s\".\n",
760 dtl_filename, dtl_cmd);
767 /* seek ECOM after command's last argument and optional whitespace */
768 static Token token = ""; /* DTL token */
769 nread = read_token (dtl, token);
770 /* test for end of input, or reading error */
771 if (strlen (token) == 0)
777 "(dt2dv) : end of input, or reading error.\n");
781 if (strcmp (token, ECOM) != 0)
784 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
785 fprintf (stderr, "ECOM (\"%s\") expected, not `%c' (char %d).\n",
786 ECOM, token[0], token[0]);
792 ++ ncom; /* one more command successfully read and interpreted */
797 fprintf (stderr, "(dt2dv) :\n");
798 fprintf (stderr, "Read (from file \"%s\") ", dtl_filename);
799 fprintf (stderr, WF, dtl_read);
800 fprintf (stderr, " DTL bytes (");
801 fprintf (stderr, UF4, dtl_line.num);
802 fprintf (stderr, " lines);\n");
803 fprintf (stderr, "wrote (to file \"%s\") ", dvi_filename);
804 fprintf (stderr, WF, dvi_written);
805 fprintf (stderr, " DVI bytes;\n");
806 fprintf (stderr, "completely interpreted ");
807 fprintf (stderr, WF, ncom);
808 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
809 fprintf (stderr, "\n");
811 (Void) free_cmds (cmd_table);
831 fprintf (stderr, "(gmalloc) : INTERNAL ERROR : ");
833 "unreasonable request to malloc %ld bytes\n",
837 p = (Void *) malloc ((size_t) size);
841 fprintf (stderr, "(gmalloc) : MEMORY ALLOCATION ERROR : ");
843 "operating system failed to malloc %ld bytes\n",
856 fprintf (stderr, "(dinfo) : ");
857 fprintf (stderr, "Current DTL input line ");
858 fprintf (stderr, UF4, dtl_line.num);
859 fprintf (stderr, " :\n");
860 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
861 fprintf (stderr, "Read ");
862 fprintf (stderr, WF, dtl_read);
863 fprintf (stderr, " DTL bytes (");
864 fprintf (stderr, WF, com_read);
865 fprintf (stderr, " in current command), wrote ");
866 fprintf (stderr, WF, dvi_written);
867 fprintf (stderr, " DVI bytes.\n");
868 fprintf (stderr, "Successfully interpreted ");
869 fprintf (stderr, WF, ncom);
870 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
886 fprintf (stderr, "(dexit) : exiting with status %d.\n", n);
895 (int nprefixes, CmdPrefix * prefix, CmdTable cmds)
897 (nprefixes, prefix, cmds)
903 int code; /* first opcode for a given command prefix */
904 int opcode; /* command's opcode */
905 int nsuffixes; /* number of commands with a given prefix */
906 int isuffix; /**** integer suffix, of at most three digits ****/
907 String suffix; /* suffix string generated from integer suffix */
908 size_t plen = 0; /* prefix length */
909 size_t slen; /* suffix length */
910 size_t clen; /* whole command name length */
911 int i, j; /* loop indices */
913 for (i=0; i < nprefixes; prefix++, i++)
915 code = prefix->first_code;
916 if (code < 0 || code > 255)
919 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
921 "prefix listed internally with code = %d, must be 0 to 255\n",
925 if (prefix->has_suffix)
927 plen = strlen (prefix->name);
928 /**** Suffixes in DTL are Integers, in Sequence */
929 if (prefix->last_suffix < prefix->first_suffix)
932 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
934 "prefix's last suffix %d < first suffix (%d)\n",
935 prefix->last_suffix, prefix->first_suffix);
938 nsuffixes = prefix->last_suffix - prefix->first_suffix + 1;
939 opcode = prefix->first_code;
940 for (j=0; j < nsuffixes; j++, opcode++)
942 isuffix = prefix->first_suffix + j;
943 if (0 <= code && code <= 127) /* SETCHAR */
945 /* SETCHAR's suffix is written in uppercase hexadecimal */
946 sprintf (suffix, "%02X", isuffix);
948 else /* 128 <= code && code <= 255 */ /* other DTL commands */
950 /* other commands' suffices are written in decimal */
951 sprintf (suffix, "%d", isuffix);
953 slen = strlen (suffix);
955 cmds[opcode] = (char *) gmalloc (clen+1);
956 strcpy (cmds[opcode], prefix->name);
957 strcat (cmds[opcode], suffix);
960 else /* command name = prefix */
962 plen = strlen (prefix->name);
964 opcode = prefix->first_code;
965 cmds[opcode] = (char *) gmalloc (clen+1);
966 strcpy (cmds[opcode], prefix->name);
985 for (i=0; i < NCMDS; i++)
986 gfree (cmd_table[i]);
994 (FILE * fp, Line * line, int max)
1001 /* read a (Line *) line from fp, return length */
1002 /* adapted from K&R (second, alias ANSI C, edition, 1988), page 165 */
1004 if (fgets (line->buf, max, fp) == NULL)
1009 line->wrote = strlen (line->buf);
1020 (FILE * fp, int * ch)
1026 /* read one character from dtl_line if possible, */
1027 /* otherwise read another dtl_line from fp */
1028 /* return 1 if a character is read, 0 if at end of fp file */
1030 extern Line dtl_line;
1031 if (dtl_line.wrote == 0 || dtl_line.read >= dtl_line.wrote)
1034 /* refill line buffer */
1035 line_status = get_line (fp, &dtl_line, MAXLINE);
1036 if (line_status == 0)
1038 /* at end of DTL file */
1042 fprintf (stderr, "(read_line_char) : end of DTL file\n");
1049 /* new DTL line was read */
1053 fprintf (stderr, "(read_line_char) : new DTL input line:\n");
1054 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
1058 *ch = dtl_line.buf [dtl_line.read ++];
1060 ++ com_read; /* count another DTL command character */
1063 /* read_line_char */
1069 (FILE * fp, int * ch)
1075 /* Read next character, if any, from file fp. */
1076 /* Write it into *ch. */
1077 /* If no character is read, then *ch value < 0. */
1078 /* Return 1 if OK, 0 if EOF or error. */
1081 int c; /* in case ch points awry, we still have something in c. */
1084 if (read_line_char (fp, &c) == 0)
1086 /* end of fp file, or error reading it */
1095 "(read_char) : character %d not in range 0 to 255\n",
1100 else if ( ! isprint (c) && ! isspace (c))
1104 "(read_char) : character %d %s.\n",
1106 "not printable and not white space");
1126 /* read and check DTL variety signature */
1127 /* return number of DTL bytes written */
1128 /* DTL variety is _NEVER_ grouped by BCOM and ECOM. */
1129 /* Uniformity here enables the program easily to modify its behavior. */
1131 COUNT vread = 0; /* number of DTL bytes read by read_variety */
1132 COUNT nread = 0; /* number of DTL bytes read by read_token */
1133 static Token token = "";
1135 /* read the DTL VARIETY keyword */
1136 nread = read_token (dtl, token);
1138 /* test whether signature begins correctly */
1139 if (strcmp (token, "variety") != 0)
1142 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1143 fprintf (stderr, "DTL signature must begin with \"%s\", not \"%s\".\n",
1148 /* read the DTL variety */
1149 nread = read_token (dtl, token);
1151 /* test whether variety is correct */
1152 if (strcmp (token, VARIETY) != 0)
1155 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1156 fprintf (stderr, "DTL variety must be \"%s\", not \"%s\".\n",
1162 fprintf (stderr, "(read_variety) : DTL variety %s is OK.\n", VARIETY);
1164 return vread; /* OK */
1172 (FILE * fp, int * ch)
1178 /* Skip whitespace characters in file fp. */
1179 /* Write in *ch the last character read from fp, */
1180 /* or < 0 if fp could not be read. */
1181 /* Return number of characters read from fp. */
1183 int c; /* character read (if any) */
1184 COUNT count; /* number (0 or more) of whitespace characters read */
1185 int nchar; /* number (0 or 1) of characters read by read_char */
1187 /* loop ends at: end of fp file, or reading error, or not a white space */
1188 for (count=0; ((nchar = read_char (fp, &c)) == 1 && isspace (c)); ++count)
1190 /* otherwise, more white spaces to skip */
1193 /* report when each DTL end of line is reached */
1197 fprintf (stderr, "(skip_space) : ");
1198 fprintf (stderr, "end of DTL line (at least) ");
1199 fprintf (stderr, WF, dtl_line.num);
1200 fprintf (stderr, "\n");
1210 *ch = c; /* c will be < 0 if read_char could not read fp */
1211 return (count + nchar);
1219 (FILE * dtl, char * token)
1225 /* read next token from dtl file. */
1226 /* return number of DTL bytes read. */
1227 /* A token is one of:
1228 a string from BMES_CHAR to the next unescaped EMES_CHAR, inclusive;
1229 BCOM or ECOM, unless these are empty strings;
1231 any other sequence of non-whitespace characters.
1234 COUNT nread; /* number of DTL bytes read by read_token */
1235 int ch; /* most recent character read */
1239 /* obtain first non-space character */
1240 /* add to nread the number of characters read from dtl by skip_space */
1241 nread += skip_space (dtl, &ch);
1245 /* end of dtl file */
1246 /* write an empty token */
1251 fprintf (stderr, "(read_token) : end of dtl file.\n");
1254 else if (group && ch == BCOM_CHAR)
1256 strcpy (token, BCOM);
1258 else if (group && ch == ECOM_CHAR)
1260 strcpy (token, ECOM);
1266 if (ch == BMES_CHAR) /* string token; read until unescaped EMES_CHAR */
1268 nread += read_mes (dtl, token+1);
1270 else if (ch == BSEQ_CHAR || ch == ESEQ_CHAR)
1272 /* token is complete */
1274 else /* any other string not containing (ECOM_CHAR or) whitespace */
1276 nread += read_misc (dtl, token+1);
1283 fprintf (stderr, "(read_token) : token = \"%s\"\n", token);
1286 return (nread); /* number of bytes read from dtl file */
1293 #define CHAR_EOS (-1)
1298 (FILE * fp, int * ch)
1305 int status = CHAR_OK; /* OK so far */
1308 if (read_char (fp, &c) == 0)
1309 status = CHAR_FAIL; /* fail */
1311 if (c == EMES_CHAR) /* end-of-string char */
1313 status = CHAR_EOS; /* end of string */
1315 else if (c == ESC_CHAR) /* escape character */
1317 /* accept the next character literally, even ESC_CHAR and EMES_CHAR */
1318 if (read_char (fp, &c) == 0)
1319 status = CHAR_FAIL; /* fail */
1325 /* read_string_char */
1331 (FILE * fp, Token token)
1340 /* loop ends at: end of fp file, or reading error, or a space */
1341 for (count=0; count <= MAXTOKLEN; ++count)
1343 if (read_char (fp, &c) == 0 || isspace (c))
1345 if (group && c == ECOM_CHAR)
1347 (Void) unread_char ();
1353 token[count] = '\0';
1362 (FILE * fp, char * token)
1368 /* called **AFTER** a BMES_CHAR has been read */
1369 /* read file fp for characters up to next unescaped EMES_CHAR */
1370 /* this is called a "string token" */
1371 /* write string, including EMES_CHAR, into token[] */
1372 /* return number of characters read from fp */
1374 COUNT dtl_count; /* number of DTL characters read by read_mes from fp */
1375 int more; /* flag more == 0 to terminate loop */
1376 int escape; /* flag escape == 1 if previous character was ESC_CHAR */
1377 int ch; /* current DTL character */
1384 if (read_char (fp, &ch) == 0)
1386 /* end of fp file, or reading error */
1389 else /* error checking passed */
1392 if (ch == EMES_CHAR && escape == 0) /* end of string */
1394 /* include final EMES_CHAR */
1398 else if (ch == ESC_CHAR && escape == 0)
1400 /* next character is not end of string */
1405 /* store any other character, */
1406 /* including escaped EMES_CHAR and ESC_CHAR*/
1420 /* wind input back, to allow rereading of one character */
1421 /* return 1 if this works, 0 on error */
1423 extern Line dtl_line;
1425 if (dtl_line.read > 0)
1427 -- dtl_line.read; /* back up one character in dtl_line */
1428 -- dtl_read; /* correct the count of DTL characters */
1429 -- com_read; /* count another DTL command character */
1430 status = 1; /* OK */
1432 else /* current DTL line is empty */
1434 status = 0; /* error */
1444 (char * command, int * opcode)
1455 for (i=0; i < NCMDS; i++)
1457 if ((cmd_table[i] != 0) && (strcmp (command, cmd_table[i]) == 0))
1480 if (byte < 0 || byte > 255)
1483 fprintf (stderr, "(check_byte) : INTERNAL ERROR : ");
1484 fprintf (stderr, "byte %d not in the range of 0 to 255.\n", byte);
1495 (FILE * dtl, FILE * dvi, int opcode)
1505 if (opcode >= 0 && opcode <= 127)
1506 ; /* SETCHAR uses no data */
1507 else if (opcode >= 128 && opcode <= 170)
1509 word_t this_bop_address = last_bop_address;
1511 if (opcode == 139) /* BOP */
1513 this_bop_address = dvi_written - 1;
1515 put_table (op_128_170, opcode, dtl, dvi);
1516 if (opcode == 139) /* BOP */
1518 xfer_bop_address (dtl, dvi);
1519 last_bop_address = this_bop_address;
1522 else if (opcode >= 171 && opcode <= 234)
1523 ; /* FONTNUM uses no data */
1524 else if (opcode >= 235 && opcode <= 238)
1525 put_table (fnt, opcode, dtl, dvi);
1526 else if (opcode >= 239 && opcode <= 242)
1529 special (dtl, dvi, n);
1531 else if (opcode >= 243 && opcode <= 246)
1534 fontdef (dtl, dvi, n);
1536 else if (opcode == 247)
1537 preamble (dtl, dvi);
1538 else if (opcode == 248)
1539 postamble (dtl, dvi);
1540 else if (opcode == 249)
1541 post_post (dtl, dvi);
1542 else if (opcode >= 250 && opcode <= 255)
1543 ; /* these, undefined, opcodes use no data */
1548 "(xfer_args) : opcode %d not handled.\n",
1560 (FILE * dtl, FILE * dvi)
1566 /* Called _after_ a BSEQ_CHAR command */
1567 /* Read bytes from dtl file, */
1568 /* writing corresponding SETCHAR or SET1 commands to DVI file, */
1569 /* _until_ unescaped ESEQ_CHAR is found */
1570 /* Return 1 if OK, 0 on error */
1571 /**** dt2dv assumes 8 bit characters, ****/
1572 /**** but some day one might change that. ****/
1574 int status = 1; /* status = 1 if OK, 0 if error */
1575 int more; /* sequence of font characters continuing? */
1576 int escape = 0; /* flag set if previous character was an escape */
1577 int ch; /* character read from DTL file */
1581 /* ignore read_char status, to allow unprintable characters */
1582 (Void) read_char (dtl, &ch);
1583 /* but check for end of dtl file, or serious file reading error */
1587 fprintf (stderr, "(set_seq) : ");
1588 fprintf (stderr, "end of dtl file, ");
1589 fprintf (stderr, "or serious dtl file reading error\n");
1592 status = 0; /* bad news */
1594 else /* read dtl file, okay */
1596 if (ch == ESC_CHAR && escape == 0) /* escape next character */
1602 if (ch == ESEQ_CHAR && escape == 0) /* end of sequence */
1606 else if (ch <= 127) /* can use SETCHAR */
1610 else if (ch <= 255) /* can use SET1 */
1612 put_byte (128, dvi); /* SET1 opcode */
1613 put_unsigned (1, (U4) ch, dvi);
1618 fprintf (stderr, "(set_seq) : ");
1620 "ERROR : DTL character %d is not in range 0 to 255\n",
1624 status = 0; /* Error, because dt2dv assumes 8 bit characters */
1626 escape = 0; /* current ch is not an escape character */
1638 (int n, FILE * dtl, FILE * dvi)
1645 /* translate unsigned n-byte hexadecimal number from dtl to dvi file. */
1646 /* return value of hexadecimal number */
1648 U4 unum = 0; /* at most this space needed */
1649 COUNT nread = 0; /* number of DTL bytes read by read_token */
1650 int nconv = 0; /* number of arguments converted by sscanf */
1651 static Token token = ""; /* DTL token */
1657 "(xfer_hex) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1662 nread = read_token (dtl, token);
1664 nconv = sscanf (token, XF4, &unum);
1669 fprintf (stderr, "(xfer_hex) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1670 dtl_filename, "hexadecimal number expected, not", token);
1674 put_unsigned (n, unum, dvi);
1684 (int n, FILE * dtl, FILE * dvi)
1691 /* translate unsigned n-byte octal number from dtl to dvi file. */
1692 /* return value of octal number */
1694 U4 unum = 0; /* at most this space needed */
1695 COUNT nread = 0; /* number of DTL bytes read by read_token */
1696 int nconv = 0; /* number of arguments converted by sscanf */
1697 static Token token = ""; /* DTL token */
1703 "(xfer_oct) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1708 nread = read_token (dtl, token);
1710 nconv = sscanf (token, OF4, &unum);
1715 fprintf (stderr, "(xfer_oct) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1716 dtl_filename, "octal number expected, not", token);
1720 put_unsigned (n, unum, dvi);
1730 (int n, FILE * dtl, FILE * dvi)
1737 /* translate unsigned n-byte number from dtl to dvi file. */
1738 /* return value of unsigned number */
1740 U4 unum = 0; /* at most this space needed */
1742 unum = get_unsigned (dtl);
1743 put_unsigned (n, unum, dvi);
1753 (int n, FILE * dtl, FILE * dvi)
1760 /* translate signed n-byte number from dtl to dvi file. */
1761 /* return value of signed number */
1765 snum = get_signed (dtl);
1766 put_signed (n, snum, dvi);
1781 /* read unsigned number from dtl file. */
1782 /* return value of unsigned number */
1784 U4 unum = 0; /* at most this space needed */
1785 COUNT nread = 0; /* number of DTL bytes read by read_token */
1786 int nconv = 0; /* number of arguments converted by sscanf */
1787 static Token token = ""; /* DTL token */
1789 nread = read_token (dtl, token);
1791 nconv = sscanf (token, UF4, &unum);
1796 fprintf (stderr, "(get_unsigned) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1797 dtl_filename, "unsigned number expected, not", token);
1814 /* read signed number from dtl file. */
1815 /* return value of signed number */
1818 COUNT nread = 0; /* number of DTL bytes read by read_token */
1819 int nconv = 0; /* number of sscanf arguments converted and assigned */
1820 static Token token = "";
1822 nread = read_token (dtl, token);
1824 nconv = sscanf (token, SF4, &snum);
1829 fprintf (stderr, "(get_signed) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1830 dtl_filename, "signed number expected, not", token);
1842 (int n, U4 unum, FILE * dvi)
1849 /* put unsigned in-byte integer to dvi file */
1850 /* DVI format uses Big-endian storage of numbers: */
1851 /* most significant byte is first. */
1852 /* return number of bytes written. */
1854 Byte ubyte[4]; /* at most 4 bytes needed in DVI format */
1861 "(put_unsigned) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1866 /* Big-endian storage. */
1867 for (i = 0; i < n; i++)
1869 ubyte[i] = (Byte) (unum % 256);
1872 /* Reverse order for big-endian representation. */
1873 for (i = n-1; i >= 0; i--)
1875 put_byte ((int) ubyte[i], dvi);
1886 (int n, S4 snum, FILE * dvi)
1893 /* put signed in-byte integer to dvi file */
1894 /* DVI format uses 2's complement Big-endian storage of signed numbers: */
1895 /* most significant byte is first. */
1896 /* return number of bytes written. */
1898 /* Will this deal properly with the sign? */
1904 "(put_signed) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1909 /* How do we ensure 2's complement representation? */
1910 /* Here's a trick that I hope is portable, given ANSI C. */
1911 /* See K&R (2nd edition), Appendix A6.2 "Integral Conversions". */
1913 /* Convert snum to U4 data type */
1914 put_unsigned (n, (U4) snum, dvi);
1929 /* check that a BMES_CHAR is the next non-whitespace character in dtl */
1931 int ch; /* next non-whitespace character in dtl */
1933 /* `(Void)' because we ignore the number of spaces skipped */
1934 (Void) skip_space (dtl, &ch);
1939 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1941 fprintf (stderr, "end of dtl file, or reading error\n");
1945 if (ch != BMES_CHAR)
1948 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1950 fprintf (stderr, "BMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1951 BMES_CHAR, "expected before string", ch, ch);
1968 /* check that an EMES_CHAR is the next character in dtl */
1970 int ch; /* dtl character */
1972 if (read_char (dtl, &ch) == 0)
1975 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1977 fprintf (stderr, "end of dtl file, or reading error\n");
1981 if (ch != EMES_CHAR)
1984 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1986 fprintf (stderr, "EMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1987 EMES_CHAR, "expected to follow string", ch, ch);
1996 /* Size typically used in this program for Lstring variables */
2003 (Lstring * lsp, long int n)
2012 lsp->s = (char *) gmalloc (n);
2029 lsp->s = NULL; /* to be sure */
2031 /* de_init_Lstring */
2044 lsp = (Lstring *) gmalloc (sizeof (Lstring));
2045 init_Lstring (lsp, n);
2069 (int ch, Lstring * lstr)
2075 /* write byte ch into Lstring *lstr */
2077 if (lstr->l >= lstr->m)
2080 fprintf (stderr, "(ls_putb) : ERROR : No more room in Lstring.\n");
2085 lstr->s [(lstr->l)++] = ch;
2094 (FILE * dtl, Lstring * lstr)
2100 /* get a string from dtl file, store as an Lstring in *lstr. */
2101 /* lstr must already be allocated and initialised. */
2102 /* return length of Lstring *lstr */
2105 int char_status = CHAR_OK; /* OK so far */
2110 fprintf (stderr, "(get_Lstring) : entering get_Lstring.\n");
2119 "(get_Lstring) : string is: \"");
2122 for (nch=0; ; nch++)
2126 char_status = read_string_char (dtl, &ch);
2128 if (char_status == CHAR_FAIL)
2130 /* end of dtl file, or reading error */
2131 fprintf (stderr, "\n");
2133 fprintf (stderr, "(get_Lstring) : DTL FILE ERROR (%s) : ",
2135 fprintf (stderr, "cannot read string[");
2136 fprintf (stderr, UF4, nch);
2137 fprintf (stderr, "] from dtl file.\n");
2143 fprintf (stderr, "%c", ch);
2146 if (char_status == CHAR_EOS)
2148 if (ch != EMES_CHAR)
2151 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2152 fprintf (stderr, "char_status = CHAR_FAIL,\n");
2154 "but ch = %c (char %d) is not EMES_CHAR = %c (char %d)\n",
2155 ch, ch, EMES_CHAR, EMES_CHAR);
2158 (Void) unread_char ();
2159 break; /* end of string */
2161 else if (char_status == CHAR_OK)
2168 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2169 fprintf (stderr, "char_status = %d is unfamiliar!\n", char_status);
2177 fprintf (stderr, "\".\n");
2185 fprintf (stderr, "(get_Lstring) : leaving get_Lstring.\n");
2196 (const Lstring * lstr, FILE * dvi)
2204 fwret = fwrite ((lstr->s), sizeof (char), (size_t) (lstr->l), dvi);
2206 dvi_written += fwret;
2208 if (fwret < lstr->l)
2212 "(put_Lstring) : DVI File ERROR : not all bytes written ");
2213 fprintf (stderr, "(%ld of %ld).\n",
2214 (long int) fwret, (long int) (lstr->l));
2224 (int n, FILE * dtl, FILE * dvi)
2231 /* transfer (length and) quoted string from dtl to dvi file, */
2232 /* return number of bytes written to dvi file. */
2234 U4 k, k2, lstr_maxsize;
2240 fprintf (stderr, "(xfer_len_string) : entering xfer_len_string.\n");
2243 /* k[n] : length of special string */
2245 k = get_unsigned (dtl);
2247 lstr_maxsize = (k > LSIZE) ? k : LSIZE;
2248 init_Lstring (&lstr, lstr_maxsize);
2253 fprintf (stderr, "(xfer_len_string) : string's nominal length k = ");
2254 fprintf (stderr, UF4, k);
2255 fprintf (stderr, " characters.\n");
2258 k2 = get_Lstring (dtl, &lstr);
2263 fprintf (stderr, "(xfer_len_string) : WARNING : string length (");
2264 fprintf (stderr, UF4, k);
2265 fprintf (stderr, ") in DTL file is wrong\n");
2266 fprintf (stderr, "Writing correct value (");
2267 fprintf (stderr, UF4, k2);
2268 fprintf (stderr, ") to DVI file\n");
2271 put_unsigned (n, k2, dvi);
2273 put_Lstring (&lstr, dvi);
2278 fprintf (stderr, "(xfer_len_string) : leaving xfer_len_string.\n");
2281 de_init_Lstring (&lstr);
2285 /* xfer_len_string */
2291 (FILE * dtl, FILE * dvi)
2297 /* translate signed 4-byte bop address from dtl to dvi file. */
2298 /* return value of bop address written to DVI file */
2300 S4 snum = 0; /* at most this space needed for byte address */
2301 COUNT nread = 0; /* number of DTL bytes read by read_token */
2302 int nconv = 0; /* number of arguments converted by sscanf */
2303 static Token token = ""; /* DTL token */
2305 nread += read_token (dtl, token);
2307 nconv = sscanf (token, SF4, &snum);
2312 fprintf (stderr, "(xfer_bop_address) : DTL FILE ERROR (%s) : ",
2314 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2318 if (snum != last_bop_address)
2321 fprintf (stderr, "(xfer_bop_address) : WARNING : byte address (");
2322 fprintf (stderr, WF, snum);
2323 fprintf (stderr, ")\n");
2324 fprintf (stderr, "for previous bop in DTL file is wrong\n");
2325 fprintf (stderr, "Writing correct value (");
2326 fprintf (stderr, WF, last_bop_address);
2327 fprintf (stderr, ") to DVI file\n");
2330 put_signed (4, last_bop_address, dvi);
2332 return last_bop_address;
2334 /* xfer_bop_address */
2338 xfer_postamble_address
2340 (FILE * dtl, FILE * dvi)
2346 /* translate signed 4-byte postamble address from dtl to dvi file. */
2347 /* return value of postamble address written to DVI file */
2349 S4 snum = 0; /* at most this space needed for byte address */
2350 COUNT nread = 0; /* number of DTL bytes read by read_token */
2351 int nconv = 0; /* number of arguments converted by sscanf */
2352 static Token token = ""; /* DTL token */
2354 nread += read_token (dtl, token);
2356 nconv = sscanf (token, SF4, &snum);
2361 fprintf (stderr, "(xfer_postamble_address) : DTL FILE ERROR (%s) : ",
2363 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2367 if (snum != postamble_address)
2370 fprintf (stderr, "(xfer_postamble_address) : WARNING : byte address (");
2371 fprintf (stderr, WF, snum);
2372 fprintf (stderr, ")\n");
2373 fprintf (stderr, "for postamble in DTL file is wrong\n");
2374 fprintf (stderr, "Writing correct value (");
2375 fprintf (stderr, WF, postamble_address);
2376 fprintf (stderr, ") to DVI file\n");
2379 put_signed (4, postamble_address, dvi);
2381 return postamble_address;
2383 /* xfer_postamble_address */
2389 (op_table table, int opcode, FILE * dtl, FILE * dvi)
2391 (table, opcode, dtl, dvi)
2398 /* table: {char * name; int first, last; op_info * list; }; */
2399 /* op_info: {int code; char * name; int nargs; char * args; }; */
2401 op_info op; /* entry in table */
2403 int pos; /* current position in string being scanned */
2404 static String args = "";
2406 /* Defensive programming. */
2407 if (opcode < table.first || opcode > table.last)
2411 "(put_table) : DTL FILE (OR INTERNAL) ERROR : opcode %d ", opcode);
2412 fprintf (stderr, "is outside table %s [ %d to %d ] !\n",
2413 table.name, table.first, table.last);
2417 op = table.list [ opcode - table.first ];
2419 /* More defensive programming. */
2420 if (opcode != op.code)
2424 "(put_table) : INTERNAL ERROR : opcode %d for command \"%s\"",
2426 fprintf (stderr, " faulty in table \"%s\".\n", table.name);
2430 /* process all the arguments, according to size and sign */
2432 strncpy (args, op.args, MAXSTRLEN);
2435 for (i=0; i < op.nargs; i++)
2438 int nscan = 0; /* number of bytes read by sscanf */
2439 int nconv = 0; /* number of sscanf arguments converted & assigned */
2441 /* sscanf() does NOT advance over its input: */
2442 /* C strings lack internal state information, which C files have. */
2443 /* On Sun/OS, sscanf calls ungetc on the string it reads, */
2444 /* which therefore has to be writable. */
2446 nconv = sscanf (args + pos, "%d%n", &argtype, &nscan);
2448 if (nconv < 1 || nscan < 1)
2452 "(put_table) : INTERNAL ERROR : internal read of table %s failed!\n",
2460 xfer_signed (-argtype, dtl, dvi);
2462 xfer_unsigned (argtype, dtl, dvi);
2471 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
2477 (FILE * dtl, FILE * dvi, int n)
2484 /* read special (1 <= n <= 4 byte) data from dtl, and write in dvi */
2485 /* return number of bytes written */
2492 fprintf (stderr, "(special) : entering special.\n");
2498 fprintf (stderr, "(special) : DTL FILE ERROR (%s) : special %d, ",
2500 fprintf (stderr, "range is 1 to 4.\n");
2504 /* k[n] : length of special string */
2505 /* x[k] : special string */
2507 nk = xfer_len_string (n, dtl, dvi);
2512 fprintf (stderr, "(special) : leaving special.\n");
2523 (FILE * dtl, FILE * dvi, int suffix)
2530 /* read fontdef fnt_def1 .. fnt_def4 from dtl, and write in dvi */
2531 /* suffix is the fontdef suffix : 1 to 4 */
2532 /* return number of bytes written */
2536 Lstring lstr1, lstr2;
2541 fprintf (stderr, "(fontdef) : entering fontdef.\n");
2544 if (suffix < 1 || suffix > 4)
2547 fprintf (stderr, "(fontdef) : DTL FILE ERROR (%s) : ",
2549 fprintf (stderr, "font def %d, but range is 1 to 4.\n", suffix);
2553 init_Lstring (&lstr1, LSIZE);
2554 init_Lstring (&lstr2, LSIZE);
2559 fprintf (stderr, "(fontdef) : about to read font number.\n");
2562 /* k[suffix] : font number */
2564 k = xfer_signed (suffix, dtl, dvi);
2566 k = xfer_unsigned (suffix, dtl, dvi);
2571 fprintf (stderr, "(fontdef) : font ");
2572 fprintf (stderr, UF4, k);
2573 fprintf (stderr, ".\n");
2577 /* c[4] : (hexadecimal) checksum : I (gt) would prefer this */
2578 xfer_hex (4, dtl, dvi);
2579 #else /*NOT HEX_CHECKSUM */
2580 /* c[4] : checksum (octal, for comparison with tftopl's .pl file) */
2581 xfer_oct (4, dtl, dvi);
2585 xfer_unsigned (4, dtl, dvi);
2588 xfer_unsigned (4, dtl, dvi);
2590 /* If DTL file's edited, a and l may be wrong. */
2592 /* a[1] : length of font `area' (directory) portion of pathname string */
2593 a = get_unsigned (dtl);
2595 /* l[1] : length of font portion of pathname string */
2596 l = get_unsigned (dtl);
2598 /* n[a+l] : font pathname string <= area + font */
2600 a2 = get_Lstring (dtl, &lstr1);
2605 fprintf (stderr, "(fontdef) : WARNING : font area string's length (");
2606 fprintf (stderr, UF4, a);
2607 fprintf (stderr, ") in DTL file is wrong\n");
2608 fprintf (stderr, "Writing correct value (");
2609 fprintf (stderr, UF4, a2);
2610 fprintf (stderr, ") to DVI file\n");
2613 put_unsigned (1, a2, dvi);
2615 l2 = get_Lstring (dtl, &lstr2);
2620 fprintf (stderr, "(fontdef) : WARNING : font string's length (");
2621 fprintf (stderr, UF4, l);
2622 fprintf (stderr, ") in DTL file is wrong\n");
2623 fprintf (stderr, "Writing correct value (");
2624 fprintf (stderr, UF4, l2);
2625 fprintf (stderr, ") to DVI file\n");
2628 put_unsigned (1, l2, dvi);
2630 put_Lstring (&lstr1, dvi);
2631 put_Lstring (&lstr2, dvi);
2633 de_init_Lstring (&lstr2);
2634 de_init_Lstring (&lstr1);
2639 fprintf (stderr, "(fontdef) : leaving fontdef.\n");
2642 return (suffix + 4*4 + 2*1 + a2 + l2);
2650 (FILE * dtl, FILE * dvi)
2656 /* read preamble from dtl, and write in dvi */
2657 /* return number of bytes written */
2664 fprintf (stderr, "(preamble) : entering preamble.\n");
2668 xfer_unsigned (1, dtl, dvi);
2671 xfer_unsigned (4, dtl, dvi);
2674 xfer_unsigned (4, dtl, dvi);
2677 xfer_unsigned (4, dtl, dvi);
2679 /* k[1] : length of comment */
2680 /* x[k] : comment string */
2682 k1 = xfer_len_string (1, dtl, dvi);
2687 fprintf (stderr, "(preamble) : leaving preamble.\n");
2690 return (1 + 3*4 + k1);
2698 (FILE * dtl, FILE * dvi)
2704 /* read postamble from dtl, and write in dvi */
2705 /* return number of bytes written */
2707 postamble_address = dvi_written - 1;
2709 /* p[4] : DVI address of previous bop command */
2710 /* --- unsigned? --- or signed, as I assume? */
2711 /* For, surely p should be -1 if the DVI file has NO bop? */
2712 xfer_bop_address (dtl, dvi);
2715 xfer_unsigned (4, dtl, dvi);
2718 xfer_unsigned (4, dtl, dvi);
2721 xfer_unsigned (4, dtl, dvi);
2724 xfer_unsigned (4, dtl, dvi);
2727 xfer_unsigned (4, dtl, dvi);
2730 xfer_unsigned (2, dtl, dvi);
2733 xfer_unsigned (2, dtl, dvi);
2743 (FILE * dtl, FILE * dvi)
2749 /* read post_post from dtl, and write in dvi */
2750 /* return number of bytes written */
2752 /* hope I'm writing the "223" bytes in an 8-bit clean way */
2753 int n223 = 0; /* number of "223" bytes in final padding */
2755 /* q[4] : DVI address of post command */
2756 /* --- unsigned? --- or signed, as I assume? */
2757 /* what happens if there is NO postamble command? */
2758 /* shouldn't q be -1 then? */
2760 xfer_postamble_address (dtl, dvi);
2762 /* i[1] : DVI identification byte = 2 */
2763 xfer_unsigned (1, dtl, dvi);
2765 for (n223 = 0; true; n223++)
2767 COUNT nread = 0; /* number of DTL bytes read by read_token */
2772 nread = read_token (dtl, token);
2774 /* check whether end of dtl file */
2779 /* dtl file shouldn't end before an ECOM */
2781 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2783 fprintf (stderr, "premature end of DTL file!\n");
2785 "%d complete iterations of \"padding byte\" loop;\n", n223);
2786 fprintf (stderr, "troublesome token = \"%s\"\n", token);
2789 /* leave the "223" loop */
2792 else if (strcmp (token, "223") == 0)
2794 /* token is a "223" padding byte */
2799 /* read a non-empty token that wasn't "223" */
2800 (Void) unread_char ();
2803 if (strcmp (token, ECOM) == 0)
2805 /* end of DTL's post_post command */
2809 /* error : expected end of post_post */
2811 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2813 fprintf (stderr, "token \"%s\" should be ECOM (\"%s\")\n",
2818 /* leave the "223" loop */
2827 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : \n",
2829 fprintf (stderr, "fewer than four `223' padding bytes.\n");
2830 fprintf (stderr, "Will write at least four `223' padding bytes.\n");
2833 /* check whether the DVI file size is a multiple of 4 bytes */
2834 if ((dvi_written + n223) % 4 != 0)
2837 fprintf (stderr, "(post_post) : WARNING : \n");
2838 fprintf (stderr, "DVI size ");
2839 fprintf (stderr, WF, dvi_written);
2840 fprintf (stderr, " (bytes) wouldn't be a multiple of 4 !\n");
2842 "Will write (at least four) `223' padding bytes until it is.\n");
2845 /* final padding of DVI file by "223" bytes to a multiple of 4 bytes, */
2846 /* with at least 4 bytes */
2848 for (n223 = 0; (n223 < 4) || (dvi_written % 4 != 0); n223++)
2850 /* add a "223" padding byte */
2851 put_byte (223, dvi);
2854 return (4 + 1 + n223);
2859 /* end of dt2dv.c */