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

9 10 11 12
#include "libxml.h"

#ifdef LIBXML_HTML_ENABLED

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

16 17

#ifdef HAVE_SYS_TYPES_H
18
#include <sys/types.h>
19
#endif
20 21 22 23 24 25 26 27 28
#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
29
#ifdef HAVE_STDLIB_H
30
#include <stdlib.h>
31
#endif
32

33 34 35 36
#include <libxml/xmlmemory.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/debugXML.h>
37
#include <libxml/xmlerror.h>
38
#include <libxml/globals.h>
39

40
#ifdef LIBXML_DEBUG_ENABLED
41
static int debug = 0;
42
#endif
43
static int copy = 0;
44 45 46
static int sax = 0;
static int repeat = 0;
static int noout = 0;
47
#ifdef LIBXML_PUSH_ENABLED
48
static int push = 0;
49
#endif /* LIBXML_PUSH_ENABLED */
50
static char *encoding = NULL;
51
static int options = 0;
52

53
static xmlSAXHandler emptySAXHandlerStruct = {
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
    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 */
79
    NULL, /* cdataBlock */
80
    NULL, /* externalSubset */
81 82 83 84 85
    1,    /* initialized */
    NULL, /* private */
    NULL, /* startElementNsSAX2Func */
    NULL, /* endElementNsSAX2Func */
    NULL  /* xmlStructuredErrorFunc */
86 87
};

88
static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
extern xmlSAXHandlerPtr debugSAXHandler;

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

/**
 * isStandaloneDebug:
 * @ctxt:  An XML parser context
 *
 * Is this document tagged standalone ?
 *
 * Returns 1 if true
 */
105
static int
106
isStandaloneDebug(void *ctx ATTRIBUTE_UNUSED)
107 108 109 110 111 112 113 114 115 116 117 118 119
{
    fprintf(stdout, "SAX.isStandalone()\n");
    return(0);
}

/**
 * hasInternalSubsetDebug:
 * @ctxt:  An XML parser context
 *
 * Does this document has an internal subset
 *
 * Returns 1 if true
 */
120
static int
121
hasInternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
122 123 124 125 126 127 128 129 130 131 132 133 134
{
    fprintf(stdout, "SAX.hasInternalSubset()\n");
    return(0);
}

/**
 * hasExternalSubsetDebug:
 * @ctxt:  An XML parser context
 *
 * Does this document has an external subset
 *
 * Returns 1 if true
 */
135
static int
136
hasExternalSubsetDebug(void *ctx ATTRIBUTE_UNUSED)
137 138 139 140 141 142 143 144 145 146 147
{
    fprintf(stdout, "SAX.hasExternalSubset()\n");
    return(0);
}

/**
 * hasInternalSubsetDebug:
 * @ctxt:  An XML parser context
 *
 * Does this document has an internal subset
 */
148
static void
149
internalSubsetDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
150 151
	       const xmlChar *ExternalID, const xmlChar *SystemID)
{
152 153 154 155 156 157 158 159 160
    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);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
}

/**
 * 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.
 */
177
static xmlParserInputPtr
178
resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xmlChar *systemId)
179 180 181
{
    /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */

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
    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");
/*********
    if (systemId != NULL) {
        return(xmlNewInputFromFile(ctxt, (char *) systemId));
    }
 *********/
    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.
 */
209
static xmlEntityPtr
210
getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
    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
 */
225
static xmlEntityPtr
226
getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
227 228 229 230 231 232 233 234 235
{
    fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
    return(NULL);
}


/**
 * entityDeclDebug:
 * @ctxt:  An XML parser context
236 237
 * @name:  the entity name
 * @type:  the entity type
238 239 240 241 242 243
 * @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
 */
244
static void
245
entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
246 247 248 249 250 251 252 253 254
          const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
{
    fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
            name, type, publicId, systemId, content);
}

