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