Back to index

tetex-bin  3.0
comp_hash.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998,2001,2003 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /*
00035  *     comp_hash.c --- Routines to deal with the hashtable of capability
00036  *                   names.
00037  *
00038  */
00039 
00040 #include <curses.priv.h>
00041 
00042 #include <tic.h>
00043 #include <hashsize.h>
00044 
00045 #ifdef MAIN_PROGRAM
00046 #include <ctype.h>
00047 #undef  DEBUG
00048 #define DEBUG(level, params)       /*nothing */
00049 #endif
00050 
00051 MODULE_ID("$Id: comp_hash.c,v 1.26 2003/11/08 21:58:36 tom Exp $")
00052 
00053 static int hash_function(const char *);
00054 
00055 /*
00056  *     _nc_make_hash_table()
00057  *
00058  *     Takes the entries in table[] and hashes them into hash_table[]
00059  *     by name.  There are CAPTABSIZE entries in table[] and HASHTABSIZE
00060  *     slots in hash_table[].
00061  *
00062  */
00063 
00064 #ifdef MAIN_PROGRAM
00065 
00066 #undef MODULE_ID
00067 #define MODULE_ID(id)              /*nothing */
00068 #include <tinfo/doalloc.c>
00069 
00070 static void
00071 _nc_make_hash_table(struct name_table_entry *table,
00072                   struct name_table_entry **hash_table)
00073 {
00074     int i;
00075     int hashvalue;
00076     int collisions = 0;
00077 
00078     for (i = 0; i < CAPTABSIZE; i++) {
00079        hashvalue = hash_function(table[i].nte_name);
00080 
00081        if (hash_table[hashvalue] != (struct name_table_entry *) 0)
00082            collisions++;
00083 
00084        if (hash_table[hashvalue] != 0)
00085            table[i].nte_link = (short) (hash_table[hashvalue] - table);
00086        hash_table[hashvalue] = &table[i];
00087     }
00088 
00089     DEBUG(4, ("Hash table complete: %d collisions out of %d entries",
00090              collisions, CAPTABSIZE));
00091 }
00092 #endif
00093 
00094 /*
00095  *     int hash_function(string)
00096  *
00097  *     Computes the hashing function on the given string.
00098  *
00099  *     The current hash function is the sum of each consectutive pair
00100  *     of characters, taken as two-byte integers, mod HASHTABSIZE.
00101  *
00102  */
00103 
00104 static
00105 int
00106 hash_function(const char *string)
00107 {
00108     long sum = 0;
00109 
00110     DEBUG(9, ("hashing %s", string));
00111     while (*string) {
00112        sum += (long) (*string + (*(string + 1) << 8));
00113        string++;
00114     }
00115 
00116     DEBUG(9, ("sum is %ld", sum));
00117     return (int) (sum % HASHTABSIZE);
00118 }
00119 
00120 /*
00121  *     struct name_table_entry *
00122  *     find_entry(string)
00123  *
00124  *     Finds the entry for the given string in the hash table if present.
00125  *     Returns a pointer to the entry in the table or 0 if not found.
00126  *
00127  */
00128 
00129 #ifndef MAIN_PROGRAM
00130 NCURSES_EXPORT(struct name_table_entry const *)
00131 _nc_find_entry
00132 (const char *string, const struct name_table_entry *const *hash_table)
00133 {
00134     int hashvalue;
00135     struct name_table_entry const *ptr;
00136 
00137     hashvalue = hash_function(string);
00138 
00139     if ((ptr = hash_table[hashvalue]) != 0) {
00140        while (strcmp(ptr->nte_name, string) != 0) {
00141            if (ptr->nte_link < 0)
00142               return 0;
00143            ptr = ptr->nte_link + hash_table[HASHTABSIZE];
00144        }
00145     }
00146 
00147     return (ptr);
00148 }
00149 
00150 /*
00151  *     struct name_table_entry *
00152  *     find_type_entry(string, type, table)
00153  *
00154  *     Finds the first entry for the given name with the given type in the
00155  *     given table if present (as distinct from find_entry, which finds the
00156  *     the last entry regardless of type).  You can use this if you detect
00157  *     a name clash.  It's slower, though.  Returns a pointer to the entry
00158  *     in the table or 0 if not found.
00159  */
00160 
00161 NCURSES_EXPORT(struct name_table_entry const *)
00162 _nc_find_type_entry
00163 (const char *string,
00164  int type,
00165  const struct name_table_entry *table)
00166 {
00167     struct name_table_entry const *ptr;
00168 
00169     for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
00170        if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0)
00171            return (ptr);
00172     }
00173 
00174     return ((struct name_table_entry *) NULL);
00175 }
00176 #endif
00177 
00178 #ifdef MAIN_PROGRAM
00179 /*
00180  * This filter reads from standard input a list of tab-delimited columns,
00181  * (e.g., from Caps.filtered) computes the hash-value of a specified column and
00182  * writes the hashed tables to standard output.
00183  *
00184  * By compiling the hash table at build time, we're able to make the entire
00185  * set of terminfo and termcap tables readonly (and also provide some runtime
00186  * performance enhancement).
00187  */
00188 
00189 #define MAX_COLUMNS BUFSIZ  /* this _has_ to be worst-case */
00190 
00191 static char **
00192 parse_columns(char *buffer)
00193 {
00194     static char **list;
00195 
00196     int col = 0;
00197 
00198     if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
00199          return (0);
00200 
00201     if (*buffer != '#') {
00202        while (*buffer != '\0') {
00203            char *s;
00204            for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
00205               /*EMPTY */ ;
00206            if (s != buffer) {
00207               char mark = *s;
00208               *s = '\0';
00209               if ((s - buffer) > 1
00210                   && (*buffer == '"')
00211                   && (s[-1] == '"')) {    /* strip the quotes */
00212                   buffer++;
00213                   s[-1] = '\0';
00214               }
00215               list[col] = buffer;
00216               col++;
00217               if (mark == '\0')
00218                   break;
00219               while (*++s && isspace(UChar(*s)))
00220                   /*EMPTY */ ;
00221               buffer = s;
00222            } else
00223               break;
00224        }
00225     }
00226     return col ? list : 0;
00227 }
00228 
00229 int
00230 main(int argc, char **argv)
00231 {
00232     struct name_table_entry *name_table = typeCalloc(struct
00233                                                name_table_entry, CAPTABSIZE);
00234     struct name_table_entry **hash_table = typeCalloc(struct name_table_entry
00235                                                 *, HASHTABSIZE);
00236     const char *root_name = "";
00237     int column = 0;
00238     int n;
00239     char buffer[BUFSIZ];
00240 
00241     static const char *typenames[] =
00242     {"BOOLEAN", "NUMBER", "STRING"};
00243 
00244     short BoolCount = 0;
00245     short NumCount = 0;
00246     short StrCount = 0;
00247 
00248     /* The first argument is the column-number (starting with 0).
00249      * The second is the root name of the tables to generate.
00250      */
00251     if (argc <= 2
00252        || (column = atoi(argv[1])) <= 0
00253        || (column >= MAX_COLUMNS)
00254        || *(root_name = argv[2]) == 0) {
00255        fprintf(stderr, "usage: make_hash column root_name\n");
00256        exit(EXIT_FAILURE);
00257     }
00258 
00259     /*
00260      * Read the table into our arrays.
00261      */
00262     for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
00263        char **list, *nlp = strchr(buffer, '\n');
00264        if (nlp)
00265            *nlp = '\0';
00266        list = parse_columns(buffer);
00267        if (list == 0)              /* blank or comment */
00268            continue;
00269        name_table[n].nte_link = -1;       /* end-of-hash */
00270        name_table[n].nte_name = strdup(list[column]);
00271        if (!strcmp(list[2], "bool")) {
00272            name_table[n].nte_type = BOOLEAN;
00273            name_table[n].nte_index = BoolCount++;
00274        } else if (!strcmp(list[2], "num")) {
00275            name_table[n].nte_type = NUMBER;
00276            name_table[n].nte_index = NumCount++;
00277        } else if (!strcmp(list[2], "str")) {
00278            name_table[n].nte_type = STRING;
00279            name_table[n].nte_index = StrCount++;
00280        } else {
00281            fprintf(stderr, "Unknown type: %s\n", list[2]);
00282            exit(EXIT_FAILURE);
00283        }
00284        n++;
00285     }
00286     _nc_make_hash_table(name_table, hash_table);
00287 
00288     /*
00289      * Write the compiled tables to standard output
00290      */
00291     printf("static struct name_table_entry const _nc_%s_table[] =\n",
00292           root_name);
00293     printf("{\n");
00294     for (n = 0; n < CAPTABSIZE; n++) {
00295        sprintf(buffer, "\"%s\"",
00296               name_table[n].nte_name);
00297        printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
00298               buffer,
00299               typenames[name_table[n].nte_type],
00300               name_table[n].nte_index,
00301               name_table[n].nte_link,
00302               n < CAPTABSIZE - 1 ? ',' : ' ');
00303     }
00304     printf("};\n\n");
00305 
00306     printf("const struct name_table_entry * const _nc_%s_hash_table[%d] =\n",
00307           root_name,
00308           HASHTABSIZE + 1);
00309     printf("{\n");
00310     for (n = 0; n < HASHTABSIZE; n++) {
00311        if (hash_table[n] != 0) {
00312            sprintf(buffer, "_nc_%s_table + %3ld",
00313                   root_name,
00314                   (long) (hash_table[n] - name_table));
00315        } else {
00316            strcpy(buffer, "0");
00317        }
00318        printf("\t%s,\n", buffer);
00319     }
00320     printf("\t_nc_%s_table\t/* base-of-table */\n", root_name);
00321     printf("};\n\n");
00322 
00323     printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
00324           BoolCount, NumCount, StrCount);
00325     printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
00326     printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
00327     printf("#endif\n\n");
00328 
00329     return EXIT_SUCCESS;
00330 }
00331 #endif