File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / cfger.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Fri Jun 30 08:41:50 2017 UTC (6 years, 11 months ago) by misho
Branches: MAIN
CVS tags: tools3_0, TOOLS2_9, HEAD
ver 2.9

    1: /*************************************************************************
    2:  * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
    3:  *  by Michael Pounov <misho@aitbg.com>
    4:  *
    5:  * $Author: misho $
    6:  * $Id: cfger.c,v 1.5 2017/06/30 08:41:50 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 - 2017
   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: int Verbose;
   50: extern char compiled[], compiledby[], compilehost[];
   51: 
   52: cfg_root_t cfg;
   53: 
   54: 
   55: static void
   56: Usage()
   57: {
   58: 
   59: 	printf(	"CFGer is tool for managment R/W operation with CFGs\n"
   60: 		"=== %s === %s@%s ===\n\n"
   61: 		"  Syntax: cfger [options] <file_config> [data]\n\n"
   62: 		"\t-v\t\tVerbose ...\n"
   63: 		"\t-q\t\tQuiet mode in Get variable mode. Output is only variable value\n"
   64: 		"\t-S\t\tShell script mode, without whitespaces between attribute and value\n"
   65: 		"\t-C\t\tWithout program comment header\n"
   66: 		"\t-J\t\tFilter output, just variables, exclude entire different data from a/v pair\n"
   67: 		"\t-o <output>\tOutput result to file\n"
   68: 		"\t-l\t\tList A/V pairs\n"
   69: 		"\t-c <comment>\tAdds comment on program header of file\n"
   70: 		"\t-s <attr>\tSet A/V pair\n"
   71: 		"\t-g <attr>\tGet value from A/V pair\n"
   72: 		"*\"attr\" format: [section/]attribute\n"
   73: 		"\n", compiled, compiledby, compilehost);
   74: }
   75: 
   76: int
   77: main(int argc, char **argv)
   78: {
   79: 	char ch, *str, m = 0, q = 0, sh = 42, jv = 0, szAttr[STRSIZ], szName[PATH_MAX] = { 0 };
   80: 	int ret = 0;
   81: 	ait_val_t data = AIT_VAL_INITIALIZER(data);
   82: 	FILE *out = stdout;
   83: 	time_t tim;
   84: 	struct tm tm;
   85: 	char cm = 42, szComment[STRSIZ] = { [0 ... STRSIZ - 1] = 0 }, szTim[STRSIZ] = { 0 };
   86: 	ait_val_t *v;
   87: 
   88: 	while ((ch = getopt(argc, argv, "hvqlSJCs:g:o:c:")) != -1)
   89: 		switch (ch) {
   90: 			case 'v':
   91: 				Verbose++;
   92: 				break;
   93: 			case 'q':
   94: 				q = 42;
   95: 				break;
   96: 			case 'S':
   97: 				sh ^= sh;
   98: 				break;
   99: 			case 'C':
  100: 				cm ^= cm;
  101: 				break;
  102: 			case 'J':
  103: 				jv = 42;
  104: 				break;
  105: 			case 'o':
  106: 				strlcpy(szName, optarg, sizeof szName);
  107: 				break;
  108: 			case 'c':
  109: 				strlcpy(szComment, optarg, sizeof szComment);
  110: 				break;
  111: 			case 'l':
  112: 				if (m) {
  113: 					Usage();
  114: 					return 1;
  115: 				} else
  116: 					m = 'l';
  117: 				break;
  118: 			case 's':
  119: 				if (m) {
  120: 					Usage();
  121: 					return 1;
  122: 				} else
  123: 					m = 's';
  124: 				strlcpy(szAttr, optarg, sizeof szAttr);
  125: 				break;
  126: 			case 'g':
  127: 				if (m) {
  128: 					Usage();
  129: 					return 1;
  130: 				} else
  131: 					m = 'g';
  132: 				strlcpy(szAttr, optarg, sizeof szAttr);
  133: 				break;
  134: 			case 'h':
  135: 			default:
  136: 				Usage();
  137: 				return 1;
  138: 		}
  139: 	argc -= optind;
  140: 	argv += optind;
  141: 	if (!argc) {
  142: 		Usage();
  143: 		return 1;
  144: 	}
  145: 	if (argc > 1 && argv[1])
  146: 		AIT_SET_STR(&data, argv[1]);
  147: 
  148: 	if (cfgLoadConfig(*argv, &cfg)) {
  149: 		ELIBERR(cfg);
  150: 		ret = 2;
  151: 		goto end;
  152: 	}
  153: 
  154: 	if (*szName) {
  155: 		out = fopen(szName, "w");
  156: 		if (!out) {
  157: 			printf("Error:: Can't create file %s\n", szName);
  158: 			ret = 2;
  159: 			out = stdout;
  160: 			goto end;
  161: 		}
  162: 	}
  163: 
  164: 	switch (m) {
  165: 		case 'g':
  166: 			if (!q)
  167: 				fprintf(out, "%s = ", szAttr);
  168: 			str = strchr(szAttr, '/');
  169: 			if (str) {
  170: 				*str++ = 0;
  171: 				str = (char*) cfg_getAttribute(&cfg, szAttr, str);
  172: 			} else
  173: 				str = (char*) cfg_getAttribute(&cfg, str, szAttr);
  174: 			fprintf(out, "%s\n", str ? str : "\rError:: Variable not found!");
  175: 			break;
  176: 		case 's':
  177: 			str = strchr(szAttr, '/');
  178: 			if (str) {
  179: 				*str++ = 0;
  180: 				if (AIT_ISEMPTY(&data))
  181: 					cfg_unsetAttribute(&cfg, szAttr, str);
  182: 				else
  183: 					cfg_setAttribute(&cfg, szAttr, str, 
  184: 							AIT_GET_STRZ(&data));
  185: 			} else {
  186: 				if (AIT_ISEMPTY(&data))
  187: 					cfg_unsetAttribute(&cfg, str, szAttr);
  188: 				else
  189: 					cfg_setAttribute(&cfg, str, szAttr, 
  190: 							AIT_GET_STRZ(&data));
  191: 			}
  192: 		case 'l':
  193: 		default:
  194: 			if (cm) {
  195: 				time(&tim);
  196: 				localtime_r(&tim, &tm);
  197: 				strftime(szTim, sizeof szTim, "%Y-%m-%d %H:%M:%S", &tm);
  198: 
  199: 				fprintf(out, "# Auto-generated file %s from cfger at %s\n#\n# %s\n\n", *argv, szTim, szComment);
  200: 			}
  201: 
  202: 			if (jv)
  203: 				cfgWriteConfig(out, &cfg, sh);
  204: 			else {
  205: 				v = cfgWriteLines(out, NULL, NULL, NULL, &cfg);
  206: 				ait_freeVar(&v);
  207: 			}
  208: 
  209: 			break;
  210: 	}
  211: end:
  212: 	if (out != stdout)
  213: 		fclose(out);
  214: 	AIT_FREE_VAL(&data);
  215: 	cfgUnloadConfig(&cfg);
  216: 	return ret;
  217: }

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