1f1b9d127SSheldon Hearn /*
2f1b9d127SSheldon Hearn * Copyright (c) 2000, Boris Popov
3f1b9d127SSheldon Hearn * All rights reserved.
4f1b9d127SSheldon Hearn *
5f1b9d127SSheldon Hearn * Redistribution and use in source and binary forms, with or without
6f1b9d127SSheldon Hearn * modification, are permitted provided that the following conditions
7f1b9d127SSheldon Hearn * are met:
8f1b9d127SSheldon Hearn * 1. Redistributions of source code must retain the above copyright
9f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer.
10f1b9d127SSheldon Hearn * 2. Redistributions in binary form must reproduce the above copyright
11f1b9d127SSheldon Hearn * notice, this list of conditions and the following disclaimer in the
12f1b9d127SSheldon Hearn * documentation and/or other materials provided with the distribution.
13f1b9d127SSheldon Hearn * 3. All advertising materials mentioning features or use of this software
14f1b9d127SSheldon Hearn * must display the following acknowledgement:
15f1b9d127SSheldon Hearn * This product includes software developed by Boris Popov.
16f1b9d127SSheldon Hearn * 4. Neither the name of the author nor the names of any co-contributors
17f1b9d127SSheldon Hearn * may be used to endorse or promote products derived from this software
18f1b9d127SSheldon Hearn * without specific prior written permission.
19f1b9d127SSheldon Hearn *
20f1b9d127SSheldon Hearn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21f1b9d127SSheldon Hearn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22f1b9d127SSheldon Hearn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f1b9d127SSheldon Hearn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24f1b9d127SSheldon Hearn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25f1b9d127SSheldon Hearn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26f1b9d127SSheldon Hearn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27f1b9d127SSheldon Hearn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28f1b9d127SSheldon Hearn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29f1b9d127SSheldon Hearn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30f1b9d127SSheldon Hearn * SUCH DAMAGE.
31f1b9d127SSheldon Hearn *
32f1b9d127SSheldon Hearn * $Id: rcfile.c,v 1.5 2001/04/16 12:46:46 bp Exp $
33f1b9d127SSheldon Hearn */
341ac62e0bSDavid E. O'Brien
35f1b9d127SSheldon Hearn #include <sys/types.h>
36f1b9d127SSheldon Hearn #include <sys/queue.h>
37f1b9d127SSheldon Hearn #include <ctype.h>
38f1b9d127SSheldon Hearn #include <errno.h>
39f1b9d127SSheldon Hearn #include <stdio.h>
40f1b9d127SSheldon Hearn #include <string.h>
41f1b9d127SSheldon Hearn #include <stdlib.h>
42f1b9d127SSheldon Hearn #include <pwd.h>
43f1b9d127SSheldon Hearn #include <unistd.h>
44f1b9d127SSheldon Hearn #include <err.h>
45f1b9d127SSheldon Hearn
46f1b9d127SSheldon Hearn #include <cflib.h>
47f1b9d127SSheldon Hearn #include "rcfile_priv.h"
48f1b9d127SSheldon Hearn
49f1b9d127SSheldon Hearn SLIST_HEAD(rcfile_head, rcfile);
50f1b9d127SSheldon Hearn static struct rcfile_head pf_head = {NULL};
51f1b9d127SSheldon Hearn
52f1b9d127SSheldon Hearn static struct rcfile* rc_cachelookup(const char *filename);
53f1b9d127SSheldon Hearn static struct rcsection *rc_findsect(struct rcfile *rcp, const char *sectname);
54f1b9d127SSheldon Hearn static struct rcsection *rc_addsect(struct rcfile *rcp, const char *sectname);
55f1b9d127SSheldon Hearn static int rc_freesect(struct rcfile *rcp, struct rcsection *rsp);
56f1b9d127SSheldon Hearn static struct rckey *rc_sect_findkey(struct rcsection *rsp, const char *keyname);
57f1b9d127SSheldon Hearn static struct rckey *rc_sect_addkey(struct rcsection *rsp, const char *name, const char *value);
58f1b9d127SSheldon Hearn static void rc_key_free(struct rckey *p);
59f1b9d127SSheldon Hearn static void rc_parse(struct rcfile *rcp);
60f1b9d127SSheldon Hearn
61f1b9d127SSheldon Hearn
62f1b9d127SSheldon Hearn /*
63f1b9d127SSheldon Hearn * open rcfile and load its content, if already open - return previous handle
64f1b9d127SSheldon Hearn */
65f1b9d127SSheldon Hearn int
rc_open(const char * filename,const char * mode,struct rcfile ** rcfile)66f1b9d127SSheldon Hearn rc_open(const char *filename, const char *mode, struct rcfile **rcfile)
67f1b9d127SSheldon Hearn {
68f1b9d127SSheldon Hearn struct rcfile *rcp;
69f1b9d127SSheldon Hearn FILE *f;
70f1b9d127SSheldon Hearn
71f1b9d127SSheldon Hearn rcp = rc_cachelookup(filename);
72f1b9d127SSheldon Hearn if (rcp) {
73f1b9d127SSheldon Hearn *rcfile = rcp;
74f1b9d127SSheldon Hearn return 0;
75f1b9d127SSheldon Hearn }
76f1b9d127SSheldon Hearn f = fopen(filename, mode);
77f1b9d127SSheldon Hearn if (f == NULL)
78f1b9d127SSheldon Hearn return errno;
79f1b9d127SSheldon Hearn rcp = malloc(sizeof(struct rcfile));
80f1b9d127SSheldon Hearn if (rcp == NULL) {
81f1b9d127SSheldon Hearn fclose(f);
82f1b9d127SSheldon Hearn return ENOMEM;
83f1b9d127SSheldon Hearn }
84f1b9d127SSheldon Hearn bzero(rcp, sizeof(struct rcfile));
85f1b9d127SSheldon Hearn rcp->rf_name = strdup(filename);
86f1b9d127SSheldon Hearn rcp->rf_f = f;
87f1b9d127SSheldon Hearn SLIST_INSERT_HEAD(&pf_head, rcp, rf_next);
88f1b9d127SSheldon Hearn rc_parse(rcp);
89f1b9d127SSheldon Hearn *rcfile = rcp;
90f1b9d127SSheldon Hearn return 0;
91f1b9d127SSheldon Hearn }
92f1b9d127SSheldon Hearn
93f1b9d127SSheldon Hearn int
rc_merge(const char * filename,struct rcfile ** rcfile)94f1b9d127SSheldon Hearn rc_merge(const char *filename, struct rcfile **rcfile)
95f1b9d127SSheldon Hearn {
96f1b9d127SSheldon Hearn struct rcfile *rcp = *rcfile;
97f1b9d127SSheldon Hearn FILE *f, *t;
98f1b9d127SSheldon Hearn
99f1b9d127SSheldon Hearn if (rcp == NULL) {
100f1b9d127SSheldon Hearn return rc_open(filename, "r", rcfile);
101f1b9d127SSheldon Hearn }
102f1b9d127SSheldon Hearn f = fopen (filename, "r");
103f1b9d127SSheldon Hearn if (f == NULL)
104f1b9d127SSheldon Hearn return errno;
105f1b9d127SSheldon Hearn t = rcp->rf_f;
106f1b9d127SSheldon Hearn rcp->rf_f = f;
107f1b9d127SSheldon Hearn rc_parse(rcp);
108f1b9d127SSheldon Hearn rcp->rf_f = t;
109f1b9d127SSheldon Hearn fclose(f);
110f1b9d127SSheldon Hearn return 0;
111f1b9d127SSheldon Hearn }
112f1b9d127SSheldon Hearn
113f1b9d127SSheldon Hearn int
rc_close(struct rcfile * rcp)114f1b9d127SSheldon Hearn rc_close(struct rcfile *rcp)
115f1b9d127SSheldon Hearn {
116f1b9d127SSheldon Hearn struct rcsection *p, *n;
117f1b9d127SSheldon Hearn
118f1b9d127SSheldon Hearn fclose(rcp->rf_f);
119f1b9d127SSheldon Hearn for(p = SLIST_FIRST(&rcp->rf_sect); p;) {
120f1b9d127SSheldon Hearn n = p;
121f1b9d127SSheldon Hearn p = SLIST_NEXT(p,rs_next);
122f1b9d127SSheldon Hearn rc_freesect(rcp, n);
123f1b9d127SSheldon Hearn }
124f1b9d127SSheldon Hearn free(rcp->rf_name);
125f1b9d127SSheldon Hearn SLIST_REMOVE(&pf_head, rcp, rcfile, rf_next);
126f1b9d127SSheldon Hearn free(rcp);
127f1b9d127SSheldon Hearn return 0;
128f1b9d127SSheldon Hearn }
129f1b9d127SSheldon Hearn
130f1b9d127SSheldon Hearn static struct rcfile*
rc_cachelookup(const char * filename)131f1b9d127SSheldon Hearn rc_cachelookup(const char *filename)
132f1b9d127SSheldon Hearn {
133f1b9d127SSheldon Hearn struct rcfile *p;
134f1b9d127SSheldon Hearn
135f1b9d127SSheldon Hearn SLIST_FOREACH(p, &pf_head, rf_next)
136f1b9d127SSheldon Hearn if (strcmp (filename, p->rf_name) == 0)
137f1b9d127SSheldon Hearn return p;
138f1b9d127SSheldon Hearn return 0;
139f1b9d127SSheldon Hearn }
140f1b9d127SSheldon Hearn
141f1b9d127SSheldon Hearn static struct rcsection *
rc_findsect(struct rcfile * rcp,const char * sectname)142f1b9d127SSheldon Hearn rc_findsect(struct rcfile *rcp, const char *sectname)
143f1b9d127SSheldon Hearn {
144f1b9d127SSheldon Hearn struct rcsection *p;
145f1b9d127SSheldon Hearn
146f1b9d127SSheldon Hearn SLIST_FOREACH(p, &rcp->rf_sect, rs_next)
147f1b9d127SSheldon Hearn if (strcmp(p->rs_name, sectname)==0)
148f1b9d127SSheldon Hearn return p;
149f1b9d127SSheldon Hearn return NULL;
150f1b9d127SSheldon Hearn }
151f1b9d127SSheldon Hearn
152f1b9d127SSheldon Hearn static struct rcsection *
rc_addsect(struct rcfile * rcp,const char * sectname)153f1b9d127SSheldon Hearn rc_addsect(struct rcfile *rcp, const char *sectname)
154f1b9d127SSheldon Hearn {
155f1b9d127SSheldon Hearn struct rcsection *p;
156*4d6e5658SGleb Popov const char* sectletter = sectname;
157f1b9d127SSheldon Hearn
158f1b9d127SSheldon Hearn p = rc_findsect(rcp, sectname);
159f1b9d127SSheldon Hearn if (p) return p;
160f1b9d127SSheldon Hearn p = malloc(sizeof(*p));
161f1b9d127SSheldon Hearn if (!p) return NULL;
162*4d6e5658SGleb Popov for(sectletter = sectname; *sectletter; sectletter++) {
163*4d6e5658SGleb Popov if (islower(*sectletter)) {
164*4d6e5658SGleb Popov if (strcmp(sectname, "default"))
165*4d6e5658SGleb Popov dprintf(STDERR_FILENO, "warning: section name [%s] contains lower-case letters\n", sectname);
166*4d6e5658SGleb Popov break;
167*4d6e5658SGleb Popov }
168*4d6e5658SGleb Popov }
169f1b9d127SSheldon Hearn p->rs_name = strdup(sectname);
170f1b9d127SSheldon Hearn SLIST_INIT(&p->rs_keys);
171f1b9d127SSheldon Hearn SLIST_INSERT_HEAD(&rcp->rf_sect, p, rs_next);
172f1b9d127SSheldon Hearn return p;
173f1b9d127SSheldon Hearn }
174f1b9d127SSheldon Hearn
175f1b9d127SSheldon Hearn static int
rc_freesect(struct rcfile * rcp,struct rcsection * rsp)176f1b9d127SSheldon Hearn rc_freesect(struct rcfile *rcp, struct rcsection *rsp)
177f1b9d127SSheldon Hearn {
178f1b9d127SSheldon Hearn struct rckey *p,*n;
179f1b9d127SSheldon Hearn
180f1b9d127SSheldon Hearn SLIST_REMOVE(&rcp->rf_sect, rsp, rcsection, rs_next);
181f1b9d127SSheldon Hearn for(p = SLIST_FIRST(&rsp->rs_keys);p;) {
182f1b9d127SSheldon Hearn n = p;
183f1b9d127SSheldon Hearn p = SLIST_NEXT(p,rk_next);
184f1b9d127SSheldon Hearn rc_key_free(n);
185f1b9d127SSheldon Hearn }
186f1b9d127SSheldon Hearn free(rsp->rs_name);
187f1b9d127SSheldon Hearn free(rsp);
188f1b9d127SSheldon Hearn return 0;
189f1b9d127SSheldon Hearn }
190f1b9d127SSheldon Hearn
191f1b9d127SSheldon Hearn static struct rckey *
rc_sect_findkey(struct rcsection * rsp,const char * keyname)192f1b9d127SSheldon Hearn rc_sect_findkey(struct rcsection *rsp, const char *keyname)
193f1b9d127SSheldon Hearn {
194f1b9d127SSheldon Hearn struct rckey *p;
195f1b9d127SSheldon Hearn
196f1b9d127SSheldon Hearn SLIST_FOREACH(p, &rsp->rs_keys, rk_next)
197f1b9d127SSheldon Hearn if (strcmp(p->rk_name, keyname)==0)
198f1b9d127SSheldon Hearn return p;
199f1b9d127SSheldon Hearn return NULL;
200f1b9d127SSheldon Hearn }
201f1b9d127SSheldon Hearn
202f1b9d127SSheldon Hearn static struct rckey *
rc_sect_addkey(struct rcsection * rsp,const char * name,const char * value)203f1b9d127SSheldon Hearn rc_sect_addkey(struct rcsection *rsp, const char *name, const char *value)
204f1b9d127SSheldon Hearn {
205f1b9d127SSheldon Hearn struct rckey *p;
206f1b9d127SSheldon Hearn
207f1b9d127SSheldon Hearn p = rc_sect_findkey(rsp, name);
208f1b9d127SSheldon Hearn if (p) {
209f1b9d127SSheldon Hearn free(p->rk_value);
210f1b9d127SSheldon Hearn } else {
211f1b9d127SSheldon Hearn p = malloc(sizeof(*p));
212f1b9d127SSheldon Hearn if (!p) return NULL;
213f1b9d127SSheldon Hearn SLIST_INSERT_HEAD(&rsp->rs_keys, p, rk_next);
214f1b9d127SSheldon Hearn p->rk_name = strdup(name);
215f1b9d127SSheldon Hearn }
216f1b9d127SSheldon Hearn p->rk_value = value ? strdup(value) : strdup("");
217f1b9d127SSheldon Hearn return p;
218f1b9d127SSheldon Hearn }
219f1b9d127SSheldon Hearn
220f1b9d127SSheldon Hearn #if 0
221f1b9d127SSheldon Hearn void
222f1b9d127SSheldon Hearn rc_sect_delkey(struct rcsection *rsp, struct rckey *p)
223f1b9d127SSheldon Hearn {
224f1b9d127SSheldon Hearn
225f1b9d127SSheldon Hearn SLIST_REMOVE(&rsp->rs_keys, p, rckey, rk_next);
226f1b9d127SSheldon Hearn rc_key_free(p);
227f1b9d127SSheldon Hearn return;
228f1b9d127SSheldon Hearn }
229f1b9d127SSheldon Hearn #endif
230f1b9d127SSheldon Hearn
231f1b9d127SSheldon Hearn static void
rc_key_free(struct rckey * p)232f1b9d127SSheldon Hearn rc_key_free(struct rckey *p)
233f1b9d127SSheldon Hearn {
234f1b9d127SSheldon Hearn free(p->rk_value);
235f1b9d127SSheldon Hearn free(p->rk_name);
236f1b9d127SSheldon Hearn free(p);
237f1b9d127SSheldon Hearn }
238f1b9d127SSheldon Hearn
239f1b9d127SSheldon Hearn enum { stNewLine, stHeader, stSkipToEOL, stGetKey, stGetValue};
240f1b9d127SSheldon Hearn
241f1b9d127SSheldon Hearn static void
rc_parse(struct rcfile * rcp)242f1b9d127SSheldon Hearn rc_parse(struct rcfile *rcp)
243f1b9d127SSheldon Hearn {
244f1b9d127SSheldon Hearn FILE *f = rcp->rf_f;
245f1b9d127SSheldon Hearn int state = stNewLine, c;
246f1b9d127SSheldon Hearn struct rcsection *rsp = NULL;
247f1b9d127SSheldon Hearn struct rckey *rkp = NULL;
248f1b9d127SSheldon Hearn char buf[2048];
249f1b9d127SSheldon Hearn char *next = buf, *last = &buf[sizeof(buf)-1];
250f1b9d127SSheldon Hearn
251f1b9d127SSheldon Hearn while ((c = getc (f)) != EOF) {
252f1b9d127SSheldon Hearn if (c == '\r')
253f1b9d127SSheldon Hearn continue;
254f1b9d127SSheldon Hearn if (state == stNewLine) {
255f1b9d127SSheldon Hearn next = buf;
256f1b9d127SSheldon Hearn if (isspace(c))
257f1b9d127SSheldon Hearn continue; /* skip leading junk */
258f1b9d127SSheldon Hearn if (c == '[') {
259f1b9d127SSheldon Hearn state = stHeader;
260f1b9d127SSheldon Hearn rsp = NULL;
261f1b9d127SSheldon Hearn continue;
262f1b9d127SSheldon Hearn }
263f1b9d127SSheldon Hearn if (c == '#' || c == ';') {
264f1b9d127SSheldon Hearn state = stSkipToEOL;
265f1b9d127SSheldon Hearn } else { /* something meaningfull */
266f1b9d127SSheldon Hearn state = stGetKey;
267f1b9d127SSheldon Hearn }
268f1b9d127SSheldon Hearn }
269f1b9d127SSheldon Hearn if (state == stSkipToEOL || next == last) {/* ignore long lines */
270f1b9d127SSheldon Hearn if (c == '\n'){
271f1b9d127SSheldon Hearn state = stNewLine;
272f1b9d127SSheldon Hearn next = buf;
273f1b9d127SSheldon Hearn }
274f1b9d127SSheldon Hearn continue;
275f1b9d127SSheldon Hearn }
276f1b9d127SSheldon Hearn if (state == stHeader) {
277f1b9d127SSheldon Hearn if (c == ']') {
278f1b9d127SSheldon Hearn *next = 0;
279f1b9d127SSheldon Hearn next = buf;
280f1b9d127SSheldon Hearn rsp = rc_addsect(rcp, buf);
281f1b9d127SSheldon Hearn state = stSkipToEOL;
282f1b9d127SSheldon Hearn } else
283f1b9d127SSheldon Hearn *next++ = c;
284f1b9d127SSheldon Hearn continue;
285f1b9d127SSheldon Hearn }
286f1b9d127SSheldon Hearn if (state == stGetKey) {
287f1b9d127SSheldon Hearn if (c == ' ' || c == '\t')/* side effect: 'key name='*/
288f1b9d127SSheldon Hearn continue; /* become 'keyname=' */
289f1b9d127SSheldon Hearn if (c == '\n') { /* silently ignore ... */
290f1b9d127SSheldon Hearn state = stNewLine;
291f1b9d127SSheldon Hearn continue;
292f1b9d127SSheldon Hearn }
293f1b9d127SSheldon Hearn if (c != '=') {
294f1b9d127SSheldon Hearn *next++ = c;
295f1b9d127SSheldon Hearn continue;
296f1b9d127SSheldon Hearn }
297f1b9d127SSheldon Hearn *next = 0;
298f1b9d127SSheldon Hearn if (rsp == NULL) {
299f1b9d127SSheldon Hearn fprintf(stderr, "Key '%s' defined before section\n", buf);
300f1b9d127SSheldon Hearn state = stSkipToEOL;
301f1b9d127SSheldon Hearn continue;
302f1b9d127SSheldon Hearn }
303f1b9d127SSheldon Hearn rkp = rc_sect_addkey(rsp, buf, NULL);
304f1b9d127SSheldon Hearn next = buf;
305f1b9d127SSheldon Hearn state = stGetValue;
306f1b9d127SSheldon Hearn continue;
307f1b9d127SSheldon Hearn }
308f1b9d127SSheldon Hearn /* only stGetValue left */
309f1b9d127SSheldon Hearn if (state != stGetValue) {
310f1b9d127SSheldon Hearn fprintf(stderr, "Well, I can't parse file '%s'\n",rcp->rf_name);
311f1b9d127SSheldon Hearn state = stSkipToEOL;
312f1b9d127SSheldon Hearn }
313f1b9d127SSheldon Hearn if (c != '\n') {
314f1b9d127SSheldon Hearn *next++ = c;
315f1b9d127SSheldon Hearn continue;
316f1b9d127SSheldon Hearn }
317f1b9d127SSheldon Hearn *next = 0;
318f1b9d127SSheldon Hearn rkp->rk_value = strdup(buf);
319f1b9d127SSheldon Hearn state = stNewLine;
320f1b9d127SSheldon Hearn rkp = NULL;
321f1b9d127SSheldon Hearn } /* while */
322f1b9d127SSheldon Hearn if (c == EOF && state == stGetValue) {
323f1b9d127SSheldon Hearn *next = 0;
324f1b9d127SSheldon Hearn rkp->rk_value = strdup(buf);
325f1b9d127SSheldon Hearn }
326f1b9d127SSheldon Hearn return;
327f1b9d127SSheldon Hearn }
328f1b9d127SSheldon Hearn
329f1b9d127SSheldon Hearn int
rc_getstringptr(struct rcfile * rcp,const char * section,const char * key,char ** dest)330f1b9d127SSheldon Hearn rc_getstringptr(struct rcfile *rcp, const char *section, const char *key,
331f1b9d127SSheldon Hearn char **dest)
332f1b9d127SSheldon Hearn {
333f1b9d127SSheldon Hearn struct rcsection *rsp;
334f1b9d127SSheldon Hearn struct rckey *rkp;
335f1b9d127SSheldon Hearn
336f1b9d127SSheldon Hearn *dest = NULL;
337f1b9d127SSheldon Hearn rsp = rc_findsect(rcp, section);
338f1b9d127SSheldon Hearn if (!rsp) return ENOENT;
339f1b9d127SSheldon Hearn rkp = rc_sect_findkey(rsp,key);
340f1b9d127SSheldon Hearn if (!rkp) return ENOENT;
341f1b9d127SSheldon Hearn *dest = rkp->rk_value;
342f1b9d127SSheldon Hearn return 0;
343f1b9d127SSheldon Hearn }
344f1b9d127SSheldon Hearn
345f1b9d127SSheldon Hearn int
rc_getstring(struct rcfile * rcp,const char * section,const char * key,size_t maxlen,char * dest)346f1b9d127SSheldon Hearn rc_getstring(struct rcfile *rcp, const char *section, const char *key,
347f1b9d127SSheldon Hearn size_t maxlen, char *dest)
348f1b9d127SSheldon Hearn {
349f1b9d127SSheldon Hearn char *value;
350f1b9d127SSheldon Hearn int error;
351f1b9d127SSheldon Hearn
352f1b9d127SSheldon Hearn error = rc_getstringptr(rcp, section, key, &value);
353f1b9d127SSheldon Hearn if (error)
354f1b9d127SSheldon Hearn return error;
355f1b9d127SSheldon Hearn if (strlen(value) >= maxlen) {
3561ac62e0bSDavid E. O'Brien warnx("line too long for key '%s' in section '%s', max = %zd\n", key, section, maxlen);
357f1b9d127SSheldon Hearn return EINVAL;
358f1b9d127SSheldon Hearn }
359f1b9d127SSheldon Hearn strcpy(dest, value);
360f1b9d127SSheldon Hearn return 0;
361f1b9d127SSheldon Hearn }
362f1b9d127SSheldon Hearn
363f1b9d127SSheldon Hearn int
rc_getint(struct rcfile * rcp,const char * section,const char * key,int * value)364f1b9d127SSheldon Hearn rc_getint(struct rcfile *rcp, const char *section, const char *key, int *value)
365f1b9d127SSheldon Hearn {
366f1b9d127SSheldon Hearn struct rcsection *rsp;
367f1b9d127SSheldon Hearn struct rckey *rkp;
368f1b9d127SSheldon Hearn
369f1b9d127SSheldon Hearn rsp = rc_findsect(rcp, section);
370f1b9d127SSheldon Hearn if (!rsp)
371f1b9d127SSheldon Hearn return ENOENT;
372f1b9d127SSheldon Hearn rkp = rc_sect_findkey(rsp, key);
373f1b9d127SSheldon Hearn if (!rkp)
374f1b9d127SSheldon Hearn return ENOENT;
375f1b9d127SSheldon Hearn errno = 0;
376f1b9d127SSheldon Hearn *value = strtol(rkp->rk_value, NULL, 0);
377f1b9d127SSheldon Hearn if (errno) {
378f1b9d127SSheldon Hearn warnx("invalid int value '%s' for key '%s' in section '%s'\n", rkp->rk_value, key, section);
379f1b9d127SSheldon Hearn return errno;
380f1b9d127SSheldon Hearn }
381f1b9d127SSheldon Hearn return 0;
382f1b9d127SSheldon Hearn }
383f1b9d127SSheldon Hearn
384f1b9d127SSheldon Hearn /*
385f1b9d127SSheldon Hearn * 1,yes,true
386f1b9d127SSheldon Hearn * 0,no,false
387f1b9d127SSheldon Hearn */
388f1b9d127SSheldon Hearn int
rc_getbool(struct rcfile * rcp,const char * section,const char * key,int * value)389f1b9d127SSheldon Hearn rc_getbool(struct rcfile *rcp, const char *section, const char *key, int *value)
390f1b9d127SSheldon Hearn {
391f1b9d127SSheldon Hearn struct rcsection *rsp;
392f1b9d127SSheldon Hearn struct rckey *rkp;
393f1b9d127SSheldon Hearn char *p;
394f1b9d127SSheldon Hearn
395f1b9d127SSheldon Hearn rsp = rc_findsect(rcp, section);
396f1b9d127SSheldon Hearn if (!rsp) return ENOENT;
397f1b9d127SSheldon Hearn rkp = rc_sect_findkey(rsp,key);
398f1b9d127SSheldon Hearn if (!rkp) return ENOENT;
399f1b9d127SSheldon Hearn p = rkp->rk_value;
400f1b9d127SSheldon Hearn while (*p && isspace(*p)) p++;
401f1b9d127SSheldon Hearn if (*p == '0' || strcasecmp(p,"no") == 0 || strcasecmp(p,"false") == 0) {
402f1b9d127SSheldon Hearn *value = 0;
403f1b9d127SSheldon Hearn return 0;
404f1b9d127SSheldon Hearn }
405f1b9d127SSheldon Hearn if (*p == '1' || strcasecmp(p,"yes") == 0 || strcasecmp(p,"true") == 0) {
406f1b9d127SSheldon Hearn *value = 1;
407f1b9d127SSheldon Hearn return 0;
408f1b9d127SSheldon Hearn }
409f1b9d127SSheldon Hearn fprintf(stderr, "invalid boolean value '%s' for key '%s' in section '%s' \n",p, key, section);
410f1b9d127SSheldon Hearn return EINVAL;
411f1b9d127SSheldon Hearn }
412f1b9d127SSheldon Hearn
413f1b9d127SSheldon Hearn /*
414f1b9d127SSheldon Hearn * Unified command line/rc file parser
415f1b9d127SSheldon Hearn */
416f1b9d127SSheldon Hearn int
opt_args_parse(struct rcfile * rcp,struct opt_args * ap,const char * sect,opt_callback_t * callback)417f1b9d127SSheldon Hearn opt_args_parse(struct rcfile *rcp, struct opt_args *ap, const char *sect,
418f1b9d127SSheldon Hearn opt_callback_t *callback)
419f1b9d127SSheldon Hearn {
420f1b9d127SSheldon Hearn int len, error;
421f1b9d127SSheldon Hearn
422f1b9d127SSheldon Hearn for (; ap->opt; ap++) {
423f1b9d127SSheldon Hearn switch (ap->type) {
424f1b9d127SSheldon Hearn case OPTARG_STR:
425f1b9d127SSheldon Hearn if (rc_getstringptr(rcp, sect, ap->name, &ap->str) != 0)
426f1b9d127SSheldon Hearn break;
427f1b9d127SSheldon Hearn len = strlen(ap->str);
428f1b9d127SSheldon Hearn if (len > ap->ival) {
429f1b9d127SSheldon Hearn warnx("rc: argument for option '%c' (%s) too long\n", ap->opt, ap->name);
430f1b9d127SSheldon Hearn return EINVAL;
431f1b9d127SSheldon Hearn }
432f1b9d127SSheldon Hearn callback(ap);
433f1b9d127SSheldon Hearn break;
434f1b9d127SSheldon Hearn case OPTARG_BOOL:
435f1b9d127SSheldon Hearn error = rc_getbool(rcp, sect, ap->name, &ap->ival);
436f1b9d127SSheldon Hearn if (error == ENOENT)
437f1b9d127SSheldon Hearn break;
438f1b9d127SSheldon Hearn if (error)
439f1b9d127SSheldon Hearn return EINVAL;
440f1b9d127SSheldon Hearn callback(ap);
441f1b9d127SSheldon Hearn break;
442f1b9d127SSheldon Hearn case OPTARG_INT:
443f1b9d127SSheldon Hearn if (rc_getint(rcp, sect, ap->name, &ap->ival) != 0)
444f1b9d127SSheldon Hearn break;
445f1b9d127SSheldon Hearn if (((ap->flag & OPTFL_HAVEMIN) && ap->ival < ap->min) ||
446f1b9d127SSheldon Hearn ((ap->flag & OPTFL_HAVEMAX) && ap->ival > ap->max)) {
447f1b9d127SSheldon Hearn warnx("rc: argument for option '%c' (%s) should be in [%d-%d] range\n",
448f1b9d127SSheldon Hearn ap->opt, ap->name, ap->min, ap->max);
449f1b9d127SSheldon Hearn return EINVAL;
450f1b9d127SSheldon Hearn }
451f1b9d127SSheldon Hearn callback(ap);
452f1b9d127SSheldon Hearn break;
453f1b9d127SSheldon Hearn default:
454f1b9d127SSheldon Hearn break;
455f1b9d127SSheldon Hearn }
456f1b9d127SSheldon Hearn }
457f1b9d127SSheldon Hearn return 0;
458f1b9d127SSheldon Hearn }
459f1b9d127SSheldon Hearn
460f1b9d127SSheldon Hearn int
opt_args_parseopt(struct opt_args * ap,int opt,char * arg,opt_callback_t * callback)461f1b9d127SSheldon Hearn opt_args_parseopt(struct opt_args *ap, int opt, char *arg,
462f1b9d127SSheldon Hearn opt_callback_t *callback)
463f1b9d127SSheldon Hearn {
464f1b9d127SSheldon Hearn int len;
465f1b9d127SSheldon Hearn
466f1b9d127SSheldon Hearn for (; ap->opt; ap++) {
467f1b9d127SSheldon Hearn if (ap->opt != opt)
468f1b9d127SSheldon Hearn continue;
469f1b9d127SSheldon Hearn switch (ap->type) {
470f1b9d127SSheldon Hearn case OPTARG_STR:
471f1b9d127SSheldon Hearn ap->str = arg;
472f1b9d127SSheldon Hearn if (arg) {
473f1b9d127SSheldon Hearn len = strlen(ap->str);
474f1b9d127SSheldon Hearn if (len > ap->ival) {
475f1b9d127SSheldon Hearn warnx("opt: Argument for option '%c' (%s) too long\n", ap->opt, ap->name);
476f1b9d127SSheldon Hearn return EINVAL;
477f1b9d127SSheldon Hearn }
478f1b9d127SSheldon Hearn callback(ap);
479f1b9d127SSheldon Hearn }
480f1b9d127SSheldon Hearn break;
481f1b9d127SSheldon Hearn case OPTARG_BOOL:
482f1b9d127SSheldon Hearn ap->ival = 0;
483f1b9d127SSheldon Hearn callback(ap);
484f1b9d127SSheldon Hearn break;
485f1b9d127SSheldon Hearn case OPTARG_INT:
486f1b9d127SSheldon Hearn errno = 0;
487f1b9d127SSheldon Hearn ap->ival = strtol(arg, NULL, 0);
488f1b9d127SSheldon Hearn if (errno) {
489f1b9d127SSheldon Hearn warnx("opt: Invalid integer value for option '%c' (%s).\n",ap->opt,ap->name);
490f1b9d127SSheldon Hearn return EINVAL;
491f1b9d127SSheldon Hearn }
492f1b9d127SSheldon Hearn if (((ap->flag & OPTFL_HAVEMIN) &&
493f1b9d127SSheldon Hearn (ap->ival < ap->min)) ||
494f1b9d127SSheldon Hearn ((ap->flag & OPTFL_HAVEMAX) &&
495f1b9d127SSheldon Hearn (ap->ival > ap->max))) {
496f1b9d127SSheldon Hearn warnx("opt: Argument for option '%c' (%s) should be in [%d-%d] range\n",ap->opt,ap->name,ap->min,ap->max);
497f1b9d127SSheldon Hearn return EINVAL;
498f1b9d127SSheldon Hearn }
499f1b9d127SSheldon Hearn callback(ap);
500f1b9d127SSheldon Hearn break;
501f1b9d127SSheldon Hearn default:
502f1b9d127SSheldon Hearn break;
503f1b9d127SSheldon Hearn }
504f1b9d127SSheldon Hearn break;
505f1b9d127SSheldon Hearn }
506f1b9d127SSheldon Hearn return 0;
507f1b9d127SSheldon Hearn }
508f1b9d127SSheldon Hearn
509