Annotation of libaitcfg/src/parse.c, revision 1.6.4.3
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.3 ! misho 6: * $Id: parse.c,v 1.6.4.2 2012/04/03 09:21:06 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 };
208:
209: time(&tim);
210: strftime(szTime, sizeof szTime, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
211: if (!cfg_Write(f, "## Write Config :: %s\n#\n", szTime)) {
212: LOGERR;
213: return -1;
214: }
215:
216: CFG_RC_LOCK(cfg);
217: _invertQueue(cfg);
218: SLIST_FOREACH(av, cfg, cfg_next) {
219: if (!AIT_ISEMPTY(&av->cfg_sec) &&
220: strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
221: strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
222: if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
223: LOGERR;
224: CFG_RC_UNLOCK(cfg);
225: return -1;
226: }
227: }
228: if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
229: memset(szSection, 0, sizeof szSection);
230: if (!cfg_Write(f, "\n[]\n")) {
231: LOGERR;
232: CFG_RC_UNLOCK(cfg);
233: return -1;
234: }
235: }
236:
237: if (!cfg_Write(f, ((whitespace) ? "%s = %s\n" : "%s=%s\n"),
238: AIT_GET_STR(&av->cfg_attr), AIT_GET_STR(&av->cfg_val))) {
239: LOGERR;
240: CFG_RC_UNLOCK(cfg);
241: return -1;
242: }
243: }
244: _invertQueue(cfg);
245: CFG_RC_UNLOCK(cfg);
246:
247: memset(szTime, 0, sizeof szTime);
248: time(&tim);
249: strftime(szTime, sizeof szTime, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
250: if (!cfg_Write(f, "\n#\n## Done. :: %s\n", szTime)) {
251: LOGERR;
252: return -1;
253: }
254:
255: return 0;
256: }
257:
1.6.4.1 misho 258: #if 0
1.1 misho 259: /*
1.3 misho 260: * ConcatConfig() Concat two list in one
261: * @cfg = Head list element of main list
262: * @add_cfg = Head list element of added list
263: * return: 0 ok; -1 error:: can`t concat lists
264: */
265: int ConcatConfig(sl_config * __restrict cfg, sl_config * __restrict add_cfg)
266: {
267: struct tagPair *item;
268: int ret = 0;
269:
270: if (!cfg || !add_cfg)
271: return -1;
272:
273: for (item = cfg->slh_first; item->sle_next; item = item->sle_next);
274: item->sle_next = add_cfg->slh_first;
275:
276: add_cfg->slh_first = NULL;
277:
278: return ret;
279: }
1.6.4.3 ! misho 280: #endif
1.1 misho 281:
1.3 misho 282: /*
1.6.4.3 ! misho 283: * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
! 284: *
! 285: * @cfg = Config root of main list
! 286: * @add_cfg = Merged config will be destroy after merge
! 287: * return: -1 error or 0 ok
! 288: */
! 289: int
! 290: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
1.3 misho 291: {
1.6.4.3 ! misho 292: struct tagCfg *item, *merge, *add_next, *next = NULL;
1.3 misho 293: int flg;
1.1 misho 294:
1.3 misho 295: if (!cfg || !add_cfg)
1.1 misho 296: return -1;
297:
1.6.4.3 ! misho 298: CFG_RC_LOCK(add_cfg);
! 299: CFG_RC_LOCK(cfg);
! 300: SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
! 301: flg = 0;
! 302: SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
! 303: if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
! 304: SLIST_INSERT_AFTER(merge, item, cfg_next);
! 305: RB_INSERT(tagRC, cfg, item);
1.3 misho 306: flg = 1;
307: break;
308: }
1.6.4.3 ! misho 309: if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) &&
! 310: !strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
! 311: SLIST_INSERT_AFTER(merge, item, cfg_next);
! 312: RB_INSERT(tagRC, cfg, item);
1.3 misho 313: flg = 1;
314: break;
1.1 misho 315: }
316: }
1.3 misho 317:
318: if (!flg) {
1.6.4.3 ! misho 319: SLIST_INSERT_AFTER(merge, item, cfg_next);
! 320: RB_INSERT(tagRC, cfg, item);
1.1 misho 321: }
322: }
1.6.4.3 ! misho 323: CFG_RC_UNLOCK(cfg);
! 324: CFG_RC_UNLOCK(add_cfg);
1.1 misho 325:
1.3 misho 326: add_cfg->slh_first = NULL;
1.6.4.3 ! misho 327: add_cfg->rbh_root = NULL;
! 328: pthread_mutex_destroy(&add_cfg->rc_mtx);
1.1 misho 329: return 0;
330: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>