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 27 #ifndef _FIGPAR_H_ 28 #define _FIGPAR_H_ 29 30 #include <sys/types.h> 31 32 /* 33 * Union for storing various types of data in a single common container. 34 */ 35 union figpar_cfgvalue { 36 void *data; /* Pointer to NUL-terminated string */ 37 char *str; /* Pointer to NUL-terminated string */ 38 char **strarray; /* Pointer to an array of strings */ 39 int32_t num; /* Signed 32-bit integer value */ 40 uint32_t u_num; /* Unsigned 32-bit integer value */ 41 uint32_t boolean:1; /* Boolean integer value (0 or 1) */ 42 }; 43 44 /* 45 * Option types (based on above cfgvalue union) 46 */ 47 enum figpar_cfgtype { 48 FIGPAR_TYPE_NONE = 0x0000, /* directives with no value */ 49 FIGPAR_TYPE_BOOL = 0x0001, /* boolean */ 50 FIGPAR_TYPE_INT = 0x0002, /* signed 32 bit integer */ 51 FIGPAR_TYPE_UINT = 0x0004, /* unsigned 32 bit integer */ 52 FIGPAR_TYPE_STR = 0x0008, /* string pointer */ 53 FIGPAR_TYPE_STRARRAY = 0x0010, /* string array pointer */ 54 FIGPAR_TYPE_DATA1 = 0x0020, /* void data type-1 (open) */ 55 FIGPAR_TYPE_DATA2 = 0x0040, /* void data type-2 (open) */ 56 FIGPAR_TYPE_DATA3 = 0x0080, /* void data type-3 (open) */ 57 FIGPAR_TYPE_RESERVED1 = 0x0100, /* reserved data type-1 */ 58 FIGPAR_TYPE_RESERVED2 = 0x0200, /* reserved data type-2 */ 59 FIGPAR_TYPE_RESERVED3 = 0x0400, /* reserved data type-3 */ 60 }; 61 62 /* 63 * Options to parse_config() for processing_options bitmask 64 */ 65 #define FIGPAR_BREAK_ON_EQUALS 0x0001 /* stop reading directive at `=' */ 66 #define FIGPAR_BREAK_ON_SEMICOLON 0x0002 /* `;' starts a new line */ 67 #define FIGPAR_CASE_SENSITIVE 0x0004 /* directives are case sensitive */ 68 #define FIGPAR_REQUIRE_EQUALS 0x0008 /* assignment directives only */ 69 #define FIGPAR_STRICT_EQUALS 0x0010 /* `=' must be part of directive */ 70 71 /* 72 * Anatomy of a config file option 73 */ 74 struct figpar_config { 75 enum figpar_cfgtype type; /* Option value type */ 76 const char *directive; /* config file keyword */ 77 union figpar_cfgvalue value; /* NB: set by action */ 78 79 /* 80 * Function pointer; action to be taken when the directive is found 81 */ 82 int (*action)(struct figpar_config *option, uint32_t line, 83 char *directive, char *value); 84 }; 85 extern struct figpar_config figpar_dummy_config; 86 87 __BEGIN_DECLS 88 int parse_config(struct figpar_config _options[], 89 const char *_path, 90 int (*_unknown)(struct figpar_config *_option, 91 uint32_t _line, char *_directive, char *_value), 92 uint16_t _processing_options); 93 struct figpar_config *get_config_option(struct figpar_config _options[], 94 const char *_directive); 95 __END_DECLS 96 97 #endif /* _FIGPAR_H_ */ 98