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 #endif /* LIBXML_PUSH_ENABLED */
172 #ifdef HAVE_SYS_MMAN_H
173 static int memory = 0;
175 static int testIO = 0;
176 static char *encoding = NULL;
177 #ifdef LIBXML_XINCLUDE_ENABLED
178 static int xinclude = 0;
180 static int dtdattrs = 0;
181 static int loaddtd = 0;
182 static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
183 static int timing = 0;
184 static int generate = 0;
185 static int dropdtd = 0;
186 #ifdef LIBXML_CATALOG_ENABLED
187 static int catalogs = 0;
188 static int nocatalogs = 0;
190 #ifdef LIBXML_C14N_ENABLED
191 static int canonical = 0;
192 static int canonical_11 = 0;
193 static int exc_canonical = 0;
195 #ifdef LIBXML_READER_ENABLED
196 static int stream = 0;
197 static int walker = 0;
198 #endif /* LIBXML_READER_ENABLED */
199 static int chkregister = 0;
200 static int nbregister = 0;
201 #ifdef LIBXML_SAX1_ENABLED
203 #endif /* LIBXML_SAX1_ENABLED */
204 #ifdef LIBXML_PATTERN_ENABLED
205 static const char *pattern = NULL;
206 static xmlPatternPtr patternc = NULL;
207 static xmlStreamCtxtPtr patstream = NULL;
209 #ifdef LIBXML_XPATH_ENABLED
210 static const char *xpathquery = NULL;
212 static int options = XML_PARSE_COMPACT;
214 static int oldxml10 = 0;
216 /************************************************************************
218 * Entity loading control and customization. *
220 ************************************************************************/
223 # define PATH_SEPARATOR ';'
225 # define PATH_SEPARATOR ':'
227 static xmlChar *paths[MAX_PATHS + 1];
228 static int nbpaths = 0;
229 static int load_trace = 0;
232 void parsePath(const xmlChar *path) {
238 if (nbpaths >= MAX_PATHS) {
239 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
243 while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
246 while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
249 paths[nbpaths] = xmlStrndup(path, cur - path);
250 if (paths[nbpaths] != NULL)
257 static xmlExternalEntityLoader defaultEntityLoader = NULL;
259 static xmlParserInputPtr
260 xmllintExternalEntityLoader(const char *URL, const char *ID,
261 xmlParserCtxtPtr ctxt) {
262 xmlParserInputPtr ret;
263 warningSAXFunc warning = NULL;
264 errorSAXFunc err = NULL;
267 const char *lastsegment = URL;
268 const char *iter = URL;
270 if ((nbpaths > 0) && (iter != NULL)) {
273 lastsegment = iter + 1;
278 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
279 warning = ctxt->sax->warning;
280 err = ctxt->sax->error;
281 ctxt->sax->warning = NULL;
282 ctxt->sax->error = NULL;
285 if (defaultEntityLoader != NULL) {
286 ret = defaultEntityLoader(URL, ID, ctxt);
289 ctxt->sax->warning = warning;
291 ctxt->sax->error = err;
295 "Loaded URL=\"%s\" ID=\"%s\"\n",
296 URL ? URL : "(null)",
302 for (i = 0;i < nbpaths;i++) {
305 newURL = xmlStrdup((const xmlChar *) paths[i]);
306 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
307 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
308 if (newURL != NULL) {
309 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
312 ctxt->sax->warning = warning;
314 ctxt->sax->error = err;
318 "Loaded URL=\"%s\" ID=\"%s\"\n",
329 ctxt->sax->error = err;
330 if (warning != NULL) {
331 ctxt->sax->warning = warning;
333 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
335 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
339 /************************************************************************
341 * Memory allocation consumption debugging *
343 ************************************************************************/
348 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
349 progresult = XMLLINT_ERR_MEM;
353 myFreeFunc(void *mem)
358 myMallocFunc(size_t size)
362 ret = xmlMemMalloc(size);
364 if (xmlMemUsed() > maxmem) {
373 myReallocFunc(void *mem, size_t size)
377 ret = xmlMemRealloc(mem, size);
379 if (xmlMemUsed() > maxmem) {
388 myStrdupFunc(const char *str)
392 ret = xmlMemoryStrdup(str);
394 if (xmlMemUsed() > maxmem) {
402 /************************************************************************
404 * Internal timing routines to remove the necessity to have *
405 * unix-specific function calls. *
407 ************************************************************************/
409 #ifndef HAVE_GETTIMEOFDAY
410 #ifdef HAVE_SYS_TIMEB_H
411 #ifdef HAVE_SYS_TIME_H
415 my_gettimeofday(struct timeval *tvp, void *tzp)
417 struct timeb timebuffer;
421 tvp->tv_sec = timebuffer.time;
422 tvp->tv_usec = timebuffer.millitm * 1000L;
426 #define HAVE_GETTIMEOFDAY 1
427 #define gettimeofday my_gettimeofday
429 #endif /* HAVE_FTIME */
430 #endif /* HAVE_SYS_TIME_H */
431 #endif /* HAVE_SYS_TIMEB_H */
432 #endif /* !HAVE_GETTIMEOFDAY */
434 #if defined(HAVE_GETTIMEOFDAY)
435 static struct timeval begin, end;
438 * startTimer: call where you want to start timing
443 gettimeofday(&begin, NULL);
447 * endTimer: call where you want to stop timing and to print out a
448 * message about the timing performed; format is a printf
452 endTimer(const char *fmt, ...)
457 gettimeofday(&end, NULL);
458 msec = end.tv_sec - begin.tv_sec;
460 msec += (end.tv_usec - begin.tv_usec) / 1000;
462 #ifndef HAVE_STDARG_H
463 #error "endTimer required stdarg functions"
466 vfprintf(stderr, fmt, ap);
469 fprintf(stderr, " took %ld ms\n", msec);
471 #elif defined(HAVE_TIME_H)
473 * No gettimeofday function, so we have to make do with calling clock.
474 * This is obviously less accurate, but there's little we can do about
477 #ifndef CLOCKS_PER_SEC
478 #define CLOCKS_PER_SEC 100
481 static clock_t begin, end;
488 endTimer(const char *fmt, ...)
494 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
496 #ifndef HAVE_STDARG_H
497 #error "endTimer required stdarg functions"
500 vfprintf(stderr, fmt, ap);
502 fprintf(stderr, " took %ld ms\n", msec);
507 * We don't have a gettimeofday or time.h, so we just don't do timing
517 endTimer(char *format, ...)
520 * We cannot do anything because we don't have a timing function
524 va_start(ap, format);
525 vfprintf(stderr, format, ap);
527 fprintf(stderr, " was not timed\n");
529 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
535 /************************************************************************
539 ************************************************************************/
540 static char buffer[50000];
543 xmlHTMLEncodeSend(void) {
546 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
548 xmlGenericError(xmlGenericErrorContext, "%s", result);
555 * xmlHTMLPrintFileInfo:
556 * @input: an xmlParserInputPtr input
558 * Displays the associated file and line informations for the current input
562 xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
564 xmlGenericError(xmlGenericErrorContext, "<p>");
566 len = strlen(buffer);
568 if (input->filename) {
569 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
572 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
579 * xmlHTMLPrintFileContext:
580 * @input: an xmlParserInputPtr input
582 * Displays current context within the input content for error tracking
586 xmlHTMLPrintFileContext(xmlParserInputPtr input) {
587 const xmlChar *cur, *base;
591 if (input == NULL) return;
592 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
595 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
599 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
601 if ((*cur == '\n') || (*cur == '\r')) cur++;
604 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
605 len = strlen(buffer);
606 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
607 (unsigned char) *cur++);
610 len = strlen(buffer);
611 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
613 while ((*cur == '\n') || (*cur == '\r'))
616 while ((cur != base) && (n++ < 80)) {
617 len = strlen(buffer);
618 snprintf(&buffer[len], sizeof(buffer) - len, " ");
621 len = strlen(buffer);
622 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
624 xmlGenericError(xmlGenericErrorContext, "</pre>");
629 * @ctx: an XML parser context
630 * @msg: the message to display/transmit
631 * @...: extra parameters for the message display
633 * Display and format an error messages, gives file, line, position and
637 xmlHTMLError(void *ctx, const char *msg, ...)
639 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
640 xmlParserInputPtr input;
646 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
647 input = ctxt->inputTab[ctxt->inputNr - 2];
650 xmlHTMLPrintFileInfo(input);
652 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
654 len = strlen(buffer);
655 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
658 xmlGenericError(xmlGenericErrorContext, "</p>\n");
660 xmlHTMLPrintFileContext(input);
666 * @ctx: an XML parser context
667 * @msg: the message to display/transmit
668 * @...: extra parameters for the message display
670 * Display and format a warning messages, gives file, line, position and
674 xmlHTMLWarning(void *ctx, const char *msg, ...)
676 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
677 xmlParserInputPtr input;
683 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
684 input = ctxt->inputTab[ctxt->inputNr - 2];
688 xmlHTMLPrintFileInfo(input);
690 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
692 len = strlen(buffer);
693 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
696 xmlGenericError(xmlGenericErrorContext, "</p>\n");
698 xmlHTMLPrintFileContext(input);
703 * xmlHTMLValidityError:
704 * @ctx: an XML parser context
705 * @msg: the message to display/transmit
706 * @...: extra parameters for the message display
708 * Display and format an validity error messages, gives file,
709 * line, position and extra parameters.
712 xmlHTMLValidityError(void *ctx, const char *msg, ...)
714 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
715 xmlParserInputPtr input;
721 if ((input->filename == NULL) && (ctxt->inputNr > 1))
722 input = ctxt->inputTab[ctxt->inputNr - 2];
724 xmlHTMLPrintFileInfo(input);
726 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
727 len = strlen(buffer);
729 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
732 xmlGenericError(xmlGenericErrorContext, "</p>\n");
734 xmlHTMLPrintFileContext(input);
736 progresult = XMLLINT_ERR_VALID;
740 * xmlHTMLValidityWarning:
741 * @ctx: an XML parser context
742 * @msg: the message to display/transmit
743 * @...: extra parameters for the message display
745 * Display and format a validity warning messages, gives file, line,
746 * position and extra parameters.
749 xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
751 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
752 xmlParserInputPtr input;
758 if ((input->filename == NULL) && (ctxt->inputNr > 1))
759 input = ctxt->inputTab[ctxt->inputNr - 2];
761 xmlHTMLPrintFileInfo(input);
763 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
765 len = strlen(buffer);
766 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
769 xmlGenericError(xmlGenericErrorContext, "</p>\n");
771 xmlHTMLPrintFileContext(input);
775 /************************************************************************
779 ************************************************************************/
780 #ifdef LIBXML_DEBUG_ENABLED
781 #ifdef LIBXML_XPATH_ENABLED
784 * @prompt: the prompt value
788 * Returns a pointer to it or NULL on EOF the caller is expected to
789 * free the returned string.
792 xmlShellReadline(char *prompt) {
793 #ifdef HAVE_LIBREADLINE
796 /* Get a line from the user. */
797 line_read = readline (prompt);
799 /* If the line has any text in it, save it on the history. */
800 if (line_read && *line_read)
801 add_history (line_read);
810 fprintf(stdout, "%s", prompt);
811 if (!fgets(line_read, 500, stdin))
814 len = strlen(line_read);
815 ret = (char *) malloc(len + 1);
817 memcpy (ret, line_read, len + 1);
822 #endif /* LIBXML_XPATH_ENABLED */
823 #endif /* LIBXML_DEBUG_ENABLED */
825 /************************************************************************
829 ************************************************************************/
831 static int myRead(FILE *f, char * buf, int len) {
832 return(fread(buf, 1, len, f));
834 static void myClose(FILE *f) {
840 /************************************************************************
844 ************************************************************************/
849 static xmlSAXHandler emptySAXHandlerStruct = {
850 NULL, /* internalSubset */
851 NULL, /* isStandalone */
852 NULL, /* hasInternalSubset */
853 NULL, /* hasExternalSubset */
854 NULL, /* resolveEntity */
855 NULL, /* getEntity */
856 NULL, /* entityDecl */
857 NULL, /* notationDecl */
858 NULL, /* attributeDecl */
859 NULL, /* elementDecl */
860 NULL, /* unparsedEntityDecl */
861 NULL, /* setDocumentLocator */
862 NULL, /* startDocument */
863 NULL, /* endDocument */
864 NULL, /* startElement */
865 NULL, /* endElement */
866 NULL, /* reference */
867 NULL, /* characters */
868 NULL, /* ignorableWhitespace */
869 NULL, /* processingInstruction */
871 NULL, /* xmlParserWarning */
872 NULL, /* xmlParserError */
873 NULL, /* xmlParserError */
874 NULL, /* getParameterEntity */
875 NULL, /* cdataBlock; */
876 NULL, /* externalSubset; */
879 NULL, /* startElementNs */
880 NULL, /* endElementNs */
881 NULL /* xmlStructuredErrorFunc */
884 static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
885 extern xmlSAXHandlerPtr debugSAXHandler;
886 static int callbacks;
890 * @ctxt: An XML parser context
892 * Is this document tagged standalone ?
897 isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
902 fprintf(stdout, "SAX.isStandalone()\n");
907 * hasInternalSubsetDebug:
908 * @ctxt: An XML parser context
910 * Does this document has an internal subset
915 hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
920 fprintf(stdout, "SAX.hasInternalSubset()\n");
925 * hasExternalSubsetDebug:
926 * @ctxt: An XML parser context
928 * Does this document has an external subset
933 hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
938 fprintf(stdout, "SAX.hasExternalSubset()\n");
943 * internalSubsetDebug:
944 * @ctxt: An XML parser context
946 * Does this document has an internal subset
949 internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
950 const xmlChar *ExternalID, const xmlChar *SystemID)
955 fprintf(stdout, "SAX.internalSubset(%s,", name);
956 if (ExternalID == NULL)
957 fprintf(stdout, " ,");
959 fprintf(stdout, " %s,", ExternalID);
960 if (SystemID == NULL)
961 fprintf(stdout, " )\n");
963 fprintf(stdout, " %s)\n", SystemID);
967 * externalSubsetDebug:
968 * @ctxt: An XML parser context
970 * Does this document has an external subset
973 externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
974 const xmlChar *ExternalID, const xmlChar *SystemID)
979 fprintf(stdout, "SAX.externalSubset(%s,", name);
980 if (ExternalID == NULL)
981 fprintf(stdout, " ,");
983 fprintf(stdout, " %s,", ExternalID);
984 if (SystemID == NULL)
985 fprintf(stdout, " )\n");
987 fprintf(stdout, " %s)\n", SystemID);
991 * resolveEntityDebug:
992 * @ctxt: An XML parser context
993 * @publicId: The public ID of the entity
994 * @systemId: The system ID of the entity
996 * Special entity resolver, better left to the parser, it has
997 * more context than the application layer.
998 * The default behaviour is to NOT resolve the entities, in that case
999 * the ENTITY_REF nodes are built in the structure (and the parameter
1002 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1004 static xmlParserInputPtr
1005 resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
1010 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1013 fprintf(stdout, "SAX.resolveEntity(");
1014 if (publicId != NULL)
1015 fprintf(stdout, "%s", (char *)publicId);
1017 fprintf(stdout, " ");
1018 if (systemId != NULL)
1019 fprintf(stdout, ", %s)\n", (char *)systemId);
1021 fprintf(stdout, ", )\n");
1027 * @ctxt: An XML parser context
1028 * @name: The entity name
1030 * Get an entity by name
1032 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1035 getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1040 fprintf(stdout, "SAX.getEntity(%s)\n", name);
1045 * getParameterEntityDebug:
1046 * @ctxt: An XML parser context
1047 * @name: The entity name
1049 * Get a parameter entity by name
1051 * Returns the xmlParserInputPtr
1054 getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1059 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
1066 * @ctxt: An XML parser context
1067 * @name: the entity name
1068 * @type: the entity type
1069 * @publicId: The public ID of the entity
1070 * @systemId: The system ID of the entity
1071 * @content: the entity value (without processing).
1073 * An entity definition has been parsed
1076 entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1077 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
1079 const xmlChar *nullstr = BAD_CAST "(null)";
1080 /* not all libraries handle printing null pointers nicely */
1081 if (publicId == NULL)
1083 if (systemId == NULL)
1085 if (content == NULL)
1086 content = (xmlChar *)nullstr;
1090 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1091 name, type, publicId, systemId, content);
1095 * attributeDeclDebug:
1096 * @ctxt: An XML parser context
1097 * @name: the attribute name
1098 * @type: the attribute type
1100 * An attribute definition has been parsed
1103 attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
1104 const xmlChar * name, int type, int def,
1105 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1110 if (defaultValue == NULL)
1111 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1112 elem, name, type, def);
1114 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1115 elem, name, type, def, defaultValue);
1116 xmlFreeEnumeration(tree);
1121 * @ctxt: An XML parser context
1122 * @name: the element name
1123 * @type: the element type
1124 * @content: the element value (without processing).
1126 * An element definition has been parsed
1129 elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1130 xmlElementContentPtr content ATTRIBUTE_UNUSED)
1135 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
1140 * notationDeclDebug:
1141 * @ctxt: An XML parser context
1142 * @name: The name of the notation
1143 * @publicId: The public ID of the entity
1144 * @systemId: The system ID of the entity
1146 * What to do when a notation declaration has been parsed.
1149 notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1150 const xmlChar *publicId, const xmlChar *systemId)
1155 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
1156 (char *) name, (char *) publicId, (char *) systemId);
1160 * unparsedEntityDeclDebug:
1161 * @ctxt: An XML parser context
1162 * @name: The name of the entity
1163 * @publicId: The public ID of the entity
1164 * @systemId: The system ID of the entity
1165 * @notationName: the name of the notation
1167 * What to do when an unparsed entity declaration is parsed
1170 unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1171 const xmlChar *publicId, const xmlChar *systemId,
1172 const xmlChar *notationName)
1174 const xmlChar *nullstr = BAD_CAST "(null)";
1176 if (publicId == NULL)
1178 if (systemId == NULL)
1180 if (notationName == NULL)
1181 notationName = nullstr;
1185 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1186 (char *) name, (char *) publicId, (char *) systemId,
1187 (char *) notationName);
1191 * setDocumentLocatorDebug:
1192 * @ctxt: An XML parser context
1193 * @loc: A SAX Locator
1195 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1196 * Everything is available on the context, so this is useless in our case.
1199 setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
1204 fprintf(stdout, "SAX.setDocumentLocator()\n");
1208 * startDocumentDebug:
1209 * @ctxt: An XML parser context
1211 * called when the document start being processed.
1214 startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1219 fprintf(stdout, "SAX.startDocument()\n");
1224 * @ctxt: An XML parser context
1226 * called when the document end has been detected.
1229 endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1234 fprintf(stdout, "SAX.endDocument()\n");
1238 * startElementDebug:
1239 * @ctxt: An XML parser context
1240 * @name: The element name
1242 * called when an opening tag has been processed.
1245 startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
1252 fprintf(stdout, "SAX.startElement(%s", (char *) name);
1254 for (i = 0;(atts[i] != NULL);i++) {
1255 fprintf(stdout, ", %s='", atts[i++]);
1256 if (atts[i] != NULL)
1257 fprintf(stdout, "%s'", atts[i]);
1260 fprintf(stdout, ")\n");
1265 * @ctxt: An XML parser context
1266 * @name: The element name
1268 * called when the end of an element has been detected.
1271 endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1276 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
1281 * @ctxt: An XML parser context
1282 * @ch: a xmlChar string
1283 * @len: the number of xmlChar
1285 * receiving some chars from the parser.
1286 * Question: how much at a time ???
1289 charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1297 for (i = 0;(i<len) && (i < 30);i++)
1301 fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
1306 * @ctxt: An XML parser context
1307 * @name: The entity name
1309 * called when an entity reference is detected.
1312 referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1317 fprintf(stdout, "SAX.reference(%s)\n", name);
1321 * ignorableWhitespaceDebug:
1322 * @ctxt: An XML parser context
1323 * @ch: a xmlChar string
1324 * @start: the first char in the string
1325 * @len: the number of xmlChar
1327 * receiving some ignorable whitespaces from the parser.
1328 * Question: how much at a time ???
1331 ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1339 for (i = 0;(i<len) && (i < 30);i++)
1342 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
1346 * processingInstructionDebug:
1347 * @ctxt: An XML parser context
1348 * @target: the target name
1349 * @data: the PI data's
1350 * @len: the number of xmlChar
1352 * A processing instruction has been parsed.
1355 processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
1356 const xmlChar *data)
1362 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
1363 (char *) target, (char *) data);
1365 fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
1371 * @ctx: the user data (XML parser context)
1372 * @value: The pcdata content
1373 * @len: the block length
1375 * called when a pcdata block has been parsed
1378 cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
1383 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
1384 (char *) value, len);
1389 * @ctxt: An XML parser context
1390 * @value: the comment content
1392 * A comment has been parsed.
1395 commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
1400 fprintf(stdout, "SAX.comment(%s)\n", value);
1405 * @ctxt: An XML parser context
1406 * @msg: the message to display/transmit
1407 * @...: extra parameters for the message display
1409 * Display and format a warning messages, gives file, line, position and
1412 static void XMLCDECL
1413 warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1420 va_start(args, msg);
1421 fprintf(stdout, "SAX.warning: ");
1422 vfprintf(stdout, msg, args);
1428 * @ctxt: An XML parser context
1429 * @msg: the message to display/transmit
1430 * @...: extra parameters for the message display
1432 * Display and format a error messages, gives file, line, position and
1435 static void XMLCDECL
1436 errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1443 va_start(args, msg);
1444 fprintf(stdout, "SAX.error: ");
1445 vfprintf(stdout, msg, args);
1451 * @ctxt: An XML parser context
1452 * @msg: the message to display/transmit
1453 * @...: extra parameters for the message display
1455 * Display and format a fatalError messages, gives file, line, position and
1458 static void XMLCDECL
1459 fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1466 va_start(args, msg);
1467 fprintf(stdout, "SAX.fatalError: ");
1468 vfprintf(stdout, msg, args);
1472 static xmlSAXHandler debugSAXHandlerStruct = {
1473 internalSubsetDebug,
1475 hasInternalSubsetDebug,
1476 hasExternalSubsetDebug,
1483 unparsedEntityDeclDebug,
1484 setDocumentLocatorDebug,
1491 ignorableWhitespaceDebug,
1492 processingInstructionDebug,
1497 getParameterEntityDebug,
1499 externalSubsetDebug,
1507 xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1510 * SAX2 specific callbacks
1513 * startElementNsDebug:
1514 * @ctxt: An XML parser context
1515 * @name: The element name
1517 * called when an opening tag has been processed.
1520 startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1521 const xmlChar *localname,
1522 const xmlChar *prefix,
1525 const xmlChar **namespaces,
1528 const xmlChar **attributes)
1535 fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
1537 fprintf(stdout, ", NULL");
1539 fprintf(stdout, ", %s", (char *) prefix);
1541 fprintf(stdout, ", NULL");
1543 fprintf(stdout, ", '%s'", (char *) URI);
1544 fprintf(stdout, ", %d", nb_namespaces);
1546 if (namespaces != NULL) {
1547 for (i = 0;i < nb_namespaces * 2;i++) {
1548 fprintf(stdout, ", xmlns");
1549 if (namespaces[i] != NULL)
1550 fprintf(stdout, ":%s", namespaces[i]);
1552 fprintf(stdout, "='%s'", namespaces[i]);
1555 fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
1556 if (attributes != NULL) {
1557 for (i = 0;i < nb_attributes * 5;i += 5) {
1558 if (attributes[i + 1] != NULL)
1559 fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
1561 fprintf(stdout, ", %s='", attributes[i]);
1562 fprintf(stdout, "%.4s...', %d", attributes[i + 3],
1563 (int)(attributes[i + 4] - attributes[i + 3]));
1566 fprintf(stdout, ")\n");
1571 * @ctxt: An XML parser context
1572 * @name: The element name
1574 * called when the end of an element has been detected.
1577 endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1578 const xmlChar *localname,
1579 const xmlChar *prefix,
1585 fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
1587 fprintf(stdout, ", NULL");
1589 fprintf(stdout, ", %s", (char *) prefix);
1591 fprintf(stdout, ", NULL)\n");
1593 fprintf(stdout, ", '%s')\n", (char *) URI);
1596 static xmlSAXHandler debugSAX2HandlerStruct = {
1597 internalSubsetDebug,
1599 hasInternalSubsetDebug,
1600 hasExternalSubsetDebug,
1607 unparsedEntityDeclDebug,
1608 setDocumentLocatorDebug,
1615 ignorableWhitespaceDebug,
1616 processingInstructionDebug,
1621 getParameterEntityDebug,
1623 externalSubsetDebug,
1626 startElementNsDebug,
1631 static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
1634 testSAX(const char *filename) {
1635 xmlSAXHandlerPtr handler;
1636 const char *user_data = "user_data"; /* mostly for debugging */
1637 xmlParserInputBufferPtr buf = NULL;
1638 xmlParserInputPtr inputStream;
1639 xmlParserCtxtPtr ctxt = NULL;
1640 xmlSAXHandlerPtr old_sax = NULL;
1645 handler = emptySAXHandler;
1646 #ifdef LIBXML_SAX1_ENABLED
1648 handler = debugSAXHandler;
1651 handler = debugSAX2Handler;
1655 * it's not the simplest code but the most generic in term of I/O
1657 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1662 #ifdef LIBXML_SCHEMAS_ENABLED
1663 if (wxschemas != NULL) {
1665 xmlSchemaValidCtxtPtr vctxt;
1667 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1668 xmlSchemaSetValidErrors(vctxt,
1669 (xmlSchemaValidityErrorFunc) fprintf,
1670 (xmlSchemaValidityWarningFunc) fprintf,
1673 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
1677 fprintf(stderr, "%s validates\n", filename);
1678 } else if (ret > 0) {
1679 fprintf(stderr, "%s fails to validate\n", filename);
1680 progresult = XMLLINT_ERR_VALID;
1682 fprintf(stderr, "%s validation generated an internal error\n",
1684 progresult = XMLLINT_ERR_VALID;
1687 xmlSchemaFreeValidCtxt(vctxt);
1692 * Create the parser context amd hook the input
1694 ctxt = xmlNewParserCtxt();
1696 xmlFreeParserInputBuffer(buf);
1699 old_sax = ctxt->sax;
1700 ctxt->sax = handler;
1701 ctxt->userData = (void *) user_data;
1702 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1703 if (inputStream == NULL) {
1704 xmlFreeParserInputBuffer(buf);
1707 inputPush(ctxt, inputStream);
1709 /* do the parsing */
1710 xmlParseDocument(ctxt);
1712 if (ctxt->myDoc != NULL) {
1713 fprintf(stderr, "SAX generated a doc !\n");
1714 xmlFreeDoc(ctxt->myDoc);
1721 ctxt->sax = old_sax;
1722 xmlFreeParserCtxt(ctxt);
1726 /************************************************************************
1728 * Stream Test processing *
1730 ************************************************************************/
1731 #ifdef LIBXML_READER_ENABLED
1732 static void processNode(xmlTextReaderPtr reader) {
1733 const xmlChar *name, *value;
1736 type = xmlTextReaderNodeType(reader);
1737 empty = xmlTextReaderIsEmptyElement(reader);
1740 name = xmlTextReaderConstName(reader);
1742 name = BAD_CAST "--";
1744 value = xmlTextReaderConstValue(reader);
1747 printf("%d %d %s %d %d",
1748 xmlTextReaderDepth(reader),
1752 xmlTextReaderHasValue(reader));
1756 printf(" %s\n", value);
1759 #ifdef LIBXML_PATTERN_ENABLED
1761 xmlChar *path = NULL;
1764 if (type == XML_READER_TYPE_ELEMENT) {
1765 /* do the check only on element start */
1766 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1769 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
1770 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1771 printf("Node %s matches pattern %s\n", path, pattern);
1773 printf("Node %s matches pattern %s\n",
1774 xmlTextReaderConstName(reader), pattern);
1778 if (patstream != NULL) {
1781 if (type == XML_READER_TYPE_ELEMENT) {
1782 ret = xmlStreamPush(patstream,
1783 xmlTextReaderConstLocalName(reader),
1784 xmlTextReaderConstNamespaceUri(reader));
1786 fprintf(stderr, "xmlStreamPush() failure\n");
1787 xmlFreeStreamCtxt(patstream);
1789 } else if (ret != match) {
1790 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
1792 path = xmlGetNodePath(
1793 xmlTextReaderCurrentNode(reader));
1797 "xmlPatternMatch and xmlStreamPush disagree\n");
1799 fprintf(stderr, " pattern %s node %s\n",
1802 fprintf(stderr, " pattern %s node %s\n",
1803 pattern, xmlTextReaderConstName(reader));
1807 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1808 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
1809 ret = xmlStreamPop(patstream);
1811 fprintf(stderr, "xmlStreamPop() failure\n");
1812 xmlFreeStreamCtxt(patstream);
1823 static void streamFile(char *filename) {
1824 xmlTextReaderPtr reader;
1826 #ifdef HAVE_SYS_MMAN_H
1829 const char *base = NULL;
1830 xmlParserInputBufferPtr input = NULL;
1833 if (stat(filename, &info) < 0)
1835 if ((fd = open(filename, O_RDONLY)) < 0)
1837 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
1838 if (base == (void *) MAP_FAILED)
1841 reader = xmlReaderForMemory(base, info.st_size, filename,
1845 reader = xmlReaderForFile(filename, NULL, options);
1846 #ifdef LIBXML_PATTERN_ENABLED
1847 if (pattern != NULL) {
1848 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1849 if (patternc == NULL) {
1850 xmlGenericError(xmlGenericErrorContext,
1851 "Pattern %s failed to compile\n", pattern);
1852 progresult = XMLLINT_ERR_SCHEMAPAT;
1856 if (patternc != NULL) {
1857 patstream = xmlPatternGetStreamCtxt(patternc);
1858 if (patstream != NULL) {
1859 ret = xmlStreamPush(patstream, NULL, NULL);
1861 fprintf(stderr, "xmlStreamPush() failure\n");
1862 xmlFreeStreamCtxt(patstream);
1870 if (reader != NULL) {
1871 #ifdef LIBXML_VALID_ENABLED
1873 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
1875 #endif /* LIBXML_VALID_ENABLED */
1876 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
1877 #ifdef LIBXML_SCHEMAS_ENABLED
1878 if (relaxng != NULL) {
1879 if ((timing) && (!repeat)) {
1882 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1884 xmlGenericError(xmlGenericErrorContext,
1885 "Relax-NG schema %s failed to compile\n", relaxng);
1886 progresult = XMLLINT_ERR_SCHEMACOMP;
1889 if ((timing) && (!repeat)) {
1890 endTimer("Compiling the schemas");
1893 if (schema != NULL) {
1894 if ((timing) && (!repeat)) {
1897 ret = xmlTextReaderSchemaValidate(reader, schema);
1899 xmlGenericError(xmlGenericErrorContext,
1900 "XSD schema %s failed to compile\n", schema);
1901 progresult = XMLLINT_ERR_SCHEMACOMP;
1904 if ((timing) && (!repeat)) {
1905 endTimer("Compiling the schemas");
1911 * Process all nodes in sequence
1913 if ((timing) && (!repeat)) {
1916 ret = xmlTextReaderRead(reader);
1919 #ifdef LIBXML_PATTERN_ENABLED
1923 processNode(reader);
1924 ret = xmlTextReaderRead(reader);
1926 if ((timing) && (!repeat)) {
1927 #ifdef LIBXML_SCHEMAS_ENABLED
1928 if (relaxng != NULL)
1929 endTimer("Parsing and validating");
1932 #ifdef LIBXML_VALID_ENABLED
1934 endTimer("Parsing and validating");
1937 endTimer("Parsing");
1940 #ifdef LIBXML_VALID_ENABLED
1942 if (xmlTextReaderIsValid(reader) != 1) {
1943 xmlGenericError(xmlGenericErrorContext,
1944 "Document %s does not validate\n", filename);
1945 progresult = XMLLINT_ERR_VALID;
1948 #endif /* LIBXML_VALID_ENABLED */
1949 #ifdef LIBXML_SCHEMAS_ENABLED
1950 if ((relaxng != NULL) || (schema != NULL)) {
1951 if (xmlTextReaderIsValid(reader) != 1) {
1952 fprintf(stderr, "%s fails to validate\n", filename);
1953 progresult = XMLLINT_ERR_VALID;
1955 fprintf(stderr, "%s validates\n", filename);
1960 * Done, cleanup and status
1962 xmlFreeTextReader(reader);
1964 fprintf(stderr, "%s : failed to parse\n", filename);
1965 progresult = XMLLINT_ERR_UNCLASS;
1968 fprintf(stderr, "Unable to open %s\n", filename);
1969 progresult = XMLLINT_ERR_UNCLASS;
1971 #ifdef LIBXML_PATTERN_ENABLED
1972 if (patstream != NULL) {
1973 xmlFreeStreamCtxt(patstream);
1977 #ifdef HAVE_SYS_MMAN_H
1979 xmlFreeParserInputBuffer(input);
1980 munmap((char *) base, info.st_size);
1986 static void walkDoc(xmlDocPtr doc) {
1987 xmlTextReaderPtr reader;
1990 #ifdef LIBXML_PATTERN_ENABLED
1992 const xmlChar *namespaces[22];
1996 root = xmlDocGetRootElement(doc);
1997 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
1998 namespaces[i++] = ns->href;
1999 namespaces[i++] = ns->prefix;
2001 namespaces[i++] = NULL;
2002 namespaces[i] = NULL;
2004 if (pattern != NULL) {
2005 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
2007 if (patternc == NULL) {
2008 xmlGenericError(xmlGenericErrorContext,
2009 "Pattern %s failed to compile\n", pattern);
2010 progresult = XMLLINT_ERR_SCHEMAPAT;
2014 if (patternc != NULL) {
2015 patstream = xmlPatternGetStreamCtxt(patternc);
2016 if (patstream != NULL) {
2017 ret = xmlStreamPush(patstream, NULL, NULL);
2019 fprintf(stderr, "xmlStreamPush() failure\n");
2020 xmlFreeStreamCtxt(patstream);
2025 #endif /* LIBXML_PATTERN_ENABLED */
2026 reader = xmlReaderWalker(doc);
2027 if (reader != NULL) {
2028 if ((timing) && (!repeat)) {
2031 ret = xmlTextReaderRead(reader);
2034 #ifdef LIBXML_PATTERN_ENABLED
2038 processNode(reader);
2039 ret = xmlTextReaderRead(reader);
2041 if ((timing) && (!repeat)) {
2042 endTimer("walking through the doc");
2044 xmlFreeTextReader(reader);
2046 fprintf(stderr, "failed to walk through the doc\n");
2047 progresult = XMLLINT_ERR_UNCLASS;
2050 fprintf(stderr, "Failed to crate a reader from the document\n");
2051 progresult = XMLLINT_ERR_UNCLASS;
2053 #ifdef LIBXML_PATTERN_ENABLED
2054 if (patstream != NULL) {
2055 xmlFreeStreamCtxt(patstream);
2060 #endif /* LIBXML_READER_ENABLED */
2062 #ifdef LIBXML_XPATH_ENABLED
2063 /************************************************************************
2067 ************************************************************************/
2069 static void doXPathDump(xmlXPathObjectPtr cur) {
2071 case XPATH_NODESET: {
2074 #ifdef LIBXML_OUTPUT_ENABLED
2075 xmlSaveCtxtPtr ctxt;
2077 if ((cur->nodesetval == NULL) || (cur->nodesetval->nodeNr <= 0)) {
2078 fprintf(stderr, "XPath set is empty\n");
2079 progresult = XMLLINT_ERR_XPATH;
2082 ctxt = xmlSaveToFd(1, NULL, 0);
2084 fprintf(stderr, "Out of memory for XPath\n");
2085 progresult = XMLLINT_ERR_MEM;
2088 for (i = 0;i < cur->nodesetval->nodeNr;i++) {
2089 node = cur->nodesetval->nodeTab[i];
2090 xmlSaveTree(ctxt, node);
2094 printf("xpath returned %d nodes\n", cur->nodesetval->nodeNr);
2099 if (cur->boolval) printf("true");
2100 else printf("false");
2103 switch (xmlXPathIsInf(cur->floatval)) {
2108 printf("-Infinity");
2111 if (xmlXPathIsNaN(cur->floatval)) {
2114 printf("%0g", cur->floatval);
2119 printf("%s", (const char *) cur->stringval);
2121 case XPATH_UNDEFINED:
2122 fprintf(stderr, "XPath Object is uninitialized\n");
2123 progresult = XMLLINT_ERR_XPATH;
2126 fprintf(stderr, "XPath object of unexpected type\n");
2127 progresult = XMLLINT_ERR_XPATH;
2132 static void doXPathQuery(xmlDocPtr doc, const char *query) {
2133 xmlXPathContextPtr ctxt;
2134 xmlXPathObjectPtr res;
2136 ctxt = xmlXPathNewContext(doc);
2138 fprintf(stderr, "Out of memory for XPath\n");
2139 progresult = XMLLINT_ERR_MEM;
2142 ctxt->node = xmlDocGetRootElement(doc);
2143 res = xmlXPathEval(BAD_CAST query, ctxt);
2144 xmlXPathFreeContext(ctxt);
2147 fprintf(stderr, "XPath evaluation failure\n");
2148 progresult = XMLLINT_ERR_XPATH;
2152 xmlXPathFreeObject(res);
2154 #endif /* LIBXML_XPATH_ENABLED */
2156 /************************************************************************
2158 * Tree Test processing *
2160 ************************************************************************/
2161 static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
2162 xmlDocPtr doc = NULL;
2163 #ifdef LIBXML_TREE_ENABLED
2165 #endif /* LIBXML_TREE_ENABLED */
2167 if ((timing) && (!repeat))
2171 #ifdef LIBXML_TREE_ENABLED
2172 if (filename == NULL) {
2176 doc = xmlNewDoc(BAD_CAST "1.0");
2177 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
2178 xmlNodeSetContent(n, BAD_CAST "abc");
2179 xmlDocSetRootElement(doc, n);
2182 #endif /* LIBXML_TREE_ENABLED */
2183 #ifdef LIBXML_HTML_ENABLED
2184 #ifdef LIBXML_PUSH_ENABLED
2185 else if ((html) && (push)) {
2188 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2189 f = fopen(filename, "rb");
2191 f = fopen(filename, "r");
2196 htmlParserCtxtPtr ctxt;
2200 res = fread(chars, 1, 4, f);
2202 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
2203 chars, res, filename, XML_CHAR_ENCODING_NONE);
2204 while ((res = fread(chars, 1, size, f)) > 0) {
2205 htmlParseChunk(ctxt, chars, res, 0);
2207 htmlParseChunk(ctxt, chars, 0, 1);
2209 htmlFreeParserCtxt(ctxt);
2214 #endif /* LIBXML_PUSH_ENABLED */
2215 #ifdef HAVE_SYS_MMAN_H
2216 else if ((html) && (memory)) {
2220 if (stat(filename, &info) < 0)
2222 if ((fd = open(filename, O_RDONLY)) < 0)
2224 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2225 if (base == (void *) MAP_FAILED)
2228 doc = htmlReadMemory((char *) base, info.st_size, filename,
2231 munmap((char *) base, info.st_size);
2236 doc = htmlReadFile(filename, NULL, options);
2238 #endif /* LIBXML_HTML_ENABLED */
2240 #ifdef LIBXML_PUSH_ENABLED
2242 * build an XML tree from a string;
2247 /* '-' Usually means stdin -<sven@zen.org> */
2248 if ((filename[0] == '-') && (filename[1] == 0)) {
2251 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2252 f = fopen(filename, "rb");
2254 f = fopen(filename, "r");
2259 int res, size = 1024;
2261 xmlParserCtxtPtr ctxt;
2263 /* if (repeat) size = 1024; */
2264 res = fread(chars, 1, 4, f);
2266 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2267 chars, res, filename);
2268 xmlCtxtUseOptions(ctxt, options);
2269 while ((res = fread(chars, 1, size, f)) > 0) {
2270 xmlParseChunk(ctxt, chars, res, 0);
2272 xmlParseChunk(ctxt, chars, 0, 1);
2274 ret = ctxt->wellFormed;
2275 xmlFreeParserCtxt(ctxt);
2285 #endif /* LIBXML_PUSH_ENABLED */
2287 if ((filename[0] == '-') && (filename[1] == 0)) {
2288 doc = xmlReadFd(0, NULL, NULL, options);
2292 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2293 f = fopen(filename, "rb");
2295 f = fopen(filename, "r");
2299 doc = xmlReadIO((xmlInputReadCallback) myRead,
2300 (xmlInputCloseCallback) myClose, f,
2301 filename, NULL, options);
2303 doc = xmlCtxtReadIO(rectxt,
2304 (xmlInputReadCallback) myRead,
2305 (xmlInputCloseCallback) myClose, f,
2306 filename, NULL, options);
2310 } else if (htmlout) {
2311 xmlParserCtxtPtr ctxt;
2314 ctxt = xmlNewParserCtxt();
2320 ctxt->sax->error = xmlHTMLError;
2321 ctxt->sax->warning = xmlHTMLWarning;
2322 ctxt->vctxt.error = xmlHTMLValidityError;
2323 ctxt->vctxt.warning = xmlHTMLValidityWarning;
2325 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2328 xmlFreeParserCtxt(ctxt);
2330 #ifdef HAVE_SYS_MMAN_H
2331 } else if (memory) {
2335 if (stat(filename, &info) < 0)
2337 if ((fd = open(filename, O_RDONLY)) < 0)
2339 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2340 if (base == (void *) MAP_FAILED)
2344 doc = xmlReadMemory((char *) base, info.st_size,
2345 filename, NULL, options);
2347 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2348 filename, NULL, options);
2350 munmap((char *) base, info.st_size);
2353 #ifdef LIBXML_VALID_ENABLED
2355 xmlParserCtxtPtr ctxt = NULL;
2358 ctxt = xmlNewParserCtxt();
2364 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2366 if (ctxt->valid == 0)
2367 progresult = XMLLINT_ERR_RDFILE;
2369 xmlFreeParserCtxt(ctxt);
2371 #endif /* LIBXML_VALID_ENABLED */
2374 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
2376 #ifdef LIBXML_SAX1_ENABLED
2378 doc = xmlParseFile(filename);
2380 #endif /* LIBXML_SAX1_ENABLED */
2381 doc = xmlReadFile(filename, NULL, options);
2387 * If we don't have a document we might as well give up. Do we
2388 * want an error message here? <sven@zen.org> */
2390 progresult = XMLLINT_ERR_UNCLASS;
2394 if ((timing) && (!repeat)) {
2395 endTimer("Parsing");
2399 * Remove DOCTYPE nodes
2404 dtd = xmlGetIntSubset(doc);
2406 xmlUnlinkNode((xmlNodePtr)dtd);
2411 #ifdef LIBXML_XINCLUDE_ENABLED
2413 if ((timing) && (!repeat)) {
2416 if (xmlXIncludeProcessFlags(doc, options) < 0)
2417 progresult = XMLLINT_ERR_UNCLASS;
2418 if ((timing) && (!repeat)) {
2419 endTimer("Xinclude processing");
2424 #ifdef LIBXML_XPATH_ENABLED
2425 if (xpathquery != NULL) {
2426 doXPathQuery(doc, xpathquery);
2430 #ifdef LIBXML_DEBUG_ENABLED
2431 #ifdef LIBXML_XPATH_ENABLED
2436 xmlXPathOrderDocElems(doc);
2437 xmlShell(doc, filename, xmlShellReadline, stdout);
2442 #ifdef LIBXML_TREE_ENABLED
2444 * test intermediate copy if needed.
2451 doc = xmlCopyDoc(doc, 1);
2453 endTimer("Copying");
2460 endTimer("Freeing original");
2463 #endif /* LIBXML_TREE_ENABLED */
2465 #ifdef LIBXML_VALID_ENABLED
2466 if ((insert) && (!html)) {
2467 const xmlChar* list[256];
2471 if (doc->children != NULL) {
2472 node = doc->children;
2473 while ((node != NULL) && (node->last == NULL)) node = node->next;
2475 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2477 fprintf(stderr, "could not get valid list of elements\n");
2478 } else if (nb == 0) {
2479 fprintf(stderr, "No element can be inserted under root\n");
2481 fprintf(stderr, "%d element types can be inserted under root:\n",
2483 for (i = 0;i < nb;i++) {
2484 fprintf(stderr, "%s\n", (char *) list[i]);
2490 #endif /* LIBXML_VALID_ENABLED */
2491 #ifdef LIBXML_READER_ENABLED
2495 #endif /* LIBXML_READER_ENABLED */
2496 #ifdef LIBXML_OUTPUT_ENABLED
2503 #ifdef LIBXML_DEBUG_ENABLED
2506 if ((timing) && (!repeat)) {
2509 #ifdef LIBXML_HTML_ENABLED
2510 if ((html) && (!xmlout)) {
2512 htmlSaveFile(output ? output : "-", doc);
2514 else if (encoding != NULL) {
2516 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2519 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2522 else if (format == 1) {
2523 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2530 out = fopen(output,"wb");
2533 if (htmlDocDump(out, doc) < 0)
2534 progresult = XMLLINT_ERR_OUT;
2539 fprintf(stderr, "failed to open %s\n", output);
2540 progresult = XMLLINT_ERR_OUT;
2543 if ((timing) && (!repeat)) {
2548 #ifdef LIBXML_C14N_ENABLED
2550 xmlChar *result = NULL;
2553 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_0, NULL, 1, &result);
2555 if (write(1, result, size) == -1) {
2556 fprintf(stderr, "Can't write data\n");
2560 fprintf(stderr, "Failed to canonicalize\n");
2561 progresult = XMLLINT_ERR_OUT;
2563 } else if (canonical) {
2564 xmlChar *result = NULL;
2567 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_1_1, NULL, 1, &result);
2569 if (write(1, result, size) == -1) {
2570 fprintf(stderr, "Can't write data\n");
2574 fprintf(stderr, "Failed to canonicalize\n");
2575 progresult = XMLLINT_ERR_OUT;
2578 if (exc_canonical) {
2579 xmlChar *result = NULL;
2582 size = xmlC14NDocDumpMemory(doc, NULL, XML_C14N_EXCLUSIVE_1_0, NULL, 1, &result);
2584 if (write(1, result, size) == -1) {
2585 fprintf(stderr, "Can't write data\n");
2589 fprintf(stderr, "Failed to canonicalize\n");
2590 progresult = XMLLINT_ERR_OUT;
2594 #ifdef HAVE_SYS_MMAN_H
2599 if (encoding != NULL) {
2601 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
2603 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2607 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2609 xmlDocDumpMemory(doc, &result, &len);
2611 if (result == NULL) {
2612 fprintf(stderr, "Failed to save\n");
2613 progresult = XMLLINT_ERR_OUT;
2615 if (write(1, result, len) == -1) {
2616 fprintf(stderr, "Can't write data\n");
2622 #endif /* HAVE_SYS_MMAN_H */
2624 xmlSaveFile(output ? output : "-", doc);
2625 } else if (oldout) {
2626 if (encoding != NULL) {
2628 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2632 ret = xmlSaveFileEnc(output ? output : "-", doc,
2636 fprintf(stderr, "failed save to %s\n",
2637 output ? output : "-");
2638 progresult = XMLLINT_ERR_OUT;
2640 } else if (format == 1) {
2641 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2643 fprintf(stderr, "failed save to %s\n",
2644 output ? output : "-");
2645 progresult = XMLLINT_ERR_OUT;
2652 out = fopen(output,"wb");
2655 if (xmlDocDump(out, doc) < 0)
2656 progresult = XMLLINT_ERR_OUT;
2661 fprintf(stderr, "failed to open %s\n", output);
2662 progresult = XMLLINT_ERR_OUT;
2666 xmlSaveCtxtPtr ctxt;
2670 saveOpts |= XML_SAVE_FORMAT;
2671 else if (format == 2)
2672 saveOpts |= XML_SAVE_WSNONSIG;
2674 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
2676 saveOpts |= XML_SAVE_AS_XML;
2680 ctxt = xmlSaveToFd(1, encoding, saveOpts);
2682 ctxt = xmlSaveToFilename(output, encoding, saveOpts);
2685 if (xmlSaveDoc(ctxt, doc) < 0) {
2686 fprintf(stderr, "failed save to %s\n",
2687 output ? output : "-");
2688 progresult = XMLLINT_ERR_OUT;
2692 progresult = XMLLINT_ERR_OUT;
2695 if ((timing) && (!repeat)) {
2698 #ifdef LIBXML_DEBUG_ENABLED
2704 out = fopen(output,"wb");
2707 xmlDebugDumpDocument(out, doc);
2712 fprintf(stderr, "failed to open %s\n", output);
2713 progresult = XMLLINT_ERR_OUT;
2718 #endif /* LIBXML_OUTPUT_ENABLED */
2720 #ifdef LIBXML_VALID_ENABLED
2722 * A posteriori validation test
2724 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
2727 if ((timing) && (!repeat)) {
2730 if (dtdvalid != NULL)
2731 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
2733 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
2734 if ((timing) && (!repeat)) {
2735 endTimer("Parsing DTD");
2738 if (dtdvalid != NULL)
2739 xmlGenericError(xmlGenericErrorContext,
2740 "Could not parse DTD %s\n", dtdvalid);
2742 xmlGenericError(xmlGenericErrorContext,
2743 "Could not parse DTD %s\n", dtdvalidfpi);
2744 progresult = XMLLINT_ERR_DTD;
2746 xmlValidCtxtPtr cvp;
2748 if ((cvp = xmlNewValidCtxt()) == NULL) {
2749 xmlGenericError(xmlGenericErrorContext,
2750 "Couldn't allocate validation context\n");
2753 cvp->userData = (void *) stderr;
2754 cvp->error = (xmlValidityErrorFunc) fprintf;
2755 cvp->warning = (xmlValidityWarningFunc) fprintf;
2757 if ((timing) && (!repeat)) {
2760 if (!xmlValidateDtd(cvp, doc, dtd)) {
2761 if (dtdvalid != NULL)
2762 xmlGenericError(xmlGenericErrorContext,
2763 "Document %s does not validate against %s\n",
2764 filename, dtdvalid);
2766 xmlGenericError(xmlGenericErrorContext,
2767 "Document %s does not validate against %s\n",
2768 filename, dtdvalidfpi);
2769 progresult = XMLLINT_ERR_VALID;
2771 if ((timing) && (!repeat)) {
2772 endTimer("Validating against DTD");
2774 xmlFreeValidCtxt(cvp);
2777 } else if (postvalid) {
2778 xmlValidCtxtPtr cvp;
2780 if ((cvp = xmlNewValidCtxt()) == NULL) {
2781 xmlGenericError(xmlGenericErrorContext,
2782 "Couldn't allocate validation context\n");
2786 if ((timing) && (!repeat)) {
2789 cvp->userData = (void *) stderr;
2790 cvp->error = (xmlValidityErrorFunc) fprintf;
2791 cvp->warning = (xmlValidityWarningFunc) fprintf;
2792 if (!xmlValidateDocument(cvp, doc)) {
2793 xmlGenericError(xmlGenericErrorContext,
2794 "Document %s does not validate\n", filename);
2795 progresult = XMLLINT_ERR_VALID;
2797 if ((timing) && (!repeat)) {
2798 endTimer("Validating");
2800 xmlFreeValidCtxt(cvp);
2802 #endif /* LIBXML_VALID_ENABLED */
2803 #ifdef LIBXML_SCHEMATRON_ENABLED
2804 if (wxschematron != NULL) {
2805 xmlSchematronValidCtxtPtr ctxt;
2809 if ((timing) && (!repeat)) {
2814 flag = XML_SCHEMATRON_OUT_XML;
2816 flag = XML_SCHEMATRON_OUT_TEXT;
2818 flag |= XML_SCHEMATRON_OUT_QUIET;
2819 ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
2821 xmlSchematronSetValidErrors(ctxt,
2822 (xmlSchematronValidityErrorFunc) fprintf,
2823 (xmlSchematronValidityWarningFunc) fprintf,
2826 ret = xmlSchematronValidateDoc(ctxt, doc);
2828 fprintf(stderr, "%s validates\n", filename);
2829 } else if (ret > 0) {
2830 fprintf(stderr, "%s fails to validate\n", filename);
2831 progresult = XMLLINT_ERR_VALID;
2833 fprintf(stderr, "%s validation generated an internal error\n",
2835 progresult = XMLLINT_ERR_VALID;
2837 xmlSchematronFreeValidCtxt(ctxt);
2838 if ((timing) && (!repeat)) {
2839 endTimer("Validating");
2843 #ifdef LIBXML_SCHEMAS_ENABLED
2844 if (relaxngschemas != NULL) {
2845 xmlRelaxNGValidCtxtPtr ctxt;
2848 if ((timing) && (!repeat)) {
2852 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2853 xmlRelaxNGSetValidErrors(ctxt,
2854 (xmlRelaxNGValidityErrorFunc) fprintf,
2855 (xmlRelaxNGValidityWarningFunc) fprintf,
2857 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2859 fprintf(stderr, "%s validates\n", filename);
2860 } else if (ret > 0) {
2861 fprintf(stderr, "%s fails to validate\n", filename);
2862 progresult = XMLLINT_ERR_VALID;
2864 fprintf(stderr, "%s validation generated an internal error\n",
2866 progresult = XMLLINT_ERR_VALID;
2868 xmlRelaxNGFreeValidCtxt(ctxt);
2869 if ((timing) && (!repeat)) {
2870 endTimer("Validating");
2872 } else if (wxschemas != NULL) {
2873 xmlSchemaValidCtxtPtr ctxt;
2876 if ((timing) && (!repeat)) {
2880 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2881 xmlSchemaSetValidErrors(ctxt,
2882 (xmlSchemaValidityErrorFunc) fprintf,
2883 (xmlSchemaValidityWarningFunc) fprintf,
2885 ret = xmlSchemaValidateDoc(ctxt, doc);
2887 fprintf(stderr, "%s validates\n", filename);
2888 } else if (ret > 0) {
2889 fprintf(stderr, "%s fails to validate\n", filename);
2890 progresult = XMLLINT_ERR_VALID;
2892 fprintf(stderr, "%s validation generated an internal error\n",
2894 progresult = XMLLINT_ERR_VALID;
2896 xmlSchemaFreeValidCtxt(ctxt);
2897 if ((timing) && (!repeat)) {
2898 endTimer("Validating");
2903 #ifdef LIBXML_DEBUG_ENABLED
2904 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
2905 if ((debugent) && (!html))
2906 xmlDebugDumpEntities(stderr, doc);
2913 if ((timing) && (!repeat)) {
2917 if ((timing) && (!repeat)) {
2918 endTimer("Freeing");
2922 /************************************************************************
2926 ************************************************************************/
2928 static void showVersion(const char *name) {
2929 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2930 fprintf(stderr, " compiled with: ");
2931 if (xmlHasFeature(XML_WITH_THREAD)) fprintf(stderr, "Threads ");
2932 if (xmlHasFeature(XML_WITH_TREE)) fprintf(stderr, "Tree ");
2933 if (xmlHasFeature(XML_WITH_OUTPUT)) fprintf(stderr, "Output ");
2934 if (xmlHasFeature(XML_WITH_PUSH)) fprintf(stderr, "Push ");
2935 if (xmlHasFeature(XML_WITH_READER)) fprintf(stderr, "Reader ");
2936 if (xmlHasFeature(XML_WITH_PATTERN)) fprintf(stderr, "Patterns ");
2937 if (xmlHasFeature(XML_WITH_WRITER)) fprintf(stderr, "Writer ");
2938 if (xmlHasFeature(XML_WITH_SAX1)) fprintf(stderr, "SAXv1 ");
2939 if (xmlHasFeature(XML_WITH_FTP)) fprintf(stderr, "FTP ");
2940 if (xmlHasFeature(XML_WITH_HTTP)) fprintf(stderr, "HTTP ");
2941 if (xmlHasFeature(XML_WITH_VALID)) fprintf(stderr, "DTDValid ");
2942 if (xmlHasFeature(XML_WITH_HTML)) fprintf(stderr, "HTML ");
2943 if (xmlHasFeature(XML_WITH_LEGACY)) fprintf(stderr, "Legacy ");
2944 if (xmlHasFeature(XML_WITH_C14N)) fprintf(stderr, "C14N ");
2945 if (xmlHasFeature(XML_WITH_CATALOG)) fprintf(stderr, "Catalog ");
2946 if (xmlHasFeature(XML_WITH_XPATH)) fprintf(stderr, "XPath ");
2947 if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
2948 if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
2949 if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
2950 if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
2951 if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
2952 if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
2953 if (xmlHasFeature(XML_WITH_AUTOMATA)) fprintf(stderr, "Automata ");
2954 if (xmlHasFeature(XML_WITH_EXPR)) fprintf(stderr, "Expr ");
2955 if (xmlHasFeature(XML_WITH_SCHEMAS)) fprintf(stderr, "Schemas ");
2956 if (xmlHasFeature(XML_WITH_SCHEMATRON)) fprintf(stderr, "Schematron ");
2957 if (xmlHasFeature(XML_WITH_MODULES)) fprintf(stderr, "Modules ");
2958 if (xmlHasFeature(XML_WITH_DEBUG)) fprintf(stderr, "Debug ");
2959 if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug ");
2960 if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug ");
2961 if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib ");
2962 if (xmlHasFeature(XML_WITH_LZMA)) fprintf(stderr, "Lzma ");
2963 fprintf(stderr, "\n");
2966 static void usage(const char *name) {
2967 printf("Usage : %s [options] XMLfiles ...\n", name);
2968 #ifdef LIBXML_OUTPUT_ENABLED
2969 printf("\tParse the XML files and output the result of the parsing\n");
2971 printf("\tParse the XML files\n");
2972 #endif /* LIBXML_OUTPUT_ENABLED */
2973 printf("\t--version : display the version of the XML library used\n");
2974 #ifdef LIBXML_DEBUG_ENABLED
2975 printf("\t--debug : dump a debug tree of the in-memory document\n");
2976 printf("\t--shell : run a navigating shell\n");
2977 printf("\t--debugent : debug the entities defined in the document\n");
2979 #ifdef LIBXML_READER_ENABLED
2980 printf("\t--debug : dump the nodes content when using --stream\n");
2981 #endif /* LIBXML_READER_ENABLED */
2983 #ifdef LIBXML_TREE_ENABLED
2984 printf("\t--copy : used to test the internal copy implementation\n");
2985 #endif /* LIBXML_TREE_ENABLED */
2986 printf("\t--recover : output what was parsable on broken XML documents\n");
2987 printf("\t--huge : remove any internal arbitrary parser limits\n");
2988 printf("\t--noent : substitute entity references by their value\n");
2989 printf("\t--noenc : ignore any encoding specified inside the document\n");
2990 printf("\t--noout : don't output the result tree\n");
2991 printf("\t--path 'paths': provide a set of paths for resources\n");
2992 printf("\t--load-trace : print trace of all external entites loaded\n");
2993 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
2994 printf("\t--nocompact : do not generate compact text nodes\n");
2995 printf("\t--htmlout : output results as HTML\n");
2996 printf("\t--nowrap : do not put HTML doc wrapper\n");
2997 #ifdef LIBXML_VALID_ENABLED
2998 printf("\t--valid : validate the document in addition to std well-formed check\n");
2999 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
3000 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
3001 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
3002 #endif /* LIBXML_VALID_ENABLED */
3003 printf("\t--timing : print some timings\n");
3004 printf("\t--output file or -o file: save to a given file\n");
3005 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
3006 printf("\t--insert : ad-hoc test for valid insertions\n");
3007 #ifdef LIBXML_OUTPUT_ENABLED
3009 printf("\t--compress : turn on gzip compression of output\n");
3011 #endif /* LIBXML_OUTPUT_ENABLED */
3012 #ifdef LIBXML_HTML_ENABLED
3013 printf("\t--html : use the HTML parser\n");
3014 printf("\t--xmlout : force to use the XML serializer when using --html\n");
3015 printf("\t--nodefdtd : do not default HTML doctype\n");
3017 #ifdef LIBXML_PUSH_ENABLED
3018 printf("\t--push : use the push mode of the parser\n");
3019 #endif /* LIBXML_PUSH_ENABLED */
3020 #ifdef HAVE_SYS_MMAN_H
3021 printf("\t--memory : parse from memory\n");
3023 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
3024 printf("\t--nowarning : do not emit warnings from parser/validator\n");
3025 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
3026 printf("\t--nocdata : replace cdata section with text nodes\n");
3027 #ifdef LIBXML_OUTPUT_ENABLED
3028 printf("\t--format : reformat/reindent the input\n");
3029 printf("\t--encode encoding : output in the given encoding\n");
3030 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
3031 printf("\t--pretty STYLE : pretty-print in a particular style\n");
3032 printf("\t 0 Do not pretty print\n");
3033 printf("\t 1 Format the XML content, as --format\n");
3034 printf("\t 2 Add whitespace inside tags, preserving content\n");
3035 #endif /* LIBXML_OUTPUT_ENABLED */
3036 printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
3037 printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
3038 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
3039 #ifdef LIBXML_C14N_ENABLED
3040 #endif /* LIBXML_C14N_ENABLED */
3041 printf("\t--nsclean : remove redundant namespace declarations\n");
3042 printf("\t--testIO : test user I/O support\n");
3043 #ifdef LIBXML_CATALOG_ENABLED
3044 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
3045 printf("\t otherwise XML Catalogs starting from \n");
3046 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
3047 printf("\t--nocatalogs: deactivate all catalogs\n");
3049 printf("\t--auto : generate a small doc on the fly\n");
3050 #ifdef LIBXML_XINCLUDE_ENABLED
3051 printf("\t--xinclude : do XInclude processing\n");
3052 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
3053 printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
3055 printf("\t--loaddtd : fetch external DTD\n");
3056 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
3057 #ifdef LIBXML_READER_ENABLED
3058 printf("\t--stream : use the streaming interface to process very large files\n");
3059 printf("\t--walker : create a reader and walk though the resulting doc\n");
3060 #endif /* LIBXML_READER_ENABLED */
3061 #ifdef LIBXML_PATTERN_ENABLED
3062 printf("\t--pattern pattern_value : test the pattern support\n");
3064 printf("\t--chkregister : verify the node registration code\n");
3065 #ifdef LIBXML_SCHEMAS_ENABLED
3066 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
3067 printf("\t--schema schema : do validation against the WXS schema\n");
3069 #ifdef LIBXML_SCHEMATRON_ENABLED
3070 printf("\t--schematron schema : do validation against a schematron\n");
3072 #ifdef LIBXML_SAX1_ENABLED
3073 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
3075 printf("\t--sax: do not build a tree but work just at the SAX level\n");
3076 printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
3077 #ifdef LIBXML_XPATH_ENABLED
3078 printf("\t--xpath expr: evaluate the XPath expression, inply --noout\n");
3081 printf("\nLibxml project home page: http://xmlsoft.org/\n");
3082 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
3085 static void registerNode(xmlNodePtr node)
3087 node->_private = malloc(sizeof(long));
3088 *(long*)node->_private = (long) 0x81726354;
3092 static void deregisterNode(xmlNodePtr node)
3094 assert(node->_private != NULL);
3095 assert(*(long*)node->_private == (long) 0x81726354);
3096 free(node->_private);
3101 main(int argc, char **argv) {
3112 for (i = 1; i < argc ; i++) {
3113 if (!strcmp(argv[i], "-"))
3116 if (argv[i][0] != '-')
3118 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
3121 #ifdef LIBXML_DEBUG_ENABLED
3122 if ((!strcmp(argv[i], "-shell")) ||
3123 (!strcmp(argv[i], "--shell"))) {
3128 #ifdef LIBXML_TREE_ENABLED
3129 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
3132 #endif /* LIBXML_TREE_ENABLED */
3133 if ((!strcmp(argv[i], "-recover")) ||
3134 (!strcmp(argv[i], "--recover"))) {
3136 options |= XML_PARSE_RECOVER;
3137 } else if ((!strcmp(argv[i], "-huge")) ||
3138 (!strcmp(argv[i], "--huge"))) {
3139 options |= XML_PARSE_HUGE;
3140 } else if ((!strcmp(argv[i], "-noent")) ||
3141 (!strcmp(argv[i], "--noent"))) {
3143 options |= XML_PARSE_NOENT;
3144 } else if ((!strcmp(argv[i], "-noenc")) ||
3145 (!strcmp(argv[i], "--noenc"))) {
3147 options |= XML_PARSE_IGNORE_ENC;
3148 } else if ((!strcmp(argv[i], "-nsclean")) ||
3149 (!strcmp(argv[i], "--nsclean"))) {
3150 options |= XML_PARSE_NSCLEAN;
3151 } else if ((!strcmp(argv[i], "-nocdata")) ||
3152 (!strcmp(argv[i], "--nocdata"))) {
3153 options |= XML_PARSE_NOCDATA;
3154 } else if ((!strcmp(argv[i], "-nodict")) ||
3155 (!strcmp(argv[i], "--nodict"))) {
3156 options |= XML_PARSE_NODICT;
3157 } else if ((!strcmp(argv[i], "-version")) ||
3158 (!strcmp(argv[i], "--version"))) {
3159 showVersion(argv[0]);
3161 } else if ((!strcmp(argv[i], "-noout")) ||
3162 (!strcmp(argv[i], "--noout")))
3164 #ifdef LIBXML_OUTPUT_ENABLED
3165 else if ((!strcmp(argv[i], "-o")) ||
3166 (!strcmp(argv[i], "-output")) ||
3167 (!strcmp(argv[i], "--output"))) {
3171 #endif /* LIBXML_OUTPUT_ENABLED */
3172 else if ((!strcmp(argv[i], "-htmlout")) ||
3173 (!strcmp(argv[i], "--htmlout")))
3175 else if ((!strcmp(argv[i], "-nowrap")) ||
3176 (!strcmp(argv[i], "--nowrap")))
3178 #ifdef LIBXML_HTML_ENABLED
3179 else if ((!strcmp(argv[i], "-html")) ||
3180 (!strcmp(argv[i], "--html"))) {
3183 else if ((!strcmp(argv[i], "-xmlout")) ||
3184 (!strcmp(argv[i], "--xmlout"))) {
3186 } else if ((!strcmp(argv[i], "-nodefdtd")) ||
3187 (!strcmp(argv[i], "--nodefdtd"))) {
3189 options |= HTML_PARSE_NODEFDTD;
3191 #endif /* LIBXML_HTML_ENABLED */
3192 else if ((!strcmp(argv[i], "-loaddtd")) ||
3193 (!strcmp(argv[i], "--loaddtd"))) {
3195 options |= XML_PARSE_DTDLOAD;
3196 } else if ((!strcmp(argv[i], "-dtdattr")) ||
3197 (!strcmp(argv[i], "--dtdattr"))) {
3200 options |= XML_PARSE_DTDATTR;
3202 #ifdef LIBXML_VALID_ENABLED
3203 else if ((!strcmp(argv[i], "-valid")) ||
3204 (!strcmp(argv[i], "--valid"))) {
3206 options |= XML_PARSE_DTDVALID;
3207 } else if ((!strcmp(argv[i], "-postvalid")) ||
3208 (!strcmp(argv[i], "--postvalid"))) {
3211 options |= XML_PARSE_DTDLOAD;
3212 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
3213 (!strcmp(argv[i], "--dtdvalid"))) {
3217 options |= XML_PARSE_DTDLOAD;
3218 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3219 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3221 dtdvalidfpi = argv[i];
3223 options |= XML_PARSE_DTDLOAD;
3225 #endif /* LIBXML_VALID_ENABLED */
3226 else if ((!strcmp(argv[i], "-dropdtd")) ||
3227 (!strcmp(argv[i], "--dropdtd")))
3229 else if ((!strcmp(argv[i], "-insert")) ||
3230 (!strcmp(argv[i], "--insert")))
3232 else if ((!strcmp(argv[i], "-timing")) ||
3233 (!strcmp(argv[i], "--timing")))
3235 else if ((!strcmp(argv[i], "-auto")) ||
3236 (!strcmp(argv[i], "--auto")))
3238 else if ((!strcmp(argv[i], "-repeat")) ||
3239 (!strcmp(argv[i], "--repeat"))) {
3245 #ifdef LIBXML_PUSH_ENABLED
3246 else if ((!strcmp(argv[i], "-push")) ||
3247 (!strcmp(argv[i], "--push")))
3249 #endif /* LIBXML_PUSH_ENABLED */
3250 #ifdef HAVE_SYS_MMAN_H
3251 else if ((!strcmp(argv[i], "-memory")) ||
3252 (!strcmp(argv[i], "--memory")))
3255 else if ((!strcmp(argv[i], "-testIO")) ||
3256 (!strcmp(argv[i], "--testIO")))
3258 #ifdef LIBXML_XINCLUDE_ENABLED
3259 else if ((!strcmp(argv[i], "-xinclude")) ||
3260 (!strcmp(argv[i], "--xinclude"))) {
3262 options |= XML_PARSE_XINCLUDE;
3264 else if ((!strcmp(argv[i], "-noxincludenode")) ||
3265 (!strcmp(argv[i], "--noxincludenode"))) {
3267 options |= XML_PARSE_XINCLUDE;
3268 options |= XML_PARSE_NOXINCNODE;
3270 else if ((!strcmp(argv[i], "-nofixup-base-uris")) ||
3271 (!strcmp(argv[i], "--nofixup-base-uris"))) {
3273 options |= XML_PARSE_XINCLUDE;
3274 options |= XML_PARSE_NOBASEFIX;
3277 #ifdef LIBXML_OUTPUT_ENABLED
3279 else if ((!strcmp(argv[i], "-compress")) ||
3280 (!strcmp(argv[i], "--compress"))) {
3282 xmlSetCompressMode(9);
3285 #endif /* LIBXML_OUTPUT_ENABLED */
3286 else if ((!strcmp(argv[i], "-nowarning")) ||
3287 (!strcmp(argv[i], "--nowarning"))) {
3288 xmlGetWarningsDefaultValue = 0;
3289 xmlPedanticParserDefault(0);
3290 options |= XML_PARSE_NOWARNING;
3292 else if ((!strcmp(argv[i], "-pedantic")) ||
3293 (!strcmp(argv[i], "--pedantic"))) {
3294 xmlGetWarningsDefaultValue = 1;
3295 xmlPedanticParserDefault(1);
3296 options |= XML_PARSE_PEDANTIC;
3298 #ifdef LIBXML_DEBUG_ENABLED
3299 else if ((!strcmp(argv[i], "-debugent")) ||
3300 (!strcmp(argv[i], "--debugent"))) {
3302 xmlParserDebugEntities = 1;
3305 #ifdef LIBXML_C14N_ENABLED
3306 else if ((!strcmp(argv[i], "-c14n")) ||
3307 (!strcmp(argv[i], "--c14n"))) {
3309 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3311 else if ((!strcmp(argv[i], "-c14n11")) ||
3312 (!strcmp(argv[i], "--c14n11"))) {
3314 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3316 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3317 (!strcmp(argv[i], "--exc-c14n"))) {
3319 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3322 #ifdef LIBXML_CATALOG_ENABLED
3323 else if ((!strcmp(argv[i], "-catalogs")) ||
3324 (!strcmp(argv[i], "--catalogs"))) {
3326 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3327 (!strcmp(argv[i], "--nocatalogs"))) {
3331 else if ((!strcmp(argv[i], "-encode")) ||
3332 (!strcmp(argv[i], "--encode"))) {
3336 * OK it's for testing purposes
3338 xmlAddEncodingAlias("UTF-8", "DVEnc");
3340 else if ((!strcmp(argv[i], "-noblanks")) ||
3341 (!strcmp(argv[i], "--noblanks"))) {
3343 xmlKeepBlanksDefault(0);
3345 else if ((!strcmp(argv[i], "-maxmem")) ||
3346 (!strcmp(argv[i], "--maxmem"))) {
3348 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3349 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3355 else if ((!strcmp(argv[i], "-format")) ||
3356 (!strcmp(argv[i], "--format"))) {
3358 #ifdef LIBXML_OUTPUT_ENABLED
3360 #endif /* LIBXML_OUTPUT_ENABLED */
3361 xmlKeepBlanksDefault(0);
3363 else if ((!strcmp(argv[i], "-pretty")) ||
3364 (!strcmp(argv[i], "--pretty"))) {
3366 #ifdef LIBXML_OUTPUT_ENABLED
3367 format = atoi(argv[i]);
3370 xmlKeepBlanksDefault(0);
3372 #endif /* LIBXML_OUTPUT_ENABLED */
3374 #ifdef LIBXML_READER_ENABLED
3375 else if ((!strcmp(argv[i], "-stream")) ||
3376 (!strcmp(argv[i], "--stream"))) {
3379 else if ((!strcmp(argv[i], "-walker")) ||
3380 (!strcmp(argv[i], "--walker"))) {
3384 #endif /* LIBXML_READER_ENABLED */
3385 #ifdef LIBXML_SAX1_ENABLED
3386 else if ((!strcmp(argv[i], "-sax1")) ||
3387 (!strcmp(argv[i], "--sax1"))) {
3389 options |= XML_PARSE_SAX1;
3391 #endif /* LIBXML_SAX1_ENABLED */
3392 else if ((!strcmp(argv[i], "-sax")) ||
3393 (!strcmp(argv[i], "--sax"))) {
3396 else if ((!strcmp(argv[i], "-chkregister")) ||
3397 (!strcmp(argv[i], "--chkregister"))) {
3399 #ifdef LIBXML_SCHEMAS_ENABLED
3400 } else if ((!strcmp(argv[i], "-relaxng")) ||
3401 (!strcmp(argv[i], "--relaxng"))) {
3405 options |= XML_PARSE_NOENT;
3406 } else if ((!strcmp(argv[i], "-schema")) ||
3407 (!strcmp(argv[i], "--schema"))) {
3412 #ifdef LIBXML_SCHEMATRON_ENABLED
3413 } else if ((!strcmp(argv[i], "-schematron")) ||
3414 (!strcmp(argv[i], "--schematron"))) {
3416 schematron = argv[i];
3419 } else if ((!strcmp(argv[i], "-nonet")) ||
3420 (!strcmp(argv[i], "--nonet"))) {
3421 options |= XML_PARSE_NONET;
3422 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
3423 } else if ((!strcmp(argv[i], "-nocompact")) ||
3424 (!strcmp(argv[i], "--nocompact"))) {
3425 options &= ~XML_PARSE_COMPACT;
3426 } else if ((!strcmp(argv[i], "-load-trace")) ||
3427 (!strcmp(argv[i], "--load-trace"))) {
3429 } else if ((!strcmp(argv[i], "-path")) ||
3430 (!strcmp(argv[i], "--path"))) {
3432 parsePath(BAD_CAST argv[i]);
3433 #ifdef LIBXML_PATTERN_ENABLED
3434 } else if ((!strcmp(argv[i], "-pattern")) ||
3435 (!strcmp(argv[i], "--pattern"))) {
3439 #ifdef LIBXML_XPATH_ENABLED
3440 } else if ((!strcmp(argv[i], "-xpath")) ||
3441 (!strcmp(argv[i], "--xpath"))) {
3444 xpathquery = argv[i];
3446 } else if ((!strcmp(argv[i], "-oldxml10")) ||
3447 (!strcmp(argv[i], "--oldxml10"))) {
3449 options |= XML_PARSE_OLD10;
3451 fprintf(stderr, "Unknown option %s\n", argv[i]);
3457 #ifdef LIBXML_CATALOG_ENABLED
3458 if (nocatalogs == 0) {
3462 catal = getenv("SGML_CATALOG_FILES");
3463 if (catal != NULL) {
3464 xmlLoadCatalogs(catal);
3466 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3472 #ifdef LIBXML_SAX1_ENABLED
3474 xmlSAXDefaultVersion(1);
3476 xmlSAXDefaultVersion(2);
3477 #endif /* LIBXML_SAX1_ENABLED */
3480 xmlRegisterNodeDefault(registerNode);
3481 xmlDeregisterNodeDefault(deregisterNode);
3484 indent = getenv("XMLLINT_INDENT");
3485 if(indent != NULL) {
3486 xmlTreeIndentString = indent;
3490 defaultEntityLoader = xmlGetExternalEntityLoader();
3491 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3493 xmlLineNumbersDefault(1);
3495 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3497 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
3498 if (noent != 0) xmlSubstituteEntitiesDefault(1);
3499 #ifdef LIBXML_VALID_ENABLED
3500 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
3501 #endif /* LIBXML_VALID_ENABLED */
3502 if ((htmlout) && (!nowrap)) {
3503 xmlGenericError(xmlGenericErrorContext,
3504 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
3505 xmlGenericError(xmlGenericErrorContext,
3506 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3507 xmlGenericError(xmlGenericErrorContext,
3508 "<html><head><title>%s output</title></head>\n",
3510 xmlGenericError(xmlGenericErrorContext,
3511 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3515 #ifdef LIBXML_SCHEMATRON_ENABLED
3516 if ((schematron != NULL) && (sax == 0)
3517 #ifdef LIBXML_READER_ENABLED
3519 #endif /* LIBXML_READER_ENABLED */
3521 xmlSchematronParserCtxtPtr ctxt;
3523 /* forces loading the DTDs */
3524 xmlLoadExtDtdDefaultValue |= 1;
3525 options |= XML_PARSE_DTDLOAD;
3529 ctxt = xmlSchematronNewParserCtxt(schematron);
3531 xmlSchematronSetParserErrors(ctxt,
3532 (xmlSchematronValidityErrorFunc) fprintf,
3533 (xmlSchematronValidityWarningFunc) fprintf,
3536 wxschematron = xmlSchematronParse(ctxt);
3537 if (wxschematron == NULL) {
3538 xmlGenericError(xmlGenericErrorContext,
3539 "Schematron schema %s failed to compile\n", schematron);
3540 progresult = XMLLINT_ERR_SCHEMACOMP;
3543 xmlSchematronFreeParserCtxt(ctxt);
3545 endTimer("Compiling the schemas");
3549 #ifdef LIBXML_SCHEMAS_ENABLED
3550 if ((relaxng != NULL) && (sax == 0)
3551 #ifdef LIBXML_READER_ENABLED
3553 #endif /* LIBXML_READER_ENABLED */
3555 xmlRelaxNGParserCtxtPtr ctxt;
3557 /* forces loading the DTDs */
3558 xmlLoadExtDtdDefaultValue |= 1;
3559 options |= XML_PARSE_DTDLOAD;
3563 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3564 xmlRelaxNGSetParserErrors(ctxt,
3565 (xmlRelaxNGValidityErrorFunc) fprintf,
3566 (xmlRelaxNGValidityWarningFunc) fprintf,
3568 relaxngschemas = xmlRelaxNGParse(ctxt);
3569 if (relaxngschemas == NULL) {
3570 xmlGenericError(xmlGenericErrorContext,
3571 "Relax-NG schema %s failed to compile\n", relaxng);
3572 progresult = XMLLINT_ERR_SCHEMACOMP;
3575 xmlRelaxNGFreeParserCtxt(ctxt);
3577 endTimer("Compiling the schemas");
3579 } else if ((schema != NULL)
3580 #ifdef LIBXML_READER_ENABLED
3584 xmlSchemaParserCtxtPtr ctxt;
3589 ctxt = xmlSchemaNewParserCtxt(schema);
3590 xmlSchemaSetParserErrors(ctxt,
3591 (xmlSchemaValidityErrorFunc) fprintf,
3592 (xmlSchemaValidityWarningFunc) fprintf,
3594 wxschemas = xmlSchemaParse(ctxt);
3595 if (wxschemas == NULL) {
3596 xmlGenericError(xmlGenericErrorContext,
3597 "WXS schema %s failed to compile\n", schema);
3598 progresult = XMLLINT_ERR_SCHEMACOMP;
3601 xmlSchemaFreeParserCtxt(ctxt);
3603 endTimer("Compiling the schemas");
3606 #endif /* LIBXML_SCHEMAS_ENABLED */
3607 #ifdef LIBXML_PATTERN_ENABLED
3608 if ((pattern != NULL)
3609 #ifdef LIBXML_READER_ENABLED
3613 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
3614 if (patternc == NULL) {
3615 xmlGenericError(xmlGenericErrorContext,
3616 "Pattern %s failed to compile\n", pattern);
3617 progresult = XMLLINT_ERR_SCHEMAPAT;
3621 #endif /* LIBXML_PATTERN_ENABLED */
3622 for (i = 1; i < argc ; i++) {
3623 if ((!strcmp(argv[i], "-encode")) ||
3624 (!strcmp(argv[i], "--encode"))) {
3627 } else if ((!strcmp(argv[i], "-o")) ||
3628 (!strcmp(argv[i], "-output")) ||
3629 (!strcmp(argv[i], "--output"))) {
3633 #ifdef LIBXML_VALID_ENABLED
3634 if ((!strcmp(argv[i], "-dtdvalid")) ||
3635 (!strcmp(argv[i], "--dtdvalid"))) {
3639 if ((!strcmp(argv[i], "-path")) ||
3640 (!strcmp(argv[i], "--path"))) {
3644 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3645 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3649 #endif /* LIBXML_VALID_ENABLED */
3650 if ((!strcmp(argv[i], "-relaxng")) ||
3651 (!strcmp(argv[i], "--relaxng"))) {
3655 if ((!strcmp(argv[i], "-maxmem")) ||
3656 (!strcmp(argv[i], "--maxmem"))) {
3660 if ((!strcmp(argv[i], "-pretty")) ||
3661 (!strcmp(argv[i], "--pretty"))) {
3665 if ((!strcmp(argv[i], "-schema")) ||
3666 (!strcmp(argv[i], "--schema"))) {
3670 if ((!strcmp(argv[i], "-schematron")) ||
3671 (!strcmp(argv[i], "--schematron"))) {
3675 #ifdef LIBXML_PATTERN_ENABLED
3676 if ((!strcmp(argv[i], "-pattern")) ||
3677 (!strcmp(argv[i], "--pattern"))) {
3682 #ifdef LIBXML_XPATH_ENABLED
3683 if ((!strcmp(argv[i], "-xpath")) ||
3684 (!strcmp(argv[i], "--xpath"))) {
3689 if ((timing) && (repeat))
3691 /* Remember file names. "-" means stdin. <sven@zen.org> */
3692 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
3694 xmlParserCtxtPtr ctxt = NULL;
3696 for (acount = 0;acount < repeat;acount++) {
3697 #ifdef LIBXML_READER_ENABLED
3699 streamFile(argv[i]);
3701 #endif /* LIBXML_READER_ENABLED */
3706 ctxt = xmlNewParserCtxt();
3707 parseAndPrintFile(argv[i], ctxt);
3709 #ifdef LIBXML_READER_ENABLED
3711 #endif /* LIBXML_READER_ENABLED */
3714 xmlFreeParserCtxt(ctxt);
3718 #ifdef LIBXML_READER_ENABLED
3720 streamFile(argv[i]);
3722 #endif /* LIBXML_READER_ENABLED */
3726 parseAndPrintFile(argv[i], NULL);
3729 if ((chkregister) && (nbregister != 0)) {
3730 fprintf(stderr, "Registration count off: %d\n", nbregister);
3731 progresult = XMLLINT_ERR_RDREGIS;
3735 if ((timing) && (repeat)) {
3736 endTimer("%d iterations", repeat);
3741 parseAndPrintFile(NULL, NULL);
3742 if ((htmlout) && (!nowrap)) {
3743 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
3745 if ((files == 0) && (!generate) && (version == 0)) {
3748 #ifdef LIBXML_SCHEMATRON_ENABLED
3749 if (wxschematron != NULL)
3750 xmlSchematronFree(wxschematron);
3752 #ifdef LIBXML_SCHEMAS_ENABLED
3753 if (relaxngschemas != NULL)
3754 xmlRelaxNGFree(relaxngschemas);
3755 if (wxschemas != NULL)
3756 xmlSchemaFree(wxschemas);
3757 xmlRelaxNGCleanupTypes();
3759 #ifdef LIBXML_PATTERN_ENABLED
3760 if (patternc != NULL)
3761 xmlFreePattern(patternc);