Annotation of XML/testHTML.c, revision 1.16

1.1       daniel      1: /*
                      2:  * testHTML.c : a small tester program for HTML input.
                      3:  *
                      4:  * See Copyright for the status of this software.
                      5:  *
                      6:  * Daniel.Veillard@w3.org
                      7:  */
                      8: 
                      9: #ifdef WIN32
1.9       daniel     10: #include "win32config.h"
1.1       daniel     11: #else
1.4       daniel     12: #include "config.h"
1.1       daniel     13: #endif
1.3       daniel     14: 
1.12      daniel     15: #include "xmlversion.h"
                     16: #ifdef LIBXML_HTML_ENABLED
                     17: 
1.3       daniel     18: #include <stdio.h>
                     19: #include <string.h>
1.7       daniel     20: #include <stdarg.h>
                     21: 
1.3       daniel     22: 
                     23: #ifdef HAVE_SYS_TYPES_H
1.1       daniel     24: #include <sys/types.h>
1.3       daniel     25: #endif
1.1       daniel     26: #ifdef HAVE_SYS_STAT_H
                     27: #include <sys/stat.h>
                     28: #endif
                     29: #ifdef HAVE_FCNTL_H
                     30: #include <fcntl.h>
                     31: #endif
                     32: #ifdef HAVE_UNISTD_H
                     33: #include <unistd.h>
                     34: #endif
1.3       daniel     35: #ifdef HAVE_STDLIB_H
1.1       daniel     36: #include <stdlib.h>
1.3       daniel     37: #endif
1.1       daniel     38: 
1.12      daniel     39: #include <libxml/xmlmemory.h>
                     40: #include <libxml/HTMLparser.h>
                     41: #include <libxml/HTMLtree.h>
                     42: #include <libxml/debugXML.h>
1.1       daniel     43: 
1.12      daniel     44: #ifdef LIBXML_DEBUG_ENABLED
1.1       daniel     45: static int debug = 0;
1.12      daniel     46: #endif
1.1       daniel     47: static int copy = 0;
1.7       daniel     48: static int sax = 0;
                     49: static int repeat = 0;
                     50: static int noout = 0;
1.10      daniel     51: static int push = 0;
1.13      veillard   52: static char *encoding = NULL;
1.1       daniel     53: 
1.7       daniel     54: xmlSAXHandler emptySAXHandlerStruct = {
                     55:     NULL, /* internalSubset */
                     56:     NULL, /* isStandalone */
                     57:     NULL, /* hasInternalSubset */
                     58:     NULL, /* hasExternalSubset */
                     59:     NULL, /* resolveEntity */
                     60:     NULL, /* getEntity */
                     61:     NULL, /* entityDecl */
                     62:     NULL, /* notationDecl */
                     63:     NULL, /* attributeDecl */
                     64:     NULL, /* elementDecl */
                     65:     NULL, /* unparsedEntityDecl */
                     66:     NULL, /* setDocumentLocator */
                     67:     NULL, /* startDocument */
                     68:     NULL, /* endDocument */
                     69:     NULL, /* startElement */
                     70:     NULL, /* endElement */
                     71:     NULL, /* reference */
                     72:     NULL, /* characters */
                     73:     NULL, /* ignorableWhitespace */
                     74:     NULL, /* processingInstruction */
                     75:     NULL, /* comment */
                     76:     NULL, /* xmlParserWarning */
                     77:     NULL, /* xmlParserError */
                     78:     NULL, /* xmlParserError */
                     79:     NULL, /* getParameterEntity */
                     80: };
                     81: 
                     82: xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
                     83: extern xmlSAXHandlerPtr debugSAXHandler;
                     84: 
                     85: /************************************************************************
                     86:  *                                                                     *
                     87:  *                             Debug Handlers                          *
                     88:  *                                                                     *
                     89:  ************************************************************************/
                     90: 
                     91: /**
                     92:  * isStandaloneDebug:
                     93:  * @ctxt:  An XML parser context
                     94:  *
                     95:  * Is this document tagged standalone ?
                     96:  *
                     97:  * Returns 1 if true
                     98:  */
                     99: int
                    100: isStandaloneDebug(void *ctx)
                    101: {
                    102:     fprintf(stdout, "SAX.isStandalone()\n");
                    103:     return(0);
                    104: }
                    105: 
                    106: /**
                    107:  * hasInternalSubsetDebug:
                    108:  * @ctxt:  An XML parser context
                    109:  *
                    110:  * Does this document has an internal subset
                    111:  *
                    112:  * Returns 1 if true
                    113:  */
                    114: int
                    115: hasInternalSubsetDebug(void *ctx)
                    116: {
                    117:     fprintf(stdout, "SAX.hasInternalSubset()\n");
                    118:     return(0);
                    119: }
                    120: 
                    121: /**
                    122:  * hasExternalSubsetDebug:
                    123:  * @ctxt:  An XML parser context
                    124:  *
                    125:  * Does this document has an external subset
                    126:  *
                    127:  * Returns 1 if true
                    128:  */
                    129: int
                    130: hasExternalSubsetDebug(void *ctx)
                    131: {
                    132:     fprintf(stdout, "SAX.hasExternalSubset()\n");
                    133:     return(0);
                    134: }
                    135: 
                    136: /**
                    137:  * hasInternalSubsetDebug:
                    138:  * @ctxt:  An XML parser context
                    139:  *
                    140:  * Does this document has an internal subset
                    141:  */
                    142: void
                    143: internalSubsetDebug(void *ctx, const xmlChar *name,
                    144:               const xmlChar *ExternalID, const xmlChar *SystemID)
                    145: {
                    146:     /* xmlDtdPtr externalSubset; */
                    147: 
                    148:     fprintf(stdout, "SAX.internalSubset(%s, %s, %s)\n",
1.16    ! veillard  149:             name, (ExternalID == NULL) ? "(null)" : ExternalID,
        !           150:                   (SystemID   == NULL) ? "(null)" : SystemID);
1.7       daniel    151: 
                    152: /***********
                    153:     if ((ExternalID != NULL) || (SystemID != NULL)) {
                    154:         externalSubset = xmlParseDTD(ExternalID, SystemID);
                    155:        if (externalSubset != NULL) {
                    156:            xmlFreeDtd(externalSubset);
                    157:        }
                    158:     }
                    159:  ***********/
                    160: }
                    161: 
                    162: /**
                    163:  * resolveEntityDebug:
                    164:  * @ctxt:  An XML parser context
                    165:  * @publicId: The public ID of the entity
                    166:  * @systemId: The system ID of the entity
                    167:  *
                    168:  * Special entity resolver, better left to the parser, it has
                    169:  * more context than the application layer.
                    170:  * The default behaviour is to NOT resolve the entities, in that case
                    171:  * the ENTITY_REF nodes are built in the structure (and the parameter
                    172:  * values).
                    173:  *
                    174:  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
                    175:  */
                    176: xmlParserInputPtr
                    177: resolveEntityDebug(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
                    178: {
                    179:     /* xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; */
                    180: 
                    181:     
                    182:     fprintf(stdout, "SAX.resolveEntity(");
                    183:     if (publicId != NULL)
                    184:        fprintf(stdout, "%s", (char *)publicId);
                    185:     else
                    186:        fprintf(stdout, " ");
                    187:     if (systemId != NULL)
                    188:        fprintf(stdout, ", %s)\n", (char *)systemId);
                    189:     else
                    190:        fprintf(stdout, ", )\n");
                    191: /*********
                    192:     if (systemId != NULL) {
                    193:         return(xmlNewInputFromFile(ctxt, (char *) systemId));
                    194:     }
                    195:  *********/
                    196:     return(NULL);
                    197: }
                    198: 
                    199: /**
                    200:  * getEntityDebug:
                    201:  * @ctxt:  An XML parser context
                    202:  * @name: The entity name
                    203:  *
                    204:  * Get an entity by name
                    205:  *
                    206:  * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
                    207:  */
                    208: xmlEntityPtr
                    209: getEntityDebug(void *ctx, const xmlChar *name)
                    210: {
                    211:     fprintf(stdout, "SAX.getEntity(%s)\n", name);
                    212:     return(NULL);
                    213: }
                    214: 
                    215: /**
                    216:  * getParameterEntityDebug:
                    217:  * @ctxt:  An XML parser context
                    218:  * @name: The entity name
                    219:  *
                    220:  * Get a parameter entity by name
                    221:  *
                    222:  * Returns the xmlParserInputPtr
                    223:  */
                    224: xmlEntityPtr
                    225: getParameterEntityDebug(void *ctx, const xmlChar *name)
                    226: {
                    227:     fprintf(stdout, "SAX.getParameterEntity(%s)\n", name);
                    228:     return(NULL);
                    229: }
                    230: 
                    231: 
                    232: /**
                    233:  * entityDeclDebug:
                    234:  * @ctxt:  An XML parser context
                    235:  * @name:  the entity name 
                    236:  * @type:  the entity type 
                    237:  * @publicId: The public ID of the entity
                    238:  * @systemId: The system ID of the entity
                    239:  * @content: the entity value (without processing).
                    240:  *
                    241:  * An entity definition has been parsed
                    242:  */
                    243: void
                    244: entityDeclDebug(void *ctx, const xmlChar *name, int type,
                    245:           const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
                    246: {
                    247:     fprintf(stdout, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
                    248:             name, type, publicId, systemId, content);
                    249: }
                    250: 
                    251: /**
                    252:  * attributeDeclDebug:
                    253:  * @ctxt:  An XML parser context
                    254:  * @name:  the attribute name 
                    255:  * @type:  the attribute type 
                    256:  *
                    257:  * An attribute definition has been parsed
                    258:  */
                    259: void
                    260: attributeDeclDebug(void *ctx, const xmlChar *elem, const xmlChar *name,
                    261:               int type, int def, const xmlChar *defaultValue,
                    262:              xmlEnumerationPtr tree)
                    263: {
                    264:     fprintf(stdout, "SAX.attributeDecl(%s, %s, %d, %d, %s, ...)\n",
                    265:             elem, name, type, def, defaultValue);
                    266: }
                    267: 
                    268: /**
                    269:  * elementDeclDebug:
                    270:  * @ctxt:  An XML parser context
                    271:  * @name:  the element name 
                    272:  * @type:  the element type 
                    273:  * @content: the element value (without processing).
                    274:  *
                    275:  * An element definition has been parsed
                    276:  */
                    277: void
                    278: elementDeclDebug(void *ctx, const xmlChar *name, int type,
                    279:            xmlElementContentPtr content)
                    280: {
                    281:     fprintf(stdout, "SAX.elementDecl(%s, %d, ...)\n",
                    282:             name, type);
                    283: }
                    284: 
                    285: /**
                    286:  * notationDeclDebug:
                    287:  * @ctxt:  An XML parser context
                    288:  * @name: The name of the notation
                    289:  * @publicId: The public ID of the entity
                    290:  * @systemId: The system ID of the entity
                    291:  *
                    292:  * What to do when a notation declaration has been parsed.
                    293:  */
                    294: void
                    295: notationDeclDebug(void *ctx, const xmlChar *name,
                    296:             const xmlChar *publicId, const xmlChar *systemId)
                    297: {
                    298:     fprintf(stdout, "SAX.notationDecl(%s, %s, %s)\n",
                    299:             (char *) name, (char *) publicId, (char *) systemId);
                    300: }
                    301: 
                    302: /**
                    303:  * unparsedEntityDeclDebug:
                    304:  * @ctxt:  An XML parser context
                    305:  * @name: The name of the entity
                    306:  * @publicId: The public ID of the entity
                    307:  * @systemId: The system ID of the entity
                    308:  * @notationName: the name of the notation
                    309:  *
                    310:  * What to do when an unparsed entity declaration is parsed
                    311:  */
                    312: void
                    313: unparsedEntityDeclDebug(void *ctx, const xmlChar *name,
                    314:                   const xmlChar *publicId, const xmlChar *systemId,
                    315:                   const xmlChar *notationName)
                    316: {
                    317:     fprintf(stdout, "SAX.unparsedEntityDecl(%s, %s, %s, %s)\n",
                    318:             (char *) name, (char *) publicId, (char *) systemId,
                    319:            (char *) notationName);
                    320: }
                    321: 
                    322: /**
                    323:  * setDocumentLocatorDebug:
                    324:  * @ctxt:  An XML parser context
                    325:  * @loc: A SAX Locator
                    326:  *
                    327:  * Receive the document locator at startup, actually xmlDefaultSAXLocator
                    328:  * Everything is available on the context, so this is useless in our case.
                    329:  */
                    330: void
                    331: setDocumentLocatorDebug(void *ctx, xmlSAXLocatorPtr loc)
                    332: {
                    333:     fprintf(stdout, "SAX.setDocumentLocator()\n");
                    334: }
                    335: 
                    336: /**
                    337:  * startDocumentDebug:
                    338:  * @ctxt:  An XML parser context
                    339:  *
                    340:  * called when the document start being processed.
                    341:  */
                    342: void
                    343: startDocumentDebug(void *ctx)
                    344: {
                    345:     fprintf(stdout, "SAX.startDocument()\n");
                    346: }
                    347: 
                    348: /**
                    349:  * endDocumentDebug:
                    350:  * @ctxt:  An XML parser context
                    351:  *
                    352:  * called when the document end has been detected.
                    353:  */
                    354: void
                    355: endDocumentDebug(void *ctx)
                    356: {
                    357:     fprintf(stdout, "SAX.endDocument()\n");
                    358: }
                    359: 
                    360: /**
                    361:  * startElementDebug:
                    362:  * @ctxt:  An XML parser context
                    363:  * @name:  The element name
                    364:  *
                    365:  * called when an opening tag has been processed.
                    366:  */
                    367: void
                    368: startElementDebug(void *ctx, const xmlChar *name, const xmlChar **atts)
                    369: {
                    370:     int i;
                    371: 
                    372:     fprintf(stdout, "SAX.startElement(%s", (char *) name);
                    373:     if (atts != NULL) {
                    374:         for (i = 0;(atts[i] != NULL);i++) {
                    375:            fprintf(stdout, ", %s='", atts[i++]);
1.16    ! veillard  376:            fprintf(stdout, "%s'", (atts[i] == NULL) ? "(null)" : atts[i]);
1.7       daniel    377:        }
                    378:     }
                    379:     fprintf(stdout, ")\n");
                    380: }
                    381: 
                    382: /**
                    383:  * endElementDebug:
                    384:  * @ctxt:  An XML parser context
                    385:  * @name:  The element name
                    386:  *
                    387:  * called when the end of an element has been detected.
                    388:  */
                    389: void
                    390: endElementDebug(void *ctx, const xmlChar *name)
                    391: {
                    392:     fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
                    393: }
                    394: 
                    395: /**
                    396:  * charactersDebug:
                    397:  * @ctxt:  An XML parser context
                    398:  * @ch:  a xmlChar string
                    399:  * @len: the number of xmlChar
                    400:  *
                    401:  * receiving some chars from the parser.
                    402:  * Question: how much at a time ???
                    403:  */
                    404: void
                    405: charactersDebug(void *ctx, const xmlChar *ch, int len)
                    406: {
1.15      veillard  407:     char output[40];
1.7       daniel    408:     int i;
                    409: 
1.15      veillard  410:     for (i = 0;(i<len) && (i < 30);i++)
                    411:        output[i] = ch[i];
                    412:     output[i] = 0;
                    413: 
                    414:     fprintf(stdout, "SAX.characters(%s, %d)\n", output, len);
1.7       daniel    415: }
                    416: 
                    417: /**
                    418:  * referenceDebug:
                    419:  * @ctxt:  An XML parser context
                    420:  * @name:  The entity name
                    421:  *
                    422:  * called when an entity reference is detected. 
                    423:  */
                    424: void
                    425: referenceDebug(void *ctx, const xmlChar *name)
                    426: {
                    427:     fprintf(stdout, "SAX.reference(%s)\n", name);
                    428: }
                    429: 
                    430: /**
                    431:  * ignorableWhitespaceDebug:
                    432:  * @ctxt:  An XML parser context
                    433:  * @ch:  a xmlChar string
                    434:  * @start: the first char in the string
                    435:  * @len: the number of xmlChar
                    436:  *
                    437:  * receiving some ignorable whitespaces from the parser.
                    438:  * Question: how much at a time ???
                    439:  */
                    440: void
                    441: ignorableWhitespaceDebug(void *ctx, const xmlChar *ch, int len)
                    442: {
1.15      veillard  443:     char output[40];
                    444:     int i;
                    445: 
                    446:     for (i = 0;(i<len) && (i < 30);i++)
                    447:        output[i] = ch[i];
                    448:     output[i] = 0;
                    449: 
                    450:     fprintf(stdout, "SAX.ignorableWhitespace(%s, %d)\n", output, len);
1.7       daniel    451: }
                    452: 
                    453: /**
                    454:  * processingInstructionDebug:
                    455:  * @ctxt:  An XML parser context
                    456:  * @target:  the target name
                    457:  * @data: the PI data's
                    458:  * @len: the number of xmlChar
                    459:  *
                    460:  * A processing instruction has been parsed.
                    461:  */
                    462: void
                    463: processingInstructionDebug(void *ctx, const xmlChar *target,
                    464:                       const xmlChar *data)
                    465: {
                    466:     fprintf(stdout, "SAX.processingInstruction(%s, %s)\n",
                    467:             (char *) target, (char *) data);
                    468: }
                    469: 
                    470: /**
                    471:  * commentDebug:
                    472:  * @ctxt:  An XML parser context
                    473:  * @value:  the comment content
                    474:  *
                    475:  * A comment has been parsed.
                    476:  */
                    477: void
                    478: commentDebug(void *ctx, const xmlChar *value)
                    479: {
                    480:     fprintf(stdout, "SAX.comment(%s)\n", value);
                    481: }
                    482: 
                    483: /**
                    484:  * warningDebug:
                    485:  * @ctxt:  An XML parser context
                    486:  * @msg:  the message to display/transmit
                    487:  * @...:  extra parameters for the message display
                    488:  *
                    489:  * Display and format a warning messages, gives file, line, position and
                    490:  * extra parameters.
                    491:  */
                    492: void
                    493: warningDebug(void *ctx, const char *msg, ...)
                    494: {
                    495:     va_list args;
                    496: 
                    497:     va_start(args, msg);
                    498:     fprintf(stdout, "SAX.warning: ");
                    499:     vfprintf(stdout, msg, args);
                    500:     va_end(args);
                    501: }
                    502: 
                    503: /**
                    504:  * errorDebug:
                    505:  * @ctxt:  An XML parser context
                    506:  * @msg:  the message to display/transmit
                    507:  * @...:  extra parameters for the message display
                    508:  *
                    509:  * Display and format a error messages, gives file, line, position and
                    510:  * extra parameters.
                    511:  */
                    512: void
                    513: errorDebug(void *ctx, const char *msg, ...)
                    514: {
                    515:     va_list args;
                    516: 
                    517:     va_start(args, msg);
                    518:     fprintf(stdout, "SAX.error: ");
                    519:     vfprintf(stdout, msg, args);
                    520:     va_end(args);
                    521: }
                    522: 
                    523: /**
                    524:  * fatalErrorDebug:
                    525:  * @ctxt:  An XML parser context
                    526:  * @msg:  the message to display/transmit
                    527:  * @...:  extra parameters for the message display
                    528:  *
                    529:  * Display and format a fatalError messages, gives file, line, position and
                    530:  * extra parameters.
                    531:  */
                    532: void
                    533: fatalErrorDebug(void *ctx, const char *msg, ...)
                    534: {
                    535:     va_list args;
                    536: 
                    537:     va_start(args, msg);
                    538:     fprintf(stdout, "SAX.fatalError: ");
                    539:     vfprintf(stdout, msg, args);
                    540:     va_end(args);
                    541: }
                    542: 
                    543: xmlSAXHandler debugSAXHandlerStruct = {
                    544:     internalSubsetDebug,
                    545:     isStandaloneDebug,
                    546:     hasInternalSubsetDebug,
                    547:     hasExternalSubsetDebug,
                    548:     resolveEntityDebug,
                    549:     getEntityDebug,
                    550:     entityDeclDebug,
                    551:     notationDeclDebug,
                    552:     attributeDeclDebug,
                    553:     elementDeclDebug,
                    554:     unparsedEntityDeclDebug,
                    555:     setDocumentLocatorDebug,
                    556:     startDocumentDebug,
                    557:     endDocumentDebug,
                    558:     startElementDebug,
                    559:     endElementDebug,
                    560:     referenceDebug,
                    561:     charactersDebug,
                    562:     ignorableWhitespaceDebug,
                    563:     processingInstructionDebug,
                    564:     commentDebug,
                    565:     warningDebug,
                    566:     errorDebug,
                    567:     fatalErrorDebug,
                    568:     getParameterEntityDebug,
                    569: };
                    570: 
                    571: xmlSAXHandlerPtr debugSAXHandler = &debugSAXHandlerStruct;
1.1       daniel    572: /************************************************************************
                    573:  *                                                                     *
                    574:  *                             Debug                                   *
                    575:  *                                                                     *
                    576:  ************************************************************************/
                    577: 
1.7       daniel    578: void parseSAXFile(char *filename) {
                    579:     htmlDocPtr doc;
                    580:     /*
                    581:      * Empty callbacks for checking
                    582:      */
1.15      veillard  583:     if (push) {
                    584:        FILE *f;
                    585: 
                    586:        f = fopen(filename, "r");
                    587:        if (f != NULL) {
                    588:            int res, size = 3;
                    589:            char chars[4096];
                    590:            htmlParserCtxtPtr ctxt;
1.7       daniel    591: 
1.15      veillard  592:            /* if (repeat) */
                    593:                size = 4096;
                    594:            res = fread(chars, 1, 4, f);
                    595:            if (res > 0) {
                    596:                ctxt = htmlCreatePushParserCtxt(emptySAXHandler, NULL,
                    597:                            chars, res, filename, 0);
                    598:                while ((res = fread(chars, 1, size, f)) > 0) {
                    599:                    htmlParseChunk(ctxt, chars, res, 0);
                    600:                }
                    601:                htmlParseChunk(ctxt, chars, 0, 1);
                    602:                doc = ctxt->myDoc;
                    603:                htmlFreeParserCtxt(ctxt);
                    604:            }
                    605:            if (doc != NULL) {
                    606:                fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    607:                xmlFreeDoc(doc);
                    608:            }
                    609:            fclose(f);
                    610:        }
                    611:        if (!noout) {
                    612:            f = fopen(filename, "r");
                    613:            if (f != NULL) {
                    614:                int res, size = 3;
                    615:                char chars[4096];
                    616:                htmlParserCtxtPtr ctxt;
                    617: 
                    618:                /* if (repeat) */
                    619:                    size = 4096;
                    620:                res = fread(chars, 1, 4, f);
                    621:                if (res > 0) {
                    622:                    ctxt = htmlCreatePushParserCtxt(debugSAXHandler, NULL,
                    623:                                chars, res, filename, 0);
                    624:                    while ((res = fread(chars, 1, size, f)) > 0) {
                    625:                        htmlParseChunk(ctxt, chars, res, 0);
                    626:                    }
                    627:                    htmlParseChunk(ctxt, chars, 0, 1);
                    628:                    doc = ctxt->myDoc;
                    629:                    htmlFreeParserCtxt(ctxt);
                    630:                }
                    631:                if (doc != NULL) {
                    632:                    fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    633:                    xmlFreeDoc(doc);
                    634:                }
                    635:                fclose(f);
                    636:            }
                    637:        }
                    638:     } else {   
                    639:        doc = htmlSAXParseFile(filename, NULL, emptySAXHandler, NULL);
1.7       daniel    640:        if (doc != NULL) {
                    641:            fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    642:            xmlFreeDoc(doc);
                    643:        }
1.15      veillard  644: 
                    645:        if (!noout) {
                    646:            /*
                    647:             * Debug callback
                    648:             */
                    649:            doc = htmlSAXParseFile(filename, NULL, debugSAXHandler, NULL);
                    650:            if (doc != NULL) {
                    651:                fprintf(stdout, "htmlSAXParseFile returned non-NULL\n");
                    652:                xmlFreeDoc(doc);
                    653:            }
                    654:        }
1.7       daniel    655:     }
                    656: }
                    657: 
1.1       daniel    658: void parseAndPrintFile(char *filename) {
1.11      daniel    659:     htmlDocPtr doc = NULL, tmp;
1.1       daniel    660: 
                    661:     /*
                    662:      * build an HTML tree from a string;
                    663:      */
1.10      daniel    664:     if (push) {
                    665:        FILE *f;
                    666: 
                    667:        f = fopen(filename, "r");
                    668:        if (f != NULL) {
                    669:            int res, size = 3;
1.14      veillard  670:            char chars[4096];
1.10      daniel    671:            htmlParserCtxtPtr ctxt;
                    672: 
1.14      veillard  673:            /* if (repeat) */
                    674:                size = 4096;
1.10      daniel    675:            res = fread(chars, 1, 4, f);
                    676:            if (res > 0) {
                    677:                ctxt = htmlCreatePushParserCtxt(NULL, NULL,
                    678:                            chars, res, filename, 0);
                    679:                while ((res = fread(chars, 1, size, f)) > 0) {
                    680:                    htmlParseChunk(ctxt, chars, res, 0);
                    681:                }
                    682:                htmlParseChunk(ctxt, chars, 0, 1);
                    683:                doc = ctxt->myDoc;
                    684:                htmlFreeParserCtxt(ctxt);
                    685:            }
1.15      veillard  686:            fclose(f);
1.10      daniel    687:        }
                    688:     } else {   
                    689:        doc = htmlParseFile(filename, NULL);
                    690:     }
                    691:     if (doc == NULL) {
                    692:         fprintf(stderr, "Could not parse %s\n", filename);
                    693:     }
1.1       daniel    694: 
                    695:     /*
                    696:      * test intermediate copy if needed.
                    697:      */
                    698:     if (copy) {
                    699:         tmp = doc;
                    700:        doc = xmlCopyDoc(doc, 1);
                    701:        xmlFreeDoc(tmp);
                    702:     }
                    703: 
                    704:     /*
                    705:      * print it.
                    706:      */
1.7       daniel    707:     if (!noout) { 
1.12      daniel    708: #ifdef LIBXML_DEBUG_ENABLED
1.13      veillard  709:        if (!debug) {
                    710:            if (encoding)
                    711:                htmlSaveFileEnc("-", doc, encoding);
                    712:            else
                    713:                htmlDocDump(stdout, doc);
                    714:        } else
1.7       daniel    715:            xmlDebugDumpDocument(stdout, doc);
1.12      daniel    716: #else
1.13      veillard  717:        if (encoding)
                    718:            htmlSaveFileEnc("-", doc, encoding);
                    719:        else
                    720:            htmlDocDump(stdout, doc);
1.12      daniel    721: #endif
1.7       daniel    722:     }  
1.1       daniel    723: 
                    724:     /*
                    725:      * free it.
                    726:      */
                    727:     xmlFreeDoc(doc);
                    728: }
                    729: 
                    730: int main(int argc, char **argv) {
1.7       daniel    731:     int i, count;
1.1       daniel    732:     int files = 0;
                    733: 
                    734:     for (i = 1; i < argc ; i++) {
1.12      daniel    735: #ifdef LIBXML_DEBUG_ENABLED
1.1       daniel    736:        if ((!strcmp(argv[i], "-debug")) || (!strcmp(argv[i], "--debug")))
                    737:            debug++;
1.12      daniel    738:        else
                    739: #endif
                    740:            if ((!strcmp(argv[i], "-copy")) || (!strcmp(argv[i], "--copy")))
1.1       daniel    741:            copy++;
1.10      daniel    742:        else if ((!strcmp(argv[i], "-push")) || (!strcmp(argv[i], "--push")))
                    743:            push++;
1.7       daniel    744:        else if ((!strcmp(argv[i], "-sax")) || (!strcmp(argv[i], "--sax")))
                    745:            sax++;
                    746:        else if ((!strcmp(argv[i], "-noout")) || (!strcmp(argv[i], "--noout")))
                    747:            noout++;
                    748:        else if ((!strcmp(argv[i], "-repeat")) ||
                    749:                 (!strcmp(argv[i], "--repeat")))
                    750:            repeat++;
1.13      veillard  751:        else if ((!strcmp(argv[i], "-encode")) ||
                    752:                 (!strcmp(argv[i], "--encode"))) {
                    753:            i++;
                    754:            encoding = argv[i];
                    755:         }
1.1       daniel    756:     }
                    757:     for (i = 1; i < argc ; i++) {
1.13      veillard  758:        if ((!strcmp(argv[i], "-encode")) ||
                    759:                 (!strcmp(argv[i], "--encode"))) {
                    760:            i++;
                    761:            continue;
                    762:         }
1.1       daniel    763:        if (argv[i][0] != '-') {
1.7       daniel    764:            if (repeat) {
                    765:                for (count = 0;count < 100 * repeat;count++) {
                    766:                    if (sax)
                    767:                        parseSAXFile(argv[i]);
                    768:                    else   
                    769:                        parseAndPrintFile(argv[i]);
                    770:                }    
                    771:            } else {
                    772:                if (sax)
                    773:                    parseSAXFile(argv[i]);
                    774:                else   
                    775:                    parseAndPrintFile(argv[i]);
                    776:            }
1.1       daniel    777:            files ++;
                    778:        }
                    779:     }
                    780:     if (files == 0) {
1.7       daniel    781:        printf("Usage : %s [--debug] [--copy] [--copy] HTMLfiles ...\n",
1.1       daniel    782:               argv[0]);
                    783:        printf("\tParse the HTML files and output the result of the parsing\n");
1.12      daniel    784: #ifdef LIBXML_DEBUG_ENABLED
1.1       daniel    785:        printf("\t--debug : dump a debug tree of the in-memory document\n");
1.12      daniel    786: #endif
1.1       daniel    787:        printf("\t--copy : used to test the internal copy implementation\n");
1.7       daniel    788:        printf("\t--sax : debug the sequence of SAX callbacks\n");
1.10      daniel    789:        printf("\t--repeat : parse the file 100 times, for timing\n");
1.7       daniel    790:        printf("\t--noout : do not print the result\n");
1.10      daniel    791:        printf("\t--push : use the push mode parser\n");
1.13      veillard  792:        printf("\t--encode encoding : output in the given encoding\n");
1.1       daniel    793:     }
1.8       daniel    794:     xmlCleanupParser();
1.6       daniel    795:     xmlMemoryDump();
1.1       daniel    796: 
                    797:     return(0);
                    798: }
1.12      daniel    799: #else /* !LIBXML_HTML_ENABLED */
                    800: #include <stdio.h>
                    801: int main(int argc, char **argv) {
                    802:     printf("%s : HTML support not compiled in\n", argv[0]);
                    803:     return(0);
                    804: }
                    805: #endif

Webmaster