2 * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 2001 Nominum, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
24 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
26 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 /* $Id: sexpr.c,v 1.9 2007/08/28 07:20:43 tbox Exp $ */
42 #include <isc/assertions.h>
43 #include <isccc/sexpr.h>
44 #include <isccc/util.h>
46 static isccc_sexpr_t sexpr_t = { ISCCC_SEXPRTYPE_T, { NULL } };
48 #define CAR(s) (s)->value.as_dottedpair.car
49 #define CDR(s) (s)->value.as_dottedpair.cdr
52 isccc_sexpr_cons(isccc_sexpr_t *car, isccc_sexpr_t *cdr)
56 sexpr = malloc(sizeof(*sexpr));
59 sexpr->type = ISCCC_SEXPRTYPE_DOTTEDPAIR;
67 isccc_sexpr_tconst(void)
73 isccc_sexpr_fromstring(const char *str)
77 sexpr = malloc(sizeof(*sexpr));
80 sexpr->type = ISCCC_SEXPRTYPE_STRING;
81 sexpr->value.as_string = strdup(str);
82 if (sexpr->value.as_string == NULL) {
91 isccc_sexpr_frombinary(const isccc_region_t *region)
94 unsigned int region_size;
96 sexpr = malloc(sizeof(*sexpr));
99 sexpr->type = ISCCC_SEXPRTYPE_BINARY;
100 region_size = REGION_SIZE(*region);
102 * We add an extra byte when we malloc so we can NUL terminate
103 * the binary data. This allows the caller to use it as a C
104 * string. It's up to the caller to ensure this is safe. We don't
105 * add 1 to the length of the binary region, because the NUL is
106 * not part of the binary data.
108 sexpr->value.as_region.rstart = malloc(region_size + 1);
109 if (sexpr->value.as_region.rstart == NULL) {
113 sexpr->value.as_region.rend = sexpr->value.as_region.rstart +
115 memcpy(sexpr->value.as_region.rstart, region->rstart, region_size);
119 sexpr->value.as_region.rstart[region_size] = '\0';
125 isccc_sexpr_free(isccc_sexpr_t **sexprp)
127 isccc_sexpr_t *sexpr;
133 switch (sexpr->type) {
134 case ISCCC_SEXPRTYPE_STRING:
135 free(sexpr->value.as_string);
137 case ISCCC_SEXPRTYPE_DOTTEDPAIR:
140 isccc_sexpr_free(&item);
143 isccc_sexpr_free(&item);
145 case ISCCC_SEXPRTYPE_BINARY:
146 free(sexpr->value.as_region.rstart);
155 printable(isccc_region_t *r)
160 while (curr != r->rend) {
170 isccc_sexpr_print(isccc_sexpr_t *sexpr, FILE *stream)
173 unsigned int size, i;
177 fprintf(stream, "nil");
181 switch (sexpr->type) {
182 case ISCCC_SEXPRTYPE_T:
183 fprintf(stream, "t");
185 case ISCCC_SEXPRTYPE_STRING:
186 fprintf(stream, "\"%s\"", sexpr->value.as_string);
188 case ISCCC_SEXPRTYPE_DOTTEDPAIR:
189 fprintf(stream, "(");
191 isccc_sexpr_print(CAR(sexpr), stream);
194 fprintf(stream, " ");
195 if (cdr->type != ISCCC_SEXPRTYPE_DOTTEDPAIR) {
196 fprintf(stream, ". ");
197 isccc_sexpr_print(cdr, stream);
202 } while (sexpr != NULL);
203 fprintf(stream, ")");
205 case ISCCC_SEXPRTYPE_BINARY:
206 size = REGION_SIZE(sexpr->value.as_region);
207 curr = sexpr->value.as_region.rstart;
208 if (printable(&sexpr->value.as_region)) {
209 fprintf(stream, "'%.*s'", (int)size, curr);
211 fprintf(stream, "0x");
212 for (i = 0; i < size; i++)
213 fprintf(stream, "%02x", *curr++);
222 isccc_sexpr_car(isccc_sexpr_t *list)
224 REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
230 isccc_sexpr_cdr(isccc_sexpr_t *list)
232 REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
238 isccc_sexpr_setcar(isccc_sexpr_t *pair, isccc_sexpr_t *car)
240 REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
246 isccc_sexpr_setcdr(isccc_sexpr_t *pair, isccc_sexpr_t *cdr)
248 REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
254 isccc_sexpr_addtolist(isccc_sexpr_t **l1p, isccc_sexpr_t *l2)
256 isccc_sexpr_t *last, *elt, *l1;
258 REQUIRE(l1p != NULL);
260 REQUIRE(l1 == NULL || l1->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
262 elt = isccc_sexpr_cons(l2, NULL);
269 for (last = l1; CDR(last) != NULL; last = CDR(last))
277 isccc_sexpr_listp(isccc_sexpr_t *sexpr)
279 if (sexpr == NULL || sexpr->type == ISCCC_SEXPRTYPE_DOTTEDPAIR)
285 isccc_sexpr_emptyp(isccc_sexpr_t *sexpr)
293 isccc_sexpr_stringp(isccc_sexpr_t *sexpr)
295 if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_STRING)
301 isccc_sexpr_binaryp(isccc_sexpr_t *sexpr)
303 if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY)
309 isccc_sexpr_tostring(isccc_sexpr_t *sexpr)
311 REQUIRE(sexpr != NULL &&
312 (sexpr->type == ISCCC_SEXPRTYPE_STRING ||
313 sexpr->type == ISCCC_SEXPRTYPE_BINARY));
315 if (sexpr->type == ISCCC_SEXPRTYPE_BINARY)
316 return ((char *)sexpr->value.as_region.rstart);
317 return (sexpr->value.as_string);
321 isccc_sexpr_tobinary(isccc_sexpr_t *sexpr)
323 REQUIRE(sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY);
324 return (&sexpr->value.as_region);