testSAX.c 27 KB
Newer Older
1
/*
2
 * testSAX.c : a small tester program for parsing using the SAX API.
3 4 5
 *
 * See Copyright for the status of this software.
 *
6
 * daniel@veillard.com
7 8
 */

9 10
#include "libxml.h"

11 12 13
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
14 15 16
#ifdef HAVE_SYS_TIMEB_H
#include <sys/timeb.h>
#endif
17 18 19 20
#ifdef HAVE_TIME_H
#include <time.h>
#endif

21
#ifdef LIBXML_SAX1_ENABLED
22 23 24 25
#include <string.h>
#include <stdarg.h>

#ifdef HAVE_SYS_TYPES_H
26
#include <sys/types.h>
27
#endif
28 29 30 31 32 33 34 35 36
#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
37
#ifdef HAVE_STDLIB_H
38
#include <stdlib.h>
39
#endif
40 41 42
#ifdef HAVE_STRING_H
#include <string.h>
#endif
43

44

45
#include <libxml/globals.h>
46
#include <libxml/xmlerror.h>
47 48 49 50 51
#include <libxml/parser.h>
#include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
#include <libxml/tree.h>
#include <libxml/debugXML.h>
#include <libxml/xmlmemory.h>
52 53 54 55

static int debug = 0;
static int copy = 0;
static int recovery = 0;
56
static int push = 0;
57
static int speed = 0;
58
static int noent = 0;
59
static int quiet = 0;
60 61
static int nonull = 0;
static int sax2 = 0;
62
static int repeat = 0;
63
static int callbacks = 0;
64 65 66 67 68 69 70 71 72 73
static int timing = 0;

/*
 * Timing routines.
 */
/*
 * Internal timing routines to remove the necessity to have unix-specific
 * function calls
 */

74
#ifndef HAVE_GETTIMEOFDAY
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
#ifdef HAVE_SYS_TIMEB_H
#ifdef HAVE_SYS_TIME_H
#ifdef HAVE_FTIME

static int
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 */

#if defined(HAVE_GETTIMEOFDAY)
static struct timeval begin, end;
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115
/*
 * 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
 */
116
static void XMLCDECL
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
endTimer(const char *fmt, ...)
{
    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
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    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.
 */
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC 100
#endif

static clock_t begin, end;
static void
startTimer(void)
{
    begin = clock();
}
152
static void XMLCDECL
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
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
     */
}
181
static void XMLCDECL
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
endTimer(char *format, ...)
{
    /*
     * We cannot do anything because we don't have a timing function
     */
#ifdef HAVE_STDARG_H
    va_start(ap, format);
    vfprintf(stderr, format, ap);
    va_end(ap);
    fprintf(stderr, " was not timed\n", msec);
#else
    /* We don't have gettimeofday, time or stdarg.h, what crazy world is
     * this ?!
     */
#endif
}
#endif

/*
 * empty SAX block
 */
203
static xmlSAXHandler emptySAXHandlerStruct = {
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    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 */
228
    NULL, /* getParameterEntity */
229
    NULL, /* cdataBlock; */
230 231 232 233
    NULL, /* externalSubset; */
    1,
    NULL,
    NULL, /* startElementNs */
234 235
    NULL, /* endElementNs */
    NULL  /* xmlStructuredErrorFunc */
236 237
};

238
static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
239
extern xmlSAXHandlerPtr debugSAXHandler;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

/************************************************************************
 *									*
 *				Debug Handlers				*
 *									*
 ************************************************************************/

/**
 * isStandaloneDebug:
 * @ctxt:  An XML parser context
 *
 * Is this document tagged standalone ?
 *
 * Returns 1 if true
 */
255
static int
256
isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
257
{
258 259 260
    callbacks++;
    if (quiet)
	return(0);
261
    fprintf(stdout, "SAX.isStandalone()\n");
262 263 264 265 266 267 268 269 270 271 272
    return(0);
}

/**
 * hasInternalSubsetDebug:
 * @ctxt:  An XML parser context
 *
 * Does this document has an internal subset
 *
 * Returns 1 if true
 */
273
static int
274
hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
275
{
276 277 278
    callbacks++;
    if (quiet)
	return(0);
279
    fprintf(stdout, "SAX.hasInternalSubset()\n");
280 281 282 283 284 285 286 287 288 289 290
    return(0);
}

