xref: /freebsd/lib/libfigpar/figpar.h (revision 9d9cc24662d95b56a87e2d92d57899224cec42df)
1041394f3SDevin Teske /*-
2*9d9cc246SDevin Teske  * Copyright (c) 2002-2015 Devin Teske <dteske@FreeBSD.org>
3041394f3SDevin Teske  * All rights reserved.
4041394f3SDevin Teske  *
5041394f3SDevin Teske  * Redistribution and use in source and binary forms, with or without
6041394f3SDevin Teske  * modification, are permitted provided that the following conditions
7041394f3SDevin Teske  * are met:
8041394f3SDevin Teske  * 1. Redistributions of source code must retain the above copyright
9041394f3SDevin Teske  *    notice, this list of conditions and the following disclaimer.
10041394f3SDevin Teske  * 2. Redistributions in binary form must reproduce the above copyright
11041394f3SDevin Teske  *    notice, this list of conditions and the following disclaimer in the
12041394f3SDevin Teske  *    documentation and/or other materials provided with the distribution.
13041394f3SDevin Teske  *
14041394f3SDevin Teske  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15041394f3SDevin Teske  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16041394f3SDevin Teske  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17041394f3SDevin Teske  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18041394f3SDevin Teske  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19041394f3SDevin Teske  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20041394f3SDevin Teske  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21041394f3SDevin Teske  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22041394f3SDevin Teske  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23041394f3SDevin Teske  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24041394f3SDevin Teske  * SUCH DAMAGE.
25041394f3SDevin Teske  *
26041394f3SDevin Teske  * $FreeBSD$
27041394f3SDevin Teske  */
28041394f3SDevin Teske 
29041394f3SDevin Teske #ifndef _FIGPAR_H_
30041394f3SDevin Teske #define _FIGPAR_H_
31041394f3SDevin Teske 
32041394f3SDevin Teske #include <sys/types.h>
33041394f3SDevin Teske 
34041394f3SDevin Teske /*
35041394f3SDevin Teske  * Union for storing various types of data in a single common container.
36041394f3SDevin Teske  */
37*9d9cc246SDevin Teske union figpar_cfgvalue {
38041394f3SDevin Teske 	void		*data;		/* Pointer to NUL-terminated string */
39041394f3SDevin Teske 	char		*str;		/* Pointer to NUL-terminated string */
40041394f3SDevin Teske 	char		**strarray;	/* Pointer to an array of strings */
41041394f3SDevin Teske 	int32_t		num;		/* Signed 32-bit integer value */
42041394f3SDevin Teske 	uint32_t	u_num;		/* Unsigned 32-bit integer value */
43041394f3SDevin Teske 	uint32_t	boolean:1;	/* Boolean integer value (0 or 1) */
44041394f3SDevin Teske };
45041394f3SDevin Teske 
46041394f3SDevin Teske /*
47041394f3SDevin Teske  * Option types (based on above cfgvalue union)
48041394f3SDevin Teske  */
49*9d9cc246SDevin Teske enum figpar_cfgtype {
50*9d9cc246SDevin Teske 	FIGPAR_TYPE_NONE	= 0x0000, /* directives with no value */
51*9d9cc246SDevin Teske 	FIGPAR_TYPE_BOOL	= 0x0001, /* boolean */
52*9d9cc246SDevin Teske 	FIGPAR_TYPE_INT		= 0x0002, /* signed 32 bit integer */
53*9d9cc246SDevin Teske 	FIGPAR_TYPE_UINT	= 0x0004, /* unsigned 32 bit integer */
54*9d9cc246SDevin Teske 	FIGPAR_TYPE_STR		= 0x0008, /* string pointer */
55*9d9cc246SDevin Teske 	FIGPAR_TYPE_STRARRAY	= 0x0010, /* string array pointer */
56*9d9cc246SDevin Teske 	FIGPAR_TYPE_DATA1	= 0x0020, /* void data type-1 (open) */
57*9d9cc246SDevin Teske 	FIGPAR_TYPE_DATA2	= 0x0040, /* void data type-2 (open) */
58*9d9cc246SDevin Teske 	FIGPAR_TYPE_DATA3	= 0x0080, /* void data type-3 (open) */
59*9d9cc246SDevin Teske 	FIGPAR_TYPE_RESERVED1	= 0x0100, /* reserved data type-1 */
60*9d9cc246SDevin Teske 	FIGPAR_TYPE_RESERVED2	= 0x0200, /* reserved data type-2 */
61*9d9cc246SDevin Teske 	FIGPAR_TYPE_RESERVED3	= 0x0400, /* reserved data type-3 */
62041394f3SDevin Teske };
63041394f3SDevin Teske 
64041394f3SDevin Teske /*
65041394f3SDevin Teske  * Options to parse_config() for processing_options bitmask
66041394f3SDevin Teske  */
67*9d9cc246SDevin Teske #define FIGPAR_BREAK_ON_EQUALS    0x0001 /* stop reading directive at `=' */
68*9d9cc246SDevin Teske #define FIGPAR_BREAK_ON_SEMICOLON 0x0002 /* `;' starts a new line */
69*9d9cc246SDevin Teske #define FIGPAR_CASE_SENSITIVE     0x0004 /* directives are case sensitive */
70*9d9cc246SDevin Teske #define FIGPAR_REQUIRE_EQUALS     0x0008 /* assignment directives only */
71*9d9cc246SDevin Teske #define FIGPAR_STRICT_EQUALS      0x0010 /* `=' must be part of directive */
72041394f3SDevin Teske 
73041394f3SDevin Teske /*
74041394f3SDevin Teske  * Anatomy of a config file option
75041394f3SDevin Teske  */
76*9d9cc246SDevin Teske struct figpar_config {
77*9d9cc246SDevin Teske 	enum figpar_cfgtype	type;		/* Option value type */
78041394f3SDevin Teske 	const char		*directive;	/* config file keyword */
79*9d9cc246SDevin Teske 	union figpar_cfgvalue	value;		/* NB: set by action */
80041394f3SDevin Teske 
81041394f3SDevin Teske 	/*
82041394f3SDevin Teske 	 * Function pointer; action to be taken when the directive is found
83041394f3SDevin Teske 	 */
84*9d9cc246SDevin Teske 	int (*action)(struct figpar_config *option, uint32_t line,
85*9d9cc246SDevin Teske 	    char *directive, char *value);
86041394f3SDevin Teske };
87*9d9cc246SDevin Teske extern struct figpar_config figpar_dummy_config;
88041394f3SDevin Teske 
89041394f3SDevin Teske __BEGIN_DECLS
90*9d9cc246SDevin Teske int			 parse_config(struct figpar_config _options[],
91041394f3SDevin Teske 			    const char *_path,
92*9d9cc246SDevin Teske 			    int (*_unknown)(struct figpar_config *_option,
93041394f3SDevin Teske 			    uint32_t _line, char *_directive, char *_value),
94041394f3SDevin Teske 			    uint16_t _processing_options);
95*9d9cc246SDevin Teske struct figpar_config	*get_config_option(struct figpar_config _options[],
96041394f3SDevin Teske 			    const char *_directive);
97041394f3SDevin Teske __END_DECLS
98041394f3SDevin Teske 
99041394f3SDevin Teske #endif /* _FIGPAR_H_ */
100