1 /* $NetBSD: lex_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001 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: lex_test.c,v 1.23 2007/06/19 23:46:59 tbox Exp */
25 #include <isc/commandline.h>
33 isc_lexspecials_t specials
;
36 print_token(isc_token_t
*tokenp
, FILE *stream
) {
37 switch (tokenp
->type
) {
38 case isc_tokentype_unknown
:
39 fprintf(stream
, "UNKNOWN");
41 case isc_tokentype_string
:
42 fprintf(stream
, "STRING %.*s",
43 (int)tokenp
->value
.as_region
.length
,
44 tokenp
->value
.as_region
.base
);
46 case isc_tokentype_number
:
47 fprintf(stream
, "NUMBER %lu", tokenp
->value
.as_ulong
);
49 case isc_tokentype_qstring
:
50 fprintf(stream
, "QSTRING \"%.*s\"",
51 (int)tokenp
->value
.as_region
.length
,
52 tokenp
->value
.as_region
.base
);
54 case isc_tokentype_eol
:
55 fprintf(stream
, "EOL");
57 case isc_tokentype_eof
:
58 fprintf(stream
, "EOF");
60 case isc_tokentype_initialws
:
61 fprintf(stream
, "INITIALWS");
63 case isc_tokentype_special
:
64 fprintf(stream
, "SPECIAL %c", tokenp
->value
.as_char
);
66 case isc_tokentype_nomore
:
67 fprintf(stream
, "NOMORE");
70 FATAL_ERROR(__FILE__
, __LINE__
, "Unexpected type %d",
76 main(int argc
, char *argv
[]) {
83 unsigned int options
= 0;
86 while ((c
= isc_commandline_parse(argc
, argv
, "qmcs")) != -1) {
103 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
104 RUNTIME_CHECK(isc_lex_create(mctx
, 256, &lex
) == ISC_R_SUCCESS
);
107 /* Set up to lex DNS master file. */
112 isc_lex_setspecials(lex
, specials
);
113 options
= ISC_LEXOPT_DNSMULTILINE
| ISC_LEXOPT_ESCAPE
|
115 ISC_LEXOPT_QSTRING
| ISC_LEXOPT_NOMORE
;
116 isc_lex_setcomments(lex
, ISC_LEXCOMMENT_DNSMASTERFILE
);
118 /* Set up to lex DNS config file. */
127 isc_lex_setspecials(lex
, specials
);
128 options
= ISC_LEXOPT_EOF
|
130 ISC_LEXOPT_NUMBER
| ISC_LEXOPT_NOMORE
;
131 isc_lex_setcomments(lex
, (ISC_LEXCOMMENT_C
|
132 ISC_LEXCOMMENT_CPLUSPLUS
|
133 ISC_LEXCOMMENT_SHELL
));
136 RUNTIME_CHECK(isc_lex_openstream(lex
, stdin
) == ISC_R_SUCCESS
);
138 while ((result
= isc_lex_gettoken(lex
, options
, &token
)) ==
139 ISC_R_SUCCESS
&& !done
) {
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
);
147 if (token
.type
== isc_tokentype_eof
)
149 if (token
.type
== isc_tokentype_nomore
)
152 if (result
!= ISC_R_SUCCESS
)
153 printf("Result: %s\n", isc_result_totext(result
));
156 isc_lex_destroy(&lex
);
158 isc_mem_stats(mctx
, stdout
);
159 isc_mem_destroy(&mctx
);