Back to index

tetex-bin  3.0
otp2ocp.c
Go to the documentation of this file.
00001 /* otp2ocp.c: Main program for transforming OTP files into OCP files.
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 Copyright (c) 2002 Behdad Esfahbod
00008 Copyright (c) 2002 Roozbeh Pournader
00009 
00010 Omega is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation; either version 2 of the License, or
00013 (at your option) any later version.
00014 
00015 Omega is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with Omega; if not, write to the Free Software Foundation, Inc.,
00022 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00023 
00024 */
00025 
00026 #include <kpathsea/config.h>
00027 #include <kpathsea/types.h>
00028 #include <kpathsea/c-proto.h>
00029 #include <kpathsea/c-std.h>
00030 #include <kpathsea/c-fopen.h>
00031 #include <kpathsea/tex-file.h>
00032 #include "routines.h"
00033 
00034 extern FILE *yyin;
00035 int yywrap
00036 P1H (void)
00037 {
00038   return 1;
00039 }
00040 
00041 void output
00042 P2C (FILE *, output_file, int, i)
00043 {
00044 /* make sure output is in big-endian form */
00045   char j;
00046   int k;
00047   k = i;
00048   j = k >> 24;
00049   fwrite (&j, 1, 1, output_file);
00050   k = k & 0xffffff;
00051   j = k >> 16;
00052   fwrite (&j, 1, 1, output_file);
00053   k = k & 0xffff;
00054   j = k >> 8;
00055   fwrite (&j, 1, 1, output_file);
00056   j = k & 0xff;
00057   fwrite (&j, 1, 1, output_file);
00058 }
00059 
00060 void otp_read
00061 P2C (string, input_name, string, output_name)
00062 {
00063   int i, j, len, no_words;
00064   int *table, *instrs;
00065   FILE *input_file, *output_file;
00066 
00067   input_file = xfopen (input_name, FOPEN_R_MODE);
00068   store_state ("INITIAL");
00069   yyin = input_file;
00070   if (yyparse ())
00071     exit (EXIT_FAILURE);
00072   output_file = xfopen (output_name, FOPEN_WBIN_MODE);
00073   room_for_tables = 0;
00074   for (i = 0; i < no_tables; i++)
00075     {
00076       room_for_tables = room_for_tables + tables[i].length;
00077     }
00078   room_for_states = 0;
00079   for (i = 0; i < no_states; i++)
00080     {
00081       room_for_states = room_for_states + states[i].length;
00082     }
00083   no_words = no_tables + room_for_tables + no_states + room_for_states + 7;
00084   output (output_file, no_words);
00085   output (output_file, input_bytes);
00086   output (output_file, output_bytes);
00087   output (output_file, no_tables);
00088   output (output_file, room_for_tables);
00089   output (output_file, no_states);
00090   output (output_file, room_for_states);
00091   for (i = 0; i < no_tables; i++)
00092     {
00093       len = tables[i].length;
00094       output (output_file, len);
00095     }
00096   for (i = 0; i < no_tables; i++)
00097     {
00098       len = tables[i].length;
00099       table = tables[i].table;
00100       for (j = 0; j < len; j++)
00101        {
00102          output (output_file, table[j]);
00103        }
00104     }
00105   for (i = 0; i < no_states; i++)
00106     {
00107       len = states[i].length;
00108       output (output_file, len);
00109     }
00110   for (i = 0; i < no_states; i++)
00111     {
00112       len = states[i].length;
00113       instrs = states[i].instrs;
00114       for (j = 0; j < len; j++)
00115        {
00116          output (output_file, instrs[j]);
00117        }
00118     }
00119 }
00120 
00121 int main
00122 P2C (int, argc, string *, argv)
00123 {
00124   string input_name, full_input_name;
00125   string output_name;
00126 
00127   kpse_set_progname (argv[0]);
00128   switch (argc)
00129     {
00130     case 1:
00131       fprintf (stderr, "otp2ocp: No file given\n");
00132       return EXIT_FAILURE;
00133     case 2:
00134       {
00135        int len;
00136        input_name = argv[1];
00137        len = strlen (input_name);
00138        output_name = malloc (len + 5);
00139        if (!output_name)
00140          {
00141            fprintf (stderr, "otp2ocp: Memory exhausted\n");
00142            return EXIT_FAILURE;
00143          }
00144        strcpy (output_name, xbasename (input_name));
00145        len = strlen (output_name);
00146        if (len > 4 && 0 == strcmp (output_name + len - 4, ".otp"))
00147          {
00148            strcpy (output_name + len - 4, ".ocp");
00149          }
00150        else
00151          {
00152            strcat (output_name, ".ocp");
00153          }
00154        break;
00155       }
00156     case 3:
00157       input_name = argv[1];
00158       output_name = argv[2];
00159       break;
00160     default:
00161       fprintf (stderr, "otp2ocp: Too many arguments\n");
00162       return EXIT_FAILURE;
00163     }
00164   full_input_name = kpse_find_file (input_name, kpse_otp_format, true);
00165   if (!full_input_name)
00166     {
00167       fprintf (stderr, "otp2ocp: %s not found\n", input_name);
00168       return EXIT_FAILURE;
00169     }
00170   otp_read (full_input_name, output_name);
00171 
00172   return EXIT_SUCCESS;
00173 }