If snapshots are disabled and the snapshots directory contains no
[dragonfly.git] / lib / libncp / ncpl_rcfile.c
blob0ccb22a9d8eaee130d98efc460eb29cbefc76879
1 /*
2 * Copyright (c) 1999, Boris Popov
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
30 * SUCH DAMAGE.
32 * $FreeBSD: src/lib/libncp/ncpl_rcfile.c,v 1.1 1999/10/12 11:56:40 bp Exp $
33 * $DragonFly: src/lib/libncp/ncpl_rcfile.c,v 1.2 2003/06/17 04:26:50 dillon Exp $
35 #include <sys/types.h>
36 #include <sys/queue.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <pwd.h>
43 #include <unistd.h>
45 #include <netncp/ncp_lib.h>
46 #include <netncp/ncp_rcfile.h>
47 #include <netncp/ncp_cfg.h>
49 #define NWFS_CFG_FILE NCP_PREFIX"/etc/nwfs.conf"
51 struct rcfile *ncp_rc = NULL;
53 SLIST_HEAD(rcfile_head, rcfile);
54 static struct rcfile_head pf_head = {NULL};
56 int rc_merge(char *filename,struct rcfile **rcfile);
57 static struct rcfile* rc_find(char *filename);
58 static struct rcsection *rc_findsect(struct rcfile *rcp, char *sectname);
59 static struct rcsection *rc_addsect(struct rcfile *rcp, char *sectname);
60 static int rc_sect_free(struct rcsection *rsp);
61 static struct rckey *rc_sect_findkey(struct rcsection *rsp, char *keyname);
62 static struct rckey *rc_sect_addkey(struct rcsection *rsp, char *name, char *value);
63 static void rc_key_free(struct rckey *p);
64 static void rc_parse(struct rcfile *rcp);
68 * open rcfile and load its content, if already open - return previous handle
70 int
71 rc_open(char *filename,char *mode,struct rcfile **rcfile) {
72 struct rcfile *rcp;
73 FILE *f;
75 rcp = rc_find(filename);
76 if( rcp ) {
77 *rcfile = rcp;
78 return 0;
80 f = fopen (filename, mode);
81 if (f==NULL)
82 return errno;
83 rcp = malloc(sizeof(struct rcfile));
84 if (rcp==NULL) {
85 fclose(f);
86 return ENOMEM;
88 bzero(rcp, sizeof(struct rcfile));
89 rcp->rf_name = strdup (filename);
90 rcp->rf_f = f;
91 SLIST_INSERT_HEAD(&pf_head, rcp, rf_next);
92 rc_parse(rcp);
93 *rcfile = rcp;
94 return 0;
97 int
98 rc_merge(char *filename,struct rcfile **rcfile) {
99 struct rcfile *rcp = *rcfile;
100 FILE *f, *t;
102 if (rcp == NULL) {
103 return rc_open(filename,"r",rcfile);
105 f = fopen (filename, "r");
106 if (f==NULL)
107 return errno;
108 t = rcp->rf_f;
109 rcp->rf_f = f;
110 rc_parse(rcp);
111 rcp->rf_f = t;
112 fclose(f);
113 return 0;
117 rc_close(struct rcfile *rcp) {
118 struct rcsection *p,*n;
120 fclose(rcp->rf_f);
121 for(p = SLIST_FIRST(&rcp->rf_sect);p;) {
122 n = p;
123 p = SLIST_NEXT(p,rs_next);
124 rc_sect_free(n);
126 free(rcp->rf_name);
127 SLIST_REMOVE(&pf_head, rcp, rcfile, rf_next);
128 free(rcp);
129 return 0;
132 static struct rcfile*
133 rc_find(char *filename) {
134 struct rcfile *p;
136 SLIST_FOREACH(p, &pf_head, rf_next)
137 if (strcmp (filename, p->rf_name)==0)
138 return p;
139 return 0;
142 static struct rcsection *
143 rc_findsect(struct rcfile *rcp, char *sectname) {
144 struct rcsection *p;
146 SLIST_FOREACH(p, &rcp->rf_sect, rs_next)
147 if (strcmp(p->rs_name, sectname)==0)
148 return p;
149 return NULL;
152 static struct rcsection *
153 rc_addsect(struct rcfile *rcp, char *sectname) {
154 struct rcsection *p;
156 p = rc_findsect(rcp, sectname);
157 if (p) return p;
158 p = malloc(sizeof(*p));
159 if (!p) return NULL;
160 p->rs_name = strdup(sectname);
161 SLIST_INIT(&p->rs_keys);
162 SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next);
163 return p;
166 static int
167 rc_sect_free(struct rcsection *rsp) {
168 struct rckey *p,*n;
170 for(p = SLIST_FIRST(&rsp->rs_keys);p;) {
171 n = p;
172 p = SLIST_NEXT(p,rk_next);
173 rc_key_free(n);
175 free(rsp->rs_name);
176 free(rsp);
177 return 0;
180 static struct rckey *
181 rc_sect_findkey(struct rcsection *rsp, char *keyname) {
182 struct rckey *p;
184 SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
185 if (strcmp(p->rk_name, keyname)==0)
186 return p;
187 return NULL;
190 static struct rckey *
191 rc_sect_addkey(struct rcsection *rsp, char *name, char *value) {
192 struct rckey *p;
194 p = rc_sect_findkey(rsp, name);
195 if (p) {
196 free(p->rk_value);
197 } else {
198 p = malloc(sizeof(*p));
199 if (!p) return NULL;
200 SLIST_INSERT_HEAD(&rsp->rs_keys, p, rk_next);
201 p->rk_name = strdup(name);
203 p->rk_value = value ? strdup(value) : strdup("");
204 return p;
207 void
208 rc_sect_delkey(struct rcsection *rsp, struct rckey *p) {
210 SLIST_REMOVE(&rsp->rs_keys,p,rckey,rk_next);
211 rc_key_free(p);
212 return;
215 static void
216 rc_key_free(struct rckey *p){
217 free(p->rk_value);
218 free(p->rk_name);
219 free(p);
222 enum { stNewLine, stHeader, stSkipToEOL, stGetKey, stGetValue};
224 static void
225 rc_parse(struct rcfile *rcp) {
226 FILE *f = rcp->rf_f;
227 int state = stNewLine, c;
228 struct rcsection *rsp = NULL;
229 struct rckey *rkp = NULL;
230 char buf[2048];
231 char *next = buf, *last = &buf[sizeof(buf)-1];
233 while ((c = getc (f)) != EOF) {
234 if (c == '\r')
235 continue;
236 if (state == stNewLine) {
237 next = buf;
238 if (isspace(c))
239 continue; /* skip leading junk */
240 if (c == '[') {
241 state = stHeader;
242 rsp = NULL;
243 continue;
245 if (c == '#' || c == ';') {
246 state = stSkipToEOL;
247 } else { /* something meaningfull */
248 state = stGetKey;
251 if (state == stSkipToEOL || next == last) {/* ignore long lines */
252 if (c == '\n'){
253 state = stNewLine;
254 next = buf;
256 continue;
258 if (state == stHeader) {
259 if (c == ']') {
260 *next = 0;
261 next = buf;
262 rsp = rc_addsect(rcp, buf);
263 state = stSkipToEOL;
264 } else
265 *next++ = c;
266 continue;
268 if (state == stGetKey) {
269 if (c == ' ' || c == '\t')/* side effect: 'key name='*/
270 continue; /* become 'keyname=' */
271 if (c == '\n') { /* silently ignore ... */
272 state = stNewLine;
273 continue;
275 if (c != '=') {
276 *next++ = c;
277 continue;
279 *next = 0;
280 if (rsp == NULL) {
281 fprintf(stderr, "Key '%s' defined before section\n", buf);
282 state = stSkipToEOL;
283 continue;
285 rkp = rc_sect_addkey(rsp, buf, NULL);
286 next = buf;
287 state = stGetValue;
288 continue;
290 /* only stGetValue left */
291 if (state != stGetValue) {
292 fprintf(stderr, "Well, I can't parse file '%s'\n",rcp->rf_name);
293 state = stSkipToEOL;
295 if (c != '\n') {
296 *next++ = c;
297 continue;
299 *next = 0;
300 rkp->rk_value = strdup(buf);
301 state = stNewLine;
302 rkp = NULL;
303 } /* while */
304 if (c == EOF && state == stGetValue) {
305 *next = 0;
306 rkp->rk_value = strdup(buf);
308 return;
312 rc_getstringptr(struct rcfile *rcp,char *section, char *key,char **dest) {
313 struct rcsection *rsp;
314 struct rckey *rkp;
316 *dest = NULL;
317 rsp = rc_findsect(rcp, section);
318 if (!rsp) return ENOENT;
319 rkp = rc_sect_findkey(rsp,key);
320 if (!rkp) return ENOENT;
321 *dest = rkp->rk_value;
322 return 0;
326 rc_getstring(struct rcfile *rcp,char *section, char *key,int maxlen,char *dest) {
327 char *value;
328 int error;
330 error = rc_getstringptr(rcp, section, key, &value);
331 if (error) return error;
332 if (strlen(value) >= maxlen) {
333 fprintf(stderr, "line too long for key '%s' in section '%s', max = %d\n",key, section, maxlen);
334 return EINVAL;
336 strcpy(dest,value);
337 return 0;
341 rc_getint(struct rcfile *rcp,char *section, char *key,int *value) {
342 struct rcsection *rsp;
343 struct rckey *rkp;
345 rsp = rc_findsect(rcp, section);
346 if (!rsp) return ENOENT;
347 rkp = rc_sect_findkey(rsp,key);
348 if (!rkp) return ENOENT;
349 errno = 0;
350 *value = strtol(rkp->rk_value,NULL,0);
351 if (errno) {
352 fprintf(stderr, "invalid int value '%s' for key '%s' in section '%s'\n",rkp->rk_value,key,section);
353 return errno;
355 return 0;
359 * 1,yes,true
360 * 0,no,false
363 rc_getbool(struct rcfile *rcp,char *section, char *key,int *value) {
364 struct rcsection *rsp;
365 struct rckey *rkp;
366 char *p;
368 rsp = rc_findsect(rcp, section);
369 if (!rsp) return ENOENT;
370 rkp = rc_sect_findkey(rsp,key);
371 if (!rkp) return ENOENT;
372 p = rkp->rk_value;
373 while (*p && isspace(*p)) p++;
374 if (*p == '0' || strcasecmp(p,"no") == 0 || strcasecmp(p,"false") == 0) {
375 *value = 0;
376 return 0;
378 if (*p == '1' || strcasecmp(p,"yes") == 0 || strcasecmp(p,"true") == 0) {
379 *value = 1;
380 return 0;
382 fprintf(stderr, "invalid boolean value '%s' for key '%s' in section '%s' \n",p, key, section);
383 return EINVAL;
387 * first read ~/.nwfsrc, next try to merge NWFS_CFG_FILE
390 ncp_open_rcfile(void) {
391 char *home, *fn;
392 int error;
394 home = getenv("HOME");
395 if (home) {
396 fn = malloc(strlen(home) + 20);
397 sprintf(fn, "%s/.nwfsrc", home);
398 error = rc_open(fn,"r",&ncp_rc);
399 free (fn);
401 error = rc_merge(NWFS_CFG_FILE, &ncp_rc);
402 if( ncp_rc == NULL ) {
403 printf("Warning: no cfg files found.\n");
404 return 1;
406 return 0;