lex_test.c revision 15a44745412679c30a6d022733925af70a38b715
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley/*
6dcde6ca366549264b23e191ff01162f9dd03350David Lawrence * Copyright (C) 1998-2000 Internet Software Consortium.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley *
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.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
15a44745412679c30a6d022733925af70a38b715David Lawrence/* $Id: lex_test.c,v 1.16 2000/07/27 09:38:03 tale Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
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}