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