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