/*
* xmllint.c : a small tester program for XML input.
*
* See Copyright for the status of this software.
*
*
[email protected]
*/
#include "libxml.h"
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#if defined (_WIN32) && !defined(__CYGWIN__)
#if defined (_MSC_VER) || defined(__BORLANDC__)
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define gettimeofday(p1,p2)
#endif /* _MSC_VER */
#endif /* _WIN32 */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef __MINGW32__
#define _WINSOCKAPI_
#include <wsockcompat.h>
#include <winsock2.h>
#undef XML_SOCKLEN_T
#define XML_SOCKLEN_T unsigned int
#endif
#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
/* seems needed for Solaris */
#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#ifdef HAVE_LIBHISTORY
#include <readline/history.h>
#endif
#endif
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/debugXML.h>
#include <libxml/xmlerror.h>
#ifdef LIBXML_XINCLUDE_ENABLED
#include <libxml/xinclude.h>
#endif
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
#include <libxml/globals.h>
#include <libxml/xmlreader.h>
#ifdef LIBXML_SCHEMATRON_ENABLED
#include <libxml/schematron.h>
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
#endif
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
#ifdef LIBXML_C14N_ENABLED
#include <libxml/c14n.h>
#endif
#ifdef LIBXML_OUTPUT_ENABLED
#include <libxml/xmlsave.h>
#endif
#ifndef XML_XML_DEFAULT_CATALOG
#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
#endif
typedef enum {
XMLLINT_RETURN_OK = 0, /* No error */
XMLLINT_ERR_UNCLASS = 1, /* Unclassified */
XMLLINT_ERR_DTD = 2, /* Error in DTD */
XMLLINT_ERR_VALID = 3, /* Validation error */
XMLLINT_ERR_RDFILE = 4, /* CtxtReadFile error */
XMLLINT_ERR_SCHEMACOMP = 5, /* Schema compilation */
XMLLINT_ERR_OUT = 6, /* Error writing output */
XMLLINT_ERR_SCHEMAPAT = 7, /* Error in schema pattern */
XMLLINT_ERR_RDREGIS = 8, /* Error in Reader registration */
XMLLINT_ERR_MEM = 9, /* Out of memory error */
XMLLINT_ERR_XPATH = 10 /* XPath evaluation error */
} xmllintReturnCode;
#ifdef LIBXML_DEBUG_ENABLED
static int shell = 0;
static int debugent = 0;
#endif
static int debug = 0;
static int maxmem = 0;
#ifdef LIBXML_TREE_ENABLED
static int copy = 0;
#endif /* LIBXML_TREE_ENABLED */
static int recovery = 0;
static int noent = 0;
static int noenc = 0;
static int noblanks = 0;
static int noout = 0;
static int nowrap = 0;
#ifdef LIBXML_OUTPUT_ENABLED
static int format = 0;
static const char *output = NULL;
static int compress = 0;
static int oldout = 0;
#endif /* LIBXML_OUTPUT_ENABLED */
#ifdef LIBXML_VALID_ENABLED
static int valid = 0;
static int postvalid = 0;
static char * dtdvalid = NULL;
static char * dtdvalidfpi = NULL;
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
static char * relaxng = NULL;
static xmlRelaxNGPtr relaxngschemas = NULL;
static char * schema = NULL;
static xmlSchemaPtr wxschemas = NULL;
#endif
#ifdef LIBXML_SCHEMATRON_ENABLED
static char * schematron = NULL;
static xmlSchematronPtr wxschematron = NULL;
#endif
static int repeat = 0;
static int insert = 0;
#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
static int html = 0;
static int xmlout = 0;
#endif
static int htmlout = 0;
#if defined(LIBXML_HTML_ENABLED)
static int nodefdtd = 0;
#endif
#ifdef LIBXML_PUSH_ENABLED
static int push = 0;
#endif /* LIBXML_PUSH_ENABLED */
#ifdef HAVE_SYS_MMAN_H
static int memory = 0;
#endif
static int testIO = 0;
static char *encoding = NULL;
#ifdef LIBXML_XINCLUDE_ENABLED
static int xinclude = 0;
#endif
static int dtdattrs = 0;
static int loaddtd = 0;
static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
static int timing = 0;
static int generate = 0;
static int dropdtd = 0;
#ifdef LIBXML_CATALOG_ENABLED
static int catalogs = 0;
static int nocatalogs = 0;
#endif
#ifdef LIBXML_C14N_ENABLED
static int canonical = 0;
static int canonical_11 = 0;
static int exc_canonical = 0;
#endif
#ifdef LIBXML_READER_ENABLED
static int stream = 0;
static int walker = 0;
#endif /* LIBXML_READER_ENABLED */
static int chkregister = 0;
static int nbregister = 0;
#ifdef LIBXML_SAX1_ENABLED
static int sax1 = 0;
#endif /* LIBXML_SAX1_ENABLED */
#ifdef LIBXML_PATTERN_ENABLED
static const char *pattern = NULL;
static xmlPatternPtr patternc = NULL;
static xmlStreamCtxtPtr patstream = NULL;
#endif
#ifdef LIBXML_XPATH_ENABLED
static const char *xpathquery = NULL;
#endif
static int options = XML_PARSE_COMPACT;
static int sax = 0;
static int oldxml10 = 0;
/************************************************************************
* *
* Entity loading control and customization. *
* *
************************************************************************/
#define MAX_PATHS 64
#ifdef _WIN32
# define PATH_SEPARATOR ';'
#else
# define PATH_SEPARATOR ':'
#endif
static xmlChar *paths[MAX_PATHS + 1];
static int nbpaths = 0;
static int load_trace = 0;
static
void parsePath(const xmlChar *path) {
const xmlChar *cur;
if (path == NULL)
return;
while (*path != 0) {
if (nbpaths >= MAX_PATHS) {
fprintf(stderr, "MAX_PATHS reached: too many paths\n");
return;
}
cur = path;
while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
cur++;
path = cur;
while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
cur++;
if (cur != path) {
paths[nbpaths] = xmlStrndup(path, cur - path);
if (paths[nbpaths] != NULL)
nbpaths++;
path = cur;
}
}
}
static xmlExternalEntityLoader defaultEntityLoader = NULL;
static xmlParserInputPtr
xmllintExternalEntityLoader(const char *URL, const char *ID,
xmlParserCtxtPtr ctxt) {
xmlParserInputPtr ret;
warningSAXFunc warning = NULL;
errorSAXFunc err = NULL;
int i;
const char *lastsegment = URL;
const char *iter = URL;
if ((nbpaths > 0) && (iter != NULL)) {
while (*iter != 0) {
if (*iter == '/')
lastsegment = iter + 1;
iter++;
}
}
if ((ctxt != NULL) && (ctxt->sax != NULL)) {
warning = ctxt->sax->warning;
err = ctxt->sax->error;
ctxt->sax->warning = NULL;
ctxt->sax->error = NULL;
}
if (defaultEntityLoader != NULL) {
ret = defaultEntityLoader(URL, ID, ctxt);
if (ret != NULL) {
if (warning != NULL)
ctxt->sax->warning = warning;
if (err != NULL)
ctxt->sax->error = err;
if (load_trace) {
fprintf \
(stderr,
"Loaded URL=\"%s\" ID=\"%s\"\n",
URL ? URL : "(null)",
ID ? ID : "(null)");
}
return(ret);
}
}
for (i = 0;i < nbpaths;i++) {
xmlChar *newURL;
newURL = xmlStrdup((const xmlChar *) paths[i]);
newURL = xmlStrcat(newURL, (const xmlChar *) "/");
newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
if (newURL != NULL) {
ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
if (ret != NULL) {
if (warning != NULL)
ctxt->sax->warning = warning;
if (err != NULL)
ctxt->sax->error = err;
if (load_trace) {
fprintf \
(stderr,
"Loaded URL=\"%s\" ID=\"%s\"\n",
newURL,
ID ? ID : "(null)");
}
xmlFree(newURL);
return(ret);
}
xmlFree(newURL);
}
}
if (err != NULL)
ctxt->sax->error = err;
if (warning != NULL) {
ctxt->sax->warning = warning;
if (URL != NULL)
warning(ctxt, "failed to load external entity \"%s\"\n", URL)