xmllint.c 98 KB
Newer Older
1
2
3
4
5
/*
 * xmllint.c : a small tester program for XML input.
 *
 * See Copyright for the status of this software.
 *
6
 * daniel@veillard.com
7
8
 */

Bjorn Reese's avatar
Bjorn Reese committed
9
#include "libxml.h"
10
11
12

#include <string.h>
#include <stdarg.h>
13
14
#include <assert.h>

15
#ifdef HAVE_SYS_TIME_H
16
#include <sys/time.h>
17
#endif
18
19
20
#ifdef HAVE_TIME_H
#include <time.h>
#endif
21

22
23
24
#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#endif
25
26
27
28
29
30
31
32
33
34
35
36
37

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
38
39
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
40
41
42
43
/* seems needed for Solaris */
#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif
44
#endif
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#ifdef HAVE_LIBHISTORY
#include <readline/history.h>
#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>
63
#include <libxml/xmlerror.h>
64
65
66
#ifdef LIBXML_XINCLUDE_ENABLED
#include <libxml/xinclude.h>
#endif
67
68
69
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
70
#include <libxml/globals.h>
71
#include <libxml/xmlreader.h>
72
73
74
#ifdef LIBXML_SCHEMATRON_ENABLED
#include <libxml/schematron.h>
#endif
75
76
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/relaxng.h>
77
#include <libxml/xmlschemas.h>
78
#endif
79
80
81
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
82
83
84
#ifdef LIBXML_C14N_ENABLED
#include <libxml/c14n.h>
#endif
85
86
87
#ifdef LIBXML_OUTPUT_ENABLED
#include <libxml/xmlsave.h>
#endif
88

89
90
91
92
#ifndef XML_XML_DEFAULT_CATALOG
#define XML_XML_DEFAULT_CATALOG "file:///etc/xml/catalog"
#endif

93
94
typedef enum {
    XMLLINT_RETURN_OK = 0,	/* No error */
95
96
97
98
99
100
101
102
103
104
    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,	/* Error in Reader registration */
    XMLLINT_ERR_MEM = 9,	/* Out of memory error */
    XMLLINT_ERR_XPATH = 10	/* XPath evaluation error */
105
} xmllintReturnCode;
106
107
108
109
#ifdef LIBXML_DEBUG_ENABLED
static int shell = 0;
static int debugent = 0;
#endif
110
static int debug = 0;
111
static int maxmem = 0;
112
#ifdef LIBXML_TREE_ENABLED
113
static int copy = 0;
114
#endif /* LIBXML_TREE_ENABLED */
115
116
static int recovery = 0;
static int noent = 0;
117
static int noenc = 0;
118
static int noblanks = 0;
119
120
static int noout = 0;
static int nowrap = 0;
121
122
123
124
#ifdef LIBXML_OUTPUT_ENABLED
static int format = 0;
static const char *output = NULL;
static int compress = 0;
125
static int oldout = 0;
126
#endif /* LIBXML_OUTPUT_ENABLED */
127
#ifdef LIBXML_VALID_ENABLED
128
129
static int valid = 0;
static int postvalid = 0;
130
static char * dtdvalid = NULL;
131
static char * dtdvalidfpi = NULL;
132
#endif
133
134
135
#ifdef LIBXML_SCHEMAS_ENABLED
static char * relaxng = NULL;
static xmlRelaxNGPtr relaxngschemas = NULL;
136
137
static char * schema = NULL;
static xmlSchemaPtr wxschemas = NULL;
138
#endif
139
#ifdef LIBXML_SCHEMATRON_ENABLED
140
141
142
static char * schematron = NULL;
static xmlSchematronPtr wxschematron = NULL;
#endif
143
144
static int repeat = 0;
static int insert = 0;
145
#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED)
146
static int html = 0;
147
static int xmlout = 0;
148
#endif
149
static int htmlout = 0;
150
151
152
#if defined(LIBXML_HTML_ENABLED)
static int nodefdtd = 0;
#endif
153
#ifdef LIBXML_PUSH_ENABLED
154
static int push = 0;
155
static int pushsize = 4096;
156
#endif /* LIBXML_PUSH_ENABLED */
157
#ifdef HAVE_MMAP
158
159
static int memory = 0;
#endif
160
static int testIO = 0;
161
static char *encoding = NULL;
162
163
164
#ifdef LIBXML_XINCLUDE_ENABLED
static int xinclude = 0;
#endif
165
static int dtdattrs = 0;
166
static int loaddtd = 0;
167
static xmllintReturnCode progresult = XMLLINT_RETURN_OK;
168
static int timing = 0;
169
static int generate = 0;
170
static int dropdtd = 0;
171
172
173
174
#ifdef LIBXML_CATALOG_ENABLED
static int catalogs = 0;
static int nocatalogs = 0;
#endif
175
176
#ifdef LIBXML_C14N_ENABLED
static int canonical = 0;
177
static int canonical_11 = 0;
178
static int exc_canonical = 0;
179
#endif
180
#ifdef LIBXML_READER_ENABLED
181
static int stream = 0;
182
static int walker = 0;
183
184
185
186
187
#ifdef LIBXML_PATTERN_ENABLED
static const char *pattern = NULL;
static xmlPatternPtr patternc = NULL;
static xmlStreamCtxtPtr patstream = NULL;
#endif
188
#endif /* LIBXML_READER_ENABLED */
189
static int chkregister = 0;
190
static int nbregister = 0;
191
#ifdef LIBXML_SAX1_ENABLED
192
static int sax1 = 0;
193
#endif /* LIBXML_SAX1_ENABLED */
194
195
196
#ifdef LIBXML_XPATH_ENABLED
static const char *xpathquery = NULL;
#endif
197
static int options = XML_PARSE_COMPACT | XML_PARSE_BIG_LINES;
198
static int sax = 0;
199
static int oldxml10 = 0;
200

