Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / symtab.c
blob6c66f901356cd51d1a6113f809ef9e96a5dc8e79
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1996-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: symtab.c,v 1.30 2007/06/19 23:47:17 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <ctype.h>
28 #include <isc/magic.h>
29 #include <isc/mem.h>
30 #include <isc/string.h>
31 #include <isc/symtab.h>
32 #include <isc/util.h>
34 typedef struct elt {
35 char * key;
36 unsigned int type;
37 isc_symvalue_t value;
38 LINK(struct elt) link;
39 } elt_t;
41 typedef LIST(elt_t) eltlist_t;
43 #define SYMTAB_MAGIC ISC_MAGIC('S', 'y', 'm', 'T')
44 #define VALID_SYMTAB(st) ISC_MAGIC_VALID(st, SYMTAB_MAGIC)
46 struct isc_symtab {
47 /* Unlocked. */
48 unsigned int magic;
49 isc_mem_t * mctx;
50 unsigned int size;
51 eltlist_t * table;
52 isc_symtabaction_t undefine_action;
53 void * undefine_arg;
54 isc_boolean_t case_sensitive;
57 isc_result_t
58 isc_symtab_create(isc_mem_t *mctx, unsigned int size,
59 isc_symtabaction_t undefine_action,
60 void *undefine_arg,
61 isc_boolean_t case_sensitive,
62 isc_symtab_t **symtabp)
64 isc_symtab_t *symtab;
65 unsigned int i;
67 REQUIRE(mctx != NULL);
68 REQUIRE(symtabp != NULL && *symtabp == NULL);
69 REQUIRE(size > 0); /* Should be prime. */
71 symtab = (isc_symtab_t *)isc_mem_get(mctx, sizeof(*symtab));
72 if (symtab == NULL)
73 return (ISC_R_NOMEMORY);
74 symtab->table = (eltlist_t *)isc_mem_get(mctx,
75 size * sizeof(eltlist_t));
76 if (symtab->table == NULL) {
77 isc_mem_put(mctx, symtab, sizeof(*symtab));
78 return (ISC_R_NOMEMORY);
80 for (i = 0; i < size; i++)
81 INIT_LIST(symtab->table[i]);
82 symtab->mctx = mctx;
83 symtab->size = size;
84 symtab->undefine_action = undefine_action;
85 symtab->undefine_arg = undefine_arg;
86 symtab->case_sensitive = case_sensitive;
87 symtab->magic = SYMTAB_MAGIC;
89 *symtabp = symtab;
91 return (ISC_R_SUCCESS);
94 void
95 isc_symtab_destroy(isc_symtab_t **symtabp) {
96 isc_symtab_t *symtab;
97 unsigned int i;
98 elt_t *elt, *nelt;
100 REQUIRE(symtabp != NULL);
101 symtab = *symtabp;
102 REQUIRE(VALID_SYMTAB(symtab));
104 for (i = 0; i < symtab->size; i++) {
105 for (elt = HEAD(symtab->table[i]); elt != NULL; elt = nelt) {
106 nelt = NEXT(elt, link);
107 if (symtab->undefine_action != NULL)
108 (symtab->undefine_action)(elt->key,
109 elt->type,
110 elt->value,
111 symtab->undefine_arg);
112 isc_mem_put(symtab->mctx, elt, sizeof(*elt));
115 isc_mem_put(symtab->mctx, symtab->table,
116 symtab->size * sizeof(eltlist_t));
117 symtab->magic = 0;
118 isc_mem_put(symtab->mctx, symtab, sizeof(*symtab));
120 *symtabp = NULL;
123 static inline unsigned int
124 hash(const char *key, isc_boolean_t case_sensitive) {
125 const char *s;
126 unsigned int h = 0;
127 int c;
130 * This hash function is similar to the one Ousterhout
131 * uses in Tcl.
134 if (case_sensitive) {
135 for (s = key; *s != '\0'; s++) {
136 h += (h << 3) + *s;
138 } else {
139 for (s = key; *s != '\0'; s++) {
140 c = *s;
141 c = tolower((unsigned char)c);
142 h += (h << 3) + c;
146 return (h);
149 #define FIND(s, k, t, b, e) \
150 b = hash((k), (s)->case_sensitive) % (s)->size; \
151 if ((s)->case_sensitive) { \
152 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
153 if (((t) == 0 || e->type == (t)) && \
154 strcmp(e->key, (k)) == 0) \
155 break; \
157 } else { \
158 for (e = HEAD((s)->table[b]); e != NULL; e = NEXT(e, link)) { \
159 if (((t) == 0 || e->type == (t)) && \
160 strcasecmp(e->key, (k)) == 0) \
161 break; \
165 isc_result_t
166 isc_symtab_lookup(isc_symtab_t *symtab, const char *key, unsigned int type,
167 isc_symvalue_t *value)
169 unsigned int bucket;
170 elt_t *elt;
172 REQUIRE(VALID_SYMTAB(symtab));
173 REQUIRE(key != NULL);
175 FIND(symtab, key, type, bucket, elt);
177 if (elt == NULL)
178 return (ISC_R_NOTFOUND);
180 if (value != NULL)
181 *value = elt->value;
183 return (ISC_R_SUCCESS);
186 isc_result_t
187 isc_symtab_define(isc_symtab_t *symtab, const char *key, unsigned int type,
188 isc_symvalue_t value, isc_symexists_t exists_policy)
190 unsigned int bucket;
191 elt_t *elt;
193 REQUIRE(VALID_SYMTAB(symtab));
194 REQUIRE(key != NULL);
195 REQUIRE(type != 0);
197 FIND(symtab, key, type, bucket, elt);
199 if (exists_policy != isc_symexists_add && elt != NULL) {
200 if (exists_policy == isc_symexists_reject)
201 return (ISC_R_EXISTS);
202 INSIST(exists_policy == isc_symexists_replace);
203 UNLINK(symtab->table[bucket], elt, link);
204 if (symtab->undefine_action != NULL)
205 (symtab->undefine_action)(elt->key, elt->type,
206 elt->value,
207 symtab->undefine_arg);
208 } else {
209 elt = (elt_t *)isc_mem_get(symtab->mctx, sizeof(*elt));
210 if (elt == NULL)
211 return (ISC_R_NOMEMORY);
212 ISC_LINK_INIT(elt, link);
216 * Though the "key" can be const coming in, it is not stored as const
217 * so that the calling program can easily have writable access to
218 * it in its undefine_action function. In the event that it *was*
219 * truly const coming in and then the caller modified it anyway ...
220 * well, don't do that!
222 DE_CONST(key, elt->key);
223 elt->type = type;
224 elt->value = value;
227 * We prepend so that the most recent definition will be found.
229 PREPEND(symtab->table[bucket], elt, link);
231 return (ISC_R_SUCCESS);
234 isc_result_t
235 isc_symtab_undefine(isc_symtab_t *symtab, const char *key, unsigned int type) {
236 unsigned int bucket;
237 elt_t *elt;
239 REQUIRE(VALID_SYMTAB(symtab));
240 REQUIRE(key != NULL);
242 FIND(symtab, key, type, bucket, elt);
244 if (elt == NULL)
245 return (ISC_R_NOTFOUND);
247 if (symtab->undefine_action != NULL)
248 (symtab->undefine_action)(elt->key, elt->type,
249 elt->value, symtab->undefine_arg);
250 UNLINK(symtab->table[bucket], elt, link);
251 isc_mem_put(symtab->mctx, elt, sizeof(*elt));
253 return (ISC_R_SUCCESS);