1 /* $NetBSD: cfg_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009-2011 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2001, 2002 Internet Software Consortium.
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.
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.
20 /* Id: cfg_test.c,v 1.25 2011/09/05 23:46:54 tbox Exp */
30 #include <isc/string.h>
33 #include <isccfg/namedconf.h>
38 check_result(isc_result_t result
, const char *format
, ...) {
41 if (result
== ISC_R_SUCCESS
)
44 va_start(args
, format
);
45 vfprintf(stderr
, format
, args
);
47 fprintf(stderr
, ": %s\n", isc_result_totext(result
));
52 output(void *closure
, const char *text
, int textlen
) {
54 (void) fwrite(text
, 1, textlen
, stdout
);
59 fprintf(stderr
, "usage: cfg_test --rndc|--named "
60 "[--grammar] [--memstats] conffile\n");
65 main(int argc
, char **argv
) {
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
;
78 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
80 result
= isc_log_create(mctx
, &lctx
, &lcfg
);
81 check_result(result
, "isc_log_create()");
82 isc_log_setcontext(lctx
);
85 * Create and install the default channel.
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",
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()");
100 * Set the initial debug level.
102 isc_log_setdebuglevel(lctx
, 2);
108 if (strcmp(argv
[1], "--grammar") == 0) {
110 } else if (strcmp(argv
[1], "--memstats") == 0) {
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] == '-') {
127 cfg_print_grammar(type
, output
, NULL
);
129 if (type
== NULL
|| filename
== NULL
)
131 RUNTIME_CHECK(cfg_parser_create(mctx
, lctx
, &pctx
) == ISC_R_SUCCESS
);
133 result
= cfg_parse_file(pctx
, filename
, type
, &cfg
);
135 fprintf(stderr
, "read config: %s\n", isc_result_totext(result
));
137 if (result
!= ISC_R_SUCCESS
)
140 cfg_print(cfg
, output
, NULL
);
142 cfg_obj_destroy(pctx
, &cfg
);
144 cfg_parser_destroy(&pctx
);
147 isc_log_destroy(&lctx
);
149 isc_mem_stats(mctx
, stderr
);
150 isc_mem_destroy(&mctx
);
153 if (ferror(stdout
)) {
154 fprintf(stderr
, "write error\n");