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

1.1.2.1   misho       1: #include "global.h"
1.1.2.2 ! misho       2: #include <axl.h>
        !             3: #include <axl_ns.h>
        !             4: #include <axl_decl.h>
        !             5: #include "xmler.h"
1.1.2.1   misho       6: 
                      7: 
1.1.2.2 ! misho       8: int Verbose;
        !             9: extern char compiled[], compiledby[], compilehost[];
        !            10: 
        !            11: 
        !            12: static void Usage()
        !            13: {
        !            14: 
        !            15:        printf( "XMLer is tool for managment R/W operation with XMLs\n"
        !            16:                "=== %s === %s@%s ===\n\n"
        !            17:                "  Syntax: xmler [options] <file.xml> [data]\n\n"
        !            18:                "\t-v\t\tVerbose ...\n"
        !            19:                "\t-s <av_pair>\tSet Attribute/Value pair\n"
        !            20:                "\t-g <av_pair>\tGet Attribute/Value pair\n"
        !            21:                "*\"av_pair\" format: [ns:]node[[|attribute[=value]]?data]\n"
        !            22:                "\n", compiled, compiledby, compilehost);
        !            23: }
        !            24: 
1.1.2.1   misho      25: int
                     26: main(int argc, char **argv)
                     27: {
1.1.2.2 ! misho      28:        char ch, *ctx, str[STRSIZ], szName[MAXPATHLEN], m = 0;
        !            29:        int ctxlen, ret = 0;
        !            30:        axlDoc *doc = NULL;
        !            31:        axlError *err = NULL;
        !            32:        axlNode *node = NULL;
        !            33:        struct tagReqXML xr;
        !            34: 
        !            35:        memset(str, 0, STRSIZ);
        !            36:        memset(&xr, 0, sizeof xr);
        !            37:        while ((ch = getopt(argc, argv, "hvs:g:")) != -1)
        !            38:                switch (ch) {
        !            39:                        case 'v':
        !            40:                                Verbose++;
        !            41:                                break;
        !            42:                        case 's':
        !            43:                                if (m) {
        !            44:                                        Usage();
        !            45:                                        return 1;
        !            46:                                } else
        !            47:                                        m = 's';
        !            48:                                strlcpy(str, optarg, STRSIZ);
        !            49:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
        !            50:                                        printf("Error:: in XML request %s\n", str);
        !            51:                                        return 1;
        !            52:                                }
        !            53:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
        !            54:                                break;
        !            55:                        case 'g':
        !            56:                                if (m) {
        !            57:                                        Usage();
        !            58:                                        return 1;
        !            59:                                } else
        !            60:                                        m = 'g';
        !            61:                                strlcpy(str, optarg, STRSIZ);
        !            62:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
        !            63:                                        printf("Error:: in XML request %s\n", str);
        !            64:                                        return 1;
        !            65:                                }
        !            66:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
        !            67:                                break;
        !            68:                        case 'h':
        !            69:                        default:
        !            70:                                Usage();
        !            71:                                return 1;
        !            72:                }
        !            73:        argc -= optind;
        !            74:        argv += optind;
        !            75:        if (!argc) {
        !            76:                Usage();
        !            77:                return 1;
        !            78:        } else
        !            79:                strlcpy(szName, *argv, MAXPATHLEN);
        !            80:        if (argc > 1 && argv[1]) {
        !            81:                xr.xml_data.value = argv[1];
        !            82:                xr.xml_data.vallen = strlen(argv[1]);
        !            83:        }
        !            84: 
        !            85:        axl_init();
        !            86:        if (!(doc = axl_doc_parse_from_file(szName, &err))) {
        !            87:                printf("Error:: xml file %s #%d - %s\n", szName, 
        !            88:                                axl_error_get_code(err), axl_error_get(err));
        !            89:                axl_error_free(err);
        !            90:                axl_end();
        !            91:                return 2;
        !            92:        }
        !            93:        if (!axl_ns_doc_validate(doc, &err)) {
        !            94:                printf("Error:: xml file %s namespace validation #%d - %s\n", szName, 
        !            95:                                axl_error_get_code(err), axl_error_get(err));
        !            96:                axl_error_free(err);
        !            97:                axl_end();
        !            98:                return 2;
        !            99:        }
        !           100: 
        !           101:        switch (m) {
        !           102:                case 's':
        !           103:                        break;
        !           104:                case 'g':
        !           105:                        if (!xr.xml_namespace.vallen) {
        !           106:                                if (!(node = axl_doc_find_called(doc, xr.xml_container.value))) {
        !           107:                                        printf("GET:: node %s - not found!\n", xr.xml_container.value);
        !           108:                                        ret = 1;
        !           109:                                        goto end;
        !           110:                                }
        !           111:                        } else {
        !           112:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
        !           113:                                strlcat(str, ":", sizeof str);
        !           114:                                strlcat(str, xr.xml_container.value, sizeof str);
        !           115:                                if (!(node = axl_doc_find_called(doc, str))) {
        !           116:                                        printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
        !           117:                                                        xr.xml_container.value);
        !           118:                                        ret = 1;
        !           119:                                        goto end;
        !           120:                                }
        !           121:                        }
        !           122: 
        !           123:                        if (xr.xml_data.vallen) {
        !           124:                                if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
        !           125:                                        axl_node_free(node);
        !           126:                                        printf("GET:: data %s for node %s - not found!\n", 
        !           127:                                                        xr.xml_data.value, xr.xml_attribute.value);
        !           128:                                        ret = 1;
        !           129:                                        goto end;
        !           130:                                } else
        !           131:                                        VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
        !           132: 
        !           133:                                VERB(1) printf("\n");
        !           134:                                if (!(ret = strcmp(ctx, xr.xml_data.value)))
        !           135:                                        printf("DATA::1\n");
        !           136:                                else
        !           137:                                        printf("DATA::0\n");
        !           138:                        }
        !           139: 
        !           140:                        if (xr.xml_attribute.vallen) {
        !           141:                                if ((ret = axl_node_num_attributes(node)) < 1) {
        !           142:                                        axl_node_free(node);
        !           143:                                        printf("GET:: attribute %s for node %s - not found!\n", 
        !           144:                                                        xr.xml_container.value, xr.xml_attribute.value);
        !           145:                                        ret = 1;
        !           146:                                        goto end;
        !           147:                                } else {
        !           148:                                        VERB(1) printf("Verbose:: node have %d attributes\n", ret);
        !           149: 
        !           150:                                        if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) {
        !           151:                                                axl_node_free(node);
        !           152:                                                printf("GET:: attribute %s for node %s - not found!\n", 
        !           153:                                                                xr.xml_container.value, xr.xml_attribute.value);
        !           154:                                                ret = 1;
        !           155:                                                goto end;
        !           156:                                        }
        !           157: 
        !           158:                                        if (xr.xml_value.vallen) {
        !           159:                                                if (!(ret = strcmp(ctx, xr.xml_value.value)))
        !           160:                                                        ctx = "VALUE::1";
        !           161:                                                else
        !           162:                                                        ctx = "VALUE::0";
        !           163:                                        }
        !           164:                                }
        !           165:                        } else {
        !           166:                                axl_node_dump_pretty(node, &ctx, &ctxlen, 4);
        !           167:                                VERB(1) printf("Verbose:: Node length=%d\n", ctxlen);
        !           168:                        }
        !           169: 
        !           170:                        VERB(1) printf("\n");
        !           171:                        printf("%s\n", ctx);
        !           172:                        axl_node_free(node);
        !           173:                        ret = 0;
        !           174:                        break;
        !           175:                default:
        !           176:                        axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4);
        !           177:                        VERB(1) printf("Verbose:: Document length=%d\n", ctxlen);
        !           178: 
        !           179:                        VERB(1) printf("\n");
        !           180:                        printf("%s\n", ctx);
        !           181:                        free(ctx);
        !           182:        }
        !           183: end:
        !           184:        axl_doc_free(doc);
        !           185:        axl_end();
        !           186:        return ret;
1.1.2.1   misho     187: }

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