File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / parse.c
Revision 1.1.1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Mon Oct 13 17:29:18 2008 UTC (15 years, 8 months ago) by misho
Branches: cfg3_0
Diff to: branchpoint 1.1.1.1: preferred, unified
security fixes

    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.1.1.1.2.3 2008/10/13 17:29:18 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: #include "aitcfg.h"
   11: #include "tools.h"
   12: 
   13: 
   14: // cfgDbg() Debug/Log operation
   15: static inline int cfgDbg(FILE *f, char *fmt, ...)
   16: {
   17: 	int ret = 0;
   18: 	va_list lst;
   19: 
   20: 	va_start(lst, fmt);
   21: 	ret = vfprintf(f, fmt, lst);
   22: 	va_end(lst);
   23: 
   24: 	return ret;
   25: }
   26: 
   27: // -----------------------------------------
   28: 
   29: /*
   30:  * ReadConfig() Read from file and add new item to config list
   31:  * @f = file resource
   32:  * @cfg = Head list element
   33:  * return: 0 ok; -1 error:: can`t allocate memory
   34: */
   35: int ReadConfig(FILE *f, sl_config * __restrict cfg)
   36: {
   37: 	u_char szLine[MAX_STR + 1];
   38: 	u_char szSection[MAX_STR + 1], *psAttr, *psVal;
   39: 	int pos;
   40: 	struct tagPair *av;
   41: 
   42: 	memset(szSection, 0, MAX_STR + 1);
   43: 	while (!feof(f)) {
   44: 		memset(szLine, 0, MAX_STR + 1);
   45: 		fgets((char*) szLine, MAX_STR, f);
   46: 		trim(szLine);
   47: #ifdef __DEBUG
   48: 		cfgDbg(stdout, "DEBUG:: RAW |%s|\n", szLine);
   49: #endif
   50: 
   51: 		// End of config
   52: 		if (*szLine == '.')
   53: 			break;
   54: 		// Comment
   55: 		if (*szLine == '#' || *szLine == ';' || !*szLine)
   56: 			continue;
   57: 
   58: #ifdef __DEBUG
   59: 		cfgDbg(stdout, "DEBUG:: Clean |%s|\n", szLine);
   60: #endif
   61: 
   62: 		// Section
   63: 		if (*szLine == '[') {
   64: 			pos = strlen((char*) szLine) - 1;
   65: 			if (szLine[pos] != ']') {
   66: #ifdef __DEBUG
   67: 				cfgDbg(stdout, "WARNING:: Ignore section %s ... not closed breket\n", szLine);
   68: #endif
   69: 			} else {
   70: 				szLine[pos] = 0; 
   71: 				strncpy((char*) szSection, (char*) szLine + 1, MAX_STR);
   72: #ifdef __DEBUG
   73: 				cfgDbg(stdout, "DEBUG:: Section %s\n", szSection);
   74: #endif
   75: 			}
   76: 			continue;
   77: 		}
   78: 
   79: 		// Devide pairs
   80: 		pos = strchr((char*) szLine, '=') ? strchr((char*) szLine, '=') - (char*) szLine : 0;
   81: 		if (!pos) {
   82: #ifdef __DEBUG
   83: 			cfgDbg(stdout, "WARNING:: Ignore a/v %s ... format error!\n", szLine);
   84: #endif
   85: 			continue;
   86: 		} else {
   87: 			av = malloc(sizeof(struct tagPair));
   88: 			if (!av) {
   89: 				LOGERR;
   90: 				return -1;
   91: 			} else {
   92: 				// added new element
   93: 				av->sle_next = cfg->slh_first;
   94: 				cfg->slh_first = av;
   95: 			}
   96: 			// added section name to element
   97: 			if (*szSection) {
   98: 				av->psSection = malloc(strlen((char*) szSection) + 1);
   99: 				if (!av->psSection) {
  100: 					LOGERR;
  101: 					free(av);
  102: 					return -1;
  103: 				} else
  104: 					strlcpy((char*) av->psSection, (char*) szSection, strlen((char*) szSection) + 1);
  105: 			} else
  106: 				av->psSection = NULL;
  107: 
  108: 			psAttr = szLine;
  109: 			psVal = (szLine + pos + 1);
  110: 			szLine[pos] = 0;
  111: 			rtrim(psAttr);
  112: 			ltrim(psVal);
  113: #ifdef __DEBUG
  114: 			cfgDbg(stdout, "DEBUG:: Attr(%p) ->%s size=%d Value(%p) ->%s size=%d\n", 
  115: 					psAttr, psAttr, strlen((char*) psAttr), psVal, psVal, strlen((char*) psVal));
  116: #endif
  117: 			// added attribute to element
  118: 			av->psAttribute = malloc(strlen((char*) psAttr) + 1);
  119: 			if (!av->psAttribute) {
  120: 				LOGERR;
  121: 				free(av->psSection);
  122: 				free(av);
  123: 				return -1;
  124: 			} else
  125: 				strlcpy((char*) av->psAttribute, (char*) psAttr, strlen((char*) psAttr) + 1);
  126: 			// added value to element
  127: 			av->psValue = malloc(strlen((char*) psVal) + 1);
  128: 			if (!av->psValue) {
  129: 				LOGERR;
  130: 				free(av->psAttribute);
  131: 				free(av->psSection);
  132: 				free(av);
  133: 				return -1;
  134: 			} else
  135: 				strlcpy((char*) av->psValue, (char*) psVal, strlen((char*) psVal) + 1);
  136: 		}
  137: 	}
  138: 
  139: 	return 0;
  140: }
  141: 
  142: /*
  143:  * WriteConfig() Write to file from items in config list
  144:  * @f = file resource
  145:  * @cfg = Head list element
  146:  * return: 0 ok; -1 error:: can`t write to file
  147: */
  148: int WriteConfig(FILE *f, sl_config * __restrict cfg)
  149: {
  150: 	struct tagPair *av;
  151: 	time_t tim;
  152: 	char szTime[MAX_STR + 1];
  153: 	u_char szSection[MAX_STR + 1];
  154: 
  155: 	bzero(szSection, MAX_STR + 1);
  156: 
  157: 	bzero(szTime, MAX_STR + 1);
  158: 	time(&tim);
  159: 	strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
  160: 	if (!cfgDbg(f, "## Write Config :: %s\n#\n", szTime)) {
  161: 		LOGERR;
  162: 		return -1;
  163: 	}
  164: 
  165: 	for (av = cfg->slh_first; av; av = av->sle_next) {
  166: 		if (av->psSection && strcmp((char*) av->psSection, (char*) szSection)) {
  167: 			strlcpy((char*) szSection, (char*) av->psSection, MAX_STR + 1);
  168: 			if (!cfgDbg(f, "\n[%s]\n", av->psSection)) {
  169: 				LOGERR;
  170: 				return -1;
  171: 			}
  172: 		}
  173: 		if (!av->psSection && *szSection) {
  174: 			bzero(szSection, MAX_STR + 1);
  175: 			if (!cfgDbg(f, "\n[]\n")) {
  176: 				LOGERR;
  177: 				return -1;
  178: 			}
  179: 		}
  180: 
  181: 		if (!cfgDbg(f, "%s = %s\n", av->psAttribute, av->psValue)) {
  182: 			LOGERR;
  183: 			return -1;
  184: 		}
  185: 	}
  186: 
  187: 	bzero(szTime, MAX_STR + 1);
  188: 	time(&tim);
  189: 	strftime(szTime, MAX_STR, "(UTC) %Y-%m-%d %H:%M:%S", gmtime(&tim));
  190: 	if (!cfgDbg(f, "\n#\n## Done. :: %s\n", szTime)) {
  191: 		LOGERR;
  192: 		return -1;
  193: 	}
  194: 
  195: 	return 0;
  196: }

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