1 /*
2 * Copyright (c) 2007-2014, Anthony Minessale II
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 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of the original author; nor the names of any contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /**
35 * @defgroup config Config File Parser
36 * @ingroup config
37 * This module implements a basic interface and file format parser
38 *
39 * <pre>
40 *
41 * EXAMPLE
42 *
43 * [category1]
44 * var1 => val1
45 * var2 => val2
46 * \# lines that begin with \# are comments
47 * \#var3 => val3
48 * </pre>
49 * @{
50 */
51
52 #ifndef ESL_CONFIG_H
53 #define ESL_CONFIG_H
54
55 #include "esl.h"
56
57 #ifdef __cplusplus
58 extern "C" {
59 #endif /* defined(__cplusplus) */
60
61
62 #define ESL_URL_SEPARATOR "://"
63
64
65 #ifdef WIN32
66 #define ESL_PATH_SEPARATOR "\\"
67 #ifndef ESL_CONFIG_DIR
68 #define ESL_CONFIG_DIR "c:\\openesl"
69 #endif
70 #define esl_is_file_path(file) (*(file +1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR))
71 #else
72 #define ESL_PATH_SEPARATOR "/"
73 #ifndef ESL_CONFIG_DIR
74 #define ESL_CONFIG_DIR "/etc/openesl"
75 #endif
76 #define esl_is_file_path(file) ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR))
77 #endif
78
79 /*!
80 \brief Evaluate the truthfullness of a string expression
81 \param expr a string expression
82 \return true or false
83 */
esl_true(const char * expr)84 static __inline__ int esl_true(const char *expr) {
85 return (expr && (!strcasecmp(expr, "yes")
86 || !strcasecmp(expr, "on")
87 || !strcasecmp(expr, "true")
88 || !strcasecmp(expr, "enabled")
89 || !strcasecmp(expr, "active")
90 || !strcasecmp(expr, "allow")
91 || atoi(expr)));
92 }
93
94 /*!
95 \brief Evaluate the falsefullness of a string expression
96 \param expr a string expression
97 \return true or false
98 */
esl_false(const char * expr)99 static __inline__ int esl_false(const char *expr) {
100 return (expr && (!strcasecmp(expr, "no")
101 || !strcasecmp(expr, "off")
102 || !strcasecmp(expr, "false")
103 || !strcasecmp(expr, "disabled")
104 || !strcasecmp(expr, "inactive")
105 || !strcasecmp(expr, "disallow")
106 || !atoi(expr)));
107 }
108
109 typedef struct esl_config esl_config_t;
110
111 /*! \brief A simple file handle representing an open configuration file **/
112 struct esl_config {
113 /*! FILE stream buffer to the opened file */
114 FILE *file;
115 /*! path to the file */
116 char path[1024];
117 /*! current category */
118 char category[256];
119 /*! current section */
120 char section[256];
121 /*! buffer of current line being read */
122 char buf[1024];
123 /*! current line number in file */
124 int lineno;
125 /*! current category number in file */
126 int catno;
127 /*! current section number in file */
128 int sectno;
129
130 int lockto;
131 };
132
133 /*!
134 \brief Open a configuration file
135 \param cfg (esl_config_t *) config handle to use
136 \param file_path path to the file
137 \return 1 (true) on success 0 (false) on failure
138 */
139 ESL_DECLARE(int) esl_config_open_file(esl_config_t * cfg, const char *file_path);
140
141 /*!
142 \brief Close a previously opened configuration file
143 \param cfg (esl_config_t *) config handle to use
144 */
145 ESL_DECLARE(void) esl_config_close_file(esl_config_t * cfg);
146
147 /*!
148 \brief Retrieve next name/value pair from configuration file
149 \param cfg (esl_config_t *) config handle to use
150 \param var pointer to aim at the new variable name
151 \param val pointer to aim at the new value
152 */
153 ESL_DECLARE(int) esl_config_next_pair(esl_config_t * cfg, char **var, char **val);
154
155 /*!
156 \brief Retrieve the CAS bits from a configuration string value
157 \param strvalue pointer to the configuration string value (expected to be in format whatever:xxxx)
158 \param outbits pointer to aim at the CAS bits
159 */
160 ESL_DECLARE(int) esl_config_get_cas_bits(char *strvalue, unsigned char *outbits);
161
162
163 /** @} */
164
165 #ifdef __cplusplus
166 }
167 #endif /* defined(__cplusplus) */
168
169 #endif /* defined(ESL_CONFIG_H) */
170
171 /* For Emacs:
172 * Local Variables:
173 * mode:c
174 * indent-tabs-mode:t
175 * tab-width:4
176 * c-basic-offset:4
177 * End:
178 * For VIM:
179 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
180 */
181