sexpr.c revision 431a83fb29482c5170b3e4026e59bb14849a6707
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington/*
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User * Portions Copyright (C) 2004, 2005, 2007, 2014 Internet Systems Consortium, Inc. ("ISC")
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * Portions Copyright (C) 2001 Internet Software Consortium.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater *
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * copyright notice and this permission notice appear in all copies.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater *
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater *
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * Portions Copyright (C) 2001 Nominum, Inc.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *
271c4c7ffafeb0bda21278af6cac4535c0193f18Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * purpose with or without fee is hereby granted, provided that the above
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * copyright notice and this permission notice appear in all copies.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
271c4c7ffafeb0bda21278af6cac4535c0193f18Automatic Updater/* $Id: sexpr.c,v 1.9 2007/08/28 07:20:43 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <config.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <ctype.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <stdlib.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <string.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/assertions.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/sexpr.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/util.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isccc_sexpr_t sexpr_t = { ISCCC_SEXPRTYPE_T, { NULL } };
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define CAR(s) (s)->value.as_dottedpair.car
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define CDR(s) (s)->value.as_dottedpair.cdr
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_cons(isccc_sexpr_t *car, isccc_sexpr_t *cdr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *sexpr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson sexpr = malloc(sizeof(*sexpr));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->type = ISCCC_SEXPRTYPE_DOTTEDPAIR;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CAR(sexpr) = car;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CDR(sexpr) = cdr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_tconst(void)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (&sexpr_t);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_fromstring(const char *str)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *sexpr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson sexpr = malloc(sizeof(*sexpr));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->type = ISCCC_SEXPRTYPE_STRING;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->value.as_string = strdup(str);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr->value.as_string == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington free(sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_frombinary(const isccc_region_t *region)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *sexpr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int region_size;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson sexpr = malloc(sizeof(*sexpr));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->type = ISCCC_SEXPRTYPE_BINARY;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington region_size = REGION_SIZE(*region);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * We add an extra byte when we malloc so we can NUL terminate
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * the binary data. This allows the caller to use it as a C
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * string. It's up to the caller to ensure this is safe. We don't
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * add 1 to the length of the binary region, because the NUL is
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * not part of the binary data.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->value.as_region.rstart = malloc(region_size + 1);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr->value.as_region.rstart == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington free(sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->value.as_region.rend = sexpr->value.as_region.rstart +
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington region_size;
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(sexpr->value.as_region.rstart, region->rstart, region_size);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * NUL terminate.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->value.as_region.rstart[region_size] = '\0';
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_free(isccc_sexpr_t **sexprp)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *sexpr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *item;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr = *sexprp;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington switch (sexpr->type) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case ISCCC_SEXPRTYPE_STRING:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington free(sexpr->value.as_string);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case ISCCC_SEXPRTYPE_DOTTEDPAIR:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington item = CAR(sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (item != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&item);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington item = CDR(sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (item != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&item);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case ISCCC_SEXPRTYPE_BINARY:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington free(sexpr->value.as_region.rstart);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington free(sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *sexprp = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic isc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonprintable(isccc_region_t *r)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned char *curr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington curr = r->rstart;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington while (curr != r->rend) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (!isprint(*curr))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington curr++;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_print(isccc_sexpr_t *sexpr, FILE *stream)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *cdr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int size, i;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned char *curr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "nil");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington switch (sexpr->type) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case ISCCC_SEXPRTYPE_T:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "t");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case ISCCC_SEXPRTYPE_STRING:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "\"%s\"", sexpr->value.as_string);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case ISCCC_SEXPRTYPE_DOTTEDPAIR:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "(");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington do {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_print(CAR(sexpr), stream);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington cdr = CDR(sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (cdr != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, " ");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (cdr->type != ISCCC_SEXPRTYPE_DOTTEDPAIR) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, ". ");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_print(cdr, stream);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington cdr = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr = cdr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } while (sexpr != NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, ")");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington case ISCCC_SEXPRTYPE_BINARY:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington size = REGION_SIZE(sexpr->value.as_region);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington curr = sexpr->value.as_region.rstart;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (printable(&sexpr->value.as_region)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "'%.*s'", (int)size, curr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } else {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "0x");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington for (i = 0; i < size; i++)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "%02x", *curr++);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington default:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(0);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_car(isccc_sexpr_t *list)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (CAR(list));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_cdr(isccc_sexpr_t *list)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (CDR(list));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_setcar(isccc_sexpr_t *pair, isccc_sexpr_t *car)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CAR(pair) = car;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_setcdr(isccc_sexpr_t *pair, isccc_sexpr_t *cdr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CDR(pair) = cdr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_addtolist(isccc_sexpr_t **l1p, isccc_sexpr_t *l2)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *last, *elt, *l1;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(l1p != NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington l1 = *l1p;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(l1 == NULL || l1->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington elt = isccc_sexpr_cons(l2, NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (elt == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (l1 == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *l1p = elt;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (elt);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington for (last = l1; CDR(last) != NULL; last = CDR(last))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /* Nothing */;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CDR(last) = elt;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (elt);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_listp(isccc_sexpr_t *sexpr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL || sexpr->type == ISCCC_SEXPRTYPE_DOTTEDPAIR)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_emptyp(isccc_sexpr_t *sexpr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_stringp(isccc_sexpr_t *sexpr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_STRING)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_binaryp(isccc_sexpr_t *sexpr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonchar *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_tostring(isccc_sexpr_t *sexpr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(sexpr != NULL &&
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington (sexpr->type == ISCCC_SEXPRTYPE_STRING ||
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington sexpr->type == ISCCC_SEXPRTYPE_BINARY));
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (sexpr->type == ISCCC_SEXPRTYPE_BINARY)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return ((char *)sexpr->value.as_region.rstart);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (sexpr->value.as_string);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_region_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_tobinary(isccc_sexpr_t *sexpr)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (&sexpr->value.as_region);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}