File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.10.2.3: download - view: text, annotated - select for diffs - revision graph
Thu Aug 30 14:16:51 2012 UTC (11 years, 9 months ago) by misho
Branches: cfg5_5
Diff to: branchpoint 1.10: preferred, unified
shuffle code in cfgReadLines ... ability to read 'section'

    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.10.2.3 2012/08/30 14:16: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: 			strlcpy(line, AIT_GET_STRZ(&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_STRZ(&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;
  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: 
  318: 	/* merge lists */
  319: 	SLIST_FOREACH_SAFE(item, add_cfg, cfg_next, add_next) {
  320: 		flg = 0;
  321: 		SLIST_FOREACH_SAFE(merge, cfg, cfg_next, next) {
  322: 			if (AIT_ISEMPTY(&merge->cfg_sec) && AIT_ISEMPTY(&item->cfg_sec)) {
  323: 				flg = 1;
  324: 				break;
  325: 			}
  326: 			if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) && 
  327: 					AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
  328: 					!strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
  329: 				flg = 1;
  330: 				break;
  331: 			}
  332: 		}
  333: 
  334: 		if (!flg)
  335: 			SLIST_INSERT_HEAD(cfg, item, cfg_next);
  336: 		else
  337: 			SLIST_INSERT_AFTER(merge, item, cfg_next);
  338: 		RB_INSERT(tagRC, cfg, item);
  339: 	}
  340: 
  341: 	CFG_RC_UNLOCK(cfg);
  342: 	CFG_RC_UNLOCK(add_cfg);
  343: 
  344: 	add_cfg->slh_first = NULL;
  345: 	add_cfg->rbh_root = NULL;
  346: 	pthread_mutex_destroy(&add_cfg->rc_mtx);
  347: 	return 0;
  348: }
  349: 
  350: /*
  351:  * cfgReadLines() - Read custom lines and add new item at config root
  352:  *
  353:  * @f = File resource
  354:  * @delim = Custom delimiter, if =NULL default is '='
  355:  * @end = Custom user end of file, if =NULL default is EOF
  356:  * @cfg = Config root
  357:  * return: -1 error or 0 ok
  358:  */
  359: int
  360: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
  361: {
  362: 	char line[BUFSIZ];
  363: 	struct tagCfg *d, *av = NULL;
  364: 	char *p, *psSec, *psAttr, *psVal;
  365: 
  366: 	if (!cfg)
  367: 		return -1;
  368: 	if (!delim)
  369: 		delim = ATR_LINES_DELIM;
  370: 
  371: 	while (!feof(f)) {
  372: 		psSec = psAttr = psVal = NULL;
  373: 		memset(line, 0, sizeof line);
  374: 		fgets(line, sizeof line - 1, f);
  375: 		/* check for user end-of-file */
  376: 		if (strspn(line, end))
  377: 			break;
  378: 
  379: 		if (!(psAttr = strpbrk(line, "\r\n"))) {
  380: 			/* skip line, too long */
  381: 			continue;
  382: 		} else {
  383: 			*psAttr = 0;
  384: 			io_TrimStr(line);
  385: 			if (!*line)
  386: 				continue;
  387: 		}
  388: 
  389: 		if (!io_MakeAV2(line, delim, &p, &psVal))
  390: 			continue;
  391: 		else {
  392: 			io_RTrimStr(p);
  393: 			io_LTrimStr(psVal);
  394: 		}
  395: 		if (!io_MakeAV2(p, SEC_LINES_DELIM, &psSec, &psAttr))
  396: 			psAttr = p;
  397: 
  398: 		/* *NEW PAIR* alloc new pair element */
  399: 		av = io_malloc(sizeof(struct tagCfg));
  400: 		if (!av) {
  401: 			LOGERR;
  402: 			return -1;
  403: 		} else
  404: 			memset(av, 0, sizeof(struct tagCfg));
  405: 
  406: 		if (psSec) {
  407: 			AIT_SET_STR(&av->cfg_sec, psSec);
  408: 			AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
  409: 					io_align(AIT_LEN(&av->cfg_sec) - 1, 1) / 2);
  410: 		}
  411: 		if (psVal)
  412: 			AIT_SET_STR(&av->cfg_val, psVal);
  413: 		AIT_SET_STR(&av->cfg_attr, psAttr);
  414: 		AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
  415: 				io_align(AIT_LEN(&av->cfg_attr) - 1, 1) / 2);
  416: 
  417: 		CFG_RC_LOCK(cfg);
  418: 		/* find & delete duplicates */
  419: 		if ((d = RB_FIND(tagRC, cfg, av))) {
  420: 			RB_REMOVE(tagRC, cfg, d);
  421: 			SLIST_REMOVE(cfg, d, tagCfg, cfg_next);
  422: 
  423: 			AIT_FREE_VAL(&d->cfg_val);
  424: 			AIT_FREE_VAL(&d->cfg_attr);
  425: 			AIT_FREE_VAL(&d->cfg_sec);
  426: 			io_free(d);
  427: 		}
  428: 
  429: 		SLIST_INSERT_HEAD(cfg, av, cfg_next);
  430: 		RB_INSERT(tagRC, cfg, av);
  431: 		CFG_RC_UNLOCK(cfg);
  432: 	}
  433: 
  434: 	return 0;
  435: }
  436: 
  437: /*
  438:  * cfgWriteLines() - Write custom lines and export data to variable
  439:  *
  440:  * @f = File resource
  441:  * @delim = Custom delimiter, if =NULL default is '='
  442:  * @eol = End of line string, if =NULL default is "\n"
  443:  * @section = Export only section, if =NULL default is all
  444:  * @cfg = Config root
  445:  * return: =NULL error or !=NULL exported data, must be free after use with io_freeVar()
  446:  */
  447: ait_val_t *
  448: cfgWriteLines(FILE *f, const char *delim, const char *eol, const char *section, cfg_root_t * __restrict cfg)
  449: {
  450: 	ait_val_t *v = NULL;
  451: 	struct tagCfg *av;
  452: 
  453: 	if (!cfg)
  454: 		return NULL;
  455: 	if (!delim)
  456: 		delim = ATR_LINES_DELIM;
  457: 	if (!eol)
  458: 		eol = EOL_LINES_DELIM;
  459: 	if (!(v = io_allocVar())) {
  460: 		cfg_SetErr(io_GetErrno(), "%s", io_GetError());
  461: 		return NULL;
  462: 	} else
  463: 		AIT_INIT_VAL2(v, string);
  464: 
  465: 	SLIST_FOREACH(av, cfg, cfg_next) {
  466: 		if (AIT_ISEMPTY(&av->cfg_attr))
  467: 			continue;
  468: 		if (section) {
  469: 			if (!AIT_ISEMPTY(&av->cfg_sec) && *section)
  470: 				continue;
  471: 			if (strcmp(section, AIT_GET_STR(&av->cfg_sec)))
  472: 				continue;
  473: 		}
  474: 
  475: 		if (!AIT_ISEMPTY(&av->cfg_sec)) {
  476: 			AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_sec));
  477: 			AIT_SET_STRCAT(v, SEC_LINES_DELIM);
  478: 		}
  479: 		AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_attr));
  480: 		AIT_SET_STRCAT(v, delim);
  481: 		if (!AIT_ISEMPTY(&av->cfg_val))
  482: 			AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val));
  483: 		AIT_SET_STRCAT(v, eol);
  484: 	}
  485: 
  486: 	if (f)
  487: 		fputs(AIT_GET_STR(v), f);
  488: 	return v;
  489: }

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