VirtualBox

source: vbox/trunk/src/libs/libxml2-2.6.30/xmllint.c@ 28837

Last change on this file since 28837 was 6076, checked in by vboxsync, 17 years ago

Merged dmik/s2 branch (r25959:26751) to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Revision Author Id
File size: 89.6 KB
Line 
1/*
2 * xmllint.c : a small tester program for XML input.
3 *
4 * See Copyright for the status of this software.
5 *
6 * daniel@veillard.com
7 */
8
9#include "libxml.h"
10
11#include <string.h>
12#include <stdarg.h>
13#include <assert.h>
14
15#if defined (_WIN32) && !defined(__CYGWIN__)
16#if defined (_MSC_VER) || defined(__BORLANDC__)
17#include <winsock2.h>
18#pragma comment(lib, "ws2_32.lib")
19#define gettimeofday(p1,p2)
20#endif /* _MSC_VER */
21#endif /* _WIN32 */
22
23#ifdef HAVE_SYS_TIME_H
24#include <sys/time.h>
25#endif
26#ifdef HAVE_TIME_H
27#include <time.h>
28#endif
29
30#ifdef __MINGW32__
31#define _WINSOCKAPI_
32#include <wsockcompat.h>
33#include <winsock2.h>
34#undef XML_SOCKLEN_T
35#define XML_SOCKLEN_T unsigned int
36#endif
37
38#ifdef HAVE_SYS_TIMEB_H
39#include <sys/timeb.h>
40#endif
41
42#ifdef HAVE_SYS_TYPES_H
43#include <sys/types.h>
44#endif
45#ifdef HAVE_SYS_STAT_H
46#include <sys/stat.h>
47#endif
48#ifdef HAVE_FCNTL_H
49#include <fcntl.h>
50#endif
51#ifdef HAVE_UNISTD_H
52#include <unistd.h>
53#endif
54#ifdef HAVE_SYS_MMAN_H
55#include <sys/mman.h>
56/* seems needed for Solaris */
57#ifndef MAP_FAILED
58#define MAP_FAILED ((void *) -1)
59#endif
60#endif
61#ifdef HAVE_STDLIB_H
62#include <stdlib.h>
63#endif
64#ifdef HAVE_LIBREADLINE
65#include <readline/readline.h>
66#ifdef HAVE_LIBHISTORY
67#include <readline/history.h>
68#endif
69#endif
70
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>
82#endif
83#ifdef LIBXML_CATALOG_ENABLED
84#include <libxml/catalog.h>
85#endif
86#include <libxml/globals.h>
87#include <libxml/xmlreader.h>
88#ifdef LIBXML_SCHEMATRON_ENABLED
89#include <libxml/schematron.h>
90#endif
91#ifdef LIBXML_SCHEMAS_ENABLED
92#include <libxml/relaxng.h>
93#include <libxml/xmlschemas.h>
94#endif
95#ifdef LIBXML_PATTERN_ENABLED
96#include <libxml/pattern.h>
97#endif
98#ifdef LIBXML_C14N_ENABLED
99#include <libxml/c14n.h>
100#endif
101#ifdef LIBXML_OUTPUT_ENABLED
102#include <libxml/xmlsave.h>
103#endif
104
105#ifndef XML_XML_DEFAULT_CATALOG
106#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
107#endif
108
109typedef enum {
110 XMLLINT_RETURN_OK = 0, /* No error */
111 XMLLINT_ERR_UNCLASS, /* Unclassified */
112 XMLLINT_ERR_DTD, /* Error in DTD */
113 XMLLINT_ERR_VALID, /* Validation error */
114 XMLLINT_ERR_RDFILE, /* CtxtReadFile error */
115 XMLLINT_ERR_SCHEMACOMP, /* Schema compilation */
116 XMLLINT_ERR_OUT, /* Error writing output */
117 XMLLINT_ERR_SCHEMAPAT, /* Error in schema pattern */
118 XMLLINT_ERR_RDREGIS, /* Error in Reader registration */
119 XMLLINT_ERR_MEM /* Out of memory error */
120} xmllintReturnCode;
121#ifdef LIBXML_DEBUG_ENABLED
122static int shell = 0;
123static int debugent = 0;
124#endif
125static int debug = 0;
126static int maxmem = 0;
127#ifdef LIBXML_TREE_ENABLED
128static int copy = 0;
129#endif /* LIBXML_TREE_ENABLED */
130static int recovery = 0;
131static int noent = 0;
132static int noblanks = 0;
133static int noout = 0;
134static int nowrap = 0;
135#ifdef LIBXML_OUTPUT_ENABLED
136static int format = 0;
137static const char *output = NULL;
138static int compress = 0;
139static int oldout = 0;
140#endif /* LIBXML_OUTPUT_ENABLED */
141#ifdef LIBXML_VALID_ENABLED
142static int valid = 0;
143static int postvalid = 0;
144static char * dtdvalid = NULL;
145static char * dtdvalidfpi = NULL;
146#endif
147#ifdef LIBXML_SCHEMAS_ENABLED
148static char * relaxng = NULL;
149static xmlRelaxNGPtr relaxngschemas = NULL;
150static char * schema = NULL;
151static xmlSchemaPtr wxschemas = NULL;
152#endif
153#ifdef LIBXML_SCHEMATRON_ENABLED
154static char * schematron = NULL;
155static xmlSchematronPtr wxschematron = NULL;
156#endif
157static int repeat = 0;
158static int insert = 0;
159#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
160static int html = 0;
161static int xmlout = 0;
162#endif
163static int htmlout = 0;
164#ifdef LIBXML_PUSH_ENABLED
165static int push = 0;
166#endif /* LIBXML_PUSH_ENABLED */
167#ifdef HAVE_SYS_MMAN_H
168static int memory = 0;
169#endif
170static int testIO = 0;
171static char *encoding = NULL;
172#ifdef LIBXML_XINCLUDE_ENABLED
173static int xinclude = 0;
174#endif
175static int dtdattrs = 0;
176static int loaddtd = 0;
177static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
178static int timing = 0;
179static int generate = 0;
180static int dropdtd = 0;
181#ifdef LIBXML_CATALOG_ENABLED
182static int catalogs = 0;
183static int nocatalogs = 0;
184#endif
185#ifdef LIBXML_C14N_ENABLED
186static int canonical = 0;
187static int exc_canonical = 0;
188#endif
189#ifdef LIBXML_READER_ENABLED
190static int stream = 0;
191static int walker = 0;
192#endif /* LIBXML_READER_ENABLED */
193static int chkregister = 0;
194static int nbregister = 0;
195#ifdef LIBXML_SAX1_ENABLED
196static int sax1 = 0;
197#endif /* LIBXML_SAX1_ENABLED */
198#ifdef LIBXML_PATTERN_ENABLED
199static const char *pattern = NULL;
200static xmlPatternPtr patternc = NULL;
201static xmlStreamCtxtPtr patstream = NULL;
202#endif
203static int options = XML_PARSE_COMPACT;
204static int sax = 0;
205
206/************************************************************************
207 * *
208 * Entity loading control and customization. *
209 * *
210 ************************************************************************/
211#define MAX_PATHS 64
212#ifdef _WIN32
213# define PATH_SEPARATOR ';'
214#else
215# define PATH_SEPARATOR ':'
216#endif
217static xmlChar *paths[MAX_PATHS + 1];
218static int nbpaths = 0;
219static int load_trace = 0;
220
221static
222void parsePath(const xmlChar *path) {
223 const xmlChar *cur;
224
225 if (path == NULL)
226 return;
227 while (*path != 0) {
228 if (nbpaths >= MAX_PATHS) {
229 fprintf(stderr, "MAX_PATHS reached: too many paths\n");
230 return;
231 }
232 cur = path;
233 while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
234 cur++;
235 path = cur;
236 while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
237 cur++;
238 if (cur != path) {
239 paths[nbpaths] = xmlStrndup(path, cur - path);
240 if (paths[nbpaths] != NULL)
241 nbpaths++;
242 path = cur;
243 }
244 }
245}
246
247static xmlExternalEntityLoader defaultEntityLoader = NULL;
248
249static xmlParserInputPtr
250xmllintExternalEntityLoader(const char *URL, const char *ID,
251 xmlParserCtxtPtr ctxt) {
252 xmlParserInputPtr ret;
253 warningSAXFunc warning = NULL;
254 errorSAXFunc err = NULL;
255
256 int i;
257 const char *lastsegment = URL;
258 const char *iter = URL;
259
260 if (nbpaths > 0) {
261 while (*iter != 0) {
262 if (*iter == '/')
263 lastsegment = iter + 1;
264 iter++;
265 }
266 }
267
268 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
269 warning = ctxt->sax->warning;
270 err = ctxt->sax->error;
271 ctxt->sax->warning = NULL;
272 ctxt->sax->error = NULL;
273 }
274
275 if (defaultEntityLoader != NULL) {
276 ret = defaultEntityLoader(URL, ID, ctxt);
277 if (ret != NULL) {
278 if (warning != NULL)
279 ctxt->sax->warning = warning;
280 if (err != NULL)
281 ctxt->sax->error = err;
282 if (load_trace) {
283 fprintf \
284 (stderr,
285 "Loaded URL=\"%s\" ID=\"%s\"\n",
286 URL ? URL : "(null)",
287 ID ? ID : "(null)");
288 }
289 return(ret);
290 }
291 }
292 for (i = 0;i < nbpaths;i++) {
293 xmlChar *newURL;
294
295 newURL = xmlStrdup((const xmlChar *) paths[i]);
296 newURL = xmlStrcat(newURL, (const xmlChar *) "/");
297 newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
298 if (newURL != NULL) {
299 ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
300 if (ret != NULL) {
301 if (warning != NULL)
302 ctxt->sax->warning = warning;
303 if (err != NULL)
304 ctxt->sax->error = err;
305 if (load_trace) {
306 fprintf \
307 (stderr,
308 "Loaded URL=\"%s\" ID=\"%s\"\n",
309 newURL,
310 ID ? ID : "(null)");
311 }
312 xmlFree(newURL);
313 return(ret);
314 }
315 xmlFree(newURL);
316 }
317 }
318 if (err != NULL)
319 ctxt->sax->error = err;
320 if (warning != NULL) {
321 ctxt->sax->warning = warning;
322 if (URL != NULL)
323 warning(ctxt, "failed to load external entity \"%s\"\n", URL);
324 else if (ID != NULL)
325 warning(ctxt, "failed to load external entity \"%s\"\n", ID);
326 }
327 return(NULL);
328}
329/************************************************************************
330 * *
331 * Memory allocation consumption debugging *
332 * *
333 ************************************************************************/
334
335static void
336OOM(void)
337{
338 fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
339 progresult = XMLLINT_ERR_MEM;
340}
341
342static void
343myFreeFunc(void *mem)
344{
345 xmlMemFree(mem);
346}
347static void *
348myMallocFunc(size_t size)
349{
350 void *ret;
351
352 ret = xmlMemMalloc(size);
353 if (ret != NULL) {
354 if (xmlMemUsed() > maxmem) {
355 OOM();
356 xmlMemFree(ret);
357 return (NULL);
358 }
359 }
360 return (ret);
361}
362static void *
363myReallocFunc(void *mem, size_t size)
364{
365 void *ret;
366
367 ret = xmlMemRealloc(mem, size);
368 if (ret != NULL) {
369 if (xmlMemUsed() > maxmem) {
370 OOM();
371 xmlMemFree(ret);
372 return (NULL);
373 }
374 }
375 return (ret);
376}
377static char *
378myStrdupFunc(const char *str)
379{
380 char *ret;
381
382 ret = xmlMemoryStrdup(str);
383 if (ret != NULL) {
384 if (xmlMemUsed() > maxmem) {
385 OOM();
386 xmlFree(ret);
387 return (NULL);
388 }
389 }
390 return (ret);
391}
392/************************************************************************
393 * *
394 * Internal timing routines to remove the necessity to have *
395 * unix-specific function calls. *
396 * *
397 ************************************************************************/
398
399#ifndef HAVE_GETTIMEOFDAY
400#ifdef HAVE_SYS_TIMEB_H
401#ifdef HAVE_SYS_TIME_H
402#ifdef HAVE_FTIME
403
404static int
405my_gettimeofday(struct timeval *tvp, void *tzp)
406{
407 struct timeb timebuffer;
408
409 ftime(&timebuffer);
410 if (tvp) {
411 tvp->tv_sec = timebuffer.time;
412 tvp->tv_usec = timebuffer.millitm * 1000L;
413 }
414 return (0);
415}
416#define HAVE_GETTIMEOFDAY 1
417#define gettimeofday my_gettimeofday
418
419#endif /* HAVE_FTIME */
420#endif /* HAVE_SYS_TIME_H */
421#endif /* HAVE_SYS_TIMEB_H */
422#endif /* !HAVE_GETTIMEOFDAY */
423
424#if defined(HAVE_GETTIMEOFDAY)
425static struct timeval begin, end;
426
427/*
428 * startTimer: call where you want to start timing
429 */
430static void
431startTimer(void)
432{
433 gettimeofday(&begin, NULL);
434}
435
436/*
437 * endTimer: call where you want to stop timing and to print out a
438 * message about the timing performed; format is a printf
439 * type argument
440 */
441static void XMLCDECL
442endTimer(const char *fmt, ...)
443{
444 long msec;
445 va_list ap;
446
447 gettimeofday(&end, NULL);
448 msec = end.tv_sec - begin.tv_sec;
449 msec *= 1000;
450 msec += (end.tv_usec - begin.tv_usec) / 1000;
451
452#ifndef HAVE_STDARG_H
453#error "endTimer required stdarg functions"
454#endif
455 va_start(ap, fmt);
456 vfprintf(stderr, fmt, ap);
457 va_end(ap);
458
459 fprintf(stderr, " took %ld ms\n", msec);
460}
461#elif defined(HAVE_TIME_H)
462/*
463 * No gettimeofday function, so we have to make do with calling clock.
464 * This is obviously less accurate, but there's little we can do about
465 * that.
466 */
467#ifndef CLOCKS_PER_SEC
468#define CLOCKS_PER_SEC 100
469#endif
470
471static clock_t begin, end;
472static void
473startTimer(void)
474{
475 begin = clock();
476}
477static void XMLCDECL
478endTimer(const char *fmt, ...)
479{
480 long msec;
481 va_list ap;
482
483 end = clock();
484 msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;
485
486#ifndef HAVE_STDARG_H
487#error "endTimer required stdarg functions"
488#endif
489 va_start(ap, fmt);
490 vfprintf(stderr, fmt, ap);
491 va_end(ap);
492 fprintf(stderr, " took %ld ms\n", msec);
493}
494#else
495
496/*
497 * We don't have a gettimeofday or time.h, so we just don't do timing
498 */
499static void
500startTimer(void)
501{
502 /*
503 * Do nothing
504 */
505}
506static void XMLCDECL
507endTimer(char *format, ...)
508{
509 /*
510 * We cannot do anything because we don't have a timing function
511 */
512#ifdef HAVE_STDARG_H
513 va_start(ap, format);
514 vfprintf(stderr, format, ap);
515 va_end(ap);
516 fprintf(stderr, " was not timed\n", msec);
517#else
518 /* We don't have gettimeofday, time or stdarg.h, what crazy world is
519 * this ?!
520 */
521#endif
522}
523#endif
524/************************************************************************
525 * *
526 * HTML ouput *
527 * *
528 ************************************************************************/
529static char buffer[50000];
530
531static void
532xmlHTMLEncodeSend(void) {
533 char *result;
534
535 result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
536 if (result) {
537 xmlGenericError(xmlGenericErrorContext, "%s", result);
538 xmlFree(result);
539 }
540 buffer[0] = 0;
541}
542
543/**
544 * xmlHTMLPrintFileInfo:
545 * @input: an xmlParserInputPtr input
546 *
547 * Displays the associated file and line informations for the current input
548 */
549
550static void
551xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
552 int len;
553 xmlGenericError(xmlGenericErrorContext, "<p>");
554
555 len = strlen(buffer);
556 if (input != NULL) {
557 if (input->filename) {
558 snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
559 input->line);
560 } else {
561 snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
562 }
563 }
564 xmlHTMLEncodeSend();
565}
566
567/**
568 * xmlHTMLPrintFileContext:
569 * @input: an xmlParserInputPtr input
570 *
571 * Displays current context within the input content for error tracking
572 */
573
574static void
575xmlHTMLPrintFileContext(xmlParserInputPtr input) {
576 const xmlChar *cur, *base;
577 int len;
578 int n;
579
580 if (input == NULL) return;
581 xmlGenericError(xmlGenericErrorContext, "<pre>\n");
582 cur = input->cur;
583 base = input->base;
584 while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
585 cur--;
586 }
587 n = 0;
588 while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
589 cur--;
590 if ((*cur == '\n') || (*cur == '\r')) cur++;
591 base = cur;
592 n = 0;
593 while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
594 len = strlen(buffer);
595 snprintf(&buffer[len], sizeof(buffer) - len, "%c",
596 (unsigned char) *cur++);
597 n++;
598 }
599 len = strlen(buffer);
600 snprintf(&buffer[len], sizeof(buffer) - len, "\n");
601 cur = input->cur;
602 while ((*cur == '\n') || (*cur == '\r'))
603 cur--;
604 n = 0;
605 while ((cur != base) && (n++ < 80)) {
606 len = strlen(buffer);
607 snprintf(&buffer[len], sizeof(buffer) - len, " ");
608 base++;
609 }
610 len = strlen(buffer);
611 snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
612 xmlHTMLEncodeSend();
613 xmlGenericError(xmlGenericErrorContext, "</pre>");
614}
615
616/**
617 * xmlHTMLError:
618 * @ctx: an XML parser context
619 * @msg: the message to display/transmit
620 * @...: extra parameters for the message display
621 *
622 * Display and format an error messages, gives file, line, position and
623 * extra parameters.
624 */
625static void XMLCDECL
626xmlHTMLError(void *ctx, const char *msg, ...)
627{
628 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
629 xmlParserInputPtr input;
630 va_list args;
631 int len;
632
633 buffer[0] = 0;
634 input = ctxt->input;
635 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
636 input = ctxt->inputTab[ctxt->inputNr - 2];
637 }
638
639 xmlHTMLPrintFileInfo(input);
640
641 xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
642 va_start(args, msg);
643 len = strlen(buffer);
644 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
645 va_end(args);
646 xmlHTMLEncodeSend();
647 xmlGenericError(xmlGenericErrorContext, "</p>\n");
648
649 xmlHTMLPrintFileContext(input);
650 xmlHTMLEncodeSend();
651}
652
653/**
654 * xmlHTMLWarning:
655 * @ctx: an XML parser context
656 * @msg: the message to display/transmit
657 * @...: extra parameters for the message display
658 *
659 * Display and format a warning messages, gives file, line, position and
660 * extra parameters.
661 */
662static void XMLCDECL
663xmlHTMLWarning(void *ctx, const char *msg, ...)
664{
665 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
666 xmlParserInputPtr input;
667 va_list args;
668 int len;
669
670 buffer[0] = 0;
671 input = ctxt->input;
672 if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
673 input = ctxt->inputTab[ctxt->inputNr - 2];
674 }
675
676
677 xmlHTMLPrintFileInfo(input);
678
679 xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
680 va_start(args, msg);
681 len = strlen(buffer);
682 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
683 va_end(args);
684 xmlHTMLEncodeSend();
685 xmlGenericError(xmlGenericErrorContext, "</p>\n");
686
687 xmlHTMLPrintFileContext(input);
688 xmlHTMLEncodeSend();
689}
690
691/**
692 * xmlHTMLValidityError:
693 * @ctx: an XML parser context
694 * @msg: the message to display/transmit
695 * @...: extra parameters for the message display
696 *
697 * Display and format an validity error messages, gives file,
698 * line, position and extra parameters.
699 */
700static void XMLCDECL
701xmlHTMLValidityError(void *ctx, const char *msg, ...)
702{
703 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
704 xmlParserInputPtr input;
705 va_list args;
706 int len;
707
708 buffer[0] = 0;
709 input = ctxt->input;
710 if ((input->filename == NULL) && (ctxt->inputNr > 1))
711 input = ctxt->inputTab[ctxt->inputNr - 2];
712
713 xmlHTMLPrintFileInfo(input);
714
715 xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
716 len = strlen(buffer);
717 va_start(args, msg);
718 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
719 va_end(args);
720 xmlHTMLEncodeSend();
721 xmlGenericError(xmlGenericErrorContext, "</p>\n");
722
723 xmlHTMLPrintFileContext(input);
724 xmlHTMLEncodeSend();
725 progresult = XMLLINT_ERR_VALID;
726}
727
728/**
729 * xmlHTMLValidityWarning:
730 * @ctx: an XML parser context
731 * @msg: the message to display/transmit
732 * @...: extra parameters for the message display
733 *
734 * Display and format a validity warning messages, gives file, line,
735 * position and extra parameters.
736 */
737static void XMLCDECL
738xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
739{
740 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
741 xmlParserInputPtr input;
742 va_list args;
743 int len;
744
745 buffer[0] = 0;
746 input = ctxt->input;
747 if ((input->filename == NULL) && (ctxt->inputNr > 1))
748 input = ctxt->inputTab[ctxt->inputNr - 2];
749
750 xmlHTMLPrintFileInfo(input);
751
752 xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
753 va_start(args, msg);
754 len = strlen(buffer);
755 vsnprintf(&buffer[len], sizeof(buffer) - len, msg, args);
756 va_end(args);
757 xmlHTMLEncodeSend();
758 xmlGenericError(xmlGenericErrorContext, "</p>\n");
759
760 xmlHTMLPrintFileContext(input);
761 xmlHTMLEncodeSend();
762}
763
764/************************************************************************
765 * *
766 * Shell Interface *
767 * *
768 ************************************************************************/
769#ifdef LIBXML_DEBUG_ENABLED
770#ifdef LIBXML_XPATH_ENABLED
771/**
772 * xmlShellReadline:
773 * @prompt: the prompt value
774 *
775 * Read a string
776 *
777 * Returns a pointer to it or NULL on EOF the caller is expected to
778 * free the returned string.
779 */
780static char *
781xmlShellReadline(char *prompt) {
782#ifdef HAVE_LIBREADLINE
783 char *line_read;
784
785 /* Get a line from the user. */
786 line_read = readline (prompt);
787
788 /* If the line has any text in it, save it on the history. */
789 if (line_read && *line_read)
790 add_history (line_read);
791
792 return (line_read);
793#else
794 char line_read[501];
795 char *ret;
796 int len;
797
798 if (prompt != NULL)
799 fprintf(stdout, "%s", prompt);
800 if (!fgets(line_read, 500, stdin))
801 return(NULL);
802 line_read[500] = 0;
803 len = strlen(line_read);
804 ret = (char *) malloc(len + 1);
805 if (ret != NULL) {
806 memcpy (ret, line_read, len + 1);
807 }
808 return(ret);
809#endif
810}
811#endif /* LIBXML_XPATH_ENABLED */
812#endif /* LIBXML_DEBUG_ENABLED */
813
814/************************************************************************
815 * *
816 * I/O Interfaces *
817 * *
818 ************************************************************************/
819
820static int myRead(FILE *f, char * buf, int len) {
821 return(fread(buf, 1, len, f));
822}
823static void myClose(FILE *f) {
824 if (f != stdin) {
825 fclose(f);
826 }
827}
828
829/************************************************************************
830 * *
831 * SAX based tests *
832 * *
833 ************************************************************************/
834
835/*
836 * empty SAX block
837 */
838static xmlSAXHandler emptySAXHandlerStruct = {
839 NULL, /* internalSubset */
840 NULL, /* isStandalone */
841 NULL, /* hasInternalSubset */
842 NULL, /* hasExternalSubset */
843 NULL, /* resolveEntity */
844 NULL, /* getEntity */
845 NULL, /* entityDecl */
846 NULL, /* notationDecl */
847 NULL, /* attributeDecl */
848 NULL, /* elementDecl */
849 NULL, /* unparsedEntityDecl */
850 NULL, /* setDocumentLocator */
851 NULL, /* startDocument */
852 NULL, /* endDocument */
853 NULL, /* startElement */
854 NULL, /* endElement */
855 NULL, /* reference */
856 NULL, /* characters */
857 NULL, /* ignorableWhitespace */
858 NULL, /* processingInstruction */
859 NULL, /* comment */
860 NULL, /* xmlParserWarning */
861 NULL, /* xmlParserError */
862 NULL, /* xmlParserError */
863 NULL, /* getParameterEntity */
864 NULL, /* cdataBlock; */
865 NULL, /* externalSubset; */
866 XML_SAX2_MAGIC,
867 NULL,
868 NULL, /* startElementNs */
869 NULL, /* endElementNs */
870 NULL /* xmlStructuredErrorFunc */
871};
872
873static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
874extern xmlSAXHandlerPtr debugSAXHandler;
875static int callbacks;
876
877/**
878 * isStandaloneDebug:
879 * @ctxt: An XML parser context
880 *
881 * Is this document tagged standalone ?
882 *
883 * Returns 1 if true
884 */
885static int
886isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
887{
888 callbacks++;
889 if (noout)
890 return(0);
891 fprintf(stdout, "SAX.isStandalone()\n");
892 return(0);
893}
894
895/**
896 * hasInternalSubsetDebug:
897 * @ctxt: An XML parser context
898 *
899 * Does this document has an internal subset
900 *
901 * Returns 1 if true
902 */
903static int
904hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
905{
906 callbacks++;
907 if (noout)
908 return(0);
909 fprintf(stdout, "SAX.hasInternalSubset()\n");
910 return(0);
911}
912
913/**
914 * hasExternalSubsetDebug:
915 * @ctxt: An XML parser context
916 *
917 * Does this document has an external subset
918 *
919 * Returns 1 if true
920 */
921static int
922hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
923{
924 callbacks++;
925 if (noout)
926 return(0);
927 fprintf(stdout, "SAX.hasExternalSubset()\n");
928 return(0);
929}
930
931/**
932 * internalSubsetDebug:
933 * @ctxt: An XML parser context
934 *
935 * Does this document has an internal subset
936 */
937static void
938internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
939 const xmlChar *ExternalID, const xmlChar *SystemID)
940{
941 callbacks++;
942 if (noout)
943 return;
944 fprintf(stdout, "SAX.internalSubset(%s,", name);
945 if (ExternalID == NULL)
946 fprintf(stdout, " ,");
947 else
948 fprintf(stdout, " %s,", ExternalID);
949 if (SystemID == NULL)
950 fprintf(stdout, " )\n");
951 else
952 fprintf(stdout, " %s)\n", SystemID);
953}
954
955/**
956 * externalSubsetDebug:
957 * @ctxt: An XML parser context
958 *
959 * Does this document has an external subset
960 */
961static void
962externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
963 const xmlChar *ExternalID, const xmlChar *SystemID)
964{
965 callbacks++;
966 if (noout)
967 return;
968 fprintf(stdout, "SAX.externalSubset(%s,", name);
969 if (ExternalID == NULL)
970 fprintf(stdout, " ,");
971 else
972 fprintf(stdout, " %s,", ExternalID);
973 if (SystemID == NULL)
974 fprintf(stdout, " )\n");
975 else
976 fprintf(stdout, " %s)\n", SystemID);
977}
978
979/**
980 * resolveEntityDebug:
981 * @ctxt: An XML parser context
982 * @publicId: The public ID of the entity
983 * @systemId: The system ID of the entity
984 *
985 * Special entity resolver, better left to the parser, it has
986 * more context than the application layer.
987 * The default behaviour is to NOT resolve the entities, in that case
988 * the ENTITY_REF nodes are built in the structure (and the parameter
989 * values).
990 *
991 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
992 */
993static xmlParserInputPtr
994resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
995{
996 callbacks++;
997 if (noout)
998 return(NULL);
999 /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
1000
1001
1002 fprintf(stdout, "SAX.resolveEntity(");
1003 if (publicId != NULL)
1004 fprintf(stdout, "%s", (char *)publicId);
1005 else
1006 fprintf(stdout, " ");
1007 if (systemId != NULL)
1008 fprintf(stdout, ", %s)\n", (char *)systemId);
1009 else
1010 fprintf(stdout, ", )\n");
1011 return(NULL);
1012}
1013
1014/**
1015 * getEntityDebug:
1016 * @ctxt: An XML parser context
1017 * @name: The entity name
1018 *
1019 * Get an entity by name
1020 *
1021 * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
1022 */
1023static xmlEntityPtr
1024getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1025{
1026 callbacks++;
1027 if (noout)
1028 return(NULL);
1029 fprintf(stdout, "SAX.getEntity(%s)\n", name);
1030 return(NULL);
1031}
1032
1033/**
1034 * getParameterEntityDebug:
1035 * @ctxt: An XML parser context
1036 * @name: The entity name
1037 *
1038 * Get a parameter entity by name
1039 *
1040 * Returns the xmlParserInputPtr
1041 */
1042static xmlEntityPtr
1043getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1044{
1045 callbacks++;
1046 if (noout)
1047 return(NULL);
1048 fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
1049 return(NULL);
1050}
1051
1052
1053/**
1054 * entityDeclDebug:
1055 * @ctxt: An XML parser context
1056 * @name: the entity name
1057 * @type: the entity type
1058 * @publicId: The public ID of the entity
1059 * @systemId: The system ID of the entity
1060 * @content: the entity value (without processing).
1061 *
1062 * An entity definition has been parsed
1063 */
1064static void
1065entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1066 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
1067{
1068const xmlChar *nullstr = BAD_CAST "(null)";
1069 /* not all libraries handle printing null pointers nicely */
1070 if (publicId == NULL)
1071 publicId = nullstr;
1072 if (systemId == NULL)
1073 systemId = nullstr;
1074 if (content == NULL)
1075 content = (xmlChar *)nullstr;
1076 callbacks++;
1077 if (noout)
1078 return;
1079 fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
1080 name, type, publicId, systemId, content);
1081}
1082
1083/**
1084 * attributeDeclDebug:
1085 * @ctxt: An XML parser context
1086 * @name: the attribute name
1087 * @type: the attribute type
1088 *
1089 * An attribute definition has been parsed
1090 */
1091static void
1092attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
1093 const xmlChar * name, int type, int def,
1094 const xmlChar * defaultValue, xmlEnumerationPtr tree)
1095{
1096 callbacks++;
1097 if (noout)
1098 return;
1099 if (defaultValue == NULL)
1100 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
1101 elem, name, type, def);
1102 else
1103 fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
1104 elem, name, type, def, defaultValue);
1105 xmlFreeEnumeration(tree);
1106}
1107
1108/**
1109 * elementDeclDebug:
1110 * @ctxt: An XML parser context
1111 * @name: the element name
1112 * @type: the element type
1113 * @content: the element value (without processing).
1114 *
1115 * An element definition has been parsed
1116 */
1117static void
1118elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
1119 xmlElementContentPtr content ATTRIBUTE_UNUSED)
1120{
1121 callbacks++;
1122 if (noout)
1123 return;
1124 fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
1125 name, type);
1126}
1127
1128/**
1129 * notationDeclDebug:
1130 * @ctxt: An XML parser context
1131 * @name: The name of the notation
1132 * @publicId: The public ID of the entity
1133 * @systemId: The system ID of the entity
1134 *
1135 * What to do when a notation declaration has been parsed.
1136 */
1137static void
1138notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1139 const xmlChar *publicId, const xmlChar *systemId)
1140{
1141 callbacks++;
1142 if (noout)
1143 return;
1144 fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
1145 (char *) name, (char *) publicId, (char *) systemId);
1146}
1147
1148/**
1149 * unparsedEntityDeclDebug:
1150 * @ctxt: An XML parser context
1151 * @name: The name of the entity
1152 * @publicId: The public ID of the entity
1153 * @systemId: The system ID of the entity
1154 * @notationName: the name of the notation
1155 *
1156 * What to do when an unparsed entity declaration is parsed
1157 */
1158static void
1159unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
1160 const xmlChar *publicId, const xmlChar *systemId,
1161 const xmlChar *notationName)
1162{
1163const xmlChar *nullstr = BAD_CAST "(null)";
1164
1165 if (publicId == NULL)
1166 publicId = nullstr;
1167 if (systemId == NULL)
1168 systemId = nullstr;
1169 if (notationName == NULL)
1170 notationName = nullstr;
1171 callbacks++;
1172 if (noout)
1173 return;
1174 fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
1175 (char *) name, (char *) publicId, (char *) systemId,
1176 (char *) notationName);
1177}
1178
1179/**
1180 * setDocumentLocatorDebug:
1181 * @ctxt: An XML parser context
1182 * @loc: A SAX Locator
1183 *
1184 * Receive the document locator at startup, actually xmlDefaultSAXLocator
1185 * Everything is available on the context, so this is useless in our case.
1186 */
1187static void
1188setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
1189{
1190 callbacks++;
1191 if (noout)
1192 return;
1193 fprintf(stdout, "SAX.setDocumentLocator()\n");
1194}
1195
1196/**
1197 * startDocumentDebug:
1198 * @ctxt: An XML parser context
1199 *
1200 * called when the document start being processed.
1201 */
1202static void
1203startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1204{
1205 callbacks++;
1206 if (noout)
1207 return;
1208 fprintf(stdout, "SAX.startDocument()\n");
1209}
1210
1211/**
1212 * endDocumentDebug:
1213 * @ctxt: An XML parser context
1214 *
1215 * called when the document end has been detected.
1216 */
1217static void
1218endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
1219{
1220 callbacks++;
1221 if (noout)
1222 return;
1223 fprintf(stdout, "SAX.endDocument()\n");
1224}
1225
1226/**
1227 * startElementDebug:
1228 * @ctxt: An XML parser context
1229 * @name: The element name
1230 *
1231 * called when an opening tag has been processed.
1232 */
1233static void
1234startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
1235{
1236 int i;
1237
1238 callbacks++;
1239 if (noout)
1240 return;
1241 fprintf(stdout, "SAX.startElement(%s", (char *) name);
1242 if (atts != NULL) {
1243 for (i = 0;(atts[i] != NULL);i++) {
1244 fprintf(stdout, ", %s='", atts[i++]);
1245 if (atts[i] != NULL)
1246 fprintf(stdout, "%s'", atts[i]);
1247 }
1248 }
1249 fprintf(stdout, ")\n");
1250}
1251
1252/**
1253 * endElementDebug:
1254 * @ctxt: An XML parser context
1255 * @name: The element name
1256 *
1257 * called when the end of an element has been detected.
1258 */
1259static void
1260endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1261{
1262 callbacks++;
1263 if (noout)
1264 return;
1265 fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
1266}
1267
1268/**
1269 * charactersDebug:
1270 * @ctxt: An XML parser context
1271 * @ch: a xmlChar string
1272 * @len: the number of xmlChar
1273 *
1274 * receiving some chars from the parser.
1275 * Question: how much at a time ???
1276 */
1277static void
1278charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1279{
1280 char out[40];
1281 int i;
1282
1283 callbacks++;
1284 if (noout)
1285 return;
1286 for (i = 0;(i<len) && (i < 30);i++)
1287 out[i] = ch[i];
1288 out[i] = 0;
1289
1290 fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
1291}
1292
1293/**
1294 * referenceDebug:
1295 * @ctxt: An XML parser context
1296 * @name: The entity name
1297 *
1298 * called when an entity reference is detected.
1299 */
1300static void
1301referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
1302{
1303 callbacks++;
1304 if (noout)
1305 return;
1306 fprintf(stdout, "SAX.reference(%s)\n", name);
1307}
1308
1309/**
1310 * ignorableWhitespaceDebug:
1311 * @ctxt: An XML parser context
1312 * @ch: a xmlChar string
1313 * @start: the first char in the string
1314 * @len: the number of xmlChar
1315 *
1316 * receiving some ignorable whitespaces from the parser.
1317 * Question: how much at a time ???
1318 */
1319static void
1320ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
1321{
1322 char out[40];
1323 int i;
1324
1325 callbacks++;
1326 if (noout)
1327 return;
1328 for (i = 0;(i<len) && (i < 30);i++)
1329 out[i] = ch[i];
1330 out[i] = 0;
1331 fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
1332}
1333
1334/**
1335 * processingInstructionDebug:
1336 * @ctxt: An XML parser context
1337 * @target: the target name
1338 * @data: the PI data's
1339 * @len: the number of xmlChar
1340 *
1341 * A processing instruction has been parsed.
1342 */
1343static void
1344processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
1345 const xmlChar *data)
1346{
1347 callbacks++;
1348 if (noout)
1349 return;
1350 if (data != NULL)
1351 fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
1352 (char *) target, (char *) data);
1353 else
1354 fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
1355 (char *) target);
1356}
1357
1358/**
1359 * cdataBlockDebug:
1360 * @ctx: the user data (XML parser context)
1361 * @value: The pcdata content
1362 * @len: the block length
1363 *
1364 * called when a pcdata block has been parsed
1365 */
1366static void
1367cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
1368{
1369 callbacks++;
1370 if (noout)
1371 return;
1372 fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
1373 (char *) value, len);
1374}
1375
1376/**
1377 * commentDebug:
1378 * @ctxt: An XML parser context
1379 * @value: the comment content
1380 *
1381 * A comment has been parsed.
1382 */
1383static void
1384commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
1385{
1386 callbacks++;
1387 if (noout)
1388 return;
1389 fprintf(stdout, "SAX.comment(%s)\n", value);
1390}
1391
1392/**
1393 * warningDebug:
1394 * @ctxt: An XML parser context
1395 * @msg: the message to display/transmit
1396 * @...: extra parameters for the message display
1397 *
1398 * Display and format a warning messages, gives file, line, position and
1399 * extra parameters.
1400 */
1401static void XMLCDECL
1402warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1403{
1404 va_list args;
1405
1406 callbacks++;
1407 if (noout)
1408 return;
1409 va_start(args, msg);
1410 fprintf(stdout, "SAX.warning: ");
1411 vfprintf(stdout, msg, args);
1412 va_end(args);
1413}
1414
1415/**
1416 * errorDebug:
1417 * @ctxt: An XML parser context
1418 * @msg: the message to display/transmit
1419 * @...: extra parameters for the message display
1420 *
1421 * Display and format a error messages, gives file, line, position and
1422 * extra parameters.
1423 */
1424static void XMLCDECL
1425errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1426{
1427 va_list args;
1428
1429 callbacks++;
1430 if (noout)
1431 return;
1432 va_start(args, msg);
1433 fprintf(stdout, "SAX.error: ");
1434 vfprintf(stdout, msg, args);
1435 va_end(args);
1436}
1437
1438/**
1439 * fatalErrorDebug:
1440 * @ctxt: An XML parser context
1441 * @msg: the message to display/transmit
1442 * @...: extra parameters for the message display
1443 *
1444 * Display and format a fatalError messages, gives file, line, position and
1445 * extra parameters.
1446 */
1447static void XMLCDECL
1448fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
1449{
1450 va_list args;
1451
1452 callbacks++;
1453 if (noout)
1454 return;
1455 va_start(args, msg);
1456 fprintf(stdout, "SAX.fatalError: ");
1457 vfprintf(stdout, msg, args);
1458 va_end(args);
1459}
1460
1461static xmlSAXHandler debugSAXHandlerStruct = {
1462 internalSubsetDebug,
1463 isStandaloneDebug,
1464 hasInternalSubsetDebug,
1465 hasExternalSubsetDebug,
1466 resolveEntityDebug,
1467 getEntityDebug,
1468 entityDeclDebug,
1469 notationDeclDebug,
1470 attributeDeclDebug,
1471 elementDeclDebug,
1472 unparsedEntityDeclDebug,
1473 setDocumentLocatorDebug,
1474 startDocumentDebug,
1475 endDocumentDebug,
1476 startElementDebug,
1477 endElementDebug,
1478 referenceDebug,
1479 charactersDebug,
1480 ignorableWhitespaceDebug,
1481 processingInstructionDebug,
1482 commentDebug,
1483 warningDebug,
1484 errorDebug,
1485 fatalErrorDebug,
1486 getParameterEntityDebug,
1487 cdataBlockDebug,
1488 externalSubsetDebug,
1489 1,
1490 NULL,
1491 NULL,
1492 NULL,
1493 NULL
1494};
1495
1496xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1497
1498/*
1499 * SAX2 specific callbacks
1500 */
1501/**
1502 * startElementNsDebug:
1503 * @ctxt: An XML parser context
1504 * @name: The element name
1505 *
1506 * called when an opening tag has been processed.
1507 */
1508static void
1509startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1510 const xmlChar *localname,
1511 const xmlChar *prefix,
1512 const xmlChar *URI,
1513 int nb_namespaces,
1514 const xmlChar **namespaces,
1515 int nb_attributes,
1516 int nb_defaulted,
1517 const xmlChar **attributes)
1518{
1519 int i;
1520
1521 callbacks++;
1522 if (noout)
1523 return;
1524 fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
1525 if (prefix == NULL)
1526 fprintf(stdout, ", NULL");
1527 else
1528 fprintf(stdout, ", %s", (char *) prefix);
1529 if (URI == NULL)
1530 fprintf(stdout, ", NULL");
1531 else
1532 fprintf(stdout, ", '%s'", (char *) URI);
1533 fprintf(stdout, ", %d", nb_namespaces);
1534
1535 if (namespaces != NULL) {
1536 for (i = 0;i < nb_namespaces * 2;i++) {
1537 fprintf(stdout, ", xmlns");
1538 if (namespaces[i] != NULL)
1539 fprintf(stdout, ":%s", namespaces[i]);
1540 i++;
1541 fprintf(stdout, "='%s'", namespaces[i]);
1542 }
1543 }
1544 fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
1545 if (attributes != NULL) {
1546 for (i = 0;i < nb_attributes * 5;i += 5) {
1547 if (attributes[i + 1] != NULL)
1548 fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
1549 else
1550 fprintf(stdout, ", %s='", attributes[i]);
1551 fprintf(stdout, "%.4s...', %d", attributes[i + 3],
1552 (int)(attributes[i + 4] - attributes[i + 3]));
1553 }
1554 }
1555 fprintf(stdout, ")\n");
1556}
1557
1558/**
1559 * endElementDebug:
1560 * @ctxt: An XML parser context
1561 * @name: The element name
1562 *
1563 * called when the end of an element has been detected.
1564 */
1565static void
1566endElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
1567 const xmlChar *localname,
1568 const xmlChar *prefix,
1569 const xmlChar *URI)
1570{
1571 callbacks++;
1572 if (noout)
1573 return;
1574 fprintf(stdout, "SAX.endElementNs(%s", (char *) localname);
1575 if (prefix == NULL)
1576 fprintf(stdout, ", NULL");
1577 else
1578 fprintf(stdout, ", %s", (char *) prefix);
1579 if (URI == NULL)
1580 fprintf(stdout, ", NULL)\n");
1581 else
1582 fprintf(stdout, ", '%s')\n", (char *) URI);
1583}
1584
1585static xmlSAXHandler debugSAX2HandlerStruct = {
1586 internalSubsetDebug,
1587 isStandaloneDebug,
1588 hasInternalSubsetDebug,
1589 hasExternalSubsetDebug,
1590 resolveEntityDebug,
1591 getEntityDebug,
1592 entityDeclDebug,
1593 notationDeclDebug,
1594 attributeDeclDebug,
1595 elementDeclDebug,
1596 unparsedEntityDeclDebug,
1597 setDocumentLocatorDebug,
1598 startDocumentDebug,
1599 endDocumentDebug,
1600 NULL,
1601 NULL,
1602 referenceDebug,
1603 charactersDebug,
1604 ignorableWhitespaceDebug,
1605 processingInstructionDebug,
1606 commentDebug,
1607 warningDebug,
1608 errorDebug,
1609 fatalErrorDebug,
1610 getParameterEntityDebug,
1611 cdataBlockDebug,
1612 externalSubsetDebug,
1613 XML_SAX2_MAGIC,
1614 NULL,
1615 startElementNsDebug,
1616 endElementNsDebug,
1617 NULL
1618};
1619
1620static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
1621
1622static void
1623testSAX(const char *filename) {
1624 xmlSAXHandlerPtr handler;
1625 const char *user_data = "user_data"; /* mostly for debugging */
1626 xmlParserInputBufferPtr buf = NULL;
1627 xmlParserInputPtr inputStream;
1628 xmlParserCtxtPtr ctxt = NULL;
1629 xmlSAXHandlerPtr old_sax = NULL;
1630
1631 callbacks = 0;
1632
1633 if (noout) {
1634 handler = emptySAXHandler;
1635#ifdef LIBXML_SAX1_ENABLED
1636 } else if (sax1) {
1637 handler = debugSAXHandler;
1638#endif
1639 } else {
1640 handler = debugSAX2Handler;
1641 }
1642
1643 /*
1644 * it's not the simplest code but the most generic in term of I/O
1645 */
1646 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1647 if (buf == NULL) {
1648 goto error;
1649 }
1650
1651#ifdef LIBXML_SCHEMAS_ENABLED
1652 if (wxschemas != NULL) {
1653 int ret;
1654 xmlSchemaValidCtxtPtr vctxt;
1655
1656 vctxt = xmlSchemaNewValidCtxt(wxschemas);
1657 xmlSchemaSetValidErrors(vctxt,
1658 (xmlSchemaValidityErrorFunc) fprintf,
1659 (xmlSchemaValidityWarningFunc) fprintf,
1660 stderr);
1661
1662 ret = xmlSchemaValidateStream(vctxt, buf, 0, handler,
1663 (void *)user_data);
1664 if (repeat == 0) {
1665 if (ret == 0) {
1666 fprintf(stderr, "%s validates\n", filename);
1667 } else if (ret > 0) {
1668 fprintf(stderr, "%s fails to validate\n", filename);
1669 progresult = XMLLINT_ERR_VALID;
1670 } else {
1671 fprintf(stderr, "%s validation generated an internal error\n",
1672 filename);
1673 progresult = XMLLINT_ERR_VALID;
1674 }
1675 }
1676 xmlSchemaFreeValidCtxt(vctxt);
1677 } else
1678#endif
1679 {
1680 /*
1681 * Create the parser context amd hook the input
1682 */
1683 ctxt = xmlNewParserCtxt();
1684 if (ctxt == NULL) {
1685 xmlFreeParserInputBuffer(buf);
1686 goto error;
1687 }
1688 old_sax = ctxt->sax;
1689 ctxt->sax = handler;
1690 ctxt->userData = (void *) user_data;
1691 inputStream = xmlNewIOInputStream(ctxt, buf, XML_CHAR_ENCODING_NONE);
1692 if (inputStream == NULL) {
1693 xmlFreeParserInputBuffer(buf);
1694 goto error;
1695 }
1696 inputPush(ctxt, inputStream);
1697
1698 /* do the parsing */
1699 xmlParseDocument(ctxt);
1700
1701 if (ctxt->myDoc != NULL) {
1702 fprintf(stderr, "SAX generated a doc !\n");
1703 xmlFreeDoc(ctxt->myDoc);
1704 ctxt->myDoc = NULL;
1705 }
1706 }
1707
1708error:
1709 if (ctxt != NULL) {
1710 ctxt->sax = old_sax;
1711 xmlFreeParserCtxt(ctxt);
1712 }
1713}
1714
1715/************************************************************************
1716 * *
1717 * Stream Test processing *
1718 * *
1719 ************************************************************************/
1720#ifdef LIBXML_READER_ENABLED
1721static void processNode(xmlTextReaderPtr reader) {
1722 const xmlChar *name, *value;
1723 int type, empty;
1724
1725 type = xmlTextReaderNodeType(reader);
1726 empty = xmlTextReaderIsEmptyElement(reader);
1727
1728 if (debug) {
1729 name = xmlTextReaderConstName(reader);
1730 if (name == NULL)
1731 name = BAD_CAST "--";
1732
1733 value = xmlTextReaderConstValue(reader);
1734
1735
1736 printf("%d %d %s %d %d",
1737 xmlTextReaderDepth(reader),
1738 type,
1739 name,
1740 empty,
1741 xmlTextReaderHasValue(reader));
1742 if (value == NULL)
1743 printf("\n");
1744 else {
1745 printf(" %s\n", value);
1746 }
1747 }
1748#ifdef LIBXML_PATTERN_ENABLED
1749 if (patternc) {
1750 xmlChar *path = NULL;
1751 int match = -1;
1752
1753 if (type == XML_READER_TYPE_ELEMENT) {
1754 /* do the check only on element start */
1755 match = xmlPatternMatch(patternc, xmlTextReaderCurrentNode(reader));
1756
1757 if (match) {
1758 path = xmlGetNodePath(xmlTextReaderCurrentNode(reader));
1759 printf("Node %s matches pattern %s\n", path, pattern);
1760 }
1761 }
1762 if (patstream != NULL) {
1763 int ret;
1764
1765 if (type == XML_READER_TYPE_ELEMENT) {
1766 ret = xmlStreamPush(patstream,
1767 xmlTextReaderConstLocalName(reader),
1768 xmlTextReaderConstNamespaceUri(reader));
1769 if (ret < 0) {
1770 fprintf(stderr, "xmlStreamPush() failure\n");
1771 xmlFreeStreamCtxt(patstream);
1772 patstream = NULL;
1773 } else if (ret != match) {
1774 if (path == NULL) {
1775 path = xmlGetNodePath(
1776 xmlTextReaderCurrentNode(reader));
1777 }
1778 fprintf(stderr,
1779 "xmlPatternMatch and xmlStreamPush disagree\n");
1780 fprintf(stderr,
1781 " pattern %s node %s\n",
1782 pattern, path);
1783 }
1784
1785
1786 }
1787 if ((type == XML_READER_TYPE_END_ELEMENT) ||
1788 ((type == XML_READER_TYPE_ELEMENT) && (empty))) {
1789 ret = xmlStreamPop(patstream);
1790 if (ret < 0) {
1791 fprintf(stderr, "xmlStreamPop() failure\n");
1792 xmlFreeStreamCtxt(patstream);
1793 patstream = NULL;
1794 }
1795 }
1796 }
1797 if (path != NULL)
1798 xmlFree(path);
1799 }
1800#endif
1801}
1802
1803static void streamFile(char *filename) {
1804 xmlTextReaderPtr reader;
1805 int ret;
1806#ifdef HAVE_SYS_MMAN_H
1807 int fd = -1;
1808 struct stat info;
1809 const char *base = NULL;
1810 xmlParserInputBufferPtr input = NULL;
1811
1812 if (memory) {
1813 if (stat(filename, &info) < 0)
1814 return;
1815 if ((fd = open(filename, O_RDONLY)) < 0)
1816 return;
1817 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
1818 if (base == (void *) MAP_FAILED)
1819 return;
1820
1821 reader = xmlReaderForMemory(base, info.st_size, filename,
1822 NULL, options);
1823 } else
1824#endif
1825 reader = xmlReaderForFile(filename, NULL, options);
1826#ifdef LIBXML_PATTERN_ENABLED
1827 if (pattern != NULL) {
1828 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
1829 if (patternc == NULL) {
1830 xmlGenericError(xmlGenericErrorContext,
1831 "Pattern %s failed to compile\n", pattern);
1832 progresult = XMLLINT_ERR_SCHEMAPAT;
1833 pattern = NULL;
1834 }
1835 }
1836 if (patternc != NULL) {
1837 patstream = xmlPatternGetStreamCtxt(patternc);
1838 if (patstream != NULL) {
1839 ret = xmlStreamPush(patstream, NULL, NULL);
1840 if (ret < 0) {
1841 fprintf(stderr, "xmlStreamPush() failure\n");
1842 xmlFreeStreamCtxt(patstream);
1843 patstream = NULL;
1844 }
1845 }
1846 }
1847#endif
1848
1849
1850 if (reader != NULL) {
1851#ifdef LIBXML_VALID_ENABLED
1852 if (valid)
1853 xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1);
1854 else
1855#endif /* LIBXML_VALID_ENABLED */
1856 xmlTextReaderSetParserProp(reader, XML_PARSER_LOADDTD, 1);
1857#ifdef LIBXML_SCHEMAS_ENABLED
1858 if (relaxng != NULL) {
1859 if ((timing) && (!repeat)) {
1860 startTimer();
1861 }
1862 ret = xmlTextReaderRelaxNGValidate(reader, relaxng);
1863 if (ret < 0) {
1864 xmlGenericError(xmlGenericErrorContext,
1865 "Relax-NG schema %s failed to compile\n", relaxng);
1866 progresult = XMLLINT_ERR_SCHEMACOMP;
1867 relaxng = NULL;
1868 }
1869 if ((timing) && (!repeat)) {
1870 endTimer("Compiling the schemas");
1871 }
1872 }
1873 if (schema != NULL) {
1874 if ((timing) && (!repeat)) {
1875 startTimer();
1876 }
1877 ret = xmlTextReaderSchemaValidate(reader, schema);
1878 if (ret < 0) {
1879 xmlGenericError(xmlGenericErrorContext,
1880 "XSD schema %s failed to compile\n", schema);
1881 progresult = XMLLINT_ERR_SCHEMACOMP;
1882 schema = NULL;
1883 }
1884 if ((timing) && (!repeat)) {
1885 endTimer("Compiling the schemas");
1886 }
1887 }
1888#endif
1889
1890 /*
1891 * Process all nodes in sequence
1892 */
1893 if ((timing) && (!repeat)) {
1894 startTimer();
1895 }
1896 ret = xmlTextReaderRead(reader);
1897 while (ret == 1) {
1898 if ((debug)
1899#ifdef LIBXML_PATTERN_ENABLED
1900 || (patternc)
1901#endif
1902 )
1903 processNode(reader);
1904 ret = xmlTextReaderRead(reader);
1905 }
1906 if ((timing) && (!repeat)) {
1907#ifdef LIBXML_SCHEMAS_ENABLED
1908 if (relaxng != NULL)
1909 endTimer("Parsing and validating");
1910 else
1911#endif
1912#ifdef LIBXML_VALID_ENABLED
1913 if (valid)
1914 endTimer("Parsing and validating");
1915 else
1916#endif
1917 endTimer("Parsing");
1918 }
1919
1920#ifdef LIBXML_VALID_ENABLED
1921 if (valid) {
1922 if (xmlTextReaderIsValid(reader) != 1) {
1923 xmlGenericError(xmlGenericErrorContext,
1924 "Document %s does not validate\n", filename);
1925 progresult = XMLLINT_ERR_VALID;
1926 }
1927 }
1928#endif /* LIBXML_VALID_ENABLED */
1929#ifdef LIBXML_SCHEMAS_ENABLED
1930 if ((relaxng != NULL) || (schema != NULL)) {
1931 if (xmlTextReaderIsValid(reader) != 1) {
1932 fprintf(stderr, "%s fails to validate\n", filename);
1933 progresult = XMLLINT_ERR_VALID;
1934 } else {
1935 fprintf(stderr, "%s validates\n", filename);
1936 }
1937 }
1938#endif
1939 /*
1940 * Done, cleanup and status
1941 */
1942 xmlFreeTextReader(reader);
1943 if (ret != 0) {
1944 fprintf(stderr, "%s : failed to parse\n", filename);
1945 progresult = XMLLINT_ERR_UNCLASS;
1946 }
1947 } else {
1948 fprintf(stderr, "Unable to open %s\n", filename);
1949 progresult = XMLLINT_ERR_UNCLASS;
1950 }
1951#ifdef LIBXML_PATTERN_ENABLED
1952 if (patstream != NULL) {
1953 xmlFreeStreamCtxt(patstream);
1954 patstream = NULL;
1955 }
1956#endif
1957#ifdef HAVE_SYS_MMAN_H
1958 if (memory) {
1959 xmlFreeParserInputBuffer(input);
1960 munmap((char *) base, info.st_size);
1961 close(fd);
1962 }
1963#endif
1964}
1965
1966static void walkDoc(xmlDocPtr doc) {
1967 xmlTextReaderPtr reader;
1968 int ret;
1969
1970#ifdef LIBXML_PATTERN_ENABLED
1971 xmlNodePtr root;
1972 const xmlChar *namespaces[22];
1973 int i;
1974 xmlNsPtr ns;
1975
1976 root = xmlDocGetRootElement(doc);
1977 for (ns = root->nsDef, i = 0;ns != NULL && i < 20;ns=ns->next) {
1978 namespaces[i++] = ns->href;
1979 namespaces[i++] = ns->prefix;
1980 }
1981 namespaces[i++] = NULL;
1982 namespaces[i++] = NULL;
1983
1984 if (pattern != NULL) {
1985 patternc = xmlPatterncompile((const xmlChar *) pattern, doc->dict,
1986 0, &namespaces[0]);
1987 if (patternc == NULL) {
1988 xmlGenericError(xmlGenericErrorContext,
1989 "Pattern %s failed to compile\n", pattern);
1990 progresult = XMLLINT_ERR_SCHEMAPAT;
1991 pattern = NULL;
1992 }
1993 }
1994 if (patternc != NULL) {
1995 patstream = xmlPatternGetStreamCtxt(patternc);
1996 if (patstream != NULL) {
1997 ret = xmlStreamPush(patstream, NULL, NULL);
1998 if (ret < 0) {
1999 fprintf(stderr, "xmlStreamPush() failure\n");
2000 xmlFreeStreamCtxt(patstream);
2001 patstream = NULL;
2002 }
2003 }
2004 }
2005#endif /* LIBXML_PATTERN_ENABLED */
2006 reader = xmlReaderWalker(doc);
2007 if (reader != NULL) {
2008 if ((timing) && (!repeat)) {
2009 startTimer();
2010 }
2011 ret = xmlTextReaderRead(reader);
2012 while (ret == 1) {
2013 if ((debug)
2014#ifdef LIBXML_PATTERN_ENABLED
2015 || (patternc)
2016#endif
2017 )
2018 processNode(reader);
2019 ret = xmlTextReaderRead(reader);
2020 }
2021 if ((timing) && (!repeat)) {
2022 endTimer("walking through the doc");
2023 }
2024 xmlFreeTextReader(reader);
2025 if (ret != 0) {
2026 fprintf(stderr, "failed to walk through the doc\n");
2027 progresult = XMLLINT_ERR_UNCLASS;
2028 }
2029 } else {
2030 fprintf(stderr, "Failed to crate a reader from the document\n");
2031 progresult = XMLLINT_ERR_UNCLASS;
2032 }
2033#ifdef LIBXML_PATTERN_ENABLED
2034 if (patstream != NULL) {
2035 xmlFreeStreamCtxt(patstream);
2036 patstream = NULL;
2037 }
2038#endif
2039}
2040#endif /* LIBXML_READER_ENABLED */
2041
2042/************************************************************************
2043 * *
2044 * Tree Test processing *
2045 * *
2046 ************************************************************************/
2047static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
2048 xmlDocPtr doc = NULL;
2049#ifdef LIBXML_TREE_ENABLED
2050 xmlDocPtr tmp;
2051#endif /* LIBXML_TREE_ENABLED */
2052
2053 if ((timing) && (!repeat))
2054 startTimer();
2055
2056
2057#ifdef LIBXML_TREE_ENABLED
2058 if (filename == NULL) {
2059 if (generate) {
2060 xmlNodePtr n;
2061
2062 doc = xmlNewDoc(BAD_CAST "1.0");
2063 n = xmlNewDocNode(doc, NULL, BAD_CAST "info", NULL);
2064 xmlNodeSetContent(n, BAD_CAST "abc");
2065 xmlDocSetRootElement(doc, n);
2066 }
2067 }
2068#endif /* LIBXML_TREE_ENABLED */
2069#ifdef LIBXML_HTML_ENABLED
2070#ifdef LIBXML_PUSH_ENABLED
2071 else if ((html) && (push)) {
2072 FILE *f;
2073
2074#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2075 f = fopen(filename, "rb");
2076#else
2077 f = fopen(filename, "r");
2078#endif
2079 if (f != NULL) {
2080 int res, size = 3;
2081 char chars[4096];
2082 htmlParserCtxtPtr ctxt;
2083
2084 /* if (repeat) */
2085 size = 4096;
2086 res = fread(chars, 1, 4, f);
2087 if (res > 0) {
2088 ctxt = htmlCreatePushParserCtxt(NULL, NULL,
2089 chars, res, filename, XML_CHAR_ENCODING_NONE);
2090 while ((res = fread(chars, 1, size, f)) > 0) {
2091 htmlParseChunk(ctxt, chars, res, 0);
2092 }
2093 htmlParseChunk(ctxt, chars, 0, 1);
2094 doc = ctxt->myDoc;
2095 htmlFreeParserCtxt(ctxt);
2096 }
2097 fclose(f);
2098 }
2099 }
2100#endif /* LIBXML_PUSH_ENABLED */
2101#ifdef HAVE_SYS_MMAN_H
2102 else if ((html) && (memory)) {
2103 int fd;
2104 struct stat info;
2105 const char *base;
2106 if (stat(filename, &info) < 0)
2107 return;
2108 if ((fd = open(filename, O_RDONLY)) < 0)
2109 return;
2110 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2111 if (base == (void *) MAP_FAILED)
2112 return;
2113
2114 doc = htmlReadMemory((char *) base, info.st_size, filename,
2115 NULL, options);
2116
2117 munmap((char *) base, info.st_size);
2118 close(fd);
2119 }
2120#endif
2121 else if (html) {
2122 doc = htmlReadFile(filename, NULL, options);
2123 }
2124#endif /* LIBXML_HTML_ENABLED */
2125 else {
2126#ifdef LIBXML_PUSH_ENABLED
2127 /*
2128 * build an XML tree from a string;
2129 */
2130 if (push) {
2131 FILE *f;
2132
2133 /* '-' Usually means stdin -<sven@zen.org> */
2134 if ((filename[0] == '-') && (filename[1] == 0)) {
2135 f = stdin;
2136 } else {
2137#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2138 f = fopen(filename, "rb");
2139#else
2140 f = fopen(filename, "r");
2141#endif
2142 }
2143 if (f != NULL) {
2144 int ret;
2145 int res, size = 1024;
2146 char chars[1024];
2147 xmlParserCtxtPtr ctxt;
2148
2149 /* if (repeat) size = 1024; */
2150 res = fread(chars, 1, 4, f);
2151 if (res > 0) {
2152 ctxt = xmlCreatePushParserCtxt(NULL, NULL,
2153 chars, res, filename);
2154 xmlCtxtUseOptions(ctxt, options);
2155 while ((res = fread(chars, 1, size, f)) > 0) {
2156 xmlParseChunk(ctxt, chars, res, 0);
2157 }
2158 xmlParseChunk(ctxt, chars, 0, 1);
2159 doc = ctxt->myDoc;
2160 ret = ctxt->wellFormed;
2161 xmlFreeParserCtxt(ctxt);
2162 if (!ret) {
2163 xmlFreeDoc(doc);
2164 doc = NULL;
2165 }
2166 }
2167 }
2168 } else
2169#endif /* LIBXML_PUSH_ENABLED */
2170 if (testIO) {
2171 if ((filename[0] == '-') && (filename[1] == 0)) {
2172 doc = xmlReadFd(0, NULL, NULL, options);
2173 } else {
2174 FILE *f;
2175
2176#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2177 f = fopen(filename, "rb");
2178#else
2179 f = fopen(filename, "r");
2180#endif
2181 if (f != NULL) {
2182 if (rectxt == NULL)
2183 doc = xmlReadIO((xmlInputReadCallback) myRead,
2184 (xmlInputCloseCallback) myClose, f,
2185 filename, NULL, options);
2186 else
2187 doc = xmlCtxtReadIO(rectxt,
2188 (xmlInputReadCallback) myRead,
2189 (xmlInputCloseCallback) myClose, f,
2190 filename, NULL, options);
2191 } else
2192 doc = NULL;
2193 }
2194 } else if (htmlout) {
2195 xmlParserCtxtPtr ctxt;
2196
2197 if (rectxt == NULL)
2198 ctxt = xmlNewParserCtxt();
2199 else
2200 ctxt = rectxt;
2201 if (ctxt == NULL) {
2202 doc = NULL;
2203 } else {
2204 ctxt->sax->error = xmlHTMLError;
2205 ctxt->sax->warning = xmlHTMLWarning;
2206 ctxt->vctxt.error = xmlHTMLValidityError;
2207 ctxt->vctxt.warning = xmlHTMLValidityWarning;
2208
2209 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2210
2211 if (rectxt == NULL)
2212 xmlFreeParserCtxt(ctxt);
2213 }
2214#ifdef HAVE_SYS_MMAN_H
2215 } else if (memory) {
2216 int fd;
2217 struct stat info;
2218 const char *base;
2219 if (stat(filename, &info) < 0)
2220 return;
2221 if ((fd = open(filename, O_RDONLY)) < 0)
2222 return;
2223 base = mmap(NULL, info.st_size, PROT_READ, MAP_SHARED, fd, 0) ;
2224 if (base == (void *) MAP_FAILED)
2225 return;
2226
2227 if (rectxt == NULL)
2228 doc = xmlReadMemory((char *) base, info.st_size,
2229 filename, NULL, options);
2230 else
2231 doc = xmlCtxtReadMemory(rectxt, (char *) base, info.st_size,
2232 filename, NULL, options);
2233
2234 munmap((char *) base, info.st_size);
2235 close(fd);
2236#endif
2237#ifdef LIBXML_VALID_ENABLED
2238 } else if (valid) {
2239 xmlParserCtxtPtr ctxt = NULL;
2240
2241 if (rectxt == NULL)
2242 ctxt = xmlNewParserCtxt();
2243 else
2244 ctxt = rectxt;
2245 if (ctxt == NULL) {
2246 doc = NULL;
2247 } else {
2248 doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
2249
2250 if (ctxt->valid == 0)
2251 progresult = XMLLINT_ERR_RDFILE;
2252 if (rectxt == NULL)
2253 xmlFreeParserCtxt(ctxt);
2254 }
2255#endif /* LIBXML_VALID_ENABLED */
2256 } else {
2257 if (rectxt != NULL)
2258 doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
2259 else {
2260#ifdef LIBXML_SAX1_ENABLED
2261 if (sax1)
2262 doc = xmlParseFile(filename);
2263 else
2264#endif /* LIBXML_SAX1_ENABLED */
2265 doc = xmlReadFile(filename, NULL, options);
2266 }
2267 }
2268 }
2269
2270 /*
2271 * If we don't have a document we might as well give up. Do we
2272 * want an error message here? <sven@zen.org> */
2273 if (doc == NULL) {
2274 progresult = XMLLINT_ERR_UNCLASS;
2275 return;
2276 }
2277
2278 if ((timing) && (!repeat)) {
2279 endTimer("Parsing");
2280 }
2281
2282 /*
2283 * Remove DOCTYPE nodes
2284 */
2285 if (dropdtd) {
2286 xmlDtdPtr dtd;
2287
2288 dtd = xmlGetIntSubset(doc);
2289 if (dtd != NULL) {
2290 xmlUnlinkNode((xmlNodePtr)dtd);
2291 xmlFreeDtd(dtd);
2292 }
2293 }
2294
2295#ifdef LIBXML_XINCLUDE_ENABLED
2296 if (xinclude) {
2297 if ((timing) && (!repeat)) {
2298 startTimer();
2299 }
2300 if (xmlXIncludeProcessFlags(doc, options) < 0)
2301 progresult = XMLLINT_ERR_UNCLASS;
2302 if ((timing) && (!repeat)) {
2303 endTimer("Xinclude processing");
2304 }
2305 }
2306#endif
2307
2308#ifdef LIBXML_DEBUG_ENABLED
2309#ifdef LIBXML_XPATH_ENABLED
2310 /*
2311 * shell interaction
2312 */
2313 if (shell) {
2314 xmlXPathOrderDocElems(doc);
2315 xmlShell(doc, filename, xmlShellReadline, stdout);
2316 }
2317#endif
2318#endif
2319
2320#ifdef LIBXML_TREE_ENABLED
2321 /*
2322 * test intermediate copy if needed.
2323 */
2324 if (copy) {
2325 tmp = doc;
2326 if (timing) {
2327 startTimer();
2328 }
2329 doc = xmlCopyDoc(doc, 1);
2330 if (timing) {
2331 endTimer("Copying");
2332 }
2333 if (timing) {
2334 startTimer();
2335 }
2336 xmlFreeDoc(tmp);
2337 if (timing) {
2338 endTimer("Freeing original");
2339 }
2340 }
2341#endif /* LIBXML_TREE_ENABLED */
2342
2343#ifdef LIBXML_VALID_ENABLED
2344 if ((insert) && (!html)) {
2345 const xmlChar* list[256];
2346 int nb, i;
2347 xmlNodePtr node;
2348
2349 if (doc->children != NULL) {
2350 node = doc->children;
2351 while ((node != NULL) && (node->last == NULL)) node = node->next;
2352 if (node != NULL) {
2353 nb = xmlValidGetValidElements(node->last, NULL, list, 256);
2354 if (nb < 0) {
2355 fprintf(stderr, "could not get valid list of elements\n");
2356 } else if (nb == 0) {
2357 fprintf(stderr, "No element can be inserted under root\n");
2358 } else {
2359 fprintf(stderr, "%d element types can be inserted under root:\n",
2360 nb);
2361 for (i = 0;i < nb;i++) {
2362 fprintf(stderr, "%s\n", (char *) list[i]);
2363 }
2364 }
2365 }
2366 }
2367 }else
2368#endif /* LIBXML_VALID_ENABLED */
2369#ifdef LIBXML_READER_ENABLED
2370 if (walker) {
2371 walkDoc(doc);
2372 }
2373#endif /* LIBXML_READER_ENABLED */
2374#ifdef LIBXML_OUTPUT_ENABLED
2375 if (noout == 0) {
2376 int ret;
2377
2378 /*
2379 * print it.
2380 */
2381#ifdef LIBXML_DEBUG_ENABLED
2382 if (!debug) {
2383#endif
2384 if ((timing) && (!repeat)) {
2385 startTimer();
2386 }
2387#ifdef LIBXML_HTML_ENABLED
2388 if ((html) && (!xmlout)) {
2389 if (compress) {
2390 htmlSaveFile(output ? output : "-", doc);
2391 }
2392 else if (encoding != NULL) {
2393 if ( format ) {
2394 htmlSaveFileFormat(output ? output : "-", doc, encoding, 1);
2395 }
2396 else {
2397 htmlSaveFileFormat(output ? output : "-", doc, encoding, 0);
2398 }
2399 }
2400 else if (format) {
2401 htmlSaveFileFormat(output ? output : "-", doc, NULL, 1);
2402 }
2403 else {
2404 FILE *out;
2405 if (output == NULL)
2406 out = stdout;
2407 else {
2408 out = fopen(output,"wb");
2409 }
2410 if (out != NULL) {
2411 if (htmlDocDump(out, doc) < 0)
2412 progresult = XMLLINT_ERR_OUT;
2413
2414 if (output != NULL)
2415 fclose(out);
2416 } else {
2417 fprintf(stderr, "failed to open %s\n", output);
2418 progresult = XMLLINT_ERR_OUT;
2419 }
2420 }
2421 if ((timing) && (!repeat)) {
2422 endTimer("Saving");
2423 }
2424 } else
2425#endif
2426#ifdef LIBXML_C14N_ENABLED
2427 if (canonical) {
2428 xmlChar *result = NULL;
2429 int size;
2430
2431 size = xmlC14NDocDumpMemory(doc, NULL, 0, NULL, 1, &result);
2432 if (size >= 0) {
2433 write(1, result, size);
2434 xmlFree(result);
2435 } else {
2436 fprintf(stderr, "Failed to canonicalize\n");
2437 progresult = XMLLINT_ERR_OUT;
2438 }
2439 } else
2440 if (exc_canonical) {
2441 xmlChar *result = NULL;
2442 int size;
2443
2444 size = xmlC14NDocDumpMemory(doc, NULL, 1, NULL, 1, &result);
2445 if (size >= 0) {
2446 write(1, result, size);
2447 xmlFree(result);
2448 } else {
2449 fprintf(stderr, "Failed to canonicalize\n");
2450 progresult = XMLLINT_ERR_OUT;
2451 }
2452 } else
2453#endif
2454#ifdef HAVE_SYS_MMAN_H
2455 if (memory) {
2456 xmlChar *result;
2457 int len;
2458
2459 if (encoding != NULL) {
2460 if ( format ) {
2461 xmlDocDumpFormatMemoryEnc(doc, &result, &len, encoding, 1);
2462 } else {
2463 xmlDocDumpMemoryEnc(doc, &result, &len, encoding);
2464 }
2465 } else {
2466 if (format)
2467 xmlDocDumpFormatMemory(doc, &result, &len, 1);
2468 else
2469 xmlDocDumpMemory(doc, &result, &len);
2470 }
2471 if (result == NULL) {
2472 fprintf(stderr, "Failed to save\n");
2473 progresult = XMLLINT_ERR_OUT;
2474 } else {
2475 write(1, result, len);
2476 xmlFree(result);
2477 }
2478
2479 } else
2480#endif /* HAVE_SYS_MMAN_H */
2481 if (compress) {
2482 xmlSaveFile(output ? output : "-", doc);
2483 } else if (oldout) {
2484 if (encoding != NULL) {
2485 if ( format ) {
2486 ret = xmlSaveFormatFileEnc(output ? output : "-", doc,
2487 encoding, 1);
2488 }
2489 else {
2490 ret = xmlSaveFileEnc(output ? output : "-", doc,
2491 encoding);
2492 }
2493 if (ret < 0) {
2494 fprintf(stderr, "failed save to %s\n",
2495 output ? output : "-");
2496 progresult = XMLLINT_ERR_OUT;
2497 }
2498 } else if (format) {
2499 ret = xmlSaveFormatFile(output ? output : "-", doc, 1);
2500 if (ret < 0) {
2501 fprintf(stderr, "failed save to %s\n",
2502 output ? output : "-");
2503 progresult = XMLLINT_ERR_OUT;
2504 }
2505 } else {
2506 FILE *out;
2507 if (output == NULL)
2508 out = stdout;
2509 else {
2510 out = fopen(output,"wb");
2511 }
2512 if (out != NULL) {
2513 if (xmlDocDump(out, doc) < 0)
2514 progresult = XMLLINT_ERR_OUT;
2515
2516 if (output != NULL)
2517 fclose(out);
2518 } else {
2519 fprintf(stderr, "failed to open %s\n", output);
2520 progresult = XMLLINT_ERR_OUT;
2521 }
2522 }
2523 } else {
2524 xmlSaveCtxtPtr ctxt;
2525 int saveOpts = 0;
2526
2527 if (format)
2528 saveOpts |= XML_SAVE_FORMAT;
2529
2530 if (output == NULL)
2531 ctxt = xmlSaveToFd(1, encoding, saveOpts);
2532 else
2533 ctxt = xmlSaveToFilename(output, encoding, saveOpts);
2534
2535 if (ctxt != NULL) {
2536 if (xmlSaveDoc(ctxt, doc) < 0) {
2537 fprintf(stderr, "failed save to %s\n",
2538 output ? output : "-");
2539 progresult = XMLLINT_ERR_OUT;
2540 }
2541 xmlSaveClose(ctxt);
2542 } else {
2543 progresult = XMLLINT_ERR_OUT;
2544 }
2545 }
2546 if ((timing) && (!repeat)) {
2547 endTimer("Saving");
2548 }
2549#ifdef LIBXML_DEBUG_ENABLED
2550 } else {
2551 FILE *out;
2552 if (output == NULL)
2553 out = stdout;
2554 else {
2555 out = fopen(output,"wb");
2556 }
2557 if (out != NULL) {
2558 xmlDebugDumpDocument(out, doc);
2559
2560 if (output != NULL)
2561 fclose(out);
2562 } else {
2563 fprintf(stderr, "failed to open %s\n", output);
2564 progresult = XMLLINT_ERR_OUT;
2565 }
2566 }
2567#endif
2568 }
2569#endif /* LIBXML_OUTPUT_ENABLED */
2570
2571#ifdef LIBXML_VALID_ENABLED
2572 /*
2573 * A posteriori validation test
2574 */
2575 if ((dtdvalid != NULL) || (dtdvalidfpi != NULL)) {
2576 xmlDtdPtr dtd;
2577
2578 if ((timing) && (!repeat)) {
2579 startTimer();
2580 }
2581 if (dtdvalid != NULL)
2582 dtd = xmlParseDTD(NULL, (const xmlChar *)dtdvalid);
2583 else
2584 dtd = xmlParseDTD((const xmlChar *)dtdvalidfpi, NULL);
2585 if ((timing) && (!repeat)) {
2586 endTimer("Parsing DTD");
2587 }
2588 if (dtd == NULL) {
2589 if (dtdvalid != NULL)
2590 xmlGenericError(xmlGenericErrorContext,
2591 "Could not parse DTD %s\n", dtdvalid);
2592 else
2593 xmlGenericError(xmlGenericErrorContext,
2594 "Could not parse DTD %s\n", dtdvalidfpi);
2595 progresult = XMLLINT_ERR_DTD;
2596 } else {
2597 xmlValidCtxtPtr cvp;
2598
2599 if ((cvp = xmlNewValidCtxt()) == NULL) {
2600 xmlGenericError(xmlGenericErrorContext,
2601 "Couldn't allocate validation context\n");
2602 exit(-1);
2603 }
2604 cvp->userData = (void *) stderr;
2605 cvp->error = (xmlValidityErrorFunc) fprintf;
2606 cvp->warning = (xmlValidityWarningFunc) fprintf;
2607
2608 if ((timing) && (!repeat)) {
2609 startTimer();
2610 }
2611 if (!xmlValidateDtd(cvp, doc, dtd)) {
2612 if (dtdvalid != NULL)
2613 xmlGenericError(xmlGenericErrorContext,
2614 "Document %s does not validate against %s\n",
2615 filename, dtdvalid);
2616 else
2617 xmlGenericError(xmlGenericErrorContext,
2618 "Document %s does not validate against %s\n",
2619 filename, dtdvalidfpi);
2620 progresult = XMLLINT_ERR_VALID;
2621 }
2622 if ((timing) && (!repeat)) {
2623 endTimer("Validating against DTD");
2624 }
2625 xmlFreeValidCtxt(cvp);
2626 xmlFreeDtd(dtd);
2627 }
2628 } else if (postvalid) {
2629 xmlValidCtxtPtr cvp;
2630
2631 if ((cvp = xmlNewValidCtxt()) == NULL) {
2632 xmlGenericError(xmlGenericErrorContext,
2633 "Couldn't allocate validation context\n");
2634 exit(-1);
2635 }
2636
2637 if ((timing) && (!repeat)) {
2638 startTimer();
2639 }
2640 cvp->userData = (void *) stderr;
2641 cvp->error = (xmlValidityErrorFunc) fprintf;
2642 cvp->warning = (xmlValidityWarningFunc) fprintf;
2643 if (!xmlValidateDocument(cvp, doc)) {
2644 xmlGenericError(xmlGenericErrorContext,
2645 "Document %s does not validate\n", filename);
2646 progresult = XMLLINT_ERR_VALID;
2647 }
2648 if ((timing) && (!repeat)) {
2649 endTimer("Validating");
2650 }
2651 xmlFreeValidCtxt(cvp);
2652 }
2653#endif /* LIBXML_VALID_ENABLED */
2654#ifdef LIBXML_SCHEMATRON_ENABLED
2655 if (wxschematron != NULL) {
2656 xmlSchematronValidCtxtPtr ctxt;
2657 int ret;
2658 int flag;
2659
2660 if ((timing) && (!repeat)) {
2661 startTimer();
2662 }
2663
2664 if (debug)
2665 flag = XML_SCHEMATRON_OUT_XML;
2666 else
2667 flag = XML_SCHEMATRON_OUT_TEXT;
2668 if (noout)
2669 flag |= XML_SCHEMATRON_OUT_QUIET;
2670 ctxt = xmlSchematronNewValidCtxt(wxschematron, flag);
2671#if 0
2672 xmlSchematronSetValidErrors(ctxt,
2673 (xmlSchematronValidityErrorFunc) fprintf,
2674 (xmlSchematronValidityWarningFunc) fprintf,
2675 stderr);
2676#endif
2677 ret = xmlSchematronValidateDoc(ctxt, doc);
2678 if (ret == 0) {
2679 fprintf(stderr, "%s validates\n", filename);
2680 } else if (ret > 0) {
2681 fprintf(stderr, "%s fails to validate\n", filename);
2682 progresult = XMLLINT_ERR_VALID;
2683 } else {
2684 fprintf(stderr, "%s validation generated an internal error\n",
2685 filename);
2686 progresult = XMLLINT_ERR_VALID;
2687 }
2688 xmlSchematronFreeValidCtxt(ctxt);
2689 if ((timing) && (!repeat)) {
2690 endTimer("Validating");
2691 }
2692 }
2693#endif
2694#ifdef LIBXML_SCHEMAS_ENABLED
2695 if (relaxngschemas != NULL) {
2696 xmlRelaxNGValidCtxtPtr ctxt;
2697 int ret;
2698
2699 if ((timing) && (!repeat)) {
2700 startTimer();
2701 }
2702
2703 ctxt = xmlRelaxNGNewValidCtxt(relaxngschemas);
2704 xmlRelaxNGSetValidErrors(ctxt,
2705 (xmlRelaxNGValidityErrorFunc) fprintf,
2706 (xmlRelaxNGValidityWarningFunc) fprintf,
2707 stderr);
2708 ret = xmlRelaxNGValidateDoc(ctxt, doc);
2709 if (ret == 0) {
2710 fprintf(stderr, "%s validates\n", filename);
2711 } else if (ret > 0) {
2712 fprintf(stderr, "%s fails to validate\n", filename);
2713 progresult = XMLLINT_ERR_VALID;
2714 } else {
2715 fprintf(stderr, "%s validation generated an internal error\n",
2716 filename);
2717 progresult = XMLLINT_ERR_VALID;
2718 }
2719 xmlRelaxNGFreeValidCtxt(ctxt);
2720 if ((timing) && (!repeat)) {
2721 endTimer("Validating");
2722 }
2723 } else if (wxschemas != NULL) {
2724 xmlSchemaValidCtxtPtr ctxt;
2725 int ret;
2726
2727 if ((timing) && (!repeat)) {
2728 startTimer();
2729 }
2730
2731 ctxt = xmlSchemaNewValidCtxt(wxschemas);
2732 xmlSchemaSetValidErrors(ctxt,
2733 (xmlSchemaValidityErrorFunc) fprintf,
2734 (xmlSchemaValidityWarningFunc) fprintf,
2735 stderr);
2736 ret = xmlSchemaValidateDoc(ctxt, doc);
2737 if (ret == 0) {
2738 fprintf(stderr, "%s validates\n", filename);
2739 } else if (ret > 0) {
2740 fprintf(stderr, "%s fails to validate\n", filename);
2741 progresult = XMLLINT_ERR_VALID;
2742 } else {
2743 fprintf(stderr, "%s validation generated an internal error\n",
2744 filename);
2745 progresult = XMLLINT_ERR_VALID;
2746 }
2747 xmlSchemaFreeValidCtxt(ctxt);
2748 if ((timing) && (!repeat)) {
2749 endTimer("Validating");
2750 }
2751 }
2752#endif
2753
2754#ifdef LIBXML_DEBUG_ENABLED
2755 if ((debugent) && (!html))
2756 xmlDebugDumpEntities(stderr, doc);
2757#endif
2758
2759 /*
2760 * free it.
2761 */
2762 if ((timing) && (!repeat)) {
2763 startTimer();
2764 }
2765 xmlFreeDoc(doc);
2766 if ((timing) && (!repeat)) {
2767 endTimer("Freeing");
2768 }
2769}
2770
2771/************************************************************************
2772 * *
2773 * Usage and Main *
2774 * *
2775 ************************************************************************/
2776
2777static void showVersion(const char *name) {
2778 fprintf(stderr, "%s: using libxml version %s\n", name, xmlParserVersion);
2779 fprintf(stderr, " compiled with: ");
2780 if (xmlHasFeature(XML_WITH_THREAD)) fprintf(stderr, "Threads ");
2781 if (xmlHasFeature(XML_WITH_TREE)) fprintf(stderr, "Tree ");
2782 if (xmlHasFeature(XML_WITH_OUTPUT)) fprintf(stderr, "Output ");
2783 if (xmlHasFeature(XML_WITH_PUSH)) fprintf(stderr, "Push ");
2784 if (xmlHasFeature(XML_WITH_READER)) fprintf(stderr, "Reader ");
2785 if (xmlHasFeature(XML_WITH_PATTERN)) fprintf(stderr, "Patterns ");
2786 if (xmlHasFeature(XML_WITH_WRITER)) fprintf(stderr, "Writer ");
2787 if (xmlHasFeature(XML_WITH_SAX1)) fprintf(stderr, "SAXv1 ");
2788 if (xmlHasFeature(XML_WITH_FTP)) fprintf(stderr, "FTP ");
2789 if (xmlHasFeature(XML_WITH_HTTP)) fprintf(stderr, "HTTP ");
2790 if (xmlHasFeature(XML_WITH_VALID)) fprintf(stderr, "DTDValid ");
2791 if (xmlHasFeature(XML_WITH_HTML)) fprintf(stderr, "HTML ");
2792 if (xmlHasFeature(XML_WITH_LEGACY)) fprintf(stderr, "Legacy ");
2793 if (xmlHasFeature(XML_WITH_C14N)) fprintf(stderr, "C14N ");
2794 if (xmlHasFeature(XML_WITH_CATALOG)) fprintf(stderr, "Catalog ");
2795 if (xmlHasFeature(XML_WITH_XPATH)) fprintf(stderr, "XPath ");
2796 if (xmlHasFeature(XML_WITH_XPTR)) fprintf(stderr, "XPointer ");
2797 if (xmlHasFeature(XML_WITH_XINCLUDE)) fprintf(stderr, "XInclude ");
2798 if (xmlHasFeature(XML_WITH_ICONV)) fprintf(stderr, "Iconv ");
2799 if (xmlHasFeature(XML_WITH_ISO8859X)) fprintf(stderr, "ISO8859X ");
2800 if (xmlHasFeature(XML_WITH_UNICODE)) fprintf(stderr, "Unicode ");
2801 if (xmlHasFeature(XML_WITH_REGEXP)) fprintf(stderr, "Regexps ");
2802 if (xmlHasFeature(XML_WITH_AUTOMATA)) fprintf(stderr, "Automata ");
2803 if (xmlHasFeature(XML_WITH_EXPR)) fprintf(stderr, "Expr ");
2804 if (xmlHasFeature(XML_WITH_SCHEMAS)) fprintf(stderr, "Schemas ");
2805 if (xmlHasFeature(XML_WITH_SCHEMATRON)) fprintf(stderr, "Schematron ");
2806 if (xmlHasFeature(XML_WITH_MODULES)) fprintf(stderr, "Modules ");
2807 if (xmlHasFeature(XML_WITH_DEBUG)) fprintf(stderr, "Debug ");
2808 if (xmlHasFeature(XML_WITH_DEBUG_MEM)) fprintf(stderr, "MemDebug ");
2809 if (xmlHasFeature(XML_WITH_DEBUG_RUN)) fprintf(stderr, "RunDebug ");
2810 if (xmlHasFeature(XML_WITH_ZLIB)) fprintf(stderr, "Zlib ");
2811 fprintf(stderr, "\n");
2812}
2813
2814static void usage(const char *name) {
2815 printf("Usage : %s [options] XMLfiles ...\n", name);
2816#ifdef LIBXML_OUTPUT_ENABLED
2817 printf("\tParse the XML files and output the result of the parsing\n");
2818#else
2819 printf("\tParse the XML files\n");
2820#endif /* LIBXML_OUTPUT_ENABLED */
2821 printf("\t--version : display the version of the XML library used\n");
2822#ifdef LIBXML_DEBUG_ENABLED
2823 printf("\t--debug : dump a debug tree of the in-memory document\n");
2824 printf("\t--shell : run a navigating shell\n");
2825 printf("\t--debugent : debug the entities defined in the document\n");
2826#else
2827#ifdef LIBXML_READER_ENABLED
2828 printf("\t--debug : dump the nodes content when using --stream\n");
2829#endif /* LIBXML_READER_ENABLED */
2830#endif
2831#ifdef LIBXML_TREE_ENABLED
2832 printf("\t--copy : used to test the internal copy implementation\n");
2833#endif /* LIBXML_TREE_ENABLED */
2834 printf("\t--recover : output what was parsable on broken XML documents\n");
2835 printf("\t--noent : substitute entity references by their value\n");
2836 printf("\t--noout : don't output the result tree\n");
2837 printf("\t--path 'paths': provide a set of paths for resources\n");
2838 printf("\t--load-trace : print trace of all external entites loaded\n");
2839 printf("\t--nonet : refuse to fetch DTDs or entities over network\n");
2840 printf("\t--nocompact : do not generate compact text nodes\n");
2841 printf("\t--htmlout : output results as HTML\n");
2842 printf("\t--nowrap : do not put HTML doc wrapper\n");
2843#ifdef LIBXML_VALID_ENABLED
2844 printf("\t--valid : validate the document in addition to std well-formed check\n");
2845 printf("\t--postvalid : do a posteriori validation, i.e after parsing\n");
2846 printf("\t--dtdvalid URL : do a posteriori validation against a given DTD\n");
2847 printf("\t--dtdvalidfpi FPI : same but name the DTD with a Public Identifier\n");
2848#endif /* LIBXML_VALID_ENABLED */
2849 printf("\t--timing : print some timings\n");
2850 printf("\t--output file or -o file: save to a given file\n");
2851 printf("\t--repeat : repeat 100 times, for timing or profiling\n");
2852 printf("\t--insert : ad-hoc test for valid insertions\n");
2853#ifdef LIBXML_OUTPUT_ENABLED
2854#ifdef HAVE_ZLIB_H
2855 printf("\t--compress : turn on gzip compression of output\n");
2856#endif
2857#endif /* LIBXML_OUTPUT_ENABLED */
2858#ifdef LIBXML_HTML_ENABLED
2859 printf("\t--html : use the HTML parser\n");
2860 printf("\t--xmlout : force to use the XML serializer when using --html\n");
2861#endif
2862#ifdef LIBXML_PUSH_ENABLED
2863 printf("\t--push : use the push mode of the parser\n");
2864#endif /* LIBXML_PUSH_ENABLED */
2865#ifdef HAVE_SYS_MMAN_H
2866 printf("\t--memory : parse from memory\n");
2867#endif
2868 printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n");
2869 printf("\t--nowarning : do not emit warnings from parser/validator\n");
2870 printf("\t--noblanks : drop (ignorable?) blanks spaces\n");
2871 printf("\t--nocdata : replace cdata section with text nodes\n");
2872#ifdef LIBXML_OUTPUT_ENABLED
2873 printf("\t--format : reformat/reindent the input\n");
2874 printf("\t--encode encoding : output in the given encoding\n");
2875 printf("\t--dropdtd : remove the DOCTYPE of the input docs\n");
2876#endif /* LIBXML_OUTPUT_ENABLED */
2877 printf("\t--c14n : save in W3C canonical format (with comments)\n");
2878 printf("\t--exc-c14n : save in W3C exclusive canonical format (with comments)\n");
2879#ifdef LIBXML_C14N_ENABLED
2880#endif /* LIBXML_C14N_ENABLED */
2881 printf("\t--nsclean : remove redundant namespace declarations\n");
2882 printf("\t--testIO : test user I/O support\n");
2883#ifdef LIBXML_CATALOG_ENABLED
2884 printf("\t--catalogs : use SGML catalogs from $SGML_CATALOG_FILES\n");
2885 printf("\t otherwise XML Catalogs starting from \n");
2886 printf("\t %s are activated by default\n", XML_XML_DEFAULT_CATALOG);
2887 printf("\t--nocatalogs: deactivate all catalogs\n");
2888#endif
2889 printf("\t--auto : generate a small doc on the fly\n");
2890#ifdef LIBXML_XINCLUDE_ENABLED
2891 printf("\t--xinclude : do XInclude processing\n");
2892 printf("\t--noxincludenode : same but do not generate XInclude nodes\n");
2893#endif
2894 printf("\t--loaddtd : fetch external DTD\n");
2895 printf("\t--dtdattr : loaddtd + populate the tree with inherited attributes \n");
2896#ifdef LIBXML_READER_ENABLED
2897 printf("\t--stream : use the streaming interface to process very large files\n");
2898 printf("\t--walker : create a reader and walk though the resulting doc\n");
2899#endif /* LIBXML_READER_ENABLED */
2900#ifdef LIBXML_PATTERN_ENABLED
2901 printf("\t--pattern pattern_value : test the pattern support\n");
2902#endif
2903 printf("\t--chkregister : verify the node registration code\n");
2904#ifdef LIBXML_SCHEMAS_ENABLED
2905 printf("\t--relaxng schema : do RelaxNG validation against the schema\n");
2906 printf("\t--schema schema : do validation against the WXS schema\n");
2907#endif
2908#ifdef LIBXML_SCHEMATRON_ENABLED
2909 printf("\t--schematron schema : do validation against a schematron\n");
2910#endif
2911#ifdef LIBXML_SAX1_ENABLED
2912 printf("\t--sax1: use the old SAX1 interfaces for processing\n");
2913#endif
2914 printf("\t--sax: do not build a tree but work just at the SAX level\n");
2915
2916 printf("\nLibxml project home page: http://xmlsoft.org/\n");
2917 printf("To report bugs or get some help check: http://xmlsoft.org/bugs.html\n");
2918}
2919
2920static void registerNode(xmlNodePtr node)
2921{
2922 node->_private = malloc(sizeof(long));
2923 *(long*)node->_private = (long) 0x81726354;
2924 nbregister++;
2925}
2926
2927static void deregisterNode(xmlNodePtr node)
2928{
2929 assert(node->_private != NULL);
2930 assert(*(long*)node->_private == (long) 0x81726354);
2931 free(node->_private);
2932 nbregister--;
2933}
2934
2935int
2936main(int argc, char **argv) {
2937 int i, acount;
2938 int files = 0;
2939 int version = 0;
2940 const char* indent;
2941
2942 if (argc <= 1) {
2943 usage(argv[0]);
2944 return(1);
2945 }
2946 LIBXML_TEST_VERSION
2947 for (i = 1; i < argc ; i++) {
2948 if (!strcmp(argv[i], "-"))
2949 break;
2950
2951 if (argv[i][0] != '-')
2952 continue;
2953 if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
2954 debug++;
2955 else
2956#ifdef LIBXML_DEBUG_ENABLED
2957 if ((!strcmp(argv[i], "-shell")) ||
2958 (!strcmp(argv[i], "--shell"))) {
2959 shell++;
2960 noout = 1;
2961 } else
2962#endif
2963#ifdef LIBXML_TREE_ENABLED
2964 if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
2965 copy++;
2966 else
2967#endif /* LIBXML_TREE_ENABLED */
2968 if ((!strcmp(argv[i], "-recover")) ||
2969 (!strcmp(argv[i], "--recover"))) {
2970 recovery++;
2971 options |= XML_PARSE_RECOVER;
2972 } else if ((!strcmp(argv[i], "-noent")) ||
2973 (!strcmp(argv[i], "--noent"))) {
2974 noent++;
2975 options |= XML_PARSE_NOENT;
2976 } else if ((!strcmp(argv[i], "-nsclean")) ||
2977 (!strcmp(argv[i], "--nsclean"))) {
2978 options |= XML_PARSE_NSCLEAN;
2979 } else if ((!strcmp(argv[i], "-nocdata")) ||
2980 (!strcmp(argv[i], "--nocdata"))) {
2981 options |= XML_PARSE_NOCDATA;
2982 } else if ((!strcmp(argv[i], "-nodict")) ||
2983 (!strcmp(argv[i], "--nodict"))) {
2984 options |= XML_PARSE_NODICT;
2985 } else if ((!strcmp(argv[i], "-version")) ||
2986 (!strcmp(argv[i], "--version"))) {
2987 showVersion(argv[0]);
2988 version = 1;
2989 } else if ((!strcmp(argv[i], "-noout")) ||
2990 (!strcmp(argv[i], "--noout")))
2991 noout++;
2992#ifdef LIBXML_OUTPUT_ENABLED
2993 else if ((!strcmp(argv[i], "-o")) ||
2994 (!strcmp(argv[i], "-output")) ||
2995 (!strcmp(argv[i], "--output"))) {
2996 i++;
2997 output = argv[i];
2998 }
2999#endif /* LIBXML_OUTPUT_ENABLED */
3000 else if ((!strcmp(argv[i], "-htmlout")) ||
3001 (!strcmp(argv[i], "--htmlout")))
3002 htmlout++;
3003 else if ((!strcmp(argv[i], "-nowrap")) ||
3004 (!strcmp(argv[i], "--nowrap")))
3005 nowrap++;
3006#ifdef LIBXML_HTML_ENABLED
3007 else if ((!strcmp(argv[i], "-html")) ||
3008 (!strcmp(argv[i], "--html"))) {
3009 html++;
3010 }
3011 else if ((!strcmp(argv[i], "-xmlout")) ||
3012 (!strcmp(argv[i], "--xmlout"))) {
3013 xmlout++;
3014 }
3015#endif /* LIBXML_HTML_ENABLED */
3016 else if ((!strcmp(argv[i], "-loaddtd")) ||
3017 (!strcmp(argv[i], "--loaddtd"))) {
3018 loaddtd++;
3019 options |= XML_PARSE_DTDLOAD;
3020 } else if ((!strcmp(argv[i], "-dtdattr")) ||
3021 (!strcmp(argv[i], "--dtdattr"))) {
3022 loaddtd++;
3023 dtdattrs++;
3024 options |= XML_PARSE_DTDATTR;
3025 }
3026#ifdef LIBXML_VALID_ENABLED
3027 else if ((!strcmp(argv[i], "-valid")) ||
3028 (!strcmp(argv[i], "--valid"))) {
3029 valid++;
3030 options |= XML_PARSE_DTDVALID;
3031 } else if ((!strcmp(argv[i], "-postvalid")) ||
3032 (!strcmp(argv[i], "--postvalid"))) {
3033 postvalid++;
3034 loaddtd++;
3035 options |= XML_PARSE_DTDLOAD;
3036 } else if ((!strcmp(argv[i], "-dtdvalid")) ||
3037 (!strcmp(argv[i], "--dtdvalid"))) {
3038 i++;
3039 dtdvalid = argv[i];
3040 loaddtd++;
3041 options |= XML_PARSE_DTDLOAD;
3042 } else if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3043 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3044 i++;
3045 dtdvalidfpi = argv[i];
3046 loaddtd++;
3047 options |= XML_PARSE_DTDLOAD;
3048 }
3049#endif /* LIBXML_VALID_ENABLED */
3050 else if ((!strcmp(argv[i], "-dropdtd")) ||
3051 (!strcmp(argv[i], "--dropdtd")))
3052 dropdtd++;
3053 else if ((!strcmp(argv[i], "-insert")) ||
3054 (!strcmp(argv[i], "--insert")))
3055 insert++;
3056 else if ((!strcmp(argv[i], "-timing")) ||
3057 (!strcmp(argv[i], "--timing")))
3058 timing++;
3059 else if ((!strcmp(argv[i], "-auto")) ||
3060 (!strcmp(argv[i], "--auto")))
3061 generate++;
3062 else if ((!strcmp(argv[i], "-repeat")) ||
3063 (!strcmp(argv[i], "--repeat"))) {
3064 if (repeat)
3065 repeat *= 10;
3066 else
3067 repeat = 100;
3068 }
3069#ifdef LIBXML_PUSH_ENABLED
3070 else if ((!strcmp(argv[i], "-push")) ||
3071 (!strcmp(argv[i], "--push")))
3072 push++;
3073#endif /* LIBXML_PUSH_ENABLED */
3074#ifdef HAVE_SYS_MMAN_H
3075 else if ((!strcmp(argv[i], "-memory")) ||
3076 (!strcmp(argv[i], "--memory")))
3077 memory++;
3078#endif
3079 else if ((!strcmp(argv[i], "-testIO")) ||
3080 (!strcmp(argv[i], "--testIO")))
3081 testIO++;
3082#ifdef LIBXML_XINCLUDE_ENABLED
3083 else if ((!strcmp(argv[i], "-xinclude")) ||
3084 (!strcmp(argv[i], "--xinclude"))) {
3085 xinclude++;
3086 options |= XML_PARSE_XINCLUDE;
3087 }
3088 else if ((!strcmp(argv[i], "-noxincludenode")) ||
3089 (!strcmp(argv[i], "--noxincludenode"))) {
3090 xinclude++;
3091 options |= XML_PARSE_XINCLUDE;
3092 options |= XML_PARSE_NOXINCNODE;
3093 }
3094#endif
3095#ifdef LIBXML_OUTPUT_ENABLED
3096#ifdef HAVE_ZLIB_H
3097 else if ((!strcmp(argv[i], "-compress")) ||
3098 (!strcmp(argv[i], "--compress"))) {
3099 compress++;
3100 xmlSetCompressMode(9);
3101 }
3102#endif
3103#endif /* LIBXML_OUTPUT_ENABLED */
3104 else if ((!strcmp(argv[i], "-nowarning")) ||
3105 (!strcmp(argv[i], "--nowarning"))) {
3106 xmlGetWarningsDefaultValue = 0;
3107 xmlPedanticParserDefault(0);
3108 options |= XML_PARSE_NOWARNING;
3109 }
3110 else if ((!strcmp(argv[i], "-pedantic")) ||
3111 (!strcmp(argv[i], "--pedantic"))) {
3112 xmlGetWarningsDefaultValue = 1;
3113 xmlPedanticParserDefault(1);
3114 options |= XML_PARSE_PEDANTIC;
3115 }
3116#ifdef LIBXML_DEBUG_ENABLED
3117 else if ((!strcmp(argv[i], "-debugent")) ||
3118 (!strcmp(argv[i], "--debugent"))) {
3119 debugent++;
3120 xmlParserDebugEntities = 1;
3121 }
3122#endif
3123#ifdef LIBXML_C14N_ENABLED
3124 else if ((!strcmp(argv[i], "-c14n")) ||
3125 (!strcmp(argv[i], "--c14n"))) {
3126 canonical++;
3127 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3128 }
3129 else if ((!strcmp(argv[i], "-exc-c14n")) ||
3130 (!strcmp(argv[i], "--exc-c14n"))) {
3131 exc_canonical++;
3132 options |= XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_DTDLOAD;
3133 }
3134#endif
3135#ifdef LIBXML_CATALOG_ENABLED
3136 else if ((!strcmp(argv[i], "-catalogs")) ||
3137 (!strcmp(argv[i], "--catalogs"))) {
3138 catalogs++;
3139 } else if ((!strcmp(argv[i], "-nocatalogs")) ||
3140 (!strcmp(argv[i], "--nocatalogs"))) {
3141 nocatalogs++;
3142 }
3143#endif
3144 else if ((!strcmp(argv[i], "-encode")) ||
3145 (!strcmp(argv[i], "--encode"))) {
3146 i++;
3147 encoding = argv[i];
3148 /*
3149 * OK it's for testing purposes
3150 */
3151 xmlAddEncodingAlias("UTF-8", "DVEnc");
3152 }
3153 else if ((!strcmp(argv[i], "-noblanks")) ||
3154 (!strcmp(argv[i], "--noblanks"))) {
3155 noblanks++;
3156 xmlKeepBlanksDefault(0);
3157 }
3158 else if ((!strcmp(argv[i], "-maxmem")) ||
3159 (!strcmp(argv[i], "--maxmem"))) {
3160 i++;
3161 if (sscanf(argv[i], "%d", &maxmem) == 1) {
3162 xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc,
3163 myStrdupFunc);
3164 } else {
3165 maxmem = 0;
3166 }
3167 }
3168 else if ((!strcmp(argv[i], "-format")) ||
3169 (!strcmp(argv[i], "--format"))) {
3170 noblanks++;
3171#ifdef LIBXML_OUTPUT_ENABLED
3172 format++;
3173#endif /* LIBXML_OUTPUT_ENABLED */
3174 xmlKeepBlanksDefault(0);
3175 }
3176#ifdef LIBXML_READER_ENABLED
3177 else if ((!strcmp(argv[i], "-stream")) ||
3178 (!strcmp(argv[i], "--stream"))) {
3179 stream++;
3180 }
3181 else if ((!strcmp(argv[i], "-walker")) ||
3182 (!strcmp(argv[i], "--walker"))) {
3183 walker++;
3184 noout++;
3185 }
3186#endif /* LIBXML_READER_ENABLED */
3187#ifdef LIBXML_SAX1_ENABLED
3188 else if ((!strcmp(argv[i], "-sax1")) ||
3189 (!strcmp(argv[i], "--sax1"))) {
3190 sax1++;
3191 }
3192#endif /* LIBXML_SAX1_ENABLED */
3193 else if ((!strcmp(argv[i], "-sax")) ||
3194 (!strcmp(argv[i], "--sax"))) {
3195 sax++;
3196 }
3197 else if ((!strcmp(argv[i], "-chkregister")) ||
3198 (!strcmp(argv[i], "--chkregister"))) {
3199 chkregister++;
3200#ifdef LIBXML_SCHEMAS_ENABLED
3201 } else if ((!strcmp(argv[i], "-relaxng")) ||
3202 (!strcmp(argv[i], "--relaxng"))) {
3203 i++;
3204 relaxng = argv[i];
3205 noent++;
3206 options |= XML_PARSE_NOENT;
3207 } else if ((!strcmp(argv[i], "-schema")) ||
3208 (!strcmp(argv[i], "--schema"))) {
3209 i++;
3210 schema = argv[i];
3211 noent++;
3212#endif
3213#ifdef LIBXML_SCHEMATRON_ENABLED
3214 } else if ((!strcmp(argv[i], "-schematron")) ||
3215 (!strcmp(argv[i], "--schematron"))) {
3216 i++;
3217 schematron = argv[i];
3218 noent++;
3219#endif
3220 } else if ((!strcmp(argv[i], "-nonet")) ||
3221 (!strcmp(argv[i], "--nonet"))) {
3222 options |= XML_PARSE_NONET;
3223 xmlSetExternalEntityLoader(xmlNoNetExternalEntityLoader);
3224 } else if ((!strcmp(argv[i], "-nocompact")) ||
3225 (!strcmp(argv[i], "--nocompact"))) {
3226 options &= ~XML_PARSE_COMPACT;
3227 } else if ((!strcmp(argv[i], "-load-trace")) ||
3228 (!strcmp(argv[i], "--load-trace"))) {
3229 load_trace++;
3230 } else if ((!strcmp(argv[i], "-path")) ||
3231 (!strcmp(argv[i], "--path"))) {
3232 i++;
3233 parsePath(BAD_CAST argv[i]);
3234#ifdef LIBXML_PATTERN_ENABLED
3235 } else if ((!strcmp(argv[i], "-pattern")) ||
3236 (!strcmp(argv[i], "--pattern"))) {
3237 i++;
3238 pattern = argv[i];
3239#endif
3240 } else {
3241 fprintf(stderr, "Unknown option %s\n", argv[i]);
3242 usage(argv[0]);
3243 return(1);
3244 }
3245 }
3246
3247#ifdef LIBXML_CATALOG_ENABLED
3248 if (nocatalogs == 0) {
3249 if (catalogs) {
3250 const char *catal;
3251
3252 catal = getenv("SGML_CATALOG_FILES");
3253 if (catal != NULL) {
3254 xmlLoadCatalogs(catal);
3255 } else {
3256 fprintf(stderr, "Variable $SGML_CATALOG_FILES not set\n");
3257 }
3258 }
3259 }
3260#endif
3261
3262#ifdef LIBXML_SAX1_ENABLED
3263 if (sax1)
3264 xmlSAXDefaultVersion(1);
3265 else
3266 xmlSAXDefaultVersion(2);
3267#endif /* LIBXML_SAX1_ENABLED */
3268
3269 if (chkregister) {
3270 xmlRegisterNodeDefault(registerNode);
3271 xmlDeregisterNodeDefault(deregisterNode);
3272 }
3273
3274 indent = getenv("XMLLINT_INDENT");
3275 if(indent != NULL) {
3276 xmlTreeIndentString = indent;
3277 }
3278
3279
3280 defaultEntityLoader = xmlGetExternalEntityLoader();
3281 xmlSetExternalEntityLoader(xmllintExternalEntityLoader);
3282
3283 xmlLineNumbersDefault(1);
3284 if (loaddtd != 0)
3285 xmlLoadExtDtdDefaultValue |= XML_DETECT_IDS;
3286 if (dtdattrs)
3287 xmlLoadExtDtdDefaultValue |= XML_COMPLETE_ATTRS;
3288 if (noent != 0) xmlSubstituteEntitiesDefault(1);
3289#ifdef LIBXML_VALID_ENABLED
3290 if (valid != 0) xmlDoValidityCheckingDefaultValue = 1;
3291#endif /* LIBXML_VALID_ENABLED */
3292 if ((htmlout) && (!nowrap)) {
3293 xmlGenericError(xmlGenericErrorContext,
3294 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n");
3295 xmlGenericError(xmlGenericErrorContext,
3296 "\t\"http://www.w3.org/TR/REC-html40/loose.dtd\">\n");
3297 xmlGenericError(xmlGenericErrorContext,
3298 "<html><head><title>%s output</title></head>\n",
3299 argv[0]);
3300 xmlGenericError(xmlGenericErrorContext,
3301 "<body bgcolor=\"#ffffff\"><h1 align=\"center\">%s output</h1>\n",
3302 argv[0]);
3303 }
3304
3305#ifdef LIBXML_SCHEMATRON_ENABLED
3306 if ((schematron != NULL) && (sax == 0)
3307#ifdef LIBXML_READER_ENABLED
3308 && (stream == 0)
3309#endif /* LIBXML_READER_ENABLED */
3310 ) {
3311 xmlSchematronParserCtxtPtr ctxt;
3312
3313 /* forces loading the DTDs */
3314 xmlLoadExtDtdDefaultValue |= 1;
3315 options |= XML_PARSE_DTDLOAD;
3316 if (timing) {
3317 startTimer();
3318 }
3319 ctxt = xmlSchematronNewParserCtxt(schematron);
3320#if 0
3321 xmlSchematronSetParserErrors(ctxt,
3322 (xmlSchematronValidityErrorFunc) fprintf,
3323 (xmlSchematronValidityWarningFunc) fprintf,
3324 stderr);
3325#endif
3326 wxschematron = xmlSchematronParse(ctxt);
3327 if (wxschematron == NULL) {
3328 xmlGenericError(xmlGenericErrorContext,
3329 "Schematron schema %s failed to compile\n", schematron);
3330 progresult = XMLLINT_ERR_SCHEMACOMP;
3331 schematron = NULL;
3332 }
3333 xmlSchematronFreeParserCtxt(ctxt);
3334 if (timing) {
3335 endTimer("Compiling the schemas");
3336 }
3337 }
3338#endif
3339#ifdef LIBXML_SCHEMAS_ENABLED
3340 if ((relaxng != NULL) && (sax == 0)
3341#ifdef LIBXML_READER_ENABLED
3342 && (stream == 0)
3343#endif /* LIBXML_READER_ENABLED */
3344 ) {
3345 xmlRelaxNGParserCtxtPtr ctxt;
3346
3347 /* forces loading the DTDs */
3348 xmlLoadExtDtdDefaultValue |= 1;
3349 options |= XML_PARSE_DTDLOAD;
3350 if (timing) {
3351 startTimer();
3352 }
3353 ctxt = xmlRelaxNGNewParserCtxt(relaxng);
3354 xmlRelaxNGSetParserErrors(ctxt,
3355 (xmlRelaxNGValidityErrorFunc) fprintf,
3356 (xmlRelaxNGValidityWarningFunc) fprintf,
3357 stderr);
3358 relaxngschemas = xmlRelaxNGParse(ctxt);
3359 if (relaxngschemas == NULL) {
3360 xmlGenericError(xmlGenericErrorContext,
3361 "Relax-NG schema %s failed to compile\n", relaxng);
3362 progresult = XMLLINT_ERR_SCHEMACOMP;
3363 relaxng = NULL;
3364 }
3365 xmlRelaxNGFreeParserCtxt(ctxt);
3366 if (timing) {
3367 endTimer("Compiling the schemas");
3368 }
3369 } else if ((schema != NULL)
3370#ifdef LIBXML_READER_ENABLED
3371 && (stream == 0)
3372#endif
3373 ) {
3374 xmlSchemaParserCtxtPtr ctxt;
3375
3376 if (timing) {
3377 startTimer();
3378 }
3379 ctxt = xmlSchemaNewParserCtxt(schema);
3380 xmlSchemaSetParserErrors(ctxt,
3381 (xmlSchemaValidityErrorFunc) fprintf,
3382 (xmlSchemaValidityWarningFunc) fprintf,
3383 stderr);
3384 wxschemas = xmlSchemaParse(ctxt);
3385 if (wxschemas == NULL) {
3386 xmlGenericError(xmlGenericErrorContext,
3387 "WXS schema %s failed to compile\n", schema);
3388 progresult = XMLLINT_ERR_SCHEMACOMP;
3389 schema = NULL;
3390 }
3391 xmlSchemaFreeParserCtxt(ctxt);
3392 if (timing) {
3393 endTimer("Compiling the schemas");
3394 }
3395 }
3396#endif /* LIBXML_SCHEMAS_ENABLED */
3397#ifdef LIBXML_PATTERN_ENABLED
3398 if ((pattern != NULL)
3399#ifdef LIBXML_READER_ENABLED
3400 && (walker == 0)
3401#endif
3402 ) {
3403 patternc = xmlPatterncompile((const xmlChar *) pattern, NULL, 0, NULL);
3404 if (patternc == NULL) {
3405 xmlGenericError(xmlGenericErrorContext,
3406 "Pattern %s failed to compile\n", pattern);
3407 progresult = XMLLINT_ERR_SCHEMAPAT;
3408 pattern = NULL;
3409 }
3410 }
3411#endif /* LIBXML_PATTERN_ENABLED */
3412 for (i = 1; i < argc ; i++) {
3413 if ((!strcmp(argv[i], "-encode")) ||
3414 (!strcmp(argv[i], "--encode"))) {
3415 i++;
3416 continue;
3417 } else if ((!strcmp(argv[i], "-o")) ||
3418 (!strcmp(argv[i], "-output")) ||
3419 (!strcmp(argv[i], "--output"))) {
3420 i++;
3421 continue;
3422 }
3423#ifdef LIBXML_VALID_ENABLED
3424 if ((!strcmp(argv[i], "-dtdvalid")) ||
3425 (!strcmp(argv[i], "--dtdvalid"))) {
3426 i++;
3427 continue;
3428 }
3429 if ((!strcmp(argv[i], "-path")) ||
3430 (!strcmp(argv[i], "--path"))) {
3431 i++;
3432 continue;
3433 }
3434 if ((!strcmp(argv[i], "-dtdvalidfpi")) ||
3435 (!strcmp(argv[i], "--dtdvalidfpi"))) {
3436 i++;
3437 continue;
3438 }
3439#endif /* LIBXML_VALID_ENABLED */
3440 if ((!strcmp(argv[i], "-relaxng")) ||
3441 (!strcmp(argv[i], "--relaxng"))) {
3442 i++;
3443 continue;
3444 }
3445 if ((!strcmp(argv[i], "-maxmem")) ||
3446 (!strcmp(argv[i], "--maxmem"))) {
3447 i++;
3448 continue;
3449 }
3450 if ((!strcmp(argv[i], "-schema")) ||
3451 (!strcmp(argv[i], "--schema"))) {
3452 i++;
3453 continue;
3454 }
3455 if ((!strcmp(argv[i], "-schematron")) ||
3456 (!strcmp(argv[i], "--schematron"))) {
3457 i++;
3458 continue;
3459 }
3460#ifdef LIBXML_PATTERN_ENABLED
3461 if ((!strcmp(argv[i], "-pattern")) ||
3462 (!strcmp(argv[i], "--pattern"))) {
3463 i++;
3464 continue;
3465 }
3466#endif
3467 if ((timing) && (repeat))
3468 startTimer();
3469 /* Remember file names. "-" means stdin. <sven@zen.org> */
3470 if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) {
3471 if (repeat) {
3472 xmlParserCtxtPtr ctxt = NULL;
3473
3474 for (acount = 0;acount < repeat;acount++) {
3475#ifdef LIBXML_READER_ENABLED
3476 if (stream != 0) {
3477 streamFile(argv[i]);
3478 } else {
3479#endif /* LIBXML_READER_ENABLED */
3480 if (sax) {
3481 testSAX(argv[i]);
3482 } else {
3483 if (ctxt == NULL)
3484 ctxt = xmlNewParserCtxt();
3485 parseAndPrintFile(argv[i], ctxt);
3486 }
3487#ifdef LIBXML_READER_ENABLED
3488 }
3489#endif /* LIBXML_READER_ENABLED */
3490 }
3491 if (ctxt != NULL)
3492 xmlFreeParserCtxt(ctxt);
3493 } else {
3494 nbregister = 0;
3495
3496#ifdef LIBXML_READER_ENABLED
3497 if (stream != 0)
3498 streamFile(argv[i]);
3499 else
3500#endif /* LIBXML_READER_ENABLED */
3501 if (sax) {
3502 testSAX(argv[i]);
3503 } else {
3504 parseAndPrintFile(argv[i], NULL);
3505 }
3506
3507 if ((chkregister) && (nbregister != 0)) {
3508 fprintf(stderr, "Registration count off: %d\n", nbregister);
3509 progresult = XMLLINT_ERR_RDREGIS;
3510 }
3511 }
3512 files ++;
3513 if ((timing) && (repeat)) {
3514 endTimer("%d iterations", repeat);
3515 }
3516 }
3517 }
3518 if (generate)
3519 parseAndPrintFile(NULL, NULL);
3520 if ((htmlout) && (!nowrap)) {
3521 xmlGenericError(xmlGenericErrorContext, "</body></html>\n");
3522 }
3523 if ((files == 0) && (!generate) && (version == 0)) {
3524 usage(argv[0]);
3525 }
3526#ifdef LIBXML_SCHEMATRON_ENABLED
3527 if (wxschematron != NULL)
3528 xmlSchematronFree(wxschematron);
3529#endif
3530#ifdef LIBXML_SCHEMAS_ENABLED
3531 if (relaxngschemas != NULL)
3532 xmlRelaxNGFree(relaxngschemas);
3533 if (wxschemas != NULL)
3534 xmlSchemaFree(wxschemas);
3535 xmlRelaxNGCleanupTypes();
3536#endif
3537#ifdef LIBXML_PATTERN_ENABLED
3538 if (patternc != NULL)
3539 xmlFreePattern(patternc);
3540#endif
3541 xmlCleanupParser();
3542 xmlMemoryDump();
3543
3544 return(progresult);
3545}
3546
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette