2001-02-15 05:15:27 +00:00
|
|
|
/*
|
2010-01-11 23:48:37 +00:00
|
|
|
* Copyright (C) 2004, 2005, 2007, 2009, 2010 Internet Systems Consortium, Inc. ("ISC")
|
2002-02-20 03:35:59 +00:00
|
|
|
* Copyright (C) 2001, 2002 Internet Software Consortium.
|
2001-02-15 05:15:27 +00:00
|
|
|
*
|
2007-06-18 23:47:57 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2001-02-15 05:15:27 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
2001-02-15 05:15:27 +00:00
|
|
|
*/
|
|
|
|
|
2010-01-11 23:48:37 +00:00
|
|
|
/* $Id: cfg_test.c,v 1.23 2010/01/11 23:48:37 tbox Exp $ */
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2001-02-15 05:15:27 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2002-01-04 02:32:16 +00:00
|
|
|
#include <isccfg/namedconf.h>
|
2001-02-15 05:15:27 +00:00
|
|
|
|
|
|
|
#include <dns/log.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_result(isc_result_t result, const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(stderr, format, args);
|
|
|
|
va_end(args);
|
|
|
|
fprintf(stderr, ": %s\n", isc_result_totext(result));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
output(void *closure, const char *text, int textlen) {
|
|
|
|
UNUSED(closure);
|
2010-01-11 10:49:14 +00:00
|
|
|
(void) isc_util_fwrite(text, 1, textlen, stdout);
|
2001-02-15 05:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-10-22 23:50:31 +00:00
|
|
|
usage(void) {
|
2001-07-23 18:57:53 +00:00
|
|
|
fprintf(stderr, "usage: cfg_test --rndc|--named "
|
|
|
|
"[--grammar] [--memstats] conffile\n");
|
2001-02-15 05:15:27 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv) {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_mem_t *mctx = NULL;
|
|
|
|
isc_log_t *lctx = NULL;
|
|
|
|
isc_logconfig_t *lcfg = NULL;
|
|
|
|
isc_logdestination_t destination;
|
|
|
|
cfg_parser_t *pctx = NULL;
|
|
|
|
cfg_obj_t *cfg = NULL;
|
2001-05-31 11:00:40 +00:00
|
|
|
cfg_type_t *type = NULL;
|
2001-06-29 18:36:13 +00:00
|
|
|
isc_boolean_t grammar = ISC_FALSE;
|
2009-03-02 23:47:43 +00:00
|
|
|
isc_boolean_t memstats = ISC_FALSE;
|
2001-06-29 18:36:13 +00:00
|
|
|
char *filename = NULL;
|
2001-02-15 05:15:27 +00:00
|
|
|
|
|
|
|
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
result = isc_log_create(mctx, &lctx, &lcfg);
|
|
|
|
check_result(result, "isc_log_create()");
|
|
|
|
isc_log_setcontext(lctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create and install the default channel.
|
|
|
|
*/
|
|
|
|
destination.file.stream = stderr;
|
|
|
|
destination.file.name = NULL;
|
|
|
|
destination.file.versions = ISC_LOG_ROLLNEVER;
|
|
|
|
destination.file.maximum_size = 0;
|
|
|
|
result = isc_log_createchannel(lcfg, "_default",
|
|
|
|
ISC_LOG_TOFILEDESC,
|
|
|
|
ISC_LOG_DYNAMIC,
|
|
|
|
&destination, ISC_LOG_PRINTTIME);
|
|
|
|
check_result(result, "isc_log_createchannel()");
|
|
|
|
result = isc_log_usechannel(lcfg, "_default", NULL, NULL);
|
|
|
|
check_result(result, "isc_log_usechannel()");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the initial debug level.
|
|
|
|
*/
|
|
|
|
isc_log_setdebuglevel(lctx, 2);
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
usage();
|
|
|
|
|
2001-06-29 18:36:13 +00:00
|
|
|
while (argc > 1) {
|
|
|
|
if (strcmp(argv[1], "--grammar") == 0) {
|
|
|
|
grammar = ISC_TRUE;
|
|
|
|
} else if (strcmp(argv[1], "--memstats") == 0) {
|
|
|
|
memstats = ISC_TRUE;
|
|
|
|
} else if (strcmp(argv[1], "--named") == 0) {
|
|
|
|
type = &cfg_type_namedconf;
|
|
|
|
} else if (strcmp(argv[1], "--rndc") == 0) {
|
|
|
|
type = &cfg_type_rndcconf;
|
|
|
|
} else if (argv[1][0] == '-') {
|
|
|
|
usage();
|
|
|
|
} else {
|
|
|
|
filename = argv[1];
|
|
|
|
}
|
2009-03-02 23:47:43 +00:00
|
|
|
argv++, argc--;
|
2001-06-29 18:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (grammar) {
|
|
|
|
if (type == NULL)
|
|
|
|
usage();
|
|
|
|
cfg_print_grammar(type, output, NULL);
|
|
|
|
} else {
|
|
|
|
if (type == NULL || filename == NULL)
|
|
|
|
usage();
|
|
|
|
RUNTIME_CHECK(cfg_parser_create(mctx, lctx, &pctx) == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
result = cfg_parse_file(pctx, filename, type, &cfg);
|
|
|
|
|
|
|
|
fprintf(stderr, "read config: %s\n", isc_result_totext(result));
|
|
|
|
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
cfg_print(cfg, output, NULL);
|
|
|
|
|
|
|
|
cfg_obj_destroy(pctx, &cfg);
|
|
|
|
|
|
|
|
cfg_parser_destroy(&pctx);
|
|
|
|
}
|
2001-02-15 05:15:27 +00:00
|
|
|
|
|
|
|
isc_log_destroy(&lctx);
|
2001-06-29 18:36:13 +00:00
|
|
|
if (memstats)
|
|
|
|
isc_mem_stats(mctx, stderr);
|
2001-02-15 05:15:27 +00:00
|
|
|
isc_mem_destroy(&mctx);
|
|
|
|
|
2009-03-02 02:42:50 +00:00
|
|
|
fflush(stdout);
|
|
|
|
if (ferror(stdout)) {
|
|
|
|
fprintf(stderr, "write error\n");
|
|
|
|
return (1);
|
|
|
|
} else
|
|
|
|
return (0);
|
2001-02-15 05:15:27 +00:00
|
|
|
}
|