Back to index

tetex-bin  3.0
outocp.c
Go to the documentation of this file.
00001 /* outocp.c: ASCII output for OCP files, mainly for debugging purposes.
00002 
00003 This file is part of Omega,
00004 which is based on the web2c distribution of TeX,
00005 
00006 Copyright (c) 1994--2001 John Plaice and Yannis Haralambous
00007 
00008 Omega is free software; you can redistribute it and/or modify
00009 it under the terms of the GNU General Public License as published by
00010 the Free Software Foundation; either version 2 of the License, or
00011 (at your option) any later version.
00012 
00013 Omega is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Omega; if not, write to the Free Software Foundation, Inc.,
00020 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00021 
00022 */
00023 
00024 #include <kpathsea/config.h>
00025 #include <kpathsea/types.h>
00026 #include <kpathsea/c-proto.h>
00027 #include <kpathsea/c-std.h>
00028 #include <kpathsea/c-fopen.h>
00029 #include <kpathsea/tex-file.h>
00030 #include "otp.h"
00031 
00032 char *otp_names[] = {
00033   "                   ",
00034   "OTP_RIGHT_OUTPUT   ",
00035   "OTP_RIGHT_NUM      ",
00036   "OTP_RIGHT_CHAR     ",
00037   "OTP_RIGHT_LCHAR    ",
00038   "OTP_RIGHT_SOME     ",
00039   "OTP_PBACK_OUTPUT   ",
00040   "OTP_PBACK_NUM      ",
00041   "OTP_PBACK_CHAR     ",
00042   "OTP_PBACK_LCHAR    ",
00043   "OTP_PBACK_SOME     ",
00044   "OTP_ADD            ",
00045   "OTP_SUB            ",
00046   "OTP_MULT           ",
00047   "OTP_DIV            ",
00048   "OTP_MOD            ",
00049   "OTP_LOOKUP         ",
00050   "OTP_PUSH_NUM       ",
00051   "OTP_PUSH_CHAR      ",
00052   "OTP_PUSH_LCHAR     ",
00053   "OTP_STATE_CHANGE   ",
00054   "OTP_STATE_PUSH     ",
00055   "OTP_STATE_POP      ",
00056   "OTP_LEFT_START     ",
00057   "OTP_LEFT_RETURN    ",
00058   "OTP_LEFT_BACKUP    ",
00059   "OTP_GOTO           ",
00060   "OTP_GOTO_NE        ",
00061   "OTP_GOTO_EQ        ",
00062   "OTP_GOTO_LT        ",
00063   "OTP_GOTO_LE        ",
00064   "OTP_GOTO_GT        ",
00065   "OTP_GOTO_GE        ",
00066   "OTP_GOTO_NO_ADVANCE",
00067   "OTP_GOTO_BEG       ",
00068   "OTP_GOTO_END       ",
00069   "OTP_STOP           "
00070 };
00071 
00072 
00073 void ctp_abort
00074 P1C (string, s)
00075 {
00076   fprintf (stderr, s);
00077   exit (EXIT_FAILURE);
00078 }
00079 
00080 int no_words_read = 0;
00081 
00082 int ctp_get
00083 P1C (FILE *, input_file)
00084 {
00085   int ctpchar;
00086   ctpchar = getc (input_file);
00087   if (ctpchar == EOF)
00088     {
00089       ctp_abort ("Unexpected end of file\n");
00090     }
00091   return ctpchar;
00092 }
00093 
00094 int ctp_read
00095 P1C (FILE *, input_file)
00096 {
00097   int ctpword;
00098   no_words_read++;
00099   ctpword = ctp_get (input_file);
00100   if (ctpword > 127)
00101     {
00102       ctp_abort ("first octet > 127");
00103     }
00104   ctpword = ctpword * 256 + ctp_get (input_file);
00105   ctpword = ctpword * 256 + ctp_get (input_file);
00106   ctpword = ctpword * 256 + ctp_get (input_file);
00107   return ctpword;
00108 }
00109 
00110 void ctp_explain
00111 P1C (FILE *, input_file)
00112 {
00113   int i, j, arg, entry, instruction;
00114   int ctp_length, real_ctp_length, ctp_input, ctp_output,
00115     ctp_no_tables, ctp_room_tables, ctp_no_states, ctp_room_states;
00116   int room_tables[100], room_states[100];
00117 
00118   ctp_length = ctp_read (input_file);
00119   fprintf (stdout, "ctp_length     : %3x(%3d)\n", ctp_length, ctp_length);
00120   real_ctp_length = ctp_length - 7;
00121   fprintf (stdout, "real_ctp_length: %3x(%3d)\n",
00122           real_ctp_length, real_ctp_length);
00123   ctp_input = ctp_read (input_file);
00124   fprintf (stdout, "ctp_input      : %3x(%3d)\n", ctp_input, ctp_input);
00125   ctp_output = ctp_read (input_file);
00126   fprintf (stdout, "ctp_output     : %3x(%3d)\n", ctp_output, ctp_output);
00127   ctp_no_tables = ctp_read (input_file);
00128   fprintf (stdout, "ctp_no_tables  : %3x(%3d)\n",
00129           ctp_no_tables, ctp_no_tables);
00130   ctp_room_tables = ctp_read (input_file);
00131   fprintf (stdout, "ctp_room_tables: %3x(%3d)\n",
00132           ctp_room_tables, ctp_room_tables);
00133   ctp_no_states = ctp_read (input_file);
00134   fprintf (stdout, "ctp_no_states  : %3x(%3d)\n",
00135           ctp_no_states, ctp_no_states);
00136   ctp_room_states = ctp_read (input_file);
00137   fprintf (stdout, "ctp_room_states: %3x(%3d)\n\n",
00138           ctp_room_states, ctp_room_states);
00139   if (ctp_no_tables >= 100)
00140     {
00141       ctp_abort ("Too many tables\n");
00142     }
00143   if (ctp_no_states >= 100)
00144     {
00145       ctp_abort ("Too many states\n");
00146     }
00147   if (ctp_no_tables != 0)
00148     {
00149       for (i = 0; i < ctp_no_tables; i++)
00150        {
00151          room_tables[i] = ctp_read (input_file);
00152          fprintf (stdout, "Table %2x(%2d): %3x(%3d)entries\n",
00153                  i, i, room_tables[i], room_tables[i]);
00154        }
00155       fprintf (stdout, "\n");
00156       for (i = 0; i < ctp_no_tables; i++)
00157        {
00158          for (j = 0; j < room_tables[i]; j++)
00159            {
00160              entry = ctp_read (input_file);
00161              fprintf (stdout, "Table %2x(%2d), entry %3x(%3d): %2x(%3d)\n",
00162                      i, i, j, j, entry, entry);
00163            }
00164        }
00165       fprintf (stdout, "\n");
00166     }
00167   if (ctp_no_states != 0)
00168     {
00169       for (i = 0; i < ctp_no_states; i++)
00170        {
00171          room_states[i] = ctp_read (input_file);
00172          fprintf (stdout, "State %2x(%2d): %3x(%3d) entries\n",
00173                  i, i, room_states[i], room_states[i]);
00174        }
00175       fprintf (stdout, "\n");
00176       for (i = 0; i < ctp_no_states; i++)
00177        {
00178          for (j = 0; j < room_states[i]; j++)
00179            {
00180              instruction = ctp_read (input_file);
00181              arg = instruction & 0xffffff;
00182              if ((arg >= 32) && (arg < 127))
00183               fprintf (stdout,
00184                       "State %2x(%2d), entry %3x(%3d): %s %2x(%3d,`%c')\n",
00185                       i, i, j, j, otp_names[instruction >> 24],
00186                       arg, arg, arg);
00187              else
00188               fprintf (stdout,
00189                       "State %2x(%2d), entry %3x(%3d): %s %2x(%3d)\n",
00190                       i, i, j, j, otp_names[instruction >> 24], arg, arg);
00191            }
00192        }
00193     }
00194   fprintf (stdout, "\nfile length should be: %3x(%3d)\n",
00195           ctp_length, ctp_length);
00196   fprintf (stdout, "number words read    : %3x(%3d)\n",
00197           no_words_read, no_words_read);
00198 }
00199 
00200 int main
00201 P2C (int, argc, string *, argv)
00202 {
00203   string input_name, full_input_name;
00204   FILE *input_file;
00205 
00206   kpse_set_progname (argv[0]);
00207   switch (argc)
00208     {
00209     case 1:
00210       fprintf (stderr, "outocp: No file given\n");
00211       return EXIT_FAILURE;
00212     case 2:
00213       input_name = argv[1];
00214       break;
00215     default:
00216       fprintf (stderr, "outocp: Too many arguments\n");
00217       return EXIT_FAILURE;
00218     }
00219   full_input_name = kpse_find_file (input_name, kpse_ocp_format, true);
00220   if (!full_input_name)
00221     {
00222       fprintf (stderr, "outocp: %s not found\n", input_name);
00223       return EXIT_FAILURE;
00224     }
00225   input_file = xfopen (full_input_name, FOPEN_RBIN_MODE);
00226   ctp_explain (input_file);
00227   return EXIT_SUCCESS;
00228 }