2 * xmllint.c : a small tester program for XML input.
4 * See Copyright for the status of this software.
15 #if defined (_WIN32) && !defined(__CYGWIN__)
16 #if defined (_MSC_VER) || defined(__BORLANDC__)
18 #pragma comment(lib, "ws2_32.lib")
19 #define gettimeofday(p1,p2)
23 #ifdef HAVE_SYS_TIME_H
32 #include <wsockcompat.h>
35 #define XML_SOCKLEN_T unsigned int
38 #ifdef HAVE_SYS_TIMEB_H
39 #include <sys/timeb.h>
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
54 #ifdef HAVE_SYS_MMAN_H
56 /* seems needed for Solaris */
58 #define MAP_FAILED ((void *) -1)
64 #ifdef HAVE_LIBREADLINE
65 #include <readline/readline.h>
66 #ifdef HAVE_LIBHISTORY
67 #include <readline/history.h>
71 #include <libxml/xmlmemory.h>
72 #include <libxml/parser.h>
73 #include <libxml/parserInternals.h>
74 #include <libxml/HTMLparser.h>
75 #include <libxml/HTMLtree.h>
76 #include <libxml/tree.h>
77 #include <libxml/xpath.h>
78 #include <libxml/debugXML.h>
79 #include <libxml/xmlerror.h>
80 #ifdef LIBXML_XINCLUDE_ENABLED
81 #include <libxml/xinclude.h>
83 #ifdef LIBXML_CATALOG_ENABLED
84 #include <libxml/catalog.h>
86 #include <libxml/globals.h>
87 #include <libxml/xmlreader.h>
88 #ifdef LIBXML_SCHEMATRON_ENABLED
89 #include <libxml/schematron.h>
91 #ifdef LIBXML_SCHEMAS_ENABLED
92 #include <libxml/relaxng.h>
93 #include <libxml/xmlschemas.h>
95 #ifdef LIBXML_PATTERN_ENABLED
96 #include <libxml/pattern.h>
98 #ifdef LIBXML_C14N_ENABLED
99 #include <libxml/c14n.h>
101 #ifdef LIBXML_OUTPUT_ENABLED
102 #include <libxml/xmlsave.h>
105 #ifndef XML_XML_DEFAULT_CATALOG
106 #define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
110 XMLLINT_RETURN_OK = 0, /* No error */
111 XMLLINT_ERR_UNCLASS = 1, /* Unclassified */
112 XMLLINT_ERR_DTD = 2, /* Error in DTD */
113 XMLLINT_ERR_VALID = 3, /* Validation error */
114 XMLLINT_ERR_RDFILE = 4, /* CtxtReadFile error */
115 XMLLINT_ERR_SCHEMACOMP = 5, /* Schema compilation */
116 XMLLINT_ERR_OUT = 6, /* Error writing output */
117 XMLLINT_ERR_SCHEMAPAT = 7, /* Error in schema pattern */
118 XMLLINT_ERR_RDREGIS = 8, /* Error in Reader registration */
119 XMLLINT_ERR_MEM = 9, /* Out of memory error */
120 XMLLINT_ERR_XPATH = 10 /* XPath evaluation error */
122 #ifdef LIBXML_DEBUG_ENABLED
123 static int shell = 0;
124 static int debugent = 0;
126 static int debug = 0;
127 static int maxmem = 0;
128 #ifdef LIBXML_TREE_ENABLED
130 #endif /* LIBXML_TREE_ENABLED */
131 static int recovery = 0;
132 static int noent = 0;
133 static int noenc = 0;
134 static int noblanks = 0;
135 static int noout = 0;
136 static int nowrap = 0;
137 #ifdef LIBXML_OUTPUT_ENABLED
138 static int format = 0;
139 static const char *output = NULL;
140 static int compress = 0;
141 static int oldout = 0;
142 #endif /* LIBXML_OUTPUT_ENABLED */
143 #ifdef LIBXML_VALID_ENABLED
144 static int valid = 0;
145 static int postvalid = 0;
146 static char * dtdvalid = NULL;
147 static char * dtdvalidfpi = NULL;
149 #ifdef LIBXML_SCHEMAS_ENABLED
150 static char * relaxng = NULL;
151 static xmlRelaxNGPtr relaxngschemas = NULL;
152 static char * schema = NULL;
153 static xmlSchemaPtr wxschemas = NULL;
155 #ifdef LIBXML_SCHEMATRON_ENABLED
156 static char * schematron = NULL;
157 static xmlSchematronPtr wxschematron = NULL;
159 static int repeat = 0;
160 static int insert = 0;
161 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
163 static int xmlout = 0;
165 static int htmlout = 0;
166 #if defined(LIBXML_HTML_ENABLED)
167 static int nodefdtd = 0;
169 #ifdef LIBXML_PUSH_ENABLED
171 static int pushsize = 4096;
172 #endif /* LIBXML_PUSH_ENABLED */
174 static int memory = 0;
176 static int testIO = 0;
177 static char *encoding = NULL;
178 #ifdef LIBXML_XINCLUDE_ENABLED
179 static int xinclude = 0;
181 static int dtdattrs = 0;
182 static int loaddtd = 0;
183 static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
184 static int timing = 0;
185 static int generate = 0;
186 static int dropdtd = 0;
187 #ifdef LIBXML_CATALOG_ENABLED
188 static int catalogs = 0;
189 static int nocatalogs = 0;
191 #ifdef LIBXML_C14N_ENABLED
192 static int canonical = 0;
193 static int canonical_11 = 0;
194 static int exc_canonical = 0;
196 #ifdef LIBXML_READER_ENABLED
197 static int stream = 0;
198 static int walker = 0;
199 #endif /* LIBXML_READER_ENABLED */
200 static int chkregister = 0;
201 static int nbregister = 0;
202 #ifdef LIBXML_SAX1_ENABLED
204 #endif /* LIBXML_SAX1_ENABLED */
205 #ifdef LIBXML_PATTERN_ENABLED
206 static const char *pattern = NULL;
207 static xmlPatternPtr patternc = NULL;
208 static xmlStreamCtxtPtr patstream = NULL;
210 #ifdef LIBXML_XPATH_ENABLED
211 static const char *xpathquery = NULL;
213 static int options = XML_PARSE_COMPACT | XML_PARSE_BIG_LINES;
215 static int oldxml10 = 0;
217 /************************************************************************
219 * Entity loading control and customization. *
221 ************************************************************************/
224 # define PATH_SEPARATOR ';'
226 # define PATH_SEPARATOR ':'
228 static xmlChar *paths[MAX_PATHS + 1];
229 static int nbpaths = 0;
230 static int load_trace = 0;
233 void parsePath(const xmlChar *path) {
239 if (nbpaths >= MAX_PATHS) {
240 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
244 while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
247 while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
250 paths[nbpaths] = xmlStrndup(path, cur - path);
251 if (paths[nbpaths] != NULL)
258 static xmlExternalEntityLoader defaultEntityLoader = NULL;
260 static xmlParserInputPtr
261 xmllintExternalEntityLoader(const char *URL, const char *ID,
262 xmlParserCtxtPtr ctxt) {
263 xmlParserInputPtr ret;
264 warningSAXFunc warning = NULL;
265 errorSAXFunc err = NULL;
268 const char *lastsegment = URL;
269 const char *iter = URL;
271 if ((nbpaths > 0) && (iter != NULL)) {
274 lastsegment = iter + 1;
279 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
280 warning = ctxt->sax->warning;
281 err = ctxt->sax->error;
282 ctxt->sax->warning = NULL;
283 ctxt->sax->error = NULL;
286 if (defaultEntityLoader != NULL) {
287 ret = defaultEntityLoader(URL, ID, ctxt);
290 ctxt->sax->warning = warning;
292 ctxt->sax->error = err;
296 "Loaded URL=\"%s\" ID=\"%s\"\n",
297 URL ? URL : "(null)",
303 for (i = 0;i < nbpaths;i++) {
306 newURL = xmlStrdup((const xmlChar *) paths[i]);
307 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
308 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
309 if (newURL != NULL) {
310 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
313 ctxt->sax->warning = warning;
315 ctxt->sax->error = err;
319 "Loaded URL=\"%s\" ID=\"%s\"\n",
330 ctxt->sax->error = err;
331 if (warning != NULL) {
332 ctxt->sax->warning = warning;
334 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
336 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
340 /************************************************************************
342 * Memory allocation consumption debugging *
344 ************************************************************************/
349 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
350 progresult = XMLLINT_ERR_MEM;
354 myFreeFunc(void *mem)
359 myMallocFunc(size_t size)
363 ret = xmlMemMalloc(size);
365 if (xmlMemUsed() > maxmem) {
374 myReallocFunc(void *mem, size_t size)
378 ret = xmlMemRealloc(mem, size);
380 if (xmlMemUsed() > maxmem) {
389 myStrdupFunc(const char *str)
393 ret = xmlMemoryStrdup(str);
395 if (xmlMemUsed() > maxmem) {
403 /************************************************************************
405 * Internal timing routines to remove the necessity to have *
406 * unix-specific function calls. *
408 ************************************************************************/
410 #ifndef HAVE_GETTIMEOFDAY
411 #ifdef HAVE_SYS_TIMEB_H
412 #ifdef HAVE_SYS_TIME_H
416 my_gettimeofday(struct timeval *tvp, void *tzp)
418 struct timeb timebuffer;
422 tvp->tv_sec = timebuffer.time;
423 tvp->tv_usec = timebuffer.millitm * 1000L;
427 #define HAVE_GETTIMEOFDAY 1
428 #define gettimeofday my_gettimeofday
430 #endif /* HAVE_FTIME */
431 #endif /* HAVE_SYS_TIME_H */
432 #endif /* HAVE_SYS_TIMEB_H */
433 #endif /* !HAVE_GETTIMEOFDAY */
435 #if defined(HAVE_GETTIMEOFDAY)
436 static struct timeval begin, end;
439 * startTimer: call where you want to start timing
444 gettimeofday(&begin, NULL);
448 * endTimer: call where you want to stop timing and to print out a
449 * message about the timing performed; format is a printf
453 endTimer(const char *fmt, ...)
458 gettimeofday(&end, NULL);
459 msec = end.tv_sec - begin.tv_sec;
461 msec += (end.tv_usec - begin.tv_usec) / 1000;
463 #ifndef HAVE_STDARG_H
464 #error "endTimer required stdarg functions"
467 vfprintf(stderr, fmt, ap);
470 fprintf(stderr, " took %ld ms\n", msec);
472 #elif defined(HAVE_TIME_H)
474 * No gettimeofday function, so we have to make do with calling clock.
475 * This is obviously less accurate, but there's little we can do about
478 #ifndef CLOCKS_PER_SEC
479 #define CLOCKS_PER_SEC 100
482 static clock_t begin, end;
489 endTimer(const char *fmt, ...)
495 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
497 #ifndef HAVE_STDARG_H
498 #error "endTimer required stdarg functions"
501 vfprintf(stderr, fmt, ap);
503 fprintf(stderr, " took %ld ms\n", msec);
508 * We don't have a gettimeofday or time.h, so we just don't do timing
518 endTimer(char *format, ...)
521 * We cannot do anything because we don't have a timing function
525 va_start(ap, format);
526 vfprintf(stderr, format, ap);
528 fprintf(stderr, " was not timed\n");
530 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
536 /************************************************************************
540 ************************************************************************/
541 static char buffer[50000];
544 xmlHTMLEncodeSend(void) {
547 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
549 xmlGenericError(xmlGenericErrorContext, "%s", result);
556 * xmlHTMLPrintFileInfo:
557 * @input: an xmlParserInputPtr input
559 * Displays the associated file and line informations for the current input
563 xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
565 xmlGenericError(xmlGenericErrorContext, "<p>");
567 len = strlen(buffer);
569 if (input->filename) {
570 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
573 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
580 * xmlHTMLPrintFileContext:
581 * @input: an xmlParserInputPtr input
583 * Displays current context within the input content for error tracking
587 xmlHTMLPrintFileContext(xmlParserInputPtr input) {
588 const xmlChar *cur, *base;
592 if (input == NULL) return;
593 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
596 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
600 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
602 if ((*cur == '\n') || (*cur == '\r')) cur++;
605 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
606 len = strlen(buffer);
607 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
608 (unsigned char) *cur++);
611 len = strlen(buffer);
612 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
614 while ((*cur == '\n') || (*cur == '\r'))
617 while ((cur != base) && (n++ < 80)) {
618 len = strlen(buffer);
619 snprintf(&buffer[len], sizeof(buffer) - len, " ");
622 len = strlen(buffer);
623 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
625 xmlGenericError(xmlGenericErrorContext, "</pre>");
630 * @ctx: an XML parser context
631 * @msg: the message to display/transmit
632 * @...: extra parameters for the message display
634 * Display and format an error messages, gives file, line, position and
638 xmlHTMLError(void *ctx, const char *msg, ...)
640 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
641 xmlParserInputPtr input;
647 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
648 input = ctxt->inputTab[ctxt->inputNr - 2];
651 xmlHTMLPrintFileInfo(input);
653 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
655 len = strlen(buffer);
656 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
659 xmlGenericError(xmlGenericErrorContext, "</p>\n");
661 xmlHTMLPrintFileContext(input);
667 * @ctx: an XML parser context
668 * @msg: the message to display/transmit
669 * @...: extra parameters for the message display
671 * Display and format a warning messages, gives file, line, position and
675 xmlHTMLWarning(void *ctx, const char *msg, ...)
677 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
678 xmlParserInputPtr input;
684 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
685 input = ctxt->inputTab[ctxt->inputNr - 2];
689 xmlHTMLPrintFileInfo(input);
691 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
693 len = strlen(buffer);
694 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
697 xmlGenericError(xmlGenericErrorContext, "</p>\n");
699 xmlHTMLPrintFileContext(input);
704 * xmlHTMLValidityError:
705 * @ctx: an XML parser context
706 * @msg: the message to display/transmit
707 * @...: extra parameters for the message display
709 * Display and format an validity error messages, gives file,
710 * line, position and extra parameters.
713 xmlHTMLValidityError(void *ctx, const char *msg, ...)
715 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
716 xmlParserInputPtr input;
722 if ((input->filename == NULL) && (ctxt->inputNr > 1))
723 input = ctxt->inputTab[ctxt->inputNr - 2];
725 xmlHTMLPrintFileInfo(input);
727 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
728 len = strlen(buffer);
730 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
733 xmlGenericError(xmlGenericErrorContext, "</p>\n");
735 xmlHTMLPrintFileContext(input);
737 progresult = XMLLINT_ERR_VALID;
741 * xmlHTMLValidityWarning:
742 * @ctx: an XML parser context
743 * @msg: the message to display/transmit
744 * @...: extra parameters for the message display
746 * Display and format a validity warning messages, gives file, line,
747 * position and extra parameters.
750 xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
752 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
753 xmlParserInputPtr input;
759 if ((input->filename == NULL) && (ctxt->inputNr > 1))
760 input = ctxt->inputTab[ctxt->inputNr - 2];
762 xmlHTMLPrintFileInfo(input);
764 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
766 len = strlen(buffer);
767 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
770 xmlGenericError(xmlGenericErrorContext, "</p>\n");
772 xmlHTMLPrintFileContext(input);
776 /************************************************************************
780 ************************************************************************/
781 #ifdef LIBXML_DEBUG_ENABLED
782 #ifdef LIBXML_XPATH_ENABLED
785 * @prompt: the prompt value
789 * Returns a pointer to it or NULL on EOF the caller is expected to
790 * free the returned string.
793 xmlShellReadline(char *prompt) {
794 #ifdef HAVE_LIBREADLINE
797 /* Get a line from the user. */
798 line_read = readline (prompt);
800 /* If the line has any text in it, save it on the history. */
801 if (line_read && *line_read)
802 add_history (line_read);
811 fprintf(stdout, "%s", prompt);
812 if (!fgets(line_read, 500, stdin))
815 len = strlen(line_read);
816 ret = (char *) malloc(len + 1);
818 memcpy (ret, line_read, len + 1);
823 #endif /* LIBXML_XPATH_ENABLED */
824 #endif /* LIBXML_DEBUG_ENABLED */
826 /************************************************************************
830 ************************************************************************/
832 static int myRead(FILE *f, char * buf, int len) {
833 return(fread(buf, 1, len, f));
835 static void myClose(FILE *f) {
841 /************************************************************************
845 ************************************************************************/
850 static xmlSAXHandler emptySAXHandlerStruct = {
851 NULL, /* internalSubset */
852 NULL, /* isStandalone */
853 NULL, /* hasInternalSubset */
854 NULL, /* hasExternalSubset */
855 NULL, /* resolveEntity */
856 NULL, /* getEntity */
857 NULL, /* entityDecl */
858 NULL, /* notationDecl */
859 NULL, /* attributeDecl */
860 NULL, /* elementDecl */
861 NULL, /* unparsedEntityDecl */
862 NULL, /* setDocumentLocator */
863 NULL, /* startDocument */
864 NULL, /* endDocument */
865 NULL, /* startElement */
866 NULL, /* endElement */
867 NULL, /* reference */
868 NULL, /* characters */
869 NULL, /* ignorableWhitespace */
870 NULL, /* processingInstruction */
872 NULL, /* xmlParserWarning */
873 NULL, /* xmlParserError */
874 NULL, /* xmlParserError */
875 NULL, /* getParameterEntity */
876 NULL, /* cdataBlock; */
877 NULL, /* externalSubset; */
880 NULL, /* startElementNs */
881 NULL, /* endElementNs */
882 NULL /* xmlStructuredErrorFunc */
885 static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
886 extern xmlSAXHandlerPtr debugSAXHandler;
887 static int callbacks;
891 * @ctxt: An XML parser context
893 * Is this document tagged standalone ?
898 isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
903 fprintf(stdout, "SAX.isStandalone()\n");
908 * hasInternalSubsetDebug:
909 * @ctxt: An XML parser context
911 * Does this document has an internal subset
916 hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
921 fprintf(stdout, "SAX.hasInternalSubset()\n");
926 * hasExternalSubsetDebug:
927 * @ctxt: An XML parser context
929 * Does this document has an external subset
934 hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
939 fprintf(stdout, "SAX.hasExternalSubset()\n");
944 * internalSubsetDebug:
945 * @ctxt: An XML parser context
947 * Does this document has an internal subset
950 internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
951 const xmlChar *ExternalID, const xmlChar *SystemID)
956 fprintf(stdout, "SAX.internalSubset(%s,", name);
957 if (ExternalID == NULL)
958 fprintf(stdout, " ,");
960 fprintf(stdout, " %s,", ExternalID);
961 if (SystemID == NULL)
962 fprintf(stdout, " )\n");
964 fprintf(stdout, " %s)\n", SystemID);
968 * externalSubsetDebug:
969 * @ctxt: An XML parser context
971 * Does this document has an external subset
974 externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
975 const xmlChar *ExternalID, const xmlChar *SystemID)
980 fprintf(stdout, "SAX.externalSubset(%s,", name);
981 if (ExternalID == NULL)
982 fprintf(stdout, " ,");
984 fprintf(stdout, " %s,", ExternalID);
985 if (SystemID == NULL)
986 fprintf(stdout, " )\n");
988 fprintf(stdout, " %s)\n", SystemID);
992 * resolveEntityDebug:
993 * @ctxt: An XML parser context
994 * @publicId: The public ID of the entity
995 * @systemId: The system ID of the entity
997 * Special entity resolver, better left to the parser, it has
998 * more context than the application layer.
999 * The default behaviour is to NOT resolve the entities, in that case
1000 * the ENTITY_REF nodes are built in the structure (and the parameter
1003 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1005 static xmlParserInputPtr
1006 resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
1011 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1014 fprintf(stdout, "SAX.resolveEntity(");
1015 if (publicId != NULL)
1016 fprintf(stdout, "%s", (char *)publicId);
1018 fprintf(stdout, " ");
1019 if (systemId != NULL)
1020 fprintf(stdout, ", %s)\n", (char *)systemId);
1022 fprintf(stdout, ", )\n");
1028 * @ctxt: An XML parser context
1029 * @name: The entity name
1031 * Get an entity by name
1033 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1036 getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1041 fprintf(stdout, "SAX.getEntity(%s)\n", name);
1046 * getParameterEntityDebug:
1047 * @ctxt: An XML parser context
1048 * @name: The entity name
1050 * Get a parameter entity by name
1052 * Returns the xmlParserInputPtr
1055 getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1060 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
1067 * @ctxt: An XML parser context
1068 * @name: the entity name
1069 * @type: the entity type
1070 * @publicId: The public ID of the entity
1071 * @systemId: The system ID of the entity
1072 * @content: the entity value (without processing).
1074 * An entity definition has been parsed
1077 entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1078 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
1080 const xmlChar *nullstr = BAD_CAST "(null)";
1081 /* not all libraries handle printing null pointers nicely */
1082 if (publicId == NULL)
1084 if (systemId == NULL)
1086 if (content == NULL)
1087 content = (xmlChar *)nullstr;
1091 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1092 name, type, publicId, systemId, content);
1096 * attributeDeclDebug:
1097 * @ctxt: An XML parser context
1098 * @name: the attribute name
1099 * @type: the attribute type
1101 * An attribute definition has been parsed
1104 attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
1105 const xmlChar * name, int type, int def,
1106 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1111 if (defaultValue == NULL)
1112 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1113 elem, name, type, def);
1115 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1116 elem, name, type, def, defaultValue);
1117 xmlFreeEnumeration(tree);
1122 * @ctxt: An XML parser context
1123 * @name: the element name
1124 * @type: the element type
1125 * @content: the element value (without processing).
1127 * An element definition has been parsed
1130 elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1131 xmlElementContentPtr content ATTRIBUTE_UNUSED)
1136 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
1141 * notationDeclDebug:
1142 * @ctxt: An XML parser context
1143 * @name: The name of the notation
1144 * @publicId: The public ID of the entity
1145 * @systemId: The system ID of the entity
1147 * What to do when a notation declaration has been parsed.
1150 notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1151 const xmlChar *publicId, const xmlChar *systemId)
1156 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
1157 (char *) name, (char *) publicId, (char *) systemId);
1161 * unparsedEntityDeclDebug:
1162 * @ctxt: An XML parser context
1163 * @name: The name of the entity
1164 * @publicId: The public ID of the entity
1165 * @systemId: The system ID of the entity
1166 * @notationName: the name of the notation
1168 * What to do when an unparsed entity declaration is parsed
1171 unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1172 const xmlChar *publicId, const xmlChar *systemId,
1173 const xmlChar *notationName)
1175 const xmlChar *nullstr = BAD_CAST "(null)";
1177 if (publicId == NULL)
1179 if (systemId == NULL)
1181 if (notationName == NULL)
1182 notationName = nullstr;
1186 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1187 (char *) name, (char *) publicId, (char *) systemId,
1188 (char *) notationName);
1192 * setDocumentLocatorDebug:
1193 * @ctxt: An XML parser context
1194 * @loc: A SAX Locator
1196 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1197 * Everything is available on the context, so this is useless in our case.
1200 setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
1205 fprintf(stdout, "SAX.setDocumentLocator()\n");
1209 * startDocumentDebug:
1210 * @ctxt: An XML parser context
1212 * called when the document start being processed.
1215 startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1220 fprintf(stdout, "SAX.startDocument()\n");
1225 * @ctxt: An XML parser context
1227 * called when the document end has been detected.
1230 endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1235 fprintf(stdout, "SAX.endDocument()\n");
1239 * startElementDebug:
1240 * @ctxt: An XML parser context
1241 * @name: The element name
1243 * called when an opening tag has been processed.
1246 startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
1253 fprintf(stdout, "SAX.startElement(%s", (char *) name);
1255 for (i = 0;(atts[i] != NULL);i++) {
1256 fprintf(stdout, ", %s='", atts[i++]);
1257 if (atts[i] != NULL)
1258 fprintf(stdout, "%s'", atts[i]);
1261 fprintf(stdout, ")\n");
1266 * @ctxt: An XML parser context
1267 * @name: The element name
1269 * called when the end of an element has been detected.
1272 endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1277 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
1282 * @ctxt: An XML parser context
1283 * @ch: a xmlChar string
1284 * @len: the number of xmlChar
1286 * receiving some chars from the parser.
1287 * Question: how much at a time ???
1290 charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1298 for (i = 0;(i<len) && (i < 30);i++)
1302 fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
1307 * @ctxt: An XML parser context
1308 * @name: The entity name
1310 * called when an entity reference is detected.
1313 referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1318 fprintf(stdout, "SAX.reference(%s)\n", name);
1322 * ignorableWhitespaceDebug:
1323 * @ctxt: An XML parser context
1324 * @ch: a xmlChar string
1325 * @start: the first char in the string
1326 * @len: the number of xmlChar
1328 * receiving some ignorable whitespaces from the parser.
1329 * Question: how much at a time ???
1332 ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1340 for (i = 0;(i<len) && (i < 30);i++)
1343 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
1347 * processingInstructionDebug:
1348 * @ctxt: An XML parser context
1349 * @target: the target name
1350 * @data: the PI data's
1351 * @len: the number of xmlChar
1353 * A processing instruction has been parsed.
1356 processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
1357 const xmlChar *data)
1363 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
1364 (char *) target, (char *) data);
1366 fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
1372 * @ctx: the user data (XML parser context)
1373 * @value: The pcdata content
1374 * @len: the block length
1376 * called when a pcdata block has been parsed
1379 cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
1384 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
1385 (char *) value, len);
1390 * @ctxt: An XML parser context
1391 * @value: the comment content
1393 * A comment has been parsed.
1396 commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
1401 fprintf(stdout, "SAX.comment(%s)\n", value);
1406 * @ctxt: An XML parser context
1407 * @msg: the message to display/transmit
1408 * @...: extra parameters for the message display
1410 * Display and format a warning messages, gives file, line, position and
1413 static void XMLCDECL
1414 warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1421 va_start(args, msg);
1422 fprintf(stdout, "SAX.warning: ");
1423 vfprintf(stdout, msg, args);
1429 * @ctxt: An XML parser context
1430 * @msg: the message to display/transmit
1431 * @...: extra parameters for the message display
1433 * Display and format a error messages, gives file, line, position and
1436 static void XMLCDECL
1437 errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1444 va_start(args, msg);
1445 fprintf(stdout, "SAX.error: ");
1446 vfprintf(stdout, msg, args);
1452 * @ctxt: An XML parser context
1453 * @msg: the message to display/transmit
1454 * @...: extra parameters for the message display
1456 * Display and format a fatalError messages, gives file, line, position and
1459 static void XMLCDECL
1460 fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1467 va_start(args, msg);
1468 fprintf(stdout, "SAX.fatalError: ");
1469 vfprintf(stdout, msg, args);
1473 static xmlSAXHandler debugSAXHandlerStruct = {
1474 internalSubsetDebug,
1476 hasInternalSubsetDebug,
1477 hasExternalSubsetDebug,
1484 unparsedEntityDeclDebug,
1485 setDocumentLocatorDebug,
1492 ignorableWhitespaceDebug,
1493 processingInstructionDebug,
1498 getParameterEntityDebug,
1500 externalSubsetDebug,
1508 xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1511 * SAX2 specific callbacks
1514 * startElementNsDebug:
1515 * @ctxt: An XML parser context
1516 * @name: The element name
1518 * called when an opening tag has been processed.
1521 startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1522 const xmlChar *localname,
1523 const xmlChar *prefix,
1526 const xmlChar **namespaces,
1529 const xmlChar **attributes)
1536 fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
1538 fprintf(stdout, ", NULL");
1540 fprintf(stdout, ", %s", (char *) prefix);
1542 fprintf(stdout, ", NULL");
1544 fprintf(stdout, ", '%s'", (char *) URI);
1545 fprintf(stdout, ", %d", nb_namespaces);
1547 if (namespaces != NULL) {
1548 for (i = 0;i < nb_namespaces * 2;i++) {
1549 fprintf(stdout, ", xmlns");
1550 if (namespaces[i] != NULL)
1551 fprintf(stdout, ":%s", namespaces[i]);
1553 fprintf(stdout, "='%s'", namespaces[i]);
1556 fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
1557 if (attributes != NULL) {
1558 for (i = 0;i < nb_attributes * 5;i += 5) {
1559 if (attributes[i + 1] != NULL)
1560 fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
1562 fprintf(stdout, ", %s='", attributes[i]);
1563 fprintf(stdout, "%.4s...', %d", attributes[i + 3],
1564 (int)(attributes[i + 4] - attributes[i + 3]));
1567 fprintf(stdout, ")\n");
1572 * @ctxt: An XML parser context
1573 * @name: The element name
1575 * called when the end of an element has been detected.
1578 endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1579 const xmlChar *localname,
1580 const xmlChar *prefix,
1586 fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
1588 fprintf(stdout, ", NULL");
1590 fprintf(stdout, ", %s", (char *) prefix);
1592 fprintf(stdout, ", NULL)\n");
1594 fprintf(stdout, ", '%s')\n", (char *) URI);
1597 static xmlSAXHandler debugSAX2HandlerStruct = {
1598 internalSubsetDebug,
1600 hasInternalSubsetDebug,
1601 hasExternalSubsetDebug,
1608 unparsedEntityDeclDebug,
1609 setDocumentLocatorDebug,
1616 ignorableWhitespaceDebug,
1617 processingInstructionDebug,
1622 getParameterEntityDebug,
1624 externalSubsetDebug,
1627 startElementNsDebug,
1632 static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
1635 testSAX(const char *filename) {
1636 xmlSAXHandlerPtr handler;
1637 const char *user_data = "user_data"; /* mostly for debugging */
1638 xmlParserInputBufferPtr buf = NULL;
1639 xmlParserInputPtr inputStream;
1640 xmlParserCtxtPtr ctxt = NULL;
1641 xmlSAXHandlerPtr old_sax = NULL;
1646 handler = emptySAXHandler;
1647 #ifdef LIBXML_SAX1_ENABLED
1649 handler = debugSAXHandler;
1652 handler = debugSAX2Handler;
1656 * it's not the simplest code but the most generic in term of I/O
1658 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1663 #ifdef LIBXML_SCHEMAS_ENABLED
1664 if (wxschemas != NULL) {
1666 xmlSchemaValidCtxtPtr vctxt;
1668 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1669 xmlSchemaSetValidErrors(vctxt,
1670 (xmlSchemaValidityErrorFunc) fprintf,
1671 (xmlSchemaValidityWarningFunc) fprintf,
1673 xmlSchemaValidateSetFilename(vctxt, filename);
1675 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
1679 fprintf(stderr, "%s validates\n", filename);
1680 } else if (ret > 0) {
1681 fprintf(stderr, "%s fails to validate\n", filename);
1682 progresult = XMLLINT_ERR_VALID;
1684 fprintf(stderr, "%s validation generated an internal error\n",
1686 progresult = XMLLINT_ERR_VALID;
1689 xmlSchemaFreeValidCtxt(vctxt);
1694 * Create the parser context amd hook the input
1696 ctxt = xmlNewParserCtxt();
1698 xmlFreeParserInputBuffer(buf);
1701 old_sax = ctxt->sax;
1702 ctxt->sax = handler;
1703 ctxt->userData = (void *) user_data;
1704 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1705 if (inputStream == NULL) {
1706 xmlFreeParserInputBuffer(buf);
1709 inputPush(ctxt, inputStream);
1711 /* do the parsing */
1712 xmlParseDocument(ctxt);
1714 if (ctxt->myDoc != NULL) {
1715 fprintf(stderr, "SAX generated a doc !\n");
1716 xmlFreeDoc(ctxt->myDoc);
1723 ctxt->sax = old_sax;
1724 xmlFreeParserCtxt(ctxt);
1728 /************************************************************************
1730 * Stream Test processing *
1732 ************************************************************************/
1733 #ifdef LIBXML_READER_ENABLED
1734 static void processNode(xmlTextReaderPtr reader) {
1735 const xmlChar *name, *value;
1738 type = xmlTextReaderNodeType(reader);
1739 empty = xmlTextReaderIsEmptyElement(reader);
1742 name = xmlTextReaderConstName(reader);
1744 name = BAD_CAST "--";
1746 value = xmlTextReaderConstValue(reader);
1749 printf("%d %d %s %d %d",
1750 xmlTextReaderDepth(reader),
1754 xmlTextReaderHasValue(reader));
1758 printf(" %s\n", value);
1761 #ifdef LIBXML_PATTERN_ENABLED
1763 xmlChar *path = NULL;
1766 if (type == XML_READER_TYPE_ELEMENT) {
1767 /* do the check only on element start */
1768 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1771 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
1772 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1773 printf("Node %s matches pattern %s\n", path, pattern);
1775 printf("Node %s matches pattern %s\n",
1776 xmlTextReaderConstName(reader), pattern);
1780 if (patstream != NULL) {
1783 if (type == XML_READER_TYPE_ELEMENT) {
1784 ret = xmlStreamPush(patstream,
1785 xmlTextReaderConstLocalName(reader),
1786 xmlTextReaderConstNamespaceUri(reader));
1788 fprintf(stderr, "xmlStreamPush() failure\n");
1789 xmlFreeStreamCtxt(patstream);
1791 } else if (ret != match) {
1792 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
1794 path = xmlGetNodePath(
1795 xmlTextReaderCurrentNode(reader));
1799 "xmlPatternMatch and xmlStreamPush disagree\n");
1801 fprintf(stderr, " pattern %s node %s\n",
1804 fprintf(stderr, " pattern %s node %s\n",
1805 pattern, xmlTextReaderConstName(reader));
1809 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1810 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
1811 ret = xmlStreamPop(patstream);
1813 fprintf(stderr, "xmlStreamPop() failure\n");
1814 xmlFreeStreamCtxt(patstream);
1825 static void streamFile(char *filename) {
1826 xmlTextReaderPtr reader;
1831 const char *base = NULL;
1832 xmlParserInputBufferPtr input = NULL;
1835 if (stat(filename, &info) < 0)
1837 if ((fd = open(filename, O_RDONLY)) < 0)
1839 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
1840 if (base == (void *) MAP_FAILED)
1843 reader = xmlReaderForMemory(base, info.st_size, filename,
1847 reader = xmlReaderForFile(filename, NULL, options);
1848 #ifdef LIBXML_PATTERN_ENABLED
1849 if (pattern != NULL) {
1850 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1851 if (patternc == NULL) {
1852 xmlGenericError(xmlGenericErrorContext,
1853 "Pattern %s failed to compile\n", pattern);
1854 progresult = XMLLINT_ERR_SCHEMAPAT;
1858 if (patternc != NULL) {
1859 patstream = xmlPatternGetStreamCtxt(patternc);
1860 if (patstream != NULL) {
1861 ret = xmlStreamPush(patstream, NULL, NULL);
1863 fprintf(stderr, "xmlStreamPush() failure\n");
1864 xmlFreeStreamCtxt(patstream);
1872 if (reader != NULL) {
1873 #ifdef LIBXML_VALID_ENABLED
1875 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
1877 #endif /* LIBXML_VALID_ENABLED */
1879 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
1880 #ifdef LIBXML_SCHEMAS_ENABLED
1881 if (relaxng != NULL) {
1882 if ((timing) && (!repeat)) {
1885 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1887 xmlGenericError(xmlGenericErrorContext,
1888 "Relax-NG schema %s failed to compile\n", relaxng);
1889 progresult = XMLLINT_ERR_SCHEMACOMP;
1892 if ((timing) && (!repeat)) {
1893 endTimer("Compiling the schemas");
1896 if (schema != NULL) {
1897 if ((timing) && (!repeat)) {
1900 ret = xmlTextReaderSchemaValidate(reader, schema);
1902 xmlGenericError(xmlGenericErrorContext,
1903 "XSD schema %s failed to compile\n", schema);
1904 progresult = XMLLINT_ERR_SCHEMACOMP;
1907 if ((timing) && (!repeat)) {
1908 endTimer("Compiling the schemas");
1914 * Process all nodes in sequence
1916 if ((timing) && (!repeat)) {
1919 ret = xmlTextReaderRead(reader);
1922 #ifdef LIBXML_PATTERN_ENABLED
1926 processNode(reader);
1927 ret = xmlTextReaderRead(reader);
1929 if ((timing) && (!repeat)) {
1930 #ifdef LIBXML_SCHEMAS_ENABLED
1931 if (relaxng != NULL)
1932 endTimer("Parsing and validating");
1935 #ifdef LIBXML_VALID_ENABLED
1937 endTimer("Parsing and validating");
1940 endTimer("Parsing");
1943 #ifdef LIBXML_VALID_ENABLED
1945 if (xmlTextReaderIsValid(reader) != 1) {
1946 xmlGenericError(xmlGenericErrorContext,
1947 "Document %s does not validate\n", filename);
1948 progresult = XMLLINT_ERR_VALID;
1951 #endif /* LIBXML_VALID_ENABLED */
1952 #ifdef LIBXML_SCHEMAS_ENABLED
1953 if ((relaxng != NULL) || (schema != NULL)) {
1954 if (xmlTextReaderIsValid(reader) != 1) {
1955 fprintf(stderr, "%s fails to validate\n", filename);
1956 progresult = XMLLINT_ERR_VALID;
1958 fprintf(stderr, "%s validates\n", filename);
1963 * Done, cleanup and status
1965 xmlFreeTextReader(reader);
1967 fprintf(stderr, "%s : failed to parse\n", filename);
1968 progresult = XMLLINT_ERR_UNCLASS;
1971 fprintf(stderr, "Unable to open %s\n", filename);
1972 progresult = XMLLINT_ERR_UNCLASS;
1974 #ifdef LIBXML_PATTERN_ENABLED
1975 if (patstream != NULL) {
1976 xmlFreeStreamCtxt(patstream);
1982 xmlFreeParserInputBuffer(input);
1983 munmap((char *) base, info.st_size);
1989 static void walkDoc(xmlDocPtr doc) {
1990 xmlTextReaderPtr reader;
1993 #ifdef LIBXML_PATTERN_ENABLED
1995 const xmlChar *namespaces[22];
1999 root = xmlDocGetRootElement(doc);
2000 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
2001 namespaces[i++] = ns->href;
2002 namespaces[i++] = ns->prefix;
2004 namespaces[i++] = NULL;
2005 namespaces[i] = NULL;
2007 if (pattern != NULL) {
2008 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
2010 if (patternc == NULL) {
2011 xmlGenericError(xmlGenericErrorContext,
2012 "Pattern %s failed to compile\n", pattern);
2013 progresult = XMLLINT_ERR_SCHEMAPAT;
2017 if (patternc != NULL) {
2018 patstream = xmlPatternGetStreamCtxt(patternc);
2019 if (patstream != NULL) {
2020 ret = xmlStreamPush(patstream, NULL, NULL);
2022 fprintf(stderr, "xmlStreamPush() failure\n");
2023 xmlFreeStreamCtxt(patstream);
2028 #endif /* LIBXML_PATTERN_ENABLED */
2029 reader = xmlReaderWalker(doc);
2030 if (reader != NULL) {
2031 if ((timing) && (!repeat)) {
2034 ret = xmlTextReaderRead(reader);
2037 #ifdef LIBXML_PATTERN_ENABLED
2041 processNode(reader);
2042 ret = xmlTextReaderRead(reader);
2044 if ((timing) && (!repeat)) {
2045 endTimer("walking through the doc");
2047 xmlFreeTextReader(reader);
2049 fprintf(stderr, "failed to walk through the doc\n");
2050 progresult = XMLLINT_ERR_UNCLASS;
2053 fprintf(stderr, "Failed to crate a reader from the document\n");
2054 progresult = XMLLINT_ERR_UNCLASS;
2056 #ifdef LIBXML_PATTERN_ENABLED
2057 if (patstream != NULL) {
2058 xmlFreeStreamCtxt(patstream);
2063 #endif /* LIBXML_READER_ENABLED */
2065 #ifdef LIBXML_XPATH_ENABLED
2066 /************************************************************************
2070 ************************************************************************/
2072 static void doXPathDump(xmlXPathObjectPtr cur) {
2074 case XPATH_NODESET: {
2077 #ifdef LIBXML_OUTPUT_ENABLED
2078 xmlSaveCtxtPtr ctxt;
2080 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr <= 0)) {
2081 fprintf(stderr, "XPath set is empty\n");
2082 progresult = XMLLINT_ERR_XPATH;
2085 ctxt = xmlSaveToFd(1, NULL, 0);
2087 fprintf(stderr, "Out of memory for XPath\n");
2088 progresult = XMLLINT_ERR_MEM;
2091 for (i = 0;i < cur->nodesetval->nodeNr;i++) {
2092 node = cur->nodesetval->nodeTab[i];
2093 xmlSaveTree(ctxt, node);
2097 printf("xpath returned %d nodes\n", cur->nodesetval->nodeNr);
2102 if (cur->boolval) printf("true");
2103 else printf("false");
2106 switch (xmlXPathIsInf(cur->floatval)) {
2111 printf("-Infinity");
2114 if (xmlXPathIsNaN(cur->floatval)) {
2117 printf("%0g", cur->floatval);
2122 printf("%s", (const char *) cur->stringval);
2124 case XPATH_UNDEFINED:
2125 fprintf(stderr, "XPath Object is uninitialized\n");
2126 progresult = XMLLINT_ERR_XPATH;
2129 fprintf(stderr, "XPath object of unexpected type\n");
2130 progresult = XMLLINT_ERR_XPATH;
2135 static void doXPathQuery(xmlDocPtr doc, const char *query) {
2136 xmlXPathContextPtr ctxt;
2137 xmlXPathObjectPtr res;
2139 ctxt = xmlXPathNewContext(doc);
2141 fprintf(stderr, "Out of memory for XPath\n");
2142 progresult = XMLLINT_ERR_MEM;
2145 ctxt->node = (xmlNodePtr) doc;
2146 res = xmlXPathEval(BAD_CAST query, ctxt);
2147 xmlXPathFreeContext(ctxt);
2150 fprintf(stderr, "XPath evaluation failure\n");
2151 progresult = XMLLINT_ERR_XPATH;
2155 xmlXPathFreeObject(res);
2157 #endif /* LIBXML_XPATH_ENABLED */
2159 /************************************************************************
2161 * Tree Test processing *
2163 ************************************************************************/
2164 static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
2165 xmlDocPtr doc = NULL;
2166 #ifdef LIBXML_TREE_ENABLED
2168 #endif /* LIBXML_TREE_ENABLED */
2170 if ((timing) && (!repeat))
2174 #ifdef LIBXML_TREE_ENABLED
2175 if (filename == NULL) {
2179 doc = xmlNewDoc(BAD_CAST "1.0");
2180 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
2181 xmlNodeSetContent(n, BAD_CAST "abc");
2182 xmlDocSetRootElement(doc, n);
2185 #endif /* LIBXML_TREE_ENABLED */
2186 #ifdef LIBXML_HTML_ENABLED
2187 #ifdef LIBXML_PUSH_ENABLED
2188 else if ((html) && (push)) {
2191 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2192 f = fopen(filename, "rb");
2194 f = fopen(filename, "r");
2199 htmlParserCtxtPtr ctxt;
2201 res = fread(chars, 1, 4, f);
2203 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
2204 chars, res, filename, XML_CHAR_ENCODING_NONE);
2205 while ((res = fread(chars, 1, pushsize, f)) > 0) {
2206 htmlParseChunk(ctxt, chars, res, 0);
2208 htmlParseChunk(ctxt, chars, 0, 1);
2210 htmlFreeParserCtxt(ctxt);
2215 #endif /* LIBXML_PUSH_ENABLED */
2217 else if ((html) && (memory)) {
2221 if (stat(filename, &info) < 0)
2223 if ((fd = open(filename, O_RDONLY)) < 0)
2225 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2226 if (base == (void *) MAP_FAILED)
2229 doc = htmlReadMemory((char *) base, info.st_size, filename,
2232 munmap((char *) base, info.st_size);
2237 doc = htmlReadFile(filename, NULL, options);
2239 #endif /* LIBXML_HTML_ENABLED */
2241 #ifdef LIBXML_PUSH_ENABLED
2243 * build an XML tree from a string;
2248 /* '-' Usually means stdin -<sven@zen.org> */
2249 if ((filename[0] == '-') && (filename[1] == 0)) {
2252 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2253 f = fopen(filename, "rb");
2255 f = fopen(filename, "r");
2260 int res, size = 1024;
2262 xmlParserCtxtPtr ctxt;
2264 /* if (repeat) size = 1024; */
2265 res = fread(chars, 1, 4, f);
2267 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2268 chars, res, filename);
2269 xmlCtxtUseOptions(ctxt, options);
2270 while ((res = fread(chars, 1, size, f)) > 0) {
2271 xmlParseChunk(ctxt, chars, res, 0);
2273 xmlParseChunk(ctxt, chars, 0, 1);
2275 ret = ctxt->wellFormed;
2276 xmlFreeParserCtxt(ctxt);
2286 #endif /* LIBXML_PUSH_ENABLED */
2288 if ((filename[0] == '-') && (filename[1] == 0)) {
2289 doc = xmlReadFd(0, NULL, NULL, options);
2293 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2294 f = fopen(filename, "rb");
2296 f = fopen(filename, "r");
2300 doc = xmlReadIO((xmlInputReadCallback) myRead,
2301 (xmlInputCloseCallback) myClose, f,
2302 filename, NULL, options);
2304 doc = xmlCtxtReadIO(rectxt,
2305 (xmlInputReadCallback) myRead,
2306 (xmlInputCloseCallback) myClose, f,
2307 filename, NULL, options);
2311 } else if (htmlout) {
2312 xmlParserCtxtPtr ctxt;
2315 ctxt = xmlNewParserCtxt();
2321 ctxt->sax->error = xmlHTMLError;
2322 ctxt->sax->warning = xmlHTMLWarning;
2323 ctxt->vctxt.error = xmlHTMLValidityError;
2324 ctxt->vctxt.warning = xmlHTMLValidityWarning;
2326 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2329 xmlFreeParserCtxt(ctxt);
2332 } else if (memory) {
2336 if (stat(filename, &info) < 0)
2338 if ((fd = open(filename, O_RDONLY)) < 0)
2340 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2341 if (base == (void *) MAP_FAILED)
2345 doc = xmlReadMemory((char *) base, info.st_size,
2346 filename, NULL, options);
2348 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2349 filename, NULL, options);
2351 munmap((char *) base, info.st_size);
2354 #ifdef LIBXML_VALID_ENABLED
2356 xmlParserCtxtPtr ctxt = NULL;
2359 ctxt = xmlNewParserCtxt();
2365 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2367 if (ctxt->valid == 0)
2368 progresult = XMLLINT_ERR_RDFILE;
2370 xmlFreeParserCtxt(ctxt);
2372 #endif /* LIBXML_VALID_ENABLED */
2375 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
2377 #ifdef LIBXML_SAX1_ENABLED
2379 doc = xmlParseFile(filename);
2381 #endif /* LIBXML_SAX1_ENABLED */
2382 doc = xmlReadFile(filename, NULL, options);
2388 * If we don't have a document we might as well give up. Do we
2389 * want an error message here? <sven@zen.org> */
2391 progresult = XMLLINT_ERR_UNCLASS;
2395 if ((timing) && (!repeat)) {
2396 endTimer("Parsing");
2400 * Remove DOCTYPE nodes
2405 dtd = xmlGetIntSubset(doc);
2407 xmlUnlinkNode((xmlNodePtr)dtd);
2412 #ifdef LIBXML_XINCLUDE_ENABLED
2414 if ((timing) && (!repeat)) {
2417 if (xmlXIncludeProcessFlags(doc, options) < 0)
2418 progresult = XMLLINT_ERR_UNCLASS;
2419 if ((timing) && (!repeat)) {
2420 endTimer("Xinclude processing");
2425 #ifdef LIBXML_XPATH_ENABLED
2426 if (xpathquery != NULL) {
2427 doXPathQuery(doc, xpathquery);
2431 #ifdef LIBXML_DEBUG_ENABLED
2432 #ifdef LIBXML_XPATH_ENABLED
2437 xmlXPathOrderDocElems(doc);
2438 xmlShell(doc, filename, xmlShellReadline, stdout);
2443 #ifdef LIBXML_TREE_ENABLED
2445 * test intermediate copy if needed.
2452 doc = xmlCopyDoc(doc, 1);
2454 endTimer("Copying");
2461 endTimer("Freeing original");
2464 #endif /* LIBXML_TREE_ENABLED */
2466 #ifdef LIBXML_VALID_ENABLED
2467 if ((insert) && (!html)) {
2468 const xmlChar* list[256];
2472 if (doc->children != NULL) {
2473 node = doc->children;
2474 while ((node != NULL) && (node->last == NULL)) node = node->next;
2476 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2478 fprintf(stderr, "could not get valid list of elements\n");
2479 } else if (nb == 0) {
2480 fprintf(stderr, "No element can be inserted under root\n");
2482 fprintf(stderr, "%d element types can be inserted under root:\n",
2484 for (i = 0;i < nb;i++) {
2485 fprintf(stderr, "%s\n", (char *) list[i]);
2491 #endif /* LIBXML_VALID_ENABLED */
2492 #ifdef LIBXML_READER_ENABLED
2496 #endif /* LIBXML_READER_ENABLED */
2497 #ifdef LIBXML_OUTPUT_ENABLED
2504 #ifdef LIBXML_DEBUG_ENABLED
2507 if ((timing) && (!repeat)) {
2510 #ifdef LIBXML_HTML_ENABLED
2511 if ((html) && (!xmlout)) {
2513 htmlSaveFile(output ? output : "-", doc);
2515 else if (encoding != NULL) {
2517 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2520 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2523 else if (format == 1) {
2524 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2531 out = fopen(output,"wb");
2534 if (htmlDocDump(out, doc) < 0)
2535 progresult = XMLLINT_ERR_OUT;
2540 fprintf(stderr, "failed to open %s\n", output);
2541 progresult = XMLLINT_ERR_OUT;
2544 if ((timing) && (!repeat)) {
2549 #ifdef LIBXML_C14N_ENABLED
2551 xmlChar *result = NULL;
2554 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_0, NULL, 1, &result);
2556 if (write(1, result, size) == -1) {
2557 fprintf(stderr, "Can't write data\n");
2561 fprintf(stderr, "Failed to canonicalize\n");
2562 progresult = XMLLINT_ERR_OUT;
2564 } else if (canonical) {
2565 xmlChar *result = NULL;
2568 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_1, NULL, 1, &result);
2570 if (write(1, result, size) == -1) {
2571 fprintf(stderr, "Can't write data\n");
2575 fprintf(stderr, "Failed to canonicalize\n");
2576 progresult = XMLLINT_ERR_OUT;
2579 if (exc_canonical) {
2580 xmlChar *result = NULL;
2583 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_EXCLUSIVE_1_0, NULL, 1, &result);
2585 if (write(1, result, size) == -1) {
2586 fprintf(stderr, "Can't write data\n");
2590 fprintf(stderr, "Failed to canonicalize\n");
2591 progresult = XMLLINT_ERR_OUT;
2600 if (encoding != NULL) {
2602 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
2604 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2608 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2610 xmlDocDumpMemory(doc, &result, &len);
2612 if (result == NULL) {
2613 fprintf(stderr, "Failed to save\n");
2614 progresult = XMLLINT_ERR_OUT;
2616 if (write(1, result, len) == -1) {
2617 fprintf(stderr, "Can't write data\n");
2623 #endif /* HAVE_MMAP */
2625 xmlSaveFile(output ? output : "-", doc);
2626 } else if (oldout) {
2627 if (encoding != NULL) {
2629 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2633 ret = xmlSaveFileEnc(output ? output : "-", doc,
2637 fprintf(stderr, "failed save to %s\n",
2638 output ? output : "-");
2639 progresult = XMLLINT_ERR_OUT;
2641 } else if (format == 1) {
2642 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2644 fprintf(stderr, "failed save to %s\n",
2645 output ? output : "-");
2646 progresult = XMLLINT_ERR_OUT;
2653 out = fopen(output,"wb");
2656 if (xmlDocDump(out, doc) < 0)
2657 progresult = XMLLINT_ERR_OUT;
2662 fprintf(stderr, "failed to open %s\n", output);
2663 progresult = XMLLINT_ERR_OUT;
2667 xmlSaveCtxtPtr ctxt;
2671 saveOpts |= XML_SAVE_FORMAT;
2672 else if (format == 2)
2673 saveOpts |= XML_SAVE_WSNONSIG;
2675 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
2677 saveOpts |= XML_SAVE_AS_XML;
2681 ctxt = xmlSaveToFd(1, encoding, saveOpts);
2683 ctxt = xmlSaveToFilename(output, encoding, saveOpts);
2686 if (xmlSaveDoc(ctxt, doc) < 0) {
2687 fprintf(stderr, "failed save to %s\n",
2688 output ? output : "-");
2689 progresult = XMLLINT_ERR_OUT;
2693 progresult = XMLLINT_ERR_OUT;
2696 if ((timing) && (!repeat)) {
2699 #ifdef LIBXML_DEBUG_ENABLED
2705 out = fopen(output,"wb");
2708 xmlDebugDumpDocument(out, doc);
2713 fprintf(stderr, "failed to open %s\n", output);
2714 progresult = XMLLINT_ERR_OUT;
2719 #endif /* LIBXML_OUTPUT_ENABLED */
2721 #ifdef LIBXML_VALID_ENABLED
2723 * A posteriori validation test
2725 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
2728 if ((timing) && (!repeat)) {
2731 if (dtdvalid != NULL)
2732 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
2734 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
2735 if ((timing) && (!repeat)) {
2736 endTimer("Parsing DTD");
2739 if (dtdvalid != NULL)
2740 xmlGenericError(xmlGenericErrorContext,
2741 "Could not parse DTD %s\n", dtdvalid);
2743 xmlGenericError(xmlGenericErrorContext,
2744 "Could not parse DTD %s\n", dtdvalidfpi);
2745 progresult = XMLLINT_ERR_DTD;
2747 xmlValidCtxtPtr cvp;
2749 if ((cvp = xmlNewValidCtxt()) == NULL) {
2750 xmlGenericError(xmlGenericErrorContext,
2751 "Couldn't allocate validation context\n");
2754 cvp->userData = (void *) stderr;
2755 cvp->error = (xmlValidityErrorFunc) fprintf;
2756 cvp->warning = (xmlValidityWarningFunc) fprintf;
2758 if ((timing) && (!repeat)) {
2761 if (!xmlValidateDtd(cvp, doc, dtd)) {
2762 if (dtdvalid != NULL)
2763 xmlGenericError(xmlGenericErrorContext,
2764 "Document %s does not validate against %s\n",
2765 filename, dtdvalid);
2767 xmlGenericError(xmlGenericErrorContext,
2768 "Document %s does not validate against %s\n",
2769 filename, dtdvalidfpi);
2770 progresult = XMLLINT_ERR_VALID;
2772 if ((timing) && (!repeat)) {
2773 endTimer("Validating against DTD");
2775 xmlFreeValidCtxt(cvp);
2778 } else if (postvalid) {
2779 xmlValidCtxtPtr cvp;
2781 if ((cvp = xmlNewValidCtxt()) == NULL) {
2782 xmlGenericError(xmlGenericErrorContext,
2783 "Couldn't allocate validation context\n");
2787 if ((timing) && (!repeat)) {
2790 cvp->userData = (void *) stderr;
2791 cvp->error = (xmlValidityErrorFunc) fprintf;
2792 cvp->warning = (xmlValidityWarningFunc) fprintf;
2793 if (!xmlValidateDocument(cvp, doc)) {
2794 xmlGenericError(xmlGenericErrorContext,
2795 "Document %s does not validate\n", filename);
2796 progresult = XMLLINT_ERR_VALID;
2798 if ((timing) && (!repeat)) {
2799 endTimer("Validating");
2801 xmlFreeValidCtxt(cvp);
2803 #endif /* LIBXML_VALID_ENABLED */
2804 #ifdef LIBXML_SCHEMATRON_ENABLED
2805 if (wxschematron != NULL) {
2806 xmlSchematronValidCtxtPtr ctxt;
2810 if ((timing) && (!repeat)) {
2815 flag = XML_SCHEMATRON_OUT_XML;
2817 flag = XML_SCHEMATRON_OUT_TEXT;
2819 flag |= XML_SCHEMATRON_OUT_QUIET;
2820 ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
2822 xmlSchematronSetValidErrors(ctxt,
2823 (xmlSchematronValidityErrorFunc) fprintf,
2824 (xmlSchematronValidityWarningFunc) fprintf,
2827 ret = xmlSchematronValidateDoc(ctxt, doc);
2829 fprintf(stderr, "%s validates\n", filename);
2830 } else if (ret > 0) {
2831 fprintf(stderr, "%s fails to validate\n", filename);
2832 progresult = XMLLINT_ERR_VALID;
2834 fprintf(stderr, "%s validation generated an internal error\n",
2836 progresult = XMLLINT_ERR_VALID;
2838 xmlSchematronFreeValidCtxt(ctxt);
2839 if ((timing) && (!repeat)) {
2840 endTimer("Validating");
2844 #ifdef LIBXML_SCHEMAS_ENABLED
2845 if (relaxngschemas != NULL) {
2846 xmlRelaxNGValidCtxtPtr ctxt;
2849 if ((timing) && (!repeat)) {
2853 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2854 xmlRelaxNGSetValidErrors(ctxt,
2855 (xmlRelaxNGValidityErrorFunc) fprintf,
2856 (xmlRelaxNGValidityWarningFunc) fprintf,
2858 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2860 fprintf(stderr, "%s validates\n", filename);
2861 } else if (ret > 0) {
2862 fprintf(stderr, "%s fails to validate\n", filename);
2863 progresult = XMLLINT_ERR_VALID;
2865 fprintf(stderr, "%s validation generated an internal error\n",
2867 progresult = XMLLINT_ERR_VALID;
2869 xmlRelaxNGFreeValidCtxt(ctxt);
2870 if ((timing) && (!repeat)) {
2871 endTimer("Validating");
2873 } else if (wxschemas != NULL) {
2874 xmlSchemaValidCtxtPtr ctxt;
2877 if ((timing) && (!repeat)) {
2881 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2882 xmlSchemaSetValidErrors(ctxt,
2883 (xmlSchemaValidityErrorFunc) fprintf,
2884 (xmlSchemaValidityWarningFunc) fprintf,
2886 ret = xmlSchemaValidateDoc(ctxt, doc);
2888 fprintf(stderr, "%s validates\n", filename);
2889 } else if (ret > 0) {
2890 fprintf(stderr, "%s fails to validate\n", filename);
2891 progresult = XMLLINT_ERR_VALID;
2893 fprintf(stderr, "%s validation generated an internal error\n",
2895 progresult = XMLLINT_ERR_VALID;
2897 xmlSchemaFreeValidCtxt(ctxt);
2898 if ((timing) && (!repeat)) {
2899 endTimer("Validating");
2904 #ifdef LIBXML_DEBUG_ENABLED
2905 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
2906 if ((debugent) && (!html))
2907 xmlDebugDumpEntities(stderr, doc);
2914 if ((timing) && (!repeat)) {
2918 if ((timing) && (!repeat)) {
2919 endTimer("Freeing");
2923 /************************************************************************
2927 ************************************************************************/
2929 static void showVersion(const char *name) {
2930 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2931 fprintf(stderr, " compiled with: ");
2932 if (xmlHasFeature(XML_WITH_THREAD)) fprintf(stderr, "Threads ");
2933 if (xmlHasFeature(XML_WITH_TREE)) fprintf(stderr, "Tree ");
2934 if (xmlHasFeature(XML_WITH_OUTPUT)) fprintf(stderr, "Output ");
2935 if (xmlHasFeature(XML_WITH_PUSH)) fprintf(stderr, "Push ");
2936 if (xmlHasFeature(XML_WITH_READER)) fprintf(stderr, "Reader ");
2937 if (xmlHasFeature(XML_WITH_PATTERN)) fprintf(stderr, "Patterns ");
2938 if (xmlHasFeature(XML_WITH_WRITER)) fprintf(stderr, "Writer ");
2939 if (xmlHasFeature(XML_WITH_SAX1)) fprintf(stderr, "SAXv1 ");
2940 if (xmlHasFeature(XML_WITH_FTP)) fprintf(stderr, "FTP ");
2941 if (xmlHasFeature(XML_WITH_HTTP)) fprintf(stderr, "HTTP ");
2942 if (xmlHasFeature(XML_WITH_VALID)) fprintf(stderr, "DTDValid ");
2943 if (xmlHasFeature(XML_WITH_HTML)) fprintf(stderr, "HTML ");
2944 if (xmlHasFeature(XML_WITH_LEGACY)) fprintf(stderr, "Legacy ");
2945 if (xmlHasFeature(XML_WITH_C14N)) fprintf(stderr, "C14N ");
2946 if (xmlHasFeature(XML_WITH_CATALOG)) fprintf(stderr, "Catalog ");
2947 if (xmlHasFeature(XML_WITH_XPATH)) fprintf(stderr, "XPath ");
2948 if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
2949 if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
2950 if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
2951 if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
2952 if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
2953 if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
2954 if (xmlHasFeature(XML_WITH_AUTOMATA)) fprintf(stderr, "Automata ");
2955 if (xmlHasFeature(XML_WITH_EXPR)) fprintf(stderr, "Expr ");
2956 if (xmlHasFeature(XML_WITH_SCHEMAS)) fprintf(stderr, "Schemas ");
2957 if (xmlHasFeature(XML_WITH_SCHEMATRON)) fprintf(stderr, "Schematron ");
2958 if (xmlHasFeature(XML_WITH_MODULES)) fprintf(stderr, "Modules ");
2959 if (xmlHasFeature(XML_WITH_DEBUG)) fprintf(stderr, "Debug ");
2960 if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug ");
2961 if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug ");
2962 if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib ");
2963 if (xmlHasFeature(XML_WITH_LZMA)) fprintf(stderr, "Lzma ");
2964 fprintf(stderr, "\n");
2967 static void usage(const char *name) {
2968 printf("Usage : %s [options] XMLfiles ...\n", name);
2969 #ifdef LIBXML_OUTPUT_ENABLED
2970 printf("\tParse the XML files and output the result of the parsing\n");
2972 printf("\tParse the XML files\n");
2973 #endif /* LIBXML_OUTPUT_ENABLED */
2974 printf("\t--version : display the version of the XML library used\n");
2975 #ifdef LIBXML_DEBUG_ENABLED
2976 printf("\t--debug : dump a debug tree of the in-memory document\n");
2977 printf("\t--shell : run a navigating shell\n");
2978 printf("\t--debugent : debug the entities defined in the document\n");
2980 #ifdef LIBXML_READER_ENABLED
2981 printf("\t--debug : dump the nodes content when using --stream\n");
2982 #endif /* LIBXML_READER_ENABLED */
2984 #ifdef LIBXML_TREE_ENABLED
2985 printf("\t--copy : used to test the internal copy implementation\n");
2986 #endif /* LIBXML_TREE_ENABLED */
2987 printf("\t--recover : output what was parsable on broken XML documents\n");
2988 printf("\t--huge : remove any internal arbitrary parser limits\n");
2989 printf("\t--noent : substitute entity references by their value\n");
2990 printf("\t--noenc : ignore any encoding specified inside the document\n");
2991 printf("\t--noout : don't output the result tree\n");
2992 printf("\t--path 'paths': provide a set of paths for resources\n");
2993 printf("\t--load-trace : print trace of all external entites loaded\n");
2994 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
2995 printf("\t--nocompact : do not generate compact text nodes\n");
2996 printf("\t--htmlout : output results as HTML\n");
2997 printf("\t--nowrap : do not put HTML doc wrapper\n");
2998 #ifdef LIBXML_VALID_ENABLED
2999 printf("\t--valid : validate the document in addition to std well-formed check\n");
3000 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
3001 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
3002 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
3003 #endif /* LIBXML_VALID_ENABLED */
3004 printf("\t--timing : print some timings\n");
3005 printf("\t--output file or -o file: save to a given file\n");
3006 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
3007 printf("\t--insert : ad-hoc test for valid insertions\n");
3008 #ifdef LIBXML_OUTPUT_ENABLED
3010 printf("\t--compress : turn on gzip compression of output\n");
3012 #endif /* LIBXML_OUTPUT_ENABLED */
3013 #ifdef LIBXML_HTML_ENABLED
3014 printf("\t--html : use the HTML parser\n");
3015 printf("\t--xmlout : force to use the XML serializer when using --html\n");
3016 printf("\t--nodefdtd : do not default HTML doctype\n");
3018 #ifdef LIBXML_PUSH_ENABLED
3019 printf("\t--push : use the push mode of the parser\n");
3020 printf("\t--pushsmall : use the push mode of the parser using tiny increments\n");
3021 #endif /* LIBXML_PUSH_ENABLED */
3023 printf("\t--memory : parse from memory\n");
3025 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
3026 printf("\t--nowarning : do not emit warnings from parser/validator\n");
3027 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
3028 printf("\t--nocdata : replace cdata section with text nodes\n");
3029 #ifdef LIBXML_OUTPUT_ENABLED
3030 printf("\t--format : reformat/reindent the input\n");
3031 printf("\t--encode encoding : output in the given encoding\n");
3032 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
3033 printf("\t--pretty STYLE : pretty-print in a particular style\n");
3034 printf("\t 0 Do not pretty print\n");
3035 printf("\t 1 Format the XML content, as --format\n");
3036 printf("\t 2 Add whitespace inside tags, preserving content\n");
3037 #endif /* LIBXML_OUTPUT_ENABLED */
3038 printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
3039 printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
3040 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
3041 #ifdef LIBXML_C14N_ENABLED
3042 #endif /* LIBXML_C14N_ENABLED */
3043 printf("\t--nsclean : remove redundant namespace declarations\n");
3044 printf("\t--testIO : test user I/O support\n");
3045 #ifdef LIBXML_CATALOG_ENABLED
3046 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
3047 printf("\t otherwise XML Catalogs starting from \n");
3048 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
3049 printf("\t--nocatalogs: deactivate all catalogs\n");
3051 printf("\t--auto : generate a small doc on the fly\n");
3052 #ifdef LIBXML_XINCLUDE_ENABLED
3053 printf("\t--xinclude : do XInclude processing\n");
3054 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
3055 printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
3057 printf("\t--loaddtd : fetch external DTD\n");
3058 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
3059 #ifdef LIBXML_READER_ENABLED
3060 printf("\t--stream : use the streaming interface to process very large files\n");
3061 printf("\t--walker : create a reader and walk though the resulting doc\n");
3062 #endif /* LIBXML_READER_ENABLED */
3063 #ifdef LIBXML_PATTERN_ENABLED
3064 printf("\t--pattern pattern_value : test the pattern support\n");
3066 printf("\t--chkregister : verify the node registration code\n");
3067 #ifdef LIBXML_SCHEMAS_ENABLED
3068 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
3069 printf("\t--schema schema : do validation against the WXS schema\n");
3071 #ifdef LIBXML_SCHEMATRON_ENABLED
3072 printf("\t--schematron schema : do validation against a schematron\n");
3074 #ifdef LIBXML_SAX1_ENABLED
3075 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
3077 printf("\t--sax: do not build a tree but work just at the SAX level\n");
3078 printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
3079 #ifdef LIBXML_XPATH_ENABLED
3080 printf("\t--xpath expr: evaluate the XPath expression, imply --noout\n");
3083 printf("\nLibxml project home page: http://xmlsoft.org/\n");
3084 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
3087 static void registerNode(xmlNodePtr node)
3089 node->_private = malloc(sizeof(long));
3090 *(long*)node->_private = (long) 0x81726354;
3094 static void deregisterNode(xmlNodePtr node)
3096 assert(node->_private != NULL);
3097 assert(*(long*)node->_private == (long) 0x81726354);
3098 free(node->_private);
3103 main(int argc, char **argv) {
3114 for (i = 1; i < argc ; i++) {
3115 if (!strcmp(argv[i], "-"))
3118 if (argv[i][0] != '-')
3120 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
3123 #ifdef LIBXML_DEBUG_ENABLED
3124 if ((!strcmp(argv[i], "-shell")) ||
3125 (!strcmp(argv[i], "--shell"))) {
3130 #ifdef LIBXML_TREE_ENABLED
3131 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
3134 #endif /* LIBXML_TREE_ENABLED */
3135 if ((!strcmp(argv[i], "-recover")) ||
3136 (!strcmp(argv[i], "--recover"))) {
3138 options |= XML_PARSE_RECOVER;
3139 } else if ((!strcmp(argv[i], "-huge")) ||
3140 (!strcmp(argv[i], "--huge"))) {
3141 options |= XML_PARSE_HUGE;
3142 } else if ((!strcmp(argv[i], "-noent")) ||
3143 (!strcmp(argv[i], "--noent"))) {
3145 options |= XML_PARSE_NOENT;
3146 } else if ((!strcmp(argv[i], "-noenc")) ||
3147 (!strcmp(argv[i], "--noenc"))) {
3149 options |= XML_PARSE_IGNORE_ENC;
3150 } else if ((!strcmp(argv[i], "-nsclean")) ||
3151 (!strcmp(argv[i], "--nsclean"))) {
3152 options |= XML_PARSE_NSCLEAN;
3153 } else if ((!strcmp(argv[i], "-nocdata")) ||
3154 (!strcmp(argv[i], "--nocdata"))) {
3155 options |= XML_PARSE_NOCDATA;
3156 } else if ((!strcmp(argv[i], "-nodict")) ||
3157 (!strcmp(argv[i], "--nodict"))) {
3158 options |= XML_PARSE_NODICT;
3159 } else if ((!strcmp(argv[i], "-version")) ||
3160 (!strcmp(argv[i], "--version"))) {
3161 showVersion(argv[0]);
3163 } else if ((!strcmp(argv[i], "-noout")) ||
3164 (!strcmp(argv[i], "--noout")))
3166 #ifdef LIBXML_OUTPUT_ENABLED
3167 else if ((!strcmp(argv[i], "-o")) ||
3168 (!strcmp(argv[i], "-output")) ||
3169 (!strcmp(argv[i], "--output"))) {
3173 #endif /* LIBXML_OUTPUT_ENABLED */
3174 else if ((!strcmp(argv[i], "-htmlout")) ||
3175 (!strcmp(argv[i], "--htmlout")))
3177 else if ((!strcmp(argv[i], "-nowrap")) ||
3178 (!strcmp(argv[i], "--nowrap")))
3180 #ifdef LIBXML_HTML_ENABLED
3181 else if ((!strcmp(argv[i], "-html")) ||
3182 (!strcmp(argv[i], "--html"))) {
3185 else if ((!strcmp(argv[i], "-xmlout")) ||
3186 (!strcmp(argv[i], "--xmlout"))) {
3188 } else if ((!strcmp(argv[i], "-nodefdtd")) ||
3189 (!strcmp(argv[i], "--nodefdtd"))) {
3191 options |= HTML_PARSE_NODEFDTD;
3193 #endif /* LIBXML_HTML_ENABLED */
3194 else if ((!strcmp(argv[i], "-loaddtd")) ||
3195 (!strcmp(argv[i], "--loaddtd"))) {
3197 options |= XML_PARSE_DTDLOAD;
3198 } else if ((!strcmp(argv[i], "-dtdattr")) ||
3199 (!strcmp(argv[i], "--dtdattr"))) {
3202 options |= XML_PARSE_DTDATTR;
3204 #ifdef LIBXML_VALID_ENABLED
3205 else if ((!strcmp(argv[i], "-valid")) ||
3206 (!strcmp(argv[i], "--valid"))) {
3208 options |= XML_PARSE_DTDVALID;
3209 } else if ((!strcmp(argv[i], "-postvalid")) ||
3210 (!strcmp(argv[i], "--postvalid"))) {
3213 options |= XML_PARSE_DTDLOAD;
3214 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
3215 (!strcmp(argv[i], "--dtdvalid"))) {
3219 options |= XML_PARSE_DTDLOAD;
3220 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3221 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3223 dtdvalidfpi = argv[i];
3225 options |= XML_PARSE_DTDLOAD;
3227 #endif /* LIBXML_VALID_ENABLED */
3228 else if ((!strcmp(argv[i], "-dropdtd")) ||
3229 (!strcmp(argv[i], "--dropdtd")))
3231 else if ((!strcmp(argv[i], "-insert")) ||
3232 (!strcmp(argv[i], "--insert")))
3234 else if ((!strcmp(argv[i], "-timing")) ||
3235 (!strcmp(argv[i], "--timing")))
3237 else if ((!strcmp(argv[i], "-auto")) ||
3238 (!strcmp(argv[i], "--auto")))
3240 else if ((!strcmp(argv[i], "-repeat")) ||
3241 (!strcmp(argv[i], "--repeat"))) {
3247 #ifdef LIBXML_PUSH_ENABLED
3248 else if ((!strcmp(argv[i], "-push")) ||
3249 (!strcmp(argv[i], "--push")))
3251 else if ((!strcmp(argv[i], "-pushsmall")) ||
3252 (!strcmp(argv[i], "--pushsmall"))) {
3256 #endif /* LIBXML_PUSH_ENABLED */
3258 else if ((!strcmp(argv[i], "-memory")) ||
3259 (!strcmp(argv[i], "--memory")))
3262 else if ((!strcmp(argv[i], "-testIO")) ||
3263 (!strcmp(argv[i], "--testIO")))
3265 #ifdef LIBXML_XINCLUDE_ENABLED
3266 else if ((!strcmp(argv[i], "-xinclude")) ||
3267 (!strcmp(argv[i], "--xinclude"))) {
3269 options |= XML_PARSE_XINCLUDE;
3271 else if ((!strcmp(argv[i], "-noxincludenode")) ||
3272 (!strcmp(argv[i], "--noxincludenode"))) {
3274 options |= XML_PARSE_XINCLUDE;
3275 options |= XML_PARSE_NOXINCNODE;
3277 else if ((!strcmp(argv[i], "-nofixup-base-uris")) ||
3278 (!strcmp(argv[i], "--nofixup-base-uris"))) {
3280 options |= XML_PARSE_XINCLUDE;
3281 options |= XML_PARSE_NOBASEFIX;
3284 #ifdef LIBXML_OUTPUT_ENABLED
3286 else if ((!strcmp(argv[i], "-compress")) ||
3287 (!strcmp(argv[i], "--compress"))) {
3289 xmlSetCompressMode(9);
3292 #endif /* LIBXML_OUTPUT_ENABLED */
3293 else if ((!strcmp(argv[i], "-nowarning")) ||
3294 (!strcmp(argv[i], "--nowarning"))) {
3295 xmlGetWarningsDefaultValue = 0;
3296 xmlPedanticParserDefault(0);
3297 options |= XML_PARSE_NOWARNING;
3299 else if ((!strcmp(argv[i], "-pedantic")) ||
3300 (!strcmp(argv[i], "--pedantic"))) {
3301 xmlGetWarningsDefaultValue = 1;
3302 xmlPedanticParserDefault(1);
3303 options |= XML_PARSE_PEDANTIC;
3305 #ifdef LIBXML_DEBUG_ENABLED
3306 else if ((!strcmp(argv[i], "-debugent")) ||
3307 (!strcmp(argv[i], "--debugent"))) {
3309 xmlParserDebugEntities = 1;
3312 #ifdef LIBXML_C14N_ENABLED
3313 else if ((!strcmp(argv[i], "-c14n")) ||
3314 (!strcmp(argv[i], "--c14n"))) {
3316 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3318 else if ((!strcmp(argv[i], "-c14n11")) ||
3319 (!strcmp(argv[i], "--c14n11"))) {
3321 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3323 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3324 (!strcmp(argv[i], "--exc-c14n"))) {
3326 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3329 #ifdef LIBXML_CATALOG_ENABLED
3330 else if ((!strcmp(argv[i], "-catalogs")) ||
3331 (!strcmp(argv[i], "--catalogs"))) {
3333 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3334 (!strcmp(argv[i], "--nocatalogs"))) {
3338 else if ((!strcmp(argv[i], "-encode")) ||
3339 (!strcmp(argv[i], "--encode"))) {
3343 * OK it's for testing purposes
3345 xmlAddEncodingAlias("UTF-8", "DVEnc");
3347 else if ((!strcmp(argv[i], "-noblanks")) ||
3348 (!strcmp(argv[i], "--noblanks"))) {
3350 xmlKeepBlanksDefault(0);
3351 options |= XML_PARSE_NOBLANKS;
3353 else if ((!strcmp(argv[i], "-maxmem")) ||
3354 (!strcmp(argv[i], "--maxmem"))) {
3356 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3357 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3363 else if ((!strcmp(argv[i], "-format")) ||
3364 (!strcmp(argv[i], "--format"))) {
3366 #ifdef LIBXML_OUTPUT_ENABLED
3368 #endif /* LIBXML_OUTPUT_ENABLED */
3369 xmlKeepBlanksDefault(0);
3371 else if ((!strcmp(argv[i], "-pretty")) ||
3372 (!strcmp(argv[i], "--pretty"))) {
3374 #ifdef LIBXML_OUTPUT_ENABLED
3375 format = atoi(argv[i]);
3378 xmlKeepBlanksDefault(0);
3380 #endif /* LIBXML_OUTPUT_ENABLED */
3382 #ifdef LIBXML_READER_ENABLED
3383 else if ((!strcmp(argv[i], "-stream")) ||
3384 (!strcmp(argv[i], "--stream"))) {
3387 else if ((!strcmp(argv[i], "-walker")) ||
3388 (!strcmp(argv[i], "--walker"))) {
3392 #endif /* LIBXML_READER_ENABLED */
3393 #ifdef LIBXML_SAX1_ENABLED
3394 else if ((!strcmp(argv[i], "-sax1")) ||
3395 (!strcmp(argv[i], "--sax1"))) {
3397 options |= XML_PARSE_SAX1;
3399 #endif /* LIBXML_SAX1_ENABLED */
3400 else if ((!strcmp(argv[i], "-sax")) ||
3401 (!strcmp(argv[i], "--sax"))) {
3404 else if ((!strcmp(argv[i], "-chkregister")) ||
3405 (!strcmp(argv[i], "--chkregister"))) {
3407 #ifdef LIBXML_SCHEMAS_ENABLED
3408 } else if ((!strcmp(argv[i], "-relaxng")) ||
3409 (!strcmp(argv[i], "--relaxng"))) {
3413 options |= XML_PARSE_NOENT;
3414 } else if ((!strcmp(argv[i], "-schema")) ||
3415 (!strcmp(argv[i], "--schema"))) {
3420 #ifdef LIBXML_SCHEMATRON_ENABLED
3421 } else if ((!strcmp(argv[i], "-schematron")) ||
3422 (!strcmp(argv[i], "--schematron"))) {
3424 schematron = argv[i];
3427 } else if ((!strcmp(argv[i], "-nonet")) ||
3428 (!strcmp(argv[i], "--nonet"))) {
3429 options |= XML_PARSE_NONET;
3430 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
3431 } else if ((!strcmp(argv[i], "-nocompact")) ||
3432 (!strcmp(argv[i], "--nocompact"))) {
3433 options &= ~XML_PARSE_COMPACT;
3434 } else if ((!strcmp(argv[i], "-load-trace")) ||
3435 (!strcmp(argv[i], "--load-trace"))) {
3437 } else if ((!strcmp(argv[i], "-path")) ||
3438 (!strcmp(argv[i], "--path"))) {
3440 parsePath(BAD_CAST argv[i]);
3441 #ifdef LIBXML_PATTERN_ENABLED
3442 } else if ((!strcmp(argv[i], "-pattern")) ||
3443 (!strcmp(argv[i], "--pattern"))) {
3447 #ifdef LIBXML_XPATH_ENABLED
3448 } else if ((!strcmp(argv[i], "-xpath")) ||
3449 (!strcmp(argv[i], "--xpath"))) {
3452 xpathquery = argv[i];
3454 } else if ((!strcmp(argv[i], "-oldxml10")) ||
3455 (!strcmp(argv[i], "--oldxml10"))) {
3457 options |= XML_PARSE_OLD10;
3459 fprintf(stderr, "Unknown option %s\n", argv[i]);
3465 #ifdef LIBXML_CATALOG_ENABLED
3466 if (nocatalogs == 0) {
3470 catal = getenv("SGML_CATALOG_FILES");
3471 if (catal != NULL) {
3472 xmlLoadCatalogs(catal);
3474 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3480 #ifdef LIBXML_SAX1_ENABLED
3482 xmlSAXDefaultVersion(1);
3484 xmlSAXDefaultVersion(2);
3485 #endif /* LIBXML_SAX1_ENABLED */
3488 xmlRegisterNodeDefault(registerNode);
3489 xmlDeregisterNodeDefault(deregisterNode);
3492 indent = getenv("XMLLINT_INDENT");
3493 if(indent != NULL) {
3494 xmlTreeIndentString = indent;
3498 defaultEntityLoader = xmlGetExternalEntityLoader();
3499 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3501 xmlLineNumbersDefault(1);
3503 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3505 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
3506 if (noent != 0) xmlSubstituteEntitiesDefault(1);
3507 #ifdef LIBXML_VALID_ENABLED
3508 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
3509 #endif /* LIBXML_VALID_ENABLED */
3510 if ((htmlout) && (!nowrap)) {
3511 xmlGenericError(xmlGenericErrorContext,
3512 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
3513 xmlGenericError(xmlGenericErrorContext,
3514 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3515 xmlGenericError(xmlGenericErrorContext,
3516 "<html><head><title>%s output</title></head>\n",
3518 xmlGenericError(xmlGenericErrorContext,
3519 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3523 #ifdef LIBXML_SCHEMATRON_ENABLED
3524 if ((schematron != NULL) && (sax == 0)
3525 #ifdef LIBXML_READER_ENABLED
3527 #endif /* LIBXML_READER_ENABLED */
3529 xmlSchematronParserCtxtPtr ctxt;
3531 /* forces loading the DTDs */
3532 xmlLoadExtDtdDefaultValue |= 1;
3533 options |= XML_PARSE_DTDLOAD;
3537 ctxt = xmlSchematronNewParserCtxt(schematron);
3539 xmlSchematronSetParserErrors(ctxt,
3540 (xmlSchematronValidityErrorFunc) fprintf,
3541 (xmlSchematronValidityWarningFunc) fprintf,
3544 wxschematron = xmlSchematronParse(ctxt);
3545 if (wxschematron == NULL) {
3546 xmlGenericError(xmlGenericErrorContext,
3547 "Schematron schema %s failed to compile\n", schematron);
3548 progresult = XMLLINT_ERR_SCHEMACOMP;
3551 xmlSchematronFreeParserCtxt(ctxt);
3553 endTimer("Compiling the schemas");
3557 #ifdef LIBXML_SCHEMAS_ENABLED
3558 if ((relaxng != NULL) && (sax == 0)
3559 #ifdef LIBXML_READER_ENABLED
3561 #endif /* LIBXML_READER_ENABLED */
3563 xmlRelaxNGParserCtxtPtr ctxt;
3565 /* forces loading the DTDs */
3566 xmlLoadExtDtdDefaultValue |= 1;
3567 options |= XML_PARSE_DTDLOAD;
3571 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3572 xmlRelaxNGSetParserErrors(ctxt,
3573 (xmlRelaxNGValidityErrorFunc) fprintf,
3574 (xmlRelaxNGValidityWarningFunc) fprintf,
3576 relaxngschemas = xmlRelaxNGParse(ctxt);
3577 if (relaxngschemas == NULL) {
3578 xmlGenericError(xmlGenericErrorContext,
3579 "Relax-NG schema %s failed to compile\n", relaxng);
3580 progresult = XMLLINT_ERR_SCHEMACOMP;
3583 xmlRelaxNGFreeParserCtxt(ctxt);
3585 endTimer("Compiling the schemas");
3587 } else if ((schema != NULL)
3588 #ifdef LIBXML_READER_ENABLED
3592 xmlSchemaParserCtxtPtr ctxt;
3597 ctxt = xmlSchemaNewParserCtxt(schema);
3598 xmlSchemaSetParserErrors(ctxt,
3599 (xmlSchemaValidityErrorFunc) fprintf,
3600 (xmlSchemaValidityWarningFunc) fprintf,
3602 wxschemas = xmlSchemaParse(ctxt);
3603 if (wxschemas == NULL) {
3604 xmlGenericError(xmlGenericErrorContext,
3605 "WXS schema %s failed to compile\n", schema);
3606 progresult = XMLLINT_ERR_SCHEMACOMP;
3609 xmlSchemaFreeParserCtxt(ctxt);
3611 endTimer("Compiling the schemas");
3614 #endif /* LIBXML_SCHEMAS_ENABLED */
3615 #ifdef LIBXML_PATTERN_ENABLED
3616 if ((pattern != NULL)
3617 #ifdef LIBXML_READER_ENABLED
3621 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
3622 if (patternc == NULL) {
3623 xmlGenericError(xmlGenericErrorContext,
3624 "Pattern %s failed to compile\n", pattern);
3625 progresult = XMLLINT_ERR_SCHEMAPAT;
3629 #endif /* LIBXML_PATTERN_ENABLED */
3630 for (i = 1; i < argc ; i++) {
3631 if ((!strcmp(argv[i], "-encode")) ||
3632 (!strcmp(argv[i], "--encode"))) {
3635 } else if ((!strcmp(argv[i], "-o")) ||
3636 (!strcmp(argv[i], "-output")) ||
3637 (!strcmp(argv[i], "--output"))) {
3641 #ifdef LIBXML_VALID_ENABLED
3642 if ((!strcmp(argv[i], "-dtdvalid")) ||
3643 (!strcmp(argv[i], "--dtdvalid"))) {
3647 if ((!strcmp(argv[i], "-path")) ||
3648 (!strcmp(argv[i], "--path"))) {
3652 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3653 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3657 #endif /* LIBXML_VALID_ENABLED */
3658 if ((!strcmp(argv[i], "-relaxng")) ||
3659 (!strcmp(argv[i], "--relaxng"))) {
3663 if ((!strcmp(argv[i], "-maxmem")) ||
3664 (!strcmp(argv[i], "--maxmem"))) {
3668 if ((!strcmp(argv[i], "-pretty")) ||
3669 (!strcmp(argv[i], "--pretty"))) {
3673 if ((!strcmp(argv[i], "-schema")) ||
3674 (!strcmp(argv[i], "--schema"))) {
3678 if ((!strcmp(argv[i], "-schematron")) ||
3679 (!strcmp(argv[i], "--schematron"))) {
3683 #ifdef LIBXML_PATTERN_ENABLED
3684 if ((!strcmp(argv[i], "-pattern")) ||
3685 (!strcmp(argv[i], "--pattern"))) {
3690 #ifdef LIBXML_XPATH_ENABLED
3691 if ((!strcmp(argv[i], "-xpath")) ||
3692 (!strcmp(argv[i], "--xpath"))) {
3697 if ((timing) && (repeat))
3699 /* Remember file names. "-" means stdin. <sven@zen.org> */
3700 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
3702 xmlParserCtxtPtr ctxt = NULL;
3704 for (acount = 0;acount < repeat;acount++) {
3705 #ifdef LIBXML_READER_ENABLED
3707 streamFile(argv[i]);
3709 #endif /* LIBXML_READER_ENABLED */
3714 ctxt = xmlNewParserCtxt();
3715 parseAndPrintFile(argv[i], ctxt);
3717 #ifdef LIBXML_READER_ENABLED
3719 #endif /* LIBXML_READER_ENABLED */
3722 xmlFreeParserCtxt(ctxt);
3726 #ifdef LIBXML_READER_ENABLED
3728 streamFile(argv[i]);
3730 #endif /* LIBXML_READER_ENABLED */
3734 parseAndPrintFile(argv[i], NULL);
3737 if ((chkregister) && (nbregister != 0)) {
3738 fprintf(stderr, "Registration count off: %d\n", nbregister);
3739 progresult = XMLLINT_ERR_RDREGIS;
3743 if ((timing) && (repeat)) {
3744 endTimer("%d iterations", repeat);
3749 parseAndPrintFile(NULL, NULL);
3750 if ((htmlout) && (!nowrap)) {
3751 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
3753 if ((files == 0) && (!generate) && (version == 0)) {
3756 #ifdef LIBXML_SCHEMATRON_ENABLED
3757 if (wxschematron != NULL)
3758 xmlSchematronFree(wxschematron);
3760 #ifdef LIBXML_SCHEMAS_ENABLED
3761 if (relaxngschemas != NULL)
3762 xmlRelaxNGFree(relaxngschemas);
3763 if (wxschemas != NULL)
3764 xmlSchemaFree(wxschemas);
3765 xmlRelaxNGCleanupTypes();
3767 #ifdef LIBXML_PATTERN_ENABLED
3768 if (patternc != NULL)
3769 xmlFreePattern(patternc);