1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $Id: grammar.h,v 1.6 2020/09/15 08:19:29 florian Exp $ */
18 
19 #ifndef ISCCFG_GRAMMAR_H
20 #define ISCCFG_GRAMMAR_H 1
21 
22 /*! \file isccfg/grammar.h */
23 
24 #include <isc/lex.h>
25 #include <isc/sockaddr.h>
26 #include <isc/region.h>
27 #include <isc/types.h>
28 
29 #include <isccfg/cfg.h>
30 
31 /*
32  * Definitions shared between the configuration parser
33  * and the grammars; not visible to users of the parser.
34  */
35 
36 typedef struct cfg_clausedef cfg_clausedef_t;
37 typedef ISC_LIST(cfg_listelt_t) cfg_list_t;
38 typedef struct cfg_map cfg_map_t;
39 typedef struct cfg_rep cfg_rep_t;
40 
41 /*
42  * Function types for configuration object methods
43  */
44 
45 typedef isc_result_t (*cfg_parsefunc_t)(cfg_parser_t *, const cfg_type_t *type,
46 					cfg_obj_t **);
47 typedef void	     (*cfg_freefunc_t)(cfg_parser_t *, cfg_obj_t *);
48 
49 /*
50  * Structure definitions
51  */
52 
53 /*% A clause definition. */
54 struct cfg_clausedef {
55 	const char      *name;
56 	cfg_type_t      *type;
57 	unsigned int	flags;
58 };
59 
60 /*% A configuration object type definition. */
61 struct cfg_type {
62 	const char *name;	/*%< For debugging purposes only */
63 	cfg_parsefunc_t	parse;
64 	cfg_rep_t *	rep;	/*%< Data representation */
65 	const void *	of;	/*%< Additional data for meta-types */
66 };
67 
68 struct cfg_map {
69 	cfg_obj_t	 *id; /*%< Used for 'named maps' like keys, zones, &c */
70 	const cfg_clausedef_t * const *clausesets; /*%< The clauses that
71 						      can occur in this map;
72 						      used for printing */
73 	isc_symtab_t     *symtab;
74 };
75 
76 /*%
77  * A configuration data representation.
78  */
79 struct cfg_rep {
80 	const char *	name;	/*%< For debugging only */
81 	cfg_freefunc_t 	free;	/*%< How to free this kind of data. */
82 };
83 
84 /*%
85  * A configuration object.  This is the main building block
86  * of the configuration parse tree.
87  */
88 
89 struct cfg_obj {
90 	const cfg_type_t *type;
91 	union {
92 		isc_textregion_t string; /*%< null terminated, too */
93 		cfg_map_t	map;
94 		cfg_list_t	list;
95 	}               value;
96 	const char *	file;
97 	unsigned int    line;
98 };
99 
100 /*% A list element. */
101 struct cfg_listelt {
102 	cfg_obj_t               *obj;
103 	ISC_LINK(cfg_listelt_t)  link;
104 };
105 
106 /*% The parser object. */
107 struct cfg_parser {
108 	isc_log_t *	lctx;
109 	isc_lex_t *	lexer;
110 	unsigned int    errors;
111 	isc_token_t     token;
112 
113 	/*% We are at the end of all input. */
114 	int	seen_eof;
115 
116 	/*% The current token has been pushed back. */
117 	int	ungotten;
118 
119 	/*%
120 	 * The stack of currently active files, represented
121 	 * as a configuration list of configuration strings.
122 	 * The head is the top-level file, subsequent elements
123 	 * (if any) are the nested include files, and the
124 	 * last element is the file currently being parsed.
125 	 */
126 	cfg_obj_t *	open_files;
127 
128 	/*%
129 	 * Names of files that we have parsed and closed
130 	 * and were previously on the open_file list.
131 	 * We keep these objects around after closing
132 	 * the files because the file names may still be
133 	 * referenced from other configuration objects
134 	 * for use in reporting semantic errors after
135 	 * parsing is complete.
136 	 */
137 	cfg_obj_t *	closed_files;
138 
139 	/*%
140 	 * Current line number.  We maintain our own
141 	 * copy of this so that it is available even
142 	 * when a file has just been closed.
143 	 */
144 	unsigned int	line;
145 
146 	/*%
147 	 * Parser context flags, used for maintaining state
148 	 * from one token to the next.
149 	 */
150 	unsigned int flags;
151 };
152 
153 /*@{*/
154 /*%
155  * Predefined data representation types.
156  */
157 extern cfg_rep_t cfg_rep_map;
158 /*@}*/
159 
160 /*@{*/
161 /*%
162  * Predefined configuration object types.
163  */
164 extern cfg_type_t cfg_type_astring;
165 extern cfg_type_t cfg_type_sstring;
166 /*@}*/
167 
168 isc_result_t
169 cfg_parse_named_map(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
170 
171 isc_result_t
172 cfg_parse_mapbody(cfg_parser_t *pctx, const cfg_type_t *type, cfg_obj_t **ret);
173 
174 #endif /* ISCCFG_GRAMMAR_H */
175