/**
 * hasExternalSubsetDebug:
 * @ctxt:  An XML parser context
 *
 * Does this document has an external subset
 *
 * Returns 1 if true
 */
291
static int
292
hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
293
{
294 295 296
    callbacks++;
    if (quiet)
	return(0);
297
    fprintf(stdout, "SAX.hasExternalSubset()\n");
298 299 300 301
    return(0);
}

/**
302
 * internalSubsetDebug:
303 304 305 306
 * @ctxt:  An XML parser context
 *
 * Does this document has an internal subset
 */
307
static void
308
internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
309
	       const xmlChar *ExternalID, const xmlChar *SystemID)
310
{
311 312 313
    callbacks++;
    if (quiet)
	return;
314 315 316 317 318 319 320 321 322
    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);
323 324
}

325 326 327 328 329 330 331
/**
 * externalSubsetDebug:
 * @ctxt:  An XML parser context
 *
 * Does this document has an external subset
 */
static void
332
externalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
333 334
	       const xmlChar *ExternalID, const xmlChar *SystemID)
{
335 336 337
    callbacks++;
    if (quiet)
	return;
338 339 340 341 342 343 344 345 346 347 348
    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);
}

349 350 351 352 353 354 355 356 357 358 359 360 361 362
/**
 * 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.
 */
363
static xmlParserInputPtr
364
resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
365
{
366 367 368
    callbacks++;
    if (quiet)
	return(NULL);
369
    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
370

371

372 373 374 375 376 377 378 379 380
    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");
381
/*********
382
    if (systemId != NULL) {
383
        return(xmlNewInputFromFile(ctxt, (char *) systemId));
384
    }
385
 *********/
386 387 388 389 390 391 392 393 394 395 396 397
    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.
 */
398
static xmlEntityPtr
399
getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
400
{
401 402 403
    callbacks++;
    if (quiet)
	return(NULL);
404
    fprintf(stdout, "SAX.getEntity(%s)\n", name);
405 406 407
    return(NULL);
}

408 409 410 411 412 413 414 415 416
/**
 * getParameterEntityDebug:
 * @ctxt:  An XML parser context
 * @name: The entity name
 *
 * Get a parameter entity by name
 *
 * Returns the xmlParserInputPtr
 */
417
static xmlEntityPtr
418
getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
419
{
420 421 422
    callbacks++;
    if (quiet)
	return(NULL);
423 424 425 426
    fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
    return(NULL);
}

427 428 429 430

/**
 * entityDeclDebug:
 * @ctxt:  An XML parser context
431 432
 * @name:  the entity name
 * @type:  the entity type
433 434 435 436 437 438
 * @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
 */
439
static void
440
entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
441
          const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
442
{
443 444 445 446 447 448 449 450
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;
451 452 453
    callbacks++;
    if (quiet)
	return;
454
    fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
455 456 457 458 459 460
            name, type, publicId, systemId, content);
}

/**
 * attributeDeclDebug:
 * @ctxt:  An XML parser context
461 462
 * @name:  the attribute name
 * @type:  the attribute type
463 464 465
 *
 * An attribute definition has been parsed
 */
466
static void
467 468 469
attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar * elem,
                   const xmlChar * name, int type, int def,
                   const xmlChar * defaultValue, xmlEnumerationPtr tree)
470
{
471 472
    callbacks++;
    if (quiet)
473
        return;
474
    if (defaultValue == NULL)
475 476
        fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, NULL, ...)\n",
                elem, name, type, def);
477
    else
478 479 480
        fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
                elem, name, type, def, defaultValue);
    xmlFreeEnumeration(tree);
481 482 483 484 485
}

/**
 * elementDeclDebug:
 * @ctxt:  An XML parser context
486 487
 * @name:  the element name
 * @type:  the element type
488 489 490 491
 * @content: the element value (without processing).
 *
 * An element definition has been parsed
 */
492
static void
493 494
elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
	    xmlElementContentPtr content ATTRIBUTE_UNUSED)
495
{
496 497 498
    callbacks++;
    if (quiet)
	return;
499
    fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
500 501 502 503 504 505 506 507 508 509 510 511
            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.
 */
512
static void
513
notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
514
	     const xmlChar *publicId, const xmlChar *systemId)
