]> git.sur5r.net Git - openldap/blob - build/unproto/symbol.c
ITS#1991 fix + use struct berval
[openldap] / build / unproto / symbol.c
1 /*++
2 /* NAME
3 /*      symbol 3
4 /* SUMMARY
5 /*      rudimentary symbol table package
6 /* SYNOPSIS
7 /*      #include "symbol.h"
8 /*
9 /*      void sym_init()
10 /*
11 /*      void sym_enter(name, type)
12 /*      char *name;
13 /*      int type;
14 /*
15 /*      struct symbol *sym_find(name)
16 /*      char *name;
17 /* DESCRIPTION
18 /*      This is a rudimentary symbol-table package, just enough to
19 /*      keep track of a couple of C keywords.
20 /*
21 /*      sym_init() primes the table with C keywords. At present, most of
22 /*      the keywords that have to do with types are left out.
23 /*      We need a different strategy to detect type definitions because
24 /*      we do not keep track of typedef names.
25 /*
26 /*      sym_enter() adds an entry to the symbol table.
27 /*
28 /*      sym_find() locates a symbol table entry (it returns 0 if
29 /*      it is not found).
30 /* AUTHOR(S)
31 /*      Wietse Venema
32 /*      Eindhoven University of Technology
33 /*      Department of Mathematics and Computer Science
34 /*      Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
35 /* LAST MODIFICATION
36 /*      92/02/15 18:59:56
37 /* VERSION/RELEASE
38 /*      1.4
39 /*--*/
40
41 static char symbol_sccsid[] = "@(#) symbol.c 1.4 92/02/15 18:59:56";
42
43 /* C library */
44
45 extern char *strcpy();
46 extern char *malloc();
47
48 /* Application-specific stuff */
49
50 #include "error.h"
51 #include "token.h"
52 #include "symbol.h"
53
54 #define SYM_TABSIZE     20
55
56 static struct symbol *sym_tab[SYM_TABSIZE] = {0,};
57
58 /* More string stuff. Maybe it should go to an #include file. */
59
60 #define STREQ(x,y)      (*(x) == *(y) && strcmp((x),(y)) == 0)
61
62 /* sym_enter - enter symbol into table */
63
64 void    sym_enter(name, type)
65 char   *name;
66 int     type;
67 {
68     struct symbol *s;
69     int     where;
70
71     if ((s = (struct symbol *) malloc(sizeof(*s))) == 0
72         || (s->name = malloc(strlen(name) + 1)) == 0)
73         fatal("out of memory");
74     (void) strcpy(s->name, name);
75     s->type = type;
76
77     where = hash(name, SYM_TABSIZE);
78     s->next = sym_tab[where];
79     sym_tab[where] = s;
80 }
81
82 /* sym_find - locate symbol definition */
83
84 struct symbol *sym_find(name)
85 register char *name;
86 {
87     register struct symbol *s;
88
89     /*
90      * This function is called for almost every "word" token, so it better be
91      * fast.
92      */
93
94     for (s = sym_tab[hash(name, SYM_TABSIZE)]; s; s = s->next)
95         if (STREQ(name, s->name))
96             return (s);
97     return (0);
98 }
99
100  /*
101   * Initialization data for symbol table. We do not enter keywords for types.
102   * We use a different strategy to detect type declarations because we do not
103   * keep track of typedef names.
104   */
105
106 struct sym {
107     char   *name;
108     int     tokno;
109 };
110
111 static struct sym syms[] = {
112     "if", TOK_CONTROL,
113     "else", TOK_CONTROL,
114     "for", TOK_CONTROL,
115     "while", TOK_CONTROL,
116     "do", TOK_CONTROL,
117     "switch", TOK_CONTROL,
118     "case", TOK_CONTROL,
119     "default", TOK_CONTROL,
120     "return", TOK_CONTROL,
121     "continue", TOK_CONTROL,
122     "break", TOK_CONTROL,
123     "goto", TOK_CONTROL,
124     "struct", TOK_COMPOSITE,
125     "union", TOK_COMPOSITE,
126     "__DATE__", TOK_DATE,
127     "__TIME__", TOK_TIME,
128 #if defined(MAP_VOID_STAR) || defined(MAP_VOID)
129     "void", TOK_VOID,
130 #endif
131     "asm", TOK_OTHER,
132     0,
133 };
134
135 /* sym_init - enter known keywords into symbol table */
136
137 void    sym_init()
138 {
139     register struct sym *p;
140
141     for (p = syms; p->name; p++)
142         sym_enter(p->name, p->tokno);
143 }
144