201
202
203
204
205
206
/************************************************************************
 *									*
 *		 Entity loading control and customization.		*
 *									*
 ************************************************************************/
#define MAX_PATHS 64
207
208
209
210
211
#ifdef _WIN32
# define PATH_SEPARATOR ';'
#else
# define PATH_SEPARATOR ':'
#endif
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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(stderr, "MAX_PATHS reached: too many paths\n");
	    return;
	}
	cur = path;
228
	while ((*cur == ' ') || (*cur == PATH_SEPARATOR))
229
230
	    cur++;
	path = cur;
231
	while ((*cur != 0) && (*cur != ' ') && (*cur != PATH_SEPARATOR))
232
233
234
235
236
237
238
239
240
241
	    cur++;
	if (cur != path) {
	    paths[nbpaths] = xmlStrndup(path, cur - path);
	    if (paths[nbpaths] != NULL)
		nbpaths++;
	    path = cur;
	}
    }
}

242
static xmlExternalEntityLoader defaultEntityLoader = NULL;
243

244
static xmlParserInputPtr
245
246
247
248
xmllintExternalEntityLoader(const char *URL, const char *ID,
			     xmlParserCtxtPtr ctxt) {
    xmlParserInputPtr ret;
    warningSAXFunc warning = NULL;
249
    errorSAXFunc err = NULL;
250
251
252
253
254

    int i;
    const char *lastsegment = URL;
    const char *iter = URL;

255
    if ((nbpaths > 0) && (iter != NULL)) {
256
257
258
259
260
261
262
263
264
	while (*iter != 0) {
	    if (*iter == '/')
		lastsegment = iter + 1;
	    iter++;
	}
    }

    if ((ctxt != NULL) && (ctxt->sax != NULL)) {
	warning = ctxt->sax->warning;
265
	err = ctxt->sax->error;
266
	ctxt->sax->warning = NULL;
267
	ctxt->sax->error = NULL;
268
269
270
271
272
273
274
    }

    if (defaultEntityLoader != NULL) {
	ret = defaultEntityLoader(URL, ID, ctxt);
	if (ret != NULL) {
	    if (warning != NULL)
		ctxt->sax->warning = warning;
275
276
	    if (err != NULL)
		ctxt->sax->error = err;
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
	    if (load_trace) {
		fprintf \
			(stderr,
			 "Loaded URL=\"%s\" ID=\"%s\"\n",
			 URL ? URL : "(null)",
			 ID ? ID : "(null)");
	    }
	    return(ret);
	}
    }
    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) {
	    ret = defaultEntityLoader((const char *)newURL, ID, ctxt);
	    if (ret != NULL) {
		if (warning != NULL)
		    ctxt->sax->warning = warning;
298
299
		if (err != NULL)
		    ctxt->sax->error = err;
300
301
		if (load_trace) {
		    fprintf \
302
303
			(stderr,
			 "Loaded URL=\"%s\" ID=\"%s\"\n",
304
			 newURL,
305
			 ID ? ID : "(null)");
306
307
308
309
310
311
312
		}
		xmlFree(newURL);
		return(ret);
	    }
	    xmlFree(newURL);
	}
    }
