File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.11.4.1: download - view: text, annotated - select for diffs - revision graph
Thu Jan 17 13:42:10 2013 UTC (11 years, 3 months ago) by misho
Branches: cfg7_0
Diff to: branchpoint 1.11: preferred, unified
changed apis

    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.11.4.1 2013/01/17 13:42:10 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, 2013
   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: 
   48: 
   49: static inline void
   50: _invertQueue(cfg_root_t * __restrict cfg)
   51: {
   52: 	struct tagCfg *item, *next, *prev = NULL;
   53: 
   54: 	SLIST_FOREACH_SAFE(item, cfg, cfg_next, next) {
   55: 		item->cfg_next.sle_next = prev;
   56: 		prev = item;
   57: 	}
   58: 	cfg->slh_first = prev;
   59: }
   60: 
   61: 
   62: /*
   63:  * cfgReadConfig() - Read file and add new item at config root
   64:  *
   65:  * @f = File resource
   66:  * @cfg = Config root
   67:  * return: -1 error or 0 ok
   68:  */
   69: int
   70: cfgReadConfig(FILE *f, cfg_root_t * __restrict cfg)
   71: {
   72: 	char line[BUFSIZ];
   73: 	struct tagCfg *av = NULL;
   74: 	int flg = 0;
   75: 	char *psAttr, *psVal, szSection[STRSIZ] = { 0 };
   76: 
   77: 	if (!f || !cfg) {
   78: 		cfg_SetErr(EINVAL, "Invalid parameter(s)");
   79: 		return -1;
   80: 	}
   81: 
   82: 	while (!feof(f)) {
   83: 		memset(line, 0, sizeof line);
   84: 		fgets(line, sizeof line - 1, f);
   85: #ifdef SUPPORT_USER_EOF
   86: 		/* check for user end-of-file */
   87: 		if (line[0] == '.' && line[1] == '\n')
   88: 			break;
   89: #endif
   90: 		if (!(psAttr = strpbrk(line, "\r\n"))) {
   91: 			/* skip line, too long */
   92: 			continue;
   93: 		} else {
   94: 			*psAttr = 0;
   95: 			str_Trim(line);
   96: 		}
   97: 
   98: 		if (flg) {
   99: 			/* continues line */
  100: 			if (!av)
  101: 				continue;
  102: 			else
  103: 				psAttr = line + strlen(line) - 1;
  104: 			if (*psAttr == '\\')
  105: 				*psAttr = 0;
  106: 			else
  107: 				flg = 0;
  108: 			/* concat line to value */
  109: 			AIT_SET_STRCAT(&av->cfg_val, line);
  110: 			if (!flg && AIT_ADDR(&av->cfg_val))
  111: 				str_Unquot((char*) AIT_GET_STR(&av->cfg_val));
  112: 			continue;
  113: 		}
  114: 
  115: 		/* *NEW PAIR* alloc new pair element */
  116: 		av = e_malloc(sizeof(struct tagCfg));
  117: 		if (!av) {
  118: 			cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  119: 			return -1;
  120: 		} else {
  121: 			memset(av, 0, sizeof(struct tagCfg));
  122: 			CFG_RC_LOCK(cfg);
  123: 			SLIST_INSERT_HEAD(cfg, av, cfg_next);
  124: 			CFG_RC_UNLOCK(cfg);
  125: 		}
  126: 
  127: 		/* check for continues line */
  128: 		psAttr = line + (*line ? strlen(line) : 1) - 1;
  129: 		if (*psAttr == '\\') {
  130: 			*psAttr = 0;
  131: 			flg = 1;
  132: 		}
  133: 
  134: 		/* check for comment or empty line */
  135: 		if (!*line || *line == '#' || *line == ';') {
  136: 			AIT_SET_STR(&av->cfg_val, line);
  137: 			continue;
  138: 		}
  139: 		/* section */
  140: 		if (*line == '[') {
  141: 			AIT_SET_STR(&av->cfg_val, line);
  142: 			psAttr = line + strlen(line) - 1;
  143: 			if (*psAttr == ']') {
  144: 				*psAttr = 0; 
  145: 				flg = 0;
  146: 				strlcpy(szSection, line + 1, sizeof szSection);
  147: 			} else
  148: 				EDEBUG(7, "Ignore section '%s' ... not found ']'", line);
  149: 			continue;
  150: 		}
  151: 		/* parse pair */
  152: 		if (!(psAttr = strchr(line, '='))) {
  153: 			AIT_SET_STR(&av->cfg_val, line);
  154: 			EDEBUG(7, "Ignore a/v '%s' ... not found '='", line);
  155: 			continue;
  156: 		} else {
  157: 			*psAttr = 0;
  158: 			psVal = psAttr + 1;
  159: 			psAttr = line;
  160: 		}
  161: 
  162: 		/* if exists, added section name to element */
  163: 		if (*szSection) {
  164: 			AIT_SET_STR(&av->cfg_sec, szSection);
  165: 			AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
  166: 					E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
  167: 		}
  168: 
  169: 		str_RTrim(psAttr);
  170: 		str_LTrim(psVal);
  171: 		if (!flg)
  172: 			str_Unquot(psVal);
  173: 		AIT_SET_STR(&av->cfg_val, psVal);
  174: 		AIT_SET_STR(&av->cfg_attr, psAttr);
  175: 		AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
  176: 				E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
  177: 
  178: 		CFG_RC_LOCK(cfg);
  179: 		RB_INSERT(tagRC, cfg, av);
  180: 		CFG_RC_UNLOCK(cfg);
  181: 	}
  182: 
  183: 	return 0;
  184: }
  185: 
  186: /*
  187:  * cfgWriteConfig() - Write config from memory
  188:  *
  189:  * @f = File handle
  190:  * @cfg = Config root
  191:  * @whitespace = Additional whitespace characters to file
  192:  * return: -1 error or 0 ok
  193:  */
  194: int
  195: cfgWriteConfig(FILE *f, cfg_root_t * __restrict cfg, int whitespace)
  196: {
  197: 	struct tagCfg *av;
  198: 	time_t tim;
  199: 	char line[BUFSIZ] = { 0 }, szSection[STRSIZ] = { 0 };
  200: 
  201: 	if (!f || !cfg) {
  202: 		cfg_SetErr(EINVAL, "Invalid parameter(s)");
  203: 		return -1;
  204: 	}
  205: 
  206: 	CFG_RC_LOCK(cfg);
  207: 	_invertQueue(cfg);
  208: 	SLIST_FOREACH(av, cfg, cfg_next) {
  209: 		/* add +1 line for section [] */
  210: 		if (!AIT_ISEMPTY(&av->cfg_sec) && AIT_ADDR(&av->cfg_sec) && 
  211: 				strcmp(AIT_GET_STR(&av->cfg_sec), szSection)) {
  212: 			strlcpy(szSection, AIT_GET_STR(&av->cfg_sec), sizeof szSection);
  213: 			if (!cfg_Write(f, "\n[%s]\n", AIT_GET_STR(&av->cfg_sec))) {
  214: 				LOGERR;
  215: 				CFG_RC_UNLOCK(cfg);
  216: 				return -1;
  217: 			}
  218: 		}
  219: 		if (AIT_ISEMPTY(&av->cfg_sec) && *szSection) {
  220: 			memset(szSection, 0, sizeof szSection);
  221: 			if (!cfg_Write(f, "\n[]\n")) {
  222: 				LOGERR;
  223: 				CFG_RC_UNLOCK(cfg);
  224: 				return -1;
  225: 			}
  226: 		}
  227: 
  228: 		/* build line */
  229: 		memset(line, 0, sizeof line);
  230: 		if (!AIT_ISEMPTY(&av->cfg_attr) && AIT_TYPE(&av->cfg_attr) == string) {
  231: 			strlcpy(line, AIT_GET_STRZ(&av->cfg_attr), sizeof line);
  232: 			if (whitespace)
  233: 				strlcat(line, " = ", sizeof line);
  234: 			else
  235: 				strlcat(line, "=", sizeof line);
  236: 		}
  237: 		if (!AIT_ISEMPTY(&av->cfg_val) && AIT_TYPE(&av->cfg_val) == string)
  238: 			strlcat(line, AIT_GET_STRZ(&av->cfg_val), sizeof line);
  239: 
  240: 		/* write */
  241: 		if (!cfg_Write(f, "%s\n", line)) {
  242: 			LOGERR;
  243: 			_invertQueue(cfg);
  244: 			CFG_RC_UNLOCK(cfg);
  245: 			return -1;
  246: 		}
  247: 	}
  248: 	_invertQueue(cfg);
  249: 	CFG_RC_UNLOCK(cfg);
  250: 
  251: 	if (whitespace) {
  252: 		time(&tim);
  253: 		memset(line, 0, sizeof line);
  254: 		strftime(line, sizeof line, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
  255: 		cfg_Write(f, "\n## Config was saved at :: %s ##\n", line);
  256: 	}
  257: 
  258: 	return 0;
  259: }
  260: 
  261: /*
  262:  * cfgConcatConfig() - Concat two configs into one
  263:  *
  264:  * @cfg = Config root
  265:  * @add_cfg = Concated config will be destroy after merge
  266:  * return: -1 error or 0 ok
  267:  */
  268: int
  269: cfgConcatConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
  270: {
  271: 	struct tagCfg *item;
  272: 
  273: 	if (!cfg || !add_cfg)
  274: 		return -1;
  275: 
  276: 	CFG_RC_LOCK(add_cfg);
  277: 	CFG_RC_LOCK(cfg);
  278: 
  279: 	/* concat lists */
  280: 	for (item = SLIST_FIRST(cfg); SLIST_NEXT(item, cfg_next); item = SLIST_NEXT(item, cfg_next));
  281: 	SLIST_NEXT(item, cfg_next) = SLIST_FIRST(add_cfg);
  282: 
  283: 	/* concat red-black trees */
  284: 	SLIST_FOREACH(item, add_cfg, cfg_next)
  285: 		RB_INSERT(tagRC, cfg, item);
  286: 
  287: 	CFG_RC_UNLOCK(cfg);
  288: 
  289: 	add_cfg->slh_first = NULL;
  290: 	add_cfg->rbh_root = NULL;
  291: 	CFG_RC_UNLOCK(add_cfg);
  292: 	pthread_mutex_destroy(&add_cfg->rc_mtx);
  293: 	return 0;
  294: }
  295: 
  296: /*
  297:  * cfgMergeConfig() - Marge two list in one cfg and destroy add_cfg
  298:  *
  299:  * @cfg = Config root of main list
  300:  * @add_cfg = Merged config will be destroy after merge
  301:  * return: -1 error or 0 ok
  302:  */
  303: int
  304: cfgMergeConfig(cfg_root_t * __restrict cfg, cfg_root_t * __restrict add_cfg)
  305: {
  306: 	struct tagCfg *item, *merge, *add_next, *next;
  307: 	int flg;
  308: 
  309: 	if (!cfg || !add_cfg)
  310: 		return -1;
  311: 
  312: 	CFG_RC_LOCK(add_cfg);
  313: 	CFG_RC_LOCK(cfg);
  314: 
  315: 	/* merge lists */
  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: 				flg = 1;
  321: 				break;
  322: 			}
  323: 			if (!AIT_ISEMPTY(&merge->cfg_sec) && !AIT_ISEMPTY(&item->cfg_sec) && 
  324: 					AIT_ADDR(&merge->cfg_sec) && AIT_ADDR(&item->cfg_sec) &&
  325: 					!strcmp(AIT_GET_STR(&merge->cfg_sec), AIT_GET_STR(&item->cfg_sec))) {
  326: 				flg = 1;
  327: 				break;
  328: 			}
  329: 		}
  330: 
  331: 		if (!flg)
  332: 			SLIST_INSERT_HEAD(cfg, item, cfg_next);
  333: 		else
  334: 			SLIST_INSERT_AFTER(merge, item, cfg_next);
  335: 		RB_INSERT(tagRC, cfg, item);
  336: 	}
  337: 
  338: 	CFG_RC_UNLOCK(cfg);
  339: 
  340: 	add_cfg->slh_first = NULL;
  341: 	add_cfg->rbh_root = NULL;
  342: 	CFG_RC_UNLOCK(add_cfg);
  343: 	pthread_mutex_destroy(&add_cfg->rc_mtx);
  344: 	return 0;
  345: }
  346: 
  347: /*
  348:  * cfgReadLines() - Read custom lines and add new item at config root
  349:  *
  350:  * @f = File resource
  351:  * @delim = Custom delimiter, if =NULL default is '='
  352:  * @end = Custom user end of file, if =NULL default is EOF
  353:  * @cfg = Config root
  354:  * return: -1 error or 0 ok
  355:  */
  356: int
  357: cfgReadLines(FILE *f, const char *delim, const char *end, cfg_root_t * __restrict cfg)
  358: {
  359: 	char line[BUFSIZ];
  360: 	struct tagCfg *d, *av = NULL;
  361: 	char *p, *psSec, *psAttr, *psVal;
  362: 
  363: 	if (!cfg)
  364: 		return -1;
  365: 	if (!delim)
  366: 		delim = ATR_LINES_DELIM;
  367: 
  368: 	while (!feof(f)) {
  369: 		psSec = psAttr = psVal = NULL;
  370: 		memset(line, 0, sizeof line);
  371: 		fgets(line, sizeof line - 1, f);
  372: 		/* check for user end-of-file */
  373: 		if (strspn(line, end))
  374: 			break;
  375: 
  376: 		if (!(psAttr = strpbrk(line, "\r\n"))) {
  377: 			/* skip line, too long */
  378: 			continue;
  379: 		} else {
  380: 			*psAttr = 0;
  381: 			str_Trim(line);
  382: 			if (!*line)
  383: 				continue;
  384: 		}
  385: 
  386: 		if (!av_MakeExt(line, delim, &p, &psVal))
  387: 			continue;
  388: 		else {
  389: 			str_RTrim(p);
  390: 			str_LTrim(psVal);
  391: 		}
  392: 		if (!av_MakeExt(p, SEC_LINES_DELIM, &psSec, &psAttr))
  393: 			psAttr = p;
  394: 
  395: 		/* *NEW PAIR* alloc new pair element */
  396: 		av = e_malloc(sizeof(struct tagCfg));
  397: 		if (!av) {
  398: 			LOGERR;
  399: 			return -1;
  400: 		} else
  401: 			memset(av, 0, sizeof(struct tagCfg));
  402: 
  403: 		if (psSec) {
  404: 			AIT_SET_STR(&av->cfg_sec, psSec);
  405: 			AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
  406: 					E_ALIGN(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
  407: 		}
  408: 		if (psVal)
  409: 			AIT_SET_STR(&av->cfg_val, psVal);
  410: 		AIT_SET_STR(&av->cfg_attr, psAttr);
  411: 		AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
  412: 				E_ALIGN(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
  413: 
  414: 		CFG_RC_LOCK(cfg);
  415: 		/* find & delete duplicates */
  416: 		if ((d = RB_FIND(tagRC, cfg, av))) {
  417: 			RB_REMOVE(tagRC, cfg, d);
  418: 			SLIST_REMOVE(cfg, d, tagCfg, cfg_next);
  419: 
  420: 			AIT_FREE_VAL(&d->cfg_val);
  421: 			AIT_FREE_VAL(&d->cfg_attr);
  422: 			AIT_FREE_VAL(&d->cfg_sec);
  423: 			e_free(d);
  424: 		}
  425: 
  426: 		SLIST_INSERT_HEAD(cfg, av, cfg_next);
  427: 		RB_INSERT(tagRC, cfg, av);
  428: 		CFG_RC_UNLOCK(cfg);
  429: 	}
  430: 
  431: 	return 0;
  432: }
  433: 
  434: /*
  435:  * cfgWriteLines() - Write custom lines and export data to variable
  436:  *
  437:  * @f = File resource
  438:  * @delim = Custom delimiter, if =NULL default is '='
  439:  * @eol = End of line string, if =NULL default is "\n"
  440:  * @section = Export only section, if =NULL default is all
  441:  * @cfg = Config root
  442:  * return: =NULL error or !=NULL exported data, must be free after use with ait_freeVar()
  443:  */
  444: ait_val_t *
  445: cfgWriteLines(FILE *f, const char *delim, const char *eol, const char *section, cfg_root_t * __restrict cfg)
  446: {
  447: 	ait_val_t *v = NULL;
  448: 	struct tagCfg *av;
  449: 
  450: 	if (!cfg)
  451: 		return NULL;
  452: 	if (!delim)
  453: 		delim = ATR_LINES_DELIM;
  454: 	if (!eol)
  455: 		eol = EOL_LINES_DELIM;
  456: 	if (!(v = ait_allocVar())) {
  457: 		cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  458: 		return NULL;
  459: 	} else
  460: 		AIT_INIT_VAL2(v, string);
  461: 
  462: 	SLIST_FOREACH(av, cfg, cfg_next) {
  463: 		if (AIT_ISEMPTY(&av->cfg_attr))
  464: 			continue;
  465: 		if (section) {
  466: 			if (!AIT_ISEMPTY(&av->cfg_sec) && *section)
  467: 				continue;
  468: 			if (strcmp(section, AIT_GET_STR(&av->cfg_sec)))
  469: 				continue;
  470: 		}
  471: 
  472: 		if (!AIT_ISEMPTY(&av->cfg_sec)) {
  473: 			AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_sec));
  474: 			AIT_SET_STRCAT(v, SEC_LINES_DELIM);
  475: 		}
  476: 		AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_attr));
  477: 		AIT_SET_STRCAT(v, delim);
  478: 		if (!AIT_ISEMPTY(&av->cfg_val))
  479: 			AIT_SET_STRCAT(v, AIT_GET_STR(&av->cfg_val));
  480: 		AIT_SET_STRCAT(v, eol);
  481: 	}
  482: 
  483: 	if (f)
  484: 		fputs(AIT_GET_STR(v), f);
  485: 	return v;
  486: }

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