Annotation of libaitcfg/src/parse.c, revision 1.6.4.7
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.6.4.7 ! misho 6: * $Id: parse.c,v 1.6.4.6 2012/04/04 11:43:21 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.6.4.1 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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: #include "aitcfg.h"
48:
49:
1.6.4.2 misho 50: static inline int
51: cfg_Write(FILE *f, char *fmt, ...)
1.1 misho 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:
1.6.4.2 misho 63: static inline void
64: _invertQueue(cfg_root_t * __restrict cfg)
1.3 misho 65: {
1.6.4.2 misho 66: struct tagCfg *item, *next, *prev = NULL;
1.3 misho 67:
1.6.4.2 misho 68: SLIST_FOREACH_SAFE(item, cfg, cfg_next, next) {
69: item->cfg_next.sle_next = prev;
1.3 misho 70: prev = item;
71: }
72: cfg->slh_first = prev;
73: }
74:
1.1 misho 75:
76: /*
1.6.4.1 misho 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 cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
1.1 misho 84: {
1.6.4.1 misho 85: char line[BUFSIZ];
86: struct tagCfg *av = NULL;
87: int flg = 0;
88: char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
1.1 misho 89:
90: while (!feof(f)) {
1.6.4.1 misho 91: memset(line, 0, sizeof line);
92: fgets(line, sizeof line - 1, f);
93: #ifdef SUPPORT_USER_EOF
94: /* check for user end-of-file */
95: if (line[0] == '.' && line[1] == '\n')
96: break;
1.1 misho 97: #endif
1.6.4.1 misho 98: if (!(psAttr = strpbrk(line, "\r\n"))) {
99: /* skip line, too long */
100: continue;
101: } else {
102: *psAttr = 0;
103: io_TrimStr(line);
104: }
1.1 misho 105:
1.6.4.1 misho 106: if (flg) {
107: /* continues line */
108: if (!av)
109: continue;
110: else
111: psAttr = line + strlen(line) - 1;
112: if (*psAttr == '\\')
113: *psAttr = 0;
114: else
115: flg = 0;
116: /* concat line to value */
117: AIT_SET_STRCAT(&av->cfg_val, line);
118: if (!flg)
119: io_UnquotStr((char*) AIT_GET_STR(&av->cfg_val));
1.1 misho 120: continue;
1.6.4.1 misho 121: }
1.1 misho 122:
1.6.4.1 misho 123: /* *NEW PAIR* alloc new pair element */
124: av = malloc(sizeof(struct tagCfg));
125: if (!av) {
126: LOGERR;
127: return -1;
128: } else {
129: memset(av, 0, sizeof(struct tagCfg));
130: CFG_RC_LOCK(cfg);
131: SLIST_INSERT_HEAD(cfg, av, cfg_next);
132: CFG_RC_UNLOCK(cfg);
133: }
1.1 misho 134:
1.6.4.1 misho 135: /* check for continues line */
136: psAttr = line + strlen(line) - 1;
137: if (*psAttr == '\\') {
138: *psAttr = 0;
139: flg = 1;
1.1 misho 140: }
141:
1.6.4.1 misho 142: /* check for comment or empty line */
143: if (!*line || *line == '#' || *line == ';') {
144: AIT_SET_STR(&av->cfg_val, line);
1.1 misho 145: continue;
1.6.4.1 misho 146: }
147: /* section */
148: if (*line == '[') {
149: AIT_SET_STR(&av->cfg_val, line);
150: psAttr = line + strlen(line) - 1;
151: if (*psAttr == ']') {
152: *psAttr = 0;
153: flg = 0;
154: strlcpy(szSection, line + 1, sizeof szSection);
1.1 misho 155: } else
1.6.4.1 misho 156: ioDEBUG(7, "Ignore section '%s' ... not found ']'", line);
157: continue;
158: }
159: /* parse pair */
160: if (!(psAttr = strchr(line, '='))) {
161: AIT_SET_STR(&av->cfg_val, line);
162: ioDEBUG(7, "Ignore a/v '%s' ... not found '='", line);
163: continue;
164: } else {
165: *psAttr = 0;
166: psVal = psAttr + 1;
167: psAttr = line;
168: }
1.1 misho 169:
1.6.4.1 misho 170: /* if exists, added section name to element */
171: if (*szSection) {
172: AIT_SET_STR(&av->cfg_sec, szSection);
173: AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*),
174: io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
1.1 misho 175: }
1.6.4.1 misho 176:
177: io_RTrimStr(psAttr);
178: io_LTrimStr(psVal);
179: if (!flg)
180: io_UnquotStr(psVal);
181: AIT_SET_STR(&av->cfg_val, psVal);
182: AIT_SET_STR(&av->cfg_attr, psAttr);
183: AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*),
184: io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
185:
186: CFG_RC_LOCK(cfg);
187: RB_INSERT(tagRC, cfg, av);
188: CFG_RC_UNLOCK(cfg);
1.1 misho 189: }
190:
191: return 0;
192: }
193:
1.6.4.2 misho 194: /*
195: * cfgWriteConfig() - Write config from memory
196: *
197: * @f = File handle
198: * @cfg = Config root
199: * @whitespace = Additional whitespace characters to file
200: * return: -1 error or 0 ok
201: */
202: int
203: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
204: {
205: struct tagCfg *av;
206: time_t tim;
207: char szTime[STRSIZ] = { 0 }, szSection[STRSIZ] = { 0 };
1.6.4.7 ! misho 208: char line[BUFSIZ];
1.6.4.2 misho 209:
210: time(&tim);
211: strftime(szTime, sizeof szTime, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
212: if (!cfg_Write(f, "## Write Config :: %s\n#\n", szTime)) {
213: LOGERR;
214: return -1;
215: }
216:
217: CFG_RC_LOCK(cfg);
218: _invertQueue(cfg);
219: SLIST_FOREACH(av, cfg, cfg_next) {
1.6.4.7 ! misho 220: /* add +1 line for section [] */
1.6.4.2 misho 221: if (!AIT_ISEMPTY(&av->cfg_sec) &&
222: strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
223: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
224: if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
225: LOGERR;
226: CFG_RC_UNLOCK(cfg);
227: return -1;
228: }
229: }
230: if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
231: memset(szSection, 0, sizeof szSection);
232: if (!cfg_Write(f, "\n[]\n")) {
233: LOGERR;
234: CFG_RC_UNLOCK(cfg);
235: return -1;
236: }
237: }
238:
1.6.4.7 ! misho 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_STR(&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_STR(&av->cfg_val), sizeof line);
! 250:
! 251: /* write */
! 252: if (!cfg_Write(f, "%s\n", line)) {
1.6.4.2 misho 253: LOGERR;
254: CFG_RC_UNLOCK(cfg);
255: return -1;
256: }
257: }
258: _invertQueue(cfg);
259: CFG_RC_UNLOCK(cfg);
260:
261: memset(szTime, 0, sizeof szTime);
262: time(&tim);
263: strftime(szTime, sizeof szTime, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
264: if (!cfg_Write(f, "\n#\n## Done. :: %s\n", szTime)) {
265: LOGERR;
266: return -1;
267: }
268:
269: return 0;
270: }
271:
1.1 misho 272: /*
1.6.4.4 misho 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)
1.3 misho 281: {
1.6.4.4 misho 282: struct tagCfg *item;
1.3 misho 283:
284: if (!cfg || !add_cfg)
285: return -1;
286:
1.6.4.4 misho 287: CFG_RC_LOCK(add_cfg);
288: CFG_RC_LOCK(cfg);
1.3 misho 289:
1.6.4.4 misho 290: /* concat lists */
291: for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
292: SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
293:
294: /* concat red-black trees */
295: SLIST_FOREACH(item, add_cfg, cfg_next)
296: RB_INSERT(tagRC, cfg, item);
1.3 misho 297:
1.6.4.4 misho 298: CFG_RC_UNLOCK(cfg);
299: CFG_RC_UNLOCK(add_cfg);
300:
301: add_cfg->slh_first = NULL;
302: add_cfg->rbh_root = NULL;
303: pthread_mutex_destroy(&add_cfg->rc_mtx);
304: return 0;
1.3 misho 305: }
1.1 misho 306:
1.3 misho 307: /*
1.6.4.3 misho 308: * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
309: *
310: * @cfg = Config root of main list
311: * @add_cfg = Merged config will be destroy after merge
312: * return: -1 error or 0 ok
313: */
314: int
315: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3 misho 316: {
1.6.4.3 misho 317: struct tagCfg *item, *merge, *add_next, *next = NULL;
1.3 misho 318: int flg;
1.1 misho 319:
1.3 misho 320: if (!cfg || !add_cfg)
1.1 misho 321: return -1;
322:
1.6.4.3 misho 323: CFG_RC_LOCK(add_cfg);
324: CFG_RC_LOCK(cfg);
325: SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
326: flg = 0;
327: SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
328: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
329: SLIST_INSERT_AFTER(merge, item, cfg_next);
330: RB_INSERT(tagRC, cfg, item);
1.3 misho 331: flg = 1;
332: break;
333: }
1.6.4.3 misho 334: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
335: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
336: SLIST_INSERT_AFTER(merge, item, cfg_next);
337: RB_INSERT(tagRC, cfg, item);
1.3 misho 338: flg = 1;
339: break;
1.1 misho 340: }
341: }
1.3 misho 342:
343: if (!flg) {
1.6.4.3 misho 344: SLIST_INSERT_AFTER(merge, item, cfg_next);
345: RB_INSERT(tagRC, cfg, item);
1.1 misho 346: }
347: }
1.6.4.3 misho 348: CFG_RC_UNLOCK(cfg);
349: CFG_RC_UNLOCK(add_cfg);
1.1 misho 350:
1.3 misho 351: add_cfg->slh_first = NULL;
1.6.4.3 misho 352: add_cfg->rbh_root = NULL;
353: pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1 misho 354: return 0;
355: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>