lex_test.c revision dafcb997e390efa4423883dafd100c975c4095d6
e2d579a02171396d24b1b5fc5ad238d85cabd134Danny Mayer/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
e2d579a02171396d24b1b5fc5ad238d85cabd134Danny Mayer * Copyright (C) 1998-2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
e2d579a02171396d24b1b5fc5ad238d85cabd134Danny Mayer * copyright notice and this permission notice appear in all copies.
e2d579a02171396d24b1b5fc5ad238d85cabd134Danny Mayer *
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
e2d579a02171396d24b1b5fc5ad238d85cabd134Danny Mayer * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
e2d579a02171396d24b1b5fc5ad238d85cabd134Danny Mayer * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
c4958494a98a59ce25e9fecad76a9ab0e36cc59fDanny Mayer * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
e2d579a02171396d24b1b5fc5ad238d85cabd134Danny Mayer * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * PERFORMANCE OF THIS SOFTWARE.
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson */
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson/* $Id: lex_test.c,v 1.19 2004/03/05 04:58:37 marka Exp $ */
#include <config.h>
#include <isc/commandline.h>
#include <isc/lex.h>
#include <isc/mem.h>
#include <isc/util.h>
isc_mem_t *mctx;
isc_lex_t *lex;
isc_lexspecials_t specials;
static void
print_token(isc_token_t *tokenp, FILE *stream) {
switch (tokenp->type) {
case isc_tokentype_unknown:
fprintf(stream, "UNKNOWN");
break;
case isc_tokentype_string:
fprintf(stream, "STRING %.*s",
(int)tokenp->value.as_region.length,
tokenp->value.as_region.base);
break;
case isc_tokentype_number:
fprintf(stream, "NUMBER %lu", tokenp->value.as_ulong);
break;
case isc_tokentype_qstring:
fprintf(stream, "QSTRING \"%.*s\"",
(int)tokenp->value.as_region.length,
tokenp->value.as_region.base);
break;
case isc_tokentype_eol:
fprintf(stream, "EOL");
break;
case isc_tokentype_eof:
fprintf(stream, "EOF");
break;
case isc_tokentype_initialws:
fprintf(stream, "INITIALWS");
break;
case isc_tokentype_special:
fprintf(stream, "SPECIAL %c", tokenp->value.as_char);
break;
case isc_tokentype_nomore:
fprintf(stream, "NOMORE");
break;
default:
FATAL_ERROR(__FILE__, __LINE__, "Unexpected type %d",
tokenp->type);
}
}
int
main(int argc, char *argv[]) {
isc_token_t token;
isc_result_t result;
int quiet = 0;
int c;
int masterfile = 1;
int stats = 0;
unsigned int options = 0;
int done = 0;
while ((c = isc_commandline_parse(argc, argv, "qmcs")) != -1) {
switch (c) {
case 'q':
quiet = 1;
break;
case 'm':
masterfile = 1;
break;
case 'c':
masterfile = 0;
break;
case 's':
stats = 1;
break;
}
}
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS);
if (masterfile) {
/* Set up to lex DNS master file. */
specials['('] = 1;
specials[')'] = 1;
specials['"'] = 1;
isc_lex_setspecials(lex, specials);
options = ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE |
ISC_LEXOPT_EOF |
ISC_LEXOPT_QSTRING | ISC_LEXOPT_NOMORE;
isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
} else {
/* Set up to lex DNS config file. */
specials['{'] = 1;
specials['}'] = 1;
specials[';'] = 1;
specials['/'] = 1;
specials['"'] = 1;
specials['!'] = 1;
specials['*'] = 1;
isc_lex_setspecials(lex, specials);
options = ISC_LEXOPT_EOF |
ISC_LEXOPT_QSTRING |
ISC_LEXOPT_NUMBER | ISC_LEXOPT_NOMORE;
isc_lex_setcomments(lex, (ISC_LEXCOMMENT_C|
ISC_LEXCOMMENT_CPLUSPLUS|
ISC_LEXCOMMENT_SHELL));
}
RUNTIME_CHECK(isc_lex_openstream(lex, stdin) == ISC_R_SUCCESS);
while ((result = isc_lex_gettoken(lex, options, &token)) ==
ISC_R_SUCCESS && !done) {
if (!quiet) {
char *name = isc_lex_getsourcename(lex);
print_token(&token, stdout);
printf(" line = %lu file = %s\n",
isc_lex_getsourceline(lex),
(name == NULL) ? "<none>" : name);
}
if (token.type == isc_tokentype_eof)
isc_lex_close(lex);
if (token.type == isc_tokentype_nomore)
done = 1;
}
if (result != ISC_R_SUCCESS)
printf("Result: %s\n", isc_result_totext(result));
isc_lex_close(lex);
isc_lex_destroy(&lex);
if (!quiet && stats)
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
return (0);
}