|
version 1.1.2.2, 2010/09/27 18:19:57
|
version 1.2.2.2, 2011/06/13 20:23:35
|
|
Line 1
|
Line 1
|
| |
/************************************************************************* |
| |
* (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com> |
| |
* by Michael Pounov <misho@aitbg.com> |
| |
* |
| |
* $Author$ |
| |
* $Id$ |
| |
* |
| |
************************************************************************* |
| |
The ELWIX and AITNET software is distributed under the following |
| |
terms: |
| |
|
| |
All of the documentation and software included in the ELWIX and AITNET |
| |
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
| |
|
| |
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
| |
by Michael Pounov <misho@elwix.org>. All rights reserved. |
| |
|
| |
Redistribution and use in source and binary forms, with or without |
| |
modification, are permitted provided that the following conditions |
| |
are met: |
| |
1. Redistributions of source code must retain the above copyright |
| |
notice, this list of conditions and the following disclaimer. |
| |
2. Redistributions in binary form must reproduce the above copyright |
| |
notice, this list of conditions and the following disclaimer in the |
| |
documentation and/or other materials provided with the distribution. |
| |
3. All advertising materials mentioning features or use of this software |
| |
must display the following acknowledgement: |
| |
This product includes software developed by Michael Pounov <misho@elwix.org> |
| |
ELWIX - Embedded LightWeight unIX and its contributors. |
| |
4. Neither the name of AITNET nor the names of its contributors |
| |
may be used to endorse or promote products derived from this software |
| |
without specific prior written permission. |
| |
|
| |
THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND |
| |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| |
SUCH DAMAGE. |
| |
*/ |
| #include "global.h" |
#include "global.h" |
| #include <axl.h> |
#include <axl.h> |
| #include <axl_ns.h> |
#include <axl_ns.h> |
| #include <axl_decl.h> |
#include <axl_decl.h> |
| #include "xmler.h" |
|
| |
|
| |
|
| int Verbose; |
int Verbose; |
| extern char compiled[], compiledby[], compilehost[]; |
extern char compiled[], compiledby[], compilehost[]; |
| |
|
| |
|
| static void Usage() | static void |
| | Usage() |
| { |
{ |
| |
|
| printf( "XMLer is tool for managment R/W operation with XMLs\n" |
printf( "XMLer is tool for managment R/W operation with XMLs\n" |
| "=== %s === %s@%s ===\n\n" |
"=== %s === %s@%s ===\n\n" |
| " Syntax: xmler [options] <file.xml> [data]\n\n" |
" Syntax: xmler [options] <file.xml> [data]\n\n" |
| "\t-v\t\tVerbose ...\n" |
"\t-v\t\tVerbose ...\n" |
| "\t-s <av_pair>\tSet Attribute/Value pair\n" | "\t-l <av_pair>\tList node\n" |
| "\t-g <av_pair>\tGet Attribute/Value pair\n" | "\t-d <av_pair>\tDelete node\n" |
| | "\t-s <av_pair>\tSet node command\n" |
| | "\t-g <av_pair>\tGet node command\n" |
| "*\"av_pair\" format: [ns:]node[[|attribute[=value]]?data]\n" |
"*\"av_pair\" format: [ns:]node[[|attribute[=value]]?data]\n" |
| "\n", compiled, compiledby, compilehost); |
"\n", compiled, compiledby, compilehost); |
| } |
} |
| |
|
| |
static int |
| |
ShowXML(axlDoc *doc, const char *csNode) |
| |
{ |
| |
axlNode *node = NULL; |
| |
int ctxlen; |
| |
char *ctx = NULL; |
| |
|
| |
if (csNode) { |
| |
if (!(node = axl_doc_find_called(doc, csNode))) { |
| |
printf("GET:: node %s - not found!\n", csNode); |
| |
return 1; |
| |
} |
| |
|
| |
axl_node_dump_pretty(node, &ctx, &ctxlen, 4); |
| |
VERB(1) printf("Verbose:: Node length=%d\n", ctxlen); |
| |
} else { |
| |
axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4); |
| |
VERB(1) printf("Verbose:: Document length=%d\n", ctxlen); |
| |
} |
| |
|
| |
VERB(1) printf("\n"); |
| |
if (ctx) { |
| |
printf("%s", ctx); |
| |
free(ctx); |
| |
} |
| |
VERB(1) printf("\n"); |
| |
return 0; |
| |
} |
| |
|
| |
static int |
| |
ShowItem(axlNode *node, int lvl) |
| |
{ |
| |
register int i; |
| |
int ctxlen; |
| |
axlNode *child; |
| |
|
| |
for (i = 0; i < axl_node_get_child_num(node); i++) { |
| |
child = axl_node_get_child_nth(node, i); |
| |
|
| |
if (!lvl) |
| |
printf("%s %s \"%s\"", axl_node_get_name(node), axl_node_get_name(child), |
| |
axl_node_get_content(child, &ctxlen)); |
| |
else |
| |
printf(" %s \"%s\"", axl_node_get_name(child), axl_node_get_content(child, &ctxlen)); |
| |
|
| |
ShowItem(child, 1); |
| |
|
| |
if (!lvl) |
| |
printf("\n"); |
| |
} |
| |
|
| |
return 0; |
| |
} |
| |
|
| int |
int |
| main(int argc, char **argv) |
main(int argc, char **argv) |
| { |
{ |
| char ch, *ctx, str[STRSIZ], szName[MAXPATHLEN], m = 0; | char ch, str[STRSIZ], szName[MAXPATHLEN], *ctx = NULL, m = 0; |
| int ctxlen, ret = 0; | int ctxlen, n, ret = 0; |
| axlDoc *doc = NULL; |
axlDoc *doc = NULL; |
| axlError *err = NULL; |
axlError *err = NULL; |
| axlNode *node = NULL; | axlNode *nnode = NULL, *node = NULL; |
| struct tagReqXML xr; |
struct tagReqXML xr; |
| |
|
| memset(str, 0, STRSIZ); |
memset(str, 0, STRSIZ); |
| memset(&xr, 0, sizeof xr); |
memset(&xr, 0, sizeof xr); |
| while ((ch = getopt(argc, argv, "hvs:g:")) != -1) | while ((ch = getopt(argc, argv, "hvl:s:g:d:")) != -1) |
| switch (ch) { |
switch (ch) { |
| case 'v': |
case 'v': |
| Verbose++; |
Verbose++; |
| break; |
break; |
| |
case 'l': |
| |
if (m) { |
| |
Usage(); |
| |
return 1; |
| |
} else |
| |
m = 'l'; |
| |
strlcpy(str, optarg, STRSIZ); |
| |
if ((ret = ioXMLGet(str, &xr)) < 1) { |
| |
printf("Error:: in XML request %s\n", str); |
| |
return 1; |
| |
} |
| |
VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret); |
| |
break; |
| |
case 'd': |
| |
if (m) { |
| |
Usage(); |
| |
return 1; |
| |
} else |
| |
m = 'd'; |
| |
strlcpy(str, optarg, STRSIZ); |
| |
if ((ret = ioXMLGet(str, &xr)) < 1) { |
| |
printf("Error:: in XML request %s\n", str); |
| |
return 1; |
| |
} |
| |
VERB(3) printf("Verbose(3):: XMLGet=0x%x\n", ret); |
| |
break; |
| case 's': |
case 's': |
| if (m) { |
if (m) { |
| Usage(); |
Usage(); |
|
Line 99 main(int argc, char **argv)
|
Line 226 main(int argc, char **argv)
|
| } |
} |
| |
|
| switch (m) { |
switch (m) { |
| case 's': |
|
| break; |
|
| case 'g': |
case 'g': |
| if (!xr.xml_namespace.vallen) { |
if (!xr.xml_namespace.vallen) { |
| if (!(node = axl_doc_find_called(doc, xr.xml_container.value))) { | if (ret == 32) { |
| printf("GET:: node %s - not found!\n", xr.xml_container.value); | if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) { |
| ret = 1; | printf("GET:: path %s - not found!\n", xr.xml_node.path.value); |
| goto end; | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { |
| | printf("GET:: node %s - not found!\n", xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| } |
} |
| } else { |
} else { |
| strlcpy(str, xr.xml_namespace.value, sizeof str); |
strlcpy(str, xr.xml_namespace.value, sizeof str); |
| strlcat(str, ":", sizeof str); |
strlcat(str, ":", sizeof str); |
| strlcat(str, xr.xml_container.value, sizeof str); | strlcat(str, xr.xml_node.container.value, sizeof str); |
| if (!(node = axl_doc_find_called(doc, str))) { | if (ret == 32) { |
| printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, | if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) { |
| xr.xml_container.value); | printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, |
| ret = 1; | xr.xml_node.path.value); |
| goto end; | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | if (!(node = axl_doc_find_called(doc, str))) { |
| | printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, |
| | xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| } |
} |
| } |
} |
| |
|
| if (xr.xml_data.vallen) { | if (!(ret & 32) && xr.xml_data.vallen) { |
| if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) { |
if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) { |
| axl_node_free(node); |
|
| printf("GET:: data %s for node %s - not found!\n", |
printf("GET:: data %s for node %s - not found!\n", |
| xr.xml_data.value, xr.xml_attribute.value); | xr.xml_data.value, xr.xml_node.container.value); |
| ret = 1; |
ret = 1; |
| goto end; |
goto end; |
| } else |
} else |
| VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); |
VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); |
| |
|
| VERB(1) printf("\n"); |
VERB(1) printf("\n"); |
| if (!(ret = strcmp(ctx, xr.xml_data.value))) | if (!strcmp(ctx, xr.xml_data.value)) |
| printf("DATA::1\n"); |
printf("DATA::1\n"); |
| else |
else |
| printf("DATA::0\n"); |
printf("DATA::0\n"); |
| } |
} |
| |
|
| if (xr.xml_attribute.vallen) { | if (!(ret & 32) && xr.xml_attribute.vallen) { |
| if ((ret = axl_node_num_attributes(node)) < 1) { | if ((n = axl_node_num_attributes(node)) < 1) { |
| axl_node_free(node); | |
| printf("GET:: attribute %s for node %s - not found!\n", |
printf("GET:: attribute %s for node %s - not found!\n", |
| xr.xml_container.value, xr.xml_attribute.value); | xr.xml_attribute.value, xr.xml_node.container.value); |
| ret = 1; |
ret = 1; |
| goto end; |
goto end; |
| } else { |
} else { |
| VERB(1) printf("Verbose:: node have %d attributes\n", ret); | VERB(1) printf("Verbose:: node have %d attributes\n", n); |
| |
|
| if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) { |
if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) { |
| axl_node_free(node); |
|
| printf("GET:: attribute %s for node %s - not found!\n", |
printf("GET:: attribute %s for node %s - not found!\n", |
| xr.xml_container.value, xr.xml_attribute.value); | xr.xml_attribute.value, xr.xml_node.container.value); |
| ret = 1; |
ret = 1; |
| goto end; |
goto end; |
| } |
} |
| |
|
| if (xr.xml_value.vallen) { |
if (xr.xml_value.vallen) { |
| if (!(ret = strcmp(ctx, xr.xml_value.value))) | if (!strcmp(ctx, xr.xml_value.value)) |
| ctx = "VALUE::1"; |
ctx = "VALUE::1"; |
| else |
else |
| ctx = "VALUE::0"; |
ctx = "VALUE::0"; |
| } |
} |
| } |
} |
| } else { |
} else { |
| axl_node_dump_pretty(node, &ctx, &ctxlen, 4); | if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) { |
| VERB(1) printf("Verbose:: Node length=%d\n", ctxlen); | printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } else |
| | VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); |
| } |
} |
| |
|
| VERB(1) printf("\n"); |
VERB(1) printf("\n"); |
| printf("%s\n", ctx); |
printf("%s\n", ctx); |
| axl_node_free(node); |
|
| ret = 0; |
ret = 0; |
| break; |
break; |
| default: | case 'd': |
| axl_doc_dump_pretty(doc, &ctx, &ctxlen, 4); | if (!xr.xml_namespace.vallen) { |
| VERB(1) printf("Verbose:: Document length=%d\n", ctxlen); | if (ret == 32) { |
| | if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { |
| | printf("DEL:: path %s - not found!\n", xr.xml_node.path.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { |
| | printf("DEL:: node %s - not found!\n", xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } |
| | } else { |
| | strlcpy(str, xr.xml_namespace.value, sizeof str); |
| | strlcat(str, ":", sizeof str); |
| | strlcat(str, xr.xml_node.container.value, sizeof str); |
| | if (ret == 32) { |
| | if (!(node = axl_doc_get(doc, str))) { |
| | printf("DEL:: path %s:%s - not found!\n", xr.xml_namespace.value, |
| | xr.xml_node.path.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | if (!(node = axl_doc_find_called(doc, str))) { |
| | printf("DEL:: node %s:%s - not found!\n", xr.xml_namespace.value, |
| | xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } |
| | } |
| |
|
| VERB(1) printf("\n"); | axl_node_remove(node, 1); |
| printf("%s\n", ctx); | ret = ShowXML(doc, NULL); |
| free(ctx); | break; |
| | case 's': |
| | if (ret == 32) { |
| | if (!xr.xml_data.vallen || !(nnode = axl_node_create(xr.xml_data.value))) { |
| | printf("SET:: container %s at path %s - Error!\n", |
| | xr.xml_data.value, xr.xml_node.path.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } |
| | if (!xr.xml_namespace.vallen) { |
| | if (ret == 32) { |
| | // insert new |
| | if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { |
| | printf("SET:: path %s - not found!\n", xr.xml_node.path.value); |
| | axl_node_free(nnode); |
| | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | // update old |
| | if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { |
| | printf("SET:: node %s - not found!\n", xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } |
| | } else { |
| | strlcpy(str, xr.xml_namespace.value, sizeof str); |
| | strlcat(str, ":", sizeof str); |
| | strlcat(str, xr.xml_node.container.value, sizeof str); |
| | if (ret == 32) { |
| | // insert new |
| | if (!(node = axl_doc_get(doc, str))) { |
| | printf("SET:: path %s:%s - not found!\n", xr.xml_namespace.value, |
| | xr.xml_node.path.value); |
| | axl_node_free(nnode); |
| | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | // update old |
| | if (!(node = axl_doc_find_called(doc, str))) { |
| | printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, |
| | xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } |
| | } |
| | |
| | if (!(ret & 32) && xr.xml_data.vallen) { |
| | axl_node_set_is_empty(node, 1); |
| | axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen); |
| | } |
| | if (!(ret & 32) && xr.xml_attribute.vallen) { |
| | axl_node_remove_attribute(node, xr.xml_attribute.value); |
| | axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value); |
| | } |
| | |
| | if (ret & 32) |
| | axl_node_set_child(node, nnode); |
| | ret = ShowXML(doc, NULL); |
| | break; |
| | case 'l': |
| | if (!xr.xml_namespace.vallen) { |
| | if (ret == 32) { |
| | if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { |
| | printf("LST:: path %s - not found!\n", xr.xml_node.path.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { |
| | printf("LST:: node %s - not found!\n", xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } |
| | } else { |
| | strlcpy(str, xr.xml_namespace.value, sizeof str); |
| | strlcat(str, ":", sizeof str); |
| | strlcat(str, xr.xml_node.container.value, sizeof str); |
| | if (ret == 32) { |
| | if (!(node = axl_doc_get(doc, str))) { |
| | printf("LST:: path %s:%s - not found!\n", xr.xml_namespace.value, |
| | xr.xml_node.path.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } else { |
| | if (!(node = axl_doc_find_called(doc, str))) { |
| | printf("LST:: node %s:%s - not found!\n", xr.xml_namespace.value, |
| | xr.xml_node.container.value); |
| | ret = 1; |
| | goto end; |
| | } |
| | } |
| | } |
| | |
| | ret = ShowItem(node, 0); |
| | break; |
| | default: |
| | ret = ShowXML(doc, xr.xml_data.vallen ? xr.xml_data.value : NULL); |
| } |
} |
| end: |
end: |
| axl_doc_free(doc); |
axl_doc_free(doc); |