Back to home page

LXR

 
 

    


0001 /*
0002  * conmakehash.c
0003  *
0004  * Create arrays for initializing the kernel folded tables (using a hash
0005  * table turned out to be to limiting...)  Unfortunately we can't simply
0006  * preinitialize the tables at compile time since kfree() cannot accept
0007  * memory not allocated by kmalloc(), and doing our own memory management
0008  * just for this seems like massive overkill.
0009  *
0010  * Copyright (C) 1995-1997 H. Peter Anvin
0011  *
0012  * This program is a part of the Linux kernel, and may be freely
0013  * copied under the terms of the GNU General Public License (GPL),
0014  * version 2, or at your option any later version.
0015  */
0016 
0017 #include <stdio.h>
0018 #include <stdlib.h>
0019 #include <sysexits.h>
0020 #include <string.h>
0021 #include <ctype.h>
0022 
0023 #define MAX_FONTLEN 256
0024 
0025 typedef unsigned short unicode;
0026 
0027 static void usage(char *argv0)
0028 {
0029   fprintf(stderr, "Usage: \n"
0030          "        %s chartable [hashsize] [hashstep] [maxhashlevel]\n", argv0);
0031   exit(EX_USAGE);
0032 }
0033 
0034 static int getunicode(char **p0)
0035 {
0036   char *p = *p0;
0037 
0038   while (*p == ' ' || *p == '\t')
0039     p++;
0040   if (*p != 'U' || p[1] != '+' ||
0041       !isxdigit(p[2]) || !isxdigit(p[3]) || !isxdigit(p[4]) ||
0042       !isxdigit(p[5]) || isxdigit(p[6]))
0043     return -1;
0044   *p0 = p+6;
0045   return strtol(p+2,0,16);
0046 }
0047 
0048 unicode unitable[MAX_FONTLEN][255];
0049                 /* Massive overkill, but who cares? */
0050 int unicount[MAX_FONTLEN];
0051 
0052 static void addpair(int fp, int un)
0053 {
0054   int i;
0055 
0056   if ( un <= 0xfffe )
0057     {
0058       /* Check it isn't a duplicate */
0059 
0060       for ( i = 0 ; i < unicount[fp] ; i++ )
0061     if ( unitable[fp][i] == un )
0062       return;
0063 
0064       /* Add to list */
0065 
0066       if ( unicount[fp] > 254 )
0067     {
0068       fprintf(stderr, "ERROR: Only 255 unicodes/glyph permitted!\n");
0069       exit(EX_DATAERR);
0070     }
0071 
0072       unitable[fp][unicount[fp]] = un;
0073       unicount[fp]++;
0074     }
0075 
0076   /* otherwise: ignore */
0077 }
0078 
0079 int main(int argc, char *argv[])
0080 {
0081   FILE *ctbl;
0082   char *tblname;
0083   char buffer[65536];
0084   int fontlen;
0085   int i, nuni, nent;
0086   int fp0, fp1, un0, un1;
0087   char *p, *p1;
0088 
0089   if ( argc < 2 || argc > 5 )
0090     usage(argv[0]);
0091 
0092   if ( !strcmp(argv[1],"-") )
0093     {
0094       ctbl = stdin;
0095       tblname = "stdin";
0096     }
0097   else
0098     {
0099       ctbl = fopen(tblname = argv[1], "r");
0100       if ( !ctbl )
0101     {
0102       perror(tblname);
0103       exit(EX_NOINPUT);
0104     }
0105     }
0106 
0107   /* For now we assume the default font is always 256 characters. */
0108   fontlen = 256;
0109 
0110   /* Initialize table */
0111 
0112   for ( i = 0 ; i < fontlen ; i++ )
0113     unicount[i] = 0;
0114 
0115   /* Now we come to the tricky part.  Parse the input table. */
0116 
0117   while ( fgets(buffer, sizeof(buffer), ctbl) != NULL )
0118     {
0119       if ( (p = strchr(buffer, '\n')) != NULL )
0120     *p = '\0';
0121       else
0122     fprintf(stderr, "%s: Warning: line too long\n", tblname);
0123 
0124       p = buffer;
0125 
0126 /*
0127  * Syntax accepted:
0128  *  <fontpos>   <unicode> <unicode> ...
0129  *  <range>     idem
0130  *  <range>     <unicode range>
0131  *
0132  * where <range> ::= <fontpos>-<fontpos>
0133  * and <unicode> ::= U+<h><h><h><h>
0134  * and <h> ::= <hexadecimal digit>
0135  */
0136 
0137       while (*p == ' ' || *p == '\t')
0138     p++;
0139       if (!*p || *p == '#')
0140     continue;   /* skip comment or blank line */
0141 
0142       fp0 = strtol(p, &p1, 0);
0143       if (p1 == p)
0144     {
0145       fprintf(stderr, "Bad input line: %s\n", buffer);
0146       exit(EX_DATAERR);
0147         }
0148       p = p1;
0149 
0150       while (*p == ' ' || *p == '\t')
0151     p++;
0152       if (*p == '-')
0153     {
0154       p++;
0155       fp1 = strtol(p, &p1, 0);
0156       if (p1 == p)
0157         {
0158           fprintf(stderr, "Bad input line: %s\n", buffer);
0159           exit(EX_DATAERR);
0160         }
0161       p = p1;
0162         }
0163       else
0164     fp1 = 0;
0165 
0166       if ( fp0 < 0 || fp0 >= fontlen )
0167     {
0168         fprintf(stderr,
0169             "%s: Glyph number (0x%x) larger than font length\n",
0170             tblname, fp0);
0171         exit(EX_DATAERR);
0172     }
0173       if ( fp1 && (fp1 < fp0 || fp1 >= fontlen) )
0174     {
0175         fprintf(stderr,
0176             "%s: Bad end of range (0x%x)\n",
0177             tblname, fp1);
0178         exit(EX_DATAERR);
0179     }
0180 
0181       if (fp1)
0182     {
0183       /* we have a range; expect the word "idem" or a Unicode range of the
0184          same length */
0185       while (*p == ' ' || *p == '\t')
0186         p++;
0187       if (!strncmp(p, "idem", 4))
0188         {
0189           for (i=fp0; i<=fp1; i++)
0190         addpair(i,i);
0191           p += 4;
0192         }
0193       else
0194         {
0195           un0 = getunicode(&p);
0196           while (*p == ' ' || *p == '\t')
0197         p++;
0198           if (*p != '-')
0199         {
0200           fprintf(stderr,
0201 "%s: Corresponding to a range of font positions, there should be a Unicode range\n",
0202               tblname);
0203           exit(EX_DATAERR);
0204             }
0205           p++;
0206           un1 = getunicode(&p);
0207           if (un0 < 0 || un1 < 0)
0208         {
0209           fprintf(stderr,
0210 "%s: Bad Unicode range corresponding to font position range 0x%x-0x%x\n",
0211               tblname, fp0, fp1);
0212           exit(EX_DATAERR);
0213             }
0214           if (un1 - un0 != fp1 - fp0)
0215         {
0216           fprintf(stderr,
0217 "%s: Unicode range U+%x-U+%x not of the same length as font position range 0x%x-0x%x\n",
0218               tblname, un0, un1, fp0, fp1);
0219           exit(EX_DATAERR);
0220             }
0221           for(i=fp0; i<=fp1; i++)
0222         addpair(i,un0-fp0+i);
0223         }
0224         }
0225       else
0226     {
0227         /* no range; expect a list of unicode values for a single font position */
0228 
0229         while ( (un0 = getunicode(&p)) >= 0 )
0230           addpair(fp0, un0);
0231     }
0232       while (*p == ' ' || *p == '\t')
0233     p++;
0234       if (*p && *p != '#')
0235     fprintf(stderr, "%s: trailing junk (%s) ignored\n", tblname, p);
0236     }
0237 
0238   /* Okay, we hit EOF, now output hash table */
0239 
0240   fclose(ctbl);
0241 
0242 
0243   /* Compute total size of Unicode list */
0244   nuni = 0;
0245   for ( i = 0 ; i < fontlen ; i++ )
0246     nuni += unicount[i];
0247 
0248   printf("\
0249 /*\n\
0250  * Do not edit this file; it was automatically generated by\n\
0251  *\n\
0252  * conmakehash %s > [this file]\n\
0253  *\n\
0254  */\n\
0255 \n\
0256 #include <linux/types.h>\n\
0257 \n\
0258 u8 dfont_unicount[%d] = \n\
0259 {\n\t", argv[1], fontlen);
0260 
0261   for ( i = 0 ; i < fontlen ; i++ )
0262     {
0263       printf("%3d", unicount[i]);
0264       if ( i == fontlen-1 )
0265         printf("\n};\n");
0266       else if ( i % 8 == 7 )
0267         printf(",\n\t");
0268       else
0269         printf(", ");
0270     }
0271 
0272   printf("\nu16 dfont_unitable[%d] = \n{\n\t", nuni);
0273 
0274   fp0 = 0;
0275   nent = 0;
0276   for ( i = 0 ; i < nuni ; i++ )
0277     {
0278       while ( nent >= unicount[fp0] )
0279     {
0280       fp0++;
0281       nent = 0;
0282     }
0283       printf("0x%04x", unitable[fp0][nent++]);
0284       if ( i == nuni-1 )
0285          printf("\n};\n");
0286        else if ( i % 8 == 7 )
0287          printf(",\n\t");
0288        else
0289          printf(", ");
0290     }
0291 
0292   exit(EX_OK);
0293 }