--- embedtools/src/cfger.c 2014/01/24 15:51:32 1.1 +++ embedtools/src/cfger.c 2014/02/05 15:44:05 1.2 @@ -0,0 +1,183 @@ +/************************************************************************* + * (C) 2014 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: cfger.c,v 1.2 2014/02/05 15:44:05 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 + +Copyright 2004 - 2014 + by Michael Pounov . 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 +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] [data]\n\n" + "\t-v\t\tVerbose ...\n" + "\t-q\t\tQuiet mode\n" + "\t-o \tOutput result to file\n" + "\t-l\t\tList A/V pairs\n" + "\t-s \tSet A/V pair\n" + "\t-g \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, "hvqls: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'; + 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; +}