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