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 - Reference: "The DVI Driver Standard, Level 0",
14 by The TUG DVI Driver Standards Committee.
15 Appendix A, "Device-Independent File Format".
18 /* unix version; read from stdin, write to stdout, by default. */
29 /* by default, read and write regular files */
33 /* maximum number of characters in a DTL input line */
39 COUNT num; /* current line number */
40 size_t max; /* capacity of buf */
41 S4 wrote; /* number of characters written into buf */
42 size_t read; /* position in buf of next character to read from buf */
43 char * buf; /* line buffer */
46 char linebuf[MAXLINE+1];
48 Line dtl_line = {0, 0, 0, MAXLINE, linebuf};
50 /* a DTL token either is:
51 a quoted string (admitting an escape character),
52 or BCOM (if that is a nonempty string),
53 or ECOM (if that is a nonempty string),
54 or a string _not_ including ECOM_CHAR or space.
57 /* maximum expected length of a DTL token */
59 typedef char Token[MAXTOKLEN+1];
61 typedef unsigned char Byte;
67 /* command prefixes */
73 Byte first_suffix, last_suffix;
76 CmdPrefix cmd_prefixes [] =
78 {0, SETCHAR, true, 0, 127},
79 {128, SET, true, 1, 4},
80 {132, SETRULE, false, 0, 0},
81 {133, PUT, true, 1, 4},
82 {137, PUTRULE, false, 0, 0},
83 {138, NOP, false, 0, 0},
84 {139, BOP, false, 0, 0},
85 {140, EOP, false, 0, 0},
86 {141, PUSH, false, 0, 0},
87 {142, POP, false, 0, 0},
88 {143, RIGHT, true, 1, 4},
91 {157, DOWN, true, 1, 4},
94 {171, FONTNUM, true, 0, 63},
95 {235, FONT, true, 1, 4},
96 {239, SPECIAL, true, 1, 4},
97 {243, FONTDEF, true, 1, 4},
98 {247, PRE, false, 0, 0},
99 {248, POST, false, 0, 0},
100 {249, POSTPOST, false, 0, 0},
101 {250, OPCODE, true, 250, 255}
105 /* Number of DVI commands, including those officially undefined */
108 /* table of command name (string) pointers */
109 typedef char * CmdTable [NCMDS];
111 /* initially all command name pointers are NULL */
114 /* operation's opcode, name, number of args, string of arguments. */
123 /* name of table, first opcode, last opcode, pointer to opcode info. */
132 /* Table for opcodes 128 to 170 inclusive. */
134 op_info op_info_128_170 [] =
139 {131, SET4, 1, "-4"},
140 {132, SETRULE, 2, "-4 -4"},
144 {136, PUT4, 1, "-4"},
145 {137, PUTRULE, 2, "-4 -4"},
147 /* bop: not counting last argument, a signed address: */
148 {139, BOP, 10, "-4 -4 -4 -4 -4 -4 -4 -4 -4 -4"},
152 {143, RIGHT1, 1, "-1"},
153 {144, RIGHT2, 1, "-2"},
154 {145, RIGHT3, 1, "-3"},
155 {146, RIGHT4, 1, "-4"},
166 {157, DOWN1, 1, "-1"},
167 {158, DOWN2, 1, "-2"},
168 {159, DOWN3, 1, "-3"},
169 {160, DOWN4, 1, "-4"},
181 /* op_info op_info_128_170 [] */
183 op_table op_128_170 = {"op_128_170", 128, 170, op_info_128_170};
185 /* Table for fnt1 to fnt4 (opcodes 235 to 238) inclusive. */
189 {235, FONT1, 1, "1"},
190 {236, FONT2, 1, "2"},
191 {237, FONT3, 1, "3"},
192 {238, FONT4, 1, "-4"}
194 /* op_info fnt_n [] */
196 op_table fnt = {FONT, 235, 238, fnt_n};
199 /* Function prototypes */
201 Void mem_viol ARGS((int sig));
202 Void give_help (VOID);
203 int parse ARGS((char * s));
204 Void process ARGS((char * s));
207 Void dtl_stdin (VOID);
208 Void dvi_stdout (VOID);
210 int open_dtl ARGS((char * dtl_file, FILE ** pdtl));
211 int open_dvi ARGS((char * dvi_file, FILE ** pdvi));
213 int dt2dv ARGS((FILE * dtl, FILE * dvi));
215 Void * gmalloc ARGS((long int size));
218 Void dexit ARGS((int n));
220 int cons_cmds ARGS((int nprefixes, CmdPrefix * prefix, CmdTable cmds));
221 Void free_cmds ARGS((CmdTable cmd_table));
223 int get_line ARGS((FILE * fp, Line * line, int max));
224 int read_line_char ARGS((FILE * fp, int * ch));
225 int read_char ARGS((FILE * fp, int * ch));
226 int unread_char (VOID);
227 int read_string_char ARGS((FILE * fp, int * ch));
229 COUNT read_variety ARGS((FILE * dtl));
230 COUNT read_token ARGS((FILE * dtl, char * token));
231 COUNT skip_space ARGS((FILE * fp, int * ch));
232 COUNT read_misc ARGS((FILE * fp, Token token));
233 COUNT read_mes ARGS((FILE * fp, char * token));
235 int find_command ARGS((char * command, int * opcode));
236 int xfer_args ARGS((FILE * dtl, FILE * dvi, int opcode));
238 int set_seq ARGS((FILE * dtl, FILE * dvi));
240 int check_byte ARGS((int byte));
241 int put_byte ARGS((int onebyte, FILE * dvi));
243 U4 xfer_hex ARGS((int n, FILE * dtl, FILE * dvi));
244 U4 xfer_oct ARGS((int n, FILE * dtl, FILE * dvi));
245 U4 xfer_unsigned ARGS((int n, FILE * dtl, FILE * dvi));
246 S4 xfer_signed ARGS((int n, FILE * dtl, FILE * dvi));
248 int check_bmes ARGS((FILE * dtl));
249 int check_emes ARGS((FILE * dtl));
251 Void init_Lstring ARGS((Lstring * lsp, long int n));
252 Void de_init_Lstring ARGS((Lstring * lsp));
253 Lstring * alloc_Lstring ARGS((long int n));
254 Void free_Lstring ARGS((Lstring * lstr));
255 Void ls_putb ARGS((int ch, Lstring * lstr));
257 S4 get_Lstring ARGS((FILE * dtl, Lstring * lstr));
258 Void put_Lstring ARGS((const Lstring * lstr, FILE * dvi));
259 U4 xfer_len_string ARGS((int n, FILE * dtl, FILE * dvi));
261 U4 get_unsigned ARGS((FILE * dtl));
262 S4 get_signed ARGS((FILE * dtl));
264 int put_unsigned ARGS((int n, U4 unum, FILE * dvi));
265 int put_signed ARGS((int n, S4 snum, FILE * dvi));
267 S4 xfer_bop_address ARGS((FILE * dtl, FILE * dvi));
268 S4 xfer_postamble_address ARGS((FILE * dtl, FILE * dvi));
270 int put_table ARGS((op_table table, int opcode, FILE * dtl, FILE * dvi));
272 U4 special ARGS((FILE * dtl, FILE * dvi, int n));
273 int fontdef ARGS((FILE * dtl, FILE * dvi, int n));
275 U4 preamble ARGS((FILE * dtl, FILE * dvi));
276 int postamble ARGS((FILE * dtl, FILE * dvi));
277 int post_post ARGS((FILE * dtl, FILE * dvi));
282 char * keyword; /* command line option keyword */
283 int * p_var; /* pointer to option variable */
284 char * desc; /* description of keyword and value */
285 Void (* p_fn) (VOID); /* pointer to function called when option is set */
290 {"-debug", &debug, "detailed debugging", no_op},
291 {"-group", &group, "each DTL command is in parentheses", no_op},
292 {"-si", &rd_stdin, "read all DTL commands from standard input", dtl_stdin},
293 {"-so", &wr_stdout, "write all DVI commands to standard output", dvi_stdout},
294 {NULL, NULL, NULL, NULL}
298 char * progname = ""; /* intended for name of this program */
299 int nfile = 0; /* number of filename arguments on the command line */
301 #define PRINT_PROGNAME fprintf (stderr, "%s ", progname)
303 /* Harbison & Steele (1991) warn that some C implementations */
304 /* of free() do not treat NULL pointers correctly. */
305 #define gfree(p) {if (p) free (p);}
308 FILE * dtl_fp = NULL;
309 FILE * dvi_fp = NULL;
311 char * dtl_filename = "";
312 char * dvi_filename = "";
318 (int argc, char * argv[])
325 Void (*handler) ARGS((int)); /* Previous signal handler */
328 progname = argv[0]; /* name of this program */
330 /* memory violation signal handler */
332 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
345 #define C_LEVEL "non-"
346 #endif /* NOT STDC */
348 /* message about program and compiler */
349 /* NB: LTU EE's Sun/OS library is BSD, even though gcc 2.2.2 is ANSI */
351 fprintf (stderr, "\n");
353 "Program \"%s\" version %s compiled %s %s in %sstandard C.\n",
354 progname, VERSION, __DATE__, __TIME__, C_LEVEL);
356 /* interpret command line arguments */
359 dtl_fp = dvi_fp = NULL;
360 dtl_filename = dvi_filename = "";
362 for (i=1; i < argc; i++)
364 /* parse options, followed by any explicit filenames */
368 if (nfile != 2) /* not exactly two files specified, so give help */
372 dt2dv (dtl_fp, dvi_fp);
388 Void (* handler) ARGS((int));
389 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
394 "(mem_viol) : called with wrong signal!\n");
397 fprintf (stderr, "(mem_viol) : RUNTIME MEMORY ERROR : memory violation, ");
398 fprintf (stderr, "dtl line >= ");
399 fprintf (stderr, WF, dtl_line.num);
400 fprintf (stderr, "\n");
411 fprintf (stderr, "usage: ");
413 fprintf (stderr, "[options] dtl_file dvi_file");
414 fprintf (stderr, "\n");
415 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
417 fprintf (stderr, " ");
418 fprintf (stderr, "[%s]", keyword);
419 fprintf (stderr, " ");
420 fprintf (stderr, "%s", opts[i].desc);
421 fprintf (stderr, "\n");
423 fprintf (stderr, "Messages, like this one, go to stderr.\n");
433 Void dtl_stdin (VOID)
435 extern FILE * dtl_fp;
439 dtl_filename = "Standard Input";
443 Void dvi_stdout (VOID)
445 extern FILE * dvi_fp;
448 /* ! Perilous to monitors! */
450 dvi_filename = "Standard Output";
463 /* parse one command-line argument, `s' */
467 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
469 if (strncmp (s, keyword, strlen (keyword)) == 0)
472 (*(opts[i].p_var)) = 1; /* turn option on */
473 if ((pfn = opts[i].p_fn) != NULL)
474 (*pfn) (); /* call option function */
478 /* reached here, so not an option: assume it's a filename */
488 (char * dtl_file, FILE ** pdtl)
494 /* I: dtl_file; I: pdtl; O: *pdtl. */
496 extern char * dtl_filename;
498 dtl_filename = dtl_file;
500 if (dtl_filename == NULL)
504 "(open_dtl) : INTERNAL ERROR : dtl file's name is NULL.\n");
512 "(open_dtl) : INTERNAL ERROR : address of dtl variable is NULL.\n");
516 *pdtl = fopen (dtl_file, "r");
522 "(open_dtl) : DTL FILE ERROR : Cannot open \"%s\" for text reading.\n",
535 (char * dvi_file, FILE ** pdvi)
541 /* I: dvi_file; I: pdvi; O: *pdvi. */
543 extern char * dvi_filename;
545 dvi_filename = dvi_file;
547 if (dvi_filename == NULL)
551 "(open_dvi) : INTERNAL ERROR : dvi file's name is NULL.\n");
559 "(open_dvi) : INTERNAL ERROR : address of dvi variable is NULL.\n");
563 *pdvi = fopen (dvi_file, "wb");
569 "(open_dvi) : DVI FILE ERROR : Cannot open \"%s\" for binary writing.\n",
588 extern FILE * dtl_fp, * dvi_fp;
590 if (dtl_fp == NULL) /* first filename assumed to be DTL input */
592 open_dtl (s, &dtl_fp);
594 else if (dvi_fp == NULL) /* second filename assumed to be DVI output */
596 open_dvi (s, &dvi_fp);
601 fprintf (stderr, "(process) : at most two filenames allowed.\n");
609 COUNT dtl_read = 0; /* bytes read from dtl file */
610 COUNT dvi_written = 0; /* bytes written to dvi file */
611 word_t last_bop_address = -1; /* byte address of last bop; first bop uses -1 */
612 word_t postamble_address = -1; /* byte address of postamble */
613 COUNT ncom = 0; /* commands successfully read and interpreted from dtl file */
614 COUNT com_read = 0; /* bytes read in current (command and arguments), */
615 /* since and including the opening BCOM_CHAR, if any */
621 (int byte, FILE * dvi)
627 /* write byte into dvi file */
630 /* if (fprintf (dvi, "%c", byte) != 1) */
631 if (fprintf (dvi, "%c", byte) < 0)
635 "(put_byte) : DVI FILE ERROR (%s) : cannot write to dvi file.\n",
648 (FILE * dtl, FILE * dvi)
655 int nprefixes = 0; /* number of prefixes in cmd_prefixes[] list. */
656 static Token dtl_cmd = ""; /* DTL command name */
657 COUNT nread = 0; /* number of bytes read by a function from dtl file. */
659 nprefixes = sizeof (cmd_prefixes) / sizeof (CmdPrefix);
661 /* Construct array of all NCMDS == 256 DTL commands */
663 (Void) cons_cmds (nprefixes, cmd_prefixes, cmd_table);
665 /* DTL commands have the form "[ ]*command arg ... arg[ ]*", */
666 /* possibly enclosed in a BCOM, ECOM pair, */
667 /* and are separated by optional whitespace, typically newlines. */
668 /* That is, each command and its arguments are parenthesised, */
669 /* with optional spaces after the BCOM and before the ECOM, if any. */
671 /* dt2dv is now at the very start of the DTL file */
676 /* The very first thing should be the "variety" signature */
678 nread = read_variety (dtl);
680 /* while not end of dtl file or reading error, */
681 /* read, interpret, and write commands */
692 static Token token = ""; /* DTL token */
693 nread = read_token (dtl, token);
694 /* test for end of input, or reading error */
695 if (strlen (token) == 0)
700 fprintf (stderr, "(dt2dv) : end of input, or reading error.\n");
704 /* test whether this command begins correctly */
705 else if (strcmp (token, BCOM) != 0)
708 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
709 fprintf (stderr, "command must begin with \"%s\", ", BCOM);
710 fprintf (stderr, "not `%c' (char %d).\n", token[0], token[0]);
716 /* read the command name */
717 nread = read_token (dtl, dtl_cmd);
718 /* test for end of input, or reading error */
719 if (strlen (dtl_cmd) == 0)
725 "(dt2dv) : end of input, or reading error.\n");
734 fprintf (stderr, "(dt2dv) : command ");
735 fprintf (stderr, WF, ncom);
736 fprintf (stderr, " = \"%s\".\n", dtl_cmd);
739 /* find opcode for this command */
740 if (find_command (dtl_cmd, &opcode) == 1)
742 /* write the opcode, if we can */
743 put_byte (opcode, dvi);
745 /* treat the arguments, if any */
746 xfer_args (dtl, dvi, opcode);
748 else if (dtl_cmd[0] == BSEQ_CHAR)
750 /* sequence of font characters for SETCHAR */
757 "(dt2dv) : DTL FILE ERROR (%s) : unknown command \"%s\".\n",
758 dtl_filename, dtl_cmd);
765 /* seek ECOM after command's last argument and optional whitespace */
766 static Token token = ""; /* DTL token */
767 nread = read_token (dtl, token);
768 /* test for end of input, or reading error */
769 if (strlen (token) == 0)
775 "(dt2dv) : end of input, or reading error.\n");
779 if (strcmp (token, ECOM) != 0)
782 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
783 fprintf (stderr, "ECOM (\"%s\") expected, not `%c' (char %d).\n",
784 ECOM, token[0], token[0]);
790 ++ ncom; /* one more command successfully read and interpreted */
795 fprintf (stderr, "(dt2dv) :\n");
796 fprintf (stderr, "Read (from file \"%s\") ", dtl_filename);
797 fprintf (stderr, WF, dtl_read);
798 fprintf (stderr, " DTL bytes (");
799 fprintf (stderr, UF4, dtl_line.num);
800 fprintf (stderr, " lines);\n");
801 fprintf (stderr, "wrote (to file \"%s\") ", dvi_filename);
802 fprintf (stderr, WF, dvi_written);
803 fprintf (stderr, " DVI bytes;\n");
804 fprintf (stderr, "completely interpreted ");
805 fprintf (stderr, WF, ncom);
806 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
807 fprintf (stderr, "\n");
809 (Void) free_cmds (cmd_table);
829 fprintf (stderr, "(gmalloc) : INTERNAL ERROR : ");
831 "unreasonable request to malloc %ld bytes\n",
835 p = (Void *) malloc ((size_t) size);
839 fprintf (stderr, "(gmalloc) : MEMORY ALLOCATION ERROR : ");
841 "operating system failed to malloc %ld bytes\n",
854 fprintf (stderr, "(dinfo) : ");
855 fprintf (stderr, "Current DTL input line ");
856 fprintf (stderr, UF4, dtl_line.num);
857 fprintf (stderr, " :\n");
858 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
859 fprintf (stderr, "Read ");
860 fprintf (stderr, WF, dtl_read);
861 fprintf (stderr, " DTL bytes (");
862 fprintf (stderr, WF, com_read);
863 fprintf (stderr, " in current command), wrote ");
864 fprintf (stderr, WF, dvi_written);
865 fprintf (stderr, " DVI bytes.\n");
866 fprintf (stderr, "Successfully interpreted ");
867 fprintf (stderr, WF, ncom);
868 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
884 fprintf (stderr, "(dexit) : exiting with status %d.\n", n);
893 (int nprefixes, CmdPrefix * prefix, CmdTable cmds)
895 (nprefixes, prefix, cmds)
901 int code; /* first opcode for a given command prefix */
902 int opcode; /* command's opcode */
903 int nsuffixes; /* number of commands with a given prefix */
904 int isuffix; /**** integer suffix, of at most three digits ****/
905 String suffix; /* suffix string generated from integer suffix */
906 size_t plen = 0; /* prefix length */
907 size_t slen; /* suffix length */
908 size_t clen; /* whole command name length */
909 int i, j; /* loop indices */
911 for (i=0; i < nprefixes; prefix++, i++)
913 code = prefix->first_code;
914 if (code < 0 || code > 255)
917 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
919 "prefix listed internally with code = %d, must be 0 to 255\n",
923 if (prefix->has_suffix)
925 plen = strlen (prefix->name);
926 /**** Suffixes in DTL are Integers, in Sequence */
927 if (prefix->last_suffix < prefix->first_suffix)
930 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
932 "prefix's last suffix %d < first suffix (%d)\n",
933 prefix->last_suffix, prefix->first_suffix);
936 nsuffixes = prefix->last_suffix - prefix->first_suffix + 1;
937 opcode = prefix->first_code;
938 for (j=0; j < nsuffixes; j++, opcode++)
940 isuffix = prefix->first_suffix + j;
941 if (0 <= code && code <= 127) /* SETCHAR */
943 /* SETCHAR's suffix is written in uppercase hexadecimal */
944 sprintf (suffix, "%02X", isuffix);
946 else /* 128 <= code && code <= 255 */ /* other DTL commands */
948 /* other commands' suffices are written in decimal */
949 sprintf (suffix, "%d", isuffix);
951 slen = strlen (suffix);
953 cmds[opcode] = (char *) gmalloc (clen+1);
954 strcpy (cmds[opcode], prefix->name);
955 strcat (cmds[opcode], suffix);
958 else /* command name = prefix */
960 plen = strlen (prefix->name);
962 opcode = prefix->first_code;
963 cmds[opcode] = (char *) gmalloc (clen+1);
964 strcpy (cmds[opcode], prefix->name);
983 for (i=0; i < NCMDS; i++)
984 gfree (cmd_table[i]);
992 (FILE * fp, Line * line, int max)
999 /* read a (Line *) line from fp, return length */
1000 /* adapted from K&R (second, alias ANSI C, edition, 1988), page 165 */
1002 if (fgets (line->buf, max, fp) == NULL)
1007 line->wrote = strlen (line->buf);
1018 (FILE * fp, int * ch)
1024 /* read one character from dtl_line if possible, */
1025 /* otherwise read another dtl_line from fp */
1026 /* return 1 if a character is read, 0 if at end of fp file */
1028 extern Line dtl_line;
1029 if (dtl_line.wrote == 0 || dtl_line.read >= dtl_line.wrote)
1032 /* refill line buffer */
1033 line_status = get_line (fp, &dtl_line, MAXLINE);
1034 if (line_status == 0)
1036 /* at end of DTL file */
1040 fprintf (stderr, "(read_line_char) : end of DTL file\n");
1047 /* new DTL line was read */
1051 fprintf (stderr, "(read_line_char) : new DTL input line:\n");
1052 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
1056 *ch = dtl_line.buf [dtl_line.read ++];
1058 ++ com_read; /* count another DTL command character */
1061 /* read_line_char */
1067 (FILE * fp, int * ch)
1073 /* Read next character, if any, from file fp. */
1074 /* Write it into *ch. */
1075 /* If no character is read, then *ch value < 0. */
1076 /* Return 1 if OK, 0 if EOF or error. */
1079 int c; /* in case ch points awry, we still have something in c. */
1082 if (read_line_char (fp, &c) == 0)
1084 /* end of fp file, or error reading it */
1093 "(read_char) : character %d not in range 0 to 255\n",
1098 else if ( ! isprint (c) && ! isspace (c))
1102 "(read_char) : character %d %s.\n",
1104 "not printable and not white space");
1124 /* read and check DTL variety signature */
1125 /* return number of DTL bytes written */
1126 /* DTL variety is _NEVER_ grouped by BCOM and ECOM. */
1127 /* Uniformity here enables the program easily to modify its behavior. */
1129 COUNT vread = 0; /* number of DTL bytes read by read_variety */
1130 COUNT nread = 0; /* number of DTL bytes read by read_token */
1131 static Token token = "";
1133 /* read the DTL VARIETY keyword */
1134 nread = read_token (dtl, token);
1136 /* test whether signature begins correctly */
1137 if (strcmp (token, "variety") != 0)
1140 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1141 fprintf (stderr, "DTL signature must begin with \"%s\", not \"%s\".\n",
1146 /* read the DTL variety */
1147 nread = read_token (dtl, token);
1149 /* test whether variety is correct */
1150 if (strcmp (token, VARIETY) != 0)
1153 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1154 fprintf (stderr, "DTL variety must be \"%s\", not \"%s\".\n",
1160 fprintf (stderr, "(read_variety) : DTL variety %s is OK.\n", VARIETY);
1162 return vread; /* OK */
1170 (FILE * fp, int * ch)
1176 /* Skip whitespace characters in file fp. */
1177 /* Write in *ch the last character read from fp, */
1178 /* or < 0 if fp could not be read. */
1179 /* Return number of characters read from fp. */
1181 int c; /* character read (if any) */
1182 COUNT count; /* number (0 or more) of whitespace characters read */
1183 int nchar; /* number (0 or 1) of characters read by read_char */
1185 /* loop ends at: end of fp file, or reading error, or not a white space */
1186 for (count=0; ((nchar = read_char (fp, &c)) == 1 && isspace (c)); ++count)
1188 /* otherwise, more white spaces to skip */
1191 /* report when each DTL end of line is reached */
1195 fprintf (stderr, "(skip_space) : ");
1196 fprintf (stderr, "end of DTL line (at least) ");
1197 fprintf (stderr, WF, dtl_line.num);
1198 fprintf (stderr, "\n");
1208 *ch = c; /* c will be < 0 if read_char could not read fp */
1209 return (count + nchar);
1217 (FILE * dtl, char * token)
1223 /* read next token from dtl file. */
1224 /* return number of DTL bytes read. */
1225 /* A token is one of:
1226 a string from BMES_CHAR to the next unescaped EMES_CHAR, inclusive;
1227 BCOM or ECOM, unless these are empty strings;
1229 any other sequence of non-whitespace characters.
1232 COUNT nread; /* number of DTL bytes read by read_token */
1233 int ch; /* most recent character read */
1237 /* obtain first non-space character */
1238 /* add to nread the number of characters read from dtl by skip_space */
1239 nread += skip_space (dtl, &ch);
1243 /* end of dtl file */
1244 /* write an empty token */
1249 fprintf (stderr, "(read_token) : end of dtl file.\n");
1252 else if (group && ch == BCOM_CHAR)
1254 strcpy (token, BCOM);
1256 else if (group && ch == ECOM_CHAR)
1258 strcpy (token, ECOM);
1264 if (ch == BMES_CHAR) /* string token; read until unescaped EMES_CHAR */
1266 nread += read_mes (dtl, token+1);
1268 else if (ch == BSEQ_CHAR || ch == ESEQ_CHAR)
1270 /* token is complete */
1272 else /* any other string not containing (ECOM_CHAR or) whitespace */
1274 nread += read_misc (dtl, token+1);
1281 fprintf (stderr, "(read_token) : token = \"%s\"\n", token);
1284 return (nread); /* number of bytes read from dtl file */
1291 #define CHAR_EOS (-1)
1296 (FILE * fp, int * ch)
1303 int status = CHAR_OK; /* OK so far */
1306 if (read_char (fp, &c) == 0)
1307 status = CHAR_FAIL; /* fail */
1309 if (c == EMES_CHAR) /* end-of-string char */
1311 status = CHAR_EOS; /* end of string */
1313 else if (c == ESC_CHAR) /* escape character */
1315 /* accept the next character literally, even ESC_CHAR and EMES_CHAR */
1316 if (read_char (fp, &c) == 0)
1317 status = CHAR_FAIL; /* fail */
1323 /* read_string_char */
1329 (FILE * fp, Token token)
1338 /* loop ends at: end of fp file, or reading error, or a space */
1339 for (count=0; count <= MAXTOKLEN; ++count)
1341 if (read_char (fp, &c) == 0 || isspace (c))
1343 if (group && c == ECOM_CHAR)
1345 (Void) unread_char ();
1351 token[count] = '\0';
1360 (FILE * fp, char * token)
1366 /* called **AFTER** a BMES_CHAR has been read */
1367 /* read file fp for characters up to next unescaped EMES_CHAR */
1368 /* this is called a "string token" */
1369 /* write string, including EMES_CHAR, into token[] */
1370 /* return number of characters read from fp */
1372 COUNT dtl_count; /* number of DTL characters read by read_mes from fp */
1373 int more; /* flag more == 0 to terminate loop */
1374 int escape; /* flag escape == 1 if previous character was ESC_CHAR */
1375 int ch; /* current DTL character */
1382 if (read_char (fp, &ch) == 0)
1384 /* end of fp file, or reading error */
1387 else /* error checking passed */
1390 if (ch == EMES_CHAR && escape == 0) /* end of string */
1392 /* include final EMES_CHAR */
1396 else if (ch == ESC_CHAR && escape == 0)
1398 /* next character is not end of string */
1403 /* store any other character, */
1404 /* including escaped EMES_CHAR and ESC_CHAR*/
1418 /* wind input back, to allow rereading of one character */
1419 /* return 1 if this works, 0 on error */
1421 extern Line dtl_line;
1423 if (dtl_line.read > 0)
1425 -- dtl_line.read; /* back up one character in dtl_line */
1426 -- dtl_read; /* correct the count of DTL characters */
1427 -- com_read; /* count another DTL command character */
1428 status = 1; /* OK */
1430 else /* current DTL line is empty */
1432 status = 0; /* error */
1442 (char * command, int * opcode)
1453 for (i=0; i < NCMDS; i++)
1455 if ((cmd_table[i] != 0) && (strcmp (command, cmd_table[i]) == 0))
1478 if (byte < 0 || byte > 255)
1481 fprintf (stderr, "(check_byte) : INTERNAL ERROR : ");
1482 fprintf (stderr, "byte %d not in the range of 0 to 255.\n", byte);
1493 (FILE * dtl, FILE * dvi, int opcode)
1503 if (opcode >= 0 && opcode <= 127)
1504 ; /* SETCHAR uses no data */
1505 else if (opcode >= 128 && opcode <= 170)
1507 word_t this_bop_address = last_bop_address;
1509 if (opcode == 139) /* BOP */
1511 this_bop_address = dvi_written - 1;
1513 put_table (op_128_170, opcode, dtl, dvi);
1514 if (opcode == 139) /* BOP */
1516 xfer_bop_address (dtl, dvi);
1517 last_bop_address = this_bop_address;
1520 else if (opcode >= 171 && opcode <= 234)
1521 ; /* FONTNUM uses no data */
1522 else if (opcode >= 235 && opcode <= 238)
1523 put_table (fnt, opcode, dtl, dvi);
1524 else if (opcode >= 239 && opcode <= 242)
1527 special (dtl, dvi, n);
1529 else if (opcode >= 243 && opcode <= 246)
1532 fontdef (dtl, dvi, n);
1534 else if (opcode == 247)
1535 preamble (dtl, dvi);
1536 else if (opcode == 248)
1537 postamble (dtl, dvi);
1538 else if (opcode == 249)
1539 post_post (dtl, dvi);
1540 else if (opcode >= 250 && opcode <= 255)
1541 ; /* these, undefined, opcodes use no data */
1546 "(xfer_args) : opcode %d not handled.\n",
1558 (FILE * dtl, FILE * dvi)
1564 /* Called _after_ a BSEQ_CHAR command */
1565 /* Read bytes from dtl file, */
1566 /* writing corresponding SETCHAR or SET1 commands to DVI file, */
1567 /* _until_ unescaped ESEQ_CHAR is found */
1568 /* Return 1 if OK, 0 on error */
1569 /**** dt2dv assumes 8 bit characters, ****/
1570 /**** but some day one might change that. ****/
1572 int status = 1; /* status = 1 if OK, 0 if error */
1573 int more; /* sequence of font characters continuing? */
1574 int escape = 0; /* flag set if previous character was an escape */
1575 int ch; /* character read from DTL file */
1579 /* ignore read_char status, to allow unprintable characters */
1580 (Void) read_char (dtl, &ch);
1581 /* but check for end of dtl file, or serious file reading error */
1585 fprintf (stderr, "(set_seq) : ");
1586 fprintf (stderr, "end of dtl file, ");
1587 fprintf (stderr, "or serious dtl file reading error\n");
1590 status = 0; /* bad news */
1592 else /* read dtl file, okay */
1594 if (ch == ESC_CHAR && escape == 0) /* escape next character */
1600 if (ch == ESEQ_CHAR && escape == 0) /* end of sequence */
1604 else if (ch <= 127) /* can use SETCHAR */
1608 else if (ch <= 255) /* can use SET1 */
1610 put_byte (128, dvi); /* SET1 opcode */
1611 put_unsigned (1, (U4) ch, dvi);
1616 fprintf (stderr, "(set_seq) : ");
1618 "ERROR : DTL character %d is not in range 0 to 255\n",
1622 status = 0; /* Error, because dt2dv assumes 8 bit characters */
1624 escape = 0; /* current ch is not an escape character */
1636 (int n, FILE * dtl, FILE * dvi)
1643 /* translate unsigned n-byte hexadecimal number from dtl to dvi file. */
1644 /* return value of hexadecimal number */
1646 U4 unum = 0; /* at most this space needed */
1647 COUNT nread = 0; /* number of DTL bytes read by read_token */
1648 int nconv = 0; /* number of arguments converted by sscanf */
1649 static Token token = ""; /* DTL token */
1655 "(xfer_hex) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1660 nread = read_token (dtl, token);
1662 nconv = sscanf (token, XF4, &unum);
1667 fprintf (stderr, "(xfer_hex) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1668 dtl_filename, "hexadecimal number expected, not", token);
1672 put_unsigned (n, unum, dvi);
1682 (int n, FILE * dtl, FILE * dvi)
1689 /* translate unsigned n-byte octal number from dtl to dvi file. */
1690 /* return value of octal number */
1692 U4 unum = 0; /* at most this space needed */
1693 COUNT nread = 0; /* number of DTL bytes read by read_token */
1694 int nconv = 0; /* number of arguments converted by sscanf */
1695 static Token token = ""; /* DTL token */
1701 "(xfer_oct) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1706 nread = read_token (dtl, token);
1708 nconv = sscanf (token, OF4, &unum);
1713 fprintf (stderr, "(xfer_oct) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1714 dtl_filename, "octal number expected, not", token);
1718 put_unsigned (n, unum, dvi);
1728 (int n, FILE * dtl, FILE * dvi)
1735 /* translate unsigned n-byte number from dtl to dvi file. */
1736 /* return value of unsigned number */
1738 U4 unum = 0; /* at most this space needed */
1740 unum = get_unsigned (dtl);
1741 put_unsigned (n, unum, dvi);
1751 (int n, FILE * dtl, FILE * dvi)
1758 /* translate signed n-byte number from dtl to dvi file. */
1759 /* return value of signed number */
1763 snum = get_signed (dtl);
1764 put_signed (n, snum, dvi);
1779 /* read unsigned number from dtl file. */
1780 /* return value of unsigned number */
1782 U4 unum = 0; /* at most this space needed */
1783 COUNT nread = 0; /* number of DTL bytes read by read_token */
1784 int nconv = 0; /* number of arguments converted by sscanf */
1785 static Token token = ""; /* DTL token */
1787 nread = read_token (dtl, token);
1789 nconv = sscanf (token, UF4, &unum);
1794 fprintf (stderr, "(get_unsigned) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1795 dtl_filename, "unsigned number expected, not", token);
1812 /* read signed number from dtl file. */
1813 /* return value of signed number */
1816 COUNT nread = 0; /* number of DTL bytes read by read_token */
1817 int nconv = 0; /* number of sscanf arguments converted and assigned */
1818 static Token token = "";
1820 nread = read_token (dtl, token);
1822 nconv = sscanf (token, SF4, &snum);
1827 fprintf (stderr, "(get_signed) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1828 dtl_filename, "signed number expected, not", token);
1840 (int n, U4 unum, FILE * dvi)
1847 /* put unsigned in-byte integer to dvi file */
1848 /* DVI format uses Big-endian storage of numbers: */
1849 /* most significant byte is first. */
1850 /* return number of bytes written. */
1852 Byte ubyte[4]; /* at most 4 bytes needed in DVI format */
1859 "(put_unsigned) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1864 /* Big-endian storage. */
1865 for (i = 0; i < n; i++)
1867 ubyte[i] = (Byte) (unum % 256);
1870 /* Reverse order for big-endian representation. */
1871 for (i = n-1; i >= 0; i--)
1873 put_byte ((int) ubyte[i], dvi);
1884 (int n, S4 snum, FILE * dvi)
1891 /* put signed in-byte integer to dvi file */
1892 /* DVI format uses 2's complement Big-endian storage of signed numbers: */
1893 /* most significant byte is first. */
1894 /* return number of bytes written. */
1896 /* Will this deal properly with the sign? */
1902 "(put_signed) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1907 /* How do we ensure 2's complement representation? */
1908 /* Here's a trick that I hope is portable, given ANSI C. */
1909 /* See K&R (2nd edition), Appendix A6.2 "Integral Conversions". */
1911 /* Convert snum to U4 data type */
1912 put_unsigned (n, (U4) snum, dvi);
1927 /* check that a BMES_CHAR is the next non-whitespace character in dtl */
1929 int ch; /* next non-whitespace character in dtl */
1931 /* `(Void)' because we ignore the number of spaces skipped */
1932 (Void) skip_space (dtl, &ch);
1937 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1939 fprintf (stderr, "end of dtl file, or reading error\n");
1943 if (ch != BMES_CHAR)
1946 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1948 fprintf (stderr, "BMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1949 BMES_CHAR, "expected before string", ch, ch);
1966 /* check that an EMES_CHAR is the next character in dtl */
1968 int ch; /* dtl character */
1970 if (read_char (dtl, &ch) == 0)
1973 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1975 fprintf (stderr, "end of dtl file, or reading error\n");
1979 if (ch != EMES_CHAR)
1982 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1984 fprintf (stderr, "EMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1985 EMES_CHAR, "expected to follow string", ch, ch);
1994 /* Size typically used in this program for Lstring variables */
2001 (Lstring * lsp, long int n)
2010 lsp->s = (char *) gmalloc (n);
2027 lsp->s = NULL; /* to be sure */
2029 /* de_init_Lstring */
2042 lsp = (Lstring *) gmalloc (sizeof (Lstring));
2043 init_Lstring (lsp, n);
2067 (int ch, Lstring * lstr)
2073 /* write byte ch into Lstring *lstr */
2075 if (lstr->l >= lstr->m)
2078 fprintf (stderr, "(ls_putb) : ERROR : No more room in Lstring.\n");
2083 lstr->s [(lstr->l)++] = ch;
2092 (FILE * dtl, Lstring * lstr)
2098 /* get a string from dtl file, store as an Lstring in *lstr. */
2099 /* lstr must already be allocated and initialised. */
2100 /* return length of Lstring *lstr */
2103 int char_status = CHAR_OK; /* OK so far */
2108 fprintf (stderr, "(get_Lstring) : entering get_Lstring.\n");
2117 "(get_Lstring) : string is: \"");
2120 for (nch=0; ; nch++)
2124 char_status = read_string_char (dtl, &ch);
2126 if (char_status == CHAR_FAIL)
2128 /* end of dtl file, or reading error */
2129 fprintf (stderr, "\n");
2131 fprintf (stderr, "(get_Lstring) : DTL FILE ERROR (%s) : ",
2133 fprintf (stderr, "cannot read string[");
2134 fprintf (stderr, UF4, nch);
2135 fprintf (stderr, "] from dtl file.\n");
2141 fprintf (stderr, "%c", ch);
2144 if (char_status == CHAR_EOS)
2146 if (ch != EMES_CHAR)
2149 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2150 fprintf (stderr, "char_status = CHAR_FAIL,\n");
2152 "but ch = %c (char %d) is not EMES_CHAR = %c (char %d)\n",
2153 ch, ch, EMES_CHAR, EMES_CHAR);
2156 (Void) unread_char ();
2157 break; /* end of string */
2159 else if (char_status == CHAR_OK)
2166 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2167 fprintf (stderr, "char_status = %d is unfamiliar!\n", char_status);
2175 fprintf (stderr, "\".\n");
2183 fprintf (stderr, "(get_Lstring) : leaving get_Lstring.\n");
2194 (const Lstring * lstr, FILE * dvi)
2202 fwret = fwrite ((lstr->s), sizeof (char), (size_t) (lstr->l), dvi);
2204 dvi_written += fwret;
2206 if (fwret < lstr->l)
2210 "(put_Lstring) : DVI File ERROR : not all bytes written ");
2211 fprintf (stderr, "(%ld of %ld).\n",
2212 (long int) fwret, (long int) (lstr->l));
2222 (int n, FILE * dtl, FILE * dvi)
2229 /* transfer (length and) quoted string from dtl to dvi file, */
2230 /* return number of bytes written to dvi file. */
2238 fprintf (stderr, "(xfer_len_string) : entering xfer_len_string.\n");
2241 init_Lstring (&lstr, LSIZE);
2243 /* k[n] : length of special string */
2245 k = get_unsigned (dtl);
2250 fprintf (stderr, "(xfer_len_string) : string's nominal length k = ");
2251 fprintf (stderr, UF4, k);
2252 fprintf (stderr, " characters.\n");
2255 k2 = get_Lstring (dtl, &lstr);
2260 fprintf (stderr, "(xfer_len_string) : WARNING : string length (");
2261 fprintf (stderr, UF4, k);
2262 fprintf (stderr, ") in DTL file is wrong\n");
2263 fprintf (stderr, "Writing correct value (");
2264 fprintf (stderr, UF4, k2);
2265 fprintf (stderr, ") to DVI file\n");
2268 put_unsigned (n, k2, dvi);
2270 put_Lstring (&lstr, dvi);
2275 fprintf (stderr, "(xfer_len_string) : leaving xfer_len_string.\n");
2278 de_init_Lstring (&lstr);
2282 /* xfer_len_string */
2288 (FILE * dtl, FILE * dvi)
2294 /* translate signed 4-byte bop address from dtl to dvi file. */
2295 /* return value of bop address written to DVI file */
2297 S4 snum = 0; /* at most this space needed for byte address */
2298 COUNT nread = 0; /* number of DTL bytes read by read_token */
2299 int nconv = 0; /* number of arguments converted by sscanf */
2300 static Token token = ""; /* DTL token */
2302 nread += read_token (dtl, token);
2304 nconv = sscanf (token, SF4, &snum);
2309 fprintf (stderr, "(xfer_bop_address) : DTL FILE ERROR (%s) : ",
2311 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2315 if (snum != last_bop_address)
2318 fprintf (stderr, "(xfer_bop_address) : WARNING : byte address (");
2319 fprintf (stderr, WF, snum);
2320 fprintf (stderr, ")\n");
2321 fprintf (stderr, "for previous bop in DTL file is wrong\n");
2322 fprintf (stderr, "Writing correct value (");
2323 fprintf (stderr, WF, last_bop_address);
2324 fprintf (stderr, ") to DVI file\n");
2327 put_signed (4, last_bop_address, dvi);
2329 return last_bop_address;
2331 /* xfer_bop_address */
2335 xfer_postamble_address
2337 (FILE * dtl, FILE * dvi)
2343 /* translate signed 4-byte postamble address from dtl to dvi file. */
2344 /* return value of postamble address written to DVI file */
2346 S4 snum = 0; /* at most this space needed for byte address */
2347 COUNT nread = 0; /* number of DTL bytes read by read_token */
2348 int nconv = 0; /* number of arguments converted by sscanf */
2349 static Token token = ""; /* DTL token */
2351 nread += read_token (dtl, token);
2353 nconv = sscanf (token, SF4, &snum);
2358 fprintf (stderr, "(xfer_postamble_address) : DTL FILE ERROR (%s) : ",
2360 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2364 if (snum != postamble_address)
2367 fprintf (stderr, "(xfer_postamble_address) : WARNING : byte address (");
2368 fprintf (stderr, WF, snum);
2369 fprintf (stderr, ")\n");
2370 fprintf (stderr, "for postamble in DTL file is wrong\n");
2371 fprintf (stderr, "Writing correct value (");
2372 fprintf (stderr, WF, postamble_address);
2373 fprintf (stderr, ") to DVI file\n");
2376 put_signed (4, postamble_address, dvi);
2378 return postamble_address;
2380 /* xfer_postamble_address */
2386 (op_table table, int opcode, FILE * dtl, FILE * dvi)
2388 (table, opcode, dtl, dvi)
2395 /* table: {char * name; int first, last; op_info * list; }; */
2396 /* op_info: {int code; char * name; int nargs; char * args; }; */
2398 op_info op; /* entry in table */
2400 int pos; /* current position in string being scanned */
2401 static String args = "";
2403 /* Defensive programming. */
2404 if (opcode < table.first || opcode > table.last)
2408 "(put_table) : DTL FILE (OR INTERNAL) ERROR : opcode %d ", opcode);
2409 fprintf (stderr, "is outside table %s [ %d to %d ] !\n",
2410 table.name, table.first, table.last);
2414 op = table.list [ opcode - table.first ];
2416 /* More defensive programming. */
2417 if (opcode != op.code)
2421 "(put_table) : INTERNAL ERROR : opcode %d for command \"%s\"",
2423 fprintf (stderr, " faulty in table \"%s\".\n", table.name);
2427 /* process all the arguments, according to size and sign */
2429 strncpy (args, op.args, MAXSTRLEN);
2432 for (i=0; i < op.nargs; i++)
2435 int nscan = 0; /* number of bytes read by sscanf */
2436 int nconv = 0; /* number of sscanf arguments converted & assigned */
2438 /* sscanf() does NOT advance over its input: */
2439 /* C strings lack internal state information, which C files have. */
2440 /* On Sun/OS, sscanf calls ungetc on the string it reads, */
2441 /* which therefore has to be writable. */
2443 nconv = sscanf (args + pos, "%d%n", &argtype, &nscan);
2445 if (nconv < 1 || nscan < 1)
2449 "(put_table) : INTERNAL ERROR : internal read of table %s failed!\n",
2457 xfer_signed (-argtype, dtl, dvi);
2459 xfer_unsigned (argtype, dtl, dvi);
2468 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
2474 (FILE * dtl, FILE * dvi, int n)
2481 /* read special (1 <= n <= 4 byte) data from dtl, and write in dvi */
2482 /* return number of bytes written */
2489 fprintf (stderr, "(special) : entering special.\n");
2495 fprintf (stderr, "(special) : DTL FILE ERROR (%s) : special %d, ",
2497 fprintf (stderr, "range is 1 to 4.\n");
2501 /* k[n] : length of special string */
2502 /* x[k] : special string */
2504 nk = xfer_len_string (n, dtl, dvi);
2509 fprintf (stderr, "(special) : leaving special.\n");
2520 (FILE * dtl, FILE * dvi, int suffix)
2527 /* read fontdef fnt_def1 .. fnt_def4 from dtl, and write in dvi */
2528 /* suffix is the fontdef suffix : 1 to 4 */
2529 /* return number of bytes written */
2533 Lstring lstr1, lstr2;
2538 fprintf (stderr, "(fontdef) : entering fontdef.\n");
2541 if (suffix < 1 || suffix > 4)
2544 fprintf (stderr, "(fontdef) : DTL FILE ERROR (%s) : ",
2546 fprintf (stderr, "font def %d, but range is 1 to 4.\n", suffix);
2550 init_Lstring (&lstr1, LSIZE);
2551 init_Lstring (&lstr2, LSIZE);
2556 fprintf (stderr, "(fontdef) : about to read font number.\n");
2559 /* k[suffix] : font number */
2561 k = xfer_signed (suffix, dtl, dvi);
2563 k = xfer_unsigned (suffix, dtl, dvi);
2568 fprintf (stderr, "(fontdef) : font ");
2569 fprintf (stderr, UF4, k);
2570 fprintf (stderr, ".\n");
2574 /* c[4] : (hexadecimal) checksum : I (gt) would prefer this */
2575 xfer_hex (4, dtl, dvi);
2576 #else /NOT HEX_CHECKSUM */
2577 /* c[4] : checksum (octal, for comparison with tftopl's .pl file) */
2578 xfer_oct (4, dtl, dvi);
2582 xfer_unsigned (4, dtl, dvi);
2585 xfer_unsigned (4, dtl, dvi);
2587 /* If DTL file's edited, a and l may be wrong. */
2589 /* a[1] : length of font `area' (directory) portion of pathname string */
2590 a = get_unsigned (dtl);
2592 /* l[1] : length of font portion of pathname string */
2593 l = get_unsigned (dtl);
2595 /* n[a+l] : font pathname string <= area + font */
2597 a2 = get_Lstring (dtl, &lstr1);
2602 fprintf (stderr, "(fontdef) : WARNING : font area string's length (");
2603 fprintf (stderr, UF4, a);
2604 fprintf (stderr, ") in DTL file is wrong\n");
2605 fprintf (stderr, "Writing correct value (");
2606 fprintf (stderr, UF4, a2);
2607 fprintf (stderr, ") to DVI file\n");
2610 put_unsigned (1, a2, dvi);
2612 l2 = get_Lstring (dtl, &lstr2);
2617 fprintf (stderr, "(fontdef) : WARNING : font string's length (");
2618 fprintf (stderr, UF4, l);
2619 fprintf (stderr, ") in DTL file is wrong\n");
2620 fprintf (stderr, "Writing correct value (");
2621 fprintf (stderr, UF4, l2);
2622 fprintf (stderr, ") to DVI file\n");
2625 put_unsigned (1, l2, dvi);
2627 put_Lstring (&lstr1, dvi);
2628 put_Lstring (&lstr2, dvi);
2630 de_init_Lstring (&lstr2);
2631 de_init_Lstring (&lstr1);
2636 fprintf (stderr, "(fontdef) : leaving fontdef.\n");
2639 return (suffix + 4*4 + 2*1 + a2 + l2);
2647 (FILE * dtl, FILE * dvi)
2653 /* read preamble from dtl, and write in dvi */
2654 /* return number of bytes written */
2661 fprintf (stderr, "(preamble) : entering preamble.\n");
2665 xfer_unsigned (1, dtl, dvi);
2668 xfer_unsigned (4, dtl, dvi);
2671 xfer_unsigned (4, dtl, dvi);
2674 xfer_unsigned (4, dtl, dvi);
2676 /* k[1] : length of comment */
2677 /* x[k] : comment string */
2679 k1 = xfer_len_string (1, dtl, dvi);
2684 fprintf (stderr, "(preamble) : leaving preamble.\n");
2687 return (1 + 3*4 + k1);
2695 (FILE * dtl, FILE * dvi)
2701 /* read postamble from dtl, and write in dvi */
2702 /* return number of bytes written */
2704 postamble_address = dvi_written - 1;
2706 /* p[4] : DVI address of previous bop command */
2707 /* --- unsigned? --- or signed, as I assume? */
2708 /* For, surely p should be -1 if the DVI file has NO bop? */
2709 xfer_bop_address (dtl, dvi);
2712 xfer_unsigned (4, 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 (2, dtl, dvi);
2730 xfer_unsigned (2, dtl, dvi);
2740 (FILE * dtl, FILE * dvi)
2746 /* read post_post from dtl, and write in dvi */
2747 /* return number of bytes written */
2749 /* hope I'm writing the "223" bytes in an 8-bit clean way */
2750 int n223 = 0; /* number of "223" bytes in final padding */
2752 /* q[4] : DVI address of post command */
2753 /* --- unsigned? --- or signed, as I assume? */
2754 /* what happens if there is NO postamble command? */
2755 /* shouldn't q be -1 then? */
2757 xfer_postamble_address (dtl, dvi);
2759 /* i[1] : DVI identification byte = 2 */
2760 xfer_unsigned (1, dtl, dvi);
2762 for (n223 = 0; true; n223++)
2764 COUNT nread = 0; /* number of DTL bytes read by read_token */
2769 nread = read_token (dtl, token);
2771 /* check whether end of dtl file */
2776 /* dtl file shouldn't end before an ECOM */
2778 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2780 fprintf (stderr, "premature end of DTL file!\n");
2782 "%d complete iterations of \"padding byte\" loop;\n", n223);
2783 fprintf (stderr, "troublesome token = \"%s\"\n", token);
2786 /* leave the "223" loop */
2789 else if (strcmp (token, "223") == 0)
2791 /* token is a "223" padding byte */
2796 /* read a non-empty token that wasn't "223" */
2797 (Void) unread_char ();
2800 if (strcmp (token, ECOM) == 0)
2802 /* end of DTL's post_post command */
2806 /* error : expected end of post_post */
2808 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2810 fprintf (stderr, "token \"%s\" should be ECOM (\"%s\")\n",
2815 /* leave the "223" loop */
2824 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : \n",
2826 fprintf (stderr, "fewer than four `223' padding bytes.\n");
2827 fprintf (stderr, "Will write at least four `223' padding bytes.\n");
2830 /* check whether the DVI file size is a multiple of 4 bytes */
2831 if ((dvi_written + n223) % 4 != 0)
2834 fprintf (stderr, "(post_post) : WARNING : \n");
2835 fprintf (stderr, "DVI size ");
2836 fprintf (stderr, WF, dvi_written);
2837 fprintf (stderr, " (bytes) wouldn't be a multiple of 4 !\n");
2839 "Will write (at least four) `223' padding bytes until it is.\n");
2842 /* final padding of DVI file by "223" bytes to a multiple of 4 bytes, */
2843 /* with at least 4 bytes */
2845 for (n223 = 0; (n223 < 4) || (dvi_written % 4 != 0); n223++)
2847 /* add a "223" padding byte */
2848 put_byte (223, dvi);
2851 return (4 + 1 + n223);
2856 /* end of dt2dv.c */