xref: /minix3/external/bsd/bind/dist/bin/tests/cfg_test.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: cfg_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009-2011  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2001, 2002  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: cfg_test.c,v 1.25 2011/09/05 23:46:54 tbox Exp  */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <errno.h>
27 #include <stdlib.h>
28 
29 #include <isc/mem.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
32 
33 #include <isccfg/namedconf.h>
34 
35 #include <dns/log.h>
36 
37 static void
check_result(isc_result_t result,const char * format,...)38 check_result(isc_result_t result, const char *format, ...) {
39 	va_list args;
40 
41 	if (result == ISC_R_SUCCESS)
42 		return;
43 
44 	va_start(args, format);
45 	vfprintf(stderr, format, args);
46 	va_end(args);
47 	fprintf(stderr, ": %s\n", isc_result_totext(result));
48 	exit(1);
49 }
50 
51 static void
output(void * closure,const char * text,int textlen)52 output(void *closure, const char *text, int textlen) {
53 	UNUSED(closure);
54 	(void) fwrite(text, 1, textlen, stdout);
55 }
56 
57 static void
usage(void)58 usage(void) {
59 	fprintf(stderr, "usage: cfg_test --rndc|--named "
60 		"[--grammar] [--memstats] conffile\n");
61 	exit(1);
62 }
63 
64 int
main(int argc,char ** argv)65 main(int argc, char **argv) {
66 	isc_result_t result;
67 	isc_mem_t *mctx = NULL;
68 	isc_log_t *lctx = NULL;
69 	isc_logconfig_t *lcfg = NULL;
70 	isc_logdestination_t destination;
71 	cfg_parser_t *pctx = NULL;
72 	cfg_obj_t *cfg = NULL;
73 	cfg_type_t *type = NULL;
74 	isc_boolean_t grammar = ISC_FALSE;
75 	isc_boolean_t memstats = ISC_FALSE;
76 	char *filename = NULL;
77 
78 	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
79 
80 	result = isc_log_create(mctx, &lctx, &lcfg);
81 	check_result(result, "isc_log_create()");
82 	isc_log_setcontext(lctx);
83 
84 	/*
85 	 * Create and install the default channel.
86 	 */
87 	destination.file.stream = stderr;
88 	destination.file.name = NULL;
89 	destination.file.versions = ISC_LOG_ROLLNEVER;
90 	destination.file.maximum_size = 0;
91 	result = isc_log_createchannel(lcfg, "_default",
92 				       ISC_LOG_TOFILEDESC,
93 				       ISC_LOG_DYNAMIC,
94 				       &destination, ISC_LOG_PRINTTIME);
95 	check_result(result, "isc_log_createchannel()");
96 	result = isc_log_usechannel(lcfg, "_default", NULL, NULL);
97 	check_result(result, "isc_log_usechannel()");
98 
99 	/*
100 	 * Set the initial debug level.
101 	 */
102 	isc_log_setdebuglevel(lctx, 2);
103 
104 	if (argc < 3)
105 		usage();
106 
107 	while (argc > 1) {
108 		if (strcmp(argv[1], "--grammar") == 0) {
109 			grammar = ISC_TRUE;
110 		} else if (strcmp(argv[1], "--memstats") == 0) {
111 			memstats = ISC_TRUE;
112 		} else if (strcmp(argv[1], "--named") == 0) {
113 			type = &cfg_type_namedconf;
114 		} else if (strcmp(argv[1], "--rndc") == 0) {
115 			type = &cfg_type_rndcconf;
116 		} else if (argv[1][0] == '-') {
117 			usage();
118 		} else {
119 			filename = argv[1];
120 		}
121 		argv++, argc--;
122 	}
123 
124 	if (grammar) {
125 		if (type == NULL)
126 			usage();
127 		cfg_print_grammar(type, output, NULL);
128 	} else {
129 		if (type == NULL || filename == NULL)
130 			usage();
131 		RUNTIME_CHECK(cfg_parser_create(mctx, lctx, &pctx) == ISC_R_SUCCESS);
132 
133 		result = cfg_parse_file(pctx, filename, type, &cfg);
134 
135 		fprintf(stderr, "read config: %s\n", isc_result_totext(result));
136 
137 		if (result != ISC_R_SUCCESS)
138 			exit(1);
139 
140 		cfg_print(cfg, output, NULL);
141 
142 		cfg_obj_destroy(pctx, &cfg);
143 
144 		cfg_parser_destroy(&pctx);
145 	}
146 
147 	isc_log_destroy(&lctx);
148 	if (memstats)
149 		isc_mem_stats(mctx, stderr);
150 	isc_mem_destroy(&mctx);
151 
152 	fflush(stdout);
153 	if (ferror(stdout)) {
154 		fprintf(stderr, "write error\n");
155 		return (1);
156 	} else
157 		return (0);
158 }
159