2 * Copyright (c) 2000, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $Id: rcfile.c,v 1.5 2001/04/16 12:46:46 bp Exp $
34 #include <sys/types.h>
35 #include <sys/queue.h>
46 #include "rcfile_priv.h"
48 SLIST_HEAD(rcfile_head, rcfile);
49 static struct rcfile_head pf_head = {NULL};
51 static struct rcfile* rc_cachelookup(const char *filename);
52 static struct rcsection *rc_findsect(struct rcfile *rcp, const char *sectname);
53 static struct rcsection *rc_addsect(struct rcfile *rcp, const char *sectname);
54 static int rc_freesect(struct rcfile *rcp, struct rcsection *rsp);
55 static struct rckey *rc_sect_findkey(struct rcsection *rsp, const char *keyname);
56 static struct rckey *rc_sect_addkey(struct rcsection *rsp, const char *name, const char *value);
57 static void rc_key_free(struct rckey *p);
58 static void rc_parse(struct rcfile *rcp);
62 * open rcfile and load its content, if already open - return previous handle
65 rc_open(const char *filename, const char *mode, struct rcfile **rcfile)
70 rcp = rc_cachelookup(filename);
75 f = fopen(filename, mode);
78 rcp = malloc(sizeof(struct rcfile));
83 bzero(rcp, sizeof(struct rcfile));
84 rcp->rf_name = strdup(filename);
86 SLIST_INSERT_HEAD(&pf_head, rcp, rf_next);
93 rc_merge(const char *filename, struct rcfile **rcfile)
95 struct rcfile *rcp = *rcfile;
99 return rc_open(filename, "r", rcfile);
101 f = fopen (filename, "r");
113 rc_close(struct rcfile *rcp)
115 struct rcsection *p, *n;
118 for(p = SLIST_FIRST(&rcp->rf_sect); p;) {
120 p = SLIST_NEXT(p,rs_next);
124 SLIST_REMOVE(&pf_head, rcp, rcfile, rf_next);
129 static struct rcfile*
130 rc_cachelookup(const char *filename)
134 SLIST_FOREACH(p, &pf_head, rf_next)
135 if (strcmp (filename, p->rf_name) == 0)
140 static struct rcsection *
141 rc_findsect(struct rcfile *rcp, const char *sectname)
145 SLIST_FOREACH(p, &rcp->rf_sect, rs_next)
146 if (strcmp(p->rs_name, sectname)==0)
151 static struct rcsection *
152 rc_addsect(struct rcfile *rcp, const char *sectname)
156 p = rc_findsect(rcp, sectname);
158 p = malloc(sizeof(*p));
160 p->rs_name = strdup(sectname);
161 SLIST_INIT(&p->rs_keys);
162 SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next);
167 rc_freesect(struct rcfile *rcp, struct rcsection *rsp)
171 SLIST_REMOVE(&rcp->rf_sect, rsp, rcsection, rs_next);
172 for(p = SLIST_FIRST(&rsp->rs_keys);p;) {
174 p = SLIST_NEXT(p,rk_next);
182 static struct rckey *
183 rc_sect_findkey(struct rcsection *rsp, const char *keyname)
187 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
188 if (strcmp(p->rk_name, keyname)==0)
193 static struct rckey *
194 rc_sect_addkey(struct rcsection *rsp, const char *name, const char *value)
198 p = rc_sect_findkey(rsp, name);
202 p = malloc(sizeof(*p));
204 SLIST_INSERT_HEAD(&rsp->rs_keys, p, rk_next);
205 p->rk_name = strdup(name);
207 p->rk_value = value ? strdup(value) : strdup("");
213 rc_sect_delkey(struct rcsection *rsp, struct rckey *p)
216 SLIST_REMOVE(&rsp->rs_keys, p, rckey, rk_next);
223 rc_key_free(struct rckey *p)
230 enum { stNewLine, stHeader, stSkipToEOL, stGetKey, stGetValue};
233 rc_parse(struct rcfile *rcp)
236 int state = stNewLine, c;
237 struct rcsection *rsp = NULL;
238 struct rckey *rkp = NULL;
240 char *next = buf, *last = &buf[sizeof(buf)-1];
242 while ((c = getc (f)) != EOF) {
245 if (state == stNewLine) {
248 continue; /* skip leading junk */
254 if (c == '#' || c == ';') {
256 } else { /* something meaningfull */
260 if (state == stSkipToEOL || next == last) {/* ignore long lines */
267 if (state == stHeader) {
271 rsp = rc_addsect(rcp, buf);
277 if (state == stGetKey) {
278 if (c == ' ' || c == '\t')/* side effect: 'key name='*/
279 continue; /* become 'keyname=' */
280 if (c == '\n') { /* silently ignore ... */
290 fprintf(stderr, "Key '%s' defined before section\n", buf);
294 rkp = rc_sect_addkey(rsp, buf, NULL);
299 /* only stGetValue left */
300 if (state != stGetValue) {
301 fprintf(stderr, "Well, I can't parse file '%s'\n",rcp->rf_name);
309 rkp->rk_value = strdup(buf);
313 if (c == EOF && state == stGetValue) {
315 rkp->rk_value = strdup(buf);
321 rc_getstringptr(struct rcfile *rcp, const char *section, const char *key,
324 struct rcsection *rsp;
328 rsp = rc_findsect(rcp, section);
329 if (!rsp) return ENOENT;
330 rkp = rc_sect_findkey(rsp,key);
331 if (!rkp) return ENOENT;
332 *dest = rkp->rk_value;
337 rc_getstring(struct rcfile *rcp, const char *section, const char *key,
338 size_t maxlen, char *dest)
343 error = rc_getstringptr(rcp, section, key, &value);
346 if (strlen(value) >= maxlen) {
347 warnx("line too long for key '%s' in section '%s', max = %d\n", key, section, maxlen);
355 rc_getint(struct rcfile *rcp, const char *section, const char *key, int *value)
357 struct rcsection *rsp;
360 rsp = rc_findsect(rcp, section);
363 rkp = rc_sect_findkey(rsp, key);
367 *value = strtol(rkp->rk_value, NULL, 0);
369 warnx("invalid int value '%s' for key '%s' in section '%s'\n", rkp->rk_value, key, section);
380 rc_getbool(struct rcfile *rcp, const char *section, const char *key, int *value)
382 struct rcsection *rsp;
386 rsp = rc_findsect(rcp, section);
387 if (!rsp) return ENOENT;
388 rkp = rc_sect_findkey(rsp,key);
389 if (!rkp) return ENOENT;
391 while (*p && isspace(*p)) p++;
392 if (*p == '0' || strcasecmp(p,"no") == 0 || strcasecmp(p,"false") == 0) {
396 if (*p == '1' || strcasecmp(p,"yes") == 0 || strcasecmp(p,"true") == 0) {
400 fprintf(stderr, "invalid boolean value '%s' for key '%s' in section '%s' \n",p, key, section);
405 * Unified command line/rc file parser
408 opt_args_parse(struct rcfile *rcp, struct opt_args *ap, const char *sect,
409 opt_callback_t *callback)
413 for (; ap->opt; ap++) {
416 if (rc_getstringptr(rcp, sect, ap->name, &ap->str) != 0)
418 len = strlen(ap->str);
419 if (len > ap->ival) {
420 warnx("rc: argument for option '%c' (%s) too long\n", ap->opt, ap->name);
426 error = rc_getbool(rcp, sect, ap->name, &ap->ival);
434 if (rc_getint(rcp, sect, ap->name, &ap->ival) != 0)
436 if (((ap->flag & OPTFL_HAVEMIN) && ap->ival < ap->min) ||
437 ((ap->flag & OPTFL_HAVEMAX) && ap->ival > ap->max)) {
438 warnx("rc: argument for option '%c' (%s) should be in [%d-%d] range\n",
439 ap->opt, ap->name, ap->min, ap->max);
452 opt_args_parseopt(struct opt_args *ap, int opt, char *arg,
453 opt_callback_t *callback)
457 for (; ap->opt; ap++) {
464 len = strlen(ap->str);
465 if (len > ap->ival) {
466 warnx("opt: Argument for option '%c' (%s) too long\n", ap->opt, ap->name);
478 ap->ival = strtol(arg, NULL, 0);
480 warnx("opt: Invalid integer value for option '%c' (%s).\n",ap->opt,ap->name);
483 if (((ap->flag & OPTFL_HAVEMIN) &&
484 (ap->ival < ap->min)) ||
485 ((ap->flag & OPTFL_HAVEMAX) &&
486 (ap->ival > ap->max))) {
487 warnx("opt: Argument for option '%c' (%s) should be in [%d-%d] range\n",ap->opt,ap->name,ap->min,ap->max);