Annotation of XML/HTMLparser.c, revision 1.46

1.1       daniel      1: /*
                      2:  * HTMLparser.c : an HTML 4.0 non-verifying parser
                      3:  *
                      4:  * See Copyright for the status of this software.
                      5:  *
                      6:  * Daniel.Veillard@w3.org
                      7:  */
                      8: 
                      9: #ifdef WIN32
1.29      daniel     10: #include "win32config.h"
1.1       daniel     11: #else
1.13      daniel     12: #include "config.h"
1.1       daniel     13: #endif
1.13      daniel     14: 
1.39      daniel     15: #include "xmlversion.h"
                     16: #ifdef LIBXML_HTML_ENABLED
                     17: 
1.1       daniel     18: #include <stdio.h>
1.13      daniel     19: #include <string.h> /* for memset() only */
                     20: #ifdef HAVE_CTYPE_H
1.1       daniel     21: #include <ctype.h>
1.13      daniel     22: #endif
                     23: #ifdef HAVE_STDLIB_H
1.1       daniel     24: #include <stdlib.h>
1.13      daniel     25: #endif
                     26: #ifdef HAVE_SYS_STAT_H
1.1       daniel     27: #include <sys/stat.h>
1.13      daniel     28: #endif
1.1       daniel     29: #ifdef HAVE_FCNTL_H
                     30: #include <fcntl.h>
                     31: #endif
                     32: #ifdef HAVE_UNISTD_H
                     33: #include <unistd.h>
                     34: #endif
                     35: #ifdef HAVE_ZLIB_H
                     36: #include <zlib.h>
                     37: #endif
                     38: 
1.39      daniel     39: #include <libxml/xmlmemory.h>
                     40: #include <libxml/tree.h>
                     41: #include <libxml/HTMLparser.h>
                     42: #include <libxml/entities.h>
                     43: #include <libxml/encoding.h>
                     44: #include <libxml/valid.h>
                     45: #include <libxml/parserInternals.h>
                     46: #include <libxml/xmlIO.h>
1.31      daniel     47: #include "xml-error.h"
1.5       daniel     48: 
                     49: #define HTML_MAX_NAMELEN 1000
                     50: #define INPUT_CHUNK     50
1.31      daniel     51: #define HTML_PARSER_BIG_BUFFER_SIZE 1024
                     52: #define HTML_PARSER_BUFFER_SIZE 100
1.1       daniel     53: 
                     54: /* #define DEBUG */
1.31      daniel     55: /* #define DEBUG_PUSH */
1.1       daniel     56: 
                     57: /************************************************************************
                     58:  *                                                                     *
                     59:  *             Parser stacks related functions and macros              *
                     60:  *                                                                     *
                     61:  ************************************************************************/
                     62: 
                     63: /*
                     64:  * Generic function for accessing stacks in the Parser Context
                     65:  */
                     66: 
1.30      daniel     67: #define PUSH_AND_POP(scope, type, name)                                        \
                     68: scope int html##name##Push(htmlParserCtxtPtr ctxt, type value) {       \
1.1       daniel     69:     if (ctxt->name##Nr >= ctxt->name##Max) {                           \
                     70:        ctxt->name##Max *= 2;                                           \
1.11      daniel     71:         ctxt->name##Tab = (void *) xmlRealloc(ctxt->name##Tab,         \
1.1       daniel     72:                     ctxt->name##Max * sizeof(ctxt->name##Tab[0]));     \
                     73:         if (ctxt->name##Tab == NULL) {                                 \
                     74:            fprintf(stderr, "realloc failed !\n");                      \
1.33      daniel     75:            return(0);                                                  \
1.1       daniel     76:        }                                                               \
                     77:     }                                                                  \
                     78:     ctxt->name##Tab[ctxt->name##Nr] = value;                           \
                     79:     ctxt->name = value;                                                        \
                     80:     return(ctxt->name##Nr++);                                          \
                     81: }                                                                      \
1.30      daniel     82: scope type html##name##Pop(htmlParserCtxtPtr ctxt) {                   \
1.1       daniel     83:     type ret;                                                          \
1.18      daniel     84:     if (ctxt->name##Nr < 0) return(0);                                 \
1.1       daniel     85:     ctxt->name##Nr--;                                                  \
1.18      daniel     86:     if (ctxt->name##Nr < 0) return(0);                                 \
1.1       daniel     87:     if (ctxt->name##Nr > 0)                                            \
                     88:        ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1];               \
                     89:     else                                                               \
                     90:         ctxt->name = NULL;                                             \
                     91:     ret = ctxt->name##Tab[ctxt->name##Nr];                             \
                     92:     ctxt->name##Tab[ctxt->name##Nr] = 0;                               \
                     93:     return(ret);                                                       \
                     94: }                                                                      \
                     95: 
1.30      daniel     96: PUSH_AND_POP(extern, xmlNodePtr, node)
                     97: PUSH_AND_POP(extern, xmlChar*, name)
1.1       daniel     98: 
                     99: /*
                    100:  * Macros for accessing the content. Those should be used only by the parser,
                    101:  * and not exported.
                    102:  *
                    103:  * Dirty macros, i.e. one need to make assumption on the context to use them
                    104:  *
1.14      daniel    105:  *   CUR_PTR return the current pointer to the xmlChar to be parsed.
                    106:  *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
1.1       daniel    107:  *           in ISO-Latin or UTF-8, and the current 16 bit value if compiled
                    108:  *           in UNICODE mode. This should be used internally by the parser
                    109:  *           only to compare to ASCII values otherwise it would break when
                    110:  *           running with UTF-8 encoding.
1.14      daniel    111:  *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
1.1       daniel    112:  *           to compare on ASCII based substring.
1.14      daniel    113:  *   UPP(n)  returns the n'th next xmlChar converted to uppercase. Same as CUR
1.1       daniel    114:  *           it should be used only to compare on ASCII based substring.
1.14      daniel    115:  *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
1.1       daniel    116:  *           strings within the parser.
                    117:  *
                    118:  * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
                    119:  *
                    120:  *   CURRENT Returns the current char value, with the full decoding of
                    121:  *           UTF-8 if we are using this mode. It returns an int.
                    122:  *   NEXT    Skip to the next character, this does the proper decoding
                    123:  *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
                    124:  *   COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
                    125:  */
                    126: 
1.36      daniel    127: #define CUR ((int) (*ctxt->input->cur))
                    128:     
1.1       daniel    129: #define UPPER (toupper(*ctxt->input->cur))
1.36      daniel    130: 
1.26      daniel    131: #define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val)
1.36      daniel    132: 
1.1       daniel    133: #define NXT(val) ctxt->input->cur[(val)]
1.36      daniel    134: 
1.1       daniel    135: #define UPP(val) (toupper(ctxt->input->cur[(val)]))
1.36      daniel    136: 
1.1       daniel    137: #define CUR_PTR ctxt->input->cur
1.36      daniel    138: 
1.5       daniel    139: #define SHRINK  xmlParserInputShrink(ctxt->input)
1.36      daniel    140: 
1.5       daniel    141: #define GROW  xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
1.1       daniel    142: 
1.36      daniel    143: #define CURRENT ((int) (*ctxt->input->cur))
1.1       daniel    144: 
1.36      daniel    145: #define NEXT htmlNextChar(ctxt);
1.35      daniel    146: 
1.36      daniel    147: #define SKIP_BLANKS htmlSkipBlankChars(ctxt);
1.35      daniel    148: 
                    149: /**
                    150:  * htmlNextChar:
                    151:  * @ctxt:  the HTML parser context
                    152:  *
                    153:  * Skip to the next char input char.
                    154:  */
                    155: 
                    156: void
                    157: htmlNextChar(htmlParserCtxtPtr ctxt) {
1.44      daniel    158:     if (ctxt->instate == XML_PARSER_EOF)
                    159:        return;
1.35      daniel    160:     if ((*ctxt->input->cur == 0) &&
                    161:         (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
                    162:            xmlPopInput(ctxt);
                    163:     } else {
                    164:         if (*(ctxt->input->cur) == '\n') {
                    165:            ctxt->input->line++; ctxt->input->col = 1;
                    166:        } else ctxt->input->col++;
                    167:        ctxt->input->cur++;
                    168:        ctxt->nbChars++;
                    169:         if (*ctxt->input->cur == 0)
                    170:            xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
                    171:     }
                    172: }
1.5       daniel    173: 
1.36      daniel    174: /**
                    175:  * htmlSkipBlankChars:
                    176:  * @ctxt:  the HTML parser context
                    177:  *
                    178:  * skip all blanks character found at that point in the input streams.
                    179:  *
                    180:  * Returns the number of space chars skipped
                    181:  */
                    182: 
                    183: int
                    184: htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
                    185:     int res = 0;
                    186: 
                    187:     while (IS_BLANK(*(ctxt->input->cur))) {
                    188:        if ((*ctxt->input->cur == 0) &&
                    189:            (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
                    190:                xmlPopInput(ctxt);
                    191:        } else {
                    192:            if (*(ctxt->input->cur) == '\n') {
                    193:                ctxt->input->line++; ctxt->input->col = 1;
                    194:            } else ctxt->input->col++;
                    195:            ctxt->input->cur++;
                    196:            ctxt->nbChars++;
                    197:            if (*ctxt->input->cur == 0)
                    198:                xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
                    199:        }
                    200:        res++;
                    201:     }
                    202:     return(res);
                    203: }
1.1       daniel    204: 
                    205: 
1.5       daniel    206: 
1.1       daniel    207: /************************************************************************
                    208:  *                                                                     *
                    209:  *             The list of HTML elements and their properties          *
                    210:  *                                                                     *
                    211:  ************************************************************************/
                    212: 
                    213: /*
                    214:  *  Start Tag: 1 means the start tag can be ommited
                    215:  *  End Tag:   1 means the end tag can be ommited
                    216:  *             2 means it's forbidden (empty elements)
                    217:  *  Depr:      this element is deprecated
                    218:  *  DTD:       1 means that this element is valid only in the Loose DTD
                    219:  *             2 means that this element is valid only in the Frameset DTD
                    220:  *
                    221:  * Name,Start Tag,End Tag,  Empty,  Depr.,    DTD, Description
                    222:  */
                    223: htmlElemDesc  html40ElementTable[] = {
1.26      daniel    224: { "a",         0,      0,      0,      0,      0, "anchor " },
                    225: { "abbr",      0,      0,      0,      0,      0, "abbreviated form" },
                    226: { "acronym",   0,      0,      0,      0,      0, "" },
                    227: { "address",   0,      0,      0,      0,      0, "information on author " },
                    228: { "applet",    0,      0,      0,      1,      1, "java applet " },
                    229: { "area",      0,      2,      1,      0,      0, "client-side image map area " },
                    230: { "b",         0,      0,      0,      0,      0, "bold text style" },
                    231: { "base",      0,      2,      1,      0,      0, "document base uri " },
                    232: { "basefont",  0,      2,      1,      1,      1, "base font size " },
                    233: { "bdo",       0,      0,      0,      0,      0, "i18n bidi over-ride " },
                    234: { "big",       0,      0,      0,      0,      0, "large text style" },
                    235: { "blockquote",        0,      0,      0,      0,      0, "long quotation " },
                    236: { "body",      1,      1,      0,      0,      0, "document body " },
                    237: { "br",                0,      2,      1,      0,      0, "forced line break " },
                    238: { "button",    0,      0,      0,      0,      0, "push button " },
                    239: { "caption",   0,      0,      0,      0,      0, "table caption " },
                    240: { "center",    0,      0,      0,      1,      1, "shorthand for div align=center " },
                    241: { "cite",      0,      0,      0,      0,      0, "citation" },
                    242: { "code",      0,      0,      0,      0,      0, "computer code fragment" },
                    243: { "col",       0,      2,      1,      0,      0, "table column " },
                    244: { "colgroup",  0,      1,      0,      0,      0, "table column group " },
                    245: { "dd",                0,      1,      0,      0,      0, "definition description " },
                    246: { "del",       0,      0,      0,      0,      0, "deleted text " },
                    247: { "dfn",       0,      0,      0,      0,      0, "instance definition" },
                    248: { "dir",       0,      0,      0,      1,      1, "directory list" },
                    249: { "div",       0,      0,      0,      0,      0, "generic language/style container"},
                    250: { "dl",                0,      0,      0,      0,      0, "definition list " },
                    251: { "dt",                0,      1,      0,      0,      0, "definition term " },
                    252: { "em",                0,      0,      0,      0,      0, "emphasis" },
                    253: { "fieldset",  0,      0,      0,      0,      0, "form control group " },
                    254: { "font",      0,      0,      0,      1,      1, "local change to font " },
                    255: { "form",      0,      0,      0,      0,      0, "interactive form " },
                    256: { "frame",     0,      2,      1,      0,      2, "subwindow " },
                    257: { "frameset",  0,      0,      0,      0,      2, "window subdivision" },
                    258: { "h1",                0,      0,      0,      0,      0, "heading " },
                    259: { "h2",                0,      0,      0,      0,      0, "heading " },
                    260: { "h3",                0,      0,      0,      0,      0, "heading " },
                    261: { "h4",                0,      0,      0,      0,      0, "heading " },
                    262: { "h5",                0,      0,      0,      0,      0, "heading " },
                    263: { "h6",                0,      0,      0,      0,      0, "heading " },
                    264: { "head",      1,      1,      0,      0,      0, "document head " },
                    265: { "hr",                0,      2,      1,      0,      0, "horizontal rule " },
                    266: { "html",      1,      1,      0,      0,      0, "document root element " },
                    267: { "i",         0,      0,      0,      0,      0, "italic text style" },
                    268: { "iframe",    0,      0,      0,      0,      1, "inline subwindow " },
                    269: { "img",       0,      2,      1,      0,      0, "embedded image " },
                    270: { "input",     0,      2,      1,      0,      0, "form control " },
                    271: { "ins",       0,      0,      0,      0,      0, "inserted text" },
                    272: { "isindex",   0,      2,      1,      1,      1, "single line prompt " },
                    273: { "kbd",       0,      0,      0,      0,      0, "text to be entered by the user" },
                    274: { "label",     0,      0,      0,      0,      0, "form field label text " },
                    275: { "legend",    0,      0,      0,      0,      0, "fieldset legend " },
                    276: { "li",                0,      1,      0,      0,      0, "list item " },
                    277: { "link",      0,      2,      1,      0,      0, "a media-independent link " },
                    278: { "map",       0,      0,      0,      0,      0, "client-side image map " },
                    279: { "menu",      0,      0,      0,      1,      1, "menu list " },
                    280: { "meta",      0,      2,      1,      0,      0, "generic metainformation " },
                    281: { "noframes",  0,      0,      0,      0,      2, "alternate content container for non frame-based rendering " },
                    282: { "noscript",  0,      0,      0,      0,      0, "alternate content container for non script-based rendering " },
                    283: { "object",    0,      0,      0,      0,      0, "generic embedded object " },
                    284: { "ol",                0,      0,      0,      0,      0, "ordered list " },
                    285: { "optgroup",  0,      0,      0,      0,      0, "option group " },
                    286: { "option",    0,      1,      0,      0,      0, "selectable choice " },
                    287: { "p",         0,      1,      0,      0,      0, "paragraph " },
                    288: { "param",     0,      2,      1,      0,      0, "named property value " },
                    289: { "pre",       0,      0,      0,      0,      0, "preformatted text " },
                    290: { "q",         0,      0,      0,      0,      0, "short inline quotation " },
                    291: { "s",         0,      0,      0,      1,      1, "strike-through text style" },
                    292: { "samp",      0,      0,      0,      0,      0, "sample program output, scripts, etc." },
                    293: { "script",    0,      0,      0,      0,      0, "script statements " },
                    294: { "select",    0,      0,      0,      0,      0, "option selector " },
                    295: { "small",     0,      0,      0,      0,      0, "small text style" },
                    296: { "span",      0,      0,      0,      0,      0, "generic language/style container " },
                    297: { "strike",    0,      0,      0,      1,      1, "strike-through text" },
                    298: { "strong",    0,      0,      0,      0,      0, "strong emphasis" },
                    299: { "style",     0,      0,      0,      0,      0, "style info " },
                    300: { "sub",       0,      0,      0,      0,      0, "subscript" },
                    301: { "sup",       0,      0,      0,      0,      0, "superscript " },
                    302: { "table",     0,      0,      0,      0,      0, "&#160;" },
                    303: { "tbody",     1,      1,      0,      0,      0, "table body " },
                    304: { "td",                0,      1,      0,      0,      0, "table data cell" },
                    305: { "textarea",  0,      0,      0,      0,      0, "multi-line text field " },
                    306: { "tfoot",     0,      1,      0,      0,      0, "table footer " },
                    307: { "th",                0,      1,      0,      0,      0, "table header cell" },
                    308: { "thead",     0,      1,      0,      0,      0, "table header " },
                    309: { "title",     0,      0,      0,      0,      0, "document title " },
                    310: { "tr",                0,      1,      0,      0,      0, "table row " },
                    311: { "tt",                0,      0,      0,      0,      0, "teletype or monospaced text style" },
                    312: { "u",         0,      0,      0,      1,      1, "underlined text style" },
                    313: { "ul",                0,      0,      0,      0,      0, "unordered list " },
                    314: { "var",       0,      0,      0,      0,      0, "instance of a variable or program argument" },
1.1       daniel    315: };
                    316: 
                    317: /*
                    318:  * start tags that imply the end of a current element
                    319:  * any tag of each line implies the end of the current element if the type of
                    320:  * that element is in the same line
                    321:  */
1.8       daniel    322: char *htmlEquEnd[] = {
1.26      daniel    323: "dt", "dd", "li", "option", NULL,
                    324: "h1", "h2", "h3", "h4", "h5", "h6", NULL,
                    325: "ol", "menu", "dir", "address", "pre", "listing", "xmp", NULL,
1.1       daniel    326: NULL
                    327: };
                    328: /*
                    329:  * acording the HTML DTD, HR should be added to the 2nd line above, as it
                    330:  * is not allowed within a H1, H2, H3, etc. But we should tolerate that case
                    331:  * because many documents contain rules in headings...
                    332:  */
                    333: 
                    334: /*
                    335:  * start tags that imply the end of current element
                    336:  */
1.8       daniel    337: char *htmlStartClose[] = {
1.26      daniel    338: "form",                "form", "p", "hr", "h1", "h2", "h3", "h4", "h5", "h6",
                    339:                "dl", "ul", "ol", "menu", "dir", "address", "pre",
                    340:                "listing", "xmp", "head", NULL,
                    341: "head",                "p", NULL,
                    342: "title",       "p", NULL,
                    343: "body",                "head", "style", "link", "title", "p", NULL,
                    344: "li",          "p", "h1", "h2", "h3", "h4", "h5", "h6", "dl", "address",
                    345:                "pre", "listing", "xmp", "head", "li", NULL,
                    346: "hr",          "p", "head", NULL,
                    347: "h1",          "p", "head", NULL,
                    348: "h2",          "p", "head", NULL,
                    349: "h3",          "p", "head", NULL,
                    350: "h4",          "p", "head", NULL,
                    351: "h5",          "p", "head", NULL,
                    352: "h6",          "p", "head", NULL,
                    353: "dir",         "p", "head", NULL,
                    354: "address",     "p", "head", "ul", NULL,
                    355: "pre",         "p", "head", "ul", NULL,
                    356: "listing",     "p", "head", NULL,
                    357: "xmp",         "p", "head", NULL,
                    358: "blockquote",  "p", "head", NULL,
                    359: "dl",          "p", "dt", "menu", "dir", "address", "pre", "listing",
                    360:                "xmp", "head", NULL,
                    361: "dt",          "p", "menu", "dir", "address", "pre", "listing", "xmp",
                    362:                 "head", "dd", NULL,
                    363: "dd",          "p", "menu", "dir", "address", "pre", "listing", "xmp",
                    364:                 "head", "dt", NULL,
                    365: "ul",          "p", "head", "ol", "menu", "dir", "address", "pre",
                    366:                "listing", "xmp", NULL,
                    367: "ol",          "p", "head", "ul", NULL,
                    368: "menu",                "p", "head", "ul", NULL,
                    369: "p",           "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", NULL,
                    370: "div",         "p", "head", NULL,
                    371: "noscript",    "p", "head", NULL,
                    372: "center",      "font", "b", "i", "p", "head", NULL,
                    373: "a",           "a", NULL,
                    374: "caption",     "p", NULL,
                    375: "colgroup",    "caption", "colgroup", "col", "p", NULL,
                    376: "col",         "caption", "col", "p", NULL,
                    377: "table",       "p", "head", "h1", "h2", "h3", "h4", "h5", "h6", "pre",
                    378:                "listing", "xmp", "a", NULL,
                    379: "th",          "th", "td", NULL,
                    380: "td",          "th", "td", "p", NULL,
                    381: "tr",          "th", "td", "tr", "caption", "col", "colgroup", "p", NULL,
                    382: "thead",       "caption", "col", "colgroup", NULL,
                    383: "tfoot",       "th", "td", "tr", "caption", "col", "colgroup", "thead",
                    384:                "tbody", "p", NULL,
                    385: "tbody",       "th", "td", "tr", "caption", "col", "colgroup", "thead",
                    386:                "tfoot", "tbody", "p", NULL,
                    387: "optgroup",    "option", NULL,
                    388: "fieldset",    "legend", "p", "head", "h1", "h2", "h3", "h4", "h5", "h6",
                    389:                "pre", "listing", "xmp", "a", NULL,
1.1       daniel    390: NULL
                    391: };
                    392: 
1.43      daniel    393: 
1.8       daniel    394: static char** htmlStartCloseIndex[100];
1.1       daniel    395: static int htmlStartCloseIndexinitialized = 0;
                    396: 
                    397: /************************************************************************
                    398:  *                                                                     *
                    399:  *             functions to handle HTML specific data                  *
                    400:  *                                                                     *
                    401:  ************************************************************************/
                    402: 
                    403: /**
                    404:  * htmlInitAutoClose:
                    405:  *
                    406:  * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
                    407:  *
                    408:  */
                    409: void
                    410: htmlInitAutoClose(void) {
                    411:     int index, i = 0;
                    412: 
                    413:     if (htmlStartCloseIndexinitialized) return;
                    414: 
                    415:     for (index = 0;index < 100;index ++) htmlStartCloseIndex[index] = NULL;
                    416:     index = 0;
                    417:     while ((htmlStartClose[i] != NULL) && (index < 100 - 1)) {
                    418:         htmlStartCloseIndex[index++] = &htmlStartClose[i];
                    419:        while (htmlStartClose[i] != NULL) i++;
                    420:        i++;
                    421:     }
                    422: }
                    423: 
                    424: /**
                    425:  * htmlTagLookup:
                    426:  * @tag:  The tag name
                    427:  *
                    428:  * Lookup the HTML tag in the ElementTable
                    429:  *
                    430:  * Returns the related htmlElemDescPtr or NULL if not found.
                    431:  */
                    432: htmlElemDescPtr
1.14      daniel    433: htmlTagLookup(const xmlChar *tag) {
1.1       daniel    434:     int i = 0;
                    435: 
                    436:     for (i = 0; i < (sizeof(html40ElementTable) /
                    437:                      sizeof(html40ElementTable[0]));i++) {
1.8       daniel    438:         if (!xmlStrcmp(tag, BAD_CAST html40ElementTable[i].name))
1.1       daniel    439:            return(&html40ElementTable[i]);
                    440:     }
                    441:     return(NULL);
                    442: }
                    443: 
                    444: /**
                    445:  * htmlCheckAutoClose:
                    446:  * @new:  The new tag name
                    447:  * @old:  The old tag name
                    448:  *
                    449:  * Checks wether the new tag is one of the registered valid tags for closing old.
                    450:  * Initialize the htmlStartCloseIndex for fast lookup of closing tags names.
                    451:  *
                    452:  * Returns 0 if no, 1 if yes.
                    453:  */
                    454: int
1.14      daniel    455: htmlCheckAutoClose(const xmlChar *new, const xmlChar *old) {
1.1       daniel    456:     int i, index;
1.8       daniel    457:     char **close;
1.1       daniel    458: 
                    459:     if (htmlStartCloseIndexinitialized == 0) htmlInitAutoClose();
                    460: 
                    461:     /* inefficient, but not a big deal */
                    462:     for (index = 0; index < 100;index++) {
                    463:         close = htmlStartCloseIndex[index];
                    464:        if (close == NULL) return(0);
1.8       daniel    465:        if (!xmlStrcmp(BAD_CAST *close, new)) break;
1.1       daniel    466:     }
                    467: 
                    468:     i = close - htmlStartClose;
                    469:     i++;
                    470:     while (htmlStartClose[i] != NULL) {
1.8       daniel    471:         if (!xmlStrcmp(BAD_CAST htmlStartClose[i], old)) {
1.1       daniel    472:            return(1);
                    473:        }
                    474:        i++;
                    475:     }
                    476:     return(0);
                    477: }
                    478: 
                    479: /**
                    480:  * htmlAutoClose:
                    481:  * @ctxt:  an HTML parser context
                    482:  * @new:  The new tag name
                    483:  *
                    484:  * The HTmL DtD allows a tag to implicitely close other tags.
                    485:  * The list is kept in htmlStartClose array. This function is
                    486:  * called when a new tag has been detected and generates the
                    487:  * appropriates closes if possible/needed.
                    488:  */
                    489: void
1.14      daniel    490: htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar *new) {
1.15      daniel    491:     xmlChar *oldname;
                    492:     while ((ctxt->name != NULL) && 
                    493:            (htmlCheckAutoClose(new, ctxt->name))) {
1.1       daniel    494: #ifdef DEBUG
1.18      daniel    495:        fprintf(stderr,"htmlAutoClose: %s closes %s\n", new, ctxt->name);
1.1       daniel    496: #endif
                    497:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1.15      daniel    498:            ctxt->sax->endElement(ctxt->userData, ctxt->name);
1.24      daniel    499:        oldname = htmlnamePop(ctxt);
1.18      daniel    500:        if (oldname != NULL) {
                    501: #ifdef DEBUG
                    502:            fprintf(stderr,"htmlAutoClose: popped %s\n", oldname);
                    503: #endif
1.17      daniel    504:            xmlFree(oldname);
1.18      daniel    505:         }
1.1       daniel    506:     }
                    507: }
                    508: 
                    509: /**
1.28      daniel    510:  * htmlAutoCloseTag:
                    511:  * @doc:  the HTML document
                    512:  * @name:  The tag name
                    513:  * @elem:  the HTML element
                    514:  *
                    515:  * The HTmL DtD allows a tag to implicitely close other tags.
                    516:  * The list is kept in htmlStartClose array. This function checks
                    517:  * if the element or one of it's children would autoclose the
                    518:  * given tag.
                    519:  *
                    520:  * Returns 1 if autoclose, 0 otherwise
                    521:  */
                    522: int
                    523: htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
                    524:     htmlNodePtr child;
                    525: 
                    526:     if (elem == NULL) return(1);
                    527:     if (!xmlStrcmp(name, elem->name)) return(0);
                    528:     if (htmlCheckAutoClose(elem->name, name)) return(1);
1.37      daniel    529:     child = elem->children;
1.28      daniel    530:     while (child != NULL) {
                    531:         if (htmlAutoCloseTag(doc, name, child)) return(1);
                    532:        child = child->next;
                    533:     }
                    534:     return(0);
                    535: }
                    536: 
                    537: /**
                    538:  * htmlIsAutoClosed:
                    539:  * @doc:  the HTML document
                    540:  * @elem:  the HTML element
                    541:  *
                    542:  * The HTmL DtD allows a tag to implicitely close other tags.
                    543:  * The list is kept in htmlStartClose array. This function checks
                    544:  * if a tag is autoclosed by one of it's child
                    545:  *
                    546:  * Returns 1 if autoclosed, 0 otherwise
                    547:  */
                    548: int
                    549: htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
                    550:     htmlNodePtr child;
                    551: 
                    552:     if (elem == NULL) return(1);
