| 1 |
/* outocp.c: ASCII output for OCP files, mainly for debugging purposes. |
| 2 |
|
| 3 |
This file is part of Omega, |
| 4 |
which is based on the web2c distribution of TeX. |
| 5 |
|
| 6 |
Copyright (c) 1994--2001 John Plaice and Yannis Haralambous |
| 7 |
Copyright (C) 2005 Roozbeh Pournader |
| 8 |
|
| 9 |
Omega is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 2 of the License, or |
| 12 |
(at your option) any later version. |
| 13 |
|
| 14 |
Omega is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with Omega; if not, write to the Free Software Foundation, Inc., |
| 21 |
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 22 |
|
| 23 |
*/ |
| 24 |
|
| 25 |
#include <kpathsea/config.h> |
| 26 |
#include <kpathsea/types.h> |
| 27 |
#include <kpathsea/c-std.h> |
| 28 |
#include <kpathsea/c-fopen.h> |
| 29 |
#include <kpathsea/tex-file.h> |
| 30 |
#include "otp.h" |
| 31 |
|
| 32 |
const char *otp_names[] = { |
| 33 |
" ", |
| 34 |
"OTP_RIGHT_OUTPUT ", |
| 35 |
"OTP_RIGHT_NUM ", |
| 36 |
"OTP_RIGHT_CHAR ", |
| 37 |
"OTP_RIGHT_LCHAR ", |
| 38 |
"OTP_RIGHT_SOME ", |
| 39 |
"OTP_PBACK_OUTPUT ", |
| 40 |
"OTP_PBACK_NUM ", |
| 41 |
"OTP_PBACK_CHAR ", |
| 42 |
"OTP_PBACK_LCHAR ", |
| 43 |
"OTP_PBACK_SOME ", |
| 44 |
"OTP_ADD ", |
| 45 |
"OTP_SUB ", |
| 46 |
"OTP_MULT ", |
| 47 |
"OTP_DIV ", |
| 48 |
"OTP_MOD ", |
| 49 |
"OTP_LOOKUP ", |
| 50 |
"OTP_PUSH_NUM ", |
| 51 |
"OTP_PUSH_CHAR ", |
| 52 |
"OTP_PUSH_LCHAR ", |
| 53 |
"OTP_STATE_CHANGE ", |
| 54 |
"OTP_STATE_PUSH ", |
| 55 |
"OTP_STATE_POP ", |
| 56 |
"OTP_LEFT_START ", |
| 57 |
"OTP_LEFT_RETURN ", |
| 58 |
"OTP_LEFT_BACKUP ", |
| 59 |
"OTP_GOTO ", |
| 60 |
"OTP_GOTO_NE ", |
| 61 |
"OTP_GOTO_EQ ", |
| 62 |
"OTP_GOTO_LT ", |
| 63 |
"OTP_GOTO_LE ", |
| 64 |
"OTP_GOTO_GT ", |
| 65 |
"OTP_GOTO_GE ", |
| 66 |
"OTP_GOTO_NO_ADVANCE", |
| 67 |
"OTP_GOTO_BEG ", |
| 68 |
"OTP_GOTO_END ", |
| 69 |
"OTP_STOP " |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
int no_words_read = 0; |
| 74 |
|
| 75 |
static int |
| 76 |
ctp_get(FILE *input_file) |
| 77 |
{ |
| 78 |
int ctpchar; |
| 79 |
ctpchar = getc (input_file); |
| 80 |
if (ctpchar == EOF) |
| 81 |
{ |
| 82 |
FATAL ("Unexpected end of file"); |
| 83 |
} |
| 84 |
return ctpchar; |
| 85 |
} |
| 86 |
|
| 87 |
static int |
| 88 |
ctp_read(FILE *input_file) |
| 89 |
{ |
| 90 |
int ctpword; |
| 91 |
no_words_read++; |
| 92 |
ctpword = ctp_get (input_file); |
| 93 |
if (ctpword > 127) |
| 94 |
{ |
| 95 |
FATAL ("First octet > 127"); |
| 96 |
} |
| 97 |
ctpword = ctpword * 256 + ctp_get (input_file); |
| 98 |
ctpword = ctpword * 256 + ctp_get (input_file); |
| 99 |
ctpword = ctpword * 256 + ctp_get (input_file); |
| 100 |
return ctpword; |
| 101 |
} |
| 102 |
|
| 103 |
static void |
| 104 |
ctp_explain(FILE *input_file) |
| 105 |
{ |
| 106 |
int i, j, arg, entry, instruction; |
| 107 |
int ctp_length, real_ctp_length, ctp_input, ctp_output, |
| 108 |
ctp_no_tables, ctp_room_tables, ctp_no_states, ctp_room_states; |
| 109 |
int room_tables[OTP_MAXTABLES], room_states[OTP_MAXSTATES]; |
| 110 |
|
| 111 |
ctp_length = ctp_read (input_file); |
| 112 |
fprintf (stdout, "ctp_length : %3x(%3d)\n", ctp_length, ctp_length); |
| 113 |
real_ctp_length = ctp_length - 7; |
| 114 |
fprintf (stdout, "real_ctp_length: %3x(%3d)\n", |
| 115 |
real_ctp_length, real_ctp_length); |
| 116 |
ctp_input = ctp_read (input_file); |
| 117 |
fprintf (stdout, "ctp_input : %3x(%3d)\n", ctp_input, ctp_input); |
| 118 |
ctp_output = ctp_read (input_file); |
| 119 |
fprintf (stdout, "ctp_output : %3x(%3d)\n", ctp_output, ctp_output); |
| 120 |
ctp_no_tables = ctp_read (input_file); |
| 121 |
fprintf (stdout, "ctp_no_tables : %3x(%3d)\n", |
| 122 |
ctp_no_tables, ctp_no_tables); |
| 123 |
ctp_room_tables = ctp_read (input_file); |
| 124 |
fprintf (stdout, "ctp_room_tables: %3x(%3d)\n", |
| 125 |
ctp_room_tables, ctp_room_tables); |
| 126 |
ctp_no_states = ctp_read (input_file); |
| 127 |
fprintf (stdout, "ctp_no_states : %3x(%3d)\n", |
| 128 |
ctp_no_states, ctp_no_states); |
| 129 |
ctp_room_states = ctp_read (input_file); |
| 130 |
fprintf (stdout, "ctp_room_states: %3x(%3d)\n\n", |
| 131 |
ctp_room_states, ctp_room_states); |
| 132 |
if (ctp_no_tables >= OTP_MAXTABLES) |
| 133 |
{ |
| 134 |
FATAL ("Too many tables"); |
| 135 |
} |
| 136 |
if (ctp_no_states >= OTP_MAXSTATES) |
| 137 |
{ |
| 138 |
FATAL ("Too many states"); |
| 139 |
} |
| 140 |
if (ctp_no_tables != 0) |
| 141 |
{ |
| 142 |
for (i = 0; i < ctp_no_tables; i++) |
| 143 |
{ |
| 144 |
room_tables[i] = ctp_read (input_file); |
| 145 |
fprintf (stdout, "Table %2x(%2d): %3x(%3d)entries\n", |
| 146 |
i, i, room_tables[i], room_tables[i]); |
| 147 |
} |
| 148 |
fprintf (stdout, "\n"); |
| 149 |
for (i = 0; i < ctp_no_tables; i++) |
| 150 |
{ |
| 151 |
for (j = 0; j < room_tables[i]; j++) |
| 152 |
{ |
| 153 |
entry = ctp_read (input_file); |
| 154 |
fprintf (stdout, "Table %2x(%2d), entry %3x(%3d): %2x(%3d)\n", |
| 155 |
i, i, j, j, entry, entry); |
| 156 |
} |
| 157 |
} |
| 158 |
fprintf (stdout, "\n"); |
| 159 |
} |
| 160 |
if (ctp_no_states != 0) |
| 161 |
{ |
| 162 |
for (i = 0; i < ctp_no_states; i++) |
| 163 |
{ |
| 164 |
room_states[i] = ctp_read (input_file); |
| 165 |
fprintf (stdout, "State %2x(%2d): %3x(%3d) entries\n", |
| 166 |
i, i, room_states[i], room_states[i]); |
| 167 |
} |
| 168 |
fprintf (stdout, "\n"); |
| 169 |
for (i = 0; i < ctp_no_states; i++) |
| 170 |
{ |
| 171 |
for (j = 0; j < room_states[i]; j++) |
| 172 |
{ |
| 173 |
instruction = ctp_read (input_file); |
| 174 |
arg = instruction & 0xffffff; |
| 175 |
if ((arg >= 32) && (arg < 127)) |
| 176 |
fprintf (stdout, |
| 177 |
"State %2x(%2d), entry %3x(%3d): %s %2x(%3d,`%c')\n", |
| 178 |
i, i, j, j, otp_names[instruction >> 24], |
| 179 |
arg, arg, arg); |
| 180 |
else |
| 181 |
fprintf (stdout, |
| 182 |
"State %2x(%2d), entry %3x(%3d): %s %2x(%3d)\n", |
| 183 |
i, i, j, j, otp_names[instruction >> 24], arg, arg); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
fprintf (stdout, "\nfile length should be: %3x(%3d)\n", |
| 188 |
ctp_length, ctp_length); |
| 189 |
fprintf (stdout, "number words read : %3x(%3d)\n", |
| 190 |
no_words_read, no_words_read); |
| 191 |
} |
| 192 |
|
| 193 |
int |
| 194 |
main(int argc, string *argv) |
| 195 |
{ |
| 196 |
string input_name, full_input_name; |
| 197 |
FILE *input_file; |
| 198 |
|
| 199 |
kpse_set_program_name (argv[0], "outocp"); |
| 200 |
switch (argc) |
| 201 |
{ |
| 202 |
case 1: |
| 203 |
FATAL ("No file given"); |
| 204 |
case 2: |
| 205 |
input_name = argv[1]; |
| 206 |
break; |
| 207 |
default: |
| 208 |
FATAL ("Too many arguments"); |
| 209 |
} |
| 210 |
full_input_name = kpse_find_file (input_name, kpse_ocp_format, true); |
| 211 |
if (!full_input_name) |
| 212 |
{ |
| 213 |
FATAL1 ("%s not found", input_name); |
| 214 |
} |
| 215 |
input_file = xfopen (full_input_name, FOPEN_RBIN_MODE); |
| 216 |
ctp_explain (input_file); |
| 217 |
|
| 218 |
return EXIT_SUCCESS; |
| 219 |
} |