lex_test.c revision ab023a65562e62b85a824509d829b6fad87e00b1
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley/*
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 1998-2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * Permission to use, copy, modify, and distribute this software for any
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * purpose with or without fee is hereby granted, provided that the above
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/* $Id: lex_test.c,v 1.20 2005/04/27 04:56:09 sra Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#include <config.h>
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
6a759e38699d133302d0df120d7afcd4af721951David Lawrence#include <isc/commandline.h>
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#include <isc/lex.h>
f73e404eb9e1d314f85128cae48cdee036b405ecAndreas Gustafsson#include <isc/mem.h>
7df0472d8a76c83d2137bd9549414a30787d58cfBrian Wellington#include <isc/util.h>
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyisc_mem_t *mctx;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyisc_lex_t *lex;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyisc_lexspecials_t specials;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleystatic void
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyprint_token(isc_token_t *tokenp, FILE *stream) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley switch (tokenp->type) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_unknown:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley fprintf(stream, "UNKNOWN");
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_string:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley fprintf(stream, "STRING %.*s",
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley (int)tokenp->value.as_region.length,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley tokenp->value.as_region.base);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_number:
941f41d6139c868e357892869c94c4693b943373Bob Halley fprintf(stream, "NUMBER %lu", tokenp->value.as_ulong);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_qstring:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley fprintf(stream, "QSTRING \"%.*s\"",
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley (int)tokenp->value.as_region.length,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley tokenp->value.as_region.base);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_eol:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley fprintf(stream, "EOL");
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_eof:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley fprintf(stream, "EOF");
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_initialws:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley fprintf(stream, "INITIALWS");
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case isc_tokentype_special:
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley fprintf(stream, "SPECIAL %c", tokenp->value.as_char);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley case isc_tokentype_nomore:
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley fprintf(stream, "NOMORE");
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley default:
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley FATAL_ERROR(__FILE__, __LINE__, "Unexpected type %d",
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley tokenp->type);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley}
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyint
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleymain(int argc, char *argv[]) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_token_t token;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_result_t result;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley int quiet = 0;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley int c;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley int masterfile = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley int stats = 0;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned int options = 0;
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley int done = 0;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
6a759e38699d133302d0df120d7afcd4af721951David Lawrence while ((c = isc_commandline_parse(argc, argv, "qmcs")) != -1) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley switch (c) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case 'q':
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley quiet = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case 'm':
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley masterfile = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case 'c':
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley masterfile = 0;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley case 's':
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley stats = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley break;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley RUNTIME_CHECK(isc_lex_create(mctx, 256, &lex) == ISC_R_SUCCESS);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (masterfile) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* Set up to lex DNS master file. */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['('] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials[')'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['"'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_lex_setspecials(lex, specials);
c1bfdd299587e790f59378351df66f6168f1e09eAndreas Gustafsson options = ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE |
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley ISC_LEXOPT_EOF |
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley ISC_LEXOPT_QSTRING | ISC_LEXOPT_NOMORE;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley } else {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* Set up to lex DNS config file. */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['{'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['}'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials[';'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['/'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['"'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['!'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley specials['*'] = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_lex_setspecials(lex, specials);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley options = ISC_LEXOPT_EOF |
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley ISC_LEXOPT_QSTRING |
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley ISC_LEXOPT_NUMBER | ISC_LEXOPT_NOMORE;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_lex_setcomments(lex, (ISC_LEXCOMMENT_C|
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley ISC_LEXCOMMENT_CPLUSPLUS|
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley ISC_LEXCOMMENT_SHELL));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley RUNTIME_CHECK(isc_lex_openstream(lex, stdin) == ISC_R_SUCCESS);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley while ((result = isc_lex_gettoken(lex, options, &token)) ==
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley ISC_R_SUCCESS && !done) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (!quiet) {
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews char *name = isc_lex_getsourcename(lex);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley print_token(&token, stdout);
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence printf(" line = %lu file = %s\n",
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews isc_lex_getsourceline(lex),
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews (name == NULL) ? "<none>" : name);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley if (token.type == isc_tokentype_eof)
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley isc_lex_close(lex);
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley if (token.type == isc_tokentype_nomore)
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley done = 1;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley if (result != ISC_R_SUCCESS)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley printf("Result: %s\n", isc_result_totext(result));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_lex_close(lex);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_lex_destroy(&lex);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (!quiet && stats)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_mem_stats(mctx, stdout);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_mem_destroy(&mctx);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return (0);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley}