-
Notifications
You must be signed in to change notification settings - Fork 366
/
xmllint.c
3521 lines (3255 loc) · 91.9 KB
/
xmllint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* xmllint.c : a small tester program for XML input.
*
* See Copyright for the status of this software.
*
*/
#include "libxml.h"
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#include <sys/timeb.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif
#if HAVE_DECL_MMAP
#include <sys/mman.h>
/* seems needed for Solaris */
#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif
#endif
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/debugXML.h>
#include <libxml/xmlerror.h>
#ifdef LIBXML_XINCLUDE_ENABLED
#include <libxml/xinclude.h>
#endif
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
#include <libxml/xmlreader.h>
#ifdef LIBXML_SCHEMATRON_ENABLED
#include <libxml/schematron.h>
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
#endif
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
#ifdef LIBXML_C14N_ENABLED
#include <libxml/c14n.h>
#endif
#ifdef LIBXML_OUTPUT_ENABLED
#include <libxml/xmlsave.h>
#endif
#include "private/shell.h"
#ifdef XMLLINT_FUZZ
#define ERR_STREAM stdout
#else
#define ERR_STREAM stderr
#endif
#ifndef XML_XML_DEFAULT_CATALOG
#define XML_XML_DEFAULT_CATALOG "file://" SYSCONFDIR "/xml/catalog"
#endif
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
typedef enum {
XMLLINT_RETURN_OK = 0, /* No error */
XMLLINT_ERR_UNCLASS = 1, /* Unclassified */
XMLLINT_ERR_DTD = 2, /* Error in DTD */
XMLLINT_ERR_VALID = 3, /* Validation error */
XMLLINT_ERR_RDFILE = 4, /* CtxtReadFile error */
XMLLINT_ERR_SCHEMACOMP = 5, /* Schema compilation */
XMLLINT_ERR_OUT = 6, /* Error writing output */
XMLLINT_ERR_SCHEMAPAT = 7, /* Error in schema pattern */
/*XMLLINT_ERR_RDREGIS = 8,*/
XMLLINT_ERR_MEM = 9, /* Out of memory error */
XMLLINT_ERR_XPATH = 10, /* XPath evaluation error */
XMLLINT_ERR_XPATH_EMPTY = 11 /* XPath result is empty */
} xmllintReturnCode;
static int shell = 0;
#ifdef LIBXML_DEBUG_ENABLED
static int debugent = 0;
#endif
static int debug = 0;
static int maxmem = 0;
static int copy = 0;
static int noout = 0;
#ifdef LIBXML_OUTPUT_ENABLED
static const char *output = NULL;
static int format = 0;
static const char *encoding = NULL;
static int compress = 0;
#endif /* LIBXML_OUTPUT_ENABLED */
#ifdef LIBXML_VALID_ENABLED
static int postvalid = 0;
static const char *dtdvalid = NULL;
static const char *dtdvalidfpi = NULL;
static int insert = 0;
#endif
#ifdef LIBXML_SCHEMAS_ENABLED
static const char *relaxng = NULL;
static xmlRelaxNGPtr relaxngschemas = NULL;
static const char *schema = NULL;
static xmlSchemaPtr wxschemas = NULL;
#endif
#ifdef LIBXML_SCHEMATRON_ENABLED
static const char *schematron = NULL;
static xmlSchematronPtr wxschematron = NULL;
#endif
static int repeat = 0;
#if defined(LIBXML_HTML_ENABLED)
static int html = 0;
static int xmlout = 0;
#endif
static int htmlout = 0;
#ifdef LIBXML_PUSH_ENABLED
static int push = 0;
static const int pushsize = 4096;
#endif /* LIBXML_PUSH_ENABLED */
#if HAVE_DECL_MMAP
static int memory = 0;
static char *memoryData;
static size_t memorySize;
#endif
static int testIO = 0;
#ifdef LIBXML_XINCLUDE_ENABLED
static int xinclude = 0;
#endif
static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
static int quiet = 0;
static int timing = 0;
static int generate = 0;
static int dropdtd = 0;
#ifdef LIBXML_C14N_ENABLED
static int canonical = 0;
static int canonical_11 = 0;
static int exc_canonical = 0;
#endif
#ifdef LIBXML_READER_ENABLED
static int walker = 0;
#ifdef LIBXML_PATTERN_ENABLED
static const char *pattern = NULL;
static xmlPatternPtr patternc = NULL;
static xmlStreamCtxtPtr patstream = NULL;
#endif
#endif /* LIBXML_READER_ENABLED */
#ifdef LIBXML_XPATH_ENABLED
static const char *xpathquery = NULL;
#endif
static int options = XML_PARSE_COMPACT | XML_PARSE_BIG_LINES;
static unsigned maxAmpl = 0;
/************************************************************************
* *
* Entity loading control and customization. *
* *
************************************************************************/
#define MAX_PATHS 64
#ifdef _WIN32
# define PATH_SEPARATOR ';'
#else
# define PATH_SEPARATOR ':'
#endif
static xmlChar *paths[MAX_PATHS + 1];
static int nbpaths = 0;
static int load_trace = 0;
static
void parsePath(const xmlChar *path) {
const xmlChar *cur;
if (path == NULL)
return;
while (*path != 0) {
if (nbpaths >= MAX_PATHS) {
fprintf(ERR_STREAM, "MAX_PATHS reached: too many paths\n");
return;
}
cur = path;
while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
cur++;
path = cur;
while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
cur++;
if (cur != path) {
paths[nbpaths] = xmlStrndup(path, cur - path);
if (paths[nbpaths] != NULL)
nbpaths++;
path = cur;
}
}
}
static xmlResourceLoader defaultResourceLoader = NULL;
static int
xmllintResourceLoader(void *ctxt ATTRIBUTE_UNUSED, const char *URL,
const char *ID, xmlResourceType type, int flags,
xmlParserInputPtr *out) {
int code;
int i;
const char *lastsegment = URL;
const char *iter = URL;
if ((nbpaths > 0) && (iter != NULL)) {
while (*iter != 0) {
if (*iter == '/')
lastsegment = iter + 1;
iter++;
}
}
if (defaultResourceLoader != NULL)
code = defaultResourceLoader(NULL, URL, ID, type, flags, out);
else
code = xmlNewInputFromUrl(URL, flags, out);
if (code != XML_IO_ENOENT) {
if ((load_trace) && (code == XML_ERR_OK)) {
fprintf(ERR_STREAM, "Loaded URL=\"%s\" ID=\"%s\"\n",
URL, ID ? ID : "(null)");
}
return(code);
}
for (i = 0; i < nbpaths; i++) {
xmlChar *newURL;
newURL = xmlStrdup((const xmlChar *) paths[i]);
newURL = xmlStrcat(newURL, (const xmlChar *) "/");
newURL = xmlStrcat(newURL, (const xmlChar *) lastsegment);
if (newURL != NULL) {
if (defaultResourceLoader != NULL)
code = defaultResourceLoader(NULL, (const char *) newURL, ID,
type, flags, out);
else
code = xmlNewInputFromUrl((const char *) newURL, flags, out);
if (code != XML_IO_ENOENT) {
if ((load_trace) && (code == XML_ERR_OK)) {
fprintf(ERR_STREAM, "Loaded URL=\"%s\" ID=\"%s\"\n",
newURL, ID ? ID : "(null)");
}
xmlFree(newURL);
return(code);
}
xmlFree(newURL);
}
}
return(XML_IO_ENOENT);
}
/************************************************************************
* *
* Core parsing functions *
* *
************************************************************************/
static int
myRead(void *f, char *buf, int len) {
return(fread(buf, 1, len, (FILE *) f));
}
static int
myClose(void *context) {
FILE *f = (FILE *) context;
if (f == stdin)
return(0);
return(fclose(f));
}
static xmlDocPtr
parseXml(xmlParserCtxtPtr ctxt, const char *filename) {
xmlDocPtr doc;
xmlCtxtSetResourceLoader(ctxt, xmllintResourceLoader, NULL);
if (maxAmpl > 0)
xmlCtxtSetMaxAmplification(ctxt, maxAmpl);
#if HAVE_DECL_MMAP
if (memory) {
xmlParserInputPtr input;
input = xmlNewInputFromMemory(filename, memoryData, memorySize,
XML_INPUT_BUF_STATIC |
XML_INPUT_BUF_ZERO_TERMINATED);
if (input == NULL) {
progresult = XMLLINT_ERR_MEM;
return(NULL);
}
doc = xmlCtxtParseDocument(ctxt, input);
return(doc);
}
#endif
if (testIO) {
FILE *f;
if ((filename[0] == '-') && (filename[1] == 0)) {
f = stdin;
} else {
f = fopen(filename, "rb");
if (f == NULL) {
fprintf(ERR_STREAM, "Can't open %s\n", filename);
progresult = XMLLINT_ERR_RDFILE;
return(NULL);
}
}
doc = xmlCtxtReadIO(ctxt, myRead, myClose, f, filename, NULL,
options);
} else {
if (strcmp(filename, "-") == 0)
doc = xmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL, options);
else
doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
}
return(doc);
}
#ifdef LIBXML_HTML_ENABLED
static xmlDocPtr
parseHtml(htmlParserCtxtPtr ctxt, const char *filename) {
xmlDocPtr doc;
#if HAVE_DECL_MMAP
if (memory) {
xmlParserInputPtr input;
input = xmlNewInputFromMemory(filename, memoryData, memorySize,
XML_INPUT_BUF_STATIC |
XML_INPUT_BUF_ZERO_TERMINATED);
if (input == NULL) {
progresult = XMLLINT_ERR_MEM;
return(NULL);
}
doc = htmlCtxtParseDocument(ctxt, input);
return(doc);
}
#endif
if (strcmp(filename, "-") == 0)
doc = htmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL, options);
else
doc = htmlCtxtReadFile(ctxt, filename, NULL, options);
return(doc);
}
#endif /* LIBXML_HTML_ENABLED */
/************************************************************************
* *
* Memory allocation consumption debugging *
* *
************************************************************************/
static void
OOM(void)
{
fprintf(ERR_STREAM, "Ran out of memory needs > %d bytes\n", maxmem);
progresult = XMLLINT_ERR_MEM;
}
static void
myFreeFunc(void *mem)
{
xmlMemFree(mem);
}
static void *
myMallocFunc(size_t size)
{
void *ret;
ret = xmlMemMalloc(size);
if (ret != NULL) {
if (xmlMemUsed() > maxmem) {
OOM();
xmlMemFree(ret);
return (NULL);
}
}
return (ret);
}
static void *
myReallocFunc(void *mem, size_t size)
{
size_t oldsize = xmlMemSize(mem);
if (xmlMemUsed() + size - oldsize > (size_t) maxmem) {
OOM();
return (NULL);
}
return (xmlMemRealloc(mem, size));
}
static char *
myStrdupFunc(const char *str)
{
char *ret;
ret = xmlMemoryStrdup(str);
if (ret != NULL) {
if (xmlMemUsed() > maxmem) {
OOM();
xmlMemFree(ret);
return (NULL);
}
}
return (ret);
}
/************************************************************************
* *
* Internal timing routines to remove the necessity to have *
* unix-specific function calls. *
* *
************************************************************************/
#ifdef _WIN32
typedef __time64_t xmlSeconds;
#else
typedef time_t xmlSeconds;
#endif
typedef struct {
xmlSeconds sec;
int usec;
} xmlTime;
static xmlTime begin, end;
static void
getTime(xmlTime *time) {
#ifdef _WIN32
struct __timeb64 timebuffer;
_ftime64(&timebuffer);
time->sec = timebuffer.time;
time->usec = timebuffer.millitm * 1000;
#else /* _WIN32 */
struct timeval tv;
gettimeofday(&tv, NULL);
time->sec = tv.tv_sec;
time->usec = tv.tv_usec;
#endif /* _WIN32 */
}
/*
* startTimer: call where you want to start timing
*/
static void
startTimer(void)
{
getTime(&begin);
}
/*
* endTimer: call where you want to stop timing and to print out a
* message about the timing performed; format is a printf
* type argument
*/
static void LIBXML_ATTR_FORMAT(1,2)
endTimer(const char *fmt, ...)
{
xmlSeconds msec;
va_list ap;
getTime(&end);
msec = end.sec - begin.sec;
msec *= 1000;
msec += (end.usec - begin.usec) / 1000;
va_start(ap, fmt);
vfprintf(ERR_STREAM, fmt, ap);
va_end(ap);
fprintf(ERR_STREAM, " took %ld ms\n", (long) msec);
}
/************************************************************************
* *
* HTML output *
* *
************************************************************************/
static char buffer[50000];
static int htmlBufLen;
static void
xmlHTMLEncodeSend(void) {
char *result;
/*
* xmlEncodeEntitiesReentrant assumes valid UTF-8, but the buffer might
* end with a truncated UTF-8 sequence. This is a hack to at least avoid
* an out-of-bounds read.
*/
memset(&buffer[sizeof(buffer)-4], 0, 4);
result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
if (result) {
fprintf(ERR_STREAM, "%s", result);
xmlFree(result);
}
htmlBufLen = 0;
}
static void
xmlHTMLBufCat(void *data ATTRIBUTE_UNUSED, const char *fmt, ...) {
va_list ap;
int res;
va_start(ap, fmt);
res = vsnprintf(&buffer[htmlBufLen], sizeof(buffer) - htmlBufLen, fmt, ap);
va_end(ap);
if (res > 0) {
if ((size_t) res > sizeof(buffer) - htmlBufLen - 1)
htmlBufLen = sizeof(buffer) - 1;
else
htmlBufLen += res;
}
}
/**
* xmlHTMLError:
* @ctx: an XML parser context
* @msg: the message to display/transmit
* @...: extra parameters for the message display
*
* Display and format an error messages, gives file, line, position and
* extra parameters.
*/
static void
xmlHTMLError(void *vctxt, const xmlError *error)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) vctxt;
xmlParserInputPtr input;
xmlGenericErrorFunc oldError;
void *oldErrorCtxt;
input = ctxt->input;
if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
input = ctxt->inputTab[ctxt->inputNr - 2];
}
oldError = xmlGenericError;
oldErrorCtxt = xmlGenericErrorContext;
xmlSetGenericErrorFunc(NULL, xmlHTMLBufCat);
fprintf(ERR_STREAM, "<p>");
xmlParserPrintFileInfo(input);
xmlHTMLEncodeSend();
fprintf(ERR_STREAM, "<b>%s%s</b>: ",
(error->domain == XML_FROM_VALID) ||
(error->domain == XML_FROM_DTD) ? "validity " : "",
error->level == XML_ERR_WARNING ? "warning" : "error");
snprintf(buffer, sizeof(buffer), "%s", error->message);
xmlHTMLEncodeSend();
fprintf(ERR_STREAM, "</p>\n");
if (input != NULL) {
fprintf(ERR_STREAM, "<pre>\n");
xmlParserPrintFileContext(input);
xmlHTMLEncodeSend();
fprintf(ERR_STREAM, "</pre>");
}
xmlSetGenericErrorFunc(oldErrorCtxt, oldError);
}
/************************************************************************
* *
* SAX based tests *
* *
************************************************************************/
/*
* empty SAX block
*/
static xmlSAXHandler emptySAXHandlerStruct = {
NULL, /* internalSubset */
NULL, /* isStandalone */
NULL, /* hasInternalSubset */
NULL, /* hasExternalSubset */
NULL, /* resolveEntity */
NULL, /* getEntity */
NULL, /* entityDecl */
NULL, /* notationDecl */
NULL, /* attributeDecl */
NULL, /* elementDecl */
NULL, /* unparsedEntityDecl */
NULL, /* setDocumentLocator */
NULL, /* startDocument */
NULL, /* endDocument */
NULL, /* startElement */
NULL, /* endElement */
NULL, /* reference */
NULL, /* characters */
NULL, /* ignorableWhitespace */
NULL, /* processingInstruction */
NULL, /* comment */
NULL, /* xmlParserWarning */
NULL, /* xmlParserError */
NULL, /* xmlParserError */
NULL, /* getParameterEntity */
NULL, /* cdataBlock; */
NULL, /* externalSubset; */
XML_SAX2_MAGIC,
NULL,
NULL, /* startElementNs */
NULL, /* endElementNs */
NULL /* xmlStructuredErrorFunc */
};
static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
extern xmlSAXHandlerPtr debugSAXHandler;
static int callbacks;
/**
* isStandaloneDebug:
* @ctxt: An XML parser context
*
* Is this document tagged standalone ?
*
* Returns 1 if true
*/
static int
isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
{
callbacks++;
if (noout)
return(0);
fprintf(stdout, "SAX.isStandalone()\n");
return(0);
}
/**
* hasInternalSubsetDebug:
* @ctxt: An XML parser context
*
* Does this document has an internal subset
*
* Returns 1 if true
*/
static int
hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
{
callbacks++;
if (noout)
return(0);
fprintf(stdout, "SAX.hasInternalSubset()\n");
return(0);
}
/**
* hasExternalSubsetDebug:
* @ctxt: An XML parser context
*
* Does this document has an external subset
*
* Returns 1 if true
*/
static int
hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
{
callbacks++;
if (noout)
return(0);
fprintf(stdout, "SAX.hasExternalSubset()\n");
return(0);
}
/**
* internalSubsetDebug:
* @ctxt: An XML parser context
*
* Does this document has an internal subset
*/
static void
internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
const xmlChar *ExternalID, const xmlChar *SystemID)
{
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.internalSubset(%s,", name);
if (ExternalID == NULL)
fprintf(stdout, " ,");
else
fprintf(stdout, " %s,", ExternalID);
if (SystemID == NULL)
fprintf(stdout, " )\n");
else
fprintf(stdout, " %s)\n", SystemID);
}
/**
* externalSubsetDebug:
* @ctxt: An XML parser context
*
* Does this document has an external subset
*/
static void
externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
const xmlChar *ExternalID, const xmlChar *SystemID)
{
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.externalSubset(%s,", name);
if (ExternalID == NULL)
fprintf(stdout, " ,");
else
fprintf(stdout, " %s,", ExternalID);
if (SystemID == NULL)
fprintf(stdout, " )\n");
else
fprintf(stdout, " %s)\n", SystemID);
}
/**
* resolveEntityDebug:
* @ctxt: An XML parser context
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
*
* Special entity resolver, better left to the parser, it has
* more context than the application layer.
* The default behaviour is to NOT resolve the entities, in that case
* the ENTITY_REF nodes are built in the structure (and the parameter
* values).
*
* Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
*/
static xmlParserInputPtr
resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
{
callbacks++;
if (noout)
return(NULL);
/* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
fprintf(stdout, "SAX.resolveEntity(");
if (publicId != NULL)
fprintf(stdout, "%s", (char *)publicId);
else
fprintf(stdout, " ");
if (systemId != NULL)
fprintf(stdout, ", %s)\n", (char *)systemId);
else
fprintf(stdout, ", )\n");
return(NULL);
}
/**
* getEntityDebug:
* @ctxt: An XML parser context
* @name: The entity name
*
* Get an entity by name
*
* Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
*/
static xmlEntityPtr
getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
{
callbacks++;
if (noout)
return(NULL);
fprintf(stdout, "SAX.getEntity(%s)\n", name);
return(NULL);
}
/**
* getParameterEntityDebug:
* @ctxt: An XML parser context
* @name: The entity name
*
* Get a parameter entity by name
*
* Returns the xmlParserInputPtr
*/
static xmlEntityPtr
getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
{
callbacks++;
if (noout)
return(NULL);
fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
return(NULL);
}
/**
* entityDeclDebug:
* @ctxt: An XML parser context
* @name: the entity name
* @type: the entity type
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
* @content: the entity value (without processing).
*
* An entity definition has been parsed
*/
static void
entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
{
const xmlChar *nullstr = BAD_CAST "(null)";
/* not all libraries handle printing null pointers nicely */
if (publicId == NULL)
publicId = nullstr;
if (systemId == NULL)
systemId = nullstr;
if (content == NULL)
content = (xmlChar *)nullstr;
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
name, type, publicId, systemId, content);
}
/**
* attributeDeclDebug:
* @ctxt: An XML parser context
* @name: the attribute name
* @type: the attribute type
*
* An attribute definition has been parsed
*/
static void
attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
const xmlChar * name, int type, int def,
const xmlChar * defaultValue, xmlEnumerationPtr tree)
{
callbacks++;
if (noout)
return;
if (defaultValue == NULL)
fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
elem, name, type, def);
else
fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
elem, name, type, def, defaultValue);
xmlFreeEnumeration(tree);
}
/**
* elementDeclDebug:
* @ctxt: An XML parser context
* @name: the element name
* @type: the element type
* @content: the element value (without processing).
*
* An element definition has been parsed
*/
static void
elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
xmlElementContentPtr content ATTRIBUTE_UNUSED)
{
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
name, type);
}
/**
* notationDeclDebug:
* @ctxt: An XML parser context
* @name: The name of the notation
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
*
* What to do when a notation declaration has been parsed.
*/
static void
notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
const xmlChar *publicId, const xmlChar *systemId)
{
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
(char *) name, (char *) publicId, (char *) systemId);
}
/**
* unparsedEntityDeclDebug:
* @ctxt: An XML parser context
* @name: The name of the entity
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
* @notationName: the name of the notation
*
* What to do when an unparsed entity declaration is parsed
*/
static void
unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
const xmlChar *publicId, const xmlChar *systemId,
const xmlChar *notationName)
{
const xmlChar *nullstr = BAD_CAST "(null)";
if (publicId == NULL)
publicId = nullstr;
if (systemId == NULL)
systemId = nullstr;
if (notationName == NULL)
notationName = nullstr;
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
(char *) name, (char *) publicId, (char *) systemId,
(char *) notationName);
}
/**
* setDocumentLocatorDebug:
* @ctxt: An XML parser context
* @loc: A SAX Locator
*
* Receive the document locator at startup, actually xmlDefaultSAXLocator
* Everything is available on the context, so this is useless in our case.
*/
static void
setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
{
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.setDocumentLocator()\n");
}
/**
* startDocumentDebug:
* @ctxt: An XML parser context
*
* called when the document start being processed.
*/
static void
startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
{
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.startDocument()\n");
}
/**
* endDocumentDebug:
* @ctxt: An XML parser context
*
* called when the document end has been detected.
*/
static void
endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
{
callbacks++;
if (noout)
return;
fprintf(stdout, "SAX.endDocument()\n");
}
/**
* startElementDebug:
* @ctxt: An XML parser context
* @name: The element name