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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/queue.h>
46 #include <netncp/ncp_lib.h>
47 #include <netncp/ncp_rcfile.h>
48 #include <netncp/ncp_cfg.h>
50 #define NWFS_CFG_FILE NCP_PREFIX"/etc/nwfs.conf"
52 struct rcfile *ncp_rc = NULL;
54 SLIST_HEAD(rcfile_head, rcfile);
55 static struct rcfile_head pf_head = {NULL};
57 int rc_merge(char *filename,struct rcfile **rcfile);
58 static struct rcfile* rc_find(char *filename);
59 static struct rcsection *rc_findsect(struct rcfile *rcp, char *sectname);
60 static struct rcsection *rc_addsect(struct rcfile *rcp, char *sectname);
61 static int rc_sect_free(struct rcsection *rsp);
62 static struct rckey *rc_sect_findkey(struct rcsection *rsp, char *keyname);
63 static struct rckey *rc_sect_addkey(struct rcsection *rsp, char *name, char *value);
64 static void rc_key_free(struct rckey *p);
65 static void rc_parse(struct rcfile *rcp);
69 * open rcfile and load its content, if already open - return previous handle
72 rc_open(char *filename,char *mode,struct rcfile **rcfile) {
76 rcp = rc_find(filename);
81 f = fopen (filename, mode);
84 rcp = malloc(sizeof(struct rcfile));
89 bzero(rcp, sizeof(struct rcfile));
90 rcp->rf_name = strdup (filename);
92 SLIST_INSERT_HEAD(&pf_head, rcp, rf_next);
99 rc_merge(char *filename,struct rcfile **rcfile) {
100 struct rcfile *rcp = *rcfile;
104 return rc_open(filename,"r",rcfile);
106 f = fopen (filename, "r");
118 rc_close(struct rcfile *rcp) {
119 struct rcsection *p,*n;
122 for(p = SLIST_FIRST(&rcp->rf_sect);p;) {
124 p = SLIST_NEXT(p,rs_next);
128 SLIST_REMOVE(&pf_head, rcp, rcfile, rf_next);
133 static struct rcfile*
134 rc_find(char *filename) {
137 SLIST_FOREACH(p, &pf_head, rf_next)
138 if (strcmp (filename, p->rf_name)==0)
143 static struct rcsection *
144 rc_findsect(struct rcfile *rcp, char *sectname) {
147 SLIST_FOREACH(p, &rcp->rf_sect, rs_next)
148 if (strcmp(p->rs_name, sectname)==0)
153 static struct rcsection *
154 rc_addsect(struct rcfile *rcp, char *sectname) {
157 p = rc_findsect(rcp, sectname);
159 p = malloc(sizeof(*p));
161 p->rs_name = strdup(sectname);
162 SLIST_INIT(&p->rs_keys);
163 SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next);
168 rc_sect_free(struct rcsection *rsp) {
171 for(p = SLIST_FIRST(&rsp->rs_keys);p;) {
173 p = SLIST_NEXT(p,rk_next);
181 static struct rckey *
182 rc_sect_findkey(struct rcsection *rsp, char *keyname) {
185 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
186 if (strcmp(p->rk_name, keyname)==0)
191 static struct rckey *
192 rc_sect_addkey(struct rcsection *rsp, char *name, char *value) {
195 p = rc_sect_findkey(rsp, name);
199 p = malloc(sizeof(*p));
201 SLIST_INSERT_HEAD(&rsp->rs_keys, p, rk_next);
202 p->rk_name = strdup(name);
204 p->rk_value = value ? strdup(value) : strdup("");
209 rc_sect_delkey(struct rcsection *rsp, struct rckey *p) {
211 SLIST_REMOVE(&rsp->rs_keys,p,rckey,rk_next);
217 rc_key_free(struct rckey *p){
223 enum { stNewLine, stHeader, stSkipToEOL, stGetKey, stGetValue};
226 rc_parse(struct rcfile *rcp) {
228 int state = stNewLine, c;
229 struct rcsection *rsp = NULL;
230 struct rckey *rkp = NULL;
232 char *next = buf, *last = &buf[sizeof(buf)-1];
234 while ((c = getc (f)) != EOF) {
237 if (state == stNewLine) {
240 continue; /* skip leading junk */
246 if (c == '#' || c == ';') {
248 } else { /* something meaningfull */
252 if (state == stSkipToEOL || next == last) {/* ignore long lines */
259 if (state == stHeader) {
263 rsp = rc_addsect(rcp, buf);
269 if (state == stGetKey) {
270 if (c == ' ' || c == '\t')/* side effect: 'key name='*/
271 continue; /* become 'keyname=' */
272 if (c == '\n') { /* silently ignore ... */
282 fprintf(stderr, "Key '%s' defined before section\n", buf);
286 rkp = rc_sect_addkey(rsp, buf, NULL);
291 /* only stGetValue left */
292 if (state != stGetValue) {
293 fprintf(stderr, "Well, I can't parse file '%s'\n",rcp->rf_name);
301 rkp->rk_value = strdup(buf);
305 if (c == EOF && state == stGetValue) {
307 rkp->rk_value = strdup(buf);
313 rc_getstringptr(struct rcfile *rcp,char *section, char *key,char **dest) {
314 struct rcsection *rsp;
318 rsp = rc_findsect(rcp, section);
319 if (!rsp) return ENOENT;
320 rkp = rc_sect_findkey(rsp,key);
321 if (!rkp) return ENOENT;
322 *dest = rkp->rk_value;
327 rc_getstring(struct rcfile *rcp,char *section, char *key,int maxlen,char *dest) {
331 error = rc_getstringptr(rcp, section, key, &value);
332 if (error) return error;
333 if (strlen(value) >= maxlen) {
334 fprintf(stderr, "line too long for key '%s' in section '%s', max = %d\n",key, section, maxlen);
342 rc_getint(struct rcfile *rcp,char *section, char *key,int *value) {
343 struct rcsection *rsp;
346 rsp = rc_findsect(rcp, section);
347 if (!rsp) return ENOENT;
348 rkp = rc_sect_findkey(rsp,key);
349 if (!rkp) return ENOENT;
351 *value = strtol(rkp->rk_value,NULL,0);
353 fprintf(stderr, "invalid int value '%s' for key '%s' in section '%s'\n",rkp->rk_value,key,section);
364 rc_getbool(struct rcfile *rcp,char *section, char *key,int *value) {
365 struct rcsection *rsp;
369 rsp = rc_findsect(rcp, section);
370 if (!rsp) return ENOENT;
371 rkp = rc_sect_findkey(rsp,key);
372 if (!rkp) return ENOENT;
374 while (*p && isspace(*p)) p++;
375 if (*p == '0' || strcasecmp(p,"no") == 0 || strcasecmp(p,"false") == 0) {
379 if (*p == '1' || strcasecmp(p,"yes") == 0 || strcasecmp(p,"true") == 0) {
383 fprintf(stderr, "invalid boolean value '%s' for key '%s' in section '%s' \n",p, key, section);
388 * first read ~/.nwfsrc, next try to merge NWFS_CFG_FILE
391 ncp_open_rcfile(void) {
395 home = getenv("HOME");
397 fn = malloc(strlen(home) + 20);
398 sprintf(fn, "%s/.nwfsrc", home);
399 error = rc_open(fn,"r",&ncp_rc);
402 error = rc_merge(NWFS_CFG_FILE, &ncp_rc);
403 if( ncp_rc == NULL ) {
404 printf("Warning: no cfg files found.\n");