1 /* $OpenBSD: comp_hash.c,v 1.3 1999/03/15 19:12:22 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 37 /* 38 * comp_hash.c --- Routines to deal with the hashtable of capability 39 * names. 40 * 41 */ 42 43 #include <curses.priv.h> 44 45 #include <tic.h> 46 #include <hashsize.h> 47 48 #ifdef MAIN_PROGRAM 49 #include <ctype.h> 50 #undef DEBUG 51 #define DEBUG(level, params) /*nothing*/ 52 #endif 53 54 MODULE_ID("$From: comp_hash.c,v 1.20 1999/03/14 12:23:26 tom Exp $") 55 56 static int hash_function(const char *); 57 58 /* 59 * _nc_make_hash_table() 60 * 61 * Takes the entries in table[] and hashes them into hash_table[] 62 * by name. There are CAPTABSIZE entries in table[] and HASHTABSIZE 63 * slots in hash_table[]. 64 * 65 */ 66 67 #ifdef MAIN_PROGRAM 68 69 #undef USE_RCS_IDS 70 #include <tinfo/doalloc.c> 71 72 static void _nc_make_hash_table(struct name_table_entry *table, 73 struct name_table_entry **hash_table) 74 { 75 int i; 76 int hashvalue; 77 int collisions = 0; 78 79 for (i = 0; i < CAPTABSIZE; i++) { 80 hashvalue = hash_function(table[i].nte_name); 81 82 if (hash_table[hashvalue] != (struct name_table_entry *) 0) 83 collisions++; 84 85 if (hash_table[hashvalue] != 0) 86 table[i].nte_link = (short)(hash_table[hashvalue] - table); 87 hash_table[hashvalue] = &table[i]; 88 } 89 90 DEBUG(4, ("Hash table complete: %d collisions out of %d entries", collisions, CAPTABSIZE)); 91 } 92 #endif 93 94 95 /* 96 * int hash_function(string) 97 * 98 * Computes the hashing function on the given string. 99 * 100 * The current hash function is the sum of each consectutive pair 101 * of characters, taken as two-byte integers, mod Hashtabsize. 102 * 103 */ 104 105 static 106 int 107 hash_function(const char *string) 108 { 109 long sum = 0; 110 111 DEBUG(9, ("hashing %s", string)); 112 while (*string) { 113 sum += (long)(*string + (*(string + 1) << 8)); 114 string++; 115 } 116 117 DEBUG(9, ("sum is %ld", sum)); 118 return (int)(sum % HASHTABSIZE); 119 } 120 121 122 /* 123 * struct name_table_entry * 124 * find_entry(string) 125 * 126 * Finds the entry for the given string in the hash table if present. 127 * Returns a pointer to the entry in the table or 0 if not found. 128 * 129 */ 130 131 #ifndef MAIN_PROGRAM 132 struct name_table_entry const * 133 _nc_find_entry(const char *string, const struct name_table_entry *const *hash_table) 134 { 135 int hashvalue; 136 struct name_table_entry const *ptr; 137 138 hashvalue = hash_function(string); 139 140 if ((ptr = hash_table[hashvalue]) != 0) { 141 while (strcmp(ptr->nte_name, string) != 0) { 142 if (ptr->nte_link < 0) 143 return 0; 144 ptr = ptr->nte_link + hash_table[HASHTABSIZE]; 145 } 146 } 147 148 return (ptr); 149 } 150 151 /* 152 * struct name_table_entry * 153 * find_type_entry(string, type, table) 154 * 155 * Finds the first entry for the given name with the given type in the 156 * given table if present (as distinct from find_entry, which finds the 157 * the last entry regardless of type). You can use this if you detect 158 * a name clash. It's slower, though. Returns a pointer to the entry 159 * in the table or 0 if not found. 160 */ 161 162 struct name_table_entry const * 163 _nc_find_type_entry(const char *string, 164 int type, 165 const struct name_table_entry *table) 166 { 167 struct name_table_entry const *ptr; 168 169 for (ptr = table; ptr < table + CAPTABSIZE; ptr++) { 170 if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0) 171 return(ptr); 172 } 173 174 return ((struct name_table_entry *)NULL); 175 } 176 #endif 177 178 #ifdef MAIN_PROGRAM 179 /* 180 * This filter reads from standard input a list of tab-delimited columns, 181 * (e.g., from Caps.filtered) computes the hash-value of a specified column and 182 * writes the hashed tables to standard output. 183 * 184 * By compiling the hash table at build time, we're able to make the entire 185 * set of terminfo and termcap tables readonly (and also provide some runtime 186 * performance enhancement). 187 */ 188 189 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */ 190 191 static char **parse_columns(char *buffer) 192 { 193 static char **list; 194 195 int col = 0; 196 197 if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0) 198 return(0); 199 200 if (*buffer != '#') { 201 while (*buffer != '\0') { 202 char *s; 203 for (s = buffer; (*s != '\0') && !isspace(*s); s++) 204 /*EMPTY*/; 205 if (s != buffer) { 206 char mark = *s; 207 *s = '\0'; 208 if ((s - buffer) > 1 209 && (*buffer == '"') 210 && (s[-1] == '"')) { /* strip the quotes */ 211 buffer++; 212 s[-1] = '\0'; 213 } 214 list[col] = buffer; 215 col++; 216 if (mark == '\0') 217 break; 218 while (*++s && isspace(*s)) 219 /*EMPTY*/; 220 buffer = s; 221 } else 222 break; 223 } 224 } 225 return col ? list : 0; 226 } 227 228 int main(int argc, char **argv) 229 { 230 struct name_table_entry *name_table = typeCalloc(struct name_table_entry, CAPTABSIZE); 231 struct name_table_entry **hash_table = typeCalloc(struct name_table_entry *, HASHTABSIZE); 232 const char *root_name = ""; 233 int column = 0; 234 int n; 235 char buffer[BUFSIZ]; 236 237 static const char * typenames[] = { "BOOLEAN", "NUMBER", "STRING" }; 238 239 short BoolCount = 0; 240 short NumCount = 0; 241 short StrCount = 0; 242 243 /* The first argument is the column-number (starting with 0). 244 * The second is the root name of the tables to generate. 245 */ 246 if (argc <= 2 247 || (column = atoi(argv[1])) <= 0 248 || (column >= MAX_COLUMNS) 249 || *(root_name = argv[2]) == 0) { 250 fprintf(stderr, "usage: make_hash column root_name\n"); 251 exit(EXIT_FAILURE); 252 } 253 254 /* 255 * Read the table into our arrays. 256 */ 257 for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin); ) { 258 char **list, *nlp = strchr(buffer, '\n'); 259 if (nlp) 260 *nlp = '\0'; 261 list = parse_columns(buffer); 262 if (list == 0) /* blank or comment */ 263 continue; 264 name_table[n].nte_link = -1; /* end-of-hash */ 265 name_table[n].nte_name = strdup(list[column]); 266 if (!strcmp(list[2], "bool")) { 267 name_table[n].nte_type = BOOLEAN; 268 name_table[n].nte_index = BoolCount++; 269 } else if (!strcmp(list[2], "num")) { 270 name_table[n].nte_type = NUMBER; 271 name_table[n].nte_index = NumCount++; 272 } else if (!strcmp(list[2], "str")) { 273 name_table[n].nte_type = STRING; 274 name_table[n].nte_index = StrCount++; 275 } else { 276 fprintf(stderr, "Unknown type: %s\n", list[2]); 277 exit(EXIT_FAILURE); 278 } 279 n++; 280 } 281 _nc_make_hash_table(name_table, hash_table); 282 283 /* 284 * Write the compiled tables to standard output 285 */ 286 printf("static struct name_table_entry const _nc_%s_table[] =\n", 287 root_name); 288 printf("{\n"); 289 for (n = 0; n < CAPTABSIZE; n++) { 290 sprintf(buffer, "\"%s\"", 291 name_table[n].nte_name); 292 printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n", 293 buffer, 294 typenames[name_table[n].nte_type], 295 name_table[n].nte_index, 296 name_table[n].nte_link, 297 n < CAPTABSIZE - 1 ? ',' : ' '); 298 } 299 printf("};\n\n"); 300 301 printf("const struct name_table_entry * const _nc_%s_hash_table[%d] =\n", 302 root_name, 303 HASHTABSIZE+1); 304 printf("{\n"); 305 for (n = 0; n < HASHTABSIZE; n++) { 306 if (hash_table[n] != 0) { 307 sprintf(buffer, "_nc_%s_table + %3ld", 308 root_name, 309 (long) (hash_table[n] - name_table)); 310 } else { 311 strcpy(buffer, "0"); 312 } 313 printf("\t%s,\n", buffer); 314 } 315 printf("\t_nc_%s_table\t/* base-of-table */\n", root_name); 316 printf("};\n\n"); 317 318 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n", 319 BoolCount, NumCount, StrCount); 320 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n"); 321 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n"); 322 printf("#endif\n\n"); 323 324 return EXIT_SUCCESS; 325 } 326 #endif 327