Annotation of libaitcfg/src/parse.c, revision 1.10.2.3
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.2.3! misho 6: * $Id: parse.c,v 1.10.2.2 2012/08/30 13:49:44 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.2.1 misho 234: if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
235: strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
1.7 misho 236: if (whitespace)
237: strlcat(line, " = ", sizeof line);
238: else
239: strlcat(line, "=", sizeof line);
240: }
1.10.2.1 misho 241: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string)
242: strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
1.1 misho 243:
1.7 misho 244: /* write */
245: if (!cfg_Write(f, "%s\n", line)) {
246: LOGERR;
247: CFG_RC_UNLOCK(cfg);
248: return -1;
1.1 misho 249: }
250: }
1.7 misho 251: _invertQueue(cfg);
252: CFG_RC_UNLOCK(cfg);
253:
254: if (whitespace) {
255: time(&tim);
256: memset(line, 0, sizeof line);
257: strftime(line, sizeof line, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
258: cfg_Write(f, "\n## Config was saved at :: %s ##\n", line);
259: }
1.1 misho 260:
261: return 0;
262: }
263:
264: /*
1.7 misho 265: * cfgConcatConfig() - Concat two configs into one
266: *
267: * @cfg = Config root
268: * @add_cfg = Concated config will be destroy after merge
269: * return: -1 error or 0 ok
270: */
271: int
272: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.1 misho 273: {
1.7 misho 274: struct tagCfg *item;
1.3 misho 275:
1.7 misho 276: if (!cfg || !add_cfg)
277: return -1;
1.3 misho 278:
1.7 misho 279: CFG_RC_LOCK(add_cfg);
280: CFG_RC_LOCK(cfg);
1.3 misho 281:
1.7 misho 282: /* concat lists */
283: for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
284: SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
285:
286: /* concat red-black trees */
287: SLIST_FOREACH(item, add_cfg, cfg_next)
288: RB_INSERT(tagRC, cfg, item);
1.3 misho 289:
1.7 misho 290: CFG_RC_UNLOCK(cfg);
291: CFG_RC_UNLOCK(add_cfg);
1.3 misho 292:
293: add_cfg->slh_first = NULL;
1.7 misho 294: add_cfg->rbh_root = NULL;
295: pthread_mutex_destroy(&add_cfg->rc_mtx);
296: return 0;
1.3 misho 297: }
1.1 misho 298:
1.3 misho 299: /*
1.7 misho 300: * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
301: *
302: * @cfg = Config root of main list
303: * @add_cfg = Merged config will be destroy after merge
304: * return: -1 error or 0 ok
305: */
306: int
307: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3 misho 308: {
1.10 misho 309: struct tagCfg *item, *merge, *add_next, *next;
1.3 misho 310: int flg;
1.1 misho 311:
1.3 misho 312: if (!cfg || !add_cfg)
1.1 misho 313: return -1;
314:
1.7 misho 315: CFG_RC_LOCK(add_cfg);
316: CFG_RC_LOCK(cfg);
1.10 misho 317:
318: /* merge lists */
1.7 misho 319: SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
320: flg = 0;
321: SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
322: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
1.3 misho 323: flg = 1;
324: break;
325: }
1.7 misho 326: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
1.10 misho 327: AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
1.7 misho 328: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
1.3 misho 329: flg = 1;
330: break;
1.1 misho 331: }
332: }
1.3 misho 333:
1.10 misho 334: if (!flg)
335: SLIST_INSERT_HEAD(cfg, item, cfg_next);
336: else
1.7 misho 337: SLIST_INSERT_AFTER(merge, item, cfg_next);
1.10 misho 338: RB_INSERT(tagRC, cfg, item);
1.1 misho 339: }
1.10 misho 340:
1.7 misho 341: CFG_RC_UNLOCK(cfg);
342: CFG_RC_UNLOCK(add_cfg);
1.1 misho 343:
1.3 misho 344: add_cfg->slh_first = NULL;
1.7 misho 345: add_cfg->rbh_root = NULL;
346: pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1 misho 347: return 0;
348: }
1.9 misho 349:
350: /*
351: * cfgReadLines() - Read custom lines and add new item at config root
352: *
353: * @f = File resource
354: * @delim = Custom delimiter, if =NULL default is '='
355: * @end = Custom user end of file, if =NULL default is EOF
356: * @cfg = Config root
357: * return: -1 error or 0 ok
358: */
359: int
360: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
361: {
362: char line[BUFSIZ];
363: struct tagCfg *d, *av = NULL;
1.10.2.3! misho 364: char *p, *psSec, *psAttr, *psVal;
1.9 misho 365:
1.10.2.2 misho 366: if (!cfg)
367: return -1;
368: if (!delim)
369: delim = ATR_LINES_DELIM;
370:
1.9 misho 371: while (!feof(f)) {
1.10.2.3! misho 372: psSec = psAttr = psVal = NULL;
1.9 misho 373: memset(line, 0, sizeof line);
374: fgets(line, sizeof line - 1, f);
375: /* check for user end-of-file */
376: if (strspn(line, end))
377: break;
378:
379: if (!(psAttr = strpbrk(line, "\r\n"))) {
380: /* skip line, too long */
381: continue;
382: } else {
383: *psAttr = 0;
384: io_TrimStr(line);
385: if (!*line)
386: continue;
387: }
388:
1.10.2.3! misho 389: if (!io_MakeAV2(line, delim, &p, &psVal))
1.9 misho 390: continue;
391: else {
1.10.2.3! misho 392: io_RTrimStr(p);
1.9 misho 393: io_LTrimStr(psVal);
394: }
1.10.2.3! misho 395: if (!io_MakeAV2(p, SEC_LINES_DELIM, &psSec, &psAttr))
! 396: psAttr = p;
1.9 misho 397:
398: /* *NEW PAIR* alloc new pair element */
399: av = io_malloc(sizeof(struct tagCfg));
400: if (!av) {
401: LOGERR;
402: return -1;
403: } else
404: memset(av, 0, sizeof(struct tagCfg));
405:
1.10.2.3! misho 406: if (psSec) {
! 407: AIT_SET_STR(&av->cfg_sec, psSec);
! 408: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
! 409: io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
! 410: }
1.9 misho 411: if (psVal)
412: AIT_SET_STR(&av->cfg_val, psVal);
413: AIT_SET_STR(&av->cfg_attr, psAttr);
414: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
415: io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
416:
417: CFG_RC_LOCK(cfg);
418: /* find & delete duplicates */
419: if ((d = RB_FIND(tagRC, cfg, av))) {
420: RB_REMOVE(tagRC, cfg, d);
421: SLIST_REMOVE(cfg, d, tagCfg, cfg_next);
422:
423: AIT_FREE_VAL(&d->cfg_val);
424: AIT_FREE_VAL(&d->cfg_attr);
425: AIT_FREE_VAL(&d->cfg_sec);
426: io_free(d);
427: }
428:
429: SLIST_INSERT_HEAD(cfg, av, cfg_next);
430: RB_INSERT(tagRC, cfg, av);
431: CFG_RC_UNLOCK(cfg);
432: }
433:
434: return 0;
435: }
436:
1.10.2.2 misho 437: /*
438: * cfgWriteLines() - Write custom lines and export data to variable
439: *
440: * @f = File resource
441: * @delim = Custom delimiter, if =NULL default is '='
442: * @eol = End of line string, if =NULL default is "\n"
443: * @section = Export only section, if =NULL default is all
444: * @cfg = Config root
445: * return: =NULL error or !=NULL exported data, must be free after use with io_freeVar()
446: */
447: ait_val_t *
448: cfgWriteLines(FILE *f, const char *delim, const char *eol, const char *section, cfg_root_t * __restrict cfg)
449: {
450: ait_val_t *v = NULL;
451: struct tagCfg *av;
452:
453: if (!cfg)
454: return NULL;
455: if (!delim)
456: delim = ATR_LINES_DELIM;
457: if (!eol)
458: eol = EOL_LINES_DELIM;
459: if (!(v = io_allocVar())) {
460: cfg_SetErr(io_GetErrno(), "%s", io_GetError());
461: return NULL;
462: } else
463: AIT_INIT_VAL2(v, string);
464:
465: SLIST_FOREACH(av, cfg, cfg_next) {
466: if (AIT_ISEMPTY(&av->cfg_attr))
467: continue;
468: if (section) {
469: if (!AIT_ISEMPTY(&av->cfg_sec) && *section)
470: continue;
471: if (strcmp(section, AIT_GET_STR(&av->cfg_sec)))
472: continue;
473: }
474:
475: if (!AIT_ISEMPTY(&av->cfg_sec)) {
476: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_sec));
477: AIT_SET_STRCAT(v, SEC_LINES_DELIM);
478: }
479: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_attr));
480: AIT_SET_STRCAT(v, delim);
481: if (!AIT_ISEMPTY(&av->cfg_val))
482: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val));
483: AIT_SET_STRCAT(v, eol);
484: }
485:
486: if (f)
487: fputs(AIT_GET_STR(v), f);
488: return v;
489: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>