1 |
|
|
/* $OpenBSD: comp_hash.c,v 1.8 2010/01/12 23:22:06 nicm Exp $ */ |
2 |
|
|
|
3 |
|
|
/**************************************************************************** |
4 |
|
|
* Copyright (c) 1998-2007,2008 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 |
|
|
* and: Thomas E. Dickey 1996-on * |
35 |
|
|
****************************************************************************/ |
36 |
|
|
|
37 |
|
|
/* |
38 |
|
|
* comp_hash.c --- Routines to deal with the hashtable of capability |
39 |
|
|
* names. |
40 |
|
|
* |
41 |
|
|
*/ |
42 |
|
|
|
43 |
|
|
#define USE_TERMLIB 1 |
44 |
|
|
#include <curses.priv.h> |
45 |
|
|
|
46 |
|
|
#include <tic.h> |
47 |
|
|
#include <hashsize.h> |
48 |
|
|
|
49 |
|
|
#ifdef MAIN_PROGRAM |
50 |
|
|
#include <ctype.h> |
51 |
|
|
#undef DEBUG |
52 |
|
|
#define DEBUG(level, params) /*nothing */ |
53 |
|
|
#endif |
54 |
|
|
|
55 |
|
|
MODULE_ID("$Id: comp_hash.c,v 1.8 2010/01/12 23:22:06 nicm Exp $") |
56 |
|
|
|
57 |
|
|
static int hash_function(const char *); |
58 |
|
|
|
59 |
|
|
/* |
60 |
|
|
* _nc_make_hash_table() |
61 |
|
|
* |
62 |
|
|
* Takes the entries in table[] and hashes them into hash_table[] |
63 |
|
|
* by name. There are CAPTABSIZE entries in table[] and HASHTABSIZE |
64 |
|
|
* slots in hash_table[]. |
65 |
|
|
* |
66 |
|
|
*/ |
67 |
|
|
|
68 |
|
|
#ifdef MAIN_PROGRAM |
69 |
|
|
|
70 |
|
|
#undef MODULE_ID |
71 |
|
|
#define MODULE_ID(id) /*nothing */ |
72 |
|
|
#include <tinfo/doalloc.c> |
73 |
|
|
|
74 |
|
|
static void |
75 |
|
|
_nc_make_hash_table(struct name_table_entry *table, |
76 |
|
|
short *hash_table) |
77 |
|
|
{ |
78 |
|
|
short i; |
79 |
|
|
int hashvalue; |
80 |
|
|
int collisions = 0; |
81 |
|
|
|
82 |
✓✓ |
3982 |
for (i = 0; i < HASHTABSIZE; i++) { |
83 |
|
1988 |
hash_table[i] = -1; |
84 |
|
|
} |
85 |
✓✓ |
1992 |
for (i = 0; i < CAPTABSIZE; i++) { |
86 |
|
994 |
hashvalue = hash_function(table[i].nte_name); |
87 |
|
|
|
88 |
✓✓ |
994 |
if (hash_table[hashvalue] >= 0) |
89 |
|
250 |
collisions++; |
90 |
|
|
|
91 |
✓✗ |
994 |
if (hash_table[hashvalue] != 0) |
92 |
|
994 |
table[i].nte_link = hash_table[hashvalue]; |
93 |
|
994 |
hash_table[hashvalue] = i; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
DEBUG(4, ("Hash table complete: %d collisions out of %d entries", |
97 |
|
|
collisions, CAPTABSIZE)); |
98 |
|
2 |
} |
99 |
|
|
#endif |
100 |
|
|
|
101 |
|
|
/* |
102 |
|
|
* int hash_function(string) |
103 |
|
|
* |
104 |
|
|
* Computes the hashing function on the given string. |
105 |
|
|
* |
106 |
|
|
* The current hash function is the sum of each consectutive pair |
107 |
|
|
* of characters, taken as two-byte integers, mod HASHTABSIZE. |
108 |
|
|
* |
109 |
|
|
*/ |
110 |
|
|
|
111 |
|
|
static int |
112 |
|
|
hash_function(const char *string) |
113 |
|
|
{ |
114 |
|
|
long sum = 0; |
115 |
|
|
|
116 |
|
|
DEBUG(9, ("hashing %s", string)); |
117 |
✓✓ |
8756 |
while (*string) { |
118 |
|
2887 |
sum += (long) (*string + (*(string + 1) << 8)); |
119 |
|
2887 |
string++; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
DEBUG(9, ("sum is %ld", sum)); |
123 |
|
994 |
return (int) (sum % HASHTABSIZE); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
/* |
127 |
|
|
* struct name_table_entry * |
128 |
|
|
* find_entry(string) |
129 |
|
|
* |
130 |
|
|
* Finds the entry for the given string in the hash table if present. |
131 |
|
|
* Returns a pointer to the entry in the table or 0 if not found. |
132 |
|
|
* |
133 |
|
|
*/ |
134 |
|
|
|
135 |
|
|
#ifndef MAIN_PROGRAM |
136 |
|
|
NCURSES_EXPORT(struct name_table_entry const *) |
137 |
|
|
_nc_find_entry(const char *string, |
138 |
|
|
const short *hash_table) |
139 |
|
|
{ |
140 |
|
|
int hashvalue; |
141 |
|
|
struct name_table_entry const *ptr = 0; |
142 |
|
|
struct name_table_entry const *real_table; |
143 |
|
|
|
144 |
|
|
hashvalue = hash_function(string); |
145 |
|
|
|
146 |
|
|
if (hash_table[hashvalue] >= 0) { |
147 |
|
|
real_table = _nc_get_table(hash_table != _nc_get_hash_table(FALSE)); |
148 |
|
|
ptr = real_table + hash_table[hashvalue]; |
149 |
|
|
while (strcmp(ptr->nte_name, string) != 0) { |
150 |
|
|
if (ptr->nte_link < 0) |
151 |
|
|
return 0; |
152 |
|
|
ptr = real_table + (ptr->nte_link + hash_table[HASHTABSIZE]); |
153 |
|
|
} |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
return (ptr); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
/* |
160 |
|
|
* struct name_table_entry * |
161 |
|
|
* find_type_entry(string, type, table) |
162 |
|
|
* |
163 |
|
|
* Finds the first entry for the given name with the given type in the |
164 |
|
|
* given table if present (as distinct from find_entry, which finds the |
165 |
|
|
* the last entry regardless of type). You can use this if you detect |
166 |
|
|
* a name clash. It's slower, though. Returns a pointer to the entry |
167 |
|
|
* in the table or 0 if not found. |
168 |
|
|
*/ |
169 |
|
|
|
170 |
|
|
NCURSES_EXPORT(struct name_table_entry const *) |
171 |
|
|
_nc_find_type_entry(const char *string, |
172 |
|
|
int type, |
173 |
|
|
const struct name_table_entry *table) |
174 |
|
|
{ |
175 |
|
|
struct name_table_entry const *ptr; |
176 |
|
|
|
177 |
|
|
for (ptr = table; ptr < table + CAPTABSIZE; ptr++) { |
178 |
|
|
if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0) |
179 |
|
|
return (ptr); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
return ((struct name_table_entry *) NULL); |
183 |
|
|
} |
184 |
|
|
#endif |
185 |
|
|
|
186 |
|
|
#ifdef MAIN_PROGRAM |
187 |
|
|
/* |
188 |
|
|
* This filter reads from standard input a list of tab-delimited columns, |
189 |
|
|
* (e.g., from Caps.filtered) computes the hash-value of a specified column and |
190 |
|
|
* writes the hashed tables to standard output. |
191 |
|
|
* |
192 |
|
|
* By compiling the hash table at build time, we're able to make the entire |
193 |
|
|
* set of terminfo and termcap tables readonly (and also provide some runtime |
194 |
|
|
* performance enhancement). |
195 |
|
|
*/ |
196 |
|
|
|
197 |
|
|
#define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */ |
198 |
|
|
|
199 |
|
|
static char ** |
200 |
|
|
parse_columns(char *buffer) |
201 |
|
|
{ |
202 |
|
|
static char **list; |
203 |
|
|
|
204 |
|
|
int col = 0; |
205 |
|
|
|
206 |
✓✓✗✓
|
4214 |
if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0) |
207 |
|
|
return (0); |
208 |
|
|
|
209 |
✓✓ |
2106 |
if (*buffer != '#') { |
210 |
✓✓ |
22584 |
while (*buffer != '\0') { |
211 |
|
|
char *s; |
212 |
✓✓✓✓
|
188588 |
for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++) |
213 |
|
|
/*EMPTY */ ; |
214 |
✓✗ |
11290 |
if (s != buffer) { |
215 |
|
11290 |
char mark = *s; |
216 |
|
11290 |
*s = '\0'; |
217 |
✗✗ |
11290 |
if ((s - buffer) > 1 |
218 |
✓✓ |
21032 |
&& (*buffer == '"') |
219 |
✗✓ |
9742 |
&& (s[-1] == '"')) { /* strip the quotes */ |
220 |
|
|
assert(s > buffer + 1); |
221 |
|
|
s[-1] = '\0'; |
222 |
|
|
buffer++; |
223 |
|
|
} |
224 |
|
11290 |
list[col] = buffer; |
225 |
|
11290 |
col++; |
226 |
✓✓ |
11290 |
if (mark == '\0') |
227 |
|
994 |
break; |
228 |
✓✗✓✓
|
38634 |
while (*++s && isspace(UChar(*s))) |
229 |
|
|
/*EMPTY */ ; |
230 |
|
|
buffer = s; |
231 |
✓✓ |
10296 |
} else |
232 |
|
|
break; |
233 |
✓✓ |
10296 |
} |
234 |
|
|
} |
235 |
|
2106 |
return col ? list : 0; |
236 |
|
2106 |
} |
237 |
|
|
|
238 |
|
|
int |
239 |
|
|
main(int argc, char **argv) |
240 |
|
|
{ |
241 |
|
4 |
struct name_table_entry *name_table = typeCalloc(struct |
242 |
|
|
name_table_entry, CAPTABSIZE); |
243 |
|
2 |
short *hash_table = typeCalloc(short, HASHTABSIZE); |
244 |
|
|
const char *root_name = ""; |
245 |
|
|
int column = 0; |
246 |
|
|
int bigstring = 0; |
247 |
|
|
int n; |
248 |
|
2 |
char buffer[BUFSIZ]; |
249 |
|
|
|
250 |
|
|
static const char *typenames[] = |
251 |
|
|
{"BOOLEAN", "NUMBER", "STRING"}; |
252 |
|
|
|
253 |
|
|
short BoolCount = 0; |
254 |
|
|
short NumCount = 0; |
255 |
|
|
short StrCount = 0; |
256 |
|
|
|
257 |
|
|
/* The first argument is the column-number (starting with 0). |
258 |
|
|
* The second is the root name of the tables to generate. |
259 |
|
|
*/ |
260 |
|
2 |
if (argc <= 3 |
261 |
✓✗ |
4 |
|| (column = atoi(argv[1])) <= 0 |
262 |
✓✗ |
2 |
|| (column >= MAX_COLUMNS) |
263 |
|
2 |
|| *(root_name = argv[2]) == 0 |
264 |
✓✗ |
4 |
|| (bigstring = atoi(argv[3])) < 0 |
265 |
✗✓ |
4 |
|| name_table == 0 |
266 |
|
2 |
|| hash_table == 0) { |
267 |
|
|
fprintf(stderr, "usage: make_hash column root_name bigstring\n"); |
268 |
|
|
exit(EXIT_FAILURE); |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
/* |
272 |
|
|
* Read the table into our arrays. |
273 |
|
|
*/ |
274 |
✓✓✓✗
|
5210 |
for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) { |
275 |
|
2106 |
char **list, *nlp = strchr(buffer, '\n'); |
276 |
✓✗ |
2106 |
if (nlp) |
277 |
|
2106 |
*nlp = '\0'; |
278 |
|
2106 |
list = parse_columns(buffer); |
279 |
✓✓ |
2106 |
if (list == 0) /* blank or comment */ |
280 |
|
1112 |
continue; |
281 |
|
994 |
name_table[n].nte_link = -1; /* end-of-hash */ |
282 |
|
994 |
name_table[n].nte_name = strdup(list[column]); |
283 |
✓✓ |
994 |
if (!strcmp(list[2], "bool")) { |
284 |
|
88 |
name_table[n].nte_type = BOOLEAN; |
285 |
|
88 |
name_table[n].nte_index = BoolCount++; |
286 |
✓✓ |
994 |
} else if (!strcmp(list[2], "num")) { |
287 |
|
78 |
name_table[n].nte_type = NUMBER; |
288 |
|
78 |
name_table[n].nte_index = NumCount++; |
289 |
✓✗ |
906 |
} else if (!strcmp(list[2], "str")) { |
290 |
|
828 |
name_table[n].nte_type = STRING; |
291 |
|
828 |
name_table[n].nte_index = StrCount++; |
292 |
|
|
} else { |
293 |
|
|
fprintf(stderr, "Unknown type: %s\n", list[2]); |
294 |
|
|
exit(EXIT_FAILURE); |
295 |
|
|
} |
296 |
|
994 |
n++; |
297 |
✓✓ |
994 |
} |
298 |
|
2 |
_nc_make_hash_table(name_table, hash_table); |
299 |
|
|
|
300 |
|
|
/* |
301 |
|
|
* Write the compiled tables to standard output |
302 |
|
|
*/ |
303 |
✓✗ |
2 |
if (bigstring) { |
304 |
|
|
int len = 0; |
305 |
|
|
int nxt; |
306 |
|
|
|
307 |
|
2 |
printf("static const char %s_names_text[] = \\\n", root_name); |
308 |
✓✓ |
1992 |
for (n = 0; n < CAPTABSIZE; n++) { |
309 |
|
994 |
nxt = (int) strlen(name_table[n].nte_name) + 5; |
310 |
✓✓ |
994 |
if (nxt + len > 72) { |
311 |
|
112 |
printf("\\\n"); |
312 |
|
|
len = 0; |
313 |
|
112 |
} |
314 |
|
994 |
printf("\"%s\\0\" ", name_table[n].nte_name); |
315 |
|
994 |
len += nxt; |
316 |
|
|
} |
317 |
|
2 |
printf(";\n\n"); |
318 |
|
|
|
319 |
|
|
len = 0; |
320 |
|
2 |
printf("static name_table_data const %s_names_data[] =\n", |
321 |
|
|
root_name); |
322 |
|
2 |
printf("{\n"); |
323 |
✓✓ |
1992 |
for (n = 0; n < CAPTABSIZE; n++) { |
324 |
|
994 |
printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n", |
325 |
|
|
len, |
326 |
|
994 |
typenames[name_table[n].nte_type], |
327 |
|
994 |
name_table[n].nte_index, |
328 |
|
994 |
name_table[n].nte_link, |
329 |
|
994 |
n < CAPTABSIZE - 1 ? ',' : ' '); |
330 |
|
994 |
len += (int) strlen(name_table[n].nte_name) + 1; |
331 |
|
|
} |
332 |
|
2 |
printf("};\n\n"); |
333 |
|
2 |
printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name); |
334 |
|
2 |
} else { |
335 |
|
|
|
336 |
|
|
printf("static struct name_table_entry %s _nc_%s_table[] =\n", |
337 |
|
|
bigstring ? "" : "const", |
338 |
|
|
root_name); |
339 |
|
|
printf("{\n"); |
340 |
|
|
for (n = 0; n < CAPTABSIZE; n++) { |
341 |
|
|
snprintf(buffer, sizeof(buffer), "\"%s\"", |
342 |
|
|
name_table[n].nte_name); |
343 |
|
|
printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n", |
344 |
|
|
buffer, |
345 |
|
|
typenames[name_table[n].nte_type], |
346 |
|
|
name_table[n].nte_index, |
347 |
|
|
name_table[n].nte_link, |
348 |
|
|
n < CAPTABSIZE - 1 ? ',' : ' '); |
349 |
|
|
} |
350 |
|
|
printf("};\n\n"); |
351 |
|
|
} |
352 |
|
|
|
353 |
|
2 |
printf("static const short _nc_%s_hash_table[%d] =\n", |
354 |
|
|
root_name, |
355 |
|
|
HASHTABSIZE + 1); |
356 |
|
2 |
printf("{\n"); |
357 |
✓✓ |
3980 |
for (n = 0; n < HASHTABSIZE; n++) { |
358 |
|
1988 |
printf("\t%3d,\n", hash_table[n]); |
359 |
|
|
} |
360 |
|
2 |
printf("\t0\t/* base-of-table */\n"); |
361 |
|
2 |
printf("};\n\n"); |
362 |
|
|
|
363 |
|
2 |
printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n", |
364 |
|
2 |
BoolCount, NumCount, StrCount); |
365 |
|
2 |
printf("#error\t--> term.h and comp_captab.c disagree about the <--\n"); |
366 |
|
2 |
printf("#error\t--> numbers of booleans, numbers and/or strings <--\n"); |
367 |
|
2 |
printf("#endif\n\n"); |
368 |
|
|
|
369 |
|
2 |
free(hash_table); |
370 |
|
|
return EXIT_SUCCESS; |
371 |
|
2 |
} |
372 |
|
|
#endif |