515
{
516 517 518
    callbacks++;
    if (quiet)
	return;
519
    fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
520 521 522 523 524 525 526 527 528 529 530 531 532
            (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
 */
533
static void
534
unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
535 536
		   const xmlChar *publicId, const xmlChar *systemId,
		   const xmlChar *notationName)
537
{
538 539 540 541 542 543 544 545
const xmlChar *nullstr = BAD_CAST "(null)";

    if (publicId == NULL)
        publicId = nullstr;
    if (systemId == NULL)
        systemId = nullstr;
    if (notationName == NULL)
        notationName = nullstr;
546 547 548
    callbacks++;
    if (quiet)
	return;
549
    fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
550 551 552 553 554 555 556 557 558 559 560 561
            (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.
 */
562
static void
563
setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
564
{
565 566 567
    callbacks++;
    if (quiet)
	return;
568
    fprintf(stdout, "SAX.setDocumentLocator()\n");
569 570 571 572 573 574 575 576
}

/**
 * startDocumentDebug:
 * @ctxt:  An XML parser context
 *
 * called when the document start being processed.
 */
577
static void
578
startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
579
{
580 581 582
    callbacks++;
    if (quiet)
	return;
583
    fprintf(stdout, "SAX.startDocument()\n");
584 585 586 587 588 589 590 591
}

/**
 * endDocumentDebug:
 * @ctxt:  An XML parser context
 *
 * called when the document end has been detected.
 */
592
static void
593
endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
594
{
595 596 597
    callbacks++;
    if (quiet)
	return;
598
    fprintf(stdout, "SAX.endDocument()\n");
599 600 601 602 603 604 605 606 607
}

/**
 * startElementDebug:
 * @ctxt:  An XML parser context
 * @name:  The element name
 *
 * called when an opening tag has been processed.
 */
608
static void
609
startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
610 611 612
{
    int i;

613 614 615
    callbacks++;
    if (quiet)
	return;
616
    fprintf(stdout, "SAX.startElement(%s", (char *) name);
617 618
    if (atts != NULL) {
        for (i = 0;(atts[i] != NULL);i++) {
619
	    fprintf(stdout, ", %s='", atts[i++]);
620 621
	    if (atts[i] != NULL)
	        fprintf(stdout, "%s'", atts[i]);
622 623
	}
    }
624
    fprintf(stdout, ")\n");
625 626 627 628 629 630 631 632 633
}

/**
 * endElementDebug:
 * @ctxt:  An XML parser context
 * @name:  The element name
 *
 * called when the end of an element has been detected.
 */
634
static void
635
endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
636
{
637 638 639
    callbacks++;
    if (quiet)
	return;
640
    fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
641 642 643 644 645
}

/**
 * charactersDebug:
 * @ctxt:  An XML parser context
646 647
 * @ch:  a xmlChar string
 * @len: the number of xmlChar
648 649 650 651
 *
 * receiving some chars from the parser.
 * Question: how much at a time ???
 */
652
static void
653
charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
654
{
655
    char output[40];
656 657
    int i;

658 659 660
    callbacks++;
    if (quiet)
	return;
661 662 663 664 665
    for (i = 0;(i<len) && (i < 30);i++)
	output[i] = ch[i];
    output[i] = 0;

    fprintf(stdout, "SAX.characters(%s, %d)\n", output, len);
666 667 668 669 670 671 672
}

/**
 * referenceDebug:
 * @ctxt:  An XML parser context
 * @name:  The entity name
 *
673
 * called when an entity reference is detected.
674
 */
675
static void
676
referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
677
{
678 679 680
    callbacks++;
    if (quiet)
	return;
681
    fprintf(stdout, "SAX.reference(%s)\n", name);
682 683 684 685 686
}

/**
 * ignorableWhitespaceDebug:
 * @ctxt:  An XML parser context
687
 * @ch:  a xmlChar string
688
 * @start: the first char in the string
689
 * @len: the number of xmlChar
690 691 692 693
 *
 * receiving some ignorable whitespaces from the parser.
 * Question: how much at a time ???
 */
694
static void
695
ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
696
{
697 698 699
    char output[40];
    int i;

700 701 702
    callbacks++;
    if (quiet)
	return;
703 704 705 706
    for (i = 0;(i<len) && (i < 30);i++)
	output[i] = ch[i];
    output[i] = 0;
    fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len);
707 708 709 710 711 712 713
}

/**
 * processingInstructionDebug:
 * @ctxt:  An XML parser context
 * @target:  the target name
 * @data: the PI data's
714
 * @len: the number of xmlChar
715 716 717
 *
 * A processing instruction has been parsed.
 */
718
static void
719
processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
720
                      const xmlChar *data)
721
{
722 723 724
    callbacks++;
    if (quiet)
	return;
725 726 727 728 729 730
    if (data != NULL)
	fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
		(char *) target, (char *) data);
    else
	fprintf(stdout, "SAX.processingInstruction(%s, NULL)\n",
		(char *) target);
731 732
}

