File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.9.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Aug 6 14:52:51 2012 UTC (11 years, 9 months ago) by misho
Branches: cfg5_4
Diff to: branchpoint 1.9: preferred, unified
fix bugs appeared from last version of libaitio

    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.2.1 2012/08/06 14:52:51 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 && AIT_ADDR(&av->cfg_val))
  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) && AIT_ADDR(&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: 				AIT_ADDR(&av->cfg_attr)) {
  236: 			strlcpy(line, AIT_GET_STR(&av->cfg_attr), sizeof line);
  237: 			if (whitespace)
  238: 				strlcat(line, " = ", sizeof line);
  239: 			else
  240: 				strlcat(line, "=", sizeof line);
  241: 		}
  242: 		if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string && 
  243: 				AIT_ADDR(&av->cfg_val))
  244: 			strlcat(line, AIT_GET_STR(&av->cfg_val), sizeof line);
  245: 
  246: 		/* write */
  247: 		if (!cfg_Write(f, "%s\n", line)) {
  248: 			LOGERR;
  249: 			CFG_RC_UNLOCK(cfg);
  250: 			return -1;
  251: 		}
  252: 	}
  253: 	_invertQueue(cfg);
  254: 	CFG_RC_UNLOCK(cfg);
  255: 
  256: 	if (whitespace) {
  257: 		time(&tim);
  258: 		memset(line, 0, sizeof line);
  259: 		strftime(line, sizeof line, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
  260: 		cfg_Write(f, "\n## Config was saved at :: %s ##\n", line);
  261: 	}
  262: 
  263: 	return 0;
  264: }
  265: 
  266: /*
  267:  * cfgConcatConfig() - Concat two configs into one
  268:  *
  269:  * @cfg = Config root
  270:  * @add_cfg = Concated config will be destroy after merge
  271:  * return: -1 error or 0 ok
  272:  */
  273: int
  274: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
  275: {
  276: 	struct tagCfg *item;
  277: 
  278: 	if (!cfg || !add_cfg)
  279: 		return -1;
  280: 
  281: 	CFG_RC_LOCK(add_cfg);
  282: 	CFG_RC_LOCK(cfg);
  283: 
  284: 	/* concat lists */
  285: 	for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
  286: 	SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
  287: 
  288: 	/* concat red-black trees */
  289: 	SLIST_FOREACH(item, add_cfg, cfg_next)
  290: 		RB_INSERT(tagRC, cfg, item);
  291: 
  292: 	CFG_RC_UNLOCK(cfg);
  293: 	CFG_RC_UNLOCK(add_cfg);
  294: 
  295: 	add_cfg->slh_first = NULL;
  296: 	add_cfg->rbh_root = NULL;
  297: 	pthread_mutex_destroy(&add_cfg->rc_mtx);
  298: 	return 0;
  299: }
  300: 
  301: /*
  302:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
  303:  *
  304:  * @cfg = Config root of main list
  305:  * @add_cfg = Merged config will be destroy after merge
  306:  * return: -1 error or 0 ok
  307:  */
  308: int
  309: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
  310: {
  311: 	struct tagCfg *item, *merge, *add_next, *next;
  312: 	int flg;
  313: 
  314: 	if (!cfg || !add_cfg)
  315: 		return -1;
  316: 
  317: 	CFG_RC_LOCK(add_cfg);
  318: 	CFG_RC_LOCK(cfg);
  319: 
  320: 	/* merge lists */
  321: 	SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
  322: 		flg = 0;
  323: 		SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
  324: 			if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
  325: 				flg = 1;
  326: 				break;
  327: 			}
  328: 			if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) && 
  329: 					AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
  330: 					!strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
  331: 				flg = 1;
  332: 				break;
  333: 			}
  334: 		}
  335: 
  336: 		if (!flg)
  337: 			SLIST_INSERT_HEAD(cfg, item, cfg_next);
  338: 		else
  339: 			SLIST_INSERT_AFTER(merge, item, cfg_next);
  340: 		RB_INSERT(tagRC, cfg, item);
  341: 	}
  342: 
  343: 	CFG_RC_UNLOCK(cfg);
  344: 	CFG_RC_UNLOCK(add_cfg);
  345: 
  346: 	add_cfg->slh_first = NULL;
  347: 	add_cfg->rbh_root = NULL;
  348: 	pthread_mutex_destroy(&add_cfg->rc_mtx);
  349: 	return 0;
  350: }
  351: 
  352: /*
  353:  * cfgReadLines() - Read custom lines and add new item at config root
  354:  *
  355:  * @f = File resource
  356:  * @delim = Custom delimiter, if =NULL default is '='
  357:  * @end = Custom user end of file, if =NULL default is EOF
  358:  * @cfg = Config root
  359:  * return: -1 error or 0 ok
  360:  */
  361: int
  362: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
  363: {
  364: 	char line[BUFSIZ];
  365: 	struct tagCfg *d, *av = NULL;
  366: 	char *psAttr, *psVal = NULL;
  367: 
  368: 	while (!feof(f)) {
  369: 		memset(line, 0, sizeof line);
  370: 		fgets(line, sizeof line - 1, f);
  371: 		/* check for user end-of-file */
  372: 		if (strspn(line, end))
  373: 			break;
  374: 
  375: 		if (!(psAttr = strpbrk(line, "\r\n"))) {
  376: 			/* skip line, too long */
  377: 			continue;
  378: 		} else {
  379: 			*psAttr = 0;
  380: 			io_TrimStr(line);
  381: 			if (!*line)
  382: 				continue;
  383: 		}
  384: 
  385: 		if (!io_MakeAV2(line, delim, &psAttr, &psVal))
  386: 			continue;
  387: 		else {
  388: 			io_LTrimStr(psVal);
  389: 			io_RTrimStr(psAttr);
  390: 		}
  391: 
  392: 		/* *NEW PAIR* alloc new pair element */
  393: 		av = io_malloc(sizeof(struct tagCfg));
  394: 		if (!av) {
  395: 			LOGERR;
  396: 			return -1;
  397: 		} else
  398: 			memset(av, 0, sizeof(struct tagCfg));
  399: 
  400: 		if (psVal)
  401: 			AIT_SET_STR(&av->cfg_val, psVal);
  402: 		AIT_SET_STR(&av->cfg_attr, psAttr);
  403: 		AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
  404: 				io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
  405: 
  406: 		CFG_RC_LOCK(cfg);
  407: 		/* find & delete duplicates */
  408: 		if ((d = RB_FIND(tagRC, cfg, av))) {
  409: 			RB_REMOVE(tagRC, cfg, d);
  410: 			SLIST_REMOVE(cfg, d, tagCfg, cfg_next);
  411: 
  412: 			AIT_FREE_VAL(&d->cfg_val);
  413: 			AIT_FREE_VAL(&d->cfg_attr);
  414: 			AIT_FREE_VAL(&d->cfg_sec);
  415: 			io_free(d);
  416: 		}
  417: 
  418: 		SLIST_INSERT_HEAD(cfg, av, cfg_next);
  419: 		RB_INSERT(tagRC, cfg, av);
  420: 		CFG_RC_UNLOCK(cfg);
  421: 	}
  422: 
  423: 	return 0;
  424: }
  425: 

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