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