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