1 /*===========================================================================
2 Copyright (c) 1998-2000, The Santa Cruz Operation
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 *Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 *Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 *Neither name of The Santa Cruz Operation nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 =========================================================================*/
33 /* cscope - interactive C symbol cross-reference
35 * keyword look-up routine for the C symbol scanner
41 static char const rcsid[] = "$Id: lookup.c,v 1.4 2006/04/21 10:45:48 broeker Exp $";
43 /* keyword text for fast testing of keywords in the scanner */
44 char enumtext[] = "enum";
45 char externtext[] = "extern";
46 char structtext[] = "struct";
47 char typedeftext[] = "typedef";
48 char uniontext[] = "union";
50 /* This keyword table is also used for keyword text compression. Keywords
51 * with an index less than the numeric value of a space are replaced with the
52 * control character corresponding to the index, so they cannot be moved
53 * without changing the database file version and adding compatibility code
56 struct keystruct keyword[] = {
57 {"", '\0', NULL}, /* dummy entry */
58 {"#define", ' ', NULL}, /* must be table entry 1 */
59 {"#include", ' ', NULL}, /* must be table entry 2 */
60 {"break", '\0', NULL}, /* rarely in cross-reference */
63 {"continue", '\0', NULL}, /* rarely in cross-reference */
64 {"default", '\0', NULL}, /* rarely in cross-reference */
65 {"double", ' ', NULL},
66 {"\t", '\0', NULL}, /* must be the table entry 9 */
67 {"\n", '\0', NULL}, /* must be the table entry 10 */
69 {enumtext, ' ', NULL},
70 {externtext, ' ', NULL},
77 {"register", ' ', NULL},
78 {"return", '\0', NULL},
80 {"sizeof", '\0', NULL},
81 {"static", ' ', NULL},
82 {structtext, ' ', NULL},
83 {"switch", '(', NULL},
84 {typedeftext, ' ', NULL},
85 {uniontext, ' ', NULL},
86 {"unsigned", ' ', NULL},
90 /* these keywords are not compressed */
93 {"fortran", ' ', NULL},
95 {"signed", ' ', NULL},
96 {"volatile", ' ', NULL},
98 #define KEYWORDS (sizeof(keyword) / sizeof(struct keystruct))
100 #define HASHMOD (KEYWORDS * 2 + 1)
102 static struct keystruct *hashtab[HASHMOD]; /* pointer table */
104 /* put the keywords into the symbol table */
112 for (i = 1; i < KEYWORDS; ++i) {
114 j = hash(p->text) % HASHMOD;
115 p->next = hashtab[j];
120 /* see if this identifier is a keyword */
128 /* look up the identifier in the keyword table */
129 for (p = hashtab[hash(ident) % HASHMOD]; p != NULL; p = p->next) {
130 if (strequal(ident, p->text)) {
131 if (compress == YES && (c = p - keyword) < ' ') {
132 ident[0] = c; /* compress the keyword */
137 /* this is an identifier */
141 /* form hash value for string */
146 unsigned char *s = (unsigned char *)ss;
148 for (i = 0; *s != '\0'; )
149 i += *s++; /* += is faster than <<= for cscope */