File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.19.4.1: download - view: text, annotated - select for diffs - revision graph
Mon Dec 5 22:02:11 2022 UTC (17 months, 3 weeks ago) by misho
Branches: cfg9_2
Diff to: branchpoint 1.19: preferred, unified
satisfy GNU cross compiler for unused return result

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

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