* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
* \brief The "lex" module provides a lightweight tokenizer. It can operate
* on files or buffers, and can handle "include". It is designed for
* parsing of DNS master files and the BIND configuration file, but
* should be general enough to tokenize other things, e.g. HTTP.
* No synchronization is provided. Clients must ensure exclusive
* Various options for isc_lex_gettoken().
#define ISC_LEXOPT_EOL 0x01 /*%< Want end-of-line token. */
#define ISC_LEXOPT_EOF 0x02 /*%< Want end-of-file token. */
#define ISC_LEXOPT_INITIALWS 0x04 /*%< Want initial whitespace. */
#define ISC_LEXOPT_NUMBER 0x08 /*%< Recognize numbers. */
#define ISC_LEXOPT_QSTRING 0x10 /*%< Recognize qstrings. */
* The ISC_LEXOPT_DNSMULTILINE option handles the processing of '(' and ')' in
* the DNS master file format. If this option is set, then the
* ISC_LEXOPT_INITIALWS and ISC_LEXOPT_EOL options will be ignored when
* the paren count is > 0. To use this option, '(' and ')' must be special
#define ISC_LEXOPT_DNSMULTILINE 0x20 /*%< Handle '(' and ')'. */
#define ISC_LEXOPT_NOMORE 0x40 /*%< Want "no more" token. */
#define ISC_LEXOPT_CNUMBER 0x80 /*%< Recognize octal and hex. */
#define ISC_LEXOPT_ESCAPE 0x100 /*%< Recognize escapes. */
#define ISC_LEXOPT_QSTRINGMULTILINE 0x200 /*%< Allow multiline "" strings */
#define ISC_LEXOPT_OCTAL 0x400 /*%< Expect a octal number. */
#define ISC_LEXOPT_BTEXT 0x800 /*%< Bracketed text. */
* Various commenting styles, which may be changed at any time with
#define ISC_LEXCOMMENT_C 0x01
#define ISC_LEXCOMMENT_CPLUSPLUS 0x02
#define ISC_LEXCOMMENT_SHELL 0x04
#define ISC_LEXCOMMENT_DNSMASTERFILE 0x08
typedef char isc_lexspecials_t[256];
isc_tokentype_unknown = 0,
isc_tokentype_string = 1,
isc_tokentype_number = 2,
isc_tokentype_qstring = 3,
isc_tokentype_initialws = 6,
isc_tokentype_special = 7,
isc_tokentype_nomore = 8,
isc_textregion_t as_textregion;
typedef struct isc_token {
isc_lex_create(isc_mem_t *mctx, size_t max_token, isc_lex_t **lexp);
* 'max_token' is a hint of the number of bytes in the largest token.
*\li '*lexp' is a valid lexer.
*\li On success, *lexp is attached to the newly created lexer.
isc_lex_destroy(isc_lex_t **lexp);
*\li '*lexp' is a valid lexer.
isc_lex_getcomments(isc_lex_t *lex);
* Return the current lexer commenting styles.
*\li 'lex' is a valid lexer.
*\li The commenting styles which are currently allowed.
isc_lex_setcomments(isc_lex_t *lex, unsigned int comments);
* Set allowed lexer commenting styles.
*\li 'lex' is a valid lexer.
*\li 'comments' has meaningful values.
isc_lex_getspecials(isc_lex_t *lex, isc_lexspecials_t specials);
* Put the current list of specials into 'specials'.
*\li 'lex' is a valid lexer.
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.
*\li Comment processing takes precedence over special character
*\li 'lex' is a valid lexer.
isc_lex_openfile(isc_lex_t *lex, const char *filename);
* Open 'filename' and make it the current input source for 'lex'.
*\li 'lex' is a valid lexer.
*\li filename is a valid C string.
*\li #ISC_R_NOMEMORY Out of memory
*\li #ISC_R_NOTFOUND File not found
*\li #ISC_R_NOPERM No permission to open file
*\li #ISC_R_FAILURE Couldn't open file, not sure why
isc_lex_openstream(isc_lex_t *lex, FILE *stream);
* Make 'stream' the current input source for 'lex'.
*\li 'lex' is a valid lexer.
*\li 'stream' is a valid C stream.
*\li #ISC_R_NOMEMORY Out of memory
isc_lex_openbuffer(isc_lex_t *lex, isc_buffer_t *buffer);
* Make 'buffer' the current input source for 'lex'.
*\li 'lex' is a valid lexer.
*\li 'buffer' is a valid buffer.
*\li #ISC_R_NOMEMORY Out of memory
isc_lex_close(isc_lex_t *lex);
* Close the most recently opened object (i.e. file or buffer).
*\li #ISC_R_NOMORE No more input sources
isc_lex_gettoken(isc_lex_t *lex, unsigned int options, isc_token_t *tokenp);
*\li 'lex' is a valid lexer.
*\li 'lex' has an input source.
*\li 'options' contains valid options.
*\li '*tokenp' is a valid pointer.
*\li #ISC_R_UNEXPECTEDEND
* These two results are returned only if their corresponding lexer
*\li #ISC_R_EOF End of input source
*\li #ISC_R_NOMORE No more input sources
isc_lex_getmastertoken(isc_lex_t *lex, isc_token_t *token,
isc_tokentype_t expect, bool eol);
* Get the next token from a DNS master file type stream. This is a
* convenience function that sets appropriate options and handles quoted
* strings and end of line correctly for master files. It also ungets
* unexpected tokens. If `eol` is set then expect end-of-line otherwise
*\li 'lex' is a valid lexer.
*\li 'token' is a valid pointer
* \li any return code from isc_lex_gettoken().
isc_lex_getoctaltoken(isc_lex_t *lex, isc_token_t *token, bool eol);
* Get the next token from a DNS master file type stream. This is a
* convenience function that sets appropriate options and handles end
* of line correctly for master files. It also ungets unexpected tokens.
* If `eol` is set then expect end-of-line otherwise eol is a error.
*\li 'lex' is a valid lexer.
*\li 'token' is a valid pointer
* \li any return code from isc_lex_gettoken().
isc_lex_ungettoken(isc_lex_t *lex, isc_token_t *tokenp);
* Unget the current token.
*\li 'lex' is a valid lexer.
*\li 'lex' has an input source.
*\li 'tokenp' points to a valid token.
*\li There is no ungotten token already.
isc_lex_getlasttokentext(isc_lex_t *lex, isc_token_t *tokenp, isc_region_t *r);
* Returns a region containing the text of the last token returned.
*\li 'lex' is a valid lexer.
*\li 'lex' has an input source.
*\li 'tokenp' points to a valid token.
*\li A token has been gotten and not ungotten.
isc_lex_getsourcename(isc_lex_t *lex);
* Return the input source name.
*\li 'lex' is a valid lexer.
* \li source name or NULL if no current source.
*\li result valid while current input source exists.
isc_lex_getsourceline(isc_lex_t *lex);
* Return the input source line number.
*\li 'lex' is a valid lexer.
*\li Current line number or 0 if no current source.
isc_lex_setsourcename(isc_lex_t *lex, const char *name);
* Assigns a new name to the input source.
* \li 'lex' is a valid lexer.
* \li #ISC_R_NOTFOUND - there are no sources.
isc_lex_setsourceline(isc_lex_t *lex, unsigned long line);
* Assigns a new line number to the input source. This can be used
* when parsing a buffer that's been excerpted from the middle a file,
* allowing logged messages to display the correct line number,
* rather than the line number within the buffer.
* \li 'lex' is a valid lexer.
* \li #ISC_R_NOTFOUND - there are no sources.
isc_lex_isfile(isc_lex_t *lex);
* Return whether the current input source is a file.
*\li 'lex' is a valid lexer.
* \li #true if the current input is a file,