Imported Upstream version 15.8a
[platform/upstream/cscope.git] / src / lookup.c
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 static char const rcsid[] = "$Id: lookup.c,v 1.4 2006/04/21 10:45:48 broeker Exp $";
42
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";
49
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
54  * for old databases.
55  */
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 */
61         {"case",        ' ',    NULL},
62         {"char",        ' ',    NULL},
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 */
68         {"else",        ' ',    NULL},
69         {enumtext,      ' ',    NULL},
70         {externtext,    ' ',    NULL},
71         {"float",       ' ',    NULL},
72         {"for",         '(',    NULL},
73         {"goto",        ' ',    NULL},
74         {"if",          '(',    NULL},
75         {"int",         ' ',    NULL},
76         {"long",        ' ',    NULL},
77         {"register",    ' ',    NULL},
78         {"return",      '\0',   NULL},
79         {"short",       ' ',    NULL},
80         {"sizeof",      '\0',   NULL},
81         {"static",      ' ',    NULL},
82         {structtext,    ' ',    NULL},
83         {"switch",      '(',    NULL},
84         {typedeftext,   ' ',    NULL},
85         {uniontext,     ' ',    NULL},
86         {"unsigned",    ' ',    NULL},
87         {"void",        ' ',    NULL},
88         {"while",       '(',    NULL},
89         
90         /* these keywords are not compressed */
91         {"do",          '\0',   NULL},
92         {"auto",        ' ',    NULL},
93         {"fortran",     ' ',    NULL},
94         {"const",       ' ',    NULL},
95         {"signed",      ' ',    NULL},
96         {"volatile",    ' ',    NULL},
97 };
98 #define KEYWORDS        (sizeof(keyword) / sizeof(struct keystruct))
99
100 #define HASHMOD (KEYWORDS * 2 + 1)
101
102 static  struct  keystruct *hashtab[HASHMOD]; /* pointer table */
103
104 /* put the keywords into the symbol table */
105
106 void
107 initsymtab(void)
108 {
109     unsigned int i, j;
110     struct keystruct *p;
111         
112     for (i = 1; i < KEYWORDS; ++i) {
113         p = keyword + i;
114         j = hash(p->text) % HASHMOD;
115         p->next = hashtab[j];
116         hashtab[j] = p;
117     }
118 }
119
120 /* see if this identifier is a keyword */
121
122 char *
123 lookup(char *ident)
124 {
125         struct  keystruct *p;
126         int     c;
127         
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 */
133                         }
134                         return(p->text);
135                 }
136         }
137         /* this is an identifier */
138         return(NULL);
139 }
140
141 /* form hash value for string */
142 int
143 hash(char *ss)
144 {
145         int     i;
146         unsigned char   *s = (unsigned char *)ss;
147         
148         for (i = 0; *s != '\0'; )
149                 i += *s++;      /* += is faster than <<= for cscope */
150         return(i);
151 }