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

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: 
1.1.2.5   misho      12: static void
                     13: Usage()
1.1.2.2   misho      14: {
                     15: 
                     16:        printf( "XMLer is tool for managment R/W operation with XMLs\n"
                     17:                "=== %s === %s@%s ===\n\n"
                     18:                "  Syntax: xmler [options] <file.xml> [data]\n\n"
                     19:                "\t-v\t\tVerbose ...\n"
1.1.2.5   misho      20:                "\t-d <av_pair>\tDelete node\n"
                     21:                "\t-s <av_pair>\tSet node command\n"
                     22:                "\t-g <av_pair>\tGet node command\n"
1.1.2.2   misho      23:                "*\"av_pair\" format: [ns:]node[[|attribute[=value]]?data]\n"
                     24:                "\n", compiled, compiledby, compilehost);
                     25: }
                     26: 
1.1.2.5   misho      27: static int
                     28: ShowXML(axlDoc *doc, const char *csNode)
                     29: {
                     30:        axlNode *node = NULL;
                     31:        int ctxlen;
                     32:        char *ctx = NULL;
                     33: 
                     34:        if (csNode) {
                     35:                if (!(node = axl_doc_find_called(doc, csNode))) {
                     36:                        printf("GET:: node %s - not found!\n", csNode);
                     37:                        return 1;
                     38:                }
                     39: 
                     40:                axl_node_dump_pretty(node, &ctx, &ctxlen, 4);
                     41:                VERB(1) printf("Verbose:: Node length=%d\n", ctxlen);
                     42:        } else {
                     43:                axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4);
                     44:                VERB(1) printf("Verbose:: Document length=%d\n", ctxlen);
                     45:        }
                     46: 
                     47:        VERB(1) printf("\n");
                     48:        if (ctx) {
                     49:                printf("%s", ctx);
                     50:                free(ctx);
                     51:        }
                     52:        VERB(1) printf("\n");
                     53:        return 0;
                     54: }
                     55: 
1.1.2.1   misho      56: int
                     57: main(int argc, char **argv)
                     58: {
1.1.2.2   misho      59:        char ch, *ctx, str[STRSIZ], szName[MAXPATHLEN], m = 0;
1.1.2.3   misho      60:        int ctxlen, n, ret = 0;
1.1.2.2   misho      61:        axlDoc *doc = NULL;
                     62:        axlError *err = NULL;
1.1.2.5   misho      63:        axlNode *nnode = NULL, *node = NULL;
1.1.2.2   misho      64:        struct tagReqXML xr;
                     65: 
                     66:        memset(str, 0, STRSIZ);
                     67:        memset(&xr, 0, sizeof xr);
1.1.2.5   misho      68:        while ((ch = getopt(argc, argv, "hvs:g:d:")) != -1)
1.1.2.2   misho      69:                switch (ch) {
                     70:                        case 'v':
                     71:                                Verbose++;
                     72:                                break;
1.1.2.5   misho      73:                        case 'd':
                     74:                                if (m) {
                     75:                                        Usage();
                     76:                                        return 1;
                     77:                                } else
                     78:                                        m = 'd';
                     79:                                strlcpy(str, optarg, STRSIZ);
                     80:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
                     81:                                        printf("Error:: in XML request %s\n", str);
                     82:                                        return 1;
                     83:                                }
                     84:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
                     85:                                break;
1.1.2.2   misho      86:                        case 's':
                     87:                                if (m) {
                     88:                                        Usage();
                     89:                                        return 1;
                     90:                                } else
                     91:                                        m = 's';
                     92:                                strlcpy(str, optarg, STRSIZ);
                     93:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
                     94:                                        printf("Error:: in XML request %s\n", str);
                     95:                                        return 1;
                     96:                                }
                     97:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
                     98:                                break;
                     99:                        case 'g':
                    100:                                if (m) {
                    101:                                        Usage();
                    102:                                        return 1;
                    103:                                } else
                    104:                                        m = 'g';
                    105:                                strlcpy(str, optarg, STRSIZ);
                    106:                                if ((ret = ioXMLGet(str, &xr)) < 1) {
                    107:                                        printf("Error:: in XML request %s\n", str);
                    108:                                        return 1;
                    109:                                }
                    110:                                VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret);
                    111:                                break;
                    112:                        case 'h':
                    113:                        default:
                    114:                                Usage();
                    115:                                return 1;
                    116:                }
                    117:        argc -= optind;
                    118:        argv += optind;
                    119:        if (!argc) {
                    120:                Usage();
                    121:                return 1;
                    122:        } else
                    123:                strlcpy(szName, *argv, MAXPATHLEN);
                    124:        if (argc > 1 && argv[1]) {
                    125:                xr.xml_data.value = argv[1];
                    126:                xr.xml_data.vallen = strlen(argv[1]);
                    127:        }
                    128: 
                    129:        axl_init();
                    130:        if (!(doc = axl_doc_parse_from_file(szName, &err))) {
                    131:                printf("Error:: xml file %s #%d - %s\n", szName, 
                    132:                                axl_error_get_code(err), axl_error_get(err));
                    133:                axl_error_free(err);
                    134:                axl_end();
                    135:                return 2;
                    136:        }
                    137:        if (!axl_ns_doc_validate(doc, &err)) {
                    138:                printf("Error:: xml file %s namespace validation #%d - %s\n", szName, 
                    139:                                axl_error_get_code(err), axl_error_get(err));
                    140:                axl_error_free(err);
                    141:                axl_end();
                    142:                return 2;
                    143:        }
                    144: 
                    145:        switch (m) {
                    146:                case 'g':
                    147:                        if (!xr.xml_namespace.vallen) {
1.1.2.3   misho     148:                                if (ret == 32) {
                    149:                                        if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) {
                    150:                                                printf("GET:: path %s - not found!\n", xr.xml_node.path.value);
                    151:                                                ret = 1;
                    152:                                                goto end;
                    153:                                        }
                    154:                                } else {
                    155:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
                    156:                                                printf("GET:: node %s - not found!\n", xr.xml_node.container.value);
                    157:                                                ret = 1;
                    158:                                                goto end;
                    159:                                        }
1.1.2.2   misho     160:                                }
                    161:                        } else {
                    162:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
                    163:                                strlcat(str, ":", sizeof str);
1.1.2.3   misho     164:                                strlcat(str, xr.xml_node.container.value, sizeof str);
                    165:                                if (ret == 32) {
                    166:                                        if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) {
                    167:                                                printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, 
                    168:                                                                xr.xml_node.path.value);
                    169:                                                ret = 1;
                    170:                                                goto end;
                    171:                                        }
                    172:                                } else {
                    173:                                        if (!(node = axl_doc_find_called(doc, str))) {
                    174:                                                printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
                    175:                                                                xr.xml_node.container.value);
                    176:                                                ret = 1;
                    177:                                                goto end;
                    178:                                        }