733 734 735 736 737 738 739 740
/**
 * cdataBlockDebug:
 * @ctx: the user data (XML parser context)
 * @value:  The pcdata content
 * @len:  the block length
 *
 * called when a pcdata block has been parsed
 */
741
static void
742
cdataBlockDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value, int len)
743
{
744 745 746
    callbacks++;
    if (quiet)
	return;
747
    fprintf(stdout, "SAX.pcdata(%.20s, %d)\n",
748 749 750
	    (char *) value, len);
}

751 752 753 754 755 756 757
/**
 * commentDebug:
 * @ctxt:  An XML parser context
 * @value:  the comment content
 *
 * A comment has been parsed.
 */
758
static void
759
commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
760
{
761 762 763
    callbacks++;
    if (quiet)
	return;
764
    fprintf(stdout, "SAX.comment(%s)\n", value);
765 766 767 768 769 770 771 772 773 774 775
}

/**
 * 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.
 */
776
static void XMLCDECL
777
warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
778 779 780
{
    va_list args;

781 782 783
    callbacks++;
    if (quiet)
	return;
784
    va_start(args, msg);
785 786
    fprintf(stdout, "SAX.warning: ");
    vfprintf(stdout, msg, args);
787 788 789 790 791 792 793 794 795 796 797 798
    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.
 */
799
static void XMLCDECL
800
errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
801 802 803
{
    va_list args;

804 805 806
    callbacks++;
    if (quiet)
	return;
807
    va_start(args, msg);
808 809
    fprintf(stdout, "SAX.error: ");
    vfprintf(stdout, msg, args);
810 811 812 813 814 815 816 817 818 819 820 821
    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.
 */
822
static void XMLCDECL
823
fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
824 825 826
{
    va_list args;

827 828 829
    callbacks++;
    if (quiet)
	return;
830
    va_start(args, msg);
831 832
    fprintf(stdout, "SAX.fatalError: ");
    vfprintf(stdout, msg, args);
833 834 835
    va_end(args);
}

836
static xmlSAXHandler debugSAXHandlerStruct = {
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    internalSubsetDebug,
    isStandaloneDebug,
    hasInternalSubsetDebug,
    hasExternalSubsetDebug,
    resolveEntityDebug,
    getEntityDebug,
    entityDeclDebug,
    notationDeclDebug,
    attributeDeclDebug,
    elementDeclDebug,
    unparsedEntityDeclDebug,
    setDocumentLocatorDebug,
    startDocumentDebug,
    endDocumentDebug,
    startElementDebug,
    endElementDebug,
    referenceDebug,
    charactersDebug,
    ignorableWhitespaceDebug,
    processingInstructionDebug,
    commentDebug,
    warningDebug,
    errorDebug,
    fatalErrorDebug,
861
    getParameterEntityDebug,
862
    cdataBlockDebug,
863
    externalSubsetDebug,
864 865 866
    1,
    NULL,
    NULL,
867
    NULL,
868
    NULL
869 870 871 872
};

xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;

873 874 875 876
/*
 * SAX2 specific callbacks
 */
/**
877
 * startElementNsDebug:
878 879 880 881 882 883 884 885 886 887 888 889
 * @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,
890 891 892
		    int nb_attributes,
		    int nb_defaulted,
		    const xmlChar **attributes)
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
{
    int i;

    callbacks++;
    if (quiet)
	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);
909

910 911 912 913 914 915 916 917 918
    if (namespaces != NULL) {
        for (i = 0;i < nb_namespaces * 2;i++) {
	    fprintf(stdout, ", xmlns");
	    if (namespaces[i] != NULL)
	        fprintf(stdout, ":%s", namespaces[i]);
	    i++;
	    fprintf(stdout, "='%s'", namespaces[i]);
	}
    }
919 920
    fprintf(stdout, ", %d, %d", nb_attributes, nb_defaulted);
    if (attributes != NULL) {
921
        for (i = 0;i < nb_attributes * 5;i += 5) {
922 923 924 925 926
	    if (attributes[i + 1] != NULL)
		fprintf(stdout, ", %s:%s='", attributes[i + 1], attributes[i]);
	    else
		fprintf(stdout, ", %s='", attributes[i]);
	    fprintf(stdout, "%.4s...', %d", attributes[i + 3],
927
		    (int)(attributes[i + 4] - attributes[i + 3]));
928 929 930
	}
    }
    fprintf(stdout, ")\n");
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
}

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

960
static xmlSAXHandler debugSAX2HandlerStruct = {
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
    internalSubsetDebug,
    isStandaloneDebug,
    hasInternalSubsetDebug,
    hasExternalSubsetDebug,
    resolveEntityDebug,
    getEntityDebug,
    entityDeclDebug,
    notationDeclDebug,
    attributeDeclDebug,
    elementDeclDebug,
    unparsedEntityDeclDebug,
    setDocumentLocatorDebug,
    startDocumentDebug,
    endDocumentDebug,
    NULL,
    NULL,
    referenceDebug,
    charactersDebug,
    ignorableWhitespaceDebug,
    processingInstructionDebug,
    commentDebug,
    warningDebug,
    errorDebug,
    fatalErrorDebug,
    getParameterEntityDebug,
    cdataBlockDebug,
    externalSubsetDebug,
988
    XML_SAX2_MAGIC,
989 990
    NULL,
    startElementNsDebug,
991 992
    endElementNsDebug,
    NULL
993 994
};

995
static xmlSAXHandlerPtr debugSAX2Handler = &debugSAX2HandlerStruct;
996

997 998 999 1000 1001 1002
/************************************************************************
 *									*
 *				Debug					*
 *									*
 ************************************************************************/

1003 1004
static void
parseAndPrintFile(char *filename) {
1005
    int res;
1006

1007
#ifdef LIBXML_PUSH_ENABLED
1008 1009 1010
    if (push) {
	FILE *f;

1011 1012 1013 1014
        if ((!quiet) && (!nonull)) {
	    /*
	     * Empty callbacks for checking
	     */
1015 1016 1017
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
	    f = fopen(filename, "rb");
#else
1018
	    f = fopen(filename, "r");
1019
#endif
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	    if (f != NULL) {
		int ret;
		char chars[10];
		xmlParserCtxtPtr ctxt;

		ret = fread(chars, 1, 4, f);
		if (ret > 0) {
		    ctxt = xmlCreatePushParserCtxt(emptySAXHandler, NULL,
				chars, ret, filename);
		    while ((ret = fread(chars, 1, 3, f)) > 0) {
			xmlParseChunk(ctxt, chars, ret, 0);
		    }
		    xmlParseChunk(ctxt, chars, 0, 1);
		    xmlFreeParserCtxt(ctxt);
1034
		}
1035 1036 1037 1038
		fclose(f);
	    } else {
		xmlGenericError(xmlGenericErrorContext,
			"Cannot read file %s\n", filename);
1039 1040 1041 1042 1043
	    }
	}
	/*
	 * Debug callback
	 */
1044 1045 1046
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
	f = fopen(filename, "rb");
#else
1047
	f = fopen(filename, "r");
1048
#endif
1049
	if (f != NULL) {
1050
	    int ret;
1051 1052 1053
	    char chars[10];
	    xmlParserCtxtPtr ctxt;

1054 1055
	    ret = fread(chars, 1, 4, f);
	    if (ret > 0) {
1056 1057 1058 1059 1060 1061
	        if (sax2)
		    ctxt = xmlCreatePushParserCtxt(debugSAX2Handler, NULL,
				chars, ret, filename);
		else
		    ctxt = xmlCreatePushParserCtxt(debugSAXHandler, NULL,
				chars, ret, filename);
1062 1063
		while ((ret = fread(chars, 1, 3, f)) > 0) {
		    xmlParseChunk(ctxt, chars, ret, 0);
1064
		}
1065
		ret = xmlParseChunk(ctxt, chars, 0, 1);
1066
		xmlFreeParserCtxt(ctxt);
1067
		if (ret != 0) {
1068
		    fprintf(stdout,
1069
		            "xmlSAXUserParseFile returned error %d\n", ret);
1070 1071 1072 1073 1074
		}
	    }
	    fclose(f);
	}
    } else {
1075
#endif /* LIBXML_PUSH_ENABLED */
1076 1077 1078 1079
	if (!speed) {
	    /*
	     * Empty callbacks for checking
	     */
1080
	    if ((!quiet) && (!nonull)) {
1081 1082 1083 1084
		res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
		if (res != 0) {
		    fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
		}
1085
	    }
1086

1087 1088 1089
	    /*
	     * Debug callback
	     */
1090
	    callbacks = 0;
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	    if (repeat) {
	        int i;
		for (i = 0;i < 99;i++) {
		    if (sax2)
			res = xmlSAXUserParseFile(debugSAX2Handler, NULL,
			                          filename);
		    else
			res = xmlSAXUserParseFile(debugSAXHandler, NULL,
			                          filename);
		}
	    }
1102 1103 1104 1105
	    if (sax2)
	        res = xmlSAXUserParseFile(debugSAX2Handler, NULL, filename);
	    else
		res = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
1106 1107 1108
	    if (res != 0) {
		fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
	    }
1109 1110
	    if (quiet)
		fprintf(stdout, "%d callbacks generated\n", callbacks);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	} else {
	    /*
	     * test 100x the SAX parse
	     */
	    int i;

	    for (i = 0; i<100;i++)
		res = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
	    if (res != 0) {
		fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
	    }
1122
	}
1123
#ifdef LIBXML_PUSH_ENABLED
1124
    }
1125
#endif
1126 1127 1128 1129 1130 1131 1132
}


int main(int argc, char **argv) {
    int i;
    int files = 0;

1133
    LIBXML_TEST_VERSION	/* be safe, plus calls xmlInitParser */
1134

1135 1136 1137 1138 1139 1140 1141 1142
    for (i = 1; i < argc ; i++) {
	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
	    debug++;
	else if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
	    copy++;
	else if ((!strcmp(argv[i], "-recover")) ||
	         (!strcmp(argv[i], "--recover")))
	    recovery++;
1143 1144
	else if ((!strcmp(argv[i], "-push")) ||
	         (!strcmp(argv[i], "--push")))
1145
#ifdef LIBXML_PUSH_ENABLED
1146
	    push++;
1147 1148 1149
#else
	    fprintf(stderr,"'push' not enabled in library - ignoring\n");
#endif /* LIBXML_PUSH_ENABLED */
1150 1151 1152
	else if ((!strcmp(argv[i], "-speed")) ||
	         (!strcmp(argv[i], "--speed")))
	    speed++;
1153 1154 1155 1156 1157 1158
	else if ((!strcmp(argv[i], "-timing")) ||
	         (!strcmp(argv[i], "--timing"))) {
	    nonull++;
	    timing++;
	    quiet++;
	} else if ((!strcmp(argv[i], "-repeat")) ||
1159 1160 1161 1162
	         (!strcmp(argv[i], "--repeat"))) {
	    repeat++;
	    quiet++;
	} else if ((!strcmp(argv[i], "-noent")) ||
1163 1164
	         (!strcmp(argv[i], "--noent")))
	    noent++;
1165 1166 1167
	else if ((!strcmp(argv[i], "-quiet")) ||
	         (!strcmp(argv[i], "--quiet")))
	    quiet++;
1168 1169 1170 1171 1172 1173
	else if ((!strcmp(argv[i], "-sax2")) ||
	         (!strcmp(argv[i], "--sax2")))
	    sax2++;
	else if ((!strcmp(argv[i], "-nonull")) ||
	         (!strcmp(argv[i], "--nonull")))
	    nonull++;
1174
    }
1175
    if (noent != 0) xmlSubstituteEntitiesDefault(1);
1176 1177
    for (i = 1; i < argc ; i++) {
	if (argv[i][0] != '-') {
1178 1179 1180
	    if (timing) {
		startTimer();
	    }
1181
	    parseAndPrintFile(argv[i]);
1182 1183 1184
	    if (timing) {
		endTimer("Parsing");
	    }
1185 1186 1187
	    files ++;
	}
    }
1188 1189
    xmlCleanupParser();
    xmlMemoryDump();
1190 1191 1192

    return(0);
}
1193 1194 1195 1196 1197 1198
#else
int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
    printf("%s : SAX1 parsing support not compiled in\n", argv[0]);
    return(0);
}
#endif /* LIBXML_SAX1_ENABLED */