xref: /freebsd/lib/libfigpar/figpar.h (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*-
2  * Copyright (c) 2002-2015 Devin Teske <dteske@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _FIGPAR_H_
30 #define _FIGPAR_H_
31 
32 #include <sys/types.h>
33 
34 /*
35  * Union for storing various types of data in a single common container.
36  */
37 union figpar_cfgvalue {
38 	void		*data;		/* Pointer to NUL-terminated string */
39 	char		*str;		/* Pointer to NUL-terminated string */
40 	char		**strarray;	/* Pointer to an array of strings */
41 	int32_t		num;		/* Signed 32-bit integer value */
42 	uint32_t	u_num;		/* Unsigned 32-bit integer value */
43 	uint32_t	boolean:1;	/* Boolean integer value (0 or 1) */
44 };
45 
46 /*
47  * Option types (based on above cfgvalue union)
48  */
49 enum figpar_cfgtype {
50 	FIGPAR_TYPE_NONE	= 0x0000, /* directives with no value */
51 	FIGPAR_TYPE_BOOL	= 0x0001, /* boolean */
52 	FIGPAR_TYPE_INT		= 0x0002, /* signed 32 bit integer */
53 	FIGPAR_TYPE_UINT	= 0x0004, /* unsigned 32 bit integer */
54 	FIGPAR_TYPE_STR		= 0x0008, /* string pointer */
55 	FIGPAR_TYPE_STRARRAY	= 0x0010, /* string array pointer */
56 	FIGPAR_TYPE_DATA1	= 0x0020, /* void data type-1 (open) */
57 	FIGPAR_TYPE_DATA2	= 0x0040, /* void data type-2 (open) */
58 	FIGPAR_TYPE_DATA3	= 0x0080, /* void data type-3 (open) */
59 	FIGPAR_TYPE_RESERVED1	= 0x0100, /* reserved data type-1 */
60 	FIGPAR_TYPE_RESERVED2	= 0x0200, /* reserved data type-2 */
61 	FIGPAR_TYPE_RESERVED3	= 0x0400, /* reserved data type-3 */
62 };
63 
64 /*
65  * Options to parse_config() for processing_options bitmask
66  */
67 #define FIGPAR_BREAK_ON_EQUALS    0x0001 /* stop reading directive at `=' */
68 #define FIGPAR_BREAK_ON_SEMICOLON 0x0002 /* `;' starts a new line */
69 #define FIGPAR_CASE_SENSITIVE     0x0004 /* directives are case sensitive */
70 #define FIGPAR_REQUIRE_EQUALS     0x0008 /* assignment directives only */
71 #define FIGPAR_STRICT_EQUALS      0x0010 /* `=' must be part of directive */
72 
73 /*
74  * Anatomy of a config file option
75  */
76 struct figpar_config {
77 	enum figpar_cfgtype	type;		/* Option value type */
78 	const char		*directive;	/* config file keyword */
79 	union figpar_cfgvalue	value;		/* NB: set by action */
80 
81 	/*
82 	 * Function pointer; action to be taken when the directive is found
83 	 */
84 	int (*action)(struct figpar_config *option, uint32_t line,
85 	    char *directive, char *value);
86 };
87 extern struct figpar_config figpar_dummy_config;
88 
89 __BEGIN_DECLS
90 int			 parse_config(struct figpar_config _options[],
91 			    const char *_path,
92 			    int (*_unknown)(struct figpar_config *_option,
93 			    uint32_t _line, char *_directive, char *_value),
94 			    uint16_t _processing_options);
95 struct figpar_config	*get_config_option(struct figpar_config _options[],
96 			    const char *_directive);
97 __END_DECLS
98 
99 #endif /* _FIGPAR_H_ */
100