DOCBparser.c revision 3487c8d9bbf0c2d54effc1cd7566c2ececb18752
1/*
2 * DOCBparser.c : an attempt to parse SGML Docbook documents
3 *
4 * This is extremely hackish. It also adds one extension
5 *    <?sgml-declaration encoding="ISO-8859-1"?>
6 * allowing to store the encoding of the document within the instance.
7 *
8 * See Copyright for the status of this software.
9 *
10 * daniel@veillard.com
11 */
12
13#define IN_LIBXML
14#include "libxml.h"
15#ifdef LIBXML_DOCB_ENABLED
16
17#include <string.h>
18#ifdef HAVE_CTYPE_H
19#include <ctype.h>
20#endif
21#ifdef HAVE_STDLIB_H
22#include <stdlib.h>
23#endif
24#ifdef HAVE_SYS_STAT_H
25#include <sys/stat.h>
26#endif
27#ifdef HAVE_FCNTL_H
28#include <fcntl.h>
29#endif
30#ifdef HAVE_UNISTD_H
31#include <unistd.h>
32#endif
33#ifdef HAVE_ZLIB_H
34#include <zlib.h>
35#endif
36
37#include <libxml/xmlmemory.h>
38#include <libxml/tree.h>
39#include <libxml/SAX.h>
40#include <libxml/parser.h>
41#include <libxml/parserInternals.h>
42#include <libxml/xmlerror.h>
43#include <libxml/DOCBparser.h>
44#include <libxml/entities.h>
45#include <libxml/encoding.h>
46#include <libxml/valid.h>
47#include <libxml/xmlIO.h>
48#include <libxml/uri.h>
49#include <libxml/globals.h>
50
51/*
52 * DocBook XML current versions
53 */
54
55#define XML_DOCBOOK_XML_PUBLIC (const xmlChar *)			\
56             "-//OASIS//DTD DocBook XML V4.1.2//EN"
57#define XML_DOCBOOK_XML_SYSTEM (const xmlChar *)			\
58             "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"
59
60/*
61 * Internal description of an SGML entity
62 */
63typedef struct _docbEntityDesc docbEntityDesc;
64typedef docbEntityDesc *docbEntityDescPtr;
65struct _docbEntityDesc {
66    int value;         /* the UNICODE value for the character */
67    const char *name;  /* The entity name */
68    const char *desc;   /* the description */
69};
70
71#if 0
72docbElemDescPtr        docbTagLookup   (const xmlChar *tag);
73docbEntityDescPtr      docbEntityLookup(const xmlChar *name);
74docbEntityDescPtr      docbEntityValueLookup(int value);
75
76int                    docbIsAutoClosed(docbDocPtr doc,
77                                        docbNodePtr elem);
78int                    docbAutoCloseTag(docbDocPtr doc,
79                                        const xmlChar *name,
80                                        docbNodePtr elem);
81
82#endif
83static int             docbParseCharRef(docbParserCtxtPtr ctxt);
84static xmlEntityPtr    docbParseEntityRef(docbParserCtxtPtr ctxt,
85                                        xmlChar **str);
86static void            docbParseElement(docbParserCtxtPtr ctxt);
87static void            docbParseContent(docbParserCtxtPtr ctxt);
88
89/*
90 * Internal description of an SGML element
91 */
92typedef struct _docbElemDesc docbElemDesc;
93typedef docbElemDesc *docbElemDescPtr;
94struct _docbElemDesc {
95    const char *name;  /* The tag name */
96    int startTag;       /* Whether the start tag can be implied */
97    int endTag;         /* Whether the end tag can be implied */
98    int empty;          /* Is this an empty element ? */
99    int depr;           /* Is this a deprecated element ? */
100    int dtd;            /* 1: only in Loose DTD, 2: only Frameset one */
101    const char *desc;   /* the description */
102};
103
104
105#define DOCB_MAX_NAMELEN 1000
106#define DOCB_PARSER_BIG_BUFFER_SIZE 1000
107#define DOCB_PARSER_BUFFER_SIZE 100
108
109/* #define DEBUG */
110/* #define DEBUG_PUSH */
111
112/************************************************************************
113 *                                                                     *
114 *             Parser stacks related functions and macros              *
115 *                                                                     *
116 ************************************************************************/
117
118/*
119 * Generic function for accessing stacks in the Parser Context
120 */
121
122#define PUSH_AND_POP(scope, type, name)                                \
123scope int docb##name##Push(docbParserCtxtPtr ctxt, type value) {       \
124    if (ctxt->name##Nr >= ctxt->name##Max) {                           \
125       ctxt->name##Max *= 2;                                           \
126        ctxt->name##Tab = (type *) xmlRealloc(ctxt->name##Tab,         \
127                    ctxt->name##Max * sizeof(ctxt->name##Tab[0]));     \
128        if (ctxt->name##Tab == NULL) {                                 \
129           xmlGenericError(xmlGenericErrorContext,                     \
130		           "realloc failed !\n");                      \
131           return(0);                                                  \
132       }                                                               \
133    }                                                                  \
134    ctxt->name##Tab[ctxt->name##Nr] = value;                           \
135    ctxt->name = value;                                                \
136    return(ctxt->name##Nr++);                                          \
137}                                                                      \
138scope type docb##name##Pop(docbParserCtxtPtr ctxt) {                   \
139    type ret;                                                          \
140    if (ctxt->name##Nr < 0) return(0);                                 \
141    ctxt->name##Nr--;                                                  \
142    if (ctxt->name##Nr < 0) return(0);                                 \
143    if (ctxt->name##Nr > 0)                                            \
144       ctxt->name = ctxt->name##Tab[ctxt->name##Nr - 1];               \
145    else                                                               \
146        ctxt->name = NULL;                                             \
147    ret = ctxt->name##Tab[ctxt->name##Nr];                             \
148    ctxt->name##Tab[ctxt->name##Nr] = 0;                               \
149    return(ret);                                                       \
150}                                                                      \
151
152/* PUSH_AND_POP(static, xmlNodePtr, node) */
153PUSH_AND_POP(static, xmlChar*, name)
154
155/*
156 * Macros for accessing the content. Those should be used only by the parser,
157 * and not exported.
158 *
159 * Dirty macros, i.e. one need to make assumption on the context to use them
160 *
161 *   CUR_PTR return the current pointer to the xmlChar to be parsed.
162 *   CUR     returns the current xmlChar value, i.e. a 8 bit value if compiled
163 *           in ISO-Latin or UTF-8, and the current 16 bit value if compiled
164 *           in UNICODE mode. This should be used internally by the parser
165 *           only to compare to ASCII values otherwise it would break when
166 *           running with UTF-8 encoding.
167 *   NXT(n)  returns the n'th next xmlChar. Same as CUR is should be used only
168 *           to compare on ASCII based substring.
169 *   UPP(n)  returns the n'th next xmlChar converted to uppercase. Same as CUR
170 *           it should be used only to compare on ASCII based substring.
171 *   SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
172 *           strings within the parser.
173 *
174 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
175 *
176 *   CURRENT Returns the current char value, with the full decoding of
177 *           UTF-8 if we are using this mode. It returns an int.
178 *   NEXT    Skip to the next character, this does the proper decoding
179 *           in UTF-8 mode. It also pop-up unfinished entities on the fly.
180 *   COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
181 */
182
183#define UPPER (toupper(*ctxt->input->cur))
184
185#define SKIP(val) ctxt->nbChars += (val),ctxt->input->cur += (val)
186
187#define NXT(val) ctxt->input->cur[(val)]
188
189#define UPP(val) (toupper(ctxt->input->cur[(val)]))
190
191#define CUR_PTR ctxt->input->cur
192
193#define SHRINK  xmlParserInputShrink(ctxt->input)
194
195#define GROW  xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
196
197#define CURRENT ((int) (*ctxt->input->cur))
198
199#define SKIP_BLANKS docbSkipBlankChars(ctxt)
200
201/* Imported from XML */
202
203/* #define CUR (ctxt->token ? ctxt->token : (int) (*ctxt->input->cur)) */
204#define CUR ((int) (*ctxt->input->cur))
205#define NEXT xmlNextChar(ctxt),ctxt->nbChars++
206
207#define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
208#define NXT(val) ctxt->input->cur[(val)]
209#define CUR_PTR ctxt->input->cur
210
211
212#define NEXTL(l) do {                                                  \
213    if (*(ctxt->input->cur) == '\n') {                                 \
214       ctxt->input->line++; ctxt->input->col = 1;                      \
215    } else ctxt->input->col++;                                         \
216    ctxt->token = 0; ctxt->input->cur += l; ctxt->nbChars++;           \
217  } while (0)
218
219/************
220    \
221    if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);    \
222    if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
223 ************/
224
225#define CUR_CHAR(l) docbCurrentChar(ctxt, &l)
226#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
227
228#define COPY_BUF(l,b,i,v)                                              \
229    if (l == 1) b[i++] = (xmlChar) v;                                  \
230    else i += xmlCopyChar(l,&b[i],v)
231
232/**
233 * docbCurrentChar:
234 * @ctxt:  the DocBook SGML parser context
235 * @len:  pointer to the length of the char read
236 *
237 * The current char value, if using UTF-8 this may actually span multiple
238 * bytes in the input buffer. Implement the end of line normalization:
239 * 2.11 End-of-Line Handling
240 * If the encoding is unspecified, in the case we find an ISO-Latin-1
241 * char, then the encoding converter is plugged in automatically.
242 *
243 * Returns the current char value and its length
244 */
245
246static int
247docbCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
248    if (ctxt->instate == XML_PARSER_EOF)
249       return(0);
250
251    if (ctxt->token != 0) {
252       *len = 0;
253       return(ctxt->token);
254    }
255    if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
256       /*
257        * We are supposed to handle UTF8, check it's valid
258        * From rfc2044: encoding of the Unicode values on UTF-8:
259        *
260        * UCS-4 range (hex.)           UTF-8 octet sequence (binary)
261        * 0000 0000-0000 007F   0xxxxxxx
262        * 0000 0080-0000 07FF   110xxxxx 10xxxxxx
263        * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx
264        *
265        * Check for the 0x110000 limit too
266        */
267       const unsigned char *cur = ctxt->input->cur;
268       unsigned char c;
269       unsigned int val;
270
271       c = *cur;
272       if (c & 0x80) {
273           if (cur[1] == 0)
274               xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
275           if ((cur[1] & 0xc0) != 0x80)
276               goto encoding_error;
277           if ((c & 0xe0) == 0xe0) {
278
279               if (cur[2] == 0)
280                   xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
281               if ((cur[2] & 0xc0) != 0x80)
282                   goto encoding_error;
283               if ((c & 0xf0) == 0xf0) {
284                   if (cur[3] == 0)
285                       xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
286                   if (((c & 0xf8) != 0xf0) ||
287                       ((cur[3] & 0xc0) != 0x80))
288                       goto encoding_error;
289                   /* 4-byte code */
290                   *len = 4;
291                   val = (cur[0] & 0x7) << 18;
292                   val |= (cur[1] & 0x3f) << 12;
293                   val |= (cur[2] & 0x3f) << 6;
294                   val |= cur[3] & 0x3f;
295               } else {
296                 /* 3-byte code */
297                   *len = 3;
298                   val = (cur[0] & 0xf) << 12;
299                   val |= (cur[1] & 0x3f) << 6;
300                   val |= cur[2] & 0x3f;
301               }
302           } else {
303             /* 2-byte code */
304               *len = 2;
305               val = (cur[0] & 0x1f) << 6;
306               val |= cur[1] & 0x3f;
307           }
308           if (!IS_CHAR(val)) {
309               ctxt->errNo = XML_ERR_INVALID_ENCODING;
310               if ((ctxt->sax != NULL) &&
311                   (ctxt->sax->error != NULL))
312                   ctxt->sax->error(ctxt->userData,
313                                    "Char 0x%X out of allowed range\n", val);
314               ctxt->wellFormed = 0;
315               ctxt->disableSAX = 1;
316           }
317           return(val);
318       } else {
319           /* 1-byte code */
320           *len = 1;
321           return((int) *ctxt->input->cur);
322       }
323    }
324    /*
325     * Assume it's a fixed length encoding (1) with
326     * a compatible encoding for the ASCII set, since
327     * XML constructs only use < 128 chars
328     */
329    *len = 1;
330    if ((int) *ctxt->input->cur < 0x80)
331       return((int) *ctxt->input->cur);
332
333    /*
334     * Humm this is bad, do an automatic flow conversion
335     */
336    xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
337    ctxt->charset = XML_CHAR_ENCODING_UTF8;
338    return(xmlCurrentChar(ctxt, len));
339
340encoding_error:
341    /*
342     * If we detect an UTF8 error that probably mean that the
343     * input encoding didn't get properly advertized in the
344     * declaration header. Report the error and switch the encoding
345     * to ISO-Latin-1 (if you don't like this policy, just declare the
346     * encoding !)
347     */
348    ctxt->errNo = XML_ERR_INVALID_ENCODING;
349    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
350       ctxt->sax->error(ctxt->userData,
351                        "Input is not proper UTF-8, indicate encoding !\n");
352       ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
353                       ctxt->input->cur[0], ctxt->input->cur[1],
354                       ctxt->input->cur[2], ctxt->input->cur[3]);
355    }
356
357    ctxt->charset = XML_CHAR_ENCODING_8859_1;
358    *len = 1;
359    return((int) *ctxt->input->cur);
360}
361
362#if 0
363/**
364 * sgmlNextChar:
365 * @ctxt:  the DocBook SGML parser context
366 *
367 * Skip to the next char input char.
368 */
369
370static void
371sgmlNextChar(docbParserCtxtPtr ctxt) {
372    if (ctxt->instate == XML_PARSER_EOF)
373       return;
374    if ((*ctxt->input->cur == 0) &&
375        (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
376           xmlPopInput(ctxt);
377    } else {
378        if (*(ctxt->input->cur) == '\n') {
379           ctxt->input->line++; ctxt->input->col = 1;
380       } else ctxt->input->col++;
381       ctxt->input->cur++;
382       ctxt->nbChars++;
383        if (*ctxt->input->cur == 0)
384           xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
385    }
386}
387#endif
388
389/**
390 * docbSkipBlankChars:
391 * @ctxt:  the DocBook SGML parser context
392 *
393 * skip all blanks character found at that point in the input streams.
394 *
395 * Returns the number of space chars skipped
396 */
397
398static int
399docbSkipBlankChars(xmlParserCtxtPtr ctxt) {
400    int res = 0;
401
402    while (IS_BLANK(*(ctxt->input->cur))) {
403       if ((*ctxt->input->cur == 0) &&
404           (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
405               xmlPopInput(ctxt);
406       } else {
407           if (*(ctxt->input->cur) == '\n') {
408               ctxt->input->line++; ctxt->input->col = 1;
409           } else ctxt->input->col++;
410           ctxt->input->cur++;
411           ctxt->nbChars++;
412           if (*ctxt->input->cur == 0)
413               xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
414       }
415       res++;
416    }
417    return(res);
418}
419
420
421
422/************************************************************************
423 *                                                                     *
424 *             The list of SGML elements and their properties          *
425 *                                                                     *
426 ************************************************************************/
427
428/*
429 *  Start Tag: 1 means the start tag can be ommited
430 *  End Tag:   1 means the end tag can be ommited
431 *             2 means it's forbidden (empty elements)
432 *  Depr:      this element is deprecated
433 *  DTD:       1 means that this element is valid only in the Loose DTD
434 *             2 means that this element is valid only in the Frameset DTD
435 *
436 * Name,Start Tag,End Tag,  Empty,  Depr.,    DTD, Description
437 */
438static docbElemDesc
439docbookElementTable[] = {
440{ "abbrev",    0,      0,      0,      3,      0, "" }, /* word */
441{ "abstract",  0,      0,      0,      9,      0, "" }, /* title */
442{ "accel",     0,      0,      0,      7,      0, "" }, /* smallcptr */
443{ "ackno",     0,      0,      0,      4,      0, "" }, /* docinfo */
444{ "acronym",   0,      0,      0,      3,      0, "" }, /* word */
445{ "action",    0,      0,      0,      7,      0, "" }, /* smallcptr */
446{ "address",   0,      0,      0,      1,      0, "" },
447{ "affiliation",0,     0,      0,      9,      0, "" }, /* shortaffil */
448{ "alt",       0,      0,      0,      1,      0, "" },
449{ "anchor",    0,      2,      1,      0,      0, "" },
450{ "answer",    0,      0,      0,      9,      0, "" }, /* label */
451{ "appendix",  0,      0,      0,      9,      0, "" }, /* appendixinfo */
452{ "appendixinfo",0,    0,      0,      9,      0, "" }, /* graphic */
453{ "application",0,     0,      0,      2,      0, "" }, /* para */
454{ "area",      0,      2,      1,      0,      0, "" },
455{ "areaset",   0,      0,      0,      9,      0, "" }, /* area */
456{ "areaspec",  0,      0,      0,      9,      0, "" }, /* area */
457{ "arg",       0,      0,      0,      1,      0, "" },
458{ "artheader", 0,      0,      0,      9,      0, "" },
459{ "article",   0,      0,      0,      9,      0, "" }, /* div.title.content */
460{ "articleinfo",0,     0,      0,      9,      0, "" }, /* graphic */
461{ "artpagenums",0,     0,      0,      4,      0, "" }, /* docinfo */
462{ "attribution",0,     0,      0,      2,      0, "" }, /* para */
463{ "audiodata", 0,      2,      1,      0,      0, "" },
464{ "audioobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
465{ "authorblurb",0,     0,      0,      9,      0, "" }, /* title */
466{ "authorgroup",0,     0,      0,      9,      0, "" }, /* author */
467{ "authorinitials",0,  0,      0,      4,      0, "" }, /* docinfo */
468{ "author",    0,      0,      0,      9,      0, "" }, /* person.ident.mix */
469{ "beginpage", 0,      2,      1,      0,      0, "" },
470{ "bibliodiv", 0,      0,      0,      9,      0, "" }, /* sect.title.content */
471{ "biblioentry",0,     0,      0,      9,      0, "" }, /* articleinfo */
472{ "bibliography",0,    0,      0,      9,      0, "" }, /* bibliographyinfo */
473{ "bibliographyinfo",0,        0,      0,      9,      0, "" }, /* graphic */
474{ "bibliomisc",        0,      0,      0,      2,      0, "" }, /* para */
475{ "bibliomixed",0,     0,      0,      1,      0, "" }, /* %bibliocomponent.mix, bibliomset) */
476{ "bibliomset",        0,      0,      0,      1,      0, "" }, /* %bibliocomponent.mix; | bibliomset) */
477{ "biblioset", 0,      0,      0,      9,      0, "" }, /* bibliocomponent.mix */
478{ "blockquote",        0,      0,      0,      9,      0, "" }, /* title */
479{ "book",      0,      0,      0,      9,      0, "" }, /* div.title.content */
480{ "bookinfo",  0,      0,      0,      9,      0, "" }, /* graphic */
481{ "bridgehead",        0,      0,      0,      8,      0, "" }, /* title */
482{ "callout",   0,      0,      0,      9,      0, "" }, /* component.mix */
483{ "calloutlist",0,     0,      0,      9,      0, "" }, /* formalobject.title.content */
484{ "caption",   0,      0,      0,      9,      0, "" }, /* textobject.mix */
485{ "caution",   0,      0,      0,      9,      0, "" }, /* title */
486{ "chapter",   0,      0,      0,      9,      0, "" }, /* chapterinfo */
487{ "chapterinfo",0,     0,      0,      9,      0, "" }, /* graphic */
488{ "citation",  0,      0,      0,      2,      0, "" }, /* para */
489{ "citerefentry",0,    0,      0,      9,      0, "" }, /* refentrytitle */
490{ "citetitle", 0,      0,      0,      2,      0, "" }, /* para */
491{ "city",      0,      0,      0,      4,      0, "" }, /* docinfo */
492{ "classname", 0,      0,      0,      7,      0, "" }, /* smallcptr */
493{ "classsynopsisinfo",0,0,     0,      9,      0, "" }, /* cptr */
494{ "classsynopsis",0,   0,      0,      9,      0, "" }, /* ooclass */
495{ "cmdsynopsis",0,     0,      0,      9,      0, "" }, /* command */
496{ "co",                0,      2,      1,      0,      0, "" },
497{ "collab",    0,      0,      0,      9,      0, "" }, /* collabname */
498{ "collabname",        0,      0,      0,      4,      0, "" }, /* docinfo */
499{ "colophon",  0,      0,      0,      9,      0, "" }, /* sect.title.content */
500{ "colspec",   0,      2,      1,      0,      0, "" },
501{ "colspec",   0,      2,      1,      0,      0, "" },
502{ "command",   0,      0,      0,      9,      0, "" }, /* cptr */
503{ "computeroutput",0,  0,      0,      9,      0, "" }, /* cptr */
504{ "confdates", 0,      0,      0,      4,      0, "" }, /* docinfo */
505{ "confgroup", 0,      0,      0,      9,      0, "" }, /* confdates */
506{ "confnum",   0,      0,      0,      4,      0, "" }, /* docinfo */
507{ "confsponsor",0,     0,      0,      4,      0, "" }, /* docinfo */
508{ "conftitle", 0,      0,      0,      4,      0, "" }, /* docinfo */
509{ "constant",  0,      0,      0,      7,      0, "" }, /* smallcptr */
510{ "constructorsynopsis",0,0,   0,      9,      0, "" }, /* modifier */
511{ "contractnum",0,     0,      0,      4,      0, "" }, /* docinfo */
512{ "contractsponsor",0, 0,      0,      4,      0, "" }, /* docinfo */
513{ "contrib",   0,      0,      0,      4,      0, "" }, /* docinfo */
514{ "copyright", 0,      0,      0,      9,      0, "" }, /* year */
515{ "corpauthor",        0,      0,      0,      4,      0, "" }, /* docinfo */
516{ "corpname",  0,      0,      0,      4,      0, "" }, /* docinfo */
517{ "country",   0,      0,      0,      4,      0, "" }, /* docinfo */
518{ "database",  0,      0,      0,      7,      0, "" }, /* smallcptr */
519{ "date",      0,      0,      0,      4,      0, "" }, /* docinfo */
520{ "dedication",        0,      0,      0,      9,      0, "" }, /* sect.title.content */
521{ "destructorsynopsis",0,0,    0,      9,      0, "" }, /* modifier */
522{ "docinfo",   0,      0,      0,      9,      0, "" },
523{ "edition",   0,      0,      0,      4,      0, "" }, /* docinfo */
524{ "editor",    0,      0,      0,      9,      0, "" }, /* person.ident.mix */
525{ "email",     0,      0,      0,      4,      0, "" }, /* docinfo */
526{ "emphasis",  0,      0,      0,      2,      0, "" }, /* para */
527{ "entry",     0,      0,      0,      9,      0, "" }, /* tbl.entry.mdl */
528{ "entrytbl",  0,      0,      0,      9,      0, "" }, /* tbl.entrytbl.mdl */
529{ "envar",     0,      0,      0,      7,      0, "" }, /* smallcptr */
530{ "epigraph",  0,      0,      0,      9,      0, "" }, /* attribution */
531{ "equation",  0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
532{ "errorcode", 0,      0,      0,      7,      0, "" }, /* smallcptr */
533{ "errorname", 0,      0,      0,      7,      0, "" }, /* smallcptr */
534{ "errortype", 0,      0,      0,      7,      0, "" }, /* smallcptr */
535{ "example",   0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
536{ "exceptionname",0,   0,      0,      7,      0, "" }, /* smallcptr */
537{ "fax",       0,      0,      0,      4,      0, "" }, /* docinfo */
538{ "fieldsynopsis",     0,      0,      0,      9,      0, "" }, /* modifier */
539{ "figure",    0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
540{ "filename",  0,      0,      0,      7,      0, "" }, /* smallcptr */
541{ "firstname", 0,      0,      0,      4,      0, "" }, /* docinfo */
542{ "firstterm", 0,      0,      0,      3,      0, "" }, /* word */
543{ "footnote",  0,      0,      0,      9,      0, "" }, /* footnote.mix */
544{ "footnoteref",0,     2,      1,      0,      0, "" },
545{ "foreignphrase",0,   0,      0,      2,      0, "" }, /* para */
546{ "formalpara",        0,      0,      0,      9,      0, "" }, /* title */
547{ "funcdef",   0,      0,      0,      1,      0, "" },
548{ "funcparams",        0,      0,      0,      9,      0, "" }, /* cptr */
549{ "funcprototype",0,   0,      0,      9,      0, "" }, /* funcdef */
550{ "funcsynopsis",0,    0,      0,      9,      0, "" }, /* funcsynopsisinfo */
551{ "funcsynopsisinfo",  0,      0,      0,      9,      0, "" }, /* cptr */
552{ "function",  0,      0,      0,      9,      0, "" }, /* cptr */
553{ "glossary",  0,      0,      0,      9,      0, "" }, /* glossaryinfo */
554{ "glossaryinfo",0,    0,      0,      9,      0, "" }, /* graphic */
555{ "glossdef",  0,      0,      0,      9,      0, "" }, /* glossdef.mix */
556{ "glossdiv",  0,      0,      0,      9,      0, "" }, /* sect.title.content */
557{ "glossentry",        0,      0,      0,      9,      0, "" }, /* glossterm */
558{ "glosslist", 0,      0,      0,      9,      0, "" }, /* glossentry */
559{ "glossseealso",0,    0,      1,      2,      0, "" }, /* para */
560{ "glosssee",  0,      0,      1,      2,      0, "" }, /* para */
561{ "glossterm", 0,      0,      0,      2,      0, "" }, /* para */
562{ "graphic",   0,      0,      0,      9,      0, "" },
563{ "graphicco", 0,      0,      0,      9,      0, "" }, /* areaspec */
564{ "group",     0,      0,      0,      9,      0, "" }, /* arg */
565{ "guibutton", 0,      0,      0,      7,      0, "" }, /* smallcptr */
566{ "guiicon",   0,      0,      0,      7,      0, "" }, /* smallcptr */
567{ "guilabel",  0,      0,      0,      7,      0, "" }, /* smallcptr */
568{ "guimenuitem",0,     0,      0,      7,      0, "" }, /* smallcptr */
569{ "guimenu",   0,      0,      0,      7,      0, "" }, /* smallcptr */
570{ "guisubmenu",        0,      0,      0,      7,      0, "" }, /* smallcptr */
571{ "hardware",  0,      0,      0,      7,      0, "" }, /* smallcptr */
572{ "highlights",        0,      0,      0,      9,      0, "" }, /* highlights.mix */
573{ "holder",    0,      0,      0,      4,      0, "" }, /* docinfo */
574{ "honorific", 0,      0,      0,      4,      0, "" }, /* docinfo */
575{ "imagedata", 0,      2,      1,      0,      0, "" },
576{ "imageobjectco",0,   0,      0,      9,      0, "" }, /* areaspec */
577{ "imageobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
578{ "important", 0,      0,      0,      9,      0, "" }, /* title */
579{ "indexdiv",  0,      0,      0,      9,      0, "" }, /* sect.title.content */
580{ "indexentry",        0,      0,      0,      9,      0, "" }, /* primaryie */
581{ "index",     0,      0,      0,      9,      0, "" }, /* indexinfo */
582{ "indexinfo", 0,      0,      0,      9,      0, "" }, /* graphic */
583{ "indexterm", 0,      0,      0,      9,      0, "" }, /* primary */
584{ "informalequation",0,        0,      0,      9,      0, "" }, /* equation.content */
585{ "informalexample",0, 0,      0,      9,      0, "" }, /* example.mix */
586{ "informalfigure",0,  0,      0,      9,      0, "" }, /* figure.mix */
587{ "informaltable",0,   0,      0,      9,      0, "" }, /* graphic */
588{ "initializer",0,     0,      0,      7,      0, "" }, /* smallcptr */
589{ "inlineequation",0,  0,      0,      9,      0, "" }, /* inlineequation.content */
590{ "inlinegraphic",0,   0,      0,      9,      0, "" },
591{ "inlinemediaobject",0,0,     0,      9,      0, "" }, /* objectinfo */
592{ "interfacename",0,   0,      0,      7,      0, "" }, /* smallcptr */
593{ "interface", 0,      0,      0,      7,      0, "" }, /* smallcptr */
594{ "invpartnumber",0,   0,      0,      4,      0, "" }, /* docinfo */
595{ "isbn",      0,      0,      0,      4,      0, "" }, /* docinfo */
596{ "issn",      0,      0,      0,      4,      0, "" }, /* docinfo */
597{ "issuenum",  0,      0,      0,      4,      0, "" }, /* docinfo */
598{ "itemizedlist",0,    0,      0,      9,      0, "" }, /* formalobject.title.content */
599{ "itermset",  0,      0,      0,      9,      0, "" }, /* indexterm */
600{ "jobtitle",  0,      0,      0,      4,      0, "" }, /* docinfo */
601{ "keycap",    0,      0,      0,      7,      0, "" }, /* smallcptr */
602{ "keycode",   0,      0,      0,      7,      0, "" }, /* smallcptr */
603{ "keycombo",  0,      0,      0,      9,      0, "" }, /* keycap */
604{ "keysym",    0,      0,      0,      7,      0, "" }, /* smallcptr */
605{ "keyword",   0,      0,      0,      1,      0, "" },
606{ "keywordset",        0,      0,      0,      9,      0, "" }, /* keyword */
607{ "label",     0,      0,      0,      3,      0, "" }, /* word */
608{ "legalnotice",0,     0,      0,      9,      0, "" }, /* title */
609{ "lineage",   0,      0,      0,      4,      0, "" }, /* docinfo */
610{ "lineannotation",0,  0,      0,      2,      0, "" }, /* para */
611{ "link",      0,      0,      0,      2,      0, "" }, /* para */
612{ "listitem",  0,      0,      0,      9,      0, "" }, /* component.mix */
613{ "literal",   0,      0,      0,      9,      0, "" }, /* cptr */
614{ "literallayout",0,   0,      0,      2,      0, "" }, /* para */
615{ "lot",       0,      0,      0,      9,      0, "" }, /* bookcomponent.title.content */
616{ "lotentry",  0,      0,      0,      2,      0, "" }, /* para */
617{ "manvolnum", 0,      0,      0,      3,      0, "" }, /* word */
618{ "markup",    0,      0,      0,      7,      0, "" }, /* smallcptr */
619{ "medialabel",        0,      0,      0,      7,      0, "" }, /* smallcptr */
620{ "mediaobjectco",0,   0,      0,      9,      0, "" }, /* objectinfo */
621{ "mediaobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
622{ "member",    0,      0,      0,      2,      0, "" }, /* para */
623{ "menuchoice",        0,      0,      0,      9,      0, "" }, /* shortcut */
624{ "methodname",        0,      0,      0,      7,      0, "" }, /* smallcptr */
625{ "methodparam",0,     0,      0,      9,      0, "" }, /* modifier */
626{ "methodsynopsis",0,  0,      0,      9,      0, "" }, /* modifier */
627{ "modespec",  0,      0,      0,      4,      0, "" }, /* docinfo */
628{ "modifier",  0,      0,      0,      7,      0, "" }, /* smallcptr */
629{ "mousebutton",0,     0,      0,      7,      0, "" }, /* smallcptr */
630{ "msgaud",    0,      0,      0,      2,      0, "" }, /* para */
631{ "msgentry",  0,      0,      0,      9,      0, "" }, /* msg */
632{ "msgexplan", 0,      0,      0,      9,      0, "" }, /* title */
633{ "msginfo",   0,      0,      0,      9,      0, "" }, /* msglevel */
634{ "msglevel",  0,      0,      0,      7,      0, "" }, /* smallcptr */
635{ "msgmain",   0,      0,      0,      9,      0, "" }, /* title */
636{ "msgorig",   0,      0,      0,      7,      0, "" }, /* smallcptr */
637{ "msgrel",    0,      0,      0,      9,      0, "" }, /* title */
638{ "msgset",    0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
639{ "msgsub",    0,      0,      0,      9,      0, "" }, /* title */
640{ "msgtext",   0,      0,      0,      9,      0, "" }, /* component.mix */
641{ "msg",       0,      0,      0,      9,      0, "" }, /* title */
642{ "note",      0,      0,      0,      9,      0, "" }, /* title */
643{ "objectinfo",        0,      0,      0,      9,      0, "" }, /* graphic */
644{ "olink",     0,      0,      0,      2,      0, "" }, /* para */
645{ "ooclass",   0,      0,      0,      9,      0, "" }, /* modifier */
646{ "ooexception",0,     0,      0,      9,      0, "" }, /* modifier */
647{ "oointerface",0,     0,      0,      9,      0, "" }, /* modifier */
648{ "optional",  0,      0,      0,      9,      0, "" }, /* cptr */
649{ "option",    0,      0,      0,      7,      0, "" }, /* smallcptr */
650{ "orderedlist",0,     0,      0,      9,      0, "" }, /* formalobject.title.content */
651{ "orgdiv",    0,      0,      0,      4,      0, "" }, /* docinfo */
652{ "orgname",   0,      0,      0,      4,      0, "" }, /* docinfo */
653{ "otheraddr", 0,      0,      0,      4,      0, "" }, /* docinfo */
654{ "othercredit",0,     0,      0,      9,      0, "" }, /* person.ident.mix */
655{ "othername", 0,      0,      0,      4,      0, "" }, /* docinfo */
656{ "pagenums",  0,      0,      0,      4,      0, "" }, /* docinfo */
657{ "paramdef",  0,      0,      0,      1,      0, "" },
658{ "parameter", 0,      0,      0,      7,      0, "" }, /* smallcptr */
659{ "para",      0,      0,      0,      2,      0, "" }, /* para */
660{ "partinfo",  0,      0,      0,      9,      0, "" }, /* graphic */
661{ "partintro", 0,      0,      0,      9,      0, "" }, /* div.title.content */
662{ "part",      0,      0,      0,      9,      0, "" }, /* partinfo */
663{ "phone",     0,      0,      0,      4,      0, "" }, /* docinfo */
664{ "phrase",    0,      0,      0,      2,      0, "" }, /* para */
665{ "pob",       0,      0,      0,      4,      0, "" }, /* docinfo */
666{ "postcode",  0,      0,      0,      4,      0, "" }, /* docinfo */
667{ "prefaceinfo",0,     0,      0,      9,      0, "" }, /* graphic */
668{ "preface",   0,      0,      0,      9,      0, "" }, /* prefaceinfo */
669{ "primaryie", 0,      0,      0,      4,      0, "" }, /* ndxterm */
670{ "primary", 0,      0,      0,      9,      0, "" }, /* ndxterm */
671{ "printhistory",0,    0,      0,      9,      0, "" }, /* para.class */
672{ "procedure", 0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
673{ "productname",0,     0,      0,      2,      0, "" }, /* para */
674{ "productnumber",0,   0,      0,      4,      0, "" }, /* docinfo */
675{ "programlistingco",0,        0,      0,      9,      0, "" }, /* areaspec */
676{ "programlisting",0,  0,      0,      2,      0, "" }, /* para */
677{ "prompt",    0,      0,      0,      7,      0, "" }, /* smallcptr */
678{ "property",  0,      0,      0,      7,      0, "" }, /* smallcptr */
679{ "pubdate",   0,      0,      0,      4,      0, "" }, /* docinfo */
680{ "publishername",0,   0,      0,      4,      0, "" }, /* docinfo */
681{ "publisher", 0,      0,      0,      9,      0, "" }, /* publishername */
682{ "pubsnumber",        0,      0,      0,      4,      0, "" }, /* docinfo */
683{ "qandadiv",  0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
684{ "qandaentry",        0,      0,      0,      9,      0, "" }, /* revhistory */
685{ "qandaset",  0,      0,      0,      9,      0, "" }, /* formalobject.title.content */
686{ "question",  0,      0,      0,      9,      0, "" }, /* label */
687{ "quote",     0,      0,      0,      2,      0, "" }, /* para */
688{ "refclass",  0,      0,      0,      9,      0, "" }, /* refclass.char.mix */
689{ "refdescriptor",0,   0,      0,      9,      0, "" }, /* refname.char.mix */
690{ "refentryinfo",0,    0,      0,      9,      0, "" }, /* graphic */
691{ "refentry",  0,      0,      0,      9,      0, "" }, /* ndxterm.class */
692{ "refentrytitle",0,   0,      0,      2,      0, "" }, /* para */
693{ "referenceinfo",0,   0,      0,      9,      0, "" }, /* graphic */
694{ "reference", 0,      0,      0,      9,      0, "" }, /* referenceinfo */
695{ "refmeta",   0,      0,      0,      9,      0, "" }, /* ndxterm.class */
696{ "refmiscinfo",0,     0,      0,      4,      0, "" }, /* docinfo */
697{ "refnamediv",        0,      0,      0,      9,      0, "" }, /* refdescriptor */
698{ "refname",   0,      0,      0,      9,      0, "" }, /* refname.char.mix */
699{ "refpurpose",        0,      0,      0,      9,      0, "" }, /* refinline.char.mix */
700{ "refsect1info",0,    0,      0,      9,      0, "" }, /* graphic */
701{ "refsect1",  0,      0,      0,      9,      0, "" }, /* refsect */
702{ "refsect2info",0,    0,      0,      9,      0, "" }, /* graphic */
703{ "refsect2",  0,      0,      0,      9,      0, "" }, /* refsect */
704{ "refsect3info",0,    0,      0,      9,      0, "" }, /* graphic */
705{ "refsect3",  0,      0,      0,      9,      0, "" }, /* refsect */
706{ "refsynopsisdivinfo",0,0,    0,      9,      0, "" }, /* graphic */
707{ "refsynopsisdiv",0,  0,      0,      9,      0, "" }, /* refsynopsisdivinfo */
708{ "releaseinfo",0,     0,      0,      4,      0, "" }, /* docinfo */
709{ "remark",    0,      0,      0,      2,      0, "" }, /* para */
710{ "replaceable",0,     0,      0,      1,      0, "" },
711{ "returnvalue",0,     0,      0,      7,      0, "" }, /* smallcptr */
712{ "revdescription",0,  0,      0,      9,      0, "" }, /* revdescription.mix */
713{ "revhistory",        0,      0,      0,      9,      0, "" }, /* revision */
714{ "revision",  0,      0,      0,      9,      0, "" }, /* revnumber */
715{ "revnumber", 0,      0,      0,      4,      0, "" }, /* docinfo */
716{ "revremark", 0,      0,      0,      4,      0, "" }, /* docinfo */
717{ "row",       0,      0,      0,      9,      0, "" }, /* tbl.row.mdl */
718{ "row",       0,      0,      0,      9,      0, "" }, /* tbl.row.mdl */
719{ "sbr",       0,      2,      1,      0,      0, "" },
720{ "screenco",  0,      0,      0,      9,      0, "" }, /* areaspec */
721{ "screeninfo",        0,      0,      0,      2,      0, "" }, /* para */
722{ "screen",    0,      0,      0,      2,      0, "" }, /* para */
723{ "screenshot",        0,      0,      0,      9,      0, "" }, /* screeninfo */
724{ "secondaryie",0,     0,      0,      4,      0, "" }, /* ndxterm */
725{ "secondary", 0,      0,      0,      4,      0, "" }, /* ndxterm */
726{ "sect1info", 0,      0,      0,      9,      0, "" }, /* graphic */
727{ "sect1",     0,      0,      0,      9,      0, "" }, /* sect */
728{ "sect2info", 0,      0,      0,      9,      0, "" }, /* graphic */
729{ "sect2",     0,      0,      0,      9,      0, "" }, /* sect */
730{ "sect3info", 0,      0,      0,      9,      0, "" }, /* graphic */
731{ "sect3",     0,      0,      0,      9,      0, "" }, /* sect */
732{ "sect4info", 0,      0,      0,      9,      0, "" }, /* graphic */
733{ "sect4",     0,      0,      0,      9,      0, "" }, /* sect */
734{ "sect5info", 0,      0,      0,      9,      0, "" }, /* graphic */
735{ "sect5",     0,      0,      0,      9,      0, "" }, /* sect */
736{ "sectioninfo",0,     0,      0,      9,      0, "" }, /* graphic */
737{ "section",   0,      0,      0,      9,      0, "" }, /* sectioninfo */
738{ "seealsoie", 0,      0,      0,      4,      0, "" }, /* ndxterm */
739{ "seealso",   0,      0,      0,      4,      0, "" }, /* ndxterm */
740{ "seeie",     0,      0,      0,      4,      0, "" }, /* ndxterm */
741{ "see",       0,      0,      0,      4,      0, "" }, /* ndxterm */
742{ "seglistitem",0,     0,      0,      9,      0, "" }, /* seg */
743{ "segmentedlist",0,   0,      0,      9,      0, "" }, /* formalobject.title.content */
744{ "seg",       0,      0,      0,      2,      0, "" }, /* para */
745{ "segtitle",  0,      0,      0,      8,      0, "" }, /* title */
746{ "seriesvolnums",     0,      0,      0,      4,      0, "" }, /* docinfo */
747{ "set",       0,      0,      0,      9,      0, "" }, /* div.title.content */
748{ "setindexinfo",0,    0,      0,      9,      0, "" }, /* graphic */
749{ "setindex",  0,      0,      0,      9,      0, "" }, /* setindexinfo */
750{ "setinfo",   0,      0,      0,      9,      0, "" }, /* graphic */
751{ "sgmltag",   0,      0,      0,      7,      0, "" }, /* smallcptr */
752{ "shortaffil",        0,      0,      0,      4,      0, "" }, /* docinfo */
753{ "shortcut",  0,      0,      0,      9,      0, "" }, /* keycap */
754{ "sidebarinfo",0,     0,      0,      9,      0, "" }, /* graphic */
755{ "sidebar",   0,      0,      0,      9,      0, "" }, /* sidebarinfo */
756{ "simpara",   0,      0,      0,      2,      0, "" }, /* para */
757{ "simplelist",        0,      0,      0,      9,      0, "" }, /* member */
758{ "simplemsgentry",    0,      0,      0,      9,      0, "" }, /* msgtext */
759{ "simplesect",        0,      0,      0,      9,      0, "" }, /* sect.title.content */
760{ "spanspec",  0,      2,      1,      0,      0, "" },
761{ "state",     0,      0,      0,      4,      0, "" }, /* docinfo */
762{ "step",      0,      0,      0,      9,      0, "" }, /* title */
763{ "street",    0,      0,      0,      4,      0, "" }, /* docinfo */
764{ "structfield",0,     0,      0,      7,      0, "" }, /* smallcptr */
765{ "structname",        0,      0,      0,      7,      0, "" }, /* smallcptr */
766{ "subjectset",        0,      0,      0,      9,      0, "" }, /* subject */
767{ "subject",   0,      0,      0,      9,      0, "" }, /* subjectterm */
768{ "subjectterm",0,     0,      0,      1,      0, "" },
769{ "subscript", 0,      0,      0,      1,      0, "" },
770{ "substeps",  0,      0,      0,      9,      0, "" }, /* step */
771{ "subtitle",  0,      0,      0,      8,      0, "" }, /* title */
772{ "superscript",       0,      0,      0,      1,      0, "" },
773{ "surname",   0,      0,      0,      4,      0, "" }, /* docinfo */
774{ "symbol",    0,      0,      0,      7,      0, "" }, /* smallcptr */
775{ "synopfragment",     0,      0,      0,      9,      0, "" }, /* arg */
776{ "synopfragmentref",  0,      0,      0,      1,      0, "" },
777{ "synopsis",  0,      0,      0,      2,      0, "" }, /* para */
778{ "systemitem",        0,      0,      0,      7,      0, "" }, /* smallcptr */
779{ "table",     0,      0,      0,      9,      0, "" }, /* tbl.table.mdl */
780/* { "%tbl.table.name;",       0,      0,      0,      9,      0, "" },*/ /* tbl.table.mdl */
781{ "tbody",     0,      0,      0,      9,      0, "" }, /* row */
782{ "tbody",     0,      0,      0,      9,      0, "" }, /* row */
783{ "term",      0,      0,      0,      2,      0, "" }, /* para */
784{ "tertiaryie",        0,      0,      0,      4,      0, "" }, /* ndxterm */
785{ "tertiary ", 0,      0,      0,      4,      0, "" }, /* ndxterm */
786{ "textobject",        0,      0,      0,      9,      0, "" }, /* objectinfo */
787{ "tfoot",     0,      0,      0,      9,      0, "" }, /* tbl.hdft.mdl */
788{ "tgroup",    0,      0,      0,      9,      0, "" }, /* tbl.tgroup.mdl */
789{ "tgroup",    0,      0,      0,      9,      0, "" }, /* tbl.tgroup.mdl */
790{ "thead",     0,      0,      0,      9,      0, "" }, /* row */
791{ "thead",     0,      0,      0,      9,      0, "" }, /* tbl.hdft.mdl */
792{ "tip",       0,      0,      0,      9,      0, "" }, /* title */
793{ "titleabbrev",0,     0,      0,      8,      0, "" }, /* title */
794{ "title",     0,      0,      0,      8,      0, "" }, /* title */
795{ "tocback",   0,      0,      0,      2,      0, "" }, /* para */
796{ "toc",       0,      0,      0,      9,      0, "" }, /* bookcomponent.title.content */
797{ "tocchap",   0,      0,      0,      9,      0, "" }, /* tocentry */
798{ "tocentry",  0,      0,      0,      2,      0, "" }, /* para */
799{ "tocfront",  0,      0,      0,      2,      0, "" }, /* para */
800{ "toclevel1", 0,      0,      0,      9,      0, "" }, /* tocentry */
801{ "toclevel2", 0,      0,      0,      9,      0, "" }, /* tocentry */
802{ "toclevel3", 0,      0,      0,      9,      0, "" }, /* tocentry */
803{ "toclevel4", 0,      0,      0,      9,      0, "" }, /* tocentry */
804{ "toclevel5", 0,      0,      0,      9,      0, "" }, /* tocentry */
805{ "tocpart",   0,      0,      0,      9,      0, "" }, /* tocentry */
806{ "token",     0,      0,      0,      7,      0, "" }, /* smallcptr */
807{ "trademark", 0,      0,      0,      1,      0, "" },
808{ "type",      0,      0,      0,      7,      0, "" }, /* smallcptr */
809{ "ulink",     0,      0,      0,      2,      0, "" }, /* para */
810{ "userinput", 0,      0,      0,      9,      0, "" }, /* cptr */
811{ "varargs",   0,      2,      1,      0,      0, "" },
812{ "variablelist",0,    0,      0,      9,      0, "" }, /* formalobject.title.content */
813{ "varlistentry",0,    0,      0,      9,      0, "" }, /* term */
814{ "varname",   0,      0,      0,      7,      0, "" }, /* smallcptr */
815{ "videodata", 0,      2,      1,      0,      0, "" },
816{ "videoobject",0,     0,      0,      9,      0, "" }, /* objectinfo */
817{ "void",      0,      2,      1,      0,      0, "" },
818{ "volumenum", 0,      0,      0,      4,      0, "" }, /* docinfo */
819{ "warning",   0,      0,      0,      9,      0, "" }, /* title */
820{ "wordasword",        0,      0,      0,      3,      0, "" }, /* word */
821{ "xref",      0,      2,      1,      0,      0, "" },
822{ "year",      0,      0,      0,      4,      0, "" }, /* docinfo */
823};
824
825#if 0
826/*
827 * start tags that imply the end of a current element
828 * any tag of each line implies the end of the current element if the type of
829 * that element is in the same line
830 */
831static const char *docbEquEnd[] = {
832"dt", "dd", "li", "option", NULL,
833"h1", "h2", "h3", "h4", "h5", "h6", NULL,
834"ol", "menu", "dir", "address", "pre", "listing", "xmp", NULL,
835NULL
836};
837#endif
838
839/*
840 * according the SGML DTD, HR should be added to the 2nd line above, as it
841 * is not allowed within a H1, H2, H3, etc. But we should tolerate that case
842 * because many documents contain rules in headings...
843 */
844
845/*
846 * start tags that imply the end of current element
847 */
848static const char *docbStartClose[] = {
849NULL
850};
851
852static const char** docbStartCloseIndex[100];
853static int docbStartCloseIndexinitialized = 0;
854
855/************************************************************************
856 *                                                                     *
857 *             functions to handle SGML specific data                  *
858 *                                                                     *
859 ************************************************************************/
860
861/**
862 * docbInitAutoClose:
863 *
864 * Initialize the docbStartCloseIndex for fast lookup of closing tags names.
865 *
866 */
867static void
868docbInitAutoClose(void) {
869    int indx, i = 0;
870
871    if (docbStartCloseIndexinitialized) return;
872
873    for (indx = 0;indx < 100;indx ++) docbStartCloseIndex[indx] = NULL;
874    indx = 0;
875    while ((docbStartClose[i] != NULL) && (indx < 100 - 1)) {
876        docbStartCloseIndex[indx++] = &docbStartClose[i];
877       while (docbStartClose[i] != NULL) i++;
878       i++;
879    }
880}
881
882/**
883 * docbTagLookup:
884 * @tag:  The tag name
885 *
886 * Lookup the SGML tag in the ElementTable
887 *
888 * Returns the related docbElemDescPtr or NULL if not found.
889 */
890static docbElemDescPtr
891docbTagLookup(const xmlChar *tag) {
892    unsigned int i;
893
894    for (i = 0; i < (sizeof(docbookElementTable) /
895                     sizeof(docbookElementTable[0]));i++) {
896        if (xmlStrEqual(tag, BAD_CAST docbookElementTable[i].name))
897           return(&docbookElementTable[i]);
898    }
899    return(NULL);
900}
901
902/**
903 * docbCheckAutoClose:
904 * @newtag:  The new tag name
905 * @oldtag:  The old tag name
906 *
907 * Checks whether the new tag is one of the registered valid tags for
908 * closing old.
909 * Initialize the docbStartCloseIndex for fast lookup of closing tags names.
910 *
911 * Returns 0 if no, 1 if yes.
912 */
913static int
914docbCheckAutoClose(const xmlChar *newtag, const xmlChar *oldtag) {
915    int i, indx;
916    const char **closed = NULL;
917
918    if (docbStartCloseIndexinitialized == 0) docbInitAutoClose();
919
920    /* inefficient, but not a big deal */
921    for (indx = 0; indx < 100;indx++) {
922        closed = docbStartCloseIndex[indx];
923       if (closed == NULL) return(0);
924       if (xmlStrEqual(BAD_CAST *closed, newtag)) break;
925    }
926
927    i = closed - docbStartClose;
928    i++;
929    while (docbStartClose[i] != NULL) {
930        if (xmlStrEqual(BAD_CAST docbStartClose[i], oldtag)) {
931           return(1);
932       }
933       i++;
934    }
935    return(0);
936}
937
938/**
939 * docbAutoCloseOnClose:
940 * @ctxt:  an SGML parser context
941 * @newtag:  The new tag name
942 *
943 * The DocBook DTD allows an ending tag to implicitly close other tags.
944 */
945static void
946docbAutoCloseOnClose(docbParserCtxtPtr ctxt, const xmlChar *newtag) {
947    docbElemDescPtr info;
948    xmlChar *oldname;
949    int i;
950
951    if ((newtag[0] == '/') && (newtag[1] == 0))
952       return;
953
954#ifdef DEBUG
955    xmlGenericError(xmlGenericErrorContext,"Close of %s stack: %d elements\n", newtag, ctxt->nameNr);
956    for (i = 0;i < ctxt->nameNr;i++)
957        xmlGenericError(xmlGenericErrorContext,"%d : %s\n", i, ctxt->nameTab[i]);
958#endif
959
960    for (i = (ctxt->nameNr - 1);i >= 0;i--) {
961        if (xmlStrEqual(newtag, ctxt->nameTab[i])) break;
962    }
963    if (i < 0) return;
964
965    while (!xmlStrEqual(newtag, ctxt->name)) {
966       info = docbTagLookup(ctxt->name);
967       if ((info == NULL) || (info->endTag == 1)) {
968#ifdef DEBUG
969           xmlGenericError(xmlGenericErrorContext,"docbAutoCloseOnClose: %s closes %s\n", newtag, ctxt->name);
970#endif
971        } else {
972           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
973               ctxt->sax->error(ctxt->userData,
974                "Opening and ending tag mismatch: %s and %s\n",
975                                newtag, ctxt->name);
976           ctxt->wellFormed = 0;
977       }
978       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
979           ctxt->sax->endElement(ctxt->userData, ctxt->name);
980       oldname = docbnamePop(ctxt);
981       if (oldname != NULL) {
982#ifdef DEBUG
983           xmlGenericError(xmlGenericErrorContext,"docbAutoCloseOnClose: popped %s\n", oldname);
984#endif
985           xmlFree(oldname);
986       }
987    }
988}
989
990/**
991 * docbAutoClose:
992 * @ctxt:  an SGML parser context
993 * @newtag:  The new tag name or NULL
994 *
995 * The DocBook DTD allows a tag to implicitly close other tags.
996 * The list is kept in docbStartClose array. This function is
997 * called when a new tag has been detected and generates the
998 * appropriates closes if possible/needed.
999 * If newtag is NULL this mean we are at the end of the resource
1000 * and we should check
1001 */
1002static void
1003docbAutoClose(docbParserCtxtPtr ctxt, const xmlChar *newtag) {
1004    xmlChar *oldname;
1005    while ((newtag != NULL) && (ctxt->name != NULL) &&
1006           (docbCheckAutoClose(newtag, ctxt->name))) {
1007#ifdef DEBUG
1008       xmlGenericError(xmlGenericErrorContext,"docbAutoClose: %s closes %s\n", newtag, ctxt->name);
1009#endif
1010       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
1011           ctxt->sax->endElement(ctxt->userData, ctxt->name);
1012       oldname = docbnamePop(ctxt);
1013       if (oldname != NULL) {
1014#ifdef DEBUG
1015           xmlGenericError(xmlGenericErrorContext,"docbAutoClose: popped %s\n", oldname);
1016#endif
1017           xmlFree(oldname);
1018        }
1019    }
1020}
1021
1022/**
1023 * docbAutoCloseTag:
1024 * @doc:  the SGML document
1025 * @name:  The tag name
1026 * @elem:  the SGML element
1027 *
1028 * The DocBook DTD allows a tag to implicitly close other tags.
1029 * The list is kept in docbStartClose array. This function checks
1030 * if the element or one of it's children would autoclose the
1031 * given tag.
1032 *
1033 * Returns 1 if autoclose, 0 otherwise
1034 */
1035static int
1036docbAutoCloseTag(docbDocPtr doc, const xmlChar *name, docbNodePtr elem) {
1037    docbNodePtr child;
1038
1039    if (elem == NULL) return(1);
1040    if (xmlStrEqual(name, elem->name)) return(0);
1041    if (docbCheckAutoClose(elem->name, name)) return(1);
1042    child = elem->children;
1043    while (child != NULL) {
1044        if (docbAutoCloseTag(doc, name, child)) return(1);
1045       child = child->next;
1046    }
1047    return(0);
1048}
1049
1050/************************************************************************
1051 *                                                                     *
1052 *             The list of SGML predefined entities                    *
1053 *                                                                     *
1054 ************************************************************************/
1055
1056
1057static docbEntityDesc
1058docbookEntitiesTable[] = {
1059/*
1060 * the 4 absolute ones, plus apostrophe.
1061 */
1062{ 0x0026, "amp", "AMPERSAND" },
1063{ 0x003C, "lt",        "LESS-THAN SIGN" },
1064
1065/*
1066 * Converted with VI macros from docbook ent files
1067 */
1068{ 0x0021, "excl", "EXCLAMATION MARK" },
1069{ 0x0022, "quot", "QUOTATION MARK" },
1070{ 0x0023, "num", "NUMBER SIGN" },
1071{ 0x0024, "dollar", "DOLLAR SIGN" },
1072{ 0x0025, "percnt", "PERCENT SIGN" },
1073{ 0x0027, "apos", "APOSTROPHE" },
1074{ 0x0028, "lpar", "LEFT PARENTHESIS" },
1075{ 0x0029, "rpar", "RIGHT PARENTHESIS" },
1076{ 0x002A, "ast", "ASTERISK OPERATOR" },
1077{ 0x002B, "plus", "PLUS SIGN" },
1078{ 0x002C, "comma", "COMMA" },
1079{ 0x002D, "hyphen", "HYPHEN-MINUS" },
1080{ 0x002E, "period", "FULL STOP" },
1081{ 0x002F, "sol", "SOLIDUS" },
1082{ 0x003A, "colon", "COLON" },
1083{ 0x003B, "semi", "SEMICOLON" },
1084{ 0x003D, "equals", "EQUALS SIGN" },
1085{ 0x003E, "gt", "GREATER-THAN SIGN" },
1086{ 0x003F, "quest", "QUESTION MARK" },
1087{ 0x0040, "commat", "COMMERCIAL AT" },
1088{ 0x005B, "lsqb", "LEFT SQUARE BRACKET" },
1089{ 0x005C, "bsol", "REVERSE SOLIDUS" },
1090{ 0x005D, "rsqb", "RIGHT SQUARE BRACKET" },
1091{ 0x005E, "circ", "RING OPERATOR" },
1092{ 0x005F, "lowbar", "LOW LINE" },
1093{ 0x0060, "grave", "GRAVE ACCENT" },
1094{ 0x007B, "lcub", "LEFT CURLY BRACKET" },
1095{ 0x007C, "verbar", "VERTICAL LINE" },
1096{ 0x007D, "rcub", "RIGHT CURLY BRACKET" },
1097{ 0x00A0, "nbsp", "NO-BREAK SPACE" },
1098{ 0x00A1, "iexcl", "INVERTED EXCLAMATION MARK" },
1099{ 0x00A2, "cent", "CENT SIGN" },
1100{ 0x00A3, "pound", "POUND SIGN" },
1101{ 0x00A4, "curren", "CURRENCY SIGN" },
1102{ 0x00A5, "yen", "YEN SIGN" },
1103{ 0x00A6, "brvbar", "BROKEN BAR" },
1104{ 0x00A7, "sect", "SECTION SIGN" },
1105{ 0x00A8, "die", "" },
1106{ 0x00A8, "Dot", "" },
1107{ 0x00A8, "uml", "" },
1108{ 0x00A9, "copy", "COPYRIGHT SIGN" },
1109{ 0x00AA, "ordf", "FEMININE ORDINAL INDICATOR" },
1110{ 0x00AB, "laquo", "LEFT-POINTING DOUBLE ANGLE QUOTATION MARK" },
1111{ 0x00AC, "not", "NOT SIGN" },
1112{ 0x00AD, "shy", "SOFT HYPHEN" },
1113{ 0x00AE, "reg", "REG TRADE MARK SIGN" },
1114{ 0x00AF, "macr", "MACRON" },
1115{ 0x00B0, "deg", "DEGREE SIGN" },
1116{ 0x00B1, "plusmn", "PLUS-MINUS SIGN" },
1117{ 0x00B2, "sup2", "SUPERSCRIPT TWO" },
1118{ 0x00B3, "sup3", "SUPERSCRIPT THREE" },
1119{ 0x00B4, "acute", "ACUTE ACCENT" },
1120{ 0x00B5, "micro", "MICRO SIGN" },
1121{ 0x00B6, "para", "PILCROW SIGN" },
1122{ 0x00B7, "middot", "MIDDLE DOT" },
1123{ 0x00B8, "cedil", "CEDILLA" },
1124{ 0x00B9, "sup1", "SUPERSCRIPT ONE" },
1125{ 0x00BA, "ordm", "MASCULINE ORDINAL INDICATOR" },
1126{ 0x00BB, "raquo", "RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK" },
1127{ 0x00BC, "frac14", "VULGAR FRACTION ONE QUARTER" },
1128{ 0x00BD, "frac12", "VULGAR FRACTION ONE HALF" },
1129{ 0x00BD, "half", "VULGAR FRACTION ONE HALF" },
1130{ 0x00BE, "frac34", "VULGAR FRACTION THREE QUARTERS" },
1131{ 0x00BF, "iquest", "INVERTED QUESTION MARK" },
1132{ 0x00C0, "Agrave", "LATIN CAPITAL LETTER A WITH GRAVE" },
1133{ 0x00C1, "Aacute", "LATIN CAPITAL LETTER A WITH ACUTE" },
1134{ 0x00C2, "Acirc", "LATIN CAPITAL LETTER A WITH CIRCUMFLEX" },
1135{ 0x00C3, "Atilde", "LATIN CAPITAL LETTER A WITH TILDE" },
1136{ 0x00C4, "Auml", "LATIN CAPITAL LETTER A WITH DIAERESIS" },
1137{ 0x00C5, "Aring", "LATIN CAPITAL LETTER A WITH RING ABOVE" },
1138{ 0x00C6, "AElig", "LATIN CAPITAL LETTER AE" },
1139{ 0x00C7, "Ccedil", "LATIN CAPITAL LETTER C WITH CEDILLA" },
1140{ 0x00C8, "Egrave", "LATIN CAPITAL LETTER E WITH GRAVE" },
1141{ 0x00C9, "Eacute", "LATIN CAPITAL LETTER E WITH ACUTE" },
1142{ 0x00CA, "Ecirc", "LATIN CAPITAL LETTER E WITH CIRCUMFLEX" },
1143{ 0x00CB, "Euml", "LATIN CAPITAL LETTER E WITH DIAERESIS" },
1144{ 0x00CC, "Igrave", "LATIN CAPITAL LETTER I WITH GRAVE" },
1145{ 0x00CD, "Iacute", "LATIN CAPITAL LETTER I WITH ACUTE" },
1146{ 0x00CE, "Icirc", "LATIN CAPITAL LETTER I WITH CIRCUMFLEX" },
1147{ 0x00CF, "Iuml", "LATIN CAPITAL LETTER I WITH DIAERESIS" },
1148{ 0x00D0, "ETH", "LATIN CAPITAL LETTER ETH" },
1149{ 0x00D1, "Ntilde", "LATIN CAPITAL LETTER N WITH TILDE" },
1150{ 0x00D2, "Ograve", "LATIN CAPITAL LETTER O WITH GRAVE" },
1151{ 0x00D3, "Oacute", "LATIN CAPITAL LETTER O WITH ACUTE" },
1152{ 0x00D4, "Ocirc", "LATIN CAPITAL LETTER O WITH CIRCUMFLEX" },
1153{ 0x00D5, "Otilde", "LATIN CAPITAL LETTER O WITH TILDE" },
1154{ 0x00D6, "Ouml", "LATIN CAPITAL LETTER O WITH DIAERESIS" },
1155{ 0x00D7, "times", "MULTIPLICATION SIGN" },
1156{ 0x00D8, "Oslash", "LATIN CAPITAL LETTER O WITH STROKE" },
1157{ 0x00D9, "Ugrave", "LATIN CAPITAL LETTER U WITH GRAVE" },
1158{ 0x00DA, "Uacute", "LATIN CAPITAL LETTER U WITH ACUTE" },
1159{ 0x00DB, "Ucirc", "LATIN CAPITAL LETTER U WITH CIRCUMFLEX" },
1160{ 0x00DC, "Uuml", "LATIN CAPITAL LETTER U WITH DIAERESIS" },
1161{ 0x00DD, "Yacute", "LATIN CAPITAL LETTER Y WITH ACUTE" },
1162{ 0x00DE, "THORN", "LATIN CAPITAL LETTER THORN" },
1163{ 0x00DF, "szlig", "LATIN SMALL LETTER SHARP S" },
1164{ 0x00E0, "agrave", "LATIN SMALL LETTER A WITH GRAVE" },
1165{ 0x00E1, "aacute", "LATIN SMALL LETTER A WITH ACUTE" },
1166{ 0x00E2, "acirc", "LATIN SMALL LETTER A WITH CIRCUMFLEX" },
1167{ 0x00E3, "atilde", "LATIN SMALL LETTER A WITH TILDE" },
1168{ 0x00E4, "auml", "LATIN SMALL LETTER A WITH DIAERESIS" },
1169{ 0x00E5, "aring", "LATIN SMALL LETTER A WITH RING ABOVE" },
1170{ 0x00E6, "aelig", "LATIN SMALL LETTER AE" },
1171{ 0x00E7, "ccedil", "LATIN SMALL LETTER C WITH CEDILLA" },
1172{ 0x00E8, "egrave", "LATIN SMALL LETTER E WITH GRAVE" },
1173{ 0x00E9, "eacute", "LATIN SMALL LETTER E WITH ACUTE" },
1174{ 0x00EA, "ecirc", "LATIN SMALL LETTER E WITH CIRCUMFLEX" },
1175{ 0x00EB, "euml", "LATIN SMALL LETTER E WITH DIAERESIS" },
1176{ 0x00EC, "igrave", "LATIN SMALL LETTER I WITH GRAVE" },
1177{ 0x00ED, "iacute", "LATIN SMALL LETTER I WITH ACUTE" },
1178{ 0x00EE, "icirc", "LATIN SMALL LETTER I WITH CIRCUMFLEX" },
1179{ 0x00EF, "iuml", "LATIN SMALL LETTER I WITH DIAERESIS" },
1180{ 0x00F0, "eth", "LATIN SMALL LETTER ETH" },
1181{ 0x00F1, "ntilde", "LATIN SMALL LETTER N WITH TILDE" },
1182{ 0x00F2, "ograve", "LATIN SMALL LETTER O WITH GRAVE" },
1183{ 0x00F3, "oacute", "LATIN SMALL LETTER O WITH ACUTE" },
1184{ 0x00F4, "ocirc", "LATIN SMALL LETTER O WITH CIRCUMFLEX" },
1185{ 0x00F5, "otilde", "LATIN SMALL LETTER O WITH TILDE" },
1186{ 0x00F6, "ouml", "LATIN SMALL LETTER O WITH DIAERESIS" },
1187{ 0x00F7, "divide", "DIVISION SIGN" },
1188{ 0x00F8, "oslash", "CIRCLED DIVISION SLASH" },
1189{ 0x00F9, "ugrave", "LATIN SMALL LETTER U WITH GRAVE" },
1190{ 0x00FA, "uacute", "LATIN SMALL LETTER U WITH ACUTE" },
1191{ 0x00FB, "ucirc", "LATIN SMALL LETTER U WITH CIRCUMFLEX" },
1192{ 0x00FC, "uuml", "LATIN SMALL LETTER U WITH DIAERESIS" },
1193{ 0x00FD, "yacute", "LATIN SMALL LETTER Y WITH ACUTE" },
1194{ 0x00FE, "thorn", "LATIN SMALL LETTER THORN" },
1195{ 0x00FF, "yuml", "LATIN SMALL LETTER Y WITH DIAERESIS" },
1196{ 0x0100, "Amacr", "LATIN CAPITAL LETTER A WITH MACRON" },
1197{ 0x0101, "amacr", "LATIN SMALL LETTER A WITH MACRON" },
1198{ 0x0102, "Abreve", "LATIN CAPITAL LETTER A WITH BREVE" },
1199{ 0x0103, "abreve", "LATIN SMALL LETTER A WITH BREVE" },
1200{ 0x0104, "Aogon", "LATIN CAPITAL LETTER A WITH OGONEK" },
1201{ 0x0105, "aogon", "LATIN SMALL LETTER A WITH OGONEK" },
1202{ 0x0106, "Cacute", "LATIN CAPITAL LETTER C WITH ACUTE" },
1203{ 0x0107, "cacute", "LATIN SMALL LETTER C WITH ACUTE" },
1204{ 0x0108, "Ccirc", "LATIN CAPITAL LETTER C WITH CIRCUMFLEX" },
1205{ 0x0109, "ccirc", "LATIN SMALL LETTER C WITH CIRCUMFLEX" },
1206{ 0x010A, "Cdot", "LATIN CAPITAL LETTER C WITH DOT ABOVE" },
1207{ 0x010B, "cdot", "DOT OPERATOR" },
1208{ 0x010C, "Ccaron", "LATIN CAPITAL LETTER C WITH CARON" },
1209{ 0x010D, "ccaron", "LATIN SMALL LETTER C WITH CARON" },
1210{ 0x010E, "Dcaron", "LATIN CAPITAL LETTER D WITH CARON" },
1211{ 0x010F, "dcaron", "LATIN SMALL LETTER D WITH CARON" },
1212{ 0x0110, "Dstrok", "LATIN CAPITAL LETTER D WITH STROKE" },
1213{ 0x0111, "dstrok", "LATIN SMALL LETTER D WITH STROKE" },
1214{ 0x0112, "Emacr", "LATIN CAPITAL LETTER E WITH MACRON" },
1215{ 0x0113, "emacr", "LATIN SMALL LETTER E WITH MACRON" },
1216{ 0x0116, "Edot", "LATIN CAPITAL LETTER E WITH DOT ABOVE" },
1217{ 0x0117, "edot", "LATIN SMALL LETTER E WITH DOT ABOVE" },
1218{ 0x0118, "Eogon", "LATIN CAPITAL LETTER E WITH OGONEK" },
1219{ 0x0119, "eogon", "LATIN SMALL LETTER E WITH OGONEK" },
1220{ 0x011A, "Ecaron", "LATIN CAPITAL LETTER E WITH CARON" },
1221{ 0x011B, "ecaron", "LATIN SMALL LETTER E WITH CARON" },
1222{ 0x011C, "Gcirc", "LATIN CAPITAL LETTER G WITH CIRCUMFLEX" },
1223{ 0x011D, "gcirc", "LATIN SMALL LETTER G WITH CIRCUMFLEX" },
1224{ 0x011E, "Gbreve", "LATIN CAPITAL LETTER G WITH BREVE" },
1225{ 0x011F, "gbreve", "LATIN SMALL LETTER G WITH BREVE" },
1226{ 0x0120, "Gdot", "LATIN CAPITAL LETTER G WITH DOT ABOVE" },
1227{ 0x0121, "gdot", "LATIN SMALL LETTER G WITH DOT ABOVE" },
1228{ 0x0122, "Gcedil", "LATIN CAPITAL LETTER G WITH CEDILLA" },
1229{ 0x0124, "Hcirc", "LATIN CAPITAL LETTER H WITH CIRCUMFLEX" },
1230{ 0x0125, "hcirc", "LATIN SMALL LETTER H WITH CIRCUMFLEX" },
1231{ 0x0126, "Hstrok", "LATIN CAPITAL LETTER H WITH STROKE" },
1232{ 0x0127, "hstrok", "LATIN SMALL LETTER H WITH STROKE" },
1233{ 0x0128, "Itilde", "LATIN CAPITAL LETTER I WITH TILDE" },
1234{ 0x0129, "itilde", "LATIN SMALL LETTER I WITH TILDE" },
1235{ 0x012A, "Imacr", "LATIN CAPITAL LETTER I WITH MACRON" },
1236{ 0x012B, "imacr", "LATIN SMALL LETTER I WITH MACRON" },
1237{ 0x012E, "Iogon", "LATIN CAPITAL LETTER I WITH OGONEK" },
1238{ 0x012F, "iogon", "LATIN SMALL LETTER I WITH OGONEK" },
1239{ 0x0130, "Idot", "LATIN CAPITAL LETTER I WITH DOT ABOVE" },
1240{ 0x0131, "inodot", "LATIN SMALL LETTER DOTLESS I" },
1241{ 0x0131, "inodot", "LATIN SMALL LETTER DOTLESS I" },
1242{ 0x0132, "IJlig", "LATIN CAPITAL LIGATURE IJ" },
1243{ 0x0133, "ijlig", "LATIN SMALL LIGATURE IJ" },
1244{ 0x0134, "Jcirc", "LATIN CAPITAL LETTER J WITH CIRCUMFLEX" },
1245{ 0x0135, "jcirc", "LATIN SMALL LETTER J WITH CIRCUMFLEX" },
1246{ 0x0136, "Kcedil", "LATIN CAPITAL LETTER K WITH CEDILLA" },
1247{ 0x0137, "kcedil", "LATIN SMALL LETTER K WITH CEDILLA" },
1248{ 0x0138, "kgreen", "LATIN SMALL LETTER KRA" },
1249{ 0x0139, "Lacute", "LATIN CAPITAL LETTER L WITH ACUTE" },
1250{ 0x013A, "lacute", "LATIN SMALL LETTER L WITH ACUTE" },
1251{ 0x013B, "Lcedil", "LATIN CAPITAL LETTER L WITH CEDILLA" },
1252{ 0x013C, "lcedil", "LATIN SMALL LETTER L WITH CEDILLA" },
1253{ 0x013D, "Lcaron", "LATIN CAPITAL LETTER L WITH CARON" },
1254{ 0x013E, "lcaron", "LATIN SMALL LETTER L WITH CARON" },
1255{ 0x013F, "Lmidot", "LATIN CAPITAL LETTER L WITH MIDDLE DOT" },
1256{ 0x0140, "lmidot", "LATIN SMALL LETTER L WITH MIDDLE DOT" },
1257{ 0x0141, "Lstrok", "LATIN CAPITAL LETTER L WITH STROKE" },
1258{ 0x0142, "lstrok", "LATIN SMALL LETTER L WITH STROKE" },
1259{ 0x0143, "Nacute", "LATIN CAPITAL LETTER N WITH ACUTE" },
1260{ 0x0144, "nacute", "LATIN SMALL LETTER N WITH ACUTE" },
1261{ 0x0145, "Ncedil", "LATIN CAPITAL LETTER N WITH CEDILLA" },
1262{ 0x0146, "ncedil", "LATIN SMALL LETTER N WITH CEDILLA" },
1263{ 0x0147, "Ncaron", "LATIN CAPITAL LETTER N WITH CARON" },
1264{ 0x0148, "ncaron", "LATIN SMALL LETTER N WITH CARON" },
1265{ 0x0149, "napos", "LATIN SMALL LETTER N PRECEDED BY APOSTROPHE" },
1266{ 0x014A, "ENG", "LATIN CAPITAL LETTER ENG" },
1267{ 0x014B, "eng", "LATIN SMALL LETTER ENG" },
1268{ 0x014C, "Omacr", "LATIN CAPITAL LETTER O WITH MACRON" },
1269{ 0x014D, "omacr", "LATIN SMALL LETTER O WITH MACRON" },
1270{ 0x0150, "Odblac", "LATIN CAPITAL LETTER O WITH DOUBLE ACUTE" },
1271{ 0x0151, "odblac", "LATIN SMALL LETTER O WITH DOUBLE ACUTE" },
1272{ 0x0152, "OElig", "LATIN CAPITAL LIGATURE OE" },
1273{ 0x0153, "oelig", "LATIN SMALL LIGATURE OE" },
1274{ 0x0154, "Racute", "LATIN CAPITAL LETTER R WITH ACUTE" },
1275{ 0x0155, "racute", "LATIN SMALL LETTER R WITH ACUTE" },
1276{ 0x0156, "Rcedil", "LATIN CAPITAL LETTER R WITH CEDILLA" },
1277{ 0x0157, "rcedil", "LATIN SMALL LETTER R WITH CEDILLA" },
1278{ 0x0158, "Rcaron", "LATIN CAPITAL LETTER R WITH CARON" },
1279{ 0x0159, "rcaron", "LATIN SMALL LETTER R WITH CARON" },
1280{ 0x015A, "Sacute", "LATIN CAPITAL LETTER S WITH ACUTE" },
1281{ 0x015B, "sacute", "LATIN SMALL LETTER S WITH ACUTE" },
1282{ 0x015C, "Scirc", "LATIN CAPITAL LETTER S WITH CIRCUMFLEX" },
1283{ 0x015D, "scirc", "LATIN SMALL LETTER S WITH CIRCUMFLEX" },
1284{ 0x015E, "Scedil", "LATIN CAPITAL LETTER S WITH CEDILLA" },
1285{ 0x015F, "scedil", "LATIN SMALL LETTER S WITH CEDILLA" },
1286{ 0x0160, "Scaron", "LATIN CAPITAL LETTER S WITH CARON" },
1287{ 0x0161, "scaron", "LATIN SMALL LETTER S WITH CARON" },
1288{ 0x0162, "Tcedil", "LATIN CAPITAL LETTER T WITH CEDILLA" },
1289{ 0x0163, "tcedil", "LATIN SMALL LETTER T WITH CEDILLA" },
1290{ 0x0164, "Tcaron", "LATIN CAPITAL LETTER T WITH CARON" },
1291{ 0x0165, "tcaron", "LATIN SMALL LETTER T WITH CARON" },
1292{ 0x0166, "Tstrok", "LATIN CAPITAL LETTER T WITH STROKE" },
1293{ 0x0167, "tstrok", "LATIN SMALL LETTER T WITH STROKE" },
1294{ 0x0168, "Utilde", "LATIN CAPITAL LETTER U WITH TILDE" },
1295{ 0x0169, "utilde", "LATIN SMALL LETTER U WITH TILDE" },
1296{ 0x016A, "Umacr", "LATIN CAPITAL LETTER U WITH MACRON" },
1297{ 0x016B, "umacr", "LATIN SMALL LETTER U WITH MACRON" },
1298{ 0x016C, "Ubreve", "LATIN CAPITAL LETTER U WITH BREVE" },
1299{ 0x016D, "ubreve", "LATIN SMALL LETTER U WITH BREVE" },
1300{ 0x016E, "Uring", "LATIN CAPITAL LETTER U WITH RING ABOVE" },
1301{ 0x016F, "uring", "LATIN SMALL LETTER U WITH RING ABOVE" },
1302{ 0x0170, "Udblac", "LATIN CAPITAL LETTER U WITH DOUBLE ACUTE" },
1303{ 0x0171, "udblac", "LATIN SMALL LETTER U WITH DOUBLE ACUTE" },
1304{ 0x0172, "Uogon", "LATIN CAPITAL LETTER U WITH OGONEK" },
1305{ 0x0173, "uogon", "LATIN SMALL LETTER U WITH OGONEK" },
1306{ 0x0174, "Wcirc", "LATIN CAPITAL LETTER W WITH CIRCUMFLEX" },
1307{ 0x0175, "wcirc", "LATIN SMALL LETTER W WITH CIRCUMFLEX" },
1308{ 0x0176, "Ycirc", "LATIN CAPITAL LETTER Y WITH CIRCUMFLEX" },
1309{ 0x0177, "ycirc", "LATIN SMALL LETTER Y WITH CIRCUMFLEX" },
1310{ 0x0178, "Yuml", "LATIN CAPITAL LETTER Y WITH DIAERESIS" },
1311{ 0x0179, "Zacute", "LATIN CAPITAL LETTER Z WITH ACUTE" },
1312{ 0x017A, "zacute", "LATIN SMALL LETTER Z WITH ACUTE" },
1313{ 0x017B, "Zdot", "LATIN CAPITAL LETTER Z WITH DOT ABOVE" },
1314{ 0x017C, "zdot", "LATIN SMALL LETTER Z WITH DOT ABOVE" },
1315{ 0x017D, "Zcaron", "LATIN CAPITAL LETTER Z WITH CARON" },
1316{ 0x017E, "zcaron", "LATIN SMALL LETTER Z WITH CARON" },
1317{ 0x0192, "fnof", "LATIN SMALL LETTER F WITH HOOK" },
1318{ 0x01F5, "gacute", "LATIN SMALL LETTER G WITH ACUTE" },
1319{ 0x02C7, "caron", "CARON" },
1320{ 0x02D8, "breve", "BREVE" },
1321{ 0x02D9, "dot", "DOT ABOVE" },
1322{ 0x02DA, "ring", "RING ABOVE" },
1323{ 0x02DB, "ogon", "OGONEK" },
1324{ 0x02DC, "tilde", "TILDE" },
1325{ 0x02DD, "dblac", "DOUBLE ACUTE ACCENT" },
1326{ 0x0386, "Aacgr", "GREEK CAPITAL LETTER ALPHA WITH TONOS" },
1327{ 0x0388, "Eacgr", "GREEK CAPITAL LETTER EPSILON WITH TONOS" },
1328{ 0x0389, "EEacgr", "GREEK CAPITAL LETTER ETA WITH TONOS" },
1329{ 0x038A, "Iacgr", "GREEK CAPITAL LETTER IOTA WITH TONOS" },
1330{ 0x038C, "Oacgr", "GREEK CAPITAL LETTER OMICRON WITH TONOS" },
1331{ 0x038E, "Uacgr", "GREEK CAPITAL LETTER UPSILON WITH TONOS" },
1332{ 0x038F, "OHacgr", "GREEK CAPITAL LETTER OMEGA WITH TONOS" },
1333{ 0x0390, "idiagr", "GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS" },
1334{ 0x0391, "Agr", "GREEK CAPITAL LETTER ALPHA" },
1335{ 0x0392, "Bgr", "GREEK CAPITAL LETTER BETA" },
1336{ 0x0393, "b.Gamma", "GREEK CAPITAL LETTER GAMMA" },
1337{ 0x0393, "Gamma", "GREEK CAPITAL LETTER GAMMA" },
1338{ 0x0393, "Ggr", "GREEK CAPITAL LETTER GAMMA" },
1339{ 0x0394, "b.Delta", "GREEK CAPITAL LETTER DELTA" },
1340{ 0x0394, "Delta", "GREEK CAPITAL LETTER DELTA" },
1341{ 0x0394, "Dgr", "GREEK CAPITAL LETTER DELTA" },
1342{ 0x0395, "Egr", "GREEK CAPITAL LETTER EPSILON" },
1343{ 0x0396, "Zgr", "GREEK CAPITAL LETTER ZETA" },
1344{ 0x0397, "EEgr", "GREEK CAPITAL LETTER ETA" },
1345{ 0x0398, "b.Theta", "GREEK CAPITAL LETTER THETA" },
1346{ 0x0398, "Theta", "GREEK CAPITAL LETTER THETA" },
1347{ 0x0398, "THgr", "GREEK CAPITAL LETTER THETA" },
1348{ 0x0399, "Igr", "GREEK CAPITAL LETTER IOTA" },
1349{ 0x039A, "Kgr", "GREEK CAPITAL LETTER KAPPA" },
1350{ 0x039B, "b.Lambda", "GREEK CAPITAL LETTER LAMDA" },
1351{ 0x039B, "Lambda", "GREEK CAPITAL LETTER LAMDA" },
1352{ 0x039B, "Lgr", "GREEK CAPITAL LETTER LAMDA" },
1353{ 0x039C, "Mgr", "GREEK CAPITAL LETTER MU" },
1354{ 0x039D, "Ngr", "GREEK CAPITAL LETTER NU" },
1355{ 0x039E, "b.Xi", "GREEK CAPITAL LETTER XI" },
1356{ 0x039E, "Xgr", "GREEK CAPITAL LETTER XI" },
1357{ 0x039E, "Xi", "GREEK CAPITAL LETTER XI" },
1358{ 0x039F, "Ogr", "GREEK CAPITAL LETTER OMICRON" },
1359{ 0x03A0, "b.Pi", "GREEK CAPITAL LETTER PI" },
1360{ 0x03A0, "Pgr", "GREEK CAPITAL LETTER PI" },
1361{ 0x03A0, "Pi", "GREEK CAPITAL LETTER PI" },
1362{ 0x03A1, "Rgr", "GREEK CAPITAL LETTER RHO" },
1363{ 0x03A3, "b.Sigma", "GREEK CAPITAL LETTER SIGMA" },
1364{ 0x03A3, "Sgr", "GREEK CAPITAL LETTER SIGMA" },
1365{ 0x03A3, "Sigma", "GREEK CAPITAL LETTER SIGMA" },
1366{ 0x03A4, "Tgr", "GREEK CAPITAL LETTER TAU" },
1367{ 0x03A5, "Ugr", "" },
1368{ 0x03A6, "b.Phi", "GREEK CAPITAL LETTER PHI" },
1369{ 0x03A6, "PHgr", "GREEK CAPITAL LETTER PHI" },
1370{ 0x03A6, "Phi", "GREEK CAPITAL LETTER PHI" },
1371{ 0x03A7, "KHgr", "GREEK CAPITAL LETTER CHI" },
1372{ 0x03A8, "b.Psi", "GREEK CAPITAL LETTER PSI" },
1373{ 0x03A8, "PSgr", "GREEK CAPITAL LETTER PSI" },
1374{ 0x03A8, "Psi", "GREEK CAPITAL LETTER PSI" },
1375{ 0x03A9, "b.Omega", "GREEK CAPITAL LETTER OMEGA" },
1376{ 0x03A9, "OHgr", "GREEK CAPITAL LETTER OMEGA" },
1377{ 0x03A9, "Omega", "GREEK CAPITAL LETTER OMEGA" },
1378{ 0x03AA, "Idigr", "GREEK CAPITAL LETTER IOTA WITH DIALYTIKA" },
1379{ 0x03AB, "Udigr", "GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA" },
1380{ 0x03AC, "aacgr", "GREEK SMALL LETTER ALPHA WITH TONOS" },
1381{ 0x03AD, "eacgr", "GREEK SMALL LETTER EPSILON WITH TONOS" },
1382{ 0x03AE, "eeacgr", "GREEK SMALL LETTER ETA WITH TONOS" },
1383{ 0x03AF, "iacgr", "GREEK SMALL LETTER IOTA WITH TONOS" },
1384{ 0x03B0, "udiagr", "GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS" },
1385{ 0x03B1, "agr", "" },
1386{ 0x03B1, "alpha", "" },
1387{ 0x03B1, "b.alpha", "" },
1388{ 0x03B2, "b.beta", "GREEK SMALL LETTER BETA" },
1389{ 0x03B2, "beta", "GREEK SMALL LETTER BETA" },
1390{ 0x03B2, "bgr", "GREEK SMALL LETTER BETA" },
1391{ 0x03B3, "b.gamma", "GREEK SMALL LETTER GAMMA" },
1392{ 0x03B3, "gamma", "GREEK SMALL LETTER GAMMA" },
1393{ 0x03B3, "ggr", "GREEK SMALL LETTER GAMMA" },
1394{ 0x03B4, "b.delta", "GREEK SMALL LETTER DELTA" },
1395{ 0x03B4, "delta", "GREEK SMALL LETTER DELTA" },
1396{ 0x03B4, "dgr", "GREEK SMALL LETTER DELTA" },
1397{ 0x03B5, "b.epsi", "" },
1398{ 0x03B5, "b.epsis", "" },
1399{ 0x03B5, "b.epsiv", "" },
1400{ 0x03B5, "egr", "" },
1401{ 0x03B5, "epsiv", "" },
1402{ 0x03B6, "b.zeta", "GREEK SMALL LETTER ZETA" },
1403{ 0x03B6, "zeta", "GREEK SMALL LETTER ZETA" },
1404{ 0x03B6, "zgr", "GREEK SMALL LETTER ZETA" },
1405{ 0x03B7, "b.eta", "GREEK SMALL LETTER ETA" },
1406{ 0x03B7, "eegr", "GREEK SMALL LETTER ETA" },
1407{ 0x03B7, "eta", "GREEK SMALL LETTER ETA" },
1408{ 0x03B8, "b.thetas", "" },
1409{ 0x03B8, "thetas", "" },
1410{ 0x03B8, "thgr", "" },
1411{ 0x03B9, "b.iota", "GREEK SMALL LETTER IOTA" },
1412{ 0x03B9, "igr", "GREEK SMALL LETTER IOTA" },
1413{ 0x03B9, "iota", "GREEK SMALL LETTER IOTA" },
1414{ 0x03BA, "b.kappa", "GREEK SMALL LETTER KAPPA" },
1415{ 0x03BA, "kappa", "GREEK SMALL LETTER KAPPA" },
1416{ 0x03BA, "kgr", "GREEK SMALL LETTER KAPPA" },
1417{ 0x03BB, "b.lambda", "GREEK SMALL LETTER LAMDA" },
1418{ 0x03BB, "lambda", "GREEK SMALL LETTER LAMDA" },
1419{ 0x03BB, "lgr", "GREEK SMALL LETTER LAMDA" },
1420{ 0x03BC, "b.mu", "GREEK SMALL LETTER MU" },
1421{ 0x03BC, "mgr", "GREEK SMALL LETTER MU" },
1422{ 0x03BC, "mu", "GREEK SMALL LETTER MU" },
1423{ 0x03BD, "b.nu", "GREEK SMALL LETTER NU" },
1424{ 0x03BD, "ngr", "GREEK SMALL LETTER NU" },
1425{ 0x03BD, "nu", "GREEK SMALL LETTER NU" },
1426{ 0x03BE, "b.xi", "GREEK SMALL LETTER XI" },
1427{ 0x03BE, "xgr", "GREEK SMALL LETTER XI" },
1428{ 0x03BE, "xi", "GREEK SMALL LETTER XI" },
1429{ 0x03BF, "ogr", "GREEK SMALL LETTER OMICRON" },
1430{ 0x03C0, "b.pi", "GREEK SMALL LETTER PI" },
1431{ 0x03C0, "pgr", "GREEK SMALL LETTER PI" },
1432{ 0x03C0, "pi", "GREEK SMALL LETTER PI" },
1433{ 0x03C1, "b.rho", "GREEK SMALL LETTER RHO" },
1434{ 0x03C1, "rgr", "GREEK SMALL LETTER RHO" },
1435{ 0x03C1, "rho", "GREEK SMALL LETTER RHO" },
1436{ 0x03C2, "b.sigmav", "" },
1437{ 0x03C2, "sfgr", "" },
1438{ 0x03C2, "sigmav", "" },
1439{ 0x03C3, "b.sigma", "GREEK SMALL LETTER SIGMA" },
1440{ 0x03C3, "sgr", "GREEK SMALL LETTER SIGMA" },
1441{ 0x03C3, "sigma", "GREEK SMALL LETTER SIGMA" },
1442{ 0x03C4, "b.tau", "GREEK SMALL LETTER TAU" },
1443{ 0x03C4, "tau", "GREEK SMALL LETTER TAU" },
1444{ 0x03C4, "tgr", "GREEK SMALL LETTER TAU" },
1445{ 0x03C5, "b.upsi", "GREEK SMALL LETTER UPSILON" },
1446{ 0x03C5, "ugr", "GREEK SMALL LETTER UPSILON" },
1447{ 0x03C5, "upsi", "GREEK SMALL LETTER UPSILON" },
1448{ 0x03C6, "b.phis", "GREEK SMALL LETTER PHI" },
1449{ 0x03C6, "phgr", "GREEK SMALL LETTER PHI" },
1450{ 0x03C6, "phis", "GREEK SMALL LETTER PHI" },
1451{ 0x03C7, "b.chi", "GREEK SMALL LETTER CHI" },
1452{ 0x03C7, "chi", "GREEK SMALL LETTER CHI" },
1453{ 0x03C7, "khgr", "GREEK SMALL LETTER CHI" },
1454{ 0x03C8, "b.psi", "GREEK SMALL LETTER PSI" },
1455{ 0x03C8, "psgr", "GREEK SMALL LETTER PSI" },
1456{ 0x03C8, "psi", "GREEK SMALL LETTER PSI" },
1457{ 0x03C9, "b.omega", "GREEK SMALL LETTER OMEGA" },
1458{ 0x03C9, "ohgr", "GREEK SMALL LETTER OMEGA" },
1459{ 0x03C9, "omega", "GREEK SMALL LETTER OMEGA" },
1460{ 0x03CA, "idigr", "GREEK SMALL LETTER IOTA WITH DIALYTIKA" },
1461{ 0x03CB, "udigr", "GREEK SMALL LETTER UPSILON WITH DIALYTIKA" },
1462{ 0x03CC, "oacgr", "GREEK SMALL LETTER OMICRON WITH TONOS" },
1463{ 0x03CD, "uacgr", "GREEK SMALL LETTER UPSILON WITH TONOS" },
1464{ 0x03CE, "ohacgr", "GREEK SMALL LETTER OMEGA WITH TONOS" },
1465{ 0x03D1, "b.thetav", "" },
1466{ 0x03D1, "thetav", "" },
1467{ 0x03D2, "b.Upsi", "" },
1468{ 0x03D2, "Upsi", "" },
1469{ 0x03D5, "b.phiv", "GREEK PHI SYMBOL" },
1470{ 0x03D5, "phiv", "GREEK PHI SYMBOL" },
1471{ 0x03D6, "b.piv", "GREEK PI SYMBOL" },
1472{ 0x03D6, "piv", "GREEK PI SYMBOL" },
1473{ 0x03DC, "b.gammad", "GREEK LETTER DIGAMMA" },
1474{ 0x03DC, "gammad", "GREEK LETTER DIGAMMA" },
1475{ 0x03F0, "b.kappav", "GREEK KAPPA SYMBOL" },
1476{ 0x03F0, "kappav", "GREEK KAPPA SYMBOL" },
1477{ 0x03F1, "b.rhov", "GREEK RHO SYMBOL" },
1478{ 0x03F1, "rhov", "GREEK RHO SYMBOL" },
1479{ 0x0401, "IOcy", "CYRILLIC CAPITAL LETTER IO" },
1480{ 0x0402, "DJcy", "CYRILLIC CAPITAL LETTER DJE" },
1481{ 0x0403, "GJcy", "CYRILLIC CAPITAL LETTER GJE" },
1482{ 0x0404, "Jukcy", "CYRILLIC CAPITAL LETTER UKRAINIAN IE" },
1483{ 0x0405, "DScy", "CYRILLIC CAPITAL LETTER DZE" },
1484{ 0x0406, "Iukcy", "CYRILLIC CAPITAL LETTER BYELORUSSIAN-UKRAINIAN I" },
1485{ 0x0407, "YIcy", "CYRILLIC CAPITAL LETTER YI" },
1486{ 0x0408, "Jsercy", "CYRILLIC CAPITAL LETTER JE" },
1487{ 0x0409, "LJcy", "CYRILLIC CAPITAL LETTER LJE" },
1488{ 0x040A, "NJcy", "CYRILLIC CAPITAL LETTER NJE" },
1489{ 0x040B, "TSHcy", "CYRILLIC CAPITAL LETTER TSHE" },
1490{ 0x040C, "KJcy", "CYRILLIC CAPITAL LETTER KJE" },
1491{ 0x040E, "Ubrcy", "CYRILLIC CAPITAL LETTER SHORT U" },
1492{ 0x040F, "DZcy", "CYRILLIC CAPITAL LETTER DZHE" },
1493{ 0x0410, "Acy", "CYRILLIC CAPITAL LETTER A" },
1494{ 0x0411, "Bcy", "CYRILLIC CAPITAL LETTER BE" },
1495{ 0x0412, "Vcy", "CYRILLIC CAPITAL LETTER VE" },
1496{ 0x0413, "Gcy", "CYRILLIC CAPITAL LETTER GHE" },
1497{ 0x0414, "Dcy", "CYRILLIC CAPITAL LETTER DE" },
1498{ 0x0415, "IEcy", "CYRILLIC CAPITAL LETTER IE" },
1499{ 0x0416, "ZHcy", "CYRILLIC CAPITAL LETTER ZHE" },
1500{ 0x0417, "Zcy", "CYRILLIC CAPITAL LETTER ZE" },
1501{ 0x0418, "Icy", "CYRILLIC CAPITAL LETTER I" },
1502{ 0x0419, "Jcy", "CYRILLIC CAPITAL LETTER SHORT I" },
1503{ 0x041A, "Kcy", "CYRILLIC CAPITAL LETTER KA" },
1504{ 0x041B, "Lcy", "CYRILLIC CAPITAL LETTER EL" },
1505{ 0x041C, "Mcy", "CYRILLIC CAPITAL LETTER EM" },
1506{ 0x041D, "Ncy", "CYRILLIC CAPITAL LETTER EN" },
1507{ 0x041E, "Ocy", "CYRILLIC CAPITAL LETTER O" },
1508{ 0x041F, "Pcy", "CYRILLIC CAPITAL LETTER PE" },
1509{ 0x0420, "Rcy", "CYRILLIC CAPITAL LETTER ER" },
1510{ 0x0421, "Scy", "CYRILLIC CAPITAL LETTER ES" },
1511{ 0x0422, "Tcy", "CYRILLIC CAPITAL LETTER TE" },
1512{ 0x0423, "Ucy", "CYRILLIC CAPITAL LETTER U" },
1513{ 0x0424, "Fcy", "CYRILLIC CAPITAL LETTER EF" },
1514{ 0x0425, "KHcy", "CYRILLIC CAPITAL LETTER HA" },
1515{ 0x0426, "TScy", "CYRILLIC CAPITAL LETTER TSE" },
1516{ 0x0427, "CHcy", "CYRILLIC CAPITAL LETTER CHE" },
1517{ 0x0428, "SHcy", "CYRILLIC CAPITAL LETTER SHA" },
1518{ 0x0429, "SHCHcy", "CYRILLIC CAPITAL LETTER SHCHA" },
1519{ 0x042A, "HARDcy", "CYRILLIC CAPITAL LETTER HARD SIGN" },
1520{ 0x042B, "Ycy", "CYRILLIC CAPITAL LETTER YERU" },
1521{ 0x042C, "SOFTcy", "CYRILLIC CAPITAL LETTER SOFT SIGN" },
1522{ 0x042D, "Ecy", "CYRILLIC CAPITAL LETTER E" },
1523{ 0x042E, "YUcy", "CYRILLIC CAPITAL LETTER YU" },
1524{ 0x042F, "YAcy", "CYRILLIC CAPITAL LETTER YA" },
1525{ 0x0430, "acy", "CYRILLIC SMALL LETTER A" },
1526{ 0x0431, "bcy", "CYRILLIC SMALL LETTER BE" },
1527{ 0x0432, "vcy", "CYRILLIC SMALL LETTER VE" },
1528{ 0x0433, "gcy", "CYRILLIC SMALL LETTER GHE" },
1529{ 0x0434, "dcy", "CYRILLIC SMALL LETTER DE" },
1530{ 0x0435, "iecy", "CYRILLIC SMALL LETTER IE" },
1531{ 0x0436, "zhcy", "CYRILLIC SMALL LETTER ZHE" },
1532{ 0x0437, "zcy", "CYRILLIC SMALL LETTER ZE" },
1533{ 0x0438, "icy", "CYRILLIC SMALL LETTER I" },
1534{ 0x0439, "jcy", "CYRILLIC SMALL LETTER SHORT I" },
1535{ 0x043A, "kcy", "CYRILLIC SMALL LETTER KA" },
1536{ 0x043B, "lcy", "CYRILLIC SMALL LETTER EL" },
1537{ 0x043C, "mcy", "CYRILLIC SMALL LETTER EM" },
1538{ 0x043D, "ncy", "CYRILLIC SMALL LETTER EN" },
1539{ 0x043E, "ocy", "CYRILLIC SMALL LETTER O" },
1540{ 0x043F, "pcy", "CYRILLIC SMALL LETTER PE" },
1541{ 0x0440, "rcy", "CYRILLIC SMALL LETTER ER" },
1542{ 0x0441, "scy", "CYRILLIC SMALL LETTER ES" },
1543{ 0x0442, "tcy", "CYRILLIC SMALL LETTER TE" },
1544{ 0x0443, "ucy", "CYRILLIC SMALL LETTER U" },
1545{ 0x0444, "fcy", "CYRILLIC SMALL LETTER EF" },
1546{ 0x0445, "khcy", "CYRILLIC SMALL LETTER HA" },
1547{ 0x0446, "tscy", "CYRILLIC SMALL LETTER TSE" },
1548{ 0x0447, "chcy", "CYRILLIC SMALL LETTER CHE" },
1549{ 0x0448, "shcy", "CYRILLIC SMALL LETTER SHA" },
1550{ 0x0449, "shchcy", "CYRILLIC SMALL LETTER SHCHA" },
1551{ 0x044A, "hardcy", "CYRILLIC SMALL LETTER HARD SIGN" },
1552{ 0x044B, "ycy", "CYRILLIC SMALL LETTER YERU" },
1553{ 0x044C, "softcy", "CYRILLIC SMALL LETTER SOFT SIGN" },
1554{ 0x044D, "ecy", "CYRILLIC SMALL LETTER E" },
1555{ 0x044E, "yucy", "CYRILLIC SMALL LETTER YU" },
1556{ 0x044F, "yacy", "CYRILLIC SMALL LETTER YA" },
1557{ 0x0451, "iocy", "CYRILLIC SMALL LETTER IO" },
1558{ 0x0452, "djcy", "CYRILLIC SMALL LETTER DJE" },
1559{ 0x0453, "gjcy", "CYRILLIC SMALL LETTER GJE" },
1560{ 0x0454, "jukcy", "CYRILLIC SMALL LETTER UKRAINIAN IE" },
1561{ 0x0455, "dscy", "CYRILLIC SMALL LETTER DZE" },
1562{ 0x0456, "iukcy", "CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I" },
1563{ 0x0457, "yicy", "CYRILLIC SMALL LETTER YI" },
1564{ 0x0458, "jsercy", "CYRILLIC SMALL LETTER JE" },
1565{ 0x0459, "ljcy", "CYRILLIC SMALL LETTER LJE" },
1566{ 0x045A, "njcy", "CYRILLIC SMALL LETTER NJE" },
1567{ 0x045B, "tshcy", "CYRILLIC SMALL LETTER TSHE" },
1568{ 0x045C, "kjcy", "CYRILLIC SMALL LETTER KJE" },
1569{ 0x045E, "ubrcy", "CYRILLIC SMALL LETTER SHORT U" },
1570{ 0x045F, "dzcy", "CYRILLIC SMALL LETTER DZHE" },
1571{ 0x2002, "ensp", "EN SPACE" },
1572{ 0x2003, "emsp", "EM SPACE" },
1573{ 0x2004, "emsp13", "THREE-PER-EM SPACE" },
1574{ 0x2005, "emsp14", "FOUR-PER-EM SPACE" },
1575{ 0x2007, "numsp", "FIGURE SPACE" },
1576{ 0x2008, "puncsp", "PUNCTUATION SPACE" },
1577{ 0x2009, "thinsp", "THIN SPACE" },
1578{ 0x200A, "hairsp", "HAIR SPACE" },
1579{ 0x2010, "dash", "HYPHEN" },
1580{ 0x2013, "ndash", "EN DASH" },
1581{ 0x2014, "mdash", "EM DASH" },
1582{ 0x2015, "horbar", "HORIZONTAL BAR" },
1583{ 0x2016, "Verbar", "DOUBLE VERTICAL LINE" },
1584{ 0x2018, "lsquo", "" },
1585{ 0x2018, "rsquor", "" },
1586{ 0x2019, "rsquo", "RIGHT SINGLE QUOTATION MARK" },
1587{ 0x201A, "lsquor", "SINGLE LOW-9 QUOTATION MARK" },
1588{ 0x201C, "ldquo", "" },
1589{ 0x201C, "rdquor", "" },
1590{ 0x201D, "rdquo", "RIGHT DOUBLE QUOTATION MARK" },
1591{ 0x201E, "ldquor", "DOUBLE LOW-9 QUOTATION MARK" },
1592{ 0x2020, "dagger", "DAGGER" },
1593{ 0x2021, "Dagger", "DOUBLE DAGGER" },
1594{ 0x2022, "bull", "BULLET" },
1595{ 0x2025, "nldr", "TWO DOT LEADER" },
1596{ 0x2026, "hellip", "HORIZONTAL ELLIPSIS" },
1597{ 0x2026, "mldr", "HORIZONTAL ELLIPSIS" },
1598{ 0x2030, "permil", "PER MILLE SIGN" },
1599{ 0x2032, "prime", "PRIME" },
1600{ 0x2032, "vprime", "PRIME" },
1601{ 0x2033, "Prime", "DOUBLE PRIME" },
1602{ 0x2034, "tprime", "TRIPLE PRIME" },
1603{ 0x2035, "bprime", "REVERSED PRIME" },
1604{ 0x2041, "caret", "CARET" },
1605{ 0x2043, "hybull", "HYPHEN BULLET" },
1606{ 0x20DB, "tdot", "COMBINING THREE DOTS ABOVE" },
1607{ 0x20DC, "DotDot", "COMBINING FOUR DOTS ABOVE" },
1608{ 0x2105, "incare", "CARE OF" },
1609{ 0x210B, "hamilt", "SCRIPT CAPITAL H" },
1610{ 0x210F, "planck", "PLANCK CONSTANT OVER TWO PI" },
1611{ 0x2111, "image", "BLACK-LETTER CAPITAL I" },
1612{ 0x2112, "lagran", "SCRIPT CAPITAL L" },
1613{ 0x2113, "ell", "SCRIPT SMALL L" },
1614{ 0x2116, "numero", "NUMERO SIGN" },
1615{ 0x2117, "copysr", "SOUND RECORDING COPYRIGHT" },
1616{ 0x2118, "weierp", "SCRIPT CAPITAL P" },
1617{ 0x211C, "real", "BLACK-LETTER CAPITAL R" },
1618{ 0x211E, "rx", "PRESCRIPTION TAKE" },
1619{ 0x2122, "trade", "TRADE MARK SIGN" },
1620{ 0x2126, "ohm", "OHM SIGN" },
1621{ 0x212B, "angst", "ANGSTROM SIGN" },
1622{ 0x212C, "bernou", "SCRIPT CAPITAL B" },
1623{ 0x2133, "phmmat", "SCRIPT CAPITAL M" },
1624{ 0x2134, "order", "SCRIPT SMALL O" },
1625{ 0x2135, "aleph", "ALEF SYMBOL" },
1626{ 0x2136, "beth", "BET SYMBOL" },
1627{ 0x2137, "gimel", "GIMEL SYMBOL" },
1628{ 0x2138, "daleth", "DALET SYMBOL" },
1629{ 0x2153, "frac13", "VULGAR FRACTION ONE THIRD" },
1630{ 0x2154, "frac23", "VULGAR FRACTION TWO THIRDS" },
1631{ 0x2155, "frac15", "VULGAR FRACTION ONE FIFTH" },
1632{ 0x2156, "frac25", "VULGAR FRACTION TWO FIFTHS" },
1633{ 0x2157, "frac35", "VULGAR FRACTION THREE FIFTHS" },
1634{ 0x2158, "frac45", "VULGAR FRACTION FOUR FIFTHS" },
1635{ 0x2159, "frac16", "VULGAR FRACTION ONE SIXTH" },
1636{ 0x215A, "frac56", "VULGAR FRACTION FIVE SIXTHS" },
1637{ 0x215B, "frac18", "" },
1638{ 0x215C, "frac38", "" },
1639{ 0x215D, "frac58", "" },
1640{ 0x215E, "frac78", "" },
1641{ 0x2190, "larr", "LEFTWARDS DOUBLE ARROW" },
1642{ 0x2191, "uarr", "UPWARDS ARROW" },
1643{ 0x2192, "rarr", "RIGHTWARDS DOUBLE ARROW" },
1644{ 0x2193, "darr", "DOWNWARDS ARROW" },
1645{ 0x2194, "harr", "LEFT RIGHT ARROW" },
1646{ 0x2194, "xhArr", "LEFT RIGHT ARROW" },
1647{ 0x2194, "xharr", "LEFT RIGHT ARROW" },
1648{ 0x2195, "varr", "UP DOWN ARROW" },
1649{ 0x2196, "nwarr", "NORTH WEST ARROW" },
1650{ 0x2197, "nearr", "NORTH EAST ARROW" },
1651{ 0x2198, "drarr", "SOUTH EAST ARROW" },
1652{ 0x2199, "dlarr", "SOUTH WEST ARROW" },
1653{ 0x219A, "nlarr", "LEFTWARDS ARROW WITH STROKE" },
1654{ 0x219B, "nrarr", "RIGHTWARDS ARROW WITH STROKE" },
1655{ 0x219D, "rarrw", "RIGHTWARDS SQUIGGLE ARROW" },
1656{ 0x219E, "Larr", "LEFTWARDS TWO HEADED ARROW" },
1657{ 0x21A0, "Rarr", "RIGHTWARDS TWO HEADED ARROW" },
1658{ 0x21A2, "larrtl", "LEFTWARDS ARROW WITH TAIL" },
1659{ 0x21A3, "rarrtl", "RIGHTWARDS ARROW WITH TAIL" },
1660{ 0x21A6, "map", "RIGHTWARDS ARROW FROM BAR" },
1661{ 0x21A9, "larrhk", "LEFTWARDS ARROW WITH HOOK" },
1662{ 0x21AA, "rarrhk", "RIGHTWARDS ARROW WITH HOOK" },
1663{ 0x21AB, "larrlp", "LEFTWARDS ARROW WITH LOOP" },
1664{ 0x21AC, "rarrlp", "RIGHTWARDS ARROW WITH LOOP" },
1665{ 0x21AD, "harrw", "LEFT RIGHT WAVE ARROW" },
1666{ 0x21AE, "nharr", "LEFT RIGHT ARROW WITH STROKE" },
1667{ 0x21B0, "lsh", "UPWARDS ARROW WITH TIP LEFTWARDS" },
1668{ 0x21B1, "rsh", "UPWARDS ARROW WITH TIP RIGHTWARDS" },
1669{ 0x21B6, "cularr", "ANTICLOCKWISE TOP SEMICIRCLE ARROW" },
1670{ 0x21B7, "curarr", "CLOCKWISE TOP SEMICIRCLE ARROW" },
1671{ 0x21BA, "olarr", "ANTICLOCKWISE OPEN CIRCLE ARROW" },
1672{ 0x21BB, "orarr", "CLOCKWISE OPEN CIRCLE ARROW" },
1673{ 0x21BC, "lharu", "LEFTWARDS HARPOON WITH BARB UPWARDS" },
1674{ 0x21BD, "lhard", "LEFTWARDS HARPOON WITH BARB DOWNWARDS" },
1675{ 0x21BE, "uharr", "UPWARDS HARPOON WITH BARB RIGHTWARDS" },
1676{ 0x21BF, "uharl", "UPWARDS HARPOON WITH BARB LEFTWARDS" },
1677{ 0x21C0, "rharu", "RIGHTWARDS HARPOON WITH BARB UPWARDS" },
1678{ 0x21C1, "rhard", "RIGHTWARDS HARPOON WITH BARB DOWNWARDS" },
1679{ 0x21C2, "dharr", "DOWNWARDS HARPOON WITH BARB RIGHTWARDS" },
1680{ 0x21C3, "dharl", "DOWNWARDS HARPOON WITH BARB LEFTWARDS" },
1681{ 0x21C4, "rlarr2", "RIGHTWARDS ARROW OVER LEFTWARDS ARROW" },
1682{ 0x21C6, "lrarr2", "LEFTWARDS ARROW OVER RIGHTWARDS ARROW" },
1683{ 0x21C7, "larr2", "LEFTWARDS PAIRED ARROWS" },
1684{ 0x21C8, "uarr2", "UPWARDS PAIRED ARROWS" },
1685{ 0x21C9, "rarr2", "RIGHTWARDS PAIRED ARROWS" },
1686{ 0x21CA, "darr2", "DOWNWARDS PAIRED ARROWS" },
1687{ 0x21CB, "lrhar2", "LEFTWARDS HARPOON OVER RIGHTWARDS HARPOON" },
1688{ 0x21CC, "rlhar2", "RIGHTWARDS HARPOON OVER LEFTWARDS HARPOON" },
1689{ 0x21CD, "nlArr", "LEFTWARDS DOUBLE ARROW WITH STROKE" },
1690{ 0x21CE, "nhArr", "LEFT RIGHT DOUBLE ARROW WITH STROKE" },
1691{ 0x21CF, "nrArr", "RIGHTWARDS DOUBLE ARROW WITH STROKE" },
1692{ 0x21D0, "lArr", "LEFTWARDS ARROW" },
1693{ 0x21D0, "xlArr", "LEFTWARDS DOUBLE ARROW" },
1694{ 0x21D1, "uArr", "UPWARDS DOUBLE ARROW" },
1695{ 0x21D2, "rArr", "RIGHTWARDS ARROW" },
1696{ 0x21D2, "xrArr", "RIGHTWARDS DOUBLE ARROW" },
1697{ 0x21D3, "dArr", "DOWNWARDS DOUBLE ARROW" },
1698{ 0x21D4, "hArr", "" },
1699{ 0x21D4, "iff", "LEFT RIGHT DOUBLE ARROW" },
1700{ 0x21D5, "vArr", "UP DOWN DOUBLE ARROW" },
1701{ 0x21DA, "lAarr", "LEFTWARDS TRIPLE ARROW" },
1702{ 0x21DB, "rAarr", "RIGHTWARDS TRIPLE ARROW" },
1703{ 0x2200, "forall", "" },
1704{ 0x2201, "comp", "COMPLEMENT" },
1705{ 0x2202, "part", "" },
1706{ 0x2203, "exist", "" },
1707{ 0x2204, "nexist", "THERE DOES NOT EXIST" },
1708{ 0x2205, "empty", "" },
1709{ 0x2207, "nabla", "NABLA" },
1710{ 0x2209, "notin", "" },
1711{ 0x220A, "epsi", "" },
1712{ 0x220A, "epsis", "" },
1713{ 0x220A, "isin", "" },
1714{ 0x220D, "bepsi", "SMALL CONTAINS AS MEMBER" },
1715{ 0x220D, "ni", "" },
1716{ 0x220F, "prod", "N-ARY PRODUCT" },
1717{ 0x2210, "amalg", "N-ARY COPRODUCT" },
1718{ 0x2210, "coprod", "N-ARY COPRODUCT" },
1719{ 0x2210, "samalg", "" },
1720{ 0x2211, "sum", "N-ARY SUMMATION" },
1721{ 0x2212, "minus", "MINUS SIGN" },
1722{ 0x2213, "mnplus", "" },
1723{ 0x2214, "plusdo", "DOT PLUS" },
1724{ 0x2216, "setmn", "SET MINUS" },
1725{ 0x2216, "ssetmn", "SET MINUS" },
1726{ 0x2217, "lowast", "ASTERISK OPERATOR" },
1727{ 0x2218, "compfn", "RING OPERATOR" },
1728{ 0x221A, "radic", "" },
1729{ 0x221D, "prop", "" },
1730{ 0x221D, "vprop", "" },
1731{ 0x221E, "infin", "" },
1732{ 0x221F, "ang90", "RIGHT ANGLE" },
1733{ 0x2220, "ang", "ANGLE" },
1734{ 0x2221, "angmsd", "MEASURED ANGLE" },
1735{ 0x2222, "angsph", "" },
1736{ 0x2223, "mid", "" },
1737{ 0x2224, "nmid", "DOES NOT DIVIDE" },
1738{ 0x2225, "par", "PARALLEL TO" },
1739{ 0x2225, "spar", "PARALLEL TO" },
1740{ 0x2226, "npar", "NOT PARALLEL TO" },
1741{ 0x2226, "nspar", "NOT PARALLEL TO" },
1742{ 0x2227, "and", "" },
1743{ 0x2228, "or", "" },
1744{ 0x2229, "cap", "" },
1745{ 0x222A, "cup", "" },
1746{ 0x222B, "int", "" },
1747{ 0x222E, "conint", "" },
1748{ 0x2234, "there4", "" },
1749{ 0x2235, "becaus", "BECAUSE" },
1750{ 0x223C, "sim", "" },
1751{ 0x223C, "thksim", "TILDE OPERATOR" },
1752{ 0x223D, "bsim", "" },
1753{ 0x2240, "wreath", "WREATH PRODUCT" },
1754{ 0x2241, "nsim", "" },
1755{ 0x2243, "sime", "" },
1756{ 0x2244, "nsime", "" },
1757{ 0x2245, "cong", "" },
1758{ 0x2247, "ncong", "NEITHER APPROXIMATELY NOR ACTUALLY EQUAL TO" },
1759{ 0x2248, "ap", "" },
1760{ 0x2248, "thkap", "ALMOST EQUAL TO" },
1761{ 0x2249, "nap", "NOT ALMOST EQUAL TO" },
1762{ 0x224A, "ape", "" },
1763{ 0x224C, "bcong", "ALL EQUAL TO" },
1764{ 0x224D, "asymp", "EQUIVALENT TO" },
1765{ 0x224E, "bump", "" },
1766{ 0x224F, "bumpe", "" },
1767{ 0x2250, "esdot", "" },
1768{ 0x2251, "eDot", "" },
1769{ 0x2252, "efDot", "" },
1770{ 0x2253, "erDot", "" },
1771{ 0x2254, "colone", "" },
1772{ 0x2255, "ecolon", "" },
1773{ 0x2256, "ecir", "" },
1774{ 0x2257, "cire", "" },
1775{ 0x2259, "wedgeq", "ESTIMATES" },
1776{ 0x225C, "trie", "" },
1777{ 0x2260, "ne", "" },
1778{ 0x2261, "equiv", "" },
1779{ 0x2262, "nequiv", "NOT IDENTICAL TO" },
1780{ 0x2264, "le", "" },
1781{ 0x2264, "les", "LESS-THAN OR EQUAL TO" },
1782{ 0x2265, "ge", "GREATER-THAN OR EQUAL TO" },
1783{ 0x2265, "ges", "GREATER-THAN OR EQUAL TO" },
1784{ 0x2266, "lE", "" },
1785{ 0x2267, "gE", "" },
1786{ 0x2268, "lnE", "" },
1787{ 0x2268, "lne", "" },
1788{ 0x2268, "lvnE", "LESS-THAN BUT NOT EQUAL TO" },
1789{ 0x2269, "gnE", "" },
1790{ 0x2269, "gne", "" },
1791{ 0x2269, "gvnE", "GREATER-THAN BUT NOT EQUAL TO" },
1792{ 0x226A, "Lt", "MUCH LESS-THAN" },
1793{ 0x226B, "Gt", "MUCH GREATER-THAN" },
1794{ 0x226C, "twixt", "BETWEEN" },
1795{ 0x226E, "nlt", "NOT LESS-THAN" },
1796{ 0x226F, "ngt", "NOT GREATER-THAN" },
1797{ 0x2270, "nlE", "" },
1798{ 0x2270, "nle", "NEITHER LESS-THAN NOR EQUAL TO" },
1799{ 0x2270, "nles", "" },
1800{ 0x2271, "ngE", "" },
1801{ 0x2271, "nge", "NEITHER GREATER-THAN NOR EQUAL TO" },
1802{ 0x2271, "nges", "" },
1803{ 0x2272, "lap", "LESS-THAN OR EQUIVALENT TO" },
1804{ 0x2272, "lsim", "LESS-THAN OR EQUIVALENT TO" },
1805{ 0x2273, "gap", "GREATER-THAN OR EQUIVALENT TO" },
1806{ 0x2273, "gsim", "GREATER-THAN OR EQUIVALENT TO" },
1807{ 0x2276, "lg", "LESS-THAN OR GREATER-THAN" },
1808{ 0x2277, "gl", "" },
1809{ 0x227A, "pr", "" },
1810{ 0x227B, "sc", "" },
1811{ 0x227C, "cupre", "" },
1812{ 0x227C, "pre", "" },
1813{ 0x227D, "sccue", "" },
1814{ 0x227D, "sce", "" },
1815{ 0x227E, "prap", "" },
1816{ 0x227E, "prsim", "" },
1817{ 0x227F, "scap", "" },
1818{ 0x227F, "scsim", "" },
1819{ 0x2280, "npr", "DOES NOT PRECEDE" },
1820{ 0x2281, "nsc", "DOES NOT SUCCEED" },
1821{ 0x2282, "sub", "" },
1822{ 0x2283, "sup", "" },
1823{ 0x2284, "nsub", "NOT A SUBSET OF" },
1824{ 0x2285, "nsup", "NOT A SUPERSET OF" },
1825{ 0x2286, "subE", "" },
1826{ 0x2286, "sube", "" },
1827{ 0x2287, "supE", "" },
1828{ 0x2287, "supe", "" },
1829{ 0x2288, "nsubE", "" },
1830{ 0x2288, "nsube", "" },
1831{ 0x2289, "nsupE", "" },
1832{ 0x2289, "nsupe", "" },
1833{ 0x228A, "subne", "" },
1834{ 0x228A, "subnE", "SUBSET OF WITH NOT EQUAL TO" },
1835{ 0x228A, "vsubne", "SUBSET OF WITH NOT EQUAL TO" },
1836{ 0x228B, "supnE", "" },
1837{ 0x228B, "supne", "" },
1838{ 0x228B, "vsupnE", "SUPERSET OF WITH NOT EQUAL TO" },
1839{ 0x228B, "vsupne", "SUPERSET OF WITH NOT EQUAL TO" },
1840{ 0x228E, "uplus", "MULTISET UNION" },
1841{ 0x228F, "sqsub", "" },
1842{ 0x2290, "sqsup", "" },
1843{ 0x2291, "sqsube", "" },
1844{ 0x2292, "sqsupe", "" },
1845{ 0x2293, "sqcap", "SQUARE CAP" },
1846{ 0x2294, "sqcup", "SQUARE CUP" },
1847{ 0x2295, "oplus", "CIRCLED PLUS" },
1848{ 0x2296, "ominus", "CIRCLED MINUS" },
1849{ 0x2297, "otimes", "CIRCLED TIMES" },
1850{ 0x2298, "osol", "CIRCLED DIVISION SLASH" },
1851{ 0x2299, "odot", "CIRCLED DOT OPERATOR" },
1852{ 0x229A, "ocir", "CIRCLED RING OPERATOR" },
1853{ 0x229B, "oast", "CIRCLED ASTERISK OPERATOR" },
1854{ 0x229D, "odash", "CIRCLED DASH" },
1855{ 0x229E, "plusb", "SQUARED PLUS" },
1856{ 0x229F, "minusb", "SQUARED MINUS" },
1857{ 0x22A0, "timesb", "SQUARED TIMES" },
1858{ 0x22A1, "sdotb", "SQUARED DOT OPERATOR" },
1859{ 0x22A2, "vdash", "" },
1860{ 0x22A3, "dashv", "" },
1861{ 0x22A4, "top", "DOWN TACK" },
1862{ 0x22A5, "bottom", "" },
1863{ 0x22A5, "perp", "" },
1864{ 0x22A7, "models", "MODELS" },
1865{ 0x22A8, "vDash", "" },
1866{ 0x22A9, "Vdash", "" },
1867{ 0x22AA, "Vvdash", "" },
1868{ 0x22AC, "nvdash", "DOES NOT PROVE" },
1869{ 0x22AD, "nvDash", "NOT TRUE" },
1870{ 0x22AE, "nVdash", "DOES NOT FORCE" },
1871{ 0x22AF, "nVDash", "NEGATED DOUBLE VERTICAL BAR DOUBLE RIGHT TURNSTILE" },
1872{ 0x22B2, "vltri", "" },
1873{ 0x22B3, "vrtri", "" },
1874{ 0x22B4, "ltrie", "" },
1875{ 0x22B5, "rtrie", "" },
1876{ 0x22B8, "mumap", "MULTIMAP" },
1877{ 0x22BA, "intcal", "INTERCALATE" },
1878{ 0x22BB, "veebar", "" },
1879{ 0x22BC, "barwed", "NAND" },
1880{ 0x22C4, "diam", "DIAMOND OPERATOR" },
1881{ 0x22C5, "sdot", "DOT OPERATOR" },
1882{ 0x22C6, "sstarf", "STAR OPERATOR" },
1883{ 0x22C6, "star", "STAR OPERATOR" },
1884{ 0x22C7, "divonx", "DIVISION TIMES" },
1885{ 0x22C8, "bowtie", "" },
1886{ 0x22C9, "ltimes", "LEFT NORMAL FACTOR SEMIDIRECT PRODUCT" },
1887{ 0x22CA, "rtimes", "RIGHT NORMAL FACTOR SEMIDIRECT PRODUCT" },
1888{ 0x22CB, "lthree", "LEFT SEMIDIRECT PRODUCT" },
1889{ 0x22CC, "rthree", "RIGHT SEMIDIRECT PRODUCT" },
1890{ 0x22CD, "bsime", "" },
1891{ 0x22CE, "cuvee", "CURLY LOGICAL OR" },
1892{ 0x22CF, "cuwed", "CURLY LOGICAL AND" },
1893{ 0x22D0, "Sub", "" },
1894{ 0x22D1, "Sup", "" },
1895{ 0x22D2, "Cap", "DOUBLE INTERSECTION" },
1896{ 0x22D3, "Cup", "DOUBLE UNION" },
1897{ 0x22D4, "fork", "" },
1898{ 0x22D6, "ldot", "" },
1899{ 0x22D7, "gsdot", "" },
1900{ 0x22D8, "Ll", "" },
1901{ 0x22D9, "Gg", "VERY MUCH GREATER-THAN" },
1902{ 0x22DA, "lEg", "" },
1903{ 0x22DA, "leg", "" },
1904{ 0x22DB, "gEl", "" },
1905{ 0x22DB, "gel", "" },
1906{ 0x22DC, "els", "" },
1907{ 0x22DD, "egs", "" },
1908{ 0x22DE, "cuepr", "" },
1909{ 0x22DF, "cuesc", "" },
1910{ 0x22E0, "npre", "DOES NOT PRECEDE OR EQUAL" },
1911{ 0x22E1, "nsce", "DOES NOT SUCCEED OR EQUAL" },
1912{ 0x22E6, "lnsim", "" },
1913{ 0x22E7, "gnsim", "GREATER-THAN BUT NOT EQUIVALENT TO" },
1914{ 0x22E8, "prnap", "" },
1915{ 0x22E8, "prnsim", "" },
1916{ 0x22E9, "scnap", "" },
1917{ 0x22E9, "scnsim", "" },
1918{ 0x22EA, "nltri", "NOT NORMAL SUBGROUP OF" },
1919{ 0x22EB, "nrtri", "DOES NOT CONTAIN AS NORMAL SUBGROUP" },
1920{ 0x22EC, "nltrie", "NOT NORMAL SUBGROUP OF OR EQUAL TO" },
1921{ 0x22ED, "nrtrie", "DOES NOT CONTAIN AS NORMAL SUBGROUP OR EQUAL" },
1922{ 0x22EE, "vellip", "" },
1923{ 0x2306, "Barwed", "PERSPECTIVE" },
1924{ 0x2308, "lceil", "LEFT CEILING" },
1925{ 0x2309, "rceil", "RIGHT CEILING" },
1926{ 0x230A, "lfloor", "LEFT FLOOR" },
1927{ 0x230B, "rfloor", "RIGHT FLOOR" },
1928{ 0x230C, "drcrop", "BOTTOM RIGHT CROP" },
1929{ 0x230D, "dlcrop", "BOTTOM LEFT CROP" },
1930{ 0x230E, "urcrop", "TOP RIGHT CROP" },
1931{ 0x230F, "ulcrop", "TOP LEFT CROP" },
1932{ 0x2315, "telrec", "TELEPHONE RECORDER" },
1933{ 0x2316, "target", "POSITION INDICATOR" },
1934{ 0x231C, "ulcorn", "TOP LEFT CORNER" },
1935{ 0x231D, "urcorn", "TOP RIGHT CORNER" },
1936{ 0x231E, "dlcorn", "BOTTOM LEFT CORNER" },
1937{ 0x231F, "drcorn", "BOTTOM RIGHT CORNER" },
1938{ 0x2322, "frown", "" },
1939{ 0x2322, "sfrown", "FROWN" },
1940{ 0x2323, "smile", "" },
1941{ 0x2323, "ssmile", "SMILE" },
1942{ 0x2423, "blank", "OPEN BOX" },
1943{ 0x24C8, "oS", "CIRCLED LATIN CAPITAL LETTER S" },
1944{ 0x2500, "boxh", "BOX DRAWINGS LIGHT HORIZONTAL" },
1945{ 0x2502, "boxv", "BOX DRAWINGS LIGHT VERTICAL" },
1946{ 0x250C, "boxdr", "BOX DRAWINGS LIGHT DOWN AND RIGHT" },
1947{ 0x2510, "boxdl", "BOX DRAWINGS LIGHT DOWN AND LEFT" },
1948{ 0x2514, "boxur", "BOX DRAWINGS LIGHT UP AND RIGHT" },
1949{ 0x2518, "boxul", "BOX DRAWINGS LIGHT UP AND LEFT" },
1950{ 0x251C, "boxvr", "BOX DRAWINGS LIGHT VERTICAL AND RIGHT" },
1951{ 0x2524, "boxvl", "BOX DRAWINGS LIGHT VERTICAL AND LEFT" },
1952{ 0x252C, "boxhd", "BOX DRAWINGS LIGHT DOWN AND HORIZONTAL" },
1953{ 0x2534, "boxhu", "BOX DRAWINGS LIGHT UP AND HORIZONTAL" },
1954{ 0x253C, "boxvh", "BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL" },
1955{ 0x2550, "boxH", "BOX DRAWINGS DOUBLE HORIZONTAL" },
1956{ 0x2551, "boxV", "BOX DRAWINGS DOUBLE VERTICAL" },
1957{ 0x2552, "boxDR", "BOX DRAWINGS DOWN SINGLE AND RIGHT DOUBLE" },
1958{ 0x2553, "boxDr", "BOX DRAWINGS DOWN DOUBLE AND RIGHT SINGLE" },
1959{ 0x2554, "boxdR", "BOX DRAWINGS DOUBLE DOWN AND RIGHT" },
1960{ 0x2555, "boxDL", "BOX DRAWINGS DOWN SINGLE AND LEFT DOUBLE" },
1961{ 0x2556, "boxdL", "BOX DRAWINGS DOWN DOUBLE AND LEFT SINGLE" },
1962{ 0x2557, "boxDl", "BOX DRAWINGS DOUBLE DOWN AND LEFT" },
1963{ 0x2558, "boxUR", "BOX DRAWINGS UP SINGLE AND RIGHT DOUBLE" },
1964{ 0x2559, "boxuR", "BOX DRAWINGS UP DOUBLE AND RIGHT SINGLE" },
1965{ 0x255A, "boxUr", "BOX DRAWINGS DOUBLE UP AND RIGHT" },
1966{ 0x255B, "boxUL", "BOX DRAWINGS UP SINGLE AND LEFT DOUBLE" },
1967{ 0x255C, "boxUl", "BOX DRAWINGS UP DOUBLE AND LEFT SINGLE" },
1968{ 0x255D, "boxuL", "BOX DRAWINGS DOUBLE UP AND LEFT" },
1969{ 0x255E, "boxvR", "BOX DRAWINGS VERTICAL SINGLE AND RIGHT DOUBLE" },
1970{ 0x255F, "boxVR", "BOX DRAWINGS VERTICAL DOUBLE AND RIGHT SINGLE" },
1971{ 0x2560, "boxVr", "BOX DRAWINGS DOUBLE VERTICAL AND RIGHT" },
1972{ 0x2561, "boxvL", "BOX DRAWINGS VERTICAL SINGLE AND LEFT DOUBLE" },
1973{ 0x2562, "boxVL", "BOX DRAWINGS VERTICAL DOUBLE AND LEFT SINGLE" },
1974{ 0x2563, "boxVl", "BOX DRAWINGS DOUBLE VERTICAL AND LEFT" },
1975{ 0x2564, "boxhD", "BOX DRAWINGS DOWN SINGLE AND HORIZONTAL DOUBLE" },
1976{ 0x2565, "boxHD", "BOX DRAWINGS DOWN DOUBLE AND HORIZONTAL SINGLE" },
1977{ 0x2566, "boxHd", "BOX DRAWINGS DOUBLE DOWN AND HORIZONTAL" },
1978{ 0x2567, "boxhU", "BOX DRAWINGS UP SINGLE AND HORIZONTAL DOUBLE" },
1979{ 0x2568, "boxHU", "BOX DRAWINGS UP DOUBLE AND HORIZONTAL SINGLE" },
1980{ 0x2569, "boxHu", "BOX DRAWINGS DOUBLE UP AND HORIZONTAL" },
1981{ 0x256A, "boxvH", "BOX DRAWINGS VERTICAL SINGLE AND HORIZONTAL DOUBLE" },
1982{ 0x256B, "boxVH", "BOX DRAWINGS VERTICAL DOUBLE AND HORIZONTAL SINGLE" },
1983{ 0x256C, "boxVh", "BOX DRAWINGS DOUBLE VERTICAL AND HORIZONTAL" },
1984{ 0x2580, "uhblk", "UPPER HALF BLOCK" },
1985{ 0x2584, "lhblk", "LOWER HALF BLOCK" },
1986{ 0x2588, "block", "FULL BLOCK" },
1987{ 0x2591, "blk14", "LIGHT SHADE" },
1988{ 0x2592, "blk12", "MEDIUM SHADE" },
1989{ 0x2593, "blk34", "DARK SHADE" },
1990{ 0x25A1, "square", "WHITE SQUARE" },
1991{ 0x25A1, "squ", "WHITE SQUARE" },
1992{ 0x25AA, "squf", "" },
1993{ 0x25AD, "rect", "WHITE RECTANGLE" },
1994{ 0x25AE, "marker", "BLACK VERTICAL RECTANGLE" },
1995{ 0x25B3, "xutri", "WHITE UP-POINTING TRIANGLE" },
1996{ 0x25B4, "utrif", "BLACK UP-POINTING TRIANGLE" },
1997{ 0x25B5, "utri", "WHITE UP-POINTING TRIANGLE" },
1998{ 0x25B8, "rtrif", "BLACK RIGHT-POINTING TRIANGLE" },
1999{ 0x25B9, "rtri", "WHITE RIGHT-POINTING TRIANGLE" },
2000{ 0x25BD, "xdtri", "WHITE DOWN-POINTING TRIANGLE" },
2001{ 0x25BE, "dtrif", "BLACK DOWN-POINTING TRIANGLE" },
2002{ 0x25BF, "dtri", "WHITE DOWN-POINTING TRIANGLE" },
2003{ 0x25C2, "ltrif", "BLACK LEFT-POINTING TRIANGLE" },
2004{ 0x25C3, "ltri", "WHITE LEFT-POINTING TRIANGLE" },
2005{ 0x25CA, "loz", "LOZENGE" },
2006{ 0x25CB, "cir", "WHITE CIRCLE" },
2007{ 0x25CB, "xcirc", "WHITE CIRCLE" },
2008{ 0x2605, "starf", "BLACK STAR" },
2009{ 0x260E, "phone", "TELEPHONE SIGN" },
2010{ 0x2640, "female", "" },
2011{ 0x2642, "male", "MALE SIGN" },
2012{ 0x2660, "spades", "BLACK SPADE SUIT" },
2013{ 0x2663, "clubs", "BLACK CLUB SUIT" },
2014{ 0x2665, "hearts", "BLACK HEART SUIT" },
2015{ 0x2666, "diams", "BLACK DIAMOND SUIT" },
2016{ 0x2669, "sung", "" },
2017{ 0x266D, "flat", "MUSIC FLAT SIGN" },
2018{ 0x266E, "natur", "MUSIC NATURAL SIGN" },
2019{ 0x266F, "sharp", "MUSIC SHARP SIGN" },
2020{ 0x2713, "check", "CHECK MARK" },
2021{ 0x2717, "cross", "BALLOT X" },
2022{ 0x2720, "malt", "MALTESE CROSS" },
2023{ 0x2726, "lozf", "" },
2024{ 0x2736, "sext", "SIX POINTED BLACK STAR" },
2025{ 0x3008, "lang", "" },
2026{ 0x3009, "rang", "" },
2027{ 0xE291, "rpargt", "" },
2028{ 0xE2A2, "lnap", "" },
2029{ 0xE2AA, "nsmid", "" },
2030{ 0xE2B3, "prnE", "" },
2031{ 0xE2B5, "scnE", "" },
2032{ 0xE2B8, "vsubnE", "" },
2033{ 0xE301, "smid", "" },
2034{ 0xE411, "gnap", "" },
2035{ 0xFB00, "fflig", "" },
2036{ 0xFB01, "filig", "" },
2037{ 0xFB02, "fllig", "" },
2038{ 0xFB03, "ffilig", "" },
2039{ 0xFB04, "ffllig", "" },
2040{ 0xFE68, "sbsol", "SMALL REVERSE SOLIDUS" },
2041};
2042
2043/************************************************************************
2044 *                                                                     *
2045 *             Commodity functions to handle entities                  *
2046 *                                                                     *
2047 ************************************************************************/
2048
2049/*
2050 * Macro used to grow the current buffer.
2051 */
2052#define growBuffer(buffer) {                                           \
2053    buffer##_size *= 2;                                                \
2054    buffer = (xmlChar *) xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
2055    if (buffer == NULL) {                                              \
2056       xmlGenericError(xmlGenericErrorContext, "realloc failed");      \
2057       return(NULL);                                                   \
2058    }                                                                  \
2059}
2060
2061/**
2062 * docbEntityLookup:
2063 * @name: the entity name
2064 *
2065 * Lookup the given entity in EntitiesTable
2066 *
2067 * TODO: the linear scan is really ugly, an hash table is really needed.
2068 *
2069 * Returns the associated docbEntityDescPtr if found, NULL otherwise.
2070 */
2071static docbEntityDescPtr
2072docbEntityLookup(const xmlChar *name) {
2073    unsigned int i;
2074
2075    for (i = 0;i < (sizeof(docbookEntitiesTable)/
2076                    sizeof(docbookEntitiesTable[0]));i++) {
2077        if (xmlStrEqual(name, BAD_CAST docbookEntitiesTable[i].name)) {
2078#ifdef DEBUG
2079            xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", name);
2080#endif
2081            return(&docbookEntitiesTable[i]);
2082       }
2083    }
2084    return(NULL);
2085}
2086
2087/**
2088 * docbEntityValueLookup:
2089 * @value: the entity's unicode value
2090 *
2091 * Lookup the given entity in EntitiesTable
2092 *
2093 * TODO: the linear scan is really ugly, an hash table is really needed.
2094 *
2095 * Returns the associated docbEntityDescPtr if found, NULL otherwise.
2096 */
2097static docbEntityDescPtr
2098docbEntityValueLookup(int value) {
2099    unsigned int i;
2100#ifdef DEBUG
2101    int lv = 0;
2102#endif
2103
2104    for (i = 0;i < (sizeof(docbookEntitiesTable)/
2105                    sizeof(docbookEntitiesTable[0]));i++) {
2106        if (docbookEntitiesTable[i].value >= value) {
2107           if (docbookEntitiesTable[i].value > value)
2108               break;
2109#ifdef DEBUG
2110           xmlGenericError(xmlGenericErrorContext,"Found entity %s\n", docbookEntitiesTable[i].name);
2111#endif
2112            return(&docbookEntitiesTable[i]);
2113       }
2114#ifdef DEBUG
2115       if (lv > docbookEntitiesTable[i].value) {
2116           xmlGenericError(xmlGenericErrorContext,
2117                   "docbookEntitiesTable[] is not sorted (%d > %d)!\n",
2118                   lv, docbookEntitiesTable[i].value);
2119       }
2120       lv = docbookEntitiesTable[i].value;
2121#endif
2122    }
2123    return(NULL);
2124}
2125
2126#if 0
2127/**
2128 * UTF8ToSgml:
2129 * @out:  a pointer to an array of bytes to store the result
2130 * @outlen:  the length of @out
2131 * @in:  a pointer to an array of UTF-8 chars
2132 * @inlen:  the length of @in
2133 *
2134 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2135 * plus SGML entities block of chars out.
2136 *
2137 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2138 * The value of @inlen after return is the number of octets consumed
2139 *     as the return value is positive, else unpredictable.
2140 * The value of @outlen after return is the number of octets consumed.
2141 */
2142int
2143UTF8ToSgml(unsigned char* out, int *outlen,
2144           const unsigned char* in, int *inlen) {
2145    const unsigned char* processed = in;
2146    const unsigned char* outend;
2147    const unsigned char* outstart = out;
2148    const unsigned char* instart = in;
2149    const unsigned char* inend;
2150    unsigned int c, d;
2151    int trailing;
2152
2153    if (in == NULL) {
2154        /*
2155        * initialization nothing to do
2156        */
2157       *outlen = 0;
2158       *inlen = 0;
2159       return(0);
2160    }
2161    inend = in + (*inlen);
2162    outend = out + (*outlen);
2163    while (in < inend) {
2164       d = *in++;
2165       if      (d < 0x80)  { c= d; trailing= 0; }
2166       else if (d < 0xC0) {
2167           /* trailing byte in leading position */
2168           *outlen = out - outstart;
2169           *inlen = processed - instart;
2170           return(-2);
2171        } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
2172        else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
2173        else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
2174       else {
2175           /* no chance for this in Ascii */
2176           *outlen = out - outstart;
2177           *inlen = processed - instart;
2178           return(-2);
2179       }
2180
2181       if (inend - in < trailing) {
2182           break;
2183       }
2184
2185       for ( ; trailing; trailing--) {
2186           if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
2187               break;
2188           c <<= 6;
2189           c |= d & 0x3F;
2190       }
2191
2192       /* assertion: c is a single UTF-4 value */
2193       if (c < 0x80) {
2194           if (out + 1 >= outend)
2195               break;
2196           *out++ = c;
2197       } else {
2198           int len;
2199           docbEntityDescPtr ent;
2200
2201           /*
2202            * Try to lookup a predefined SGML entity for it
2203            */
2204
2205           ent = docbEntityValueLookup(c);
2206           if (ent == NULL) {
2207               /* no chance for this in Ascii */
2208               *outlen = out - outstart;
2209               *inlen = processed - instart;
2210               return(-2);
2211           }
2212           len = strlen(ent->name);
2213           if (out + 2 + len >= outend)
2214               break;
2215           *out++ = '&';
2216           memcpy(out, ent->name, len);
2217           out += len;
2218           *out++ = ';';
2219       }
2220       processed = in;
2221    }
2222    *outlen = out - outstart;
2223    *inlen = processed - instart;
2224    return(0);
2225}
2226#endif
2227
2228/**
2229 * docbEncodeEntities:
2230 * @out:  a pointer to an array of bytes to store the result
2231 * @outlen:  the length of @out
2232 * @in:  a pointer to an array of UTF-8 chars
2233 * @inlen:  the length of @in
2234 * @quoteChar: the quote character to escape (' or ") or zero.
2235 *
2236 * Take a block of UTF-8 chars in and try to convert it to an ASCII
2237 * plus SGML entities block of chars out.
2238 *
2239 * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
2240 * The value of @inlen after return is the number of octets consumed
2241 *     as the return value is positive, else unpredictable.
2242 * The value of @outlen after return is the number of octets consumed.
2243 */
2244int
2245docbEncodeEntities(unsigned char* out, int *outlen,
2246                  const unsigned char* in, int *inlen, int quoteChar) {
2247    const unsigned char* processed = in;
2248    const unsigned char* outend = out + (*outlen);
2249    const unsigned char* outstart = out;
2250    const unsigned char* instart = in;
2251    const unsigned char* inend = in + (*inlen);
2252    unsigned int c, d;
2253    int trailing;
2254
2255    while (in < inend) {
2256       d = *in++;
2257       if      (d < 0x80)  { c= d; trailing= 0; }
2258       else if (d < 0xC0) {
2259           /* trailing byte in leading position */
2260           *outlen = out - outstart;
2261           *inlen = processed - instart;
2262           return(-2);
2263        } else if (d < 0xE0)  { c= d & 0x1F; trailing= 1; }
2264        else if (d < 0xF0)  { c= d & 0x0F; trailing= 2; }
2265        else if (d < 0xF8)  { c= d & 0x07; trailing= 3; }
2266       else {
2267           /* no chance for this in Ascii */
2268           *outlen = out - outstart;
2269           *inlen = processed - instart;
2270           return(-2);
2271       }
2272
2273       if (inend - in < trailing)
2274           break;
2275
2276       while (trailing--) {
2277           if (((d= *in++) & 0xC0) != 0x80) {
2278               *outlen = out - outstart;
2279               *inlen = processed - instart;
2280               return(-2);
2281           }
2282           c <<= 6;
2283           c |= d & 0x3F;
2284       }
2285
2286       /* assertion: c is a single UTF-4 value */
2287       if (c < 0x80 && c != (unsigned int) quoteChar && c != '&' && c != '<' && c != '>') {
2288           if (out >= outend)
2289               break;
2290           *out++ = c;
2291       } else {
2292           docbEntityDescPtr ent;
2293           const char *cp;
2294           char nbuf[16];
2295           int len;
2296
2297           /*
2298            * Try to lookup a predefined SGML entity for it
2299            */
2300           ent = docbEntityValueLookup(c);
2301           if (ent == NULL) {
2302               snprintf(nbuf, sizeof(nbuf), "#%u", c);
2303               cp = nbuf;
2304           }
2305           else
2306               cp = ent->name;
2307           len = strlen(cp);
2308           if (out + 2 + len > outend)
2309               break;
2310           *out++ = '&';
2311           memcpy(out, cp, len);
2312           out += len;
2313           *out++ = ';';
2314       }
2315       processed = in;
2316    }
2317    *outlen = out - outstart;
2318    *inlen = processed - instart;
2319    return(0);
2320}
2321
2322
2323/************************************************************************
2324 *                                                                     *
2325 *             Commodity functions to handle streams                   *
2326 *                                                                     *
2327 ************************************************************************/
2328
2329/**
2330 * docbNewInputStream:
2331 * @ctxt:  an SGML parser context
2332 *
2333 * Create a new input stream structure
2334 * Returns the new input stream or NULL
2335 */
2336static docbParserInputPtr
2337docbNewInputStream(docbParserCtxtPtr ctxt) {
2338    docbParserInputPtr input;
2339
2340    input = (xmlParserInputPtr) xmlMalloc(sizeof(docbParserInput));
2341    if (input == NULL) {
2342        ctxt->errNo = XML_ERR_NO_MEMORY;
2343       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2344           ctxt->sax->error(ctxt->userData,
2345                            "malloc: couldn't allocate a new input stream\n");
2346       return(NULL);
2347    }
2348    memset(input, 0, sizeof(docbParserInput));
2349    input->filename = NULL;
2350    input->directory = NULL;
2351    input->base = NULL;
2352    input->cur = NULL;
2353    input->buf = NULL;
2354    input->line = 1;
2355    input->col = 1;
2356    input->buf = NULL;
2357    input->free = NULL;
2358    input->version = NULL;
2359    input->consumed = 0;
2360    input->length = 0;
2361    return(input);
2362}
2363
2364
2365/************************************************************************
2366 *                                                                     *
2367 *             Commodity functions, cleanup needed ?                   *
2368 *                                                                     *
2369 ************************************************************************/
2370
2371/**
2372 * areBlanks:
2373 * @ctxt:  an SGML parser context
2374 * @str:  a xmlChar *
2375 * @len:  the size of @str
2376 *
2377 * Is this a sequence of blank chars that one can ignore ?
2378 *
2379 * Returns 1 if ignorable 0 otherwise.
2380 */
2381
2382static int areBlanks(docbParserCtxtPtr ctxt, const xmlChar *str, int len) {
2383    int i;
2384    xmlNodePtr lastChild;
2385
2386    for (i = 0;i < len;i++)
2387        if (!(IS_BLANK(str[i]))) return(0);
2388
2389    if (CUR == 0) return(1);
2390    if (CUR != '<') return(0);
2391    if (ctxt->name == NULL)
2392       return(1);
2393    if (ctxt->node == NULL) return(0);
2394    lastChild = xmlGetLastChild(ctxt->node);
2395    if (lastChild == NULL) {
2396        if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2397	    (ctxt->node->content != NULL)) return(0);
2398    } else if (xmlNodeIsText(lastChild))
2399        return(0);
2400    return(1);
2401}
2402
2403/************************************************************************
2404 *									*
2405 *                     External entities support			*
2406 *									*
2407 ************************************************************************/
2408
2409/**
2410 * docbParseCtxtExternalEntity:
2411 * @ctx:  the existing parsing context
2412 * @URL:  the URL for the entity to load
2413 * @ID:  the System ID for the entity to load
2414 * @list:  the return value for the set of parsed nodes
2415 *
2416 * Parse an external general entity within an existing parsing context
2417 *
2418 * Returns 0 if the entity is well formed, -1 in case of args problem and
2419 *    the parser error code otherwise
2420 */
2421
2422static int
2423docbParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL,
2424	                    const xmlChar *ID, xmlNodePtr *list) {
2425    xmlParserCtxtPtr ctxt;
2426    xmlDocPtr newDoc;
2427    xmlSAXHandlerPtr oldsax = NULL;
2428    int ret = 0;
2429
2430    if (ctx->depth > 40) {
2431	return(XML_ERR_ENTITY_LOOP);
2432    }
2433
2434    if (list != NULL)
2435        *list = NULL;
2436    if ((URL == NULL) && (ID == NULL))
2437	return(-1);
2438    if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */
2439	return(-1);
2440
2441
2442    ctxt = xmlCreateEntityParserCtxt(URL, ID, ctx->myDoc->URL);
2443    if (ctxt == NULL) return(-1);
2444    ctxt->userData = ctxt;
2445    oldsax = ctxt->sax;
2446    ctxt->sax = ctx->sax;
2447    newDoc = xmlNewDoc(BAD_CAST "1.0");
2448    if (newDoc == NULL) {
2449	xmlFreeParserCtxt(ctxt);
2450	return(-1);
2451    }
2452    if (ctx->myDoc != NULL) {
2453	newDoc->intSubset = ctx->myDoc->intSubset;
2454	newDoc->extSubset = ctx->myDoc->extSubset;
2455    }
2456    if (ctx->myDoc->URL != NULL) {
2457	newDoc->URL = xmlStrdup(ctx->myDoc->URL);
2458    }
2459    newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
2460    if (newDoc->children == NULL) {
2461	ctxt->sax = oldsax;
2462	xmlFreeParserCtxt(ctxt);
2463	newDoc->intSubset = NULL;
2464	newDoc->extSubset = NULL;
2465        xmlFreeDoc(newDoc);
2466	return(-1);
2467    }
2468    nodePush(ctxt, newDoc->children);
2469    if (ctx->myDoc == NULL) {
2470	ctxt->myDoc = newDoc;
2471    } else {
2472	ctxt->myDoc = ctx->myDoc;
2473	newDoc->children->doc = ctx->myDoc;
2474    }
2475
2476    /*
2477     * Parse a possible text declaration first
2478     */
2479    GROW;
2480    if ((RAW == '<') && (NXT(1) == '?') &&
2481	(NXT(2) == 'x') && (NXT(3) == 'm') &&
2482	(NXT(4) == 'l') && (IS_BLANK(NXT(5)))) {
2483	xmlParseTextDecl(ctxt);
2484    }
2485
2486    /*
2487     * Doing validity checking on chunk doesn't make sense
2488     */
2489    ctxt->instate = XML_PARSER_CONTENT;
2490    ctxt->validate = ctx->validate;
2491    ctxt->loadsubset = ctx->loadsubset;
2492    ctxt->depth = ctx->depth + 1;
2493    ctxt->replaceEntities = ctx->replaceEntities;
2494    if (ctxt->validate) {
2495	ctxt->vctxt.error = ctx->vctxt.error;
2496	ctxt->vctxt.warning = ctx->vctxt.warning;
2497	/* Allocate the Node stack */
2498	ctxt->vctxt.nodeTab = (xmlNodePtr *) xmlMalloc(4 * sizeof(xmlNodePtr));
2499	if (ctxt->vctxt.nodeTab == NULL) {
2500	    xmlGenericError(xmlGenericErrorContext,
2501		    "docbParseCtxtExternalEntity: out of memory\n");
2502	    ctxt->validate = 0;
2503	    ctxt->vctxt.error = NULL;
2504	    ctxt->vctxt.warning = NULL;
2505	} else {
2506	    ctxt->vctxt.nodeNr = 0;
2507	    ctxt->vctxt.nodeMax = 4;
2508	    ctxt->vctxt.node = NULL;
2509	}
2510    } else {
2511	ctxt->vctxt.error = NULL;
2512	ctxt->vctxt.warning = NULL;
2513    }
2514
2515    docbParseContent(ctxt);
2516
2517    if ((RAW == '<') && (NXT(1) == '/')) {
2518	ctxt->errNo = XML_ERR_NOT_WELL_BALANCED;
2519	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2520	    ctxt->sax->error(ctxt->userData,
2521		"chunk is not well balanced\n");
2522	ctxt->wellFormed = 0;
2523	ctxt->disableSAX = 1;
2524    } else if (RAW != 0) {
2525	ctxt->errNo = XML_ERR_EXTRA_CONTENT;
2526	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2527	    ctxt->sax->error(ctxt->userData,
2528		"extra content at the end of well balanced chunk\n");
2529	ctxt->wellFormed = 0;
2530	ctxt->disableSAX = 1;
2531    }
2532    if (ctxt->node != newDoc->children) {
2533	ctxt->errNo = XML_ERR_NOT_WELL_BALANCED;
2534	if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2535	    ctxt->sax->error(ctxt->userData,
2536		"chunk is not well balanced\n");
2537	ctxt->wellFormed = 0;
2538	ctxt->disableSAX = 1;
2539    }
2540
2541    if (!ctxt->wellFormed) {
2542        if (ctxt->errNo == 0)
2543	    ret = 1;
2544	else
2545	    ret = ctxt->errNo;
2546    } else {
2547	if (list != NULL) {
2548	    xmlNodePtr cur;
2549
2550	    /*
2551	     * Return the newly created nodeset after unlinking it from
2552	     * they pseudo parent.
2553	     */
2554	    cur = newDoc->children->children;
2555	    *list = cur;
2556	    while (cur != NULL) {
2557		cur->parent = NULL;
2558		cur = cur->next;
2559	    }
2560            newDoc->children->children = NULL;
2561	}
2562	ret = 0;
2563    }
2564    ctxt->sax = oldsax;
2565    xmlFreeParserCtxt(ctxt);
2566    newDoc->intSubset = NULL;
2567    newDoc->extSubset = NULL;
2568    xmlFreeDoc(newDoc);
2569
2570    return(ret);
2571}
2572
2573/************************************************************************
2574 *									*
2575 *			The parser itself				*
2576 *									*
2577 ************************************************************************/
2578
2579/**
2580 * docbParseSGMLName:
2581 * @ctxt:  an SGML parser context
2582 *
2583 * parse an SGML tag or attribute name, note that we convert it to lowercase
2584 * since SGML names are not case-sensitive.
2585 *
2586 * Returns the Tag Name parsed or NULL
2587 */
2588
2589static xmlChar *
2590docbParseSGMLName(docbParserCtxtPtr ctxt) {
2591    xmlChar *ret = NULL;
2592    int i = 0;
2593    xmlChar loc[DOCB_PARSER_BUFFER_SIZE];
2594
2595    if (!IS_LETTER(CUR) && (CUR != '_') &&
2596        (CUR != ':')) return(NULL);
2597
2598    while ((i < DOCB_PARSER_BUFFER_SIZE) &&
2599           ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
2600          (CUR == ':') || (CUR == '_'))) {
2601       if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
2602        else loc[i] = CUR;
2603       i++;
2604
2605       NEXT;
2606    }
2607
2608    ret = xmlStrndup(loc, i);
2609
2610    return(ret);
2611}
2612
2613/**
2614 * docbParseName:
2615 * @ctxt:  an SGML parser context
2616 *
2617 * parse an SGML name, this routine is case sensitive.
2618 *
2619 * Returns the Name parsed or NULL
2620 */
2621
2622static xmlChar *
2623docbParseName(docbParserCtxtPtr ctxt) {
2624    xmlChar buf[DOCB_MAX_NAMELEN];
2625    int len = 0;
2626
2627    GROW;
2628    if (!IS_LETTER(CUR) && (CUR != '_')) {
2629       return(NULL);
2630    }
2631
2632    while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
2633           (CUR == '.') || (CUR == '-') ||
2634          (CUR == '_') || (CUR == ':') ||
2635          (IS_COMBINING(CUR)) ||
2636          (IS_EXTENDER(CUR))) {
2637       buf[len++] = CUR;
2638       NEXT;
2639       if (len >= DOCB_MAX_NAMELEN) {
2640           xmlGenericError(xmlGenericErrorContext,
2641              "docbParseName: reached DOCB_MAX_NAMELEN limit\n");
2642           while ((IS_LETTER(CUR)) || (IS_DIGIT(CUR)) ||
2643                  (CUR == '.') || (CUR == '-') ||
2644                  (CUR == '_') || (CUR == ':') ||
2645                  (IS_COMBINING(CUR)) ||
2646                  (IS_EXTENDER(CUR)))
2647                NEXT;
2648           break;
2649       }
2650    }
2651    return(xmlStrndup(buf, len));
2652}
2653
2654/**
2655 * docbParseSGMLAttribute:
2656 * @ctxt:  an SGML parser context
2657 * @stop:  a char stop value
2658 *
2659 * parse an SGML attribute value till the stop (quote), if
2660 * stop is 0 then it stops at the first space
2661 *
2662 * Returns the attribute parsed or NULL
2663 */
2664
2665static xmlChar *
2666docbParseSGMLAttribute(docbParserCtxtPtr ctxt, const xmlChar stop) {
2667    xmlChar *buffer = NULL;
2668    int buffer_size = 0;
2669    xmlChar *out = NULL;
2670    xmlChar *name = NULL;
2671
2672    xmlChar *cur = NULL;
2673    xmlEntityPtr xent;
2674    docbEntityDescPtr ent;
2675
2676    /*
2677     * allocate a translation buffer.
2678     */
2679    buffer_size = DOCB_PARSER_BIG_BUFFER_SIZE;
2680    buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
2681    if (buffer == NULL) {
2682       xmlGenericError(xmlGenericErrorContext,
2683	               "docbParseSGMLAttribute: malloc failed");
2684       return(NULL);
2685    }
2686    out = buffer;
2687
2688    /*
2689     * Ok loop until we reach one of the ending chars
2690     */
2691    while ((CUR != 0) && (CUR != stop) && (CUR != '>')) {
2692       if ((stop == 0) && (IS_BLANK(CUR))) break;
2693        if (CUR == '&') {
2694           if (NXT(1) == '#') {
2695               unsigned int c;
2696               int bits;
2697
2698               c = docbParseCharRef(ctxt);
2699               if      (c <    0x80)
2700                       { *out++  = c;                bits= -6; }
2701               else if (c <   0x800)
2702                       { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2703               else if (c < 0x10000)
2704                       { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2705               else
2706                       { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2707
2708               for ( ; bits >= 0; bits-= 6) {
2709                   *out++  = ((c >> bits) & 0x3F) | 0x80;
2710               }
2711           } else {
2712               xent = docbParseEntityRef(ctxt, &name);
2713               if (name == NULL) {
2714                   *out++ = '&';
2715                   if (out - buffer > buffer_size - 100) {
2716                       int indx = out - buffer;
2717
2718                       growBuffer(buffer);
2719                       out = &buffer[indx];
2720                   }
2721                   *out++ = '&';
2722               } else {
2723		   ent = docbEntityLookup(name);
2724		   if (ent == NULL) {
2725		       *out++ = '&';
2726		       cur = name;
2727		       while (*cur != 0) {
2728			   if (out - buffer > buffer_size - 100) {
2729			       int indx = out - buffer;
2730
2731			       growBuffer(buffer);
2732			       out = &buffer[indx];
2733			   }
2734			   *out++ = *cur++;
2735		       }
2736		       xmlFree(name);
2737		   } else {
2738		       unsigned int c;
2739		       int bits;
2740
2741		       if (out - buffer > buffer_size - 100) {
2742			   int indx = out - buffer;
2743
2744			   growBuffer(buffer);
2745			   out = &buffer[indx];
2746		       }
2747		       c = (xmlChar)ent->value;
2748		       if      (c <    0x80)
2749			   { *out++  = c;                bits= -6; }
2750		       else if (c <   0x800)
2751			   { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2752		       else if (c < 0x10000)
2753			   { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2754		       else
2755			   { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2756
2757		       for ( ; bits >= 0; bits-= 6) {
2758			   *out++  = ((c >> bits) & 0x3F) | 0x80;
2759		       }
2760		       xmlFree(name);
2761		   }
2762	       }
2763           }
2764       } else {
2765           unsigned int c;
2766           int bits;
2767
2768           if (out - buffer > buffer_size - 100) {
2769               int indx = out - buffer;
2770
2771               growBuffer(buffer);
2772               out = &buffer[indx];
2773           }
2774           c = CUR;
2775           if      (c <    0x80)
2776                   { *out++  = c;                bits= -6; }
2777           else if (c <   0x800)
2778                   { *out++  =((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
2779           else if (c < 0x10000)
2780                   { *out++  =((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
2781           else
2782                   { *out++  =((c >> 18) & 0x07) | 0xF0;  bits= 12; }
2783
2784           for ( ; bits >= 0; bits-= 6) {
2785               *out++  = ((c >> bits) & 0x3F) | 0x80;
2786           }
2787           NEXT;
2788       }
2789    }
2790    *out++ = 0;
2791    return(buffer);
2792}
2793
2794
2795/**
2796 * docbParseEntityRef:
2797 * @ctxt:  an SGML parser context
2798 * @str:  location to store the entity name
2799 *
2800 * parse an SGML ENTITY references
2801 *
2802 * [68] EntityRef ::= '&' Name ';'
2803 *
2804 * Returns the associated xmlEntityPtr if found, or NULL otherwise,
2805 *         if non-NULL *str will have to be freed by the caller.
2806 */
2807static xmlEntityPtr
2808docbParseEntityRef(docbParserCtxtPtr ctxt, xmlChar **str) {
2809    xmlChar *name;
2810    xmlEntityPtr ent = NULL;
2811    *str = NULL;
2812
2813    if (CUR == '&') {
2814        NEXT;
2815        name = docbParseName(ctxt);
2816        if (name == NULL) {
2817            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2818                ctxt->sax->error(ctxt->userData,
2819			   "docbParseEntityRef: no name\n");
2820            ctxt->wellFormed = 0;
2821        } else {
2822           GROW;
2823            if (CUR == ';') {
2824                *str = name;
2825
2826		/*
2827		 * Ask first SAX for entity resolution, otherwise try the
2828		 * predefined set.
2829		 */
2830		if (ctxt->sax != NULL) {
2831		    if (ctxt->sax->getEntity != NULL)
2832			ent = ctxt->sax->getEntity(ctxt->userData, name);
2833		    if (ent == NULL)
2834		        ent = xmlGetPredefinedEntity(name);
2835		}
2836	        NEXT;
2837            } else {
2838                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2839                    ctxt->sax->error(ctxt->userData,
2840                                    "docbParseEntityRef: expecting ';'\n");
2841                *str = name;
2842            }
2843        }
2844    }
2845    return(ent);
2846}
2847
2848/**
2849 * docbParseAttValue:
2850 * @ctxt:  an SGML parser context
2851 *
2852 * parse a value for an attribute
2853 * Note: the parser won't do substitution of entities here, this
2854 * will be handled later in xmlStringGetNodeList, unless it was
2855 * asked for ctxt->replaceEntities != 0
2856 *
2857 * Returns the AttValue parsed or NULL.
2858 */
2859
2860static xmlChar *
2861docbParseAttValue(docbParserCtxtPtr ctxt) {
2862    xmlChar *ret = NULL;
2863
2864    if (CUR == '"') {
2865        NEXT;
2866       ret = docbParseSGMLAttribute(ctxt, '"');
2867        if (CUR != '"') {
2868           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2869               ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
2870           ctxt->wellFormed = 0;
2871       } else
2872           NEXT;
2873    } else if (CUR == '\'') {
2874        NEXT;
2875       ret = docbParseSGMLAttribute(ctxt, '\'');
2876        if (CUR != '\'') {
2877           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2878               ctxt->sax->error(ctxt->userData, "AttValue: ' expected\n");
2879           ctxt->wellFormed = 0;
2880       } else
2881           NEXT;
2882    } else {
2883        /*
2884        * That's an SGMLism, the attribute value may not be quoted
2885        */
2886       ret = docbParseSGMLAttribute(ctxt, 0);
2887       if (ret == NULL) {
2888           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2889               ctxt->sax->error(ctxt->userData, "AttValue: no value found\n");
2890           ctxt->wellFormed = 0;
2891       }
2892    }
2893    return(ret);
2894}
2895
2896/**
2897 * docbParseSystemLiteral:
2898 * @ctxt:  an SGML parser context
2899 *
2900 * parse an SGML Literal
2901 *
2902 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2903 *
2904 * Returns the SystemLiteral parsed or NULL
2905 */
2906
2907static xmlChar *
2908docbParseSystemLiteral(docbParserCtxtPtr ctxt) {
2909    const xmlChar *q;
2910    xmlChar *ret = NULL;
2911
2912    if (CUR == '"') {
2913        NEXT;
2914       q = CUR_PTR;
2915       while ((IS_CHAR(CUR)) && (CUR != '"'))
2916           NEXT;
2917       if (!IS_CHAR(CUR)) {
2918           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2919               ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
2920           ctxt->wellFormed = 0;
2921       } else {
2922           ret = xmlStrndup(q, CUR_PTR - q);
2923           NEXT;
2924        }
2925    } else if (CUR == '\'') {
2926        NEXT;
2927       q = CUR_PTR;
2928       while ((IS_CHAR(CUR)) && (CUR != '\''))
2929           NEXT;
2930       if (!IS_CHAR(CUR)) {
2931           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2932               ctxt->sax->error(ctxt->userData, "Unfinished SystemLiteral\n");
2933           ctxt->wellFormed = 0;
2934       } else {
2935           ret = xmlStrndup(q, CUR_PTR - q);
2936           NEXT;
2937        }
2938    } else {
2939       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2940           ctxt->sax->error(ctxt->userData,
2941                            "SystemLiteral \" or ' expected\n");
2942       ctxt->wellFormed = 0;
2943    }
2944
2945    return(ret);
2946}
2947
2948/**
2949 * docbParsePubidLiteral:
2950 * @ctxt:  an SGML parser context
2951 *
2952 * parse an SGML public literal
2953 *
2954 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
2955 *
2956 * Returns the PubidLiteral parsed or NULL.
2957 */
2958
2959static xmlChar *
2960docbParsePubidLiteral(docbParserCtxtPtr ctxt) {
2961    const xmlChar *q;
2962    xmlChar *ret = NULL;
2963    /*
2964     * Name ::= (Letter | '_') (NameChar)*
2965     */
2966    if (CUR == '"') {
2967        NEXT;
2968       q = CUR_PTR;
2969       while (IS_PUBIDCHAR(CUR)) NEXT;
2970       if (CUR != '"') {
2971           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2972               ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
2973           ctxt->wellFormed = 0;
2974       } else {
2975           ret = xmlStrndup(q, CUR_PTR - q);
2976           NEXT;
2977       }
2978    } else if (CUR == '\'') {
2979        NEXT;
2980       q = CUR_PTR;
2981       while ((IS_LETTER(CUR)) && (CUR != '\''))
2982           NEXT;
2983       if (!IS_LETTER(CUR)) {
2984           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2985               ctxt->sax->error(ctxt->userData, "Unfinished PubidLiteral\n");
2986           ctxt->wellFormed = 0;
2987       } else {
2988           ret = xmlStrndup(q, CUR_PTR - q);
2989           NEXT;
2990       }
2991    } else {
2992       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2993           ctxt->sax->error(ctxt->userData, "SystemLiteral \" or ' expected\n");
2994       ctxt->wellFormed = 0;
2995    }
2996
2997    return(ret);
2998}
2999
3000/**
3001 * docbParseCharData:
3002 * @ctxt:  an SGML parser context
3003 * @cdata:  int indicating whether we are within a CDATA section
3004 *
3005 * parse a CharData section.
3006 * if we are within a CDATA section ']]>' marks an end of section.
3007 *
3008 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3009 */
3010
3011static void
3012docbParseCharData(docbParserCtxtPtr ctxt) {
3013    xmlChar buf[DOCB_PARSER_BIG_BUFFER_SIZE + 5];
3014    int nbchar = 0;
3015    int cur, l;
3016
3017    SHRINK;
3018    cur = CUR_CHAR(l);
3019    while (((cur != '<') || (ctxt->token == '<')) &&
3020           ((cur != '&') || (ctxt->token == '&')) &&
3021          (IS_CHAR(cur))) {
3022       COPY_BUF(l,buf,nbchar,cur);
3023       if (nbchar >= DOCB_PARSER_BIG_BUFFER_SIZE) {
3024           /*
3025            * Ok the segment is to be consumed as chars.
3026            */
3027           if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
3028               if (areBlanks(ctxt, buf, nbchar)) {
3029                   if (ctxt->sax->ignorableWhitespace != NULL)
3030                       ctxt->sax->ignorableWhitespace(ctxt->userData,
3031                                                      buf, nbchar);
3032               } else {
3033                   if (ctxt->sax->characters != NULL)
3034                       ctxt->sax->characters(ctxt->userData, buf, nbchar);
3035               }
3036           }
3037           nbchar = 0;
3038       }
3039       NEXTL(l);
3040       cur = CUR_CHAR(l);
3041    }
3042    if (nbchar != 0) {
3043       /*
3044        * Ok the segment is to be consumed as chars.
3045        */
3046       if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
3047           if (areBlanks(ctxt, buf, nbchar)) {
3048               if (ctxt->sax->ignorableWhitespace != NULL)
3049                   ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
3050           } else {
3051               if (ctxt->sax->characters != NULL)
3052                   ctxt->sax->characters(ctxt->userData, buf, nbchar);
3053           }
3054       }
3055    }
3056}
3057
3058/**
3059 * docbParseExternalID:
3060 * @ctxt:  an SGML parser context
3061 * @publicID:  a xmlChar** receiving PubidLiteral
3062 *
3063 * Parse an External ID or a Public ID
3064 *
3065 * Returns the function returns SystemLiteral and in the second
3066 *                case publicID receives PubidLiteral,
3067 *                it is possible to return NULL and have publicID set.
3068 */
3069
3070static xmlChar *
3071docbParseExternalID(docbParserCtxtPtr ctxt, xmlChar **publicID) {
3072    xmlChar *URI = NULL;
3073
3074    if ((UPPER == 'S') && (UPP(1) == 'Y') &&
3075         (UPP(2) == 'S') && (UPP(3) == 'T') &&
3076        (UPP(4) == 'E') && (UPP(5) == 'M')) {
3077        SKIP(6);
3078       if (!IS_BLANK(CUR)) {
3079           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3080               ctxt->sax->error(ctxt->userData,
3081                   "Space required after 'SYSTEM'\n");
3082           ctxt->wellFormed = 0;
3083       }
3084        SKIP_BLANKS;
3085       URI = docbParseSystemLiteral(ctxt);
3086       if (URI == NULL) {
3087           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3088               ctxt->sax->error(ctxt->userData,
3089                 "docbParseExternalID: SYSTEM, no URI\n");
3090           ctxt->wellFormed = 0;
3091        }
3092    } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
3093              (UPP(2) == 'B') && (UPP(3) == 'L') &&
3094              (UPP(4) == 'I') && (UPP(5) == 'C')) {
3095        SKIP(6);
3096       if (!IS_BLANK(CUR)) {
3097           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3098               ctxt->sax->error(ctxt->userData,
3099                   "Space required after 'PUBLIC'\n");
3100           ctxt->wellFormed = 0;
3101       }
3102        SKIP_BLANKS;
3103       *publicID = docbParsePubidLiteral(ctxt);
3104       if (*publicID == NULL) {
3105           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3106               ctxt->sax->error(ctxt->userData,
3107                 "docbParseExternalID: PUBLIC, no Public Identifier\n");
3108           ctxt->wellFormed = 0;
3109       }
3110        SKIP_BLANKS;
3111        if ((CUR == '"') || (CUR == '\'')) {
3112           URI = docbParseSystemLiteral(ctxt);
3113       }
3114    }
3115    return(URI);
3116}
3117
3118/**
3119 * docbParsePI:
3120 * @ctxt:  an XML parser context
3121 *
3122 * parse an XML Processing Instruction.
3123 *
3124 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3125 *
3126 * The processing is transfered to SAX once parsed.
3127 */
3128
3129static void
3130docbParsePI(xmlParserCtxtPtr ctxt) {
3131    xmlChar *buf = NULL;
3132    int len = 0;
3133    int size = DOCB_PARSER_BUFFER_SIZE;
3134    int cur, l;
3135    xmlChar *target;
3136    xmlParserInputState state;
3137    int count = 0;
3138
3139    if ((RAW == '<') && (NXT(1) == '?')) {
3140	xmlParserInputPtr input = ctxt->input;
3141	state = ctxt->instate;
3142        ctxt->instate = XML_PARSER_PI;
3143	/*
3144	 * this is a Processing Instruction.
3145	 */
3146	SKIP(2);
3147	SHRINK;
3148
3149	/*
3150	 * Parse the target name and check for special support like
3151	 * namespace.
3152	 */
3153	target = xmlParseName(ctxt);
3154	if (target != NULL) {
3155	    xmlChar *encoding = NULL;
3156
3157	    if ((RAW == '?') && (NXT(1) == '>')) {
3158		if (input != ctxt->input) {
3159		    ctxt->errNo = XML_ERR_ENTITY_BOUNDARY;
3160		    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3161			ctxt->sax->error(ctxt->userData,
3162    "PI declaration doesn't start and stop in the same entity\n");
3163		    ctxt->wellFormed = 0;
3164		    ctxt->disableSAX = 1;
3165		}
3166		SKIP(2);
3167
3168		/*
3169		 * SAX: PI detected.
3170		 */
3171		if ((ctxt->sax) && (!ctxt->disableSAX) &&
3172		    (ctxt->sax->processingInstruction != NULL))
3173		    ctxt->sax->processingInstruction(ctxt->userData,
3174		                                     target, NULL);
3175		ctxt->instate = state;
3176		xmlFree(target);
3177		return;
3178	    }
3179	    if (xmlStrEqual(target, BAD_CAST "sgml-declaration")) {
3180
3181		encoding = xmlParseEncodingDecl(ctxt);
3182		if (encoding == NULL) {
3183		    xmlGenericError(xmlGenericErrorContext,
3184			"sgml-declaration: failed to find/handle encoding\n");
3185#ifdef DEBUG
3186		} else {
3187		    xmlGenericError(xmlGenericErrorContext,
3188			    "switched to encoding %s\n", encoding);
3189#endif
3190		}
3191
3192	    }
3193	    buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
3194	    if (buf == NULL) {
3195		xmlGenericError(xmlGenericErrorContext,
3196			"malloc of %d byte failed\n", size);
3197		ctxt->instate = state;
3198		return;
3199	    }
3200	    cur = CUR;
3201	    if (encoding != NULL) {
3202		len = snprintf((char *) buf, size - 1,
3203			       " encoding = \"%s\"", encoding);
3204		if (len < 0)
3205		    len = size;
3206	    } else {
3207		if (!IS_BLANK(cur)) {
3208		    ctxt->errNo = XML_ERR_SPACE_REQUIRED;
3209		    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3210			ctxt->sax->error(ctxt->userData,
3211			  "docbParsePI: PI %s space expected\n", target);
3212		    ctxt->wellFormed = 0;
3213		    ctxt->disableSAX = 1;
3214		}
3215		SKIP_BLANKS;
3216	    }
3217	    cur = CUR_CHAR(l);
3218	    while (IS_CHAR(cur) && /* checked */
3219		   ((cur != '?') || (NXT(1) != '>'))) {
3220		if (len + 5 >= size) {
3221		    size *= 2;
3222		    buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3223		    if (buf == NULL) {
3224			xmlGenericError(xmlGenericErrorContext,
3225				"realloc of %d byte failed\n", size);
3226			ctxt->instate = state;
3227			return;
3228		    }
3229		}
3230		count++;
3231		if (count > 50) {
3232		    GROW;
3233		    count = 0;
3234		}
3235		COPY_BUF(l,buf,len,cur);
3236		NEXTL(l);
3237		cur = CUR_CHAR(l);
3238		if (cur == 0) {
3239		    SHRINK;
3240		    GROW;
3241		    cur = CUR_CHAR(l);
3242		}
3243	    }
3244	    buf[len] = 0;
3245	    if (cur != '?') {
3246		ctxt->errNo = XML_ERR_PI_NOT_FINISHED;
3247		if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3248		    ctxt->sax->error(ctxt->userData,
3249		      "docbParsePI: PI %s never end ...\n", target);
3250		ctxt->wellFormed = 0;
3251		ctxt->disableSAX = 1;
3252	    } else {
3253		if (input != ctxt->input) {
3254		    ctxt->errNo = XML_ERR_ENTITY_BOUNDARY;
3255		    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3256			ctxt->sax->error(ctxt->userData,
3257    "PI declaration doesn't start and stop in the same entity\n");
3258		    ctxt->wellFormed = 0;
3259		    ctxt->disableSAX = 1;
3260		}
3261		SKIP(2);
3262
3263		/*
3264		 * SAX: PI detected.
3265		 */
3266		if ((ctxt->sax) && (!ctxt->disableSAX) &&
3267		    (ctxt->sax->processingInstruction != NULL))
3268		    ctxt->sax->processingInstruction(ctxt->userData,
3269		                                     target, buf);
3270	    }
3271	    xmlFree(buf);
3272	    xmlFree(target);
3273	} else {
3274	    ctxt->errNo = XML_ERR_PI_NOT_STARTED;
3275	    if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3276	        ctxt->sax->error(ctxt->userData,
3277		       "docbParsePI : no target name\n");
3278	    ctxt->wellFormed = 0;
3279	    ctxt->disableSAX = 1;
3280	}
3281	ctxt->instate = state;
3282    }
3283}
3284
3285/**
3286 * docbParseComment:
3287 * @ctxt:  an SGML parser context
3288 *
3289 * Parse an XML (SGML) comment <!-- .... -->
3290 *
3291 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3292 */
3293static void
3294docbParseComment(docbParserCtxtPtr ctxt) {
3295    xmlChar *buf = NULL;
3296    int len;
3297    int size = DOCB_PARSER_BUFFER_SIZE;
3298    int q, ql;
3299    int r, rl;
3300    int cur, l;
3301    xmlParserInputState state;
3302
3303    /*
3304     * Check that there is a comment right here.
3305     */
3306    if ((RAW != '<') || (NXT(1) != '!') ||
3307        (NXT(2) != '-') || (NXT(3) != '-')) return;
3308
3309    state = ctxt->instate;
3310    ctxt->instate = XML_PARSER_COMMENT;
3311    SHRINK;
3312    SKIP(4);
3313    buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
3314    if (buf == NULL) {
3315       xmlGenericError(xmlGenericErrorContext,
3316               "malloc of %d byte failed\n", size);
3317       ctxt->instate = state;
3318       return;
3319    }
3320    q = CUR_CHAR(ql);
3321    NEXTL(ql);
3322    r = CUR_CHAR(rl);
3323    NEXTL(rl);
3324    cur = CUR_CHAR(l);
3325    len = 0;
3326    while (IS_CHAR(cur) &&
3327           ((cur != '>') ||
3328           (r != '-') || (q != '-'))) {
3329       if (len + 5 >= size) {
3330           size *= 2;
3331           buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3332           if (buf == NULL) {
3333               xmlGenericError(xmlGenericErrorContext,
3334                       "realloc of %d byte failed\n", size);
3335               ctxt->instate = state;
3336               return;
3337           }
3338       }
3339       COPY_BUF(ql,buf,len,q);
3340       q = r;
3341       ql = rl;
3342       r = cur;
3343       rl = l;
3344       NEXTL(l);
3345       cur = CUR_CHAR(l);
3346       if (cur == 0) {
3347           SHRINK;
3348           GROW;
3349           cur = CUR_CHAR(l);
3350       }
3351    }
3352    buf[len] = 0;
3353    if (!IS_CHAR(cur)) {
3354       ctxt->errNo = XML_ERR_COMMENT_NOT_FINISHED;
3355       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3356           ctxt->sax->error(ctxt->userData,
3357                            "Comment not terminated \n<!--%.50s\n", buf);
3358       ctxt->wellFormed = 0;
3359       xmlFree(buf);
3360    } else {
3361        NEXT;
3362       if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3363           (!ctxt->disableSAX))
3364           ctxt->sax->comment(ctxt->userData, buf);
3365       xmlFree(buf);
3366    }
3367    ctxt->instate = state;
3368}
3369
3370/**
3371 * docbParseCharRef:
3372 * @ctxt:  an SGML parser context
3373 *
3374 * parse Reference declarations
3375 *
3376 * [66] CharRef ::= '&#' [0-9]+ ';' |
3377 *                  '&#x' [0-9a-fA-F]+ ';'
3378 *
3379 * Returns the value parsed (as an int)
3380 */
3381static int
3382docbParseCharRef(docbParserCtxtPtr ctxt) {
3383    int val = 0;
3384
3385    if ((CUR == '&') && (NXT(1) == '#') &&
3386        (NXT(2) == 'x')) {
3387       SKIP(3);
3388       while (CUR != ';') {
3389           if ((CUR >= '0') && (CUR <= '9'))
3390               val = val * 16 + (CUR - '0');
3391           else if ((CUR >= 'a') && (CUR <= 'f'))
3392               val = val * 16 + (CUR - 'a') + 10;
3393           else if ((CUR >= 'A') && (CUR <= 'F'))
3394               val = val * 16 + (CUR - 'A') + 10;
3395           else {
3396               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3397                   ctxt->sax->error(ctxt->userData,
3398                        "docbParseCharRef: invalid hexadecimal value\n");
3399               ctxt->wellFormed = 0;
3400               val = 0;
3401               break;
3402           }
3403           NEXT;
3404       }
3405       if (CUR == ';')
3406           NEXT;
3407    } else if  ((CUR == '&') && (NXT(1) == '#')) {
3408       SKIP(2);
3409       while (CUR != ';') {
3410           if ((CUR >= '0') && (CUR <= '9'))
3411               val = val * 10 + (CUR - '0');
3412           else {
3413               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3414                   ctxt->sax->error(ctxt->userData,
3415                        "docbParseCharRef: invalid decimal value\n");
3416               ctxt->wellFormed = 0;
3417               val = 0;
3418               break;
3419           }
3420           NEXT;
3421       }
3422       if (CUR == ';')
3423           NEXT;
3424    } else {
3425       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3426           ctxt->sax->error(ctxt->userData, "docbParseCharRef: invalid value\n");
3427       ctxt->wellFormed = 0;
3428    }
3429    /*
3430     * Check the value IS_CHAR ...
3431     */
3432    if (IS_CHAR(val)) {
3433        return(val);
3434    } else {
3435       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3436           ctxt->sax->error(ctxt->userData, "docbParseCharRef: invalid xmlChar value %d\n",
3437                            val);
3438       ctxt->wellFormed = 0;
3439    }
3440    return(0);
3441}
3442
3443
3444/**
3445 * docbParseDocTypeDecl :
3446 * @ctxt:  an SGML parser context
3447 *
3448 * parse a DOCTYPE declaration
3449 *
3450 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
3451 *                      ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
3452 */
3453
3454static void
3455docbParseDocTypeDecl(docbParserCtxtPtr ctxt) {
3456    xmlChar *name;
3457    xmlChar *ExternalID = NULL;
3458    xmlChar *URI = NULL;
3459
3460    /*
3461     * We know that '<!DOCTYPE' has been detected.
3462     */
3463    SKIP(9);
3464
3465    SKIP_BLANKS;
3466
3467    /*
3468     * Parse the DOCTYPE name.
3469     */
3470    name = docbParseName(ctxt);
3471    if (name == NULL) {
3472       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3473           ctxt->sax->error(ctxt->userData, "docbParseDocTypeDecl : no DOCTYPE name !\n");
3474       ctxt->wellFormed = 0;
3475    }
3476    /*
3477     * Check that upper(name) == "SGML" !!!!!!!!!!!!!
3478     */
3479
3480    SKIP_BLANKS;
3481
3482    /*
3483     * Check for SystemID and ExternalID
3484     */
3485    URI = docbParseExternalID(ctxt, &ExternalID);
3486    SKIP_BLANKS;
3487
3488    /*
3489     * Create or update the document accordingly to the DOCTYPE
3490     * But use the predefined PUBLIC and SYSTEM ID of DocBook XML
3491     */
3492    if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
3493       (!ctxt->disableSAX))
3494       ctxt->sax->internalSubset(ctxt->userData, name,
3495	                         XML_DOCBOOK_XML_PUBLIC,
3496				 XML_DOCBOOK_XML_SYSTEM);
3497
3498    if (RAW != '>') {
3499       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3500           ctxt->sax->error(ctxt->userData,
3501		   "docbParseDocTypeDecl : internal subset not handled\n");
3502    } else {
3503	NEXT;
3504    }
3505
3506    /*
3507     * Cleanup, since we don't use all those identifiers
3508     */
3509    if (URI != NULL) xmlFree(URI);
3510    if (ExternalID != NULL) xmlFree(ExternalID);
3511    if (name != NULL) xmlFree(name);
3512}
3513
3514/**
3515 * docbParseAttribute:
3516 * @ctxt:  an SGML parser context
3517 * @value:  a xmlChar ** used to store the value of the attribute
3518 *
3519 * parse an attribute
3520 *
3521 * [41] Attribute ::= Name Eq AttValue
3522 *
3523 * [25] Eq ::= S? '=' S?
3524 *
3525 * With namespace:
3526 *
3527 * [NS 11] Attribute ::= QName Eq AttValue
3528 *
3529 * Also the case QName == xmlns:??? is handled independently as a namespace
3530 * definition.
3531 *
3532 * Returns the attribute name, and the value in *value.
3533 */
3534
3535static xmlChar *
3536docbParseAttribute(docbParserCtxtPtr ctxt, xmlChar **value) {
3537    xmlChar *name, *val = NULL;
3538
3539    *value = NULL;
3540    name = docbParseName(ctxt);
3541    if (name == NULL) {
3542       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3543           ctxt->sax->error(ctxt->userData, "error parsing attribute name\n");
3544       ctxt->wellFormed = 0;
3545        return(NULL);
3546    }
3547
3548    /*
3549     * read the value
3550     */
3551    SKIP_BLANKS;
3552    if (CUR == '=') {
3553        NEXT;
3554       SKIP_BLANKS;
3555       val = docbParseAttValue(ctxt);
3556       /******
3557    } else {
3558        * TODO : some attribute must have values, some may not
3559       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3560           ctxt->sax->warning(ctxt->userData,
3561              "No value for attribute %s\n", name); */
3562    }
3563
3564    *value = val;
3565    return(name);
3566}
3567
3568/**
3569 * docbCheckEncoding:
3570 * @ctxt:  an SGML parser context
3571 * @attvalue: the attribute value
3572 *
3573 * Checks an http-equiv attribute from a Meta tag to detect
3574 * the encoding
3575 * If a new encoding is detected the parser is switched to decode
3576 * it and pass UTF8
3577 */
3578static void
3579docbCheckEncoding(docbParserCtxtPtr ctxt, const xmlChar *attvalue) {
3580    const xmlChar *encoding;
3581
3582    if ((ctxt == NULL) || (attvalue == NULL))
3583       return;
3584
3585    encoding = xmlStrstr(attvalue, BAD_CAST"charset=");
3586    if (encoding == NULL)
3587       encoding = xmlStrstr(attvalue, BAD_CAST"Charset=");
3588    if (encoding == NULL)
3589       encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET=");
3590    if (encoding != NULL) {
3591       encoding += 8;
3592    } else {
3593       encoding = xmlStrstr(attvalue, BAD_CAST"charset =");
3594       if (encoding == NULL)
3595           encoding = xmlStrstr(attvalue, BAD_CAST"Charset =");
3596       if (encoding == NULL)
3597           encoding = xmlStrstr(attvalue, BAD_CAST"CHARSET =");
3598       if (encoding != NULL)
3599           encoding += 9;
3600    }
3601    /*
3602     * Restricted from 2.3.5 */
3603    if (encoding != NULL) {
3604       xmlCharEncoding enc;
3605
3606       if (ctxt->input->encoding != NULL)
3607           xmlFree((xmlChar *) ctxt->input->encoding);
3608       ctxt->input->encoding = encoding;
3609
3610       enc = xmlParseCharEncoding((const char *) encoding);
3611       if (enc == XML_CHAR_ENCODING_8859_1) {
3612           ctxt->charset = XML_CHAR_ENCODING_8859_1;
3613       } else if (enc != XML_CHAR_ENCODING_UTF8) {
3614           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3615               ctxt->sax->error(ctxt->userData,
3616                    "Unsupported encoding %s\n", encoding);
3617           /* xmlFree(encoding); */
3618           ctxt->wellFormed = 0;
3619           ctxt->disableSAX = 1;
3620           ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
3621       }
3622    }
3623}
3624
3625/**
3626 * docbCheckMeta:
3627 * @ctxt:  an SGML parser context
3628 * @atts:  the attributes values
3629 *
3630 * Checks an attributes from a Meta tag
3631 */
3632static void
3633docbCheckMeta(docbParserCtxtPtr ctxt, const xmlChar **atts) {
3634    int i;
3635    const xmlChar *att, *value;
3636    int http = 0;
3637    const xmlChar *content = NULL;
3638
3639    if ((ctxt == NULL) || (atts == NULL))
3640       return;
3641
3642    i = 0;
3643    att = atts[i++];
3644    while (att != NULL) {
3645       value = atts[i++];
3646       if ((value != NULL) &&
3647           ((xmlStrEqual(att, BAD_CAST"http-equiv")) ||
3648            (xmlStrEqual(att, BAD_CAST"Http-Equiv")) ||
3649            (xmlStrEqual(att, BAD_CAST"HTTP-EQUIV"))) &&
3650           ((xmlStrEqual(value, BAD_CAST"Content-Type")) ||
3651            (xmlStrEqual(value, BAD_CAST"content-type")) ||
3652            (xmlStrEqual(value, BAD_CAST"CONTENT-TYPE"))))
3653           http = 1;
3654       else if ((value != NULL) &&
3655                ((xmlStrEqual(att, BAD_CAST"content")) ||
3656                 (xmlStrEqual(att, BAD_CAST"Content")) ||
3657                 (xmlStrEqual(att, BAD_CAST"CONTENT"))))
3658           content = value;
3659       att = atts[i++];
3660    }
3661    if ((http) && (content != NULL))
3662       docbCheckEncoding(ctxt, content);
3663
3664}
3665
3666/**
3667 * docbParseStartTag:
3668 * @ctxt:  an SGML parser context
3669 *
3670 * parse a start of tag either for rule element or
3671 * EmptyElement. In both case we don't parse the tag closing chars.
3672 *
3673 * [40] STag ::= '<' Name (S Attribute)* S? '>'
3674 *
3675 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
3676 *
3677 * With namespace:
3678 *
3679 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
3680 *
3681 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
3682 *
3683 */
3684
3685static void
3686docbParseStartTag(docbParserCtxtPtr ctxt) {
3687    xmlChar *name;
3688    xmlChar *attname;
3689    xmlChar *attvalue;
3690    const xmlChar **atts = NULL;
3691    int nbatts = 0;
3692    int maxatts = 0;
3693    int meta = 0;
3694    int i;
3695
3696    if (CUR != '<') return;
3697    NEXT;
3698
3699    GROW;
3700    name = docbParseSGMLName(ctxt);
3701    if (name == NULL) {
3702       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3703           ctxt->sax->error(ctxt->userData,
3704            "docbParseStartTag: invalid element name\n");
3705       ctxt->wellFormed = 0;
3706        return;
3707    }
3708    if (xmlStrEqual(name, BAD_CAST"meta"))
3709       meta = 1;
3710
3711    /*
3712     * Check for auto-closure of SGML elements.
3713     */
3714    docbAutoClose(ctxt, name);
3715
3716    /*
3717     * Now parse the attributes, it ends up with the ending
3718     *
3719     * (S Attribute)* S?
3720     */
3721    SKIP_BLANKS;
3722    while ((IS_CHAR(CUR)) &&
3723           (CUR != '>') &&
3724          ((CUR != '/') || (NXT(1) != '>'))) {
3725       long cons = ctxt->nbChars;
3726
3727       GROW;
3728       attname = docbParseAttribute(ctxt, &attvalue);
3729        if (attname != NULL) {
3730
3731           /*
3732            * Well formedness requires at most one declaration of an attribute
3733            */
3734           for (i = 0; i < nbatts;i += 2) {
3735               if (xmlStrEqual(atts[i], attname)) {
3736                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3737                       ctxt->sax->error(ctxt->userData,
3738                                        "Attribute %s redefined\n",
3739                                        attname);
3740                   ctxt->wellFormed = 0;
3741                   xmlFree(attname);
3742                   if (attvalue != NULL)
3743                       xmlFree(attvalue);
3744                   goto failed;
3745               }
3746           }
3747
3748           /*
3749            * Add the pair to atts
3750            */
3751           if (atts == NULL) {
3752               maxatts = 10;
3753               atts = (const xmlChar **) xmlMalloc(maxatts * sizeof(xmlChar *));
3754               if (atts == NULL) {
3755                   xmlGenericError(xmlGenericErrorContext,
3756                           "malloc of %ld byte failed\n",
3757                           maxatts * (long)sizeof(xmlChar *));
3758                   if (name != NULL) xmlFree(name);
3759                   return;
3760               }
3761           } else if (nbatts + 4 > maxatts) {
3762               maxatts *= 2;
3763               atts = (const xmlChar **) xmlRealloc((void *)atts, maxatts * sizeof(xmlChar *));
3764               if (atts == NULL) {
3765                   xmlGenericError(xmlGenericErrorContext,
3766                           "realloc of %ld byte failed\n",
3767                           maxatts * (long)sizeof(xmlChar *));
3768                   if (name != NULL) xmlFree(name);
3769                   return;
3770               }
3771           }
3772           atts[nbatts++] = attname;
3773           atts[nbatts++] = attvalue;
3774           atts[nbatts] = NULL;
3775           atts[nbatts + 1] = NULL;
3776       }
3777
3778failed:
3779       SKIP_BLANKS;
3780        if (cons == ctxt->nbChars) {
3781           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3782               ctxt->sax->error(ctxt->userData,
3783                "docbParseStartTag: problem parsing attributes\n");
3784           ctxt->wellFormed = 0;
3785           break;
3786       }
3787    }
3788
3789    /*
3790     * Handle specific association to the META tag
3791     */
3792    if (meta)
3793       docbCheckMeta(ctxt, atts);
3794
3795    /*
3796     * SAX: Start of Element !
3797     */
3798    docbnamePush(ctxt, xmlStrdup(name));
3799#ifdef DEBUG
3800    xmlGenericError(xmlGenericErrorContext,"Start of element %s: pushed %s\n", name, ctxt->name);
3801#endif
3802    if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
3803        ctxt->sax->startElement(ctxt->userData, name, atts);
3804
3805    if (atts != NULL) {
3806        for (i = 0;i < nbatts;i++) {
3807           if (atts[i] != NULL)
3808               xmlFree((xmlChar *) atts[i]);
3809       }
3810       xmlFree((void *) atts);
3811    }
3812    if (name != NULL) xmlFree(name);
3813}
3814
3815/**
3816 * docbParseEndTag:
3817 * @ctxt:  an SGML parser context
3818 *
3819 * parse an end of tag
3820 *
3821 * [42] ETag ::= '</' Name S? '>'
3822 *
3823 * With namespace
3824 *
3825 * [NS 9] ETag ::= '</' QName S? '>'
3826 */
3827
3828static void
3829docbParseEndTag(docbParserCtxtPtr ctxt) {
3830    xmlChar *name;
3831    xmlChar *oldname;
3832    int i;
3833
3834    if ((CUR != '<') || (NXT(1) != '/')) {
3835       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3836           ctxt->sax->error(ctxt->userData, "docbParseEndTag: '</' not found\n");
3837       ctxt->wellFormed = 0;
3838       return;
3839    }
3840    SKIP(2);
3841
3842    name = docbParseSGMLName(ctxt);
3843    if (name == NULL) {
3844       if (CUR == '>') {
3845           NEXT;
3846           oldname = docbnamePop(ctxt);
3847           if (oldname != NULL) {
3848               if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3849                   ctxt->sax->endElement(ctxt->userData, name);
3850#ifdef DEBUG
3851               xmlGenericError(xmlGenericErrorContext,"End of tag </>: popping out %s\n", oldname);
3852#endif
3853               xmlFree(oldname);
3854#ifdef DEBUG
3855           } else {
3856               xmlGenericError(xmlGenericErrorContext,"End of tag </>: stack empty !!!\n");
3857#endif
3858           }
3859           return;
3860       } else
3861           return;
3862    }
3863
3864    /*
3865     * We should definitely be at the ending "S? '>'" part
3866     */
3867    SKIP_BLANKS;
3868    if ((!IS_CHAR(CUR)) || (CUR != '>')) {
3869       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3870           ctxt->sax->error(ctxt->userData, "End tag : expected '>'\n");
3871       ctxt->wellFormed = 0;
3872    } else
3873       NEXT;
3874
3875    /*
3876     * If the name read is not one of the element in the parsing stack
3877     * then return, it's just an error.
3878     */
3879    for (i = (ctxt->nameNr - 1);i >= 0;i--) {
3880        if (xmlStrEqual(name, ctxt->nameTab[i])) break;
3881    }
3882    if (i < 0) {
3883       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3884           ctxt->sax->error(ctxt->userData,
3885            "Unexpected end tag : %s\n", name);
3886       xmlFree(name);
3887       ctxt->wellFormed = 0;
3888       return;
3889    }
3890
3891
3892    /*
3893     * Check for auto-closure of SGML elements.
3894     */
3895
3896    docbAutoCloseOnClose(ctxt, name);
3897
3898    /*
3899     * Well formedness constraints, opening and closing must match.
3900     * With the exception that the autoclose may have popped stuff out
3901     * of the stack.
3902     */
3903    if (((name[0] != '/') || (name[1] != 0)) &&
3904       (!xmlStrEqual(name, ctxt->name))) {
3905#ifdef DEBUG
3906       xmlGenericError(xmlGenericErrorContext,"End of tag %s: expecting %s\n", name, ctxt->name);
3907#endif
3908        if ((ctxt->name != NULL) &&
3909           (!xmlStrEqual(ctxt->name, name))) {
3910           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3911               ctxt->sax->error(ctxt->userData,
3912                "Opening and ending tag mismatch: %s and %s\n",
3913                                name, ctxt->name);
3914           ctxt->wellFormed = 0;
3915        }
3916    }
3917
3918    /*
3919     * SAX: End of Tag
3920     */
3921    oldname = ctxt->name;
3922    if (((name[0] == '/') && (name[1] == 0)) ||
3923       ((oldname != NULL) && (xmlStrEqual(oldname, name)))) {
3924       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
3925           ctxt->sax->endElement(ctxt->userData, name);
3926       oldname = docbnamePop(ctxt);
3927       if (oldname != NULL) {
3928#ifdef DEBUG
3929           xmlGenericError(xmlGenericErrorContext,"End of tag %s: popping out %s\n", name, oldname);
3930#endif
3931           xmlFree(oldname);
3932#ifdef DEBUG
3933       } else {
3934           xmlGenericError(xmlGenericErrorContext,"End of tag %s: stack empty !!!\n", name);
3935#endif
3936       }
3937    }
3938
3939    if (name != NULL)
3940       xmlFree(name);
3941
3942    return;
3943}
3944
3945
3946/**
3947 * docbParseReference:
3948 * @ctxt:  an SGML parser context
3949 *
3950 * parse and handle entity references in content,
3951 * this will end-up in a call to character() since this is either a
3952 * CharRef, or a predefined entity.
3953 */
3954static void
3955docbParseReference(docbParserCtxtPtr ctxt) {
3956    docbEntityDescPtr ent;
3957    xmlEntityPtr xent;
3958    xmlChar out[6];
3959    xmlChar *name;
3960    if (CUR != '&') return;
3961
3962    if (NXT(1) == '#') {
3963       unsigned int c;
3964       int bits, i = 0;
3965
3966       c = docbParseCharRef(ctxt);
3967        if      (c <    0x80) { out[i++]= c;                bits= -6; }
3968        else if (c <   0x800) { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
3969        else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
3970        else                  { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
3971
3972        for ( ; bits >= 0; bits-= 6) {
3973            out[i++]= ((c >> bits) & 0x3F) | 0x80;
3974        }
3975       out[i] = 0;
3976
3977       if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
3978           ctxt->sax->characters(ctxt->userData, out, i);
3979    } else {
3980	/*
3981	 * Lookup the entity in the table.
3982	 */
3983       xent = docbParseEntityRef(ctxt, &name);
3984       if (xent != NULL) {
3985	    if (((ctxt->replaceEntities) || (ctxt->loadsubset)) &&
3986		((xent->children == NULL) &&
3987		(xent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))) {
3988		    /*
3989		     * we really need to fetch and parse the external entity
3990		     */
3991		    int parse;
3992		    xmlNodePtr children = NULL;
3993
3994		    parse = docbParseCtxtExternalEntity(ctxt,
3995			       xent->SystemID, xent->ExternalID, &children);
3996		    xmlAddChildList((xmlNodePtr) xent, children);
3997	    }
3998	    if (ctxt->replaceEntities) {
3999		if ((ctxt->node != NULL) && (xent->children != NULL)) {
4000		    /*
4001		     * Seems we are generating the DOM content, do
4002		     * a simple tree copy
4003		     */
4004		    xmlNodePtr new;
4005		    new = xmlCopyNodeList(xent->children);
4006
4007		    xmlAddChildList(ctxt->node, new);
4008		    /*
4009		     * This is to avoid a nasty side effect, see
4010		     * characters() in SAX.c
4011		     */
4012		    ctxt->nodemem = 0;
4013		    ctxt->nodelen = 0;
4014		}
4015	    } else {
4016		if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
4017		    (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
4018		    /*
4019		     * Create a node.
4020		     */
4021		    ctxt->sax->reference(ctxt->userData, xent->name);
4022		}
4023	    }
4024       } else if (name != NULL) {
4025	   ent = docbEntityLookup(name);
4026	   if ((ent == NULL) || (ent->value <= 0)) {
4027	       if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
4028		   ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
4029		   ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
4030		   /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
4031	       }
4032	   } else {
4033	       unsigned int c;
4034	       int bits, i = 0;
4035
4036	       c = ent->value;
4037	       if      (c <    0x80)
4038		       { out[i++]= c;                bits= -6; }
4039	       else if (c <   0x800)
4040		       { out[i++]=((c >>  6) & 0x1F) | 0xC0;  bits=  0; }
4041	       else if (c < 0x10000)
4042		       { out[i++]=((c >> 12) & 0x0F) | 0xE0;  bits=  6; }
4043	       else
4044		       { out[i++]=((c >> 18) & 0x07) | 0xF0;  bits= 12; }
4045
4046	       for ( ; bits >= 0; bits-= 6) {
4047		   out[i++]= ((c >> bits) & 0x3F) | 0x80;
4048	       }
4049	       out[i] = 0;
4050
4051	       if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4052		   ctxt->sax->characters(ctxt->userData, out, i);
4053	   }
4054       } else {
4055           if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
4056               ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
4057           return;
4058       }
4059       if (name != NULL)
4060	   xmlFree(name);
4061    }
4062}
4063
4064/**
4065 * docbParseContent:
4066 * @ctxt:  an SGML parser context
4067 * @name:  the node name
4068 *
4069 * Parse a content: comment, sub-element, reference or text.
4070 *
4071 */
4072static void
4073docbParseContent(docbParserCtxtPtr ctxt)
4074{
4075    xmlChar *currentNode;
4076    int depth;
4077
4078    currentNode = xmlStrdup(ctxt->name);
4079    depth = ctxt->nameNr;
4080    while (1) {
4081        long cons = ctxt->nbChars;
4082
4083        GROW;
4084        /*
4085         * Our tag or one of it's parent or children is ending.
4086         */
4087        if ((CUR == '<') && (NXT(1) == '/')) {
4088            docbParseEndTag(ctxt);
4089            if (currentNode != NULL)
4090                xmlFree(currentNode);
4091            return;
4092        }
4093
4094        /*
4095         * Has this node been popped out during parsing of
4096         * the next element
4097         */
4098        if ((!xmlStrEqual(currentNode, ctxt->name)) &&
4099            (depth >= ctxt->nameNr)) {
4100            if (currentNode != NULL)
4101                xmlFree(currentNode);
4102            return;
4103        }
4104
4105        /*
4106         * Sometimes DOCTYPE arrives in the middle of the document
4107         */
4108        if ((CUR == '<') && (NXT(1) == '!') &&
4109            (UPP(2) == 'D') && (UPP(3) == 'O') &&
4110            (UPP(4) == 'C') && (UPP(5) == 'T') &&
4111            (UPP(6) == 'Y') && (UPP(7) == 'P') && (UPP(8) == 'E')) {
4112            if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4113                ctxt->sax->error(ctxt->userData,
4114                                 "Misplaced DOCTYPE declaration\n");
4115            ctxt->wellFormed = 0;
4116            docbParseDocTypeDecl(ctxt);
4117        }
4118
4119        /*
4120         * First case :  a comment
4121         */
4122        if ((CUR == '<') && (NXT(1) == '!') &&
4123            (NXT(2) == '-') && (NXT(3) == '-')) {
4124            docbParseComment(ctxt);
4125        }
4126
4127        /*
4128         * Second case :  a PI
4129         */
4130	else if ((RAW == '<') && (NXT(1) == '?')) {
4131            docbParsePI(ctxt);
4132        }
4133
4134        /*
4135         * Third case :  a sub-element.
4136         */
4137        else if (CUR == '<') {
4138            docbParseElement(ctxt);
4139        }
4140
4141        /*
4142         * Fourth case : a reference. If if has not been resolved,
4143         *    parsing returns it's Name, create the node
4144         */
4145        else if (CUR == '&') {
4146            docbParseReference(ctxt);
4147        }
4148
4149        /*
4150         * Fifth : end of the resource
4151         */
4152        else if (CUR == 0) {
4153            docbAutoClose(ctxt, NULL);
4154            if (ctxt->nameNr == 0)
4155                break;
4156        }
4157
4158        /*
4159         * Last case, text. Note that References are handled directly.
4160         */
4161        else {
4162            docbParseCharData(ctxt);
4163        }
4164
4165        if (cons == ctxt->nbChars) {
4166            if (ctxt->node != NULL) {
4167                if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4168                    ctxt->sax->error(ctxt->userData,
4169                                     "detected an error in element content\n");
4170                ctxt->wellFormed = 0;
4171            }
4172            break;
4173        }
4174
4175        GROW;
4176    }
4177    if (currentNode != NULL)
4178        xmlFree(currentNode);
4179}
4180
4181/**
4182 * docbParseElement:
4183 * @ctxt:  an SGML parser context
4184 *
4185 * parse an SGML element, this is highly recursive
4186 *
4187 * [39] element ::= EmptyElemTag | STag content ETag
4188 *
4189 * [41] Attribute ::= Name Eq AttValue
4190 */
4191
4192static void
4193docbParseElement(docbParserCtxtPtr ctxt) {
4194    xmlChar *name;
4195    xmlChar *currentNode = NULL;
4196    docbElemDescPtr info;
4197    docbParserNodeInfo node_info;
4198    xmlChar *oldname;
4199    int depth = ctxt->nameNr;
4200
4201    /* Capture start position */
4202    if (ctxt->record_info) {
4203        node_info.begin_pos = ctxt->input->consumed +
4204                          (CUR_PTR - ctxt->input->base);
4205       node_info.begin_line = ctxt->input->line;
4206    }
4207
4208    oldname = xmlStrdup(ctxt->name);
4209    docbParseStartTag(ctxt);
4210    name = ctxt->name;
4211#ifdef DEBUG
4212    if (oldname == NULL)
4213       xmlGenericError(xmlGenericErrorContext,
4214               "Start of element %s\n", name);
4215    else if (name == NULL)
4216       xmlGenericError(xmlGenericErrorContext,
4217               "Start of element failed, was %s\n", oldname);
4218    else
4219       xmlGenericError(xmlGenericErrorContext,
4220               "Start of element %s, was %s\n", name, oldname);
4221#endif
4222    if (((depth == ctxt->nameNr) && (xmlStrEqual(oldname, ctxt->name))) ||
4223        (name == NULL)) {
4224       if (CUR == '>')
4225           NEXT;
4226       if (oldname != NULL)
4227           xmlFree(oldname);
4228        return;
4229    }
4230    if (oldname != NULL)
4231       xmlFree(oldname);
4232
4233    /*
4234     * Lookup the info for that element.
4235     */
4236    info = docbTagLookup(name);
4237    if (info == NULL) {
4238       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4239           ctxt->sax->error(ctxt->userData, "Tag %s unknown\n",
4240                            name);
4241       ctxt->wellFormed = 0;
4242    } else if (info->depr) {
4243/***************************
4244       if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
4245           ctxt->sax->warning(ctxt->userData, "Tag %s is deprecated\n",
4246                              name);
4247 ***************************/
4248    }
4249
4250    /*
4251     * Check for an Empty Element labeled the XML/SGML way
4252     */
4253    if ((CUR == '/') && (NXT(1) == '>')) {
4254        SKIP(2);
4255       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4256           ctxt->sax->endElement(ctxt->userData, name);
4257       oldname = docbnamePop(ctxt);
4258#ifdef DEBUG
4259        xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n", oldname);
4260#endif
4261       if (oldname != NULL)
4262           xmlFree(oldname);
4263       return;
4264    }
4265
4266    if (CUR == '>') {
4267        NEXT;
4268    } else {
4269       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4270           ctxt->sax->error(ctxt->userData,
4271                            "Couldn't find end of Start Tag %s\n",
4272                            name);
4273       ctxt->wellFormed = 0;
4274
4275       /*
4276        * end of parsing of this node.
4277        */
4278       if (xmlStrEqual(name, ctxt->name)) {
4279           nodePop(ctxt);
4280           oldname = docbnamePop(ctxt);
4281#ifdef DEBUG
4282           xmlGenericError(xmlGenericErrorContext,"End of start tag problem: popping out %s\n", oldname);
4283#endif
4284           if (oldname != NULL)
4285               xmlFree(oldname);
4286       }
4287
4288       /*
4289        * Capture end position and add node
4290        */
4291       if ( currentNode != NULL && ctxt->record_info ) {
4292          node_info.end_pos = ctxt->input->consumed +
4293                             (CUR_PTR - ctxt->input->base);
4294          node_info.end_line = ctxt->input->line;
4295          node_info.node = ctxt->node;
4296          xmlParserAddNodeInfo(ctxt, &node_info);
4297       }
4298       return;
4299    }
4300
4301    /*
4302     * Check for an Empty Element from DTD definition
4303     */
4304    if ((info != NULL) && (info->empty)) {
4305       if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
4306           ctxt->sax->endElement(ctxt->userData, name);
4307       oldname = docbnamePop(ctxt);
4308#ifdef DEBUG
4309       xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
4310#endif
4311       if (oldname != NULL)
4312           xmlFree(oldname);
4313       return;
4314    }
4315
4316    /*
4317     * Parse the content of the element:
4318     */
4319    currentNode = xmlStrdup(ctxt->name);
4320    depth = ctxt->nameNr;
4321    while (IS_CHAR(CUR)) {
4322       docbParseContent(ctxt);
4323       if (ctxt->nameNr < depth) break;
4324    }
4325
4326    if (!IS_CHAR(CUR)) {
4327       /************
4328       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4329           ctxt->sax->error(ctxt->userData,
4330                "Premature end of data in tag %s\n", currentNode);
4331       ctxt->wellFormed = 0;
4332        *************/
4333
4334       /*
4335        * end of parsing of this node.
4336        */
4337       nodePop(ctxt);
4338       oldname = docbnamePop(ctxt);
4339#ifdef DEBUG
4340       xmlGenericError(xmlGenericErrorContext,"Premature end of tag %s : popping out %s\n", name, oldname);
4341#endif
4342       if (oldname != NULL)
4343           xmlFree(oldname);
4344       if (currentNode != NULL)
4345           xmlFree(currentNode);
4346       return;
4347    }
4348
4349    /*
4350     * Capture end position and add node
4351     */
4352    if ( currentNode != NULL && ctxt->record_info ) {
4353       node_info.end_pos = ctxt->input->consumed +
4354                          (CUR_PTR - ctxt->input->base);
4355       node_info.end_line = ctxt->input->line;
4356       node_info.node = ctxt->node;
4357       xmlParserAddNodeInfo(ctxt, &node_info);
4358    }
4359    if (currentNode != NULL)
4360       xmlFree(currentNode);
4361}
4362
4363/**
4364 * docbParseEntityDecl:
4365 * @ctxt:  an SGML parser context
4366 *
4367 * parse <!ENTITY declarations
4368 *
4369 */
4370
4371static void
4372docbParseEntityDecl(xmlParserCtxtPtr ctxt) {
4373    xmlChar *name = NULL;
4374    xmlChar *value = NULL;
4375    xmlChar *URI = NULL, *literal = NULL;
4376    xmlChar *ndata = NULL;
4377    int isParameter = 0;
4378    xmlChar *orig = NULL;
4379
4380    GROW;
4381    if ((RAW == '<') && (NXT(1) == '!') &&
4382        (UPP(2) == 'E') && (UPP(3) == 'N') &&
4383        (UPP(4) == 'T') && (UPP(5) == 'I') &&
4384        (UPP(6) == 'T') && (UPP(7) == 'Y')) {
4385       xmlParserInputPtr input = ctxt->input;
4386       ctxt->instate = XML_PARSER_ENTITY_DECL;
4387       SHRINK;
4388       SKIP(8);
4389       if (!IS_BLANK(CUR)) {
4390           ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4391           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4392               ctxt->sax->error(ctxt->userData,
4393                                "Space required after '<!ENTITY'\n");
4394           ctxt->wellFormed = 0;
4395           ctxt->disableSAX = 1;
4396       }
4397       SKIP_BLANKS;
4398
4399       if (RAW == '%') {
4400           NEXT;
4401           if (!IS_BLANK(CUR)) {
4402               ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4403               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4404                   ctxt->sax->error(ctxt->userData,
4405                                    "Space required after '%'\n");
4406               ctxt->wellFormed = 0;
4407               ctxt->disableSAX = 1;
4408           }
4409           SKIP_BLANKS;
4410           isParameter = 1;
4411       }
4412
4413        name = xmlParseName(ctxt);
4414       if (name == NULL) {
4415           ctxt->errNo = XML_ERR_NAME_REQUIRED;
4416           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4417               ctxt->sax->error(ctxt->userData, "sgmlarseEntityDecl: no name\n");
4418           ctxt->wellFormed = 0;
4419           ctxt->disableSAX = 1;
4420            return;
4421       }
4422       if (!IS_BLANK(CUR)) {
4423           ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4424           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4425               ctxt->sax->error(ctxt->userData,
4426                    "Space required after the entity name\n");
4427           ctxt->wellFormed = 0;
4428           ctxt->disableSAX = 1;
4429       }
4430        SKIP_BLANKS;
4431
4432       /*
4433        * handle the various case of definitions...
4434        */
4435       if (isParameter) {
4436           if ((RAW == '"') || (RAW == '\'')) {
4437               value = xmlParseEntityValue(ctxt, &orig);
4438               if (value) {
4439                   if ((ctxt->sax != NULL) &&
4440                       (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4441                       ctxt->sax->entityDecl(ctxt->userData, name,
4442                                   XML_INTERNAL_PARAMETER_ENTITY,
4443                                   NULL, NULL, value);
4444               }
4445           } else {
4446               URI = xmlParseExternalID(ctxt, &literal, 1);
4447               if ((URI == NULL) && (literal == NULL)) {
4448                   ctxt->errNo = XML_ERR_VALUE_REQUIRED;
4449                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4450                       ctxt->sax->error(ctxt->userData,
4451                           "Entity value required\n");
4452                   ctxt->wellFormed = 0;
4453                   ctxt->disableSAX = 1;
4454               }
4455               if (URI) {
4456                   xmlURIPtr uri;
4457
4458                   uri = xmlParseURI((const char *) URI);
4459                   if (uri == NULL) {
4460                       ctxt->errNo = XML_ERR_INVALID_URI;
4461                       if ((ctxt->sax != NULL) &&
4462                           (!ctxt->disableSAX) &&
4463                           (ctxt->sax->error != NULL))
4464                           ctxt->sax->error(ctxt->userData,
4465                                       "Invalid URI: %s\n", URI);
4466                       ctxt->wellFormed = 0;
4467                   } else {
4468                       if (uri->fragment != NULL) {
4469                           ctxt->errNo = XML_ERR_URI_FRAGMENT;
4470                           if ((ctxt->sax != NULL) &&
4471                               (!ctxt->disableSAX) &&
4472                               (ctxt->sax->error != NULL))
4473                               ctxt->sax->error(ctxt->userData,
4474                                           "Fragment not allowed: %s\n", URI);
4475                           ctxt->wellFormed = 0;
4476                       } else {
4477                           if ((ctxt->sax != NULL) &&
4478                               (!ctxt->disableSAX) &&
4479                               (ctxt->sax->entityDecl != NULL))
4480                               ctxt->sax->entityDecl(ctxt->userData, name,
4481                                           XML_EXTERNAL_PARAMETER_ENTITY,
4482                                           literal, URI, NULL);
4483                       }
4484                       xmlFreeURI(uri);
4485                   }
4486               }
4487           }
4488       } else {
4489           if ((RAW == '"') || (RAW == '\'')) {
4490               value = xmlParseEntityValue(ctxt, &orig);
4491               if ((ctxt->sax != NULL) &&
4492                   (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4493                   ctxt->sax->entityDecl(ctxt->userData, name,
4494                               XML_INTERNAL_GENERAL_ENTITY,
4495                               NULL, NULL, value);
4496           } else {
4497               URI = xmlParseExternalID(ctxt, &literal, 1);
4498               if ((URI == NULL) && (literal == NULL)) {
4499                   ctxt->errNo = XML_ERR_VALUE_REQUIRED;
4500                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4501                       ctxt->sax->error(ctxt->userData,
4502                           "Entity value required\n");
4503                   ctxt->wellFormed = 0;
4504                   ctxt->disableSAX = 1;
4505               }
4506               if (URI) {
4507                   xmlURIPtr uri;
4508
4509                   uri = xmlParseURI((const char *)URI);
4510                   if (uri == NULL) {
4511                       ctxt->errNo = XML_ERR_INVALID_URI;
4512                       if ((ctxt->sax != NULL) &&
4513                           (!ctxt->disableSAX) &&
4514                           (ctxt->sax->error != NULL))
4515                           ctxt->sax->error(ctxt->userData,
4516                                       "Invalid URI: %s\n", URI);
4517                       ctxt->wellFormed = 0;
4518                   } else {
4519                       if (uri->fragment != NULL) {
4520                           ctxt->errNo = XML_ERR_URI_FRAGMENT;
4521                           if ((ctxt->sax != NULL) &&
4522                               (!ctxt->disableSAX) &&
4523                               (ctxt->sax->error != NULL))
4524                               ctxt->sax->error(ctxt->userData,
4525                                           "Fragment not allowed: %s\n", URI);
4526                           ctxt->wellFormed = 0;
4527                       }
4528                       xmlFreeURI(uri);
4529                   }
4530               }
4531               if ((RAW != '>') && (!IS_BLANK(CUR))) {
4532                   ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4533                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4534                       ctxt->sax->error(ctxt->userData,
4535                           "Space required before content model\n");
4536                   ctxt->wellFormed = 0;
4537                   ctxt->disableSAX = 1;
4538               }
4539               SKIP_BLANKS;
4540
4541               /*
4542                * SGML specific: here we can get the content model
4543                */
4544               if (RAW != '>') {
4545                   xmlChar *contmod;
4546
4547                   contmod = xmlParseName(ctxt);
4548
4549                   if (contmod == NULL) {
4550                       ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4551                       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4552                           ctxt->sax->error(ctxt->userData,
4553                               "Could not parse entity content model\n");
4554                       ctxt->wellFormed = 0;
4555                       ctxt->disableSAX = 1;
4556                   } else {
4557                       if (xmlStrEqual(contmod, BAD_CAST"NDATA")) {
4558                           if (!IS_BLANK(CUR)) {
4559                               ctxt->errNo = XML_ERR_SPACE_REQUIRED;
4560                               if ((ctxt->sax != NULL) &&
4561                                   (ctxt->sax->error != NULL))
4562                                   ctxt->sax->error(ctxt->userData,
4563                                       "Space required after 'NDATA'\n");
4564                               ctxt->wellFormed = 0;
4565                               ctxt->disableSAX = 1;
4566                           }
4567                           SKIP_BLANKS;
4568                           ndata = xmlParseName(ctxt);
4569                           if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4570                               (ctxt->sax->unparsedEntityDecl != NULL)) {
4571                               ctxt->sax->unparsedEntityDecl(ctxt->userData,
4572                                       name, literal, URI, ndata);
4573                           }
4574                       } else if (xmlStrEqual(contmod, BAD_CAST"SUBDOC")) {
4575                           if ((ctxt->sax != NULL) &&
4576                               (ctxt->sax->warning != NULL))
4577                               ctxt->sax->warning(ctxt->userData,
4578                                   "SUBDOC entities are not supported\n");
4579                           SKIP_BLANKS;
4580                           ndata = xmlParseName(ctxt);
4581                           if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4582                               (ctxt->sax->unparsedEntityDecl != NULL)) {
4583                               ctxt->sax->unparsedEntityDecl(ctxt->userData,
4584                                       name, literal, URI, ndata);
4585                           }
4586                       } else if (xmlStrEqual(contmod, BAD_CAST"CDATA")) {
4587                           if ((ctxt->sax != NULL) &&
4588                               (ctxt->sax->warning != NULL))
4589                               ctxt->sax->warning(ctxt->userData,
4590                                   "CDATA entities are not supported\n");
4591                           SKIP_BLANKS;
4592                           ndata = xmlParseName(ctxt);
4593                           if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4594                               (ctxt->sax->unparsedEntityDecl != NULL)) {
4595                               ctxt->sax->unparsedEntityDecl(ctxt->userData,
4596                                       name, literal, URI, ndata);
4597                           }
4598                       }
4599                       xmlFree(contmod);
4600                   }
4601               } else {
4602                   if ((ctxt->sax != NULL) &&
4603                       (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4604                       ctxt->sax->entityDecl(ctxt->userData, name,
4605                                   XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4606                                   literal, URI, NULL);
4607               }
4608           }
4609       }
4610       SKIP_BLANKS;
4611       if (RAW != '>') {
4612           ctxt->errNo = XML_ERR_ENTITY_NOT_FINISHED;
4613           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4614               ctxt->sax->error(ctxt->userData,
4615                   "docbParseEntityDecl: entity %s not terminated\n", name);
4616           ctxt->wellFormed = 0;
4617           ctxt->disableSAX = 1;
4618       } else {
4619           if (input != ctxt->input) {
4620               ctxt->errNo = XML_ERR_ENTITY_BOUNDARY;
4621               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4622                   ctxt->sax->error(ctxt->userData,
4623"Entity declaration doesn't start and stop in the same entity\n");
4624               ctxt->wellFormed = 0;
4625               ctxt->disableSAX = 1;
4626           }
4627           NEXT;
4628       }
4629       if (orig != NULL) {
4630           /*
4631            * Ugly mechanism to save the raw entity value.
4632            */
4633           xmlEntityPtr cur = NULL;
4634
4635           if (isParameter) {
4636               if ((ctxt->sax != NULL) &&
4637                   (ctxt->sax->getParameterEntity != NULL))
4638                   cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
4639           } else {
4640               if ((ctxt->sax != NULL) &&
4641                   (ctxt->sax->getEntity != NULL))
4642                   cur = ctxt->sax->getEntity(ctxt->userData, name);
4643           }
4644            if (cur != NULL) {
4645               if (cur->orig != NULL)
4646                   xmlFree(orig);
4647               else
4648                   cur->orig = orig;
4649           } else
4650               xmlFree(orig);
4651       }
4652       if (name != NULL) xmlFree(name);
4653       if (value != NULL) xmlFree(value);
4654       if (URI != NULL) xmlFree(URI);
4655       if (literal != NULL) xmlFree(literal);
4656       if (ndata != NULL) xmlFree(ndata);
4657    }
4658}
4659
4660/**
4661 * docbParseMarkupDecl:
4662 * @ctxt:  an SGML parser context
4663 *
4664 * parse Markup declarations
4665 *
4666 * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
4667 *                     NotationDecl | PI | Comment
4668 */
4669static void
4670docbParseMarkupDecl(xmlParserCtxtPtr ctxt) {
4671    GROW;
4672    xmlParseElementDecl(ctxt);
4673    xmlParseAttributeListDecl(ctxt);
4674    docbParseEntityDecl(ctxt);
4675    xmlParseNotationDecl(ctxt);
4676    docbParsePI(ctxt);
4677    xmlParseComment(ctxt);
4678    /*
4679     * This is only for internal subset. On external entities,
4680     * the replacement is done before parsing stage
4681     */
4682    if ((ctxt->external == 0) && (ctxt->inputNr == 1))
4683       xmlParsePEReference(ctxt);
4684    ctxt->instate = XML_PARSER_DTD;
4685}
4686
4687/**
4688 * docbParseInternalSubset:
4689 * @ctxt:  an SGML parser context
4690 *
4691 * parse the internal subset declaration
4692 *
4693 * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
4694 */
4695
4696static void
4697docbParseInternalSubset(xmlParserCtxtPtr ctxt) {
4698    /*
4699     * Is there any DTD definition ?
4700     */
4701    if (RAW == '[') {
4702        ctxt->instate = XML_PARSER_DTD;
4703        NEXT;
4704       /*
4705        * Parse the succession of Markup declarations and
4706        * PEReferences.
4707        * Subsequence (markupdecl | PEReference | S)*
4708        */
4709       while (RAW != ']') {
4710           const xmlChar *check = CUR_PTR;
4711           int cons = ctxt->input->consumed;
4712
4713           SKIP_BLANKS;
4714           docbParseMarkupDecl(ctxt);
4715           xmlParsePEReference(ctxt);
4716
4717           /*
4718            * Pop-up of finished entities.
4719            */
4720           while ((RAW == 0) && (ctxt->inputNr > 1))
4721               xmlPopInput(ctxt);
4722
4723           if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
4724               ctxt->errNo = XML_ERR_INTERNAL_ERROR;
4725               if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4726                   ctxt->sax->error(ctxt->userData,
4727            "docbParseInternalSubset: error detected in Markup declaration\n");
4728               ctxt->wellFormed = 0;
4729               ctxt->disableSAX = 1;
4730               break;
4731           }
4732       }
4733       if (RAW == ']') {
4734           NEXT;
4735           SKIP_BLANKS;
4736       }
4737    }
4738
4739    /*
4740     * We should be at the end of the DOCTYPE declaration.
4741     */
4742    if (RAW != '>') {
4743       ctxt->errNo = XML_ERR_DOCTYPE_NOT_FINISHED;
4744       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4745           ctxt->sax->error(ctxt->userData, "DOCTYPE improperly terminated\n");
4746       ctxt->wellFormed = 0;
4747       ctxt->disableSAX = 1;
4748    }
4749    NEXT;
4750}
4751
4752/**
4753 * docbParseMisc:
4754 * @ctxt:  an XML parser context
4755 *
4756 * parse an XML Misc* optional field.
4757 *
4758 * [27] Misc ::= Comment | PI |  S
4759 */
4760
4761static void
4762docbParseMisc(xmlParserCtxtPtr ctxt) {
4763    while (((RAW == '<') && (NXT(1) == '?')) ||
4764           ((RAW == '<') && (NXT(1) == '!') &&
4765           (NXT(2) == '-') && (NXT(3) == '-')) ||
4766           IS_BLANK(CUR)) {
4767        if ((RAW == '<') && (NXT(1) == '?')) {
4768            docbParsePI(ctxt);
4769        } else if (IS_BLANK(CUR)) {
4770            NEXT;
4771        } else
4772            xmlParseComment(ctxt);
4773    }
4774}
4775
4776/**
4777 * docbParseDocument :
4778 * @ctxt:  an SGML parser context
4779 *
4780 * parse an SGML document (and build a tree if using the standard SAX
4781 * interface).
4782 *
4783 * Returns 0, -1 in case of error. the parser context is augmented
4784 *                as a result of the parsing.
4785 */
4786
4787int
4788docbParseDocument(docbParserCtxtPtr ctxt) {
4789    xmlChar start[4];
4790    xmlCharEncoding enc;
4791    xmlDtdPtr dtd;
4792
4793    docbDefaultSAXHandlerInit();
4794    ctxt->html = 2;
4795
4796    GROW;
4797    /*
4798     * SAX: beginning of the document processing.
4799     */
4800    if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
4801        ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
4802
4803    /*
4804     * Get the 4 first bytes and decode the charset
4805     * if enc != XML_CHAR_ENCODING_NONE
4806     * plug some encoding conversion routines.
4807     */
4808    start[0] = RAW;
4809    start[1] = NXT(1);
4810    start[2] = NXT(2);
4811    start[3] = NXT(3);
4812    enc = xmlDetectCharEncoding(start, 4);
4813    if (enc != XML_CHAR_ENCODING_NONE) {
4814        xmlSwitchEncoding(ctxt, enc);
4815    }
4816
4817    /*
4818     * Wipe out everything which is before the first '<'
4819     */
4820    SKIP_BLANKS;
4821    if (CUR == 0) {
4822       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
4823           ctxt->sax->error(ctxt->userData, "Document is empty\n");
4824       ctxt->wellFormed = 0;
4825    }
4826
4827    if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
4828       ctxt->sax->startDocument(ctxt->userData);
4829
4830
4831    /*
4832     * The Misc part of the Prolog
4833     */
4834    GROW;
4835    docbParseMisc(ctxt);
4836
4837    /*
4838     * Then possibly doc type declaration(s) and more Misc
4839     * (doctypedecl Misc*)?
4840     */
4841    GROW;
4842    if ((RAW == '<') && (NXT(1) == '!') &&
4843       (UPP(2) == 'D') && (UPP(3) == 'O') &&
4844       (UPP(4) == 'C') && (UPP(5) == 'T') &&
4845       (UPP(6) == 'Y') && (UPP(7) == 'P') &&
4846       (UPP(8) == 'E')) {
4847
4848       ctxt->inSubset = 1;
4849       docbParseDocTypeDecl(ctxt);
4850       if (RAW == '[') {
4851           ctxt->instate = XML_PARSER_DTD;
4852           docbParseInternalSubset(ctxt);
4853       }
4854
4855       /*
4856        * Create and update the external subset.
4857        */
4858       ctxt->inSubset = 2;
4859       if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
4860           (!ctxt->disableSAX))
4861           ctxt->sax->internalSubset(ctxt->userData, ctxt->intSubName,
4862                                     ctxt->extSubSystem, ctxt->extSubURI);
4863       ctxt->inSubset = 0;
4864
4865
4866       ctxt->instate = XML_PARSER_PROLOG;
4867       docbParseMisc(ctxt);
4868    }
4869
4870    /*
4871     * Time to start parsing the tree itself
4872     */
4873    docbParseContent(ctxt);
4874
4875    /*
4876     * autoclose
4877     */
4878    if (CUR == 0)
4879       docbAutoClose(ctxt, NULL);
4880
4881
4882    /*
4883     * SAX: end of the document processing.
4884     */
4885    if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
4886        ctxt->sax->endDocument(ctxt->userData);
4887
4888    if (ctxt->myDoc != NULL) {
4889       dtd = ctxt->myDoc->intSubset;
4890       ctxt->myDoc->standalone = -1;
4891       if (dtd == NULL)
4892           ctxt->myDoc->intSubset =
4893               xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "SGML",
4894                   BAD_CAST "-//W3C//DTD SGML 4.0 Transitional//EN",
4895                   BAD_CAST "http://www.w3.org/TR/REC-docbook/loose.dtd");
4896    }
4897    if (! ctxt->wellFormed) return(-1);
4898    return(0);
4899}
4900
4901
4902/************************************************************************
4903 *                                                                     *
4904 *                     Parser contexts handling                        *
4905 *                                                                     *
4906 ************************************************************************/
4907
4908/**
4909 * docbInitParserCtxt:
4910 * @ctxt:  an SGML parser context
4911 *
4912 * Initialize a parser context
4913 */
4914
4915static void
4916docbInitParserCtxt(docbParserCtxtPtr ctxt)
4917{
4918    docbSAXHandler *sax;
4919
4920    if (ctxt == NULL) return;
4921    memset(ctxt, 0, sizeof(docbParserCtxt));
4922
4923    sax = (docbSAXHandler *) xmlMalloc(sizeof(docbSAXHandler));
4924    if (sax == NULL) {
4925        xmlGenericError(xmlGenericErrorContext,
4926               "docbInitParserCtxt: out of memory\n");
4927    }
4928    memset(sax, 0, sizeof(docbSAXHandler));
4929
4930    /* Allocate the Input stack */
4931    ctxt->inputTab = (docbParserInputPtr *)
4932                      xmlMalloc(5 * sizeof(docbParserInputPtr));
4933    if (ctxt->inputTab == NULL) {
4934        xmlGenericError(xmlGenericErrorContext,
4935               "docbInitParserCtxt: out of memory\n");
4936    }
4937    ctxt->inputNr = 0;
4938    ctxt->inputMax = 5;
4939    ctxt->input = NULL;
4940    ctxt->version = NULL;
4941    ctxt->encoding = NULL;
4942    ctxt->standalone = -1;
4943    ctxt->instate = XML_PARSER_START;
4944
4945    /* Allocate the Node stack */
4946    ctxt->nodeTab = (docbNodePtr *) xmlMalloc(10 * sizeof(docbNodePtr));
4947    ctxt->nodeNr = 0;
4948    ctxt->nodeMax = 10;
4949    ctxt->node = NULL;
4950
4951    /* Allocate the Name stack */
4952    ctxt->nameTab = (xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
4953    ctxt->nameNr = 0;
4954    ctxt->nameMax = 10;
4955    ctxt->name = NULL;
4956
4957    if (sax == NULL) ctxt->sax = &docbDefaultSAXHandler;
4958    else {
4959        ctxt->sax = sax;
4960       memcpy(sax, &docbDefaultSAXHandler, sizeof(docbSAXHandler));
4961    }
4962    ctxt->userData = ctxt;
4963    ctxt->myDoc = NULL;
4964    ctxt->wellFormed = 1;
4965    ctxt->linenumbers = xmlLineNumbersDefaultValue;
4966    ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
4967    ctxt->html = 2;
4968    ctxt->record_info = 0;
4969    ctxt->validate = 0;
4970    ctxt->nbChars = 0;
4971    ctxt->checkIndex = 0;
4972    xmlInitNodeInfoSeq(&ctxt->node_seq);
4973}
4974
4975/**
4976 * docbFreeParserCtxt:
4977 * @ctxt:  an SGML parser context
4978 *
4979 * Free all the memory used by a parser context. However the parsed
4980 * document in ctxt->myDoc is not freed.
4981 */
4982
4983void
4984docbFreeParserCtxt(docbParserCtxtPtr ctxt)
4985{
4986    xmlFreeParserCtxt(ctxt);
4987}
4988
4989/**
4990 * docbCreateDocParserCtxt :
4991 * @cur:  a pointer to an array of xmlChar
4992 * @encoding: the SGML document encoding, or NULL
4993 *
4994 * Create a parser context for an SGML document.
4995 *
4996 * Returns the new parser context or NULL
4997 */
4998static docbParserCtxtPtr
4999docbCreateDocParserCtxt(xmlChar *cur, const char *encoding ATTRIBUTE_UNUSED) {
5000    docbParserCtxtPtr ctxt;
5001    docbParserInputPtr input;
5002    /* sgmlCharEncoding enc; */
5003
5004    ctxt = (docbParserCtxtPtr) xmlMalloc(sizeof(docbParserCtxt));
5005    if (ctxt == NULL) {
5006        xmlGenericError(xmlGenericErrorContext, "malloc failed");
5007        return(NULL);
5008    }
5009    docbInitParserCtxt(ctxt);
5010    input = (docbParserInputPtr) xmlMalloc(sizeof(docbParserInput));
5011    if (input == NULL) {
5012        xmlGenericError(xmlGenericErrorContext, "malloc failed");
5013        xmlFree(ctxt);
5014        return(NULL);
5015    }
5016    memset(input, 0, sizeof(docbParserInput));
5017
5018    input->line = 1;
5019    input->col = 1;
5020    input->base = cur;
5021    input->cur = cur;
5022
5023    inputPush(ctxt, input);
5024    return(ctxt);
5025}
5026
5027/************************************************************************
5028 *                                                                     *
5029 *             Progressive parsing interfaces                          *
5030 *                                                                     *
5031 ************************************************************************/
5032
5033/**
5034 * docbParseLookupSequence:
5035 * @ctxt:  an SGML parser context
5036 * @first:  the first char to lookup
5037 * @next:  the next char to lookup or zero
5038 * @third:  the next char to lookup or zero
5039 *
5040 * Try to find if a sequence (first, next, third) or  just (first next) or
5041 * (first) is available in the input stream.
5042 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
5043 * to avoid rescanning sequences of bytes, it DOES change the state of the
5044 * parser, do not use liberally.
5045 * This is basically similar to xmlParseLookupSequence()
5046 *
5047 * Returns the index to the current parsing point if the full sequence
5048 *      is available, -1 otherwise.
5049 */
5050static int
5051docbParseLookupSequence(docbParserCtxtPtr ctxt, xmlChar first,
5052                       xmlChar next, xmlChar third) {
5053    int base, len;
5054    docbParserInputPtr in;
5055    const xmlChar *buf;
5056
5057    in = ctxt->input;
5058    if (in == NULL) return(-1);
5059    base = in->cur - in->base;
5060    if (base < 0) return(-1);
5061    if (ctxt->checkIndex > base)
5062        base = ctxt->checkIndex;
5063    if (in->buf == NULL) {
5064       buf = in->base;
5065       len = in->length;
5066    } else {
5067       buf = in->buf->buffer->content;
5068       len = in->buf->buffer->use;
5069    }
5070    /* take into account the sequence length */
5071    if (third) len -= 2;
5072    else if (next) len --;
5073    for (;base < len;base++) {
5074        if (buf[base] == first) {
5075           if (third != 0) {
5076               if ((buf[base + 1] != next) ||
5077                   (buf[base + 2] != third)) continue;
5078           } else if (next != 0) {
5079               if (buf[base + 1] != next) continue;
5080           }
5081           ctxt->checkIndex = 0;
5082#ifdef DEBUG_PUSH
5083           if (next == 0)
5084               xmlGenericError(xmlGenericErrorContext,
5085                       "HPP: lookup '%c' found at %d\n",
5086                       first, base);
5087           else if (third == 0)
5088               xmlGenericError(xmlGenericErrorContext,
5089                       "HPP: lookup '%c%c' found at %d\n",
5090                       first, next, base);
5091           else
5092               xmlGenericError(xmlGenericErrorContext,
5093                       "HPP: lookup '%c%c%c' found at %d\n",
5094                       first, next, third, base);
5095#endif
5096           return(base - (in->cur - in->base));
5097       }
5098    }
5099    ctxt->checkIndex = base;
5100#ifdef DEBUG_PUSH
5101    if (next == 0)
5102       xmlGenericError(xmlGenericErrorContext,
5103               "HPP: lookup '%c' failed\n", first);
5104    else if (third == 0)
5105       xmlGenericError(xmlGenericErrorContext,
5106               "HPP: lookup '%c%c' failed\n", first, next);
5107    else
5108       xmlGenericError(xmlGenericErrorContext,
5109               "HPP: lookup '%c%c%c' failed\n", first, next, third);
5110#endif
5111    return(-1);
5112}
5113
5114/**
5115 * docbParseTryOrFinish:
5116 * @ctxt:  an SGML parser context
5117 * @terminate:  last chunk indicator
5118 *
5119 * Try to progress on parsing
5120 *
5121 * Returns zero if no parsing was possible
5122 */
5123static int
5124docbParseTryOrFinish(docbParserCtxtPtr ctxt, int terminate) {
5125    int ret = 0;
5126    docbParserInputPtr in;
5127    int avail = 0;
5128    xmlChar cur, next;
5129
5130#ifdef DEBUG_PUSH
5131    switch (ctxt->instate) {
5132       case XML_PARSER_EOF:
5133           xmlGenericError(xmlGenericErrorContext,
5134                   "HPP: try EOF\n"); break;
5135       case XML_PARSER_START:
5136           xmlGenericError(xmlGenericErrorContext,
5137                   "HPP: try START\n"); break;
5138       case XML_PARSER_MISC:
5139           xmlGenericError(xmlGenericErrorContext,
5140                   "HPP: try MISC\n");break;
5141       case XML_PARSER_COMMENT:
5142           xmlGenericError(xmlGenericErrorContext,
5143                   "HPP: try COMMENT\n");break;
5144       case XML_PARSER_PROLOG:
5145           xmlGenericError(xmlGenericErrorContext,
5146                   "HPP: try PROLOG\n");break;
5147       case XML_PARSER_START_TAG:
5148           xmlGenericError(xmlGenericErrorContext,
5149                   "HPP: try START_TAG\n");break;
5150       case XML_PARSER_CONTENT:
5151           xmlGenericError(xmlGenericErrorContext,
5152                   "HPP: try CONTENT\n");break;
5153       case XML_PARSER_CDATA_SECTION:
5154           xmlGenericError(xmlGenericErrorContext,
5155                   "HPP: try CDATA_SECTION\n");break;
5156       case XML_PARSER_END_TAG:
5157           xmlGenericError(xmlGenericErrorContext,
5158                   "HPP: try END_TAG\n");break;
5159       case XML_PARSER_ENTITY_DECL:
5160           xmlGenericError(xmlGenericErrorContext,
5161                   "HPP: try ENTITY_DECL\n");break;
5162       case XML_PARSER_ENTITY_VALUE:
5163           xmlGenericError(xmlGenericErrorContext,
5164                   "HPP: try ENTITY_VALUE\n");break;
5165       case XML_PARSER_ATTRIBUTE_VALUE:
5166           xmlGenericError(xmlGenericErrorContext,
5167                   "HPP: try ATTRIBUTE_VALUE\n");break;
5168       case XML_PARSER_DTD:
5169           xmlGenericError(xmlGenericErrorContext,
5170                   "HPP: try DTD\n");break;
5171       case XML_PARSER_EPILOG:
5172           xmlGenericError(xmlGenericErrorContext,
5173                   "HPP: try EPILOG\n");break;
5174       case XML_PARSER_PI:
5175           xmlGenericError(xmlGenericErrorContext,
5176                   "HPP: try PI\n");break;
5177    }
5178#endif
5179
5180    while (1) {
5181
5182       in = ctxt->input;
5183       if (in == NULL) break;
5184       if (in->buf == NULL)
5185           avail = in->length - (in->cur - in->base);
5186       else
5187           avail = in->buf->buffer->use - (in->cur - in->base);
5188       if ((avail == 0) && (terminate)) {
5189           docbAutoClose(ctxt, NULL);
5190           if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5191               /*
5192                * SAX: end of the document processing.
5193                */
5194               ctxt->instate = XML_PARSER_EOF;
5195               if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5196                   ctxt->sax->endDocument(ctxt->userData);
5197           }
5198       }
5199        if (avail < 1)
5200           goto done;
5201        switch (ctxt->instate) {
5202            case XML_PARSER_EOF:
5203               /*
5204                * Document parsing is done !
5205                */
5206               goto done;
5207            case XML_PARSER_START:
5208               /*
5209                * Very first chars read from the document flow.
5210                */
5211               cur = in->cur[0];
5212               if (IS_BLANK(cur)) {
5213                   SKIP_BLANKS;
5214                   if (in->buf == NULL)
5215                       avail = in->length - (in->cur - in->base);
5216                   else
5217                       avail = in->buf->buffer->use - (in->cur - in->base);
5218               }
5219               if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
5220                   ctxt->sax->setDocumentLocator(ctxt->userData,
5221                                                 &xmlDefaultSAXLocator);
5222               if ((ctxt->sax) && (ctxt->sax->startDocument) &&
5223                   (!ctxt->disableSAX))
5224                   ctxt->sax->startDocument(ctxt->userData);
5225
5226               cur = in->cur[0];
5227               next = in->cur[1];
5228               if ((cur == '<') && (next == '!') &&
5229                   (UPP(2) == 'D') && (UPP(3) == 'O') &&
5230                   (UPP(4) == 'C') && (UPP(5) == 'T') &&
5231                   (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5232                   (UPP(8) == 'E')) {
5233                   if ((!terminate) &&
5234                       (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5235                       goto done;
5236#ifdef DEBUG_PUSH
5237                   xmlGenericError(xmlGenericErrorContext,
5238                           "HPP: Parsing internal subset\n");
5239#endif
5240                   docbParseDocTypeDecl(ctxt);
5241                   ctxt->instate = XML_PARSER_PROLOG;
5242#ifdef DEBUG_PUSH
5243                   xmlGenericError(xmlGenericErrorContext,
5244                           "HPP: entering PROLOG\n");
5245#endif
5246                } else {
5247                   ctxt->instate = XML_PARSER_MISC;
5248               }
5249#ifdef DEBUG_PUSH
5250               xmlGenericError(xmlGenericErrorContext,
5251                       "HPP: entering MISC\n");
5252#endif
5253               break;
5254            case XML_PARSER_MISC:
5255               SKIP_BLANKS;
5256               if (in->buf == NULL)
5257                   avail = in->length - (in->cur - in->base);
5258               else
5259                   avail = in->buf->buffer->use - (in->cur - in->base);
5260               if (avail < 2)
5261                   goto done;
5262               cur = in->cur[0];
5263               next = in->cur[1];
5264               if ((cur == '<') && (next == '!') &&
5265                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
5266                   if ((!terminate) &&
5267                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
5268                       goto done;
5269#ifdef DEBUG_PUSH
5270                   xmlGenericError(xmlGenericErrorContext,
5271                           "HPP: Parsing Comment\n");
5272#endif
5273                   docbParseComment(ctxt);
5274                   ctxt->instate = XML_PARSER_MISC;
5275               } else if ((cur == '<') && (next == '!') &&
5276                   (UPP(2) == 'D') && (UPP(3) == 'O') &&
5277                   (UPP(4) == 'C') && (UPP(5) == 'T') &&
5278                   (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5279                   (UPP(8) == 'E')) {
5280                   if ((!terminate) &&
5281                       (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5282                       goto done;
5283#ifdef DEBUG_PUSH
5284                   xmlGenericError(xmlGenericErrorContext,
5285                           "HPP: Parsing internal subset\n");
5286#endif
5287                   docbParseDocTypeDecl(ctxt);
5288                   ctxt->instate = XML_PARSER_PROLOG;
5289#ifdef DEBUG_PUSH
5290                   xmlGenericError(xmlGenericErrorContext,
5291                           "HPP: entering PROLOG\n");
5292#endif
5293               } else if ((cur == '<') && (next == '!') &&
5294                          (avail < 9)) {
5295                   goto done;
5296               } else {
5297                   ctxt->instate = XML_PARSER_START_TAG;
5298#ifdef DEBUG_PUSH
5299                   xmlGenericError(xmlGenericErrorContext,
5300                           "HPP: entering START_TAG\n");
5301#endif
5302               }
5303               break;
5304            case XML_PARSER_PROLOG:
5305               SKIP_BLANKS;
5306               if (in->buf == NULL)
5307                   avail = in->length - (in->cur - in->base);
5308               else
5309                   avail = in->buf->buffer->use - (in->cur - in->base);
5310               if (avail < 2)
5311                   goto done;
5312               cur = in->cur[0];
5313               next = in->cur[1];
5314               if ((cur == '<') && (next == '!') &&
5315                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
5316                   if ((!terminate) &&
5317                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
5318                       goto done;
5319#ifdef DEBUG_PUSH
5320                   xmlGenericError(xmlGenericErrorContext,
5321                           "HPP: Parsing Comment\n");
5322#endif
5323                   docbParseComment(ctxt);
5324                   ctxt->instate = XML_PARSER_PROLOG;
5325               } else if ((cur == '<') && (next == '!') &&
5326                          (avail < 4)) {
5327                   goto done;
5328               } else {
5329                   ctxt->instate = XML_PARSER_START_TAG;
5330#ifdef DEBUG_PUSH
5331                   xmlGenericError(xmlGenericErrorContext,
5332                           "HPP: entering START_TAG\n");
5333#endif
5334               }
5335               break;
5336            case XML_PARSER_EPILOG:
5337               if (in->buf == NULL)
5338                   avail = in->length - (in->cur - in->base);
5339               else
5340                   avail = in->buf->buffer->use - (in->cur - in->base);
5341               if (avail < 1)
5342                   goto done;
5343               cur = in->cur[0];
5344               if (IS_BLANK(cur)) {
5345                   docbParseCharData(ctxt);
5346                   goto done;
5347               }
5348               if (avail < 2)
5349                   goto done;
5350               next = in->cur[1];
5351               if ((cur == '<') && (next == '!') &&
5352                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
5353                   if ((!terminate) &&
5354                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
5355                       goto done;
5356#ifdef DEBUG_PUSH
5357                   xmlGenericError(xmlGenericErrorContext,
5358                           "HPP: Parsing Comment\n");
5359#endif
5360                   docbParseComment(ctxt);
5361                   ctxt->instate = XML_PARSER_EPILOG;
5362               } else if ((cur == '<') && (next == '!') &&
5363                          (avail < 4)) {
5364                   goto done;
5365               } else {
5366                   ctxt->errNo = XML_ERR_DOCUMENT_END;
5367                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5368                       ctxt->sax->error(ctxt->userData,
5369                           "Extra content at the end of the document\n");
5370                   ctxt->wellFormed = 0;
5371                   ctxt->instate = XML_PARSER_EOF;
5372#ifdef DEBUG_PUSH
5373                   xmlGenericError(xmlGenericErrorContext,
5374                           "HPP: entering EOF\n");
5375#endif
5376                   if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5377                       ctxt->sax->endDocument(ctxt->userData);
5378                   goto done;
5379               }
5380               break;
5381            case XML_PARSER_START_TAG: {
5382               xmlChar *name, *oldname;
5383               int depth = ctxt->nameNr;
5384               docbElemDescPtr info;
5385
5386               if (avail < 2)
5387                   goto done;
5388               cur = in->cur[0];
5389               if (cur != '<') {
5390                   ctxt->instate = XML_PARSER_CONTENT;
5391#ifdef DEBUG_PUSH
5392                   xmlGenericError(xmlGenericErrorContext,
5393                           "HPP: entering CONTENT\n");
5394#endif
5395                   break;
5396               }
5397               if ((!terminate) &&
5398                   (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5399                   goto done;
5400
5401               oldname = xmlStrdup(ctxt->name);
5402               docbParseStartTag(ctxt);
5403               name = ctxt->name;
5404#ifdef DEBUG
5405               if (oldname == NULL)
5406                   xmlGenericError(xmlGenericErrorContext,
5407                           "Start of element %s\n", name);
5408               else if (name == NULL)
5409                   xmlGenericError(xmlGenericErrorContext,
5410                           "Start of element failed, was %s\n",
5411                           oldname);
5412               else
5413                   xmlGenericError(xmlGenericErrorContext,
5414                           "Start of element %s, was %s\n",
5415                           name, oldname);
5416#endif
5417               if (((depth == ctxt->nameNr) &&
5418                    (xmlStrEqual(oldname, ctxt->name))) ||
5419                   (name == NULL)) {
5420                   if (CUR == '>')
5421                       NEXT;
5422                   if (oldname != NULL)
5423                       xmlFree(oldname);
5424                   break;
5425               }
5426               if (oldname != NULL)
5427                   xmlFree(oldname);
5428
5429               /*
5430                * Lookup the info for that element.
5431                */
5432               info = docbTagLookup(name);
5433               if (info == NULL) {
5434                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5435                       ctxt->sax->error(ctxt->userData, "Tag %s unknown\n",
5436                                        name);
5437                   ctxt->wellFormed = 0;
5438               } else if (info->depr) {
5439                   /***************************
5440                   if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
5441                       ctxt->sax->warning(ctxt->userData,
5442                                          "Tag %s is deprecated\n",
5443                                          name);
5444                    ***************************/
5445               }
5446
5447               /*
5448                * Check for an Empty Element labeled the XML/SGML way
5449                */
5450               if ((CUR == '/') && (NXT(1) == '>')) {
5451                   SKIP(2);
5452                   if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5453                       ctxt->sax->endElement(ctxt->userData, name);
5454                   oldname = docbnamePop(ctxt);
5455#ifdef DEBUG
5456                   xmlGenericError(xmlGenericErrorContext,"End of tag the XML way: popping out %s\n",
5457                           oldname);
5458#endif
5459                   if (oldname != NULL)
5460                       xmlFree(oldname);
5461                   ctxt->instate = XML_PARSER_CONTENT;
5462#ifdef DEBUG_PUSH
5463                   xmlGenericError(xmlGenericErrorContext,
5464                           "HPP: entering CONTENT\n");
5465#endif
5466                   break;
5467               }
5468
5469               if (CUR == '>') {
5470                   NEXT;
5471               } else {
5472                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5473                       ctxt->sax->error(ctxt->userData,
5474                                        "Couldn't find end of Start Tag %s\n",
5475                                        name);
5476                   ctxt->wellFormed = 0;
5477
5478                   /*
5479                    * end of parsing of this node.
5480                    */
5481                   if (xmlStrEqual(name, ctxt->name)) {
5482                       nodePop(ctxt);
5483                       oldname = docbnamePop(ctxt);
5484#ifdef DEBUG
5485                       xmlGenericError(xmlGenericErrorContext,
5486                        "End of start tag problem: popping out %s\n", oldname);
5487#endif
5488                       if (oldname != NULL)
5489                           xmlFree(oldname);
5490                   }
5491
5492                   ctxt->instate = XML_PARSER_CONTENT;
5493#ifdef DEBUG_PUSH
5494                   xmlGenericError(xmlGenericErrorContext,
5495                           "HPP: entering CONTENT\n");
5496#endif
5497                   break;
5498               }
5499
5500               /*
5501                * Check for an Empty Element from DTD definition
5502                */
5503               if ((info != NULL) && (info->empty)) {
5504                   if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
5505                       ctxt->sax->endElement(ctxt->userData, name);
5506                   oldname = docbnamePop(ctxt);
5507#ifdef DEBUG
5508                   xmlGenericError(xmlGenericErrorContext,"End of empty tag %s : popping out %s\n", name, oldname);
5509#endif
5510                   if (oldname != NULL)
5511                       xmlFree(oldname);
5512               }
5513               ctxt->instate = XML_PARSER_CONTENT;
5514#ifdef DEBUG_PUSH
5515               xmlGenericError(xmlGenericErrorContext,
5516                       "HPP: entering CONTENT\n");
5517#endif
5518                break;
5519           }
5520            case XML_PARSER_CONTENT: {
5521               long cons;
5522                /*
5523                * Handle preparsed entities and charRef
5524                */
5525               if (ctxt->token != 0) {
5526                   xmlChar chr[2] = { 0 , 0 } ;
5527
5528                   chr[0] = (xmlChar) ctxt->token;
5529                   if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
5530                       ctxt->sax->characters(ctxt->userData, chr, 1);
5531                   ctxt->token = 0;
5532                   ctxt->checkIndex = 0;
5533               }
5534               if ((avail == 1) && (terminate)) {
5535                   cur = in->cur[0];
5536                   if ((cur != '<') && (cur != '&')) {
5537                       if (ctxt->sax != NULL) {
5538                           if (IS_BLANK(cur)) {
5539                               if (ctxt->sax->ignorableWhitespace != NULL)
5540                                   ctxt->sax->ignorableWhitespace(
5541                                           ctxt->userData, &cur, 1);
5542                           } else {
5543                               if (ctxt->sax->characters != NULL)
5544                                   ctxt->sax->characters(
5545                                           ctxt->userData, &cur, 1);
5546                           }
5547                       }
5548                       ctxt->token = 0;
5549                       ctxt->checkIndex = 0;
5550                       NEXT;
5551                   }
5552                   break;
5553               }
5554               if (avail < 2)
5555                   goto done;
5556               cur = in->cur[0];
5557               next = in->cur[1];
5558               cons = ctxt->nbChars;
5559               /*
5560                * Sometimes DOCTYPE arrives in the middle of the document
5561                */
5562               if ((cur == '<') && (next == '!') &&
5563                   (UPP(2) == 'D') && (UPP(3) == 'O') &&
5564                   (UPP(4) == 'C') && (UPP(5) == 'T') &&
5565                   (UPP(6) == 'Y') && (UPP(7) == 'P') &&
5566                   (UPP(8) == 'E')) {
5567                   if ((!terminate) &&
5568                       (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5569                       goto done;
5570                   if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5571                       ctxt->sax->error(ctxt->userData,
5572                            "Misplaced DOCTYPE declaration\n");
5573                   ctxt->wellFormed = 0;
5574                   docbParseDocTypeDecl(ctxt);
5575               } else if ((cur == '<') && (next == '!') &&
5576                   (in->cur[2] == '-') && (in->cur[3] == '-')) {
5577                   if ((!terminate) &&
5578                       (docbParseLookupSequence(ctxt, '-', '-', '>') < 0))
5579                       goto done;
5580#ifdef DEBUG_PUSH
5581                   xmlGenericError(xmlGenericErrorContext,
5582                           "HPP: Parsing Comment\n");
5583#endif
5584                   docbParseComment(ctxt);
5585                   ctxt->instate = XML_PARSER_CONTENT;
5586               } else if ((cur == '<') && (next == '!') && (avail < 4)) {
5587                   goto done;
5588               } else if ((cur == '<') && (next == '/')) {
5589                   ctxt->instate = XML_PARSER_END_TAG;
5590                   ctxt->checkIndex = 0;
5591#ifdef DEBUG_PUSH
5592                   xmlGenericError(xmlGenericErrorContext,
5593                           "HPP: entering END_TAG\n");
5594#endif
5595                   break;
5596               } else if (cur == '<') {
5597                   ctxt->instate = XML_PARSER_START_TAG;
5598                   ctxt->checkIndex = 0;
5599#ifdef DEBUG_PUSH
5600                   xmlGenericError(xmlGenericErrorContext,
5601                           "HPP: entering START_TAG\n");
5602#endif
5603                   break;
5604               } else if (cur == '&') {
5605                   if ((!terminate) &&
5606                       (docbParseLookupSequence(ctxt, ';', 0, 0) < 0))
5607                       goto done;
5608#ifdef DEBUG_PUSH
5609                   xmlGenericError(xmlGenericErrorContext,
5610                           "HPP: Parsing Reference\n");
5611#endif
5612                   /* TODO: check generation of subtrees if noent !!! */
5613                   docbParseReference(ctxt);
5614               } else {
5615                   /* TODO Avoid the extra copy, handle directly !!!!!! */
5616                   /*
5617                    * Goal of the following test is :
5618                    *  - minimize calls to the SAX 'character' callback
5619                    *    when they are mergeable
5620                    */
5621                   if ((ctxt->inputNr == 1) &&
5622                       (avail < DOCB_PARSER_BIG_BUFFER_SIZE)) {
5623                       if ((!terminate) &&
5624                           (docbParseLookupSequence(ctxt, '<', 0, 0) < 0))
5625                           goto done;
5626                    }
5627                   ctxt->checkIndex = 0;
5628#ifdef DEBUG_PUSH
5629                   xmlGenericError(xmlGenericErrorContext,
5630                           "HPP: Parsing char data\n");
5631#endif
5632                   docbParseCharData(ctxt);
5633               }
5634               if (cons == ctxt->nbChars) {
5635                   if (ctxt->node != NULL) {
5636                       if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5637                           ctxt->sax->error(ctxt->userData,
5638                                "detected an error in element content\n");
5639                       ctxt->wellFormed = 0;
5640                       NEXT;
5641                   }
5642                   break;
5643               }
5644
5645               break;
5646           }
5647            case XML_PARSER_END_TAG:
5648               if (avail < 2)
5649                   goto done;
5650               if ((!terminate) &&
5651                   (docbParseLookupSequence(ctxt, '>', 0, 0) < 0))
5652                   goto done;
5653               docbParseEndTag(ctxt);
5654               if (ctxt->nameNr == 0) {
5655                   ctxt->instate = XML_PARSER_EPILOG;
5656               } else {
5657                   ctxt->instate = XML_PARSER_CONTENT;
5658               }
5659               ctxt->checkIndex = 0;
5660#ifdef DEBUG_PUSH
5661               xmlGenericError(xmlGenericErrorContext,
5662                       "HPP: entering CONTENT\n");
5663#endif
5664               break;
5665            case XML_PARSER_CDATA_SECTION:
5666               xmlGenericError(xmlGenericErrorContext,
5667                       "HPP: internal error, state == CDATA\n");
5668               ctxt->instate = XML_PARSER_CONTENT;
5669               ctxt->checkIndex = 0;
5670#ifdef DEBUG_PUSH
5671               xmlGenericError(xmlGenericErrorContext,
5672                       "HPP: entering CONTENT\n");
5673#endif
5674               break;
5675            case XML_PARSER_DTD:
5676               xmlGenericError(xmlGenericErrorContext,
5677                       "HPP: internal error, state == DTD\n");
5678               ctxt->instate = XML_PARSER_CONTENT;
5679               ctxt->checkIndex = 0;
5680#ifdef DEBUG_PUSH
5681               xmlGenericError(xmlGenericErrorContext,
5682                       "HPP: entering CONTENT\n");
5683#endif
5684               break;
5685            case XML_PARSER_COMMENT:
5686               xmlGenericError(xmlGenericErrorContext,
5687                       "HPP: internal error, state == COMMENT\n");
5688               ctxt->instate = XML_PARSER_CONTENT;
5689               ctxt->checkIndex = 0;
5690#ifdef DEBUG_PUSH
5691               xmlGenericError(xmlGenericErrorContext,
5692                       "HPP: entering CONTENT\n");
5693#endif
5694               break;
5695            case XML_PARSER_PI:
5696               xmlGenericError(xmlGenericErrorContext,
5697                       "HPP: internal error, state == PI\n");
5698               ctxt->instate = XML_PARSER_CONTENT;
5699               ctxt->checkIndex = 0;
5700#ifdef DEBUG_PUSH
5701               xmlGenericError(xmlGenericErrorContext,
5702                       "HPP: entering CONTENT\n");
5703#endif
5704               break;
5705            case XML_PARSER_ENTITY_DECL:
5706               xmlGenericError(xmlGenericErrorContext,
5707                       "HPP: internal error, state == ENTITY_DECL\n");
5708               ctxt->instate = XML_PARSER_CONTENT;
5709               ctxt->checkIndex = 0;
5710#ifdef DEBUG_PUSH
5711               xmlGenericError(xmlGenericErrorContext,
5712                       "HPP: entering CONTENT\n");
5713#endif
5714               break;
5715            case XML_PARSER_ENTITY_VALUE:
5716               xmlGenericError(xmlGenericErrorContext,
5717                       "HPP: internal error, state == ENTITY_VALUE\n");
5718               ctxt->instate = XML_PARSER_CONTENT;
5719               ctxt->checkIndex = 0;
5720#ifdef DEBUG_PUSH
5721               xmlGenericError(xmlGenericErrorContext,
5722                       "HPP: entering DTD\n");
5723#endif
5724               break;
5725            case XML_PARSER_ATTRIBUTE_VALUE:
5726               xmlGenericError(xmlGenericErrorContext,
5727                       "HPP: internal error, state == ATTRIBUTE_VALUE\n");
5728               ctxt->instate = XML_PARSER_START_TAG;
5729               ctxt->checkIndex = 0;
5730#ifdef DEBUG_PUSH
5731               xmlGenericError(xmlGenericErrorContext,
5732                       "HPP: entering START_TAG\n");
5733#endif
5734               break;
5735           case XML_PARSER_SYSTEM_LITERAL:
5736               xmlGenericError(xmlGenericErrorContext,
5737                       "HPP: internal error, state == XML_PARSER_SYSTEM_LITERAL\n");
5738               ctxt->instate = XML_PARSER_CONTENT;
5739               ctxt->checkIndex = 0;
5740#ifdef DEBUG_PUSH
5741               xmlGenericError(xmlGenericErrorContext,
5742                       "HPP: entering CONTENT\n");
5743#endif
5744               break;
5745
5746           case XML_PARSER_IGNORE:
5747               xmlGenericError(xmlGenericErrorContext,
5748                       "HPP: internal error, state == XML_PARSER_IGNORE\n");
5749               ctxt->instate = XML_PARSER_CONTENT;
5750               ctxt->checkIndex = 0;
5751#ifdef DEBUG_PUSH
5752               xmlGenericError(xmlGenericErrorContext,
5753                       "HPP: entering CONTENT\n");
5754#endif
5755               break;
5756	    case XML_PARSER_PUBLIC_LITERAL:
5757		xmlGenericError(xmlGenericErrorContext,
5758			"HPP: internal error, state == XML_PARSER_LITERAL\n");
5759		ctxt->instate = XML_PARSER_CONTENT;
5760		ctxt->checkIndex = 0;
5761#ifdef DEBUG_PUSH
5762		xmlGenericError(xmlGenericErrorContext,
5763			"HPP: entering CONTENT\n");
5764#endif
5765		break;
5766       }
5767    }
5768done:
5769    if ((avail == 0) && (terminate)) {
5770       docbAutoClose(ctxt, NULL);
5771       if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
5772           /*
5773            * SAX: end of the document processing.
5774            */
5775           ctxt->instate = XML_PARSER_EOF;
5776           if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5777               ctxt->sax->endDocument(ctxt->userData);
5778       }
5779    }
5780    if ((ctxt->myDoc != NULL) &&
5781       ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
5782        (ctxt->instate == XML_PARSER_EPILOG))) {
5783       xmlDtdPtr dtd;
5784       dtd = ctxt->myDoc->intSubset;
5785       if (dtd == NULL)
5786           ctxt->myDoc->intSubset =
5787               xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "SGML",
5788                   BAD_CAST "-//W3C//DTD SGML 4.0 Transitional//EN",
5789                   BAD_CAST "http://www.w3.org/TR/REC-docbook/loose.dtd");
5790    }
5791#ifdef DEBUG_PUSH
5792    xmlGenericError(xmlGenericErrorContext, "HPP: done %d\n", ret);
5793#endif
5794    return(ret);
5795}
5796
5797/**
5798 * docbParseChunk:
5799 * @ctxt:  an XML parser context
5800 * @chunk:  an char array
5801 * @size:  the size in byte of the chunk
5802 * @terminate:  last chunk indicator
5803 *
5804 * Parse a Chunk of memory
5805 *
5806 * Returns zero if no error, the xmlParserErrors otherwise.
5807 */
5808int
5809docbParseChunk(docbParserCtxtPtr ctxt, const char *chunk, int size,
5810              int terminate) {
5811    if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5812        (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF))  {
5813       int base = ctxt->input->base - ctxt->input->buf->buffer->content;
5814       int cur = ctxt->input->cur - ctxt->input->base;
5815
5816       xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5817       ctxt->input->base = ctxt->input->buf->buffer->content + base;
5818       ctxt->input->cur = ctxt->input->base + cur;
5819#ifdef DEBUG_PUSH
5820       xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5821#endif
5822
5823       if ((terminate) || (ctxt->input->buf->buffer->use > 80))
5824           docbParseTryOrFinish(ctxt, terminate);
5825    } else if (ctxt->instate != XML_PARSER_EOF) {
5826       xmlParserInputBufferPush(ctxt->input->buf, 0, "");
5827        docbParseTryOrFinish(ctxt, terminate);
5828    }
5829    if (terminate) {
5830       if ((ctxt->instate != XML_PARSER_EOF) &&
5831           (ctxt->instate != XML_PARSER_EPILOG) &&
5832           (ctxt->instate != XML_PARSER_MISC)) {
5833           ctxt->errNo = XML_ERR_DOCUMENT_END;
5834           if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
5835               ctxt->sax->error(ctxt->userData,
5836                   "Extra content at the end of the document\n");
5837           ctxt->wellFormed = 0;
5838       }
5839       if (ctxt->instate != XML_PARSER_EOF) {
5840           if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
5841               ctxt->sax->endDocument(ctxt->userData);
5842       }
5843       ctxt->instate = XML_PARSER_EOF;
5844    }
5845    return((xmlParserErrors) ctxt->errNo);
5846}
5847
5848/************************************************************************
5849 *                                                                     *
5850 *                     User entry points                               *
5851 *                                                                     *
5852 ************************************************************************/
5853
5854/**
5855 * docbCreatePushParserCtxt :
5856 * @sax:  a SAX handler
5857 * @user_data:  The user data returned on SAX callbacks
5858 * @chunk:  a pointer to an array of chars
5859 * @size:  number of chars in the array
5860 * @filename:  an optional file name or URI
5861 * @enc:  an optional encoding
5862 *
5863 * Create a parser context for using the DocBook SGML parser in push mode
5864 * To allow content encoding detection, @size should be >= 4
5865 * The value of @filename is used for fetching external entities
5866 * and error/warning reports.
5867 *
5868 * Returns the new parser context or NULL
5869 */
5870docbParserCtxtPtr
5871docbCreatePushParserCtxt(docbSAXHandlerPtr sax, void *user_data,
5872                         const char *chunk, int size, const char *filename,
5873                        xmlCharEncoding enc) {
5874    docbParserCtxtPtr ctxt;
5875    docbParserInputPtr inputStream;
5876    xmlParserInputBufferPtr buf;
5877
5878    buf = xmlAllocParserInputBuffer(enc);
5879    if (buf == NULL) return(NULL);
5880
5881    ctxt = (docbParserCtxtPtr) xmlMalloc(sizeof(docbParserCtxt));
5882    if (ctxt == NULL) {
5883       xmlFree(buf);
5884       return(NULL);
5885    }
5886    memset(ctxt, 0, sizeof(docbParserCtxt));
5887    docbInitParserCtxt(ctxt);
5888    if (sax != NULL) {
5889       if (ctxt->sax != &docbDefaultSAXHandler)
5890           xmlFree(ctxt->sax);
5891       ctxt->sax = (docbSAXHandlerPtr) xmlMalloc(sizeof(docbSAXHandler));
5892       if (ctxt->sax == NULL) {
5893           xmlFree(buf);
5894           xmlFree(ctxt);
5895           return(NULL);
5896       }
5897       memcpy(ctxt->sax, sax, sizeof(docbSAXHandler));
5898       if (user_data != NULL)
5899           ctxt->userData = user_data;
5900    }
5901    if (filename == NULL) {
5902       ctxt->directory = NULL;
5903    } else {
5904        ctxt->directory = xmlParserGetDirectory(filename);
5905    }
5906
5907    inputStream = docbNewInputStream(ctxt);
5908    if (inputStream == NULL) {
5909       xmlFreeParserCtxt(ctxt);
5910       return(NULL);
5911    }
5912
5913    if (filename == NULL)
5914       inputStream->filename = NULL;
5915    else
5916       inputStream->filename = xmlMemStrdup(filename);
5917    inputStream->buf = buf;
5918    inputStream->base = inputStream->buf->buffer->content;
5919    inputStream->cur = inputStream->buf->buffer->content;
5920
5921    inputPush(ctxt, inputStream);
5922
5923    if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
5924        (ctxt->input->buf != NULL))  {
5925       xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
5926#ifdef DEBUG_PUSH
5927       xmlGenericError(xmlGenericErrorContext, "HPP: pushed %d\n", size);
5928#endif
5929    }
5930
5931    return(ctxt);
5932}
5933
5934/**
5935 * docbSAXParseDoc :
5936 * @cur:  a pointer to an array of xmlChar
5937 * @encoding:  a free form C string describing the SGML document encoding, or NULL
5938 * @sax:  the SAX handler block
5939 * @userData: if using SAX, this pointer will be provided on callbacks.
5940 *
5941 * parse an SGML in-memory document and build a tree.
5942 * It use the given SAX function block to handle the parsing callback.
5943 * If sax is NULL, fallback to the default DOM tree building routines.
5944 *
5945 * Returns the resulting document tree
5946 */
5947
5948docbDocPtr
5949docbSAXParseDoc(xmlChar *cur, const char *encoding, docbSAXHandlerPtr sax, void *userData) {
5950    docbDocPtr ret;
5951    docbParserCtxtPtr ctxt;
5952
5953    if (cur == NULL) return(NULL);
5954
5955
5956    ctxt = docbCreateDocParserCtxt(cur, encoding);
5957    if (ctxt == NULL) return(NULL);
5958    if (sax != NULL) {
5959        ctxt->sax = sax;
5960        ctxt->userData = userData;
5961    }
5962
5963    docbParseDocument(ctxt);
5964    ret = ctxt->myDoc;
5965    if (sax != NULL) {
5966       ctxt->sax = NULL;
5967       ctxt->userData = NULL;
5968    }
5969    docbFreeParserCtxt(ctxt);
5970
5971    return(ret);
5972}
5973
5974/**
5975 * docbParseDoc :
5976 * @cur:  a pointer to an array of xmlChar
5977 * @encoding:  a free form C string describing the SGML document encoding, or NULL
5978 *
5979 * parse an SGML in-memory document and build a tree.
5980 *
5981 * Returns the resulting document tree
5982 */
5983
5984docbDocPtr
5985docbParseDoc(xmlChar *cur, const char *encoding) {
5986    return(docbSAXParseDoc(cur, encoding, NULL, NULL));
5987}
5988
5989
5990/**
5991 * docbCreateFileParserCtxt :
5992 * @filename:  the filename
5993 * @encoding:  the SGML document encoding, or NULL
5994 *
5995 * Create a parser context for a file content.
5996 * Automatic support for ZLIB/Compress compressed document is provided
5997 * by default if found at compile-time.
5998 *
5999 * Returns the new parser context or NULL
6000 */
6001docbParserCtxtPtr
6002docbCreateFileParserCtxt(const char *filename,
6003	                 const char *encoding ATTRIBUTE_UNUSED)
6004{
6005    docbParserCtxtPtr ctxt;
6006    docbParserInputPtr inputStream;
6007    xmlParserInputBufferPtr buf;
6008    /* sgmlCharEncoding enc; */
6009
6010    buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
6011    if (buf == NULL) return(NULL);
6012
6013    ctxt = (docbParserCtxtPtr) xmlMalloc(sizeof(docbParserCtxt));
6014    if (ctxt == NULL) {
6015        xmlGenericError(xmlGenericErrorContext, "malloc failed");
6016        return(NULL);
6017    }
6018    memset(ctxt, 0, sizeof(docbParserCtxt));
6019    docbInitParserCtxt(ctxt);
6020    inputStream = (docbParserInputPtr) xmlMalloc(sizeof(docbParserInput));
6021    if (inputStream == NULL) {
6022        xmlGenericError(xmlGenericErrorContext, "malloc failed");
6023        xmlFree(ctxt);
6024        return(NULL);
6025    }
6026    memset(inputStream, 0, sizeof(docbParserInput));
6027
6028    inputStream->filename = xmlMemStrdup(filename);
6029    inputStream->line = 1;
6030    inputStream->col = 1;
6031    inputStream->buf = buf;
6032    inputStream->directory = NULL;
6033
6034    inputStream->base = inputStream->buf->buffer->content;
6035    inputStream->cur = inputStream->buf->buffer->content;
6036    inputStream->free = NULL;
6037
6038    inputPush(ctxt, inputStream);
6039    return(ctxt);
6040}
6041
6042/**
6043 * docbSAXParseFile :
6044 * @filename:  the filename
6045 * @encoding:  a free form C string describing the SGML document encoding, or NULL
6046 * @sax:  the SAX handler block
6047 * @userData: if using SAX, this pointer will be provided on callbacks.
6048 *
6049 * parse an SGML file and build a tree. Automatic support for ZLIB/Compress
6050 * compressed document is provided by default if found at compile-time.
6051 * It use the given SAX function block to handle the parsing callback.
6052 * If sax is NULL, fallback to the default DOM tree building routines.
6053 *
6054 * Returns the resulting document tree
6055 */
6056
6057docbDocPtr
6058docbSAXParseFile(const char *filename, const char *encoding, docbSAXHandlerPtr sax,
6059                 void *userData) {
6060    docbDocPtr ret;
6061    docbParserCtxtPtr ctxt;
6062    docbSAXHandlerPtr oldsax = NULL;
6063
6064    ctxt = docbCreateFileParserCtxt(filename, encoding);
6065    if (ctxt == NULL) return(NULL);
6066    if (sax != NULL) {
6067       oldsax = ctxt->sax;
6068        ctxt->sax = sax;
6069        ctxt->userData = userData;
6070    }
6071
6072    docbParseDocument(ctxt);
6073
6074    ret = ctxt->myDoc;
6075    if (sax != NULL) {
6076        ctxt->sax = oldsax;
6077        ctxt->userData = NULL;
6078    }
6079    docbFreeParserCtxt(ctxt);
6080
6081    return(ret);
6082}
6083
6084/**
6085 * docbParseFile :
6086 * @filename:  the filename
6087 * @encoding:  a free form C string describing document encoding, or NULL
6088 *
6089 * parse a Docbook SGML file and build a tree. Automatic support for
6090 * ZLIB/Compress compressed document is provided by default if found
6091 * at compile-time.
6092 *
6093 * Returns the resulting document tree
6094 */
6095
6096docbDocPtr
6097docbParseFile(const char *filename, const char *encoding) {
6098    return(docbSAXParseFile(filename, encoding, NULL, NULL));
6099}
6100
6101#endif /* LIBXML_DOCB_ENABLED */
6102