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