1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Portions Copyright (C) 2001, 2004, 2005, 2007, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
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: alist.c,v 1.8 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 <stdlib.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <string.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/alist.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/assertions.h>
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont#include <isc/print.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/result.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/sexpr.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isccc/util.h>
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 Wellington#define ALIST_TAG "*alist*"
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define MAX_INDENT 64
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
d70dac20d2b88e48fbf2606180d0ba6dcc4bc44fTinderbox Userstatic char spaces[MAX_INDENT + 1] =
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington " ";
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_create(void)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *alist, *tag;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington tag = isccc_sexpr_fromstring(ALIST_TAG);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (tag == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington alist = isccc_sexpr_cons(tag, NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (alist == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&tag);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (alist);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_alistp(isccc_sexpr_t *alist)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *car;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (alist == NULL || alist->type != ISCCC_SEXPRTYPE_DOTTEDPAIR)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington car = CAR(alist);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (car == NULL || car->type != ISCCC_SEXPRTYPE_STRING)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (strcmp(car->value.as_string, ALIST_TAG) != 0)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_emptyp(isccc_sexpr_t *alist)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(isccc_alist_alistp(alist));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (CDR(alist) == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TRUE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_first(isccc_sexpr_t *alist)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(isccc_alist_alistp(alist));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (CDR(alist));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_assq(isccc_sexpr_t *alist, const char *key)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *car, *caar;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(isccc_alist_alistp(alist));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * Skip alist type tag.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington alist = CDR(alist);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington while (alist != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(alist->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington car = CAR(alist);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington caar = CAR(car);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (caar->type == ISCCC_SEXPRTYPE_STRING &&
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington strcmp(caar->value.as_string, key) == 0)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (car);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington alist = CDR(alist);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_delete(isccc_sexpr_t *alist, const char *key)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *car, *caar, *rest, *prev;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(isccc_alist_alistp(alist));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington prev = alist;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rest = CDR(alist);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington while (rest != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(rest->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington car = CAR(rest);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(car != NULL && car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington caar = CAR(car);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (caar->type == ISCCC_SEXPRTYPE_STRING &&
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington strcmp(caar->value.as_string, key) == 0) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CDR(prev) = CDR(rest);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CDR(rest) = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&rest);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington break;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington prev = rest;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rest = CDR(rest);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_define(isccc_sexpr_t *alist, const char *key, isccc_sexpr_t *value)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *kv, *k, *elt;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = isccc_alist_assq(alist, key);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (kv == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * New association.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington k = isccc_sexpr_fromstring(key);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (k == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = isccc_sexpr_cons(k, value);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (kv == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington elt = isccc_sexpr_addtolist(&alist, kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (elt == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } else {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /*
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * We've already got an entry for this key. Replace it.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&CDR(kv));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington CDR(kv) = value;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_definestring(isccc_sexpr_t *alist, const char *key, const char *str)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *v, *kv;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington v = isccc_sexpr_fromstring(str);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (v == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = isccc_alist_define(alist, key, v);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (kv == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&v);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_definebinary(isccc_sexpr_t *alist, const char *key, isccc_region_t *r)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *v, *kv;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington v = isccc_sexpr_frombinary(r);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (v == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = isccc_alist_define(alist, key, v);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (kv == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_free(&v);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_sexpr_t *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_lookup(isccc_sexpr_t *alist, const char *key)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *kv;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = isccc_alist_assq(alist, key);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (kv != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (CDR(kv));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_result_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_lookupstring(isccc_sexpr_t *alist, const char *key, char **strp)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *kv, *v;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = isccc_alist_assq(alist, key);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (kv != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington v = CDR(kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (isccc_sexpr_stringp(v)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (strp != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *strp = isccc_sexpr_tostring(v);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_SUCCESS);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } else
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_EXISTS);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_NOTFOUND);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_result_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_lookupbinary(isccc_sexpr_t *alist, const char *key, isccc_region_t **r)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *kv, *v;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = isccc_alist_assq(alist, key);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (kv != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington v = CDR(kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (isccc_sexpr_binaryp(v)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (r != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *r = isccc_sexpr_tobinary(v);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_SUCCESS);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } else
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_EXISTS);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_NOTFOUND);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisccc_alist_prettyprint(isccc_sexpr_t *sexpr, unsigned int indent, FILE *stream)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_t *elt, *kv, *k, *v;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (isccc_alist_alistp(sexpr)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "{\n");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington indent += 4;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington for (elt = isccc_alist_first(sexpr);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington elt != NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington elt = CDR(elt)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington kv = CAR(elt);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(isccc_sexpr_listp(kv));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington k = CAR(kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington v = CDR(kv);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(isccc_sexpr_stringp(k));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "%.*s%s => ", (int)indent, spaces,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_tostring(k));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_alist_prettyprint(v, indent, stream);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (CDR(elt) != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, ",");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "\n");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington indent -= 4;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "%.*s}", (int)indent, spaces);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } else if (isccc_sexpr_listp(sexpr)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "(\n");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington indent += 4;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington for (elt = sexpr;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington elt != NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington elt = CDR(elt)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "%.*s", (int)indent, spaces);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_alist_prettyprint(CAR(elt), indent, stream);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (CDR(elt) != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, ",");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "\n");
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington indent -= 4;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington fprintf(stream, "%.*s)", (int)indent, spaces);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington } else
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isccc_sexpr_print(sexpr, stream);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}