diff --git a/utils/xmlgrep/ChangeLog b/utils/xmlgrep/ChangeLog deleted file mode 100644 index 2f8e4c26b..000000000 --- a/utils/xmlgrep/ChangeLog +++ /dev/null @@ -1,111 +0,0 @@ -24-05-2009 - * Add a node cache that can be enabled at compile time. - the node cache prevents recursively walking the xml tree over and over - again to find the specified nodes. - -05-05-2009 - * Various bugfixes, required to get fgrun working - * add testxml as sort of a stress test application - -30-04-2009 - * Add support for CDATA - * Fix an off by one problem. - -28-04-2009 - * changes to the code to allow walking the xml-tree using "*" as a node name - * add printxml, an example utility that walks an xml-tree and prints it - contenst - -27-04-2009 - * add xmlInitBuffer() for processing of a preallocated buffer - * add xmlErrorGetColumnNo to get the column number of the syntax error - * pass an error at a higher level to lower levels - * detect a number of extra syntax errors - -26-04-2009 - * add support for comments inside xml-tags, e.g.: - -25-04-2009 - * add support for self-contained tags like - * fix a problem if a file could not be mmaped - * add a few comments which hopefully makes the code easier to understand - * code cleanups - -20-04-2009 - * fix a case where a single-element root path (e.g. "/printer") would not - pass xmlNodeGetPath - * fix a problem where attributes or elements starting with the same letter - sequence could give a false negative result - * Add a 'clear' attribute to the xmlErrorGet functions that indicates whether - the error should be cleared or not - * detect more xml syntax errors - -18-04-2009 - * Make the code compiler correctly under windows - * Introduce a root-node that can hold extra information which is necessary - for thread safety under windows - * Add xmlErrorGetString, xmlErrorGetLineNo for syntax error detetction - * Add xmlErrGetNo for detection of, and clearing the last error - -16-04-2009 - * Rename xmlGetNode functions to xmlNodeGet for better consistancy - * likewise for xmlCopyNode en xmlCompareNode - * add xmlAttributeGetDouble, xmlAttributeGetInt, xmlAttributeGetString - xmlAttributeCopyString and xmlAttributeCompareString functions - * fix some small bugs and problems along the way - * add support for filtering on attribute value in xmlgrep - -21-07-2008 - * change a number of function parameters to const where appropriate - * fix a problem where the wrong node-name length was returned - * xmlgrep now also works when only the -e options is specified - * fix xmlgrep to show the correct node-name (it reported the parent - node-name in the previous version) - -20-07-2008 - * fix __xmlSkipComment to properly find the end of comment tag. - * add the xmlGetNodeName and xmlCopyNodeName functions - * add the xmlCopyString function - * clean up some code - -19-07-2008 - * rewrite the code to always recursively walk the node tree when searching - for a particular node. this is required for cases where a node with a - particular name is located deeper in a node with the same name; - for example -r /configuration/device/reference/device would fail in the - previous verion - * rename xmlGetElement to xmlGetNodeNum and add the possibility to request - the nth node with this name - * rename xmlGetNumElements to xmlGetNumNodes - -06-07-2008 - * reorganize the code to be able to skip comment sections - * depreciate __xmlFindNextElement and use __xmlGetNode instead - * xmlGetNextElement now returns char* instead of void* for furute use - * add preliminary support for wildcards in the search path ('*' and '?') - -01-07-2008 - * fix a problem caused by removing the last unnecessary alloc - * strip leading-, and trailing spaces from the string before comparing - * fix a problem where trailing spaces weren't removed - -30-06-2008: - * some small changes; fix some typo's and fix a small memory leak - * update the documentation in README - * remove the last unnecessary alloc - -29-06-2008: - * rename xmlGet(Int/Double/String) to xmlGetNode(Int/Double/String) - * add new xmlGet(Int/Double/String) functions - * rename xmlCompareString to xmlCompareNodeString for consistency - * rename xmlCompareElement to xmlCompareString for consistency - * add a README file with short examples of various functions - -27-06-2008: - * removed some memory allocation in xmlGetNode and XMLGetNextElement - * use the file-size for mmap and remove the root node from the xml-id - * rearrange xmlGetNode to work with complicated xml files - * add the xmlMarkId function to save the id before using xmlGetNextElement - * speed up xmlGetNextId - -23-06-2008: Initial release diff --git a/utils/xmlgrep/README b/utils/xmlgrep/README index 33294ffbd..72ab606ea 100644 --- a/utils/xmlgrep/README +++ b/utils/xmlgrep/README @@ -1,146 +1,6 @@ -This library is specially designed for reading xml configuration files and -to be as low on memory management as possible. Modifying or writing xml files -is not planned for the future. In most situations being able to gather data -by reading an xml file is more than enough and the read-only decision -provides a number of advantages over a one-size fits all approach. For isntance -the memory footprint can be kept low and the library can be kept simple. -To achieve these goals the mmap function is used to map the configuration file -to a memory region. The only places where memory is allocated is when creating -a new XML-id, when requesting a string from a node, when requestiong the node -name or when a request is made to copy a node into a new memory region. +The xmlgrep utility is part op the ZeroXML package which is now hosted at: +http://www.adalin.com -Using this library should be pretty simple for most tasks; just open a file, -read every parameter one by one and close the id again. -{ - void *xid; +Any further development will take place there. - xid = xmlOpen("/tmp/file.xml"); - xpos = xmlNodeGetDouble(xid, "/configuration/x-pos"); - ypos = xmlNodeGetDouble(xid, "/configuration/y-pos"); - zpos = xmlNodeGetDouble(xid, "/configuration/z-pos"); - xmlClose(xid); -} - -While it is certainly possible to access every node directly by calling the -xmlNodeGet(Int/Double/String) functions, when more than one node need to be -gathered from a parent node it is advised to get the id of the parent node -and work from there since the XML-id holds the boundaries of the (parent)node -which limits the searching area resulting in improved searching speed. -{ - void *xnid; - char *s; - - xnid = xmlNodeGet(id, "/configuration/setup/"); - version = xmlNodeGetDouble(xnid, "version"); - s = xmlNodeGetString(xnid, "author"); - if (s) author = s; - free(s); - free(xnid); -} - -Overview of the available functions: - ----------------------------------------------------------------------------- -# -# Functions to Open and Close the XML file -# e.g. -# id = xmlOpen("/tmp/file.xml"); -# xmlClose(id); -# -void *xmlOpen(const char *filename); -void *xmlInitBuffer(const char *buffer, size_t size); -void xmlClose(void *xid); - -# -# Get the Id of a node at the specified path -# e.g. -# xnid = xmlNodeGet(id, "/path/to/specified/node"); -# -void *xmlNodeGet(const void *xid, const char *path); -void *xmlNodeCopy(const void *xid, const char *path); - -# -# Functions to walk the node tree and process them one by one. -# e.g. -# xmid = xmlMarkId(id); -# num = xmlNodeGetNum(xmid, "node"); -# for (i=0; i -#include - -#include "xml.h" - -void print_xml(void *, char *, unsigned int); - -int main(int argc, char **argv) -{ - if (argc < 1) - { - printf("usage: printtree \n\n"); - } - else - { - void *rid; - - rid = xmlOpen(argv[1]); - if (xmlErrorGetNo(rid, 0) != XML_NO_ERROR) - { - printf("%s\n", xmlErrorGetString(rid, 1)); - } - else if (rid) - { - unsigned int i, num; - void *xid; - - xid = xmlMarkId(rid); - num = xmlNodeGetNum(xid, "*"); - for (i=0; i -#include - -#include "xml.h" - -void print_xml(void *); - -int main(int argc, char **argv) -{ - if (argc < 1) - { - printf("usage: printxml \n\n"); - } - else - { - void *rid; - - rid = xmlOpen(argv[1]); - if (xmlErrorGetNo(rid, 0) != XML_NO_ERROR) - { - printf("%s\n", xmlErrorGetString(rid, 1)); - } - else if (rid) - { - unsigned int i, num; - void *xid; - - xid = xmlMarkId(rid); - num = xmlNodeGetNum(xid, "*"); - for (i=0; i\n", name); - print_xml(xid); - printf("\n\n", name); - } - } - free(xid); - - xmlClose(rid); - } - else - { - printf("Error while opening file for reading: '%s'\n", argv[1]); - } - } -} - -void print_xml(void *id) -{ - static int level = 1; - void *xid = xmlMarkId(id); - unsigned int i, num; - - num = xmlNodeGetNum(xid, "*"); - if (num == 0) - { - char *s; - s = xmlGetString(xid); - if (s) - { - printf("%s", s); - free(s); - } - } - else - { - unsigned int i, q; - for (i=0; i", name); - level++; - print_xml(xid); - level--; - printf("", name); - } - else printf("error\n"); - } - printf("\n"); - for(q=1; q - - - - - 48000 - 20 - 2 - - - - - - - * Traffic, # taxiing to runway (. - * Traffic, # taxiing to runway (. - - - - 0 - 1.0 - - - - - 1 - 1.0 - - - - - - - - - ALSA Hardware - - hw:0 - 2 - - 44100 - 16 - - - default - 44100 - - - - - - - - - - diff --git a/utils/xmlgrep/testxml.c b/utils/xmlgrep/testxml.c deleted file mode 100644 index 732f0815a..000000000 --- a/utils/xmlgrep/testxml.c +++ /dev/null @@ -1,188 +0,0 @@ -#include -#include -#include -#include "xml.h" - -#define ROOTNODE "/Configuration/output/menu" -#define LEAFNODE "name" -#define PATH ROOTNODE"/"LEAFNODE -#define BUFLEN 4096 - -#define PRINT_ERROR_AND_EXIT(id) \ - if (xmlErrorGetNo(id, 0) != XML_NO_ERROR) { \ - const char *errstr = xmlErrorGetString(id, 0); \ - size_t column = xmlErrorGetColumnNo(id, 0); \ - size_t lineno = xmlErrorGetLineNo(id, 1); \ - printf("Error at line %i, column %i: %s\n", lineno, column, errstr); \ - exit(-1); \ - } - -int main() -{ - void *root_id; - - root_id = xmlOpen("sample.xml"); - if (root_id) - { - void *path_id, *node_id; - char *s; - - printf("\nTesting xmlNodeGetString for /*/*/test:\t\t\t\t\t"); - s = xmlNodeGetString(root_id , "/*/*/test"); - if (s) - { - printf("failed.\n\t'%s' should be empty\n", s); - free(s); - } - else - printf("succes.\n"); - - printf("Testing xmlGetString for /Configuration/output/test:\t\t\t"); - path_id = xmlNodeGet(root_id, "/Configuration/output/test"); - if (path_id) - { - s = xmlGetString(path_id); - if (s) - { - printf("failed.\n\t'%s' should be empty\n", s); - free(s); - } - else - printf("succes.\n"); - } - else - PRINT_ERROR_AND_EXIT(root_id); - - path_id = xmlNodeGet(root_id, PATH); - node_id = xmlNodeGet(root_id, ROOTNODE); - - if (path_id && node_id) - { - char buf[BUFLEN]; - size_t len; - - xmlCopyString(path_id, buf, BUFLEN); - printf("Testing xmlNodeCopyString against xmlGetString:\t\t\t\t"); - if ((s = xmlGetString(path_id)) != 0) - { - if (strcmp(s, buf)) /* not the same */ - printf("failed.\n\t'%s' differs from '%s'\n", s, buf); - else - printf("succes.\n"); - - printf("Testing xmlCopyString against xmlGetString:\t\t\t\t"); - xmlCopyString(path_id, buf, BUFLEN); - if (strcmp(s, buf)) /* not the same */ - printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf); - else - printf("succes.\n"); - } - else - PRINT_ERROR_AND_EXIT(path_id); - - printf("Testing xmlCopyString against xmlCompareString:\t\t\t\t"); - if (xmlCompareString(path_id, buf)) /* not the same */ - printf ("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf); - else - printf("succes.\n"); - - printf("Testing xmlCopyString against xmlNodeCompareString:\t\t\t"); - if (xmlNodeCompareString(node_id, LEAFNODE, buf)) /* not the same */ - printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf); - else - printf("succes.\n"); - - if (s) free(s); - - printf("Testing xmlCopyString against xmlNodeGetString:\t\t\t\t"); - if ((s = xmlNodeGetString(node_id, LEAFNODE)) != 0) - { - if (strcmp(s, buf)) /* not the same */ - printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf); - else - printf("succes.\n"); - free(s); - } - else - PRINT_ERROR_AND_EXIT(node_id); - - free(path_id); - path_id = xmlNodeGet(root_id, "/Configuration/backend/name"); - if (path_id) - { - printf("Testing xmlAttributeCopyString against xmlAttributeCompareString:\t"); - xmlAttributeCopyString(path_id, "type", buf, BUFLEN); - if (xmlAttributeCompareString(path_id, "type", buf)) /* no match */ - printf("failed.\n\t'%s' differs\n", buf); - else - printf("succes.\n"); - - printf("Testing xmlAttributeCopyString against xmlAttributeGetString:\t\t"); - if ((s = xmlAttributeGetString(path_id, "type")) != 0) - { - if (strcmp(s, buf)) /* not the same */ - printf("failed.\n\t'%s' differs from '%s'\n", s, buf); - else - printf("succes.\n"); - free(s); - } - else - PRINT_ERROR_AND_EXIT(path_id); - - } - else - PRINT_ERROR_AND_EXIT(root_id); - - free(node_id); - free(path_id); - - path_id = xmlNodeGet(root_id, "Configuration/output/sample/test"); - if (path_id) - { - xmlNodeCopyString(root_id ,"Configuration/output/menu/name", buf, BUFLEN); - printf("Testing xmlCompareString against a fixed string: \t\t\t"); - if (xmlCompareString(path_id, buf)) /* no match */ - printf("failed.\n\t'%s' differs\n", buf); - else - printf("succes.\n"); - - s = xmlGetString(path_id); - if (s) - { - printf("Testing xmlGetString against a fixed string: \t\t\t\t"); - if (strcmp(s, buf)) /* mismatch */ - printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf); - else - printf("succes.\n"); - - printf("Testing xmlCopyString gainst a fixed string: \t\t\t\t"); - xmlCopyString(path_id, buf, BUFLEN); - if (strcmp(s, buf)) /* mismatch */ - printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf); - else - printf("succes.\n"); - - free(s); - } - else - PRINT_ERROR_AND_EXIT(path_id); - - free(path_id); - } - } - - if (xmlErrorGetNo(root_id, 0) != XML_NO_ERROR) - { - const char *errstr = xmlErrorGetString(root_id, 0); - size_t column = xmlErrorGetColumnNo(root_id, 0); - size_t lineno = xmlErrorGetLineNo(root_id, 1); - - printf("Error at line %i, column %i: %s\n", lineno, column, errstr); - } - - xmlClose(root_id); - } - printf("\n"); - - return 0; -} diff --git a/utils/xmlgrep/xml.c b/utils/xmlgrep/xml.c deleted file mode 100644 index 5357ed81a..000000000 --- a/utils/xmlgrep/xml.c +++ /dev/null @@ -1,1995 +0,0 @@ -/* Copyright (c) 2007-2009 by Adalin B.V. - * Copyright (c) 2007-2009 by Erik Hofman - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * 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. - * * Neither the name of (any of) the copyrightholder(s) 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 ``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 COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * 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. - */ - -#ifdef WIN32 -# include - -#else /* !WIN32 */ -# include -# include -#endif - -#ifndef NDEBUG -# include -#endif -#include /* free, malloc */ -#include -#include /* memcmp */ -#ifndef _MSC_VER -#include /* strncasecmp */ -#else -# define strncasecmp strnicmp -#endif -#include -#include -#include -#include -#include - -#include "xml.h" - -#ifndef XML_NONVALIDATING -static const char *__xml_error_str[XML_MAX_ERROR]; - -static void __xmlErrorSet(const void *, const char *, unsigned int); -# define xmlErrorSet(a, b, c) __xmlErrorSet(a, b, c) - -# ifndef NDEBUG -# define PRINT_INFO(a) \ - assert((a) < XML_MAX_ERROR); \ - printf("at line %i: %s\n", __LINE__, __xml_error_str[(a)]) -# else -# define PRINT_INFO(a) -# endif - -# define SET_ERROR_AND_RETURN(a, b) \ - { *rlen = 0; *name = (a); *len = (b); PRINT_INFO(b); return 0; } - -#else /* !XML_NONVALIDATING */ -# define xmlErrorSet(a, b, c) -# define SET_ERROR_AND_RETURN(a, b) return 0; -#endif - -static char *__xmlNodeGetPath(void **, const char *, size_t *, char **, size_t *); -static char *__xmlNodeGet(void *, const char *, size_t *, char **, size_t *, size_t *); -static char *__xmlProcessCDATA(char **, size_t *); -static char *__xmlCommentSkip(const char *, size_t); -static char *__xmlInfoProcess(const char *, size_t); - -static void *__xml_memncasecmp(const char *, size_t *, char **, size_t *); -static void __xmlPrepareData(char **, size_t *); - -#ifdef WIN32 -/* - * map 'filename' and return a pointer to it. - */ -static void *simple_mmap(int, size_t, SIMPLE_UNMMAP *); -static void simple_unmmap(SIMPLE_UNMMAP *); - -# define mmap(a,b,c,d,e,f) simple_mmap((e), (b), &rid->un) -# define munmap(a,b) simple_unmmap(&rid->un) -#endif - -#ifndef NDEBUG -# define PRINT(a, b, c) { \ - size_t l1 = (b), l2 = (c); \ - char *s = (a); \ - if (s) { \ - size_t q, len = l2; \ - if (l1 < l2) len = l1; \ - if (len < 50000) { \ - printf("(%i) '", len); \ - for (q=0; q= 0) - { - struct stat statbuf; - void *mm; - - fstat(fd, &statbuf); - mm = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0L); - if (mm != (void *)-1) - { - rid = calloc(1, sizeof(struct _root_id)); - if (rid) - { - size_t blen = statbuf.st_size; -#ifdef XML_USE_NODECACHE - size_t num = 0, nlen = 1; - char *n = "*"; - - rid->node = cacheInit(); - __xmlNodeGet(rid->node, mm, &blen, &n, &nlen, &num); -#endif - rid->fd = fd; - rid->start = mm; - rid->len = blen; - } - } - } - } - - return (void *)rid; -} - -void * -xmlInitBuffer(const char *buffer, size_t size) -{ - struct _root_id *rid = 0; - - if (buffer && (size > 0)) - { - rid = calloc(1, sizeof(struct _root_id)); - if (rid) - { -#ifdef XML_USE_NODECACHE - size_t num = 0, nlen = 1; - size_t blen = size; - char *n = "*"; - - rid->node = cacheInit(); - __xmlNodeGet(rid->node, buffer, &blen, &n, &nlen, &num); -#endif - rid->fd = -1; - rid->start = (char *)buffer; - rid->len = size; - } - } - - return (void *)rid; -} - -void -xmlClose(void *id) -{ - struct _root_id *rid = (struct _root_id *)id; - - assert(rid != 0); - assert(rid->name == 0); - - if (rid->fd != -1) - { - munmap(rid->start, rid->len); - close(rid->fd); - } - -#ifdef XML_USE_NODECACHE - if (rid->node) cacheFree(rid->node); -#endif -#ifndef XML_NONVALIDATING - if (rid->info) free(rid->info); -#endif - free(rid); - id = 0; -} - -void * -xmlNodeGet(const void *id, const char *path) -{ - struct _xml_id *xid = (struct _xml_id *)id; - struct _xml_id *xsid = 0; - size_t len, slen; - char *ptr, *node; - void *nc, *nnc; - - assert(id != 0); - assert(path != 0); - - node = (char *)path; - len = xid->len; - slen = strlen(path); - - nnc = nc = cacheNodeGet(xid); - ptr = __xmlNodeGetPath(&nnc, xid->start, &len, &node, &slen); - if (ptr) - { - xsid = malloc(sizeof(struct _xml_id)); - if (xsid) - { - xsid->name = node; - xsid->name_len = slen; - xsid->start = ptr; - xsid->len = len; -#ifdef XML_USE_NODECACHE - xsid->node = nnc; -#endif -#ifndef XML_NONVALIDATING - if (xid->name) - xsid->root = xid->root; - else - xsid->root = (struct _root_id *)xid; -#endif - } - else - { - xmlErrorSet(xid, 0, XML_OUT_OF_MEMORY); - } - } - else if (slen == 0) - { - xmlErrorSet(xid, node, len); - } - - return (void *)xsid; -} - -void * -xmlNodeCopy(const void *id, const char *path) -{ - struct _xml_id *xid = (struct _xml_id *)id; - struct _xml_id *xsid = 0; - char *ptr, *node, *p; - size_t slen, len; - void *nc, *nnc; - - node = (char *)path; - len = xid->len; - slen = strlen(path); - - nnc = nc = cacheNodeGet(xid); - ptr = __xmlNodeGetPath(&nnc, xid->start, &len, &node, &slen); - if (ptr) - { - xsid = malloc(sizeof(struct _xml_id) + len); - if (xsid) - { - p = (char *)xsid + sizeof(struct _xml_id); - - xsid->len = len; - xsid->start = p; - xsid->name_len = slen; - xsid->name = node; -#ifdef XML_USE_NODECACHE - xsid->node = nc; -#endif -#ifndef XML_NONVALIDATING - if (xid->name) - xsid->root = xid->root; - else - xsid->root = (struct _root_id *)xid; -#endif - - memcpy(xsid->start, ptr, len); - } - else - { - xmlErrorSet(xid, 0, XML_OUT_OF_MEMORY); - } - } - else if (slen == 0) - { - xmlErrorSet(xid, node, len); - } - - return (void *)xsid; -} - -char * -xmlNodeGetName(const void *id) -{ - struct _xml_id *xid = (struct _xml_id *)id; - size_t len; - char *ret; - - assert(xid != 0); - - len = xid->name_len; - ret = malloc(len+1); - if (ret) - { - memcpy(ret, xid->name, len); - *(ret + len) = 0; - } - else - { - xmlErrorSet(xid, 0, XML_OUT_OF_MEMORY); - } - - return ret; -} - -size_t -xmlNodeCopyName(const void *id, char *buf, size_t buflen) -{ - struct _xml_id *xid = (struct _xml_id *)id; - size_t slen = 0; - - assert(buf != 0); - assert(buflen > 0); - - slen = xid->name_len; - if (slen >= buflen) - { - slen = buflen-1; - xmlErrorSet(xid, 0, XML_TRUNCATE_RESULT); - } - memcpy(buf, xid->name, slen); - *(buf + slen) = 0; - - return slen; -} - -unsigned int -xmlNodeGetNum(const void *id, const char *path) -{ - struct _xml_id *xid = (struct _xml_id *)id; - size_t num = 0; - - assert(xid != 0); - assert(path != 0); - - if (xid->len) - { - char *nodename, *pathname; - size_t len, slen; - void *nc; - char *p; - - nodename = (char *)path; - if (*path == '/') nodename++; - slen = strlen(nodename); - - nc = cacheNodeGet(xid); - pathname = strchr(nodename, '/'); - if (pathname) - { - char *node; - - len = xid->len; - pathname++; - slen -= pathname-nodename; - node = pathname; - p = __xmlNodeGetPath(&nc, xid->start, &len, &node, &slen); - if (p == 0 && slen == 0) - { - xmlErrorSet(xid, node, len); - } - } - else - { - p = xid->start; - len = xid->len; - } - - if (p) - { - char *ret, *node = nodename; -#ifndef XML_USE_NODECACHE - ret = __xmlNodeGet(nc, p, &len, &node, &slen, &num); -#else - ret = __xmlNodeGetFromCache(&nc, p, &len, &node, &slen, &num); -#endif - if (ret == 0 && slen == 0) - { - xmlErrorSet(xid, node, len); - num = 0; - } - } - } - - return num; -} - -void * -xmlNodeGetPos(const void *pid, void *id, const char *element, size_t num) -{ - struct _xml_id *xpid = (struct _xml_id *)pid; - struct _xml_id *xid = (struct _xml_id *)id; - size_t len, slen; - char *ptr, *node; - void *ret = 0; - void *nc; - - assert(xpid != 0); - assert(xid != 0); - assert(element != 0); - - len = xpid->len; - slen = strlen(element); - node = (char *)element; - nc = cacheNodeGet(xpid); -#ifndef XML_USE_NODECACHE - ptr = __xmlNodeGet(nc, xpid->start, &len, &node, &slen, &num); -#else - ptr = __xmlNodeGetFromCache(&nc, xpid->start, &len, &node, &slen, &num); -#endif - if (ptr) - { - xid->len = len; - xid->start = ptr; - xid->name = node; - xid->name_len = slen; -#ifdef XML_USE_NODECACHE - /* unused for the cache but tested at the start of this function */ - if (len == 0) xid->len = 1; - xid->node = nc; -#endif - ret = xid; - } - else if (slen == 0) - { - xmlErrorSet(xpid, node, len); - } - - return ret; -} - -char * -xmlGetString(const void *id) -{ - struct _xml_id *xid = (struct _xml_id *)id; - char *str = 0; - - assert(xid != 0); - - if (xid->len) - { - size_t len; - char *ps; - - ps = xid->start; - len = xid->len-1; - __xmlPrepareData(&ps, &len); - if (len) - { - len++; - str = malloc(len+1); - if (str) - { - memcpy(str, ps, len); - *(str+len) = 0; - } - else - { - xmlErrorSet(xid, 0, XML_OUT_OF_MEMORY); - } - } - } - - return str; -} - -size_t -xmlCopyString(const void *id, char *buffer, size_t buflen) -{ - struct _xml_id *xid = (struct _xml_id *)id; - size_t ret = 0; - - assert(xid != 0); - assert(buffer != 0); - assert(buflen > 0); - - *buffer = '\0'; - if (xid->len) - { - size_t len; - char *ps; - - ps = xid->start; - len = xid->len; - __xmlPrepareData(&ps, &len); - if (len) - { - if (len >= buflen) - { - len = buflen-1; - xmlErrorSet(xid, 0, XML_TRUNCATE_RESULT); - } - memcpy(buffer, ps, len); - *(buffer+len) = 0; - } - ret = len; - } - - return ret; -} - -int -xmlCompareString(const void *id, const char *s) -{ - struct _xml_id *xid = (struct _xml_id *)id; - int ret = -1; - - assert(xid != 0); - assert(s != 0); - - if (xid->len && (strlen(s) > 0)) - { - size_t len; - char *ps; - - ps = xid->start; - len = xid->len; - __xmlPrepareData(&ps, &len); - ret = strncasecmp(ps, s, len); - } - - return ret; -} - -char * -xmlNodeGetString(const void *id, const char *path) -{ - struct _xml_id *xid = (struct _xml_id *)id; - char *str = 0; - - assert(xid != 0); - assert(path != 0); - - if (xid->len) - { - char *ptr, *node = (char *)path; - size_t slen = strlen(node); - size_t len = xid->len; - void *nc; - - nc = cacheNodeGet(xid); - ptr = __xmlNodeGetPath(&nc, xid->start, &len, &node, &slen); - if (ptr && len) - { - __xmlPrepareData(&ptr, &len); - str = malloc(len+1); - if (str) - { - memcpy(str, ptr, len); - *(str+len) = '\0'; - } - else - { - xmlErrorSet(xid, 0, XML_OUT_OF_MEMORY); - } - } - else - { - xmlErrorSet(xid, node, len); - } - } - - return str; -} - -size_t -xmlNodeCopyString(const void *id, const char *path, char *buffer, size_t buflen) -{ - struct _xml_id *xid = (struct _xml_id *)id; - size_t ret = 0; - - assert(xid != 0); - assert(path != 0); - assert(buffer != 0); - assert(buflen > 0); - - *buffer = '\0'; - if (xid->len) - { - char *p, *node = (char *)path; - size_t slen = strlen(node); - size_t len = xid->len; - void *nc; - - nc = cacheNodeGet(xid); - p = __xmlNodeGetPath(&nc, xid->start, &len, &node, &slen); - if (p) - { - __xmlPrepareData(&p, &len); - if (len) - { - if (len >= buflen) - { - len = buflen-1; - xmlErrorSet(xid, 0, XML_TRUNCATE_RESULT); - } - - memcpy(buffer, p, len); - *(buffer+len) = '\0'; - } - ret = len; - } - else if (slen == 0) - { - xmlErrorSet(xid, node, len); - } - } - - return ret; -} - -int -xmlNodeCompareString(const void *id, const char *path, const char *s) -{ - struct _xml_id *xid = (struct _xml_id *)id; - int ret = -1; - - assert(xid != 0); - assert(path != 0); - assert(s != 0); - - if (xid->len && (strlen(s) > 0)) - { - char *node, *str, *ps, *pe; - size_t len, slen; - void *nc; - - len = xid->len; - slen = strlen(path); - node = (char *)path; - nc = cacheNodeGet(xid); - str = __xmlNodeGetPath(&nc, xid->start, &len, &node, &slen); - if (str) - { - ps = str; - __xmlPrepareData(&ps, &len); - ret = strncasecmp(ps, s, len); - } - else if (slen == 0) - { - xmlErrorSet(xid, node, len); - } - } - - return ret; -} - -long int -xmlGetInt(const void *id) -{ - struct _xml_id *xid = (struct _xml_id *)id; - long int li = 0; - - assert(xid != 0); - - if (xid->len) - { - char *end = xid->start + xid->len; - li = strtol(xid->start, &end, 10); - } - - return li; -} - -long int -xmlNodeGetInt(const void *id, const char *path) -{ - struct _xml_id *xid = (struct _xml_id *)id; - long int li = 0; - - assert(xid != 0); - assert(path != 0); - - if (xid->len) - { - size_t len, slen; - char *str, *node; - void *nc; - - len = xid->len; - slen = strlen(path); - node = (char *)path; - nc = cacheNodeGet(xid); - str = __xmlNodeGetPath(&nc, xid->start, &len, &node, &slen); - if (str) - { - char *end = str+len; - li = strtol(str, &end, 10); - } - else if (slen == 0) - { - xmlErrorSet(xid, node, len); - } - } - - return li; -} - -double -xmlGetDouble(const void *id) -{ - struct _xml_id *xid = (struct _xml_id *)id; - double d = 0.0; - - assert(xid != 0); - - if (xid->len) - { - char *end = xid->start + xid->len; - d = strtod(xid->start, &end); - } - - return d; -} - -double -xmlNodeGetDouble(const void *id, const char *path) -{ - struct _xml_id *xid = (struct _xml_id *)id; - double d = 0.0; - - assert(xid != 0); - assert(path != 0); - - if (xid->len) - { - size_t len, slen; - char *str, *node; - void *nc; - - len = xid->len; - slen = strlen(path); - node = (char *)path; - nc = cacheNodeGet(xid); - str = __xmlNodeGetPath(&nc, xid->start, &len, &node, &slen); - if (str) - { - char *end = str+len; - d = strtod(str, &end); - } - else if (slen == 0) - { - xmlErrorSet(xid, node, len); - } - } - - return d; -} - -void * -xmlMarkId(const void *id) -{ - struct _xml_id *xmid = 0; - - assert(id != 0); - - xmid = malloc(sizeof(struct _xml_id)); - if (xmid) - { - struct _root_id *xrid = (struct _root_id *)id; - if (xrid->name == 0) - { - xmid->name = ""; - xmid->name_len = 0; - xmid->start = xrid->start; - xmid->len = xrid->len; -#ifdef XML_USE_NODECACHE - xmid->node = xrid->node; -#endif -#ifndef XML_NONVALIDATING - xmid->root = xrid; -#endif - } - else - { - memcpy(xmid, id, sizeof(struct _xml_id)); - } - } - else - { - xmlErrorSet(id, 0, XML_OUT_OF_MEMORY); - } - - return (void *)xmid; -} - -double -xmlAttributeGetDouble(const void *id, const char *name) -{ - struct _xml_id *xid = (struct _xml_id *)id; - double ret = 0.0; - - assert(xid != 0); - assert(name != 0); - - if (xid->name_len) - { - size_t slen = strlen(name); - char *ps, *pe; - - assert(xid->start > xid->name); - - ps = xid->name + xid->name_len + 1; - pe = xid->start - 1; - while (ps slen) && (strncasecmp(ps, name, slen) == 0)) - { - ps += slen; - if ((psname_len) - { - size_t slen = strlen(name); - char *ps, *pe; - - assert(xid->start > xid->name); - - ps = xid->name + xid->name_len + 1; - pe = xid->start - 1; - while (ps slen) && (strncasecmp(ps, name, slen) == 0)) - { - ps += slen; - if ((psname_len) - { - size_t slen = strlen(name); - char *ps, *pe; - - assert(xid->start > xid->name); - - ps = xid->name + xid->name_len + 1; - pe = xid->start - 1; - while (ps slen) && (strncasecmp(ps, name, slen) == 0)) - { - ps += slen; - if ((ps 0); - - if (xid->name_len) - { - size_t slen = strlen(name); - char *ps, *pe; - - assert(xid->start > xid->name); - - *buffer = '\0'; - ps = xid->name + xid->name_len + 1; - pe = xid->start - 1; - while (ps slen) && (strncasecmp(ps, name, slen) == 0)) - { - ps += slen; - if ((ps= buflen) - { - restlen = buflen-1; - xmlErrorSet(xid, ps, XML_TRUNCATE_RESULT); - } - - memcpy(buffer, start, restlen); - *(buffer+restlen) = 0; - ret = restlen; - } - else - { - xmlErrorSet(xid, ps, XML_ATTRIB_NO_CLOSING_QUOTE); - return 0; - } - } - else - { - while ((psname_len && strlen(s)) - { - size_t slen = strlen(name); - char *ps, *pe; - - assert(xid->start > xid->name); - - ps = xid->name + xid->name_len + 1; - pe = xid->start - 1; - while (ps slen) && (strncasecmp(ps, name, slen) == 0)) - { - ps += slen; - if ((psname) rid = xid->root; - else rid = (struct _root_id *)xid; - - assert(rid != 0); - - if (rid->info) - { - struct _xml_error *err = rid->info; - - ret = err->err_no; - if (clear) err->err_no = 0; - } - } - - return ret; -} - -size_t -xmlErrorGetLineNo(const void *id, int clear) -{ - size_t ret = 0; - - if (id) - { - struct _xml_id *xid = (struct _xml_id *)id; - struct _root_id *rid; - - if (xid->name) rid = xid->root; - else rid = (struct _root_id *)xid; - - assert(rid != 0); - - if (rid->info) - { - struct _xml_error *err = rid->info; - char *ps = rid->start; - char *pe = err->pos; - char *new; - - ret++; - while (pserr_no = 0; - } - } - - return ret; -} - -size_t -xmlErrorGetColumnNo(const void *id, int clear) -{ - size_t ret = 0; - - if (id) - { - struct _xml_id *xid = (struct _xml_id *)id; - struct _root_id *rid; - - if (xid->name) rid = xid->root; - else rid = (struct _root_id *)xid; - - assert(rid != 0); - - if (rid->info) - { - struct _xml_error *err = rid->info; - char *ps = rid->start; - char *pe = err->pos; - char *new; - - while (pserr_no = 0; - } - } - - return ret; -} - -const char * -xmlErrorGetString(const void *id, int clear) -{ - char *ret = 0; - - if (id) - { - struct _xml_id *xid = (struct _xml_id *)id; - struct _root_id *rid; - - if (xid->name) rid = xid->root; - else rid = (struct _root_id *)xid; - - assert(rid != 0); - - if (rid->info) - { - struct _xml_error *err = rid->info; - if (XML_NO_ERROR <= err->err_no && err->err_no < XML_MAX_ERROR) - { - ret = (char *)__xml_error_str[err->err_no]; - } - else - { - ret = "incorrect error number."; - } - - if (clear) err->err_no = 0; - } - } - - return ret; -} - -#else - -int -xmlErrorGetNo(const void *id, int clear) -{ - return XML_NO_ERROR; -} - -size_t -xmlErrorGetLineNo(const void *id, int clear) -{ - return 0; -} - -size_t -xmlErrorGetColumnNo(const void *id, int clear) -{ - return 0; -} - -const char * -xmlErrorGetString(const void *id, int clear) -{ - return "error detection was not enabled at compile time: no error."; -} - -#endif - -/* -------------------------------------------------------------------------- */ - -#ifndef XML_NONVALIDATING -static const char *__xml_error_str[XML_MAX_ERROR] = -{ - "no error.", - "unable to allocate enough memory.", - "unable to open file for reading.", - "requested node name is invalid.", - "unexpected end of section.", - "buffer too small to hold all data, truncating.", - "incorrect comment section.", - "bad information block.", - "incompatible opening tag for element.", - "missing or invalid closing tag for element.", - "missing or invalid opening quote for attribute.", - "missing or invalid closing quote for attribute." -}; -#endif - -char * -__xmlNodeGetPath(void **nc, const char *start, size_t *len, char **name, size_t *nlen) -{ - char *path; - char *ret = 0; - - assert(start != 0); - assert(len != 0); - assert(name != 0); - assert(*name != 0); - assert(nlen != 0); - - path = *name; - if (*path == '/') path++; - if (*path != '\0') - { - size_t num, blocklen, pathlen, nodelen; - char *node; - - node = path; - pathlen = strlen(path); - path = strchr(node, '/'); - - if (!path) nodelen = pathlen; - else nodelen = path++ - node; - - num = 0; - blocklen = *len; - -#ifndef XML_USE_NODECACHE - ret = __xmlNodeGet(nc, start, &blocklen, &node, &nodelen, &num); -#else - ret = __xmlNodeGetFromCache(nc, start, &blocklen, &node, &nodelen, &num); -#endif - if (ret) - { - if (path) - { - ret = __xmlNodeGetPath(nc, ret, &blocklen, &path, &pathlen); - *name = path; - *len = blocklen; - *nlen = pathlen; - } - else - { - *name = node; - *nlen = nodelen; - *len = blocklen; - } - } - else - { - *len = 0; - *nlen = 0; - } - } - - return ret; -} - -char * -__xmlNodeGet(void *nc, const char *start, size_t *len, char **name, size_t *rlen, size_t *nodenum) -{ - char *cdata, *open_element = *name; - char *element, *start_tag=0; - char *new, *cur, *ne, *ret = 0; - size_t restlen, elementlen; - size_t open_len = *rlen; - size_t return_len = 0; - int found, num; - void *nnc = 0; - - assert(start != 0); - assert(len != 0); - assert(name != 0); - assert(rlen != 0); - assert(nodenum != 0); - - if (open_len == 0 || *name == 0) - SET_ERROR_AND_RETURN((char *)start, XML_INVALID_NODE_NAME); - - cdata = (char *)start; - if (*rlen > *len) - SET_ERROR_AND_RETURN((char *)start, XML_UNEXPECTED_EOF); - - found = 0; - num = *nodenum; - restlen = *len; - cur = (char *)start; - ne = cur + restlen; - -#ifdef XML_USE_NODECACHE - cacheInitLevel(nc); -#endif - - /* search for an opening tag */ - while ((new = memchr(cur, '<', restlen)) != 0) - { - size_t len_remaining; - char *rptr; - - if (*(new+1) == '/') /* end of section */ - { - *len -= restlen; - break; - } - - new++; - restlen -= new-cur; - cur = new; - - if (*cur == '!') /* comment */ - { - char *start = cur; - size_t blocklen = restlen; - new = __xmlProcessCDATA(&start, &blocklen); - if (!new && start && open_len) /* CDATA */ - SET_ERROR_AND_RETURN(cur, XML_INVALID_COMMENT); - - restlen -= new-cur; - cur = new; - continue; - } - else if (*cur == '?') /* info block */ - { - new = __xmlInfoProcess(cur, restlen); - if (!new) - SET_ERROR_AND_RETURN(cur, XML_INVALID_INFO_BLOCK); - - restlen -= new-cur; - cur = new; - continue; - } - - /* - * get element name and a pointer to after the opening tag - */ - element = *name; - elementlen = *rlen; - len_remaining = restlen; - rptr = __xml_memncasecmp(cur, &restlen, &element, &elementlen); - if (rptr) /* requested element was found */ - { - new = rptr; - return_len = elementlen; - if (found == num) - { - ret = new; - open_len = elementlen; - start_tag = element; - } - else start_tag = 0; - } - else /* different element name was foud */ - { - new = cur + (len_remaining - restlen); - if (new >= ne) - SET_ERROR_AND_RETURN(cur, XML_UNEXPECTED_EOF); - - element = *name; - } - -#ifdef XML_USE_NODECACHE - nnc = cacheNodeNew(nc); -#endif - - if (*(new-2) == '/') /* e.g. */ - { - cur = new; - if (rptr) - { -#ifdef XML_USE_NODECACHE - cacheDataSet(nnc, element, elementlen, rptr, 0); -#endif - if (found == num) - { - open_element = start_tag; - *len = 0; - } - found++; - } - continue; - } - - /* - * get the next xml tag - */ - /* restlen -= new-cur; not necessary because of __xml_memncasecmp */ - cur = new; - new = memchr(cur, '<', restlen); - if (!new) - SET_ERROR_AND_RETURN(cur, XML_ELEMENT_NO_CLOSING_TAG); - - new++; - restlen -= new-cur; - cur = new; - if (*cur == '!') /* comment, CDATA */ - { - char *start = cur; - size_t blocklen = restlen; - new = __xmlProcessCDATA(&start, &blocklen); - if (new && start && open_len) /* CDATA */ - { - cdata = ret; - } - else if (!new) - SET_ERROR_AND_RETURN(cur, XML_INVALID_COMMENT); - - restlen -= new-cur; - cur = new; - - /* - * look for the closing tag of the cascading block - */ - new = memchr(cur, '<', restlen); - if (!new) - SET_ERROR_AND_RETURN(cur, XML_ELEMENT_NO_CLOSING_TAG); - - new++; - restlen -= new-cur; - cur = new; - } - - if (*cur == '/') /* closing tag of leaf node found */ - { - if (!strncasecmp(new+1, element, elementlen)) - { -#ifdef XML_USE_NODECACHE - cacheDataSet(nnc, element, elementlen, rptr, new-rptr-1); -#endif - if (*(new+elementlen+1) != '>') - SET_ERROR_AND_RETURN(new+1, XML_ELEMENT_NO_CLOSING_TAG); - - if (found == num) - { - if (start_tag) - { - *len = new-ret-1; - open_element = start_tag; - cdata = (char *)start; - start_tag = 0; - } - else /* report error */ - SET_ERROR_AND_RETURN(new, XML_ELEMENT_NO_OPENING_TAG); - } - found++; - } - - new = memchr(cur, '>', restlen); - if (!new) - SET_ERROR_AND_RETURN(cur, XML_ELEMENT_NO_CLOSING_TAG); - - restlen -= new-cur; - cur = new; - continue; - } - - /* no leaf node, continue */ - if (*cur != '/') /* cascading tag found */ - { - char *node = "*"; - size_t slen = restlen+1; /* due to cur-1 below*/ - size_t nlen = 1; - size_t pos = -1; - - /* - * recursively walk the xml tree from here - */ - new = __xmlNodeGet(nnc, cur-1, &slen, &node, &nlen, &pos); - if (!new) - { - if (nlen == 0) /* error upstream */ - { - *rlen = nlen; - *name = node; - *len = slen; - return 0; - } - - if (slen == restlen) - SET_ERROR_AND_RETURN(cur, XML_UNEXPECTED_EOF); - - slen--; - new = cur + slen; - restlen -= slen; - } - else restlen -= slen; - - /* - * look for the closing tag of the cascading block - */ - cur = new; - new = memchr(cur, '<', restlen); - if (!new) - SET_ERROR_AND_RETURN(cur, XML_ELEMENT_NO_CLOSING_TAG); - - new++; - restlen -= new-cur; - cur = new; - } - - if (*cur == '/') /* closing tag found */ - { - if (!strncasecmp(new+1, element, elementlen)) - { - if (*(new+elementlen+1) != '>') - SET_ERROR_AND_RETURN(new+1, XML_ELEMENT_NO_CLOSING_TAG); - -#ifdef XML_USE_NODECACHE - cacheDataSet(nnc, element, elementlen, rptr, new-rptr-1); -#endif - if (found == num) - { - if (start_tag) - { - *len = new-ret-1; - open_element = start_tag; - cdata = (char *)start; - start_tag = 0; - } - else /* report error */ - SET_ERROR_AND_RETURN(new, XML_ELEMENT_NO_OPENING_TAG); - } - found++; - } - - new = memchr(cur, '>', restlen); - if (!new) - SET_ERROR_AND_RETURN(cur, XML_ELEMENT_NO_CLOSING_TAG); - - restlen -= new-cur; - cur = new; - } - else - SET_ERROR_AND_RETURN(cur, XML_ELEMENT_NO_CLOSING_TAG); - - } /* while */ - - if (found == 0) - { - ret = 0; - *rlen = 0; - *name = start_tag; - *len = XML_NO_ERROR; /* element not found, no real error */ - } - else - { - *rlen = open_len; - *name = open_element; - *nodenum = found; - } - - return ret; -} - -char * -__xmlProcessCDATA(char **start, size_t *len) -{ - char *cur, *new; - size_t restlen = *len; - - cur = *start; - if ((restlen > 6) && (*(cur+1) == '-')) /* comment */ - { - new = __xmlCommentSkip(cur, restlen); - if (new) - { - *start = new; - *len = 0; - } - return new; - } - - if (restlen < 12) return 0; /* ![CDATA[ ]]> */ - - cur = *start; - new = 0; - - if (memcmp(cur, "![CDATA[", 8) == 0) - { - *start = cur+8; - cur += 8; - restlen -= 8; - do - { - new = memchr(cur, ']', restlen); - if (new) - { - if ((restlen > 3) && (memcmp(new, "]]>", 3) == 0)) - { - *len = new-1 - *start; - restlen -= 3; - new += 3; - break; - } - cur = new+1; - } - else - { - *len = 0; - break; - } - } - while (new && (restlen > 2)); - } - - return new; -} - -char * -__xmlCommentSkip(const char *start, size_t len) -{ - char *cur, *new; - - if (len < 7) return 0; /* !-- --> */ - - cur = (char *)start; - new = 0; - - if (memcmp(cur, "!--", 3) == 0) - { - cur += 3; - len -= 3; - do - { - new = memchr(cur, '-', len); - if (new) - { - len -= new-cur; - if ((len >= 3) && (memcmp(new, "-->", 3) == 0)) - { - new += 3; - /* len -= 3; */ - break; - } - cur = new+1; - len -= cur-new; - } - else break; - } - while (new && (len > 2)); - } - - return new; -} - -char * -__xmlInfoProcess(const char *start, size_t len) -{ - char *cur, *new; - - cur = (char *)start; - new = 0; - - if (*cur == '?') - { - if (len < 3) return 0; /* */ - - cur++; - len--; - new = memchr(cur, '?', len); - if (!new || *(new+1) != '>') return 0; - - new += 2; - } - - return new; -} - - -static void -__xmlPrepareData(char **start, size_t *blocklen) -{ - size_t len = *blocklen; - char *pe, *ps = *start; - - if (len > 1) - { - pe = ps + len-1; - while ((psps) && isspace(*pe)) pe--; - len = (pe-ps)+1; - } - else if (isspace(*(ps+1))) len--; - - /* CDATA or comment */ - if ((len >= 2) && !strncmp(ps, "= 6) /* !-- --> */ - { - char *new = __xmlProcessCDATA(&start, &len); - if (new) - { - ps = start; - pe = ps + len; - - while ((psps) && isspace(*pe)) pe--; - len = (pe-ps); - } - } - } - - *start = ps; - *blocklen = len; -} - -#define NOCASECMP(a,b) ( ((a)^(b)) & 0xdf ) -void * -__xml_memncasecmp(const char *haystack, size_t *haystacklen, - char **needle, size_t *needlelen) -{ - char *rptr = 0; - - if (haystack && needle && needlelen && (*needlelen > 0) - && (*haystacklen >= *needlelen)) - { - char *hs = (char *)haystack; - char *ns; - size_t i; - - ns = *needle; - - /* search for everything */ - if ((*ns == '*') && (*needlelen == 1)) - { - char *he = hs + *haystacklen; - - while ((hs < he) && !isspace(*hs) && (*hs != '>')) hs++; - if (*(hs-1) == '/') hs--; - - *needle = (char *)haystack; - *needlelen = hs - haystack; - - ns = memchr(hs, '>', he-hs); - if (ns) hs = ns+1; - else hs = he; - - rptr = hs; - } - else - { - size_t nlen = *needlelen; - char *he = hs + *haystacklen; - - for (i=0; i')) break; - hs++; - ns++; - } - - if (i == nlen) - { - *needle = (char *)haystack; - *needlelen = hs - haystack; - - ns = memchr(hs, '>', he-hs); - if (ns) hs = ns+1; - else hs = he; - - rptr = hs; - } - else /* not found */ - { - while((hs < he) && !isspace(*hs) && (*hs != '>')) hs++; - if (*(hs-1) == '/') hs--; - - *needle = (char *)haystack; - *needlelen = hs - haystack; - - ns = memchr(hs, '>', he-hs); - if (ns) hs = ns+1; - else hs = he; - } - } - - *haystacklen -= hs - haystack; - } - - return rptr; -} - -#ifndef XML_NONVALIDATING -void -__xmlErrorSet(const void *id, const char *pos, unsigned int err_no) -{ - struct _xml_id *xid = (struct _xml_id *)id; - struct _root_id *rid; - - assert(xid != 0); - - if (xid->name) rid = xid->root; - else rid = (struct _root_id *)xid; - - assert(rid != 0); - if (rid->info == 0) - { - rid->info = malloc(sizeof(struct _xml_error)); - } - - if (rid->info) - { - struct _xml_error *err = rid->info; - - err->pos = (char *)pos; - err->err_no = err_no; - } -} -#endif - -#ifdef WIN32 -/* Source: - * https://mollyrocket.com/forums/viewtopic.php?p=2529 - */ - -void * -simple_mmap(int fd, size_t length, SIMPLE_UNMMAP *un) -{ - HANDLE f; - HANDLE m; - void *p; - - f = (HANDLE)_get_osfhandle(fd); - if (!f) return (void *)-1; - - m = CreateFileMapping(f, NULL, PAGE_READONLY, 0, 0, NULL); - if (!m) return (void *)-1; - - p = MapViewOfFile(m, FILE_MAP_READ, 0,0,0); - if (!p) - { - CloseHandle(m); - return (void *)-1; - } - - if (un) - { - un->m = m; - un->p = p; - } - - return p; -} - -void -simple_unmmap(SIMPLE_UNMMAP *un) -{ - UnmapViewOfFile(un->p); - CloseHandle(un->m); -} -#endif diff --git a/utils/xmlgrep/xml.h b/utils/xmlgrep/xml.h deleted file mode 100644 index 7431b4b5b..000000000 --- a/utils/xmlgrep/xml.h +++ /dev/null @@ -1,434 +0,0 @@ -/* Copyright (c) 2007-2009 by Adalin B.V. - * Copyright (c) 2007-2009 by Erik Hofman - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * 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. - * * Neither the name of (any of) the copyrightholder(s) 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 ``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 COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * 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. - */ - -#ifndef __XML_CONFIG -#define __XML_CONFIG 1 - -#ifdef __cplusplus -extern "C" { -#endif - -#undef XML_NONVALIDATING - -#ifdef XML_USE_NODECACHE -#include "xml_cache.h" -#else -void *cacheGet(void *); -#endif - -enum -{ - XML_NO_ERROR = 0, - XML_OUT_OF_MEMORY, - XML_FILE_NOT_FOUND, - XML_INVALID_NODE_NAME, - XML_UNEXPECTED_EOF, - XML_TRUNCATE_RESULT, - XML_INVALID_COMMENT, - XML_INVALID_INFO_BLOCK, - XML_ELEMENT_NO_OPENING_TAG, - XML_ELEMENT_NO_CLOSING_TAG, - XML_ATTRIB_NO_OPENING_QUOTE, - XML_ATTRIB_NO_CLOSING_QUOTE, - XML_MAX_ERROR -}; - -#ifdef WIN32 -# define WIN32_LEAN_AND_MEAN -# include - -typedef struct -{ - HANDLE m; - void *p; -} SIMPLE_UNMMAP; -#endif - -#ifndef XML_NONVALIDATING -struct _xml_error -{ - char *pos; - int err_no; -}; -#endif - -/* - * It is required for both the rood node and the normal xml nodes to both - * have 'char *name' defined as the first entry. The code tests whether - * name == 0 to detect the root node. - */ -struct _root_id -{ - char *name; - char *start; - size_t len; - int fd; -#ifdef XML_USE_NODECACHE - void *node; -#endif -#ifndef XML_NONVALIDATING - struct _xml_error *info; -#endif -#ifdef WIN32 - SIMPLE_UNMMAP un; -#endif -}; - -struct _xml_id -{ - char *name; - char *start; - size_t len; - size_t name_len; -#ifndef XML_NONVALIDATING - struct _root_id *root; -#endif -#ifdef XML_USE_NODECACHE - void *node; -#endif -}; - - - -/** - * Open an XML file for processing. - * - * @param fname path to the file - * @return XML-id which is used for further processing - */ -void *xmlOpen(const char *); - -/** - * Process a section of XML code in a preallocated buffer. - * The buffer may not be free'd until xmlClose has been called. - * - * @param buffer pointer to the buffer - * @param size size of the buffer - * @return XML-id which is used for further processing - */ -void *xmlInitBuffer(const char *, size_t); - -/** - * Close the XML file after which no further processing is possible. - * - * @param xid XML-id - */ -void xmlClose(void *); - - -/** - * Locate a subsection of the xml tree for further processing. - * This adds processing speed since the reuired nodes will only be searched - * in the subsection. - * - * The memory allocated for the XML-subsection-id has to be freed by the - * calling process. - * - * @param xid XML-id - * @param node path to the node containing the subsection - * @return XML-subsection-id for further processing - */ -void *xmlNodeGet(const void *, const char *); - -/** - * Copy a subsection of the xml tree for further processing. - * This is useful when it's required to process a section of the XML code - * after the file has been closed. The drawback is the added memory - * requirements. - * - * The memory allocated for the XML-subsection-id has to be freed by the - * calling process. - * - * @param xid XML-id - * @param node path to the node containing the subsection - * @return XML-subsection-id for further processing - */ -void *xmlNodeCopy(const void *, const char *); - - -/** - * Return the name of this node. - * The returned string has to be freed by the calling process. - * - * @param xid XML-id - * @return a newly alocated string containing the node name - */ -char *xmlNodeGetName(const void *); - -/** - * Copy the name of this node in a pre-allocated buffer. - * - * @param xid XML-id - * @param buffer the buffer to copy the string to - * @param buflen length of the destination buffer - * @return the length of the node name - */ -size_t xmlNodeCopyName(const void *, char *, size_t); - - -/** - * Create a marker XML-id that starts out with the same settings as the - * refference XML-id. - * - * Marker id's are required when xmlNodeGetNum() and xmlNodeGetPos() are used - * to walk a number of nodes. The xmlNodeGetPos function adjusts the contents - * of the provided XML-id to keep track of it's position within the xml section. - * The returned XML-id is limited to the boundaries of the requested XML tag - * and has to be freed by the calling process. - * - * @param xid reference XML-id - * @return a copy of the reference XML-id - */ -void *xmlMarkId(const void *); - -/** - * Get the number of nodes with the same name from a specified xml path. - * - * @param xid XML-id - * @param path path to the xml node - * @return the number count of the nodename - */ -unsigned int xmlNodeGetNum(const void *, const char *); - -/** - * Get the nth occurrence of node in the parent node. - * The return value should never be altered or freed by the caller. - * - * @param pid XML-id of the parent node of this node - * @param xid XML-id - * @param node name of the node to search for - * @param num specify which occurence to return - * @return XML-subsection-id for further processing or NULL if unsuccessful - */ -void *xmlNodeGetPos(const void *, void *, const char *, size_t); - - -/** - * Get a string of characters from the current node. - * The returned string has to be freed by the calling process. - * - * @param xid XML-id - * @return a newly alocated string containing the contents of the node - */ -char *xmlGetString(const void *); - -/** - * Get a string of characters from the current node. - * This function has the advantage of not allocating its own return buffer, - * keeping the memory management to an absolute minimum but the disadvantage - * is that it's unreliable in multithread environments. - * - * @param xid XML-id - * @param buffer the buffer to copy the string to - * @param buflen length of the destination buffer - * @return the length of the string - */ -size_t xmlCopyString(const void *, char *, size_t); - -/** - * Compare the value of this node to a reference string. - * Comparing is done in a case insensitive way. - * - * @param xid XML-id - * @param str the string to compare to - * @return an integer less than, equal to, ro greater than zero if the value - * of the node is found, respectively, to be less than, to match, or be greater - * than str - */ -int xmlCompareString(const void *, const char *); - -/** - * Get a string of characters from a specified xml path. - * The returned string has to be freed by the calling process. - * - * @param xid XML-id - * @param path path to the xml node - * @return a newly alocated string containing the contents of the node - */ -char *xmlNodeGetString(const void *, const char *); - -/** - * Get a string of characters from a specified xml path. - * This function has the advantage of not allocating its own return buffer, - * keeping the memory management to an absolute minimum but the disadvantage - * is that it's unreliable in multithread environments. - * - * @param xid XML-id - * @param path path to the xml node - * @param buffer the buffer to copy the string to - * @param buflen length of the destination buffer - * @return the length of the string - */ -size_t xmlNodeCopyString(const void *, const char *, char *, size_t); - -/** - * Compare the value of a node to a reference string. - * Comparing is done in a case insensitive way. - * - * @param xid XML-id - * @param path path to the xml node to compare to - * @param str the string to compare to - * @return an integer less than, equal to, ro greater than zero if the value - * of the node is found, respectively, to be less than, to match, or be greater - * than str - */ -int xmlNodeCompareString(const void *, const char *, const char *); - -/** - * Get a string of characters from a named attribute. - * The returned string has to be freed by the calling process. - * - * @param xid XML-id - * @param name name of the attribute to acquire - * @return the contents of the node converted to an integer value - */ -char *xmlAttributeGetString(const void *, const char *); - -/** - * Get a string of characters from a named attribute. - * This function has the advantage of not allocating its own return buffer, - * keeping the memory management to an absolute minimum but the disadvantage - * is that it's unreliable in multithread environments. - * - * @param xid XML-id - * @param name name of the attribute to acquire. - * @param buffer the buffer to copy the string to - * @param buflen length of the destination buffer - * @return the length of the string - */ -size_t xmlAttributeCopyString(const void *, const char *, char *, size_t); - -/** - * Compare the value of an attribute to a reference string. - * Comparing is done in a case insensitive way. - * - * @param xid XML-id - * @param name name of the attribute to acquire. - * @param str the string to compare to - * @return an integer less than, equal to, ro greater than zero if the value - * of the node is found, respectively, to be less than, to match, or be greater - * than str - */ -int xmlAttributeCompareString(const void *, const char *, const char *); - - -/** - * Get the integer value from the current node/ - * - * @param xid XML-id - * @return the contents of the node converted to an integer value - */ -long int xmlGetInt(const void *); - -/** - * Get an integer value from a specified xml path. - * - * @param xid XML-id - * @param path path to the xml node - * @return the contents of the node converted to an integer value - */ -long int xmlNodeGetInt(const void *, const char *); - -/** - * Get the integer value from the named attribute. - * - * @param xid XML-id - * @param name name of the attribute to acquire - * @return the contents of the node converted to an integer value - */ -long int xmlAttributeGetInt(const void *, const char *); - - -/** - * Get the double value from the curent node/ - * - * @param xid XML-id - * @return the contents of the node converted to a double value - */ -double xmlGetDouble(const void *); - -/** - * Get a double value from a specified xml path/ - * - * @param xid XML-id - * @param path path to the xml node - * @return the contents of the node converted to a double value - */ -double xmlNodeGetDouble(const void *, const char *); - -/** - * Get the double value from the named attribute. - * - * @param xid XML-id - * @param name name of the attribute to acquire - * @return the contents of the node converted to an integer value - */ -double xmlAttributeGetDouble(const void *, const char *); - - -/** - * Get the error number of the last error and clear it. - * - * @param xid XML-id - * @param clear clear the error state if non zero - * @return the numer of the last error, 0 means no error detected. - */ -int xmlErrorGetNo(const void *, int); - -/** - * Get the line number of the last detected syntax error in the xml file. - * - * @param xid XML-id - * @param clear clear the error state if non zero - * @return the line number of the detected syntax error. - */ -size_t xmlErrorGetLineNo(const void *, int); - -/** - * Get the column number of the last detected syntax error in the xml file. - * - * @param xid XML-id - * @param clear clear the error state if non zero - * @return the line number of the detected syntax error. - */ -size_t xmlErrorGetColumnNo(const void *, int); - -/** - * Get a string that explains the last error. - * - * @param xid XML-id - * @param clear clear the error state if non zero - * @return a string that explains the last error. - */ -const char *xmlErrorGetString(const void *, int); - -#ifdef __cplusplus -} -#endif - -#endif /* __XML_CONFIG */ - diff --git a/utils/xmlgrep/xml_cache.c b/utils/xmlgrep/xml_cache.c deleted file mode 100644 index 7de2e83bd..000000000 --- a/utils/xmlgrep/xml_cache.c +++ /dev/null @@ -1,250 +0,0 @@ -/* Copyright (c) 2007-2009 by Adalin B.V. - * Copyright (c) 2007-2009 by Erik Hofman - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * 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. - * * Neither the name of (any of) the copyrightholder(s) 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 ``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 COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * 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 -#include -#include - -#include "xml.h" - -#ifndef NDEBUG -# define PRINT(a, b, c) { \ - size_t l1 = (b), l2 = (c); \ - char *s = (a); \ - if (s) { \ - size_t q, len = l2; \ - if (l1 < l2) len = l1; \ - if (len < 50000) { \ - printf("(%i) '", len); \ - for (q=0; qfirst_free > num) || (cache->first_free == 0)); - - if (cache->first_free == 0) /* leaf node */ - { - rv = cache->data; - *len = cache->data_len; - *element = cache->name; - *elementlen = cache->name_len; - *nodenum = 0; - } - else if (*name == '*') - { - struct _xml_node *node = cache->node[num]; - *nc = node; - rv = node->data; - *len = node->data_len; - *element = node->name; - *elementlen = node->name_len; - *nodenum = cache->first_free; - } - else - { - size_t namelen = *elementlen; - size_t i, pos = 0; - - for (i=0; ifirst_free; i++) - { - struct _xml_node *node = cache->node[i]; - - assert(node); - - if ((node->name_len == namelen) && - (!strncasecmp(node->name, name, namelen))) - { - if (pos == num) - { - *nc = node; - rv = node->data; - *element = node->name; - *elementlen = node->name_len; - *len = node->data_len; - *nodenum = cache->first_free; - break; - } - pos++; - } - } - } - - return rv; -} - - -void * -cacheInit() -{ - return calloc(1, sizeof(struct _xml_node)); -} - -void -cacheInitLevel(void *nc) -{ - struct _xml_node *cache = (struct _xml_node *)nc; - - assert(cache != 0); - - cache->node = calloc(NODE_BLOCKSIZE, sizeof(struct _xml_node *)); - cache->no_nodes = NODE_BLOCKSIZE; -} - -void -cacheFree(void *nc) -{ - struct _xml_node *cache = (struct _xml_node *)nc; - - assert(nc != 0); - - if (cache->first_free) - { - struct _xml_node **node = (struct _xml_node **)cache->node; - size_t i = 0; - - while(i < cache->first_free) - { - cacheFree(node[i++]); - } - - free(node); - } - free(cache); -} - -void * -cacheNodeGet(void *id) -{ - struct _xml_id *xid = (struct _xml_id *)id; - struct _xml_node *cache = 0; - - assert(xid != 0); - - if (xid->name) - { - cache = xid->node; - } - else - { - struct _root_id *rid = (struct _root_id *)xid; - cache = rid->node; - } - - return cache; -} - -void * -cacheNodeNew(void *nc) -{ - struct _xml_node *cache = (struct _xml_node *)nc; - struct _xml_node *rv = 0; - size_t i = 0; - - assert(nc != 0); - - i = cache->first_free; - if (i == cache->no_nodes) - { - size_t size, no_nodes; - void *p; - - no_nodes = cache->no_nodes + NODE_BLOCKSIZE; - size = no_nodes * sizeof(struct _xml_node *); - p = realloc(cache->node, size); - if (!p) return 0; - - cache->node = p; - cache->no_nodes = no_nodes; - } - - rv = calloc(1, sizeof(struct _xml_node)); - if (rv) rv->parent = cache; - cache->node[i] = rv; - cache->first_free++; - - return rv; -} - -void -cacheDataSet(void *n, char *name, size_t namelen, char *data, size_t datalen) -{ - struct _xml_node *node = (struct _xml_node *)n; - - assert(node != 0); - assert(name != 0); - assert(namelen != 0); - assert(data != 0); - - node->name = name; - node->name_len = namelen; - node->data = data; - node->data_len = datalen; -} - -#endif - diff --git a/utils/xmlgrep/xml_cache.h b/utils/xmlgrep/xml_cache.h deleted file mode 100644 index b94fb53ee..000000000 --- a/utils/xmlgrep/xml_cache.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright (c) 2007-2009 by Adalin B.V. - * Copyright (c) 2007-2009 by Erik Hofman - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * 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. - * * Neither the name of (any of) the copyrightholder(s) 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 ``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 COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * 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. - */ - -#ifndef __XML_NODECACHE -#define __XML_NODECACHE 1 - -#ifdef __cplusplus -extern "C" { -#endif - -void *cacheInit(); -void cacheInitLevel(void *); -void cacheFree(void *); -void *cacheNodeNew(void *); - -void *cacheNodeGet(void *); -void cacheDataSet(void *, char *, size_t, char *, size_t); - -char *__xmlNodeGetFromCache(void **, const char *, size_t *, char **, size_t *, size_t *); - -#ifdef __cplusplus -} -#endif - -#endif /* __XML_NODECACHE */ - diff --git a/utils/xmlgrep/xmlgrep.c b/utils/xmlgrep/xmlgrep.c deleted file mode 100644 index 23577b0c8..000000000 --- a/utils/xmlgrep/xmlgrep.c +++ /dev/null @@ -1,442 +0,0 @@ -#include - -#define _GNU_SOURCE -#include -#include -#ifndef _MSC_VER -# include -# include /* read */ -#else -# define strncasecmp strnicmp -# include -# include -#endif -#include -#include /* fstat */ -#include /* open */ - -#include "xml.h" - -static const char *_static_root = "/"; -static const char *_static_element = "*"; -static unsigned int _fcount = 0; -static char **_filenames = 0; -static char *_element = 0; -static char *_value = 0; -static char *_root = 0; -static char *_print = 0; -static char *_attribute = 0; -static int print_filenames = 0; - -static void free_and_exit(int i); - -#define USE_BUFFER 0 -#define NODE_NAME_LEN 256 -#define STRING_LEN 2048 - -#define SHOW_NOVAL(opt) \ -{ \ - printf("option '%s' requires a value\n\n", (opt)); \ - free_and_exit(-1); \ -} - -void -show_help () -{ - printf("usage: xmlgrep [options] [file ...]\n\n"); - printf("Options:\n"); - printf("\t-h\t\tshow this help message\n"); - printf("\t-a \tprint this attribute as the output\n"); - printf("\t-e \t\tshow sections that contain this element\n"); - printf("\t-p \t\tprint this element as the output\n"); - printf("\t-r \tspecify the XML search root\n"); - printf("\t-v \tfilter sections that contain this vale\n\n"); - printf(" To print the contents of the 'type' element of the XML section "); - printf("that begins\n at '/printer/output' use the following command:\n\n"); - printf("\txmlgrep -r /printer/output -p type sample.xml\n\n"); - printf(" To filter 'output' elements under '/printer' that have attribute"); - printf(" 'n' set to '1'\n use the following command:\n\n"); - printf("\txmlgrep -r /printer -p output -a n -v 1 sample.xml\n\n"); - printf(" To filter out sections that contain the 'driver' element with "); - printf("'generic' as\n it's value use the following command:"); - printf("\n\n\txmlgrep -r /printer/output -e driver -v generic sample.xml"); - printf("\n\n"); - free_and_exit(0); -} - -void -free_and_exit(int ret) -{ - if (_root && _root != _static_root) free(_root); - if (_element && _element != _static_element) free(_element); - if (_value) free(_value); - if (_print) free(_print); - if (_attribute) free(_attribute); - if (_filenames) - { - unsigned int i; - for (i=0; i < _fcount; i++) - { - if (_filenames[i]) - { - if (print_filenames) printf("%s\n", _filenames[i]); - free(_filenames[i]); - } - } - free(_filenames); - } - - exit(ret); -} - -int -parse_option(char **args, int n, int max) -{ - char *opt, *arg = 0; - unsigned int alen = 0; - unsigned int olen; - - opt = args[n]; - if (strncmp(opt, "--", 2) == 0) - opt++; - - if ((arg = strchr(opt, '=')) != NULL) - { - *arg++ = 0; - } - else if (++n < max) - { - arg = args[n]; -#if 0 - if (arg && arg[0] == '-') - arg = 0; -#endif - } - - olen = strlen(opt); - if (strncmp(opt, "-help", olen) == 0) - { - show_help(); - } - else if (strncmp(opt, "-root", olen) == 0) - { - if (arg == 0) SHOW_NOVAL(opt); - alen = strlen(arg)+1; - if (_root) free(_root); - _root = malloc(alen); - memcpy(_root, arg, alen); - return 2; - } - else if (strncmp(opt, "-element", olen) == 0) - { - if (arg == 0) SHOW_NOVAL(opt); - alen = strlen(arg)+1; - if (_element) free(_element); - _element = malloc(alen); - memcpy(_element, arg, alen); - return 2; - } - else if (strncmp(opt, "-value", olen) == 0) - { - if (arg == 0) SHOW_NOVAL(opt); - alen = strlen(arg)+1; - if (_value) free(_value); - _value = malloc(alen); - memcpy(_value, arg, alen); - return 2; - } - else if (strncmp(opt, "-print", olen) == 0) - { - if (arg == 0) SHOW_NOVAL(opt); - alen = strlen(arg)+1; - if (_print) free(_print); - _print = malloc(alen); - memcpy(_print, arg, alen); - return 2; - } - else if (strncmp(opt, "-attribute", olen) == 0) - { - if (arg == 0) SHOW_NOVAL(opt); - alen = strlen(arg)+1; - if (_attribute) free(_attribute); - _attribute = malloc(alen); - memcpy(_attribute, arg, alen); - return 2; - } - else if (strncmp(opt, "-list-filenames", olen) == 0) - { /* undocumented test argument */ - print_filenames = 1; - return 1; - } - else if (opt[0] == '-') - { - printf("Unknown option %s\n", opt); - free_and_exit(-1); - } - else - { - int pos = _fcount++; - if (_filenames == 0) - { - _filenames = (char **)malloc(sizeof(char*)); - } - else - { - char **ptr = (char **)realloc(_filenames, _fcount*sizeof(char*)); - if (ptr == 0) - { - printf("Out of memory.\n\n"); - free_and_exit(-1); - } - _filenames = ptr; - } - - alen = strlen(opt)+1; - _filenames[pos] = malloc(alen); - memcpy(_filenames[pos], opt, alen); - } - - return 1; -} - -void walk_the_tree(size_t num, void *xid, char *tree) -{ - unsigned int i, no_elements; - - if (!tree) /* last node from the tree */ - { - void *xmid = xmlMarkId(xid); - if (xmid && _print) - { - no_elements = xmlNodeGetNum(xid, _print); - for (i=0; i%s\n", - _filenames[num], _print, _attribute, _value, - value, _print); - } - if (value) free(value); - } - else - { - printf("%s: <%s>%s\n", - _filenames[num], _print, value, _print); - } - } - } - free(xmid); - } - else if (xmid && _value) - { - no_elements = xmlNodeGetNum(xmid, _element); - for (i=0; i%s\n", - _filenames[num], nodename, _value, nodename); - } - } - } - free(xmid); - } - else if (xmid && _element) - { - char parentname[NODE_NAME_LEN]; - - xmlNodeCopyName(xid, (char *)&parentname, NODE_NAME_LEN); - - no_elements = xmlNodeGetNum(xmid, _element); - for (i=0; i <%s>%s \n", - _filenames[num], parentname, nodename, value, - nodename, parentname); - } - } - } - } - else printf("Error executing xmlMarkId\n"); - } - else if (xid) /* walk the rest of the tree */ - { - char *elem, *next; - void *xmid; - - elem = tree; - if (*elem == '/') elem++; - - next = strchr(elem, '/'); - - xmid = xmlMarkId(xid); - if (xmid) - { - if (next) - { - *next++ = 0; - } - - no_elements = xmlNodeGetNum(xid, elem); - for (i=0; i