1 /*- 2 * Copyright (c) 2002-2014 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 fp_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 fp_cfgtype { 50 FP_TYPE_NONE = 0x0000, /* for directives with no value */ 51 FP_TYPE_BOOL = 0x0001, /* boolean */ 52 FP_TYPE_INT = 0x0002, /* signed 32 bit integer */ 53 FP_TYPE_UINT = 0x0004, /* unsigned 32 bit integer */ 54 FP_TYPE_STR = 0x0008, /* string pointer */ 55 FP_TYPE_STRARRAY = 0x0010, /* string array pointer */ 56 FP_TYPE_DATA1 = 0x0020, /* void data type-1 (whatever) */ 57 FP_TYPE_DATA2 = 0x0040, /* void data type-2 (whatever) */ 58 FP_TYPE_DATA3 = 0x0080, /* void data type-3 (whatever) */ 59 FP_TYPE_RESERVED1 = 0x0100, /* reserved data type-1 (future) */ 60 FP_TYPE_RESERVED2 = 0x0200, /* reserved data type-2 (future) */ 61 FP_TYPE_RESERVED3 = 0x0400, /* reserved data type-3 (future) */ 62 }; 63 64 /* 65 * Options to parse_config() for processing_options bitmask 66 */ 67 #define FP_BREAK_ON_EQUALS 0x0001 /* stop reading directive at `=' */ 68 #define FP_BREAK_ON_SEMICOLON 0x0002 /* `;' starts a new line */ 69 #define FP_CASE_SENSITIVE 0x0004 /* directives are case sensitive */ 70 #define FP_REQUIRE_EQUALS 0x0008 /* assignment directives only */ 71 #define FP_STRICT_EQUALS 0x0010 /* `=' must be part of directive */ 72 73 /* 74 * Anatomy of a config file option 75 */ 76 struct fp_config { 77 enum fp_cfgtype type; /* Option value type */ 78 const char *directive; /* config file keyword */ 79 union fp_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 fp_config *option, uint32_t line, char *directive, 85 char *value); 86 }; 87 extern struct fp_config fp_dummy_config; 88 89 __BEGIN_DECLS 90 int parse_config(struct fp_config _options[], 91 const char *_path, 92 int (*_unknown)(struct fp_config *_option, 93 uint32_t _line, char *_directive, char *_value), 94 uint16_t _processing_options); 95 struct fp_config *get_config_option(struct fp_config _options[], 96 const char *_directive); 97 __END_DECLS 98 99 #endif /* _FIGPAR_H_ */ 100