1 /*===========================================================================
2  Copyright (c) 1998-2000, The Santa Cruz Operation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  *Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
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.
14 
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.
18 
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
26  INTERRUPTION)
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
30  DAMAGE.
31  =========================================================================*/
32 
33 /*	cscope - interactive C symbol cross-reference
34  *
35  *	keyword look-up routine for the C symbol scanner
36  */
37 
38 #include "global.h"
39 #include "lookup.h"
40 
41 /* keyword text for fast testing of keywords in the scanner */
42 char	enumtext[] = "enum";
43 char	externtext[] = "extern";
44 char	structtext[] = "struct";
45 char	typedeftext[] = "typedef";
46 char	uniontext[] = "union";
47 
48 /* This keyword table is also used for keyword text compression.  Keywords
49  * with an index less than the numeric value of a space are replaced with the
50  * control character corresponding to the index, so they cannot be moved
51  * without changing the database file version and adding compatibility code
52  * for old databases.
53  */
54 struct	keystruct keyword[] = {
55 	{"",		'\0',	NULL},	/* dummy entry */
56 	{"#define",	' ',	NULL},	/* must be table entry 1 */
57 	{"#include",	' ',	NULL},	/* must be table entry 2 */
58 	{"break",	'\0',	NULL},	/* rarely in cross-reference */
59 	{"case",	' ',	NULL},
60 	{"char",	' ',	NULL},
61 	{"continue",	'\0',	NULL},	/* rarely in cross-reference */
62 	{"default",	'\0',	NULL},	/* rarely in cross-reference */
63 	{"double",	' ',	NULL},
64 	{"\t",		'\0',	NULL},	/* must be the table entry 9 */
65 	{"\n",		'\0',	NULL},	/* must be the table entry 10 */
66 	{"else",	' ',	NULL},
67 	{enumtext,	' ',	NULL},
68 	{externtext,	' ',	NULL},
69 	{"float",	' ',	NULL},
70 	{"for",		'(',	NULL},
71 	{"goto",	' ',	NULL},
72 	{"if",		'(',	NULL},
73 	{"int",		' ',	NULL},
74 	{"long",	' ',	NULL},
75 	{"register",	' ',	NULL},
76 	{"return",	'\0',	NULL},
77 	{"short",	' ',	NULL},
78 	{"sizeof",	'\0',	NULL},
79 	{"static",	' ',	NULL},
80 	{structtext,	' ',	NULL},
81 	{"switch",	'(',	NULL},
82 	{typedeftext,	' ',	NULL},
83 	{uniontext,	' ',	NULL},
84 	{"unsigned",	' ',	NULL},
85 	{"void",	' ',	NULL},
86 	{"while",	'(',	NULL},
87 
88 	/* these keywords are not compressed */
89 	{"do",		'\0',	NULL},
90 	{"auto",	' ',	NULL},
91 	{"fortran",	' ',	NULL},
92 	{"const",	' ',	NULL},
93 	{"signed",	' ',	NULL},
94 	{"volatile",	' ',	NULL},
95 };
96 #define KEYWORDS	(sizeof(keyword) / sizeof(keyword[0]))
97 
98 #define HASHMOD	(KEYWORDS * 2 + 1)
99 
100 static	struct	keystruct *hashtab[HASHMOD]; /* pointer table */
101 
102 /* put the keywords into the symbol table */
103 
104 void
initsymtab(void)105 initsymtab(void)
106 {
107     unsigned int i, j;
108     struct keystruct *p;
109 
110     for (i = 1; i < KEYWORDS; ++i) {
111 	p = keyword + i;
112 	j = hash(p->text) % HASHMOD;
113 	p->next = hashtab[j];
114 	hashtab[j] = p;
115     }
116 }
117 
118 /* see if this identifier is a keyword */
119 
120 char *
lookup(char * ident)121 lookup(char *ident)
122 {
123 	struct	keystruct *p;
124 	int	c;
125 
126 	/* look up the identifier in the keyword table */
127 	for (p = hashtab[hash(ident) % HASHMOD]; p != NULL; p = p->next) {
128 		if (strequal(ident, p->text)) {
129 			if (compress == YES && (c = p - keyword) < ' ') {
130 				ident[0] = c;	/* compress the keyword */
131 			}
132 			return(p->text);
133 		}
134 	}
135 	/* this is an identifier */
136 	return(NULL);
137 }
138 
139 /* form hash value for string */
140 int
hash(char * ss)141 hash(char *ss)
142 {
143 	int	i;
144 	unsigned char 	*s = (unsigned char *)ss;
145 
146 	for (i = 0; *s != '\0'; )
147 		i += *s++;	/* += is faster than <<= for cscope */
148 	return(i);
149 }
150