home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 10 / Fresh_Fish_10_2352.bin / new / dev / lang / sgmls / src / genlex.c < prev    next >
C/C++ Source or Header  |  1994-07-10  |  4KB  |  141 lines

  1. /* genlex: Generate lexical tables for non-ASCII charsets. */
  2.  
  3. #include "config.h"
  4. #include "std.h"
  5. #include "tools.h"
  6.  
  7. #define CANON_ASCII_NONSGML 255  /* Canonical non-SGML character in ASCII. */
  8. #define CANON_ASCII_DATACHAR 254 /* Canonical DATACHAR in ASCII. */
  9.  
  10. extern unsigned char charset[];
  11. extern UNCH *lextabs[];
  12. extern UNCH lextran[];
  13.  
  14. static char *lextabnames[] = {
  15.      "lexcnm", "lexcon", "lexgrp", "lexlms", "lexmark", "lexsd", "lextoke",
  16.      "lexmin"
  17. };
  18.  
  19. #define UNUSED -1
  20.  
  21. extern int iso646charset[];
  22. extern int iso646G0charset[];
  23. extern int iso646C0charset[];
  24. extern int iso8859_1charset[];
  25. extern int iso6429C1charset[];
  26.  
  27. static struct {
  28.   char *name;
  29.   int *map;
  30. } charsets[] = {
  31.   { "iso646charset", iso646charset },
  32.   { "iso646G0charset", iso646G0charset },
  33.   { "iso646G0charset", iso646G0charset },
  34.   { "iso8859_1charset", iso8859_1charset },
  35.   { "iso646C0charset", iso646C0charset },
  36.   { "iso6429C1charset", iso6429C1charset },
  37. };
  38.  
  39. static VOID print_tab(s, t)
  40.      char *s;
  41.      UNCH *t;
  42. {
  43.   int i;
  44.   printf("UNCH %s[] = {\n", s);
  45.   for (i = 0; i < 256; i++)
  46.     printf("%2d,%c", t[i], (i + 1) % 16 == 0 ? '\n' : ' ');
  47.   fputs("};\n\n", stdout);
  48. }
  49.  
  50. int main(argc, argv)
  51.      int argc;
  52.      char **argv;
  53. {
  54.   int i;
  55.   UNCH tab[256];
  56.   char special[256];
  57.   /* Shunned character numbers in the reference concrete syntax. */
  58.   static UNCH refshun[] = { 
  59.     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  60.     19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 255
  61.   };
  62.   char shunned[256];
  63.   char *program_name;
  64.  
  65.   program_name = strrchr(argv[0], '/');
  66.   if (program_name)
  67.     program_name++;
  68.   else
  69.     program_name = argv[0];
  70.  
  71.   /* Check that the mapping is 1-1. */
  72.   for (i = 0; i < 256; i++)
  73.     tab[i] = 0;
  74.   for (i = 0; i < 256; i++)
  75.     tab[charset[i]] = 1;
  76.   for (i = 0; i < 256; i++)
  77.     if (!tab[i]) {
  78.       fprintf(stderr, "%s: bad mapping: no character mapped to %d\n",
  79.           program_name, i);
  80.       exit(EXIT_FAILURE);
  81.     }
  82.  
  83.   /* Compute special. */
  84.   for (i = 0; i < 256; i++)
  85.     special[i] = 0;
  86.   for (i = 0; lextabs[i]; i++) {
  87.     int j;
  88.     for (j = 0; j < 256; j++)
  89.     if (lextabs[i][j] != lextabs[i][CANON_ASCII_NONSGML]
  90.     && lextabs[i][j] != lextabs[i][CANON_ASCII_DATACHAR])
  91.       special[charset[j]] = 1;
  92.   }
  93.  
  94.   /* Compute shunned. */
  95.   for (i = 0; i < 256; i++)
  96.     shunned[i] = 0;
  97.   for (i = 0; i < sizeof(refshun); i++)
  98.     shunned[refshun[i]] = 1;
  99.  
  100.   printf("/* This file was automatically generated by %s.  Do not edit. */\n\n",
  101.     program_name);
  102.   fputs("#include \"config.h\"\n#include \"entity.h\"\n#include \"sgmldecl.h\"\n\n",
  103.     stdout);
  104.  
  105.   /* Generate each of the lexical tables. */
  106.   for (i = 0; lextabs[i]; i++) {
  107.     int j;
  108.     for (j = 0; j < 256; j++)
  109.       tab[charset[j]] = lextabs[i][j];
  110.  
  111.     for (j = 0; j < 256; j++)
  112.       if (!special[j]) {
  113.     if (shunned[j]) 
  114.       tab[j] = lextabs[i][CANON_ASCII_NONSGML];
  115.     else
  116.       tab[j] = lextabs[i][CANON_ASCII_DATACHAR];
  117.       }
  118.     print_tab(lextabnames[i], tab);
  119.   }
  120.  
  121.   /* Generate lextran. */
  122.   for (i = 0; i < 256; i++)
  123.     tab[charset[i]] = charset[lextran[i]];
  124.   print_tab("lextran", tab);
  125.  
  126.   /* Generate charsets. */
  127.   for (i = 0; i < sizeof(charsets)/sizeof(charsets[0]); i++) {
  128.     int j;
  129.     int *map = charsets[i].map;
  130.     printf("\nint %s[] = {\n", charsets[i].name);
  131.     for (j = 0; j < 256; j++)
  132.       if (map[j] == UNUSED)
  133.     printf("UNUSED,%c", (j + 1) % 8 == 0 ? '\n' : ' ');
  134.       else
  135.     printf("%3d,%c", charset[map[j]], (j + 1) % 16 == 0 ? '\n' : ' ');
  136.     fputs("};\n", stdout);
  137.   }
  138.  
  139.   exit(EXIT_SUCCESS);
  140. }
  141.