313
314
    if (err != NULL)
        ctxt->sax->error = err;
315
316
317
318
319
320
321
322
323
    if (warning != NULL) {
	ctxt->sax->warning = warning;
	if (URL != NULL)
	    warning(ctxt, "failed to load external entity \"%s\"\n", URL);
	else if (ID != NULL)
	    warning(ctxt, "failed to load external entity \"%s\"\n", ID);
    }
    return(NULL);
}
324
/************************************************************************
325
 *									*
326
 * Memory allocation consumption debugging				*
327
 *									*
328
329
 ************************************************************************/

330
331
332
static void
OOM(void)
{
333
    fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem);
334
    progresult = XMLLINT_ERR_MEM;
335
336
}

337
338
339
static void
myFreeFunc(void *mem)
{
340
341
    xmlMemFree(mem);
}
342
343
344
static void *
myMallocFunc(size_t size)
{
345
346
347
348
349
    void *ret;

    ret = xmlMemMalloc(size);
    if (ret != NULL) {
        if (xmlMemUsed() > maxmem) {
350
351
352
353
            OOM();
            xmlMemFree(ret);
            return (NULL);
        }
354
    }
355
    return (ret);
356
}
357
358
359
static void *
myReallocFunc(void *mem, size_t size)
{
360
361
362
363
364
    void *ret;

    ret = xmlMemRealloc(mem, size);
    if (ret != NULL) {
        if (xmlMemUsed() > maxmem) {
365
366
367
368
            OOM();
            xmlMemFree(ret);
            return (NULL);
        }
369
    }
370
    return (ret);
371
}
372
373
374
static char *
myStrdupFunc(const char *str)
{
375
376
377
378
379
    char *ret;

    ret = xmlMemoryStrdup(str);
    if (ret != NULL) {
        if (xmlMemUsed() > maxmem) {
380
381
382
383
            OOM();
            xmlFree(ret);
            return (NULL);
        }
384
    }
385
    return (ret);
386
387
}
/************************************************************************
388
 *									*
389
390
 * Internal timing routines to remove the necessity to have		*
 * unix-specific function calls.					*
391
 *									*
392
 ************************************************************************/
393

394
#ifndef HAVE_GETTIMEOFDAY
395
396
397
398
#ifdef HAVE_SYS_TIMEB_H
#ifdef HAVE_SYS_TIME_H
#ifdef HAVE_FTIME

399
static int
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
my_gettimeofday(struct timeval *tvp, void *tzp)
{
	struct timeb timebuffer;

	ftime(&timebuffer);
	if (tvp) {
		tvp->tv_sec = timebuffer.time;
		tvp->tv_usec = timebuffer.millitm * 1000L;
	}
	return (0);
}
#define HAVE_GETTIMEOFDAY 1
#define gettimeofday my_gettimeofday

#endif /* HAVE_FTIME */
#endif /* HAVE_SYS_TIME_H */
#endif /* HAVE_SYS_TIMEB_H */
#endif /* !HAVE_GETTIMEOFDAY */

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#if defined(HAVE_GETTIMEOFDAY)
static struct timeval begin, end;

/*
 * startTimer: call where you want to start timing
 */
static void
startTimer(void)
{
    gettimeofday(&begin, NULL);
}

/*
 * endTimer: call where you want to stop timing and to print out a
 *           message about the timing performed; format is a printf
 *           type argument
 */
436
static void XMLCDECL LIBXML_ATTR_FORMAT(1,2)
437
endTimer(const char *fmt, ...)
438
439
440
441
442
443
444
445
446
447
448
449
{
    long msec;
    va_list ap;

    gettimeofday(&end, NULL);
    msec = end.tv_sec - begin.tv_sec;
    msec *= 1000;
    msec += (end.tv_usec - begin.tv_usec) / 1000;

#ifndef HAVE_STDARG_H
#error "endTimer required stdarg functions"
#endif
450
451
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
452
453
454
455
456
457
458
459
460
461
    va_end(ap);

    fprintf(stderr, " took %ld ms\n", msec);
}
#elif defined(HAVE_TIME_H)
/*
 * No gettimeofday function, so we have to make do with calling clock.
 * This is obviously less accurate, but there's little we can do about
 * that.
 */
462
463
464
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 100
#endif
465
466
467
468
469
470
471

static clock_t begin, end;
static void
startTimer(void)
{
    begin = clock();
}
472
static void XMLCDECL LIBXML_ATTR_FORMAT(1,2)
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
endTimer(const char *fmt, ...)
{
    long msec;
    va_list ap;

    end = clock();
    msec = ((end - begin) * 1000) / CLOCKS_PER_SEC;

#ifndef HAVE_STDARG_H
#error "endTimer required stdarg functions"
#endif
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, " took %ld ms\n", msec);
}
#else

/*
 * We don't have a gettimeofday or time.h, so we just don't do timing
 */
static void
startTimer(void)
{
    /*
     * Do nothing
     */
}
501
static void XMLCDECL LIBXML_ATTR_FORMAT(1,2)
502
503
504
505
506
507
endTimer(char *format, ...)
{
    /*
     * We cannot do anything because we don't have a timing function
     */
#ifdef HAVE_STDARG_H
508
    va_list ap;
509
510
511
    va_start(ap, format);
    vfprintf(stderr, format, ap);
    va_end(ap);
512
    fprintf(stderr, " was not timed\n");
513
514
515
516
517
518
519
#else
    /* We don't have gettimeofday, time or stdarg.h, what crazy world is
     * this ?!
     */
#endif
}
#endif
520
/************************************************************************
521
 *									*
Jared Yanovich's avatar
Jared Yanovich committed
522
 *			HTML output					*
523
 *									*
524
 ************************************************************************/
525
static char buffer[50000];
526

