Annotation of libaitcfg/src/parse.c, revision 1.10
1.2 misho 1: /*************************************************************************
2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.10 ! misho 6: * $Id: parse.c,v 1.9.2.1 2012/08/06 14:52:51 misho Exp $
1.2 misho 7: *
1.6 misho 8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.7 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.6 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.1 misho 46: #include "global.h"
47: #include "aitcfg.h"
48:
49:
1.7 misho 50: static inline int
51: cfg_Write(FILE *f, char *fmt, ...)
1.1 misho 52: {
53: int ret = 0;
54: va_list lst;
55:
56: va_start(lst, fmt);
57: ret = vfprintf(f, fmt, lst);
58: va_end(lst);
59:
60: return ret;
61: }
62:
1.7 misho 63: static inline void
64: _invertQueue(cfg_root_t * __restrict cfg)
1.3 misho 65: {
1.7 misho 66: struct tagCfg *item, *next, *prev = NULL;
1.3 misho 67:
1.7 misho 68: SLIST_FOREACH_SAFE(item, cfg, cfg_next, next) {
69: item->cfg_next.sle_next = prev;
1.3 misho 70: prev = item;
71: }
72: cfg->slh_first = prev;
73: }
74:
1.7 misho 75:
76: /*
77: * cfgReadConfig() - Read file and add new item at config root
78: *
79: * @f = File resource
80: * @cfg = Config root
81: * return: -1 error or 0 ok
82: */
1.9 misho 83: int
84: cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
1.3 misho 85: {
1.7 misho 86: char line[BUFSIZ];
87: struct tagCfg *av = NULL;
88: int flg = 0;
89: char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
1.3 misho 90:
1.7 misho 91: while (!feof(f)) {
92: memset(line, 0, sizeof line);
93: fgets(line, sizeof line - 1, f);
94: #ifdef SUPPORT_USER_EOF
95: /* check for user end-of-file */
96: if (line[0] == '.' && line[1] == '\n')
97: break;
98: #endif
99: if (!(psAttr = strpbrk(line, "\r\n"))) {
100: /* skip line, too long */
101: continue;
102: } else {
103: *psAttr = 0;
104: io_TrimStr(line);
105: }
1.3 misho 106:
1.7 misho 107: if (flg) {
108: /* continues line */
109: if (!av)
110: continue;
111: else
112: psAttr = line + strlen(line) - 1;
113: if (*psAttr == '\\')
114: *psAttr = 0;
115: else
116: flg = 0;
117: /* concat line to value */
118: AIT_SET_STRCAT(&av->cfg_val, line);
1.10 ! misho 119: if (!flg && AIT_ADDR(&av->cfg_val))
1.7 misho 120: io_UnquotStr((char*) AIT_GET_STR(&av->cfg_val));
121: continue;
122: }
1.3 misho 123:
1.7 misho 124: /* *NEW PAIR* alloc new pair element */
1.8 misho 125: av = io_malloc(sizeof(struct tagCfg));
1.7 misho 126: if (!av) {
127: LOGERR;
128: return -1;
129: } else {
130: memset(av, 0, sizeof(struct tagCfg));
131: CFG_RC_LOCK(cfg);
132: SLIST_INSERT_HEAD(cfg, av, cfg_next);
133: CFG_RC_UNLOCK(cfg);
1.3 misho 134: }
1.7 misho 135:
136: /* check for continues line */
1.8 misho 137: psAttr = line + (*line ? strlen(line) : 1) - 1;
1.7 misho 138: if (*psAttr == '\\') {
139: *psAttr = 0;
140: flg = 1;
1.3 misho 141: }
142:
1.7 misho 143: /* check for comment or empty line */
144: if (!*line || *line == '#' || *line == ';') {
145: AIT_SET_STR(&av->cfg_val, line);
146: continue;
147: }
148: /* section */
149: if (*line == '[') {
150: AIT_SET_STR(&av->cfg_val, line);
151: psAttr = line + strlen(line) - 1;
152: if (*psAttr == ']') {
153: *psAttr = 0;
154: flg = 0;
155: strlcpy(szSection, line + 1, sizeof szSection);
156: } else
157: ioDEBUG(7, "Ignore section '%s' ... not found ']'", line);
158: continue;
159: }
160: /* parse pair */
161: if (!(psAttr = strchr(line, '='))) {
162: AIT_SET_STR(&av->cfg_val, line);
163: ioDEBUG(7, "Ignore a/v '%s' ... not found '='", line);
164: continue;
1.3 misho 165: } else {
1.7 misho 166: *psAttr = 0;
167: psVal = psAttr + 1;
168: psAttr = line;
169: }
170:
171: /* if exists, added section name to element */
172: if (*szSection) {
173: AIT_SET_STR(&av->cfg_sec, szSection);
174: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
175: io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
1.3 misho 176: }
177:
1.7 misho 178: io_RTrimStr(psAttr);
179: io_LTrimStr(psVal);
180: if (!flg)
181: io_UnquotStr(psVal);
182: AIT_SET_STR(&av->cfg_val, psVal);
183: AIT_SET_STR(&av->cfg_attr, psAttr);
184: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
185: io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
186:
187: CFG_RC_LOCK(cfg);
188: RB_INSERT(tagRC, cfg, av);
189: CFG_RC_UNLOCK(cfg);
1.3 misho 190: }
191:
192: return 0;
193: }
194:
1.1 misho 195: /*
1.7 misho 196: * cfgWriteConfig() - Write config from memory
197: *
198: * @f = File handle
199: * @cfg = Config root
200: * @whitespace = Additional whitespace characters to file
201: * return: -1 error or 0 ok
202: */
203: int
204: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
1.1 misho 205: {
1.7 misho 206: struct tagCfg *av;
207: time_t tim;
208: char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { 0 };
1.1 misho 209:
1.7 misho 210: CFG_RC_LOCK(cfg);
211: _invertQueue(cfg);
212: SLIST_FOREACH(av, cfg, cfg_next) {
213: /* add +1 line for section [] */
1.10 ! misho 214: if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) &&
1.7 misho 215: strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
216: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
217: if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
218: LOGERR;
219: CFG_RC_UNLOCK(cfg);
220: return -1;
1.1 misho 221: }
222: }
1.7 misho 223: if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
224: memset(szSection, 0, sizeof szSection);
225: if (!cfg_Write(f, "\n[]\n")) {
1.1 misho 226: LOGERR;
1.7 misho 227: CFG_RC_UNLOCK(cfg);
1.1 misho 228: return -1;
229: }
1.7 misho 230: }
231:
232: /* build line */
233: memset(line, 0, sizeof line);
1.10 ! misho 234: if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string &&
! 235: AIT_ADDR(&av->cfg_attr)) {
1.7 misho 236: strlcpy(line, AIT_GET_STR(&av->cfg_attr), sizeof line);
237: if (whitespace)
238: strlcat(line, " = ", sizeof line);
239: else
240: strlcat(line, "=", sizeof line);
241: }
1.10 ! misho 242: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string &&
! 243: AIT_ADDR(&av->cfg_val))
1.7 misho 244: strlcat(line, AIT_GET_STR(&av->cfg_val), sizeof line);
1.1 misho 245:
1.7 misho 246: /* write */
247: if (!cfg_Write(f, "%s\n", line)) {
248: LOGERR;
249: CFG_RC_UNLOCK(cfg);
250: return -1;
1.1 misho 251: }
252: }
1.7 misho 253: _invertQueue(cfg);
254: CFG_RC_UNLOCK(cfg);
255:
256: if (whitespace) {
257: time(&tim);
258: memset(line, 0, sizeof line);
259: strftime(line, sizeof line, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
260: cfg_Write(f, "\n## Config was saved at :: %s ##\n", line);
261: }
1.1 misho 262:
263: return 0;
264: }
265:
266: /*
1.7 misho 267: * cfgConcatConfig() - Concat two configs into one
268: *
269: * @cfg = Config root
270: * @add_cfg = Concated config will be destroy after merge
271: * return: -1 error or 0 ok
272: */
273: int
274: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.1 misho 275: {
1.7 misho 276: struct tagCfg *item;
1.3 misho 277:
1.7 misho 278: if (!cfg || !add_cfg)
279: return -1;
1.3 misho 280:
1.7 misho 281: CFG_RC_LOCK(add_cfg);
282: CFG_RC_LOCK(cfg);
1.3 misho 283:
1.7 misho 284: /* concat lists */
285: for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
286: SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
287:
288: /* concat red-black trees */
289: SLIST_FOREACH(item, add_cfg, cfg_next)
290: RB_INSERT(tagRC, cfg, item);
1.3 misho 291:
1.7 misho 292: CFG_RC_UNLOCK(cfg);
293: CFG_RC_UNLOCK(add_cfg);
1.3 misho 294:
295: add_cfg->slh_first = NULL;
1.7 misho 296: add_cfg->rbh_root = NULL;
297: pthread_mutex_destroy(&add_cfg->rc_mtx);
298: return 0;
1.3 misho 299: }
1.1 misho 300:
1.3 misho 301: /*
1.7 misho 302: * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
303: *
304: * @cfg = Config root of main list
305: * @add_cfg = Merged config will be destroy after merge
306: * return: -1 error or 0 ok
307: */
308: int
309: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3 misho 310: {
1.10 ! misho 311: struct tagCfg *item, *merge, *add_next, *next;
1.3 misho 312: int flg;
1.1 misho 313:
1.3 misho 314: if (!cfg || !add_cfg)
1.1 misho 315: return -1;
316:
1.7 misho 317: CFG_RC_LOCK(add_cfg);
318: CFG_RC_LOCK(cfg);
1.10 ! misho 319:
! 320: /* merge lists */
1.7 misho 321: SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
322: flg = 0;
323: SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
324: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
1.3 misho 325: flg = 1;
326: break;
327: }
1.7 misho 328: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
1.10 ! misho 329: AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
1.7 misho 330: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
1.3 misho 331: flg = 1;
332: break;
1.1 misho 333: }
334: }
1.3 misho 335:
1.10 ! misho 336: if (!flg)
! 337: SLIST_INSERT_HEAD(cfg, item, cfg_next);
! 338: else
1.7 misho 339: SLIST_INSERT_AFTER(merge, item, cfg_next);
1.10 ! misho 340: RB_INSERT(tagRC, cfg, item);
1.1 misho 341: }
1.10 ! misho 342:
1.7 misho 343: CFG_RC_UNLOCK(cfg);
344: CFG_RC_UNLOCK(add_cfg);
1.1 misho 345:
1.3 misho 346: add_cfg->slh_first = NULL;
1.7 misho 347: add_cfg->rbh_root = NULL;
348: pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1 misho 349: return 0;
350: }
1.9 misho 351:
352: /*
353: * cfgReadLines() - Read custom lines and add new item at config root
354: *
355: * @f = File resource
356: * @delim = Custom delimiter, if =NULL default is '='
357: * @end = Custom user end of file, if =NULL default is EOF
358: * @cfg = Config root
359: * return: -1 error or 0 ok
360: */
361: int
362: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
363: {
364: char line[BUFSIZ];
365: struct tagCfg *d, *av = NULL;
366: char *psAttr, *psVal = NULL;
367:
368: while (!feof(f)) {
369: memset(line, 0, sizeof line);
370: fgets(line, sizeof line - 1, f);
371: /* check for user end-of-file */
372: if (strspn(line, end))
373: break;
374:
375: if (!(psAttr = strpbrk(line, "\r\n"))) {
376: /* skip line, too long */
377: continue;
378: } else {
379: *psAttr = 0;
380: io_TrimStr(line);
381: if (!*line)
382: continue;
383: }
384:
385: if (!io_MakeAV2(line, delim, &psAttr, &psVal))
386: continue;
387: else {
388: io_LTrimStr(psVal);
389: io_RTrimStr(psAttr);
390: }
391:
392: /* *NEW PAIR* alloc new pair element */
393: av = io_malloc(sizeof(struct tagCfg));
394: if (!av) {
395: LOGERR;
396: return -1;
397: } else
398: memset(av, 0, sizeof(struct tagCfg));
399:
400: if (psVal)
401: AIT_SET_STR(&av->cfg_val, psVal);
402: AIT_SET_STR(&av->cfg_attr, psAttr);
403: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
404: io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
405:
406: CFG_RC_LOCK(cfg);
407: /* find & delete duplicates */
408: if ((d = RB_FIND(tagRC, cfg, av))) {
409: RB_REMOVE(tagRC, cfg, d);
410: SLIST_REMOVE(cfg, d, tagCfg, cfg_next);
411:
412: AIT_FREE_VAL(&d->cfg_val);
413: AIT_FREE_VAL(&d->cfg_attr);
414: AIT_FREE_VAL(&d->cfg_sec);
415: io_free(d);
416: }
417:
418: SLIST_INSERT_HEAD(cfg, av, cfg_next);
419: RB_INSERT(tagRC, cfg, av);
420: CFG_RC_UNLOCK(cfg);
421: }
422:
423: return 0;
424: }
425:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>