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.9 2012/08/01 00:39:11 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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: #include "aitcfg.h"
48:
49:
50: static inline int
51: cfg_Write(FILE *f, char *fmt, ...)
52: {
53: int ret = 0;
54: va_list lst;
55:
56: va_start(lst, fmt);
57: ret = vfprintf(f, fmt, lst);
58: va_end(lst);
59:
60: return ret;
61: }
62:
63: static inline void
64: _invertQueue(cfg_root_t * __restrict cfg)
65: {
66: struct tagCfg *item, *next, *prev = NULL;
67:
68: SLIST_FOREACH_SAFE(item, cfg, cfg_next, next) {
69: item->cfg_next.sle_next = prev;
70: prev = item;
71: }
72: cfg->slh_first = prev;
73: }
74:
75:
76: /*
77: * cfgReadConfig() - Read file and add new item at config root
78: *
79: * @f = File resource
80: * @cfg = Config root
81: * return: -1 error or 0 ok
82: */
83: int
84: cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
85: {
86: char line[BUFSIZ];
87: struct tagCfg *av = NULL;
88: int flg = 0;
89: char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
90:
91: while (!feof(f)) {
92: memset(line, 0, sizeof line);
93: fgets(line, sizeof line - 1, f);
94: #ifdef SUPPORT_USER_EOF
95: /* check for user end-of-file */
96: if (line[0] == '.' && line[1] == '\n')
97: break;
98: #endif
99: if (!(psAttr = strpbrk(line, "\r\n"))) {
100: /* skip line, too long */
101: continue;
102: } else {
103: *psAttr = 0;
104: io_TrimStr(line);
105: }
106:
107: if (flg) {
108: /* continues line */
109: if (!av)
110: continue;
111: else
112: psAttr = line + strlen(line) - 1;
113: if (*psAttr == '\\')
114: *psAttr = 0;
115: else
116: flg = 0;
117: /* concat line to value */
118: AIT_SET_STRCAT(&av->cfg_val, line);
119: if (!flg)
120: io_UnquotStr((char*) AIT_GET_STR(&av->cfg_val));
121: continue;
122: }
123:
124: /* *NEW PAIR* alloc new pair element */
125: av = io_malloc(sizeof(struct tagCfg));
126: if (!av) {
127: LOGERR;
128: return -1;
129: } else {
130: memset(av, 0, sizeof(struct tagCfg));
131: CFG_RC_LOCK(cfg);
132: SLIST_INSERT_HEAD(cfg, av, cfg_next);
133: CFG_RC_UNLOCK(cfg);
134: }
135:
136: /* check for continues line */
137: psAttr = line + (*line ? strlen(line) : 1) - 1;
138: if (*psAttr == '\\') {
139: *psAttr = 0;
140: flg = 1;
141: }
142:
143: /* check for comment or empty line */
144: if (!*line || *line == '#' || *line == ';') {
145: AIT_SET_STR(&av->cfg_val, line);
146: continue;
147: }
148: /* section */
149: if (*line == '[') {
150: AIT_SET_STR(&av->cfg_val, line);
151: psAttr = line + strlen(line) - 1;
152: if (*psAttr == ']') {
153: *psAttr = 0;
154: flg = 0;
155: strlcpy(szSection, line + 1, sizeof szSection);
156: } else
157: ioDEBUG(7, "Ignore section '%s' ... not found ']'", line);
158: continue;
159: }
160: /* parse pair */
161: if (!(psAttr = strchr(line, '='))) {
162: AIT_SET_STR(&av->cfg_val, line);
163: ioDEBUG(7, "Ignore a/v '%s' ... not found '='", line);
164: continue;
165: } else {
166: *psAttr = 0;
167: psVal = psAttr + 1;
168: psAttr = line;
169: }
170:
171: /* if exists, added section name to element */
172: if (*szSection) {
173: AIT_SET_STR(&av->cfg_sec, szSection);
174: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
175: io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
176: }
177:
178: io_RTrimStr(psAttr);
179: io_LTrimStr(psVal);
180: if (!flg)
181: io_UnquotStr(psVal);
182: AIT_SET_STR(&av->cfg_val, psVal);
183: AIT_SET_STR(&av->cfg_attr, psAttr);
184: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
185: io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
186:
187: CFG_RC_LOCK(cfg);
188: RB_INSERT(tagRC, cfg, av);
189: CFG_RC_UNLOCK(cfg);
190: }
191:
192: return 0;
193: }
194:
195: /*
196: * cfgWriteConfig() - Write config from memory
197: *
198: * @f = File handle
199: * @cfg = Config root
200: * @whitespace = Additional whitespace characters to file
201: * return: -1 error or 0 ok
202: */
203: int
204: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
205: {
206: struct tagCfg *av;
207: time_t tim;
208: char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { 0 };
209:
210: CFG_RC_LOCK(cfg);
211: _invertQueue(cfg);
212: SLIST_FOREACH(av, cfg, cfg_next) {
213: /* add +1 line for section [] */
214: if (!AIT_ISEMPTY(&av->cfg_sec) &&
215: strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
216: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
217: if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
218: LOGERR;
219: CFG_RC_UNLOCK(cfg);
220: return -1;
221: }
222: }
223: if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
224: memset(szSection, 0, sizeof szSection);
225: if (!cfg_Write(f, "\n[]\n")) {
226: LOGERR;
227: CFG_RC_UNLOCK(cfg);
228: return -1;
229: }
230: }
231:
232: /* build line */
233: memset(line, 0, sizeof line);
234: if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
235: strlcpy(line, AIT_GET_STR(&av->cfg_attr), sizeof line);
236: if (whitespace)
237: strlcat(line, " = ", sizeof line);
238: else
239: strlcat(line, "=", sizeof line);
240: }
241: if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string)
242: strlcat(line, AIT_GET_STR(&av->cfg_val), sizeof line);
243:
244: /* write */
245: if (!cfg_Write(f, "%s\n", line)) {
246: LOGERR;
247: CFG_RC_UNLOCK(cfg);
248: return -1;
249: }
250: }
251: _invertQueue(cfg);
252: CFG_RC_UNLOCK(cfg);
253:
254: if (whitespace) {
255: time(&tim);
256: memset(line, 0, sizeof line);
257: strftime(line, sizeof line, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
258: cfg_Write(f, "\n## Config was saved at :: %s ##\n", line);
259: }
260:
261: return 0;
262: }
263:
264: /*
265: * cfgConcatConfig() - Concat two configs into one
266: *
267: * @cfg = Config root
268: * @add_cfg = Concated config will be destroy after merge
269: * return: -1 error or 0 ok
270: */
271: int
272: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
273: {
274: struct tagCfg *item;
275:
276: if (!cfg || !add_cfg)
277: return -1;
278:
279: CFG_RC_LOCK(add_cfg);
280: CFG_RC_LOCK(cfg);
281:
282: /* concat lists */
283: for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
284: SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
285:
286: /* concat red-black trees */
287: SLIST_FOREACH(item, add_cfg, cfg_next)
288: RB_INSERT(tagRC, cfg, item);
289:
290: CFG_RC_UNLOCK(cfg);
291: CFG_RC_UNLOCK(add_cfg);
292:
293: add_cfg->slh_first = NULL;
294: add_cfg->rbh_root = NULL;
295: pthread_mutex_destroy(&add_cfg->rc_mtx);
296: return 0;
297: }
298:
299: /*
300: * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
301: *
302: * @cfg = Config root of main list
303: * @add_cfg = Merged config will be destroy after merge
304: * return: -1 error or 0 ok
305: */
306: int
307: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
308: {
309: struct tagCfg *item, *merge, *add_next, *next = NULL;
310: int flg;
311:
312: if (!cfg || !add_cfg)
313: return -1;
314:
315: CFG_RC_LOCK(add_cfg);
316: CFG_RC_LOCK(cfg);
317: SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
318: flg = 0;
319: SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
320: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
321: SLIST_INSERT_AFTER(merge, item, cfg_next);
322: RB_INSERT(tagRC, cfg, item);
323: flg = 1;
324: break;
325: }
326: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
327: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
328: SLIST_INSERT_AFTER(merge, item, cfg_next);
329: RB_INSERT(tagRC, cfg, item);
330: flg = 1;
331: break;
332: }
333: }
334:
335: if (!flg) {
336: SLIST_INSERT_AFTER(merge, item, cfg_next);
337: RB_INSERT(tagRC, cfg, item);
338: }
339: }
340: CFG_RC_UNLOCK(cfg);
341: CFG_RC_UNLOCK(add_cfg);
342:
343: add_cfg->slh_first = NULL;
344: add_cfg->rbh_root = NULL;
345: pthread_mutex_destroy(&add_cfg->rc_mtx);
346: return 0;
347: }
348:
349: /*
350: * cfgReadLines() - Read custom lines and add new item at config root
351: *
352: * @f = File resource
353: * @delim = Custom delimiter, if =NULL default is '='
354: * @end = Custom user end of file, if =NULL default is EOF
355: * @cfg = Config root
356: * return: -1 error or 0 ok
357: */
358: int
359: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
360: {
361: char line[BUFSIZ];
362: struct tagCfg *d, *av = NULL;
363: char *psAttr, *psVal = NULL;
364:
365: while (!feof(f)) {
366: memset(line, 0, sizeof line);
367: fgets(line, sizeof line - 1, f);
368: /* check for user end-of-file */
369: if (strspn(line, end))
370: break;
371:
372: if (!(psAttr = strpbrk(line, "\r\n"))) {
373: /* skip line, too long */
374: continue;
375: } else {
376: *psAttr = 0;
377: io_TrimStr(line);
378: if (!*line)
379: continue;
380: }
381:
382: if (!io_MakeAV2(line, delim, &psAttr, &psVal))
383: continue;
384: else {
385: io_LTrimStr(psVal);
386: io_RTrimStr(psAttr);
387: }
388:
389: /* *NEW PAIR* alloc new pair element */
390: av = io_malloc(sizeof(struct tagCfg));
391: if (!av) {
392: LOGERR;
393: return -1;
394: } else
395: memset(av, 0, sizeof(struct tagCfg));
396:
397: if (psVal)
398: AIT_SET_STR(&av->cfg_val, psVal);
399: AIT_SET_STR(&av->cfg_attr, psAttr);
400: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
401: io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
402:
403: CFG_RC_LOCK(cfg);
404: /* find & delete duplicates */
405: if ((d = RB_FIND(tagRC, cfg, av))) {
406: RB_REMOVE(tagRC, cfg, d);
407: SLIST_REMOVE(cfg, d, tagCfg, cfg_next);
408:
409: AIT_FREE_VAL(&d->cfg_val);
410: AIT_FREE_VAL(&d->cfg_attr);
411: AIT_FREE_VAL(&d->cfg_sec);
412: io_free(d);
413: }
414:
415: SLIST_INSERT_HEAD(cfg, av, cfg_next);
416: RB_INSERT(tagRC, cfg, av);
417: CFG_RC_UNLOCK(cfg);
418: }
419:
420: return 0;
421: }
422:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>