PageRenderTime 51ms CodeModel.GetById 2ms app.highlight 43ms RepoModel.GetById 2ms app.codeStats 0ms

/contrib/bind9/lib/isccfg/include/isccfg/grammar.h

https://bitbucket.org/freebsd/freebsd-head/
C++ Header | 474 lines | 259 code | 95 blank | 120 comment | 0 complexity | 89df11fc36fe8f1c93cb2ffb1fc98359 MD5 | raw file
  1/*
  2 * Copyright (C) 2004-2011  Internet Systems Consortium, Inc. ("ISC")
  3 * Copyright (C) 2002, 2003  Internet Software Consortium.
  4 *
  5 * Permission to use, copy, modify, and/or distribute this software for any
  6 * purpose with or without fee is hereby granted, provided that the above
  7 * copyright notice and this permission notice appear in all copies.
  8 *
  9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 15 * PERFORMANCE OF THIS SOFTWARE.
 16 */
 17
 18/* $Id: grammar.h,v 1.24 2011/01/04 23:47:14 tbox Exp $ */
 19
 20#ifndef ISCCFG_GRAMMAR_H
 21#define ISCCFG_GRAMMAR_H 1
 22
 23/*! \file isccfg/grammar.h */
 24
 25#include <isc/lex.h>
 26#include <isc/netaddr.h>
 27#include <isc/sockaddr.h>
 28#include <isc/region.h>
 29#include <isc/types.h>
 30
 31#include <isccfg/cfg.h>
 32
 33/*
 34 * Definitions shared between the configuration parser
 35 * and the grammars; not visible to users of the parser.
 36 */
 37
 38/*% Clause may occur multiple times (e.g., "zone") */
 39#define CFG_CLAUSEFLAG_MULTI 		0x00000001
 40/*% Clause is obsolete */
 41#define CFG_CLAUSEFLAG_OBSOLETE 	0x00000002
 42/*% Clause is not implemented, and may never be */
 43#define CFG_CLAUSEFLAG_NOTIMP	 	0x00000004
 44/*% Clause is not implemented yet */
 45#define CFG_CLAUSEFLAG_NYI 		0x00000008
 46/*% Default value has changed since earlier release */
 47#define CFG_CLAUSEFLAG_NEWDEFAULT	0x00000010
 48/*%
 49 * Clause needs to be interpreted during parsing
 50 * by calling a callback function, like the
 51 * "directory" option.
 52 */
 53#define CFG_CLAUSEFLAG_CALLBACK		0x00000020
 54/*% A option that is only used in testing. */
 55#define CFG_CLAUSEFLAG_TESTONLY		0x00000040
 56/*% A configuration option that was not configured at compile time. */
 57#define CFG_CLAUSEFLAG_NOTCONFIGURED	0x00000080
 58
 59typedef struct cfg_clausedef cfg_clausedef_t;
 60typedef struct cfg_tuplefielddef cfg_tuplefielddef_t;
 61typedef struct cfg_printer cfg_printer_t;
 62typedef ISC_LIST(cfg_listelt_t) cfg_list_t;
 63typedef struct cfg_map cfg_map_t;
 64typedef struct cfg_rep cfg_rep_t;
 65
 66/*
 67 * Function types for configuration object methods
 68 */
 69
 70typedef isc_result_t (*cfg_parsefunc_t)(cfg_parser_t *, const cfg_type_t *type,
 71					cfg_obj_t **);
 72typedef void	     (*cfg_printfunc_t)(cfg_printer_t *, const cfg_obj_t *);
 73typedef void	     (*cfg_docfunc_t)(cfg_printer_t *, const cfg_type_t *);
 74typedef void	     (*cfg_freefunc_t)(cfg_parser_t *, cfg_obj_t *);
 75
 76/*
 77 * Structure definitions
 78 */
 79
 80/*%
 81 * A configuration printer object.  This is an abstract
 82 * interface to a destination to which text can be printed
 83 * by calling the function 'f'.
 84 */
 85struct cfg_printer {
 86	void (*f)(void *closure, const char *text, int textlen);
 87	void *closure;
 88	int indent;
 89};
 90
 91/*% A clause definition. */
 92struct cfg_clausedef {
 93	const char      *name;
 94	cfg_type_t      *type;
 95	unsigned int	flags;
 96};
 97
 98/*% A tuple field definition. */
 99struct cfg_tuplefielddef {
100	const char      *name;
101	cfg_type_t      *type;
102	unsigned int	flags;
103};
104
105/*% A configuration object type definition. */
106struct cfg_type {
107	const char *name;	/*%< For debugging purposes only */
108	cfg_parsefunc_t	parse;
109	cfg_printfunc_t print;
110	cfg_docfunc_t	doc;	/*%< Print grammar description */
111	cfg_rep_t *	rep;	/*%< Data representation */
112	const void *	of;	/*%< Additional data for meta-types */
113};
114
115/*% A keyword-type definition, for things like "port <integer>". */
116typedef struct {
117	const char *name;
118	const cfg_type_t *type;
119} keyword_type_t;
120
121struct cfg_map {
122	cfg_obj_t	 *id; /*%< Used for 'named maps' like keys, zones, &c */
123	const cfg_clausedef_t * const *clausesets; /*%< The clauses that
124						      can occur in this map;
125						      used for printing */
126	isc_symtab_t     *symtab;
127};
128
129typedef struct cfg_netprefix cfg_netprefix_t;
130
131struct cfg_netprefix {
132	isc_netaddr_t address; /* IP4/IP6 */
133	unsigned int prefixlen;
134};
135
136/*%
137 * A configuration data representation.
138 */
139struct cfg_rep {
140	const char *	name;	/*%< For debugging only */
141	cfg_freefunc_t 	free;	/*%< How to free this kind of data. */
142};
143
144/*%
145 * A configuration object.  This is the main building block
146 * of the configuration parse tree.
147 */
148
149struct cfg_obj {
150	const cfg_type_t *type;
151	union {
152		isc_uint32_t  	uint32;
153		isc_uint64_t  	uint64;
154		isc_textregion_t string; /*%< null terminated, too */
155		isc_boolean_t 	boolean;
156		cfg_map_t	map;
157		cfg_list_t	list;
158		cfg_obj_t **	tuple;
159		isc_sockaddr_t	sockaddr;
160		cfg_netprefix_t netprefix;
161	}               value;
162	isc_refcount_t  references;     /*%< reference counter */
163	const char *	file;
164	unsigned int    line;
165};
166
167
168/*% A list element. */
169struct cfg_listelt {
170	cfg_obj_t               *obj;
171	ISC_LINK(cfg_listelt_t)  link;
172};
173
174/*% The parser object. */
175struct cfg_parser {
176	isc_mem_t *	mctx;
177	isc_log_t *	lctx;
178	isc_lex_t *	lexer;
179	unsigned int    errors;
180	unsigned int    warnings;
181	isc_token_t     token;
182
183	/*% We are at the end of all input. */
184	isc_boolean_t	seen_eof;
185
186	/*% The current token has been pushed back. */
187	isc_boolean_t	ungotten;
188
189	/*%
190	 * The stack of currently active files, represented
191	 * as a configuration list of configuration strings.
192	 * The head is the top-level file, subsequent elements
193	 * (if any) are the nested include files, and the
194	 * last element is the file currently being parsed.
195	 */
196	cfg_obj_t *	open_files;
197
198	/*%
199	 * Names of files that we have parsed and closed
200	 * and were previously on the open_file list.
201	 * We keep these objects around after closing
202	 * the files because the file names may still be
203	 * referenced from other configuration objects
204	 * for use in reporting semantic errors after
205	 * parsing is complete.
206	 */
207	cfg_obj_t *	closed_files;
208
209	/*%
210	 * Current line number.  We maintain our own
211	 * copy of this so that it is available even
212	 * when a file has just been closed.
213	 */
214	unsigned int	line;
215
216	/*%
217	 * Parser context flags, used for maintaining state
218	 * from one token to the next.
219	 */
220	unsigned int flags;
221
222	/*%< Reference counter */
223	isc_refcount_t  references;
224
225	cfg_parsecallback_t callback;
226	void *callbackarg;
227};
228
229/* Parser context flags */
230#define CFG_PCTX_SKIP		0x1
231
232/*@{*/
233/*%
234 * Flags defining whether to accept certain types of network addresses.
235 */
236#define CFG_ADDR_V4OK 		0x00000001
237#define CFG_ADDR_V4PREFIXOK 	0x00000002
238#define CFG_ADDR_V6OK 		0x00000004
239#define CFG_ADDR_WILDOK		0x00000008
240#define CFG_ADDR_MASK		(CFG_ADDR_V6OK|CFG_ADDR_V4OK)
241/*@}*/
242
243/*@{*/
244/*%
245 * Predefined data representation types.
246 */
247LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_uint32;
248LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_uint64;
249LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_string;
250LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_boolean;
251LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_map;
252LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_list;
253LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_tuple;
254LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_sockaddr;
255LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_netprefix;
256LIBISCCFG_EXTERNAL_DATA extern cfg_rep_t cfg_rep_void;
257/*@}*/
258
259/*@{*/
260/*%
261 * Predefined configuration object types.
262 */
263LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_boolean;
264LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_uint32;
265LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_uint64;
266LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_qstring;
267LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_astring;
268LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_ustring;
269LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_sockaddr;
270LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_netaddr;
271LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_netaddr4;
272LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_netaddr4wild;
273LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_netaddr6;
274LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_netaddr6wild;
275LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_netprefix;
276LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_void;
277LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_token;
278LIBISCCFG_EXTERNAL_DATA extern cfg_type_t cfg_type_unsupported;
279/*@}*/
280
281isc_result_t
282cfg_gettoken(cfg_parser_t *pctx, int options);
283
284isc_result_t
285cfg_peektoken(cfg_parser_t *pctx, int options);
286
287void
288cfg_ungettoken(cfg_parser_t *pctx);
289
290#define CFG_LEXOPT_QSTRING (ISC_LEXOPT_QSTRING | ISC_LEXOPT_QSTRINGMULTILINE)
291
292isc_result_t
293cfg_create_obj(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);
294
295void
296cfg_print_rawuint(cfg_printer_t *pctx, unsigned int u);
297
298isc_result_t
299cfg_parse_uint32(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
300
301void
302cfg_print_uint32(cfg_printer_t *pctx, const cfg_obj_t *obj);
303
304void
305cfg_print_uint64(cfg_printer_t *pctx, const cfg_obj_t *obj);
306
307isc_result_t
308cfg_parse_qstring(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
309
310void
311cfg_print_ustring(cfg_printer_t *pctx, const cfg_obj_t *obj);
312
313isc_result_t
314cfg_parse_astring(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
315
316isc_result_t
317cfg_parse_rawaddr(cfg_parser_t *pctx, unsigned int flags, isc_netaddr_t *na);
318
319void
320cfg_print_rawaddr(cfg_printer_t *pctx, const isc_netaddr_t *na);
321
322isc_boolean_t
323cfg_lookingat_netaddr(cfg_parser_t *pctx, unsigned int flags);
324
325isc_result_t
326cfg_parse_rawport(cfg_parser_t *pctx, unsigned int flags, in_port_t *port);
327
328isc_result_t
329cfg_parse_sockaddr(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
330
331isc_result_t
332cfg_parse_boolean(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
333
334void
335cfg_print_sockaddr(cfg_printer_t *pctx, const cfg_obj_t *obj);
336
337void
338cfg_print_boolean(cfg_printer_t *pctx, const cfg_obj_t *obj);
339
340void
341cfg_doc_sockaddr(cfg_printer_t *pctx, const cfg_type_t *type);
342
343isc_result_t
344cfg_parse_netprefix(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
345
346isc_result_t
347cfg_parse_special(cfg_parser_t *pctx, int special);
348/*%< Parse a required special character 'special'. */
349
350isc_result_t
351cfg_create_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);
352
353isc_result_t
354cfg_parse_tuple(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
355
356void
357cfg_print_tuple(cfg_printer_t *pctx, const cfg_obj_t *obj);
358
359void
360cfg_doc_tuple(cfg_printer_t *pctx, const cfg_type_t *type);
361
362isc_result_t
363cfg_create_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **objp);
364
365isc_result_t
366cfg_parse_listelt(cfg_parser_t *pctx, const cfg_type_t *elttype,
367		  cfg_listelt_t **ret);
368
369isc_result_t
370cfg_parse_bracketed_list(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
371
372void
373cfg_print_bracketed_list(cfg_printer_t *pctx, const cfg_obj_t *obj);
374
375void
376cfg_doc_bracketed_list(cfg_printer_t *pctx, const cfg_type_t *type);
377
378isc_result_t
379cfg_parse_spacelist(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
380
381void
382cfg_print_spacelist(cfg_printer_t *pctx, const cfg_obj_t *obj);
383
384isc_result_t
385cfg_parse_enum(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
386
387void
388cfg_doc_enum(cfg_printer_t *pctx, const cfg_type_t *type);
389
390void
391cfg_print_chars(cfg_printer_t *pctx, const char *text, int len);
392/*%< Print 'len' characters at 'text' */
393
394void
395cfg_print_cstr(cfg_printer_t *pctx, const char *s);
396/*%< Print the null-terminated string 's' */
397
398isc_result_t
399cfg_parse_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
400
401isc_result_t
402cfg_parse_named_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
403
404isc_result_t
405cfg_parse_addressed_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
406
407isc_result_t
408cfg_parse_netprefix_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **
409ret);
410
411void
412cfg_print_map(cfg_printer_t *pctx, const cfg_obj_t *obj);
413
414void
415cfg_doc_map(cfg_printer_t *pctx, const cfg_type_t *type);
416
417isc_result_t
418cfg_parse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
419
420void
421cfg_print_mapbody(cfg_printer_t *pctx, const cfg_obj_t *obj);
422
423void
424cfg_doc_mapbody(cfg_printer_t *pctx, const cfg_type_t *type);
425
426isc_result_t
427cfg_parse_void(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
428
429void
430cfg_print_void(cfg_printer_t *pctx, const cfg_obj_t *obj);
431
432void
433cfg_doc_void(cfg_printer_t *pctx, const cfg_type_t *type);
434
435isc_result_t
436cfg_parse_obj(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
437
438void
439cfg_print_obj(cfg_printer_t *pctx, const cfg_obj_t *obj);
440
441void
442cfg_doc_obj(cfg_printer_t *pctx, const cfg_type_t *type);
443/*%<
444 * Print a description of the grammar of an arbitrary configuration
445 * type 'type'
446 */
447
448void
449cfg_doc_terminal(cfg_printer_t *pctx, const cfg_type_t *type);
450/*%<
451 * Document the type 'type' as a terminal by printing its
452 * name in angle brackets, e.g., &lt;uint32>.
453 */
454
455void
456cfg_parser_error(cfg_parser_t *pctx, unsigned int flags,
457		 const char *fmt, ...) ISC_FORMAT_PRINTF(3, 4);
458/*!
459 * Pass one of these flags to cfg_parser_error() to include the
460 * token text in log message.
461 */
462#define CFG_LOG_NEAR    0x00000001	/*%< Say "near <token>" */
463#define CFG_LOG_BEFORE  0x00000002	/*%< Say "before <token>" */
464#define CFG_LOG_NOPREP  0x00000004	/*%< Say just "<token>" */
465
466void
467cfg_parser_warning(cfg_parser_t *pctx, unsigned int flags,
468		   const char *fmt, ...) ISC_FORMAT_PRINTF(3, 4);
469
470isc_boolean_t
471cfg_is_enum(const char *s, const char *const *enums);
472/*%< Return true iff the string 's' is one of the strings in 'enums' */
473
474#endif /* ISCCFG_GRAMMAR_H */