xref: /trunk/main/soltools/cpp/_nlist.c (revision 509a48ff)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "cpp.h"
26 
27 extern int Cplusplus;
28 Nlist *kwdefined;
29 char wd[128];
30 
31 /*
32 	ER: Tabelle extra gross gemacht, da es anscheinend ein Problem mit der
33 	der Verkettung gibt, irgendwann irgendwo wird mal ein nlist->next
34 	ueberschrieben, was in eineme SIGSEGV resultiert.
35 	Den GDB mit watchpoint hab ich aber nach 2 Tagen abgebrochen..
36 	so loeppt's jedenfalls erstmal..
37  */
38 #define	NLSIZE 15000
39 
40 static Nlist *nlist[NLSIZE];
41 
42 struct kwtab
43 {
44     char *kw;
45     int val;
46     int flag;
47 }   kwtab[] =
48 
49 {
50 		{"if", KIF, ISKW},
51 		{"ifdef", KIFDEF, ISKW},
52 		{"ifndef", KIFNDEF, ISKW},
53 		{"elif", KELIF, ISKW},
54 		{"else", KELSE, ISKW},
55 		{"endif", KENDIF, ISKW},
56 		{"include", KINCLUDE, ISKW},
57 		{"include_next", KINCLUDENEXT, ISKW},
58 		{"import", KIMPORT, ISKW},
59 		{"define", KDEFINE, ISKW},
60 		{"undef", KUNDEF, ISKW},
61 		{"line", KLINE, ISKW},
62 		{"error", KERROR, ISKW},
63 		{"pragma", KPRAGMA, ISKW},
64 		{"ident", KIDENT, ISKW},
65 		{"eval", KEVAL, ISKW},
66 		{"defined", KDEFINED, ISDEFINED + ISUNCHANGE},
67 		{"machine", KMACHINE, ISDEFINED + ISUNCHANGE},
68 		{"__LINE__", KLINENO, ISMAC + ISUNCHANGE},
69 		{"__FILE__", KFILE, ISMAC + ISUNCHANGE},
70 		{"__DATE__", KDATE, ISMAC + ISUNCHANGE},
71 		{"__TIME__", KTIME, ISMAC + ISUNCHANGE},
72 		{"__STDC__", KSTDC, ISUNCHANGE},
73 		{NULL, 0, 0}
74 };
75 
76 unsigned long namebit[077 + 1];
77 
78 void
setup_kwtab(void)79     setup_kwtab(void)
80 {
81     struct kwtab *kp;
82     Nlist *np;
83     Token t;
84     static Token deftoken[1] = {{NAME, 0, 0, 7, (uchar *) "defined", 0}};
85     static Tokenrow deftr = {deftoken, deftoken, deftoken + 1, 1};
86 
87     for (kp = kwtab; kp->kw; kp++)
88     {
89         t.t = (uchar *) kp->kw;
90         t.len = strlen(kp->kw);
91         np = lookup(&t, 1);
92         np->flag = (char) kp->flag;
93         np->val = (char) kp->val;
94         if (np->val == KDEFINED)
95         {
96             kwdefined = np;
97             np->val = NAME;
98             np->vp = &deftr;
99             np->ap = NULL;
100         }
101     }
102 }
103 
104 Nlist *
lookup(Token * tp,int install)105     lookup(Token * tp, int install)
106 {
107     unsigned int h;
108     Nlist *np;
109     uchar *cp, *cpe;
110 
111     h = 0;
112     for (cp = tp->t, cpe = cp + tp->len; cp < cpe;)
113         h += *cp++;
114     h %= NLSIZE;
115     np = nlist[h];
116     while (np)
117     {
118         if (*tp->t == *np->name && tp->len == (unsigned int)np->len
119             && strncmp((char *)tp->t, (char *)np->name, tp->len) == 0)
120             return np;
121         np = np->next;
122     }
123     if (install)
124     {
125         np = new(Nlist);
126         np->vp = NULL;
127         np->ap = NULL;
128         np->flag = 0;
129         np->val = 0;
130         np->len = tp->len;
131         np->name = newstring(tp->t, tp->len, 0);
132         np->next = nlist[h];
133         nlist[h] = np;
134         quickset(tp->t[0], tp->len > 1 ? tp->t[1] : 0);
135         return np;
136     }
137     return NULL;
138 }
139