File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / cfger.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Wed Jun 28 15:19:32 2017 UTC (6 years, 11 months ago) by misho
Branches: MAIN
CVS tags: tools2_8, TOOLS2_7, HEAD
ver 2.7

    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.3 2017/06/28 15:19:32 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\n"
   64: 		"\t-S\t\tShell script mode\n"
   65: 		"\t-o <output>\tOutput result to file\n"
   66: 		"\t-l\t\tList A/V pairs\n"
   67: 		"\t-s <attr>\tSet A/V pair\n"
   68: 		"\t-g <attr>\tGet value from A/V pair\n"
   69: 		"*\"attr\" format: [section/]attribute\n"
   70: 		"\n", compiled, compiledby, compilehost);
   71: }
   72: 
   73: int
   74: main(int argc, char **argv)
   75: {
   76: 	char ch, *str, m = 0, q = 0, sh = 42, szAttr[STRSIZ], szName[PATH_MAX] = { 0 };
   77: 	int ret = 0;
   78: 	ait_val_t data = AIT_VAL_INITIALIZER(data);
   79: 	FILE *out = stdout;
   80: 
   81: 	while ((ch = getopt(argc, argv, "hvqlSs:g:o:")) != -1)
   82: 		switch (ch) {
   83: 			case 'v':
   84: 				Verbose++;
   85: 				break;
   86: 			case 'q':
   87: 				q = 42;
   88: 				break;
   89: 			case 'S':
   90: 				sh ^= sh;
   91: 				break;
   92: 			case 'o':
   93: 				strlcpy(szName, optarg, sizeof szName);
   94: 				break;
   95: 			case 'l':
   96: 				if (m) {
   97: 					Usage();
   98: 					return 1;
   99: 				} else
  100: 					m = 'l';
  101: 				break;
  102: 			case 's':
  103: 				if (m) {
  104: 					Usage();
  105: 					return 1;
  106: 				} else
  107: 					m = 's';
  108: 				strlcpy(szAttr, optarg, sizeof szAttr);
  109: 				break;
  110: 			case 'g':
  111: 				if (m) {
  112: 					Usage();
  113: 					return 1;
  114: 				} else
  115: 					m = 'g';
  116: 				strlcpy(szAttr, optarg, sizeof szAttr);
  117: 				break;
  118: 			case 'h':
  119: 			default:
  120: 				Usage();
  121: 				return 1;
  122: 		}
  123: 	argc -= optind;
  124: 	argv += optind;
  125: 	if (!argc) {
  126: 		Usage();
  127: 		return 1;
  128: 	}
  129: 	if (argc > 1 && argv[1])
  130: 		AIT_SET_STR(&data, argv[1]);
  131: 
  132: 	if (cfgLoadConfig(*argv, &cfg)) {
  133: 		ELIBERR(cfg);
  134: 		ret = 2;
  135: 		goto end;
  136: 	}
  137: 
  138: 	if (*szName) {
  139: 		out = fopen(szName, "w");
  140: 		if (!out) {
  141: 			printf("Error:: Can't create file %s\n", szName);
  142: 			ret = 2;
  143: 			out = stdout;
  144: 			goto end;
  145: 		}
  146: 	}
  147: 
  148: 	switch (m) {
  149: 		case 'g':
  150: 			if (!q)
  151: 				fprintf(out, "%s = ", szAttr);
  152: 			str = strchr(szAttr, '/');
  153: 			if (str) {
  154: 				*str++ = 0;
  155: 				str = (char*) cfg_getAttribute(&cfg, szAttr, str);
  156: 			} else
  157: 				str = (char*) cfg_getAttribute(&cfg, str, szAttr);
  158: 			fprintf(out, "%s\n", str ? str : "\rError:: Variable not found!");
  159: 			break;
  160: 		case 's':
  161: 			str = strchr(szAttr, '/');
  162: 			if (str) {
  163: 				*str++ = 0;
  164: 				if (AIT_ISEMPTY(&data))
  165: 					cfg_unsetAttribute(&cfg, szAttr, str);
  166: 				else
  167: 					cfg_setAttribute(&cfg, szAttr, str, 
  168: 							AIT_GET_STRZ(&data));
  169: 			} else {
  170: 				if (AIT_ISEMPTY(&data))
  171: 					cfg_unsetAttribute(&cfg, str, szAttr);
  172: 				else
  173: 					cfg_setAttribute(&cfg, str, szAttr, 
  174: 							AIT_GET_STRZ(&data));
  175: 			}
  176: 		case 'l':
  177: 		default:
  178: 			cfgWriteConfig(out, &cfg, sh);
  179: 			break;
  180: 	}
  181: end:
  182: 	if (out != stdout)
  183: 		fclose(out);
  184: 	AIT_FREE_VAL(&data);
  185: 	cfgUnloadConfig(&cfg);
  186: 	return ret;
  187: }

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