xref: /minix/external/bsd/bind/dist/bin/tests/lex_test.c (revision 00b67f09)
1 /*	$NetBSD: lex_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2001  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: lex_test.c,v 1.23 2007/06/19 23:46:59 tbox Exp  */
21 
22 /*! \file */
23 #include <config.h>
24 
25 #include <isc/commandline.h>
26 #include <isc/lex.h>
27 #include <isc/mem.h>
28 #include <isc/util.h>
29 
30 isc_mem_t *mctx;
31 isc_lex_t *lex;
32 
33 isc_lexspecials_t specials;
34 
35 static void
print_token(isc_token_t * tokenp,FILE * stream)36 print_token(isc_token_t *tokenp, FILE *stream) {
37 	switch (tokenp->type) {
38 	case isc_tokentype_unknown:
39 		fprintf(stream, "UNKNOWN");
40 		break;
41 	case isc_tokentype_string:
42 		fprintf(stream, "STRING %.*s",
43 			(int)tokenp->value.as_region.length,
44 			tokenp->value.as_region.base);
45 		break;
46 	case isc_tokentype_number:
47 		fprintf(stream, "NUMBER %lu", tokenp->value.as_ulong);
48 		break;
49 	case isc_tokentype_qstring:
50 		fprintf(stream, "QSTRING \"%.*s\"",
51 			(int)tokenp->value.as_region.length,
52 			tokenp->value.as_region.base);
53 		break;
54 	case isc_tokentype_eol:
55 		fprintf(stream, "EOL");
56 		break;
57 	case isc_tokentype_eof:
58 		fprintf(stream, "EOF");
59 		break;
60 	case isc_tokentype_initialws:
61 		fprintf(stream, "INITIALWS");
62 		break;
63 	case isc_tokentype_special:
64 		fprintf(stream, "SPECIAL %c", tokenp->value.as_char);
65 		break;
66 	case isc_tokentype_nomore:
67 		fprintf(stream, "NOMORE");
68 		break;
69 	default:
70 		FATAL_ERROR(__FILE__, __LINE__, "Unexpected type %d",
71 			    tokenp->type);
72 	}
73 }
74 
75 int
main(int argc,char * argv[])76 main(int argc, char *argv[]) {
77 	isc_token_t token;
78 	isc_result_t result;
79 	int quiet = 0;
80 	int c;
81 	int masterfile = 1;
82 	int stats = 0;
83 	unsigned int options = 0;
84 	int done = 0;
85 
86 	while ((c = isc_commandline_parse(argc, argv, "qmcs")) != -1) {
87 		switch (c) {
88 		case 'q':
89 			quiet = 1;
90 			break;
91 		case 'm':
92 			masterfile = 1;
93 			break;
94 		case 'c':
95 			masterfile = 0;
96 			break;
97 		case 's':
98 			stats = 1;
99 			break;
100 		}
101 	}
102 
103 	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
104 	RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS);
105 
106 	if (masterfile) {
107 		/* Set up to lex DNS master file. */
108 
109 		specials['('] = 1;
110 		specials[')'] = 1;
111 		specials['"'] = 1;
112 		isc_lex_setspecials(lex, specials);
113 		options = ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE |
114 			ISC_LEXOPT_EOF |
115 			ISC_LEXOPT_QSTRING | ISC_LEXOPT_NOMORE;
116 		isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
117 	} else {
118 		/* Set up to lex DNS config file. */
119 
120 		specials['{'] = 1;
121 		specials['}'] = 1;
122 		specials[';'] = 1;
123 		specials['/'] = 1;
124 		specials['"'] = 1;
125 		specials['!'] = 1;
126 		specials['*'] = 1;
127 		isc_lex_setspecials(lex, specials);
128 		options = ISC_LEXOPT_EOF |
129 			ISC_LEXOPT_QSTRING |
130 			ISC_LEXOPT_NUMBER | ISC_LEXOPT_NOMORE;
131 		isc_lex_setcomments(lex, (ISC_LEXCOMMENT_C|
132 					  ISC_LEXCOMMENT_CPLUSPLUS|
133 					  ISC_LEXCOMMENT_SHELL));
134 	}
135 
136 	RUNTIME_CHECK(isc_lex_openstream(lex, stdin) == ISC_R_SUCCESS);
137 
138 	while ((result = isc_lex_gettoken(lex, options, &token)) ==
139 	       ISC_R_SUCCESS && !done) {
140 		if (!quiet) {
141 			char *name = isc_lex_getsourcename(lex);
142 			print_token(&token, stdout);
143 			printf(" line = %lu file = %s\n",
144 				isc_lex_getsourceline(lex),
145 				(name == NULL) ? "<none>" : name);
146 		}
147 		if (token.type == isc_tokentype_eof)
148 			isc_lex_close(lex);
149 		if (token.type == isc_tokentype_nomore)
150 			done = 1;
151 	}
152 	if (result != ISC_R_SUCCESS)
153 		printf("Result: %s\n", isc_result_totext(result));
154 
155 	isc_lex_close(lex);
156 	isc_lex_destroy(&lex);
157 	if (!quiet && stats)
158 		isc_mem_stats(mctx, stdout);
159 	isc_mem_destroy(&mctx);
160 
161 	return (0);
162 }
163