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