527
static void
528
529
530
xmlHTMLEncodeSend(void) {
    char *result;

531
532
533
534
535
536
    /*
     * 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);
537
538
    result = (char *) xmlEncodeEntitiesReentrant(NULL, BAD_CAST buffer);
    if (result) {
539
	xmlGenericError(xmlGenericErrorContext, "%s", result);
540
541
542
543
544
545
546
547
	xmlFree(result);
    }
    buffer[0] = 0;
}

/**
 * xmlHTMLPrintFileInfo:
 * @input:  an xmlParserInputPtr input
548
 *
Nick Wellnhofer's avatar
Nick Wellnhofer committed
549
 * Displays the associated file and line information for the current input
550
551
 */

552
static void
553
xmlHTMLPrintFileInfo(xmlParserInputPtr input) {
554
    int len;
555
    xmlGenericError(xmlGenericErrorContext, "<p>");
556
557

    len = strlen(buffer);
558
559
    if (input != NULL) {
	if (input->filename) {
560
	    snprintf(&buffer[len], sizeof(buffer) - len, "%s:%d: ", input->filename,
561
562
		    input->line);
	} else {
563
	    snprintf(&buffer[len], sizeof(buffer) - len, "Entity: line %d: ", input->line);
564
565
566
567
568
569
570
571
	}
    }
    xmlHTMLEncodeSend();
}

/**
 * xmlHTMLPrintFileContext:
 * @input:  an xmlParserInputPtr input
572
 *
573
574
575
 * Displays current context within the input content for error tracking
 */

576
static void
577
578
xmlHTMLPrintFileContext(xmlParserInputPtr input) {
    const xmlChar *cur, *base;
579
    int len;
580
581
582
    int n;

    if (input == NULL) return;
583
    xmlGenericError(xmlGenericErrorContext, "<pre>\n");
584
585
586
587
588
589
590
591
592
593
594
595
    cur = input->cur;
    base = input->base;
    while ((cur > base) && ((*cur == '\n') || (*cur == '\r'))) {
	cur--;
    }
    n = 0;
    while ((n++ < 80) && (cur > base) && (*cur != '\n') && (*cur != '\r'))
        cur--;
    if ((*cur == '\n') || (*cur == '\r')) cur++;
    base = cur;
    n = 0;
    while ((*cur != 0) && (*cur != '\n') && (*cur != '\r') && (n < 79)) {
596
	len = strlen(buffer);
597
        snprintf(&buffer[len], sizeof(buffer) - len, "%c",
598
		    (unsigned char) *cur++);
599
600
	n++;
    }
601
602
    len = strlen(buffer);
    snprintf(&buffer[len], sizeof(buffer) - len, "\n");
603
604
605
606
607
    cur = input->cur;
    while ((*cur == '\n') || (*cur == '\r'))
	cur--;
    n = 0;
    while ((cur != base) && (n++ < 80)) {
608
609
	len = strlen(buffer);
        snprintf(&buffer[len], sizeof(buffer) - len, " ");
610
611
        base++;
    }
612
613
    len = strlen(buffer);
    snprintf(&buffer[len], sizeof(buffer) - len, "^\n");
614
    xmlHTMLEncodeSend();
615
    xmlGenericError(xmlGenericErrorContext, "</pre>");
616
617
618
619
620
621
622
}

/**
 * xmlHTMLError:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
623
 *
624
625
626
 * Display and format an error messages, gives file, line, position and
 * extra parameters.
 */
627
static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
628
629
630
631
632
xmlHTMLError(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input;
    va_list args;
633
    int len;
634
635
636
637
638
639

    buffer[0] = 0;
    input = ctxt->input;
    if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
        input = ctxt->inputTab[ctxt->inputNr - 2];
    }
640

641
642
    xmlHTMLPrintFileInfo(input);

643
    xmlGenericError(xmlGenericErrorContext, "<b>error</b>: ");
644
    va_start(args, msg);
645
646
    len = strlen(buffer);
    vsnprintf(&buffer[len],  sizeof(buffer) - len, msg, args);
647
648
    va_end(args);
    xmlHTMLEncodeSend();
649
    xmlGenericError(xmlGenericErrorContext, "</p>\n");
650
651
652
653
654
655
656
657
658
659

    xmlHTMLPrintFileContext(input);
    xmlHTMLEncodeSend();
}

/**
 * xmlHTMLWarning:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
660
 *
661
662
663
 * Display and format a warning messages, gives file, line, position and
 * extra parameters.
 */
664
static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
665
666
667
668
669
xmlHTMLWarning(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input;
    va_list args;
670
    int len;
671
672
673
674
675
676

    buffer[0] = 0;
    input = ctxt->input;
    if ((input != NULL) && (input->filename == NULL) && (ctxt->inputNr > 1)) {
        input = ctxt->inputTab[ctxt->inputNr - 2];
    }
677

678
679

    xmlHTMLPrintFileInfo(input);
680

681
    xmlGenericError(xmlGenericErrorContext, "<b>warning</b>: ");
682
    va_start(args, msg);
683
    len = strlen(buffer);
684
    vsnprintf(&buffer[len],  sizeof(buffer) - len, msg, args);
685
686
    va_end(args);
    xmlHTMLEncodeSend();
687
    xmlGenericError(xmlGenericErrorContext, "</p>\n");
688
689
690
691
692
693
694
695
696
697

    xmlHTMLPrintFileContext(input);
    xmlHTMLEncodeSend();
}

/**
 * xmlHTMLValidityError:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
698
 *
699
700
701
 * Display and format an validity error messages, gives file,
 * line, position and extra parameters.
 */
702
static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
703
704
705
706
707
xmlHTMLValidityError(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input;
    va_list args;
708
    int len;
709
710
711
712
713

    buffer[0] = 0;
    input = ctxt->input;
    if ((input->filename == NULL) && (ctxt->inputNr > 1))
        input = ctxt->inputTab[ctxt->inputNr - 2];
714

715
716
    xmlHTMLPrintFileInfo(input);

717
    xmlGenericError(xmlGenericErrorContext, "<b>validity error</b>: ");
718
    len = strlen(buffer);
719
    va_start(args, msg);
720
    vsnprintf(&buffer[len],  sizeof(buffer) - len, msg, args);
721
722
    va_end(args);
    xmlHTMLEncodeSend();
723
    xmlGenericError(xmlGenericErrorContext, "</p>\n");
724
725
726

    xmlHTMLPrintFileContext(input);
    xmlHTMLEncodeSend();
727
    progresult = XMLLINT_ERR_VALID;
728
729
730
731
732
733
734
}

/**
 * xmlHTMLValidityWarning:
 * @ctx:  an XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
735
 *
736
737
738
 * Display and format a validity warning messages, gives file, line,
 * position and extra parameters.
 */
739
static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
740
741
742
743
744
xmlHTMLValidityWarning(void *ctx, const char *msg, ...)
{
    xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
    xmlParserInputPtr input;
    va_list args;
745
    int len;
746
747
748
749
750
751
752

    buffer[0] = 0;
    input = ctxt->input;
    if ((input->filename == NULL) && (ctxt->inputNr > 1))
        input = ctxt->inputTab[ctxt->inputNr - 2];

    xmlHTMLPrintFileInfo(input);
753

754
    xmlGenericError(xmlGenericErrorContext, "<b>validity warning</b>: ");
755
    va_start(args, msg);
756
    len = strlen(buffer);
757
    vsnprintf(&buffer[len],  sizeof(buffer) - len, msg, args);
758
759
    va_end(args);
    xmlHTMLEncodeSend();
760
    xmlGenericError(xmlGenericErrorContext, "</p>\n");
761
762
763
764
765
766

    xmlHTMLPrintFileContext(input);
    xmlHTMLEncodeSend();
}

/************************************************************************
767
768
769
 *									*
 *			Shell Interface					*
 *									*
770
 ************************************************************************/
771
#ifdef LIBXML_DEBUG_ENABLED
772
#ifdef LIBXML_XPATH_ENABLED
773
774
775
776
777
/**
 * xmlShellReadline:
 * @prompt:  the prompt value
 *
 * Read a string
778
 *
779
780
781
 * Returns a pointer to it or NULL on EOF the caller is expected to
 *     free the returned string.
 */
782
static char *
783
784
785
786
787
788
789
790
791
792
793
794
795
796
xmlShellReadline(char *prompt) {
#ifdef HAVE_LIBREADLINE
    char *line_read;

    /* Get a line from the user. */
    line_read = readline (prompt);

    /* If the line has any text in it, save it on the history. */
    if (line_read && *line_read)
	add_history (line_read);

    return (line_read);
#else
    char line_read[501];
797
798
    char *ret;
    int len;
799
800
801

    if (prompt != NULL)
	fprintf(stdout, "%s", prompt);
802
    fflush(stdout);
803
804
805
    if (!fgets(line_read, 500, stdin))
        return(NULL);
    line_read[500] = 0;
806
807
808
809
810
811
    len = strlen(line_read);
    ret = (char *) malloc(len + 1);
    if (ret != NULL) {
	memcpy (ret, line_read, len + 1);
    }
    return(ret);
812
813
#endif
}
814
#endif /* LIBXML_XPATH_ENABLED */
815
#endif /* LIBXML_DEBUG_ENABLED */
816

