1/*	$NetBSD$	*/
2
3/*
4 * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: sym_test.c,v 1.28 2007/06/19 23:46:59 tbox Exp  */
21
22#include <config.h>
23
24#include <string.h>
25
26#include <isc/commandline.h>
27#include <isc/mem.h>
28#include <isc/symtab.h>
29#include <isc/util.h>
30
31isc_mem_t *mctx;
32isc_symtab_t *st;
33
34static void
35undefine_action(char *key, unsigned int type, isc_symvalue_t value, void *arg)
36{
37	UNUSED(arg);
38
39	INSIST(type == 1);
40	isc_mem_free(mctx, key);
41	isc_mem_free(mctx, value.as_pointer);
42}
43
44int
45main(int argc, char *argv[]) {
46	char s[1000], *cp, *key;
47	size_t len;
48	isc_result_t result;
49	isc_symvalue_t value;
50	int trace = 0;
51	int c;
52	isc_symexists_t exists_policy = isc_symexists_reject;
53	isc_boolean_t case_sensitive = ISC_FALSE;
54
55	while ((c = isc_commandline_parse(argc, argv, "tarc")) != -1) {
56		switch (c) {
57		case 't':
58			trace = 1;
59			break;
60		case 'a':
61			exists_policy = isc_symexists_add;
62			break;
63		case 'r':
64			exists_policy = isc_symexists_replace;
65			break;
66		case 'c':
67			case_sensitive = ISC_TRUE;
68			break;
69		}
70	}
71
72	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
73	RUNTIME_CHECK(isc_symtab_create(mctx, 691, undefine_action, NULL,
74					case_sensitive, &st) == ISC_R_SUCCESS);
75
76	while (fgets(s, sizeof(s), stdin) != NULL) {
77		len = strlen(s);
78		if (len > 0U && s[len - 1] == '\n') {
79			s[len - 1] = '\0';
80			len--;
81		}
82
83		cp = s;
84
85		if (cp[0] == '!') {
86			cp++;
87			result = isc_symtab_undefine(st, cp, 1);
88			if (trace || result != ISC_R_SUCCESS)
89				printf("undefine('%s'): %s\n", cp,
90				       isc_result_totext(result));
91		} else {
92			key = cp;
93			while (*cp != '\0' && *cp != ' ' && *cp != '\t')
94				cp++;
95			if (*cp == '\0') {
96				result = isc_symtab_lookup(st, key, 0, &value);
97				if (trace || result != ISC_R_SUCCESS) {
98					printf("lookup('%s'): %s", key,
99					       isc_result_totext(result));
100					if (result == ISC_R_SUCCESS) {
101						cp = value.as_pointer;
102						printf(", value == '%s'", cp);
103					}
104					printf("\n");
105				}
106			} else {
107				*cp++ = '\0';
108				key = isc_mem_strdup(mctx, key);
109				value.as_pointer = isc_mem_strdup(mctx, cp);
110				result = isc_symtab_define(st, key, 1, value,
111							   exists_policy);
112				if (trace || result != ISC_R_SUCCESS) {
113					printf("define('%s', '%s'): %s\n",
114					       key, cp,
115					       isc_result_totext(result));
116					if (result != ISC_R_SUCCESS)
117						undefine_action(key, 1,
118							value, NULL);
119				}
120			}
121		}
122	}
123
124	isc_symtab_destroy(&st);
125	isc_mem_stats(mctx, stdout);
126	isc_mem_destroy(&mctx);
127
128	return (0);
129}
130