1 /* config file parser functions
3 * (C) 2000 by Harald Welte <laforge@gnumonks.org>
5 * $Id: conffile.c 4946 2003-09-28 15:19:25Z laforge $
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define DEBUGC(format, args...) fprintf(stderr, format, ## args)
29 #define DEBUGC(format, args...)
32 /* points to config entry with error */
33 config_entry_t *config_errce = NULL;
35 /* Filename of the config file */
36 static char *fname = NULL;
38 /* get_word() - Function to parse a line into words.
39 * Arguments: line line to parse
40 * delim possible word delimiters
41 * buf pointer to buffer where word is returned
42 * Return value: pointer to first char after word
43 * This function can deal with "" quotes
45 static char *get_word(char *line, char *not, char *buf)
47 char *p, *start = NULL, *stop = NULL;
50 for (p = line; *p; p++) {
56 if (!strchr(not, *p)) {
64 /* determine pointer to one char after word */
65 for (p = start; *p; p++) {
72 if (strchr(not, *p)) {
81 strncpy(buf, start, (size_t) (stop-start));
82 *(buf + (stop-start)) = '\0';
84 /* skip quote character */
86 /* yes, we can return stop + 1. If " was the last
87 * character in string, it now points to NULL-term */
94 /* do we have a config directive for this name */
95 static int config_iskey(char *name)
99 for (ce = config; ce; ce = ce->next) {
100 if (!strcmp(name, ce->key))
108 /***********************************************************************
110 ***********************************************************************/
112 /* register config file with us */
113 int config_register_file(const char *file)
115 /* FIXME: stat of file */
119 fname = (char *) malloc(strlen(file)+1);
128 /* parse config file */
129 int config_parse_file(const char *section, config_entry_t *keys)
136 char linebuf[LINE_LEN+1];
137 char *line = linebuf;
139 cfile = fopen(fname, "r");
143 DEBUGC("prasing section [%s]\n", section);
145 /* Search for correct section */
146 while (fgets(line, LINE_LEN, cfile)) {
147 char wordbuf[LINE_LEN];
153 if (!(wordend = get_word(line, " \t\n[]", (char *) wordbuf)))
155 DEBUGC("word: \"%s\"\n", wordbuf);
156 if (!strcmp(wordbuf, section)) {
167 /* Parse this section until next section */
168 while (fgets(line, LINE_LEN, cfile))
170 char wordbuf[LINE_LEN];
173 DEBUGC("line read: %s\n", line);
177 if (!(wordend = get_word(line, " =\t\n", (char *) &wordbuf)))
180 if (wordbuf[0] == '[' ) {
181 DEBUGC("Next section '%s' encountered\n", wordbuf);
185 DEBUGC("parse_file: entering main loop\n");
186 for (ce = keys; ce; ce = ce->next) {
187 DEBUGC("parse main loop, key: %s\n", ce->key);
188 if (strcmp(ce->key, (char *) &wordbuf)) {
192 wordend = get_word(wordend, " =\t\n", (char *) &wordbuf);
193 args = (char *)&wordbuf;
195 if (ce->hit && !(ce->options & CONFIG_OPT_MULTI))
197 DEBUGC("->ce-hit and option not multi!\n");
205 case CONFIG_TYPE_STRING:
207 CONFIG_VAL_STRING_LEN ) {
208 strcpy(ce->u.string, args);
209 /* FIXME: what if not ? */
212 case CONFIG_TYPE_INT:
213 ce->u.value = atoi(args);
215 case CONFIG_TYPE_CALLBACK:
216 (ce->u.parser)(args);
221 DEBUGC("parse_file: exiting main loop\n");
225 for (ce = keys; ce; ce = ce->next) {
226 DEBUGC("ce post loop, ce=%s\n", ce->key);
227 if ((ce->options & CONFIG_OPT_MANDATORY) && (ce->hit == 0)) {
228 DEBUGC("Mandatory config directive \"%s\" not found\n",