1 /* $OpenBSD: comp_hash.c,v 1.1 1999/01/18 19:10:14 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.18 1998/08/22 18:01:18 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 static void _nc_make_hash_table(struct name_table_entry *table, 69 struct name_table_entry **hash_table) 70 { 71 int i; 72 int hashvalue; 73 int collisions = 0; 74 75 for (i = 0; i < CAPTABSIZE; i++) { 76 hashvalue = hash_function(table[i].nte_name); 77 78 if (hash_table[hashvalue] != (struct name_table_entry *) 0) 79 collisions++; 80 81 if (hash_table[hashvalue] != 0) 82 table[i].nte_link = (short)(hash_table[hashvalue] - table); 83 hash_table[hashvalue] = &table[i]; 84 } 85 86 DEBUG(4, ("Hash table complete: %d collisions out of %d entries", collisions, CAPTABSIZE)); 87 } 88 #endif 89 90 91 /* 92 * int hash_function(string) 93 * 94 * Computes the hashing function on the given string. 95 * 96 * The current hash function is the sum of each consectutive pair 97 * of characters, taken as two-byte integers, mod Hashtabsize. 98 * 99 */ 100 101 static 102 int 103 hash_function(const char *string) 104 { 105 long sum = 0; 106 107 DEBUG(9, ("hashing %s", string)); 108 while (*string) { 109 sum += (long)(*string + (*(string + 1) << 8)); 110 string++; 111 } 112 113 DEBUG(9, ("sum is %ld", sum)); 114 return (int)(sum % HASHTABSIZE); 115 } 116 117 118 /* 119 * struct name_table_entry * 120 * find_entry(string) 121 * 122 * Finds the entry for the given string in the hash table if present. 123 * Returns a pointer to the entry in the table or 0 if not found. 124 * 125 */ 126 127 #ifndef MAIN_PROGRAM 128 struct name_table_entry const * 129 _nc_find_entry(const char *string, const struct name_table_entry *const *hash_table) 130 { 131 int hashvalue; 132 struct name_table_entry const *ptr; 133 134 hashvalue = hash_function(string); 135 136 if ((ptr = hash_table[hashvalue]) != 0) { 137 while (strcmp(ptr->nte_name, string) != 0) { 138 if (ptr->nte_link < 0) 139 return 0; 140 ptr = ptr->nte_link + hash_table[HASHTABSIZE]; 141 } 142 } 143 144 return (ptr); 145 } 146 147 /* 148 * struct name_table_entry * 149 * find_type_entry(string, type, table) 150 * 151 * Finds the first entry for the given name with the given type in the 152 * given table if present (as distinct from find_entry, which finds the 153 * the last entry regardless of type). You can use this if you detect 154 * a name clash. It's slower, though. Returns a pointer to the entry 155 * in the table or 0 if not found. 156 */ 157 158 struct name_table_entry const * 159 _nc_find_type_entry(const char *string, 160 int type, 161 const struct name_table_entry *table) 162 { 163 struct name_table_entry const *ptr; 164 165 for (ptr = table; ptr < table + CAPTABSIZE; ptr++) { 166 if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0) 167 return(ptr); 168 } 169 170 return ((struct name_table_entry *)NULL); 171 } 172 #endif 173 174 #ifdef MAIN_PROGRAM 175 /* 176 * This filter reads from standard input a list of tab-delimited columns, 177 * (e.g., from Caps.filtered) computes the hash-value of a specified column and 178 * writes the hashed tables to standard output. 179 * 180 * By compiling the hash table at build time, we're able to make the entire 181 * set of terminfo and termcap tables readonly (and also provide some runtime 182 * performance enhancement). 183 */ 184 185 #if !HAVE_STRDUP 186 static char *strdup (char *s) 187 { 188 char *p; 189 190 p = malloc(strlen(s)+1); 191 if (p) 192 strcpy(p,s); 193 return(p); 194 } 195 #endif /* not HAVE_STRDUP */ 196 197 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */ 198 199 static char **parse_columns(char *buffer) 200 { 201 static char **list; 202 203 int col = 0; 204 205 if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0) 206 return(0); 207 208 if (*buffer != '#') { 209 while (*buffer != '\0') { 210 char *s; 211 for (s = buffer; (*s != '\0') && !isspace(*s); s++) 212 /*EMPTY*/; 213 if (s != buffer) { 214 char mark = *s; 215 *s = '\0'; 216 if ((s - buffer) > 1 217 && (*buffer == '"') 218 && (s[-1] == '"')) { /* strip the quotes */ 219 buffer++; 220 s[-1] = '\0'; 221 } 222 list[col] = buffer; 223 col++; 224 if (mark == '\0') 225 break; 226 while (*++s && isspace(*s)) 227 /*EMPTY*/; 228 buffer = s; 229 } else 230 break; 231 } 232 } 233 return col ? list : 0; 234 } 235 236 int main(int argc, char **argv) 237 { 238 struct name_table_entry *name_table = typeCalloc(struct name_table_entry, CAPTABSIZE); 239 struct name_table_entry **hash_table = typeCalloc(struct name_table_entry *, HASHTABSIZE); 240 const char *root_name = ""; 241 int column = 0; 242 int n; 243 char buffer[BUFSIZ]; 244 245 static const char * typenames[] = { "BOOLEAN", "NUMBER", "STRING" }; 246 247 short BoolCount = 0; 248 short NumCount = 0; 249 short StrCount = 0; 250 251 /* The first argument is the column-number (starting with 0). 252 * The second is the root name of the tables to generate. 253 */ 254 if (argc <= 2 255 || (column = atoi(argv[1])) <= 0 256 || (column >= MAX_COLUMNS) 257 || *(root_name = argv[2]) == 0) { 258 fprintf(stderr, "usage: make_hash column root_name\n"); 259 exit(EXIT_FAILURE); 260 } 261 262 /* 263 * Read the table into our arrays. 264 */ 265 for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin); ) { 266 char **list, *nlp = strchr(buffer, '\n'); 267 if (nlp) 268 *nlp = '\0'; 269 list = parse_columns(buffer); 270 if (list == 0) /* blank or comment */ 271 continue; 272 name_table[n].nte_link = -1; /* end-of-hash */ 273 name_table[n].nte_name = strdup(list[column]); 274 if (!strcmp(list[2], "bool")) { 275 name_table[n].nte_type = BOOLEAN; 276 name_table[n].nte_index = BoolCount++; 277 } else if (!strcmp(list[2], "num")) { 278 name_table[n].nte_type = NUMBER; 279 name_table[n].nte_index = NumCount++; 280 } else if (!strcmp(list[2], "str")) { 281 name_table[n].nte_type = STRING; 282 name_table[n].nte_index = StrCount++; 283 } else { 284 fprintf(stderr, "Unknown type: %s\n", list[2]); 285 exit(EXIT_FAILURE); 286 } 287 n++; 288 } 289 _nc_make_hash_table(name_table, hash_table); 290 291 /* 292 * Write the compiled tables to standard output 293 */ 294 printf("static struct name_table_entry const _nc_%s_table[] =\n", 295 root_name); 296 printf("{\n"); 297 for (n = 0; n < CAPTABSIZE; n++) { 298 sprintf(buffer, "\"%s\"", 299 name_table[n].nte_name); 300 printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n", 301 buffer, 302 typenames[name_table[n].nte_type], 303 name_table[n].nte_index, 304 name_table[n].nte_link, 305 n < CAPTABSIZE - 1 ? ',' : ' '); 306 } 307 printf("};\n\n"); 308 309 printf("const struct name_table_entry * const _nc_%s_hash_table[%d] =\n", 310 root_name, 311 HASHTABSIZE+1); 312 printf("{\n"); 313 for (n = 0; n < HASHTABSIZE; n++) { 314 if (hash_table[n] != 0) { 315 sprintf(buffer, "_nc_%s_table + %3ld", 316 root_name, 317 (long) (hash_table[n] - name_table)); 318 } else { 319 strcpy(buffer, "0"); 320 } 321 printf("\t%s,\n", buffer); 322 } 323 printf("\t_nc_%s_table\t/* base-of-table */\n", root_name); 324 printf("};\n\n"); 325 326 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n", 327 BoolCount, NumCount, StrCount); 328 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n"); 329 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n"); 330 printf("#endif\n\n"); 331 332 return EXIT_SUCCESS; 333 } 334 #endif 335