xref: /freebsd/lib/libfigpar/figpar.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
27041394f3SDevin Teske #ifndef _FIGPAR_H_
28041394f3SDevin Teske #define _FIGPAR_H_
29041394f3SDevin Teske 
30041394f3SDevin Teske #include <sys/types.h>
31041394f3SDevin Teske 
32041394f3SDevin Teske /*
33041394f3SDevin Teske  * Union for storing various types of data in a single common container.
34041394f3SDevin Teske  */
35*9d9cc246SDevin Teske union figpar_cfgvalue {
36041394f3SDevin Teske 	void		*data;		/* Pointer to NUL-terminated string */
37041394f3SDevin Teske 	char		*str;		/* Pointer to NUL-terminated string */
38041394f3SDevin Teske 	char		**strarray;	/* Pointer to an array of strings */
39041394f3SDevin Teske 	int32_t		num;		/* Signed 32-bit integer value */
40041394f3SDevin Teske 	uint32_t	u_num;		/* Unsigned 32-bit integer value */
41041394f3SDevin Teske 	uint32_t	boolean:1;	/* Boolean integer value (0 or 1) */
42041394f3SDevin Teske };
43041394f3SDevin Teske 
44041394f3SDevin Teske /*
45041394f3SDevin Teske  * Option types (based on above cfgvalue union)
46041394f3SDevin Teske  */
47*9d9cc246SDevin Teske enum figpar_cfgtype {
48*9d9cc246SDevin Teske 	FIGPAR_TYPE_NONE	= 0x0000, /* directives with no value */
49*9d9cc246SDevin Teske 	FIGPAR_TYPE_BOOL	= 0x0001, /* boolean */
50*9d9cc246SDevin Teske 	FIGPAR_TYPE_INT		= 0x0002, /* signed 32 bit integer */
51*9d9cc246SDevin Teske 	FIGPAR_TYPE_UINT	= 0x0004, /* unsigned 32 bit integer */
52*9d9cc246SDevin Teske 	FIGPAR_TYPE_STR		= 0x0008, /* string pointer */
53*9d9cc246SDevin Teske 	FIGPAR_TYPE_STRARRAY	= 0x0010, /* string array pointer */
54*9d9cc246SDevin Teske 	FIGPAR_TYPE_DATA1	= 0x0020, /* void data type-1 (open) */
55*9d9cc246SDevin Teske 	FIGPAR_TYPE_DATA2	= 0x0040, /* void data type-2 (open) */
56*9d9cc246SDevin Teske 	FIGPAR_TYPE_DATA3	= 0x0080, /* void data type-3 (open) */
57*9d9cc246SDevin Teske 	FIGPAR_TYPE_RESERVED1	= 0x0100, /* reserved data type-1 */
58*9d9cc246SDevin Teske 	FIGPAR_TYPE_RESERVED2	= 0x0200, /* reserved data type-2 */
59*9d9cc246SDevin Teske 	FIGPAR_TYPE_RESERVED3	= 0x0400, /* reserved data type-3 */
60041394f3SDevin Teske };
61041394f3SDevin Teske 
62041394f3SDevin Teske /*
63041394f3SDevin Teske  * Options to parse_config() for processing_options bitmask
64041394f3SDevin Teske  */
65*9d9cc246SDevin Teske #define FIGPAR_BREAK_ON_EQUALS    0x0001 /* stop reading directive at `=' */
66*9d9cc246SDevin Teske #define FIGPAR_BREAK_ON_SEMICOLON 0x0002 /* `;' starts a new line */
67*9d9cc246SDevin Teske #define FIGPAR_CASE_SENSITIVE     0x0004 /* directives are case sensitive */
68*9d9cc246SDevin Teske #define FIGPAR_REQUIRE_EQUALS     0x0008 /* assignment directives only */
69*9d9cc246SDevin Teske #define FIGPAR_STRICT_EQUALS      0x0010 /* `=' must be part of directive */
70041394f3SDevin Teske 
71041394f3SDevin Teske /*
72041394f3SDevin Teske  * Anatomy of a config file option
73041394f3SDevin Teske  */
74*9d9cc246SDevin Teske struct figpar_config {
75*9d9cc246SDevin Teske 	enum figpar_cfgtype	type;		/* Option value type */
76041394f3SDevin Teske 	const char		*directive;	/* config file keyword */
77*9d9cc246SDevin Teske 	union figpar_cfgvalue	value;		/* NB: set by action */
78041394f3SDevin Teske 
79041394f3SDevin Teske 	/*
80041394f3SDevin Teske 	 * Function pointer; action to be taken when the directive is found
81041394f3SDevin Teske 	 */
82*9d9cc246SDevin Teske 	int (*action)(struct figpar_config *option, uint32_t line,
83*9d9cc246SDevin Teske 	    char *directive, char *value);
84041394f3SDevin Teske };
85*9d9cc246SDevin Teske extern struct figpar_config figpar_dummy_config;
86041394f3SDevin Teske 
87041394f3SDevin Teske __BEGIN_DECLS
88*9d9cc246SDevin Teske int			 parse_config(struct figpar_config _options[],
89041394f3SDevin Teske 			    const char *_path,
90*9d9cc246SDevin Teske 			    int (*_unknown)(struct figpar_config *_option,
91041394f3SDevin Teske 			    uint32_t _line, char *_directive, char *_value),
92041394f3SDevin Teske 			    uint16_t _processing_options);
93*9d9cc246SDevin Teske struct figpar_config	*get_config_option(struct figpar_config _options[],
94041394f3SDevin Teske 			    const char *_directive);
95041394f3SDevin Teske __END_DECLS
96041394f3SDevin Teske 
97041394f3SDevin Teske #endif /* _FIGPAR_H_ */
98