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, 10 months ago) by misho
Branches: MAIN
CVS tags: tools3_0, TOOLS2_9, HEAD
ver 2.9

/*************************************************************************
 * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
 *  by Michael Pounov <misho@aitbg.com>
 *
 * $Author: misho $
 * $Id: cfger.c,v 1.5 2017/06/30 08:41:50 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 - 2017
	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 in Get variable mode. Output is only variable value\n"
		"\t-S\t\tShell script mode, without whitespaces between attribute and value\n"
		"\t-C\t\tWithout program comment header\n"
		"\t-J\t\tFilter output, just variables, exclude entire different data from a/v pair\n"
		"\t-o <output>\tOutput result to file\n"
		"\t-l\t\tList A/V pairs\n"
		"\t-c <comment>\tAdds comment on program header of file\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, sh = 42, jv = 0, szAttr[STRSIZ], szName[PATH_MAX] = { 0 };
	int ret = 0;
	ait_val_t data = AIT_VAL_INITIALIZER(data);
	FILE *out = stdout;
	time_t tim;
	struct tm tm;
	char cm = 42, szComment[STRSIZ] = { [0 ... STRSIZ - 1] = 0 }, szTim[STRSIZ] = { 0 };
	ait_val_t *v;

	while ((ch = getopt(argc, argv, "hvqlSJCs:g:o:c:")) != -1)
		switch (ch) {
			case 'v':
				Verbose++;
				break;
			case 'q':
				q = 42;
				break;
			case 'S':
				sh ^= sh;
				break;
			case 'C':
				cm ^= cm;
				break;
			case 'J':
				jv = 42;
				break;
			case 'o':
				strlcpy(szName, optarg, sizeof szName);
				break;
			case 'c':
				strlcpy(szComment, optarg, sizeof szComment);
				break;
			case 'l':
				if (m) {
					Usage();
					return 1;
				} else
					m = 'l';
				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:
			if (cm) {
				time(&tim);
				localtime_r(&tim, &tm);
				strftime(szTim, sizeof szTim, "%Y-%m-%d %H:%M:%S", &tm);

				fprintf(out, "# Auto-generated file %s from cfger at %s\n#\n# %s\n\n", *argv, szTim, szComment);
			}

			if (jv)
				cfgWriteConfig(out, &cfg, sh);
			else {
				v = cfgWriteLines(out, NULL, NULL, NULL, &cfg);
				ait_freeVar(&v);
			}

			break;
	}
end:
	if (out != stdout)
		fclose(out);
	AIT_FREE_VAL(&data);
	cfgUnloadConfig(&cfg);
	return ret;
}

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