home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / GNU / recode-3.4-MIHS / src / charset.pl < prev    next >
Encoding:
Text File  |  1994-01-06  |  6.7 KB  |  315 lines

  1. # Automatically derive charset.c from rfc1345.txt.
  2. # Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  3. # Francois Pinard <pinard@iro.umontreal.ca>, 1993.
  4.  
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9.  
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14.  
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. $overall_header = <<END_OF_TEXT;
  20. /* DO NOT MODIFY THIS FILE!  It was generated by "charset.pl".  */
  21.  
  22. /* Conversion of files between different charsets and usages.
  23.    Copyright (C) 1990, 1993 Free Software Foundation, Inc.
  24.    Francois Pinard <pinard@iro.umontreal.ca>, 1993.
  25.  
  26.    This program is free software; you can redistribute it and/or modify
  27.    it under the terms of the GNU General Public License as published by
  28.    the Free Software Foundation; either version 2, or (at your option)
  29.    any later version.
  30.  
  31.    This program is distributed in the hope that it will be useful, but
  32.    WITHOUT ANY WARRANTY; without even the implied warranty of
  33.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  34.    General Public License for more details.
  35.  
  36.    You should have received a copy of the GNU General Public License
  37.    along with this program; if not, write to the Free Software
  38.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  39. */
  40.  
  41. #include "recode.h"
  42. END_OF_TEXT
  43.  
  44. $split_point = 80;
  45.  
  46. $charset_ordinal = 0;
  47. $discard_charset = 0;
  48. $alias_count = 0;
  49.  
  50. $header = "";
  51.  
  52. while (<>)
  53. {
  54.  
  55.     # Recognize `&charset'.
  56.  
  57.     if (/^&charset (.*)/)
  58.     {
  59.  
  60.     # Before beginning a new charset, process the previous one.
  61.  
  62.     $next_charset = $1;
  63.  
  64.     &complete_charset;
  65.  
  66.     $charset = $next_charset;
  67.  
  68.     # Save the charset name for further declaration.  Announce
  69.      # this charset in the array initialization section, and
  70.      # initialize its processing.
  71.  
  72.     print STDERR $charset, "...";
  73.  
  74.     $header = "\n/* $charset\n";
  75.  
  76.     $hashname = $charset;
  77.     $hashname =~ tr/A-Z/a-z/;
  78.     $hashname =~ s/[^a-z0-9]//g;
  79.     if ($used{$hashname})
  80.     {
  81.         printf STDERR " duplicate of %s...", $used{$hashname};
  82.         $discard_charset = 1;
  83.         next;
  84.     }
  85.     $used{$hashname} = $charset;
  86.  
  87.     $alias_count = 0;
  88.     @table = ();
  89.     $codedim = 0;
  90.     $code = 0;
  91.     $list = "";
  92.     $body = "";
  93.     next;
  94.     }
  95.  
  96.     # Ignore discarded charsets, and character set escapes.
  97.  
  98.     next if $discard_charset;
  99.     next if /^&g[0-4]esc/;
  100.  
  101.     # Recognize other `&' directives.
  102.  
  103.     if (/^&rem (.*)/)
  104.     {
  105.  
  106.     # Save C comments for Texinfo.
  107.  
  108.     $body .= "$1\n";
  109.     next;
  110.     }
  111.     if (/^&alias (.*)/)
  112.     {
  113.  
  114.     # Save synonymous charset names for later declarations.
  115.  
  116.     $alias = $1;
  117.     $header .= "   $alias\n";
  118.  
  119.     $hashname = $alias;
  120.     $hashname =~ tr/A-Z/a-z/;
  121.     $hashname =~ s/[^a-z0-9]//g;
  122.     if ($used{$hashname} && $used{$hashname} ne $charset)
  123.     {
  124.         printf STDERR " duplicate of %s...", $used{$hashname};
  125.         next;
  126.     }
  127.     $used{$hashname} = $charset;
  128.  
  129.     $list .= "," if $list;
  130.     $list .= $alias;
  131.     push (@declare_alias, "$alias,$charset");
  132.     $alias_count++;
  133.     next;
  134.     }
  135.     if (/^&code (.*)/)
  136.     {
  137.  
  138.     # Save the code position.
  139.  
  140.     $code = $1;
  141.     next;
  142.     }
  143.     if (/^&duplicate/)
  144.     {
  145.  
  146.     # Ignore duplicates for now.
  147.  
  148.     next;
  149.     }
  150.     if (/^&([^ ]+)/)
  151.     {
  152.  
  153.     # This is an unrecognized & line, discard the charset.
  154.  
  155.     print STDERR " &$1...";
  156.     $discard_charset = 1;
  157.     next;
  158.     }
  159.  
  160.     # Save all other tokens into the double table.
  161.  
  162.     foreach $token (split)
  163.     {
  164.     if ($token ne "??" && $token ne "__")
  165.     {
  166.         $table[$code] = $token;
  167.         if (length ($token) > $codedim)
  168.         {
  169.         $codedim = length ($token);
  170.         }
  171.     }
  172.     $code++;
  173.     }
  174. }
  175.  
  176. # Process the last accumulated charset.
  177.  
  178. &complete_charset;
  179. &complete_file (2);
  180.  
  181. # Print the documentation.
  182.  
  183. open (TEXI, ">charset.texi");
  184. for $charset (sort keys %body)
  185. {
  186.     print TEXI "\n@item $charset\n";
  187.     @list = sort (split (/,/, $list{$charset}));
  188.     if (@list == 1)
  189.     {
  190.     print TEXI "@code{", $list[0], "} is an alias for this charset.\n";
  191.     }
  192.     elsif (@list > 0)
  193.     {
  194.     $string = "@code{" . join ("}, @code{", @list) . "}";
  195.     $string =~ s/,([^,]+)$/ and\1/;
  196.     print TEXI $string, " are aliases for this charset. \n";
  197.     }
  198.     print TEXI $body{$charset};
  199. }
  200. close TEXI;
  201.  
  202. exit 0;
  203.  
  204. # Routine for printing all accumulated initialization information, and
  205. # then closing the file.  Argument is 1 or 2 depending on output file.
  206.  
  207. sub complete_file
  208. {
  209.  
  210.     # Print the collectable initialization function.
  211.  
  212.     print SRC "\n";
  213.     print SRC "void\n";
  214.     printf SRC "module_charset%d (void)\n", $_[0];
  215.     print SRC "{\n";
  216.     $counter = $_[0] == 1 ? 0 : $split_point;
  217.     while ($string = shift @declare_charset)
  218.     {
  219.     $string =~ s/(.*),/"\1", /;
  220.     print SRC "  declare_double_step (&table_$counter, $string);\n";
  221.     $counter++;
  222.     }
  223.     print SRC "\n";
  224.     while ($string = shift @declare_alias)
  225.     {
  226.     $string =~ s/,/", "/;
  227.     print SRC "  declare_alias (\"$string\");\n";
  228.     }
  229.     print SRC "}\n";
  230.     close SRC;
  231. }
  232.  
  233. # Routine for printing all accumulated information for the charset.
  234. # If the charset should be discarded, adjust tables.
  235.  
  236. sub complete_charset
  237. {
  238.     if ($discard_charset)
  239.     {
  240.     while ($alias_count-- > 0)
  241.     {
  242.         pop @declare_alias;
  243.     }
  244.     $discard_charset = 0;
  245.     print STDERR " DISCARDED\n";
  246.     }
  247.     elsif ($header)
  248.     {
  249.  
  250.     # Save the documentation.
  251.  
  252.     $list{$charset} = $list;
  253.     $body{$charset} = $body;
  254.  
  255.     # Open a new source file if needed.
  256.  
  257.     if ($charset_ordinal == 0)
  258.     {
  259.         print STDERR " writing on charset1.c...";
  260.         open (SRC, ">charset1.c");
  261.         print SRC $overall_header;
  262.     }
  263.     elsif ($charset_ordinal == $split_point)
  264.     {
  265.         &complete_file (1);
  266.         
  267.         print STDERR " writing on charset2.c...";
  268.         open (SRC, ">charset2.c");
  269.         print SRC $overall_header;
  270.     }
  271.  
  272.     # Make introductory C comments.
  273.  
  274.     print SRC $header;
  275.     print SRC "*/\n";
  276.  
  277.     # Make the table for this charset.
  278.  
  279.     print SRC "\n";
  280.     print SRC "static DOUBLE_TABLE table_$charset_ordinal = \n";
  281.     print SRC "  {\n";
  282.     $code = 0;
  283.     for ($index1 = 0; $index1 < 8; $index1++)
  284.     {
  285.         $header = "";
  286.         $flag = 0;
  287.         for ($index2 = 0; $index2 < 32; $index2++)
  288.         {
  289.         $token = $table[$code++];
  290.         $flag = 1 if $token;
  291.         $token .= " " while length ($token) < $codedim;
  292.         $header .= $token;
  293.         }
  294.         if ($flag)
  295.         {
  296.         $header =~ s/\"/\\\"/g;
  297.         print SRC "    \"", $header, "\",\n";
  298.         }
  299.         else
  300.         {
  301.         print SRC "    NULL,\n";
  302.         }
  303.     }
  304.     print SRC "  };\n";
  305.  
  306.     # Register the table.
  307.  
  308.     push (@declare_charset, "$charset,$codedim");
  309.  
  310.     print STDERR " done\n";
  311.     $charset_ordinal++;
  312.     }
  313.     $header = "";
  314. }
  315.