etc/services - sync with NetBSD-8
[minix.git] / external / bsd / flex / dist / sym.c
blob0e99bfe6cd364676c7076795725e8cf1208fae77
1 /* $NetBSD: sym.c,v 1.3 2014/10/30 18:44:05 christos Exp $ */
3 /* sym - symbol table routines */
5 /* Copyright (c) 1990 The Regents of the University of California. */
6 /* All rights reserved. */
8 /* This code is derived from software contributed to Berkeley by */
9 /* Vern Paxson. */
11 /* The United States Government has rights in this work pursuant */
12 /* to contract no. DE-AC03-76SF00098 between the United States */
13 /* Department of Energy and the University of California. */
15 /* This file is part of flex. */
17 /* Redistribution and use in source and binary forms, with or without */
18 /* modification, are permitted provided that the following conditions */
19 /* are met: */
21 /* 1. Redistributions of source code must retain the above copyright */
22 /* notice, this list of conditions and the following disclaimer. */
23 /* 2. Redistributions in binary form must reproduce the above copyright */
24 /* notice, this list of conditions and the following disclaimer in the */
25 /* documentation and/or other materials provided with the distribution. */
27 /* Neither the name of the University nor the names of its contributors */
28 /* may be used to endorse or promote products derived from this software */
29 /* without specific prior written permission. */
31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34 /* PURPOSE. */
35 #include "flexdef.h"
36 __RCSID("$NetBSD: sym.c,v 1.3 2014/10/30 18:44:05 christos Exp $");
39 /* Variables for symbol tables:
40 * sctbl - start-condition symbol table
41 * ndtbl - name-definition symbol table
42 * ccltab - character class text symbol table
45 struct hash_entry {
46 struct hash_entry *prev, *next;
47 char *name;
48 char *str_val;
49 int int_val;
52 typedef struct hash_entry **hash_table;
54 #define NAME_TABLE_HASH_SIZE 101
55 #define START_COND_HASH_SIZE 101
56 #define CCL_HASH_SIZE 101
58 static struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
59 static struct hash_entry *sctbl[START_COND_HASH_SIZE];
60 static struct hash_entry *ccltab[CCL_HASH_SIZE];
63 /* declare functions that have forward references */
65 static int addsym PROTO ((register char[], char *, int, hash_table, int));
66 static struct hash_entry *findsym PROTO ((register const char *sym,
67 hash_table table,
69 int table_size));
70 static int hashfunct PROTO ((register const char *, int));
73 /* addsym - add symbol and definitions to symbol table
75 * -1 is returned if the symbol already exists, and the change not made.
78 static int addsym (sym, str_def, int_def, table, table_size)
79 register char sym[];
80 char *str_def;
81 int int_def;
82 hash_table table;
83 int table_size;
85 int hash_val = hashfunct (sym, table_size);
86 register struct hash_entry *sym_entry = table[hash_val];
87 register struct hash_entry *new_entry;
88 register struct hash_entry *successor;
90 while (sym_entry) {
91 if (!strcmp (sym, sym_entry->name)) { /* entry already exists */
92 return -1;
95 sym_entry = sym_entry->next;
98 /* create new entry */
99 new_entry = (struct hash_entry *)
100 flex_alloc (sizeof (struct hash_entry));
102 if (new_entry == NULL)
103 flexfatal (_("symbol table memory allocation failed"));
105 if ((successor = table[hash_val]) != 0) {
106 new_entry->next = successor;
107 successor->prev = new_entry;
109 else
110 new_entry->next = NULL;
112 new_entry->prev = NULL;
113 new_entry->name = sym;
114 new_entry->str_val = str_def;
115 new_entry->int_val = int_def;
117 table[hash_val] = new_entry;
119 return 0;
123 /* cclinstal - save the text of a character class */
125 void cclinstal (ccltxt, cclnum)
126 Char ccltxt[];
127 int cclnum;
129 /* We don't bother checking the return status because we are not
130 * called unless the symbol is new.
133 (void) addsym ((char *) copy_unsigned_string (ccltxt),
134 (char *) 0, cclnum, ccltab, CCL_HASH_SIZE);
138 /* ccllookup - lookup the number associated with character class text
140 * Returns 0 if there's no CCL associated with the text.
143 int ccllookup (ccltxt)
144 Char ccltxt[];
146 return findsym ((char *) ccltxt, ccltab, CCL_HASH_SIZE)->int_val;
150 /* findsym - find symbol in symbol table */
152 static struct hash_entry *findsym (sym, table, table_size)
153 register const char *sym;
154 hash_table table;
155 int table_size;
157 static struct hash_entry empty_entry = {
158 (struct hash_entry *) 0, (struct hash_entry *) 0,
159 (char *) 0, (char *) 0, 0,
161 register struct hash_entry *sym_entry =
163 table[hashfunct (sym, table_size)];
165 while (sym_entry) {
166 if (!strcmp (sym, sym_entry->name))
167 return sym_entry;
168 sym_entry = sym_entry->next;
171 return &empty_entry;
174 /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
176 static int hashfunct (str, hash_size)
177 register const char *str;
178 int hash_size;
180 register int hashval;
181 register int locstr;
183 hashval = 0;
184 locstr = 0;
186 while (str[locstr]) {
187 hashval = (hashval << 1) + (unsigned char) str[locstr++];
188 hashval %= hash_size;
191 return hashval;
195 /* ndinstal - install a name definition */
197 void ndinstal (name, definition)
198 const char *name;
199 Char definition[];
202 if (addsym (copy_string (name),
203 (char *) copy_unsigned_string (definition), 0,
204 ndtbl, NAME_TABLE_HASH_SIZE))
205 synerr (_("name defined twice"));
209 /* ndlookup - lookup a name definition
211 * Returns a nil pointer if the name definition does not exist.
214 Char *ndlookup (nd)
215 const char *nd;
217 return (Char *) findsym (nd, ndtbl, NAME_TABLE_HASH_SIZE)->str_val;
221 /* scextend - increase the maximum number of start conditions */
223 void scextend ()
225 current_max_scs += MAX_SCS_INCREMENT;
227 ++num_reallocs;
229 scset = reallocate_integer_array (scset, current_max_scs);
230 scbol = reallocate_integer_array (scbol, current_max_scs);
231 scxclu = reallocate_integer_array (scxclu, current_max_scs);
232 sceof = reallocate_integer_array (sceof, current_max_scs);
233 scname = reallocate_char_ptr_array (scname, current_max_scs);
237 /* scinstal - make a start condition
239 * NOTE
240 * The start condition is "exclusive" if xcluflg is true.
243 void scinstal (str, xcluflg)
244 const char *str;
245 int xcluflg;
248 if (++lastsc >= current_max_scs)
249 scextend ();
251 scname[lastsc] = copy_string (str);
253 if (addsym (scname[lastsc], (char *) 0, lastsc,
254 sctbl, START_COND_HASH_SIZE))
255 format_pinpoint_message (_
256 ("start condition %s declared twice"),
257 str);
259 scset[lastsc] = mkstate (SYM_EPSILON);
260 scbol[lastsc] = mkstate (SYM_EPSILON);
261 scxclu[lastsc] = xcluflg;
262 sceof[lastsc] = false;
266 /* sclookup - lookup the number associated with a start condition
268 * Returns 0 if no such start condition.
271 int sclookup (str)
272 const char *str;
274 return findsym (str, sctbl, START_COND_HASH_SIZE)->int_val;