etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / cfg_test.c
blob435879e8de359989d6c0deada250bf9d74d818b2
1 /* $NetBSD: cfg_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
3 /*
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 */
22 /*! \file */
24 #include <config.h>
26 #include <errno.h>
27 #include <stdlib.h>
29 #include <isc/mem.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
33 #include <isccfg/namedconf.h>
35 #include <dns/log.h>
37 static void
38 check_result(isc_result_t result, const char *format, ...) {
39 va_list args;
41 if (result == ISC_R_SUCCESS)
42 return;
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);
51 static void
52 output(void *closure, const char *text, int textlen) {
53 UNUSED(closure);
54 (void) fwrite(text, 1, textlen, stdout);
57 static void
58 usage(void) {
59 fprintf(stderr, "usage: cfg_test --rndc|--named "
60 "[--grammar] [--memstats] conffile\n");
61 exit(1);
64 int
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;
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",
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()");
100 * Set the initial debug level.
102 isc_log_setdebuglevel(lctx, 2);
104 if (argc < 3)
105 usage();
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];
121 argv++, argc--;
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);
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)
138 exit(1);
140 cfg_print(cfg, output, NULL);
142 cfg_obj_destroy(pctx, &cfg);
144 cfg_parser_destroy(&pctx);
147 isc_log_destroy(&lctx);
148 if (memstats)
149 isc_mem_stats(mctx, stderr);
150 isc_mem_destroy(&mctx);
152 fflush(stdout);
153 if (ferror(stdout)) {
154 fprintf(stderr, "write error\n");
155 return (1);
156 } else
157 return (0);