1.37      daniel    553:     child = elem->children;
1.28      daniel    554:     while (child != NULL) {
                    555:        if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
                    556:        child = child->next;
                    557:     }
                    558:     return(0);
                    559: }
                    560: 
                    561: /**
1.1       daniel    562:  * htmlAutoCloseOnClose:
                    563:  * @ctxt:  an HTML parser context
                    564:  * @new:  The new tag name
                    565:  *
                    566:  * The HTmL DtD allows an ending tag to implicitely close other tags.
                    567:  */
                    568: void
1.14      daniel    569: htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar *new) {
1.1       daniel    570:     htmlElemDescPtr info;
1.15      daniel    571:     xmlChar *oldname;
1.18      daniel    572:     int i;
1.1       daniel    573: 
1.18      daniel    574: #ifdef DEBUG
                    575:     fprintf(stderr,"Close of %s stack: %d elements\n", new, ctxt->nameNr);
                    576:     for (i = 0;i < ctxt->nameNr;i++) 
                    577:         fprintf(stderr,"%d : %s\n", i, ctxt->nameTab[i]);
                    578: #endif
                    579: 
                    580:     for (i = (ctxt->nameNr - 1);i >= 0;i--) {
                    581:         if (!xmlStrcmp(new, ctxt->nameTab[i])) break;
                    582:     }
                    583:     if (i < 0) return;
                    584: 
                    585:     while (xmlStrcmp(new, ctxt->name)) {
1.15      daniel    586:        info = htmlTagLookup(ctxt->name);
1.1       daniel    587:        if ((info == NULL) || (info->endTag == 1)) {
                    588: #ifdef DEBUG
1.18      daniel    589:            fprintf(stderr,"htmlAutoCloseOnClose: %s closes %s\n", new, ctxt->name);
                    590: #endif
                    591:         } else {
                    592:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                    593:                ctxt->sax->error(ctxt->userData,
                    594:                 "Opening and ending tag mismatch: %s and %s\n",
                    595:                                 new, ctxt->name);
                    596:            ctxt->wellFormed = 0;
                    597:        }
                    598:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                    599:            ctxt->sax->endElement(ctxt->userData, ctxt->name);
1.24      daniel    600:        oldname = htmlnamePop(ctxt);
1.18      daniel    601:        if (oldname != NULL) {
                    602: #ifdef DEBUG
                    603:            fprintf(stderr,"htmlAutoCloseOnClose: popped %s\n", oldname);
1.1       daniel    604: #endif
1.18      daniel    605:            xmlFree(oldname);
                    606:        }       
1.1       daniel    607:     }
                    608: }
                    609: 
1.43      daniel    610: /**
                    611:  * htmlCheckImplied:
                    612:  * @ctxt:  an HTML parser context
                    613:  * @new:  The new tag name
                    614:  *
                    615:  * The HTmL DtD allows a tag to exists only implicitely
                    616:  * called when a new tag has been detected and generates the
                    617:  * appropriates implicit tags if missing
                    618:  */
                    619: void
                    620: htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *new) {
1.46    ! daniel    621:     if (!xmlStrcmp(new, BAD_CAST"html"))
1.43      daniel    622:        return;
                    623:     if (ctxt->nameNr <= 0) {
                    624: #ifdef DEBUG
                    625:        fprintf(stderr,"Implied element html: pushed html\n");
                    626: #endif    
                    627:        htmlnamePush(ctxt, xmlStrdup(BAD_CAST"html"));
                    628:        if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    629:            ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
                    630:     }
1.46    ! daniel    631:     if ((!xmlStrcmp(new, BAD_CAST"body")) || (!xmlStrcmp(new, BAD_CAST"head")))
1.43      daniel    632:         return;
                    633:     if (ctxt->nameNr <= 1) {
1.46    ! daniel    634:        if ((!xmlStrcmp(new, BAD_CAST"script")) ||
        !           635:            (!xmlStrcmp(new, BAD_CAST"style")) ||
        !           636:            (!xmlStrcmp(new, BAD_CAST"meta")) ||
        !           637:            (!xmlStrcmp(new, BAD_CAST"link")) ||
        !           638:            (!xmlStrcmp(new, BAD_CAST"title")) ||
        !           639:            (!xmlStrcmp(new, BAD_CAST"base"))) {
1.43      daniel    640:            /* 
                    641:             * dropped OBJECT ... i you put it first BODY will be
                    642:             * assumed !
                    643:             */
                    644: #ifdef DEBUG
                    645:            fprintf(stderr,"Implied element head: pushed head\n");
                    646: #endif    
                    647:            htmlnamePush(ctxt, xmlStrdup(BAD_CAST"head"));
                    648:            if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    649:                ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
                    650:        } else {
                    651: #ifdef DEBUG
                    652:            fprintf(stderr,"Implied element body: pushed body\n");
                    653: #endif    
                    654:            htmlnamePush(ctxt, xmlStrdup(BAD_CAST"body"));
                    655:            if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                    656:                ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
                    657:        }
                    658:     }
                    659: }
                    660: 
