2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1999, Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. 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.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * from: FreeBSD: src/lib/libncp/ncpl_rcfile.c,v 1.5 2007/01/09 23:27:39 imp Exp
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/queue.h>
46 #include "nandsim_rcfile.h"
48 SLIST_HEAD(rcfile_head, rcfile);
49 static struct rcfile_head pf_head = {NULL};
50 static struct rcsection *rc_findsect(struct rcfile *rcp,
51 const char *sectname, int sect_id);
52 static struct rcsection *rc_addsect(struct rcfile *rcp,
53 const char *sectname);
54 static int rc_sect_free(struct rcsection *rsp);
55 static struct rckey *rc_sect_findkey(struct rcsection *rsp,
57 static struct rckey *rc_sect_addkey(struct rcsection *rsp, const char *name,
59 static void rc_key_free(struct rckey *p);
60 static void rc_parse(struct rcfile *rcp);
62 static struct rcfile* rc_find(const char *filename);
65 * open rcfile and load its content, if already open - return previous handle
68 rc_open(const char *filename, const char *mode,struct rcfile **rcfile)
72 rcp = rc_find(filename);
77 f = fopen (filename, mode);
80 rcp = malloc(sizeof(struct rcfile));
85 bzero(rcp, sizeof(struct rcfile));
86 rcp->rf_name = strdup(filename);
88 SLIST_INSERT_HEAD(&pf_head, rcp, rf_next);
95 rc_close(struct rcfile *rcp)
97 struct rcsection *p,*n;
100 for (p = SLIST_FIRST(&rcp->rf_sect); p; ) {
102 p = SLIST_NEXT(p,rs_next);
106 SLIST_REMOVE(&pf_head, rcp, rcfile, rf_next);
111 static struct rcfile*
112 rc_find(const char *filename)
116 SLIST_FOREACH(p, &pf_head, rf_next)
117 if (strcmp (filename, p->rf_name) == 0)
122 /* Find section with given name and id */
123 static struct rcsection *
124 rc_findsect(struct rcfile *rcp, const char *sectname, int sect_id)
128 SLIST_FOREACH(p, &rcp->rf_sect, rs_next)
129 if (strcmp(p->rs_name, sectname) == 0 && p->rs_id == sect_id)
134 static struct rcsection *
135 rc_addsect(struct rcfile *rcp, const char *sectname)
139 p = rc_findsect(rcp, sectname, 0);
142 * If section with that name already exists -- add one more,
143 * same named, but with different id (higher by one)
147 p = rc_findsect(rcp, sectname, id);
150 p = malloc(sizeof(*p));
153 p->rs_name = strdup(sectname);
155 SLIST_INIT(&p->rs_keys);
156 SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next);
161 rc_sect_free(struct rcsection *rsp)
165 for (p = SLIST_FIRST(&rsp->rs_keys); p; ) {
167 p = SLIST_NEXT(p,rk_next);
175 static struct rckey *
176 rc_sect_findkey(struct rcsection *rsp, const char *keyname)
180 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
181 if (strcmp(p->rk_name, keyname)==0)
186 static struct rckey *
187 rc_sect_addkey(struct rcsection *rsp, const char *name, char *value)
190 p = rc_sect_findkey(rsp, name);
194 p = malloc(sizeof(*p));
197 SLIST_INSERT_HEAD(&rsp->rs_keys, p, rk_next);
198 p->rk_name = strdup(name);
200 p->rk_value = value ? strdup(value) : strdup("");
205 rc_key_free(struct rckey *p)
212 enum { stNewLine, stHeader, stSkipToEOL, stGetKey, stGetValue};
215 rc_parse(struct rcfile *rcp)
218 int state = stNewLine, c;
219 struct rcsection *rsp = NULL;
220 struct rckey *rkp = NULL;
222 char *next = buf, *last = &buf[sizeof(buf)-1];
224 while ((c = getc (f)) != EOF) {
227 if (state == stNewLine) {
230 continue; /* skip leading junk */
236 if (c == '#' || c == ';') {
238 } else { /* something meaningful */
242 if (state == stSkipToEOL || next == last) {/* ignore long lines */
249 if (state == stHeader) {
253 rsp = rc_addsect(rcp, buf);
259 if (state == stGetKey) {
260 if (c == ' ' || c == '\t')/* side effect: 'key name='*/
261 continue; /* become 'keyname=' */
262 if (c == '\n') { /* silently ignore ... */
272 fprintf(stderr, "Key '%s' defined before "
277 rkp = rc_sect_addkey(rsp, buf, NULL);
282 /* only stGetValue left */
283 if (state != stGetValue) {
284 fprintf(stderr, "Well, I can't parse file "
285 "'%s'\n",rcp->rf_name);
293 rkp->rk_value = strdup(buf);
297 if (c == EOF && state == stGetValue) {
299 rkp->rk_value = strdup(buf);
304 rc_getstringptr(struct rcfile *rcp, const char *section, int sect_id,
305 const char *key, char **dest)
307 struct rcsection *rsp;
311 rsp = rc_findsect(rcp, section, sect_id);
314 rkp = rc_sect_findkey(rsp,key);
317 *dest = rkp->rk_value;
322 rc_getstring(struct rcfile *rcp, const char *section, int sect_id,
323 const char *key, unsigned int maxlen, char *dest)
328 error = rc_getstringptr(rcp, section, sect_id, key, &value);
331 if (strlen(value) >= maxlen) {
332 fprintf(stderr, "line too long for key '%s' in section '%s',"
333 "max = %d\n",key, section, maxlen);
341 rc_getint(struct rcfile *rcp, const char *section, int sect_id,
342 const char *key, int *value)
344 struct rcsection *rsp;
347 rsp = rc_findsect(rcp, section, sect_id);
350 rkp = rc_sect_findkey(rsp,key);
354 *value = strtol(rkp->rk_value,NULL,0);
356 fprintf(stderr, "invalid int value '%s' for key '%s' in "
357 "section '%s'\n",rkp->rk_value,key,section);
368 rc_getbool(struct rcfile *rcp, const char *section, int sect_id,
369 const char *key, int *value)
371 struct rcsection *rsp;
375 rsp = rc_findsect(rcp, section, sect_id);
378 rkp = rc_sect_findkey(rsp,key);
382 while (*p && isspace(*p)) p++;
383 if (*p == '0' || strcasecmp(p,"no") == 0 ||
384 strcasecmp(p, "false") == 0 ||
385 strcasecmp(p, "off") == 0) {
389 if (*p == '1' || strcasecmp(p,"yes") == 0 ||
390 strcasecmp(p, "true") == 0 ||
391 strcasecmp(p, "on") == 0) {
395 fprintf(stderr, "invalid boolean value '%s' for key '%s' in section "
396 "'%s' \n",p, key, section);
400 /* Count how many sections with given name exists in configuration. */
401 int rc_getsectionscount(struct rcfile *f, const char *sectname)
406 p = rc_findsect(f, sectname, 0);
409 count = p->rs_id + 1;
410 p = rc_findsect(f, sectname, count);
418 rc_getkeys(struct rcfile *rcp, const char *sectname, int sect_id)
420 struct rcsection *rsp;
423 int i = 0, count = 0;
425 rsp = rc_findsect(rcp, sectname, sect_id);
429 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
432 names_tbl = malloc(sizeof(char *) * (count + 1));
433 if (names_tbl == NULL)
436 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
437 names_tbl[i++] = p->rk_name;