1: /*************************************************************************
2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: parse.c,v 1.21 2023/01/23 23:27:26 misho Exp $
7: *
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:
15: Copyright 2004 - 2023
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: */
46: #include "global.h"
47:
48:
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: */
56: int
57: cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
58: {
59: char line[BUFSIZ], origin[BUFSIZ], chkattr[STRSIZ];
60: struct tagCfg *av = NULL;
61: int flg = 0;
62: char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
63: FILE *ff;
64:
65: if (!f || !cfg) {
66: cfg_SetErr(EINVAL, "Invalid parameter(s)");
67: return -1;
68: }
69:
70: while (!feof(f)) {
71: memset(line, 0, sizeof line);
72: if (!fgets(line, sizeof(line) - 1, f))
73: break;
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;
84: strlcpy(origin, line, sizeof origin);
85: str_Trim(line);
86: }
87:
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);
100: if (!flg && AIT_ADDR(&av->cfg_val))
101: av->cfg_quoted += str_Unquot((char*) AIT_GET_STR(&av->cfg_val));
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
112: EDEBUG(ELWIX_DEBUG_LOG, "Error:: Can't open %s file",
113: AIT_GET_STR(&av->cfg_val));
114: }
115: continue;
116: }
117:
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:
128: /* *NEW PAIR* alloc new pair element */
129: av = e_malloc(sizeof(struct tagCfg));
130: if (!av) {
131: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
132: return -1;
133: } else {
134: memset(av, 0, sizeof(struct tagCfg));
135: CFG_RC_LOCK(cfg);
136: TAILQ_INSERT_TAIL(cfg, av, cfg_next);
137: CFG_RC_UNLOCK(cfg);
138: }
139:
140: /* check for comment or empty line */
141: if (!*line || *line == '#' || *line == ';') {
142: AIT_SET_STR(&av->cfg_val, line);
143: continue;
144: }
145:
146: /* check for continues line */
147: psAttr = line + (*line ? strlen(line) : 1) - 1;
148: if (*psAttr == '\\') {
149: *psAttr = 0;
150: flg = 1;
151: }
152:
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);
160: AIT_SET_STR(&av->cfg_sec, line);
161: } else
162: EDEBUG(ELWIX_DEBUG_LOG, "Ignore section '%s' ... not found ']'", line);
163: continue;
164: }
165: /* parse pair */
166: if (!(psAttr = strchr(line, '='))) {
167: AIT_SET_STR(&av->cfg_val, origin);
168: EDEBUG(ELWIX_DEBUG_LOG, "Ignore a/v '%s' ... not found '='", line);
169: continue;
170: } else {
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*),
180: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
181: }
182:
183: str_RTrim(psAttr);
184: str_LTrim(psVal);
185: if (!flg)
186: av->cfg_quoted += str_Unquot(psVal);
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*),
190: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
191:
192: CFG_RC_LOCK(cfg);
193: RB_INSERT(tagRC, cfg, av);
194: CFG_RC_UNLOCK(cfg);
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
204: EDEBUG(ELWIX_DEBUG_LOG, "Error:: Can't open %s file",
205: AIT_GET_STR(&av->cfg_val));
206: }
207: }
208:
209: return 0;
210: }
211:
212: /*
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)
222: {
223: struct tagCfg *av;
224: char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
225:
226: if (!f || !cfg) {
227: cfg_SetErr(EINVAL, "Invalid parameter(s)");
228: return -1;
229: }
230:
231: CFG_RC_LOCK(cfg);
232: RB_FOREACH(av, tagRC, cfg) {
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 [] */
242: if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) &&
243: strcmp(AIT_GET_STRZ(&av->cfg_sec), szSection)) {
244: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
245: if (!cfg_Write(f, "[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
246: LOGERR;
247: CFG_RC_UNLOCK(cfg);
248: return -1;
249: }
250: } else if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
251: memset(szSection, 0, sizeof szSection);
252: if (!cfg_Write(f, "[]\n")) {
253: LOGERR;
254: CFG_RC_UNLOCK(cfg);
255: return -1;
256: }
257: }
258:
259: /* build line */
260: memset(line, 0, sizeof line);
261: if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
262: strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
263: if (whitespace)
264: strlcat(line, " = ", sizeof line);
265: else
266: strlcat(line, "=", sizeof line);
267: }
268: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string) {
269: if (av->cfg_quoted)
270: strlcat(line, "\"", sizeof line);
271: strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
272: if (av->cfg_quoted)
273: strlcat(line, "\"", sizeof line);
274: }
275: skip_sec:
276: /* write */
277: if (!cfg_Write(f, "%s\n", line)) {
278: LOGERR;
279: CFG_RC_UNLOCK(cfg);
280: return -1;
281: }
282: }
283: CFG_RC_UNLOCK(cfg);
284:
285: return 0;
286: }
287:
288: /*
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);
321: if (!cfg_Write(f, "[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
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);
328: if (!cfg_Write(f, "[]\n")) {
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: }
344: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string) {
345: if (av->cfg_quoted)
346: strlcat(line, "\"", sizeof line);
347: strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
348: if (av->cfg_quoted)
349: strlcat(line, "\"", sizeof line);
350: }
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: /*
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)
372: {
373: struct tagCfg *item;
374:
375: if (!cfg || !add_cfg)
376: return -1;
377:
378: CFG_RC_LOCK(add_cfg);
379: CFG_RC_LOCK(cfg);
380:
381: /* concat lists & red-black trees */
382: TAILQ_FOREACH(item, add_cfg, cfg_next) {
383: TAILQ_INSERT_TAIL(cfg, item, cfg_next);
384: RB_INSERT(tagRC, cfg, item);
385: }
386:
387: CFG_RC_UNLOCK(cfg);
388:
389: TAILQ_INIT(add_cfg);
390: RB_INIT(add_cfg);
391: CFG_RC_UNLOCK(add_cfg);
392: pthread_mutex_destroy(&add_cfg->rc_mtx);
393: return 0;
394: }
395:
396: /*
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)
405: {
406: struct tagCfg *item, *merge, *add_next, *next;
407: int flg;
408:
409: if (!cfg || !add_cfg)
410: return -1;
411:
412: CFG_RC_LOCK(add_cfg);
413: CFG_RC_LOCK(cfg);
414:
415: /* merge lists */
416: TAILQ_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
417: flg = 0;
418: TAILQ_FOREACH_SAFE(merge, cfg, cfg_next, next) {
419: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
420: flg = 1;
421: break;
422: }
423: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
424: AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
425: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
426: flg = -1;
427: break;
428: }
429: }
430:
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: }
441: RB_INSERT(tagRC, cfg, item);
442: }
443:
444: CFG_RC_UNLOCK(cfg);
445:
446: TAILQ_INIT(add_cfg);
447: RB_INIT(add_cfg);
448: CFG_RC_UNLOCK(add_cfg);
449: pthread_mutex_destroy(&add_cfg->rc_mtx);
450: return 0;
451: }
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;
467: char *p, *psSec, *psAttr, *psVal;
468:
469: if (!cfg)
470: return -1;
471: if (!delim)
472: delim = ATR_LINES_DELIM;
473:
474: while (!feof(f)) {
475: psSec = psAttr = psVal = NULL;
476: memset(line, 0, sizeof line);
477: if (!fgets(line, sizeof(line) - 1, f))
478: break;
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;
488: str_Trim(line);
489: if (!*line)
490: continue;
491: }
492:
493: if (!av_MakeExt(line, delim, &p, &psVal))
494: continue;
495: else {
496: str_RTrim(p);
497: str_LTrim(psVal);
498: }
499: if (!av_MakeExt(p, SEC_LINES_DELIM, &psSec, &psAttr))
500: psAttr = p;
501:
502: /* check for duplicated element */
503: if (psAttr && cfg_findAttribute(cfg, psSec, psAttr))
504: cfg_unsetAttribute(cfg, psSec, psAttr);
505:
506: /* *NEW PAIR* alloc new pair element */
507: av = e_malloc(sizeof(struct tagCfg));
508: if (!av) {
509: LOGERR;
510: return -1;
511: } else
512: memset(av, 0, sizeof(struct tagCfg));
513:
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*),
517: E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
518: }
519: if (psVal) {
520: av->cfg_quoted = str_Unquot(psVal);
521: AIT_SET_STR(&av->cfg_val, psVal);
522: }
523: AIT_SET_STR(&av->cfg_attr, psAttr);
524: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
525: E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
526:
527: CFG_RC_LOCK(cfg);
528: /* find & delete duplicates */
529: if ((d = RB_FIND(tagRC, cfg, av))) {
530: RB_REMOVE(tagRC, cfg, d);
531: TAILQ_REMOVE(cfg, d, cfg_next);
532:
533: AIT_FREE_VAL(&d->cfg_val);
534: AIT_FREE_VAL(&d->cfg_attr);
535: AIT_FREE_VAL(&d->cfg_sec);
536: e_free(d);
537: }
538:
539: TAILQ_INSERT_TAIL(cfg, av, cfg_next);
540: RB_INSERT(tagRC, cfg, av);
541: CFG_RC_UNLOCK(cfg);
542: }
543:
544: return 0;
545: }
546:
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
555: * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
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;
569: if (!(v = ait_allocVar())) {
570: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
571: return NULL;
572: } else
573: AIT_INIT_VAL2(v, string);
574:
575: TAILQ_FOREACH(av, cfg, cfg_next) {
576: if (section) {
577: if (!AIT_ISEMPTY(&av->cfg_sec) && *section)
578: continue;
579: if (strcmp(section, AIT_GET_STRZ(&av->cfg_sec)))
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: }
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: }
591: if (!AIT_ISEMPTY(&av->cfg_val)) {
592: if (av->cfg_quoted)
593: AIT_SET_STRCAT(v, "\"");
594: AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val));
595: if (av->cfg_quoted)
596: AIT_SET_STRCAT(v, "\"");
597: }
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>