2 * Copyright (c) 2007, Anthony Minessale II
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
35 #include "esl_config.h"
37 ESL_DECLARE(int) esl_config_open_file(esl_config_t *cfg, const char *file_path)
40 const char *path = NULL;
43 if (file_path[0] == '/') {
46 esl_snprintf(path_buf, sizeof(path_buf), "%s%s%s", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR, file_path);
54 memset(cfg, 0, sizeof(*cfg));
56 esl_log(ESL_LOG_DEBUG, "Configuration file is %s.\n", path);
60 if (file_path[0] != '/') {
64 esl_snprintf(path_buf, sizeof(path_buf), "%s%sopenesl.conf", ESL_CONFIG_DIR, ESL_PATH_SEPARATOR);
67 if ((f = fopen(path, "r")) == 0) {
72 esl_set_string(cfg->path, path);
74 while (esl_config_next_pair(cfg, &var, &val)) {
75 if ((cfg->sectno != last) && !strcmp(cfg->section, file_path)) {
76 cfg->lockto = cfg->sectno;
81 esl_config_close_file(cfg);
82 memset(cfg, 0, sizeof(*cfg));
89 esl_set_string(cfg->path, path);
94 ESL_DECLARE(void) esl_config_close_file(esl_config_t *cfg)
101 memset(cfg, 0, sizeof(*cfg));
106 ESL_DECLARE(int) esl_config_next_pair(esl_config_t *cfg, char **var, char **val)
120 if (!fgets(cfg->buf, sizeof(cfg->buf), cfg->file)) {
126 if (**var == '[' && (end = strchr(*var, ']')) != 0) {
131 esl_copy_string(cfg->section, *var, sizeof(cfg->section));
134 if (cfg->lockto > -1 && cfg->sectno != cfg->lockto) {
143 esl_copy_string(cfg->category, *var, sizeof(cfg->category));
151 if (**var == '#' || **var == ';' || **var == '\n' || **var == '\r') {
155 if (!strncmp(*var, "__END__", 7)) {
160 if ((end = strchr(*var, ';')) && *(end+1) == *end) {
163 } else if ((end = strchr(*var, '\n')) != 0) {
164 if (*(end - 1) == '\r') {
171 while ((*p == ' ' || *p == '\t') && p != end) {
178 if ((*val = strchr(*var, '=')) == 0) {
180 /* log_printf(0, server.log, "Invalid syntax on %s: line %d\n", cfg->path, cfg->lineno); */
186 if (*(*val) == '>') {
191 while ((*p == ' ' || *p == '\t') && p != *var) {
197 while ((*p == ' ' || *p == '\t') && p != end) {
212 ESL_DECLARE(int) esl_config_get_cas_bits(char *strvalue, unsigned char *outbits)
215 unsigned char bit = 0x8;
216 char *double_colon = strchr(strvalue, ':');
220 esl_log(ESL_LOG_ERROR, "No CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
228 if (sscanf(double_colon, "%c%c%c%c", &cas_bits[0], &cas_bits[1], &cas_bits[2], &cas_bits[3]) != 4) {
229 esl_log(ESL_LOG_ERROR, "Invalid CAS bits specified: %s, :xxxx definition expected, where x is 1 or 0\n", double_colon);
233 esl_log(ESL_LOG_DEBUG, "CAS bits specification found: %s\n", cas_bits);
235 for (; cas_bits[x]; x++) {
236 if ('1' == cas_bits[x]) {
238 } else if ('0' != cas_bits[x]) {
239 esl_log(ESL_LOG_ERROR, "Invalid CAS pattern specified: %s, just 0 or 1 allowed for each bit\n");
255 * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab: