2 * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2001 Internet Software Consortium.
4 * Portions Copyright (C) 2001 Nominum, Inc.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* $Id: sexpr.c,v 1.2.2.1 2004/03/09 06:12:25 marka Exp $ */
27 #include <isc/assertions.h>
28 #include <isccc/sexpr.h>
29 #include <isccc/util.h>
31 static isccc_sexpr_t sexpr_t = { ISCCC_SEXPRTYPE_T, { NULL } };
33 #define CAR(s) (s)->value.as_dottedpair.car
34 #define CDR(s) (s)->value.as_dottedpair.cdr
37 isccc_sexpr_cons(isccc_sexpr_t *car, isccc_sexpr_t *cdr)
41 sexpr = malloc(sizeof *sexpr);
44 sexpr->type = ISCCC_SEXPRTYPE_DOTTEDPAIR;
52 isccc_sexpr_tconst(void)
58 isccc_sexpr_fromstring(const char *str)
62 sexpr = malloc(sizeof *sexpr);
65 sexpr->type = ISCCC_SEXPRTYPE_STRING;
66 sexpr->value.as_string = strdup(str);
67 if (sexpr->value.as_string == NULL) {
76 isccc_sexpr_frombinary(const isccc_region_t *region)
79 unsigned int region_size;
81 sexpr = malloc(sizeof *sexpr);
84 sexpr->type = ISCCC_SEXPRTYPE_BINARY;
85 region_size = REGION_SIZE(*region);
87 * We add an extra byte when we malloc so we can NUL terminate
88 * the binary data. This allows the caller to use it as a C
89 * string. It's up to the caller to ensure this is safe. We don't
90 * add 1 to the length of the binary region, because the NUL is
91 * not part of the binary data.
93 sexpr->value.as_region.rstart = malloc(region_size + 1);
94 if (sexpr->value.as_region.rstart == NULL) {
98 sexpr->value.as_region.rend = sexpr->value.as_region.rstart +
100 memcpy(sexpr->value.as_region.rstart, region->rstart, region_size);
104 sexpr->value.as_region.rstart[region_size] = '\0';
110 isccc_sexpr_free(isccc_sexpr_t **sexprp)
112 isccc_sexpr_t *sexpr;
118 switch (sexpr->type) {
119 case ISCCC_SEXPRTYPE_STRING:
120 free(sexpr->value.as_string);
122 case ISCCC_SEXPRTYPE_DOTTEDPAIR:
125 isccc_sexpr_free(&item);
128 isccc_sexpr_free(&item);
130 case ISCCC_SEXPRTYPE_BINARY:
131 free(sexpr->value.as_region.rstart);
140 printable(isccc_region_t *r)
145 while (curr != r->rend) {
155 isccc_sexpr_print(isccc_sexpr_t *sexpr, FILE *stream)
158 unsigned int size, i;
162 fprintf(stream, "nil");
166 switch (sexpr->type) {
167 case ISCCC_SEXPRTYPE_T:
168 fprintf(stream, "t");
170 case ISCCC_SEXPRTYPE_STRING:
171 fprintf(stream, "\"%s\"", sexpr->value.as_string);
173 case ISCCC_SEXPRTYPE_DOTTEDPAIR:
174 fprintf(stream, "(");
176 isccc_sexpr_print(CAR(sexpr), stream);
179 fprintf(stream, " ");
180 if (cdr->type != ISCCC_SEXPRTYPE_DOTTEDPAIR) {
181 fprintf(stream, ". ");
182 isccc_sexpr_print(cdr, stream);
187 } while (sexpr != NULL);
188 fprintf(stream, ")");
190 case ISCCC_SEXPRTYPE_BINARY:
191 size = REGION_SIZE(sexpr->value.as_region);
192 curr = sexpr->value.as_region.rstart;
193 if (printable(&sexpr->value.as_region)) {
194 fprintf(stream, "'%.*s'", (int)size, curr);
196 fprintf(stream, "0x");
197 for (i = 0; i < size; i++)
198 fprintf(stream, "%02x", *curr++);
207 isccc_sexpr_car(isccc_sexpr_t *list)
209 REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
215 isccc_sexpr_cdr(isccc_sexpr_t *list)
217 REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
223 isccc_sexpr_setcar(isccc_sexpr_t *pair, isccc_sexpr_t *car)
225 REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
231 isccc_sexpr_setcdr(isccc_sexpr_t *pair, isccc_sexpr_t *cdr)
233 REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
239 isccc_sexpr_addtolist(isccc_sexpr_t **l1p, isccc_sexpr_t *l2)
241 isccc_sexpr_t *last, *elt, *l1;
243 REQUIRE(l1p != NULL);
245 REQUIRE(l1 == NULL || l1->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
247 elt = isccc_sexpr_cons(l2, NULL);
254 for (last = l1; CDR(last) != NULL; last = CDR(last))
262 isccc_sexpr_listp(isccc_sexpr_t *sexpr)
264 if (sexpr == NULL || sexpr->type == ISCCC_SEXPRTYPE_DOTTEDPAIR)
270 isccc_sexpr_emptyp(isccc_sexpr_t *sexpr)
278 isccc_sexpr_stringp(isccc_sexpr_t *sexpr)
280 if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_STRING)
286 isccc_sexpr_binaryp(isccc_sexpr_t *sexpr)
288 if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY)
294 isccc_sexpr_tostring(isccc_sexpr_t *sexpr)
296 REQUIRE(sexpr != NULL &&
297 (sexpr->type == ISCCC_SEXPRTYPE_STRING ||
298 sexpr->type == ISCCC_SEXPRTYPE_BINARY));
300 if (sexpr->type == ISCCC_SEXPRTYPE_BINARY)
301 return ((char *)sexpr->value.as_region.rstart);
302 return (sexpr->value.as_string);
306 isccc_sexpr_tobinary(isccc_sexpr_t *sexpr)
308 REQUIRE(sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY);
309 return (&sexpr->value.as_region);