File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / Attic / xmler.c
Revision 1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Tue Sep 28 11:37:04 2010 UTC (13 years, 9 months ago) by misho
Branches: tools1_0
start set support

    1: #include "global.h"
    2: #include <axl.h>
    3: #include <axl_ns.h>
    4: #include <axl_decl.h>
    5: #include "xmler.h"
    6: 
    7: 
    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: 
   25: int
   26: main(int argc, char **argv)
   27: {
   28: 	char ch, *ctx, str[STRSIZ], szName[MAXPATHLEN], m = 0;
   29: 	int ctxlen, n, 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 'g':
  103: 			if (!xr.xml_namespace.vallen) {
  104: 				if (ret == 32) {
  105: 					if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) {
  106: 						printf("GET:: path %s - not found!\n", xr.xml_node.path.value);
  107: 						ret = 1;
  108: 						goto end;
  109: 					}
  110: 				} else {
  111: 					if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
  112: 						printf("GET:: node %s - not found!\n", xr.xml_node.container.value);
  113: 						ret = 1;
  114: 						goto end;
  115: 					}
  116: 				}
  117: 			} else {
  118: 				strlcpy(str, xr.xml_namespace.value, sizeof str);
  119: 				strlcat(str, ":", sizeof str);
  120: 				strlcat(str, xr.xml_node.container.value, sizeof str);
  121: 				if (ret == 32) {
  122: 					if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) {
  123: 						printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, 
  124: 								xr.xml_node.path.value);
  125: 						ret = 1;
  126: 						goto end;
  127: 					}
  128: 				} else {
  129: 					if (!(node = axl_doc_find_called(doc, str))) {
  130: 						printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
  131: 								xr.xml_node.container.value);
  132: 						ret = 1;
  133: 						goto end;
  134: 					}
  135: 				}
  136: 			}
  137: 
  138: 			if (!(ret & 32) && xr.xml_data.vallen) {
  139: 				if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
  140: 					printf("GET:: data %s for node %s - not found!\n", 
  141: 							xr.xml_data.value, xr.xml_node.container.value);
  142: 					ret = 1;
  143: 					goto end;
  144: 				} else
  145: 					VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
  146: 
  147: 				VERB(1) printf("\n");
  148: 				if (!strcmp(ctx, xr.xml_data.value))
  149: 					printf("DATA::1\n");
  150: 				else
  151: 					printf("DATA::0\n");
  152: 			}
  153: 
  154: 			if (!(ret & 32) && xr.xml_attribute.vallen) {
  155: 				if ((n = axl_node_num_attributes(node)) < 1) {
  156: 					printf("GET:: attribute %s for node %s - not found!\n", 
  157: 							xr.xml_attribute.value, xr.xml_node.container.value);
  158: 					ret = 1;
  159: 					goto end;
  160: 				} else {
  161: 					VERB(1) printf("Verbose:: node have %d attributes\n", n);
  162: 
  163: 					if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) {
  164: 						printf("GET:: attribute %s for node %s - not found!\n", 
  165: 								xr.xml_attribute.value, xr.xml_node.container.value);
  166: 						ret = 1;
  167: 						goto end;
  168: 					}
  169: 
  170: 					if (xr.xml_value.vallen) {
  171: 						if (!strcmp(ctx, xr.xml_value.value))
  172: 							ctx = "VALUE::1";
  173: 						else
  174: 							ctx = "VALUE::0";
  175: 					}
  176: 				}
  177: 			} else {
  178: 				if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) {
  179: 					printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value);
  180: 					ret = 1;
  181: 					goto end;
  182: 				} else
  183: 					VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen);
  184: 			}
  185: 
  186: 			VERB(1) printf("\n");
  187: 			printf("%s\n", ctx);
  188: 			ret = 0;
  189: 			break;
  190: 		case 's':
  191: 			if (!xr.xml_namespace.vallen) {
  192: 				if (ret == 32) {
  193: 					// insert new
  194: 				} else {
  195: 					// update old
  196: 					if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) {
  197: 						printf("SET:: node %s - not found!\n", xr.xml_node.container.value);
  198: 						ret = 1;
  199: 						goto end;
  200: 					}
  201: 				}
  202: 			} else {
  203: 				strlcpy(str, xr.xml_namespace.value, sizeof str);
  204: 				strlcat(str, ":", sizeof str);
  205: 				strlcat(str, xr.xml_node.container.value, sizeof str);
  206: 				if (ret == 32) {
  207: 					// insert new
  208: 				} else {
  209: 					// update old
  210: 					if (!(node = axl_doc_find_called(doc, str))) {
  211: 						printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, 
  212: 								xr.xml_node.container.value);
  213: 						ret = 1;
  214: 						goto end;
  215: 					}
  216: 				}
  217: 			}
  218: 
  219: 			if (!(ret & 32) && xr.xml_data.vallen) {
  220: 				axl_node_set_is_empty(node, 1);
  221: 				axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen);
  222: 			}
  223: 			if (!(ret & 32) && xr.xml_attribute.vallen) {
  224: 				axl_node_remove_attribute(node, xr.xml_attribute.value);
  225: 				axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value);
  226: 			}
  227: 
  228: 			xr.xml_data = xr.xml_node.container;
  229: 			ret = 0;
  230: 		default:
  231: 			if (xr.xml_data.vallen) {
  232: 				if (!(node = axl_doc_find_called(doc, xr.xml_data.value))) {
  233: 					printf("GET:: node %s - not found!\n", xr.xml_data.value);
  234: 					ret = 1;
  235: 					goto end;
  236: 				}
  237: 				axl_node_dump_pretty(node, &ctx, &ctxlen, 4);
  238: 				VERB(1) printf("Verbose:: Node length=%d\n", ctxlen);
  239: 			} else {
  240: 				axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4);
  241: 				VERB(1) printf("Verbose:: Document length=%d\n", ctxlen);
  242: 			}
  243: 
  244: 			VERB(1) printf("\n");
  245: 			printf("%s\n", ctx);
  246: 			if (ctx)
  247: 				free(ctx);
  248: 	}
  249: end:
  250: 	axl_doc_free(doc);
  251: 	axl_end();
  252: 	return ret;
  253: }

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