add lex_test
This commit is contained in:
@@ -4,5 +4,6 @@ rwlock_test
|
||||
mem_test
|
||||
sock_test
|
||||
sym_test
|
||||
lex_test
|
||||
task_test
|
||||
timer_test
|
||||
|
||||
@@ -18,6 +18,7 @@ TARGETS = name_test \
|
||||
rwlock_test \
|
||||
sock_test \
|
||||
sym_test \
|
||||
lex_test \
|
||||
task_test \
|
||||
timer_test
|
||||
|
||||
@@ -35,6 +36,9 @@ sock_test: sock_test.o ../../lib/isc/libisc.a ../../lib/dns/libdns.a
|
||||
sym_test: sym_test.o ../../lib/isc/libisc.a
|
||||
${CC} -o $@ sym_test.o ${LIBS}
|
||||
|
||||
lex_test: lex_test.o ../../lib/isc/libisc.a
|
||||
${CC} -o $@ lex_test.o ${LIBS}
|
||||
|
||||
task_test: task_test.o ../../lib/isc/libisc.a ../../lib/dns/libdns.a
|
||||
${CC} -o $@ task_test.o ${LIBS}
|
||||
|
||||
|
||||
170
bin/tests/lex_test.c
Normal file
170
bin/tests/lex_test.c
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (C) 1998 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
||||
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
||||
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
||||
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
||||
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
||||
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <isc/assertions.h>
|
||||
#include <isc/lex.h>
|
||||
|
||||
isc_mem_t *mctx;
|
||||
isc_lex_t *lex;
|
||||
|
||||
isc_lexspecials_t specials;
|
||||
|
||||
static void
|
||||
print_token(isc_token_t *tokenp, FILE *stream) {
|
||||
switch (tokenp->type) {
|
||||
case isc_tokentype_unknown:
|
||||
fprintf(stream, "UNKNOWN");
|
||||
break;
|
||||
case isc_tokentype_string:
|
||||
fprintf(stream, "STRING %.*s",
|
||||
(int)tokenp->value.as_region.length,
|
||||
tokenp->value.as_region.base);
|
||||
break;
|
||||
case isc_tokentype_number:
|
||||
fprintf(stream, "NUMBER %.*s",
|
||||
(int)tokenp->value.as_region.length,
|
||||
tokenp->value.as_region.base);
|
||||
break;
|
||||
case isc_tokentype_qstring:
|
||||
fprintf(stream, "QSTRING \"%.*s\"",
|
||||
(int)tokenp->value.as_region.length,
|
||||
tokenp->value.as_region.base);
|
||||
break;
|
||||
case isc_tokentype_eol:
|
||||
fprintf(stream, "EOL");
|
||||
break;
|
||||
case isc_tokentype_eof:
|
||||
fprintf(stream, "EOF");
|
||||
break;
|
||||
case isc_tokentype_initialws:
|
||||
fprintf(stream, "INITIALWS");
|
||||
break;
|
||||
case isc_tokentype_special:
|
||||
fprintf(stream, "SPECIAL %c", tokenp->value.as_char);
|
||||
break;
|
||||
default:
|
||||
INSIST(0);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[]) {
|
||||
isc_token_t token;
|
||||
isc_result_t result;
|
||||
int quiet = 0;
|
||||
int c;
|
||||
int masterfile = 1;
|
||||
int stats = 0;
|
||||
unsigned int options = 0;
|
||||
unsigned int parens = 0;
|
||||
|
||||
while ((c = getopt(argc, argv, "qmcs")) != -1) {
|
||||
switch (c) {
|
||||
case 'q':
|
||||
quiet = 1;
|
||||
break;
|
||||
case 'm':
|
||||
masterfile = 1;
|
||||
break;
|
||||
case 'c':
|
||||
masterfile = 0;
|
||||
break;
|
||||
case 's':
|
||||
stats = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
INSIST(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
||||
INSIST(isc_lex_create(mctx, 256, &lex) ==
|
||||
ISC_R_SUCCESS);
|
||||
|
||||
if (masterfile) {
|
||||
/* Set up to lex DNS master file. */
|
||||
|
||||
specials['('] = 1;
|
||||
specials[')'] = 1;
|
||||
specials['"'] = 1;
|
||||
isc_lex_setspecials(lex, specials);
|
||||
options = ISC_LEXOPT_EOL |
|
||||
ISC_LEXOPT_EOF |
|
||||
ISC_LEXOPT_INITIALWS |
|
||||
ISC_LEXOPT_QSTRING;
|
||||
isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
|
||||
} else {
|
||||
/* Set up to lex DNS config file. */
|
||||
|
||||
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_lex_setcomments(lex, (ISC_LEXCOMMENT_C|
|
||||
ISC_LEXCOMMENT_CPLUSPLUS|
|
||||
ISC_LEXCOMMENT_SHELL));
|
||||
}
|
||||
|
||||
INSIST(isc_lex_openstream(lex, stdin) == ISC_R_SUCCESS);
|
||||
|
||||
while ((result = isc_lex_gettoken(lex, options, &token)) ==
|
||||
ISC_R_SUCCESS) {
|
||||
if (masterfile && token.type == isc_tokentype_special) {
|
||||
if (token.value.as_char == '(') {
|
||||
parens++;
|
||||
options &= ~ISC_LEXOPT_EOL;
|
||||
options &= ~ISC_LEXOPT_INITIALWS;
|
||||
} else if (token.value.as_char == ')') {
|
||||
if (parens == 0) {
|
||||
printf("mismatched parens\n");
|
||||
exit(1);
|
||||
}
|
||||
parens--;
|
||||
if (parens == 0) {
|
||||
options |= ISC_LEXOPT_EOL;
|
||||
options |= ISC_LEXOPT_INITIALWS;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!quiet) {
|
||||
print_token(&token, stdout);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
if (result != ISC_R_EOF)
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user