Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / lib / isccc / alist.c
blob01a7724e39537cb0cae96e2f46a566de34cf693e
1 /* $NetBSD: alist.c,v 1.4 2014/12/10 04:38:01 christos Exp $ */
3 /*
4 * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 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 AND NOMINUM DISCLAIMS ALL
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Portions Copyright (C) 2001 Nominum, Inc.
21 * Permission to use, copy, modify, and/or distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
26 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
28 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 /* Id: alist.c,v 1.8 2007/08/28 07:20:43 tbox Exp */
36 /*! \file */
38 #include <config.h>
40 #include <stdlib.h>
41 #include <string.h>
43 #include <isccc/alist.h>
44 #include <isc/assertions.h>
45 #include <isccc/result.h>
46 #include <isccc/sexpr.h>
47 #include <isccc/util.h>
49 #define CAR(s) (s)->value.as_dottedpair.car
50 #define CDR(s) (s)->value.as_dottedpair.cdr
52 #define ALIST_TAG "*alist*"
53 #define MAX_INDENT 64
55 static char spaces[MAX_INDENT + 1] =
56 " ";
58 isccc_sexpr_t *
59 isccc_alist_create(void)
61 isccc_sexpr_t *alist, *tag;
63 tag = isccc_sexpr_fromstring(ALIST_TAG);
64 if (tag == NULL)
65 return (NULL);
66 alist = isccc_sexpr_cons(tag, NULL);
67 if (alist == NULL) {
68 isccc_sexpr_free(&tag);
69 return (NULL);
72 return (alist);
75 isc_boolean_t
76 isccc_alist_alistp(isccc_sexpr_t *alist)
78 isccc_sexpr_t *car;
80 if (alist == NULL || alist->type != ISCCC_SEXPRTYPE_DOTTEDPAIR)
81 return (ISC_FALSE);
82 car = CAR(alist);
83 if (car == NULL || car->type != ISCCC_SEXPRTYPE_STRING)
84 return (ISC_FALSE);
85 if (strcmp(car->value.as_string, ALIST_TAG) != 0)
86 return (ISC_FALSE);
87 return (ISC_TRUE);
90 isc_boolean_t
91 isccc_alist_emptyp(isccc_sexpr_t *alist)
93 REQUIRE(isccc_alist_alistp(alist));
95 if (CDR(alist) == NULL)
96 return (ISC_TRUE);
97 return (ISC_FALSE);
100 isccc_sexpr_t *
101 isccc_alist_first(isccc_sexpr_t *alist)
103 REQUIRE(isccc_alist_alistp(alist));
105 return (CDR(alist));
108 isccc_sexpr_t *
109 isccc_alist_assq(isccc_sexpr_t *alist, const char *key)
111 isccc_sexpr_t *car, *caar;
113 REQUIRE(isccc_alist_alistp(alist));
116 * Skip alist type tag.
118 alist = CDR(alist);
120 while (alist != NULL) {
121 INSIST(alist->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
122 car = CAR(alist);
123 INSIST(car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
124 caar = CAR(car);
125 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
126 strcmp(caar->value.as_string, key) == 0)
127 return (car);
128 alist = CDR(alist);
131 return (NULL);
134 void
135 isccc_alist_delete(isccc_sexpr_t *alist, const char *key)
137 isccc_sexpr_t *car, *caar, *rest, *prev;
139 REQUIRE(isccc_alist_alistp(alist));
141 prev = alist;
142 rest = CDR(alist);
143 while (rest != NULL) {
144 INSIST(rest->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
145 car = CAR(rest);
146 INSIST(car != NULL && car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
147 caar = CAR(car);
148 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
149 strcmp(caar->value.as_string, key) == 0) {
150 CDR(prev) = CDR(rest);
151 CDR(rest) = NULL;
152 isccc_sexpr_free(&rest);
153 break;
155 prev = rest;
156 rest = CDR(rest);
160 isccc_sexpr_t *
161 isccc_alist_define(isccc_sexpr_t *alist, const char *key, isccc_sexpr_t *value)
163 isccc_sexpr_t *kv, *k, *elt;
165 kv = isccc_alist_assq(alist, key);
166 if (kv == NULL) {
168 * New association.
170 k = isccc_sexpr_fromstring(key);
171 if (k == NULL)
172 return (NULL);
173 kv = isccc_sexpr_cons(k, value);
174 if (kv == NULL) {
175 isccc_sexpr_free(&kv);
176 return (NULL);
178 elt = isccc_sexpr_addtolist(&alist, kv);
179 if (elt == NULL) {
180 isccc_sexpr_free(&kv);
181 return (NULL);
183 } else {
185 * We've already got an entry for this key. Replace it.
187 isccc_sexpr_free(&CDR(kv));
188 CDR(kv) = value;
191 return (kv);
194 isccc_sexpr_t *
195 isccc_alist_definestring(isccc_sexpr_t *alist, const char *key, const char *str)
197 isccc_sexpr_t *v, *kv;
199 v = isccc_sexpr_fromstring(str);
200 if (v == NULL)
201 return (NULL);
202 kv = isccc_alist_define(alist, key, v);
203 if (kv == NULL)
204 isccc_sexpr_free(&v);
206 return (kv);
209 isccc_sexpr_t *
210 isccc_alist_definebinary(isccc_sexpr_t *alist, const char *key, isccc_region_t *r)
212 isccc_sexpr_t *v, *kv;
214 v = isccc_sexpr_frombinary(r);
215 if (v == NULL)
216 return (NULL);
217 kv = isccc_alist_define(alist, key, v);
218 if (kv == NULL)
219 isccc_sexpr_free(&v);
221 return (kv);
224 isccc_sexpr_t *
225 isccc_alist_lookup(isccc_sexpr_t *alist, const char *key)
227 isccc_sexpr_t *kv;
229 kv = isccc_alist_assq(alist, key);
230 if (kv != NULL)
231 return (CDR(kv));
232 return (NULL);
235 isc_result_t
236 isccc_alist_lookupstring(isccc_sexpr_t *alist, const char *key, char **strp)
238 isccc_sexpr_t *kv, *v;
240 kv = isccc_alist_assq(alist, key);
241 if (kv != NULL) {
242 v = CDR(kv);
243 if (isccc_sexpr_stringp(v)) {
244 if (strp != NULL)
245 *strp = isccc_sexpr_tostring(v);
246 return (ISC_R_SUCCESS);
247 } else
248 return (ISC_R_EXISTS);
251 return (ISC_R_NOTFOUND);
254 isc_result_t
255 isccc_alist_lookupbinary(isccc_sexpr_t *alist, const char *key, isccc_region_t **r)
257 isccc_sexpr_t *kv, *v;
259 kv = isccc_alist_assq(alist, key);
260 if (kv != NULL) {
261 v = CDR(kv);
262 if (isccc_sexpr_binaryp(v)) {
263 if (r != NULL)
264 *r = isccc_sexpr_tobinary(v);
265 return (ISC_R_SUCCESS);
266 } else
267 return (ISC_R_EXISTS);
270 return (ISC_R_NOTFOUND);
273 void
274 isccc_alist_prettyprint(isccc_sexpr_t *sexpr, unsigned int indent, FILE *stream)
276 isccc_sexpr_t *elt, *kv, *k, *v;
278 if (isccc_alist_alistp(sexpr)) {
279 fprintf(stream, "{\n");
280 indent += 4;
281 for (elt = isccc_alist_first(sexpr);
282 elt != NULL;
283 elt = CDR(elt)) {
284 kv = CAR(elt);
285 INSIST(isccc_sexpr_listp(kv));
286 k = CAR(kv);
287 v = CDR(kv);
288 INSIST(isccc_sexpr_stringp(k));
289 fprintf(stream, "%.*s%s => ", (int)indent, spaces,
290 isccc_sexpr_tostring(k));
291 isccc_alist_prettyprint(v, indent, stream);
292 if (CDR(elt) != NULL)
293 fprintf(stream, ",");
294 fprintf(stream, "\n");
296 indent -= 4;
297 fprintf(stream, "%.*s}", (int)indent, spaces);
298 } else if (isccc_sexpr_listp(sexpr)) {
299 fprintf(stream, "(\n");
300 indent += 4;
301 for (elt = sexpr;
302 elt != NULL;
303 elt = CDR(elt)) {
304 fprintf(stream, "%.*s", (int)indent, spaces);
305 isccc_alist_prettyprint(CAR(elt), indent, stream);
306 if (CDR(elt) != NULL)
307 fprintf(stream, ",");
308 fprintf(stream, "\n");
310 indent -= 4;
311 fprintf(stream, "%.*s)", (int)indent, spaces);
312 } else
313 isccc_sexpr_print(sexpr, stream);