2 * Copyright (C) 2004-2007, 2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
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.
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.
18 /* $Id: cfg.h,v 1.46 2010/08/13 23:47:04 tbox Exp $ */
21 #define ISCCFG_CFG_H 1
27 /*! \file isccfg/cfg.h
29 * This is the new, table-driven, YACC-free configuration file parser.
36 #include <isc/formatcheck.h>
38 #include <isc/refcount.h>
39 #include <isc/types.h>
48 * A configuration parser.
50 typedef struct cfg_parser cfg_parser_t;
53 * A configuration type definition object. There is a single
54 * static cfg_type_t object for each data type supported by
55 * the configuration parser.
57 typedef struct cfg_type cfg_type_t;
60 * A configuration object. This is the basic building block of the
61 * configuration parse tree. It contains a value (which may be
62 * of one of several types) and information identifying the file
63 * and line number the value came from, for printing error
66 typedef struct cfg_obj cfg_obj_t;
69 * A configuration object list element.
71 typedef struct cfg_listelt cfg_listelt_t;
74 * A callback function to be called when parsing an option
75 * that needs to be interpreted at parsing time, like
79 (*cfg_parsecallback_t)(const char *clausename, const cfg_obj_t *obj, void *arg);
88 cfg_parser_attach(cfg_parser_t *src, cfg_parser_t **dest);
90 * Reference a parser object.
94 cfg_parser_create(isc_mem_t *mctx, isc_log_t *lctx, cfg_parser_t **ret);
96 * Create a configuration file parser. Any warning and error
97 * messages will be logged to 'lctx'.
99 * The parser object returned can be used for a single call
100 * to cfg_parse_file() or cfg_parse_buffer(). It must not
101 * be reused for parsing multiple files or buffers.
105 cfg_parser_setcallback(cfg_parser_t *pctx,
106 cfg_parsecallback_t callback,
109 * Make the parser call 'callback' whenever it encounters
110 * a configuration clause with the callback attribute,
111 * passing it the clause name, the clause value,
112 * and 'arg' as arguments.
114 * To restore the default of not invoking callbacks, pass
115 * callback==NULL and arg==NULL.
119 cfg_parse_file(cfg_parser_t *pctx, const char *filename,
120 const cfg_type_t *type, cfg_obj_t **ret);
122 cfg_parse_buffer(cfg_parser_t *pctx, isc_buffer_t *buffer,
123 const cfg_type_t *type, cfg_obj_t **ret);
125 * Read a configuration containing data of type 'type'
126 * and make '*ret' point to its parse tree.
128 * The configuration is read from the file 'filename'
129 * (isc_parse_file()) or the buffer 'buffer'
130 * (isc_parse_buffer()).
132 * Returns an error if the file does not parse correctly.
135 *\li "filename" is valid.
137 *\li "type" is valid.
138 *\li "cfg" is non-NULL and "*cfg" is NULL.
141 * \li #ISC_R_SUCCESS - success
142 *\li #ISC_R_NOMEMORY - no memory available
143 *\li #ISC_R_INVALIDFILE - file doesn't exist or is unreadable
144 *\li others - file contains errors
148 cfg_parser_destroy(cfg_parser_t **pctxp);
150 * Remove a reference to a configuration parser; destroy it if there are no
155 cfg_obj_isvoid(const cfg_obj_t *obj);
157 * Return true iff 'obj' is of void type (e.g., an optional
158 * value not specified).
162 cfg_obj_ismap(const cfg_obj_t *obj);
164 * Return true iff 'obj' is of a map type.
168 cfg_map_get(const cfg_obj_t *mapobj, const char* name, const cfg_obj_t **obj);
170 * Extract an element from a configuration object, which
171 * must be of a map type.
174 * \li 'mapobj' points to a valid configuration object of a map type.
175 * \li 'name' points to a null-terminated string.
176 * \li 'obj' is non-NULL and '*obj' is NULL.
179 * \li #ISC_R_SUCCESS - success
180 * \li #ISC_R_NOTFOUND - name not found in map
184 cfg_map_getname(const cfg_obj_t *mapobj);
186 * Get the name of a named map object, like a server "key" clause.
189 * \li 'mapobj' points to a valid configuration object of a map type.
192 * \li A pointer to a configuration object naming the map object,
193 * or NULL if the map object does not have a name.
197 cfg_obj_istuple(const cfg_obj_t *obj);
199 * Return true iff 'obj' is of a map type.
203 cfg_tuple_get(const cfg_obj_t *tupleobj, const char *name);
205 * Extract an element from a configuration object, which
206 * must be of a tuple type.
209 * \li 'tupleobj' points to a valid configuration object of a tuple type.
210 * \li 'name' points to a null-terminated string naming one of the
211 *\li fields of said tuple type.
215 cfg_obj_isuint32(const cfg_obj_t *obj);
217 * Return true iff 'obj' is of integer type.
221 cfg_obj_asuint32(const cfg_obj_t *obj);
223 * Returns the value of a configuration object of 32-bit integer type.
226 * \li 'obj' points to a valid configuration object of 32-bit integer type.
229 * \li A 32-bit unsigned integer.
233 cfg_obj_isuint64(const cfg_obj_t *obj);
235 * Return true iff 'obj' is of integer type.
239 cfg_obj_asuint64(const cfg_obj_t *obj);
241 * Returns the value of a configuration object of 64-bit integer type.
244 * \li 'obj' points to a valid configuration object of 64-bit integer type.
247 * \li A 64-bit unsigned integer.
251 cfg_obj_isstring(const cfg_obj_t *obj);
253 * Return true iff 'obj' is of string type.
257 cfg_obj_asstring(const cfg_obj_t *obj);
259 * Returns the value of a configuration object of a string type
260 * as a null-terminated string.
263 * \li 'obj' points to a valid configuration object of a string type.
266 * \li A pointer to a null terminated string.
270 cfg_obj_isboolean(const cfg_obj_t *obj);
272 * Return true iff 'obj' is of a boolean type.
276 cfg_obj_asboolean(const cfg_obj_t *obj);
278 * Returns the value of a configuration object of a boolean type.
281 * \li 'obj' points to a valid configuration object of a boolean type.
284 * \li A boolean value.
288 cfg_obj_issockaddr(const cfg_obj_t *obj);
290 * Return true iff 'obj' is a socket address.
293 const isc_sockaddr_t *
294 cfg_obj_assockaddr(const cfg_obj_t *obj);
296 * Returns the value of a configuration object representing a socket address.
299 * \li 'obj' points to a valid configuration object of a socket address type.
302 * \li A pointer to a sockaddr. The sockaddr must be copied by the caller
307 cfg_obj_isnetprefix(const cfg_obj_t *obj);
309 * Return true iff 'obj' is a network prefix.
313 cfg_obj_asnetprefix(const cfg_obj_t *obj, isc_netaddr_t *netaddr,
314 unsigned int *prefixlen);
316 * Gets the value of a configuration object representing a network
317 * prefix. The network address is returned through 'netaddr' and the
318 * prefix length in bits through 'prefixlen'.
321 * \li 'obj' points to a valid configuration object of network prefix type.
322 *\li 'netaddr' and 'prefixlen' are non-NULL.
326 cfg_obj_islist(const cfg_obj_t *obj);
328 * Return true iff 'obj' is of list type.
331 const cfg_listelt_t *
332 cfg_list_first(const cfg_obj_t *obj);
334 * Returns the first list element in a configuration object of a list type.
337 * \li 'obj' points to a valid configuration object of a list type or NULL.
340 * \li A pointer to a cfg_listelt_t representing the first list element,
341 * or NULL if the list is empty or nonexistent.
344 const cfg_listelt_t *
345 cfg_list_next(const cfg_listelt_t *elt);
347 * Returns the next element of a list of configuration objects.
350 * \li 'elt' points to cfg_listelt_t obtained from cfg_list_first() or
351 * a previous call to cfg_list_next().
354 * \li A pointer to a cfg_listelt_t representing the next element,
355 * or NULL if there are no more elements.
359 cfg_list_length(const cfg_obj_t *obj, isc_boolean_t recurse);
361 * Returns the length of a list of configure objects. If obj is
362 * not a list, returns 0. If recurse is true, add in the length of
363 * all contained lists.
367 cfg_listelt_value(const cfg_listelt_t *elt);
369 * Returns the configuration object associated with cfg_listelt_t.
372 * \li 'elt' points to cfg_listelt_t obtained from cfg_list_first() or
376 * \li A non-NULL pointer to a configuration object.
380 cfg_print(const cfg_obj_t *obj,
381 void (*f)(void *closure, const char *text, int textlen),
384 * Print the configuration object 'obj' by repeatedly calling the
385 * function 'f', passing 'closure' and a region of text starting
386 * at 'text' and comprising 'textlen' characters.
390 cfg_print_grammar(const cfg_type_t *type,
391 void (*f)(void *closure, const char *text, int textlen),
394 * Print a summary of the grammar of the configuration type 'type'.
398 cfg_obj_istype(const cfg_obj_t *obj, const cfg_type_t *type);
400 * Return true iff 'obj' is of type 'type'.
404 cfg_obj_attach(cfg_obj_t *src, cfg_obj_t **dest);
406 * Reference a configuration object.
410 cfg_obj_destroy(cfg_parser_t *pctx, cfg_obj_t **obj);
412 * Delete a reference to a configuration object; destroy the object if
413 * there are no more references.
417 cfg_obj_log(const cfg_obj_t *obj, isc_log_t *lctx, int level,
418 const char *fmt, ...)
419 ISC_FORMAT_PRINTF(4, 5);
421 * Log a message concerning configuration object 'obj' to the logging
422 * channel of 'pctx', at log level 'level'. The message will be prefixed
423 * with the file name(s) and line number where 'obj' was defined.
427 cfg_obj_file(const cfg_obj_t *obj);
429 * Return the file that defined this object.
433 cfg_obj_line(const cfg_obj_t *obj);
435 * Return the line in file where this object was defined.
441 #endif /* ISCCFG_CFG_H */