1 /* dt2dv - convert human-readable "DTL" file to DVI format
2 - this is intended to invert dv2dt version 0.6.0
3 - version 0.6.2 - 27 July 2005
4 - Geoffrey Tobin G.Tobin@ee.latrobe.edu.au
5 - fixes: Michal Tomczak-Jaegermann ntomczak@vm.ucs.ualberta.ca
6 Nelson H. F. Beebe beebe@math.utah.edu
7 Angus Leeming leeming@lyx.org: Enable dt2dv to handle
8 .dvi files containing strings longer than 1024 chars.
9 - Reference: "The DVI Driver Standard, Level 0",
10 by The TUG DVI Driver Standards Committee.
11 Appendix A, "Device-Independent File Format".
14 /* unix version; read from stdin, write to stdout, by default. */
25 /* by default, read and write regular files */
29 /* maximum number of characters in a DTL input line */
35 COUNT num; /* current line number */
36 size_t max; /* capacity of buf */
37 S4 wrote; /* number of characters written into buf */
38 size_t read; /* position in buf of next character to read from buf */
39 char * buf; /* line buffer */
42 char linebuf[MAXLINE+1];
44 Line dtl_line = {0, 0, 0, MAXLINE, linebuf};
46 /* a DTL token either is:
47 a quoted string (admitting an escape character),
48 or BCOM (if that is a nonempty string),
49 or ECOM (if that is a nonempty string),
50 or a string _not_ including ECOM_CHAR or space.
53 /* maximum expected length of a DTL token */
55 typedef char Token[MAXTOKLEN+1];
57 typedef unsigned char Byte;
63 /* command prefixes */
69 Byte first_suffix, last_suffix;
72 CmdPrefix cmd_prefixes [] =
74 {0, SETCHAR, true, 0, 127},
75 {128, SET, true, 1, 4},
76 {132, SETRULE, false, 0, 0},
77 {133, PUT, true, 1, 4},
78 {137, PUTRULE, false, 0, 0},
79 {138, NOP, false, 0, 0},
80 {139, BOP, false, 0, 0},
81 {140, EOP, false, 0, 0},
82 {141, PUSH, false, 0, 0},
83 {142, POP, false, 0, 0},
84 {143, RIGHT, true, 1, 4},
87 {157, DOWN, true, 1, 4},
90 {171, FONTNUM, true, 0, 63},
91 {235, FONT, true, 1, 4},
92 {239, SPECIAL, true, 1, 4},
93 {243, FONTDEF, true, 1, 4},
94 {247, PRE, false, 0, 0},
95 {248, POST, false, 0, 0},
96 {249, POSTPOST, false, 0, 0},
97 {250, OPCODE, true, 250, 255}
101 /* Number of DVI commands, including those officially undefined */
104 /* table of command name (string) pointers */
105 typedef char * CmdTable [NCMDS];
107 /* initially all command name pointers are NULL */
110 /* operation's opcode, name, number of args, string of arguments. */
119 /* name of table, first opcode, last opcode, pointer to opcode info. */
128 /* Table for opcodes 128 to 170 inclusive. */
130 op_info op_info_128_170 [] =
135 {131, SET4, 1, "-4"},
136 {132, SETRULE, 2, "-4 -4"},
140 {136, PUT4, 1, "-4"},
141 {137, PUTRULE, 2, "-4 -4"},
143 /* bop: not counting last argument, a signed address: */
144 {139, BOP, 10, "-4 -4 -4 -4 -4 -4 -4 -4 -4 -4"},
148 {143, RIGHT1, 1, "-1"},
149 {144, RIGHT2, 1, "-2"},
150 {145, RIGHT3, 1, "-3"},
151 {146, RIGHT4, 1, "-4"},
162 {157, DOWN1, 1, "-1"},
163 {158, DOWN2, 1, "-2"},
164 {159, DOWN3, 1, "-3"},
165 {160, DOWN4, 1, "-4"},
177 /* op_info op_info_128_170 [] */
179 op_table op_128_170 = {"op_128_170", 128, 170, op_info_128_170};
181 /* Table for fnt1 to fnt4 (opcodes 235 to 238) inclusive. */
185 {235, FONT1, 1, "1"},
186 {236, FONT2, 1, "2"},
187 {237, FONT3, 1, "3"},
188 {238, FONT4, 1, "-4"}
190 /* op_info fnt_n [] */
192 op_table fnt = {FONT, 235, 238, fnt_n};
195 /* Function prototypes */
197 Void mem_viol ARGS((int sig));
198 Void give_help (VOID);
199 int parse ARGS((char * s));
200 Void process ARGS((char * s));
203 Void dtl_stdin (VOID);
204 Void dvi_stdout (VOID);
206 int open_dtl ARGS((char * dtl_file, FILE ** pdtl));
207 int open_dvi ARGS((char * dvi_file, FILE ** pdvi));
209 int dt2dv ARGS((FILE * dtl, FILE * dvi));
211 Void * gmalloc ARGS((long int size));
214 Void dexit ARGS((int n));
216 int cons_cmds ARGS((int nprefixes, CmdPrefix * prefix, CmdTable cmds));
217 Void free_cmds ARGS((CmdTable cmd_table));
219 int get_line ARGS((FILE * fp, Line * line, int max));
220 int read_line_char ARGS((FILE * fp, int * ch));
221 int read_char ARGS((FILE * fp, int * ch));
222 int unread_char (VOID);
223 int read_string_char ARGS((FILE * fp, int * ch));
225 COUNT read_variety ARGS((FILE * dtl));
226 COUNT read_token ARGS((FILE * dtl, char * token));
227 COUNT skip_space ARGS((FILE * fp, int * ch));
228 COUNT read_misc ARGS((FILE * fp, Token token));
229 COUNT read_mes ARGS((FILE * fp, char * token));
231 int find_command ARGS((char * command, int * opcode));
232 int xfer_args ARGS((FILE * dtl, FILE * dvi, int opcode));
234 int set_seq ARGS((FILE * dtl, FILE * dvi));
236 int check_byte ARGS((int byte));
237 int put_byte ARGS((int onebyte, FILE * dvi));
239 U4 xfer_hex ARGS((int n, FILE * dtl, FILE * dvi));
240 U4 xfer_oct ARGS((int n, FILE * dtl, FILE * dvi));
241 U4 xfer_unsigned ARGS((int n, FILE * dtl, FILE * dvi));
242 S4 xfer_signed ARGS((int n, FILE * dtl, FILE * dvi));
244 int check_bmes ARGS((FILE * dtl));
245 int check_emes ARGS((FILE * dtl));
247 Void init_Lstring ARGS((Lstring * lsp, long int n));
248 Void de_init_Lstring ARGS((Lstring * lsp));
249 Lstring * alloc_Lstring ARGS((long int n));
250 Void free_Lstring ARGS((Lstring * lstr));
251 Void ls_putb ARGS((int ch, Lstring * lstr));
253 S4 get_Lstring ARGS((FILE * dtl, Lstring * lstr));
254 Void put_Lstring ARGS((const Lstring * lstr, FILE * dvi));
255 U4 xfer_len_string ARGS((int n, FILE * dtl, FILE * dvi));
257 U4 get_unsigned ARGS((FILE * dtl));
258 S4 get_signed ARGS((FILE * dtl));
260 int put_unsigned ARGS((int n, U4 unum, FILE * dvi));
261 int put_signed ARGS((int n, S4 snum, FILE * dvi));
263 S4 xfer_bop_address ARGS((FILE * dtl, FILE * dvi));
264 S4 xfer_postamble_address ARGS((FILE * dtl, FILE * dvi));
266 int put_table ARGS((op_table table, int opcode, FILE * dtl, FILE * dvi));
268 U4 special ARGS((FILE * dtl, FILE * dvi, int n));
269 int fontdef ARGS((FILE * dtl, FILE * dvi, int n));
271 U4 preamble ARGS((FILE * dtl, FILE * dvi));
272 int postamble ARGS((FILE * dtl, FILE * dvi));
273 int post_post ARGS((FILE * dtl, FILE * dvi));
278 char * keyword; /* command line option keyword */
279 int * p_var; /* pointer to option variable */
280 char * desc; /* description of keyword and value */
281 Void (* p_fn) (VOID); /* pointer to function called when option is set */
286 {"-debug", &debug, "detailed debugging", no_op},
287 {"-group", &group, "each DTL command is in parentheses", no_op},
288 {"-si", &rd_stdin, "read all DTL commands from standard input", dtl_stdin},
289 {"-so", &wr_stdout, "write all DVI commands to standard output", dvi_stdout},
290 {NULL, NULL, NULL, NULL}
294 char * progname = ""; /* intended for name of this program */
295 int nfile = 0; /* number of filename arguments on the command line */
297 #define PRINT_PROGNAME fprintf (stderr, "%s ", progname)
299 /* Harbison & Steele (1991) warn that some C implementations */
300 /* of free() do not treat NULL pointers correctly. */
301 #define gfree(p) {if (p) free (p);}
304 FILE * dtl_fp = NULL;
305 FILE * dvi_fp = NULL;
307 char * dtl_filename = "";
308 char * dvi_filename = "";
314 (int argc, char * argv[])
321 Void (*handler) ARGS((int)); /* Previous signal handler */
324 progname = argv[0]; /* name of this program */
326 /* memory violation signal handler */
328 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
341 #define C_LEVEL "non-"
342 #endif /* NOT STDC */
344 /* message about program and compiler */
345 /* NB: LTU EE's Sun/OS library is BSD, even though gcc 2.2.2 is ANSI */
347 fprintf (stderr, "\n");
349 "Program \"%s\" version %s compiled %s %s in %sstandard C.\n",
350 progname, VERSION, __DATE__, __TIME__, C_LEVEL);
352 /* interpret command line arguments */
355 dtl_fp = dvi_fp = NULL;
356 dtl_filename = dvi_filename = "";
358 for (i=1; i < argc; i++)
360 /* parse options, followed by any explicit filenames */
364 if (nfile != 2) /* not exactly two files specified, so give help */
368 dt2dv (dtl_fp, dvi_fp);
384 Void (* handler) ARGS((int));
385 handler = (Void (*) ARGS((int))) signal (SIGSEGV, mem_viol);
390 "(mem_viol) : called with wrong signal!\n");
393 fprintf (stderr, "(mem_viol) : RUNTIME MEMORY ERROR : memory violation, ");
394 fprintf (stderr, "dtl line >= ");
395 fprintf (stderr, WF, dtl_line.num);
396 fprintf (stderr, "\n");
407 fprintf (stderr, "usage: ");
409 fprintf (stderr, "[options] dtl_file dvi_file");
410 fprintf (stderr, "\n");
411 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
413 fprintf (stderr, " ");
414 fprintf (stderr, "[%s]", keyword);
415 fprintf (stderr, " ");
416 fprintf (stderr, "%s", opts[i].desc);
417 fprintf (stderr, "\n");
419 fprintf (stderr, "Messages, like this one, go to stderr.\n");
429 Void dtl_stdin (VOID)
431 extern FILE * dtl_fp;
435 dtl_filename = "Standard Input";
439 Void dvi_stdout (VOID)
441 extern FILE * dvi_fp;
444 /* ! Perilous to monitors! */
446 dvi_filename = "Standard Output";
459 /* parse one command-line argument, `s' */
463 for (i=0; (keyword = opts[i].keyword) != NULL; i++)
465 if (strncmp (s, keyword, strlen (keyword)) == 0)
468 (*(opts[i].p_var)) = 1; /* turn option on */
469 if ((pfn = opts[i].p_fn) != NULL)
470 (*pfn) (); /* call option function */
474 /* reached here, so not an option: assume it's a filename */
484 (char * dtl_file, FILE ** pdtl)
490 /* I: dtl_file; I: pdtl; O: *pdtl. */
492 extern char * dtl_filename;
494 dtl_filename = dtl_file;
496 if (dtl_filename == NULL)
500 "(open_dtl) : INTERNAL ERROR : dtl file's name is NULL.\n");
508 "(open_dtl) : INTERNAL ERROR : address of dtl variable is NULL.\n");
512 *pdtl = fopen (dtl_file, "r");
518 "(open_dtl) : DTL FILE ERROR : Cannot open \"%s\" for text reading.\n",
531 (char * dvi_file, FILE ** pdvi)
537 /* I: dvi_file; I: pdvi; O: *pdvi. */
539 extern char * dvi_filename;
541 dvi_filename = dvi_file;
543 if (dvi_filename == NULL)
547 "(open_dvi) : INTERNAL ERROR : dvi file's name is NULL.\n");
555 "(open_dvi) : INTERNAL ERROR : address of dvi variable is NULL.\n");
559 *pdvi = fopen (dvi_file, "wb");
565 "(open_dvi) : DVI FILE ERROR : Cannot open \"%s\" for binary writing.\n",
584 extern FILE * dtl_fp, * dvi_fp;
586 if (dtl_fp == NULL) /* first filename assumed to be DTL input */
588 open_dtl (s, &dtl_fp);
590 else if (dvi_fp == NULL) /* second filename assumed to be DVI output */
592 open_dvi (s, &dvi_fp);
597 fprintf (stderr, "(process) : at most two filenames allowed.\n");
605 COUNT dtl_read = 0; /* bytes read from dtl file */
606 COUNT dvi_written = 0; /* bytes written to dvi file */
607 word_t last_bop_address = -1; /* byte address of last bop; first bop uses -1 */
608 word_t postamble_address = -1; /* byte address of postamble */
609 COUNT ncom = 0; /* commands successfully read and interpreted from dtl file */
610 COUNT com_read = 0; /* bytes read in current (command and arguments), */
611 /* since and including the opening BCOM_CHAR, if any */
617 (int byte, FILE * dvi)
623 /* write byte into dvi file */
626 /* if (fprintf (dvi, "%c", byte) != 1) */
627 if (fprintf (dvi, "%c", byte) < 0)
631 "(put_byte) : DVI FILE ERROR (%s) : cannot write to dvi file.\n",
644 (FILE * dtl, FILE * dvi)
651 int nprefixes = 0; /* number of prefixes in cmd_prefixes[] list. */
652 static Token dtl_cmd = ""; /* DTL command name */
653 COUNT nread = 0; /* number of bytes read by a function from dtl file. */
655 nprefixes = sizeof (cmd_prefixes) / sizeof (CmdPrefix);
657 /* Construct array of all NCMDS == 256 DTL commands */
659 (Void) cons_cmds (nprefixes, cmd_prefixes, cmd_table);
661 /* DTL commands have the form "[ ]*command arg ... arg[ ]*", */
662 /* possibly enclosed in a BCOM, ECOM pair, */
663 /* and are separated by optional whitespace, typically newlines. */
664 /* That is, each command and its arguments are parenthesised, */
665 /* with optional spaces after the BCOM and before the ECOM, if any. */
667 /* dt2dv is now at the very start of the DTL file */
672 /* The very first thing should be the "variety" signature */
674 nread = read_variety (dtl);
676 /* while not end of dtl file or reading error, */
677 /* read, interpret, and write commands */
688 static Token token = ""; /* DTL token */
689 nread = read_token (dtl, token);
690 /* test for end of input, or reading error */
691 if (strlen (token) == 0)
696 fprintf (stderr, "(dt2dv) : end of input, or reading error.\n");
700 /* test whether this command begins correctly */
701 else if (strcmp (token, BCOM) != 0)
704 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
705 fprintf (stderr, "command must begin with \"%s\", ", BCOM);
706 fprintf (stderr, "not `%c' (char %d).\n", token[0], token[0]);
712 /* read the command name */
713 nread = read_token (dtl, dtl_cmd);
714 /* test for end of input, or reading error */
715 if (strlen (dtl_cmd) == 0)
721 "(dt2dv) : end of input, or reading error.\n");
730 fprintf (stderr, "(dt2dv) : command ");
731 fprintf (stderr, WF, ncom);
732 fprintf (stderr, " = \"%s\".\n", dtl_cmd);
735 /* find opcode for this command */
736 if (find_command (dtl_cmd, &opcode) == 1)
738 /* write the opcode, if we can */
739 put_byte (opcode, dvi);
741 /* treat the arguments, if any */
742 xfer_args (dtl, dvi, opcode);
744 else if (dtl_cmd[0] == BSEQ_CHAR)
746 /* sequence of font characters for SETCHAR */
753 "(dt2dv) : DTL FILE ERROR (%s) : unknown command \"%s\".\n",
754 dtl_filename, dtl_cmd);
761 /* seek ECOM after command's last argument and optional whitespace */
762 static Token token = ""; /* DTL token */
763 nread = read_token (dtl, token);
764 /* test for end of input, or reading error */
765 if (strlen (token) == 0)
771 "(dt2dv) : end of input, or reading error.\n");
775 if (strcmp (token, ECOM) != 0)
778 fprintf (stderr, "(dt2dv) : DTL FILE ERROR (%s) : ", dtl_filename);
779 fprintf (stderr, "ECOM (\"%s\") expected, not `%c' (char %d).\n",
780 ECOM, token[0], token[0]);
786 ++ ncom; /* one more command successfully read and interpreted */
791 fprintf (stderr, "(dt2dv) :\n");
792 fprintf (stderr, "Read (from file \"%s\") ", dtl_filename);
793 fprintf (stderr, WF, dtl_read);
794 fprintf (stderr, " DTL bytes (");
795 fprintf (stderr, UF4, dtl_line.num);
796 fprintf (stderr, " lines);\n");
797 fprintf (stderr, "wrote (to file \"%s\") ", dvi_filename);
798 fprintf (stderr, WF, dvi_written);
799 fprintf (stderr, " DVI bytes;\n");
800 fprintf (stderr, "completely interpreted ");
801 fprintf (stderr, WF, ncom);
802 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
803 fprintf (stderr, "\n");
805 (Void) free_cmds (cmd_table);
825 fprintf (stderr, "(gmalloc) : INTERNAL ERROR : ");
827 "unreasonable request to malloc %ld bytes\n",
831 p = (Void *) malloc ((size_t) size);
835 fprintf (stderr, "(gmalloc) : MEMORY ALLOCATION ERROR : ");
837 "operating system failed to malloc %ld bytes\n",
850 fprintf (stderr, "(dinfo) : ");
851 fprintf (stderr, "Current DTL input line ");
852 fprintf (stderr, UF4, dtl_line.num);
853 fprintf (stderr, " :\n");
854 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
855 fprintf (stderr, "Read ");
856 fprintf (stderr, WF, dtl_read);
857 fprintf (stderr, " DTL bytes (");
858 fprintf (stderr, WF, com_read);
859 fprintf (stderr, " in current command), wrote ");
860 fprintf (stderr, WF, dvi_written);
861 fprintf (stderr, " DVI bytes.\n");
862 fprintf (stderr, "Successfully interpreted ");
863 fprintf (stderr, WF, ncom);
864 fprintf (stderr, " DVI command%s.\n", (ncom == 1 ? "" : "s"));
880 fprintf (stderr, "(dexit) : exiting with status %d.\n", n);
889 (int nprefixes, CmdPrefix * prefix, CmdTable cmds)
891 (nprefixes, prefix, cmds)
897 int code; /* first opcode for a given command prefix */
898 int opcode; /* command's opcode */
899 int nsuffixes; /* number of commands with a given prefix */
900 int isuffix; /**** integer suffix, of at most three digits ****/
901 String suffix; /* suffix string generated from integer suffix */
902 size_t plen = 0; /* prefix length */
903 size_t slen; /* suffix length */
904 size_t clen; /* whole command name length */
905 int i, j; /* loop indices */
907 for (i=0; i < nprefixes; prefix++, i++)
909 code = prefix->first_code;
910 if (code < 0 || code > 255)
913 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
915 "prefix listed internally with code = %d, must be 0 to 255\n",
919 if (prefix->has_suffix)
921 plen = strlen (prefix->name);
922 /**** Suffixes in DTL are Integers, in Sequence */
923 if (prefix->last_suffix < prefix->first_suffix)
926 fprintf (stderr, "(cons_cmds) : INTERNAL ERROR : ");
928 "prefix's last suffix %d < first suffix (%d)\n",
929 prefix->last_suffix, prefix->first_suffix);
932 nsuffixes = prefix->last_suffix - prefix->first_suffix + 1;
933 opcode = prefix->first_code;
934 for (j=0; j < nsuffixes; j++, opcode++)
936 isuffix = prefix->first_suffix + j;
937 if (0 <= code && code <= 127) /* SETCHAR */
939 /* SETCHAR's suffix is written in uppercase hexadecimal */
940 sprintf (suffix, "%02X", isuffix);
942 else /* 128 <= code && code <= 255 */ /* other DTL commands */
944 /* other commands' suffices are written in decimal */
945 sprintf (suffix, "%d", isuffix);
947 slen = strlen (suffix);
949 cmds[opcode] = (char *) gmalloc (clen+1);
950 strcpy (cmds[opcode], prefix->name);
951 strcat (cmds[opcode], suffix);
954 else /* command name = prefix */
956 plen = strlen (prefix->name);
958 opcode = prefix->first_code;
959 cmds[opcode] = (char *) gmalloc (clen+1);
960 strcpy (cmds[opcode], prefix->name);
979 for (i=0; i < NCMDS; i++)
980 gfree (cmd_table[i]);
988 (FILE * fp, Line * line, int max)
995 /* read a (Line *) line from fp, return length */
996 /* adapted from K&R (second, alias ANSI C, edition, 1988), page 165 */
998 if (fgets (line->buf, max, fp) == NULL)
1003 line->wrote = strlen (line->buf);
1014 (FILE * fp, int * ch)
1020 /* read one character from dtl_line if possible, */
1021 /* otherwise read another dtl_line from fp */
1022 /* return 1 if a character is read, 0 if at end of fp file */
1024 extern Line dtl_line;
1025 if (dtl_line.wrote == 0 || dtl_line.read >= dtl_line.wrote)
1028 /* refill line buffer */
1029 line_status = get_line (fp, &dtl_line, MAXLINE);
1030 if (line_status == 0)
1032 /* at end of DTL file */
1036 fprintf (stderr, "(read_line_char) : end of DTL file\n");
1043 /* new DTL line was read */
1047 fprintf (stderr, "(read_line_char) : new DTL input line:\n");
1048 fprintf (stderr, "\"%s\"\n", dtl_line.buf);
1052 *ch = dtl_line.buf [dtl_line.read ++];
1054 ++ com_read; /* count another DTL command character */
1057 /* read_line_char */
1063 (FILE * fp, int * ch)
1069 /* Read next character, if any, from file fp. */
1070 /* Write it into *ch. */
1071 /* If no character is read, then *ch value < 0. */
1072 /* Return 1 if OK, 0 if EOF or error. */
1075 int c; /* in case ch points awry, we still have something in c. */
1078 if (read_line_char (fp, &c) == 0)
1080 /* end of fp file, or error reading it */
1089 "(read_char) : character %d not in range 0 to 255\n",
1094 else if ( ! isprint (c) && ! isspace (c))
1098 "(read_char) : character %d %s.\n",
1100 "not printable and not white space");
1120 /* read and check DTL variety signature */
1121 /* return number of DTL bytes written */
1122 /* DTL variety is _NEVER_ grouped by BCOM and ECOM. */
1123 /* Uniformity here enables the program easily to modify its behavior. */
1125 COUNT vread = 0; /* number of DTL bytes read by read_variety */
1126 COUNT nread = 0; /* number of DTL bytes read by read_token */
1127 static Token token = "";
1129 /* read the DTL VARIETY keyword */
1130 nread = read_token (dtl, token);
1132 /* test whether signature begins correctly */
1133 if (strcmp (token, "variety") != 0)
1136 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1137 fprintf (stderr, "DTL signature must begin with \"%s\", not \"%s\".\n",
1142 /* read the DTL variety */
1143 nread = read_token (dtl, token);
1145 /* test whether variety is correct */
1146 if (strcmp (token, VARIETY) != 0)
1149 fprintf (stderr, "(read_variety) : DTL FILE ERROR (%s) : ", dtl_filename);
1150 fprintf (stderr, "DTL variety must be \"%s\", not \"%s\".\n",
1156 fprintf (stderr, "(read_variety) : DTL variety %s is OK.\n", VARIETY);
1158 return vread; /* OK */
1166 (FILE * fp, int * ch)
1172 /* Skip whitespace characters in file fp. */
1173 /* Write in *ch the last character read from fp, */
1174 /* or < 0 if fp could not be read. */
1175 /* Return number of characters read from fp. */
1177 int c; /* character read (if any) */
1178 COUNT count; /* number (0 or more) of whitespace characters read */
1179 int nchar; /* number (0 or 1) of characters read by read_char */
1181 /* loop ends at: end of fp file, or reading error, or not a white space */
1182 for (count=0; ((nchar = read_char (fp, &c)) == 1 && isspace (c)); ++count)
1184 /* otherwise, more white spaces to skip */
1187 /* report when each DTL end of line is reached */
1191 fprintf (stderr, "(skip_space) : ");
1192 fprintf (stderr, "end of DTL line (at least) ");
1193 fprintf (stderr, WF, dtl_line.num);
1194 fprintf (stderr, "\n");
1204 *ch = c; /* c will be < 0 if read_char could not read fp */
1205 return (count + nchar);
1213 (FILE * dtl, char * token)
1219 /* read next token from dtl file. */
1220 /* return number of DTL bytes read. */
1221 /* A token is one of:
1222 a string from BMES_CHAR to the next unescaped EMES_CHAR, inclusive;
1223 BCOM or ECOM, unless these are empty strings;
1225 any other sequence of non-whitespace characters.
1228 COUNT nread; /* number of DTL bytes read by read_token */
1229 int ch; /* most recent character read */
1233 /* obtain first non-space character */
1234 /* add to nread the number of characters read from dtl by skip_space */
1235 nread += skip_space (dtl, &ch);
1239 /* end of dtl file */
1240 /* write an empty token */
1245 fprintf (stderr, "(read_token) : end of dtl file.\n");
1248 else if (group && ch == BCOM_CHAR)
1250 strcpy (token, BCOM);
1252 else if (group && ch == ECOM_CHAR)
1254 strcpy (token, ECOM);
1260 if (ch == BMES_CHAR) /* string token; read until unescaped EMES_CHAR */
1262 nread += read_mes (dtl, token+1);
1264 else if (ch == BSEQ_CHAR || ch == ESEQ_CHAR)
1266 /* token is complete */
1268 else /* any other string not containing (ECOM_CHAR or) whitespace */
1270 nread += read_misc (dtl, token+1);
1277 fprintf (stderr, "(read_token) : token = \"%s\"\n", token);
1280 return (nread); /* number of bytes read from dtl file */
1287 #define CHAR_EOS (-1)
1292 (FILE * fp, int * ch)
1299 int status = CHAR_OK; /* OK so far */
1302 if (read_char (fp, &c) == 0)
1303 status = CHAR_FAIL; /* fail */
1305 if (c == EMES_CHAR) /* end-of-string char */
1307 status = CHAR_EOS; /* end of string */
1309 else if (c == ESC_CHAR) /* escape character */
1311 /* accept the next character literally, even ESC_CHAR and EMES_CHAR */
1312 if (read_char (fp, &c) == 0)
1313 status = CHAR_FAIL; /* fail */
1319 /* read_string_char */
1325 (FILE * fp, Token token)
1334 /* loop ends at: end of fp file, or reading error, or a space */
1335 for (count=0; count <= MAXTOKLEN; ++count)
1337 if (read_char (fp, &c) == 0 || isspace (c))
1339 if (group && c == ECOM_CHAR)
1341 (Void) unread_char ();
1347 token[count] = '\0';
1356 (FILE * fp, char * token)
1362 /* called **AFTER** a BMES_CHAR has been read */
1363 /* read file fp for characters up to next unescaped EMES_CHAR */
1364 /* this is called a "string token" */
1365 /* write string, including EMES_CHAR, into token[] */
1366 /* return number of characters read from fp */
1368 COUNT dtl_count; /* number of DTL characters read by read_mes from fp */
1369 int more; /* flag more == 0 to terminate loop */
1370 int escape; /* flag escape == 1 if previous character was ESC_CHAR */
1371 int ch; /* current DTL character */
1378 if (read_char (fp, &ch) == 0)
1380 /* end of fp file, or reading error */
1383 else /* error checking passed */
1386 if (ch == EMES_CHAR && escape == 0) /* end of string */
1388 /* include final EMES_CHAR */
1392 else if (ch == ESC_CHAR && escape == 0)
1394 /* next character is not end of string */
1399 /* store any other character, */
1400 /* including escaped EMES_CHAR and ESC_CHAR*/
1414 /* wind input back, to allow rereading of one character */
1415 /* return 1 if this works, 0 on error */
1417 extern Line dtl_line;
1419 if (dtl_line.read > 0)
1421 -- dtl_line.read; /* back up one character in dtl_line */
1422 -- dtl_read; /* correct the count of DTL characters */
1423 -- com_read; /* count another DTL command character */
1424 status = 1; /* OK */
1426 else /* current DTL line is empty */
1428 status = 0; /* error */
1438 (char * command, int * opcode)
1449 for (i=0; i < NCMDS; i++)
1451 if ((cmd_table[i] != 0) && (strcmp (command, cmd_table[i]) == 0))
1474 if (byte < 0 || byte > 255)
1477 fprintf (stderr, "(check_byte) : INTERNAL ERROR : ");
1478 fprintf (stderr, "byte %d not in the range of 0 to 255.\n", byte);
1489 (FILE * dtl, FILE * dvi, int opcode)
1499 if (opcode >= 0 && opcode <= 127)
1500 ; /* SETCHAR uses no data */
1501 else if (opcode >= 128 && opcode <= 170)
1503 word_t this_bop_address = last_bop_address;
1505 if (opcode == 139) /* BOP */
1507 this_bop_address = dvi_written - 1;
1509 put_table (op_128_170, opcode, dtl, dvi);
1510 if (opcode == 139) /* BOP */
1512 xfer_bop_address (dtl, dvi);
1513 last_bop_address = this_bop_address;
1516 else if (opcode >= 171 && opcode <= 234)
1517 ; /* FONTNUM uses no data */
1518 else if (opcode >= 235 && opcode <= 238)
1519 put_table (fnt, opcode, dtl, dvi);
1520 else if (opcode >= 239 && opcode <= 242)
1523 special (dtl, dvi, n);
1525 else if (opcode >= 243 && opcode <= 246)
1528 fontdef (dtl, dvi, n);
1530 else if (opcode == 247)
1531 preamble (dtl, dvi);
1532 else if (opcode == 248)
1533 postamble (dtl, dvi);
1534 else if (opcode == 249)
1535 post_post (dtl, dvi);
1536 else if (opcode >= 250 && opcode <= 255)
1537 ; /* these, undefined, opcodes use no data */
1542 "(xfer_args) : opcode %d not handled.\n",
1554 (FILE * dtl, FILE * dvi)
1560 /* Called _after_ a BSEQ_CHAR command */
1561 /* Read bytes from dtl file, */
1562 /* writing corresponding SETCHAR or SET1 commands to DVI file, */
1563 /* _until_ unescaped ESEQ_CHAR is found */
1564 /* Return 1 if OK, 0 on error */
1565 /**** dt2dv assumes 8 bit characters, ****/
1566 /**** but some day one might change that. ****/
1568 int status = 1; /* status = 1 if OK, 0 if error */
1569 int more; /* sequence of font characters continuing? */
1570 int escape = 0; /* flag set if previous character was an escape */
1571 int ch; /* character read from DTL file */
1575 /* ignore read_char status, to allow unprintable characters */
1576 (Void) read_char (dtl, &ch);
1577 /* but check for end of dtl file, or serious file reading error */
1581 fprintf (stderr, "(set_seq) : ");
1582 fprintf (stderr, "end of dtl file, ");
1583 fprintf (stderr, "or serious dtl file reading error\n");
1586 status = 0; /* bad news */
1588 else /* read dtl file, okay */
1590 if (ch == ESC_CHAR && escape == 0) /* escape next character */
1596 if (ch == ESEQ_CHAR && escape == 0) /* end of sequence */
1600 else if (ch <= 127) /* can use SETCHAR */
1604 else if (ch <= 255) /* can use SET1 */
1606 put_byte (128, dvi); /* SET1 opcode */
1607 put_unsigned (1, (U4) ch, dvi);
1612 fprintf (stderr, "(set_seq) : ");
1614 "ERROR : DTL character %d is not in range 0 to 255\n",
1618 status = 0; /* Error, because dt2dv assumes 8 bit characters */
1620 escape = 0; /* current ch is not an escape character */
1632 (int n, FILE * dtl, FILE * dvi)
1639 /* translate unsigned n-byte hexadecimal number from dtl to dvi file. */
1640 /* return value of hexadecimal number */
1642 U4 unum = 0; /* at most this space needed */
1643 COUNT nread = 0; /* number of DTL bytes read by read_token */
1644 int nconv = 0; /* number of arguments converted by sscanf */
1645 static Token token = ""; /* DTL token */
1651 "(xfer_hex) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1656 nread = read_token (dtl, token);
1658 nconv = sscanf (token, XF4, &unum);
1663 fprintf (stderr, "(xfer_hex) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1664 dtl_filename, "hexadecimal number expected, not", token);
1668 put_unsigned (n, unum, dvi);
1678 (int n, FILE * dtl, FILE * dvi)
1685 /* translate unsigned n-byte octal number from dtl to dvi file. */
1686 /* return value of octal number */
1688 U4 unum = 0; /* at most this space needed */
1689 COUNT nread = 0; /* number of DTL bytes read by read_token */
1690 int nconv = 0; /* number of arguments converted by sscanf */
1691 static Token token = ""; /* DTL token */
1697 "(xfer_oct) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1702 nread = read_token (dtl, token);
1704 nconv = sscanf (token, OF4, &unum);
1709 fprintf (stderr, "(xfer_oct) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1710 dtl_filename, "octal number expected, not", token);
1714 put_unsigned (n, unum, dvi);
1724 (int n, FILE * dtl, FILE * dvi)
1731 /* translate unsigned n-byte number from dtl to dvi file. */
1732 /* return value of unsigned number */
1734 U4 unum = 0; /* at most this space needed */
1736 unum = get_unsigned (dtl);
1737 put_unsigned (n, unum, dvi);
1747 (int n, FILE * dtl, FILE * dvi)
1754 /* translate signed n-byte number from dtl to dvi file. */
1755 /* return value of signed number */
1759 snum = get_signed (dtl);
1760 put_signed (n, snum, dvi);
1775 /* read unsigned number from dtl file. */
1776 /* return value of unsigned number */
1778 U4 unum = 0; /* at most this space needed */
1779 COUNT nread = 0; /* number of DTL bytes read by read_token */
1780 int nconv = 0; /* number of arguments converted by sscanf */
1781 static Token token = ""; /* DTL token */
1783 nread = read_token (dtl, token);
1785 nconv = sscanf (token, UF4, &unum);
1790 fprintf (stderr, "(get_unsigned) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1791 dtl_filename, "unsigned number expected, not", token);
1808 /* read signed number from dtl file. */
1809 /* return value of signed number */
1812 COUNT nread = 0; /* number of DTL bytes read by read_token */
1813 int nconv = 0; /* number of sscanf arguments converted and assigned */
1814 static Token token = "";
1816 nread = read_token (dtl, token);
1818 nconv = sscanf (token, SF4, &snum);
1823 fprintf (stderr, "(get_signed) : DTL FILE ERROR (%s) : %s \"%s\".\n",
1824 dtl_filename, "signed number expected, not", token);
1836 (int n, U4 unum, FILE * dvi)
1843 /* put unsigned in-byte integer to dvi file */
1844 /* DVI format uses Big-endian storage of numbers: */
1845 /* most significant byte is first. */
1846 /* return number of bytes written. */
1848 Byte ubyte[4]; /* at most 4 bytes needed in DVI format */
1855 "(put_unsigned) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1860 /* Big-endian storage. */
1861 for (i = 0; i < n; i++)
1863 ubyte[i] = (Byte) (unum % 256);
1866 /* Reverse order for big-endian representation. */
1867 for (i = n-1; i >= 0; i--)
1869 put_byte ((int) ubyte[i], dvi);
1880 (int n, S4 snum, FILE * dvi)
1887 /* put signed in-byte integer to dvi file */
1888 /* DVI format uses 2's complement Big-endian storage of signed numbers: */
1889 /* most significant byte is first. */
1890 /* return number of bytes written. */
1892 /* Will this deal properly with the sign? */
1898 "(put_signed) : INTERNAL ERROR : asked for %d bytes. Must be 1 to 4.\n",
1903 /* How do we ensure 2's complement representation? */
1904 /* Here's a trick that I hope is portable, given ANSI C. */
1905 /* See K&R (2nd edition), Appendix A6.2 "Integral Conversions". */
1907 /* Convert snum to U4 data type */
1908 put_unsigned (n, (U4) snum, dvi);
1923 /* check that a BMES_CHAR is the next non-whitespace character in dtl */
1925 int ch; /* next non-whitespace character in dtl */
1927 /* `(Void)' because we ignore the number of spaces skipped */
1928 (Void) skip_space (dtl, &ch);
1933 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1935 fprintf (stderr, "end of dtl file, or reading error\n");
1939 if (ch != BMES_CHAR)
1942 fprintf (stderr, "(check_bmes) : DTL FILE ERROR (%s) : ",
1944 fprintf (stderr, "BMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1945 BMES_CHAR, "expected before string", ch, ch);
1962 /* check that an EMES_CHAR is the next character in dtl */
1964 int ch; /* dtl character */
1966 if (read_char (dtl, &ch) == 0)
1969 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1971 fprintf (stderr, "end of dtl file, or reading error\n");
1975 if (ch != EMES_CHAR)
1978 fprintf (stderr, "(check_emes) : DTL FILE ERROR (%s) : ",
1980 fprintf (stderr, "EMES_CHAR (`%c') %s, not `%c' (char %d).\n",
1981 EMES_CHAR, "expected to follow string", ch, ch);
1990 /* Size typically used in this program for Lstring variables */
1997 (Lstring * lsp, long int n)
2006 lsp->s = (char *) gmalloc (n);
2023 lsp->s = NULL; /* to be sure */
2025 /* de_init_Lstring */
2038 lsp = (Lstring *) gmalloc (sizeof (Lstring));
2039 init_Lstring (lsp, n);
2063 (int ch, Lstring * lstr)
2069 /* write byte ch into Lstring *lstr */
2071 if (lstr->l >= lstr->m)
2074 fprintf (stderr, "(ls_putb) : ERROR : No more room in Lstring.\n");
2079 lstr->s [(lstr->l)++] = ch;
2088 (FILE * dtl, Lstring * lstr)
2094 /* get a string from dtl file, store as an Lstring in *lstr. */
2095 /* lstr must already be allocated and initialised. */
2096 /* return length of Lstring *lstr */
2099 int char_status = CHAR_OK; /* OK so far */
2104 fprintf (stderr, "(get_Lstring) : entering get_Lstring.\n");
2113 "(get_Lstring) : string is: \"");
2116 for (nch=0; ; nch++)
2120 char_status = read_string_char (dtl, &ch);
2122 if (char_status == CHAR_FAIL)
2124 /* end of dtl file, or reading error */
2125 fprintf (stderr, "\n");
2127 fprintf (stderr, "(get_Lstring) : DTL FILE ERROR (%s) : ",
2129 fprintf (stderr, "cannot read string[");
2130 fprintf (stderr, UF4, nch);
2131 fprintf (stderr, "] from dtl file.\n");
2137 fprintf (stderr, "%c", ch);
2140 if (char_status == CHAR_EOS)
2142 if (ch != EMES_CHAR)
2145 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2146 fprintf (stderr, "char_status = CHAR_FAIL,\n");
2148 "but ch = %c (char %d) is not EMES_CHAR = %c (char %d)\n",
2149 ch, ch, EMES_CHAR, EMES_CHAR);
2152 (Void) unread_char ();
2153 break; /* end of string */
2155 else if (char_status == CHAR_OK)
2162 fprintf (stderr, "(get_Lstring) : INTERNAL ERROR : ");
2163 fprintf (stderr, "char_status = %d is unfamiliar!\n", char_status);
2171 fprintf (stderr, "\".\n");
2179 fprintf (stderr, "(get_Lstring) : leaving get_Lstring.\n");
2190 (const Lstring * lstr, FILE * dvi)
2198 fwret = fwrite ((lstr->s), sizeof (char), (size_t) (lstr->l), dvi);
2200 dvi_written += fwret;
2202 if (fwret < lstr->l)
2206 "(put_Lstring) : DVI File ERROR : not all bytes written ");
2207 fprintf (stderr, "(%ld of %ld).\n",
2208 (long int) fwret, (long int) (lstr->l));
2218 (int n, FILE * dtl, FILE * dvi)
2225 /* transfer (length and) quoted string from dtl to dvi file, */
2226 /* return number of bytes written to dvi file. */
2228 U4 k, k2, lstr_maxsize;
2234 fprintf (stderr, "(xfer_len_string) : entering xfer_len_string.\n");
2237 /* k[n] : length of special string */
2239 k = get_unsigned (dtl);
2241 lstr_maxsize = (k > LSIZE) ? k : LSIZE;
2242 init_Lstring (&lstr, lstr_maxsize);
2247 fprintf (stderr, "(xfer_len_string) : string's nominal length k = ");
2248 fprintf (stderr, UF4, k);
2249 fprintf (stderr, " characters.\n");
2252 k2 = get_Lstring (dtl, &lstr);
2257 fprintf (stderr, "(xfer_len_string) : WARNING : string length (");
2258 fprintf (stderr, UF4, k);
2259 fprintf (stderr, ") in DTL file is wrong\n");
2260 fprintf (stderr, "Writing correct value (");
2261 fprintf (stderr, UF4, k2);
2262 fprintf (stderr, ") to DVI file\n");
2265 put_unsigned (n, k2, dvi);
2267 put_Lstring (&lstr, dvi);
2272 fprintf (stderr, "(xfer_len_string) : leaving xfer_len_string.\n");
2275 de_init_Lstring (&lstr);
2279 /* xfer_len_string */
2285 (FILE * dtl, FILE * dvi)
2291 /* translate signed 4-byte bop address from dtl to dvi file. */
2292 /* return value of bop address written to DVI file */
2294 S4 snum = 0; /* at most this space needed for byte address */
2295 COUNT nread = 0; /* number of DTL bytes read by read_token */
2296 int nconv = 0; /* number of arguments converted by sscanf */
2297 static Token token = ""; /* DTL token */
2299 nread += read_token (dtl, token);
2301 nconv = sscanf (token, SF4, &snum);
2306 fprintf (stderr, "(xfer_bop_address) : DTL FILE ERROR (%s) : ",
2308 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2312 if (snum != last_bop_address)
2315 fprintf (stderr, "(xfer_bop_address) : WARNING : byte address (");
2316 fprintf (stderr, WF, snum);
2317 fprintf (stderr, ")\n");
2318 fprintf (stderr, "for previous bop in DTL file is wrong\n");
2319 fprintf (stderr, "Writing correct value (");
2320 fprintf (stderr, WF, last_bop_address);
2321 fprintf (stderr, ") to DVI file\n");
2324 put_signed (4, last_bop_address, dvi);
2326 return last_bop_address;
2328 /* xfer_bop_address */
2332 xfer_postamble_address
2334 (FILE * dtl, FILE * dvi)
2340 /* translate signed 4-byte postamble address from dtl to dvi file. */
2341 /* return value of postamble address written to DVI file */
2343 S4 snum = 0; /* at most this space needed for byte address */
2344 COUNT nread = 0; /* number of DTL bytes read by read_token */
2345 int nconv = 0; /* number of arguments converted by sscanf */
2346 static Token token = ""; /* DTL token */
2348 nread += read_token (dtl, token);
2350 nconv = sscanf (token, SF4, &snum);
2355 fprintf (stderr, "(xfer_postamble_address) : DTL FILE ERROR (%s) : ",
2357 fprintf (stderr, "signed number expected, not \"%s\".\n", token);
2361 if (snum != postamble_address)
2364 fprintf (stderr, "(xfer_postamble_address) : WARNING : byte address (");
2365 fprintf (stderr, WF, snum);
2366 fprintf (stderr, ")\n");
2367 fprintf (stderr, "for postamble in DTL file is wrong\n");
2368 fprintf (stderr, "Writing correct value (");
2369 fprintf (stderr, WF, postamble_address);
2370 fprintf (stderr, ") to DVI file\n");
2373 put_signed (4, postamble_address, dvi);
2375 return postamble_address;
2377 /* xfer_postamble_address */
2383 (op_table table, int opcode, FILE * dtl, FILE * dvi)
2385 (table, opcode, dtl, dvi)
2392 /* table: {char * name; int first, last; op_info * list; }; */
2393 /* op_info: {int code; char * name; int nargs; char * args; }; */
2395 op_info op; /* entry in table */
2397 int pos; /* current position in string being scanned */
2398 static String args = "";
2400 /* Defensive programming. */
2401 if (opcode < table.first || opcode > table.last)
2405 "(put_table) : DTL FILE (OR INTERNAL) ERROR : opcode %d ", opcode);
2406 fprintf (stderr, "is outside table %s [ %d to %d ] !\n",
2407 table.name, table.first, table.last);
2411 op = table.list [ opcode - table.first ];
2413 /* More defensive programming. */
2414 if (opcode != op.code)
2418 "(put_table) : INTERNAL ERROR : opcode %d for command \"%s\"",
2420 fprintf (stderr, " faulty in table \"%s\".\n", table.name);
2424 /* process all the arguments, according to size and sign */
2426 strncpy (args, op.args, MAXSTRLEN);
2429 for (i=0; i < op.nargs; i++)
2432 int nscan = 0; /* number of bytes read by sscanf */
2433 int nconv = 0; /* number of sscanf arguments converted & assigned */
2435 /* sscanf() does NOT advance over its input: */
2436 /* C strings lack internal state information, which C files have. */
2437 /* On Sun/OS, sscanf calls ungetc on the string it reads, */
2438 /* which therefore has to be writable. */
2440 nconv = sscanf (args + pos, "%d%n", &argtype, &nscan);
2442 if (nconv < 1 || nscan < 1)
2446 "(put_table) : INTERNAL ERROR : internal read of table %s failed!\n",
2454 xfer_signed (-argtype, dtl, dvi);
2456 xfer_unsigned (argtype, dtl, dvi);
2465 /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
2471 (FILE * dtl, FILE * dvi, int n)
2478 /* read special (1 <= n <= 4 byte) data from dtl, and write in dvi */
2479 /* return number of bytes written */
2486 fprintf (stderr, "(special) : entering special.\n");
2492 fprintf (stderr, "(special) : DTL FILE ERROR (%s) : special %d, ",
2494 fprintf (stderr, "range is 1 to 4.\n");
2498 /* k[n] : length of special string */
2499 /* x[k] : special string */
2501 nk = xfer_len_string (n, dtl, dvi);
2506 fprintf (stderr, "(special) : leaving special.\n");
2517 (FILE * dtl, FILE * dvi, int suffix)
2524 /* read fontdef fnt_def1 .. fnt_def4 from dtl, and write in dvi */
2525 /* suffix is the fontdef suffix : 1 to 4 */
2526 /* return number of bytes written */
2530 Lstring lstr1, lstr2;
2535 fprintf (stderr, "(fontdef) : entering fontdef.\n");
2538 if (suffix < 1 || suffix > 4)
2541 fprintf (stderr, "(fontdef) : DTL FILE ERROR (%s) : ",
2543 fprintf (stderr, "font def %d, but range is 1 to 4.\n", suffix);
2547 init_Lstring (&lstr1, LSIZE);
2548 init_Lstring (&lstr2, LSIZE);
2553 fprintf (stderr, "(fontdef) : about to read font number.\n");
2556 /* k[suffix] : font number */
2558 k = xfer_signed (suffix, dtl, dvi);
2560 k = xfer_unsigned (suffix, dtl, dvi);
2565 fprintf (stderr, "(fontdef) : font ");
2566 fprintf (stderr, UF4, k);
2567 fprintf (stderr, ".\n");
2571 /* c[4] : (hexadecimal) checksum : I (gt) would prefer this */
2572 xfer_hex (4, dtl, dvi);
2573 #else /*NOT HEX_CHECKSUM */
2574 /* c[4] : checksum (octal, for comparison with tftopl's .pl file) */
2575 xfer_oct (4, dtl, dvi);
2579 xfer_unsigned (4, dtl, dvi);
2582 xfer_unsigned (4, dtl, dvi);
2584 /* If DTL file's edited, a and l may be wrong. */
2586 /* a[1] : length of font `area' (directory) portion of pathname string */
2587 a = get_unsigned (dtl);
2589 /* l[1] : length of font portion of pathname string */
2590 l = get_unsigned (dtl);
2592 /* n[a+l] : font pathname string <= area + font */
2594 a2 = get_Lstring (dtl, &lstr1);
2599 fprintf (stderr, "(fontdef) : WARNING : font area string's length (");
2600 fprintf (stderr, UF4, a);
2601 fprintf (stderr, ") in DTL file is wrong\n");
2602 fprintf (stderr, "Writing correct value (");
2603 fprintf (stderr, UF4, a2);
2604 fprintf (stderr, ") to DVI file\n");
2607 put_unsigned (1, a2, dvi);
2609 l2 = get_Lstring (dtl, &lstr2);
2614 fprintf (stderr, "(fontdef) : WARNING : font string's length (");
2615 fprintf (stderr, UF4, l);
2616 fprintf (stderr, ") in DTL file is wrong\n");
2617 fprintf (stderr, "Writing correct value (");
2618 fprintf (stderr, UF4, l2);
2619 fprintf (stderr, ") to DVI file\n");
2622 put_unsigned (1, l2, dvi);
2624 put_Lstring (&lstr1, dvi);
2625 put_Lstring (&lstr2, dvi);
2627 de_init_Lstring (&lstr2);
2628 de_init_Lstring (&lstr1);
2633 fprintf (stderr, "(fontdef) : leaving fontdef.\n");
2636 return (suffix + 4*4 + 2*1 + a2 + l2);
2644 (FILE * dtl, FILE * dvi)
2650 /* read preamble from dtl, and write in dvi */
2651 /* return number of bytes written */
2658 fprintf (stderr, "(preamble) : entering preamble.\n");
2662 xfer_unsigned (1, dtl, dvi);
2665 xfer_unsigned (4, dtl, dvi);
2668 xfer_unsigned (4, dtl, dvi);
2671 xfer_unsigned (4, dtl, dvi);
2673 /* k[1] : length of comment */
2674 /* x[k] : comment string */
2676 k1 = xfer_len_string (1, dtl, dvi);
2681 fprintf (stderr, "(preamble) : leaving preamble.\n");
2684 return (1 + 3*4 + k1);
2692 (FILE * dtl, FILE * dvi)
2698 /* read postamble from dtl, and write in dvi */
2699 /* return number of bytes written */
2701 postamble_address = dvi_written - 1;
2703 /* p[4] : DVI address of previous bop command */
2704 /* --- unsigned? --- or signed, as I assume? */
2705 /* For, surely p should be -1 if the DVI file has NO bop? */
2706 xfer_bop_address (dtl, dvi);
2709 xfer_unsigned (4, 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 (2, dtl, dvi);
2727 xfer_unsigned (2, dtl, dvi);
2737 (FILE * dtl, FILE * dvi)
2743 /* read post_post from dtl, and write in dvi */
2744 /* return number of bytes written */
2746 /* hope I'm writing the "223" bytes in an 8-bit clean way */
2747 int n223 = 0; /* number of "223" bytes in final padding */
2749 /* q[4] : DVI address of post command */
2750 /* --- unsigned? --- or signed, as I assume? */
2751 /* what happens if there is NO postamble command? */
2752 /* shouldn't q be -1 then? */
2754 xfer_postamble_address (dtl, dvi);
2756 /* i[1] : DVI identification byte = 2 */
2757 xfer_unsigned (1, dtl, dvi);
2759 for (n223 = 0; true; n223++)
2761 COUNT nread = 0; /* number of DTL bytes read by read_token */
2766 nread = read_token (dtl, token);
2768 /* check whether end of dtl file */
2773 /* dtl file shouldn't end before an ECOM */
2775 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2777 fprintf (stderr, "premature end of DTL file!\n");
2779 "%d complete iterations of \"padding byte\" loop;\n", n223);
2780 fprintf (stderr, "troublesome token = \"%s\"\n", token);
2783 /* leave the "223" loop */
2786 else if (strcmp (token, "223") == 0)
2788 /* token is a "223" padding byte */
2793 /* read a non-empty token that wasn't "223" */
2794 (Void) unread_char ();
2797 if (strcmp (token, ECOM) == 0)
2799 /* end of DTL's post_post command */
2803 /* error : expected end of post_post */
2805 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : ",
2807 fprintf (stderr, "token \"%s\" should be ECOM (\"%s\")\n",
2812 /* leave the "223" loop */
2821 fprintf (stderr, "(post_post) : DTL FILE ERROR (%s) : \n",
2823 fprintf (stderr, "fewer than four `223' padding bytes.\n");
2824 fprintf (stderr, "Will write at least four `223' padding bytes.\n");
2827 /* check whether the DVI file size is a multiple of 4 bytes */
2828 if ((dvi_written + n223) % 4 != 0)
2831 fprintf (stderr, "(post_post) : WARNING : \n");
2832 fprintf (stderr, "DVI size ");
2833 fprintf (stderr, WF, dvi_written);
2834 fprintf (stderr, " (bytes) wouldn't be a multiple of 4 !\n");
2836 "Will write (at least four) `223' padding bytes until it is.\n");
2839 /* final padding of DVI file by "223" bytes to a multiple of 4 bytes, */
2840 /* with at least 4 bytes */
2842 for (n223 = 0; (n223 < 4) || (dvi_written % 4 != 0); n223++)
2844 /* add a "223" padding byte */
2845 put_byte (223, dvi);
2848 return (4 + 1 + n223);
2853 /* end of dt2dv.c */