Annotation of embedtools/src/cfger.c, revision 1.1.2.1

1.1.2.1 ! misho       1: /*************************************************************************
        !             2:  * (C) 2014 AITNET - Sofia/Bulgaria - <office@aitbg.com>
        !             3:  *  by Michael Pounov <misho@aitbg.com>
        !             4:  *
        !             5:  * $Author: misho $
        !             6:  * $Id: xmler.c,v 1.3 2012/07/22 22:46:48 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-l <attr>\tList A/V pair\n"
        !            64:                "\t-d <attr>\tDelete A/V pair\n"
        !            65:                "\t-s <attr>\tSet A/V pair\n"
        !            66:                "\t-g <attr>\tGet value from A/V pair\n"
        !            67:                "*\"attr\" format: [section/]attribute\n"
        !            68:                "\n", compiled, compiledby, compilehost);
        !            69: }
        !            70: 
        !            71: int
        !            72: main(int argc, char **argv)
        !            73: {
        !            74:        char ch, *str, m = 0;
        !            75:        int ret = 0;
        !            76:        ait_val_t attr = AIT_VAL_INITIALIZER(attr);
        !            77:        ait_val_t data = AIT_VAL_INITIALIZER(data);
        !            78: 
        !            79:        while ((ch = getopt(argc, argv, "hvl:s:g:d:")) != -1)
        !            80:                switch (ch) {
        !            81:                        case 'v':
        !            82:                                Verbose++;
        !            83:                                break;
        !            84:                        case 'l':
        !            85:                                AIT_FREE_VAL(&attr);
        !            86:                                if (m) {
        !            87:                                        Usage();
        !            88:                                        return 1;
        !            89:                                } else
        !            90:                                        m = 'l';
        !            91:                                AIT_SET_STR(&attr, optarg);
        !            92:                                break;
        !            93:                        case 'd':
        !            94:                                AIT_FREE_VAL(&attr);
        !            95:                                if (m) {
        !            96:                                        Usage();
        !            97:                                        return 1;
        !            98:                                } else
        !            99:                                        m = 'd';
        !           100:                                AIT_SET_STR(&attr, optarg);
        !           101:                                break;
        !           102:                        case 's':
        !           103:                                AIT_FREE_VAL(&attr);
        !           104:                                if (m) {
        !           105:                                        Usage();
        !           106:                                        return 1;
        !           107:                                } else
        !           108:                                        m = 's';
        !           109:                                AIT_SET_STR(&attr, optarg);
        !           110:                                break;
        !           111:                        case 'g':
        !           112:                                AIT_FREE_VAL(&attr);
        !           113:                                if (m) {
        !           114:                                        Usage();
        !           115:                                        return 1;
        !           116:                                } else
        !           117:                                        m = 'g';
        !           118:                                AIT_SET_STR(&attr, optarg);
        !           119:                                break;
        !           120:                        case 'h':
        !           121:                        default:
        !           122:                                Usage();
        !           123:                                return 1;
        !           124:                }
        !           125:        argc -= optind;
        !           126:        argv += optind;
        !           127:        if (!argc) {
        !           128:                Usage();
        !           129:                return 1;
        !           130:        }
        !           131:        if (argc > 1 && argv[1])
        !           132:                AIT_SET_STR(&data, argv[1]);
        !           133: 
        !           134:        if (cfgLoadConfig(*argv, &cfg)) {
        !           135:                ELIBERR(cfg);
        !           136:                ret = 2;
        !           137:                goto end;
        !           138:        }
        !           139: 
        !           140: #if 0
        !           141:        switch (m) {
        !           142:                case 'g':
        !           143:                        if (!xr.xml_namespace.vallen) {
        !           144:                                if (ret == 32) {
        !           145:                                        if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) {
        !           146:                                                printf("GET:: path %s - not found!\n", xr.xml_node.path.value);
        !           147:                                                ret = 1;
        !           148:                                                goto end;
        !           149:                                        }
        !           150:                                } else {
        !           151:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           152:                                                printf("GET:: node %s - not found!\n", xr.xml_node.container.value);
        !           153:                                                ret = 1;
        !           154:                                                goto end;
        !           155:                                        }
        !           156:                                }
        !           157:                        } else {
        !           158:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           159:                                strlcat(str, ":", sizeof str);
        !           160:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           161:                                if (ret == 32) {
        !           162:                                        if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) {
        !           163:                                                printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           164:                                                                xr.xml_node.path.value);
        !           165:                                                ret = 1;
        !           166:                                                goto end;
        !           167:                                        }
        !           168:                                } else {
        !           169:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           170:                                                printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           171:                                                                xr.xml_node.container.value);
        !           172:                                                ret = 1;
        !           173:                                                goto end;
        !           174:                                        }
        !           175:                                }
        !           176:                        }
        !           177: 
        !           178:                        if (!(ret & 32) && xr.xml_data.vallen) {
        !           179:                                if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
        !           180:                                        printf("GET:: data %s for node %s - not found!\n", 
        !           181:                                                        xr.xml_data.value, xr.xml_node.container.value);
        !           182:                                        ret = 1;
        !           183:                                        goto end;
        !           184:                                } else
        !           185:                                        VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
        !           186: 
        !           187:                                VERB(1) printf("\n");
        !           188:                                if (!strcmp(ctx, xr.xml_data.value))
        !           189:                                        printf("DATA::1\n");
        !           190:                                else
        !           191:                                        printf("DATA::0\n");
        !           192:                        }
        !           193: 
        !           194:                        if (!(ret & 32) && xr.xml_attribute.vallen) {
        !           195:                                if ((n = axl_node_num_attributes(node)) < 1) {
        !           196:                                        printf("GET:: attribute %s for node %s - not found!\n", 
        !           197:                                                        xr.xml_attribute.value, xr.xml_node.container.value);
        !           198:                                        ret = 1;
        !           199:                                        goto end;
        !           200:                                } else {
        !           201:                                        VERB(1) printf("Verbose:: node have %d attributes\n", n);
        !           202: 
        !           203:                                        if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) {
        !           204:                                                printf("GET:: attribute %s for node %s - not found!\n", 
        !           205:                                                                xr.xml_attribute.value, xr.xml_node.container.value);
        !           206:                                                ret = 1;
        !           207:                                                goto end;
        !           208:                                        }
        !           209: 
        !           210:                                        if (xr.xml_value.vallen) {
        !           211:                                                if (!strcmp(ctx, xr.xml_value.value))
        !           212:                                                        ctx = "VALUE::1";
        !           213:                                                else
        !           214:                                                        ctx = "VALUE::0";
        !           215:                                        }
        !           216:                                }
        !           217:                        } else {
        !           218:                                if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
        !           219:                                        printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value);
        !           220:                                        ret = 1;
        !           221:                                        goto end;
        !           222:                                } else
        !           223:                                        VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
        !           224:                        }
        !           225: 
        !           226:                        VERB(1) printf("\n");
        !           227:                        printf("%s\n", ctx);
        !           228:                        ret = 0;
        !           229:                        break;
        !           230:                case 'd':
        !           231:                        if (!xr.xml_namespace.vallen) {
        !           232:                                if (ret == 32) {
        !           233:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
        !           234:                                                printf("DEL:: path %s - not found!\n", xr.xml_node.path.value);
        !           235:                                                ret = 1;
        !           236:                                                goto end;
        !           237:                                        }
        !           238:                                } else {
        !           239:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           240:                                                printf("DEL:: node %s - not found!\n", xr.xml_node.container.value);
        !           241:                                                ret = 1;
        !           242:                                                goto end;
        !           243:                                        }
        !           244:                                }
        !           245:                        } else {
        !           246:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           247:                                strlcat(str, ":", sizeof str);
        !           248:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           249:                                if (ret == 32) {
        !           250:                                        if (!(node = axl_doc_get(doc, str))) {
        !           251:                                                printf("DEL:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           252:                                                                xr.xml_node.path.value);
        !           253:                                                ret = 1;
        !           254:                                                goto end;
        !           255:                                        }
        !           256:                                } else {
        !           257:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           258:                                                printf("DEL:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           259:                                                                xr.xml_node.container.value);
        !           260:                                                ret = 1;
        !           261:                                                goto end;
        !           262:                                        }
        !           263:                                }
        !           264:                        }
        !           265: 
        !           266:                        axl_node_remove(node, 1);
        !           267:                        ret = ShowXML(doc, NULL);
        !           268:                        break;
        !           269:                case 's':
        !           270:                        if (ret == 32) {
        !           271:                                if (!xr.xml_data.vallen || !(nnode = axl_node_create(xr.xml_data.value))) {
        !           272:                                        printf("SET:: container %s at path %s - Error!\n", 
        !           273:                                                        xr.xml_data.value, xr.xml_node.path.value);
        !           274:                                        ret = 1;
        !           275:                                        goto end;
        !           276:                                }
        !           277:                        }
        !           278:                        if (!xr.xml_namespace.vallen) {
        !           279:                                if (ret == 32) {
        !           280:                                        // insert new
        !           281:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
        !           282:                                                printf("SET:: path %s - not found!\n", xr.xml_node.path.value);
        !           283:                                                axl_node_free(nnode);
        !           284:                                                ret = 1;
        !           285:                                                goto end;
        !           286:                                        }
        !           287:                                } else {
        !           288:                                        // update old
        !           289:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           290:                                                printf("SET:: node %s - not found!\n", xr.xml_node.container.value);
        !           291:                                                ret = 1;
        !           292:                                                goto end;
        !           293:                                        }
        !           294:                                }
        !           295:                        } else {
        !           296:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           297:                                strlcat(str, ":", sizeof str);
        !           298:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           299:                                if (ret == 32) {
        !           300:                                        // insert new
        !           301:                                        if (!(node = axl_doc_get(doc, str))) {
        !           302:                                                printf("SET:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           303:                                                                xr.xml_node.path.value);
        !           304:                                                axl_node_free(nnode);
        !           305:                                                ret = 1;
        !           306:                                                goto end;
        !           307:                                        }
        !           308:                                } else {
        !           309:                                        // update old
        !           310:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           311:                                                printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           312:                                                                xr.xml_node.container.value);
        !           313:                                                ret = 1;
        !           314:                                                goto end;
        !           315:                                        }
        !           316:                                }
        !           317:                        }
        !           318: 
        !           319:                        if (!(ret & 32) && xr.xml_data.vallen) {
        !           320:                                axl_node_set_is_empty(node, 1);
        !           321:                                axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen);
        !           322:                        }
        !           323:                        if (!(ret & 32) && xr.xml_attribute.vallen) {
        !           324:                                axl_node_remove_attribute(node, xr.xml_attribute.value);
        !           325:                                axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value);
        !           326:                        }
        !           327: 
        !           328:                        if (ret & 32)
        !           329:                                axl_node_set_child(node, nnode);
        !           330:                        ret = ShowXML(doc, NULL);
        !           331:                        break;
        !           332:                case 'l':
        !           333:                        if (!xr.xml_namespace.vallen) {
        !           334:                                if (ret == 32) {
        !           335:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
        !           336:                                                printf("LST:: path %s - not found!\n", xr.xml_node.path.value);
        !           337:                                                ret = 1;
        !           338:                                                goto end;
        !           339:                                        }
        !           340:                                } else {
        !           341:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           342:                                                printf("LST:: node %s - not found!\n", xr.xml_node.container.value);
        !           343:                                                ret = 1;
        !           344:                                                goto end;
        !           345:                                        }
        !           346:                                }
        !           347:                        } else {
        !           348:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           349:                                strlcat(str, ":", sizeof str);
        !           350:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           351:                                if (ret == 32) {
        !           352:                                        if (!(node = axl_doc_get(doc, str))) {
        !           353:                                                printf("LST:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           354:                                                                xr.xml_node.path.value);
        !           355:                                                ret = 1;
        !           356:                                                goto end;
        !           357:                                        }
        !           358:                                } else {
        !           359:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           360:                                                printf("LST:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           361:                                                                xr.xml_node.container.value);
        !           362:                                                ret = 1;
        !           363:                                                goto end;
        !           364:                                        }
        !           365:                                }
        !           366:                        }
        !           367: 
        !           368:                        ret = ShowItem(node, 0);
        !           369:                        break;
        !           370:                default:
        !           371:                        ret = ShowXML(doc, xr.xml_data.vallen ? xr.xml_data.value : NULL);
        !           372:        }
        !           373: #endif
        !           374: end:
        !           375:        AIT_FREE_VAL(&data);
        !           376:        cfgUnloadConfig(&cfg);
        !           377:        return ret;
        !           378: }

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