Annotation of embedtools/src/xmler.c, revision 1.2

1.2     ! misho       1: /*************************************************************************
        !             2:  * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
        !             3:  *  by Michael Pounov <misho@aitbg.com>
        !             4:  *
        !             5:  * $Author: misho $
        !             6:  * $Id: xmler.c,v 1.1.2.10 2010/09/29 16:51:14 misho Exp $
        !             7:  *
        !             8:  *************************************************************************/
        !             9: #include "global.h"
        !            10: #include <axl.h>
        !            11: #include <axl_ns.h>
        !            12: #include <axl_decl.h>
        !            13: #include "xmler.h"
        !            14: 
        !            15: 
        !            16: int Verbose;
        !            17: extern char compiled[], compiledby[], compilehost[];
        !            18: 
        !            19: 
        !            20: static void
        !            21: Usage()
        !            22: {
        !            23: 
        !            24:        printf( "XMLer is tool for managment R/W operation with XMLs\n"
        !            25:                "=== %s === %s@%s ===\n\n"
        !            26:                "  Syntax: xmler [options] <file.xml> [data]\n\n"
        !            27:                "\t-v\t\tVerbose ...\n"
        !            28:                "\t-l <av_pair>\tList node\n"
        !            29:                "\t-d <av_pair>\tDelete node\n"
        !            30:                "\t-s <av_pair>\tSet node command\n"
        !            31:                "\t-g <av_pair>\tGet node command\n"
        !            32:                "*\"av_pair\" format: [ns:]node[[|attribute[=value]]?data]\n"
        !            33:                "\n", compiled, compiledby, compilehost);
        !            34: }
        !            35: 
        !            36: static int
        !            37: ShowXML(axlDoc *doc, const char *csNode)
        !            38: {
        !            39:        axlNode *node = NULL;
        !            40:        int ctxlen;
        !            41:        char *ctx = NULL;
        !            42: 
        !            43:        if (csNode) {
        !            44:                if (!(node = axl_doc_find_called(doc, csNode))) {
        !            45:                        printf("GET:: node %s - not found!\n", csNode);
        !            46:                        return 1;
        !            47:                }
        !            48: 
        !            49:                axl_node_dump_pretty(node, &ctx, &ctxlen, 4);
        !            50:                VERB(1) printf("Verbose:: Node length=%d\n", ctxlen);
        !            51:        } else {
        !            52:                axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4);
        !            53:                VERB(1) printf("Verbose:: Document length=%d\n", ctxlen);
        !            54:        }
        !            55: 
        !            56:        VERB(1) printf("\n");
        !            57:        if (ctx) {
        !            58:                printf("%s", ctx);
        !            59:                free(ctx);
        !            60:        }
        !            61:        VERB(1) printf("\n");
        !            62:        return 0;
        !            63: }
        !            64: 
        !            65: static int
        !            66: ShowItem(axlNode *node, int lvl)
        !            67: {
        !            68:        register int i;
        !            69:        int ctxlen;
        !            70:        axlNode *child;
        !            71: 
        !            72:        for (i = 0; i < axl_node_get_child_num(node); i++) {
        !            73:                child = axl_node_get_child_nth(node, i);
        !            74: 
        !            75:                if (!lvl)
        !            76:                        printf("%s %s \"%s\"", axl_node_get_name(node), axl_node_get_name(child), 
        !            77:                                        axl_node_get_content(child, &ctxlen));
        !            78:                else
        !            79:                        printf(" %s \"%s\"", axl_node_get_name(child), axl_node_get_content(child, &ctxlen));
        !            80: 
        !            81:                ShowItem(child, 1);
        !            82: 
        !            83:                if (!lvl)
        !            84:                        printf("\n");
        !            85:        }
        !            86: 
        !            87:        return 0;
        !            88: }
        !            89: 
        !            90: int
        !            91: main(int argc, char **argv)
        !            92: {
        !            93:        char ch, str[STRSIZ], szName[MAXPATHLEN], *ctx = NULL, m = 0;
        !            94:        int ctxlen, n, ret = 0;
        !            95:        axlDoc *doc = NULL;
        !            96:        axlError *err = NULL;
        !            97:        axlNode *nnode = NULL, *node = NULL;
        !            98:        struct tagReqXML xr;
        !            99: 
        !           100:        memset(str, 0, STRSIZ);
        !           101:        memset(&xr, 0, sizeof xr);
        !           102:        while ((ch = getopt(argc, argv, "hvl:s:g:d:")) != -1)
        !           103:                switch (ch) {
        !           104:                        case 'v':
        !           105:                                Verbose++;
        !           106:                                break;
        !           107:                        case 'l':
        !           108:                                if (m) {
        !           109:                                        Usage();
        !           110:                                        return 1;
        !           111:                                } else
        !           112:                                        m = 'l';
        !           113:                                strlcpy(str, optarg, STRSIZ);
        !           114:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
        !           115:                                        printf("Error:: in XML request %s\n", str);
        !           116:                                        return 1;
        !           117:                                }
        !           118:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
        !           119:                                break;
        !           120:                        case 'd':
        !           121:                                if (m) {
        !           122:                                        Usage();
        !           123:                                        return 1;
        !           124:                                } else
        !           125:                                        m = 'd';
        !           126:                                strlcpy(str, optarg, STRSIZ);
        !           127:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
        !           128:                                        printf("Error:: in XML request %s\n", str);
        !           129:                                        return 1;
        !           130:                                }
        !           131:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
        !           132:                                break;
        !           133:                        case 's':
        !           134:                                if (m) {
        !           135:                                        Usage();
        !           136:                                        return 1;
        !           137:                                } else
        !           138:                                        m = 's';
        !           139:                                strlcpy(str, optarg, STRSIZ);
        !           140:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
        !           141:                                        printf("Error:: in XML request %s\n", str);
        !           142:                                        return 1;
        !           143:                                }
        !           144:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
        !           145:                                break;
        !           146:                        case 'g':
        !           147:                                if (m) {
        !           148:                                        Usage();
        !           149:                                        return 1;
        !           150:                                } else
        !           151:                                        m = 'g';
        !           152:                                strlcpy(str, optarg, STRSIZ);
        !           153:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
        !           154:                                        printf("Error:: in XML request %s\n", str);
        !           155:                                        return 1;
        !           156:                                }
        !           157:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
        !           158:                                break;
        !           159:                        case 'h':
        !           160:                        default:
        !           161:                                Usage();
        !           162:                                return 1;
        !           163:                }
        !           164:        argc -= optind;
        !           165:        argv += optind;
        !           166:        if (!argc) {
        !           167:                Usage();
        !           168:                return 1;
        !           169:        } else
        !           170:                strlcpy(szName, *argv, MAXPATHLEN);
        !           171:        if (argc > 1 && argv[1]) {
        !           172:                xr.xml_data.value = argv[1];
        !           173:                xr.xml_data.vallen = strlen(argv[1]);
        !           174:        }
        !           175: 
        !           176:        axl_init();
        !           177:        if (!(doc = axl_doc_parse_from_file(szName, &err))) {
        !           178:                printf("Error:: xml file %s #%d - %s\n", szName, 
        !           179:                                axl_error_get_code(err), axl_error_get(err));
        !           180:                axl_error_free(err);
        !           181:                axl_end();
        !           182:                return 2;
        !           183:        }
        !           184:        if (!axl_ns_doc_validate(doc, &err)) {
        !           185:                printf("Error:: xml file %s namespace validation #%d - %s\n", szName, 
        !           186:                                axl_error_get_code(err), axl_error_get(err));
        !           187:                axl_error_free(err);
        !           188:                axl_end();
        !           189:                return 2;
        !           190:        }
        !           191: 
        !           192:        switch (m) {
        !           193:                case 'g':
        !           194:                        if (!xr.xml_namespace.vallen) {
        !           195:                                if (ret == 32) {
        !           196:                                        if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) {
        !           197:                                                printf("GET:: path %s - not found!\n", xr.xml_node.path.value);
        !           198:                                                ret = 1;
        !           199:                                                goto end;
        !           200:                                        }
        !           201:                                } else {
        !           202:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           203:                                                printf("GET:: node %s - not found!\n", xr.xml_node.container.value);
        !           204:                                                ret = 1;
        !           205:                                                goto end;
        !           206:                                        }
        !           207:                                }
        !           208:                        } else {
        !           209:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           210:                                strlcat(str, ":", sizeof str);
        !           211:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           212:                                if (ret == 32) {
        !           213:                                        if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) {
        !           214:                                                printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           215:                                                                xr.xml_node.path.value);
        !           216:                                                ret = 1;
        !           217:                                                goto end;
        !           218:                                        }
        !           219:                                } else {
        !           220:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           221:                                                printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           222:                                                                xr.xml_node.container.value);
        !           223:                                                ret = 1;
        !           224:                                                goto end;
        !           225:                                        }
        !           226:                                }
        !           227:                        }
        !           228: 
        !           229:                        if (!(ret & 32) && xr.xml_data.vallen) {
        !           230:                                if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
        !           231:                                        printf("GET:: data %s for node %s - not found!\n", 
        !           232:                                                        xr.xml_data.value, xr.xml_node.container.value);
        !           233:                                        ret = 1;
        !           234:                                        goto end;
        !           235:                                } else
        !           236:                                        VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
        !           237: 
        !           238:                                VERB(1) printf("\n");
        !           239:                                if (!strcmp(ctx, xr.xml_data.value))
        !           240:                                        printf("DATA::1\n");
        !           241:                                else
        !           242:                                        printf("DATA::0\n");
        !           243:                        }
        !           244: 
        !           245:                        if (!(ret & 32) && xr.xml_attribute.vallen) {
        !           246:                                if ((n = axl_node_num_attributes(node)) < 1) {
        !           247:                                        printf("GET:: attribute %s for node %s - not found!\n", 
        !           248:                                                        xr.xml_attribute.value, xr.xml_node.container.value);
        !           249:                                        ret = 1;
        !           250:                                        goto end;
        !           251:                                } else {
        !           252:                                        VERB(1) printf("Verbose:: node have %d attributes\n", n);
        !           253: 
        !           254:                                        if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) {
        !           255:                                                printf("GET:: attribute %s for node %s - not found!\n", 
        !           256:                                                                xr.xml_attribute.value, xr.xml_node.container.value);
        !           257:                                                ret = 1;
        !           258:                                                goto end;
        !           259:                                        }
        !           260: 
        !           261:                                        if (xr.xml_value.vallen) {
        !           262:                                                if (!strcmp(ctx, xr.xml_value.value))
        !           263:                                                        ctx = "VALUE::1";
        !           264:                                                else
        !           265:                                                        ctx = "VALUE::0";
        !           266:                                        }
        !           267:                                }
        !           268:                        } else {
        !           269:                                if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
        !           270:                                        printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value);
        !           271:                                        ret = 1;
        !           272:                                        goto end;
        !           273:                                } else
        !           274:                                        VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
        !           275:                        }
        !           276: 
        !           277:                        VERB(1) printf("\n");
        !           278:                        printf("%s\n", ctx);
        !           279:                        ret = 0;
        !           280:                        break;
        !           281:                case 'd':
        !           282:                        if (!xr.xml_namespace.vallen) {
        !           283:                                if (ret == 32) {
        !           284:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
        !           285:                                                printf("DEL:: path %s - not found!\n", xr.xml_node.path.value);
        !           286:                                                ret = 1;
        !           287:                                                goto end;
        !           288:                                        }
        !           289:                                } else {
        !           290:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           291:                                                printf("DEL:: node %s - not found!\n", xr.xml_node.container.value);
        !           292:                                                ret = 1;
        !           293:                                                goto end;
        !           294:                                        }
        !           295:                                }
        !           296:                        } else {
        !           297:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           298:                                strlcat(str, ":", sizeof str);
        !           299:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           300:                                if (ret == 32) {
        !           301:                                        if (!(node = axl_doc_get(doc, str))) {
        !           302:                                                printf("DEL:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           303:                                                                xr.xml_node.path.value);
        !           304:                                                ret = 1;
        !           305:                                                goto end;
        !           306:                                        }
        !           307:                                } else {
        !           308:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           309:                                                printf("DEL:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           310:                                                                xr.xml_node.container.value);
        !           311:                                                ret = 1;
        !           312:                                                goto end;
        !           313:                                        }
        !           314:                                }
        !           315:                        }
        !           316: 
        !           317:                        axl_node_remove(node, 1);
        !           318:                        ret = ShowXML(doc, NULL);
        !           319:                        break;
        !           320:                case 's':
        !           321:                        if (ret == 32) {
        !           322:                                if (!xr.xml_data.vallen || !(nnode = axl_node_create(xr.xml_data.value))) {
        !           323:                                        printf("SET:: container %s at path %s - Error!\n", 
        !           324:                                                        xr.xml_data.value, xr.xml_node.path.value);
        !           325:                                        ret = 1;
        !           326:                                        goto end;
        !           327:                                }
        !           328:                        }
        !           329:                        if (!xr.xml_namespace.vallen) {
        !           330:                                if (ret == 32) {
        !           331:                                        // insert new
        !           332:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
        !           333:                                                printf("SET:: path %s - not found!\n", xr.xml_node.path.value);
        !           334:                                                axl_node_free(nnode);
        !           335:                                                ret = 1;
        !           336:                                                goto end;
        !           337:                                        }
        !           338:                                } else {
        !           339:                                        // update old
        !           340:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           341:                                                printf("SET:: node %s - not found!\n", xr.xml_node.container.value);
        !           342:                                                ret = 1;
        !           343:                                                goto end;
        !           344:                                        }
        !           345:                                }
        !           346:                        } else {
        !           347:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           348:                                strlcat(str, ":", sizeof str);
        !           349:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           350:                                if (ret == 32) {
        !           351:                                        // insert new
        !           352:                                        if (!(node = axl_doc_get(doc, str))) {
        !           353:                                                printf("SET:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           354:                                                                xr.xml_node.path.value);
        !           355:                                                axl_node_free(nnode);
        !           356:                                                ret = 1;
        !           357:                                                goto end;
        !           358:                                        }
        !           359:                                } else {
        !           360:                                        // update old
        !           361:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           362:                                                printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           363:                                                                xr.xml_node.container.value);
        !           364:                                                ret = 1;
        !           365:                                                goto end;
        !           366:                                        }
        !           367:                                }
        !           368:                        }
        !           369: 
        !           370:                        if (!(ret & 32) && xr.xml_data.vallen) {
        !           371:                                axl_node_set_is_empty(node, 1);
        !           372:                                axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen);
        !           373:                        }
        !           374:                        if (!(ret & 32) && xr.xml_attribute.vallen) {
        !           375:                                axl_node_remove_attribute(node, xr.xml_attribute.value);
        !           376:                                axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value);
        !           377:                        }
        !           378: 
        !           379:                        if (ret & 32)
        !           380:                                axl_node_set_child(node, nnode);
        !           381:                        ret = ShowXML(doc, NULL);
        !           382:                        break;
        !           383:                case 'l':
        !           384:                        if (!xr.xml_namespace.vallen) {
        !           385:                                if (ret == 32) {
        !           386:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
        !           387:                                                printf("LST:: path %s - not found!\n", xr.xml_node.path.value);
        !           388:                                                ret = 1;
        !           389:                                                goto end;
        !           390:                                        }
        !           391:                                } else {
        !           392:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
        !           393:                                                printf("LST:: node %s - not found!\n", xr.xml_node.container.value);
        !           394:                                                ret = 1;
        !           395:                                                goto end;
        !           396:                                        }
        !           397:                                }
        !           398:                        } else {
        !           399:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           400:                                strlcat(str, ":", sizeof str);
        !           401:                                strlcat(str, xr.xml_node.container.value, sizeof str);
        !           402:                                if (ret == 32) {
        !           403:                                        if (!(node = axl_doc_get(doc, str))) {
        !           404:                                                printf("LST:: path %s:%s - not found!\n", xr.xml_namespace.value, 
        !           405:                                                                xr.xml_node.path.value);
        !           406:                                                ret = 1;
        !           407:                                                goto end;
        !           408:                                        }
        !           409:                                } else {
        !           410:                                        if (!(node = axl_doc_find_called(doc, str))) {
        !           411:                                                printf("LST:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           412:                                                                xr.xml_node.container.value);
        !           413:                                                ret = 1;
        !           414:                                                goto end;
        !           415:                                        }
        !           416:                                }
        !           417:                        }
        !           418: 
        !           419:                        ret = ShowItem(node, 0);
        !           420:                        break;
        !           421:                default:
        !           422:                        ret = ShowXML(doc, xr.xml_data.vallen ? xr.xml_data.value : NULL);
        !           423:        }
        !           424: end:
        !           425:        axl_doc_free(doc);
        !           426:        axl_end();
        !           427:        return ret;
        !           428: }

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