File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / cfger.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Feb 5 15:44:05 2014 UTC (10 years, 4 months ago) by misho
Branches: MAIN
CVS tags: tools2_7, tools2_6, tools2_5, tools2_4, tools2_3, tools2_2, tools2_1, TOOLS2_6, TOOLS2_5, TOOLS2_4, TOOLS2_3, TOOLS2_2, TOOLS2_1, TOOLS2_0, HEAD
version 2.0

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

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