--- embedtools/src/cfger.c 2014/01/24 15:51:32 1.1 +++ embedtools/src/cfger.c 2014/01/24 15:51:32 1.1.2.1 @@ -0,0 +1,378 @@ +/************************************************************************* + * (C) 2014 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: cfger.c,v 1.1.2.1 2014/01/24 15:51:32 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-l \tList A/V pair\n" + "\t-d \tDelete A/V pair\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; + int ret = 0; + ait_val_t attr = AIT_VAL_INITIALIZER(attr); + ait_val_t data = AIT_VAL_INITIALIZER(data); + + while ((ch = getopt(argc, argv, "hvl:s:g:d:")) != -1) + switch (ch) { + case 'v': + Verbose++; + break; + case 'l': + AIT_FREE_VAL(&attr); + if (m) { + Usage(); + return 1; + } else + m = 'l'; + AIT_SET_STR(&attr, optarg); + break; + case 'd': + AIT_FREE_VAL(&attr); + if (m) { + Usage(); + return 1; + } else + m = 'd'; + AIT_SET_STR(&attr, optarg); + break; + case 's': + AIT_FREE_VAL(&attr); + if (m) { + Usage(); + return 1; + } else + m = 's'; + AIT_SET_STR(&attr, optarg); + break; + case 'g': + AIT_FREE_VAL(&attr); + if (m) { + Usage(); + return 1; + } else + m = 'g'; + AIT_SET_STR(&attr, optarg); + 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 0 + switch (m) { + case 'g': + if (!xr.xml_namespace.vallen) { + if (ret == 32) { + if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) { + printf("GET:: path %s - not found!\n", xr.xml_node.path.value); + ret = 1; + goto end; + } + } else { + if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { + printf("GET:: node %s - not found!\n", xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } else { + strlcpy(str, xr.xml_namespace.value, sizeof str); + strlcat(str, ":", sizeof str); + strlcat(str, xr.xml_node.container.value, sizeof str); + if (ret == 32) { + if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) { + printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.path.value); + ret = 1; + goto end; + } + } else { + if (!(node = axl_doc_find_called(doc, str))) { + printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } + + if (!(ret & 32) && xr.xml_data.vallen) { + if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) { + printf("GET:: data %s for node %s - not found!\n", + xr.xml_data.value, xr.xml_node.container.value); + ret = 1; + goto end; + } else + VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); + + VERB(1) printf("\n"); + if (!strcmp(ctx, xr.xml_data.value)) + printf("DATA::1\n"); + else + printf("DATA::0\n"); + } + + if (!(ret & 32) && xr.xml_attribute.vallen) { + if ((n = axl_node_num_attributes(node)) < 1) { + printf("GET:: attribute %s for node %s - not found!\n", + xr.xml_attribute.value, xr.xml_node.container.value); + ret = 1; + goto end; + } else { + VERB(1) printf("Verbose:: node have %d attributes\n", n); + + if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) { + printf("GET:: attribute %s for node %s - not found!\n", + xr.xml_attribute.value, xr.xml_node.container.value); + ret = 1; + goto end; + } + + if (xr.xml_value.vallen) { + if (!strcmp(ctx, xr.xml_value.value)) + ctx = "VALUE::1"; + else + ctx = "VALUE::0"; + } + } + } else { + if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) { + printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value); + ret = 1; + goto end; + } else + VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); + } + + VERB(1) printf("\n"); + printf("%s\n", ctx); + ret = 0; + break; + case 'd': + if (!xr.xml_namespace.vallen) { + if (ret == 32) { + if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { + printf("DEL:: path %s - not found!\n", xr.xml_node.path.value); + ret = 1; + goto end; + } + } else { + if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { + printf("DEL:: node %s - not found!\n", xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } else { + strlcpy(str, xr.xml_namespace.value, sizeof str); + strlcat(str, ":", sizeof str); + strlcat(str, xr.xml_node.container.value, sizeof str); + if (ret == 32) { + if (!(node = axl_doc_get(doc, str))) { + printf("DEL:: path %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.path.value); + ret = 1; + goto end; + } + } else { + if (!(node = axl_doc_find_called(doc, str))) { + printf("DEL:: node %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } + + axl_node_remove(node, 1); + ret = ShowXML(doc, NULL); + break; + case 's': + if (ret == 32) { + if (!xr.xml_data.vallen || !(nnode = axl_node_create(xr.xml_data.value))) { + printf("SET:: container %s at path %s - Error!\n", + xr.xml_data.value, xr.xml_node.path.value); + ret = 1; + goto end; + } + } + if (!xr.xml_namespace.vallen) { + if (ret == 32) { + // insert new + if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { + printf("SET:: path %s - not found!\n", xr.xml_node.path.value); + axl_node_free(nnode); + ret = 1; + goto end; + } + } else { + // update old + if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { + printf("SET:: node %s - not found!\n", xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } else { + strlcpy(str, xr.xml_namespace.value, sizeof str); + strlcat(str, ":", sizeof str); + strlcat(str, xr.xml_node.container.value, sizeof str); + if (ret == 32) { + // insert new + if (!(node = axl_doc_get(doc, str))) { + printf("SET:: path %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.path.value); + axl_node_free(nnode); + ret = 1; + goto end; + } + } else { + // update old + if (!(node = axl_doc_find_called(doc, str))) { + printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } + + if (!(ret & 32) && xr.xml_data.vallen) { + axl_node_set_is_empty(node, 1); + axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen); + } + if (!(ret & 32) && xr.xml_attribute.vallen) { + axl_node_remove_attribute(node, xr.xml_attribute.value); + axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value); + } + + if (ret & 32) + axl_node_set_child(node, nnode); + ret = ShowXML(doc, NULL); + break; + case 'l': + if (!xr.xml_namespace.vallen) { + if (ret == 32) { + if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { + printf("LST:: path %s - not found!\n", xr.xml_node.path.value); + ret = 1; + goto end; + } + } else { + if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { + printf("LST:: node %s - not found!\n", xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } else { + strlcpy(str, xr.xml_namespace.value, sizeof str); + strlcat(str, ":", sizeof str); + strlcat(str, xr.xml_node.container.value, sizeof str); + if (ret == 32) { + if (!(node = axl_doc_get(doc, str))) { + printf("LST:: path %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.path.value); + ret = 1; + goto end; + } + } else { + if (!(node = axl_doc_find_called(doc, str))) { + printf("LST:: node %s:%s - not found!\n", xr.xml_namespace.value, + xr.xml_node.container.value); + ret = 1; + goto end; + } + } + } + + ret = ShowItem(node, 0); + break; + default: + ret = ShowXML(doc, xr.xml_data.vallen ? xr.xml_data.value : NULL); + } +#endif +end: + AIT_FREE_VAL(&data); + cfgUnloadConfig(&cfg); + return ret; +}