lex_test.c revision 01cc5813ca716d48c46960302d9845ef7c984f93
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence/*
a7038d1a0513c8e804937ebc95fc9cb3a46c04f5Mark Andrews * Copyright (C) 1998, 1999 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * Permission to use, copy, modify, and distribute this software for any
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * purpose with or without fee is hereby granted, provided that the above
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15a44745412679c30a6d022733925af70a38b715David Lawrence * SOFTWARE.
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence */
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
c00a1eb423623442aff428336bb55590f79013bbMark Andrews#include <config.h>
cb4a97bb1c84fcab089136f0a4686ab5fea6a07cDavid Lawrence
cb4a97bb1c84fcab089136f0a4686ab5fea6a07cDavid Lawrence#include <stdio.h>
cb4a97bb1c84fcab089136f0a4686ab5fea6a07cDavid Lawrence#include <stdlib.h>
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence#include <string.h>
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews#include <unistd.h>
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence#include <isc/assertions.h>
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence#include <isc/error.h>
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence#include <isc/lex.h>
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrenceisc_mem_t *mctx;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrenceisc_lex_t *lex;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrenceisc_lexspecials_t specials;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrencestatic void
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrenceprint_token(isc_token_t *tokenp, FILE *stream) {
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews switch (tokenp->type) {
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews case isc_tokentype_unknown:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence fprintf(stream, "UNKNOWN");
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence break;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case isc_tokentype_string:
1ab0c9558f167cac2fc6a486547334d56930b81eDavid Lawrence fprintf(stream, "STRING %.*s",
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence (int)tokenp->value.as_region.length,
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence tokenp->value.as_region.base);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence break;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case isc_tokentype_number:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence fprintf(stream, "NUMBER %lu", tokenp->value.as_ulong);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence break;
1ab0c9558f167cac2fc6a486547334d56930b81eDavid Lawrence case isc_tokentype_qstring:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence fprintf(stream, "QSTRING \"%.*s\"",
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence (int)tokenp->value.as_region.length,
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence tokenp->value.as_region.base);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence break;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence case isc_tokentype_eol:
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence fprintf(stream, "EOL");
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case isc_tokentype_eof:
fcf28b759c65388e8875f2dbcf8eb5fa786b29ecAndreas Gustafsson fprintf(stream, "EOF");
fcf28b759c65388e8875f2dbcf8eb5fa786b29ecAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case isc_tokentype_initialws:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson fprintf(stream, "INITIALWS");
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case isc_tokentype_special:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson fprintf(stream, "SPECIAL %c", tokenp->value.as_char);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case isc_tokentype_nomore:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson fprintf(stream, "NOMORE");
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson default:
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson FATAL_ERROR(__FILE__, __LINE__, "Unexpected type %d",
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson tokenp->type);
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson }
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson}
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafssonint
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafssonmain(int argc, char *argv[]) {
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson isc_token_t token;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson isc_result_t result;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson int quiet = 0;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson int c;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson int masterfile = 1;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson int stats = 0;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson unsigned int options = 0;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson int done = 0;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson while ((c = getopt(argc, argv, "qmcs")) != -1) {
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson switch (c) {
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case 'q':
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson quiet = 1;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case 'm':
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson masterfile = 1;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case 'c':
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson masterfile = 0;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson case 's':
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson stats = 1;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson break;
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson }
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson }
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson
19872fdfb75357354a83b74932f661d4b0b6e4ddAndreas Gustafsson RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
c00a1eb423623442aff428336bb55590f79013bbMark Andrews RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS);
c00a1eb423623442aff428336bb55590f79013bbMark Andrews
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence if (masterfile) {
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews /* Set up to lex DNS master file. */
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews specials['('] = 1;
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews specials[')'] = 1;
28d8b4118da7abed531ca09136a6d1402837d721Mark Andrews specials['"'] = 1;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence isc_lex_setspecials(lex, specials);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence options = ISC_LEXOPT_DNSMULTILINE |
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence ISC_LEXOPT_EOF |
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence ISC_LEXOPT_QSTRING | ISC_LEXOPT_NOMORE;
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence } else {
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence /* Set up to lex DNS config file. */
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence
0c73b546ecfa49b9d1c8fdb9a48d4cd62176124aDavid Lawrence 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) {
print_token(&token, stdout);
printf("\n");
}
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);
}