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

  1. # Automatically derive charname.h 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, ">charname.h");
  20.  
  21. print HDR <<END_OF_TEXT;
  22. /* DO NOT MODIFY THIS FILE!  It was generated by "charname.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. # Read the character comments.  Count words in charnames.
  45.  
  46. print STDERR "Reading...";
  47.  
  48. $_ = <>;
  49. while ($_)
  50. {
  51.     chop;
  52.  
  53.     # Look ahead one line and merge it if it should.
  54.  
  55.     $next = <>;
  56.     if ($next =~ /^              ( .*)/)
  57.     {
  58.     $_ .= $1;
  59.     $next = <>;
  60.     }
  61.  
  62.     # Separate fields and save needed information.
  63.  
  64.     if (/([^ ]+) +[0-9a-f]+ +(.*)/)
  65.     {
  66.     $charname{$1} = $2;
  67.     if (length ($2) > $max_length)
  68.     {
  69.         $max_length = length ($2);
  70.     }
  71.     foreach $word (split (/ /, $2))
  72.     {
  73.         $code{$word}++;
  74.     }
  75.     }
  76.     elsif (!/ +e000/)
  77.     {
  78.     print "What about <<", $_, ">>?\n";
  79.     }
  80.  
  81.     # Prepare for next line.
  82.  
  83.     $_ = $next;
  84. }
  85.  
  86. # Establish a mild compression scheme.  Words @word[0] to
  87. # @word[$singles-1] will be represented by a single byte running from
  88. # 1 to $singles.  All remaining words will be represented by two
  89. # bytes, the first one running slowly from $singles+1 to 255, the
  90. # second cycling faster from 1 to 255.
  91.  
  92. print STDERR "Sorting words...";
  93.  
  94. @word = sort descending keys %code;
  95. $count = 0 + @word;
  96. $singles = int ((255 * 255 - $count) / 254);
  97.  
  98. # Transmit a few values for further usage by the C code.
  99.  
  100. print STDERR "and charnames...";
  101.  
  102. @symbol = sort keys %charname;
  103.  
  104. printf HDR "\n#define NUMBER_OF_SINGLES %d\n", $singles;
  105. printf HDR "\n#define MAX_CHARNAME_LENGTH %d\n", $max_length;
  106. printf HDR "\n#define NUMBER_OF_CHARNAMES %d\n", (0 + @symbol);
  107.  
  108. # Establish a mild compression scheme (one or two bytes per word).
  109.  
  110. print STDERR "Writing words...";
  111.  
  112. print HDR "\n";
  113. print HDR "static const char *const word[$count] =\n";
  114. print HDR "  {\n";
  115.  
  116. $char1 = 1;
  117. $char2 = 1;
  118.  
  119. for ($counter = 0; $counter < $singles; $counter++)
  120. {
  121.     $word = $word[$counter];
  122.     $word =~ tr/A-Z/a-z/;
  123.     printf HDR "    %-28s/* %0.3o */\n", "\"$word\",", $char1;
  124.     $code{$word[$counter]} = $char1;
  125.     $char1++;
  126. }
  127.  
  128. for (; $counter < $count; $counter++)
  129. {
  130.     $word = $word[$counter];
  131.     $word =~ tr/A-Z/a-z/;
  132.     printf HDR "    %-28s/* %0.3o %0.3o */\n", "\"$word\",", $char1, $char2;
  133.     $code{$word[$counter]} = 256 * $char1 + $char2;
  134.     if ($char2 == 255)
  135.     {
  136.     $char1++;
  137.     $char2 = 1;
  138.     }
  139.     else
  140.     {
  141.     $char2++;
  142.     }
  143. }
  144. print HDR "  };\n";
  145.  
  146. # Print compressed charnames for all characters.
  147.  
  148. print STDERR "and charnames...";
  149.  
  150. print HDR "\n";
  151. print HDR "struct charname\n";
  152. print HDR "  {\n";
  153. print HDR "    const char *symbol;\n";
  154. print HDR "    const char *crypted;\n";
  155. print HDR "  };\n";
  156.  
  157. print HDR "\n";
  158. print HDR "static const struct charname charname[NUMBER_OF_CHARNAMES] =\n";
  159. print HDR "  {\n";
  160.  
  161. foreach $symbol (@symbol)
  162. {
  163.     $string = $symbol;
  164.     $string =~ s/([\"])/\\\1/g;
  165.     print HDR "    {\"$string\", \"";
  166.     foreach $word (split (' ', $charname{$symbol}))
  167.     {
  168.     $code = $code{$word};
  169.     if ($code < 256)
  170.     {
  171.         printf HDR "\\%0.3o", $code;
  172.     }
  173.     else
  174.     {
  175.         printf HDR "\\%0.3o\\%0.3o", int ($code / 256), $code % 256;
  176.     }
  177.     }
  178.     print HDR "\"},\n";
  179. }
  180.  
  181. print HDR "  };\n";
  182.  
  183. print STDERR "done\n";
  184.  
  185. close HDR;
  186. exit 0;
  187.  
  188. # Comparison routine for descending frequency sort.
  189.  
  190. sub descending
  191. {
  192.     local ($result);
  193.  
  194.     $result = $code{$b} - $code{$a};
  195.     $result == 0 ? $a cmp $b : $result;
  196. }
  197.