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 noblanks
= 0;
134 static int noout
= 0;
135 static int nowrap
= 0;
136 #ifdef LIBXML_OUTPUT_ENABLED
137 static int format
= 0;
138 static const char *output
= NULL
;
139 static int compress
= 0;
140 static int oldout
= 0;
141 #endif /* LIBXML_OUTPUT_ENABLED */
142 #ifdef LIBXML_VALID_ENABLED
143 static int valid
= 0;
144 static int postvalid
= 0;
145 static char * dtdvalid
= NULL
;
146 static char * dtdvalidfpi
= NULL
;
148 #ifdef LIBXML_SCHEMAS_ENABLED
149 static char * relaxng
= NULL
;
150 static xmlRelaxNGPtr relaxngschemas
= NULL
;
151 static char * schema
= NULL
;
152 static xmlSchemaPtr wxschemas
= NULL
;
154 #ifdef LIBXML_SCHEMATRON_ENABLED
155 static char * schematron
= NULL
;
156 static xmlSchematronPtr wxschematron
= NULL
;
158 static int repeat
= 0;
159 static int insert
= 0;
160 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
162 static int xmlout
= 0;
164 static int htmlout
= 0;
165 #ifdef LIBXML_PUSH_ENABLED
167 #endif /* LIBXML_PUSH_ENABLED */
168 #ifdef HAVE_SYS_MMAN_H
169 static int memory
= 0;
171 static int testIO
= 0;
172 static char *encoding
= NULL
;
173 #ifdef LIBXML_XINCLUDE_ENABLED
174 static int xinclude
= 0;
176 static int dtdattrs
= 0;
177 static int loaddtd
= 0;
178 static xmllintReturnCode progresult
= XMLLINT_RETURN_OK
;
179 static int timing
= 0;
180 static int generate
= 0;
181 static int dropdtd
= 0;
182 #ifdef LIBXML_CATALOG_ENABLED
183 static int catalogs
= 0;
184 static int nocatalogs
= 0;
186 #ifdef LIBXML_C14N_ENABLED
187 static int canonical
= 0;
188 static int canonical_11
= 0;
189 static int exc_canonical
= 0;
191 #ifdef LIBXML_READER_ENABLED
192 static int stream
= 0;
193 static int walker
= 0;
194 #endif /* LIBXML_READER_ENABLED */
195 static int chkregister
= 0;
196 static int nbregister
= 0;
197 #ifdef LIBXML_SAX1_ENABLED
199 #endif /* LIBXML_SAX1_ENABLED */
200 #ifdef LIBXML_PATTERN_ENABLED
201 static const char *pattern
= NULL
;
202 static xmlPatternPtr patternc
= NULL
;
203 static xmlStreamCtxtPtr patstream
= NULL
;
205 #ifdef LIBXML_XPATH_ENABLED
206 static const char *xpathquery
= NULL
;
208 static int options
= XML_PARSE_COMPACT
;
210 static int oldxml10
= 0;
212 /************************************************************************
214 * Entity loading control and customization. *
216 ************************************************************************/
219 # define PATH_SEPARATOR ';'
221 # define PATH_SEPARATOR ':'
223 static xmlChar
*paths
[MAX_PATHS
+ 1];
224 static int nbpaths
= 0;
225 static int load_trace
= 0;
228 void parsePath(const xmlChar
*path
) {
234 if (nbpaths
>= MAX_PATHS
) {
235 fprintf(stderr
, "MAX_PATHS reached: too many paths\n");
239 while ((*cur
== ' ') || (*cur
== PATH_SEPARATOR
))
242 while ((*cur
!= 0) && (*cur
!= ' ') && (*cur
!= PATH_SEPARATOR
))
245 paths
[nbpaths
] = xmlStrndup(path
, cur
- path
);
246 if (paths
[nbpaths
] != NULL
)
253 static xmlExternalEntityLoader defaultEntityLoader
= NULL
;
255 static xmlParserInputPtr
256 xmllintExternalEntityLoader(const char *URL
, const char *ID
,
257 xmlParserCtxtPtr ctxt
) {
258 xmlParserInputPtr ret
;
259 warningSAXFunc warning
= NULL
;
260 errorSAXFunc err
= NULL
;
263 const char *lastsegment
= URL
;
264 const char *iter
= URL
;
266 if ((nbpaths
> 0) && (iter
!= NULL
)) {
269 lastsegment
= iter
+ 1;
274 if ((ctxt
!= NULL
) && (ctxt
->sax
!= NULL
)) {
275 warning
= ctxt
->sax
->warning
;
276 err
= ctxt
->sax
->error
;
277 ctxt
->sax
->warning
= NULL
;
278 ctxt
->sax
->error
= NULL
;
281 if (defaultEntityLoader
!= NULL
) {
282 ret
= defaultEntityLoader(URL
, ID
, ctxt
);
285 ctxt
->sax
->warning
= warning
;
287 ctxt
->sax
->error
= err
;
291 "Loaded URL=\"%s\" ID=\"%s\"\n",
292 URL
? URL
: "(null)",
298 for (i
= 0;i
< nbpaths
;i
++) {
301 newURL
= xmlStrdup((const xmlChar
*) paths
[i
]);
302 newURL
= xmlStrcat(newURL
, (const xmlChar
*) "/");
303 newURL
= xmlStrcat(newURL
, (const xmlChar
*) lastsegment
);
304 if (newURL
!= NULL
) {
305 ret
= defaultEntityLoader((const char *)newURL
, ID
, ctxt
);
308 ctxt
->sax
->warning
= warning
;
310 ctxt
->sax
->error
= err
;
314 "Loaded URL=\"%s\" ID=\"%s\"\n",
325 ctxt
->sax
->error
= err
;
326 if (warning
!= NULL
) {
327 ctxt
->sax
->warning
= warning
;
329 warning(ctxt
, "failed to load external entity \"%s\"\n", URL
);
331 warning(ctxt
, "failed to load external entity \"%s\"\n", ID
);
335 /************************************************************************
337 * Memory allocation consumption debugging *
339 ************************************************************************/
344 fprintf(stderr
, "Ran out of memory needs > %d bytes\n", maxmem
);
345 progresult
= XMLLINT_ERR_MEM
;
349 myFreeFunc(void *mem
)
354 myMallocFunc(size_t size
)
358 ret
= xmlMemMalloc(size
);
360 if (xmlMemUsed() > maxmem
) {
369 myReallocFunc(void *mem
, size_t size
)
373 ret
= xmlMemRealloc(mem
, size
);
375 if (xmlMemUsed() > maxmem
) {
384 myStrdupFunc(const char *str
)
388 ret
= xmlMemoryStrdup(str
);
390 if (xmlMemUsed() > maxmem
) {
398 /************************************************************************
400 * Internal timing routines to remove the necessity to have *
401 * unix-specific function calls. *
403 ************************************************************************/
405 #ifndef HAVE_GETTIMEOFDAY
406 #ifdef HAVE_SYS_TIMEB_H
407 #ifdef HAVE_SYS_TIME_H
411 my_gettimeofday(struct timeval
*tvp
, void *tzp
)
413 struct timeb timebuffer
;
417 tvp
->tv_sec
= timebuffer
.time
;
418 tvp
->tv_usec
= timebuffer
.millitm
* 1000L;
422 #define HAVE_GETTIMEOFDAY 1
423 #define gettimeofday my_gettimeofday
425 #endif /* HAVE_FTIME */
426 #endif /* HAVE_SYS_TIME_H */
427 #endif /* HAVE_SYS_TIMEB_H */
428 #endif /* !HAVE_GETTIMEOFDAY */
430 #if defined(HAVE_GETTIMEOFDAY)
431 static struct timeval begin
, end
;
434 * startTimer: call where you want to start timing
439 gettimeofday(&begin
, NULL
);
443 * endTimer: call where you want to stop timing and to print out a
444 * message about the timing performed; format is a printf
448 endTimer(const char *fmt
, ...)
453 gettimeofday(&end
, NULL
);
454 msec
= end
.tv_sec
- begin
.tv_sec
;
456 msec
+= (end
.tv_usec
- begin
.tv_usec
) / 1000;
458 #ifndef HAVE_STDARG_H
459 #error "endTimer required stdarg functions"
462 vfprintf(stderr
, fmt
, ap
);
465 fprintf(stderr
, " took %ld ms\n", msec
);
467 #elif defined(HAVE_TIME_H)
469 * No gettimeofday function, so we have to make do with calling clock.
470 * This is obviously less accurate, but there's little we can do about
473 #ifndef CLOCKS_PER_SEC
474 #define CLOCKS_PER_SEC 100
477 static clock_t begin
, end
;
484 endTimer(const char *fmt
, ...)
490 msec
= ((end
- begin
) * 1000) / CLOCKS_PER_SEC
;
492 #ifndef HAVE_STDARG_H
493 #error "endTimer required stdarg functions"
496 vfprintf(stderr
, fmt
, ap
);
498 fprintf(stderr
, " took %ld ms\n", msec
);
503 * We don't have a gettimeofday or time.h, so we just don't do timing
513 endTimer(char *format
, ...)
516 * We cannot do anything because we don't have a timing function
519 va_start(ap
, format
);
520 vfprintf(stderr
, format
, ap
);
522 fprintf(stderr
, " was not timed\n", msec
);
524 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
530 /************************************************************************
534 ************************************************************************/
535 static char buffer
[50000];
538 xmlHTMLEncodeSend(void) {
541 result
= (char *) xmlEncodeEntitiesReentrant(NULL
, BAD_CAST buffer
);
543 xmlGenericError(xmlGenericErrorContext
, "%s", result
);
550 * xmlHTMLPrintFileInfo:
551 * @input: an xmlParserInputPtr input
553 * Displays the associated file and line informations for the current input
557 xmlHTMLPrintFileInfo(xmlParserInputPtr input
) {
559 xmlGenericError(xmlGenericErrorContext
, "<p>");
561 len
= strlen(buffer
);
563 if (input
->filename
) {
564 snprintf(&buffer
[len
], sizeof(buffer
) - len
, "%s:%d: ", input
->filename
,
567 snprintf(&buffer
[len
], sizeof(buffer
) - len
, "Entity: line %d: ", input
->line
);
574 * xmlHTMLPrintFileContext:
575 * @input: an xmlParserInputPtr input
577 * Displays current context within the input content for error tracking
581 xmlHTMLPrintFileContext(xmlParserInputPtr input
) {
582 const xmlChar
*cur
, *base
;
586 if (input
== NULL
) return;
587 xmlGenericError(xmlGenericErrorContext
, "<pre>\n");
590 while ((cur
> base
) && ((*cur
== '\n') || (*cur
== '\r'))) {
594 while ((n
++ < 80) && (cur
> base
) && (*cur
!= '\n') && (*cur
!= '\r'))
596 if ((*cur
== '\n') || (*cur
== '\r')) cur
++;
599 while ((*cur
!= 0) && (*cur
!= '\n') && (*cur
!= '\r') && (n
< 79)) {
600 len
= strlen(buffer
);
601 snprintf(&buffer
[len
], sizeof(buffer
) - len
, "%c",
602 (unsigned char) *cur
++);
605 len
= strlen(buffer
);
606 snprintf(&buffer
[len
], sizeof(buffer
) - len
, "\n");
608 while ((*cur
== '\n') || (*cur
== '\r'))
611 while ((cur
!= base
) && (n
++ < 80)) {
612 len
= strlen(buffer
);
613 snprintf(&buffer
[len
], sizeof(buffer
) - len
, " ");
616 len
= strlen(buffer
);
617 snprintf(&buffer
[len
], sizeof(buffer
) - len
, "^\n");
619 xmlGenericError(xmlGenericErrorContext
, "</pre>");
624 * @ctx: an XML parser context
625 * @msg: the message to display/transmit
626 * @...: extra parameters for the message display
628 * Display and format an error messages, gives file, line, position and
632 xmlHTMLError(void *ctx
, const char *msg
, ...)
634 xmlParserCtxtPtr ctxt
= (xmlParserCtxtPtr
) ctx
;
635 xmlParserInputPtr input
;
641 if ((input
!= NULL
) && (input
->filename
== NULL
) && (ctxt
->inputNr
> 1)) {
642 input
= ctxt
->inputTab
[ctxt
->inputNr
- 2];
645 xmlHTMLPrintFileInfo(input
);
647 xmlGenericError(xmlGenericErrorContext
, "<b>error</b>: ");
649 len
= strlen(buffer
);
650 vsnprintf(&buffer
[len
], sizeof(buffer
) - len
, msg
, args
);
653 xmlGenericError(xmlGenericErrorContext
, "</p>\n");
655 xmlHTMLPrintFileContext(input
);
661 * @ctx: an XML parser context
662 * @msg: the message to display/transmit
663 * @...: extra parameters for the message display
665 * Display and format a warning messages, gives file, line, position and
669 xmlHTMLWarning(void *ctx
, const char *msg
, ...)
671 xmlParserCtxtPtr ctxt
= (xmlParserCtxtPtr
) ctx
;
672 xmlParserInputPtr input
;
678 if ((input
!= NULL
) && (input
->filename
== NULL
) && (ctxt
->inputNr
> 1)) {
679 input
= ctxt
->inputTab
[ctxt
->inputNr
- 2];
683 xmlHTMLPrintFileInfo(input
);
685 xmlGenericError(xmlGenericErrorContext
, "<b>warning</b>: ");
687 len
= strlen(buffer
);
688 vsnprintf(&buffer
[len
], sizeof(buffer
) - len
, msg
, args
);
691 xmlGenericError(xmlGenericErrorContext
, "</p>\n");
693 xmlHTMLPrintFileContext(input
);
698 * xmlHTMLValidityError:
699 * @ctx: an XML parser context
700 * @msg: the message to display/transmit
701 * @...: extra parameters for the message display
703 * Display and format an validity error messages, gives file,
704 * line, position and extra parameters.
707 xmlHTMLValidityError(void *ctx
, const char *msg
, ...)
709 xmlParserCtxtPtr ctxt
= (xmlParserCtxtPtr
) ctx
;
710 xmlParserInputPtr input
;
716 if ((input
->filename
== NULL
) && (ctxt
->inputNr
> 1))
717 input
= ctxt
->inputTab
[ctxt
->inputNr
- 2];
719 xmlHTMLPrintFileInfo(input
);
721 xmlGenericError(xmlGenericErrorContext
, "<b>validity error</b>: ");
722 len
= strlen(buffer
);
724 vsnprintf(&buffer
[len
], sizeof(buffer
) - len
, msg
, args
);
727 xmlGenericError(xmlGenericErrorContext
, "</p>\n");
729 xmlHTMLPrintFileContext(input
);
731 progresult
= XMLLINT_ERR_VALID
;
735 * xmlHTMLValidityWarning:
736 * @ctx: an XML parser context
737 * @msg: the message to display/transmit
738 * @...: extra parameters for the message display
740 * Display and format a validity warning messages, gives file, line,
741 * position and extra parameters.
744 xmlHTMLValidityWarning(void *ctx
, const char *msg
, ...)
746 xmlParserCtxtPtr ctxt
= (xmlParserCtxtPtr
) ctx
;
747 xmlParserInputPtr input
;
753 if ((input
->filename
== NULL
) && (ctxt
->inputNr
> 1))
754 input
= ctxt
->inputTab
[ctxt
->inputNr
- 2];
756 xmlHTMLPrintFileInfo(input
);
758 xmlGenericError(xmlGenericErrorContext
, "<b>validity warning</b>: ");
760 len
= strlen(buffer
);
761 vsnprintf(&buffer
[len
], sizeof(buffer
) - len
, msg
, args
);
764 xmlGenericError(xmlGenericErrorContext
, "</p>\n");
766 xmlHTMLPrintFileContext(input
);
770 /************************************************************************
774 ************************************************************************/
775 #ifdef LIBXML_DEBUG_ENABLED
776 #ifdef LIBXML_XPATH_ENABLED
779 * @prompt: the prompt value
783 * Returns a pointer to it or NULL on EOF the caller is expected to
784 * free the returned string.
787 xmlShellReadline(char *prompt
) {
788 #ifdef HAVE_LIBREADLINE
791 /* Get a line from the user. */
792 line_read
= readline (prompt
);
794 /* If the line has any text in it, save it on the history. */
795 if (line_read
&& *line_read
)
796 add_history (line_read
);
805 fprintf(stdout
, "%s", prompt
);
806 if (!fgets(line_read
, 500, stdin
))
809 len
= strlen(line_read
);
810 ret
= (char *) malloc(len
+ 1);
812 memcpy (ret
, line_read
, len
+ 1);
817 #endif /* LIBXML_XPATH_ENABLED */
818 #endif /* LIBXML_DEBUG_ENABLED */
820 /************************************************************************
824 ************************************************************************/
826 static int myRead(FILE *f
, char * buf
, int len
) {
827 return(fread(buf
, 1, len
, f
));
829 static void myClose(FILE *f
) {
835 /************************************************************************
839 ************************************************************************/
844 static xmlSAXHandler emptySAXHandlerStruct
= {
845 NULL
, /* internalSubset */
846 NULL
, /* isStandalone */
847 NULL
, /* hasInternalSubset */
848 NULL
, /* hasExternalSubset */
849 NULL
, /* resolveEntity */
850 NULL
, /* getEntity */
851 NULL
, /* entityDecl */
852 NULL
, /* notationDecl */
853 NULL
, /* attributeDecl */
854 NULL
, /* elementDecl */
855 NULL
, /* unparsedEntityDecl */
856 NULL
, /* setDocumentLocator */
857 NULL
, /* startDocument */
858 NULL
, /* endDocument */
859 NULL
, /* startElement */
860 NULL
, /* endElement */
861 NULL
, /* reference */
862 NULL
, /* characters */
863 NULL
, /* ignorableWhitespace */
864 NULL
, /* processingInstruction */
866 NULL
, /* xmlParserWarning */
867 NULL
, /* xmlParserError */
868 NULL
, /* xmlParserError */
869 NULL
, /* getParameterEntity */
870 NULL
, /* cdataBlock; */
871 NULL
, /* externalSubset; */
874 NULL
, /* startElementNs */
875 NULL
, /* endElementNs */
876 NULL
/* xmlStructuredErrorFunc */
879 static xmlSAXHandlerPtr emptySAXHandler
= &emptySAXHandlerStruct
;
880 extern xmlSAXHandlerPtr debugSAXHandler
;
881 static int callbacks
;
885 * @ctxt: An XML parser context
887 * Is this document tagged standalone ?
892 isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED
)
897 fprintf(stdout
, "SAX.isStandalone()\n");
902 * hasInternalSubsetDebug:
903 * @ctxt: An XML parser context
905 * Does this document has an internal subset
910 hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
)
915 fprintf(stdout
, "SAX.hasInternalSubset()\n");
920 * hasExternalSubsetDebug:
921 * @ctxt: An XML parser context
923 * Does this document has an external subset
928 hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
)
933 fprintf(stdout
, "SAX.hasExternalSubset()\n");
938 * internalSubsetDebug:
939 * @ctxt: An XML parser context
941 * Does this document has an internal subset
944 internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
945 const xmlChar
*ExternalID
, const xmlChar
*SystemID
)
950 fprintf(stdout
, "SAX.internalSubset(%s,", name
);
951 if (ExternalID
== NULL
)
952 fprintf(stdout
, " ,");
954 fprintf(stdout
, " %s,", ExternalID
);
955 if (SystemID
== NULL
)
956 fprintf(stdout
, " )\n");
958 fprintf(stdout
, " %s)\n", SystemID
);
962 * externalSubsetDebug:
963 * @ctxt: An XML parser context
965 * Does this document has an external subset
968 externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
969 const xmlChar
*ExternalID
, const xmlChar
*SystemID
)
974 fprintf(stdout
, "SAX.externalSubset(%s,", name
);
975 if (ExternalID
== NULL
)
976 fprintf(stdout
, " ,");
978 fprintf(stdout
, " %s,", ExternalID
);
979 if (SystemID
== NULL
)
980 fprintf(stdout
, " )\n");
982 fprintf(stdout
, " %s)\n", SystemID
);
986 * resolveEntityDebug:
987 * @ctxt: An XML parser context
988 * @publicId: The public ID of the entity
989 * @systemId: The system ID of the entity
991 * Special entity resolver, better left to the parser, it has
992 * more context than the application layer.
993 * The default behaviour is to NOT resolve the entities, in that case
994 * the ENTITY_REF nodes are built in the structure (and the parameter
997 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
999 static xmlParserInputPtr
1000 resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*publicId
, const xmlChar
*systemId
)
1005 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1008 fprintf(stdout
, "SAX.resolveEntity(");
1009 if (publicId
!= NULL
)
1010 fprintf(stdout
, "%s", (char *)publicId
);
1012 fprintf(stdout
, " ");
1013 if (systemId
!= NULL
)
1014 fprintf(stdout
, ", %s)\n", (char *)systemId
);
1016 fprintf(stdout
, ", )\n");
1022 * @ctxt: An XML parser context
1023 * @name: The entity name
1025 * Get an entity by name
1027 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1030 getEntityDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
1035 fprintf(stdout
, "SAX.getEntity(%s)\n", name
);
1040 * getParameterEntityDebug:
1041 * @ctxt: An XML parser context
1042 * @name: The entity name
1044 * Get a parameter entity by name
1046 * Returns the xmlParserInputPtr
1049 getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
1054 fprintf(stdout
, "SAX.getParameterEntity(%s)\n", name
);
1061 * @ctxt: An XML parser context
1062 * @name: the entity name
1063 * @type: the entity type
1064 * @publicId: The public ID of the entity
1065 * @systemId: The system ID of the entity
1066 * @content: the entity value (without processing).
1068 * An entity definition has been parsed
1071 entityDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
, int type
,
1072 const xmlChar
*publicId
, const xmlChar
*systemId
, xmlChar
*content
)
1074 const xmlChar
*nullstr
= BAD_CAST
"(null)";
1075 /* not all libraries handle printing null pointers nicely */
1076 if (publicId
== NULL
)
1078 if (systemId
== NULL
)
1080 if (content
== NULL
)
1081 content
= (xmlChar
*)nullstr
;
1085 fprintf(stdout
, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1086 name
, type
, publicId
, systemId
, content
);
1090 * attributeDeclDebug:
1091 * @ctxt: An XML parser context
1092 * @name: the attribute name
1093 * @type: the attribute type
1095 * An attribute definition has been parsed
1098 attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
* elem
,
1099 const xmlChar
* name
, int type
, int def
,
1100 const xmlChar
* defaultValue
, xmlEnumerationPtr tree
)
1105 if (defaultValue
== NULL
)
1106 fprintf(stdout
, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1107 elem
, name
, type
, def
);
1109 fprintf(stdout
, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1110 elem
, name
, type
, def
, defaultValue
);
1111 xmlFreeEnumeration(tree
);
1116 * @ctxt: An XML parser context
1117 * @name: the element name
1118 * @type: the element type
1119 * @content: the element value (without processing).
1121 * An element definition has been parsed
1124 elementDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
, int type
,
1125 xmlElementContentPtr content ATTRIBUTE_UNUSED
)
1130 fprintf(stdout
, "SAX.elementDecl(%s, %d, ...)\n",
1135 * notationDeclDebug:
1136 * @ctxt: An XML parser context
1137 * @name: The name of the notation
1138 * @publicId: The public ID of the entity
1139 * @systemId: The system ID of the entity
1141 * What to do when a notation declaration has been parsed.
1144 notationDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
1145 const xmlChar
*publicId
, const xmlChar
*systemId
)
1150 fprintf(stdout
, "SAX.notationDecl(%s, %s, %s)\n",
1151 (char *) name
, (char *) publicId
, (char *) systemId
);
1155 * unparsedEntityDeclDebug:
1156 * @ctxt: An XML parser context
1157 * @name: The name of the entity
1158 * @publicId: The public ID of the entity
1159 * @systemId: The system ID of the entity
1160 * @notationName: the name of the notation
1162 * What to do when an unparsed entity declaration is parsed
1165 unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
,
1166 const xmlChar
*publicId
, const xmlChar
*systemId
,
1167 const xmlChar
*notationName
)
1169 const xmlChar
*nullstr
= BAD_CAST
"(null)";
1171 if (publicId
== NULL
)
1173 if (systemId
== NULL
)
1175 if (notationName
== NULL
)
1176 notationName
= nullstr
;
1180 fprintf(stdout
, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1181 (char *) name
, (char *) publicId
, (char *) systemId
,
1182 (char *) notationName
);
1186 * setDocumentLocatorDebug:
1187 * @ctxt: An XML parser context
1188 * @loc: A SAX Locator
1190 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1191 * Everything is available on the context, so this is useless in our case.
1194 setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED
, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED
)
1199 fprintf(stdout
, "SAX.setDocumentLocator()\n");
1203 * startDocumentDebug:
1204 * @ctxt: An XML parser context
1206 * called when the document start being processed.
1209 startDocumentDebug(void *ctx ATTRIBUTE_UNUSED
)
1214 fprintf(stdout
, "SAX.startDocument()\n");
1219 * @ctxt: An XML parser context
1221 * called when the document end has been detected.
1224 endDocumentDebug(void *ctx ATTRIBUTE_UNUSED
)
1229 fprintf(stdout
, "SAX.endDocument()\n");
1233 * startElementDebug:
1234 * @ctxt: An XML parser context
1235 * @name: The element name
1237 * called when an opening tag has been processed.
1240 startElementDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
, const xmlChar
**atts
)
1247 fprintf(stdout
, "SAX.startElement(%s", (char *) name
);
1249 for (i
= 0;(atts
[i
] != NULL
);i
++) {
1250 fprintf(stdout
, ", %s='", atts
[i
++]);
1251 if (atts
[i
] != NULL
)
1252 fprintf(stdout
, "%s'", atts
[i
]);
1255 fprintf(stdout
, ")\n");
1260 * @ctxt: An XML parser context
1261 * @name: The element name
1263 * called when the end of an element has been detected.
1266 endElementDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
1271 fprintf(stdout
, "SAX.endElement(%s)\n", (char *) name
);
1276 * @ctxt: An XML parser context
1277 * @ch: a xmlChar string
1278 * @len: the number of xmlChar
1280 * receiving some chars from the parser.
1281 * Question: how much at a time ???
1284 charactersDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*ch
, int len
)
1292 for (i
= 0;(i
<len
) && (i
< 30);i
++)
1296 fprintf(stdout
, "SAX.characters(%s, %d)\n", out
, len
);
1301 * @ctxt: An XML parser context
1302 * @name: The entity name
1304 * called when an entity reference is detected.
1307 referenceDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*name
)
1312 fprintf(stdout
, "SAX.reference(%s)\n", name
);
1316 * ignorableWhitespaceDebug:
1317 * @ctxt: An XML parser context
1318 * @ch: a xmlChar string
1319 * @start: the first char in the string
1320 * @len: the number of xmlChar
1322 * receiving some ignorable whitespaces from the parser.
1323 * Question: how much at a time ???
1326 ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*ch
, int len
)
1334 for (i
= 0;(i
<len
) && (i
< 30);i
++)
1337 fprintf(stdout
, "SAX.ignorableWhitespace(%s, %d)\n", out
, len
);
1341 * processingInstructionDebug:
1342 * @ctxt: An XML parser context
1343 * @target: the target name
1344 * @data: the PI data's
1345 * @len: the number of xmlChar
1347 * A processing instruction has been parsed.
1350 processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*target
,
1351 const xmlChar
*data
)
1357 fprintf(stdout
, "SAX.processingInstruction(%s, %s)\n",
1358 (char *) target
, (char *) data
);
1360 fprintf(stdout
, "SAX.processingInstruction(%s, NULL)\n",
1366 * @ctx: the user data (XML parser context)
1367 * @value: The pcdata content
1368 * @len: the block length
1370 * called when a pcdata block has been parsed
1373 cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*value
, int len
)
1378 fprintf(stdout
, "SAX.pcdata(%.20s, %d)\n",
1379 (char *) value
, len
);
1384 * @ctxt: An XML parser context
1385 * @value: the comment content
1387 * A comment has been parsed.
1390 commentDebug(void *ctx ATTRIBUTE_UNUSED
, const xmlChar
*value
)
1395 fprintf(stdout
, "SAX.comment(%s)\n", value
);
1400 * @ctxt: An XML parser context
1401 * @msg: the message to display/transmit
1402 * @...: extra parameters for the message display
1404 * Display and format a warning messages, gives file, line, position and
1407 static void XMLCDECL
1408 warningDebug(void *ctx ATTRIBUTE_UNUSED
, const char *msg
, ...)
1415 va_start(args
, msg
);
1416 fprintf(stdout
, "SAX.warning: ");
1417 vfprintf(stdout
, msg
, args
);
1423 * @ctxt: An XML parser context
1424 * @msg: the message to display/transmit
1425 * @...: extra parameters for the message display
1427 * Display and format a error messages, gives file, line, position and
1430 static void XMLCDECL
1431 errorDebug(void *ctx ATTRIBUTE_UNUSED
, const char *msg
, ...)
1438 va_start(args
, msg
);
1439 fprintf(stdout
, "SAX.error: ");
1440 vfprintf(stdout
, msg
, args
);
1446 * @ctxt: An XML parser context
1447 * @msg: the message to display/transmit
1448 * @...: extra parameters for the message display
1450 * Display and format a fatalError messages, gives file, line, position and
1453 static void XMLCDECL
1454 fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED
, const char *msg
, ...)
1461 va_start(args
, msg
);
1462 fprintf(stdout
, "SAX.fatalError: ");
1463 vfprintf(stdout
, msg
, args
);
1467 static xmlSAXHandler debugSAXHandlerStruct
= {
1468 internalSubsetDebug
,
1470 hasInternalSubsetDebug
,
1471 hasExternalSubsetDebug
,
1478 unparsedEntityDeclDebug
,
1479 setDocumentLocatorDebug
,
1486 ignorableWhitespaceDebug
,
1487 processingInstructionDebug
,
1492 getParameterEntityDebug
,
1494 externalSubsetDebug
,
1502 xmlSAXHandlerPtr debugSAXHandler
= &debugSAXHandlerStruct
;
1505 * SAX2 specific callbacks
1508 * startElementNsDebug:
1509 * @ctxt: An XML parser context
1510 * @name: The element name
1512 * called when an opening tag has been processed.
1515 startElementNsDebug(void *ctx ATTRIBUTE_UNUSED
,
1516 const xmlChar
*localname
,
1517 const xmlChar
*prefix
,
1520 const xmlChar
**namespaces
,
1523 const xmlChar
**attributes
)
1530 fprintf(stdout
, "SAX.startElementNs(%s", (char *) localname
);
1532 fprintf(stdout
, ", NULL");
1534 fprintf(stdout
, ", %s", (char *) prefix
);
1536 fprintf(stdout
, ", NULL");
1538 fprintf(stdout
, ", '%s'", (char *) URI
);
1539 fprintf(stdout
, ", %d", nb_namespaces
);
1541 if (namespaces
!= NULL
) {
1542 for (i
= 0;i
< nb_namespaces
* 2;i
++) {
1543 fprintf(stdout
, ", xmlns");
1544 if (namespaces
[i
] != NULL
)
1545 fprintf(stdout
, ":%s", namespaces
[i
]);
1547 fprintf(stdout
, "='%s'", namespaces
[i
]);
1550 fprintf(stdout
, ", %d, %d", nb_attributes
, nb_defaulted
);
1551 if (attributes
!= NULL
) {
1552 for (i
= 0;i
< nb_attributes
* 5;i
+= 5) {
1553 if (attributes
[i
+ 1] != NULL
)
1554 fprintf(stdout
, ", %s:%s='", attributes
[i
+ 1], attributes
[i
]);
1556 fprintf(stdout
, ", %s='", attributes
[i
]);
1557 fprintf(stdout
, "%.4s...', %d", attributes
[i
+ 3],
1558 (int)(attributes
[i
+ 4] - attributes
[i
+ 3]));
1561 fprintf(stdout
, ")\n");
1566 * @ctxt: An XML parser context
1567 * @name: The element name
1569 * called when the end of an element has been detected.
1572 endElementNsDebug(void *ctx ATTRIBUTE_UNUSED
,
1573 const xmlChar
*localname
,
1574 const xmlChar
*prefix
,
1580 fprintf(stdout
, "SAX.endElementNs(%s", (char *) localname
);
1582 fprintf(stdout
, ", NULL");
1584 fprintf(stdout
, ", %s", (char *) prefix
);
1586 fprintf(stdout
, ", NULL)\n");
1588 fprintf(stdout
, ", '%s')\n", (char *) URI
);
1591 static xmlSAXHandler debugSAX2HandlerStruct
= {
1592 internalSubsetDebug
,
1594 hasInternalSubsetDebug
,
1595 hasExternalSubsetDebug
,
1602 unparsedEntityDeclDebug
,
1603 setDocumentLocatorDebug
,
1610 ignorableWhitespaceDebug
,
1611 processingInstructionDebug
,
1616 getParameterEntityDebug
,
1618 externalSubsetDebug
,
1621 startElementNsDebug
,
1626 static xmlSAXHandlerPtr debugSAX2Handler
= &debugSAX2HandlerStruct
;
1629 testSAX(const char *filename
) {
1630 xmlSAXHandlerPtr handler
;
1631 const char *user_data
= "user_data"; /* mostly for debugging */
1632 xmlParserInputBufferPtr buf
= NULL
;
1633 xmlParserInputPtr inputStream
;
1634 xmlParserCtxtPtr ctxt
= NULL
;
1635 xmlSAXHandlerPtr old_sax
= NULL
;
1640 handler
= emptySAXHandler
;
1641 #ifdef LIBXML_SAX1_ENABLED
1643 handler
= debugSAXHandler
;
1646 handler
= debugSAX2Handler
;
1650 * it's not the simplest code but the most generic in term of I/O
1652 buf
= xmlParserInputBufferCreateFilename(filename
, XML_CHAR_ENCODING_NONE
);
1657 #ifdef LIBXML_SCHEMAS_ENABLED
1658 if (wxschemas
!= NULL
) {
1660 xmlSchemaValidCtxtPtr vctxt
;
1662 vctxt
= xmlSchemaNewValidCtxt(wxschemas
);
1663 xmlSchemaSetValidErrors(vctxt
,
1664 (xmlSchemaValidityErrorFunc
) fprintf
,
1665 (xmlSchemaValidityWarningFunc
) fprintf
,
1668 ret
= xmlSchemaValidateStream(vctxt
, buf
, 0, handler
,
1672 fprintf(stderr
, "%s validates\n", filename
);
1673 } else if (ret
> 0) {
1674 fprintf(stderr
, "%s fails to validate\n", filename
);
1675 progresult
= XMLLINT_ERR_VALID
;
1677 fprintf(stderr
, "%s validation generated an internal error\n",
1679 progresult
= XMLLINT_ERR_VALID
;
1682 xmlSchemaFreeValidCtxt(vctxt
);
1687 * Create the parser context amd hook the input
1689 ctxt
= xmlNewParserCtxt();
1691 xmlFreeParserInputBuffer(buf
);
1694 old_sax
= ctxt
->sax
;
1695 ctxt
->sax
= handler
;
1696 ctxt
->userData
= (void *) user_data
;
1697 inputStream
= xmlNewIOInputStream(ctxt
, buf
, XML_CHAR_ENCODING_NONE
);
1698 if (inputStream
== NULL
) {
1699 xmlFreeParserInputBuffer(buf
);
1702 inputPush(ctxt
, inputStream
);
1704 /* do the parsing */
1705 xmlParseDocument(ctxt
);
1707 if (ctxt
->myDoc
!= NULL
) {
1708 fprintf(stderr
, "SAX generated a doc !\n");
1709 xmlFreeDoc(ctxt
->myDoc
);
1716 ctxt
->sax
= old_sax
;
1717 xmlFreeParserCtxt(ctxt
);
1721 /************************************************************************
1723 * Stream Test processing *
1725 ************************************************************************/
1726 #ifdef LIBXML_READER_ENABLED
1727 static void processNode(xmlTextReaderPtr reader
) {
1728 const xmlChar
*name
, *value
;
1731 type
= xmlTextReaderNodeType(reader
);
1732 empty
= xmlTextReaderIsEmptyElement(reader
);
1735 name
= xmlTextReaderConstName(reader
);
1737 name
= BAD_CAST
"--";
1739 value
= xmlTextReaderConstValue(reader
);
1742 printf("%d %d %s %d %d",
1743 xmlTextReaderDepth(reader
),
1747 xmlTextReaderHasValue(reader
));
1751 printf(" %s\n", value
);
1754 #ifdef LIBXML_PATTERN_ENABLED
1756 xmlChar
*path
= NULL
;
1759 if (type
== XML_READER_TYPE_ELEMENT
) {
1760 /* do the check only on element start */
1761 match
= xmlPatternMatch(patternc
, xmlTextReaderCurrentNode(reader
));
1764 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
1765 path
= xmlGetNodePath(xmlTextReaderCurrentNode(reader
));
1766 printf("Node %s matches pattern %s\n", path
, pattern
);
1768 printf("Node %s matches pattern %s\n",
1769 xmlTextReaderConstName(reader
), pattern
);
1773 if (patstream
!= NULL
) {
1776 if (type
== XML_READER_TYPE_ELEMENT
) {
1777 ret
= xmlStreamPush(patstream
,
1778 xmlTextReaderConstLocalName(reader
),
1779 xmlTextReaderConstNamespaceUri(reader
));
1781 fprintf(stderr
, "xmlStreamPush() failure\n");
1782 xmlFreeStreamCtxt(patstream
);
1784 } else if (ret
!= match
) {
1785 #if defined(LIBXML_TREE_ENABLED) || defined(LIBXML_DEBUG_ENABLED)
1787 path
= xmlGetNodePath(
1788 xmlTextReaderCurrentNode(reader
));
1792 "xmlPatternMatch and xmlStreamPush disagree\n");
1794 fprintf(stderr
, " pattern %s node %s\n",
1797 fprintf(stderr
, " pattern %s node %s\n",
1798 pattern
, xmlTextReaderConstName(reader
));
1802 if ((type
== XML_READER_TYPE_END_ELEMENT
) ||
1803 ((type
== XML_READER_TYPE_ELEMENT
) && (empty
))) {
1804 ret
= xmlStreamPop(patstream
);
1806 fprintf(stderr
, "xmlStreamPop() failure\n");
1807 xmlFreeStreamCtxt(patstream
);
1818 static void streamFile(char *filename
) {
1819 xmlTextReaderPtr reader
;
1821 #ifdef HAVE_SYS_MMAN_H
1824 const char *base
= NULL
;
1825 xmlParserInputBufferPtr input
= NULL
;
1828 if (stat(filename
, &info
) < 0)
1830 if ((fd
= open(filename
, O_RDONLY
)) < 0)
1832 base
= mmap(NULL
, info
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0) ;
1833 if (base
== (void *) MAP_FAILED
)
1836 reader
= xmlReaderForMemory(base
, info
.st_size
, filename
,
1840 reader
= xmlReaderForFile(filename
, NULL
, options
);
1841 #ifdef LIBXML_PATTERN_ENABLED
1842 if (pattern
!= NULL
) {
1843 patternc
= xmlPatterncompile((const xmlChar
*) pattern
, NULL
, 0, NULL
);
1844 if (patternc
== NULL
) {
1845 xmlGenericError(xmlGenericErrorContext
,
1846 "Pattern %s failed to compile\n", pattern
);
1847 progresult
= XMLLINT_ERR_SCHEMAPAT
;
1851 if (patternc
!= NULL
) {
1852 patstream
= xmlPatternGetStreamCtxt(patternc
);
1853 if (patstream
!= NULL
) {
1854 ret
= xmlStreamPush(patstream
, NULL
, NULL
);
1856 fprintf(stderr
, "xmlStreamPush() failure\n");
1857 xmlFreeStreamCtxt(patstream
);
1865 if (reader
!= NULL
) {
1866 #ifdef LIBXML_VALID_ENABLED
1868 xmlTextReaderSetParserProp(reader
, XML_PARSER_VALIDATE
, 1);
1870 #endif /* LIBXML_VALID_ENABLED */
1871 xmlTextReaderSetParserProp(reader
, XML_PARSER_LOADDTD
, 1);
1872 #ifdef LIBXML_SCHEMAS_ENABLED
1873 if (relaxng
!= NULL
) {
1874 if ((timing
) && (!repeat
)) {
1877 ret
= xmlTextReaderRelaxNGValidate(reader
, relaxng
);
1879 xmlGenericError(xmlGenericErrorContext
,
1880 "Relax-NG schema %s failed to compile\n", relaxng
);
1881 progresult
= XMLLINT_ERR_SCHEMACOMP
;
1884 if ((timing
) && (!repeat
)) {
1885 endTimer("Compiling the schemas");
1888 if (schema
!= NULL
) {
1889 if ((timing
) && (!repeat
)) {
1892 ret
= xmlTextReaderSchemaValidate(reader
, schema
);
1894 xmlGenericError(xmlGenericErrorContext
,
1895 "XSD schema %s failed to compile\n", schema
);
1896 progresult
= XMLLINT_ERR_SCHEMACOMP
;
1899 if ((timing
) && (!repeat
)) {
1900 endTimer("Compiling the schemas");
1906 * Process all nodes in sequence
1908 if ((timing
) && (!repeat
)) {
1911 ret
= xmlTextReaderRead(reader
);
1914 #ifdef LIBXML_PATTERN_ENABLED
1918 processNode(reader
);
1919 ret
= xmlTextReaderRead(reader
);
1921 if ((timing
) && (!repeat
)) {
1922 #ifdef LIBXML_SCHEMAS_ENABLED
1923 if (relaxng
!= NULL
)
1924 endTimer("Parsing and validating");
1927 #ifdef LIBXML_VALID_ENABLED
1929 endTimer("Parsing and validating");
1932 endTimer("Parsing");
1935 #ifdef LIBXML_VALID_ENABLED
1937 if (xmlTextReaderIsValid(reader
) != 1) {
1938 xmlGenericError(xmlGenericErrorContext
,
1939 "Document %s does not validate\n", filename
);
1940 progresult
= XMLLINT_ERR_VALID
;
1943 #endif /* LIBXML_VALID_ENABLED */
1944 #ifdef LIBXML_SCHEMAS_ENABLED
1945 if ((relaxng
!= NULL
) || (schema
!= NULL
)) {
1946 if (xmlTextReaderIsValid(reader
) != 1) {
1947 fprintf(stderr
, "%s fails to validate\n", filename
);
1948 progresult
= XMLLINT_ERR_VALID
;
1950 fprintf(stderr
, "%s validates\n", filename
);
1955 * Done, cleanup and status
1957 xmlFreeTextReader(reader
);
1959 fprintf(stderr
, "%s : failed to parse\n", filename
);
1960 progresult
= XMLLINT_ERR_UNCLASS
;
1963 fprintf(stderr
, "Unable to open %s\n", filename
);
1964 progresult
= XMLLINT_ERR_UNCLASS
;
1966 #ifdef LIBXML_PATTERN_ENABLED
1967 if (patstream
!= NULL
) {
1968 xmlFreeStreamCtxt(patstream
);
1972 #ifdef HAVE_SYS_MMAN_H
1974 xmlFreeParserInputBuffer(input
);
1975 munmap((char *) base
, info
.st_size
);
1981 static void walkDoc(xmlDocPtr doc
) {
1982 xmlTextReaderPtr reader
;
1985 #ifdef LIBXML_PATTERN_ENABLED
1987 const xmlChar
*namespaces
[22];
1991 root
= xmlDocGetRootElement(doc
);
1992 for (ns
= root
->nsDef
, i
= 0;ns
!= NULL
&& i
< 20;ns
=ns
->next
) {
1993 namespaces
[i
++] = ns
->href
;
1994 namespaces
[i
++] = ns
->prefix
;
1996 namespaces
[i
++] = NULL
;
1997 namespaces
[i
] = NULL
;
1999 if (pattern
!= NULL
) {
2000 patternc
= xmlPatterncompile((const xmlChar
*) pattern
, doc
->dict
,
2002 if (patternc
== NULL
) {
2003 xmlGenericError(xmlGenericErrorContext
,
2004 "Pattern %s failed to compile\n", pattern
);
2005 progresult
= XMLLINT_ERR_SCHEMAPAT
;
2009 if (patternc
!= NULL
) {
2010 patstream
= xmlPatternGetStreamCtxt(patternc
);
2011 if (patstream
!= NULL
) {
2012 ret
= xmlStreamPush(patstream
, NULL
, NULL
);
2014 fprintf(stderr
, "xmlStreamPush() failure\n");
2015 xmlFreeStreamCtxt(patstream
);
2020 #endif /* LIBXML_PATTERN_ENABLED */
2021 reader
= xmlReaderWalker(doc
);
2022 if (reader
!= NULL
) {
2023 if ((timing
) && (!repeat
)) {
2026 ret
= xmlTextReaderRead(reader
);
2029 #ifdef LIBXML_PATTERN_ENABLED
2033 processNode(reader
);
2034 ret
= xmlTextReaderRead(reader
);
2036 if ((timing
) && (!repeat
)) {
2037 endTimer("walking through the doc");
2039 xmlFreeTextReader(reader
);
2041 fprintf(stderr
, "failed to walk through the doc\n");
2042 progresult
= XMLLINT_ERR_UNCLASS
;
2045 fprintf(stderr
, "Failed to crate a reader from the document\n");
2046 progresult
= XMLLINT_ERR_UNCLASS
;
2048 #ifdef LIBXML_PATTERN_ENABLED
2049 if (patstream
!= NULL
) {
2050 xmlFreeStreamCtxt(patstream
);
2055 #endif /* LIBXML_READER_ENABLED */
2057 #ifdef LIBXML_XPATH_ENABLED
2058 /************************************************************************
2062 ************************************************************************/
2064 static void doXPathDump(xmlXPathObjectPtr cur
) {
2066 case XPATH_NODESET
: {
2069 #ifdef LIBXML_OUTPUT_ENABLED
2070 xmlSaveCtxtPtr ctxt
;
2072 if (cur
->nodesetval
->nodeNr
<= 0) {
2073 fprintf(stderr
, "XPath set is empty\n");
2074 progresult
= XMLLINT_ERR_XPATH
;
2077 ctxt
= xmlSaveToFd(1, NULL
, 0);
2079 fprintf(stderr
, "Out of memory for XPath\n");
2080 progresult
= XMLLINT_ERR_MEM
;
2083 for (i
= 0;i
< cur
->nodesetval
->nodeNr
;i
++) {
2084 node
= cur
->nodesetval
->nodeTab
[i
];
2085 xmlSaveTree(ctxt
, node
);
2089 printf("xpath returned %d nodes\n", cur
->nodesetval
->nodeNr
);
2094 if (cur
->boolval
) printf("true");
2095 else printf("false");
2098 switch (xmlXPathIsInf(cur
->floatval
)) {
2103 printf("-Infinity");
2106 if (xmlXPathIsNaN(cur
->floatval
)) {
2109 printf("%0g", cur
->floatval
);
2114 printf("%s", (const char *) cur
->stringval
);
2116 case XPATH_UNDEFINED
:
2117 fprintf(stderr
, "XPath Object is uninitialized\n");
2118 progresult
= XMLLINT_ERR_XPATH
;
2121 fprintf(stderr
, "XPath object of unexpected type\n");
2122 progresult
= XMLLINT_ERR_XPATH
;
2127 static void doXPathQuery(xmlDocPtr doc
, const char *query
) {
2128 xmlXPathContextPtr ctxt
;
2129 xmlXPathObjectPtr res
;
2131 ctxt
= xmlXPathNewContext(doc
);
2133 fprintf(stderr
, "Out of memory for XPath\n");
2134 progresult
= XMLLINT_ERR_MEM
;
2137 ctxt
->node
= xmlDocGetRootElement(doc
);
2138 res
= xmlXPathEval(BAD_CAST query
, ctxt
);
2139 xmlXPathFreeContext(ctxt
);
2142 fprintf(stderr
, "XPath evaluation failure\n");
2143 progresult
= XMLLINT_ERR_XPATH
;
2147 xmlXPathFreeObject(res
);
2149 #endif /* LIBXML_XPATH_ENABLED */
2151 /************************************************************************
2153 * Tree Test processing *
2155 ************************************************************************/
2156 static void parseAndPrintFile(char *filename
, xmlParserCtxtPtr rectxt
) {
2157 xmlDocPtr doc
= NULL
;
2158 #ifdef LIBXML_TREE_ENABLED
2160 #endif /* LIBXML_TREE_ENABLED */
2162 if ((timing
) && (!repeat
))
2166 #ifdef LIBXML_TREE_ENABLED
2167 if (filename
== NULL
) {
2171 doc
= xmlNewDoc(BAD_CAST
"1.0");
2172 n
= xmlNewDocNode(doc
, NULL
, BAD_CAST
"info", NULL
);
2173 xmlNodeSetContent(n
, BAD_CAST
"abc");
2174 xmlDocSetRootElement(doc
, n
);
2177 #endif /* LIBXML_TREE_ENABLED */
2178 #ifdef LIBXML_HTML_ENABLED
2179 #ifdef LIBXML_PUSH_ENABLED
2180 else if ((html
) && (push
)) {
2183 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2184 f
= fopen(filename
, "rb");
2186 f
= fopen(filename
, "r");
2191 htmlParserCtxtPtr ctxt
;
2195 res
= fread(chars
, 1, 4, f
);
2197 ctxt
= htmlCreatePushParserCtxt(NULL
, NULL
,
2198 chars
, res
, filename
, XML_CHAR_ENCODING_NONE
);
2199 while ((res
= fread(chars
, 1, size
, f
)) > 0) {
2200 htmlParseChunk(ctxt
, chars
, res
, 0);
2202 htmlParseChunk(ctxt
, chars
, 0, 1);
2204 htmlFreeParserCtxt(ctxt
);
2209 #endif /* LIBXML_PUSH_ENABLED */
2210 #ifdef HAVE_SYS_MMAN_H
2211 else if ((html
) && (memory
)) {
2215 if (stat(filename
, &info
) < 0)
2217 if ((fd
= open(filename
, O_RDONLY
)) < 0)
2219 base
= mmap(NULL
, info
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0) ;
2220 if (base
== (void *) MAP_FAILED
)
2223 doc
= htmlReadMemory((char *) base
, info
.st_size
, filename
,
2226 munmap((char *) base
, info
.st_size
);
2231 doc
= htmlReadFile(filename
, NULL
, options
);
2233 #endif /* LIBXML_HTML_ENABLED */
2235 #ifdef LIBXML_PUSH_ENABLED
2237 * build an XML tree from a string;
2242 /* '-' Usually means stdin -<sven@zen.org> */
2243 if ((filename
[0] == '-') && (filename
[1] == 0)) {
2246 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2247 f
= fopen(filename
, "rb");
2249 f
= fopen(filename
, "r");
2254 int res
, size
= 1024;
2256 xmlParserCtxtPtr ctxt
;
2258 /* if (repeat) size = 1024; */
2259 res
= fread(chars
, 1, 4, f
);
2261 ctxt
= xmlCreatePushParserCtxt(NULL
, NULL
,
2262 chars
, res
, filename
);
2263 xmlCtxtUseOptions(ctxt
, options
);
2264 while ((res
= fread(chars
, 1, size
, f
)) > 0) {
2265 xmlParseChunk(ctxt
, chars
, res
, 0);
2267 xmlParseChunk(ctxt
, chars
, 0, 1);
2269 ret
= ctxt
->wellFormed
;
2270 xmlFreeParserCtxt(ctxt
);
2280 #endif /* LIBXML_PUSH_ENABLED */
2282 if ((filename
[0] == '-') && (filename
[1] == 0)) {
2283 doc
= xmlReadFd(0, NULL
, NULL
, options
);
2287 #if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2288 f
= fopen(filename
, "rb");
2290 f
= fopen(filename
, "r");
2294 doc
= xmlReadIO((xmlInputReadCallback
) myRead
,
2295 (xmlInputCloseCallback
) myClose
, f
,
2296 filename
, NULL
, options
);
2298 doc
= xmlCtxtReadIO(rectxt
,
2299 (xmlInputReadCallback
) myRead
,
2300 (xmlInputCloseCallback
) myClose
, f
,
2301 filename
, NULL
, options
);
2305 } else if (htmlout
) {
2306 xmlParserCtxtPtr ctxt
;
2309 ctxt
= xmlNewParserCtxt();
2315 ctxt
->sax
->error
= xmlHTMLError
;
2316 ctxt
->sax
->warning
= xmlHTMLWarning
;
2317 ctxt
->vctxt
.error
= xmlHTMLValidityError
;
2318 ctxt
->vctxt
.warning
= xmlHTMLValidityWarning
;
2320 doc
= xmlCtxtReadFile(ctxt
, filename
, NULL
, options
);
2323 xmlFreeParserCtxt(ctxt
);
2325 #ifdef HAVE_SYS_MMAN_H
2326 } else if (memory
) {
2330 if (stat(filename
, &info
) < 0)
2332 if ((fd
= open(filename
, O_RDONLY
)) < 0)
2334 base
= mmap(NULL
, info
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0) ;
2335 if (base
== (void *) MAP_FAILED
)
2339 doc
= xmlReadMemory((char *) base
, info
.st_size
,
2340 filename
, NULL
, options
);
2342 doc
= xmlCtxtReadMemory(rectxt
, (char *) base
, info
.st_size
,
2343 filename
, NULL
, options
);
2345 munmap((char *) base
, info
.st_size
);
2348 #ifdef LIBXML_VALID_ENABLED
2350 xmlParserCtxtPtr ctxt
= NULL
;
2353 ctxt
= xmlNewParserCtxt();
2359 doc
= xmlCtxtReadFile(ctxt
, filename
, NULL
, options
);
2361 if (ctxt
->valid
== 0)
2362 progresult
= XMLLINT_ERR_RDFILE
;
2364 xmlFreeParserCtxt(ctxt
);
2366 #endif /* LIBXML_VALID_ENABLED */
2369 doc
= xmlCtxtReadFile(rectxt
, filename
, NULL
, options
);
2371 #ifdef LIBXML_SAX1_ENABLED
2373 doc
= xmlParseFile(filename
);
2375 #endif /* LIBXML_SAX1_ENABLED */
2376 doc
= xmlReadFile(filename
, NULL
, options
);
2382 * If we don't have a document we might as well give up. Do we
2383 * want an error message here? <sven@zen.org> */
2385 progresult
= XMLLINT_ERR_UNCLASS
;
2389 if ((timing
) && (!repeat
)) {
2390 endTimer("Parsing");
2394 * Remove DOCTYPE nodes
2399 dtd
= xmlGetIntSubset(doc
);
2401 xmlUnlinkNode((xmlNodePtr
)dtd
);
2406 #ifdef LIBXML_XINCLUDE_ENABLED
2408 if ((timing
) && (!repeat
)) {
2411 if (xmlXIncludeProcessFlags(doc
, options
) < 0)
2412 progresult
= XMLLINT_ERR_UNCLASS
;
2413 if ((timing
) && (!repeat
)) {
2414 endTimer("Xinclude processing");
2419 #ifdef LIBXML_XPATH_ENABLED
2420 if (xpathquery
!= NULL
) {
2421 doXPathQuery(doc
, xpathquery
);
2425 #ifdef LIBXML_DEBUG_ENABLED
2426 #ifdef LIBXML_XPATH_ENABLED
2431 xmlXPathOrderDocElems(doc
);
2432 xmlShell(doc
, filename
, xmlShellReadline
, stdout
);
2437 #ifdef LIBXML_TREE_ENABLED
2439 * test intermediate copy if needed.
2446 doc
= xmlCopyDoc(doc
, 1);
2448 endTimer("Copying");
2455 endTimer("Freeing original");
2458 #endif /* LIBXML_TREE_ENABLED */
2460 #ifdef LIBXML_VALID_ENABLED
2461 if ((insert
) && (!html
)) {
2462 const xmlChar
* list
[256];
2466 if (doc
->children
!= NULL
) {
2467 node
= doc
->children
;
2468 while ((node
!= NULL
) && (node
->last
== NULL
)) node
= node
->next
;
2470 nb
= xmlValidGetValidElements(node
->last
, NULL
, list
, 256);
2472 fprintf(stderr
, "could not get valid list of elements\n");
2473 } else if (nb
== 0) {
2474 fprintf(stderr
, "No element can be inserted under root\n");
2476 fprintf(stderr
, "%d element types can be inserted under root:\n",
2478 for (i
= 0;i
< nb
;i
++) {
2479 fprintf(stderr
, "%s\n", (char *) list
[i
]);
2485 #endif /* LIBXML_VALID_ENABLED */
2486 #ifdef LIBXML_READER_ENABLED
2490 #endif /* LIBXML_READER_ENABLED */
2491 #ifdef LIBXML_OUTPUT_ENABLED
2498 #ifdef LIBXML_DEBUG_ENABLED
2501 if ((timing
) && (!repeat
)) {
2504 #ifdef LIBXML_HTML_ENABLED
2505 if ((html
) && (!xmlout
)) {
2507 htmlSaveFile(output
? output
: "-", doc
);
2509 else if (encoding
!= NULL
) {
2511 htmlSaveFileFormat(output
? output
: "-", doc
, encoding
, 1);
2514 htmlSaveFileFormat(output
? output
: "-", doc
, encoding
, 0);
2518 htmlSaveFileFormat(output
? output
: "-", doc
, NULL
, 1);
2525 out
= fopen(output
,"wb");
2528 if (htmlDocDump(out
, doc
) < 0)
2529 progresult
= XMLLINT_ERR_OUT
;
2534 fprintf(stderr
, "failed to open %s\n", output
);
2535 progresult
= XMLLINT_ERR_OUT
;
2538 if ((timing
) && (!repeat
)) {
2543 #ifdef LIBXML_C14N_ENABLED
2545 xmlChar
*result
= NULL
;
2548 size
= xmlC14NDocDumpMemory(doc
, NULL
, XML_C14N_1_0
, NULL
, 1, &result
);
2550 write(1, result
, size
);
2553 fprintf(stderr
, "Failed to canonicalize\n");
2554 progresult
= XMLLINT_ERR_OUT
;
2556 } else if (canonical
) {
2557 xmlChar
*result
= NULL
;
2560 size
= xmlC14NDocDumpMemory(doc
, NULL
, XML_C14N_1_1
, NULL
, 1, &result
);
2562 write(1, result
, size
);
2565 fprintf(stderr
, "Failed to canonicalize\n");
2566 progresult
= XMLLINT_ERR_OUT
;
2569 if (exc_canonical
) {
2570 xmlChar
*result
= NULL
;
2573 size
= xmlC14NDocDumpMemory(doc
, NULL
, XML_C14N_EXCLUSIVE_1_0
, NULL
, 1, &result
);
2575 write(1, result
, size
);
2578 fprintf(stderr
, "Failed to canonicalize\n");
2579 progresult
= XMLLINT_ERR_OUT
;
2583 #ifdef HAVE_SYS_MMAN_H
2588 if (encoding
!= NULL
) {
2590 xmlDocDumpFormatMemoryEnc(doc
, &result
, &len
, encoding
, 1);
2592 xmlDocDumpMemoryEnc(doc
, &result
, &len
, encoding
);
2596 xmlDocDumpFormatMemory(doc
, &result
, &len
, 1);
2598 xmlDocDumpMemory(doc
, &result
, &len
);
2600 if (result
== NULL
) {
2601 fprintf(stderr
, "Failed to save\n");
2602 progresult
= XMLLINT_ERR_OUT
;
2604 write(1, result
, len
);
2609 #endif /* HAVE_SYS_MMAN_H */
2611 xmlSaveFile(output
? output
: "-", doc
);
2612 } else if (oldout
) {
2613 if (encoding
!= NULL
) {
2615 ret
= xmlSaveFormatFileEnc(output
? output
: "-", doc
,
2619 ret
= xmlSaveFileEnc(output
? output
: "-", doc
,
2623 fprintf(stderr
, "failed save to %s\n",
2624 output
? output
: "-");
2625 progresult
= XMLLINT_ERR_OUT
;
2627 } else if (format
) {
2628 ret
= xmlSaveFormatFile(output
? output
: "-", doc
, 1);
2630 fprintf(stderr
, "failed save to %s\n",
2631 output
? output
: "-");
2632 progresult
= XMLLINT_ERR_OUT
;
2639 out
= fopen(output
,"wb");
2642 if (xmlDocDump(out
, doc
) < 0)
2643 progresult
= XMLLINT_ERR_OUT
;
2648 fprintf(stderr
, "failed to open %s\n", output
);
2649 progresult
= XMLLINT_ERR_OUT
;
2653 xmlSaveCtxtPtr ctxt
;
2657 saveOpts
|= XML_SAVE_FORMAT
;
2659 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
2661 saveOpts
|= XML_SAVE_AS_XML
;
2665 ctxt
= xmlSaveToFd(1, encoding
, saveOpts
);
2667 ctxt
= xmlSaveToFilename(output
, encoding
, saveOpts
);
2670 if (xmlSaveDoc(ctxt
, doc
) < 0) {
2671 fprintf(stderr
, "failed save to %s\n",
2672 output
? output
: "-");
2673 progresult
= XMLLINT_ERR_OUT
;
2677 progresult
= XMLLINT_ERR_OUT
;
2680 if ((timing
) && (!repeat
)) {
2683 #ifdef LIBXML_DEBUG_ENABLED
2689 out
= fopen(output
,"wb");
2692 xmlDebugDumpDocument(out
, doc
);
2697 fprintf(stderr
, "failed to open %s\n", output
);
2698 progresult
= XMLLINT_ERR_OUT
;
2703 #endif /* LIBXML_OUTPUT_ENABLED */
2705 #ifdef LIBXML_VALID_ENABLED
2707 * A posteriori validation test
2709 if ((dtdvalid
!= NULL
) || (dtdvalidfpi
!= NULL
)) {
2712 if ((timing
) && (!repeat
)) {
2715 if (dtdvalid
!= NULL
)
2716 dtd
= xmlParseDTD(NULL
, (const xmlChar
*)dtdvalid
);
2718 dtd
= xmlParseDTD((const xmlChar
*)dtdvalidfpi
, NULL
);
2719 if ((timing
) && (!repeat
)) {
2720 endTimer("Parsing DTD");
2723 if (dtdvalid
!= NULL
)
2724 xmlGenericError(xmlGenericErrorContext
,
2725 "Could not parse DTD %s\n", dtdvalid
);
2727 xmlGenericError(xmlGenericErrorContext
,
2728 "Could not parse DTD %s\n", dtdvalidfpi
);
2729 progresult
= XMLLINT_ERR_DTD
;
2731 xmlValidCtxtPtr cvp
;
2733 if ((cvp
= xmlNewValidCtxt()) == NULL
) {
2734 xmlGenericError(xmlGenericErrorContext
,
2735 "Couldn't allocate validation context\n");
2738 cvp
->userData
= (void *) stderr
;
2739 cvp
->error
= (xmlValidityErrorFunc
) fprintf
;
2740 cvp
->warning
= (xmlValidityWarningFunc
) fprintf
;
2742 if ((timing
) && (!repeat
)) {
2745 if (!xmlValidateDtd(cvp
, doc
, dtd
)) {
2746 if (dtdvalid
!= NULL
)
2747 xmlGenericError(xmlGenericErrorContext
,
2748 "Document %s does not validate against %s\n",
2749 filename
, dtdvalid
);
2751 xmlGenericError(xmlGenericErrorContext
,
2752 "Document %s does not validate against %s\n",
2753 filename
, dtdvalidfpi
);
2754 progresult
= XMLLINT_ERR_VALID
;
2756 if ((timing
) && (!repeat
)) {
2757 endTimer("Validating against DTD");
2759 xmlFreeValidCtxt(cvp
);
2762 } else if (postvalid
) {
2763 xmlValidCtxtPtr cvp
;
2765 if ((cvp
= xmlNewValidCtxt()) == NULL
) {
2766 xmlGenericError(xmlGenericErrorContext
,
2767 "Couldn't allocate validation context\n");
2771 if ((timing
) && (!repeat
)) {
2774 cvp
->userData
= (void *) stderr
;
2775 cvp
->error
= (xmlValidityErrorFunc
) fprintf
;
2776 cvp
->warning
= (xmlValidityWarningFunc
) fprintf
;
2777 if (!xmlValidateDocument(cvp
, doc
)) {
2778 xmlGenericError(xmlGenericErrorContext
,
2779 "Document %s does not validate\n", filename
);
2780 progresult
= XMLLINT_ERR_VALID
;
2782 if ((timing
) && (!repeat
)) {
2783 endTimer("Validating");
2785 xmlFreeValidCtxt(cvp
);
2787 #endif /* LIBXML_VALID_ENABLED */
2788 #ifdef LIBXML_SCHEMATRON_ENABLED
2789 if (wxschematron
!= NULL
) {
2790 xmlSchematronValidCtxtPtr ctxt
;
2794 if ((timing
) && (!repeat
)) {
2799 flag
= XML_SCHEMATRON_OUT_XML
;
2801 flag
= XML_SCHEMATRON_OUT_TEXT
;
2803 flag
|= XML_SCHEMATRON_OUT_QUIET
;
2804 ctxt
= xmlSchematronNewValidCtxt(wxschematron
, flag
);
2806 xmlSchematronSetValidErrors(ctxt
,
2807 (xmlSchematronValidityErrorFunc
) fprintf
,
2808 (xmlSchematronValidityWarningFunc
) fprintf
,
2811 ret
= xmlSchematronValidateDoc(ctxt
, doc
);
2813 fprintf(stderr
, "%s validates\n", filename
);
2814 } else if (ret
> 0) {
2815 fprintf(stderr
, "%s fails to validate\n", filename
);
2816 progresult
= XMLLINT_ERR_VALID
;
2818 fprintf(stderr
, "%s validation generated an internal error\n",
2820 progresult
= XMLLINT_ERR_VALID
;
2822 xmlSchematronFreeValidCtxt(ctxt
);
2823 if ((timing
) && (!repeat
)) {
2824 endTimer("Validating");
2828 #ifdef LIBXML_SCHEMAS_ENABLED
2829 if (relaxngschemas
!= NULL
) {
2830 xmlRelaxNGValidCtxtPtr ctxt
;
2833 if ((timing
) && (!repeat
)) {
2837 ctxt
= xmlRelaxNGNewValidCtxt(relaxngschemas
);
2838 xmlRelaxNGSetValidErrors(ctxt
,
2839 (xmlRelaxNGValidityErrorFunc
) fprintf
,
2840 (xmlRelaxNGValidityWarningFunc
) fprintf
,
2842 ret
= xmlRelaxNGValidateDoc(ctxt
, doc
);
2844 fprintf(stderr
, "%s validates\n", filename
);
2845 } else if (ret
> 0) {
2846 fprintf(stderr
, "%s fails to validate\n", filename
);
2847 progresult
= XMLLINT_ERR_VALID
;
2849 fprintf(stderr
, "%s validation generated an internal error\n",
2851 progresult
= XMLLINT_ERR_VALID
;
2853 xmlRelaxNGFreeValidCtxt(ctxt
);
2854 if ((timing
) && (!repeat
)) {
2855 endTimer("Validating");
2857 } else if (wxschemas
!= NULL
) {
2858 xmlSchemaValidCtxtPtr ctxt
;
2861 if ((timing
) && (!repeat
)) {
2865 ctxt
= xmlSchemaNewValidCtxt(wxschemas
);
2866 xmlSchemaSetValidErrors(ctxt
,
2867 (xmlSchemaValidityErrorFunc
) fprintf
,
2868 (xmlSchemaValidityWarningFunc
) fprintf
,
2870 ret
= xmlSchemaValidateDoc(ctxt
, doc
);
2872 fprintf(stderr
, "%s validates\n", filename
);
2873 } else if (ret
> 0) {
2874 fprintf(stderr
, "%s fails to validate\n", filename
);
2875 progresult
= XMLLINT_ERR_VALID
;
2877 fprintf(stderr
, "%s validation generated an internal error\n",
2879 progresult
= XMLLINT_ERR_VALID
;
2881 xmlSchemaFreeValidCtxt(ctxt
);
2882 if ((timing
) && (!repeat
)) {
2883 endTimer("Validating");
2888 #ifdef LIBXML_DEBUG_ENABLED
2889 #if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
2890 if ((debugent
) && (!html
))
2891 xmlDebugDumpEntities(stderr
, doc
);
2898 if ((timing
) && (!repeat
)) {
2902 if ((timing
) && (!repeat
)) {
2903 endTimer("Freeing");
2907 /************************************************************************
2911 ************************************************************************/
2913 static void showVersion(const char *name
) {
2914 fprintf(stderr
, "%s: using libxml version %s\n", name
, xmlParserVersion
);
2915 fprintf(stderr
, " compiled with: ");
2916 if (xmlHasFeature(XML_WITH_THREAD
)) fprintf(stderr
, "Threads ");
2917 if (xmlHasFeature(XML_WITH_TREE
)) fprintf(stderr
, "Tree ");
2918 if (xmlHasFeature(XML_WITH_OUTPUT
)) fprintf(stderr
, "Output ");
2919 if (xmlHasFeature(XML_WITH_PUSH
)) fprintf(stderr
, "Push ");
2920 if (xmlHasFeature(XML_WITH_READER
)) fprintf(stderr
, "Reader ");
2921 if (xmlHasFeature(XML_WITH_PATTERN
)) fprintf(stderr
, "Patterns ");
2922 if (xmlHasFeature(XML_WITH_WRITER
)) fprintf(stderr
, "Writer ");
2923 if (xmlHasFeature(XML_WITH_SAX1
)) fprintf(stderr
, "SAXv1 ");
2924 if (xmlHasFeature(XML_WITH_FTP
)) fprintf(stderr
, "FTP ");
2925 if (xmlHasFeature(XML_WITH_HTTP
)) fprintf(stderr
, "HTTP ");
2926 if (xmlHasFeature(XML_WITH_VALID
)) fprintf(stderr
, "DTDValid ");
2927 if (xmlHasFeature(XML_WITH_HTML
)) fprintf(stderr
, "HTML ");
2928 if (xmlHasFeature(XML_WITH_LEGACY
)) fprintf(stderr
, "Legacy ");
2929 if (xmlHasFeature(XML_WITH_C14N
)) fprintf(stderr
, "C14N ");
2930 if (xmlHasFeature(XML_WITH_CATALOG
)) fprintf(stderr
, "Catalog ");
2931 if (xmlHasFeature(XML_WITH_XPATH
)) fprintf(stderr
, "XPath ");
2932 if (xmlHasFeature(XML_WITH_XPTR
)) fprintf(stderr
, "XPointer ");
2933 if (xmlHasFeature(XML_WITH_XINCLUDE
)) fprintf(stderr
, "XInclude ");
2934 if (xmlHasFeature(XML_WITH_ICONV
)) fprintf(stderr
, "Iconv ");
2935 if (xmlHasFeature(XML_WITH_ISO8859X
)) fprintf(stderr
, "ISO8859X ");
2936 if (xmlHasFeature(XML_WITH_UNICODE
)) fprintf(stderr
, "Unicode ");
2937 if (xmlHasFeature(XML_WITH_REGEXP
)) fprintf(stderr
, "Regexps ");
2938 if (xmlHasFeature(XML_WITH_AUTOMATA
)) fprintf(stderr
, "Automata ");
2939 if (xmlHasFeature(XML_WITH_EXPR
)) fprintf(stderr
, "Expr ");
2940 if (xmlHasFeature(XML_WITH_SCHEMAS
)) fprintf(stderr
, "Schemas ");
2941 if (xmlHasFeature(XML_WITH_SCHEMATRON
)) fprintf(stderr
, "Schematron ");
2942 if (xmlHasFeature(XML_WITH_MODULES
)) fprintf(stderr
, "Modules ");
2943 if (xmlHasFeature(XML_WITH_DEBUG
)) fprintf(stderr
, "Debug ");
2944 if (xmlHasFeature(XML_WITH_DEBUG_MEM
)) fprintf(stderr
, "MemDebug ");
2945 if (xmlHasFeature(XML_WITH_DEBUG_RUN
)) fprintf(stderr
, "RunDebug ");
2946 if (xmlHasFeature(XML_WITH_ZLIB
)) fprintf(stderr
, "Zlib ");
2947 fprintf(stderr
, "\n");
2950 static void usage(const char *name
) {
2951 printf("Usage : %s [options] XMLfiles ...\n", name
);
2952 #ifdef LIBXML_OUTPUT_ENABLED
2953 printf("\tParse the XML files and output the result of the parsing\n");
2955 printf("\tParse the XML files\n");
2956 #endif /* LIBXML_OUTPUT_ENABLED */
2957 printf("\t--version : display the version of the XML library used\n");
2958 #ifdef LIBXML_DEBUG_ENABLED
2959 printf("\t--debug : dump a debug tree of the in-memory document\n");
2960 printf("\t--shell : run a navigating shell\n");
2961 printf("\t--debugent : debug the entities defined in the document\n");
2963 #ifdef LIBXML_READER_ENABLED
2964 printf("\t--debug : dump the nodes content when using --stream\n");
2965 #endif /* LIBXML_READER_ENABLED */
2967 #ifdef LIBXML_TREE_ENABLED
2968 printf("\t--copy : used to test the internal copy implementation\n");
2969 #endif /* LIBXML_TREE_ENABLED */
2970 printf("\t--recover : output what was parsable on broken XML documents\n");
2971 printf("\t--huge : remove any internal arbitrary parser limits\n");
2972 printf("\t--noent : substitute entity references by their value\n");
2973 printf("\t--noout : don't output the result tree\n");
2974 printf("\t--path 'paths': provide a set of paths for resources\n");
2975 printf("\t--load-trace : print trace of all external entites loaded\n");
2976 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
2977 printf("\t--nocompact : do not generate compact text nodes\n");
2978 printf("\t--htmlout : output results as HTML\n");
2979 printf("\t--nowrap : do not put HTML doc wrapper\n");
2980 #ifdef LIBXML_VALID_ENABLED
2981 printf("\t--valid : validate the document in addition to std well-formed check\n");
2982 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
2983 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
2984 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
2985 #endif /* LIBXML_VALID_ENABLED */
2986 printf("\t--timing : print some timings\n");
2987 printf("\t--output file or -o file: save to a given file\n");
2988 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
2989 printf("\t--insert : ad-hoc test for valid insertions\n");
2990 #ifdef LIBXML_OUTPUT_ENABLED
2992 printf("\t--compress : turn on gzip compression of output\n");
2994 #endif /* LIBXML_OUTPUT_ENABLED */
2995 #ifdef LIBXML_HTML_ENABLED
2996 printf("\t--html : use the HTML parser\n");
2997 printf("\t--xmlout : force to use the XML serializer when using --html\n");
2999 #ifdef LIBXML_PUSH_ENABLED
3000 printf("\t--push : use the push mode of the parser\n");
3001 #endif /* LIBXML_PUSH_ENABLED */
3002 #ifdef HAVE_SYS_MMAN_H
3003 printf("\t--memory : parse from memory\n");
3005 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
3006 printf("\t--nowarning : do not emit warnings from parser/validator\n");
3007 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
3008 printf("\t--nocdata : replace cdata section with text nodes\n");
3009 #ifdef LIBXML_OUTPUT_ENABLED
3010 printf("\t--format : reformat/reindent the input\n");
3011 printf("\t--encode encoding : output in the given encoding\n");
3012 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
3013 #endif /* LIBXML_OUTPUT_ENABLED */
3014 printf("\t--c14n : save in W3C canonical format v1.0 (with comments)\n");
3015 printf("\t--c14n11 : save in W3C canonical format v1.1 (with comments)\n");
3016 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
3017 #ifdef LIBXML_C14N_ENABLED
3018 #endif /* LIBXML_C14N_ENABLED */
3019 printf("\t--nsclean : remove redundant namespace declarations\n");
3020 printf("\t--testIO : test user I/O support\n");
3021 #ifdef LIBXML_CATALOG_ENABLED
3022 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
3023 printf("\t otherwise XML Catalogs starting from \n");
3024 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG
);
3025 printf("\t--nocatalogs: deactivate all catalogs\n");
3027 printf("\t--auto : generate a small doc on the fly\n");
3028 #ifdef LIBXML_XINCLUDE_ENABLED
3029 printf("\t--xinclude : do XInclude processing\n");
3030 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
3031 printf("\t--nofixup-base-uris : do not fixup xml:base uris\n");
3033 printf("\t--loaddtd : fetch external DTD\n");
3034 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
3035 #ifdef LIBXML_READER_ENABLED
3036 printf("\t--stream : use the streaming interface to process very large files\n");
3037 printf("\t--walker : create a reader and walk though the resulting doc\n");
3038 #endif /* LIBXML_READER_ENABLED */
3039 #ifdef LIBXML_PATTERN_ENABLED
3040 printf("\t--pattern pattern_value : test the pattern support\n");
3042 printf("\t--chkregister : verify the node registration code\n");
3043 #ifdef LIBXML_SCHEMAS_ENABLED
3044 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
3045 printf("\t--schema schema : do validation against the WXS schema\n");
3047 #ifdef LIBXML_SCHEMATRON_ENABLED
3048 printf("\t--schematron schema : do validation against a schematron\n");
3050 #ifdef LIBXML_SAX1_ENABLED
3051 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
3053 printf("\t--sax: do not build a tree but work just at the SAX level\n");
3054 printf("\t--oldxml10: use XML-1.0 parsing rules before the 5th edition\n");
3055 #ifdef LIBXML_XPATH_ENABLED
3056 printf("\t--xpath expr: evaluate the XPath expression, inply --noout\n");
3059 printf("\nLibxml project home page: http://xmlsoft.org/\n");
3060 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
3063 static void registerNode(xmlNodePtr node
)
3065 node
->_private
= malloc(sizeof(long));
3066 *(long*)node
->_private
= (long) 0x81726354;
3070 static void deregisterNode(xmlNodePtr node
)
3072 assert(node
->_private
!= NULL
);
3073 assert(*(long*)node
->_private
== (long) 0x81726354);
3074 free(node
->_private
);
3079 main(int argc
, char **argv
) {
3090 for (i
= 1; i
< argc
; i
++) {
3091 if (!strcmp(argv
[i
], "-"))
3094 if (argv
[i
][0] != '-')
3096 if ((!strcmp(argv
[i
], "-debug")) || (!strcmp(argv
[i
], "--debug")))
3099 #ifdef LIBXML_DEBUG_ENABLED
3100 if ((!strcmp(argv
[i
], "-shell")) ||
3101 (!strcmp(argv
[i
], "--shell"))) {
3106 #ifdef LIBXML_TREE_ENABLED
3107 if ((!strcmp(argv
[i
], "-copy")) || (!strcmp(argv
[i
], "--copy")))
3110 #endif /* LIBXML_TREE_ENABLED */
3111 if ((!strcmp(argv
[i
], "-recover")) ||
3112 (!strcmp(argv
[i
], "--recover"))) {
3114 options
|= XML_PARSE_RECOVER
;
3115 } else if ((!strcmp(argv
[i
], "-huge")) ||
3116 (!strcmp(argv
[i
], "--huge"))) {
3117 options
|= XML_PARSE_HUGE
;
3118 } else if ((!strcmp(argv
[i
], "-noent")) ||
3119 (!strcmp(argv
[i
], "--noent"))) {
3121 options
|= XML_PARSE_NOENT
;
3122 } else if ((!strcmp(argv
[i
], "-nsclean")) ||
3123 (!strcmp(argv
[i
], "--nsclean"))) {
3124 options
|= XML_PARSE_NSCLEAN
;
3125 } else if ((!strcmp(argv
[i
], "-nocdata")) ||
3126 (!strcmp(argv
[i
], "--nocdata"))) {
3127 options
|= XML_PARSE_NOCDATA
;
3128 } else if ((!strcmp(argv
[i
], "-nodict")) ||
3129 (!strcmp(argv
[i
], "--nodict"))) {
3130 options
|= XML_PARSE_NODICT
;
3131 } else if ((!strcmp(argv
[i
], "-version")) ||
3132 (!strcmp(argv
[i
], "--version"))) {
3133 showVersion(argv
[0]);
3135 } else if ((!strcmp(argv
[i
], "-noout")) ||
3136 (!strcmp(argv
[i
], "--noout")))
3138 #ifdef LIBXML_OUTPUT_ENABLED
3139 else if ((!strcmp(argv
[i
], "-o")) ||
3140 (!strcmp(argv
[i
], "-output")) ||
3141 (!strcmp(argv
[i
], "--output"))) {
3145 #endif /* LIBXML_OUTPUT_ENABLED */
3146 else if ((!strcmp(argv
[i
], "-htmlout")) ||
3147 (!strcmp(argv
[i
], "--htmlout")))
3149 else if ((!strcmp(argv
[i
], "-nowrap")) ||
3150 (!strcmp(argv
[i
], "--nowrap")))
3152 #ifdef LIBXML_HTML_ENABLED
3153 else if ((!strcmp(argv
[i
], "-html")) ||
3154 (!strcmp(argv
[i
], "--html"))) {
3157 else if ((!strcmp(argv
[i
], "-xmlout")) ||
3158 (!strcmp(argv
[i
], "--xmlout"))) {
3161 #endif /* LIBXML_HTML_ENABLED */
3162 else if ((!strcmp(argv
[i
], "-loaddtd")) ||
3163 (!strcmp(argv
[i
], "--loaddtd"))) {
3165 options
|= XML_PARSE_DTDLOAD
;
3166 } else if ((!strcmp(argv
[i
], "-dtdattr")) ||
3167 (!strcmp(argv
[i
], "--dtdattr"))) {
3170 options
|= XML_PARSE_DTDATTR
;
3172 #ifdef LIBXML_VALID_ENABLED
3173 else if ((!strcmp(argv
[i
], "-valid")) ||
3174 (!strcmp(argv
[i
], "--valid"))) {
3176 options
|= XML_PARSE_DTDVALID
;
3177 } else if ((!strcmp(argv
[i
], "-postvalid")) ||
3178 (!strcmp(argv
[i
], "--postvalid"))) {
3181 options
|= XML_PARSE_DTDLOAD
;
3182 } else if ((!strcmp(argv
[i
], "-dtdvalid")) ||
3183 (!strcmp(argv
[i
], "--dtdvalid"))) {
3187 options
|= XML_PARSE_DTDLOAD
;
3188 } else if ((!strcmp(argv
[i
], "-dtdvalidfpi")) ||
3189 (!strcmp(argv
[i
], "--dtdvalidfpi"))) {
3191 dtdvalidfpi
= argv
[i
];
3193 options
|= XML_PARSE_DTDLOAD
;
3195 #endif /* LIBXML_VALID_ENABLED */
3196 else if ((!strcmp(argv
[i
], "-dropdtd")) ||
3197 (!strcmp(argv
[i
], "--dropdtd")))
3199 else if ((!strcmp(argv
[i
], "-insert")) ||
3200 (!strcmp(argv
[i
], "--insert")))
3202 else if ((!strcmp(argv
[i
], "-timing")) ||
3203 (!strcmp(argv
[i
], "--timing")))
3205 else if ((!strcmp(argv
[i
], "-auto")) ||
3206 (!strcmp(argv
[i
], "--auto")))
3208 else if ((!strcmp(argv
[i
], "-repeat")) ||
3209 (!strcmp(argv
[i
], "--repeat"))) {
3215 #ifdef LIBXML_PUSH_ENABLED
3216 else if ((!strcmp(argv
[i
], "-push")) ||
3217 (!strcmp(argv
[i
], "--push")))
3219 #endif /* LIBXML_PUSH_ENABLED */
3220 #ifdef HAVE_SYS_MMAN_H
3221 else if ((!strcmp(argv
[i
], "-memory")) ||
3222 (!strcmp(argv
[i
], "--memory")))
3225 else if ((!strcmp(argv
[i
], "-testIO")) ||
3226 (!strcmp(argv
[i
], "--testIO")))
3228 #ifdef LIBXML_XINCLUDE_ENABLED
3229 else if ((!strcmp(argv
[i
], "-xinclude")) ||
3230 (!strcmp(argv
[i
], "--xinclude"))) {
3232 options
|= XML_PARSE_XINCLUDE
;
3234 else if ((!strcmp(argv
[i
], "-noxincludenode")) ||
3235 (!strcmp(argv
[i
], "--noxincludenode"))) {
3237 options
|= XML_PARSE_XINCLUDE
;
3238 options
|= XML_PARSE_NOXINCNODE
;
3240 else if ((!strcmp(argv
[i
], "-nofixup-base-uris")) ||
3241 (!strcmp(argv
[i
], "--nofixup-base-uris"))) {
3243 options
|= XML_PARSE_XINCLUDE
;
3244 options
|= XML_PARSE_NOBASEFIX
;
3247 #ifdef LIBXML_OUTPUT_ENABLED
3249 else if ((!strcmp(argv
[i
], "-compress")) ||
3250 (!strcmp(argv
[i
], "--compress"))) {
3252 xmlSetCompressMode(9);
3255 #endif /* LIBXML_OUTPUT_ENABLED */
3256 else if ((!strcmp(argv
[i
], "-nowarning")) ||
3257 (!strcmp(argv
[i
], "--nowarning"))) {
3258 xmlGetWarningsDefaultValue
= 0;
3259 xmlPedanticParserDefault(0);
3260 options
|= XML_PARSE_NOWARNING
;
3262 else if ((!strcmp(argv
[i
], "-pedantic")) ||
3263 (!strcmp(argv
[i
], "--pedantic"))) {
3264 xmlGetWarningsDefaultValue
= 1;
3265 xmlPedanticParserDefault(1);
3266 options
|= XML_PARSE_PEDANTIC
;
3268 #ifdef LIBXML_DEBUG_ENABLED
3269 else if ((!strcmp(argv
[i
], "-debugent")) ||
3270 (!strcmp(argv
[i
], "--debugent"))) {
3272 xmlParserDebugEntities
= 1;
3275 #ifdef LIBXML_C14N_ENABLED
3276 else if ((!strcmp(argv
[i
], "-c14n")) ||
3277 (!strcmp(argv
[i
], "--c14n"))) {
3279 options
|= XML_PARSE_NOENT
| XML_PARSE_DTDATTR
| XML_PARSE_DTDLOAD
;
3281 else if ((!strcmp(argv
[i
], "-c14n11")) ||
3282 (!strcmp(argv
[i
], "--c14n11"))) {
3284 options
|= XML_PARSE_NOENT
| XML_PARSE_DTDATTR
| XML_PARSE_DTDLOAD
;
3286 else if ((!strcmp(argv
[i
], "-exc-c14n")) ||
3287 (!strcmp(argv
[i
], "--exc-c14n"))) {
3289 options
|= XML_PARSE_NOENT
| XML_PARSE_DTDATTR
| XML_PARSE_DTDLOAD
;
3292 #ifdef LIBXML_CATALOG_ENABLED
3293 else if ((!strcmp(argv
[i
], "-catalogs")) ||
3294 (!strcmp(argv
[i
], "--catalogs"))) {
3296 } else if ((!strcmp(argv
[i
], "-nocatalogs")) ||
3297 (!strcmp(argv
[i
], "--nocatalogs"))) {
3301 else if ((!strcmp(argv
[i
], "-encode")) ||
3302 (!strcmp(argv
[i
], "--encode"))) {
3306 * OK it's for testing purposes
3308 xmlAddEncodingAlias("UTF-8", "DVEnc");
3310 else if ((!strcmp(argv
[i
], "-noblanks")) ||
3311 (!strcmp(argv
[i
], "--noblanks"))) {
3313 xmlKeepBlanksDefault(0);
3315 else if ((!strcmp(argv
[i
], "-maxmem")) ||
3316 (!strcmp(argv
[i
], "--maxmem"))) {
3318 if (sscanf(argv
[i
], "%d", &maxmem
) == 1) {
3319 xmlMemSetup(myFreeFunc
, myMallocFunc
, myReallocFunc
,
3325 else if ((!strcmp(argv
[i
], "-format")) ||
3326 (!strcmp(argv
[i
], "--format"))) {
3328 #ifdef LIBXML_OUTPUT_ENABLED
3330 #endif /* LIBXML_OUTPUT_ENABLED */
3331 xmlKeepBlanksDefault(0);
3333 #ifdef LIBXML_READER_ENABLED
3334 else if ((!strcmp(argv
[i
], "-stream")) ||
3335 (!strcmp(argv
[i
], "--stream"))) {
3338 else if ((!strcmp(argv
[i
], "-walker")) ||
3339 (!strcmp(argv
[i
], "--walker"))) {
3343 #endif /* LIBXML_READER_ENABLED */
3344 #ifdef LIBXML_SAX1_ENABLED
3345 else if ((!strcmp(argv
[i
], "-sax1")) ||
3346 (!strcmp(argv
[i
], "--sax1"))) {
3348 options
|= XML_PARSE_SAX1
;
3350 #endif /* LIBXML_SAX1_ENABLED */
3351 else if ((!strcmp(argv
[i
], "-sax")) ||
3352 (!strcmp(argv
[i
], "--sax"))) {
3355 else if ((!strcmp(argv
[i
], "-chkregister")) ||
3356 (!strcmp(argv
[i
], "--chkregister"))) {
3358 #ifdef LIBXML_SCHEMAS_ENABLED
3359 } else if ((!strcmp(argv
[i
], "-relaxng")) ||
3360 (!strcmp(argv
[i
], "--relaxng"))) {
3364 options
|= XML_PARSE_NOENT
;
3365 } else if ((!strcmp(argv
[i
], "-schema")) ||
3366 (!strcmp(argv
[i
], "--schema"))) {
3371 #ifdef LIBXML_SCHEMATRON_ENABLED
3372 } else if ((!strcmp(argv
[i
], "-schematron")) ||
3373 (!strcmp(argv
[i
], "--schematron"))) {
3375 schematron
= argv
[i
];
3378 } else if ((!strcmp(argv
[i
], "-nonet")) ||
3379 (!strcmp(argv
[i
], "--nonet"))) {
3380 options
|= XML_PARSE_NONET
;
3381 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader
);
3382 } else if ((!strcmp(argv
[i
], "-nocompact")) ||
3383 (!strcmp(argv
[i
], "--nocompact"))) {
3384 options
&= ~XML_PARSE_COMPACT
;
3385 } else if ((!strcmp(argv
[i
], "-load-trace")) ||
3386 (!strcmp(argv
[i
], "--load-trace"))) {
3388 } else if ((!strcmp(argv
[i
], "-path")) ||
3389 (!strcmp(argv
[i
], "--path"))) {
3391 parsePath(BAD_CAST argv
[i
]);
3392 #ifdef LIBXML_PATTERN_ENABLED
3393 } else if ((!strcmp(argv
[i
], "-pattern")) ||
3394 (!strcmp(argv
[i
], "--pattern"))) {
3398 #ifdef LIBXML_XPATH_ENABLED
3399 } else if ((!strcmp(argv
[i
], "-xpath")) ||
3400 (!strcmp(argv
[i
], "--xpath"))) {
3403 xpathquery
= argv
[i
];
3405 } else if ((!strcmp(argv
[i
], "-oldxml10")) ||
3406 (!strcmp(argv
[i
], "--oldxml10"))) {
3408 options
|= XML_PARSE_OLD10
;
3410 fprintf(stderr
, "Unknown option %s\n", argv
[i
]);
3416 #ifdef LIBXML_CATALOG_ENABLED
3417 if (nocatalogs
== 0) {
3421 catal
= getenv("SGML_CATALOG_FILES");
3422 if (catal
!= NULL
) {
3423 xmlLoadCatalogs(catal
);
3425 fprintf(stderr
, "Variable $SGML_CATALOG_FILES not set\n");
3431 #ifdef LIBXML_SAX1_ENABLED
3433 xmlSAXDefaultVersion(1);
3435 xmlSAXDefaultVersion(2);
3436 #endif /* LIBXML_SAX1_ENABLED */
3439 xmlRegisterNodeDefault(registerNode
);
3440 xmlDeregisterNodeDefault(deregisterNode
);
3443 indent
= getenv("XMLLINT_INDENT");
3444 if(indent
!= NULL
) {
3445 xmlTreeIndentString
= indent
;
3449 defaultEntityLoader
= xmlGetExternalEntityLoader();
3450 xmlSetExternalEntityLoader(xmllintExternalEntityLoader
);
3452 xmlLineNumbersDefault(1);
3454 xmlLoadExtDtdDefaultValue
|= XML_DETECT_IDS
;
3456 xmlLoadExtDtdDefaultValue
|= XML_COMPLETE_ATTRS
;
3457 if (noent
!= 0) xmlSubstituteEntitiesDefault(1);
3458 #ifdef LIBXML_VALID_ENABLED
3459 if (valid
!= 0) xmlDoValidityCheckingDefaultValue
= 1;
3460 #endif /* LIBXML_VALID_ENABLED */
3461 if ((htmlout
) && (!nowrap
)) {
3462 xmlGenericError(xmlGenericErrorContext
,
3463 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
3464 xmlGenericError(xmlGenericErrorContext
,
3465 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3466 xmlGenericError(xmlGenericErrorContext
,
3467 "<html><head><title>%s output</title></head>\n",
3469 xmlGenericError(xmlGenericErrorContext
,
3470 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3474 #ifdef LIBXML_SCHEMATRON_ENABLED
3475 if ((schematron
!= NULL
) && (sax
== 0)
3476 #ifdef LIBXML_READER_ENABLED
3478 #endif /* LIBXML_READER_ENABLED */
3480 xmlSchematronParserCtxtPtr ctxt
;
3482 /* forces loading the DTDs */
3483 xmlLoadExtDtdDefaultValue
|= 1;
3484 options
|= XML_PARSE_DTDLOAD
;
3488 ctxt
= xmlSchematronNewParserCtxt(schematron
);
3490 xmlSchematronSetParserErrors(ctxt
,
3491 (xmlSchematronValidityErrorFunc
) fprintf
,
3492 (xmlSchematronValidityWarningFunc
) fprintf
,
3495 wxschematron
= xmlSchematronParse(ctxt
);
3496 if (wxschematron
== NULL
) {
3497 xmlGenericError(xmlGenericErrorContext
,
3498 "Schematron schema %s failed to compile\n", schematron
);
3499 progresult
= XMLLINT_ERR_SCHEMACOMP
;
3502 xmlSchematronFreeParserCtxt(ctxt
);
3504 endTimer("Compiling the schemas");
3508 #ifdef LIBXML_SCHEMAS_ENABLED
3509 if ((relaxng
!= NULL
) && (sax
== 0)
3510 #ifdef LIBXML_READER_ENABLED
3512 #endif /* LIBXML_READER_ENABLED */
3514 xmlRelaxNGParserCtxtPtr ctxt
;
3516 /* forces loading the DTDs */
3517 xmlLoadExtDtdDefaultValue
|= 1;
3518 options
|= XML_PARSE_DTDLOAD
;
3522 ctxt
= xmlRelaxNGNewParserCtxt(relaxng
);
3523 xmlRelaxNGSetParserErrors(ctxt
,
3524 (xmlRelaxNGValidityErrorFunc
) fprintf
,
3525 (xmlRelaxNGValidityWarningFunc
) fprintf
,
3527 relaxngschemas
= xmlRelaxNGParse(ctxt
);
3528 if (relaxngschemas
== NULL
) {
3529 xmlGenericError(xmlGenericErrorContext
,
3530 "Relax-NG schema %s failed to compile\n", relaxng
);
3531 progresult
= XMLLINT_ERR_SCHEMACOMP
;
3534 xmlRelaxNGFreeParserCtxt(ctxt
);
3536 endTimer("Compiling the schemas");
3538 } else if ((schema
!= NULL
)
3539 #ifdef LIBXML_READER_ENABLED
3543 xmlSchemaParserCtxtPtr ctxt
;
3548 ctxt
= xmlSchemaNewParserCtxt(schema
);
3549 xmlSchemaSetParserErrors(ctxt
,
3550 (xmlSchemaValidityErrorFunc
) fprintf
,
3551 (xmlSchemaValidityWarningFunc
) fprintf
,
3553 wxschemas
= xmlSchemaParse(ctxt
);
3554 if (wxschemas
== NULL
) {
3555 xmlGenericError(xmlGenericErrorContext
,
3556 "WXS schema %s failed to compile\n", schema
);
3557 progresult
= XMLLINT_ERR_SCHEMACOMP
;
3560 xmlSchemaFreeParserCtxt(ctxt
);
3562 endTimer("Compiling the schemas");
3565 #endif /* LIBXML_SCHEMAS_ENABLED */
3566 #ifdef LIBXML_PATTERN_ENABLED
3567 if ((pattern
!= NULL
)
3568 #ifdef LIBXML_READER_ENABLED
3572 patternc
= xmlPatterncompile((const xmlChar
*) pattern
, NULL
, 0, NULL
);
3573 if (patternc
== NULL
) {
3574 xmlGenericError(xmlGenericErrorContext
,
3575 "Pattern %s failed to compile\n", pattern
);
3576 progresult
= XMLLINT_ERR_SCHEMAPAT
;
3580 #endif /* LIBXML_PATTERN_ENABLED */
3581 for (i
= 1; i
< argc
; i
++) {
3582 if ((!strcmp(argv
[i
], "-encode")) ||
3583 (!strcmp(argv
[i
], "--encode"))) {
3586 } else if ((!strcmp(argv
[i
], "-o")) ||
3587 (!strcmp(argv
[i
], "-output")) ||
3588 (!strcmp(argv
[i
], "--output"))) {
3592 #ifdef LIBXML_VALID_ENABLED
3593 if ((!strcmp(argv
[i
], "-dtdvalid")) ||
3594 (!strcmp(argv
[i
], "--dtdvalid"))) {
3598 if ((!strcmp(argv
[i
], "-path")) ||
3599 (!strcmp(argv
[i
], "--path"))) {
3603 if ((!strcmp(argv
[i
], "-dtdvalidfpi")) ||
3604 (!strcmp(argv
[i
], "--dtdvalidfpi"))) {
3608 #endif /* LIBXML_VALID_ENABLED */
3609 if ((!strcmp(argv
[i
], "-relaxng")) ||
3610 (!strcmp(argv
[i
], "--relaxng"))) {
3614 if ((!strcmp(argv
[i
], "-maxmem")) ||
3615 (!strcmp(argv
[i
], "--maxmem"))) {
3619 if ((!strcmp(argv
[i
], "-schema")) ||
3620 (!strcmp(argv
[i
], "--schema"))) {
3624 if ((!strcmp(argv
[i
], "-schematron")) ||
3625 (!strcmp(argv
[i
], "--schematron"))) {
3629 #ifdef LIBXML_PATTERN_ENABLED
3630 if ((!strcmp(argv
[i
], "-pattern")) ||
3631 (!strcmp(argv
[i
], "--pattern"))) {
3636 #ifdef LIBXML_XPATH_ENABLED
3637 if ((!strcmp(argv
[i
], "-xpath")) ||
3638 (!strcmp(argv
[i
], "--xpath"))) {
3643 if ((timing
) && (repeat
))
3645 /* Remember file names. "-" means stdin. <sven@zen.org> */
3646 if ((argv
[i
][0] != '-') || (strcmp(argv
[i
], "-") == 0)) {
3648 xmlParserCtxtPtr ctxt
= NULL
;
3650 for (acount
= 0;acount
< repeat
;acount
++) {
3651 #ifdef LIBXML_READER_ENABLED
3653 streamFile(argv
[i
]);
3655 #endif /* LIBXML_READER_ENABLED */
3660 ctxt
= xmlNewParserCtxt();
3661 parseAndPrintFile(argv
[i
], ctxt
);
3663 #ifdef LIBXML_READER_ENABLED
3665 #endif /* LIBXML_READER_ENABLED */
3668 xmlFreeParserCtxt(ctxt
);
3672 #ifdef LIBXML_READER_ENABLED
3674 streamFile(argv
[i
]);
3676 #endif /* LIBXML_READER_ENABLED */
3680 parseAndPrintFile(argv
[i
], NULL
);
3683 if ((chkregister
) && (nbregister
!= 0)) {
3684 fprintf(stderr
, "Registration count off: %d\n", nbregister
);
3685 progresult
= XMLLINT_ERR_RDREGIS
;
3689 if ((timing
) && (repeat
)) {
3690 endTimer("%d iterations", repeat
);
3695 parseAndPrintFile(NULL
, NULL
);
3696 if ((htmlout
) && (!nowrap
)) {
3697 xmlGenericError(xmlGenericErrorContext
, "</body></html>\n");
3699 if ((files
== 0) && (!generate
) && (version
== 0)) {
3702 #ifdef LIBXML_SCHEMATRON_ENABLED
3703 if (wxschematron
!= NULL
)
3704 xmlSchematronFree(wxschematron
);
3706 #ifdef LIBXML_SCHEMAS_ENABLED
3707 if (relaxngschemas
!= NULL
)
3708 xmlRelaxNGFree(relaxngschemas
);
3709 if (wxschemas
!= NULL
)
3710 xmlSchemaFree(wxschemas
);
3711 xmlRelaxNGCleanupTypes();
3713 #ifdef LIBXML_PATTERN_ENABLED
3714 if (patternc
!= NULL
)
3715 xmlFreePattern(patternc
);