2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <apr_general.h>
29 #include <apr_strings.h>
31 #include "hostinfod.h"
33 typedef int (*process_line_fn) (const char *path, int lineno,
34 const char *key, const char *value);
35 typedef int (*process_section_fn) (const char *path, int lineno,
37 static void process_conf_file (const char *path, int exit_if_not_exist,
38 process_line_fn process_line,
39 process_section_fn process_section);
40 static int get_bool (const char *str);
42 /* Read the main configuration file. */
43 static int main_process_line (const char *path, int lineno, const char *key, const char *value);
46 read_main_conf_file (void)
48 process_conf_file (conf_file, 0, main_process_line, NULL);
52 main_process_line (const char *path, int lineno,
53 const char *key, const char *value)
57 if (strcasecmp (key, "guests") == 0) {
59 error ("%s:%d: directive is empty: %s", path, lineno, key);
62 guests_file = apr_pstrdup (pool, value);
63 } else if (strcasecmp (key, "sockets") == 0) {
65 error ("%s:%d: directive is empty: %s", path, lineno, key);
68 socket_dir = apr_pstrdup (pool, value);
69 } else if (strcasecmp (key, "verbose") == 0) {
70 bool = get_bool (value);
72 error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
75 if (!verbose_set_on_cmdline)
77 } else if (strcasecmp (key, "foreground") == 0) {
78 bool = get_bool (value);
80 error ("%s:%d: %s: not a valid boolean - use 1 or 0", path, lineno, key);
83 if (!foreground_set_on_cmdline)
86 error ("%s:%d: unknown directive in configuration file: %s",
93 /* Configuration file parser. */
95 process_conf_file (const char *path, int exit_if_not_exist,
96 process_line_fn process_line,
97 process_section_fn process_section)
99 static const char *whitespace = " \t\n\v";
104 size_t real_len, key_len;
106 const char *key, *value;
108 debug ("begin processing configuration file %s", path);
110 fp = fopen (path, "r");
112 if (exit_if_not_exist) {
113 perrorf ("%s", path);
116 pwarningf ("%s", path);
120 while ((r = getline (&line, &len, fp)) != -1) {
123 /* Remove trailing \n */
125 if (real_len > 0 && line[real_len-1] == '\n')
126 line[--real_len] = '\0';
128 /* Ignore blank lines and comments. */
131 if (line[0] == '#' || strspn (line, whitespace) == real_len)
134 if (line[0] == '[') { /* Section. */
135 if (line[real_len-1] == ']')
136 line[--real_len] = '\0';
138 error ("%s:%d: in section header, ']' not found (is there trailing whitespace or a comment?), near: %s",
143 debug ("configuration file: section [%s]", line);
145 if (process_section && process_section (path, lineno, line) == -1)
148 else { /* Key value */
149 key_len = strcspn (line, whitespace);
150 line[key_len] = '\0';
152 value = key_len < real_len ? &line[key_len+1] : NULL;
154 value += strspn (line, whitespace);
155 if (value[0] == '\0')
159 debug ("configuration file: key '%s', value '%s'", key, value);
161 if (process_line && process_line (path, lineno, key, value) == -1)
169 error ("%s: error reading configuration file", path);
172 if (fclose (fp) == EOF) {
173 perrorf ("%s", path);
177 debug ("finished processing configuration file successfully");
181 get_bool (const char *str)
186 if (strcasecmp (str, "on") == 0)
188 if (strcasecmp (str, "off") == 0)
192 case '1': case 'y': case 'Y': case 't': case 'T': case 'e': case 'E':
194 case '0': case 'n': case 'N': case 'f': case 'F': case 'd': case 'D':