1.1.2.2   misho     179:                                }
                    180:                        }
                    181: 
1.1.2.3   misho     182:                        if (!(ret & 32) && xr.xml_data.vallen) {
1.1.2.2   misho     183:                                if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
                    184:                                        printf("GET:: data %s for node %s - not found!\n", 
1.1.2.3   misho     185:                                                        xr.xml_data.value, xr.xml_node.container.value);
1.1.2.2   misho     186:                                        ret = 1;
                    187:                                        goto end;
                    188:                                } else
                    189:                                        VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
                    190: 
                    191:                                VERB(1) printf("\n");
1.1.2.3   misho     192:                                if (!strcmp(ctx, xr.xml_data.value))
1.1.2.2   misho     193:                                        printf("DATA::1\n");
                    194:                                else
                    195:                                        printf("DATA::0\n");
                    196:                        }
                    197: 
1.1.2.3   misho     198:                        if (!(ret & 32) && xr.xml_attribute.vallen) {
                    199:                                if ((n = axl_node_num_attributes(node)) < 1) {
1.1.2.2   misho     200:                                        printf("GET:: attribute %s for node %s - not found!\n", 
1.1.2.3   misho     201:                                                        xr.xml_attribute.value, xr.xml_node.container.value);
1.1.2.2   misho     202:                                        ret = 1;
                    203:                                        goto end;
                    204:                                } else {
1.1.2.3   misho     205:                                        VERB(1) printf("Verbose:: node have %d attributes\n", n);
1.1.2.2   misho     206: 
                    207:                                        if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) {
                    208:                                                printf("GET:: attribute %s for node %s - not found!\n", 
1.1.2.3   misho     209:                                                                xr.xml_attribute.value, xr.xml_node.container.value);
1.1.2.2   misho     210:                                                ret = 1;
                    211:                                                goto end;
                    212:                                        }
                    213: 
                    214:                                        if (xr.xml_value.vallen) {
1.1.2.3   misho     215:                                                if (!strcmp(ctx, xr.xml_value.value))
1.1.2.2   misho     216:                                                        ctx = "VALUE::1";
                    217:                                                else
                    218:                                                        ctx = "VALUE::0";
                    219:                                        }
                    220:                                }
                    221:                        } else {
1.1.2.3   misho     222:                                if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
                    223:                                        printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value);
                    224:                                        ret = 1;
                    225:                                        goto end;
                    226:                                } else
                    227:                                        VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
1.1.2.2   misho     228:                        }
                    229: 
                    230:                        VERB(1) printf("\n");
                    231:                        printf("%s\n", ctx);
                    232:                        ret = 0;
                    233:                        break;
