Annotation of libaitcfg/src/parse.c, revision 1.21
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.21 ! misho 6: * $Id: parse.c,v 1.20.2.2 2023/01/23 23:24:22 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.21 ! misho 15: Copyright 2004 - 2023
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: /*
50: * cfgReadConfig() - Read file and add new item at config root
51: *
52: * @f = File resource
53: * @cfg = Config root
54: * return: -1 error or 0 ok
55: */
1.9 misho 56: int
57: cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
1.3 misho 58: {
1.21 ! misho 59: char line[BUFSIZ], origin[BUFSIZ], chkattr[STRSIZ];
1.7 misho 60: struct tagCfg *av = NULL;
61: int flg = 0;
62: char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
1.18 misho 63: FILE *ff;
1.3 misho 64:
1.11 misho 65: if (!f || !cfg) {
66: cfg_SetErr(EINVAL, "Invalid parameter(s)");
67: return -1;
68: }
69:
1.7 misho 70: while (!feof(f)) {
71: memset(line, 0, sizeof line);
1.20 misho 72: if (!fgets(line, sizeof(line) - 1, f))
73: break;
1.7 misho 74: #ifdef SUPPORT_USER_EOF
75: /* check for user end-of-file */
76: if (line[0] == '.' && line[1] == '\n')
77: break;
78: #endif
79: if (!(psAttr = strpbrk(line, "\r\n"))) {
80: /* skip line, too long */
81: continue;
82: } else {
83: *psAttr = 0;
1.17 misho 84: strlcpy(origin, line, sizeof origin);
1.12 misho 85: str_Trim(line);
1.7 misho 86: }
1.3 misho 87:
1.7 misho 88: if (flg) {
89: /* continues line */
90: if (!av)
91: continue;
92: else
93: psAttr = line + strlen(line) - 1;
94: if (*psAttr == '\\')
95: *psAttr = 0;
96: else
97: flg = 0;
98: /* concat line to value */
99: AIT_SET_STRCAT(&av->cfg_val, line);
1.10 misho 100: if (!flg && AIT_ADDR(&av->cfg_val))
1.21 ! misho 101: av->cfg_quoted += str_Unquot((char*) AIT_GET_STR(&av->cfg_val));
1.18 misho 102:
103: /* read include file */
104: if (!flg && AIT_ADDR(&av->cfg_val) &&
105: *AIT_GET_STR(&av->cfg_attr) == '%' &&
106: !strcmp(AIT_GET_STR(&av->cfg_attr), "%include")) {
107: ff = fopen(AIT_GET_STR(&av->cfg_val), "r");
108: if (ff) {
109: cfgReadConfig(ff, cfg);
110: fclose(ff);
111: } else
1.21 ! misho 112: EDEBUG(ELWIX_DEBUG_LOG, "Error:: Can't open %s file",
1.18 misho 113: AIT_GET_STR(&av->cfg_val));
114: }
1.7 misho 115: continue;
116: }
1.3 misho 117:
1.21 ! misho 118: /* check for duplicated element */
! 119: if (*line != '#' && *line != ';' && *line != '/' && *line != '%' &&
! 120: (psAttr = strchr(line, '='))) {
! 121: strncpy(chkattr, line, psAttr - line);
! 122: chkattr[psAttr - line] = 0;
! 123: str_RTrim(chkattr);
! 124: if (cfg_findAttribute(cfg, szSection, chkattr))
! 125: cfg_unsetAttribute(cfg, szSection, chkattr);
! 126: }
! 127:
1.7 misho 128: /* *NEW PAIR* alloc new pair element */
1.12 misho 129: av = e_malloc(sizeof(struct tagCfg));
1.7 misho 130: if (!av) {
1.12 misho 131: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.7 misho 132: return -1;
133: } else {
134: memset(av, 0, sizeof(struct tagCfg));
135: CFG_RC_LOCK(cfg);
1.14 misho 136: TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.7 misho 137: CFG_RC_UNLOCK(cfg);
1.3 misho 138: }
1.7 misho 139:
1.18 misho 140: /* check for comment or empty line */
141: if (!*line || *line == '#' || *line == ';') {
142: AIT_SET_STR(&av->cfg_val, line);
143: continue;
144: }
145:
1.7 misho 146: /* check for continues line */
1.8 misho 147: psAttr = line + (*line ? strlen(line) : 1) - 1;
1.7 misho 148: if (*psAttr == '\\') {
149: *psAttr = 0;
150: flg = 1;
1.3 misho 151: }
152:
1.7 misho 153: /* section */
154: if (*line == '[') {
155: psAttr = line + strlen(line) - 1;
156: if (*psAttr == ']') {
157: *psAttr = 0;
158: flg = 0;
159: strlcpy(szSection, line + 1, sizeof szSection);
1.13 misho 160: AIT_SET_STR(&av->cfg_sec, line);
1.7 misho 161: } else
1.21 ! misho 162: EDEBUG(ELWIX_DEBUG_LOG, "Ignore section '%s' ... not found ']'", line);
1.7 misho 163: continue;
164: }
165: /* parse pair */
166: if (!(psAttr = strchr(line, '='))) {
1.17 misho 167: AIT_SET_STR(&av->cfg_val, origin);
1.21 ! misho 168: EDEBUG(ELWIX_DEBUG_LOG, "Ignore a/v '%s' ... not found '='", line);
1.7 misho 169: continue;
1.3 misho 170: } else {
1.7 misho 171: *psAttr = 0;
172: psVal = psAttr + 1;
173: psAttr = line;
174: }
175:
176: /* if exists, added section name to element */
177: if (*szSection) {
178: AIT_SET_STR(&av->cfg_sec, szSection);
179: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
1.12 misho 180: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.3 misho 181: }
182:
1.12 misho 183: str_RTrim(psAttr);
184: str_LTrim(psVal);
1.7 misho 185: if (!flg)
1.21 ! misho 186: av->cfg_quoted += str_Unquot(psVal);
1.7 misho 187: AIT_SET_STR(&av->cfg_val, psVal);
188: AIT_SET_STR(&av->cfg_attr, psAttr);
189: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
1.12 misho 190: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.7 misho 191:
192: CFG_RC_LOCK(cfg);
193: RB_INSERT(tagRC, cfg, av);
194: CFG_RC_UNLOCK(cfg);
1.18 misho 195:
196: /* read include file */
197: if (!flg && *AIT_GET_STR(&av->cfg_attr) == '%' &&
198: !strcmp(AIT_GET_STR(&av->cfg_attr), "%include")) {
199: ff = fopen(AIT_GET_STR(&av->cfg_val), "r");
200: if (ff) {
201: cfgReadConfig(ff, cfg);
202: fclose(ff);
203: } else
1.21 ! misho 204: EDEBUG(ELWIX_DEBUG_LOG, "Error:: Can't open %s file",
1.18 misho 205: AIT_GET_STR(&av->cfg_val));
206: }
1.3 misho 207: }
208:
209: return 0;
210: }
211:
1.1 misho 212: /*
1.7 misho 213: * cfgWriteConfig() - Write config from memory
214: *
215: * @f = File handle
216: * @cfg = Config root
217: * @whitespace = Additional whitespace characters to file
218: * return: -1 error or 0 ok
219: */
220: int
221: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
1.1 misho 222: {
1.7 misho 223: struct tagCfg *av;
1.13 misho 224: char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
1.1 misho 225:
1.11 misho 226: if (!f || !cfg) {
227: cfg_SetErr(EINVAL, "Invalid parameter(s)");
228: return -1;
229: }
230:
1.7 misho 231: CFG_RC_LOCK(cfg);
1.14 misho 232: RB_FOREACH(av, tagRC, cfg) {
1.13 misho 233: /* empty lines or comment */
234: if (AIT_ISEMPTY(&av->cfg_attr)) {
235: if (AIT_ISEMPTY(&av->cfg_val))
236: continue;
237: strlcpy(line, AIT_GET_STR(&av->cfg_val), sizeof line);
238: goto skip_sec;
239: }
240:
241: /* section [] */
1.10 misho 242: if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) &&
1.14 misho 243: strcmp(AIT_GET_STRZ(&av->cfg_sec), szSection)) {
1.7 misho 244: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
1.21 ! misho 245: if (!cfg_Write(f, "[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
1.7 misho 246: LOGERR;
247: CFG_RC_UNLOCK(cfg);
248: return -1;
1.1 misho 249: }
1.13 misho 250: } else if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
1.7 misho 251: memset(szSection, 0, sizeof szSection);
1.21 ! misho 252: if (!cfg_Write(f, "[]\n")) {
1.1 misho 253: LOGERR;
1.7 misho 254: CFG_RC_UNLOCK(cfg);
1.1 misho 255: return -1;
256: }
1.7 misho 257: }
258:
259: /* build line */
260: memset(line, 0, sizeof line);
1.11 misho 261: if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
262: strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
1.7 misho 263: if (whitespace)
264: strlcat(line, " = ", sizeof line);
265: else
266: strlcat(line, "=", sizeof line);
267: }
1.21 ! misho 268: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string) {
! 269: if (av->cfg_quoted)
! 270: strlcat(line, "\"", sizeof line);
1.11 misho 271: strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
1.21 ! misho 272: if (av->cfg_quoted)
! 273: strlcat(line, "\"", sizeof line);
! 274: }
1.13 misho 275: skip_sec:
1.7 misho 276: /* write */
277: if (!cfg_Write(f, "%s\n", line)) {
278: LOGERR;
279: CFG_RC_UNLOCK(cfg);
280: return -1;
1.1 misho 281: }
282: }
1.7 misho 283: CFG_RC_UNLOCK(cfg);
284:
1.1 misho 285: return 0;
286: }
287:
288: /*
1.19 misho 289: * cfgWriteConfigRaw() - Write config from memory by list
290: *
291: * @f = File handle
292: * @cfg = Config root
293: * @whitespace = Additional whitespace characters to file
294: * return: -1 error or 0 ok
295: */
296: int
297: cfgWriteConfigRaw(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
298: {
299: struct tagCfg *av, *nxt;
300: char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
301:
302: if (!f || !cfg) {
303: cfg_SetErr(EINVAL, "Invalid parameter(s)");
304: return -1;
305: }
306:
307: CFG_RC_LOCK(cfg);
308: TAILQ_FOREACH_SAFE(av, cfg, cfg_next, nxt) {
309: /* empty lines or comment */
310: if (AIT_ISEMPTY(&av->cfg_attr)) {
311: if (AIT_ISEMPTY(&av->cfg_val))
312: continue;
313: strlcpy(line, AIT_GET_STR(&av->cfg_val), sizeof line);
314: goto skip_sec;
315: }
316:
317: /* section [] */
318: if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) &&
319: strcmp(AIT_GET_STRZ(&av->cfg_sec), szSection)) {
320: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
1.21 ! misho 321: if (!cfg_Write(f, "[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
1.19 misho 322: LOGERR;
323: CFG_RC_UNLOCK(cfg);
324: return -1;
325: }
326: } else if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
327: memset(szSection, 0, sizeof szSection);
1.21 ! misho 328: if (!cfg_Write(f, "[]\n")) {
1.19 misho 329: LOGERR;
330: CFG_RC_UNLOCK(cfg);
331: return -1;
332: }
333: }
334:
335: /* build line */
336: memset(line, 0, sizeof line);
337: if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
338: strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
339: if (whitespace)
340: strlcat(line, " = ", sizeof line);
341: else
342: strlcat(line, "=", sizeof line);
343: }
1.21 ! misho 344: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string) {
! 345: if (av->cfg_quoted)
! 346: strlcat(line, "\"", sizeof line);
1.19 misho 347: strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
1.21 ! misho 348: if (av->cfg_quoted)
! 349: strlcat(line, "\"", sizeof line);
! 350: }
1.19 misho 351: skip_sec:
352: /* write */
353: if (!cfg_Write(f, "%s\n", line)) {
354: LOGERR;
355: CFG_RC_UNLOCK(cfg);
356: return -1;
357: }
358: }
359: CFG_RC_UNLOCK(cfg);
360:
361: return 0;
362: }
363: /*
1.7 misho 364: * cfgConcatConfig() - Concat two configs into one
365: *
366: * @cfg = Config root
367: * @add_cfg = Concated config will be destroy after merge
368: * return: -1 error or 0 ok
369: */
370: int
371: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.1 misho 372: {
1.7 misho 373: struct tagCfg *item;
1.3 misho 374:
1.7 misho 375: if (!cfg || !add_cfg)
376: return -1;
1.3 misho 377:
1.7 misho 378: CFG_RC_LOCK(add_cfg);
379: CFG_RC_LOCK(cfg);
1.3 misho 380:
1.14 misho 381: /* concat lists & red-black trees */
382: TAILQ_FOREACH(item, add_cfg, cfg_next) {
383: TAILQ_INSERT_TAIL(cfg, item, cfg_next);
1.7 misho 384: RB_INSERT(tagRC, cfg, item);
1.14 misho 385: }
1.3 misho 386:
1.7 misho 387: CFG_RC_UNLOCK(cfg);
1.3 misho 388:
1.14 misho 389: TAILQ_INIT(add_cfg);
390: RB_INIT(add_cfg);
1.11 misho 391: CFG_RC_UNLOCK(add_cfg);
1.7 misho 392: pthread_mutex_destroy(&add_cfg->rc_mtx);
393: return 0;
1.3 misho 394: }
1.1 misho 395:
1.3 misho 396: /*
1.7 misho 397: * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
398: *
399: * @cfg = Config root of main list
400: * @add_cfg = Merged config will be destroy after merge
401: * return: -1 error or 0 ok
402: */
403: int
404: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3 misho 405: {
1.10 misho 406: struct tagCfg *item, *merge, *add_next, *next;
1.3 misho 407: int flg;
1.1 misho 408:
1.3 misho 409: if (!cfg || !add_cfg)
1.1 misho 410: return -1;
411:
1.7 misho 412: CFG_RC_LOCK(add_cfg);
413: CFG_RC_LOCK(cfg);
1.10 misho 414:
415: /* merge lists */
1.14 misho 416: TAILQ_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
1.7 misho 417: flg = 0;
1.14 misho 418: TAILQ_FOREACH_SAFE(merge, cfg, cfg_next, next) {
1.7 misho 419: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
1.3 misho 420: flg = 1;
421: break;
422: }
1.7 misho 423: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
1.10 misho 424: AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
1.7 misho 425: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
1.21 ! misho 426: flg = -1;
1.3 misho 427: break;
1.1 misho 428: }
429: }
1.3 misho 430:
1.21 ! misho 431: switch (flg) {
! 432: case -1:
! 433: continue; /* skip duplicated element */
! 434: case 1:
! 435: TAILQ_INSERT_AFTER(cfg, merge, item, cfg_next);
! 436: break;
! 437: case 0:
! 438: TAILQ_INSERT_TAIL(cfg, item, cfg_next);
! 439: break;
! 440: }
1.10 misho 441: RB_INSERT(tagRC, cfg, item);
1.1 misho 442: }
1.10 misho 443:
1.7 misho 444: CFG_RC_UNLOCK(cfg);
1.1 misho 445:
1.14 misho 446: TAILQ_INIT(add_cfg);
447: RB_INIT(add_cfg);
1.11 misho 448: CFG_RC_UNLOCK(add_cfg);
1.7 misho 449: pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1 misho 450: return 0;
451: }
1.9 misho 452:
453: /*
454: * cfgReadLines() - Read custom lines and add new item at config root
455: *
456: * @f = File resource
457: * @delim = Custom delimiter, if =NULL default is '='
458: * @end = Custom user end of file, if =NULL default is EOF
459: * @cfg = Config root
460: * return: -1 error or 0 ok
461: */
462: int
463: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
464: {
465: char line[BUFSIZ];
466: struct tagCfg *d, *av = NULL;
1.11 misho 467: char *p, *psSec, *psAttr, *psVal;
468:
469: if (!cfg)
470: return -1;
471: if (!delim)
472: delim = ATR_LINES_DELIM;
1.9 misho 473:
474: while (!feof(f)) {
1.11 misho 475: psSec = psAttr = psVal = NULL;
1.9 misho 476: memset(line, 0, sizeof line);
1.20 misho 477: if (!fgets(line, sizeof(line) - 1, f))
478: break;
1.9 misho 479: /* check for user end-of-file */
480: if (strspn(line, end))
481: break;
482:
483: if (!(psAttr = strpbrk(line, "\r\n"))) {
484: /* skip line, too long */
485: continue;
486: } else {
487: *psAttr = 0;
1.12 misho 488: str_Trim(line);
1.9 misho 489: if (!*line)
490: continue;
491: }
492:
1.12 misho 493: if (!av_MakeExt(line, delim, &p, &psVal))
1.9 misho 494: continue;
495: else {
1.12 misho 496: str_RTrim(p);
497: str_LTrim(psVal);
1.9 misho 498: }
1.12 misho 499: if (!av_MakeExt(p, SEC_LINES_DELIM, &psSec, &psAttr))
1.11 misho 500: psAttr = p;
1.9 misho 501:
1.21 ! misho 502: /* check for duplicated element */
! 503: if (psAttr && cfg_findAttribute(cfg, psSec, psAttr))
! 504: cfg_unsetAttribute(cfg, psSec, psAttr);
! 505:
1.9 misho 506: /* *NEW PAIR* alloc new pair element */
1.12 misho 507: av = e_malloc(sizeof(struct tagCfg));
1.9 misho 508: if (!av) {
509: LOGERR;
510: return -1;
511: } else
512: memset(av, 0, sizeof(struct tagCfg));
513:
1.11 misho 514: if (psSec) {
515: AIT_SET_STR(&av->cfg_sec, psSec);
516: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
1.12 misho 517: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
1.11 misho 518: }
1.21 ! misho 519: if (psVal) {
! 520: av->cfg_quoted = str_Unquot(psVal);
1.9 misho 521: AIT_SET_STR(&av->cfg_val, psVal);
1.21 ! misho 522: }
1.9 misho 523: AIT_SET_STR(&av->cfg_attr, psAttr);
524: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
1.12 misho 525: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
1.9 misho 526:
527: CFG_RC_LOCK(cfg);
528: /* find & delete duplicates */
529: if ((d = RB_FIND(tagRC, cfg, av))) {
530: RB_REMOVE(tagRC, cfg, d);
1.14 misho 531: TAILQ_REMOVE(cfg, d, cfg_next);
1.9 misho 532:
533: AIT_FREE_VAL(&d->cfg_val);
534: AIT_FREE_VAL(&d->cfg_attr);
535: AIT_FREE_VAL(&d->cfg_sec);
1.12 misho 536: e_free(d);
1.9 misho 537: }
538:
1.14 misho 539: TAILQ_INSERT_TAIL(cfg, av, cfg_next);
1.9 misho 540: RB_INSERT(tagRC, cfg, av);
541: CFG_RC_UNLOCK(cfg);
542: }
543:
544: return 0;
545: }
546:
1.11 misho 547: /*
548: * cfgWriteLines() - Write custom lines and export data to variable
549: *
550: * @f = File resource
551: * @delim = Custom delimiter, if =NULL default is '='
552: * @eol = End of line string, if =NULL default is "\n"
553: * @section = Export only section, if =NULL default is all
554: * @cfg = Config root
1.12 misho 555: * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
1.11 misho 556: */
557: ait_val_t *
558: cfgWriteLines(FILE *f, const char *delim, const char *eol, const char *section, cfg_root_t * __restrict cfg)
559: {
560: ait_val_t *v = NULL;
561: struct tagCfg *av;
562:
563: if (!cfg)
564: return NULL;
565: if (!delim)
566: delim = ATR_LINES_DELIM;
567: if (!eol)
568: eol = EOL_LINES_DELIM;
1.12 misho 569: if (!(v = ait_allocVar())) {
570: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.11 misho 571: return NULL;
572: } else
573: AIT_INIT_VAL2(v, string);
574:
1.14 misho 575: TAILQ_FOREACH(av, cfg, cfg_next) {
1.11 misho 576: if (section) {
577: if (!AIT_ISEMPTY(&av->cfg_sec) && *section)
578: continue;
1.14 misho 579: if (strcmp(section, AIT_GET_STRZ(&av->cfg_sec)))
1.11 misho 580: continue;
581: }
582:
583: if (!AIT_ISEMPTY(&av->cfg_sec)) {
584: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_sec));
585: AIT_SET_STRCAT(v, SEC_LINES_DELIM);
586: }
1.16 misho 587: if (!AIT_ISEMPTY(&av->cfg_attr)) {
588: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_attr));
589: AIT_SET_STRCAT(v, delim);
590: }
1.21 ! misho 591: if (!AIT_ISEMPTY(&av->cfg_val)) {
! 592: if (av->cfg_quoted)
! 593: AIT_SET_STRCAT(v, "\"");
1.11 misho 594: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val));
1.21 ! misho 595: if (av->cfg_quoted)
! 596: AIT_SET_STRCAT(v, "\"");
! 597: }
1.11 misho 598: AIT_SET_STRCAT(v, eol);
599: }
600:
601: if (f)
602: fputs(AIT_GET_STR(v), f);
603: return v;
604: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>