817
/************************************************************************
818
819
820
 *									*
 *			I/O Interfaces					*
 *									*
821
822
 ************************************************************************/

Nick Wellnhofer's avatar
Nick Wellnhofer committed
823
824
static int myRead(void *f, char *buf, int len) {
    return(fread(buf, 1, len, (FILE *) f));
825
}
Nick Wellnhofer's avatar
Nick Wellnhofer committed
826
827
828
829
830
static int myClose(void *context) {
    FILE *f = (FILE *) context;
    if (f == stdin)
        return(0);
    return(fclose(f));
831
832
}

833
834
/************************************************************************
 *									*
835
 *			SAX based tests					*
836
837
838
839
840
841
 *									*
 ************************************************************************/

/*
 * empty SAX block
 */
842
static xmlSAXHandler emptySAXHandlerStruct = {
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
    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; */
870
    XML_SAX2_MAGIC,
871
872
873
874
875
876
    NULL,
    NULL, /* startElementNs */
    NULL, /* endElementNs */
    NULL  /* xmlStructuredErrorFunc */
};

877
static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
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
1001
1002
1003
1004
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; */

1005

1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
    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
1060
1061
 * @name:  the entity name
 * @type:  the entity type
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
 * @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
1090
1091
 * @name:  the attribute name
 * @type:  the attribute type
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
 *
 * 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
1115
1116
 * @name:  the element name
 * @type:  the element type
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
 * @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
 *
 * called when an opening tag has been processed.
 */
static void
startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
{
    int i;

    callbacks++;
    if (noout)
	return;
    fprintf(stdout, "SAX.startElement(%s", (char *) name);
    if (atts != NULL) {
        for (i = 0;(atts[i] != NULL);i++) {
	    fprintf(stdout, ", %s='", atts[i++]);
	    if (atts[i] != NULL)
	        fprintf(stdout, "%s'", atts[i]);
	}
    }
    fprintf(stdout, ")\n");
}

/**
 * endElementDebug:
 * @ctxt:  An XML parser context
 * @name:  The element name
 *
 * called when the end of an element has been detected.
 */
static void
endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
{
    callbacks++;
    if (noout)
	return;
    fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
}

/**
 * charactersDebug:
 * @ctxt:  An XML parser context
 * @ch:  a xmlChar string
 * @len: the number of xmlChar
 *
 * receiving some chars from the parser.
 * Question: how much at a time ???
 */
static void
charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
{
    char out[40];
    int i;

    callbacks++;
    if (noout)
	return;
    for (i = 0;(i<len) && (i < 30);i++)
	out[i] = ch[i];
    out[i] = 0;

    fprintf(stdout, "SAX.characters(%s, %d)\n", out, len);
}

/**
 * referenceDebug:
 * @ctxt:  An XML parser context
 * @name:  The entity name
 *
1302
 * called when an entity reference is detected.
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
 */
static void
referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
{
    callbacks++;
    if (noout)
	return;
    fprintf(stdout, "SAX.reference(%s)\n", name);
}

/**
 * ignorableWhitespaceDebug:
 * @ctxt:  An XML parser context
 * @ch:  a xmlChar string
 * @start: the first char in the string
 * @len: the number of xmlChar
 *
 * receiving some ignorable whitespaces from the parser.
 * Question: how much at a time ???
 */
static void
ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
{
    char out[40];
    int i;

    callbacks++;
    if (noout)
	return;
    for (i = 0;(i<len) && (i < 30);i++)
	out[i] = ch[i];
    out[i] = 0;
    fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", out, len);
}

/**
 * processingInstructionDebug:
 * @ctxt:  An XML parser context
 * @target:  the target name
 * @data: the PI data's
 * @len: the number of xmlChar
 *
 * A processing instruction has been parsed.
 */
static void
processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
                      const xmlChar *data)
{
    callbacks++;
    if (noout)
	return;
    if (data != NULL)
	fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
		(char *) target, (char *) data);
    else
	fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
		(char *) target);
}

