nlist.c (2042B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include "cpp.h" 5 6 extern int getopt(int, char *const *, const char *); 7 extern char *optarg; 8 extern int optind; 9 extern int verbose; 10 extern int Cplusplus; 11 Nlist *kwdefined; 12 char wd[128]; 13 14 #define NLSIZE 128 15 16 static Nlist *nlist[NLSIZE]; 17 18 struct kwtab { 19 char *kw; 20 int val; 21 int flag; 22 } kwtab[] = { 23 "if", KIF, ISKW, 24 "ifdef", KIFDEF, ISKW, 25 "ifndef", KIFNDEF, ISKW, 26 "elif", KELIF, ISKW, 27 "else", KELSE, ISKW, 28 "endif", KENDIF, ISKW, 29 "include", KINCLUDE, ISKW, 30 "define", KDEFINE, ISKW, 31 "undef", KUNDEF, ISKW, 32 "line", KLINE, 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 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 * 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 }