1.1       daniel    661: /************************************************************************
                    662:  *                                                                     *
                    663:  *             The list of HTML predefined entities                    *
                    664:  *                                                                     *
                    665:  ************************************************************************/
                    666: 
                    667: 
                    668: htmlEntityDesc  html40EntitiesTable[] = {
                    669: /*
                    670:  * the 4 absolute ones,
                    671:  */
                    672: { 34,  "quot", "quotation mark = APL quote, U+0022 ISOnum" },
                    673: { 38,  "amp",  "ampersand, U+0026 ISOnum" },
                    674: { 60,  "lt",   "less-than sign, U+003C ISOnum" },
                    675: { 62,  "gt",   "greater-than sign, U+003E ISOnum" },
                    676: 
                    677: /*
                    678:  * A bunch still in the 128-255 range
                    679:  * Replacing them depend really on the charset used.
                    680:  */
1.28      daniel    681: { 39,  "apos", "single quote" },
1.1       daniel    682: { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
                    683: { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
                    684: { 162, "cent", "cent sign, U+00A2 ISOnum" },
                    685: { 163, "pound","pound sign, U+00A3 ISOnum" },
                    686: { 164, "curren","currency sign, U+00A4 ISOnum" },
                    687: { 165, "yen",  "yen sign = yuan sign, U+00A5 ISOnum" },
                    688: { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
                    689: { 167, "sect", "section sign, U+00A7 ISOnum" },
                    690: { 168, "uml",  "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
                    691: { 169, "copy", "copyright sign, U+00A9 ISOnum" },
                    692: { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
                    693: { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
                    694: { 172, "not",  "not sign, U+00AC ISOnum" },
                    695: { 173, "shy",  "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
                    696: { 174, "reg",  "registered sign = registered trade mark sign, U+00AE ISOnum" },
                    697: { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
                    698: { 176, "deg",  "degree sign, U+00B0 ISOnum" },
                    699: { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
                    700: { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
                    701: { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
                    702: { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
                    703: { 181, "micro","micro sign, U+00B5 ISOnum" },
                    704: { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
1.7       daniel    705: { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
1.1       daniel    706: { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
                    707: { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
                    708: { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
1.7       daniel    709: { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
1.1       daniel    710: { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
                    711: { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
                    712: { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
                    713: { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
                    714: { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
                    715: { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
                    716: { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
                    717: { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
                    718: { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
                    719: { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
                    720: { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
                    721: { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
                    722: { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
                    723: { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
                    724: { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
                    725: { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
                    726: { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
                    727: { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
                    728: { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
                    729: { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
                    730: { 208, "ETH",  "latin capital letter ETH, U+00D0 ISOlat1" },
                    731: { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
                    732: { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
                    733: { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
                    734: { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
                    735: { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
                    736: { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
                    737: { 215, "times","multiplication sign, U+00D7 ISOnum" },
1.7       daniel    738: { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
1.1       daniel    739: { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
                    740: { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
                    741: { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
                    742: { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
                    743: { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
                    744: { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
                    745: { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
                    746: { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
                    747: { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
                    748: { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
                    749: { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
                    750: { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
                    751: { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
                    752: { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
                    753: { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
                    754: { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
                    755: { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
                    756: { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
                    757: { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
                    758: { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
                    759: { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
                    760: { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
                    761: { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
                    762: { 240, "eth",  "latin small letter eth, U+00F0 ISOlat1" },
                    763: { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
                    764: { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
                    765: { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
                    766: { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
                    767: { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
                    768: { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
                    769: { 247, "divide","division sign, U+00F7 ISOnum" },
                    770: { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
                    771: { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
                    772: { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
                    773: { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
                    774: { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
                    775: { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
                    776: { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
                    777: { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
                    778: 
                    779: /*
                    780:  * Anything below should really be kept as entities references
                    781:  */
                    782: { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
                    783: 
                    784: { 913, "Alpha","greek capital letter alpha, U+0391" },
                    785: { 914, "Beta", "greek capital letter beta, U+0392" },
                    786: { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
                    787: { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
                    788: { 917, "Epsilon","greek capital letter epsilon, U+0395" },
                    789: { 918, "Zeta", "greek capital letter zeta, U+0396" },
                    790: { 919, "Eta",  "greek capital letter eta, U+0397" },
                    791: { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
                    792: { 921, "Iota", "greek capital letter iota, U+0399" },
                    793: { 922, "Kappa","greek capital letter kappa, U+039A" },
                    794: { 923, "Lambda""greek capital letter lambda, U+039B ISOgrk3" },
                    795: { 924, "Mu",   "greek capital letter mu, U+039C" },
                    796: { 925, "Nu",   "greek capital letter nu, U+039D" },
                    797: { 926, "Xi",   "greek capital letter xi, U+039E ISOgrk3" },
                    798: { 927, "Omicron","greek capital letter omicron, U+039F" },
                    799: { 928, "Pi",   "greek capital letter pi, U+03A0 ISOgrk3" },
                    800: { 929, "Rho",  "greek capital letter rho, U+03A1" },
                    801: { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
                    802: { 932, "Tau",  "greek capital letter tau, U+03A4" },
                    803: { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
                    804: { 934, "Phi",  "greek capital letter phi, U+03A6 ISOgrk3" },
                    805: { 935, "Chi",  "greek capital letter chi, U+03A7" },
                    806: { 936, "Psi",  "greek capital letter psi, U+03A8 ISOgrk3" },
                    807: { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
                    808: 
                    809: { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
                    810: { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
                    811: { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
                    812: { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
                    813: { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
                    814: { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
                    815: { 951, "eta",  "greek small letter eta, U+03B7 ISOgrk3" },
                    816: { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
                    817: { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
                    818: { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
                    819: { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
                    820: { 956, "mu",   "greek small letter mu, U+03BC ISOgrk3" },
                    821: { 957, "nu",   "greek small letter nu, U+03BD ISOgrk3" },
                    822: { 958, "xi",   "greek small letter xi, U+03BE ISOgrk3" },
                    823: { 959, "omicron","greek small letter omicron, U+03BF NEW" },
                    824: { 960, "pi",   "greek small letter pi, U+03C0 ISOgrk3" },
                    825: { 961, "rho",  "greek small letter rho, U+03C1 ISOgrk3" },
                    826: { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
                    827: { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
                    828: { 964, "tau",  "greek small letter tau, U+03C4 ISOgrk3" },
                    829: { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
                    830: { 966, "phi",  "greek small letter phi, U+03C6 ISOgrk3" },
                    831: { 967, "chi",  "greek small letter chi, U+03C7 ISOgrk3" },
                    832: { 968, "psi",  "greek small letter psi, U+03C8 ISOgrk3" },
                    833: { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
                    834: { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
                    835: { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
                    836: { 982, "piv",  "greek pi symbol, U+03D6 ISOgrk3" },
                    837: 
                    838: { 8226,        "bull", "bullet = black small circle, U+2022 ISOpub" },
                    839: { 8230,        "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
                    840: { 8242,        "prime","prime = minutes = feet, U+2032 ISOtech" },
                    841: { 8243,        "Prime","double prime = seconds = inches, U+2033 ISOtech" },
                    842: { 8254,        "oline","overline = spacing overscore, U+203E NEW" },
                    843: { 8260,        "frasl","fraction slash, U+2044 NEW" },
                    844: 
1.7       daniel    845: { 8472,        "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
1.1       daniel    846: { 8465,        "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
                    847: { 8476,        "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
                    848: { 8482,        "trade","trade mark sign, U+2122 ISOnum" },
                    849: { 8501,        "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
                    850: { 8592,        "larr", "leftwards arrow, U+2190 ISOnum" },
                    851: { 8593,        "uarr", "upwards arrow, U+2191 ISOnum" },
                    852: { 8594,        "rarr", "rightwards arrow, U+2192 ISOnum" },
                    853: { 8595,        "darr", "downwards arrow, U+2193 ISOnum" },
                    854: { 8596,        "harr", "left right arrow, U+2194 ISOamsa" },
                    855: { 8629,        "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
                    856: { 8656,        "lArr", "leftwards double arrow, U+21D0 ISOtech" },
                    857: { 8657,        "uArr", "upwards double arrow, U+21D1 ISOamsa" },
                    858: { 8658,        "rArr", "rightwards double arrow, U+21D2 ISOtech" },
                    859: { 8659,        "dArr", "downwards double arrow, U+21D3 ISOamsa" },
                    860: { 8660,        "hArr", "left right double arrow, U+21D4 ISOamsa" },
                    861: 
                    862: 
                    863: { 8704,        "forall","for all, U+2200 ISOtech" },
                    864: { 8706,        "part", "partial differential, U+2202 ISOtech" },
                    865: { 8707,        "exist","there exists, U+2203 ISOtech" },
                    866: { 8709,        "empty","empty set = null set = diameter, U+2205 ISOamso" },
                    867: { 8711,        "nabla","nabla = backward difference, U+2207 ISOtech" },
                    868: { 8712,        "isin", "element of, U+2208 ISOtech" },
                    869: { 8713,        "notin","not an element of, U+2209 ISOtech" },
                    870: { 8715,        "ni",   "contains as member, U+220B ISOtech" },
                    871: { 8719,        "prod", "n-ary product = product sign, U+220F ISOamsb" },
                    872: { 8721,        "sum",  "n-ary sumation, U+2211 ISOamsb" },
                    873: { 8722,        "minus","minus sign, U+2212 ISOtech" },
                    874: { 8727,        "lowast","asterisk operator, U+2217 ISOtech" },
                    875: { 8730,        "radic","square root = radical sign, U+221A ISOtech" },
                    876: { 8733,        "prop", "proportional to, U+221D ISOtech" },
                    877: { 8734,        "infin","infinity, U+221E ISOtech" },
                    878: { 8736,        "ang",  "angle, U+2220 ISOamso" },
                    879: { 8743,        "and",  "logical and = wedge, U+2227 ISOtech" },
                    880: { 8744,        "or",   "logical or = vee, U+2228 ISOtech" },
                    881: { 8745,        "cap",  "intersection = cap, U+2229 ISOtech" },
                    882: { 8746,        "cup",  "union = cup, U+222A ISOtech" },
                    883: { 8747,        "int",  "integral, U+222B ISOtech" },
                    884: { 8756,        "there4","therefore, U+2234 ISOtech" },
                    885: { 8764,        "sim",  "tilde operator = varies with = similar to, U+223C ISOtech" },
                    886: { 8773,        "cong", "approximately equal to, U+2245 ISOtech" },
                    887: { 8776,        "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
                    888: { 8800,        "ne",   "not equal to, U+2260 ISOtech" },
                    889: { 8801,        "equiv","identical to, U+2261 ISOtech" },
                    890: { 8804,        "le",   "less-than or equal to, U+2264 ISOtech" },
                    891: { 8805,        "ge",   "greater-than or equal to, U+2265 ISOtech" },
                    892: { 8834,        "sub",  "subset of, U+2282 ISOtech" },
                    893: { 8835,        "sup",  "superset of, U+2283 ISOtech" },
                    894: { 8836,        "nsub", "not a subset of, U+2284 ISOamsn" },
                    895: { 8838,        "sube", "subset of or equal to, U+2286 ISOtech" },
                    896: { 8839,        "supe", "superset of or equal to, U+2287 ISOtech" },
                    897: { 8853,        "oplus","circled plus = direct sum, U+2295 ISOamsb" },
                    898: { 8855,        "otimes","circled times = vector product, U+2297 ISOamsb" },
                    899: { 8869,        "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
                    900: { 8901,        "sdot", "dot operator, U+22C5 ISOamsb" },
                    901: { 8968,        "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
                    902: { 8969,        "rceil","right ceiling, U+2309 ISOamsc" },
                    903: { 8970,        "lfloor","left floor = apl downstile, U+230A ISOamsc" },
                    904: { 8971,        "rfloor","right floor, U+230B ISOamsc" },
                    905: { 9001,        "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
                    906: { 9002,        "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
                    907: { 9674,        "loz",  "lozenge, U+25CA ISOpub" },
                    908: 
                    909: { 9824,        "spades","black spade suit, U+2660 ISOpub" },
                    910: { 9827,        "clubs","black club suit = shamrock, U+2663 ISOpub" },
                    911: { 9829,        "hearts","black heart suit = valentine, U+2665 ISOpub" },
                    912: { 9830,        "diams","black diamond suit, U+2666 ISOpub" },
                    913: 
                    914: { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
                    915: { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
                    916: { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
                    917: { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
                    918: { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
                    919: { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
                    920: { 732, "tilde","small tilde, U+02DC ISOdia" },
                    921: 
                    922: { 8194,        "ensp", "en space, U+2002 ISOpub" },
                    923: { 8195,        "emsp", "em space, U+2003 ISOpub" },
                    924: { 8201,        "thinsp","thin space, U+2009 ISOpub" },
                    925: { 8204,        "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
                    926: { 8205,        "zwj",  "zero width joiner, U+200D NEW RFC 2070" },
                    927: { 8206,        "lrm",  "left-to-right mark, U+200E NEW RFC 2070" },
                    928: { 8207,        "rlm",  "right-to-left mark, U+200F NEW RFC 2070" },
                    929: { 8211,        "ndash","en dash, U+2013 ISOpub" },
                    930: { 8212,        "mdash","em dash, U+2014 ISOpub" },
                    931: { 8216,        "lsquo","left single quotation mark, U+2018 ISOnum" },
                    932: { 8217,        "rsquo","right single quotation mark, U+2019 ISOnum" },
                    933: { 8218,        "sbquo","single low-9 quotation mark, U+201A NEW" },
                    934: { 8220,        "ldquo","left double quotation mark, U+201C ISOnum" },
                    935: { 8221,        "rdquo","right double quotation mark, U+201D ISOnum" },
                    936: { 8222,        "bdquo","double low-9 quotation mark, U+201E NEW" },
                    937: { 8224,        "dagger","dagger, U+2020 ISOpub" },
                    938: { 8225,        "Dagger","double dagger, U+2021 ISOpub" },
                    939: { 8240,        "permil","per mille sign, U+2030 ISOtech" },
                    940: { 8249,        "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
1.7       daniel    941: { 8250,        "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
1.1       daniel    942: { 8364,        "euro", "euro sign, U+20AC NEW" }
                    943: };
                    944: 
                    945: /************************************************************************
                    946:  *                                                                     *
                    947:  *             Commodity functions to handle entities                  *
                    948:  *                                                                     *
                    949:  ************************************************************************/
                    950: 
                    951: /*
                    952:  * Macro used to grow the current buffer.
                    953:  */
                    954: #define growBuffer(buffer) {                                           \
                    955:     buffer##_size *= 2;                                                        \
1.14      daniel    956:     buffer = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar));  \
1.1       daniel    957:     if (buffer == NULL) {                                              \
                    958:        perror("realloc failed");                                       \
1.33      daniel    959:        return(NULL);                                                   \
1.1       daniel    960:     }                                                                  \
                    961: }
                    962: 
                    963: /**
                    964:  * htmlEntityLookup:
                    965:  * @name: the entity name
                    966:  *
                    967:  * Lookup the given entity in EntitiesTable
                    968:  *
                    969:  * TODO: the linear scan is really ugly, an hash table is really needed.
                    970:  *
                    971:  * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
                    972:  */
                    973: htmlEntityDescPtr
1.14      daniel    974: htmlEntityLookup(const xmlChar *name) {
1.1       daniel    975:     int i;
                    976: 
                    977:     for (i = 0;i < (sizeof(html40EntitiesTable)/
                    978:                     sizeof(html40EntitiesTable[0]));i++) {
1.8       daniel    979:         if (!xmlStrcmp(name, BAD_CAST html40EntitiesTable[i].name)) {
1.1       daniel    980: #ifdef DEBUG
1.18      daniel    981:             fprintf(stderr,"Found entity %s\n", name);
1.1       daniel    982: #endif
                    983:             return(&html40EntitiesTable[i]);
                    984:        }
                    985:     }
                    986:     return(NULL);
                    987: }
                    988: 
                    989: 
                    990: /**
                    991:  * htmlDecodeEntities:
                    992:  * @ctxt:  the parser context
                    993:  * @len:  the len to decode (in bytes !), -1 for no size limit
1.14      daniel    994:  * @end:  an end marker xmlChar, 0 if none
                    995:  * @end2:  an end marker xmlChar, 0 if none
                    996:  * @end3:  an end marker xmlChar, 0 if none
1.1       daniel    997:  *
                    998:  * Subtitute the HTML entities by their value
                    999:  *
1.19      daniel   1000:  * DEPRECATED !!!!
1.1       daniel   1001:  *
                   1002:  * Returns A newly allocated string with the substitution done. The caller
                   1003:  *      must deallocate it !
                   1004:  */
1.14      daniel   1005: xmlChar *
1.1       daniel   1006: htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
1.14      daniel   1007:                   xmlChar end, xmlChar  end2, xmlChar end3) {
                   1008:     xmlChar *buffer = NULL;
1.1       daniel   1009:     int buffer_size = 0;
1.14      daniel   1010:     xmlChar *out = NULL;
                   1011:     xmlChar *name = NULL;
1.1       daniel   1012: 
1.14      daniel   1013:     xmlChar *cur = NULL;
1.1       daniel   1014:     htmlEntityDescPtr ent;
1.5       daniel   1015:     int nbchars = 0;
1.1       daniel   1016:     unsigned int max = (unsigned int) len;
                   1017: 
                   1018:     /*
                   1019:      * allocate a translation buffer.
                   1020:      */
1.31      daniel   1021:     buffer_size = HTML_PARSER_BIG_BUFFER_SIZE;
1.14      daniel   1022:     buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
1.1       daniel   1023:     if (buffer == NULL) {
                   1024:        perror("htmlDecodeEntities: malloc failed");
                   1025:        return(NULL);
                   1026:     }
                   1027:     out = buffer;
                   1028: 
                   1029:     /*
                   1030:      * Ok loop until we reach one of the ending char or a size limit.
                   1031:      */
1.45      daniel   1032:     while ((nbchars < (int) max) && (CUR != end) &&
1.1       daniel   1033:            (CUR != end2) && (CUR != end3)) {
                   1034: 
                   1035:         if (CUR == '&') {
                   1036:            if (NXT(1) == '#') {
                   1037:                int val = htmlParseCharRef(ctxt);
1.8       daniel   1038:                /* invalid for UTF-8 variable encoding !!!!! */
1.1       daniel   1039:                *out++ = val;
1.5       daniel   1040:                nbchars += 3; /* !!!! */
1.1       daniel   1041:            } else {
                   1042:                ent = htmlParseEntityRef(ctxt, &name);
                   1043:                if (name != NULL) {
                   1044:                    if ((ent == NULL) || (ent->value <= 0) ||
                   1045:                        (ent->value >= 255)) {
                   1046:                        *out++ = '&';
                   1047:                        cur = name;
                   1048:                        while (*cur != 0) {
                   1049:                            if (out - buffer > buffer_size - 100) {
                   1050:                                int index = out - buffer;
                   1051: 
                   1052:                                growBuffer(buffer);
                   1053:                                out = &buffer[index];
                   1054:                            }
                   1055:                            *out++ = *cur++;
                   1056:                        }
                   1057:                        *out++ = ';';
                   1058:                    } else {
1.8       daniel   1059:                        /* invalid for UTF-8 variable encoding !!!!! */
1.14      daniel   1060:                        *out++ = (xmlChar)ent->value;
1.1       daniel   1061:                        if (out - buffer > buffer_size - 100) {
                   1062:                            int index = out - buffer;
                   1063: 
                   1064:                            growBuffer(buffer);
                   1065:                            out = &buffer[index];
                   1066:                        }
                   1067:                    }
1.5       daniel   1068:                    nbchars += 2 + xmlStrlen(name);
1.11      daniel   1069:                    xmlFree(name);
1.1       daniel   1070:                }
                   1071:            }
                   1072:        } else {
1.8       daniel   1073:            /*  invalid for UTF-8 , use COPY(out); !!!!! */
1.1       daniel   1074:            *out++ = CUR;
1.5       daniel   1075:            nbchars++;
1.1       daniel   1076:            if (out - buffer > buffer_size - 100) {
                   1077:              int index = out - buffer;
                   1078:              
                   1079:              growBuffer(buffer);
                   1080:              out = &buffer[index];
                   1081:            }
                   1082:            NEXT;
                   1083:        }
                   1084:     }
                   1085:     *out++ = 0;
                   1086:     return(buffer);
                   1087: }
                   1088: 
                   1089: 
                   1090: /************************************************************************
                   1091:  *                                                                     *
                   1092:  *             Commodity functions to handle encodings                 *
                   1093:  *                                                                     *
                   1094:  ************************************************************************/
                   1095: 
                   1096: /**
                   1097:  * htmlSwitchEncoding:
                   1098:  * @ctxt:  the parser context
                   1099:  * @len:  the len of @cur
                   1100:  *
                   1101:  * change the input functions when discovering the character encoding
                   1102:  * of a given entity.
                   1103:  *
                   1104:  */
                   1105: void
                   1106: htmlSwitchEncoding(htmlParserCtxtPtr ctxt, xmlCharEncoding enc)
                   1107: {
                   1108:     switch (enc) {
                   1109:         case XML_CHAR_ENCODING_ERROR:
                   1110:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1111:                ctxt->sax->error(ctxt->userData, "encoding unknown\n");
                   1112:            ctxt->wellFormed = 0;
                   1113:             break;
                   1114:         case XML_CHAR_ENCODING_NONE:
                   1115:            /* let's assume it's UTF-8 without the XML decl */
                   1116:             return;
                   1117:         case XML_CHAR_ENCODING_UTF8:
                   1118:            /* default encoding, no conversion should be needed */
                   1119:             return;
                   1120:         case XML_CHAR_ENCODING_UTF16LE:
                   1121:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1122:                 ctxt->sax->error(ctxt->userData,
                   1123:                  "char encoding UTF16 little endian not supported\n");
                   1124:             break;
                   1125:         case XML_CHAR_ENCODING_UTF16BE:
                   1126:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1127:                 ctxt->sax->error(ctxt->userData,
                   1128:                  "char encoding UTF16 big endian not supported\n");
                   1129:             break;
                   1130:         case XML_CHAR_ENCODING_UCS4LE:
                   1131:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1132:                 ctxt->sax->error(ctxt->userData,
                   1133:                  "char encoding USC4 little endian not supported\n");
                   1134:             break;
                   1135:         case XML_CHAR_ENCODING_UCS4BE:
                   1136:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1137:                 ctxt->sax->error(ctxt->userData,
                   1138:                  "char encoding USC4 big endian not supported\n");
                   1139:             break;
                   1140:         case XML_CHAR_ENCODING_EBCDIC:
                   1141:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1142:                 ctxt->sax->error(ctxt->userData,
                   1143:                  "char encoding EBCDIC not supported\n");
                   1144:             break;
                   1145:         case XML_CHAR_ENCODING_UCS4_2143:
                   1146:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1147:                 ctxt->sax->error(ctxt->userData,
                   1148:                  "char encoding UCS4 2143 not supported\n");
                   1149:             break;
                   1150:         case XML_CHAR_ENCODING_UCS4_3412:
                   1151:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1152:                 ctxt->sax->error(ctxt->userData,
                   1153:                  "char encoding UCS4 3412 not supported\n");
                   1154:             break;
                   1155:         case XML_CHAR_ENCODING_UCS2:
                   1156:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1157:                 ctxt->sax->error(ctxt->userData,
                   1158:                  "char encoding UCS2 not supported\n");
                   1159:             break;
                   1160:         case XML_CHAR_ENCODING_8859_1:
                   1161:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1162:                 ctxt->sax->error(ctxt->userData,
                   1163:                  "char encoding ISO_8859_1 ISO Latin 1 not supported\n");
                   1164:             break;
                   1165:         case XML_CHAR_ENCODING_8859_2:
                   1166:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1167:                 ctxt->sax->error(ctxt->userData,
                   1168:                  "char encoding ISO_8859_2 ISO Latin 2 not supported\n");
                   1169:             break;
                   1170:         case XML_CHAR_ENCODING_8859_3:
                   1171:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1172:                 ctxt->sax->error(ctxt->userData,
                   1173:                  "char encoding ISO_8859_3 not supported\n");
                   1174:             break;
                   1175:         case XML_CHAR_ENCODING_8859_4:
                   1176:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1177:                 ctxt->sax->error(ctxt->userData,
                   1178:                  "char encoding ISO_8859_4 not supported\n");
                   1179:             break;
                   1180:         case XML_CHAR_ENCODING_8859_5:
                   1181:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1182:                 ctxt->sax->error(ctxt->userData,
                   1183:                  "char encoding ISO_8859_5 not supported\n");
                   1184:             break;
                   1185:         case XML_CHAR_ENCODING_8859_6:
                   1186:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1187:                 ctxt->sax->error(ctxt->userData,
                   1188:                  "char encoding ISO_8859_6 not supported\n");
                   1189:             break;
                   1190:         case XML_CHAR_ENCODING_8859_7:
                   1191:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1192:                 ctxt->sax->error(ctxt->userData,
                   1193:                  "char encoding ISO_8859_7 not supported\n");
                   1194:             break;
                   1195:         case XML_CHAR_ENCODING_8859_8:
                   1196:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1197:                 ctxt->sax->error(ctxt->userData,
                   1198:                  "char encoding ISO_8859_8 not supported\n");
                   1199:             break;
                   1200:         case XML_CHAR_ENCODING_8859_9:
                   1201:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1202:                 ctxt->sax->error(ctxt->userData,
                   1203:                  "char encoding ISO_8859_9 not supported\n");
                   1204:             break;
                   1205:         case XML_CHAR_ENCODING_2022_JP:
                   1206:             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1207:                 ctxt->sax->error(ctxt->userData,
                   1208:                   "char encoding ISO-2022-JPnot supported\n");
                   1209:             break;
                   1210:         case XML_CHAR_ENCODING_SHIFT_JIS:
                   1211:             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1212:                 ctxt->sax->error(ctxt->userData,
                   1213:                   "char encoding Shift_JISnot supported\n");
                   1214:             break;
                   1215:         case XML_CHAR_ENCODING_EUC_JP:
                   1216:             if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1217:                 ctxt->sax->error(ctxt->userData,
                   1218:                   "char encoding EUC-JPnot supported\n");
                   1219:             break;
                   1220:     }
                   1221: }
                   1222: 
1.31      daniel   1223: /************************************************************************
                   1224:  *                                                                     *
                   1225:  *             Commodity functions to handle streams                   *
                   1226:  *                                                                     *
                   1227:  ************************************************************************/
                   1228: 
                   1229: /**
                   1230:  * htmlFreeInputStream:
                   1231:  * @input:  an htmlParserInputPtr
                   1232:  *
                   1233:  * Free up an input stream.
                   1234:  */
                   1235: void
                   1236: htmlFreeInputStream(htmlParserInputPtr input) {
                   1237:     if (input == NULL) return;
                   1238: 
                   1239:     if (input->filename != NULL) xmlFree((char *) input->filename);
                   1240:     if (input->directory != NULL) xmlFree((char *) input->directory);
                   1241:     if ((input->free != NULL) && (input->base != NULL))
                   1242:         input->free((xmlChar *) input->base);
                   1243:     if (input->buf != NULL) 
                   1244:         xmlFreeParserInputBuffer(input->buf);
                   1245:     memset(input, -1, sizeof(htmlParserInput));
                   1246:     xmlFree(input);
                   1247: }
                   1248: 
                   1249: /**
                   1250:  * htmlNewInputStream:
                   1251:  * @ctxt:  an HTML parser context
                   1252:  *
                   1253:  * Create a new input stream structure
                   1254:  * Returns the new input stream or NULL
                   1255:  */
                   1256: htmlParserInputPtr
                   1257: htmlNewInputStream(htmlParserCtxtPtr ctxt) {
                   1258:     htmlParserInputPtr input;
                   1259: 
                   1260:     input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
                   1261:     if (input == NULL) {
                   1262:         ctxt->errNo = XML_ERR_NO_MEMORY;
                   1263:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1264:            ctxt->sax->error(ctxt->userData, 
                   1265:                             "malloc: couldn't allocate a new input stream\n");
                   1266:        ctxt->errNo = XML_ERR_NO_MEMORY;
                   1267:        return(NULL);
                   1268:     }
                   1269:     input->filename = NULL;
                   1270:     input->directory = NULL;
                   1271:     input->base = NULL;
                   1272:     input->cur = NULL;
                   1273:     input->buf = NULL;
                   1274:     input->line = 1;
                   1275:     input->col = 1;
                   1276:     input->buf = NULL;
                   1277:     input->free = NULL;
                   1278:     input->consumed = 0;
                   1279:     input->length = 0;
                   1280:     return(input);
                   1281: }
                   1282: 
1.1       daniel   1283: 
                   1284: /************************************************************************
                   1285:  *                                                                     *
                   1286:  *             Commodity functions, cleanup needed ?                   *
                   1287:  *                                                                     *
                   1288:  ************************************************************************/
                   1289: 
                   1290: /**
                   1291:  * areBlanks:
                   1292:  * @ctxt:  an HTML parser context
1.14      daniel   1293:  * @str:  a xmlChar *
1.1       daniel   1294:  * @len:  the size of @str
                   1295:  *
                   1296:  * Is this a sequence of blank chars that one can ignore ?
                   1297:  *
                   1298:  * Returns 1 if ignorable 0 otherwise.
                   1299:  */
                   1300: 
1.14      daniel   1301: static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
1.1       daniel   1302:     int i;
                   1303:     xmlNodePtr lastChild;
                   1304: 
                   1305:     for (i = 0;i < len;i++)
                   1306:         if (!(IS_BLANK(str[i]))) return(0);
                   1307: 
                   1308:     if (CUR != '<') return(0);
                   1309:     if (ctxt->node == NULL) return(0);
                   1310:     lastChild = xmlGetLastChild(ctxt->node);
                   1311:     if (lastChild == NULL) {
                   1312:         if (ctxt->node->content != NULL) return(0);
                   1313:     } else if (xmlNodeIsText(lastChild))
                   1314:         return(0);
                   1315:     return(1);
                   1316: }
                   1317: 
                   1318: /**
                   1319:  * htmlHandleEntity:
                   1320:  * @ctxt:  an HTML parser context
                   1321:  * @entity:  an XML entity pointer.
                   1322:  *
                   1323:  * Default handling of an HTML entity, call the parser with the
                   1324:  * substitution string
                   1325:  */
                   1326: 
                   1327: void
                   1328: htmlHandleEntity(htmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
                   1329:     int len;
                   1330: 
                   1331:     if (entity->content == NULL) {
                   1332:         if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1333:            ctxt->sax->error(ctxt->userData, "htmlHandleEntity %s: content == NULL\n",
                   1334:                       entity->name);
                   1335:        ctxt->wellFormed = 0;
                   1336:         return;
                   1337:     }
                   1338:     len = xmlStrlen(entity->content);
                   1339: 
                   1340:     /*
                   1341:      * Just handle the content as a set of chars.
                   1342:      */
                   1343:     if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   1344:        ctxt->sax->characters(ctxt->userData, entity->content, len);
                   1345: 
                   1346: }
                   1347: 
                   1348: /**
                   1349:  * htmlNewDoc:
                   1350:  * @URI:  URI for the dtd, or NULL
                   1351:  * @ExternalID:  the external ID of the DTD, or NULL
                   1352:  *
                   1353:  * Returns a new document
                   1354:  */
                   1355: htmlDocPtr
1.14      daniel   1356: htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
1.1       daniel   1357:     xmlDocPtr cur;
                   1358: 
                   1359:     /*
                   1360:      * Allocate a new document and fill the fields.
                   1361:      */
1.11      daniel   1362:     cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
1.1       daniel   1363:     if (cur == NULL) {
                   1364:         fprintf(stderr, "xmlNewDoc : malloc failed\n");
                   1365:        return(NULL);
                   1366:     }
1.10      daniel   1367:     memset(cur, 0, sizeof(xmlDoc));
1.1       daniel   1368: 
1.20      daniel   1369:     cur->type = XML_HTML_DOCUMENT_NODE;
1.1       daniel   1370:     cur->version = NULL;
                   1371:     cur->intSubset = NULL;
1.28      daniel   1372:     if ((ExternalID == NULL) &&
                   1373:        (URI == NULL))
                   1374:        xmlCreateIntSubset(cur, BAD_CAST "HTML",
                   1375:                    BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
                   1376:                    BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
                   1377:     else
                   1378:        xmlCreateIntSubset(cur, BAD_CAST "HTML", ExternalID, URI);
1.41      daniel   1379:     cur->doc = cur;
1.1       daniel   1380:     cur->name = NULL;
1.37      daniel   1381:     cur->children = NULL; 
1.1       daniel   1382:     cur->extSubset = NULL;
                   1383:     cur->oldNs = NULL;
                   1384:     cur->encoding = NULL;
                   1385:     cur->standalone = 1;
                   1386:     cur->compression = 0;
1.12      daniel   1387:     cur->ids = NULL;
                   1388:     cur->refs = NULL;
1.1       daniel   1389: #ifndef XML_WITHOUT_CORBA
                   1390:     cur->_private = NULL;
                   1391: #endif
                   1392:     return(cur);
                   1393: }
                   1394: 
                   1395: 
                   1396: /************************************************************************
                   1397:  *                                                                     *
                   1398:  *                     The parser itself                               *
                   1399:  *     Relates to http://www.w3.org/TR/html40                          *
                   1400:  *                                                                     *
                   1401:  ************************************************************************/
                   1402: 
                   1403: /************************************************************************
                   1404:  *                                                                     *
                   1405:  *                     The parser itself                               *
                   1406:  *                                                                     *
                   1407:  ************************************************************************/
                   1408: 
                   1409: /**
                   1410:  * htmlParseHTMLName:
                   1411:  * @ctxt:  an HTML parser context
                   1412:  *
1.26      daniel   1413:  * parse an HTML tag or attribute name, note that we convert it to lowercase
1.1       daniel   1414:  * since HTML names are not case-sensitive.
                   1415:  *
                   1416:  * Returns the Tag Name parsed or NULL
                   1417:  */
                   1418: 
1.14      daniel   1419: xmlChar *
1.1       daniel   1420: htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
1.14      daniel   1421:     xmlChar *ret = NULL;
1.1       daniel   1422:     int i = 0;
1.31      daniel   1423:     xmlChar loc[HTML_PARSER_BUFFER_SIZE];
1.1       daniel   1424: 
                   1425:     if (!IS_LETTER(CUR) && (CUR != '_') &&
                   1426:         (CUR != ':')) return(NULL);
                   1427: 
1.31      daniel   1428:     while ((i < HTML_PARSER_BUFFER_SIZE) &&
1.45      daniel   1429:            ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1430:           (CUR == ':') || (CUR == '_'))) {
1.26      daniel   1431:        if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
1.1       daniel   1432:         else loc[i] = CUR;
                   1433:        i++;
                   1434:        
                   1435:        NEXT;
                   1436:     }
                   1437:     
                   1438:     ret = xmlStrndup(loc, i);
                   1439: 
                   1440:     return(ret);
                   1441: }
                   1442: 
                   1443: /**
                   1444:  * htmlParseName:
                   1445:  * @ctxt:  an HTML parser context
                   1446:  *
                   1447:  * parse an HTML name, this routine is case sensistive.
                   1448:  *
                   1449:  * Returns the Name parsed or NULL
                   1450:  */
                   1451: 
1.14      daniel   1452: xmlChar *
1.1       daniel   1453: htmlParseName(htmlParserCtxtPtr ctxt) {
1.14      daniel   1454:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   1455:     int len = 0;
1.1       daniel   1456: 
1.5       daniel   1457:     GROW;
                   1458:     if (!IS_LETTER(CUR) && (CUR != '_')) {
                   1459:        return(NULL);
                   1460:     }
1.1       daniel   1461: 
                   1462:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1463:            (CUR == '.') || (CUR == '-') ||
                   1464:           (CUR == '_') || (CUR == ':') || 
                   1465:           (IS_COMBINING(CUR)) ||
1.5       daniel   1466:           (IS_EXTENDER(CUR))) {
                   1467:        buf[len++] = CUR;
1.1       daniel   1468:        NEXT;
1.5       daniel   1469:        if (len >= HTML_MAX_NAMELEN) {
                   1470:            fprintf(stderr, 
                   1471:               "htmlParseName: reached HTML_MAX_NAMELEN limit\n");
                   1472:            while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1473:                   (CUR == '.') || (CUR == '-') ||
                   1474:                   (CUR == '_') || (CUR == ':') || 
                   1475:                   (IS_COMBINING(CUR)) ||
                   1476:                   (IS_EXTENDER(CUR)))
                   1477:                 NEXT;
                   1478:            break;
                   1479:        }
                   1480:     }
                   1481:     return(xmlStrndup(buf, len));
1.1       daniel   1482: }
                   1483: 
                   1484: /**
                   1485:  * htmlParseHTMLAttribute:
                   1486:  * @ctxt:  an HTML parser context
1.19      daniel   1487:  * @stop:  a char stop value
1.1       daniel   1488:  * 
1.19      daniel   1489:  * parse an HTML attribute value till the stop (quote), if
                   1490:  * stop is 0 then it stops at the first space
1.1       daniel   1491:  *
1.19      daniel   1492:  * Returns the attribute parsed or NULL
1.1       daniel   1493:  */
                   1494: 
1.14      daniel   1495: xmlChar *
1.19      daniel   1496: htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
1.32      daniel   1497: #if 0
1.14      daniel   1498:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   1499:     int len = 0;
1.1       daniel   1500: 
1.5       daniel   1501:     GROW;
1.19      daniel   1502:     while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
                   1503:        if ((stop == 0) && (IS_BLANK(CUR))) break;
1.5       daniel   1504:        buf[len++] = CUR;
1.1       daniel   1505:        NEXT;
1.5       daniel   1506:        if (len >= HTML_MAX_NAMELEN) {
                   1507:            fprintf(stderr, 
                   1508:               "htmlParseHTMLAttribute: reached HTML_MAX_NAMELEN limit\n");
                   1509:            while ((!IS_BLANK(CUR)) && (CUR != '<') &&
1.19      daniel   1510:                   (CUR != '>') &&
1.5       daniel   1511:                   (CUR != '\'') && (CUR != '"'))
                   1512:                 NEXT;
                   1513:            break;
                   1514:        }
                   1515:     }
                   1516:     return(xmlStrndup(buf, len));
1.32      daniel   1517: #else    
                   1518:     xmlChar *buffer = NULL;
                   1519:     int buffer_size = 0;
                   1520:     xmlChar *out = NULL;
                   1521:     xmlChar *name = NULL;
                   1522: 
                   1523:     xmlChar *cur = NULL;
                   1524:     htmlEntityDescPtr ent;
                   1525: 
                   1526:     /*
                   1527:      * allocate a translation buffer.
                   1528:      */
                   1529:     buffer_size = HTML_PARSER_BIG_BUFFER_SIZE;
                   1530:     buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
                   1531:     if (buffer == NULL) {
                   1532:        perror("htmlParseHTMLAttribute: malloc failed");
                   1533:        return(NULL);
                   1534:     }
                   1535:     out = buffer;
                   1536: 
                   1537:     /*
                   1538:      * Ok loop until we reach one of the ending chars
                   1539:      */
                   1540:     while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
                   1541:        if ((stop == 0) && (IS_BLANK(CUR))) break;
                   1542:         if (CUR == '&') {
                   1543:            if (NXT(1) == '#') {
                   1544:                int val = htmlParseCharRef(ctxt);
                   1545:                *out++ = val;
                   1546:            } else {
                   1547:                ent = htmlParseEntityRef(ctxt, &name);
                   1548:                if (name == NULL) {
                   1549:                    *out++ = '&';
                   1550:                    if (out - buffer > buffer_size - 100) {
                   1551:                        int index = out - buffer;
                   1552: 
                   1553:                        growBuffer(buffer);
                   1554:                        out = &buffer[index];
                   1555:                    }
                   1556:                } else if ((ent == NULL) || (ent->value <= 0) ||
                   1557:                           (ent->value >= 255)) {
                   1558:                    *out++ = '&';
                   1559:                    cur = name;
                   1560:                    while (*cur != 0) {
                   1561:                        if (out - buffer > buffer_size - 100) {
                   1562:                            int index = out - buffer;
                   1563: 
                   1564:                            growBuffer(buffer);
                   1565:                            out = &buffer[index];
                   1566:                        }
                   1567:                        *out++ = *cur++;
                   1568:                    }
                   1569:                    xmlFree(name);
                   1570:                } else {
                   1571:                    *out++ = ent->value;
                   1572:                    if (out - buffer > buffer_size - 100) {
                   1573:                        int index = out - buffer;
                   1574: 
                   1575:                        growBuffer(buffer);
                   1576:                        out = &buffer[index];
                   1577:                    }
                   1578:                    xmlFree(name);
                   1579:                }
                   1580:            }
                   1581:        } else {
                   1582:            *out++ = CUR;
                   1583:            if (out - buffer > buffer_size - 100) {
                   1584:              int index = out - buffer;
                   1585:              
                   1586:              growBuffer(buffer);
                   1587:              out = &buffer[index];
                   1588:            }
                   1589:            NEXT;
                   1590:        }
                   1591:     }
                   1592:     *out++ = 0;
                   1593:     return(buffer);
                   1594: #endif
1.1       daniel   1595: }
                   1596: 
                   1597: /**
                   1598:  * htmlParseNmtoken:
                   1599:  * @ctxt:  an HTML parser context
                   1600:  * 
                   1601:  * parse an HTML Nmtoken.
                   1602:  *
                   1603:  * Returns the Nmtoken parsed or NULL
                   1604:  */
                   1605: 
1.14      daniel   1606: xmlChar *
1.1       daniel   1607: htmlParseNmtoken(htmlParserCtxtPtr ctxt) {
1.14      daniel   1608:     xmlChar buf[HTML_MAX_NAMELEN];
1.5       daniel   1609:     int len = 0;
1.1       daniel   1610: 
1.5       daniel   1611:     GROW;
1.1       daniel   1612:     while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1613:            (CUR == '.') || (CUR == '-') ||
                   1614:           (CUR == '_') || (CUR == ':') || 
                   1615:           (IS_COMBINING(CUR)) ||
1.5       daniel   1616:           (IS_EXTENDER(CUR))) {
                   1617:        buf[len++] = CUR;
1.1       daniel   1618:        NEXT;
1.5       daniel   1619:        if (len >= HTML_MAX_NAMELEN) {
                   1620:            fprintf(stderr, 
                   1621:               "htmlParseNmtoken: reached HTML_MAX_NAMELEN limit\n");
                   1622:            while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
                   1623:                   (CUR == '.') || (CUR == '-') ||
                   1624:                   (CUR == '_') || (CUR == ':') || 
                   1625:                   (IS_COMBINING(CUR)) ||
                   1626:                   (IS_EXTENDER(CUR)))
                   1627:                 NEXT;
                   1628:            break;
                   1629:        }
                   1630:     }
                   1631:     return(xmlStrndup(buf, len));
1.1       daniel   1632: }
                   1633: 
                   1634: /**
                   1635:  * htmlParseEntityRef:
                   1636:  * @ctxt:  an HTML parser context
                   1637:  * @str:  location to store the entity name
                   1638:  *
                   1639:  * parse an HTML ENTITY references
                   1640:  *
                   1641:  * [68] EntityRef ::= '&' Name ';'
                   1642:  *
                   1643:  * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
                   1644:  *         if non-NULL *str will have to be freed by the caller.
                   1645:  */
                   1646: htmlEntityDescPtr
1.14      daniel   1647: htmlParseEntityRef(htmlParserCtxtPtr ctxt, xmlChar **str) {
                   1648:     xmlChar *name;
1.1       daniel   1649:     htmlEntityDescPtr ent = NULL;
                   1650:     *str = NULL;
                   1651: 
                   1652:     if (CUR == '&') {
                   1653:         NEXT;
                   1654:         name = htmlParseName(ctxt);
                   1655:        if (name == NULL) {
                   1656:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1657:                ctxt->sax->error(ctxt->userData, "htmlParseEntityRef: no name\n");
                   1658:            ctxt->wellFormed = 0;
                   1659:        } else {
1.5       daniel   1660:            GROW;
1.1       daniel   1661:            if (CUR == ';') {
                   1662:                *str = name;
                   1663: 
                   1664:                /*
                   1665:                 * Lookup the entity in the table.
                   1666:                 */
                   1667:                ent = htmlEntityLookup(name);
1.32      daniel   1668:                if (ent != NULL) /* OK that's ugly !!! */
                   1669:                    NEXT;
1.1       daniel   1670:            } else {
                   1671:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1672:                    ctxt->sax->error(ctxt->userData,
                   1673:                                     "htmlParseEntityRef: expecting ';'\n");
1.32      daniel   1674:                *str = name;
1.1       daniel   1675:            }
                   1676:        }
                   1677:     }
                   1678:     return(ent);
                   1679: }
                   1680: 
                   1681: /**
                   1682:  * htmlParseAttValue:
                   1683:  * @ctxt:  an HTML parser context
                   1684:  *
                   1685:  * parse a value for an attribute
                   1686:  * Note: the parser won't do substitution of entities here, this
                   1687:  * will be handled later in xmlStringGetNodeList, unless it was
                   1688:  * asked for ctxt->replaceEntities != 0 
                   1689:  *
                   1690:  * Returns the AttValue parsed or NULL.
                   1691:  */
                   1692: 
1.14      daniel   1693: xmlChar *
1.1       daniel   1694: htmlParseAttValue(htmlParserCtxtPtr ctxt) {
1.14      daniel   1695:     xmlChar *ret = NULL;
1.1       daniel   1696: 
                   1697:     if (CUR == '"') {
                   1698:         NEXT;
1.19      daniel   1699:        ret = htmlParseHTMLAttribute(ctxt, '"');
1.1       daniel   1700:         if (CUR != '"') {
                   1701:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1702:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   1703:            ctxt->wellFormed = 0;
                   1704:        } else
                   1705:            NEXT;
                   1706:     } else if (CUR == '\'') {
                   1707:         NEXT;
1.19      daniel   1708:        ret = htmlParseHTMLAttribute(ctxt, '\'');
1.1       daniel   1709:         if (CUR != '\'') {
                   1710:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1711:                ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
                   1712:            ctxt->wellFormed = 0;
                   1713:        } else
                   1714:            NEXT;
                   1715:     } else {
                   1716:         /*
                   1717:         * That's an HTMLism, the attribute value may not be quoted
                   1718:         */
1.19      daniel   1719:        ret = htmlParseHTMLAttribute(ctxt, 0);
1.1       daniel   1720:        if (ret == NULL) {
                   1721:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1722:                ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
                   1723:            ctxt->wellFormed = 0;
                   1724:        }
                   1725:     }
                   1726:     return(ret);
                   1727: }
                   1728: 
                   1729: /**
                   1730:  * htmlParseSystemLiteral:
                   1731:  * @ctxt:  an HTML parser context
                   1732:  * 
                   1733:  * parse an HTML Literal
                   1734:  *
                   1735:  * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
                   1736:  *
                   1737:  * Returns the SystemLiteral parsed or NULL
                   1738:  */
                   1739: 
1.14      daniel   1740: xmlChar *
1.1       daniel   1741: htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
1.14      daniel   1742:     const xmlChar *q;
                   1743:     xmlChar *ret = NULL;
1.1       daniel   1744: 
                   1745:     if (CUR == '"') {
                   1746:         NEXT;
                   1747:        q = CUR_PTR;
                   1748:        while ((IS_CHAR(CUR)) && (CUR != '"'))
                   1749:            NEXT;
                   1750:        if (!IS_CHAR(CUR)) {
                   1751:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1752:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   1753:            ctxt->wellFormed = 0;
                   1754:        } else {
                   1755:            ret = xmlStrndup(q, CUR_PTR - q);
                   1756:            NEXT;
                   1757:         }
                   1758:     } else if (CUR == '\'') {
                   1759:         NEXT;
                   1760:        q = CUR_PTR;
                   1761:        while ((IS_CHAR(CUR)) && (CUR != '\''))
                   1762:            NEXT;
                   1763:        if (!IS_CHAR(CUR)) {
                   1764:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1765:                ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
                   1766:            ctxt->wellFormed = 0;
                   1767:        } else {
                   1768:            ret = xmlStrndup(q, CUR_PTR - q);
                   1769:            NEXT;
                   1770:         }
                   1771:     } else {
                   1772:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.38      daniel   1773:            ctxt->sax->error(ctxt->userData,
                   1774:                             "SystemLiteral \" or ' expected\n");
1.1       daniel   1775:        ctxt->wellFormed = 0;
                   1776:     }
                   1777:     
                   1778:     return(ret);
                   1779: }
                   1780: 
                   1781: /**
                   1782:  * htmlParsePubidLiteral:
                   1783:  * @ctxt:  an HTML parser context
                   1784:  *
                   1785:  * parse an HTML public literal
                   1786:  *
                   1787:  * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
                   1788:  *
                   1789:  * Returns the PubidLiteral parsed or NULL.
                   1790:  */
                   1791: 
1.14      daniel   1792: xmlChar *
1.1       daniel   1793: htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
1.14      daniel   1794:     const xmlChar *q;
                   1795:     xmlChar *ret = NULL;
1.1       daniel   1796:     /*
                   1797:      * Name ::= (Letter | '_') (NameChar)*
                   1798:      */
                   1799:     if (CUR == '"') {
                   1800:         NEXT;
                   1801:        q = CUR_PTR;
                   1802:        while (IS_PUBIDCHAR(CUR)) NEXT;
                   1803:        if (CUR != '"') {
                   1804:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1805:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   1806:            ctxt->wellFormed = 0;
                   1807:        } else {
                   1808:            ret = xmlStrndup(q, CUR_PTR - q);
                   1809:            NEXT;
                   1810:        }
                   1811:     } else if (CUR == '\'') {
                   1812:         NEXT;
                   1813:        q = CUR_PTR;
                   1814:        while ((IS_LETTER(CUR)) && (CUR != '\''))
                   1815:            NEXT;
                   1816:        if (!IS_LETTER(CUR)) {
                   1817:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1818:                ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
                   1819:            ctxt->wellFormed = 0;
                   1820:        } else {
                   1821:            ret = xmlStrndup(q, CUR_PTR - q);
                   1822:            NEXT;
                   1823:        }
                   1824:     } else {
                   1825:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1826:            ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
                   1827:        ctxt->wellFormed = 0;
                   1828:     }
                   1829:     
                   1830:     return(ret);
                   1831: }
                   1832: 
                   1833: /**
                   1834:  * htmlParseCharData:
                   1835:  * @ctxt:  an HTML parser context
                   1836:  * @cdata:  int indicating whether we are within a CDATA section
                   1837:  *
                   1838:  * parse a CharData section.
                   1839:  * if we are within a CDATA section ']]>' marks an end of section.
                   1840:  *
                   1841:  * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
                   1842:  */
                   1843: 
                   1844: void
                   1845: htmlParseCharData(htmlParserCtxtPtr ctxt, int cdata) {
1.25      daniel   1846:     xmlChar *buf = NULL;
                   1847:     int len = 0;
1.31      daniel   1848:     int size = HTML_PARSER_BUFFER_SIZE;
1.25      daniel   1849:     xmlChar q;
                   1850: 
                   1851:     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
                   1852:     if (buf == NULL) {
                   1853:        fprintf(stderr, "malloc of %d byte failed\n", size);
                   1854:        return;
                   1855:     }
1.1       daniel   1856: 
1.25      daniel   1857:     q = CUR;
                   1858:     while ((IS_CHAR(q)) && (q != '<') &&
                   1859:            (q != '&')) {
                   1860:        if ((q == ']') && (NXT(1) == ']') &&
1.1       daniel   1861:            (NXT(2) == '>')) {
                   1862:            if (cdata) break;
                   1863:            else {
                   1864:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1865:                    ctxt->sax->error(ctxt->userData,
                   1866:                       "Sequence ']]>' not allowed in content\n");
                   1867:                ctxt->wellFormed = 0;
                   1868:            }
                   1869:        }
1.25      daniel   1870:        if (len + 1 >= size) {
                   1871:            size *= 2;
                   1872:            buf = xmlRealloc(buf, size * sizeof(xmlChar));
                   1873:            if (buf == NULL) {
                   1874:                fprintf(stderr, "realloc of %d byte failed\n", size);
                   1875:                return;
                   1876:            }
                   1877:        }
                   1878:        buf[len++] = q;
1.1       daniel   1879:         NEXT;
1.25      daniel   1880:        q = CUR;
                   1881:     }
                   1882:     if (len == 0) {
                   1883:        xmlFree(buf);
                   1884:        return;
1.1       daniel   1885:     }
                   1886: 
                   1887:     /*
1.25      daniel   1888:      * Ok the buffer is to be consumed as chars.
1.1       daniel   1889:      */
                   1890:     if (ctxt->sax != NULL) {
1.25      daniel   1891:        if (areBlanks(ctxt, buf, len)) {
1.1       daniel   1892:            if (ctxt->sax->ignorableWhitespace != NULL)
1.25      daniel   1893:                ctxt->sax->ignorableWhitespace(ctxt->userData, buf, len);
1.1       daniel   1894:        } else {
                   1895:            if (ctxt->sax->characters != NULL)
1.25      daniel   1896:                ctxt->sax->characters(ctxt->userData, buf, len);
1.1       daniel   1897:         }
                   1898:     }
1.25      daniel   1899:     xmlFree(buf);
1.1       daniel   1900: }
                   1901: 
                   1902: /**
                   1903:  * htmlParseExternalID:
                   1904:  * @ctxt:  an HTML parser context
1.14      daniel   1905:  * @publicID:  a xmlChar** receiving PubidLiteral
1.1       daniel   1906:  * @strict: indicate whether we should restrict parsing to only
                   1907:  *          production [75], see NOTE below
                   1908:  *
                   1909:  * Parse an External ID or a Public ID
                   1910:  *
                   1911:  * NOTE: Productions [75] and [83] interract badly since [75] can generate
                   1912:  *       'PUBLIC' S PubidLiteral S SystemLiteral
                   1913:  *
                   1914:  * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
                   1915:  *                   | 'PUBLIC' S PubidLiteral S SystemLiteral
                   1916:  *
                   1917:  * [83] PublicID ::= 'PUBLIC' S PubidLiteral
                   1918:  *
                   1919:  * Returns the function returns SystemLiteral and in the second
                   1920:  *                case publicID receives PubidLiteral, is strict is off
                   1921:  *                it is possible to return NULL and have publicID set.
                   1922:  */
                   1923: 
1.14      daniel   1924: xmlChar *
                   1925: htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
                   1926:     xmlChar *URI = NULL;
1.1       daniel   1927: 
                   1928:     if ((UPPER == 'S') && (UPP(1) == 'Y') &&
                   1929:          (UPP(2) == 'S') && (UPP(3) == 'T') &&
                   1930:         (UPP(4) == 'E') && (UPP(5) == 'M')) {
                   1931:         SKIP(6);
                   1932:        if (!IS_BLANK(CUR)) {
                   1933:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1934:                ctxt->sax->error(ctxt->userData,
                   1935:                    "Space required after 'SYSTEM'\n");
                   1936:            ctxt->wellFormed = 0;
                   1937:        }
                   1938:         SKIP_BLANKS;
                   1939:        URI = htmlParseSystemLiteral(ctxt);
                   1940:        if (URI == NULL) {
                   1941:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1942:                ctxt->sax->error(ctxt->userData,
                   1943:                  "htmlParseExternalID: SYSTEM, no URI\n");
                   1944:            ctxt->wellFormed = 0;
                   1945:         }
                   1946:     } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
                   1947:               (UPP(2) == 'B') && (UPP(3) == 'L') &&
                   1948:               (UPP(4) == 'I') && (UPP(5) == 'C')) {
                   1949:         SKIP(6);
                   1950:        if (!IS_BLANK(CUR)) {
                   1951:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1952:                ctxt->sax->error(ctxt->userData,
                   1953:                    "Space required after 'PUBLIC'\n");
                   1954:            ctxt->wellFormed = 0;
                   1955:        }
                   1956:         SKIP_BLANKS;
                   1957:        *publicID = htmlParsePubidLiteral(ctxt);
                   1958:        if (*publicID == NULL) {
                   1959:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   1960:                ctxt->sax->error(ctxt->userData, 
                   1961:                  "htmlParseExternalID: PUBLIC, no Public Identifier\n");
                   1962:            ctxt->wellFormed = 0;
                   1963:        }
1.5       daniel   1964:         SKIP_BLANKS;
                   1965:         if ((CUR == '"') || (CUR == '\'')) {
                   1966:            URI = htmlParseSystemLiteral(ctxt);
1.1       daniel   1967:        }
                   1968:     }
                   1969:     return(URI);
                   1970: }
                   1971: 
                   1972: /**
                   1973:  * htmlParseComment:
                   1974:  * @ctxt:  an HTML parser context
                   1975:  *
                   1976:  * Parse an XML (SGML) comment <!-- .... -->
                   1977:  *
                   1978:  * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
                   1979:  */
                   1980: void
1.31      daniel   1981: htmlParseComment(htmlParserCtxtPtr ctxt) {
1.25      daniel   1982:     xmlChar *buf = NULL;
                   1983:     int len = 0;
1.31      daniel   1984:     int size = HTML_PARSER_BUFFER_SIZE;
1.25      daniel   1985:     register xmlChar s, r, q;
1.1       daniel   1986: 
                   1987:     /*
                   1988:      * Check that there is a comment right here.
                   1989:      */
                   1990:     if ((CUR != '<') || (NXT(1) != '!') ||
                   1991:         (NXT(2) != '-') || (NXT(3) != '-')) return;
                   1992: 
1.25      daniel   1993:     buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
                   1994:     if (buf == NULL) {
                   1995:        fprintf(stderr, "malloc of %d byte failed\n", size);
                   1996:        return;
                   1997:     }
                   1998:     q = r = '-'; /* 0 or '-' to cover our ass against <!--> and <!---> ? !!! */
1.1       daniel   1999:     SKIP(4);
1.25      daniel   2000:     s = CUR;
                   2001:     
                   2002:     while (IS_CHAR(s) &&
                   2003:            ((s != '>') || (r != '-') || (q != '-'))) {
                   2004:        if (len + 1 >= size) {
                   2005:            size *= 2;
                   2006:            buf = xmlRealloc(buf, size * sizeof(xmlChar));
                   2007:            if (buf == NULL) {
                   2008:                fprintf(stderr, "realloc of %d byte failed\n", size);
                   2009:                return;
                   2010:            }
                   2011:        }
                   2012:        buf[len++] = s;
                   2013:         NEXT;
                   2014:        q = r;
                   2015:        r = s;
                   2016:        s = CUR;
1.1       daniel   2017:     }
1.25      daniel   2018:     buf[len - 2] = 0;
                   2019:     if (!IS_CHAR(s)) {
1.1       daniel   2020:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.25      daniel   2021:            ctxt->sax->error(ctxt->userData, "Comment not terminated \n<!--%.50s\n", buf);
1.1       daniel   2022:        ctxt->wellFormed = 0;
                   2023:     } else {
                   2024:         NEXT;
1.31      daniel   2025:        if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL)) {
                   2026:            ctxt->sax->comment(ctxt->userData, buf);
1.1       daniel   2027:        }
                   2028:     }
1.25      daniel   2029:     xmlFree(buf);
1.1       daniel   2030: }
                   2031: 
                   2032: /**
                   2033:  * htmlParseCharRef:
                   2034:  * @ctxt:  an HTML parser context
                   2035:  *
                   2036:  * parse Reference declarations
                   2037:  *
                   2038:  * [66] CharRef ::= '&#' [0-9]+ ';' |
                   2039:  *                  '&#x' [0-9a-fA-F]+ ';'
                   2040:  *
                   2041:  * Returns the value parsed (as an int)
                   2042:  */
                   2043: int
                   2044: htmlParseCharRef(htmlParserCtxtPtr ctxt) {
                   2045:     int val = 0;
                   2046: 
                   2047:     if ((CUR == '&') && (NXT(1) == '#') &&
                   2048:         (NXT(2) == 'x')) {
                   2049:        SKIP(3);
                   2050:        while (CUR != ';') {
                   2051:            if ((CUR >= '0') && (CUR <= '9')) 
                   2052:                val = val * 16 + (CUR - '0');
                   2053:            else if ((CUR >= 'a') && (CUR <= 'f'))
                   2054:                val = val * 16 + (CUR - 'a') + 10;
                   2055:            else if ((CUR >= 'A') && (CUR <= 'F'))
                   2056:                val = val * 16 + (CUR - 'A') + 10;
                   2057:            else {
                   2058:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2059:                    ctxt->sax->error(ctxt->userData, 
                   2060:                         "htmlParseCharRef: invalid hexadecimal value\n");
                   2061:                ctxt->wellFormed = 0;
                   2062:                val = 0;
                   2063:                break;
                   2064:            }
                   2065:            NEXT;
                   2066:        }
                   2067:        if (CUR == ';')
                   2068:            NEXT;
                   2069:     } else if  ((CUR == '&') && (NXT(1) == '#')) {
                   2070:        SKIP(2);
                   2071:        while (CUR != ';') {
                   2072:            if ((CUR >= '0') && (CUR <= '9')) 
                   2073:                val = val * 10 + (CUR - '0');
                   2074:            else {
                   2075:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2076:                    ctxt->sax->error(ctxt->userData, 
                   2077:                         "htmlParseCharRef: invalid decimal value\n");
                   2078:                ctxt->wellFormed = 0;
                   2079:                val = 0;
                   2080:                break;
                   2081:            }
                   2082:            NEXT;
                   2083:        }
                   2084:        if (CUR == ';')
                   2085:            NEXT;
                   2086:     } else {
                   2087:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2088:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid value\n");
                   2089:        ctxt->wellFormed = 0;
                   2090:     }
                   2091:     /*
                   2092:      * Check the value IS_CHAR ...
                   2093:      */
                   2094:     if (IS_CHAR(val)) {
                   2095:         return(val);
                   2096:     } else {
                   2097:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.14      daniel   2098:            ctxt->sax->error(ctxt->userData, "htmlParseCharRef: invalid xmlChar value %d\n",
1.1       daniel   2099:                             val);
                   2100:        ctxt->wellFormed = 0;
                   2101:     }
                   2102:     return(0);
                   2103: }
                   2104: 
                   2105: 
                   2106: /**
                   2107:  * htmlParseDocTypeDecl :
                   2108:  * @ctxt:  an HTML parser context
                   2109:  *
                   2110:  * parse a DOCTYPE declaration
                   2111:  *
                   2112:  * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? 
                   2113:  *                      ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
                   2114:  */
                   2115: 
                   2116: void
                   2117: htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
1.14      daniel   2118:     xmlChar *name;
                   2119:     xmlChar *ExternalID = NULL;
                   2120:     xmlChar *URI = NULL;
1.1       daniel   2121: 
                   2122:     /*
                   2123:      * We know that '<!DOCTYPE' has been detected.
                   2124:      */
                   2125:     SKIP(9);
                   2126: 
                   2127:     SKIP_BLANKS;
                   2128: 
                   2129:     /*
                   2130:      * Parse the DOCTYPE name.
                   2131:      */
                   2132:     name = htmlParseName(ctxt);
                   2133:     if (name == NULL) {
                   2134:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2135:            ctxt->sax->error(ctxt->userData, "htmlParseDocTypeDecl : no DOCTYPE name !\n");
                   2136:        ctxt->wellFormed = 0;
                   2137:     }
                   2138:     /*
                   2139:      * Check that upper(name) == "HTML" !!!!!!!!!!!!!
                   2140:      */
                   2141: 
                   2142:     SKIP_BLANKS;
                   2143: 
                   2144:     /*
                   2145:      * Check for SystemID and ExternalID
                   2146:      */
1.5       daniel   2147:     URI = htmlParseExternalID(ctxt, &ExternalID, 0);
1.1       daniel   2148:     SKIP_BLANKS;
                   2149: 
                   2150:     /*
                   2151:      * We should be at the end of the DOCTYPE declaration.
                   2152:      */
                   2153:     if (CUR != '>') {
                   2154:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2155:            ctxt->sax->error(ctxt->userData, "DOCTYPE unproperly terminated\n");
                   2156:        ctxt->wellFormed = 0;
                   2157:         /* We shouldn't try to resynchronize ... */
                   2158:     }
                   2159:     NEXT;
                   2160: 
                   2161:     /*
1.46    ! daniel   2162:      * Create or update the document accordingly to the DOCTYPE
1.1       daniel   2163:      */
1.46    ! daniel   2164:     if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
        !          2165:        (!ctxt->disableSAX))
        !          2166:        ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
1.1       daniel   2167: 
                   2168:     /*
                   2169:      * Cleanup, since we don't use all those identifiers
                   2170:      */
1.11      daniel   2171:     if (URI != NULL) xmlFree(URI);
                   2172:     if (ExternalID != NULL) xmlFree(ExternalID);
                   2173:     if (name != NULL) xmlFree(name);
1.1       daniel   2174: }
                   2175: 
                   2176: /**
                   2177:  * htmlParseAttribute:
                   2178:  * @ctxt:  an HTML parser context
1.14      daniel   2179:  * @value:  a xmlChar ** used to store the value of the attribute
1.1       daniel   2180:  *
                   2181:  * parse an attribute
                   2182:  *
                   2183:  * [41] Attribute ::= Name Eq AttValue
                   2184:  *
                   2185:  * [25] Eq ::= S? '=' S?
                   2186:  *
                   2187:  * With namespace:
                   2188:  *
                   2189:  * [NS 11] Attribute ::= QName Eq AttValue
                   2190:  *
                   2191:  * Also the case QName == xmlns:??? is handled independently as a namespace
                   2192:  * definition.
                   2193:  *
                   2194:  * Returns the attribute name, and the value in *value.
                   2195:  */
                   2196: 
1.14      daniel   2197: xmlChar *
                   2198: htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
1.31      daniel   2199:     xmlChar *name, *val = NULL;
1.1       daniel   2200: 
                   2201:     *value = NULL;
                   2202:     name = htmlParseName(ctxt);
                   2203:     if (name == NULL) {
                   2204:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2205:            ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
                   2206:        ctxt->wellFormed = 0;
                   2207:         return(NULL);
                   2208:     }
                   2209: 
                   2210:     /*
                   2211:      * read the value
                   2212:      */
                   2213:     SKIP_BLANKS;
                   2214:     if (CUR == '=') {
                   2215:         NEXT;
                   2216:        SKIP_BLANKS;
                   2217:        val = htmlParseAttValue(ctxt);
1.42      daniel   2218:        /******
1.1       daniel   2219:     } else {
1.42      daniel   2220:         * TODO : some attribute must have values, some may not
1.1       daniel   2221:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.31      daniel   2222:            ctxt->sax->warning(ctxt->userData,
1.42      daniel   2223:               "No value for attribute %s\n", name); */
1.1       daniel   2224:     }
                   2225: 
                   2226:     *value = val;
                   2227:     return(name);
                   2228: }
                   2229: 
                   2230: /**
                   2231:  * htmlParseStartTag:
                   2232:  * @ctxt:  an HTML parser context
                   2233:  * 
                   2234:  * parse a start of tag either for rule element or
                   2235:  * EmptyElement. In both case we don't parse the tag closing chars.
                   2236:  *
                   2237:  * [40] STag ::= '<' Name (S Attribute)* S? '>'
                   2238:  *
                   2239:  * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
                   2240:  *
                   2241:  * With namespace:
                   2242:  *
                   2243:  * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
                   2244:  *
                   2245:  * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
                   2246:  *
                   2247:  */
                   2248: 
1.18      daniel   2249: void
1.1       daniel   2250: htmlParseStartTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   2251:     xmlChar *name;
                   2252:     xmlChar *attname;
                   2253:     xmlChar *attvalue;
                   2254:     const xmlChar **atts = NULL;
1.1       daniel   2255:     int nbatts = 0;
                   2256:     int maxatts = 0;
                   2257:     int i;
                   2258: 
1.18      daniel   2259:     if (CUR != '<') return;
1.1       daniel   2260:     NEXT;
                   2261: 
1.19      daniel   2262:     GROW;
1.1       daniel   2263:     name = htmlParseHTMLName(ctxt);
                   2264:     if (name == NULL) {
                   2265:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2266:            ctxt->sax->error(ctxt->userData, 
                   2267:             "htmlParseStartTag: invalid element name\n");
                   2268:        ctxt->wellFormed = 0;
1.18      daniel   2269:         return;
1.1       daniel   2270:     }
                   2271: 
                   2272:     /*
                   2273:      * Check for auto-closure of HTML elements.
                   2274:      */
                   2275:     htmlAutoClose(ctxt, name);
1.43      daniel   2276: 
                   2277:     /*
                   2278:      * Check for implied HTML elements.
                   2279:      */
                   2280:     htmlCheckImplied(ctxt, name);
1.1       daniel   2281: 
                   2282:     /*
                   2283:      * Now parse the attributes, it ends up with the ending
                   2284:      *
                   2285:      * (S Attribute)* S?
                   2286:      */
                   2287:     SKIP_BLANKS;
                   2288:     while ((IS_CHAR(CUR)) &&
                   2289:            (CUR != '>') && 
                   2290:           ((CUR != '/') || (NXT(1) != '>'))) {
1.26      daniel   2291:        long cons = ctxt->nbChars;
1.1       daniel   2292: 
1.19      daniel   2293:        GROW;
1.1       daniel   2294:        attname = htmlParseAttribute(ctxt, &attvalue);
1.31      daniel   2295:         if (attname != NULL) {
1.1       daniel   2296:            /*
                   2297:             * Well formedness requires at most one declaration of an attribute
                   2298:             */
                   2299:            for (i = 0; i < nbatts;i += 2) {
                   2300:                if (!xmlStrcmp(atts[i], attname)) {
                   2301:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.19      daniel   2302:                        ctxt->sax->error(ctxt->userData,
                   2303:                                         "Attribute %s redefined\n",
                   2304:                                         attname);
1.1       daniel   2305:                    ctxt->wellFormed = 0;
1.11      daniel   2306:                    xmlFree(attname);
1.31      daniel   2307:                    if (attvalue != NULL)
                   2308:                        xmlFree(attvalue);
1.19      daniel   2309:                    goto failed;
1.1       daniel   2310:                }
                   2311:            }
                   2312: 
                   2313:            /*
                   2314:             * Add the pair to atts
                   2315:             */
                   2316:            if (atts == NULL) {
                   2317:                maxatts = 10;
1.14      daniel   2318:                atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
1.1       daniel   2319:                if (atts == NULL) {
                   2320:                    fprintf(stderr, "malloc of %ld byte failed\n",
1.14      daniel   2321:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2322:                    if (name != NULL) xmlFree(name);
                   2323:                    return;
1.1       daniel   2324:                }
1.23      daniel   2325:            } else if (nbatts + 4 > maxatts) {
1.1       daniel   2326:                maxatts *= 2;
1.14      daniel   2327:                atts = (const xmlChar **) xmlRealloc(atts, maxatts * sizeof(xmlChar *));
1.1       daniel   2328:                if (atts == NULL) {
                   2329:                    fprintf(stderr, "realloc of %ld byte failed\n",
1.14      daniel   2330:                            maxatts * (long)sizeof(xmlChar *));
1.18      daniel   2331:                    if (name != NULL) xmlFree(name);
                   2332:                    return;
1.1       daniel   2333:                }
                   2334:            }
                   2335:            atts[nbatts++] = attname;
                   2336:            atts[nbatts++] = attvalue;
                   2337:            atts[nbatts] = NULL;
                   2338:            atts[nbatts + 1] = NULL;
                   2339:        }
                   2340: 
1.19      daniel   2341: failed:
1.1       daniel   2342:        SKIP_BLANKS;
1.26      daniel   2343:         if (cons == ctxt->nbChars) {
1.1       daniel   2344:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2345:                ctxt->sax->error(ctxt->userData, 
                   2346:                 "htmlParseStartTag: problem parsing attributes\n");
                   2347:            ctxt->wellFormed = 0;
                   2348:            break;
                   2349:        }
                   2350:     }
                   2351: 
                   2352:     /*
                   2353:      * SAX: Start of Element !
                   2354:      */
1.15      daniel   2355:     htmlnamePush(ctxt, xmlStrdup(name));
1.18      daniel   2356: #ifdef DEBUG
                   2357:     fprintf(stderr,"Start of element %s: pushed %s\n", name, ctxt->name);
                   2358: #endif    
1.1       daniel   2359:     if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
                   2360:         ctxt->sax->startElement(ctxt->userData, name, atts);
                   2361: 
                   2362:     if (atts != NULL) {
1.31      daniel   2363:         for (i = 0;i < nbatts;i++) {
                   2364:            if (atts[i] != NULL)
                   2365:                xmlFree((xmlChar *) atts[i]);
                   2366:        }
1.45      daniel   2367:        xmlFree((void *) atts);
1.1       daniel   2368:     }
1.18      daniel   2369:     if (name != NULL) xmlFree(name);
1.1       daniel   2370: }
                   2371: 
                   2372: /**
                   2373:  * htmlParseEndTag:
                   2374:  * @ctxt:  an HTML parser context
                   2375:  *
                   2376:  * parse an end of tag
                   2377:  *
                   2378:  * [42] ETag ::= '</' Name S? '>'
                   2379:  *
                   2380:  * With namespace
                   2381:  *
                   2382:  * [NS 9] ETag ::= '</' QName S? '>'
                   2383:  */
                   2384: 
                   2385: void
1.18      daniel   2386: htmlParseEndTag(htmlParserCtxtPtr ctxt) {
1.14      daniel   2387:     xmlChar *name;
1.15      daniel   2388:     xmlChar *oldname;
1.1       daniel   2389:     int i;
                   2390: 
                   2391:     if ((CUR != '<') || (NXT(1) != '/')) {
                   2392:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2393:            ctxt->sax->error(ctxt->userData, "htmlParseEndTag: '</' not found\n");
                   2394:        ctxt->wellFormed = 0;
                   2395:        return;
                   2396:     }
                   2397:     SKIP(2);
                   2398: 
                   2399:     name = htmlParseHTMLName(ctxt);
1.24      daniel   2400:     if (name == NULL) return;
1.1       daniel   2401: 
                   2402:     /*
                   2403:      * We should definitely be at the ending "S? '>'" part
                   2404:      */
                   2405:     SKIP_BLANKS;
                   2406:     if ((!IS_CHAR(CUR)) || (CUR != '>')) {
                   2407:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2408:            ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
                   2409:        ctxt->wellFormed = 0;
                   2410:     } else
                   2411:        NEXT;
                   2412: 
                   2413:     /*
1.18      daniel   2414:      * If the name read is not one of the element in the parsing stack
                   2415:      * then return, it's just an error.
1.1       daniel   2416:      */
1.18      daniel   2417:     for (i = (ctxt->nameNr - 1);i >= 0;i--) {
                   2418:         if (!xmlStrcmp(name, ctxt->nameTab[i])) break;
1.1       daniel   2419:     }
                   2420:     if (i < 0) {
                   2421:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1.18      daniel   2422:            ctxt->sax->error(ctxt->userData,
                   2423:             "Unexpected end tag : %s\n", name);
1.11      daniel   2424:        xmlFree(name);
1.1       daniel   2425:        ctxt->wellFormed = 0;
                   2426:        return;
                   2427:     }
                   2428: 
1.18      daniel   2429: 
1.1       daniel   2430:     /*
                   2431:      * Check for auto-closure of HTML elements.
                   2432:      */
1.18      daniel   2433: 
1.1       daniel   2434:     htmlAutoCloseOnClose(ctxt, name);
                   2435: 
                   2436:     /*
                   2437:      * Well formedness constraints, opening and closing must match.
                   2438:      * With the exception that the autoclose may have popped stuff out
                   2439:      * of the stack.
                   2440:      */
1.18      daniel   2441:     if (xmlStrcmp(name, ctxt->name)) {
                   2442: #ifdef DEBUG
                   2443:        fprintf(stderr,"End of tag %s: expecting %s\n", name, ctxt->name);
                   2444: #endif
1.15      daniel   2445:         if ((ctxt->name != NULL) && 
                   2446:            (xmlStrcmp(ctxt->name, name))) {
1.1       daniel   2447:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2448:                ctxt->sax->error(ctxt->userData,
                   2449:                 "Opening and ending tag mismatch: %s and %s\n",
1.15      daniel   2450:                                 name, ctxt->name);
1.1       daniel   2451:            ctxt->wellFormed = 0;
                   2452:         }
                   2453:     }
                   2454: 
                   2455:     /*
                   2456:      * SAX: End of Tag
                   2457:      */
1.15      daniel   2458:     oldname = ctxt->name;
1.24      daniel   2459:     if ((oldname != NULL) && (!xmlStrcmp(oldname, name))) {
1.18      daniel   2460:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   2461:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   2462:        oldname = htmlnamePop(ctxt);
1.18      daniel   2463:        if (oldname != NULL) {
                   2464: #ifdef DEBUG
                   2465:            fprintf(stderr,"End of tag %s: popping out %s\n", name, oldname);
                   2466: #endif
                   2467:            xmlFree(oldname);
                   2468: #ifdef DEBUG
                   2469:        } else {
                   2470:            fprintf(stderr,"End of tag %s: stack empty !!!\n", name);
                   2471: #endif
                   2472:        }
                   2473:     }
1.1       daniel   2474: 
                   2475:     if (name != NULL)
1.11      daniel   2476:        xmlFree(name);
1.1       daniel   2477: 
                   2478:     return;
                   2479: }
                   2480: 
                   2481: 
                   2482: /**
                   2483:  * htmlParseReference:
                   2484:  * @ctxt:  an HTML parser context
                   2485:  * 
                   2486:  * parse and handle entity references in content,
                   2487:  * this will end-up in a call to character() since this is either a
                   2488:  * CharRef, or a predefined entity.
                   2489:  */
                   2490: void
                   2491: htmlParseReference(htmlParserCtxtPtr ctxt) {
                   2492:     htmlEntityDescPtr ent;
1.14      daniel   2493:     xmlChar out[2];
                   2494:     xmlChar *name;
1.1       daniel   2495:     int val;
                   2496:     if (CUR != '&') return;
                   2497: 
                   2498:     if (NXT(1) == '#') {
                   2499:        val = htmlParseCharRef(ctxt);
1.8       daniel   2500:        /* invalid for UTF-8 variable encoding !!!!! */
1.1       daniel   2501:        out[0] = val;
                   2502:        out[1] = 0;
                   2503:        if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   2504:            ctxt->sax->characters(ctxt->userData, out, 1);
                   2505:     } else {
                   2506:        ent = htmlParseEntityRef(ctxt, &name);
1.32      daniel   2507:        if (name == NULL) {
                   2508:            ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
                   2509:            return;
                   2510:        }
1.1       daniel   2511:        if ((ent == NULL) || (ent->value <= 0) || (ent->value >= 255)) {
                   2512:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
1.8       daniel   2513:                ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
1.1       daniel   2514:                ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
1.32      daniel   2515:                /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
1.1       daniel   2516:            }
                   2517:        } else {
1.8       daniel   2518:            /* invalid for UTF-8 variable encoding !!!!! */
1.1       daniel   2519:            out[0] = ent->value;
                   2520:            out[1] = 0;
                   2521:            if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   2522:                ctxt->sax->characters(ctxt->userData, out, 1);
                   2523:        }
1.11      daniel   2524:        xmlFree(name);
1.1       daniel   2525:     }
                   2526: }
                   2527: 
                   2528: /**
                   2529:  * htmlParseContent:
                   2530:  * @ctxt:  an HTML parser context
                   2531:  * @name:  the node name
                   2532:  *
                   2533:  * Parse a content: comment, sub-element, reference or text.
                   2534:  *
                   2535:  */
                   2536: 
                   2537: void
1.18      daniel   2538: htmlParseContent(htmlParserCtxtPtr ctxt) {
1.15      daniel   2539:     xmlChar *currentNode;
1.18      daniel   2540:     int depth;
1.1       daniel   2541: 
1.26      daniel   2542:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   2543:     depth = ctxt->nameNr;
                   2544:     while (1) {
1.26      daniel   2545:        long cons = ctxt->nbChars;
1.1       daniel   2546: 
1.18      daniel   2547:         GROW;
                   2548:        /*
                   2549:         * Our tag or one of it's parent or children is ending.
                   2550:         */
                   2551:         if ((CUR == '<') && (NXT(1) == '/')) {
                   2552:            htmlParseEndTag(ctxt);
1.26      daniel   2553:            if (currentNode != NULL) xmlFree(currentNode);
1.18      daniel   2554:            return;
                   2555:         }
                   2556: 
                   2557:        /*
                   2558:         * Has this node been popped out during parsing of
                   2559:         * the next element
                   2560:         */
1.26      daniel   2561:         if ((xmlStrcmp(currentNode, ctxt->name)) &&
                   2562:            (depth >= ctxt->nameNr)) {
                   2563:            if (currentNode != NULL) xmlFree(currentNode);
                   2564:            return;
                   2565:        }
1.18      daniel   2566: 
1.1       daniel   2567:        /*
                   2568:         * First case :  a comment
                   2569:         */
                   2570:        if ((CUR == '<') && (NXT(1) == '!') &&
                   2571:                 (NXT(2) == '-') && (NXT(3) == '-')) {
1.31      daniel   2572:            htmlParseComment(ctxt);
1.1       daniel   2573:        }
                   2574: 
                   2575:        /*
                   2576:         * Second case :  a sub-element.
                   2577:         */
                   2578:        else if (CUR == '<') {
                   2579:            htmlParseElement(ctxt);
                   2580:        }
                   2581: 
                   2582:        /*
                   2583:         * Third case : a reference. If if has not been resolved,
                   2584:         *    parsing returns it's Name, create the node 
                   2585:         */
                   2586:        else if (CUR == '&') {
                   2587:            htmlParseReference(ctxt);
                   2588:        }
                   2589: 
                   2590:        /*
                   2591:         * Last case, text. Note that References are handled directly.
                   2592:         */
                   2593:        else {
                   2594:            htmlParseCharData(ctxt, 0);
                   2595:        }
                   2596: 
1.26      daniel   2597:        if (cons == ctxt->nbChars) {
1.22      daniel   2598:            if (ctxt->node != NULL) {
                   2599:                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2600:                    ctxt->sax->error(ctxt->userData,
                   2601:                         "detected an error in element content\n");
                   2602:                ctxt->wellFormed = 0;
                   2603:            }
1.1       daniel   2604:             break;
                   2605:        }
1.17      daniel   2606: 
1.5       daniel   2607:         GROW;
1.1       daniel   2608:     }
1.26      daniel   2609:     if (currentNode != NULL) xmlFree(currentNode);
1.1       daniel   2610: }
                   2611: 
                   2612: /**
                   2613:  * htmlParseElement:
                   2614:  * @ctxt:  an HTML parser context
                   2615:  *
                   2616:  * parse an HTML element, this is highly recursive
                   2617:  *
                   2618:  * [39] element ::= EmptyElemTag | STag content ETag
                   2619:  *
                   2620:  * [41] Attribute ::= Name Eq AttValue
                   2621:  */
                   2622: 
                   2623: void
                   2624: htmlParseElement(htmlParserCtxtPtr ctxt) {
1.14      daniel   2625:     const xmlChar *openTag = CUR_PTR;
                   2626:     xmlChar *name;
1.16      daniel   2627:     xmlChar *currentNode = NULL;
1.1       daniel   2628:     htmlElemDescPtr info;
1.10      daniel   2629:     htmlParserNodeInfo node_info;
1.31      daniel   2630:     xmlChar *oldname;
1.18      daniel   2631:     int depth = ctxt->nameNr;
1.1       daniel   2632: 
                   2633:     /* Capture start position */
1.10      daniel   2634:     if (ctxt->record_info) {
                   2635:         node_info.begin_pos = ctxt->input->consumed +
                   2636:                           (CUR_PTR - ctxt->input->base);
                   2637:        node_info.begin_line = ctxt->input->line;
                   2638:     }
1.1       daniel   2639: 
1.26      daniel   2640:     oldname = xmlStrdup(ctxt->name);
1.18      daniel   2641:     htmlParseStartTag(ctxt);
                   2642:     name = ctxt->name;
1.19      daniel   2643: #ifdef DEBUG
                   2644:     if (oldname == NULL)
                   2645:        fprintf(stderr, "Start of element %s\n", name);
                   2646:     else if (name == NULL)     
                   2647:        fprintf(stderr, "Start of element failed, was %s\n", oldname);
                   2648:     else       
                   2649:        fprintf(stderr, "Start of element %s, was %s\n", name, oldname);
                   2650: #endif
1.26      daniel   2651:     if (((depth == ctxt->nameNr) && (!xmlStrcmp(oldname, ctxt->name))) ||
1.18      daniel   2652:         (name == NULL)) {
1.19      daniel   2653:        if (CUR == '>')
                   2654:            NEXT;
1.26      daniel   2655:        if (oldname != NULL)
                   2656:            xmlFree(oldname);
1.1       daniel   2657:         return;
                   2658:     }
1.26      daniel   2659:     if (oldname != NULL)
                   2660:        xmlFree(oldname);
1.1       daniel   2661: 
                   2662:     /*
                   2663:      * Lookup the info for that element.
                   2664:      */
                   2665:     info = htmlTagLookup(name);
                   2666:     if (info == NULL) {
                   2667:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2668:            ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   2669:                             name);
                   2670:        ctxt->wellFormed = 0;
                   2671:     } else if (info->depr) {
                   2672: /***************************
                   2673:        if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   2674:            ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
                   2675:                               name);
                   2676:  ***************************/
                   2677:     }
                   2678: 
                   2679:     /*
                   2680:      * Check for an Empty Element labelled the XML/SGML way
                   2681:      */
                   2682:     if ((CUR == '/') && (NXT(1) == '>')) {
                   2683:         SKIP(2);
                   2684:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   2685:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   2686:        oldname = htmlnamePop(ctxt);
1.18      daniel   2687: #ifdef DEBUG
                   2688:         fprintf(stderr,"End of tag the XML way: popping out %s\n", oldname);
                   2689: #endif
1.17      daniel   2690:        if (oldname != NULL)
                   2691:            xmlFree(oldname);
1.1       daniel   2692:        return;
                   2693:     }
                   2694: 
1.5       daniel   2695:     if (CUR == '>') {
                   2696:         NEXT;
                   2697:     } else {
1.1       daniel   2698:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2699:            ctxt->sax->error(ctxt->userData, "Couldn't find end of Start Tag\n%.30s\n",
                   2700:                             openTag);
                   2701:        ctxt->wellFormed = 0;
                   2702: 
                   2703:        /*
                   2704:         * end of parsing of this node.
                   2705:         */
1.18      daniel   2706:        if (!xmlStrcmp(name, ctxt->name)) { 
                   2707:            nodePop(ctxt);
1.24      daniel   2708:            oldname = htmlnamePop(ctxt);
1.18      daniel   2709: #ifdef DEBUG
                   2710:            fprintf(stderr,"End of start tag problem: popping out %s\n", oldname);
                   2711: #endif
                   2712:            if (oldname != NULL)
                   2713:                xmlFree(oldname);
                   2714:        }    
1.10      daniel   2715: 
                   2716:        /*
                   2717:         * Capture end position and add node
                   2718:         */
                   2719:        if ( currentNode != NULL && ctxt->record_info ) {
                   2720:           node_info.end_pos = ctxt->input->consumed +
                   2721:                              (CUR_PTR - ctxt->input->base);
                   2722:           node_info.end_line = ctxt->input->line;
1.15      daniel   2723:           node_info.node = ctxt->node;
1.10      daniel   2724:           xmlParserAddNodeInfo(ctxt, &node_info);
                   2725:        }
1.1       daniel   2726:        return;
                   2727:     }
                   2728: 
                   2729:     /*
                   2730:      * Check for an Empty Element from DTD definition
                   2731:      */
                   2732:     if ((info != NULL) && (info->empty)) {
                   2733:        if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   2734:            ctxt->sax->endElement(ctxt->userData, name);
1.24      daniel   2735:        oldname = htmlnamePop(ctxt);
1.18      daniel   2736: #ifdef DEBUG
                   2737:        fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
                   2738: #endif
1.17      daniel   2739:        if (oldname != NULL)
                   2740:            xmlFree(oldname);
1.1       daniel   2741:        return;
                   2742:     }
                   2743: 
                   2744:     /*
                   2745:      * Parse the content of the element:
                   2746:      */
1.26      daniel   2747:     currentNode = xmlStrdup(ctxt->name);
1.18      daniel   2748:     depth = ctxt->nameNr;
                   2749:     while (IS_CHAR(CUR)) {
                   2750:        htmlParseContent(ctxt);
                   2751:        if (ctxt->nameNr < depth) break; 
                   2752:     }  
1.1       daniel   2753: 
                   2754:     if (!IS_CHAR(CUR)) {
                   2755:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2756:            ctxt->sax->error(ctxt->userData,
1.18      daniel   2757:                 "Premature end of data in tag %s\n", currentNode);
1.1       daniel   2758:        ctxt->wellFormed = 0;
                   2759: 
                   2760:        /*
                   2761:         * end of parsing of this node.
                   2762:         */
                   2763:        nodePop(ctxt);
1.24      daniel   2764:        oldname = htmlnamePop(ctxt);
1.18      daniel   2765: #ifdef DEBUG
                   2766:        fprintf(stderr,"Premature end of tag %s : popping out %s\n", name, oldname);
                   2767: #endif
1.17      daniel   2768:        if (oldname != NULL)
                   2769:            xmlFree(oldname);
1.26      daniel   2770:        if (currentNode != NULL)
                   2771:            xmlFree(currentNode);
1.1       daniel   2772:        return;
                   2773:     }
1.10      daniel   2774: 
                   2775:     /*
                   2776:      * Capture end position and add node
                   2777:      */
                   2778:     if ( currentNode != NULL && ctxt->record_info ) {
                   2779:        node_info.end_pos = ctxt->input->consumed +
                   2780:                           (CUR_PTR - ctxt->input->base);
                   2781:        node_info.end_line = ctxt->input->line;
1.15      daniel   2782:        node_info.node = ctxt->node;
1.10      daniel   2783:        xmlParserAddNodeInfo(ctxt, &node_info);
                   2784:     }
1.26      daniel   2785:     if (currentNode != NULL)
                   2786:        xmlFree(currentNode);
1.1       daniel   2787: }
                   2788: 
                   2789: /**
                   2790:  * htmlParseDocument :
                   2791:  * @ctxt:  an HTML parser context
                   2792:  * 
                   2793:  * parse an HTML document (and build a tree if using the standard SAX
                   2794:  * interface).
                   2795:  *
                   2796:  * Returns 0, -1 in case of error. the parser context is augmented
                   2797:  *                as a result of the parsing.
                   2798:  */
                   2799: 
                   2800: int
                   2801: htmlParseDocument(htmlParserCtxtPtr ctxt) {
                   2802:     htmlDefaultSAXHandlerInit();
                   2803:     ctxt->html = 1;
                   2804: 
1.5       daniel   2805:     GROW;
1.1       daniel   2806:     /*
1.9       daniel   2807:      * SAX: beginning of the document processing.
1.1       daniel   2808:      */
                   2809:     if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   2810:         ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
                   2811: 
                   2812:     /*
                   2813:      * Wipe out everything which is before the first '<'
                   2814:      */
1.22      daniel   2815:     SKIP_BLANKS;
1.1       daniel   2816:     if (CUR == 0) {
                   2817:        if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   2818:            ctxt->sax->error(ctxt->userData, "Document is empty\n");
                   2819:        ctxt->wellFormed = 0;
                   2820:     }
                   2821: 
1.40      daniel   2822:     if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
                   2823:        ctxt->sax->startDocument(ctxt->userData);
                   2824: 
                   2825: 
1.22      daniel   2826:     /*
                   2827:      * Parse possible comments before any content
                   2828:      */
                   2829:     while ((CUR == '<') && (NXT(1) == '!') &&
                   2830:            (NXT(2) == '-') && (NXT(3) == '-')) {
1.31      daniel   2831:         htmlParseComment(ctxt);           
1.22      daniel   2832:        SKIP_BLANKS;
                   2833:     }     
                   2834: 
1.1       daniel   2835: 
                   2836:     /*
                   2837:      * Then possibly doc type declaration(s) and more Misc
                   2838:      * (doctypedecl Misc*)?
                   2839:      */
                   2840:     if ((CUR == '<') && (NXT(1) == '!') &&
                   2841:        (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   2842:        (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   2843:        (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   2844:        (UPP(8) == 'E')) {
                   2845:        htmlParseDocTypeDecl(ctxt);
                   2846:     }
                   2847:     SKIP_BLANKS;
                   2848: 
                   2849:     /*
                   2850:      * Time to start parsing the tree itself
                   2851:      */
1.22      daniel   2852:     htmlParseContent(ctxt);
1.1       daniel   2853: 
                   2854:     /*
                   2855:      * SAX: end of the document processing.
                   2856:      */
                   2857:     if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   2858:         ctxt->sax->endDocument(ctxt->userData);
                   2859:     if (! ctxt->wellFormed) return(-1);
                   2860:     return(0);
                   2861: }
                   2862: 
                   2863: 
1.30      daniel   2864: /************************************************************************
                   2865:  *                                                                     *
                   2866:  *                     Parser contexts handling                        *
                   2867:  *                                                                     *
                   2868:  ************************************************************************/
1.1       daniel   2869: 
                   2870: /**
                   2871:  * xmlInitParserCtxt:
                   2872:  * @ctxt:  an HTML parser context
                   2873:  *
                   2874:  * Initialize a parser context
                   2875:  */
                   2876: 
                   2877: void
                   2878: htmlInitParserCtxt(htmlParserCtxtPtr ctxt)
                   2879: {
                   2880:     htmlSAXHandler *sax;
                   2881: 
1.21      daniel   2882:     if (ctxt == NULL) return;
                   2883:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   2884: 
1.11      daniel   2885:     sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
1.1       daniel   2886:     if (sax == NULL) {
                   2887:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   2888:     }
1.19      daniel   2889:     memset(sax, 0, sizeof(htmlSAXHandler));
1.1       daniel   2890: 
                   2891:     /* Allocate the Input stack */
1.19      daniel   2892:     ctxt->inputTab = (htmlParserInputPtr *) 
                   2893:                       xmlMalloc(5 * sizeof(htmlParserInputPtr));
                   2894:     if (ctxt->inputTab == NULL) {
                   2895:         fprintf(stderr, "htmlInitParserCtxt: out of memory\n");
                   2896:     }
1.1       daniel   2897:     ctxt->inputNr = 0;
                   2898:     ctxt->inputMax = 5;
                   2899:     ctxt->input = NULL;
                   2900:     ctxt->version = NULL;
                   2901:     ctxt->encoding = NULL;
                   2902:     ctxt->standalone = -1;
1.30      daniel   2903:     ctxt->instate = XML_PARSER_START;
1.1       daniel   2904: 
                   2905:     /* Allocate the Node stack */
1.11      daniel   2906:     ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
1.1       daniel   2907:     ctxt->nodeNr = 0;
                   2908:     ctxt->nodeMax = 10;
                   2909:     ctxt->node = NULL;
                   2910: 
1.15      daniel   2911:     /* Allocate the Name stack */
                   2912:     ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
                   2913:     ctxt->nameNr = 0;
                   2914:     ctxt->nameMax = 10;
                   2915:     ctxt->name = NULL;
                   2916: 
1.1       daniel   2917:     if (sax == NULL) ctxt->sax = &htmlDefaultSAXHandler;
                   2918:     else {
                   2919:         ctxt->sax = sax;
                   2920:        memcpy(sax, &htmlDefaultSAXHandler, sizeof(htmlSAXHandler));
                   2921:     }
                   2922:     ctxt->userData = ctxt;
                   2923:     ctxt->myDoc = NULL;
                   2924:     ctxt->wellFormed = 1;
                   2925:     ctxt->replaceEntities = 0;
                   2926:     ctxt->html = 1;
                   2927:     ctxt->record_info = 0;
1.21      daniel   2928:     ctxt->validate = 0;
1.26      daniel   2929:     ctxt->nbChars = 0;
1.30      daniel   2930:     ctxt->checkIndex = 0;
1.1       daniel   2931:     xmlInitNodeInfoSeq(&ctxt->node_seq);
                   2932: }
                   2933: 
                   2934: /**
                   2935:  * htmlFreeParserCtxt:
                   2936:  * @ctxt:  an HTML parser context
                   2937:  *
                   2938:  * Free all the memory used by a parser context. However the parsed
                   2939:  * document in ctxt->myDoc is not freed.
                   2940:  */
                   2941: 
                   2942: void
                   2943: htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
                   2944: {
                   2945:     htmlParserInputPtr input;
1.15      daniel   2946:     xmlChar *oldname;
1.1       daniel   2947: 
                   2948:     if (ctxt == NULL) return;
                   2949: 
                   2950:     while ((input = inputPop(ctxt)) != NULL) {
                   2951:         xmlFreeInputStream(input);
                   2952:     }
                   2953: 
1.11      daniel   2954:     if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1.24      daniel   2955:     while ((oldname = htmlnamePop(ctxt)) != NULL) {
                   2956:        xmlFree(oldname);
1.15      daniel   2957:     }
                   2958:     if (ctxt->nameTab != NULL) xmlFree(ctxt->nameTab);
1.31      daniel   2959:     if (ctxt->directory != NULL) xmlFree(ctxt->directory);
1.11      daniel   2960:     if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
                   2961:     if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1.1       daniel   2962:     if ((ctxt->sax != NULL) && (ctxt->sax != &htmlDefaultSAXHandler))
1.11      daniel   2963:         xmlFree(ctxt->sax);
                   2964:     xmlFree(ctxt);
1.1       daniel   2965: }
                   2966: 
                   2967: /**
                   2968:  * htmlCreateDocParserCtxt :
1.14      daniel   2969:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   2970:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   2971:  *
                   2972:  * Create a parser context for an HTML document.
                   2973:  *
                   2974:  * Returns the new parser context or NULL
                   2975:  */
                   2976: htmlParserCtxtPtr
1.14      daniel   2977: htmlCreateDocParserCtxt(xmlChar *cur, const char *encoding) {
1.1       daniel   2978:     htmlParserCtxtPtr ctxt;
                   2979:     htmlParserInputPtr input;
                   2980:     /* htmlCharEncoding enc; */
                   2981: 
1.11      daniel   2982:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   2983:     if (ctxt == NULL) {
                   2984:         perror("malloc");
                   2985:        return(NULL);
                   2986:     }
                   2987:     htmlInitParserCtxt(ctxt);
1.11      daniel   2988:     input = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   2989:     if (input == NULL) {
                   2990:         perror("malloc");
1.11      daniel   2991:        xmlFree(ctxt);
1.1       daniel   2992:        return(NULL);
                   2993:     }
1.19      daniel   2994:     memset(input, 0, sizeof(htmlParserInput));
1.1       daniel   2995: 
                   2996:     input->line = 1;
                   2997:     input->col = 1;
                   2998:     input->base = cur;
                   2999:     input->cur = cur;
                   3000: 
                   3001:     inputPush(ctxt, input);
                   3002:     return(ctxt);
                   3003: }
                   3004: 
1.31      daniel   3005: /************************************************************************
                   3006:  *                                                                     *
                   3007:  *             Progressive parsing interfaces                          *
                   3008:  *                                                                     *
                   3009:  ************************************************************************/
                   3010: 
                   3011: /**
                   3012:  * htmlParseLookupSequence:
                   3013:  * @ctxt:  an HTML parser context
                   3014:  * @first:  the first char to lookup
                   3015:  * @next:  the next char to lookup or zero
                   3016:  * @third:  the next char to lookup or zero
                   3017:  *
                   3018:  * Try to find if a sequence (first, next, third) or  just (first next) or
                   3019:  * (first) is available in the input stream.
                   3020:  * This function has a side effect of (possibly) incrementing ctxt->checkIndex
                   3021:  * to avoid rescanning sequences of bytes, it DOES change the state of the
                   3022:  * parser, do not use liberally.
                   3023:  * This is basically similar to xmlParseLookupSequence()
                   3024:  *
                   3025:  * Returns the index to the current parsing point if the full sequence
                   3026:  *      is available, -1 otherwise.
                   3027:  */
                   3028: int
                   3029: htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
                   3030:                        xmlChar next, xmlChar third) {
                   3031:     int base, len;
                   3032:     htmlParserInputPtr in;
                   3033:     const xmlChar *buf;
                   3034: 
                   3035:     in = ctxt->input;
                   3036:     if (in == NULL) return(-1);
                   3037:     base = in->cur - in->base;
                   3038:     if (base < 0) return(-1);
                   3039:     if (ctxt->checkIndex > base)
                   3040:         base = ctxt->checkIndex;
                   3041:     if (in->buf == NULL) {
                   3042:        buf = in->base;
                   3043:        len = in->length;
                   3044:     } else {
                   3045:        buf = in->buf->buffer->content;
                   3046:        len = in->buf->buffer->use;
                   3047:     }
                   3048:     /* take into account the sequence length */
                   3049:     if (third) len -= 2;
                   3050:     else if (next) len --;
                   3051:     for (;base < len;base++) {
                   3052:         if (buf[base] == first) {
                   3053:            if (third != 0) {
                   3054:                if ((buf[base + 1] != next) ||
                   3055:                    (buf[base + 2] != third)) continue;
                   3056:            } else if (next != 0) {
                   3057:                if (buf[base + 1] != next) continue;
                   3058:            }
                   3059:            ctxt->checkIndex = 0;
                   3060: #ifdef DEBUG_PUSH
                   3061:            if (next == 0)
                   3062:                fprintf(stderr, "HPP: lookup '%c' found at %d\n",
                   3063:                        first, base);
                   3064:            else if (third == 0)
                   3065:                fprintf(stderr, "HPP: lookup '%c%c' found at %d\n",
                   3066:                        first, next, base);
                   3067:            else 
                   3068:                fprintf(stderr, "HPP: lookup '%c%c%c' found at %d\n",
                   3069:                        first, next, third, base);
                   3070: #endif
                   3071:            return(base - (in->cur - in->base));
                   3072:        }
                   3073:     }
                   3074:     ctxt->checkIndex = base;
                   3075: #ifdef DEBUG_PUSH
                   3076:     if (next == 0)
                   3077:        fprintf(stderr, "HPP: lookup '%c' failed\n", first);
                   3078:     else if (third == 0)
                   3079:        fprintf(stderr, "HPP: lookup '%c%c' failed\n", first, next);
                   3080:     else       
                   3081:        fprintf(stderr, "HPP: lookup '%c%c%c' failed\n", first, next, third);
                   3082: #endif
                   3083:     return(-1);
                   3084: }
                   3085: 
                   3086: /**
1.32      daniel   3087:  * htmlParseTryOrFinish:
1.31      daniel   3088:  * @ctxt:  an HTML parser context
1.32      daniel   3089:  * @terminate:  last chunk indicator
1.31      daniel   3090:  *
                   3091:  * Try to progress on parsing
                   3092:  *
                   3093:  * Returns zero if no parsing was possible
                   3094:  */
                   3095: int
1.32      daniel   3096: htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
1.31      daniel   3097:     int ret = 0;
                   3098:     htmlParserInputPtr in;
                   3099:     int avail;
                   3100:     xmlChar cur, next;
                   3101: 
                   3102: #ifdef DEBUG_PUSH
                   3103:     switch (ctxt->instate) {
                   3104:        case XML_PARSER_EOF:
                   3105:            fprintf(stderr, "HPP: try EOF\n"); break;
                   3106:        case XML_PARSER_START:
                   3107:            fprintf(stderr, "HPP: try START\n"); break;
                   3108:        case XML_PARSER_MISC:
                   3109:            fprintf(stderr, "HPP: try MISC\n");break;
                   3110:        case XML_PARSER_COMMENT:
                   3111:            fprintf(stderr, "HPP: try COMMENT\n");break;
                   3112:        case XML_PARSER_PROLOG:
                   3113:            fprintf(stderr, "HPP: try PROLOG\n");break;
                   3114:        case XML_PARSER_START_TAG:
                   3115:            fprintf(stderr, "HPP: try START_TAG\n");break;
                   3116:        case XML_PARSER_CONTENT:
                   3117:            fprintf(stderr, "HPP: try CONTENT\n");break;
                   3118:        case XML_PARSER_CDATA_SECTION:
                   3119:            fprintf(stderr, "HPP: try CDATA_SECTION\n");break;
                   3120:        case XML_PARSER_END_TAG:
                   3121:            fprintf(stderr, "HPP: try END_TAG\n");break;
                   3122:        case XML_PARSER_ENTITY_DECL:
                   3123:            fprintf(stderr, "HPP: try ENTITY_DECL\n");break;
                   3124:        case XML_PARSER_ENTITY_VALUE:
                   3125:            fprintf(stderr, "HPP: try ENTITY_VALUE\n");break;
                   3126:        case XML_PARSER_ATTRIBUTE_VALUE:
                   3127:            fprintf(stderr, "HPP: try ATTRIBUTE_VALUE\n");break;
                   3128:        case XML_PARSER_DTD:
                   3129:            fprintf(stderr, "HPP: try DTD\n");break;
                   3130:        case XML_PARSER_EPILOG:
                   3131:            fprintf(stderr, "HPP: try EPILOG\n");break;
                   3132:        case XML_PARSER_PI:
                   3133:            fprintf(stderr, "HPP: try PI\n");break;
                   3134:     }
                   3135: #endif
                   3136: 
                   3137:     while (1) {
                   3138: 
                   3139:        in = ctxt->input;
                   3140:        if (in == NULL) break;
                   3141:        if (in->buf == NULL)
                   3142:            avail = in->length - (in->cur - in->base);
                   3143:        else
                   3144:            avail = in->buf->buffer->use - (in->cur - in->base);
                   3145:         if (avail < 1)
                   3146:            goto done;
                   3147:         switch (ctxt->instate) {
                   3148:             case XML_PARSER_EOF:
                   3149:                /*
                   3150:                 * Document parsing is done !
                   3151:                 */
                   3152:                goto done;
                   3153:             case XML_PARSER_START:
                   3154:                /*
                   3155:                 * Very first chars read from the document flow.
                   3156:                 */
                   3157:                cur = in->cur[0];
                   3158:                if (IS_BLANK(cur)) {
                   3159:                    SKIP_BLANKS;
                   3160:                    if (in->buf == NULL)
                   3161:                        avail = in->length - (in->cur - in->base);
                   3162:                    else
                   3163:                        avail = in->buf->buffer->use - (in->cur - in->base);
                   3164:                }
                   3165:                if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
                   3166:                    ctxt->sax->setDocumentLocator(ctxt->userData,
                   3167:                                                  &xmlDefaultSAXLocator);
1.46    ! daniel   3168:                if ((ctxt->sax) && (ctxt->sax->startDocument) &&
        !          3169:                    (!ctxt->disableSAX))
        !          3170:                    ctxt->sax->startDocument(ctxt->userData);
        !          3171: 
1.31      daniel   3172:                cur = in->cur[0];
                   3173:                next = in->cur[1];
                   3174:                if ((cur == '<') && (next == '!') &&
                   3175:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3176:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3177:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3178:                    (UPP(8) == 'E')) {
1.32      daniel   3179:                    if ((!terminate) &&
                   3180:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3181:                        goto done;
                   3182: #ifdef DEBUG_PUSH
                   3183:                    fprintf(stderr, "HPP: Parsing internal subset\n");
                   3184: #endif
                   3185:                    htmlParseDocTypeDecl(ctxt);
                   3186:                    ctxt->instate = XML_PARSER_PROLOG;
                   3187: #ifdef DEBUG_PUSH
                   3188:                    fprintf(stderr, "HPP: entering PROLOG\n");
                   3189: #endif
                   3190:                 } else {
                   3191:                    ctxt->instate = XML_PARSER_MISC;
                   3192:                }
                   3193: #ifdef DEBUG_PUSH
                   3194:                fprintf(stderr, "HPP: entering MISC\n");
                   3195: #endif
                   3196:                break;
                   3197:             case XML_PARSER_MISC:
                   3198:                SKIP_BLANKS;
                   3199:                if (in->buf == NULL)
                   3200:                    avail = in->length - (in->cur - in->base);
                   3201:                else
                   3202:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   3203:                if (avail < 2)
                   3204:                    goto done;
                   3205:                cur = in->cur[0];
                   3206:                next = in->cur[1];
                   3207:                if ((cur == '<') && (next == '!') &&
                   3208:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3209:                    if ((!terminate) &&
                   3210:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3211:                        goto done;
                   3212: #ifdef DEBUG_PUSH
                   3213:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3214: #endif
                   3215:                    htmlParseComment(ctxt);
                   3216:                    ctxt->instate = XML_PARSER_MISC;
                   3217:                } else if ((cur == '<') && (next == '!') &&
                   3218:                    (UPP(2) == 'D') && (UPP(3) == 'O') &&
                   3219:                    (UPP(4) == 'C') && (UPP(5) == 'T') &&
                   3220:                    (UPP(6) == 'Y') && (UPP(7) == 'P') &&
                   3221:                    (UPP(8) == 'E')) {
1.32      daniel   3222:                    if ((!terminate) &&
                   3223:                        (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3224:                        goto done;
                   3225: #ifdef DEBUG_PUSH
                   3226:                    fprintf(stderr, "HPP: Parsing internal subset\n");
                   3227: #endif
                   3228:                    htmlParseDocTypeDecl(ctxt);
                   3229:                    ctxt->instate = XML_PARSER_PROLOG;
                   3230: #ifdef DEBUG_PUSH
                   3231:                    fprintf(stderr, "HPP: entering PROLOG\n");
                   3232: #endif
                   3233:                } else if ((cur == '<') && (next == '!') &&
                   3234:                           (avail < 9)) {
                   3235:                    goto done;
                   3236:                } else {
                   3237:                    ctxt->instate = XML_PARSER_START_TAG;
                   3238: #ifdef DEBUG_PUSH
                   3239:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   3240: #endif
                   3241:                }
                   3242:                break;
                   3243:             case XML_PARSER_PROLOG:
                   3244:                SKIP_BLANKS;
                   3245:                if (in->buf == NULL)
                   3246:                    avail = in->length - (in->cur - in->base);
                   3247:                else
                   3248:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   3249:                if (avail < 2) 
                   3250:                    goto done;
                   3251:                cur = in->cur[0];
                   3252:                next = in->cur[1];
                   3253:                if ((cur == '<') && (next == '!') &&
                   3254:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3255:                    if ((!terminate) &&
                   3256:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3257:                        goto done;
                   3258: #ifdef DEBUG_PUSH
                   3259:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3260: #endif
                   3261:                    htmlParseComment(ctxt);
                   3262:                    ctxt->instate = XML_PARSER_PROLOG;
                   3263:                } else if ((cur == '<') && (next == '!') &&
                   3264:                           (avail < 4)) {
                   3265:                    goto done;
                   3266:                } else {
                   3267:                    ctxt->instate = XML_PARSER_START_TAG;
                   3268: #ifdef DEBUG_PUSH
                   3269:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   3270: #endif
                   3271:                }
                   3272:                break;
                   3273:             case XML_PARSER_EPILOG:
                   3274:                SKIP_BLANKS;
                   3275:                if (in->buf == NULL)
                   3276:                    avail = in->length - (in->cur - in->base);
                   3277:                else
                   3278:                    avail = in->buf->buffer->use - (in->cur - in->base);
                   3279:                if (avail < 2)
                   3280:                    goto done;
                   3281:                cur = in->cur[0];
                   3282:                next = in->cur[1];
                   3283:                if ((cur == '<') && (next == '!') &&
                   3284:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3285:                    if ((!terminate) &&
                   3286:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3287:                        goto done;
                   3288: #ifdef DEBUG_PUSH
                   3289:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3290: #endif
                   3291:                    htmlParseComment(ctxt);
                   3292:                    ctxt->instate = XML_PARSER_EPILOG;
                   3293:                } else if ((cur == '<') && (next == '!') &&
                   3294:                           (avail < 4)) {
                   3295:                    goto done;
                   3296:                } else {
                   3297:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3298:                        ctxt->sax->error(ctxt->userData,
                   3299:                            "Extra content at the end of the document\n");
                   3300:                    ctxt->wellFormed = 0;
                   3301:                    ctxt->errNo = XML_ERR_DOCUMENT_END;
                   3302:                    ctxt->instate = XML_PARSER_EOF;
                   3303: #ifdef DEBUG_PUSH
                   3304:                    fprintf(stderr, "HPP: entering EOF\n");
                   3305: #endif
                   3306:                    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3307:                        ctxt->sax->endDocument(ctxt->userData);
                   3308:                    goto done;
                   3309:                }
                   3310:                break;
                   3311:             case XML_PARSER_START_TAG: {
                   3312:                xmlChar *name, *oldname;
                   3313:                int depth = ctxt->nameNr;
                   3314:                htmlElemDescPtr info;
                   3315: 
                   3316:                if (avail < 2)
                   3317:                    goto done;
                   3318:                cur = in->cur[0];
                   3319:                if (cur != '<') {
                   3320:                    ctxt->instate = XML_PARSER_CONTENT;
                   3321: #ifdef DEBUG_PUSH
                   3322:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   3323: #endif
                   3324:                    break;
                   3325:                }
1.32      daniel   3326:                if ((!terminate) &&
                   3327:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3328:                    goto done;
                   3329: 
                   3330:                oldname = xmlStrdup(ctxt->name);
                   3331:                htmlParseStartTag(ctxt);
                   3332:                name = ctxt->name;
                   3333: #ifdef DEBUG
                   3334:                if (oldname == NULL)
                   3335:                    fprintf(stderr, "Start of element %s\n", name);
                   3336:                else if (name == NULL)  
                   3337:                    fprintf(stderr, "Start of element failed, was %s\n",
                   3338:                            oldname);
                   3339:                else    
                   3340:                    fprintf(stderr, "Start of element %s, was %s\n",
                   3341:                            name, oldname);
                   3342: #endif
                   3343:                if (((depth == ctxt->nameNr) &&
                   3344:                     (!xmlStrcmp(oldname, ctxt->name))) ||
                   3345:                    (name == NULL)) {
                   3346:                    if (CUR == '>')
                   3347:                        NEXT;
                   3348:                    if (oldname != NULL)
                   3349:                        xmlFree(oldname);
                   3350:                    break;
                   3351:                }
                   3352:                if (oldname != NULL)
                   3353:                    xmlFree(oldname);
                   3354: 
                   3355:                /*
                   3356:                 * Lookup the info for that element.
                   3357:                 */
                   3358:                info = htmlTagLookup(name);
                   3359:                if (info == NULL) {
                   3360:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3361:                        ctxt->sax->error(ctxt->userData, "Tag %s invalid\n",
                   3362:                                         name);
                   3363:                    ctxt->wellFormed = 0;
                   3364:                } else if (info->depr) {
                   3365:                    /***************************
                   3366:                    if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
                   3367:                        ctxt->sax->warning(ctxt->userData,
                   3368:                                           "Tag %s is deprecated\n",
                   3369:                                           name);
                   3370:                     ***************************/
                   3371:                }
                   3372: 
                   3373:                /*
                   3374:                 * Check for an Empty Element labelled the XML/SGML way
                   3375:                 */
                   3376:                if ((CUR == '/') && (NXT(1) == '>')) {
                   3377:                    SKIP(2);
                   3378:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3379:                        ctxt->sax->endElement(ctxt->userData, name);
                   3380:                    oldname = htmlnamePop(ctxt);
                   3381: #ifdef DEBUG
                   3382:                    fprintf(stderr,"End of tag the XML way: popping out %s\n",
                   3383:                            oldname);
                   3384: #endif
                   3385:                    if (oldname != NULL)
                   3386:                        xmlFree(oldname);
                   3387:                    ctxt->instate = XML_PARSER_CONTENT;
                   3388: #ifdef DEBUG_PUSH
                   3389:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   3390: #endif
                   3391:                    break;
                   3392:                }
                   3393: 
                   3394:                if (CUR == '>') {
                   3395:                    NEXT;
                   3396:                } else {
                   3397:                    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3398:                        ctxt->sax->error(ctxt->userData, 
                   3399:                                         "Couldn't find end of Start Tag %s\n",
                   3400:                                         name);
                   3401:                    ctxt->wellFormed = 0;
                   3402: 
                   3403:                    /*
                   3404:                     * end of parsing of this node.
                   3405:                     */
                   3406:                    if (!xmlStrcmp(name, ctxt->name)) { 
                   3407:                        nodePop(ctxt);
                   3408:                        oldname = htmlnamePop(ctxt);
                   3409: #ifdef DEBUG
                   3410:                        fprintf(stderr,
                   3411:                         "End of start tag problem: popping out %s\n", oldname);
                   3412: #endif
                   3413:                        if (oldname != NULL)
                   3414:                            xmlFree(oldname);
                   3415:                    }    
                   3416: 
                   3417:                    ctxt->instate = XML_PARSER_CONTENT;
                   3418: #ifdef DEBUG_PUSH
                   3419:                    fprintf(stderr, "HPP: entering CONTENT\n");
                   3420: #endif
                   3421:                    break;
                   3422:                }
                   3423: 
                   3424:                /*
                   3425:                 * Check for an Empty Element from DTD definition
                   3426:                 */
                   3427:                if ((info != NULL) && (info->empty)) {
                   3428:                    if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
                   3429:                        ctxt->sax->endElement(ctxt->userData, name);
                   3430:                    oldname = htmlnamePop(ctxt);
                   3431: #ifdef DEBUG
                   3432:                    fprintf(stderr,"End of empty tag %s : popping out %s\n", name, oldname);
                   3433: #endif
                   3434:                    if (oldname != NULL)
                   3435:                        xmlFree(oldname);
                   3436:                }
                   3437:                ctxt->instate = XML_PARSER_CONTENT;
                   3438: #ifdef DEBUG_PUSH
                   3439:                fprintf(stderr, "HPP: entering CONTENT\n");
                   3440: #endif
                   3441:                 break;
                   3442:            }
                   3443:             case XML_PARSER_CONTENT:
                   3444:                 /*
                   3445:                 * Handle preparsed entities and charRef
                   3446:                 */
                   3447:                if (ctxt->token != 0) {
                   3448:                    xmlChar cur[2] = { 0 , 0 } ;
                   3449: 
                   3450:                    cur[0] = (xmlChar) ctxt->token;
                   3451:                    if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
                   3452:                        ctxt->sax->characters(ctxt->userData, cur, 1);
                   3453:                    ctxt->token = 0;
                   3454:                    ctxt->checkIndex = 0;
                   3455:                }
                   3456:                if (avail < 2)
                   3457:                    goto done;
                   3458:                cur = in->cur[0];
                   3459:                next = in->cur[1];
                   3460:                if ((cur == '<') && (next == '!') &&
                   3461:                    (in->cur[2] == '-') && (in->cur[3] == '-')) {
1.32      daniel   3462:                    if ((!terminate) &&
                   3463:                        (htmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
1.31      daniel   3464:                        goto done;
                   3465: #ifdef DEBUG_PUSH
                   3466:                    fprintf(stderr, "HPP: Parsing Comment\n");
                   3467: #endif
                   3468:                    htmlParseComment(ctxt);
                   3469:                    ctxt->instate = XML_PARSER_CONTENT;
                   3470:                } else if ((cur == '<') && (next == '!') && (avail < 4)) {
                   3471:                    goto done;
                   3472:                } else if ((cur == '<') && (next == '/')) {
                   3473:                    ctxt->instate = XML_PARSER_END_TAG;
                   3474:                    ctxt->checkIndex = 0;
                   3475: #ifdef DEBUG_PUSH
                   3476:                    fprintf(stderr, "HPP: entering END_TAG\n");
                   3477: #endif
                   3478:                    break;
                   3479:                } else if (cur == '<') {
                   3480:                    ctxt->instate = XML_PARSER_START_TAG;
                   3481:                    ctxt->checkIndex = 0;
                   3482: #ifdef DEBUG_PUSH
                   3483:                    fprintf(stderr, "HPP: entering START_TAG\n");
                   3484: #endif
                   3485:                    break;
                   3486:                } else if (cur == '&') {
1.32      daniel   3487:                    if ((!terminate) &&
                   3488:                        (htmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
1.31      daniel   3489:                        goto done;
                   3490: #ifdef DEBUG_PUSH
                   3491:                    fprintf(stderr, "HPP: Parsing Reference\n");
                   3492: #endif
                   3493:                    /* TODO: check generation of subtrees if noent !!! */
                   3494:                    htmlParseReference(ctxt);
                   3495:                } else {
                   3496:                    /* TODO Avoid the extra copy, handle directly !!!!!! */
                   3497:                    /*
                   3498:                     * Goal of the following test is :
                   3499:                     *  - minimize calls to the SAX 'character' callback
                   3500:                     *    when they are mergeable
                   3501:                     */
                   3502:                    if ((ctxt->inputNr == 1) &&
                   3503:                        (avail < HTML_PARSER_BIG_BUFFER_SIZE)) {
1.32      daniel   3504:                        if ((!terminate) &&
                   3505:                            (htmlParseLookupSequence(ctxt, '<', 0, 0) < 0))
1.31      daniel   3506:                            goto done;
                   3507:                     }
                   3508:                    ctxt->checkIndex = 0;
                   3509: #ifdef DEBUG_PUSH
                   3510:                    fprintf(stderr, "HPP: Parsing char data\n");
                   3511: #endif
                   3512:                    htmlParseCharData(ctxt, 0);
                   3513:                }
                   3514:                break;
                   3515:             case XML_PARSER_END_TAG:
                   3516:                if (avail < 2)
                   3517:                    goto done;
1.32      daniel   3518:                if ((!terminate) &&
                   3519:                    (htmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
1.31      daniel   3520:                    goto done;
                   3521:                htmlParseEndTag(ctxt);
                   3522:                if (ctxt->nameNr == 0) {
                   3523:                    ctxt->instate = XML_PARSER_EPILOG;
                   3524:                } else {
                   3525:                    ctxt->instate = XML_PARSER_CONTENT;
                   3526:                }
                   3527:                ctxt->checkIndex = 0;
                   3528: #ifdef DEBUG_PUSH
                   3529:                fprintf(stderr, "HPP: entering CONTENT\n");
                   3530: #endif
                   3531:                break;
                   3532:             case XML_PARSER_CDATA_SECTION:
                   3533:                fprintf(stderr, "HPP: internal error, state == CDATA\n");
                   3534:                ctxt->instate = XML_PARSER_CONTENT;
                   3535:                ctxt->checkIndex = 0;
                   3536: #ifdef DEBUG_PUSH
                   3537:                fprintf(stderr, "HPP: entering CONTENT\n");
                   3538: #endif
                   3539:                break;
                   3540:             case XML_PARSER_DTD:
                   3541:                fprintf(stderr, "HPP: internal error, state == DTD\n");
                   3542:                ctxt->instate = XML_PARSER_CONTENT;
                   3543:                ctxt->checkIndex = 0;
                   3544: #ifdef DEBUG_PUSH
                   3545:                fprintf(stderr, "HPP: entering CONTENT\n");
                   3546: #endif
                   3547:                break;
                   3548:             case XML_PARSER_COMMENT:
                   3549:                fprintf(stderr, "HPP: internal error, state == COMMENT\n");
                   3550:                ctxt->instate = XML_PARSER_CONTENT;
                   3551:                ctxt->checkIndex = 0;
                   3552: #ifdef DEBUG_PUSH
                   3553:                fprintf(stderr, "HPP: entering CONTENT\n");
                   3554: #endif
                   3555:                break;
                   3556:             case XML_PARSER_PI:
                   3557:                fprintf(stderr, "HPP: internal error, state == PI\n");
                   3558:                ctxt->instate = XML_PARSER_CONTENT;
                   3559:                ctxt->checkIndex = 0;
                   3560: #ifdef DEBUG_PUSH
                   3561:                fprintf(stderr, "HPP: entering CONTENT\n");
                   3562: #endif
                   3563:                break;
                   3564:             case XML_PARSER_ENTITY_DECL:
                   3565:                fprintf(stderr, "HPP: internal error, state == ENTITY_DECL\n");
                   3566:                ctxt->instate = XML_PARSER_CONTENT;
                   3567:                ctxt->checkIndex = 0;
                   3568: #ifdef DEBUG_PUSH
                   3569:                fprintf(stderr, "HPP: entering CONTENT\n");
                   3570: #endif
                   3571:                break;
                   3572:             case XML_PARSER_ENTITY_VALUE:
                   3573:                fprintf(stderr, "HPP: internal error, state == ENTITY_VALUE\n");
                   3574:                ctxt->instate = XML_PARSER_CONTENT;
                   3575:                ctxt->checkIndex = 0;
                   3576: #ifdef DEBUG_PUSH
                   3577:                fprintf(stderr, "HPP: entering DTD\n");
                   3578: #endif
                   3579:                break;
                   3580:             case XML_PARSER_ATTRIBUTE_VALUE:
                   3581:                fprintf(stderr, "HPP: internal error, state == ATTRIBUTE_VALUE\n");
                   3582:                ctxt->instate = XML_PARSER_START_TAG;
                   3583:                ctxt->checkIndex = 0;
                   3584: #ifdef DEBUG_PUSH
                   3585:                fprintf(stderr, "HPP: entering START_TAG\n");
                   3586: #endif
                   3587:                break;
                   3588:        }
                   3589:     }
                   3590: done:    
                   3591: #ifdef DEBUG_PUSH
                   3592:     fprintf(stderr, "HPP: done %d\n", ret);
                   3593: #endif
                   3594:     return(ret);
                   3595: }
                   3596: 
                   3597: /**
1.32      daniel   3598:  * htmlParseTry:
                   3599:  * @ctxt:  an HTML parser context
                   3600:  *
                   3601:  * Try to progress on parsing
                   3602:  *
                   3603:  * Returns zero if no parsing was possible
                   3604:  */
                   3605: int
                   3606: htmlParseTry(htmlParserCtxtPtr ctxt) {
                   3607:     return(htmlParseTryOrFinish(ctxt, 0));
                   3608: }
                   3609: 
                   3610: /**
1.31      daniel   3611:  * htmlParseChunk:
                   3612:  * @ctxt:  an XML parser context
                   3613:  * @chunk:  an char array
                   3614:  * @size:  the size in byte of the chunk
                   3615:  * @terminate:  last chunk indicator
                   3616:  *
                   3617:  * Parse a Chunk of memory
                   3618:  *
                   3619:  * Returns zero if no error, the xmlParserErrors otherwise.
                   3620:  */
                   3621: int
                   3622: htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
                   3623:               int terminate) {
                   3624:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   3625:         (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
                   3626:        int base = ctxt->input->base - ctxt->input->buf->buffer->content;
                   3627:        int cur = ctxt->input->cur - ctxt->input->base;
                   3628:        
                   3629:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   3630:        ctxt->input->base = ctxt->input->buf->buffer->content + base;
                   3631:        ctxt->input->cur = ctxt->input->base + cur;
                   3632: #ifdef DEBUG_PUSH
                   3633:        fprintf(stderr, "HPP: pushed %d\n", size);
                   3634: #endif
                   3635: 
1.34      daniel   3636:        if ((terminate) || (ctxt->input->buf->buffer->use > 80))
                   3637:            htmlParseTryOrFinish(ctxt, terminate);
1.31      daniel   3638:     } else if (ctxt->instate != XML_PARSER_EOF)
1.32      daniel   3639:         htmlParseTryOrFinish(ctxt, terminate);
1.31      daniel   3640:     if (terminate) {
                   3641:        if ((ctxt->instate != XML_PARSER_EOF) &&
                   3642:            (ctxt->instate != XML_PARSER_EPILOG) &&
                   3643:            (ctxt->instate != XML_PARSER_MISC)) {
                   3644:            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
                   3645:                ctxt->sax->error(ctxt->userData,
                   3646:                    "Extra content at the end of the document\n");
                   3647:            ctxt->wellFormed = 0;
                   3648:            ctxt->errNo = XML_ERR_DOCUMENT_END;
                   3649:        } 
                   3650:        if (ctxt->instate != XML_PARSER_EOF) {
                   3651:            if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
                   3652:                ctxt->sax->endDocument(ctxt->userData);
                   3653:        }
                   3654:        ctxt->instate = XML_PARSER_EOF;
                   3655:     }
                   3656:     return((xmlParserErrors) ctxt->errNo);           
                   3657: }
                   3658: 
                   3659: /************************************************************************
                   3660:  *                                                                     *
                   3661:  *                     User entry points                               *
                   3662:  *                                                                     *
                   3663:  ************************************************************************/
                   3664: 
                   3665: /**
                   3666:  * htmlCreatePushParserCtxt :
                   3667:  * @sax:  a SAX handler
                   3668:  * @user_data:  The user data returned on SAX callbacks
                   3669:  * @chunk:  a pointer to an array of chars
                   3670:  * @size:  number of chars in the array
                   3671:  * @filename:  an optional file name or URI
                   3672:  * @enc:  an optional encoding
                   3673:  *
                   3674:  * Create a parser context for using the HTML parser in push mode
                   3675:  * To allow content encoding detection, @size should be >= 4
                   3676:  * The value of @filename is used for fetching external entities
                   3677:  * and error/warning reports.
                   3678:  *
                   3679:  * Returns the new parser context or NULL
                   3680:  */
                   3681: htmlParserCtxtPtr
                   3682: htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data, 
                   3683:                          const char *chunk, int size, const char *filename,
                   3684:                         xmlCharEncoding enc) {
                   3685:     htmlParserCtxtPtr ctxt;
                   3686:     htmlParserInputPtr inputStream;
                   3687:     xmlParserInputBufferPtr buf;
                   3688: 
                   3689:     buf = xmlAllocParserInputBuffer(enc);
                   3690:     if (buf == NULL) return(NULL);
                   3691: 
                   3692:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
                   3693:     if (ctxt == NULL) {
                   3694:        xmlFree(buf);
                   3695:        return(NULL);
                   3696:     }
                   3697:     memset(ctxt, 0, sizeof(htmlParserCtxt));
                   3698:     htmlInitParserCtxt(ctxt);
                   3699:     if (sax != NULL) {
                   3700:        if (ctxt->sax != &htmlDefaultSAXHandler)
                   3701:            xmlFree(ctxt->sax);
                   3702:        ctxt->sax = (htmlSAXHandlerPtr) xmlMalloc(sizeof(htmlSAXHandler));
                   3703:        if (ctxt->sax == NULL) {
                   3704:            xmlFree(buf);
                   3705:            xmlFree(ctxt);
                   3706:            return(NULL);
                   3707:        }
                   3708:        memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
                   3709:        if (user_data != NULL)
                   3710:            ctxt->userData = user_data;
                   3711:     }  
                   3712:     if (filename == NULL) {
                   3713:        ctxt->directory = NULL;
                   3714:     } else {
                   3715:         ctxt->directory = xmlParserGetDirectory(filename);
                   3716:     }
                   3717: 
                   3718:     inputStream = htmlNewInputStream(ctxt);
                   3719:     if (inputStream == NULL) {
                   3720:        xmlFreeParserCtxt(ctxt);
                   3721:        return(NULL);
                   3722:     }
                   3723: 
                   3724:     if (filename == NULL)
                   3725:        inputStream->filename = NULL;
                   3726:     else
                   3727:        inputStream->filename = xmlMemStrdup(filename);
                   3728:     inputStream->buf = buf;
                   3729:     inputStream->base = inputStream->buf->buffer->content;
                   3730:     inputStream->cur = inputStream->buf->buffer->content;
                   3731: 
                   3732:     inputPush(ctxt, inputStream);
                   3733: 
                   3734:     if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
                   3735:         (ctxt->input->buf != NULL))  {       
                   3736:        xmlParserInputBufferPush(ctxt->input->buf, size, chunk);              
                   3737: #ifdef DEBUG_PUSH
                   3738:        fprintf(stderr, "HPP: pushed %d\n", size);
                   3739: #endif
                   3740:     }
                   3741: 
                   3742:     return(ctxt);
                   3743: }
1.1       daniel   3744: 
                   3745: /**
                   3746:  * htmlSAXParseDoc :
1.14      daniel   3747:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   3748:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3749:  * @sax:  the SAX handler block
                   3750:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   3751:  *
                   3752:  * parse an HTML in-memory document and build a tree.
                   3753:  * It use the given SAX function block to handle the parsing callback.
                   3754:  * If sax is NULL, fallback to the default DOM tree building routines.
                   3755:  * 
                   3756:  * Returns the resulting document tree
                   3757:  */
                   3758: 
                   3759: htmlDocPtr
1.14      daniel   3760: htmlSAXParseDoc(xmlChar *cur, const char *encoding, htmlSAXHandlerPtr sax, void *userData) {
1.1       daniel   3761:     htmlDocPtr ret;
                   3762:     htmlParserCtxtPtr ctxt;
                   3763: 
                   3764:     if (cur == NULL) return(NULL);
                   3765: 
                   3766: 
                   3767:     ctxt = htmlCreateDocParserCtxt(cur, encoding);
                   3768:     if (ctxt == NULL) return(NULL);
                   3769:     if (sax != NULL) { 
                   3770:         ctxt->sax = sax;
                   3771:         ctxt->userData = userData;
                   3772:     }
                   3773: 
                   3774:     htmlParseDocument(ctxt);
                   3775:     ret = ctxt->myDoc;
                   3776:     if (sax != NULL) {
                   3777:        ctxt->sax = NULL;
                   3778:        ctxt->userData = NULL;
                   3779:     }
                   3780:     htmlFreeParserCtxt(ctxt);
                   3781:     
                   3782:     return(ret);
                   3783: }
                   3784: 
                   3785: /**
                   3786:  * htmlParseDoc :
1.14      daniel   3787:  * @cur:  a pointer to an array of xmlChar
1.1       daniel   3788:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3789:  *
                   3790:  * parse an HTML in-memory document and build a tree.
                   3791:  * 
                   3792:  * Returns the resulting document tree
                   3793:  */
                   3794: 
                   3795: htmlDocPtr
1.14      daniel   3796: htmlParseDoc(xmlChar *cur, const char *encoding) {
1.1       daniel   3797:     return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
                   3798: }
                   3799: 
                   3800: 
                   3801: /**
                   3802:  * htmlCreateFileParserCtxt :
                   3803:  * @filename:  the filename
                   3804:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3805:  *
                   3806:  * Create a parser context for a file content. 
                   3807:  * Automatic support for ZLIB/Compress compressed document is provided
                   3808:  * by default if found at compile-time.
                   3809:  *
                   3810:  * Returns the new parser context or NULL
                   3811:  */
                   3812: htmlParserCtxtPtr
                   3813: htmlCreateFileParserCtxt(const char *filename, const char *encoding)
                   3814: {
                   3815:     htmlParserCtxtPtr ctxt;
                   3816:     htmlParserInputPtr inputStream;
1.5       daniel   3817:     xmlParserInputBufferPtr buf;
1.1       daniel   3818:     /* htmlCharEncoding enc; */
                   3819: 
1.5       daniel   3820:     buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
                   3821:     if (buf == NULL) return(NULL);
1.1       daniel   3822: 
1.11      daniel   3823:     ctxt = (htmlParserCtxtPtr) xmlMalloc(sizeof(htmlParserCtxt));
1.1       daniel   3824:     if (ctxt == NULL) {
                   3825:         perror("malloc");
                   3826:        return(NULL);
                   3827:     }
1.19      daniel   3828:     memset(ctxt, 0, sizeof(htmlParserCtxt));
1.1       daniel   3829:     htmlInitParserCtxt(ctxt);
1.11      daniel   3830:     inputStream = (htmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
1.1       daniel   3831:     if (inputStream == NULL) {
                   3832:         perror("malloc");
1.11      daniel   3833:        xmlFree(ctxt);
1.1       daniel   3834:        return(NULL);
                   3835:     }
1.19      daniel   3836:     memset(inputStream, 0, sizeof(htmlParserInput));
1.1       daniel   3837: 
1.11      daniel   3838:     inputStream->filename = xmlMemStrdup(filename);
1.1       daniel   3839:     inputStream->line = 1;
                   3840:     inputStream->col = 1;
1.5       daniel   3841:     inputStream->buf = buf;
1.21      daniel   3842:     inputStream->directory = NULL;
1.1       daniel   3843: 
1.5       daniel   3844:     inputStream->base = inputStream->buf->buffer->content;
                   3845:     inputStream->cur = inputStream->buf->buffer->content;
                   3846:     inputStream->free = NULL;
1.1       daniel   3847: 
                   3848:     inputPush(ctxt, inputStream);
                   3849:     return(ctxt);
                   3850: }
                   3851: 
                   3852: /**
                   3853:  * htmlSAXParseFile :
                   3854:  * @filename:  the filename
                   3855:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3856:  * @sax:  the SAX handler block
                   3857:  * @userData: if using SAX, this pointer will be provided on callbacks. 
                   3858:  *
                   3859:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   3860:  * compressed document is provided by default if found at compile-time.
                   3861:  * It use the given SAX function block to handle the parsing callback.
                   3862:  * If sax is NULL, fallback to the default DOM tree building routines.
                   3863:  *
                   3864:  * Returns the resulting document tree
                   3865:  */
                   3866: 
                   3867: htmlDocPtr
                   3868: htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax, 
                   3869:                  void *userData) {
                   3870:     htmlDocPtr ret;
                   3871:     htmlParserCtxtPtr ctxt;
                   3872: 
                   3873:     ctxt = htmlCreateFileParserCtxt(filename, encoding);
                   3874:     if (ctxt == NULL) return(NULL);
                   3875:     if (sax != NULL) {
                   3876:         ctxt->sax = sax;
                   3877:         ctxt->userData = userData;
                   3878:     }
                   3879: 
                   3880:     htmlParseDocument(ctxt);
                   3881: 
                   3882:     ret = ctxt->myDoc;
                   3883:     if (sax != NULL) {
                   3884:         ctxt->sax = NULL;
                   3885:         ctxt->userData = NULL;
                   3886:     }
                   3887:     htmlFreeParserCtxt(ctxt);
                   3888:     
                   3889:     return(ret);
                   3890: }
                   3891: 
                   3892: /**
                   3893:  * htmlParseFile :
                   3894:  * @filename:  the filename
                   3895:  * @encoding:  a free form C string describing the HTML document encoding, or NULL
                   3896:  *
                   3897:  * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
                   3898:  * compressed document is provided by default if found at compile-time.
                   3899:  *
                   3900:  * Returns the resulting document tree
                   3901:  */
                   3902: 
                   3903: htmlDocPtr
                   3904: htmlParseFile(const char *filename, const char *encoding) {
                   3905:     return(htmlSAXParseFile(filename, encoding, NULL, NULL));
                   3906: }
1.39      daniel   3907: 
                   3908: #endif /* LIBXML_HTML_ENABLED */

Webmaster