]> mj.ucw.cz Git - libucw.git/blob - sherlock/xml/parse.c
XML: Implemented a merger of element's contents.
[libucw.git] / sherlock / xml / parse.c
1 /*
2  *      Sherlock Library -- A simple XML parser
3  *
4  *      (c) 2007--2008 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "sherlock/sherlock.h"
13 #include "sherlock/xml/xml.h"
14 #include "sherlock/xml/dtd.h"
15 #include "sherlock/xml/internals.h"
16 #include "lib/fastbuf.h"
17 #include "lib/ff-unicode.h"
18 #include "lib/unicode.h"
19 #include "lib/chartype.h"
20 #include "lib/hashfunc.h"
21
22 #include <setjmp.h>
23
24 /*** Basic parsing ***/
25
26 void NONRET
27 xml_fatal_expected(struct xml_context *ctx, uns c)
28 {
29   if (c >= 32 && c < 128)
30     xml_fatal(ctx, "Expected '%c'", c);
31   else
32     xml_fatal(ctx, "Expected U+%04x", c);
33 }
34
35 void NONRET
36 xml_fatal_expected_white(struct xml_context *ctx)
37 {
38   xml_fatal(ctx, "Expected a white space");
39 }
40
41 void NONRET
42 xml_fatal_expected_quot(struct xml_context *ctx)
43 {
44   xml_fatal(ctx, "Expected a quotation mark");
45 }
46
47 void
48 xml_parse_eq(struct xml_context *ctx)
49 {
50   /* Eq ::= S? '=' S? */
51   xml_parse_white(ctx, 0);
52   xml_parse_char(ctx, '=');
53   xml_parse_white(ctx, 0);
54 }
55
56 /*** Names and nmtokens ***/
57
58 static char *
59 xml_parse_string(struct xml_context *ctx, struct mempool *pool, uns first_cat, uns next_cat, char *err)
60 {
61   char *p = mp_start_noalign(pool, 1);
62   if (unlikely(!(xml_peek_cat(ctx) & first_cat)))
63     xml_fatal(ctx, "%s", err);
64   do
65     {
66       p = mp_spread(pool, p, 5);
67       p = utf8_32_put(p, xml_skip_char(ctx));
68     }
69   while (xml_peek_cat(ctx) & next_cat);
70   *p++ = 0;
71   return mp_end(pool, p);
72 }
73
74 static void
75 xml_skip_string(struct xml_context *ctx, uns first_cat, uns next_cat, char *err)
76 {
77   if (unlikely(!(xml_get_cat(ctx) & first_cat)))
78     xml_fatal(ctx, "%s", err);
79   while (xml_peek_cat(ctx) & next_cat)
80     xml_skip_char(ctx);
81 }
82
83 char *
84 xml_parse_name(struct xml_context *ctx, struct mempool *pool)
85 {
86   /* Name ::= NameStartChar (NameChar)* */
87   return xml_parse_string(ctx, pool, ctx->cat_sname, ctx->cat_name, "Expected a name");
88 }
89
90 void
91 xml_skip_name(struct xml_context *ctx)
92 {
93   xml_skip_string(ctx, ctx->cat_sname, ctx->cat_name, "Expected a name");
94 }
95
96 char *
97 xml_parse_nmtoken(struct xml_context *ctx, struct mempool *pool)
98 {
99   /* Nmtoken ::= (NameChar)+ */
100   return xml_parse_string(ctx, pool, ctx->cat_name, ctx->cat_name, "Expected a nmtoken");
101 }
102
103 /*** Simple literals ***/
104
105 char *
106 xml_parse_system_literal(struct xml_context *ctx, struct mempool *pool)
107 {
108   /* SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") */
109   char *p = mp_start_noalign(pool, 1);
110   uns q = xml_parse_quote(ctx), c;
111   while ((c = xml_get_char(ctx)) != q)
112     {
113       p = mp_spread(pool, p, 5);
114       p = utf8_32_put(p, c);
115     }
116   *p++ = 0;
117   return mp_end(pool, p);
118 }
119
120 char *
121 xml_parse_pubid_literal(struct xml_context *ctx, struct mempool *pool)
122 {
123   /* PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" */
124   char *p = mp_start_noalign(pool, 1);
125   uns q = xml_parse_quote(ctx), c;
126   while ((c = xml_get_char(ctx)) != q)
127     {
128       if (unlikely(!(xml_last_cat(ctx) & XML_CHAR_PUBID)))
129         xml_fatal(ctx, "Expected a pubid character");
130       p = mp_spread(pool, p, 2);
131       *p++ = c;
132     }
133   *p++ = 0;
134   return mp_end(pool, p);
135 }
136
137 /*** Comments ***/
138
139 void
140 xml_push_comment(struct xml_context *ctx)
141 {
142   TRACE(ctx, "push_comment");
143   /* Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
144    * Already parsed: '<!-' */
145   xml_parse_char(ctx, '-');
146   struct xml_node *n = xml_push_dom(ctx, NULL);
147   n->type = XML_NODE_COMMENT;
148   char *p = mp_start_noalign(ctx->pool, 6);
149   while (1)
150     {
151       if (xml_get_char(ctx) == '-')
152         if (xml_get_char(ctx) == '-')
153           break;
154         else
155           *p++ = '-';
156       p = utf8_32_put(p, xml_last_char(ctx));
157       p = mp_spread(ctx->pool, p, 6);
158     }
159   xml_parse_char(ctx, '>');
160   *p = 0;
161   n->len = p - (char *)mp_ptr(ctx->pool);
162   n->text = mp_end(ctx->pool, p + 1);
163   if ((ctx->flags & XML_REPORT_COMMENTS) && ctx->h_comment)
164     ctx->h_comment(ctx);
165 }
166
167 void
168 xml_pop_comment(struct xml_context *ctx)
169 {
170   xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_COMMENTS));
171   xml_dec(ctx);
172   TRACE(ctx, "pop_comment");
173 }
174
175 void
176 xml_skip_comment(struct xml_context *ctx)
177 {
178   TRACE(ctx, "skip_comment");
179   xml_parse_char(ctx, '-');
180   while (xml_get_char(ctx) != '-' || xml_get_char(ctx) != '-');
181   xml_parse_char(ctx, '>');
182   xml_dec(ctx);
183 }
184
185 /*** Processing instructions ***/
186
187 void
188 xml_push_pi(struct xml_context *ctx)
189 {
190   TRACE(ctx, "push_pi");
191   /* Parses a PI to ctx->value and ctx->name:
192    *   PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
193    *   PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
194    * Already parsed: '<?' */
195   struct xml_node *n = xml_push_dom(ctx, NULL);
196   n->type = XML_NODE_PI;
197   n->name = xml_parse_name(ctx, ctx->pool);
198   if (unlikely(!strcasecmp(n->name, "xml")))
199     xml_error(ctx, "Reserved PI target");
200   char *p = mp_start_noalign(ctx->pool, 5);
201   if (!xml_parse_white(ctx, 0))
202     xml_parse_seq(ctx, "?>");
203   else
204     while (1)
205       {
206         if (xml_get_char(ctx) == '?')
207           if (xml_peek_char(ctx) == '>')
208             {
209               xml_skip_char(ctx);
210               break;
211             }
212           else
213             *p++ = '?';
214         else
215           p = utf8_32_put(p, xml_last_char(ctx));
216         p = mp_spread(ctx->pool, p, 5);
217       }
218   *p = 0;
219   n->len = p - (char *)mp_ptr(ctx->pool);
220   n->text = mp_end(ctx->pool, p + 1);
221   if ((ctx->flags & XML_REPORT_PIS) && ctx->h_pi)
222     ctx->h_pi(ctx);
223 }
224
225 void
226 xml_pop_pi(struct xml_context *ctx)
227 {
228   xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_PIS));
229   xml_dec(ctx);
230   TRACE(ctx, "pop_pi");
231 }
232
233 void
234 xml_skip_pi(struct xml_context *ctx)
235 {
236   TRACE(ctx, "skip_pi");
237   if (ctx->flags & XML_VALIDATING)
238     {
239       struct mempool_state state;
240       mp_save(ctx->stack, &state);
241       if (unlikely(!strcasecmp(xml_parse_name(ctx, ctx->stack), "xml")))
242         xml_error(ctx, "Reserved PI target");
243       mp_restore(ctx->stack, &state);
244       if (!xml_parse_white(ctx, 0))
245         {
246           xml_parse_seq(ctx, "?>");
247           xml_dec(ctx);
248           return;
249         }
250     }
251   while (1)
252     if (xml_get_char(ctx) == '?')
253       if (xml_peek_char(ctx) == '>')
254         break;
255   xml_skip_char(ctx);
256   xml_dec(ctx);
257 }
258
259 /*** Character references ***/
260
261 uns
262 xml_parse_char_ref(struct xml_context *ctx)
263 {
264   TRACE(ctx, "parse_char_ref");
265   /* CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
266    * Already parsed: '&#' */
267   uns v = 0;
268   if (xml_get_char(ctx) == 'x')
269     {
270       if (!(xml_get_cat(ctx) & XML_CHAR_XDIGIT))
271         {
272           xml_error(ctx, "Expected a hexadecimal value of character reference");
273           goto recover;
274         }
275       do
276         {
277           v = (v << 4) + Cxvalue(xml_last_char(ctx));
278         }
279       while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_XDIGIT));
280     }
281   else
282     {
283       if (!(xml_last_cat(ctx) & XML_CHAR_DIGIT))
284         {
285           xml_error(ctx, "Expected a numeric value of character reference");
286           goto recover;
287         }
288       do
289         {
290           v = v * 10 + xml_last_char(ctx) - '0';
291         }
292       while (v < 0x110000 && (xml_get_cat(ctx) & XML_CHAR_DIGIT));
293     }
294   uns cat = xml_char_cat(v);
295   if (!(cat & ctx->cat_unrestricted))
296     {
297       xml_error(ctx, "Character reference out of range");
298       goto recover;
299     }
300   if (xml_last_char(ctx) == ';')
301     {
302       xml_dec(ctx);
303       return v;
304     }
305   xml_error(ctx, "Expected ';'");
306 recover:
307   while (xml_last_char(ctx) != ';')
308     xml_get_char(ctx);
309   xml_dec(ctx);
310   return UNI_REPLACEMENT;
311 }
312
313 /*** References to general entities ***/
314
315 static void
316 xml_parse_ref(struct xml_context *ctx)
317 {
318   /* Reference ::= EntityRef | CharRef
319    * EntityRef ::= '&' Name ';'
320    * Already parsed: '&' */
321   struct fastbuf *out = &ctx->chars;
322   if (xml_peek_char(ctx) == '#')
323     {
324       xml_skip_char(ctx);
325       bput_utf8_32(out, xml_parse_char_ref(ctx));
326     }
327   else
328     {
329       TRACE(ctx, "parse_ge_ref");
330       struct mempool_state state;
331       mp_save(ctx->stack, &state);
332       char *name = xml_parse_name(ctx, ctx->stack);
333       xml_parse_char(ctx, ';');
334       struct xml_dtd_entity *ent = xml_dtd_find_entity(ctx, name);
335       if (!ent)
336         {
337           xml_error(ctx, "Unknown entity &%s;", name);
338           bputc(out, '&');
339           bputs(out, name);
340           bputc(out, ';');
341         }
342       else if (ent->flags & XML_DTD_ENTITY_TRIVIAL)
343         {
344           TRACE(ctx, "Trivial entity &%s;", name);
345           bputs(out, ent->text);
346         }
347       else
348         {
349           TRACE(ctx, "Pushed entity &%s;", name);
350           mp_restore(ctx->stack, &state);
351           xml_dec(ctx);
352           xml_push_entity(ctx, ent);
353           return;
354         }
355       mp_restore(ctx->stack, &state);
356       xml_dec(ctx);
357     }
358 }
359
360 /*** Character data ***/
361
362 void
363 xml_spout_chars(struct fastbuf *fb)
364 {
365   if (fb->bptr < fb->bufend)
366     return;
367   struct xml_context *ctx = SKIP_BACK(struct xml_context, chars, fb);
368   struct mempool *pool = ctx->pool;
369   if (fb->bufend != fb->buffer)
370     {
371       TRACE(ctx, "growing chars");
372       uns len = fb->bufend - fb->buffer;
373       uns reported = fb->bstop - fb->buffer;
374       fb->buffer = mp_expand(pool);
375       fb->bufend = fb->buffer + mp_avail(pool);
376       fb->bptr = fb->buffer + len;
377       fb->bstop = fb->buffer + reported;
378     }
379   else
380     {
381       TRACE(ctx, "starting chars");
382       mp_save(pool, &ctx->chars_state);
383       fb->bptr = fb->buffer = fb->bstop = mp_start_noalign(pool, 2);
384       fb->bufend = fb->buffer + mp_avail(pool) - 1;
385     }
386 }
387
388 static inline uns
389 xml_end_chars(struct xml_context *ctx, char **out)
390 {
391   struct fastbuf *fb = &ctx->chars;
392   uns len = fb->bptr - fb->buffer;
393   if (len)
394     {
395       TRACE(ctx, "ending chars");
396       *fb->bptr = 0;
397       *out = mp_end(ctx->pool, fb->bptr + 1);
398       fb->bufend = fb->bstop = fb->bptr = fb->buffer;
399     }
400   return len;
401 }
402
403 static inline uns
404 xml_report_chars(struct xml_context *ctx, char **out)
405 {
406   struct fastbuf *fb = &ctx->chars;
407   uns len = fb->bptr - fb->buffer;
408   if (len)
409     {
410       *fb->bptr = 0;
411       *out = fb->bstop;
412       fb->bstop = fb->bptr;
413     }
414   return len;
415 }
416
417 static inline uns
418 xml_flush_chars(struct xml_context *ctx)
419 {
420   char *text, *rtext;
421   uns len = xml_end_chars(ctx, &text), rlen;
422   if (len)
423     {
424       if (ctx->flags & XML_NO_CHARS)
425         {
426           if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_ignorable)
427             ctx->h_ignorable(ctx, text, len);
428           mp_restore(ctx->pool, &ctx->chars_state);
429           return 0;
430         }
431       if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_block && (rlen = xml_report_chars(ctx, &rtext)))
432         ctx->h_block(ctx, rtext, rlen);
433       if (!(ctx->flags & XML_ALLOC_CHARS) && !(ctx->flags & XML_REPORT_CHARS))
434         {
435           mp_restore(ctx->pool, &ctx->chars_state);
436           return 0;
437         }
438       struct xml_node *n = xml_push_dom(ctx, &ctx->chars_state);
439       n->type = XML_NODE_CHARS;
440       n->text = text;
441       n->len = len;
442       if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_chars)
443         ctx->h_chars(ctx);
444     }
445   return len;
446 }
447
448 static inline void
449 xml_pop_chars(struct xml_context *ctx)
450 {
451   xml_pop_dom(ctx, !(ctx->flags & XML_ALLOC_CHARS));
452   TRACE(ctx, "pop_chars");
453 }
454
455 static inline void
456 xml_append_chars(struct xml_context *ctx)
457 {
458   TRACE(ctx, "append_chars");
459   struct fastbuf *out = &ctx->chars;
460   if (ctx->flags & XML_NO_CHARS)
461     while (xml_get_char(ctx) != '<')
462       if (xml_last_cat(ctx) & XML_CHAR_WHITE)
463         bput_utf8_32(out, xml_last_char(ctx));
464       else
465         {
466           xml_error(ctx, "This element must not contain character data");
467           while (xml_get_char(ctx) != '<');
468           break;
469         }
470   else
471     while (xml_get_char(ctx) != '<')
472       if (xml_last_char(ctx) == '&')
473         {
474           xml_inc(ctx);
475           xml_parse_ref(ctx);
476         }
477       else
478         bput_utf8_32(out, xml_last_char(ctx));
479   xml_unget_char(ctx);
480 }
481
482 /*** CDATA sections ***/
483
484 static void
485 xml_skip_cdata(struct xml_context *ctx)
486 {
487   TRACE(ctx, "skip_cdata");
488   xml_parse_seq(ctx, "CDATA[");
489   while (xml_get_char(ctx) != ']' || xml_get_char(ctx) != ']' || xml_get_char(ctx) != '>');
490   xml_dec(ctx);
491 }
492
493 static void
494 xml_append_cdata(struct xml_context *ctx)
495 {
496   /* CDSect :== '<![CDATA[' (Char* - (Char* ']]>' Char*)) ']]>'
497    * Already parsed: '<![' */
498   TRACE(ctx, "append_cdata");
499   if (ctx->flags & XML_NO_CHARS)
500     {
501       xml_error(ctx, "This element must not contain CDATA");
502       xml_skip_cdata(ctx);
503       return;
504     }
505   xml_parse_seq(ctx, "CDATA[");
506   struct fastbuf *out = &ctx->chars;
507   uns rlen;
508   char *rtext;
509   if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_block && (rlen = xml_report_chars(ctx, &rtext)))
510     ctx->h_block(ctx, rtext, rlen);
511   while (1)
512     {
513       if (xml_get_char(ctx) == ']')
514         {
515           if (xml_get_char(ctx) == ']')
516             if (xml_get_char(ctx) == '>')
517               break;
518             else
519               bputc(out, ']');
520           bputc(out, ']');
521         }
522       bput_utf8_32(out, xml_last_char(ctx));
523     }
524   if ((ctx->flags & XML_REPORT_CHARS) && ctx->h_cdata && (rlen = xml_report_chars(ctx, &rtext)))
525     ctx->h_cdata(ctx, rtext, rlen);
526   xml_dec(ctx);
527 }
528
529 /*** Attribute values ***/
530
531 char *
532 xml_parse_attr_value(struct xml_context *ctx, struct xml_dtd_attr *attr UNUSED)
533 {
534   TRACE(ctx, "parse_attr_value");
535   /* AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'" */
536   /* FIXME: -- check value constrains / normalize leading/trailing WS and repeated WS */
537   struct mempool_state state;
538   uns quote = xml_parse_quote(ctx);
539   mp_save(ctx->stack, &state);
540   struct fastbuf *out = &ctx->chars;
541   struct xml_source *src = ctx->src;
542   while (1)
543     {
544       uns c = xml_get_char(ctx);
545       if (c == '&')
546         {
547           xml_inc(ctx);
548           xml_parse_ref(ctx);
549         }
550       else if (c == quote && src == ctx->src)
551         break;
552       else if (c == '<')
553         xml_error(ctx, "Attribute value must not contain '<'");
554       else if (xml_last_cat(ctx) & XML_CHAR_WHITE)
555         bputc(out, ' ');
556       else
557         bput_utf8_32(out, c);
558     }
559   mp_restore(ctx->stack, &state);
560   char *text;
561   return xml_end_chars(ctx, &text) ? text : "";
562 }
563
564 uns
565 xml_normalize_white(struct xml_context *ctx UNUSED, char *text)
566 {
567   char *s = text, *d = text;
568   while (*s == 0x20)
569     s++;
570   while (1)
571     {
572       while (*s & ~0x20)
573         *d++ = *s++;
574       if (!*s)
575         break;
576       while (*++s == 0x20);
577       *d++ = 0x20;
578     }
579   if (d != text && d[-1] == 0x20)
580     d--;
581   *d = 0;
582   return d - text;
583 }
584
585 /*** Attributes ***/
586
587 struct xml_attrs_table;
588
589 static inline uns
590 xml_attrs_hash(struct xml_attrs_table *t UNUSED, struct xml_node *e, char *n)
591 {
592   return hash_pointer(e) ^ hash_string(n);
593 }
594
595 static inline int
596 xml_attrs_eq(struct xml_attrs_table *t UNUSED, struct xml_node *e1, char *n1, struct xml_node *e2, char *n2)
597 {
598   return (e1 == e2) && !strcmp(n1, n2);
599 }
600
601 static inline void
602 xml_attrs_init_key(struct xml_attrs_table *t UNUSED, struct xml_attr *a, struct xml_node *e, char *name)
603 {
604   a->elem = e;
605   a->name = name;
606   a->val = NULL;
607   a->user = NULL;
608   slist_add_tail(&e->attrs, &a->n);
609 }
610
611 #define HASH_PREFIX(x) xml_attrs_##x
612 #define HASH_NODE struct xml_attr
613 #define HASH_KEY_COMPLEX(x) x elem, x name
614 #define HASH_KEY_DECL struct xml_node *elem, char *name
615 #define HASH_TABLE_DYNAMIC
616 #define HASH_GIVE_EQ
617 #define HASH_GIVE_HASHFN
618 #define HASH_GIVE_INIT_KEY
619 #define HASH_WANT_CLEANUP
620 #define HASH_WANT_REMOVE
621 #define HASH_WANT_LOOKUP
622 #define HASH_WANT_FIND
623 #define HASH_GIVE_ALLOC
624 XML_HASH_GIVE_ALLOC
625 #include "lib/hashtable.h"
626
627 static void
628 xml_parse_attr(struct xml_context *ctx)
629 {
630   TRACE(ctx, "parse_attr");
631   /* Attribute ::= Name Eq AttValue */
632   struct xml_node *e = ctx->node;
633   char *n = xml_parse_name(ctx, ctx->pool);
634   struct xml_attr *a = xml_attrs_lookup(ctx->tab_attrs, e, n);
635   xml_parse_eq(ctx);
636   char *v = xml_parse_attr_value(ctx, NULL);
637   if (a->val)
638     {
639       xml_error(ctx, "Attribute %s is not unique in element <%s>", n, e->name);
640       return;
641     }
642   a->val = v;
643   if (!e->dtd)
644     a->dtd = NULL;
645   else if (!(a->dtd = xml_dtd_find_attr(ctx, e->dtd, a->name)))
646     xml_error(ctx, "Undefined attribute %s in element <%s>", n, e->name);
647   else
648     xml_validate_attr(ctx, a->dtd, a->val);
649 }
650
651 struct xml_attr *
652 xml_attr_find(struct xml_context *ctx, struct xml_node *node, char *name)
653 {
654   return xml_attrs_find(ctx->tab_attrs, node, name);
655 }
656
657 char *
658 xml_attr_value(struct xml_context *ctx, struct xml_node *node, char *name)
659 {
660   struct xml_attr *attr = xml_attrs_find(ctx->tab_attrs, node, name);
661   if (attr)
662     return attr->val;
663   if (!node->dtd)
664     return NULL;
665   struct xml_dtd_attr *dtd = xml_dtd_find_attr(ctx, node->dtd, name);
666   return dtd ? dtd->default_value : NULL;
667 }
668
669 void
670 xml_attrs_table_init(struct xml_context *ctx)
671 {
672   xml_attrs_init(ctx->tab_attrs = xml_hash_new(ctx->pool, sizeof(struct xml_attrs_table)));
673 }
674
675 void
676 xml_attrs_table_cleanup(struct xml_context *ctx)
677 {
678   xml_attrs_cleanup(ctx->tab_attrs);
679 }
680
681 /*** Elements ***/
682
683 static uns
684 xml_validate_element(struct xml_dtd_elem_node *root, struct xml_dtd_elem *elem)
685 {
686   if (root->elem)
687     return elem == root->elem;
688   else
689     SLIST_FOR_EACH(struct xml_dtd_elem_node *, son, root->sons)
690       if (xml_validate_element(son, elem))
691         return 1;
692   return 0;
693 }
694
695 static void
696 xml_push_element(struct xml_context *ctx)
697 {
698   TRACE(ctx, "push_element");
699   /* EmptyElemTag | STag
700    * EmptyElemTag ::= '<' Name (S  Attribute)* S? '/>'
701    * STag ::= '<' Name (S  Attribute)* S? '>'
702    * Already parsed: '<' */
703   struct xml_node *e = xml_push_dom(ctx, NULL);
704   clist_init(&e->sons);
705   e->type = XML_NODE_ELEM;
706   e->name = xml_parse_name(ctx, ctx->pool);
707   slist_init(&e->attrs);
708   if (!e->parent)
709     {
710       ctx->dom = e;
711       if (ctx->doctype && strcmp(e->name, ctx->doctype))
712         xml_error(ctx, "The root element <%s> does not match the document type <%s>", e->name, ctx->doctype);
713     }
714   if (!ctx->dtd)
715     e->dtd = NULL;
716   else if (!(e->dtd = xml_dtd_find_elem(ctx, e->name)))
717     xml_error(ctx, "Undefined element <%s>", e->name);
718   else
719     {
720       struct xml_dtd_elem *dtd = e->dtd, *parent_dtd = e->parent ? e->parent->dtd : NULL;
721       if (dtd->type == XML_DTD_ELEM_MIXED)
722         ctx->flags &= ~XML_NO_CHARS;
723       else
724         ctx->flags |= XML_NO_CHARS;
725       if (parent_dtd)
726         if (parent_dtd->type == XML_DTD_ELEM_EMPTY)
727           xml_error(ctx, "Empty element must not contain children");
728         else if (parent_dtd->type != XML_DTD_ELEM_ANY)
729           {
730             // FIXME: validate regular expressions
731             if (!xml_validate_element(parent_dtd->node, dtd))
732               xml_error(ctx, "Unexpected element <%s>", e->name);
733           }
734     }
735   while (1)
736     {
737       uns white = xml_parse_white(ctx, 0);
738       uns c = xml_get_char(ctx);
739       if (c == '/')
740         {
741           xml_parse_char(ctx, '>');
742           ctx->flags |= XML_EMPTY_ELEM_TAG;
743           break;
744         }
745       else if (c == '>')
746         break;
747       else if (!white)
748         xml_fatal_expected_white(ctx);
749       xml_unget_char(ctx);
750       xml_parse_attr(ctx);
751     }
752   if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_stag)
753     ctx->h_stag(ctx);
754 }
755
756 static void
757 xml_pop_element(struct xml_context *ctx)
758 {
759   TRACE(ctx, "pop_element");
760   if ((ctx->flags & XML_REPORT_TAGS) && ctx->h_etag)
761     ctx->h_etag(ctx);
762   struct xml_node *e = ctx->node;
763   uns free = !(ctx->flags & XML_ALLOC_TAGS);
764   if (free)
765     {
766       if (!e->parent)
767         ctx->dom = NULL;
768       /* Restore hash table of attributes */
769       SLIST_FOR_EACH(struct xml_attr *, a, e->attrs)
770         xml_attrs_remove(ctx->tab_attrs, a);
771       struct xml_node *n;
772       while (n = clist_head(&e->sons))
773         {
774           if (n->type == XML_NODE_ELEM)
775             {
776               SLIST_FOR_EACH(struct xml_attr *, a, n->attrs)
777                 xml_attrs_remove(ctx->tab_attrs, a);
778               clist_insert_list_after(&n->sons, &n->n);
779             }
780           clist_remove(&n->n);
781         }
782     }
783   xml_pop_dom(ctx, free);
784   xml_dec(ctx);
785 }
786
787 static void
788 xml_parse_etag(struct xml_context *ctx)
789 {
790  /* ETag ::= '</' Name S? '>'
791   * Already parsed: '<' */
792   struct xml_node *e = ctx->node;
793   ASSERT(e);
794   char *n = e->name;
795   while (*n)
796     {
797       uns c;
798       n = utf8_32_get(n, &c);
799       if (xml_get_char(ctx) != c)
800         goto recover;
801     }
802   xml_parse_white(ctx, 0);
803   if (xml_get_char(ctx) != '>')
804     {
805 recover:
806       xml_error(ctx, "Invalid ETag, expected </%s>", e->name);
807       while (xml_get_char(ctx) != '>');
808     }
809   xml_dec(ctx);
810 }
811
812 /*** Document type declaration ***/
813
814 static void
815 xml_parse_doctype_decl(struct xml_context *ctx)
816 {
817   TRACE(ctx, "parse_doctype_decl");
818   /* doctypedecl ::= '<!DOCTYPE' S  Name (S  ExternalID)? S? ('[' intSubset ']' S?)? '>'
819    * Already parsed: '<!'
820    * Terminated before '[' or '>' */
821   if (ctx->doctype)
822     xml_fatal(ctx, "Multiple document types not allowed");
823   xml_parse_seq(ctx, "DOCTYPE");
824   xml_parse_white(ctx, 1);
825   ctx->doctype = xml_parse_name(ctx, ctx->pool);
826   TRACE(ctx, "doctype=%s", ctx->doctype);
827   uns c;
828   if (xml_parse_white(ctx, 0) && ((c = xml_peek_char(ctx)) == 'S' || c == 'P'))
829     {
830       if (c == 'S')
831         {
832           xml_parse_seq(ctx, "SYSTEM");
833           xml_parse_white(ctx, 1);
834           ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
835         }
836       else
837         {
838           xml_parse_seq(ctx, "PUBLIC");
839           xml_parse_white(ctx, 1);
840           ctx->public_id = xml_parse_pubid_literal(ctx, ctx->pool);
841           xml_parse_white(ctx, 1);
842           ctx->system_id = xml_parse_system_literal(ctx, ctx->pool);
843         }
844       xml_parse_white(ctx, 0);
845       ctx->flags |= XML_HAS_EXTERNAL_SUBSET;
846     }
847   if (xml_peek_char(ctx) == '[')
848     {
849       ctx->flags |= XML_HAS_INTERNAL_SUBSET;
850       xml_skip_char(ctx);
851       xml_inc(ctx);
852     }
853   if (ctx->h_doctype_decl)
854     ctx->h_doctype_decl(ctx);
855 }
856
857
858
859 ///////////////////////////////////////////////////////////////////////////////////////////////////////////
860
861 /* DTD: Internal subset */
862
863 static void
864 xml_parse_subset(struct xml_context *ctx, uns external)
865 {
866   // FIXME:
867   // -- comments/pi have no parent
868   // -- conditional sections in external subset
869   // -- check corectness of parameter entities
870
871   /* '[' intSubset ']'
872    * intSubset :== (markupdecl | DeclSep)
873    * Already parsed: '['
874    *
875    * extSubsetDecl ::= ( markupdecl | conditionalSect | DeclSep)*
876    */
877   while (1)
878     {
879       xml_parse_white(ctx, 0);
880       uns c = xml_get_char(ctx);
881       xml_inc(ctx);
882       if (c == '<')
883         if ((c = xml_get_char(ctx)) == '!')
884           switch (c = xml_get_char(ctx))
885             {
886               case '-':
887                 xml_push_comment(ctx);
888                 xml_pop_comment(ctx);
889                 break;
890               case 'N':
891                 xml_parse_seq(ctx, "OTATION");
892                 xml_parse_notation_decl(ctx);
893                 break;
894               case 'E':
895                 if ((c = xml_get_char(ctx)) == 'N')
896                   {
897                     xml_parse_seq(ctx, "TITY");
898                     xml_parse_entity_decl(ctx);
899                   }
900                 else if (c == 'L')
901                   {
902                     xml_parse_seq(ctx, "EMENT");
903                     xml_parse_element_decl(ctx);
904                   }
905                 else
906                   goto invalid_markup;
907                 break;
908               case 'A':
909                 xml_parse_seq(ctx, "TTLIST");
910                 xml_parse_attr_list_decl(ctx);
911                 break;
912               default:
913                 goto invalid_markup;
914             }
915         else if (c == '?')
916           {
917             xml_push_pi(ctx);
918             xml_pop_pi(ctx);
919           }
920         else
921           goto invalid_markup;
922       else if (c == '%')
923         xml_parse_pe_ref(ctx);
924       else if (c == ']' && !external)
925         {
926           break;
927         }
928       else if (c == '>' && external)
929         {
930           break;
931         }
932       else
933         goto invalid_markup;
934     }
935   xml_dec(ctx);
936   return;
937 invalid_markup: ;
938   xml_fatal(ctx, "Invalid markup in the %s subset", external ? "external" : "internal");
939 }
940
941 /*** The State Machine ***/
942
943 uns
944 xml_next(struct xml_context *ctx)
945 {
946   /* A nasty state machine */
947
948 #define PULL(x) do { if (ctx->pull & XML_PULL_##x) return ctx->state = XML_STATE_##x; case XML_STATE_##x: ; } while (0)
949 #define PULL_STATE(x, s) do { if (ctx->pull & XML_PULL_##x) return ctx->state = XML_STATE_##s, XML_STATE_##x; case XML_STATE_##s: ; } while (0)
950
951   TRACE(ctx, "xml_next (state=%u)", ctx->state);
952   jmp_buf throw_buf;
953   ctx->throw_buf = &throw_buf;
954   if (setjmp(throw_buf))
955     {
956 error:
957       if (ctx->err_code == XML_ERR_EOF && ctx->h_fatal)
958         ctx->h_fatal(ctx);
959       TRACE(ctx, "raised fatal error");
960       return ctx->state = XML_STATE_EOF;
961     }
962   uns c;
963   switch (ctx->state)
964     {
965       case XML_STATE_START:
966         TRACE(ctx, "entering prolog");
967         ctx->flags |= XML_SRC_DOCUMENT | XML_SRC_EXPECTED_DECL;
968         if (ctx->h_document_start)
969           ctx->h_document_start(ctx);
970         /* XMLDecl */
971         xml_refill(ctx);
972         if (ctx->h_xml_decl)
973           ctx->h_xml_decl(ctx);
974         PULL(XML_DECL);
975
976         /* Misc* (doctypedecl Misc*)? */
977         while (1)
978           {
979             xml_parse_white(ctx, 0);
980             xml_parse_char(ctx, '<');
981             xml_inc(ctx);
982             if ((c = xml_get_char(ctx)) == '?')
983               /* Processing intruction */
984               if (!(ctx->flags & XML_REPORT_PIS))
985                 xml_skip_pi(ctx);
986               else
987                 {
988                   xml_push_pi(ctx);
989                   PULL_STATE(PI, PROLOG_PI);
990                   xml_pop_pi(ctx);
991                 }
992             else if (c != '!')
993               {
994                 /* Found the root tag */
995                 xml_unget_char(ctx);
996                 goto first_tag;
997               }
998             else if (xml_get_char(ctx) == '-')
999               if (!(ctx->flags & XML_REPORT_COMMENTS))
1000                 xml_skip_comment(ctx);
1001               else
1002                 {
1003                   xml_push_comment(ctx);
1004                   PULL_STATE(COMMENT, PROLOG_COMMENT);
1005                   xml_pop_comment(ctx);
1006                 }
1007             else
1008               {
1009                 /* DocTypeDecl */
1010                 xml_unget_char(ctx);
1011                 xml_parse_doctype_decl(ctx);
1012                 PULL(DOCTYPE_DECL);
1013                 if (ctx->flags & XML_HAS_DTD)
1014                   if (ctx->flags & XML_PARSE_DTD)
1015                     {
1016                       xml_dtd_init(ctx);
1017                       if (ctx->h_dtd_start)
1018                         ctx->h_dtd_start(ctx);
1019                       if (ctx->flags & XML_HAS_INTERNAL_SUBSET)
1020                         {
1021                           xml_parse_subset(ctx, 0);
1022                           xml_dec(ctx);
1023                         }
1024                       if (ctx->flags & XML_HAS_EXTERNAL_SUBSET)
1025                         {
1026                           struct xml_dtd_entity ent = {
1027                             .system_id = ctx->system_id,
1028                             .public_id = ctx->public_id,
1029                           };
1030                           xml_parse_white(ctx, 0);
1031                           xml_parse_char(ctx, '>');
1032                           xml_unget_char(ctx);
1033                           ASSERT(ctx->h_resolve_entity);
1034                           ctx->h_resolve_entity(ctx, &ent);
1035                           ctx->flags |= XML_SRC_EXPECTED_DECL;
1036                           xml_parse_subset(ctx, 1);
1037                           xml_unget_char(ctx);;
1038                         }
1039                       if (ctx->h_dtd_end)
1040                         ctx->h_dtd_end(ctx);
1041                     }
1042                   else if (ctx->flags & XML_HAS_INTERNAL_SUBSET)
1043                     xml_skip_internal_subset(ctx);
1044                 xml_parse_white(ctx, 0);
1045                 xml_parse_char(ctx, '>');
1046                 xml_dec(ctx);
1047               }
1048           }
1049
1050       case XML_STATE_CHARS:
1051
1052         while (1)
1053           {
1054             if (xml_peek_char(ctx) != '<')
1055               {
1056                 /* CharData */
1057                 xml_append_chars(ctx);
1058                 continue;
1059               }
1060             else
1061               xml_skip_char(ctx);
1062             xml_inc(ctx);
1063 first_tag:
1064
1065             if ((c = xml_get_char(ctx)) == '?')
1066               {
1067                 /* PI */
1068                 if (!(ctx->flags & (XML_REPORT_PIS | XML_ALLOC_PIS)))
1069                   xml_skip_pi(ctx);
1070                 else
1071                   {
1072                     if (xml_flush_chars(ctx))
1073                       {
1074                         PULL_STATE(CHARS, CHARS_BEFORE_PI);
1075                         xml_pop_chars(ctx);
1076                       }
1077                     xml_push_pi(ctx);
1078                     PULL(PI);
1079                     xml_pop_pi(ctx);
1080                   }
1081               }
1082
1083             else if (c == '!')
1084               if ((c = xml_get_char(ctx)) == '-')
1085                 {
1086                   /* Comment */
1087                   if (!(ctx->flags & (XML_REPORT_COMMENTS | XML_ALLOC_COMMENTS)))
1088                     xml_skip_comment(ctx);
1089                   else
1090                     {
1091                       if (xml_flush_chars(ctx))
1092                         {
1093                           PULL_STATE(CHARS, CHARS_BEFORE_COMMENT);
1094                           xml_pop_chars(ctx);
1095                         }
1096                       xml_push_comment(ctx);
1097                       PULL(COMMENT);
1098                       xml_pop_comment(ctx);
1099                     }
1100                 }
1101               else if (c == '[')
1102                 {
1103                   /* CDATA */
1104                   xml_append_cdata(ctx);
1105                 }
1106               else
1107                 xml_fatal(ctx, "Unexpected character after '<!'");
1108
1109             else if (c != '/')
1110               {
1111                 /* STag | EmptyElemTag */
1112                 xml_unget_char(ctx);
1113                 if (xml_flush_chars(ctx))
1114                   {
1115                     PULL_STATE(CHARS, CHARS_BEFORE_STAG);
1116                     xml_pop_chars(ctx);
1117                   }
1118
1119                 xml_push_element(ctx);
1120                 PULL(STAG);
1121                 if (ctx->flags & XML_EMPTY_ELEM_TAG)
1122                   goto pop_element;
1123               }
1124
1125             else
1126               {
1127                 /* ETag */
1128                 if (xml_flush_chars(ctx))
1129                   {
1130                     PULL_STATE(CHARS, CHARS_BEFORE_ETAG);
1131                     xml_pop_chars(ctx);
1132                   }
1133
1134                 xml_parse_etag(ctx);
1135 pop_element:
1136                 PULL(ETAG);
1137                 xml_pop_element(ctx);
1138                 if (!ctx->node)
1139                   goto epilog;
1140               }
1141           }
1142
1143 epilog:
1144         /* Misc* */
1145         TRACE(ctx, "entering epilog");
1146         while (1)
1147           {
1148             /* Epilog whitespace is the only place, where a valid document can reach EOF */
1149             if (setjmp(throw_buf))
1150               if (ctx->err_code == XML_ERR_EOF)
1151                 {
1152                   TRACE(ctx, "reached EOF");
1153                   ctx->state = XML_STATE_EOF;
1154                   if (ctx->h_document_end)
1155                     ctx->h_document_end(ctx);
1156       case XML_STATE_EOF:
1157                   ctx->err_code = 0;
1158                   ctx->err_msg = NULL;
1159                   return XML_STATE_EOF;
1160                 }
1161               else
1162                 goto error;
1163             xml_parse_white(ctx, 0);
1164             if (setjmp(throw_buf))
1165               goto error;
1166
1167             /* Misc */
1168             xml_parse_char(ctx, '<');
1169             xml_inc(ctx);
1170             if ((c = xml_get_char(ctx)) == '?')
1171               /* Processing instruction */
1172               if (!(ctx->flags & XML_REPORT_PIS))
1173                 xml_skip_pi(ctx);
1174               else
1175                 {
1176                   xml_push_pi(ctx);
1177                   PULL_STATE(PI, EPILOG_PI);
1178                   xml_pop_pi(ctx);
1179                 }
1180             else if (c == '!')
1181               {
1182                 xml_parse_char(ctx, '-');
1183                 /* Comment */
1184                 if (!(ctx->flags & XML_REPORT_COMMENTS))
1185                   xml_skip_comment(ctx);
1186                 else
1187                   {
1188                     xml_push_comment(ctx);
1189                     PULL_STATE(COMMENT, EPILOG_COMMENT);
1190                     xml_pop_comment(ctx);
1191                   }
1192               }
1193             else
1194               xml_fatal(ctx, "Syntax error in the epilog");
1195           }
1196
1197     }
1198   ASSERT(0);
1199 }
1200
1201 uns
1202 xml_next_state(struct xml_context *ctx, uns pull)
1203 {
1204   uns saved = ctx->pull;
1205   ctx->pull = pull;
1206   uns res = xml_next(ctx);
1207   ctx->pull = saved;
1208   return res;
1209 }
1210
1211 uns
1212 xml_skip_element(struct xml_context *ctx)
1213 {
1214   ASSERT(ctx->state == XML_STATE_STAG);
1215   struct xml_node *node = ctx->node;
1216   uns saved = ctx->pull, res;
1217   ctx->pull = XML_PULL_ETAG;
1218   while ((res = xml_next(ctx)) && ctx->node != node);
1219   ctx->pull = saved;
1220   return res;
1221 }
1222
1223 uns
1224 xml_parse(struct xml_context *ctx)
1225 {
1226   /* This cycle should run only once unless the user overrides the value of ctx->pull in a SAX handler */
1227   do
1228     {
1229       ctx->pull = 0;
1230     }
1231   while (xml_next(ctx));
1232   return ctx->err_code;
1233 }
1234
1235 char *
1236 xml_merge_chars(struct xml_context *ctx UNUSED, struct xml_node *node, struct mempool *pool)
1237 {
1238   ASSERT(node->type == XML_NODE_ELEM);
1239   char *p = mp_start_noalign(pool, 1);
1240   XML_NODE_FOR_EACH(son, node)
1241     if (son->type == XML_NODE_CHARS)
1242       {
1243         p = mp_spread(pool, p, son->len + 1);
1244         memcpy(p, son->text, son->len);
1245       }
1246   *p++ = 0;
1247   return mp_end(pool, p);
1248 }
1249
1250 static char *
1251 xml_append_dom_chars(char *p, struct mempool *pool, struct xml_node *node)
1252 {
1253   XML_NODE_FOR_EACH(son, node)
1254     if (son->type == XML_NODE_CHARS)
1255       {
1256         p = mp_spread(pool, p, son->len + 1);
1257         memcpy(p, son->text, son->len);
1258       }
1259     else if (son->type == XML_NODE_ELEM)
1260       p = xml_append_dom_chars(p, pool, son);
1261   return p;
1262 }
1263
1264 char *
1265 xml_merge_dom_chars(struct xml_context *ctx UNUSED, struct xml_node *node, struct mempool *pool)
1266 {
1267   ASSERT(node->type == XML_NODE_ELEM);
1268   char *p = mp_start_noalign(pool, 1);
1269   p = xml_append_dom_chars(p, pool, node);
1270   *p++ = 0;
1271   return mp_end(pool, p);
1272 }