Annotation of libaitcfg/src/parse.c, revision 1.12.4.1
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.12.4.1! misho 6: * $Id: parse.c,v 1.12 2013/05/30 09:12:27 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.12.4.1! misho 15: Copyright 2004 - 2014
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:
48:
1.7 misho 49: static inline void
50: _invertQueue(cfg_root_t * __restrict cfg)
1.3 misho 51: {
1.7 misho 52: struct tagCfg *item, *next, *prev = NULL;
1.3 misho 53:
1.7 misho 54: SLIST_FOREACH_SAFE(item, cfg, cfg_next, next) {
55: item->cfg_next.sle_next = prev;
1.3 misho 56: prev = item;
57: }
58: cfg->slh_first = prev;
59: }
60:
1.7 misho 61:
62: /*
63: * cfgReadConfig() - Read file and add new item at config root
64: *
65: * @f = File resource
66: * @cfg = Config root
67: * return: -1 error or 0 ok
68: */
1.9 misho 69: int
70: cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
1.3 misho 71: {
1.7 misho 72: char line[BUFSIZ];
73: struct tagCfg *av = NULL;
74: int flg = 0;
75: char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
1.3 misho 76:
1.11 misho 77: if (!f || !cfg) {
78: cfg_SetErr(EINVAL, "Invalid parameter(s)");
79: return -1;
80: }
81:
1.7 misho 82: while (!feof(f)) {
83: memset(line, 0, sizeof line);
84: fgets(line, sizeof line - 1, f);
85: #ifdef SUPPORT_USER_EOF
86: /* check for user end-of-file */
87: if (line[0] == '.' && line[1] == '\n')
88: break;
89: #endif
90: if (!(psAttr = strpbrk(line, "\r\n"))) {
91: /* skip line, too long */
92: continue;
93: } else {
94: *psAttr = 0;
1.12 misho 95: str_Trim(line);
1.7 misho 96: }
1.3 misho 97:
1.7 misho 98: if (flg) {
99: /* continues line */
100: if (!av)
101: continue;
102: else
103: psAttr = line + strlen(line) - 1;
104: if (*psAttr == '\\')
105: *psAttr = 0;
106: else
107: flg = 0;
108: /* concat line to value */
109: AIT_SET_STRCAT(&av->cfg_val, line);
1.10 misho 110: if (!flg && AIT_ADDR(&av->cfg_val))
1.12 misho 111: str_Unquot((char*) AIT_GET_STR(&av->cfg_val));
1.7 misho 112: continue;
113: }
1.3 misho 114:
1.7 misho 115: /* *NEW PAIR* alloc new pair element */
1.12 misho 116: av = e_malloc(sizeof(struct tagCfg));
1.7 misho 117: if (!av) {
1.12 misho 118: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.7 misho 119: return -1;
120: } else {
121: memset(av, 0, sizeof(struct tagCfg));
122: CFG_RC_LOCK(cfg);
123: SLIST_INSERT_HEAD(cfg, av, cfg_next);
124: CFG_RC_UNLOCK(cfg);
1.3 misho 125: }
1.7 misho 126:
127: /* check for continues line */
1.8 misho 128: psAttr = line + (*line ? strlen(line) : 1) - 1;
1.7 misho 129: if (*psAttr == '\\') {
130: *psAttr = 0;
131: flg = 1;
1.3 misho 132: }
133:
1.7 misho 134: /* check for comment or empty line */
135: if (!*line || *line == '#' || *line == ';') {
136: AIT_SET_STR(&av->cfg_val, line);
137: continue;
138: }
139: /* section */
140: if (*line == '[') {
141: psAttr = line + strlen(line) - 1;
142: if (*psAttr == ']') {
143: *psAttr = 0;
144: flg = 0;
145: strlcpy(szSection, line + 1, sizeof szSection);
1.12.4.1! misho 146: AIT_SET_STR(&av->cfg_sec, line);
1.7 misho 147: } else
1.12 misho 148: EDEBUG(7, "Ignore section '%s' ... not found ']'", line);
1.7 misho 149: continue;
150: }
151: /* parse pair */
152: if (!(psAttr = strchr(line, '='))) {
153: AIT_SET_STR(&av->cfg_val, line);
1.12 misho 154: EDEBUG(7, "Ignore a/v '%s' ... not found '='", line);
1.7 misho 155: continue;
1.3 misho 156: } else {
1.7 misho 157: *psAttr = 0;
158: psVal = psAttr + 1;
159: psAttr = line;
160: }
161:
162: /* if exists, added section name to element */
163: if (*szSection) {
164: AIT_SET_STR(&av->cfg_sec, szSection);
165: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
1.12 misho 166: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.3 misho 167: }
168:
1.12 misho 169: str_RTrim(psAttr);
170: str_LTrim(psVal);
1.7 misho 171: if (!flg)
1.12 misho 172: str_Unquot(psVal);
1.7 misho 173: AIT_SET_STR(&av->cfg_val, psVal);
174: AIT_SET_STR(&av->cfg_attr, psAttr);
175: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
1.12 misho 176: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.7 misho 177:
178: CFG_RC_LOCK(cfg);
179: RB_INSERT(tagRC, cfg, av);
180: CFG_RC_UNLOCK(cfg);
1.3 misho 181: }
182:
183: return 0;
184: }
185:
1.1 misho 186: /*
1.7 misho 187: * cfgWriteConfig() - Write config from memory
188: *
189: * @f = File handle
190: * @cfg = Config root
191: * @whitespace = Additional whitespace characters to file
192: * return: -1 error or 0 ok
193: */
194: int
195: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
1.1 misho 196: {
1.7 misho 197: struct tagCfg *av;
198: time_t tim;
1.12.4.1! misho 199: char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
1.1 misho 200:
1.11 misho 201: if (!f || !cfg) {
202: cfg_SetErr(EINVAL, "Invalid parameter(s)");
203: return -1;
204: }
205:
1.7 misho 206: CFG_RC_LOCK(cfg);
207: _invertQueue(cfg);
208: SLIST_FOREACH(av, cfg, cfg_next) {
1.12.4.1! misho 209: /* empty lines or comment */
! 210: if (AIT_ISEMPTY(&av->cfg_attr)) {
! 211: if (AIT_ISEMPTY(&av->cfg_val))
! 212: continue;
! 213: strlcpy(line, AIT_GET_STR(&av->cfg_val), sizeof line);
! 214: goto skip_sec;
! 215: }
! 216:
! 217: /* section [] */
1.10 misho 218: if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) &&
1.7 misho 219: strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
220: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
1.12.4.1! misho 221: if (!cfg_Write(f, "[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
1.7 misho 222: LOGERR;
1.12.4.1! misho 223: _invertQueue(cfg);
1.7 misho 224: CFG_RC_UNLOCK(cfg);
225: return -1;
1.1 misho 226: }
1.12.4.1! misho 227: } else if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
1.7 misho 228: memset(szSection, 0, sizeof szSection);
1.12.4.1! misho 229: if (!cfg_Write(f, "[]\n")) {
1.1 misho 230: LOGERR;
1.12.4.1! misho 231: _invertQueue(cfg);
1.7 misho 232: CFG_RC_UNLOCK(cfg);
1.1 misho 233: return -1;
234: }
1.7 misho 235: }
236:
237: /* build line */
238: memset(line, 0, sizeof line);
1.11 misho 239: if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
240: strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
1.7 misho 241: if (whitespace)
242: strlcat(line, " = ", sizeof line);
243: else
244: strlcat(line, "=", sizeof line);
245: }
1.11 misho 246: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string)
247: strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
1.12.4.1! misho 248: skip_sec:
1.7 misho 249: /* write */
250: if (!cfg_Write(f, "%s\n", line)) {
251: LOGERR;
1.11 misho 252: _invertQueue(cfg);
1.7 misho 253: CFG_RC_UNLOCK(cfg);
254: return -1;
1.1 misho 255: }
256: }
1.7 misho 257: _invertQueue(cfg);
258: CFG_RC_UNLOCK(cfg);
259:
260: if (whitespace) {
261: time(&tim);
262: memset(line, 0, sizeof line);
263: strftime(line, sizeof line, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
264: cfg_Write(f, "\n## Config was saved at :: %s ##\n", line);
265: }
1.1 misho 266:
267: return 0;
268: }
269:
270: /*
1.7 misho 271: * cfgConcatConfig() - Concat two configs into one
272: *
273: * @cfg = Config root
274: * @add_cfg = Concated config will be destroy after merge
275: * return: -1 error or 0 ok
276: */
277: int
278: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.1 misho 279: {
1.7 misho 280: struct tagCfg *item;
1.3 misho 281:
1.7 misho 282: if (!cfg || !add_cfg)
283: return -1;
1.3 misho 284:
1.7 misho 285: CFG_RC_LOCK(add_cfg);
286: CFG_RC_LOCK(cfg);
1.3 misho 287:
1.7 misho 288: /* concat lists */
289: for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
290: SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
291:
292: /* concat red-black trees */
293: SLIST_FOREACH(item, add_cfg, cfg_next)
294: RB_INSERT(tagRC, cfg, item);
1.3 misho 295:
1.7 misho 296: CFG_RC_UNLOCK(cfg);
1.3 misho 297:
298: add_cfg->slh_first = NULL;
1.7 misho 299: add_cfg->rbh_root = NULL;
1.11 misho 300: CFG_RC_UNLOCK(add_cfg);
1.7 misho 301: pthread_mutex_destroy(&add_cfg->rc_mtx);
302: return 0;
1.3 misho 303: }
1.1 misho 304:
1.3 misho 305: /*
1.7 misho 306: * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
307: *
308: * @cfg = Config root of main list
309: * @add_cfg = Merged config will be destroy after merge
310: * return: -1 error or 0 ok
311: */
312: int
313: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3 misho 314: {
1.10 misho 315: struct tagCfg *item, *merge, *add_next, *next;
1.3 misho 316: int flg;
1.1 misho 317:
1.3 misho 318: if (!cfg || !add_cfg)
1.1 misho 319: return -1;
320:
1.7 misho 321: CFG_RC_LOCK(add_cfg);
322: CFG_RC_LOCK(cfg);
1.10 misho 323:
324: /* merge lists */
1.7 misho 325: SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
326: flg = 0;
327: SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
328: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
1.3 misho 329: flg = 1;
330: break;
331: }
1.7 misho 332: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
1.10 misho 333: AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
1.7 misho 334: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
1.3 misho 335: flg = 1;
336: break;
1.1 misho 337: }
338: }
1.3 misho 339:
1.10 misho 340: if (!flg)
341: SLIST_INSERT_HEAD(cfg, item, cfg_next);
342: else
1.7 misho 343: SLIST_INSERT_AFTER(merge, item, cfg_next);
1.10 misho 344: RB_INSERT(tagRC, cfg, item);
1.1 misho 345: }
1.10 misho 346:
1.7 misho 347: CFG_RC_UNLOCK(cfg);
1.1 misho 348:
1.3 misho 349: add_cfg->slh_first = NULL;
1.7 misho 350: add_cfg->rbh_root = NULL;
1.11 misho 351: CFG_RC_UNLOCK(add_cfg);
1.7 misho 352: pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1 misho 353: return 0;
354: }
1.9 misho 355:
356: /*
357: * cfgReadLines() - Read custom lines and add new item at config root
358: *
359: * @f = File resource
360: * @delim = Custom delimiter, if =NULL default is '='
361: * @end = Custom user end of file, if =NULL default is EOF
362: * @cfg = Config root
363: * return: -1 error or 0 ok
364: */
365: int
366: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
367: {
368: char line[BUFSIZ];
369: struct tagCfg *d, *av = NULL;
1.11 misho 370: char *p, *psSec, *psAttr, *psVal;
371:
372: if (!cfg)
373: return -1;
374: if (!delim)
375: delim = ATR_LINES_DELIM;
1.9 misho 376:
377: while (!feof(f)) {
1.11 misho 378: psSec = psAttr = psVal = NULL;
1.9 misho 379: memset(line, 0, sizeof line);
380: fgets(line, sizeof line - 1, f);
381: /* check for user end-of-file */
382: if (strspn(line, end))
383: break;
384:
385: if (!(psAttr = strpbrk(line, "\r\n"))) {
386: /* skip line, too long */
387: continue;
388: } else {
389: *psAttr = 0;
1.12 misho 390: str_Trim(line);
1.9 misho 391: if (!*line)
392: continue;
393: }
394:
1.12 misho 395: if (!av_MakeExt(line, delim, &p, &psVal))
1.9 misho 396: continue;
397: else {
1.12 misho 398: str_RTrim(p);
399: str_LTrim(psVal);
1.9 misho 400: }
1.12 misho 401: if (!av_MakeExt(p, SEC_LINES_DELIM, &psSec, &psAttr))
1.11 misho 402: psAttr = p;
1.9 misho 403:
404: /* *NEW PAIR* alloc new pair element */
1.12 misho 405: av = e_malloc(sizeof(struct tagCfg));
1.9 misho 406: if (!av) {
407: LOGERR;
408: return -1;
409: } else
410: memset(av, 0, sizeof(struct tagCfg));
411:
1.11 misho 412: if (psSec) {
413: AIT_SET_STR(&av->cfg_sec, psSec);
414: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
1.12 misho 415: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.11 misho 416: }
1.9 misho 417: if (psVal)
418: AIT_SET_STR(&av->cfg_val, psVal);
419: AIT_SET_STR(&av->cfg_attr, psAttr);
420: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
1.12 misho 421: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.9 misho 422:
423: CFG_RC_LOCK(cfg);
424: /* find & delete duplicates */
425: if ((d = RB_FIND(tagRC, cfg, av))) {
426: RB_REMOVE(tagRC, cfg, d);
427: SLIST_REMOVE(cfg, d, tagCfg, cfg_next);
428:
429: AIT_FREE_VAL(&d->cfg_val);
430: AIT_FREE_VAL(&d->cfg_attr);
431: AIT_FREE_VAL(&d->cfg_sec);
1.12 misho 432: e_free(d);
1.9 misho 433: }
434:
435: SLIST_INSERT_HEAD(cfg, av, cfg_next);
436: RB_INSERT(tagRC, cfg, av);
437: CFG_RC_UNLOCK(cfg);
438: }
439:
440: return 0;
441: }
442:
1.11 misho 443: /*
444: * cfgWriteLines() - Write custom lines and export data to variable
445: *
446: * @f = File resource
447: * @delim = Custom delimiter, if =NULL default is '='
448: * @eol = End of line string, if =NULL default is "\n"
449: * @section = Export only section, if =NULL default is all
450: * @cfg = Config root
1.12 misho 451: * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
1.11 misho 452: */
453: ait_val_t *
454: cfgWriteLines(FILE *f, const char *delim, const char *eol, const char *section, cfg_root_t * __restrict cfg)
455: {
456: ait_val_t *v = NULL;
457: struct tagCfg *av;
458:
459: if (!cfg)
460: return NULL;
461: if (!delim)
462: delim = ATR_LINES_DELIM;
463: if (!eol)
464: eol = EOL_LINES_DELIM;
1.12 misho 465: if (!(v = ait_allocVar())) {
466: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.11 misho 467: return NULL;
468: } else
469: AIT_INIT_VAL2(v, string);
470:
471: SLIST_FOREACH(av, cfg, cfg_next) {
472: if (AIT_ISEMPTY(&av->cfg_attr))
473: continue;
474: if (section) {
475: if (!AIT_ISEMPTY(&av->cfg_sec) && *section)
476: continue;
477: if (strcmp(section, AIT_GET_STR(&av->cfg_sec)))
478: continue;
479: }
480:
481: if (!AIT_ISEMPTY(&av->cfg_sec)) {
482: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_sec));
483: AIT_SET_STRCAT(v, SEC_LINES_DELIM);
484: }
485: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_attr));
486: AIT_SET_STRCAT(v, delim);
487: if (!AIT_ISEMPTY(&av->cfg_val))
488: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val));
489: AIT_SET_STRCAT(v, eol);
490: }
491:
492: if (f)
493: fputs(AIT_GET_STR(v), f);
494: return v;
495: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>