1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "cpp.h"
5 
6 extern	char	*optarg;
7 extern	int	optind;
8 extern	int	verbose;
9 extern	int	Cplusplus;
10 Nlist	*kwdefined;
11 char	wd[128];
12 
13 #define	NLSIZE	128
14 
15 static Nlist	*nlist[NLSIZE];
16 
17 struct	kwtab {
18 	char	*kw;
19 	int	val;
20 	int	flag;
21 } kwtab[] = {
22 	{"if",		KIF,		ISKW},
23 	{"ifdef",	KIFDEF,		ISKW},
24 	{"ifndef",	KIFNDEF,	ISKW},
25 	{"elif",		KELIF,		ISKW},
26 	{"else",		KELSE,		ISKW},
27 	{"endif",	KENDIF,		ISKW},
28 	{"include",	KINCLUDE,	ISKW},
29 	{"define",	KDEFINE,	ISKW},
30 	{"undef",	KUNDEF,		ISKW},
31 	{"line",		KLINE,		ISKW},
32 	{"warning",	KWARNING,	ISKW},
33 	{"error",	KERROR,		ISKW},
34 	{"pragma",	KPRAGMA,	ISKW},
35 	{"eval",		KEVAL,		ISKW},
36 	{"defined",	KDEFINED,	ISDEFINED+ISUNCHANGE},
37 	{"__LINE__",	KLINENO,	ISMAC+ISUNCHANGE},
38 	{"__FILE__",	KFILE,		ISMAC+ISUNCHANGE},
39 	{"__DATE__",	KDATE,		ISMAC+ISUNCHANGE},
40 	{"__TIME__",	KTIME,		ISMAC+ISUNCHANGE},
41 	{"__STDC__",	KSTDC,		ISUNCHANGE},
42 	{NULL}
43 };
44 
45 unsigned long	namebit[077+1];
46 Nlist 	*np;
47 
48 void
setup_kwtab(void)49 setup_kwtab(void)
50 {
51 	struct kwtab *kp;
52 	Nlist *np;
53 	Token t;
54 	static Token deftoken[1] = {{ NAME, 0, 0, 0, 7, (uchar*)"defined" }};
55 	static Tokenrow deftr = { deftoken, deftoken, deftoken+1, 1 };
56 
57 	for (kp=kwtab; kp->kw; kp++) {
58 		t.t = (uchar*)kp->kw;
59 		t.len = strlen(kp->kw);
60 		np = lookup(&t, 1);
61 		np->flag = kp->flag;
62 		np->val = kp->val;
63 		if (np->val == KDEFINED) {
64 			kwdefined = np;
65 			np->val = NAME;
66 			np->vp = &deftr;
67 			np->ap = 0;
68 		}
69 	}
70 }
71 
72 Nlist *
lookup(Token * tp,int install)73 lookup(Token *tp, int install)
74 {
75 	unsigned int h;
76 	Nlist *np;
77 	uchar *cp, *cpe;
78 
79 	h = 0;
80 	for (cp=tp->t, cpe=cp+tp->len; cp<cpe; )
81 		h += *cp++;
82 	h %= NLSIZE;
83 	np = nlist[h];
84 	while (np) {
85 		if (*tp->t==*np->name && tp->len==np->len
86 		 && strncmp((char*)tp->t, (char*)np->name, tp->len)==0)
87 			return np;
88 		np = np->next;
89 	}
90 	if (install) {
91 		np = new(Nlist);
92 		np->vp = NULL;
93 		np->ap = NULL;
94 		np->flag = 0;
95 		np->val = 0;
96 		np->len = tp->len;
97 		np->name = newstring(tp->t, tp->len, 0);
98 		np->next = nlist[h];
99 		nlist[h] = np;
100 		quickset(tp->t[0], tp->len>1? tp->t[1]:0);
101 		return np;
102 	}
103 	return NULL;
104 }
105