File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.13.2.2: download - view: text, annotated - select for diffs - revision graph
Thu Jan 30 08:14:56 2014 UTC (10 years, 4 months ago) by misho
Branches: cfg7_3
Diff to: branchpoint 1.13: preferred, unified
remove invertQueue

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

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