home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GRECODE / CHARSET.PL < prev    next >
Text File  |  1993-12-19  |  6KB  |  289 lines

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