1998-12-30 22:11:19 +00:00
|
|
|
/*
|
2000-02-03 23:08:31 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
|
1998-12-30 22:11:19 +00:00
|
|
|
*
|
|
|
|
* 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 <ctype.h>
|
1999-02-05 06:41:21 +00:00
|
|
|
#include <stdlib.h>
|
1998-12-30 22:11:19 +00:00
|
|
|
|
2000-04-10 21:33:36 +00:00
|
|
|
#include <isc/buffer.h>
|
2000-05-09 23:19:32 +00:00
|
|
|
#include <isc/file.h>
|
1998-12-30 22:11:19 +00:00
|
|
|
#include <isc/lex.h>
|
2000-04-10 21:33:36 +00:00
|
|
|
#include <isc/mem.h>
|
2000-05-11 16:14:05 +00:00
|
|
|
#include <isc/stdio.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1998-12-30 22:11:19 +00:00
|
|
|
|
|
|
|
typedef struct inputsource {
|
|
|
|
isc_result_t result;
|
|
|
|
isc_boolean_t is_file;
|
|
|
|
isc_boolean_t need_close;
|
1999-01-28 08:38:42 +00:00
|
|
|
isc_boolean_t at_eof;
|
1998-12-30 22:11:19 +00:00
|
|
|
isc_boolean_t have_token;
|
|
|
|
isc_token_t token;
|
|
|
|
unsigned int char_count;
|
|
|
|
int chars[2];
|
|
|
|
void * input;
|
|
|
|
char * name;
|
|
|
|
unsigned long line;
|
|
|
|
LINK(struct inputsource) link;
|
|
|
|
} inputsource;
|
|
|
|
|
|
|
|
#define LEX_MAGIC 0x4C657821U /* Lex!. */
|
|
|
|
#define VALID_LEX(l) ((l) != NULL && \
|
|
|
|
(l)->magic == LEX_MAGIC)
|
|
|
|
|
|
|
|
struct isc_lex {
|
|
|
|
/* Unlocked. */
|
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t * mctx;
|
|
|
|
size_t max_token;
|
|
|
|
char * data;
|
|
|
|
unsigned int options;
|
|
|
|
unsigned int comments;
|
|
|
|
isc_boolean_t comment_ok;
|
|
|
|
isc_boolean_t last_was_eol;
|
1999-01-26 08:26:19 +00:00
|
|
|
unsigned int paren_count;
|
1998-12-30 22:11:19 +00:00
|
|
|
isc_lexspecials_t specials;
|
|
|
|
LIST(struct inputsource) sources;
|
|
|
|
};
|
|
|
|
|
1999-12-23 05:24:12 +00:00
|
|
|
static inline isc_result_t
|
|
|
|
grow_data(isc_lex_t *lex, size_t *remainingp, char **currp, char **prevp) {
|
|
|
|
char *new;
|
|
|
|
|
|
|
|
new = isc_mem_get(lex->mctx, lex->max_token * 2 + 1);
|
2000-05-04 23:59:49 +00:00
|
|
|
if (new == NULL)
|
1999-12-23 05:24:12 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
memcpy(new, lex->data, lex->max_token + 1);
|
|
|
|
*currp = new + (*currp - lex->data);
|
|
|
|
if (*prevp != NULL)
|
|
|
|
*prevp = new + (*prevp - lex->data);
|
|
|
|
isc_mem_put(lex->mctx, lex->data, lex->max_token + 1);
|
|
|
|
lex->data = new;
|
|
|
|
*remainingp += lex->max_token;
|
|
|
|
lex->max_token *= 2;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-12-30 22:11:19 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_lex_create(isc_mem_t *mctx, size_t max_token, isc_lex_t **lexp) {
|
|
|
|
isc_lex_t *lex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a lexer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(lexp != NULL && *lexp == NULL);
|
|
|
|
REQUIRE(max_token > 0);
|
|
|
|
|
|
|
|
lex = isc_mem_get(mctx, sizeof *lex);
|
|
|
|
if (lex == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
1999-01-21 06:02:15 +00:00
|
|
|
lex->data = isc_mem_get(mctx, max_token + 1);
|
1998-12-30 22:11:19 +00:00
|
|
|
if (lex->data == NULL) {
|
|
|
|
isc_mem_put(mctx, lex, sizeof *lex);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
lex->mctx = mctx;
|
|
|
|
lex->max_token = max_token;
|
|
|
|
lex->comments = 0;
|
|
|
|
lex->comment_ok = ISC_TRUE;
|
|
|
|
lex->last_was_eol = ISC_TRUE;
|
1999-01-26 08:26:19 +00:00
|
|
|
lex->paren_count = 0;
|
1998-12-30 22:11:19 +00:00
|
|
|
memset(lex->specials, 0, 256);
|
|
|
|
INIT_LIST(lex->sources);
|
|
|
|
lex->magic = LEX_MAGIC;
|
|
|
|
|
|
|
|
*lexp = lex;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_lex_destroy(isc_lex_t **lexp) {
|
|
|
|
isc_lex_t *lex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy the lexer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(lexp != NULL);
|
|
|
|
lex = *lexp;
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
|
|
|
while (!EMPTY(lex->sources))
|
|
|
|
isc_lex_close(lex);
|
|
|
|
if (lex->data != NULL)
|
1999-01-21 06:02:15 +00:00
|
|
|
isc_mem_put(lex->mctx, lex->data, lex->max_token + 1);
|
1998-12-30 22:11:19 +00:00
|
|
|
lex->magic = 0;
|
2000-02-01 17:27:23 +00:00
|
|
|
isc_mem_put(lex->mctx, lex, sizeof *lex);
|
1998-12-30 22:11:19 +00:00
|
|
|
|
|
|
|
*lexp = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
isc_lex_getcomments(isc_lex_t *lex) {
|
|
|
|
/*
|
|
|
|
* Return the current lexer commenting styles.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
|
|
|
return (lex->comments);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_lex_setcomments(isc_lex_t *lex, unsigned int comments) {
|
|
|
|
/*
|
|
|
|
* Set allowed lexer commenting styles.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
|
|
|
lex->comments = comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_lex_getspecials(isc_lex_t *lex, isc_lexspecials_t specials) {
|
|
|
|
/*
|
|
|
|
* Put the current list of specials into 'specials'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
|
|
|
memcpy(specials, lex->specials, 256);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_lex_setspecials(isc_lex_t *lex, isc_lexspecials_t specials) {
|
|
|
|
/*
|
|
|
|
* The characters in 'specials' are returned as tokens. Along with
|
|
|
|
* whitespace, they delimit strings and numbers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
|
|
|
memcpy(lex->specials, specials, 256);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline isc_result_t
|
|
|
|
new_source(isc_lex_t *lex, isc_boolean_t is_file, isc_boolean_t need_close,
|
1999-10-25 18:42:09 +00:00
|
|
|
void *input, const char *name)
|
1998-12-30 22:11:19 +00:00
|
|
|
{
|
|
|
|
inputsource *source;
|
|
|
|
|
|
|
|
source = isc_mem_get(lex->mctx, sizeof *source);
|
|
|
|
if (source == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
source->result = ISC_R_SUCCESS;
|
|
|
|
source->is_file = is_file;
|
|
|
|
source->need_close = need_close;
|
1999-01-28 08:38:42 +00:00
|
|
|
source->at_eof = ISC_FALSE;
|
1998-12-30 22:11:19 +00:00
|
|
|
source->have_token = ISC_FALSE;
|
|
|
|
source->token.type = isc_tokentype_unknown;
|
|
|
|
source->token.value.as_pointer = NULL;
|
|
|
|
source->char_count = 0;
|
|
|
|
source->input = input;
|
|
|
|
source->name = isc_mem_strdup(lex->mctx, name);
|
1999-02-10 05:22:57 +00:00
|
|
|
if (source->name == NULL) {
|
|
|
|
isc_mem_put(lex->mctx, source, sizeof *source);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
1998-12-30 22:11:19 +00:00
|
|
|
source->line = 1;
|
|
|
|
PREPEND(lex->sources, source, link);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
1999-10-25 18:42:09 +00:00
|
|
|
isc_lex_openfile(isc_lex_t *lex, const char *filename) {
|
2000-05-09 23:19:32 +00:00
|
|
|
isc_result_t result;
|
|
|
|
FILE *stream = NULL;
|
1998-12-30 22:11:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open 'filename' and make it the current input source for 'lex'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
2000-05-11 16:14:05 +00:00
|
|
|
result = isc_stdio_open(filename, "r", &stream);
|
2000-05-09 23:19:32 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
1998-12-30 22:11:19 +00:00
|
|
|
flockfile(stream);
|
|
|
|
|
|
|
|
return (new_source(lex, ISC_TRUE, ISC_TRUE, stream, filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_lex_openstream(isc_lex_t *lex, FILE *stream) {
|
|
|
|
char name[128];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make 'stream' the current input source for 'lex'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
|
|
|
flockfile(stream);
|
|
|
|
/* This is safe. */
|
|
|
|
sprintf(name, "stream-%p", stream);
|
|
|
|
|
|
|
|
return (new_source(lex, ISC_TRUE, ISC_FALSE, stream, name));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_lex_openbuffer(isc_lex_t *lex, isc_buffer_t *buffer) {
|
|
|
|
char name[128];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make 'buffer' the current input source for 'lex'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
|
|
|
|
/* This is safe. */
|
|
|
|
sprintf(name, "buffer-%p", buffer);
|
|
|
|
|
|
|
|
return (new_source(lex, ISC_FALSE, ISC_FALSE, buffer, name));
|
|
|
|
}
|
|
|
|
|
1999-01-28 08:38:42 +00:00
|
|
|
isc_result_t
|
1998-12-30 22:11:19 +00:00
|
|
|
isc_lex_close(isc_lex_t *lex) {
|
|
|
|
inputsource *source;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close the most recently opened object (i.e. file or buffer).
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
1999-01-28 08:38:42 +00:00
|
|
|
|
1998-12-30 22:11:19 +00:00
|
|
|
source = HEAD(lex->sources);
|
1999-01-28 08:38:42 +00:00
|
|
|
if (source == NULL)
|
|
|
|
return (ISC_R_NOMORE);
|
1998-12-30 22:11:19 +00:00
|
|
|
|
|
|
|
UNLINK(lex->sources, source, link);
|
|
|
|
if (source->is_file) {
|
|
|
|
funlockfile((FILE *)(source->input));
|
|
|
|
if (source->need_close)
|
|
|
|
fclose((FILE *)(source->input));
|
|
|
|
}
|
|
|
|
isc_mem_free(lex->mctx, source->name);
|
|
|
|
isc_mem_put(lex->mctx, source, sizeof *source);
|
1999-01-28 08:38:42 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
1998-12-30 22:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
lexstate_start,
|
|
|
|
lexstate_crlf,
|
|
|
|
lexstate_string,
|
|
|
|
lexstate_number,
|
|
|
|
lexstate_maybecomment,
|
|
|
|
lexstate_ccomment,
|
|
|
|
lexstate_ccommentend,
|
|
|
|
lexstate_eatline,
|
1999-01-09 02:39:05 +00:00
|
|
|
lexstate_qstring
|
1998-12-30 22:11:19 +00:00
|
|
|
} lexstate;
|
|
|
|
|
1999-01-26 08:26:19 +00:00
|
|
|
#define IWSEOL (ISC_LEXOPT_INITIALWS | ISC_LEXOPT_EOL)
|
|
|
|
|
2000-03-22 17:39:48 +00:00
|
|
|
static void
|
|
|
|
pushback(inputsource *source, int c) {
|
|
|
|
INSIST(source->char_count < 2);
|
|
|
|
source->chars[source->char_count++] = c;
|
|
|
|
if (c == '\n')
|
|
|
|
source->line--;
|
|
|
|
}
|
|
|
|
|
1998-12-30 22:11:19 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_lex_gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *tokenp) {
|
|
|
|
inputsource *source;
|
|
|
|
int c;
|
|
|
|
isc_boolean_t done = ISC_FALSE;
|
|
|
|
isc_boolean_t no_comments = ISC_FALSE;
|
|
|
|
isc_boolean_t escaped = ISC_FALSE;
|
|
|
|
lexstate state = lexstate_start;
|
|
|
|
lexstate saved_state = lexstate_start;
|
|
|
|
isc_buffer_t *buffer;
|
|
|
|
FILE *stream;
|
|
|
|
char *curr, *prev;
|
|
|
|
size_t remaining;
|
1999-01-30 00:29:31 +00:00
|
|
|
unsigned long ulong;
|
1999-02-05 06:41:21 +00:00
|
|
|
unsigned int saved_options;
|
|
|
|
char *e;
|
1999-12-23 05:24:12 +00:00
|
|
|
isc_result_t result;
|
1998-12-30 22:11:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the next token.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
source = HEAD(lex->sources);
|
|
|
|
REQUIRE(tokenp != NULL);
|
|
|
|
|
1999-01-28 08:38:42 +00:00
|
|
|
if (source == NULL) {
|
|
|
|
if ((options & ISC_LEXOPT_NOMORE) != 0) {
|
|
|
|
tokenp->type = isc_tokentype_nomore;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
return (ISC_R_NOMORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source->result != ISC_R_SUCCESS)
|
|
|
|
return (source->result);
|
|
|
|
|
1998-12-30 22:11:19 +00:00
|
|
|
if (source->have_token) {
|
|
|
|
*tokenp = source->token;
|
|
|
|
source->have_token = ISC_FALSE;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-01-28 08:38:42 +00:00
|
|
|
if (source->char_count == 0 && source->at_eof) {
|
|
|
|
if ((options & ISC_LEXOPT_DNSMULTILINE) != 0 &&
|
|
|
|
lex->paren_count != 0)
|
|
|
|
return (ISC_R_UNBALANCED);
|
|
|
|
if ((options & ISC_LEXOPT_EOF) != 0) {
|
|
|
|
tokenp->type = isc_tokentype_eof;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
return (ISC_R_EOF);
|
|
|
|
}
|
1998-12-30 22:11:19 +00:00
|
|
|
|
1999-01-28 18:15:57 +00:00
|
|
|
saved_options = options;
|
|
|
|
if ((options & ISC_LEXOPT_DNSMULTILINE) != 0 && lex->paren_count > 0)
|
|
|
|
options &= ~IWSEOL;
|
1999-01-26 08:26:19 +00:00
|
|
|
|
1998-12-30 22:11:19 +00:00
|
|
|
curr = lex->data;
|
|
|
|
prev = NULL;
|
|
|
|
remaining = lex->max_token;
|
|
|
|
do {
|
|
|
|
if (source->char_count > 0) {
|
|
|
|
source->char_count--;
|
|
|
|
c = source->chars[source->char_count];
|
|
|
|
} else if (source->is_file) {
|
|
|
|
stream = source->input;
|
|
|
|
|
|
|
|
c = getc_unlocked(stream);
|
|
|
|
if (c == EOF) {
|
|
|
|
if (ferror(stream)) {
|
|
|
|
source->result = ISC_R_IOERROR;
|
|
|
|
return (source->result);
|
|
|
|
}
|
1999-01-28 08:38:42 +00:00
|
|
|
source->at_eof = ISC_TRUE;
|
1998-12-30 22:11:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer = source->input;
|
|
|
|
|
1999-01-06 05:38:21 +00:00
|
|
|
if (buffer->current == buffer->used) {
|
1998-12-30 22:11:19 +00:00
|
|
|
c = EOF;
|
1999-01-28 08:38:42 +00:00
|
|
|
source->at_eof = ISC_TRUE;
|
1999-01-06 05:38:21 +00:00
|
|
|
} else {
|
|
|
|
c = *((char *)buffer->base + buffer->current);
|
|
|
|
buffer->current++;
|
|
|
|
}
|
1998-12-30 22:11:19 +00:00
|
|
|
}
|
|
|
|
|
2000-03-22 17:39:48 +00:00
|
|
|
if (c == '\n')
|
1999-06-08 12:45:23 +00:00
|
|
|
source->line++;
|
|
|
|
|
1998-12-30 22:11:19 +00:00
|
|
|
if (lex->comment_ok && !no_comments) {
|
1999-05-19 00:48:16 +00:00
|
|
|
if (!escaped && c == ';' &&
|
1998-12-30 22:11:19 +00:00
|
|
|
((lex->comments & ISC_LEXCOMMENT_DNSMASTERFILE)
|
|
|
|
!= 0)) {
|
|
|
|
saved_state = state;
|
|
|
|
state = lexstate_eatline;
|
|
|
|
no_comments = ISC_TRUE;
|
|
|
|
continue;
|
|
|
|
} else if (c == '/' &&
|
|
|
|
(lex->comments &
|
|
|
|
(ISC_LEXCOMMENT_C|
|
|
|
|
ISC_LEXCOMMENT_CPLUSPLUS)) != 0) {
|
|
|
|
saved_state = state;
|
|
|
|
state = lexstate_maybecomment;
|
|
|
|
no_comments = ISC_TRUE;
|
|
|
|
continue;
|
|
|
|
} else if (c == '#' &&
|
|
|
|
((lex->comments & ISC_LEXCOMMENT_SHELL)
|
|
|
|
!= 0)) {
|
|
|
|
saved_state = state;
|
|
|
|
state = lexstate_eatline;
|
|
|
|
no_comments = ISC_TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
no_read:
|
|
|
|
/* INSIST(c == EOF || (c >= 0 && c <= 255)); */
|
|
|
|
switch (state) {
|
|
|
|
case lexstate_start:
|
|
|
|
if (c == EOF) {
|
|
|
|
lex->last_was_eol = ISC_FALSE;
|
1999-01-28 08:38:42 +00:00
|
|
|
if ((options & ISC_LEXOPT_DNSMULTILINE) != 0 &&
|
|
|
|
lex->paren_count != 0)
|
|
|
|
return (ISC_R_UNBALANCED);
|
1998-12-30 22:11:19 +00:00
|
|
|
if ((options & ISC_LEXOPT_EOF) == 0)
|
|
|
|
return (ISC_R_EOF);
|
|
|
|
tokenp->type = isc_tokentype_eof;
|
|
|
|
done = ISC_TRUE;
|
|
|
|
} else if (c == ' ' || c == '\t') {
|
|
|
|
if (lex->last_was_eol &&
|
|
|
|
(options & ISC_LEXOPT_INITIALWS)
|
|
|
|
!= 0) {
|
|
|
|
lex->last_was_eol = ISC_FALSE;
|
|
|
|
tokenp->type = isc_tokentype_initialws;
|
|
|
|
tokenp->value.as_char = c;
|
|
|
|
done = ISC_TRUE;
|
|
|
|
}
|
|
|
|
} else if (c == '\n') {
|
|
|
|
if ((options & ISC_LEXOPT_EOL) != 0) {
|
|
|
|
tokenp->type = isc_tokentype_eol;
|
|
|
|
done = ISC_TRUE;
|
|
|
|
}
|
|
|
|
lex->last_was_eol = ISC_TRUE;
|
|
|
|
} else if (c == '\r') {
|
|
|
|
if ((options & ISC_LEXOPT_EOL) != 0)
|
|
|
|
state = lexstate_crlf;
|
|
|
|
} else if (c == '"' &&
|
1999-05-19 00:48:16 +00:00
|
|
|
(options & ISC_LEXOPT_QSTRING) != 0) {
|
1998-12-30 22:11:19 +00:00
|
|
|
lex->last_was_eol = ISC_FALSE;
|
|
|
|
no_comments = ISC_TRUE;
|
|
|
|
state = lexstate_qstring;
|
|
|
|
} else if (lex->specials[c]) {
|
|
|
|
lex->last_was_eol = ISC_FALSE;
|
1999-01-26 08:26:19 +00:00
|
|
|
if ((c == '(' || c == ')') &&
|
|
|
|
(options & ISC_LEXOPT_DNSMULTILINE) != 0) {
|
|
|
|
if (c == '(') {
|
|
|
|
if (lex->paren_count == 0)
|
|
|
|
options &= ~IWSEOL;
|
|
|
|
lex->paren_count++;
|
|
|
|
} else {
|
|
|
|
if (lex->paren_count == 0)
|
|
|
|
return (ISC_R_UNBALANCED);
|
|
|
|
lex->paren_count--;
|
|
|
|
if (lex->paren_count == 0)
|
1999-01-28 18:15:57 +00:00
|
|
|
options =
|
|
|
|
saved_options;
|
1999-01-26 08:26:19 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
1998-12-30 22:11:19 +00:00
|
|
|
tokenp->type = isc_tokentype_special;
|
|
|
|
tokenp->value.as_char = c;
|
|
|
|
done = ISC_TRUE;
|
2000-05-09 22:22:25 +00:00
|
|
|
} else if (isdigit((unsigned char)c) &&
|
1999-05-19 00:48:16 +00:00
|
|
|
(options & ISC_LEXOPT_NUMBER) != 0) {
|
1998-12-30 22:11:19 +00:00
|
|
|
lex->last_was_eol = ISC_FALSE;
|
|
|
|
state = lexstate_number;
|
|
|
|
goto no_read;
|
|
|
|
} else {
|
|
|
|
lex->last_was_eol = ISC_FALSE;
|
|
|
|
state = lexstate_string;
|
|
|
|
goto no_read;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case lexstate_crlf:
|
2000-03-22 17:39:48 +00:00
|
|
|
if (c != '\n')
|
|
|
|
pushback(source, c);
|
1998-12-30 22:11:19 +00:00
|
|
|
tokenp->type = isc_tokentype_eol;
|
|
|
|
done = ISC_TRUE;
|
|
|
|
lex->last_was_eol = ISC_TRUE;
|
|
|
|
break;
|
|
|
|
case lexstate_number:
|
2000-05-09 22:22:25 +00:00
|
|
|
if (c == EOF || !isdigit((unsigned char)c)) {
|
1998-12-30 22:11:19 +00:00
|
|
|
if (c == ' ' || c == '\t' || c == '\r' ||
|
|
|
|
c == '\n' || c == EOF ||
|
|
|
|
lex->specials[c]) {
|
2000-03-22 17:39:48 +00:00
|
|
|
pushback(source, c);
|
1999-06-08 12:45:23 +00:00
|
|
|
ulong = strtoul(lex->data, &e, 0);
|
1999-02-05 06:41:21 +00:00
|
|
|
if (*e == 0) {
|
|
|
|
tokenp->type =
|
|
|
|
isc_tokentype_number;
|
|
|
|
tokenp->value.as_ulong =
|
|
|
|
ulong;
|
|
|
|
} else {
|
|
|
|
isc_tokenvalue_t *v;
|
|
|
|
|
|
|
|
tokenp->type =
|
|
|
|
isc_tokentype_string;
|
|
|
|
v = &(tokenp->value);
|
|
|
|
v->as_textregion.base =
|
|
|
|
lex->data;
|
|
|
|
v->as_textregion.length =
|
|
|
|
lex->max_token -
|
|
|
|
remaining;
|
1999-01-18 08:04:35 +00:00
|
|
|
}
|
1998-12-30 22:11:19 +00:00
|
|
|
done = ISC_TRUE;
|
|
|
|
continue;
|
1999-06-08 12:45:23 +00:00
|
|
|
} else if (!(options & ISC_LEXOPT_CNUMBER) ||
|
|
|
|
((c != 'x' && c != 'X') ||
|
|
|
|
(curr != &lex->data[1]) ||
|
|
|
|
(lex->data[0] != '0'))) {
|
|
|
|
/* Above test supports hex numbers */
|
1998-12-30 22:11:19 +00:00
|
|
|
state = lexstate_string;
|
1999-06-08 12:45:23 +00:00
|
|
|
}
|
1998-12-30 22:11:19 +00:00
|
|
|
}
|
1999-12-23 05:24:12 +00:00
|
|
|
if (remaining == 0) {
|
|
|
|
result = grow_data(lex, &remaining,
|
|
|
|
&curr, &prev);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
INSIST(remaining > 0);
|
|
|
|
*curr++ = c;
|
|
|
|
*curr = '\0';
|
|
|
|
remaining--;
|
1998-12-30 22:11:19 +00:00
|
|
|
break;
|
|
|
|
case lexstate_string:
|
1999-05-19 00:48:16 +00:00
|
|
|
if ((!escaped &&
|
|
|
|
(c == ' ' || c == '\t' || lex->specials[c])) ||
|
|
|
|
c == '\r' || c == '\n' || c == EOF) {
|
2000-03-22 17:39:48 +00:00
|
|
|
pushback(source, c);
|
1998-12-30 22:11:19 +00:00
|
|
|
tokenp->type = isc_tokentype_string;
|
1999-01-09 02:39:05 +00:00
|
|
|
tokenp->value.as_textregion.base = lex->data;
|
|
|
|
tokenp->value.as_textregion.length =
|
1998-12-30 22:11:19 +00:00
|
|
|
lex->max_token - remaining;
|
|
|
|
done = ISC_TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
1999-05-19 00:48:16 +00:00
|
|
|
if ((options & ISC_LEXOPT_ESCAPE) != 0)
|
|
|
|
escaped = (!escaped && c == '\\') ?
|
|
|
|
ISC_TRUE : ISC_FALSE;
|
1999-12-23 05:24:12 +00:00
|
|
|
if (remaining == 0) {
|
|
|
|
result = grow_data(lex, &remaining,
|
|
|
|
&curr, &prev);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
INSIST(remaining > 0);
|
|
|
|
*curr++ = c;
|
|
|
|
*curr = 0;
|
|
|
|
remaining--;
|
1998-12-30 22:11:19 +00:00
|
|
|
break;
|
|
|
|
case lexstate_maybecomment:
|
|
|
|
if (c == '*' &&
|
|
|
|
(lex->comments & ISC_LEXCOMMENT_C) != 0) {
|
|
|
|
state = lexstate_ccomment;
|
|
|
|
continue;
|
|
|
|
} else if (c == '/' &&
|
|
|
|
(lex->comments & ISC_LEXCOMMENT_CPLUSPLUS) != 0) {
|
|
|
|
state = lexstate_eatline;
|
|
|
|
continue;
|
|
|
|
}
|
2000-03-22 17:39:48 +00:00
|
|
|
pushback(source, c);
|
1998-12-30 22:11:19 +00:00
|
|
|
c = '/';
|
|
|
|
no_comments = ISC_FALSE;
|
|
|
|
state = saved_state;
|
|
|
|
goto no_read;
|
|
|
|
case lexstate_ccomment:
|
|
|
|
if (c == EOF)
|
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
|
|
|
if (c == '*')
|
|
|
|
state = lexstate_ccommentend;
|
|
|
|
break;
|
|
|
|
case lexstate_ccommentend:
|
|
|
|
if (c == EOF)
|
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
|
|
|
if (c == '/') {
|
|
|
|
/*
|
|
|
|
* C-style comments become a single space.
|
|
|
|
* We do this to ensure that a comment will
|
|
|
|
* act as a delimiter for strings and
|
|
|
|
* numbers.
|
|
|
|
*/
|
|
|
|
c = ' ';
|
|
|
|
no_comments = ISC_FALSE;
|
|
|
|
state = saved_state;
|
|
|
|
goto no_read;
|
|
|
|
} else
|
|
|
|
state = lexstate_ccomment;
|
|
|
|
break;
|
|
|
|
case lexstate_eatline:
|
|
|
|
if (c == EOF)
|
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
|
|
|
if (c == '\n') {
|
|
|
|
no_comments = ISC_FALSE;
|
|
|
|
state = saved_state;
|
|
|
|
goto no_read;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case lexstate_qstring:
|
|
|
|
if (c == EOF)
|
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
|
|
|
if (c == '"') {
|
|
|
|
if (escaped) {
|
|
|
|
escaped = ISC_FALSE;
|
|
|
|
/*
|
|
|
|
* Overwrite the preceding backslash.
|
|
|
|
*/
|
|
|
|
INSIST(prev != NULL);
|
|
|
|
*prev = '"';
|
|
|
|
} else {
|
|
|
|
tokenp->type = isc_tokentype_qstring;
|
1999-01-09 02:39:05 +00:00
|
|
|
tokenp->value.as_textregion.base =
|
1998-12-30 22:11:19 +00:00
|
|
|
lex->data;
|
1999-01-09 02:39:05 +00:00
|
|
|
tokenp->value.as_textregion.length =
|
1998-12-30 22:11:19 +00:00
|
|
|
lex->max_token - remaining;
|
|
|
|
no_comments = ISC_FALSE;
|
|
|
|
done = ISC_TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (c == '\\' && !escaped)
|
|
|
|
escaped = ISC_TRUE;
|
|
|
|
else
|
|
|
|
escaped = ISC_FALSE;
|
1999-12-23 05:24:12 +00:00
|
|
|
if (remaining == 0) {
|
|
|
|
result = grow_data(lex, &remaining,
|
|
|
|
&curr, &prev);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
INSIST(remaining > 0);
|
|
|
|
prev = curr;
|
|
|
|
*curr++ = c;
|
|
|
|
*curr = 0;
|
|
|
|
remaining--;
|
1998-12-30 22:11:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
1999-01-06 05:38:21 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
|
|
|
"Unexpected state %d", state);
|
|
|
|
/* Does not return. */
|
1998-12-30 22:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} while (!done);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_lex_ungettoken(isc_lex_t *lex, isc_token_t *tokenp) {
|
|
|
|
inputsource *source;
|
|
|
|
/*
|
|
|
|
* Unget the current token.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
source = HEAD(lex->sources);
|
|
|
|
REQUIRE(source != NULL);
|
|
|
|
REQUIRE(!source->have_token);
|
|
|
|
REQUIRE(tokenp != NULL);
|
|
|
|
|
|
|
|
source->token = *tokenp;
|
|
|
|
source->have_token = ISC_TRUE;
|
|
|
|
}
|
1999-02-05 06:41:21 +00:00
|
|
|
|
|
|
|
char *
|
|
|
|
isc_lex_getsourcename(isc_lex_t *lex) {
|
|
|
|
inputsource *source;
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
source = HEAD(lex->sources);
|
|
|
|
|
|
|
|
if (source == NULL)
|
|
|
|
return(NULL);
|
|
|
|
|
|
|
|
return (source->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
isc_lex_getsourceline(isc_lex_t *lex) {
|
|
|
|
inputsource *source;
|
|
|
|
|
|
|
|
REQUIRE(VALID_LEX(lex));
|
|
|
|
source = HEAD(lex->sources);
|
|
|
|
|
|
|
|
if (source == NULL)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
return (source->line);
|
|
|
|
}
|