2 * Copyright (c) 1999, 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. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * from: FreeBSD: src/lib/libncp/ncpl_rcfile.c,v 1.5 2007/01/09 23:27:39 imp Exp
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #include <sys/queue.h>
44 #include "nandsim_rcfile.h"
46 SLIST_HEAD(rcfile_head, rcfile);
47 static struct rcfile_head pf_head = {NULL};
48 static struct rcsection *rc_findsect(struct rcfile *rcp,
49 const char *sectname, int sect_id);
50 static struct rcsection *rc_addsect(struct rcfile *rcp,
51 const char *sectname);
52 static int rc_sect_free(struct rcsection *rsp);
53 static struct rckey *rc_sect_findkey(struct rcsection *rsp,
55 static struct rckey *rc_sect_addkey(struct rcsection *rsp, const char *name,
57 static void rc_key_free(struct rckey *p);
58 static void rc_parse(struct rcfile *rcp);
60 static struct rcfile* rc_find(const char *filename);
63 * open rcfile and load its content, if already open - return previous handle
66 rc_open(const char *filename, const char *mode,struct rcfile **rcfile)
70 rcp = rc_find(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_close(struct rcfile *rcp)
95 struct rcsection *p,*n;
98 for (p = SLIST_FIRST(&rcp->rf_sect); p; ) {
100 p = SLIST_NEXT(p,rs_next);
104 SLIST_REMOVE(&pf_head, rcp, rcfile, rf_next);
109 static struct rcfile*
110 rc_find(const char *filename)
114 SLIST_FOREACH(p, &pf_head, rf_next)
115 if (strcmp (filename, p->rf_name) == 0)
120 /* Find section with given name and id */
121 static struct rcsection *
122 rc_findsect(struct rcfile *rcp, const char *sectname, int sect_id)
126 SLIST_FOREACH(p, &rcp->rf_sect, rs_next)
127 if (strcmp(p->rs_name, sectname) == 0 && p->rs_id == sect_id)
132 static struct rcsection *
133 rc_addsect(struct rcfile *rcp, const char *sectname)
137 p = rc_findsect(rcp, sectname, 0);
140 * If section with that name already exists -- add one more,
141 * same named, but with different id (higher by one)
145 p = rc_findsect(rcp, sectname, id);
148 p = malloc(sizeof(*p));
151 p->rs_name = strdup(sectname);
153 SLIST_INIT(&p->rs_keys);
154 SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next);
159 rc_sect_free(struct rcsection *rsp)
163 for (p = SLIST_FIRST(&rsp->rs_keys); p; ) {
165 p = SLIST_NEXT(p,rk_next);
173 static struct rckey *
174 rc_sect_findkey(struct rcsection *rsp, const char *keyname)
178 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
179 if (strcmp(p->rk_name, keyname)==0)
184 static struct rckey *
185 rc_sect_addkey(struct rcsection *rsp, const char *name, char *value)
188 p = rc_sect_findkey(rsp, name);
192 p = malloc(sizeof(*p));
195 SLIST_INSERT_HEAD(&rsp->rs_keys, p, rk_next);
196 p->rk_name = strdup(name);
198 p->rk_value = value ? strdup(value) : strdup("");
203 rc_key_free(struct rckey *p)
210 enum { stNewLine, stHeader, stSkipToEOL, stGetKey, stGetValue};
213 rc_parse(struct rcfile *rcp)
216 int state = stNewLine, c;
217 struct rcsection *rsp = NULL;
218 struct rckey *rkp = NULL;
220 char *next = buf, *last = &buf[sizeof(buf)-1];
222 while ((c = getc (f)) != EOF) {
225 if (state == stNewLine) {
228 continue; /* skip leading junk */
234 if (c == '#' || c == ';') {
236 } else { /* something meaningful */
240 if (state == stSkipToEOL || next == last) {/* ignore long lines */
247 if (state == stHeader) {
251 rsp = rc_addsect(rcp, buf);
257 if (state == stGetKey) {
258 if (c == ' ' || c == '\t')/* side effect: 'key name='*/
259 continue; /* become 'keyname=' */
260 if (c == '\n') { /* silently ignore ... */
270 fprintf(stderr, "Key '%s' defined before "
275 rkp = rc_sect_addkey(rsp, buf, NULL);
280 /* only stGetValue left */
281 if (state != stGetValue) {
282 fprintf(stderr, "Well, I can't parse file "
283 "'%s'\n",rcp->rf_name);
291 rkp->rk_value = strdup(buf);
295 if (c == EOF && state == stGetValue) {
297 rkp->rk_value = strdup(buf);
302 rc_getstringptr(struct rcfile *rcp, const char *section, int sect_id,
303 const char *key, char **dest)
305 struct rcsection *rsp;
309 rsp = rc_findsect(rcp, section, sect_id);
312 rkp = rc_sect_findkey(rsp,key);
315 *dest = rkp->rk_value;
320 rc_getstring(struct rcfile *rcp, const char *section, int sect_id,
321 const char *key, unsigned int maxlen, char *dest)
326 error = rc_getstringptr(rcp, section, sect_id, key, &value);
329 if (strlen(value) >= maxlen) {
330 fprintf(stderr, "line too long for key '%s' in section '%s',"
331 "max = %d\n",key, section, maxlen);
339 rc_getint(struct rcfile *rcp, const char *section, int sect_id,
340 const char *key, int *value)
342 struct rcsection *rsp;
345 rsp = rc_findsect(rcp, section, sect_id);
348 rkp = rc_sect_findkey(rsp,key);
352 *value = strtol(rkp->rk_value,NULL,0);
354 fprintf(stderr, "invalid int value '%s' for key '%s' in "
355 "section '%s'\n",rkp->rk_value,key,section);
366 rc_getbool(struct rcfile *rcp, const char *section, int sect_id,
367 const char *key, int *value)
369 struct rcsection *rsp;
373 rsp = rc_findsect(rcp, section, sect_id);
376 rkp = rc_sect_findkey(rsp,key);
380 while (*p && isspace(*p)) p++;
381 if (*p == '0' || strcasecmp(p,"no") == 0 ||
382 strcasecmp(p, "false") == 0 ||
383 strcasecmp(p, "off") == 0) {
387 if (*p == '1' || strcasecmp(p,"yes") == 0 ||
388 strcasecmp(p, "true") == 0 ||
389 strcasecmp(p, "on") == 0) {
393 fprintf(stderr, "invalid boolean value '%s' for key '%s' in section "
394 "'%s' \n",p, key, section);
398 /* Count how many sections with given name exists in configuration. */
399 int rc_getsectionscount(struct rcfile *f, const char *sectname)
404 p = rc_findsect(f, sectname, 0);
407 count = p->rs_id + 1;
408 p = rc_findsect(f, sectname, count);
416 rc_getkeys(struct rcfile *rcp, const char *sectname, int sect_id)
418 struct rcsection *rsp;
421 int i = 0, count = 0;
423 rsp = rc_findsect(rcp, sectname, sect_id);
427 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
430 names_tbl = malloc(sizeof(char *) * (count + 1));
431 if (names_tbl == NULL)
434 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
435 names_tbl[i++] = p->rk_name;