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. 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 * $FreeBSD: src/lib/libncp/ncpl_rcfile.c,v 1.1 1999/10/12 11:56:40 bp Exp $
34 #include <sys/types.h>
35 #include <sys/queue.h>
44 #include <netncp/ncp_lib.h>
45 #include <netncp/ncp_rcfile.h>
46 #include <netncp/ncp_cfg.h>
48 #define NWFS_CFG_FILE NCP_PREFIX"/etc/nwfs.conf"
50 struct rcfile *ncp_rc = NULL;
52 SLIST_HEAD(rcfile_head, rcfile);
53 static struct rcfile_head pf_head = {NULL};
55 int rc_merge(char *filename,struct rcfile **rcfile);
56 static struct rcfile* rc_find(char *filename);
57 static struct rcsection *rc_findsect(struct rcfile *rcp, char *sectname);
58 static struct rcsection *rc_addsect(struct rcfile *rcp, char *sectname);
59 static int rc_sect_free(struct rcsection *rsp);
60 static struct rckey *rc_sect_findkey(struct rcsection *rsp, char *keyname);
61 static struct rckey *rc_sect_addkey(struct rcsection *rsp, char *name, char *value);
62 static void rc_key_free(struct rckey *p);
63 static void rc_parse(struct rcfile *rcp);
67 * open rcfile and load its content, if already open - return previous handle
70 rc_open(char *filename,char *mode,struct rcfile **rcfile) {
74 rcp = rc_find(filename);
79 f = fopen (filename, mode);
82 rcp = malloc(sizeof(struct rcfile));
87 bzero(rcp, sizeof(struct rcfile));
88 rcp->rf_name = strdup (filename);
90 SLIST_INSERT_HEAD(&pf_head, rcp, rf_next);
97 rc_merge(char *filename,struct rcfile **rcfile) {
98 struct rcfile *rcp = *rcfile;
102 return rc_open(filename,"r",rcfile);
104 f = fopen (filename, "r");
116 rc_close(struct rcfile *rcp) {
117 struct rcsection *p,*n;
120 for(p = SLIST_FIRST(&rcp->rf_sect);p;) {
122 p = SLIST_NEXT(p,rs_next);
126 SLIST_REMOVE(&pf_head, rcp, rcfile, rf_next);
131 static struct rcfile*
132 rc_find(char *filename) {
135 SLIST_FOREACH(p, &pf_head, rf_next)
136 if (strcmp (filename, p->rf_name)==0)
141 static struct rcsection *
142 rc_findsect(struct rcfile *rcp, 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, char *sectname) {
155 p = rc_findsect(rcp, sectname);
157 p = malloc(sizeof(*p));
159 p->rs_name = strdup(sectname);
160 SLIST_INIT(&p->rs_keys);
161 SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next);
166 rc_sect_free(struct rcsection *rsp) {
169 for(p = SLIST_FIRST(&rsp->rs_keys);p;) {
171 p = SLIST_NEXT(p,rk_next);
179 static struct rckey *
180 rc_sect_findkey(struct rcsection *rsp, char *keyname) {
183 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
184 if (strcmp(p->rk_name, keyname)==0)
189 static struct rckey *
190 rc_sect_addkey(struct rcsection *rsp, char *name, char *value) {
193 p = rc_sect_findkey(rsp, name);
197 p = malloc(sizeof(*p));
199 SLIST_INSERT_HEAD(&rsp->rs_keys, p, rk_next);
200 p->rk_name = strdup(name);
202 p->rk_value = value ? strdup(value) : strdup("");
207 rc_sect_delkey(struct rcsection *rsp, struct rckey *p) {
209 SLIST_REMOVE(&rsp->rs_keys,p,rckey,rk_next);
215 rc_key_free(struct rckey *p){
221 enum { stNewLine, stHeader, stSkipToEOL, stGetKey, stGetValue};
224 rc_parse(struct rcfile *rcp) {
226 int state = stNewLine, c;
227 struct rcsection *rsp = NULL;
228 struct rckey *rkp = NULL;
230 char *next = buf, *last = &buf[sizeof(buf)-1];
232 while ((c = getc (f)) != EOF) {
235 if (state == stNewLine) {
238 continue; /* skip leading junk */
244 if (c == '#' || c == ';') {
246 } else { /* something meaningfull */
250 if (state == stSkipToEOL || next == last) {/* ignore long lines */
257 if (state == stHeader) {
261 rsp = rc_addsect(rcp, buf);
267 if (state == stGetKey) {
268 if (c == ' ' || c == '\t')/* side effect: 'key name='*/
269 continue; /* become 'keyname=' */
270 if (c == '\n') { /* silently ignore ... */
280 fprintf(stderr, "Key '%s' defined before section\n", buf);
284 rkp = rc_sect_addkey(rsp, buf, NULL);
289 /* only stGetValue left */
290 if (state != stGetValue) {
291 fprintf(stderr, "Well, I can't parse file '%s'\n",rcp->rf_name);
299 rkp->rk_value = strdup(buf);
303 if (c == EOF && state == stGetValue) {
305 rkp->rk_value = strdup(buf);
311 rc_getstringptr(struct rcfile *rcp,char *section, char *key,char **dest) {
312 struct rcsection *rsp;
316 rsp = rc_findsect(rcp, section);
317 if (!rsp) return ENOENT;
318 rkp = rc_sect_findkey(rsp,key);
319 if (!rkp) return ENOENT;
320 *dest = rkp->rk_value;
325 rc_getstring(struct rcfile *rcp,char *section, char *key,int maxlen,char *dest) {
329 error = rc_getstringptr(rcp, section, key, &value);
330 if (error) return error;
331 if (strlen(value) >= maxlen) {
332 fprintf(stderr, "line too long for key '%s' in section '%s', max = %d\n",key, section, maxlen);
340 rc_getint(struct rcfile *rcp,char *section, char *key,int *value) {
341 struct rcsection *rsp;
344 rsp = rc_findsect(rcp, section);
345 if (!rsp) return ENOENT;
346 rkp = rc_sect_findkey(rsp,key);
347 if (!rkp) return ENOENT;
349 *value = strtol(rkp->rk_value,NULL,0);
351 fprintf(stderr, "invalid int value '%s' for key '%s' in section '%s'\n",rkp->rk_value,key,section);
362 rc_getbool(struct rcfile *rcp,char *section, char *key,int *value) {
363 struct rcsection *rsp;
367 rsp = rc_findsect(rcp, section);
368 if (!rsp) return ENOENT;
369 rkp = rc_sect_findkey(rsp,key);
370 if (!rkp) return ENOENT;
372 while (*p && isspace(*p)) p++;
373 if (*p == '0' || strcasecmp(p,"no") == 0 || strcasecmp(p,"false") == 0) {
377 if (*p == '1' || strcasecmp(p,"yes") == 0 || strcasecmp(p,"true") == 0) {
381 fprintf(stderr, "invalid boolean value '%s' for key '%s' in section '%s' \n",p, key, section);
386 * first read ~/.nwfsrc, next try to merge NWFS_CFG_FILE
389 ncp_open_rcfile(void) {
393 home = getenv("HOME");
395 fn = malloc(strlen(home) + 20);
396 sprintf(fn, "%s/.nwfsrc", home);
397 error = rc_open(fn,"r",&ncp_rc);
400 error = rc_merge(NWFS_CFG_FILE, &ncp_rc);
401 if( ncp_rc == NULL ) {
402 printf("Warning: no cfg files found.\n");