sexpr.c revision 271c4c7ffafeb0bda21278af6cac4535c0193f18
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs/*
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * Portions Copyright (C) 2001 Internet Software Consortium.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * Permission to use, copy, modify, and/or distribute this software for any
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * purpose with or without fee is hereby granted, provided that the above
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * copyright notice and this permission notice appear in all copies.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * Portions Copyright (C) 2001 Nominum, Inc.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * Permission to use, copy, modify, and/or distribute this software for any
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * purpose with or without fee is hereby granted, provided that the above
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * copyright notice and this permission notice appear in all copies.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs */
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs/* $Id: sexpr.c,v 1.9 2007/08/28 07:20:43 tbox Exp $ */
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs/*! \file */
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#include <config.h>
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#include <ctype.h>
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#include <stdlib.h>
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#include <string.h>
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#include <isc/assertions.h>
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#include <isccc/sexpr.h>
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#include <isccc/util.h>
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsstatic isccc_sexpr_t sexpr_t = { ISCCC_SEXPRTYPE_T, { NULL } };
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#define CAR(s) (s)->value.as_dottedpair.car
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs#define CDR(s) (s)->value.as_dottedpair.cdr
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_t *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_cons(isccc_sexpr_t *car, isccc_sexpr_t *cdr)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_t *sexpr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr = malloc(sizeof(*sexpr));
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (sexpr == NULL)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (NULL);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr->type = ISCCC_SEXPRTYPE_DOTTEDPAIR;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs CAR(sexpr) = car;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs CDR(sexpr) = cdr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs}
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_t *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_tconst(void)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (&sexpr_t);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs}
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_t *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_fromstring(const char *str)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_t *sexpr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr = malloc(sizeof(*sexpr));
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (sexpr == NULL)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (NULL);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr->type = ISCCC_SEXPRTYPE_STRING;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr->value.as_string = strdup(str);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (sexpr->value.as_string == NULL) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs free(sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (NULL);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs}
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_t *
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_frombinary(const isccc_region_t *region)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_t *sexpr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs unsigned int region_size;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr = malloc(sizeof(*sexpr));
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (sexpr == NULL)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (NULL);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr->type = ISCCC_SEXPRTYPE_BINARY;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs region_size = REGION_SIZE(*region);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs /*
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * We add an extra byte when we malloc so we can NUL terminate
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * the binary data. This allows the caller to use it as a C
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * string. It's up to the caller to ensure this is safe. We don't
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * add 1 to the length of the binary region, because the NUL is
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * not part of the binary data.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs */
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr->value.as_region.rstart = malloc(region_size + 1);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (sexpr->value.as_region.rstart == NULL) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs free(sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (NULL);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr->value.as_region.rend = sexpr->value.as_region.rstart +
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs region_size;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs memcpy(sexpr->value.as_region.rstart, region->rstart, region_size);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs /*
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs * NUL terminate.
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs */
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr->value.as_region.rstart[region_size] = '\0';
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs}
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsvoid
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_free(isccc_sexpr_t **sexprp)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_t *sexpr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_t *item;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr = *sexprp;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (sexpr == NULL)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs switch (sexpr->type) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case ISCCC_SEXPRTYPE_STRING:
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs free(sexpr->value.as_string);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs break;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case ISCCC_SEXPRTYPE_DOTTEDPAIR:
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs item = CAR(sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (item != NULL)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_free(&item);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs item = CDR(sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (item != NULL)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_free(&item);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs break;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case ISCCC_SEXPRTYPE_BINARY:
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs free(sexpr->value.as_region.rstart);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs break;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs free(sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs *sexprp = NULL;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs}
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsstatic isc_boolean_t
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsprintable(isccc_region_t *r)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs unsigned char *curr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs curr = r->rstart;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs while (curr != r->rend) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (!isprint(*curr))
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (ISC_FALSE);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs curr++;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return (ISC_TRUE);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs}
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsvoid
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacsisccc_sexpr_print(isccc_sexpr_t *sexpr, FILE *stream)
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs{
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_t *cdr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs unsigned int size, i;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs unsigned char *curr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (sexpr == NULL) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, "nil");
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs return;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs switch (sexpr->type) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case ISCCC_SEXPRTYPE_T:
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, "t");
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs break;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case ISCCC_SEXPRTYPE_STRING:
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, "\"%s\"", sexpr->value.as_string);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs break;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case ISCCC_SEXPRTYPE_DOTTEDPAIR:
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, "(");
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs do {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_print(CAR(sexpr), stream);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs cdr = CDR(sexpr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (cdr != NULL) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, " ");
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (cdr->type != ISCCC_SEXPRTYPE_DOTTEDPAIR) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, ". ");
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs isccc_sexpr_print(cdr, stream);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs cdr = NULL;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs }
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs sexpr = cdr;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs } while (sexpr != NULL);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, ")");
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs break;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs case ISCCC_SEXPRTYPE_BINARY:
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs size = REGION_SIZE(sexpr->value.as_region);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs curr = sexpr->value.as_region.rstart;
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs if (printable(&sexpr->value.as_region)) {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, "'%.*s'", (int)size, curr);
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs } else {
1bbfd08d1d3f7f7ce1f775d469063cbedb6c26cdMareks Malnacs fprintf(stream, "0x");
for (i = 0; i < size; i++)
fprintf(stream, "%02x", *curr++);
}
break;
default:
INSIST(0);
}
}
isccc_sexpr_t *
isccc_sexpr_car(isccc_sexpr_t *list)
{
REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
return (CAR(list));
}
isccc_sexpr_t *
isccc_sexpr_cdr(isccc_sexpr_t *list)
{
REQUIRE(list->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
return (CDR(list));
}
void
isccc_sexpr_setcar(isccc_sexpr_t *pair, isccc_sexpr_t *car)
{
REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
CAR(pair) = car;
}
void
isccc_sexpr_setcdr(isccc_sexpr_t *pair, isccc_sexpr_t *cdr)
{
REQUIRE(pair->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
CDR(pair) = cdr;
}
isccc_sexpr_t *
isccc_sexpr_addtolist(isccc_sexpr_t **l1p, isccc_sexpr_t *l2)
{
isccc_sexpr_t *last, *elt, *l1;
REQUIRE(l1p != NULL);
l1 = *l1p;
REQUIRE(l1 == NULL || l1->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
elt = isccc_sexpr_cons(l2, NULL);
if (elt == NULL)
return (NULL);
if (l1 == NULL) {
*l1p = elt;
return (elt);
}
for (last = l1; CDR(last) != NULL; last = CDR(last))
/* Nothing */;
CDR(last) = elt;
return (elt);
}
isc_boolean_t
isccc_sexpr_listp(isccc_sexpr_t *sexpr)
{
if (sexpr == NULL || sexpr->type == ISCCC_SEXPRTYPE_DOTTEDPAIR)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
isccc_sexpr_emptyp(isccc_sexpr_t *sexpr)
{
if (sexpr == NULL)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
isccc_sexpr_stringp(isccc_sexpr_t *sexpr)
{
if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_STRING)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
isccc_sexpr_binaryp(isccc_sexpr_t *sexpr)
{
if (sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY)
return (ISC_TRUE);
return (ISC_FALSE);
}
char *
isccc_sexpr_tostring(isccc_sexpr_t *sexpr)
{
REQUIRE(sexpr != NULL &&
(sexpr->type == ISCCC_SEXPRTYPE_STRING ||
sexpr->type == ISCCC_SEXPRTYPE_BINARY));
if (sexpr->type == ISCCC_SEXPRTYPE_BINARY)
return ((char *)sexpr->value.as_region.rstart);
return (sexpr->value.as_string);
}
isccc_region_t *
isccc_sexpr_tobinary(isccc_sexpr_t *sexpr)
{
REQUIRE(sexpr != NULL && sexpr->type == ISCCC_SEXPRTYPE_BINARY);
return (&sexpr->value.as_region);
}