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