/**
 * cdataBlockDebug:
 * @ctx: the user data (XML parser context)
 * @value:  The pcdata content
 * @len:  the block length
 *
 * called when a pcdata block has been parsed
 */
static void
cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
{
    callbacks++;
    if (noout)
	return;
    fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
	    (char *) value, len);
}

/**
 * commentDebug:
 * @ctxt:  An XML parser context
 * @value:  the comment content
 *
 * A comment has been parsed.
 */
static void
commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
{
    callbacks++;
    if (noout)
	return;
    fprintf(stdout, "SAX.comment(%s)\n", value);
}

/**
 * warningDebug:
 * @ctxt:  An XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 *
 * Display and format a warning messages, gives file, line, position and
 * extra parameters.
 */
1405
static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
{
    va_list args;

    callbacks++;
    if (noout)
	return;
    va_start(args, msg);
    fprintf(stdout, "SAX.warning: ");
    vfprintf(stdout, msg, args);
    va_end(args);
}

/**
 * errorDebug:
 * @ctxt:  An XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 *
 * Display and format a error messages, gives file, line, position and
 * extra parameters.
 */
1428
static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
{
    va_list args;

    callbacks++;
    if (noout)
	return;
    va_start(args, msg);
    fprintf(stdout, "SAX.error: ");
    vfprintf(stdout, msg, args);
    va_end(args);
}

/**
 * fatalErrorDebug:
 * @ctxt:  An XML parser context
 * @msg:  the message to display/transmit
 * @...:  extra parameters for the message display
 *
 * Display and format a fatalError messages, gives file, line, position and
 * extra parameters.
 */
1451
static void XMLCDECL LIBXML_ATTR_FORMAT(2,3)
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
{
    va_list args;

    callbacks++;
    if (noout)
	return;
    va_start(args, msg);
    fprintf(stdout, "SAX.fatalError: ");
    vfprintf(stdout, msg, args);
    va_end(args);
}

1465
static xmlSAXHandler debugSAXHandlerStruct = {
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
    internalSubsetDebug,
    isStandaloneDebug,
    hasInternalSubsetDebug,
    hasExternalSubsetDebug,
    resolveEntityDebug,
    getEntityDebug,
    entityDeclDebug,
    notationDeclDebug,
    attributeDeclDebug,
    elementDeclDebug,
    unparsedEntityDeclDebug,
    setDocumentLocatorDebug,
    startDocumentDebug,
    endDocumentDebug,
    startElementDebug,
    endElementDebug,
    referenceDebug,
    charactersDebug,
    ignorableWhitespaceDebug,
    processingInstructionDebug,
    commentDebug,
    warningDebug,
    errorDebug,
    fatalErrorDebug,
    getParameterEntityDebug,
    cdataBlockDebug,
    externalSubsetDebug,
    1,
    NULL,
    NULL,
    NULL,
    NULL
};

xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;

/*
 * SAX2 specific callbacks
 */
/**
 * startElementNsDebug:
 * @ctxt:  An XML parser context
 * @name:  The element name
 *
 * called when an opening tag has been processed.
 */
static void
startElementNsDebug(void *ctx ATTRIBUTE_UNUSED,
                    const xmlChar *localname,
                    const xmlChar *prefix,
                    const xmlChar *URI,
		    int nb_namespaces,
		    const xmlChar **namespaces,
		    int nb_attributes,
		    int nb_defaulted,
		    const xmlChar **attributes)
{
    int i;

    callbacks++;
    if (noout)
	return;
    fprintf(stdout, "SAX.startElementNs(%s", (char *) localname);
    if (prefix == NULL)
	fprintf(stdout, ", NULL");
    else
	fprintf(stdout, ", %s", (char *) prefix);
    if (URI == NULL)
	fprintf(stdout, ", NULL");
    else
	fprintf(stdout, ", '%s'", (char *) URI);
    fprintf(stdout, ", %d", nb_namespaces);
1538

1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564