/**
 * attributeDeclDebug:
 * @ctxt:  An XML parser context
255 256
 * @name:  the attribute name
 * @type:  the attribute type
257 258 259
 *
 * An attribute definition has been parsed
 */
260
static void
261
attributeDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *elem, const xmlChar *name,
262
              int type, int def, const xmlChar *defaultValue,
263
	      xmlEnumerationPtr tree ATTRIBUTE_UNUSED)
264 265 266 267 268 269 270 271
{
    fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
            elem, name, type, def, defaultValue);
}

/**
 * elementDeclDebug:
 * @ctxt:  An XML parser context
272 273
 * @name:  the element name
 * @type:  the element type
274 275 276 277
 * @content: the element value (without processing).
 *
 * An element definition has been parsed
 */
278
static void
279 280
elementDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type,
	    xmlElementContentPtr content ATTRIBUTE_UNUSED)
281 282 283 284 285 286 287 288 289 290 291 292 293 294
{
    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.
 */
295
static void
296
notationDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	     const xmlChar *publicId, const xmlChar *systemId)
{
    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
 */
313
static void
314
unparsedEntityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name,
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		   const xmlChar *publicId, const xmlChar *systemId,
		   const xmlChar *notationName)
{
    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.
 */
331
static void
332
setDocumentLocatorDebug(void *ctx ATTRIBUTE_UNUSED, xmlSAXLocatorPtr loc ATTRIBUTE_UNUSED)
333 334 335 336 337 338 339 340 341 342
{
    fprintf(stdout, "SAX.setDocumentLocator()\n");
}

/**
 * startDocumentDebug:
 * @ctxt:  An XML parser context
 *
 * called when the document start being processed.
 */
343
static void
344
startDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
345 346 347 348 349 350 351 352 353 354
{
    fprintf(stdout, "SAX.startDocument()\n");
}

/**
 * endDocumentDebug:
 * @ctxt:  An XML parser context
 *
 * called when the document end has been detected.
 */
355
static void
356
endDocumentDebug(void *ctx ATTRIBUTE_UNUSED)
357 358 359 360 361 362 363 364 365 366 367
{
    fprintf(stdout, "SAX.endDocument()\n");
}

/**
 * startElementDebug:
 * @ctxt:  An XML parser context
 * @name:  The element name
 *
 * called when an opening tag has been processed.
 */
368
static void
369
startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
370 371 372 373 374 375
{
    int i;

    fprintf(stdout, "SAX.startElement(%s", (char *) name);
    if (atts != NULL) {
        for (i = 0;(atts[i] != NULL);i++) {
376
	    fprintf(stdout, ", %s", atts[i++]);
377 378 379 380 381 382 383 384
	    if (atts[i] != NULL) {
		unsigned char output[40];
		const unsigned char *att = atts[i];
		int outlen, attlen;
	        fprintf(stdout, "='");
		while ((attlen = strlen((char*)att)) > 0) {
		    outlen = sizeof output - 1;
		    htmlEncodeEntities(output, &outlen, att, &attlen, '\'');
385
		    output[outlen] = 0;
386
		    fprintf(stdout, "%s", (char *) output);
387 388 389 390
		    att += attlen;
		}
		fprintf(stdout, "'");
	    }
391 392 393 394 395 396 397 398 399 400 401 402
	}
    }
    fprintf(stdout, ")\n");
}

/**
 * endElementDebug:
 * @ctxt:  An XML parser context
 * @name:  The element name
 *
 * called when the end of an element has been detected.
 */
403
static void
404
endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
405 406 407 408 409 410 411 412 413 414 415 416 417
{
    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 ???
 */
418
static void
419
charactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
420
{
421
    unsigned char output[40];
422
    int inlen = len, outlen = 30;
423

424
    htmlEncodeEntities(output, &outlen, ch, &inlen, 0);
425
    output[outlen] = 0;
426 427

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

430 431 432 433 434 435 436 437 438
/**
 * cdataDebug:
 * @ctxt:  An XML parser context
 * @ch:  a xmlChar string
 * @len: the number of xmlChar
 *
 * receiving some cdata chars from the parser.
 * Question: how much at a time ???
 */
439
static void
440
cdataDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
441 442 443 444 445 446 447 448 449 450
{
    unsigned char output[40];
    int inlen = len, outlen = 30;

    htmlEncodeEntities(output, &outlen, ch, &inlen, 0);
    output[outlen] = 0;

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

451 452 453 454 455
/**
 * referenceDebug:
 * @ctxt:  An XML parser context
 * @name:  The entity name
 *
456
 * called when an entity reference is detected.
457
 */
458
static void
459
referenceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
460 461 462 463 464 465 466 467 468 469 470 471 472 473
{
    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 ???
 */
474
static void
475
ignorableWhitespaceDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
476
{
477 478 479 480 481 482 483 484
    char output[40];
    int i;

    for (i = 0;(i<len) && (i < 30);i++)
	output[i] = ch[i];
    output[i] = 0;

    fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len);
485 486 487 488 489 490 491 492 493 494 495
}

/**
 * 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.
 */
496
static void
497
processingInstructionDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *target,
498 499 500 501 502 503 504 505 506 507 508 509 510
                      const xmlChar *data)
{
    fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
            (char *) target, (char *) data);
}

/**
 * commentDebug:
 * @ctxt:  An XML parser context
 * @value:  the comment content
 *
 * A comment has been parsed.
 */
511
static void
512
commentDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *value)
513 514 515 516 517 518 519 520 521 522 523 524 525
{
    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.
 */
526
static void XMLCDECL
527
warningDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
{
    va_list args;

    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.
 */
546
static void XMLCDECL
547
errorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
{
    va_list args;

    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.
 */
566
static void XMLCDECL
567
fatalErrorDebug(void *ctx ATTRIBUTE_UNUSED, const char *msg, ...)
568 569 570 571 572 573 574 575 576
{
    va_list args;

    va_start(args, msg);
    fprintf(stdout, "SAX.fatalError: ");
    vfprintf(stdout, msg, args);
    va_end(args);
}

577
static xmlSAXHandler debugSAXHandlerStruct = {
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
    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,
603
    cdataDebug,
604
    NULL,
605 606 607
    1,
    NULL,
    NULL,
608
    NULL,
609
    NULL
610 611 612
};

xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
613 614 615 616 617 618
/************************************************************************
 *									*
 *				Debug					*
 *									*
 ************************************************************************/

619 620
static void
parseSAXFile(char *filename) {
621 622
    htmlDocPtr doc = NULL;

623 624 625
    /*
     * Empty callbacks for checking
     */
626
#ifdef LIBXML_PUSH_ENABLED
627 628 629
    if (push) {
	FILE *f;

630 631 632
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
	f = fopen(filename, "rb");
#else
633
	f = fopen(filename, "r");
634
#endif
635 636 637 638
	if (f != NULL) {
	    int res, size = 3;
	    char chars[4096];
	    htmlParserCtxtPtr ctxt;
639

640 641 642 643 644
	    /* if (repeat) */
		size = 4096;
	    res = fread(chars, 1, 4, f);
	    if (res > 0) {
		ctxt = htmlCreatePushParserCtxt(emptySAXHandler, NULL,
645
			    chars, res, filename, XML_CHAR_ENCODING_NONE);
646 647 648 649 650 651 652 653 654 655 656 657 658 659
		while ((res = fread(chars, 1, size, f)) > 0) {
		    htmlParseChunk(ctxt, chars, res, 0);
		}
		htmlParseChunk(ctxt, chars, 0, 1);
		doc = ctxt->myDoc;
		htmlFreeParserCtxt(ctxt);
	    }
	    if (doc != NULL) {
		fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
		xmlFreeDoc(doc);
	    }
	    fclose(f);
	}
	if (!noout) {
660 661 662 663 664
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
		f = fopen(filename, "rb");
#else
		f = fopen(filename, "r");
#endif
665 666 667 668 669 670 671 672 673 674
	    if (f != NULL) {
		int res, size = 3;
		char chars[4096];
		htmlParserCtxtPtr ctxt;

		/* if (repeat) */
		    size = 4096;
		res = fread(chars, 1, 4, f);
		if (res > 0) {
		    ctxt = htmlCreatePushParserCtxt(debugSAXHandler, NULL,
675
				chars, res, filename, XML_CHAR_ENCODING_NONE);
676 677 678 679 680 681 682 683 684 685 686 687 688 689
		    while ((res = fread(chars, 1, size, f)) > 0) {
			htmlParseChunk(ctxt, chars, res, 0);
		    }
		    htmlParseChunk(ctxt, chars, 0, 1);
		    doc = ctxt->myDoc;
		    htmlFreeParserCtxt(ctxt);
		}
		if (doc != NULL) {
		    fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
		    xmlFreeDoc(doc);
		}
		fclose(f);
	    }
	}
690
    } else {
691
#endif /* LIBXML_PUSH_ENABLED */
692
	doc = htmlSAXParseFile(filename, NULL, emptySAXHandler, NULL);
693 694 695 696
	if (doc != NULL) {
	    fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
	    xmlFreeDoc(doc);
	}
697 698 699 700 701 702 703 704 705 706 707

	if (!noout) {
	    /*
	     * Debug callback
	     */
	    doc = htmlSAXParseFile(filename, NULL, debugSAXHandler, NULL);
	    if (doc != NULL) {
		fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
		xmlFreeDoc(doc);
	    }
	}
708
#ifdef LIBXML_PUSH_ENABLED
709
    }
710
#endif /* LIBXML_PUSH_ENABLED */
711 712
}

713 714
static void
parseAndPrintFile(char *filename) {
715
    htmlDocPtr doc = NULL;
716 717 718 719

    /*
     * build an HTML tree from a string;
     */
720
#ifdef LIBXML_PUSH_ENABLED
721 722 723
    if (push) {
	FILE *f;

724 725 726
#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
	f = fopen(filename, "rb");
#else
727
	f = fopen(filename, "r");
728
#endif
729 730
	if (f != NULL) {
	    int res, size = 3;
731
	    char chars[4096];
732 733
	    htmlParserCtxtPtr ctxt;

734 735
	    /* if (repeat) */
		size = 4096;
736 737 738
	    res = fread(chars, 1, 4, f);
	    if (res > 0) {
		ctxt = htmlCreatePushParserCtxt(NULL, NULL,
739
			    chars, res, filename, XML_CHAR_ENCODING_NONE);
740 741 742 743 744 745 746
		while ((res = fread(chars, 1, size, f)) > 0) {
		    htmlParseChunk(ctxt, chars, res, 0);
		}
		htmlParseChunk(ctxt, chars, 0, 1);
		doc = ctxt->myDoc;
		htmlFreeParserCtxt(ctxt);
	    }
747
	    fclose(f);
748
	}
749
    } else {
750
	doc = htmlReadFile(filename, NULL, options);
751
    }
752 753 754
#else
	doc = htmlReadFile(filename,NULL,options);
#endif
755
    if (doc == NULL) {
756 757
        xmlGenericError(xmlGenericErrorContext,
		"Could not parse %s\n", filename);
758
    }
759

760
#ifdef LIBXML_TREE_ENABLED
761 762 763 764
    /*
     * test intermediate copy if needed.
     */
    if (copy) {
765 766
        htmlDocPtr tmp;

767 768 769 770
        tmp = doc;
	doc = xmlCopyDoc(doc, 1);
	xmlFreeDoc(tmp);
    }
771
#endif
772

773
#ifdef LIBXML_OUTPUT_ENABLED
774 775 776
    /*
     * print it.
     */
777
    if (!noout) {
778
#ifdef LIBXML_DEBUG_ENABLED
779 780 781 782 783 784
	if (!debug) {
	    if (encoding)
		htmlSaveFileEnc("-", doc, encoding);
	    else
		htmlDocDump(stdout, doc);
	} else
785
	    xmlDebugDumpDocument(stdout, doc);
786
#else
787 788 789 790
	if (encoding)
	    htmlSaveFileEnc("-", doc, encoding);
	else
	    htmlDocDump(stdout, doc);
791
#endif
792
    }
793
#endif /* LIBXML_OUTPUT_ENABLED */
794 795 796 797 798 799 800 801

    /*
     * free it.
     */
    xmlFreeDoc(doc);
}

int main(int argc, char **argv) {
802
    int i, count;
803 804 805
    int files = 0;

    for (i = 1; i < argc ; i++) {
806
#ifdef LIBXML_DEBUG_ENABLED
807 808
	if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
	    debug++;
809 810 811
	else
#endif
	    if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
812
	    copy++;
813
#ifdef LIBXML_PUSH_ENABLED
814 815
	else if ((!strcmp(argv[i], "-push")) || (!strcmp(argv[i], "--push")))
	    push++;
816
#endif /* LIBXML_PUSH_ENABLED */
817 818 819 820 821 822 823
	else if ((!strcmp(argv[i], "-sax")) || (!strcmp(argv[i], "--sax")))
	    sax++;
	else if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout")))
	    noout++;
	else if ((!strcmp(argv[i], "-repeat")) ||
	         (!strcmp(argv[i], "--repeat")))
	    repeat++;
824 825 826 827 828
	else if ((!strcmp(argv[i], "-encode")) ||
	         (!strcmp(argv[i], "--encode"))) {
	    i++;
	    encoding = argv[i];
        }
829 830
    }
    for (i = 1; i < argc ; i++) {
831 832 833 834 835
	if ((!strcmp(argv[i], "-encode")) ||
	         (!strcmp(argv[i], "--encode"))) {
	    i++;
	    continue;
        }
836
	if (argv[i][0] != '-') {
837 838 839 840
	    if (repeat) {
		for (count = 0;count < 100 * repeat;count++) {
		    if (sax)
			parseSAXFile(argv[i]);
841
		    else
842
			parseAndPrintFile(argv[i]);
843
		}
844 845 846
	    } else {
		if (sax)
		    parseSAXFile(argv[i]);
847
		else
848 849
		    parseAndPrintFile(argv[i]);
	    }
850 851 852 853
	    files ++;
	}
    }
    if (files == 0) {
854
	printf("Usage : %s [--debug] [--copy] [--copy] HTMLfiles ...\n",
855 856
	       argv[0]);
	printf("\tParse the HTML files and output the result of the parsing\n");
857
#ifdef LIBXML_DEBUG_ENABLED
858
	printf("\t--debug : dump a debug tree of the in-memory document\n");
859
#endif
860
	printf("\t--copy : used to test the internal copy implementation\n");
861
	printf("\t--sax : debug the sequence of SAX callbacks\n");
862
	printf("\t--repeat : parse the file 100 times, for timing\n");
863
	printf("\t--noout : do not print the result\n");
864
#ifdef LIBXML_PUSH_ENABLED
865
	printf("\t--push : use the push mode parser\n");
866
#endif /* LIBXML_PUSH_ENABLED */
867
	printf("\t--encode encoding : output in the given encoding\n");
868
    }
869
    xmlCleanupParser();
870
    xmlMemoryDump();
871 872 873

    return(0);
}
874 875
#else /* !LIBXML_HTML_ENABLED */
#include <stdio.h>
876
int main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
877 878 879 880
    printf("%s : HTML support not compiled in\n", argv[0]);
    return(0);
}
#endif