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