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.2 - 14 September 2005
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 - Reference: "The DVI Driver Standard, Level 0",
17 by The TUG DVI Driver Standards Committee.
18 Appendix A, "Device-Independent File Format".
21 /* unix version; read from stdin, write to stdout, by default. */
32 /* by default, read and write regular files */
36 /* maximum number of characters in a DTL input line */
42 COUNT num; /* current line number */
43 size_t max; /* capacity of buf */
44 S4 wrote; /* number of characters written into buf */
45 size_t read; /* position in buf of next character to read from buf */
46 unsigned char * buf; /* line buffer */
49 char linebuf[MAXLINE+1];
51 Line dtl_line = {0, 0, 0, MAXLINE, linebuf};
53 /* a DTL token either is:
54 a quoted string (admitting an escape character),
55 or BCOM (if that is a nonempty string),
56 or ECOM (if that is a nonempty string),
57 or a string _not_ including ECOM_CHAR or space.
60 /* maximum expected length of a DTL token */
62 typedef char Token[MAXTOKLEN+1];
64 typedef unsigned char Byte;
70 /* command prefixes */
76 Byte first_suffix, last_suffix;
79 CmdPrefix cmd_prefixes [] =
81 {0, SETCHAR, true, 0, 127},
82 {128, SET, true, 1, 4},
83 {132, SETRULE, false, 0, 0},
84 {133, PUT, true, 1, 4},
85 {137, PUTRULE, false, 0, 0},
86 {138, NOP, false, 0, 0},
87 {139, BOP, false, 0, 0},
88 {140, EOP, false, 0, 0},
89 {141, PUSH, false, 0, 0},
90 {142, POP, false, 0, 0},
91 {143, RIGHT, true, 1, 4},
94 {157, DOWN, true, 1, 4},
97 {171, FONTNUM, true, 0, 63},
98 {235, FONT, true, 1, 4},
99 {239, SPECIAL, true, 1, 4},
100 {243, FONTDEF, true, 1, 4},
101 {247, PRE, false, 0, 0},
102 {248, POST, false, 0, 0},
103 {249, POSTPOST, false, 0, 0},
104 {250, OPCODE, true, 250, 255}
108 /* Number of DVI commands, including those officially undefined */
111 /* table of command name (string) pointers */
112 typedef char * CmdTable [NCMDS];
114 /* initially all command name pointers are NULL */
117 /* operation's opcode, name, number of args, string of arguments. */
126 /* name of table, first opcode, last opcode, pointer to opcode info. */
135 /* Table for opcodes 128 to 170 inclusive. */
137 op_info op_info_128_170 [] =
142 {131, SET4, 1, "-4"},
143 {132, SETRULE, 2, "-4 -4"},
147 {136, PUT4, 1, "-4"},
148 {137, PUTRULE, 2, "-4 -4"},
150 /* bop: not counting last argument, a signed address: */
151 {139, BOP, 10, "-4 -4 -4 -4 -4 -4 -4 -4 -4 -4"},
155 {143, RIGHT1, 1, "-1"},
156 {144, RIGHT2, 1, "-2"},
157 {145, RIGHT3, 1, "-3"},
158 {146, RIGHT4, 1, "-4"},
169 {157, DOWN1, 1, "-1"},
170 {158, DOWN2, 1, "-2"},
171 {159, DOWN3, 1, "-3"},
172 {160, DOWN4, 1, "-4"},
184 /* op_info op_info_128_170 [] */
186 op_table op_128_170 = {"op_128_170", 128, 170, op_info_128_170};
188 /* Table for fnt1 to fnt4 (opcodes 235 to 238) inclusive. */
192 {235, FONT1, 1, "1"},
193 {236, FONT2, 1, "2"},
194 {237, FONT3, 1, "3"},
195 {238, FONT4, 1, "-4"}
197 /* op_info fnt_n [] */
199 op_table fnt = {FONT, 235, 238, fnt_n};
202 /* Function prototypes */
204 Void mem_viol ARGS((int sig));
205 Void give_help (VOID);
206 int parse ARGS((char * s));
207 Void process ARGS((char * s));
210 Void dtl_stdin (VOID);
211 Void dvi_stdout (VOID);
213 int open_dtl ARGS((char * dtl_file, FILE ** pdtl));
214 int open_dvi ARGS((char * dvi_file, FILE ** pdvi));
216 int dt2dv ARGS((FILE * dtl, FILE * dvi));
218 Void * gmalloc ARGS((long int size));
221 Void dexit ARGS((int n));
223 int cons_cmds ARGS((int nprefixes, CmdPrefix * prefix, CmdTable cmds));
224 Void free_cmds ARGS((CmdTable cmd_table));
226 int get_line ARGS((FILE * fp, Line * line, int max));
227 int read_line_char ARGS((FILE * fp, int * ch));
228 int read_char ARGS((FILE * fp, int * ch));
229 int unread_char (VOID);
230 int read_string_char ARGS((FILE * fp, int * ch));
232 COUNT read_variety ARGS((FILE * dtl));
233 COUNT read_token ARGS((FILE * dtl, char * token));
234 COUNT skip_space ARGS((FILE * fp, int * ch));
235 COUNT read_misc ARGS((FILE * fp, Token token));
236 COUNT read_mes ARGS((FILE * fp, char * token));
238 int find_command ARGS((char * command, int * opcode));
239 int xfer_args ARGS((FILE * dtl, FILE * dvi, int opcode));
241 int set_seq ARGS((FILE * dtl, FILE * dvi));
243 int check_byte ARGS((int byte));
244 int put_byte ARGS((int onebyte, FILE * dvi));
246 U4 xfer_hex ARGS((int n, FILE * dtl, FILE * dvi));
247 U4 xfer_oct ARGS((int n, FILE * dtl, FILE * dvi));
248 U4 xfer_unsigned ARGS((int n, FILE * dtl, FILE * dvi));
249 S4 xfer_signed ARGS((int n, FILE * dtl, FILE * dvi));
251 int check_bmes ARGS((FILE * dtl));
252 int check_emes ARGS((FILE * dtl));
254 Void init_Lstring ARGS((Lstring * lsp, long int n));
255 Void de_init_Lstring ARGS((Lstring * lsp));
256 Lstring * alloc_Lstring ARGS((long int n));
257 Void free_Lstring ARGS((Lstring * lstr));
258 Void ls_putb ARGS((int ch, Lstring * lstr));
260 S4 get_Lstring ARGS((FILE * dtl, Lstring * lstr));
261 Void put_Lstring ARGS((const Lstring * lstr, FILE * dvi));
262 U4 xfer_len_string ARGS((int n, FILE * dtl, FILE * dvi));
264 U4 get_unsigned ARGS((FILE * dtl));
265 S4 get_signed ARGS((FILE * dtl));
267 int put_unsigned ARGS((int n, U4 unum, FILE * dvi));
268 int put_signed ARGS((int n, S4 snum, FILE * dvi));
270 S4 xfer_bop_address ARGS((FILE * dtl, FILE * dvi));
271 S4 xfer_postamble_address ARGS((FILE * dtl, FILE * dvi));
273 int put_table ARGS((op_table table, int opcode, FILE * dtl, FILE * dvi));
275 U4 special ARGS((FILE * dtl, FILE * dvi, int n));
276 int fontdef ARGS((FILE * dtl, FILE * dvi, int n));
278 U4 preamble ARGS((FILE * dtl, FILE * dvi));
279 int postamble ARGS((FILE * dtl, FILE * dvi));
280 int post_post ARGS((FILE * dtl, FILE * dvi));
285 char * keyword; /* command line option keyword */
286 int * p_var; /* pointer to option variable */
287 char * desc; /* description of keyword and value */
288 Void (* p_fn) (VOID); /* pointer to function called when option is set */
293 {"-debug", &debug, "detailed debugging", no_op},
294 {"-group", &group, "each DTL command is in parentheses", no_op},
295 {"-si", &rd_stdin, "read all DTL commands from standard input", dtl_stdin},
296 {"-so", &wr_stdout, "write all DVI commands to standard output", dvi_stdout},
297 {NULL, NULL, NULL, NULL}
301 char * progname = ""; /* intended for name of this program */
302 int nfile = 0; /* number of filename arguments on the command line */
304 #define PRINT_PROGNAME fprintf (stderr, "%s ", progname)
306 /* Harbison & Steele (1991) warn that some C implementations */
307 /* of free() do not treat NULL pointers correctly. */
308 #define gfree(p) {if (p) free (p);}
311 FILE * dtl_fp = NULL;
312 FILE * dvi_fp = NULL;
314 char * dtl_filename = "";
315 char * dvi_filename = "";
321 (int argc, char * argv[])
328 Void (*handler) ARGS((int)); /* Previous signal handler */
331 progname = argv[0]; /* name of this program */
333 /* memory violation signal handler */
335 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
348 #define C_LEVEL "non-"
349 #endif /* NOT STDC */
351 /* message about program and compiler */
352 /* NB: LTU EE's Sun/OS library is BSD, even though gcc 2.2.2 is ANSI */
354 fprintf (stderr, "\n");
356 "Program \"%s\" version %s compiled %s %s in %sstandard C.\n",
357 progname, VERSION, __DATE__, __TIME__, C_LEVEL);
359 /* interpret command line arguments */
362 dtl_fp = dvi_fp = NULL;
363 dtl_filename = dvi_filename = "";
365 for (i=1; i < argc; i++)
367 /* parse options, followed by any explicit filenames */
371 if (nfile != 2) /* not exactly two files specified, so give help */
375 dt2dv (dtl_fp, dvi_fp);
391 Void (* handler) ARGS((int));
392 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
397 "(mem_viol) : called with wrong signal!\n");
400 fprintf (stderr, "(mem_viol) : RUNTIME MEMORY ERROR : memory violation, ");
401 fprintf (stderr, "dtl line >= ");
402 fprintf (stderr, WF, dtl_line.num);
403 fprintf (stderr, "\n");
414 fprintf (stderr, "usage: ");
416 fprintf (stderr, "[options] dtl_file dvi_file");
417 fprintf (stderr, "\n");
418 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
420 fprintf (stderr, " ");
421 fprintf (stderr, "[%s]", keyword);
422 fprintf (stderr, " ");
423 fprintf (stderr, "%s", opts[i].desc);
424 fprintf (stderr, "\n");
426 fprintf (stderr, "Messages, like this one, go to stderr.\n");
436 Void dtl_stdin (VOID)
438 extern FILE * dtl_fp;
442 dtl_filename = "Standard Input";
446 Void dvi_stdout (VOID)
448 extern FILE * dvi_fp;
451 /* ! Perilous to monitors! */
453 dvi_filename = "Standard Output";
466 /* parse one command-line argument, `s' */
470 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
472 if (strncmp (s, keyword, strlen (keyword)) == 0)
475 (*(opts[i].p_var)) = 1; /* turn option on */
476 if ((pfn = opts[i].p_fn) != NULL)
477 (*pfn) (); /* call option function */
481 /* reached here, so not an option: assume it's a filename */
491 (char * dtl_file, FILE ** pdtl)
497 /* I: dtl_file; I: pdtl; O: *pdtl. */
499 extern char * dtl_filename;
501 dtl_filename = dtl_file;
503 if (dtl_filename == NULL)
507 "(open_dtl) : INTERNAL ERROR : dtl file's name is NULL.\n");
515 "(open_dtl) : INTERNAL ERROR : address of dtl variable is NULL.\n");
519 *pdtl = fopen (dtl_file, "r");
525 "(open_dtl) : DTL FILE ERROR : Cannot open \"%s\" for text reading.\n",
538 (char * dvi_file, FILE ** pdvi)
544 /* I: dvi_file; I: pdvi; O: *pdvi. */
546 extern char * dvi_filename;
548 dvi_filename = dvi_file;
550 if (dvi_filename == NULL)
554 "(open_dvi) : INTERNAL ERROR : dvi file's name is NULL.\n");
562 "(open_dvi) : INTERNAL ERROR : address of dvi variable is NULL.\n");
566 *pdvi = fopen (dvi_file, "wb");
572 "(open_dvi) : DVI FILE ERROR : Cannot open \"%s\" for binary writing.\n",
591 extern FILE * dtl_fp, * dvi_fp;
593 if (dtl_fp == NULL) /* first filename assumed to be DTL input */
595 open_dtl (s, &dtl_fp);
597 else if (dvi_fp == NULL) /* second filename assumed to be DVI output */
599 open_dvi (s, &dvi_fp);
604 fprintf (stderr, "(process) : at most two filenames allowed.\n");
612 COUNT dtl_read = 0; /* bytes read from dtl file */
613 COUNT dvi_written = 0; /* bytes written to dvi file */
614 word_t last_bop_address = -1; /* byte address of last bop; first bop uses -1 */
615 word_t postamble_address = -1; /* byte address of postamble */
616 COUNT ncom = 0; /* commands successfully read and interpreted from dtl file */
617 COUNT com_read = 0; /* bytes read in current (command and arguments), */
618 /* since and including the opening BCOM_CHAR, if any */
624 (int byte, FILE * dvi)
630 /* write byte into dvi file */
633 /* if (fprintf (dvi, "%c", byte) != 1) */
634 if (fprintf (dvi, "%c", byte) < 0)
638 "(put_byte) : DVI FILE ERROR (%s) : cannot write to dvi file.\n",
651 (FILE * dtl, FILE * dvi)
658 int nprefixes = 0; /* number of prefixes in cmd_prefixes[] list. */
659 static Token dtl_cmd = ""; /* DTL command name */
660 COUNT nread = 0; /* number of bytes read by a function from dtl file. */
662 nprefixes = sizeof (cmd_prefixes) / sizeof (CmdPrefix);
664 /* Construct array of all NCMDS == 256 DTL commands */
666 (Void) cons_cmds (nprefixes, cmd_prefixes, cmd_table);
668 /* DTL commands have the form "[ ]*command arg ... arg[ ]*", */
669 /* possibly enclosed in a BCOM, ECOM pair, */
670 /* and are separated by optional whitespace, typically newlines. */
671 /* That is, each command and its arguments are parenthesised, */
672 /* with optional spaces after the BCOM and before the ECOM, if any. */
674 /* dt2dv is now at the very start of the DTL file */
679 /* The very first thing should be the "variety" signature */
681 nread = read_variety (dtl);
683 /* while not end of dtl file or reading error, */
684 /* read, interpret, and write commands */
695 static Token token = ""; /* DTL token */
696 nread = read_token (dtl, token);
697 /* test for end of input, or reading error */
698 if (strlen (token) == 0)
703 fprintf (stderr, "(dt2dv) : end of input, or reading error.\n");
707 /* test whether this command begins correctly */
708 else if (strcmp (token, BCOM) != 0)
711 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
712 fprintf (stderr, "command must begin with \"%s\", ", BCOM);
713 fprintf (stderr, "not `%c' (char %d).\n", token[0], token[0]);
719 /* read the command name */
720 nread = read_token (dtl, dtl_cmd);
721 /* test for end of input, or reading error */
722 if (strlen (dtl_cmd) == 0)
728 "(dt2dv) : end of input, or reading error.\n");
737 fprintf (stderr, "(dt2dv) : command ");
738 fprintf (stderr, WF, ncom);
739 fprintf (stderr, " = \"%s\".\n", dtl_cmd);
742 /* find opcode for this command */
743 if (find_command (dtl_cmd, &opcode) == 1)
745 /* write the opcode, if we can */
746 put_byte (opcode, dvi);
748 /* treat the arguments, if any */
749 xfer_args (dtl, dvi, opcode);
751 else if (dtl_cmd[0] == BSEQ_CHAR)
753 /* sequence of font characters for SETCHAR */
760 "(dt2dv) : DTL FILE ERROR (%s) : unknown command \"%s\".\n",
761 dtl_filename, dtl_cmd);
768 /* seek ECOM after command's last argument and optional whitespace */
769 static Token token = ""; /* DTL token */
770 nread = read_token (dtl, token);
771 /* test for end of input, or reading error */
772 if (strlen (token) == 0)
778 "(dt2dv) : end of input, or reading error.\n");
782 if (strcmp (token, ECOM) != 0)
785 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
786 fprintf (stderr, "ECOM (\"%s\") expected, not `%c' (char %d).\n",
787 ECOM, token[0], token[0]);
793 ++ ncom; /* one more command successfully read and interpreted */
798 fprintf (stderr, "(dt2dv) :\n");
799 fprintf (stderr, "Read (from file \"%s\") ", dtl_filename);
800 fprintf (stderr, WF, dtl_read);
801 fprintf (stderr, " DTL bytes (");
802 fprintf (stderr, UF4, dtl_line.num);
803 fprintf (stderr, " lines);\n");
804 fprintf (stderr, "wrote (to file \"%s\") ", dvi_filename);
805 fprintf (stderr, WF, dvi_written);
806 fprintf (stderr, " DVI bytes;\n");
807 fprintf (stderr, "completely interpreted ");
808 fprintf (stderr, WF, ncom);
809 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
810 fprintf (stderr, "\n");
812 (Void) free_cmds (cmd_table);
832 fprintf (stderr, "(gmalloc) : INTERNAL ERROR : ");
834 "unreasonable request to malloc %ld bytes\n",
838 p = (Void *) malloc ((size_t) size);
842 fprintf (stderr, "(gmalloc) : MEMORY ALLOCATION ERROR : ");
844 "operating system failed to malloc %ld bytes\n",
857 fprintf (stderr, "(dinfo) : ");
858 fprintf (stderr, "Current DTL input line ");
859 fprintf (stderr, UF4, dtl_line.num);
860 fprintf (stderr, " :\n");
861 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
862 fprintf (stderr, "Read ");
863 fprintf (stderr, WF, dtl_read);
864 fprintf (stderr, " DTL bytes (");
865 fprintf (stderr, WF, com_read);
866 fprintf (stderr, " in current command), wrote ");
867 fprintf (stderr, WF, dvi_written);
868 fprintf (stderr, " DVI bytes.\n");
869 fprintf (stderr, "Successfully interpreted ");
870 fprintf (stderr, WF, ncom);
871 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
887 fprintf (stderr, "(dexit) : exiting with status %d.\n", n);
896 (int nprefixes, CmdPrefix * prefix, CmdTable cmds)
898 (nprefixes, prefix, cmds)
904 int code; /* first opcode for a given command prefix */
905 int opcode; /* command's opcode */
906 int nsuffixes; /* number of commands with a given prefix */
907 int isuffix; /**** integer suffix, of at most three digits ****/
908 String suffix; /* suffix string generated from integer suffix */
909 size_t plen = 0; /* prefix length */
910 size_t slen; /* suffix length */
911 size_t clen; /* whole command name length */
912 int i, j; /* loop indices */
914 for (i=0; i < nprefixes; prefix++, i++)
916 code = prefix->first_code;
917 if (code < 0 || code > 255)
920 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
922 "prefix listed internally with code = %d, must be 0 to 255\n",
926 if (prefix->has_suffix)
928 plen = strlen (prefix->name);
929 /**** Suffixes in DTL are Integers, in Sequence */
930 if (prefix->last_suffix < prefix->first_suffix)
933 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
935 "prefix's last suffix %d < first suffix (%d)\n",
936 prefix->last_suffix, prefix->first_suffix);
939 nsuffixes = prefix->last_suffix - prefix->first_suffix + 1;
940 opcode = prefix->first_code;
941 for (j=0; j < nsuffixes; j++, opcode++)
943 isuffix = prefix->first_suffix + j;
944 if (0 <= code && code <= 127) /* SETCHAR */
946 /* SETCHAR's suffix is written in uppercase hexadecimal */
947 sprintf (suffix, "%02X", isuffix);
949 else /* 128 <= code && code <= 255 */ /* other DTL commands */
951 /* other commands' suffices are written in decimal */
952 sprintf (suffix, "%d", isuffix);
954 slen = strlen (suffix);
956 cmds[opcode] = (char *) gmalloc (clen+1);
957 strcpy (cmds[opcode], prefix->name);
958 strcat (cmds[opcode], suffix);
961 else /* command name = prefix */
963 plen = strlen (prefix->name);
965 opcode = prefix->first_code;
966 cmds[opcode] = (char *) gmalloc (clen+1);
967 strcpy (cmds[opcode], prefix->name);
986 for (i=0; i < NCMDS; i++)
987 gfree (cmd_table[i]);
995 (FILE * fp, Line * line, int max)
1002 /* read a (Line *) line from fp, return length */
1003 /* adapted from K&R (second, alias ANSI C, edition, 1988), page 165 */
1005 if (fgets (line->buf, max, fp) == NULL)
1010 line->wrote = strlen (line->buf);
1021 (FILE * fp, int * ch)
1027 /* read one character from dtl_line if possible, */
1028 /* otherwise read another dtl_line from fp */
1029 /* return 1 if a character is read, 0 if at end of fp file */
1031 extern Line dtl_line;
1032 if (dtl_line.wrote == 0 || dtl_line.read >= dtl_line.wrote)
1035 /* refill line buffer */
1036 line_status = get_line (fp, &dtl_line, MAXLINE);
1037 if (line_status == 0)
1039 /* at end of DTL file */
1043 fprintf (stderr, "(read_line_char) : end of DTL file\n");
1050 /* new DTL line was read */
1054 fprintf (stderr, "(read_line_char) : new DTL input line:\n");
1055 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
1059 *ch = dtl_line.buf [dtl_line.read ++];
1061 ++ com_read; /* count another DTL command character */
1064 /* read_line_char */
1070 (FILE * fp, int * ch)
1076 /* Read next character, if any, from file fp. */
1077 /* Write it into *ch. */
1078 /* If no character is read, then *ch value < 0. */
1079 /* Return 1 if OK, 0 if EOF or error. */
1082 int c; /* in case ch points awry, we still have something in c. */
1085 if (read_line_char (fp, &c) == 0)
1087 /* end of fp file, or error reading it */
1096 "(read_char) : character %d not in range 0 to 255\n",
1101 else if ( ! isprint (c & 0x7f) && ! isspace (c))
1105 "(read_char) : character %d %s.\n",
1107 "not printable and not white space");
1127 /* read and check DTL variety signature */
1128 /* return number of DTL bytes written */
1129 /* DTL variety is _NEVER_ grouped by BCOM and ECOM. */
1130 /* Uniformity here enables the program easily to modify its behavior. */
1132 COUNT vread = 0; /* number of DTL bytes read by read_variety */
1133 COUNT nread = 0; /* number of DTL bytes read by read_token */
1134 static Token token = "";
1136 /* read the DTL VARIETY keyword */
1137 nread = read_token (dtl, token);
1139 /* test whether signature begins correctly */
1140 if (strcmp (token, "variety") != 0)
1143 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1144 fprintf (stderr, "DTL signature must begin with \"%s\", not \"%s\".\n",
1149 /* read the DTL variety */
1150 nread = read_token (dtl, token);
1152 /* test whether variety is correct */
1153 if (strcmp (token, VARIETY) != 0)
1156 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1157 fprintf (stderr, "DTL variety must be \"%s\", not \"%s\".\n",
1163 fprintf (stderr, "(read_variety) : DTL variety %s is OK.\n", VARIETY);
1165 return vread; /* OK */
1173 (FILE * fp, int * ch)
1179 /* Skip whitespace characters in file fp. */
1180 /* Write in *ch the last character read from fp, */
1181 /* or < 0 if fp could not be read. */
1182 /* Return number of characters read from fp. */
1184 int c; /* character read (if any) */
1185 COUNT count; /* number (0 or more) of whitespace characters read */
1186 int nchar; /* number (0 or 1) of characters read by read_char */
1188 /* loop ends at: end of fp file, or reading error, or not a white space */
1189 for (count=0; ((nchar = read_char (fp, &c)) == 1 && isspace (c)); ++count)
1191 /* otherwise, more white spaces to skip */
1194 /* report when each DTL end of line is reached */
1198 fprintf (stderr, "(skip_space) : ");
1199 fprintf (stderr, "end of DTL line (at least) ");
1200 fprintf (stderr, WF, dtl_line.num);
1201 fprintf (stderr, "\n");
1211 *ch = c; /* c will be < 0 if read_char could not read fp */
1212 return (count + nchar);
1220 (FILE * dtl, char * token)
1226 /* read next token from dtl file. */
1227 /* return number of DTL bytes read. */
1228 /* A token is one of:
1229 a string from BMES_CHAR to the next unescaped EMES_CHAR, inclusive;
1230 BCOM or ECOM, unless these are empty strings;
1232 any other sequence of non-whitespace characters.
1235 COUNT nread; /* number of DTL bytes read by read_token */
1236 int ch; /* most recent character read */
1240 /* obtain first non-space character */
1241 /* add to nread the number of characters read from dtl by skip_space */
1242 nread += skip_space (dtl, &ch);
1246 /* end of dtl file */
1247 /* write an empty token */
1252 fprintf (stderr, "(read_token) : end of dtl file.\n");
1255 else if (group && ch == BCOM_CHAR)
1257 strcpy (token, BCOM);
1259 else if (group && ch == ECOM_CHAR)
1261 strcpy (token, ECOM);
1267 if (ch == BMES_CHAR) /* string token; read until unescaped EMES_CHAR */
1269 nread += read_mes (dtl, token+1);
1271 else if (ch == BSEQ_CHAR || ch == ESEQ_CHAR)
1273 /* token is complete */
1275 else /* any other string not containing (ECOM_CHAR or) whitespace */
1277 nread += read_misc (dtl, token+1);
1284 fprintf (stderr, "(read_token) : token = \"%s\"\n", token);
1287 return (nread); /* number of bytes read from dtl file */
1294 #define CHAR_EOS (-1)
1299 (FILE * fp, int * ch)
1306 int status = CHAR_OK; /* OK so far */
1309 if (read_char (fp, &c) == 0)
1310 status = CHAR_FAIL; /* fail */
1312 if (c == EMES_CHAR) /* end-of-string char */
1314 status = CHAR_EOS; /* end of string */
1316 else if (c == ESC_CHAR) /* escape character */
1318 /* accept the next character literally, even ESC_CHAR and EMES_CHAR */
1319 if (read_char (fp, &c) == 0)
1320 status = CHAR_FAIL; /* fail */
1326 /* read_string_char */
1332 (FILE * fp, Token token)
1341 /* loop ends at: end of fp file, or reading error, or a space */
1342 for (count=0; count <= MAXTOKLEN; ++count)
1344 if (read_char (fp, &c) == 0 || isspace (c))
1346 if (group && c == ECOM_CHAR)
1348 (Void) unread_char ();
1354 token[count] = '\0';
1363 (FILE * fp, char * token)
1369 /* called **AFTER** a BMES_CHAR has been read */
1370 /* read file fp for characters up to next unescaped EMES_CHAR */
1371 /* this is called a "string token" */
1372 /* write string, including EMES_CHAR, into token[] */
1373 /* return number of characters read from fp */
1375 COUNT dtl_count; /* number of DTL characters read by read_mes from fp */
1376 int more; /* flag more == 0 to terminate loop */
1377 int escape; /* flag escape == 1 if previous character was ESC_CHAR */
1378 int ch; /* current DTL character */
1385 if (read_char (fp, &ch) == 0)
1387 /* end of fp file, or reading error */
1390 else /* error checking passed */
1393 if (ch == EMES_CHAR && escape == 0) /* end of string */
1395 /* include final EMES_CHAR */
1399 else if (ch == ESC_CHAR && escape == 0)
1401 /* next character is not end of string */
1406 /* store any other character, */
1407 /* including escaped EMES_CHAR and ESC_CHAR*/
1421 /* wind input back, to allow rereading of one character */
1422 /* return 1 if this works, 0 on error */
1424 extern Line dtl_line;
1426 if (dtl_line.read > 0)
1428 -- dtl_line.read; /* back up one character in dtl_line */
1429 -- dtl_read; /* correct the count of DTL characters */
1430 -- com_read; /* count another DTL command character */
1431 status = 1; /* OK */
1433 else /* current DTL line is empty */
1435 status = 0; /* error */
1445 (char * command, int * opcode)
1456 for (i=0; i < NCMDS; i++)
1458 if ((cmd_table[i] != 0) && (strcmp (command, cmd_table[i]) == 0))
1481 if (byte < 0 || byte > 255)
1484 fprintf (stderr, "(check_byte) : INTERNAL ERROR : ");
1485 fprintf (stderr, "byte %d not in the range of 0 to 255.\n", byte);
1496 (FILE * dtl, FILE * dvi, int opcode)
1506 if (opcode >= 0 && opcode <= 127)
1507 ; /* SETCHAR uses no data */
1508 else if (opcode >= 128 && opcode <= 170)
1510 word_t this_bop_address = last_bop_address;
1512 if (opcode == 139) /* BOP */
1514 this_bop_address = dvi_written - 1;
1516 put_table (op_128_170, opcode, dtl, dvi);
1517 if (opcode == 139) /* BOP */
1519 xfer_bop_address (dtl, dvi);
1520 last_bop_address = this_bop_address;
1523 else if (opcode >= 171 && opcode <= 234)
1524 ; /* FONTNUM uses no data */
1525 else if (opcode >= 235 && opcode <= 238)
1526 put_table (fnt, opcode, dtl, dvi);
1527 else if (opcode >= 239 && opcode <= 242)
1530 special (dtl, dvi, n);
1532 else if (opcode >= 243 && opcode <= 246)
1535 fontdef (dtl, dvi, n);
1537 else if (opcode == 247)
1538 preamble (dtl, dvi);
1539 else if (opcode == 248)
1540 postamble (dtl, dvi);
1541 else if (opcode == 249)
1542 post_post (dtl, dvi);
1543 else if (opcode >= 250 && opcode <= 255)
1544 ; /* these, undefined, opcodes use no data */
1549 "(xfer_args) : opcode %d not handled.\n",
1561 (FILE * dtl, FILE * dvi)
1567 /* Called _after_ a BSEQ_CHAR command */
1568 /* Read bytes from dtl file, */
1569 /* writing corresponding SETCHAR or SET1 commands to DVI file, */
1570 /* _until_ unescaped ESEQ_CHAR is found */
1571 /* Return 1 if OK, 0 on error */
1572 /**** dt2dv assumes 8 bit characters, ****/
1573 /**** but some day one might change that. ****/
1575 int status = 1; /* status = 1 if OK, 0 if error */
1576 int more; /* sequence of font characters continuing? */
1577 int escape = 0; /* flag set if previous character was an escape */
1578 int ch; /* character read from DTL file */
1582 /* ignore read_char status, to allow unprintable characters */
1583 (Void) read_char (dtl, &ch);
1584 /* but check for end of dtl file, or serious file reading error */
1588 fprintf (stderr, "(set_seq) : ");
1589 fprintf (stderr, "end of dtl file, ");
1590 fprintf (stderr, "or serious dtl file reading error\n");
1593 status = 0; /* bad news */
1595 else /* read dtl file, okay */
1597 if (ch == ESC_CHAR && escape == 0) /* escape next character */
1603 if (ch == ESEQ_CHAR && escape == 0) /* end of sequence */
1607 else if (ch <= 127) /* can use SETCHAR */
1611 else if (ch <= 255) /* can use SET1 */
1613 put_byte (128, dvi); /* SET1 opcode */
1614 put_unsigned (1, (U4) ch, dvi);
1619 fprintf (stderr, "(set_seq) : ");
1621 "ERROR : DTL character %d is not in range 0 to 255\n",
1625 status = 0; /* Error, because dt2dv assumes 8 bit characters */
1627 escape = 0; /* current ch is not an escape character */
1639 (int n, FILE * dtl, FILE * dvi)
1646 /* translate unsigned n-byte hexadecimal number from dtl to dvi file. */
1647 /* return value of hexadecimal number */
1649 U4 unum = 0; /* at most this space needed */
1650 COUNT nread = 0; /* number of DTL bytes read by read_token */
1651 int nconv = 0; /* number of arguments converted by sscanf */
1652 static Token token = ""; /* DTL token */
1658 "(xfer_hex) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1663 nread = read_token (dtl, token);
1665 nconv = sscanf (token, XF4, &unum);
1670 fprintf (stderr, "(xfer_hex) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1671 dtl_filename, "hexadecimal number expected, not", token);
1675 put_unsigned (n, unum, dvi);
1685 (int n, FILE * dtl, FILE * dvi)
1692 /* translate unsigned n-byte octal number from dtl to dvi file. */
1693 /* return value of octal number */
1695 U4 unum = 0; /* at most this space needed */
1696 COUNT nread = 0; /* number of DTL bytes read by read_token */
1697 int nconv = 0; /* number of arguments converted by sscanf */
1698 static Token token = ""; /* DTL token */
1704 "(xfer_oct) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1709 nread = read_token (dtl, token);
1711 nconv = sscanf (token, OF4, &unum);
1716 fprintf (stderr, "(xfer_oct) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1717 dtl_filename, "octal number expected, not", token);
1721 put_unsigned (n, unum, dvi);
1731 (int n, FILE * dtl, FILE * dvi)
1738 /* translate unsigned n-byte number from dtl to dvi file. */
1739 /* return value of unsigned number */
1741 U4 unum = 0; /* at most this space needed */
1743 unum = get_unsigned (dtl);
1744 put_unsigned (n, unum, dvi);
1754 (int n, FILE * dtl, FILE * dvi)
1761 /* translate signed n-byte number from dtl to dvi file. */
1762 /* return value of signed number */
1766 snum = get_signed (dtl);
1767 put_signed (n, snum, dvi);
1782 /* read unsigned number from dtl file. */
1783 /* return value of unsigned number */
1785 U4 unum = 0; /* at most this space needed */
1786 COUNT nread = 0; /* number of DTL bytes read by read_token */
1787 int nconv = 0; /* number of arguments converted by sscanf */
1788 static Token token = ""; /* DTL token */
1790 nread = read_token (dtl, token);
1792 nconv = sscanf (token, UF4, &unum);
1797 fprintf (stderr, "(get_unsigned) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1798 dtl_filename, "unsigned number expected, not", token);
1815 /* read signed number from dtl file. */
1816 /* return value of signed number */
1819 COUNT nread = 0; /* number of DTL bytes read by read_token */
1820 int nconv = 0; /* number of sscanf arguments converted and assigned */
1821 static Token token = "";
1823 nread = read_token (dtl, token);
1825 nconv = sscanf (token, SF4, &snum);
1830 fprintf (stderr, "(get_signed) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1831 dtl_filename, "signed number expected, not", token);
1843 (int n, U4 unum, FILE * dvi)
1850 /* put unsigned in-byte integer to dvi file */
1851 /* DVI format uses Big-endian storage of numbers: */
1852 /* most significant byte is first. */
1853 /* return number of bytes written. */
1855 Byte ubyte[4]; /* at most 4 bytes needed in DVI format */
1862 "(put_unsigned) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1867 /* Big-endian storage. */
1868 for (i = 0; i < n; i++)
1870 ubyte[i] = (Byte) (unum % 256);
1873 /* Reverse order for big-endian representation. */
1874 for (i = n-1; i >= 0; i--)
1876 put_byte ((int) ubyte[i], dvi);
1887 (int n, S4 snum, FILE * dvi)
1894 /* put signed in-byte integer to dvi file */
1895 /* DVI format uses 2's complement Big-endian storage of signed numbers: */
1896 /* most significant byte is first. */
1897 /* return number of bytes written. */
1899 /* Will this deal properly with the sign? */
1905 "(put_signed) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1910 /* How do we ensure 2's complement representation? */
1911 /* Here's a trick that I hope is portable, given ANSI C. */
1912 /* See K&R (2nd edition), Appendix A6.2 "Integral Conversions". */
1914 /* Convert snum to U4 data type */
1915 put_unsigned (n, (U4) snum, dvi);
1930 /* check that a BMES_CHAR is the next non-whitespace character in dtl */
1932 int ch; /* next non-whitespace character in dtl */
1934 /* `(Void)' because we ignore the number of spaces skipped */
1935 (Void) skip_space (dtl, &ch);
1940 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1942 fprintf (stderr, "end of dtl file, or reading error\n");
1946 if (ch != BMES_CHAR)
1949 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1951 fprintf (stderr, "BMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1952 BMES_CHAR, "expected before string", ch, ch);
1969 /* check that an EMES_CHAR is the next character in dtl */
1971 int ch; /* dtl character */
1973 if (read_char (dtl, &ch) == 0)
1976 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1978 fprintf (stderr, "end of dtl file, or reading error\n");
1982 if (ch != EMES_CHAR)
1985 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1987 fprintf (stderr, "EMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1988 EMES_CHAR, "expected to follow string", ch, ch);
1997 /* Size typically used in this program for Lstring variables */
2004 (Lstring * lsp, long int n)
2013 lsp->s = (char *) gmalloc (n);
2030 lsp->s = NULL; /* to be sure */
2032 /* de_init_Lstring */
2045 lsp = (Lstring *) gmalloc (sizeof (Lstring));
2046 init_Lstring (lsp, n);
2070 (int ch, Lstring * lstr)
2076 /* write byte ch into Lstring *lstr */
2078 if (lstr->l >= lstr->m)
2081 fprintf (stderr, "(ls_putb) : ERROR : No more room in Lstring.\n");
2086 lstr->s [(lstr->l)++] = ch;
2095 (FILE * dtl, Lstring * lstr)
2101 /* get a string from dtl file, store as an Lstring in *lstr. */
2102 /* lstr must already be allocated and initialised. */
2103 /* return length of Lstring *lstr */
2106 int char_status = CHAR_OK; /* OK so far */
2111 fprintf (stderr, "(get_Lstring) : entering get_Lstring.\n");
2120 "(get_Lstring) : string is: \"");
2123 for (nch=0; ; nch++)
2127 char_status = read_string_char (dtl, &ch);
2129 if (char_status == CHAR_FAIL)
2131 /* end of dtl file, or reading error */
2132 fprintf (stderr, "\n");
2134 fprintf (stderr, "(get_Lstring) : DTL FILE ERROR (%s) : ",
2136 fprintf (stderr, "cannot read string[");
2137 fprintf (stderr, UF4, nch);
2138 fprintf (stderr, "] from dtl file.\n");
2144 fprintf (stderr, "%c", ch);
2147 if (char_status == CHAR_EOS)
2149 if (ch != EMES_CHAR)
2152 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2153 fprintf (stderr, "char_status = CHAR_FAIL,\n");
2155 "but ch = %c (char %d) is not EMES_CHAR = %c (char %d)\n",
2156 ch, ch, EMES_CHAR, EMES_CHAR);
2159 (Void) unread_char ();
2160 break; /* end of string */
2162 else if (char_status == CHAR_OK)
2169 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2170 fprintf (stderr, "char_status = %d is unfamiliar!\n", char_status);
2178 fprintf (stderr, "\".\n");
2186 fprintf (stderr, "(get_Lstring) : leaving get_Lstring.\n");
2197 (const Lstring * lstr, FILE * dvi)
2205 fwret = fwrite ((lstr->s), sizeof (char), (size_t) (lstr->l), dvi);
2207 dvi_written += fwret;
2209 if (fwret < lstr->l)
2213 "(put_Lstring) : DVI File ERROR : not all bytes written ");
2214 fprintf (stderr, "(%ld of %ld).\n",
2215 (long int) fwret, (long int) (lstr->l));
2225 (int n, FILE * dtl, FILE * dvi)
2232 /* transfer (length and) quoted string from dtl to dvi file, */
2233 /* return number of bytes written to dvi file. */
2235 U4 k, k2, lstr_maxsize;
2241 fprintf (stderr, "(xfer_len_string) : entering xfer_len_string.\n");
2244 /* k[n] : length of special string */
2246 k = get_unsigned (dtl);
2248 lstr_maxsize = (k > LSIZE) ? k : LSIZE;
2249 init_Lstring (&lstr, lstr_maxsize);
2254 fprintf (stderr, "(xfer_len_string) : string's nominal length k = ");
2255 fprintf (stderr, UF4, k);
2256 fprintf (stderr, " characters.\n");
2259 k2 = get_Lstring (dtl, &lstr);
2264 fprintf (stderr, "(xfer_len_string) : WARNING : string length (");
2265 fprintf (stderr, UF4, k);
2266 fprintf (stderr, ") in DTL file is wrong\n");
2267 fprintf (stderr, "Writing correct value (");
2268 fprintf (stderr, UF4, k2);
2269 fprintf (stderr, ") to DVI file\n");
2272 put_unsigned (n, k2, dvi);
2274 put_Lstring (&lstr, dvi);
2279 fprintf (stderr, "(xfer_len_string) : leaving xfer_len_string.\n");
2282 de_init_Lstring (&lstr);
2286 /* xfer_len_string */
2292 (FILE * dtl, FILE * dvi)
2298 /* translate signed 4-byte bop address from dtl to dvi file. */
2299 /* return value of bop address written to DVI file */
2301 S4 snum = 0; /* at most this space needed for byte address */
2302 COUNT nread = 0; /* number of DTL bytes read by read_token */
2303 int nconv = 0; /* number of arguments converted by sscanf */
2304 static Token token = ""; /* DTL token */
2306 nread += read_token (dtl, token);
2308 nconv = sscanf (token, SF4, &snum);
2313 fprintf (stderr, "(xfer_bop_address) : DTL FILE ERROR (%s) : ",
2315 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2319 if (snum != last_bop_address)
2322 fprintf (stderr, "(xfer_bop_address) : WARNING : byte address (");
2323 fprintf (stderr, WF, snum);
2324 fprintf (stderr, ")\n");
2325 fprintf (stderr, "for previous bop in DTL file is wrong\n");
2326 fprintf (stderr, "Writing correct value (");
2327 fprintf (stderr, WF, last_bop_address);
2328 fprintf (stderr, ") to DVI file\n");
2331 put_signed (4, last_bop_address, dvi);
2333 return last_bop_address;
2335 /* xfer_bop_address */
2339 xfer_postamble_address
2341 (FILE * dtl, FILE * dvi)
2347 /* translate signed 4-byte postamble address from dtl to dvi file. */
2348 /* return value of postamble address written to DVI file */
2350 S4 snum = 0; /* at most this space needed for byte address */
2351 COUNT nread = 0; /* number of DTL bytes read by read_token */
2352 int nconv = 0; /* number of arguments converted by sscanf */
2353 static Token token = ""; /* DTL token */
2355 nread += read_token (dtl, token);
2357 nconv = sscanf (token, SF4, &snum);
2362 fprintf (stderr, "(xfer_postamble_address) : DTL FILE ERROR (%s) : ",
2364 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2368 if (snum != postamble_address)
2371 fprintf (stderr, "(xfer_postamble_address) : WARNING : byte address (");
2372 fprintf (stderr, WF, snum);
2373 fprintf (stderr, ")\n");
2374 fprintf (stderr, "for postamble in DTL file is wrong\n");
2375 fprintf (stderr, "Writing correct value (");
2376 fprintf (stderr, WF, postamble_address);
2377 fprintf (stderr, ") to DVI file\n");
2380 put_signed (4, postamble_address, dvi);
2382 return postamble_address;
2384 /* xfer_postamble_address */
2390 (op_table table, int opcode, FILE * dtl, FILE * dvi)
2392 (table, opcode, dtl, dvi)
2399 /* table: {char * name; int first, last; op_info * list; }; */
2400 /* op_info: {int code; char * name; int nargs; char * args; }; */
2402 op_info op; /* entry in table */
2404 int pos; /* current position in string being scanned */
2405 static String args = "";
2407 /* Defensive programming. */
2408 if (opcode < table.first || opcode > table.last)
2412 "(put_table) : DTL FILE (OR INTERNAL) ERROR : opcode %d ", opcode);
2413 fprintf (stderr, "is outside table %s [ %d to %d ] !\n",
2414 table.name, table.first, table.last);
2418 op = table.list [ opcode - table.first ];
2420 /* More defensive programming. */
2421 if (opcode != op.code)
2425 "(put_table) : INTERNAL ERROR : opcode %d for command \"%s\"",
2427 fprintf (stderr, " faulty in table \"%s\".\n", table.name);
2431 /* process all the arguments, according to size and sign */
2433 strncpy (args, op.args, MAXSTRLEN);
2436 for (i=0; i < op.nargs; i++)
2439 int nscan = 0; /* number of bytes read by sscanf */
2440 int nconv = 0; /* number of sscanf arguments converted & assigned */
2442 /* sscanf() does NOT advance over its input: */
2443 /* C strings lack internal state information, which C files have. */
2444 /* On Sun/OS, sscanf calls ungetc on the string it reads, */
2445 /* which therefore has to be writable. */
2447 nconv = sscanf (args + pos, "%d%n", &argtype, &nscan);
2449 if (nconv < 1 || nscan < 1)
2453 "(put_table) : INTERNAL ERROR : internal read of table %s failed!\n",
2461 xfer_signed (-argtype, dtl, dvi);
2463 xfer_unsigned (argtype, dtl, dvi);
2472 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
2478 (FILE * dtl, FILE * dvi, int n)
2485 /* read special (1 <= n <= 4 byte) data from dtl, and write in dvi */
2486 /* return number of bytes written */
2493 fprintf (stderr, "(special) : entering special.\n");
2499 fprintf (stderr, "(special) : DTL FILE ERROR (%s) : special %d, ",
2501 fprintf (stderr, "range is 1 to 4.\n");
2505 /* k[n] : length of special string */
2506 /* x[k] : special string */
2508 nk = xfer_len_string (n, dtl, dvi);
2513 fprintf (stderr, "(special) : leaving special.\n");
2524 (FILE * dtl, FILE * dvi, int suffix)
2531 /* read fontdef fnt_def1 .. fnt_def4 from dtl, and write in dvi */
2532 /* suffix is the fontdef suffix : 1 to 4 */
2533 /* return number of bytes written */
2537 Lstring lstr1, lstr2;
2542 fprintf (stderr, "(fontdef) : entering fontdef.\n");
2545 if (suffix < 1 || suffix > 4)
2548 fprintf (stderr, "(fontdef) : DTL FILE ERROR (%s) : ",
2550 fprintf (stderr, "font def %d, but range is 1 to 4.\n", suffix);
2554 init_Lstring (&lstr1, LSIZE);
2555 init_Lstring (&lstr2, LSIZE);
2560 fprintf (stderr, "(fontdef) : about to read font number.\n");
2563 /* k[suffix] : font number */
2565 k = xfer_signed (suffix, dtl, dvi);
2567 k = xfer_unsigned (suffix, dtl, dvi);
2572 fprintf (stderr, "(fontdef) : font ");
2573 fprintf (stderr, UF4, k);
2574 fprintf (stderr, ".\n");
2578 /* c[4] : (hexadecimal) checksum : I (gt) would prefer this */
2579 xfer_hex (4, dtl, dvi);
2580 #else /*NOT HEX_CHECKSUM */
2581 /* c[4] : checksum (octal, for comparison with tftopl's .pl file) */
2582 xfer_oct (4, dtl, dvi);
2586 xfer_unsigned (4, dtl, dvi);
2589 xfer_unsigned (4, dtl, dvi);
2591 /* If DTL file's edited, a and l may be wrong. */
2593 /* a[1] : length of font `area' (directory) portion of pathname string */
2594 a = get_unsigned (dtl);
2596 /* l[1] : length of font portion of pathname string */
2597 l = get_unsigned (dtl);
2599 /* n[a+l] : font pathname string <= area + font */
2601 a2 = get_Lstring (dtl, &lstr1);
2606 fprintf (stderr, "(fontdef) : WARNING : font area string's length (");
2607 fprintf (stderr, UF4, a);
2608 fprintf (stderr, ") in DTL file is wrong\n");
2609 fprintf (stderr, "Writing correct value (");
2610 fprintf (stderr, UF4, a2);
2611 fprintf (stderr, ") to DVI file\n");
2614 put_unsigned (1, a2, dvi);
2616 l2 = get_Lstring (dtl, &lstr2);
2621 fprintf (stderr, "(fontdef) : WARNING : font string's length (");
2622 fprintf (stderr, UF4, l);
2623 fprintf (stderr, ") in DTL file is wrong\n");
2624 fprintf (stderr, "Writing correct value (");
2625 fprintf (stderr, UF4, l2);
2626 fprintf (stderr, ") to DVI file\n");
2629 put_unsigned (1, l2, dvi);
2631 put_Lstring (&lstr1, dvi);
2632 put_Lstring (&lstr2, dvi);
2634 de_init_Lstring (&lstr2);
2635 de_init_Lstring (&lstr1);
2640 fprintf (stderr, "(fontdef) : leaving fontdef.\n");
2643 return (suffix + 4*4 + 2*1 + a2 + l2);
2651 (FILE * dtl, FILE * dvi)
2657 /* read preamble from dtl, and write in dvi */
2658 /* return number of bytes written */
2665 fprintf (stderr, "(preamble) : entering preamble.\n");
2669 xfer_unsigned (1, dtl, dvi);
2672 xfer_unsigned (4, dtl, dvi);
2675 xfer_unsigned (4, dtl, dvi);
2678 xfer_unsigned (4, dtl, dvi);
2680 /* k[1] : length of comment */
2681 /* x[k] : comment string */
2683 k1 = xfer_len_string (1, dtl, dvi);
2688 fprintf (stderr, "(preamble) : leaving preamble.\n");
2691 return (1 + 3*4 + k1);
2699 (FILE * dtl, FILE * dvi)
2705 /* read postamble from dtl, and write in dvi */
2706 /* return number of bytes written */
2708 postamble_address = dvi_written - 1;
2710 /* p[4] : DVI address of previous bop command */
2711 /* --- unsigned? --- or signed, as I assume? */
2712 /* For, surely p should be -1 if the DVI file has NO bop? */
2713 xfer_bop_address (dtl, dvi);
2716 xfer_unsigned (4, dtl, dvi);
2719 xfer_unsigned (4, dtl, dvi);
2722 xfer_unsigned (4, dtl, dvi);
2725 xfer_unsigned (4, dtl, dvi);
2728 xfer_unsigned (4, dtl, dvi);
2731 xfer_unsigned (2, dtl, dvi);
2734 xfer_unsigned (2, dtl, dvi);
2744 (FILE * dtl, FILE * dvi)
2750 /* read post_post from dtl, and write in dvi */
2751 /* return number of bytes written */
2753 /* hope I'm writing the "223" bytes in an 8-bit clean way */
2754 int n223 = 0; /* number of "223" bytes in final padding */
2756 /* q[4] : DVI address of post command */
2757 /* --- unsigned? --- or signed, as I assume? */
2758 /* what happens if there is NO postamble command? */
2759 /* shouldn't q be -1 then? */
2761 xfer_postamble_address (dtl, dvi);
2763 /* i[1] : DVI identification byte = 2 */
2764 xfer_unsigned (1, dtl, dvi);
2766 for (n223 = 0; true; n223++)
2768 COUNT nread = 0; /* number of DTL bytes read by read_token */
2773 nread = read_token (dtl, token);
2775 /* check whether end of dtl file */
2780 /* dtl file shouldn't end before an ECOM */
2782 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2784 fprintf (stderr, "premature end of DTL file!\n");
2786 "%d complete iterations of \"padding byte\" loop;\n", n223);
2787 fprintf (stderr, "troublesome token = \"%s\"\n", token);
2790 /* leave the "223" loop */
2793 else if (strcmp (token, "223") == 0)
2795 /* token is a "223" padding byte */
2800 /* read a non-empty token that wasn't "223" */
2801 (Void) unread_char ();
2804 if (strcmp (token, ECOM) == 0)
2806 /* end of DTL's post_post command */
2810 /* error : expected end of post_post */
2812 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2814 fprintf (stderr, "token \"%s\" should be ECOM (\"%s\")\n",
2819 /* leave the "223" loop */
2828 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : \n",
2830 fprintf (stderr, "fewer than four `223' padding bytes.\n");
2831 fprintf (stderr, "Will write at least four `223' padding bytes.\n");
2834 /* check whether the DVI file size is a multiple of 4 bytes */
2835 if ((dvi_written + n223) % 4 != 0)
2838 fprintf (stderr, "(post_post) : WARNING : \n");
2839 fprintf (stderr, "DVI size ");
2840 fprintf (stderr, WF, dvi_written);
2841 fprintf (stderr, " (bytes) wouldn't be a multiple of 4 !\n");
2843 "Will write (at least four) `223' padding bytes until it is.\n");
2846 /* final padding of DVI file by "223" bytes to a multiple of 4 bytes, */
2847 /* with at least 4 bytes */
2849 for (n223 = 0; (n223 < 4) || (dvi_written % 4 != 0); n223++)
2851 /* add a "223" padding byte */
2852 put_byte (223, dvi);
2855 return (4 + 1 + n223);
2860 /* end of dt2dv.c */