📄 parser.c
字号:
/* * Copyright (C) 2000-2003 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. *//* $Id: parser.c,v 1.70.2.22 2003/09/19 13:41:36 marka Exp $ */#include <config.h>#include <isc/buffer.h>#include <isc/dir.h>#include <isc/formatcheck.h>#include <isc/lex.h>#include <isc/log.h>#include <isc/mem.h>#include <isc/net.h>#include <isc/netaddr.h>#include <isc/print.h>#include <isc/string.h>#include <isc/sockaddr.h>#include <isc/util.h>#include <isc/symtab.h>#include <isccfg/cfg.h>#include <isccfg/log.h>/* Shorthand */#define CAT CFG_LOGCATEGORY_CONFIG#define MOD CFG_LOGMODULE_PARSER#define QSTRING (ISC_LEXOPT_QSTRING | ISC_LEXOPT_QSTRINGMULTILINE)/* * Pass one of these flags to parser_error() to include the * token text in log message. */#define LOG_NEAR 0x00000001 /* Say "near <token>" */#define LOG_BEFORE 0x00000002 /* Say "before <token>" */#define LOG_NOPREP 0x00000004 /* Say just "<token>" */#define MAP_SYM 1 /* Unique type for isc_symtab *//* Clause may occur multiple times (e.g., "zone") */#define CFG_CLAUSEFLAG_MULTI 0x00000001/* Clause is obsolete */#define CFG_CLAUSEFLAG_OBSOLETE 0x00000002/* Clause is not implemented, and may never be */#define CFG_CLAUSEFLAG_NOTIMP 0x00000004/* Clause is not implemented yet */#define CFG_CLAUSEFLAG_NYI 0x00000008/* Default value has changed since earlier release */#define CFG_CLAUSEFLAG_NEWDEFAULT 0x00000010/* * Clause needs to be interpreted during parsing * by calling a callback function, like the * "directory" option. */#define CFG_CLAUSEFLAG_CALLBACK 0x00000020/* * Flags defining whether to accept certain types of network addresses. */#define V4OK 0x00000001#define V4PREFIXOK 0x00000002#define V6OK 0x00000004#define WILDOK 0x00000008/* Check a return value. */#define CHECK(op) \ do { result = (op); \ if (result != ISC_R_SUCCESS) goto cleanup; \ } while (0)/* Clean up a configuration object if non-NULL. */#define CLEANUP_OBJ(obj) \ do { if ((obj) != NULL) cfg_obj_destroy(pctx, &(obj)); } while (0)typedef struct cfg_clausedef cfg_clausedef_t;typedef struct cfg_tuplefielddef cfg_tuplefielddef_t;typedef struct cfg_printer cfg_printer_t;typedef ISC_LIST(cfg_listelt_t) cfg_list_t;typedef struct cfg_map cfg_map_t;typedef struct cfg_rep cfg_rep_t;/* * Function types for configuration object methods */typedef isc_result_t (*cfg_parsefunc_t)(cfg_parser_t *, const cfg_type_t *type, cfg_obj_t **);typedef void (*cfg_printfunc_t)(cfg_printer_t *, cfg_obj_t *);typedef void (*cfg_freefunc_t)(cfg_parser_t *, cfg_obj_t *);/* * Structure definitions *//* The parser object. */struct cfg_parser { isc_mem_t * mctx; isc_log_t * lctx; isc_lex_t * lexer; unsigned int errors; unsigned int warnings; isc_token_t token; /* We are at the end of all input. */ isc_boolean_t seen_eof; /* The current token has been pushed back. */ isc_boolean_t ungotten; /* * The stack of currently active files, represented * as a configuration list of configuration strings. * The head is the top-level file, subsequent elements * (if any) are the nested include files, and the * last element is the file currently being parsed. */ cfg_obj_t * open_files; /* * Names of files that we have parsed and closed * and were previously on the open_file list. * We keep these objects around after closing * the files because the file names may still be * referenced from other configuration objects * for use in reporting semantic errors after * parsing is complete. */ cfg_obj_t * closed_files; /* * Current line number. We maintain our own * copy of this so that it is available even * when a file has just been closed. */ unsigned int line; cfg_parsecallback_t callback; void *callbackarg;};/* * A configuration printer object. This is an abstract * interface to a destination to which text can be printed * by calling the function 'f'. */struct cfg_printer { void (*f)(void *closure, const char *text, int textlen); void *closure; int indent;};/* A clause definition. */struct cfg_clausedef { const char *name; cfg_type_t *type; unsigned int flags;};/* A tuple field definition. */struct cfg_tuplefielddef { const char *name; cfg_type_t *type; unsigned int flags;};/* A configuration object type definition. */struct cfg_type { const char *name; /* For debugging purposes only */ cfg_parsefunc_t parse; cfg_printfunc_t print; cfg_rep_t * rep; /* Data representation */ const void * of; /* For meta-types */};/* A keyword-type definition, for things like "port <integer>". */typedef struct { const char *name; const cfg_type_t *type;} keyword_type_t;struct cfg_map { cfg_obj_t *id; /* Used for 'named maps' like keys, zones, &c */ const cfg_clausedef_t * const *clausesets; /* The clauses that can occur in this map; used for printing */ isc_symtab_t *symtab;};typedef struct cfg_netprefix cfg_netprefix_t;struct cfg_netprefix { isc_netaddr_t address; /* IP4/IP6 */ unsigned int prefixlen;};/* * A configuration data representation. */struct cfg_rep { const char * name; /* For debugging only */ cfg_freefunc_t free; /* How to free this kind of data. */};/* * A configuration object. This is the main building block * of the configuration parse tree. */struct cfg_obj { const cfg_type_t *type; union { isc_uint32_t uint32; isc_uint64_t uint64; isc_textregion_t string; /* null terminated, too */ isc_boolean_t boolean; cfg_map_t map; cfg_list_t list; cfg_obj_t ** tuple; isc_sockaddr_t sockaddr; cfg_netprefix_t netprefix; } value; char * file; unsigned int line;};/* A list element. */struct cfg_listelt { cfg_obj_t *obj; ISC_LINK(cfg_listelt_t) link;};/* * Forward declarations of static functions. */static isc_result_tcreate_cfgobj(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);static isc_result_tcreate_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);static isc_result_tcreate_listelt(cfg_parser_t *pctx, cfg_listelt_t **eltp);static voidfree_list(cfg_parser_t *pctx, cfg_obj_t *obj);static isc_result_tcreate_string(cfg_parser_t *pctx, const char *contents, const cfg_type_t *type, cfg_obj_t **ret);static voidfree_string(cfg_parser_t *pctx, cfg_obj_t *obj);static isc_result_tcreate_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);static isc_result_tcreate_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);static voidfree_map(cfg_parser_t *pctx, cfg_obj_t *obj);static isc_result_tget_addr(cfg_parser_t *pctx, unsigned int flags, isc_netaddr_t *na);static voidprint(cfg_printer_t *pctx, const char *text, int len);static voidprint_void(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_enum_or_other(cfg_parser_t *pctx, const cfg_type_t *enumtype, const cfg_type_t *othertype, cfg_obj_t **ret);static isc_result_tparse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static voidprint_mapbody(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static voidprint_map(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_named_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static isc_result_tparse_addressed_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static isc_result_tparse_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static voidprint_list(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static voidprint_tuple(cfg_printer_t *pctx, cfg_obj_t *obj);static voidfree_tuple(cfg_parser_t *pctx, cfg_obj_t *obj);static isc_result_tparse_spacelist(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static voidprint_spacelist(cfg_printer_t *pctx, cfg_obj_t *obj);static voidprint_sockaddr(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_addrmatchelt(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static isc_result_tparse_bracketed_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static voidprint_bracketed_list(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static isc_result_tparse_optional_keyvalue(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);static voidprint_keyvalue(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_symtab_elt(cfg_parser_t *pctx, const char *name, cfg_type_t *elttype, isc_symtab_t *symtab, isc_boolean_t callback);static voidfree_noop(cfg_parser_t *pctx, cfg_obj_t *obj);static isc_result_tcfg_gettoken(cfg_parser_t *pctx, int options);static voidcfg_ungettoken(cfg_parser_t *pctx);static isc_result_tcfg_peektoken(cfg_parser_t *pctx, int options);static isc_result_tcfg_getstringtoken(cfg_parser_t *pctx);static voidparser_error(cfg_parser_t *pctx, unsigned int flags, const char *fmt, ...) ISC_FORMAT_PRINTF(3, 4);static voidparser_warning(cfg_parser_t *pctx, unsigned int flags, const char *fmt, ...) ISC_FORMAT_PRINTF(3, 4);static voidparser_complain(cfg_parser_t *pctx, isc_boolean_t is_warning, unsigned int flags, const char *format, va_list args);static voidprint_uint32(cfg_printer_t *pctx, cfg_obj_t *obj);static voidprint_ustring(cfg_printer_t *pctx, cfg_obj_t *obj);static isc_result_tparse_enum(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);/* * Data representations. These correspond to members of the * "value" union in struct cfg_obj (except "void", which does * not need a union member). */cfg_rep_t cfg_rep_uint32 = { "uint32", free_noop };cfg_rep_t cfg_rep_uint64 = { "uint64", free_noop };cfg_rep_t cfg_rep_string = { "string", free_string };cfg_rep_t cfg_rep_boolean = { "boolean", free_noop };cfg_rep_t cfg_rep_map = { "map", free_map };cfg_rep_t cfg_rep_list = { "list", free_list };cfg_rep_t cfg_rep_tuple = { "tuple", free_tuple };cfg_rep_t cfg_rep_sockaddr = { "sockaddr", free_noop };cfg_rep_t cfg_rep_netprefix = { "netprefix", free_noop };cfg_rep_t cfg_rep_void = { "void", free_noop };/* * Forward declarations of configuration type definitions. * Additional types are declared publicly in cfg.h. */static cfg_type_t cfg_type_boolean;static cfg_type_t cfg_type_uint32;static cfg_type_t cfg_type_qstring;static cfg_type_t cfg_type_astring;static cfg_type_t cfg_type_ustring;static cfg_type_t cfg_type_optional_port;static cfg_type_t cfg_type_bracketed_aml;static cfg_type_t cfg_type_acl;static cfg_type_t cfg_type_portiplist;static cfg_type_t cfg_type_bracketed_sockaddrlist;static cfg_type_t cfg_type_sockaddr;static cfg_type_t cfg_type_netaddr;static cfg_type_t cfg_type_optional_keyref;static cfg_type_t cfg_type_options;static cfg_type_t cfg_type_view;static cfg_type_t cfg_type_viewopts;static cfg_type_t cfg_type_key;static cfg_type_t cfg_type_server;static cfg_type_t cfg_type_controls;static cfg_type_t cfg_type_bracketed_sockaddrkeylist;static cfg_type_t cfg_type_querysource4;static cfg_type_t cfg_type_querysource6;static cfg_type_t cfg_type_querysource;static cfg_type_t cfg_type_sockaddr4wild;static cfg_type_t cfg_type_sockaddr6wild;static cfg_type_t cfg_type_sockaddr;static cfg_type_t cfg_type_netprefix;static cfg_type_t cfg_type_zone;static cfg_type_t cfg_type_zoneopts;static cfg_type_t cfg_type_logging;static cfg_type_t cfg_type_optional_facility;static cfg_type_t cfg_type_void;static cfg_type_t cfg_type_optional_class;static cfg_type_t cfg_type_destinationlist;static cfg_type_t cfg_type_size;static cfg_type_t cfg_type_sizenodefault;static cfg_type_t cfg_type_negated;static cfg_type_t cfg_type_addrmatchelt;static cfg_type_t cfg_type_unsupported;static cfg_type_t cfg_type_token;static cfg_type_t cfg_type_server_key_kludge;static cfg_type_t cfg_type_optional_facility;static cfg_type_t cfg_type_logseverity;static cfg_type_t cfg_type_logfile;static cfg_type_t cfg_type_lwres;static cfg_type_t cfg_type_controls_sockaddr;static cfg_type_t cfg_type_notifytype;static cfg_type_t cfg_type_dialuptype;/* * Configuration type definitions. *//* tkey-dhkey */static cfg_tuplefielddef_t tkey_dhkey_fields[] = { { "name", &cfg_type_qstring, 0 }, { "keyid", &cfg_type_uint32, 0 }, { NULL, NULL, 0 }};static cfg_type_t cfg_type_tkey_dhkey = { "tkey-dhkey", parse_tuple, print_tuple, &cfg_rep_tuple, tkey_dhkey_fields};/* listen-on */static cfg_tuplefielddef_t listenon_fields[] = { { "port", &cfg_type_optional_port, 0 }, { "acl", &cfg_type_bracketed_aml, 0 }, { NULL, NULL, 0 }};static cfg_type_t cfg_type_listenon = { "listenon", parse_tuple, print_tuple, &cfg_rep_tuple, listenon_fields };/* acl */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -