1/*
2 * Copyright (C) 2004, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001  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: master_test.c,v 1.32 2009/09/02 23:48:01 tbox Exp $ */
19
20#include <config.h>
21
22#include <stdlib.h>
23#include <string.h>
24
25#include <isc/buffer.h>
26#include <isc/mem.h>
27#include <isc/util.h>
28
29#include <dns/callbacks.h>
30#include <dns/master.h>
31#include <dns/name.h>
32#include <dns/rdataset.h>
33#include <dns/result.h>
34
35isc_mem_t *mctx;
36
37static isc_result_t
38print_dataset(void *arg, dns_name_t *owner, dns_rdataset_t *dataset) {
39	char buf[64*1024];
40	isc_buffer_t target;
41	isc_result_t result;
42
43	UNUSED(arg);
44
45	isc_buffer_init(&target, buf, 64*1024);
46	result = dns_rdataset_totext(dataset, owner, ISC_FALSE, ISC_FALSE,
47				     &target);
48	if (result == ISC_R_SUCCESS)
49		fprintf(stdout, "%.*s\n", (int)target.used,
50					  (char*)target.base);
51	else
52		fprintf(stdout, "dns_rdataset_totext: %s\n",
53			dns_result_totext(result));
54
55	return (ISC_R_SUCCESS);
56}
57
58int
59main(int argc, char *argv[]) {
60	isc_result_t result;
61	dns_name_t origin;
62	isc_buffer_t source;
63	isc_buffer_t target;
64	unsigned char name_buf[255];
65	dns_rdatacallbacks_t callbacks;
66
67	UNUSED(argc);
68
69	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
70
71	if (argv[1]) {
72		isc_buffer_init(&source, argv[1], strlen(argv[1]));
73		isc_buffer_add(&source, strlen(argv[1]));
74		isc_buffer_setactive(&source, strlen(argv[1]));
75		isc_buffer_init(&target, name_buf, 255);
76		dns_name_init(&origin, NULL);
77		result = dns_name_fromtext(&origin, &source, dns_rootname,
78					   0, &target);
79		if (result != ISC_R_SUCCESS) {
80			fprintf(stdout, "dns_name_fromtext: %s\n",
81				dns_result_totext(result));
82			exit(1);
83		}
84
85		dns_rdatacallbacks_init_stdio(&callbacks);
86		callbacks.add = print_dataset;
87
88		result = dns_master_loadfile(argv[1], &origin, &origin,
89					     dns_rdataclass_in, 0,
90					     &callbacks, mctx);
91		fprintf(stdout, "dns_master_loadfile: %s\n",
92			dns_result_totext(result));
93	}
94	return (0);
95}
96