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