1.1.2.5   misho     234:                case 'd':
                    235:                        if (!xr.xml_namespace.vallen) {
                    236:                                if (ret == 32) {
                    237:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
                    238:                                                printf("DEL:: path %s - not found!\n", xr.xml_node.path.value);
                    239:                                                ret = 1;
                    240:                                                goto end;
                    241:                                        }
                    242:                                } else {
                    243:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
                    244:                                                printf("DEL:: node %s - not found!\n", xr.xml_node.container.value);
                    245:                                                ret = 1;
                    246:                                                goto end;
                    247:                                        }
                    248:                                }
                    249:                        } else {
                    250:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
                    251:                                strlcat(str, ":", sizeof str);
                    252:                                strlcat(str, xr.xml_node.container.value, sizeof str);
                    253:                                if (ret == 32) {
                    254:                                        if (!(node = axl_doc_get(doc, str))) {
                    255:                                                printf("DEL:: path %s:%s - not found!\n", xr.xml_namespace.value, 
                    256:                                                                xr.xml_node.path.value);
                    257:                                                ret = 1;
                    258:                                                goto end;
                    259:                                        }
                    260:                                } else {
                    261:                                        if (!(node = axl_doc_find_called(doc, str))) {
                    262:                                                printf("DEL:: node %s:%s - not found!\n", xr.xml_namespace.value, 
                    263:                                                                xr.xml_node.container.value);
                    264:                                                ret = 1;
                    265:                                                goto end;
                    266:                                        }
                    267:                                }
                    268:                        }
                    269: 
                    270:                        axl_node_remove(node, 1);
                    271:                        ret = ShowXML(doc, NULL);
                    272:                        break;
1.1.2.4   misho     273:                case 's':
1.1.2.5   misho     274:                        if (ret == 32) {
1.1.2.6 ! misho     275:                                if (!xr.xml_data.vallen || !(nnode = axl_node_create(xr.xml_data.value))) {
1.1.2.5   misho     276:                                        printf("SET:: container %s at path %s - Error!\n", 
                    277:                                                        xr.xml_data.value, xr.xml_node.path.value);
                    278:                                        ret = 1;
                    279:                                        goto end;
                    280:                                }
                    281:                        }
1.1.2.4   misho     282:                        if (!xr.xml_namespace.vallen) {
                    283:                                if (ret == 32) {
                    284:                                        // insert new
1.1.2.5   misho     285:                                        if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) {
                    286:                                                printf("SET:: path %s - not found!\n", xr.xml_node.path.value);
                    287:                                                axl_node_free(nnode);
                    288:                                                ret = 1;
                    289:                                                goto end;
                    290:                                        }
1.1.2.4   misho     291:                                } else {
                    292:                                        // update old
                    293:                                        if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
                    294:                                                printf("SET:: node %s - not found!\n", xr.xml_node.container.value);
                    295:                                                ret = 1;
                    296:                                                goto end;
                    297:                                        }
                    298:                                }
                    299:                        } else {
                    300:                                strlcpy(str, xr.xml_namespace.value, sizeof str);
                    301:                                strlcat(str, ":", sizeof str);
                    302:                                strlcat(str, xr.xml_node.container.value, sizeof str);
                    303:                                if (ret == 32) {
                    304:                                        // insert new
1.1.2.5   misho     305:                                        if (!(node = axl_doc_get(doc, str))) {
                    306:                                                printf("SET:: path %s:%s - not found!\n", xr.xml_namespace.value, 
                    307:                                                                xr.xml_node.path.value);
                    308:                                                axl_node_free(nnode);
                    309:                                                ret = 1;
                    310:                                                goto end;
                    311:                                        }
1.1.2.4   misho     312:                                } else {
                    313:                                        // update old
                    314:                                        if (!(node = axl_doc_find_called(doc, str))) {
                    315:                                                printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
                    316:                                                                xr.xml_node.container.value);
                    317:                                                ret = 1;
                    318:                                                goto end;
                    319:                                        }
                    320:                                }
                    321:                        }
                    322: 
                    323:                        if (!(ret & 32) && xr.xml_data.vallen) {
                    324:                                axl_node_set_is_empty(node, 1);
                    325:                                axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen);
                    326:                        }
                    327:                        if (!(ret & 32) && xr.xml_attribute.vallen) {
                    328:                                axl_node_remove_attribute(node, xr.xml_attribute.value);
                    329:                                axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value);
                    330:                        }
                    331: 
1.1.2.5   misho     332:                        if (ret & 32)
                    333:                                axl_node_set_child(node, nnode);
                    334:                        ret = ShowXML(doc, NULL);
                    335:                        break;
1.1.2.2   misho     336:                default:
1.1.2.5   misho     337:                        ret = ShowXML(doc, xr.xml_data.vallen ? xr.xml_data.value : NULL);
1.1.2.2   misho     338:        }
                    339: end:
                    340:        axl_doc_free(doc);
                    341:        axl_end();
                    342:        return ret;
1.1.2.1   misho     343: }

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