Debian Codegraph @

doc/misc/cfg_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 /*! \file */
15
16 #include <errno.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19
20 #include <isc/mem.h>
21 #include <isc/string.h>
22 #include <isc/util.h>
23
24 #include <dns/log.h>
25
26 #include <isccfg/grammar.h>
27 #include <isccfg/namedconf.h>
28
29 static void
30 check_result(isc_result_t result, const char *format, ...) {
31 va_list args;
32
33 if (result == ISC_R_SUCCESS) {
34 return;
35 }
36
37 va_start(args, format);
38 vfprintf(stderr, format, args);
39 va_end(args);
40 fprintf(stderr, ": %s\n", isc_result_totext(result));
41 exit(EXIT_FAILURE);
42 }
43
44 static void
45 output(void *closure, const char *text, int textlen) {
46 UNUSED(closure);
47 (void)fwrite(text, 1, textlen, stdout);
48 }
49
50 static void
51 usage(void) {
52 fprintf(stderr, "usage: cfg_test --rndc|--named "
53 "[--grammar] [--zonegrammar] [--active] "
54 "[--memstats] conffile\n");
55 exit(EXIT_FAILURE);
56 }
57
58 int
59 main(int argc, char **argv) {
60 isc_result_t result;
61 isc_mem_t *mctx = NULL;
62 isc_log_t *lctx = NULL;
63 isc_logconfig_t *lcfg = NULL;
64 isc_logdestination_t destination;
65 cfg_parser_t *pctx = NULL;
66 cfg_obj_t *cfg = NULL;
67 cfg_type_t *type = NULL;
68 bool grammar = false;
69 bool memstats = false;
70 char *filename = NULL;
71 unsigned int zonetype = 0;
72 unsigned int pflags = 0;
73
74 isc_mem_create(&mctx);
75
76 isc_log_create(mctx, &lctx, &lcfg);
77 isc_log_setcontext(lctx);
78
79 /*
80 * Create and install the default channel.
81 */
82 destination.file.stream = stderr;
83 destination.file.name = NULL;
84 destination.file.versions = ISC_LOG_ROLLNEVER;
85 destination.file.maximum_size = 0;
86 isc_log_createchannel(lcfg, "_default", ISC_LOG_TOFILEDESC,
87 ISC_LOG_DYNAMIC, &destination, ISC_LOG_PRINTTIME);
88
89 result = isc_log_usechannel(lcfg, "_default", NULL, NULL);
90 check_result(result, "isc_log_usechannel()");
91
92 /*
93 * Set the initial debug level.
94 */
95 isc_log_setdebuglevel(lctx, 2);
96
97 if (argc < 3) {
98 usage();
99 }
100
101 while (argc > 1) {
102 if (strcmp(argv[1], "--active") == 0) {
103 pflags |= CFG_PRINTER_ACTIVEONLY;
104 } else if (strcmp(argv[1], "--grammar") == 0) {
105 grammar = true;
106 } else if (strcmp(argv[1], "--zonegrammar") == 0) {
107 argv++, argc--;
108 if (argc <= 1) {
109 usage();
110 }
111 if (strcmp(argv[1], "master") == 0 ||
112 strcmp(argv[1], "primary") == 0)
113 {
114 zonetype = CFG_ZONE_PRIMARY;
115 } else if (strcmp(argv[1], "slave") == 0 ||
116 strcmp(argv[1], "secondary") == 0)
117 {
118 zonetype = CFG_ZONE_SECONDARY;
119 } else if (strcmp(argv[1], "mirror") == 0) {
120 zonetype = CFG_ZONE_MIRROR;
121 } else if (strcmp(argv[1], "stub") == 0) {
122 zonetype = CFG_ZONE_STUB;
123 } else if (strcmp(argv[1], "static-stub") == 0) {
124 zonetype = CFG_ZONE_STATICSTUB;
125 } else if (strcmp(argv[1], "hint") == 0) {
126 zonetype = CFG_ZONE_HINT;
127 } else if (strcmp(argv[1], "forward") == 0) {
128 zonetype = CFG_ZONE_FORWARD;
129 } else if (strcmp(argv[1], "redirect") == 0) {
130 zonetype = CFG_ZONE_REDIRECT;
131 } else if (strcmp(argv[1], "in-view") == 0) {
132 zonetype = CFG_ZONE_INVIEW;
133 } else {
134 usage();
135 }
136 } else if (strcmp(argv[1], "--memstats") == 0) {
137 memstats = true;
138 } else if (strcmp(argv[1], "--named") == 0) {
139 type = &cfg_type_namedconf;
140 } else if (strcmp(argv[1], "--rndc") == 0) {
141 type = &cfg_type_rndcconf;
142 } else if (argv[1][0] == '-') {
143 usage();
144 } else {
145 filename = argv[1];
146 }
147 argv++, argc--;
148 }
149
150 if (grammar) {
151 if (type == NULL) {
152 usage();
153 }
154 cfg_print_grammar(type, pflags, output, NULL);
155 } else if (zonetype != 0) {
156 cfg_print_zonegrammar(zonetype, pflags, output, NULL);
157 } else {
158 if (type == NULL || filename == NULL) {
159 usage();
160 }
161 RUNTIME_CHECK(cfg_parser_create(mctx, lctx, &pctx) ==
162 ISC_R_SUCCESS);
163
164 result = cfg_parse_file(pctx, filename, type, &cfg);
165
166 fprintf(stderr, "read config: %s\n", isc_result_totext(result));
167
168 if (result != ISC_R_SUCCESS) {
169 exit(EXIT_FAILURE);
170 }
171
172 cfg_print(cfg, output, NULL);
173
174 cfg_obj_destroy(pctx, &cfg);
175
176 cfg_parser_destroy(&pctx);
177 }
178
179 isc_log_destroy(&lctx);
180 if (memstats) {
181 isc_mem_stats(mctx, stderr);
182 }
183 isc_mem_destroy(&mctx);
184
185 fflush(stdout);
186 if (ferror(stdout)) {
187 fprintf(stderr, "write error\n");
188 return 1;
189 }
190
191 return 0;
192 }