File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / cfger.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Thu Jan 30 00:55:19 2014 UTC (10 years, 9 months ago) by misho
Branches: tools2_0
finish cfger

/*************************************************************************
 * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
 *  by Michael Pounov <misho@aitbg.com>
 *
 * $Author: misho $
 * $Id: cfger.c,v 1.1.2.2 2014/01/30 00:55:19 misho Exp $
 *
 *************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2014
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"


int Verbose;
extern char compiled[], compiledby[], compilehost[];

cfg_root_t cfg;


static void
Usage()
{

	printf(	"CFGer is tool for managment R/W operation with CFGs\n"
		"=== %s === %s@%s ===\n\n"
		"  Syntax: cfger [options] <file_config> [data]\n\n"
		"\t-v\t\tVerbose ...\n"
		"\t-q\t\tQuiet mode\n"
		"\t-o <output>\tOutput result to file\n"
		"\t-l <attr>\tList A/V pair\n"
		"\t-s <attr>\tSet A/V pair\n"
		"\t-g <attr>\tGet value from A/V pair\n"
		"*\"attr\" format: [section/]attribute\n"
		"\n", compiled, compiledby, compilehost);
}

int
main(int argc, char **argv)
{
	char ch, *str, m = 0, q = 0, szAttr[STRSIZ], szName[PATH_MAX] = { 0 };
	int ret = 0;
	ait_val_t data = AIT_VAL_INITIALIZER(data);
	FILE *out = stdout;

	while ((ch = getopt(argc, argv, "hvql:s:g:o:")) != -1)
		switch (ch) {
			case 'v':
				Verbose++;
				break;
			case 'q':
				q = 42;
				break;
			case 'o':
				strlcpy(szName, optarg, sizeof szName);
				break;
			case 'l':
				if (m) {
					Usage();
					return 1;
				} else
					m = 'l';
				strlcpy(szAttr, optarg, sizeof szAttr);
				break;
			case 's':
				if (m) {
					Usage();
					return 1;
				} else
					m = 's';
				strlcpy(szAttr, optarg, sizeof szAttr);
				break;
			case 'g':
				if (m) {
					Usage();
					return 1;
				} else
					m = 'g';
				strlcpy(szAttr, optarg, sizeof szAttr);
				break;
			case 'h':
			default:
				Usage();
				return 1;
		}
	argc -= optind;
	argv += optind;
	if (!argc) {
		Usage();
		return 1;
	}
	if (argc > 1 && argv[1])
		AIT_SET_STR(&data, argv[1]);

	if (cfgLoadConfig(*argv, &cfg)) {
		ELIBERR(cfg);
		ret = 2;
		goto end;
	}

	if (*szName) {
		out = fopen(szName, "w");
		if (!out) {
			printf("Error:: Can't create file %s\n", szName);
			ret = 2;
			out = stdout;
			goto end;
		}
	}

	switch (m) {
		case 'g':
			if (!q)
				fprintf(out, "%s = ", szAttr);
			str = strchr(szAttr, '/');
			if (str) {
				*str++ = 0;
				str = (char*) cfg_getAttribute(&cfg, szAttr, str);
			} else
				str = (char*) cfg_getAttribute(&cfg, str, szAttr);
			fprintf(out, "%s\n", str ? str : "\rError:: Variable not found!");
			break;
		case 's':
			str = strchr(szAttr, '/');
			if (str) {
				*str++ = 0;
				if (AIT_ISEMPTY(&data))
					cfg_unsetAttribute(&cfg, szAttr, str);
				else
					cfg_setAttribute(&cfg, szAttr, str, 
							AIT_GET_STRZ(&data));
			} else {
				if (AIT_ISEMPTY(&data))
					cfg_unsetAttribute(&cfg, str, szAttr);
				else
					cfg_setAttribute(&cfg, str, szAttr, 
							AIT_GET_STRZ(&data));
			}
		case 'l':
		default:
			cfgWriteConfig(out, &cfg, 42);
			break;
	}
end:
	if (out != stdout)
		fclose(out);
	AIT_FREE_VAL(&data);
	cfgUnloadConfig(&cfg);
	return ret;
}

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