File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.8: download - view: text, annotated - select for diffs - revision graph
Sun Jul 22 21:54:47 2012 UTC (11 years, 10 months ago) by misho
Branches: MAIN
CVS tags: cfg5_3, cfg5_2, HEAD, CFG5_2, CFG5_1
version 5.1

    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.8 2012/07/22 21:54:47 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 cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
   84: {
   85: 	char line[BUFSIZ];
   86: 	struct tagCfg *av = NULL;
   87: 	int flg = 0;
   88: 	char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
   89: 
   90: 	while (!feof(f)) {
   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;
   97: #endif
   98: 		if (!(psAttr = strpbrk(line, "\r\n"))) {
   99: 			/* skip line, too long */
  100: 			continue;
  101: 		} else {
  102: 			*psAttr = 0;
  103: 			io_TrimStr(line);
  104: 		}
  105: 
  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));
  120: 			continue;
  121: 		}
  122: 
  123: 		/* *NEW PAIR* alloc new pair element */
  124: 		av = io_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: 		}
  134: 
  135: 		/* check for continues line */
  136: 		psAttr = line + (*line ? strlen(line) : 1) - 1;
  137: 		if (*psAttr == '\\') {
  138: 			*psAttr = 0;
  139: 			flg = 1;
  140: 		}
  141: 
  142: 		/* check for comment or empty line */
  143: 		if (!*line || *line == '#' || *line == ';') {
  144: 			AIT_SET_STR(&av->cfg_val, line);
  145: 			continue;
  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);
  155: 			} else
  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: 		}
  169: 
  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);
  175: 		}
  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);
  189: 	}
  190: 
  191: 	return 0;
  192: }
  193: 
  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 line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { 0 };
  208: 
  209: 	CFG_RC_LOCK(cfg);
  210: 	_invertQueue(cfg);
  211: 	SLIST_FOREACH(av, cfg, cfg_next) {
  212: 		/* add +1 line for section [] */
  213: 		if (!AIT_ISEMPTY(&av->cfg_sec) && 
  214: 				strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
  215: 			strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
  216: 			if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
  217: 				LOGERR;
  218: 				CFG_RC_UNLOCK(cfg);
  219: 				return -1;
  220: 			}
  221: 		}
  222: 		if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
  223: 			memset(szSection, 0, sizeof szSection);
  224: 			if (!cfg_Write(f, "\n[]\n")) {
  225: 				LOGERR;
  226: 				CFG_RC_UNLOCK(cfg);
  227: 				return -1;
  228: 			}
  229: 		}
  230: 
  231: 		/* build line */
  232: 		memset(line, 0, sizeof line);
  233: 		if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
  234: 			strlcpy(line, AIT_GET_STR(&av->cfg_attr), sizeof line);
  235: 			if (whitespace)
  236: 				strlcat(line, " = ", sizeof line);
  237: 			else
  238: 				strlcat(line, "=", sizeof line);
  239: 		}
  240: 		if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string)
  241: 			strlcat(line, AIT_GET_STR(&av->cfg_val), sizeof line);
  242: 
  243: 		/* write */
  244: 		if (!cfg_Write(f, "%s\n", line)) {
  245: 			LOGERR;
  246: 			CFG_RC_UNLOCK(cfg);
  247: 			return -1;
  248: 		}
  249: 	}
  250: 	_invertQueue(cfg);
  251: 	CFG_RC_UNLOCK(cfg);
  252: 
  253: 	if (whitespace) {
  254: 		time(&tim);
  255: 		memset(line, 0, sizeof line);
  256: 		strftime(line, sizeof line, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
  257: 		cfg_Write(f, "\n## Config was saved at :: %s ##\n", line);
  258: 	}
  259: 
  260: 	return 0;
  261: }
  262: 
  263: /*
  264:  * cfgConcatConfig() - Concat two configs into one
  265:  *
  266:  * @cfg = Config root
  267:  * @add_cfg = Concated config will be destroy after merge
  268:  * return: -1 error or 0 ok
  269:  */
  270: int
  271: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
  272: {
  273: 	struct tagCfg *item;
  274: 
  275: 	if (!cfg || !add_cfg)
  276: 		return -1;
  277: 
  278: 	CFG_RC_LOCK(add_cfg);
  279: 	CFG_RC_LOCK(cfg);
  280: 
  281: 	/* concat lists */
  282: 	for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
  283: 	SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
  284: 
  285: 	/* concat red-black trees */
  286: 	SLIST_FOREACH(item, add_cfg, cfg_next)
  287: 		RB_INSERT(tagRC, cfg, item);
  288: 
  289: 	CFG_RC_UNLOCK(cfg);
  290: 	CFG_RC_UNLOCK(add_cfg);
  291: 
  292: 	add_cfg->slh_first = NULL;
  293: 	add_cfg->rbh_root = NULL;
  294: 	pthread_mutex_destroy(&add_cfg->rc_mtx);
  295: 	return 0;
  296: }
  297: 
  298: /*
  299:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
  300:  *
  301:  * @cfg = Config root of main list
  302:  * @add_cfg = Merged config will be destroy after merge
  303:  * return: -1 error or 0 ok
  304:  */
  305: int
  306: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
  307: {
  308: 	struct tagCfg *item, *merge, *add_next, *next = NULL;
  309: 	int flg;
  310: 
  311: 	if (!cfg || !add_cfg)
  312: 		return -1;
  313: 
  314: 	CFG_RC_LOCK(add_cfg);
  315: 	CFG_RC_LOCK(cfg);
  316: 	SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
  317: 		flg = 0;
  318: 		SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
  319: 			if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
  320: 				SLIST_INSERT_AFTER(merge, item, cfg_next);
  321: 				RB_INSERT(tagRC, cfg, item);
  322: 				flg = 1;
  323: 				break;
  324: 			}
  325: 			if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) && 
  326: 					!strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
  327: 				SLIST_INSERT_AFTER(merge, item, cfg_next);
  328: 				RB_INSERT(tagRC, cfg, item);
  329: 				flg = 1;
  330: 				break;
  331: 			}
  332: 		}
  333: 
  334: 		if (!flg) {
  335: 			SLIST_INSERT_AFTER(merge, item, cfg_next);
  336: 			RB_INSERT(tagRC, cfg, item);
  337: 		}
  338: 	}
  339: 	CFG_RC_UNLOCK(cfg);
  340: 	CFG_RC_UNLOCK(add_cfg);
  341: 
  342: 	add_cfg->slh_first = NULL;
  343: 	add_cfg->rbh_root = NULL;
  344: 	pthread_mutex_destroy(&add_cfg->rc_mtx);
  345: 	return 0;
  346: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>