File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / Attic / xmler.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Sun Jul 22 22:46:48 2012 UTC (11 years, 11 months ago) by misho
Branches: MAIN
CVS tags: tools2_0, tools1_2, TOOLS1_2, TOOLS1_1, HEAD
version 1.1

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

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