]> mj.ucw.cz Git - libucw.git/blob - sherlock/xml/dtd.c
XML: Several fixes, slightly changed the iface.
[libucw.git] / sherlock / xml / dtd.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/common.h"
16 #include "lib/fastbuf.h"
17 #include "lib/ff-unicode.h"
18 #include "lib/unicode.h"
19
20 /* Notations */
21
22 #define HASH_PREFIX(x) xml_dtd_notns_##x
23 #define HASH_NODE struct xml_dtd_notn
24 #define HASH_KEY_STRING name
25 #define HASH_ZERO_FILL
26 #define HASH_TABLE_DYNAMIC
27 #define HASH_WANT_LOOKUP
28 #define HASH_WANT_FIND
29 #define HASH_GIVE_ALLOC
30 #define HASH_TABLE_ALLOC
31 XML_HASH_GIVE_ALLOC
32 #include "lib/hashtable.h"
33
34 struct xml_dtd_notn *
35 xml_dtd_find_notn(struct xml_context *ctx, char *name)
36 {
37   struct xml_dtd *dtd = ctx->dtd;
38   struct xml_dtd_notn *notn = xml_dtd_notns_find(dtd->tab_notns, name);
39   return !notn ? NULL : (notn->flags & XML_DTD_NOTN_DECLARED) ? notn : NULL;
40 }
41
42 /* General entities */
43
44 #define HASH_PREFIX(x) xml_dtd_ents_##x
45 #define HASH_NODE struct xml_dtd_entity
46 #define HASH_KEY_STRING name
47 #define HASH_ZERO_FILL
48 #define HASH_TABLE_DYNAMIC
49 #define HASH_WANT_FIND
50 #define HASH_WANT_LOOKUP
51 #define HASH_GIVE_ALLOC
52 #define HASH_TABLE_ALLOC
53 XML_HASH_GIVE_ALLOC
54 #include "lib/hashtable.h"
55
56 static struct xml_dtd_entity *
57 xml_dtd_declare_trivial_entity(struct xml_context *ctx, char *name, char *text)
58 {
59   struct xml_dtd *dtd = ctx->dtd;
60   struct xml_dtd_entity *ent = xml_dtd_ents_lookup(dtd->tab_ents, name);
61   if (ent->flags & XML_DTD_ENTITY_DECLARED)
62     {
63       xml_warn(ctx, "Entity &%s; already declared", name);
64       return NULL;
65     }
66   slist_add_tail(&dtd->ents, &ent->n);
67   ent->flags = XML_DTD_ENTITY_DECLARED | XML_DTD_ENTITY_TRIVIAL;
68   ent->text = text;
69   return ent;
70 }
71
72 static void
73 xml_dtd_declare_default_entities(struct xml_context *ctx)
74 {
75   xml_dtd_declare_trivial_entity(ctx, "lt", "<");
76   xml_dtd_declare_trivial_entity(ctx, "gt", ">");
77   xml_dtd_declare_trivial_entity(ctx, "amp", "&");
78   xml_dtd_declare_trivial_entity(ctx, "apos", "'");
79   xml_dtd_declare_trivial_entity(ctx, "quot", "\"");
80 }
81
82 struct xml_dtd_entity *
83 xml_def_find_entity(struct xml_context *ctx UNUSED, char *name)
84 {
85 #define ENT(n, t) ent_##n = { .name = #n, .text = t, .flags = XML_DTD_ENTITY_DECLARED | XML_DTD_ENTITY_TRIVIAL }
86   static struct xml_dtd_entity ENT(lt, "<"), ENT(gt, ">"), ENT(amp, "&"), ENT(apos, "'"), ENT(quot, "\"");
87 #undef ENT
88   switch (name[0])
89     {
90       case 'l':
91         if (!strcmp(name, "lt"))
92           return &ent_lt;
93         break;
94       case 'g':
95         if (!strcmp(name, "gt"))
96           return &ent_gt;
97         break;
98       case 'a':
99         if (!strcmp(name, "amp"))
100           return &ent_amp;
101         if (!strcmp(name, "apos"))
102           return &ent_apos;
103         break;
104       case 'q':
105         if (!strcmp(name, "quot"))
106           return &ent_quot;
107         break;
108     }
109   return NULL;
110 }
111
112 struct xml_dtd_entity *
113 xml_dtd_find_entity(struct xml_context *ctx, char *name)
114 {
115   struct xml_dtd *dtd = ctx->dtd;
116   if (ctx->h_find_entity)
117     return ctx->h_find_entity(ctx, name);
118   else if (dtd)
119     {
120       struct xml_dtd_entity *ent = xml_dtd_ents_find(dtd->tab_ents, name);
121       return !ent ? NULL : (ent->flags & XML_DTD_ENTITY_DECLARED) ? ent : NULL;
122     }
123   else
124     return xml_def_find_entity(ctx, name);
125 }
126
127 /* Parameter entities */
128
129 static struct xml_dtd_entity *
130 xml_dtd_find_pentity(struct xml_context *ctx, char *name)
131 {
132   struct xml_dtd *dtd = ctx->dtd;
133   struct xml_dtd_entity *ent = xml_dtd_ents_find(dtd->tab_pents, name);
134   return !ent ? NULL : (ent->flags & XML_DTD_ENTITY_DECLARED) ? ent : NULL;
135 }
136
137 /* Elements */
138
139 #define HASH_PREFIX(x) xml_dtd_elems_##x
140 #define HASH_NODE struct xml_dtd_elem
141 #define HASH_KEY_STRING name
142 #define HASH_TABLE_DYNAMIC
143 #define HASH_ZERO_FILL
144 #define HASH_WANT_FIND
145 #define HASH_WANT_LOOKUP
146 #define HASH_GIVE_ALLOC
147 #define HASH_TABLE_ALLOC
148 XML_HASH_GIVE_ALLOC
149 #include "lib/hashtable.h"
150
151 struct xml_dtd_elem *
152 xml_dtd_find_elem(struct xml_context *ctx, char *name)
153 {
154   return ctx->dtd ? xml_dtd_elems_find(ctx->dtd->tab_elems, name) : NULL;
155 }
156
157 /* Element sons */
158
159 struct xml_dtd_enodes_table;
160
161 static inline uns
162 xml_dtd_enodes_hash(struct xml_dtd_enodes_table *tab UNUSED, struct xml_dtd_elem_node *parent, struct xml_dtd_elem *elem)
163 {
164   return hash_pointer(parent) ^ hash_pointer(elem);
165 }
166
167 static inline int
168 xml_dtd_enodes_eq(struct xml_dtd_enodes_table *tab UNUSED, struct xml_dtd_elem_node *parent1, struct xml_dtd_elem *elem1, struct xml_dtd_elem_node *parent2, struct xml_dtd_elem *elem2)
169 {
170   return (parent1 == parent2) && (elem1 == elem2);
171 }
172
173 static inline void
174 xml_dtd_enodes_init_key(struct xml_dtd_enodes_table *tab UNUSED, struct xml_dtd_elem_node *node, struct xml_dtd_elem_node *parent, struct xml_dtd_elem *elem)
175 {
176   node->parent = parent;
177   node->elem = elem;
178 }
179
180 #define HASH_PREFIX(x) xml_dtd_enodes_##x
181 #define HASH_NODE struct xml_dtd_elem_node
182 #define HASH_KEY_COMPLEX(x) x parent, x elem
183 #define HASH_KEY_DECL struct xml_dtd_elem_node *parent, struct xml_dtd_elem *elem
184 #define HASH_GIVE_HASHFN
185 #define HASH_GIVE_EQ
186 #define HASH_GIVE_INIT_KEY
187 #define HASH_TABLE_DYNAMIC
188 #define HASH_ZERO_FILL
189 #define HASH_WANT_FIND
190 #define HASH_WANT_NEW
191 #define HASH_GIVE_ALLOC
192 #define HASH_TABLE_ALLOC
193 XML_HASH_GIVE_ALLOC
194 #include "lib/hashtable.h"
195
196 /* Element attributes */
197
198 struct xml_dtd_attrs_table;
199
200 static inline uns
201 xml_dtd_attrs_hash(struct xml_dtd_attrs_table *tab UNUSED, struct xml_dtd_elem *elem, char *name)
202 {
203   return hash_pointer(elem) ^ hash_string(name);
204 }
205
206 static inline int
207 xml_dtd_attrs_eq(struct xml_dtd_attrs_table *tab UNUSED, struct xml_dtd_elem *elem1, char *name1, struct xml_dtd_elem *elem2, char *name2)
208 {
209   return (elem1 == elem2) && !strcmp(name1, name2);
210 }
211
212 static inline void
213 xml_dtd_attrs_init_key(struct xml_dtd_attrs_table *tab UNUSED, struct xml_dtd_attr *attr, struct xml_dtd_elem *elem, char *name)
214 {
215   attr->elem = elem;
216   attr->name = name;
217 }
218
219 #define HASH_PREFIX(x) xml_dtd_attrs_##x
220 #define HASH_NODE struct xml_dtd_attr
221 #define HASH_ZERO_FILL
222 #define HASH_TABLE_DYNAMIC
223 #define HASH_KEY_COMPLEX(x) x elem, x name
224 #define HASH_KEY_DECL struct xml_dtd_elem *elem, char *name
225 #define HASH_GIVE_HASHFN
226 #define HASH_GIVE_EQ
227 #define HASH_GIVE_INIT_KEY
228 #define HASH_WANT_FIND
229 #define HASH_WANT_NEW
230 #define HASH_GIVE_ALLOC
231 #define HASH_TABLE_ALLOC
232 XML_HASH_GIVE_ALLOC
233 #include "lib/hashtable.h"
234
235 struct xml_dtd_attr *
236 xml_dtd_find_attr(struct xml_context *ctx, struct xml_dtd_elem *elem, char *name)
237 {
238   return ctx->dtd ? xml_dtd_attrs_find(ctx->dtd->tab_attrs, elem, name) : NULL;
239 }
240
241 /* Enumerated attribute values */
242
243 struct xml_dtd_evals_table;
244
245 static inline uns
246 xml_dtd_evals_hash(struct xml_dtd_evals_table *tab UNUSED, struct xml_dtd_attr *attr, char *val)
247 {
248   return hash_pointer(attr) ^ hash_string(val);
249 }
250
251 static inline int
252 xml_dtd_evals_eq(struct xml_dtd_evals_table *tab UNUSED, struct xml_dtd_attr *attr1, char *val1, struct xml_dtd_attr *attr2, char *val2)
253 {
254   return (attr1 == attr2) && !strcmp(val1, val2);
255 }
256
257 static inline void
258 xml_dtd_evals_init_key(struct xml_dtd_evals_table *tab UNUSED, struct xml_dtd_eval *eval, struct xml_dtd_attr *attr, char *val)
259 {
260   eval->attr = attr;
261   eval->val = val;
262 }
263
264 #define HASH_PREFIX(x) xml_dtd_evals_##x
265 #define HASH_NODE struct xml_dtd_eval
266 #define HASH_TABLE_DYNAMIC
267 #define HASH_KEY_COMPLEX(x) x attr, x val
268 #define HASH_KEY_DECL struct xml_dtd_attr *attr, char *val
269 #define HASH_GIVE_HASHFN
270 #define HASH_GIVE_EQ
271 #define HASH_GIVE_INIT_KEY
272 #define HASH_WANT_FIND
273 #define HASH_WANT_NEW
274 #define HASH_GIVE_ALLOC
275 #define HASH_TABLE_ALLOC
276 XML_HASH_GIVE_ALLOC
277 #include "lib/hashtable.h"
278
279 /* Enumerated attribute notations */
280
281 struct xml_dtd_enotns_table;
282
283 static inline uns
284 xml_dtd_enotns_hash(struct xml_dtd_enotns_table *tab UNUSED, struct xml_dtd_attr *attr, struct xml_dtd_notn *notn)
285 {
286   return hash_pointer(attr) ^ hash_pointer(notn);
287 }
288
289 static inline int
290 xml_dtd_enotns_eq(struct xml_dtd_enotns_table *tab UNUSED, struct xml_dtd_attr *attr1, struct xml_dtd_notn *notn1, struct xml_dtd_attr *attr2, struct xml_dtd_notn *notn2)
291 {
292   return (attr1 == attr2) && (notn1 == notn2);
293 }
294
295 static inline void
296 xml_dtd_enotns_init_key(struct xml_dtd_enotns_table *tab UNUSED, struct xml_dtd_enotn *enotn, struct xml_dtd_attr *attr, struct xml_dtd_notn *notn)
297 {
298   enotn->attr = attr;
299   enotn->notn = notn;
300 }
301
302 #define HASH_PREFIX(x) xml_dtd_enotns_##x
303 #define HASH_NODE struct xml_dtd_enotn
304 #define HASH_TABLE_DYNAMIC
305 #define HASH_KEY_COMPLEX(x) x attr, x notn
306 #define HASH_KEY_DECL struct xml_dtd_attr *attr, struct xml_dtd_notn *notn
307 #define HASH_GIVE_HASHFN
308 #define HASH_GIVE_EQ
309 #define HASH_GIVE_INIT_KEY
310 #define HASH_WANT_FIND
311 #define HASH_WANT_NEW
312 #define HASH_GIVE_ALLOC
313 #define HASH_TABLE_ALLOC
314 XML_HASH_GIVE_ALLOC
315 #include "lib/hashtable.h"
316
317 /* DTD initialization/cleanup */
318
319 void
320 xml_dtd_init(struct xml_context *ctx)
321 {
322   if (ctx->dtd)
323     return;
324   struct mempool *pool = mp_new(4096);
325   struct xml_dtd *dtd = ctx->dtd = mp_alloc_zero(pool, sizeof(*ctx->dtd));
326   dtd->pool = pool;
327   xml_dtd_ents_init(dtd->tab_ents = xml_hash_new(pool, sizeof(struct xml_dtd_ents_table)));
328   xml_dtd_ents_init(dtd->tab_pents = xml_hash_new(pool, sizeof(struct xml_dtd_ents_table)));
329   xml_dtd_notns_init(dtd->tab_notns = xml_hash_new(pool, sizeof(struct xml_dtd_notns_table)));
330   xml_dtd_elems_init(dtd->tab_elems = xml_hash_new(pool, sizeof(struct xml_dtd_elems_table)));
331   xml_dtd_enodes_init(dtd->tab_enodes = xml_hash_new(pool, sizeof(struct xml_dtd_enodes_table)));
332   xml_dtd_attrs_init(dtd->tab_attrs = xml_hash_new(pool, sizeof(struct xml_dtd_attrs_table)));
333   xml_dtd_evals_init(dtd->tab_evals = xml_hash_new(pool, sizeof(struct xml_dtd_evals_table)));
334   xml_dtd_enotns_init(dtd->tab_enotns = xml_hash_new(pool, sizeof(struct xml_dtd_enotns_table)));
335   xml_dtd_declare_default_entities(ctx);
336 }
337
338 void
339 xml_dtd_cleanup(struct xml_context *ctx)
340 {
341   if (!ctx->dtd)
342     return;
343   mp_delete(ctx->dtd->pool);
344   ctx->dtd = NULL;
345 }
346
347 void
348 xml_dtd_finish(struct xml_context *ctx)
349 {
350   if (!ctx->dtd)
351     return;
352   // FIXME: validity checks
353 }
354
355 /*** Parsing functions ***/
356
357 /* References to parameter entities */
358
359 void
360 xml_parse_pe_ref(struct xml_context *ctx)
361 {
362   /* PEReference ::= '%' Name ';'
363    * Already parsed: '%' */
364   struct mempool_state state;
365   mp_save(ctx->stack, &state);
366   char *name = xml_parse_name(ctx, ctx->stack);
367   xml_parse_char(ctx, ';');
368   struct xml_dtd_entity *ent = xml_dtd_find_pentity(ctx, name);
369   if (!ent)
370     xml_error(ctx, "Unknown entity %%%s;", name);
371   else
372     {
373       TRACE(ctx, "Pushed entity %%%s;", name);
374       mp_restore(ctx->stack, &state);
375       xml_dec(ctx);
376       xml_push_entity(ctx, ent);
377       return;
378     }
379   mp_restore(ctx->stack, &state);
380   xml_dec(ctx);
381 }
382
383 static void
384 xml_parse_dtd_pe(struct xml_context *ctx)
385 {
386   do
387     {
388       xml_skip_char(ctx);
389       xml_inc(ctx);
390       while (xml_peek_cat(ctx) & XML_CHAR_WHITE)
391         xml_skip_char(ctx);
392       xml_parse_pe_ref(ctx);
393     }
394   while (xml_peek_char(ctx) != '%');
395 }
396
397 static inline uns
398 xml_parse_dtd_white(struct xml_context *ctx, uns mandatory)
399 {
400   /* Whitespace or parameter entity */
401   uns cnt = 0;
402   while (xml_peek_cat(ctx) & XML_CHAR_WHITE)
403     {
404       xml_skip_char(ctx);
405       cnt = 1;
406     }
407   if (xml_peek_char(ctx) == '%')
408     {
409       xml_parse_dtd_pe(ctx);
410       return 1;
411     }
412   else if (unlikely(mandatory && !cnt))
413     xml_fatal_expected_white(ctx);
414   return cnt;
415 }
416
417 static void
418 xml_dtd_parse_external_id(struct xml_context *ctx, char **system_id, char **public_id, uns allow_public)
419 {
420   struct xml_dtd *dtd = ctx->dtd;
421   uns c = xml_peek_char(ctx);
422   if (c == 'S')
423     {
424       xml_parse_seq(ctx, "SYSTEM");
425       xml_parse_dtd_white(ctx, 1);
426       *public_id = NULL;
427       *system_id = xml_parse_system_literal(ctx, dtd->pool);
428     }
429   else if (c == 'P')
430     {
431       xml_parse_seq(ctx, "PUBLIC");
432       xml_parse_dtd_white(ctx, 1);
433       *system_id = NULL;
434       *public_id = xml_parse_pubid_literal(ctx, dtd->pool);
435       if (xml_parse_dtd_white(ctx, !allow_public))
436         if ((c = xml_peek_char(ctx)) == '\'' || c == '"' || !allow_public)
437           *system_id = xml_parse_system_literal(ctx, dtd->pool);
438     }
439   else
440     xml_fatal(ctx, "Expected an external ID");
441 }
442
443 /* DTD: <!NOTATION ...> */
444
445 void
446 xml_parse_notation_decl(struct xml_context *ctx)
447 {
448   /* NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
449    * Already parsed: '<!NOTATION' */
450   TRACE(ctx, "parse_notation_decl");
451   struct xml_dtd *dtd = ctx->dtd;
452   xml_parse_dtd_white(ctx, 1);
453
454   struct xml_dtd_notn *notn = xml_dtd_notns_lookup(dtd->tab_notns, xml_parse_name(ctx, dtd->pool));
455   xml_parse_dtd_white(ctx, 1);
456   char *system_id, *public_id;
457   xml_dtd_parse_external_id(ctx, &system_id, &public_id, 1);
458   xml_parse_dtd_white(ctx, 0);
459   xml_parse_char(ctx, '>');
460
461   if (notn->flags & XML_DTD_NOTN_DECLARED)
462     xml_warn(ctx, "Notation %s already declared", notn->name);
463   else
464     {
465       notn->flags = XML_DTD_NOTN_DECLARED;
466       notn->system_id = system_id;
467       notn->public_id = public_id;
468       slist_add_tail(&dtd->notns, &notn->n);
469     }
470   xml_dec(ctx);
471 }
472
473 /* DTD: <!ENTITY ...> */
474
475 void
476 xml_parse_entity_decl(struct xml_context *ctx)
477 {
478   /* Already parsed: '<!ENTITY' */
479   TRACE(ctx, "parse_entity_decl");
480   struct xml_dtd *dtd = ctx->dtd;
481   xml_parse_dtd_white(ctx, 1);
482
483   uns flags = (xml_get_char(ctx) == '%') ? XML_DTD_ENTITY_PARAMETER : 0;
484   if (flags)
485     xml_parse_dtd_white(ctx, 1);
486   else
487     xml_unget_char(ctx);
488
489   struct xml_dtd_entity *ent = xml_dtd_ents_lookup(flags ? dtd->tab_pents : dtd->tab_ents, xml_parse_name(ctx, dtd->pool));
490   slist *list = flags ? &dtd->pents : &dtd->ents;
491   xml_parse_dtd_white(ctx, 1);
492   if (ent->flags & XML_DTD_ENTITY_DECLARED)
493     {
494        xml_fatal(ctx, "Entity &%s; already declared, skipping not implemented", ent->name);
495        // FIXME: should be only warning
496     }
497
498   uns c, sep = xml_get_char(ctx);
499   if (sep == '\'' || sep == '"')
500     {
501       /* Internal entity:
502        * EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | "'" ([^%&'] | PEReference | Reference)* "'" */
503       char *p = mp_start_noalign(dtd->pool, 1);
504       while (1)
505         {
506           if ((c = xml_get_char(ctx)) == sep)
507             break;
508           if (c == '%')
509             {
510               // FIXME
511               ASSERT(0);
512               //xml_parse_parameter_ref(ctx);
513               continue;
514             }
515           if (c == '&')
516             {
517               xml_inc(ctx);
518               if (xml_peek_char(ctx) != '#')
519                 {
520                   /* Bypass references to general entities */
521                   struct mempool_state state;
522                   mp_save(ctx->stack, &state);
523                   char *n = xml_parse_name(ctx, ctx->stack);
524                   xml_parse_char(ctx, ';');
525                   xml_dec(ctx);
526                   uns l = strlen(n);
527                   p = mp_spread(dtd->pool, p, 3 + l);
528                   *p++ = '&';
529                   memcpy(p, n, l);
530                   p += l;
531                   *p++ = ';';;
532                   mp_restore(ctx->stack, &state);
533                   continue;
534                 }
535               else
536                 {
537                   xml_skip_char(ctx);
538                   c = xml_parse_char_ref(ctx);
539                 }
540             }
541           p = mp_spread(dtd->pool, p, 5);
542           p = utf8_32_put(p, c);
543         }
544       *p = 0;
545       ent->len = p - (char *)mp_ptr(dtd->pool);
546       ent->text = mp_end(dtd->pool, p + 1);
547       slist_add_tail(list, &ent->n);
548       ent->flags = flags | XML_DTD_ENTITY_DECLARED;
549     }
550   else
551     {
552       /* External entity */
553       struct xml_dtd_notn *notn = NULL;
554       char *system_id, *public_id;
555       xml_unget_char(ctx);
556       xml_dtd_parse_external_id(ctx, &system_id, &public_id, 0);
557       if (xml_parse_dtd_white(ctx, 0) && flags && xml_peek_char(ctx) != '>')
558         {
559           /* General external unparsed entity */
560           flags |= XML_DTD_ENTITY_UNPARSED;
561           xml_parse_seq(ctx, "NDATA");
562           xml_parse_dtd_white(ctx, 1);
563           notn = xml_dtd_notns_lookup(dtd->tab_notns, xml_parse_name(ctx, dtd->pool));
564         }
565       slist_add_tail(list, &ent->n);
566       ent->flags = flags | XML_DTD_ENTITY_DECLARED | XML_DTD_ENTITY_EXTERNAL;
567       ent->system_id = system_id;
568       ent->public_id = public_id;
569       ent->notn = notn;
570     }
571   xml_parse_dtd_white(ctx, 0);
572   xml_parse_char(ctx, '>');
573   xml_dec(ctx);
574 }
575
576 /* DTD: <!ELEMENT ...> */
577
578 void
579 xml_parse_element_decl(struct xml_context *ctx)
580 {
581   /* Elementdecl ::= '<!ELEMENT' S  Name  S  contentspec  S? '>'
582    * Already parsed: '<!ELEMENT' */
583   struct xml_dtd *dtd = ctx->dtd;
584   xml_parse_dtd_white(ctx, 1);
585   char *name = xml_parse_name(ctx, dtd->pool);
586   xml_parse_dtd_white(ctx, 1);
587   struct xml_dtd_elem *elem = xml_dtd_elems_lookup(dtd->tab_elems, name);
588   if (elem->flags & XML_DTD_ELEM_DECLARED)
589     xml_fatal(ctx, "Element <%s> already declared", name);
590
591   /* contentspec ::= 'EMPTY' | 'ANY' | Mixed | children */
592   uns c = xml_peek_char(ctx);
593   if (c == 'E')
594     {
595       xml_parse_seq(ctx, "EMPTY");
596       elem->type = XML_DTD_ELEM_EMPTY;
597     }
598   else if (c == 'A')
599     {
600       xml_parse_seq(ctx, "ANY");
601       elem->type = XML_DTD_ELEM_ANY;
602     }
603   else if (c == '(')
604     {
605       xml_skip_char(ctx);
606       xml_inc(ctx);
607       xml_parse_dtd_white(ctx, 0);
608       struct xml_dtd_elem_node *parent = elem->node = mp_alloc_zero(dtd->pool, sizeof(*parent));
609       if (xml_peek_char(ctx) == '#')
610         {
611           /* Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | '(' S? '#PCDATA' S? ')' */
612           xml_skip_char(ctx);
613           xml_parse_seq(ctx, "PCDATA");
614           elem->type = XML_DTD_ELEM_MIXED;
615           parent->type = XML_DTD_ELEM_PCDATA;
616           while (1)
617             {
618               xml_parse_dtd_white(ctx, 0);
619               if ((c = xml_get_char(ctx)) == ')')
620                 break;
621               else if (c != '|')
622                 xml_fatal_expected(ctx, ')');
623               xml_parse_dtd_white(ctx, 0);
624               struct xml_dtd_elem *son_elem = xml_dtd_elems_lookup(dtd->tab_elems, xml_parse_name(ctx, dtd->pool));
625               if (xml_dtd_enodes_find(dtd->tab_enodes, parent, son_elem))
626                 xml_error(ctx, "Duplicate content '%s'", son_elem->name);
627               else
628                 {
629                   struct xml_dtd_elem_node *son = xml_dtd_enodes_new(dtd->tab_enodes, parent, son_elem);
630                   slist_add_tail(&parent->sons, &son->n);
631                 }
632             }
633           xml_dec(ctx);
634           if (xml_peek_char(ctx) == '*')
635             {
636               xml_skip_char(ctx);
637               parent->occur = XML_DTD_ELEM_OCCUR_MULT;
638             }
639           else if (!slist_head(&parent->sons))
640             parent->occur = XML_DTD_ELEM_OCCUR_ONCE;
641           else
642             xml_fatal_expected(ctx, '*');
643         }
644       else
645         {
646           /* children ::= (choice | seq) ('?' | '*' | '+')?
647            * cp ::= (Name | choice | seq) ('?' | '*' | '+')?
648            * choice ::= '(' S? cp ( S? '|' S? cp )+ S? ')'
649            * seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' */
650
651           elem->type = XML_DTD_ELEM_CHILDREN;
652           parent->type = XML_DTD_ELEM_PCDATA;
653           uns c;
654           goto first;
655
656           while (1)
657             {
658               /* After name */
659               xml_parse_dtd_white(ctx, 0);
660               if ((c = xml_get_char(ctx)) ==  ')')
661                 {
662                   xml_dec(ctx);
663                   if (parent->type == XML_DTD_ELEM_PCDATA)
664                     parent->type = XML_DTD_ELEM_SEQ;
665                   if ((c = xml_get_char(ctx)) == '?')
666                     parent->occur = XML_DTD_ELEM_OCCUR_OPT;
667                   else if (c == '*')
668                     parent->occur = XML_DTD_ELEM_OCCUR_MULT;
669                   else if (c == '+')
670                     parent->occur = XML_DTD_ELEM_OCCUR_PLUS;
671                   else
672                     {
673                       xml_unget_char(ctx);
674                       parent->occur = XML_DTD_ELEM_OCCUR_ONCE;
675                     }
676                   if (!parent->parent)
677                     break;
678                   parent = parent->parent;
679                   continue;
680                 }
681               else if (c == '|')
682                 {
683                   if (parent->type == XML_DTD_ELEM_PCDATA)
684                     parent->type = XML_DTD_ELEM_OR;
685                   else if (parent->type != XML_DTD_ELEM_OR)
686                     xml_fatal(ctx, "Mixed operators in the list of element children");
687                 }
688               else if (c == ',')
689                 {
690                   if (parent->type == XML_DTD_ELEM_PCDATA)
691                     parent->type = XML_DTD_ELEM_SEQ;
692                   else if (parent->type != XML_DTD_ELEM_SEQ)
693                     xml_fatal(ctx, "Mixed operators in the list of element children");
694                 }
695               else if (c == '(')
696                 {
697                   xml_inc(ctx);
698                   struct xml_dtd_elem_node *son = mp_alloc_zero(dtd->pool, sizeof(*son));
699                   son->parent = parent;
700                   slist_add_tail(&parent->sons, &son->n);
701                   parent = son->parent;
702                   son->type = XML_DTD_ELEM_MIXED;
703                 }
704               else
705                 xml_unget_char(ctx);
706
707               /* Before name */
708               xml_parse_dtd_white(ctx, 0);
709 first:;
710               struct xml_dtd_elem *son_elem = xml_dtd_elems_lookup(dtd->tab_elems, xml_parse_name(ctx, dtd->pool));
711               // FIXME: duplicates, occurance
712               //struct xml_dtd_elem_node *son = xml_dtd_enodes_new(dtd->tab_enodes, parent, son_elem);
713               struct xml_dtd_elem_node *son = mp_alloc_zero(dtd->pool, sizeof(*son));
714               son->parent = parent;
715               son->elem = son_elem;
716               slist_add_tail(&parent->sons, &son->n);
717             }
718         }
719     }
720   else
721     xml_fatal(ctx, "Expected element content specification");
722
723   xml_parse_dtd_white(ctx, 0);
724   xml_parse_char(ctx, '>');
725   xml_dec(ctx);
726 }
727
728 void
729 xml_parse_attr_list_decl(struct xml_context *ctx)
730 {
731   /* AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
732    * AttDef ::= S Name S AttType S DefaultDecl
733    * Already parsed: '<!ATTLIST' */
734   struct xml_dtd *dtd = ctx->dtd;
735   xml_parse_dtd_white(ctx, 1);
736   struct xml_dtd_elem *elem = xml_dtd_elems_lookup(ctx->dtd->tab_elems, xml_parse_name(ctx, dtd->pool));
737
738   while (xml_parse_dtd_white(ctx, 0) && xml_peek_char(ctx) != '>')
739     {
740       char *name = xml_parse_name(ctx, dtd->pool);
741       struct xml_dtd_attr *attr = xml_dtd_attrs_find(dtd->tab_attrs, elem, name);
742       uns ignored = 0;
743       if (attr)
744         {
745           xml_warn(ctx, "Duplicate attribute definition");
746           ignored++;
747         }
748       else
749         attr = xml_dtd_attrs_new(ctx->dtd->tab_attrs, elem, name);
750       xml_parse_dtd_white(ctx, 1);
751       if (xml_peek_char(ctx) == '(')
752         {
753           xml_skip_char(ctx); // FIXME: xml_inc/dec ?
754           if (!ignored)
755             attr->type = XML_ATTR_ENUM;
756           do
757             {
758               xml_parse_dtd_white(ctx, 0);
759               char *value = xml_parse_nmtoken(ctx, dtd->pool);
760               if (!ignored)
761                 if (xml_dtd_evals_find(ctx->dtd->tab_evals, attr, value))
762                   xml_error(ctx, "Duplicate enumeration value");
763                 else
764                   xml_dtd_evals_new(ctx->dtd->tab_evals, attr, value);
765               xml_parse_dtd_white(ctx, 0);
766             }
767           while (xml_get_char(ctx) == '|');
768           xml_unget_char(ctx);
769           xml_parse_char(ctx, ')');
770         }
771       else
772         {
773           char *type = xml_parse_name(ctx, dtd->pool);
774           enum xml_dtd_attr_type t = XML_ATTR_CDATA;
775           if (!strcmp(type, "CDATA"))
776             t = XML_ATTR_CDATA;
777           else if (!strcmp(type, "ID"))
778             t = XML_ATTR_ID;
779           else if (!strcmp(type, "IDREF"))
780             t = XML_ATTR_IDREF;
781           else if (!strcmp(type, "IDREFS"))
782             t = XML_ATTR_IDREFS;
783           else if (!strcmp(type, "ENTITY"))
784             t = XML_ATTR_ENTITY;
785           else if (!strcmp(type, "ENTITIES"))
786             t = XML_ATTR_ENTITIES;
787           else if (!strcmp(type, "NMTOKEN"))
788             t = XML_ATTR_NMTOKEN;
789           else if (!strcmp(type, "NMTOKENS"))
790             t = XML_ATTR_NMTOKENS;
791           else if (!strcmp(type, "NOTATION"))
792             {
793               if (elem->type == XML_DTD_ELEM_EMPTY)
794                 xml_fatal(ctx, "Empty element must not have notation attribute");
795               // FIXME: An element type MUST NOT have more than one NOTATION attribute specified.
796               t = XML_ATTR_NOTATION;
797               xml_parse_dtd_white(ctx, 1);
798               xml_parse_char(ctx, '(');
799               do
800                 {
801                   xml_parse_dtd_white(ctx, 0);
802                   struct xml_dtd_notn *n = xml_dtd_notns_lookup(ctx->dtd->tab_notns, xml_parse_name(ctx, dtd->pool));
803                   if (!ignored)
804                     if (xml_dtd_enotns_find(ctx->dtd->tab_enotns, attr, n))
805                       xml_error(ctx, "Duplicate enumerated notation");
806                     else
807                       xml_dtd_enotns_new(ctx->dtd->tab_enotns, attr, n);
808                   xml_parse_dtd_white(ctx, 0);
809                 }
810               while (xml_get_char(ctx) == '|');
811               xml_unget_char(ctx);
812               xml_parse_char(ctx, ')');
813             }
814           else
815             xml_fatal(ctx, "Unknown attribute type");
816           if (!ignored)
817             attr->type = t;
818         }
819       xml_parse_dtd_white(ctx, 1);
820       enum xml_dtd_attr_default def = XML_ATTR_NONE;
821       if (xml_get_char(ctx) == '#')
822         switch (xml_peek_char(ctx))
823           {
824             case 'R':
825               xml_parse_seq(ctx, "REQUIRED");
826               def = XML_ATTR_REQUIRED;
827               break;
828             case 'I':
829               xml_parse_seq(ctx, "IMPLIED");
830               def = XML_ATTR_IMPLIED;
831               break;
832             case 'F':
833               xml_parse_seq(ctx, "FIXED");
834               def = XML_ATTR_FIXED;
835               xml_parse_dtd_white(ctx, 1);
836               break;
837             default:
838               xml_fatal(ctx, "Expected a modifier for default attribute value");
839           }
840       else
841         xml_unget_char(ctx);
842       if (def != XML_ATTR_REQUIRED && def != XML_ATTR_IMPLIED)
843         {
844           char *v = xml_parse_attr_value(ctx, attr);
845           if (!ignored)
846             attr->default_value = v;
847         }
848       if (!ignored)
849         attr->default_mode = def;
850     }
851   xml_skip_char(ctx);
852   xml_dec(ctx);
853 }
854
855 void
856 xml_skip_internal_subset(struct xml_context *ctx)
857 {
858   TRACE(ctx, "skip_internal_subset");
859   /* AlreadyParsed: '[' */
860   uns c;
861   while ((c = xml_get_char(ctx)) != ']')
862     {
863       if (c != '<')
864         continue;
865       if ((c = xml_get_char(ctx)) == '?')
866         {
867           xml_inc(ctx);
868           xml_skip_pi(ctx);
869         }
870       else if (c != '!')
871         xml_dec(ctx);
872       else if (xml_get_char(ctx) == '-')
873         {
874           xml_inc(ctx);
875           xml_skip_comment(ctx);
876         }
877       else
878         while ((c = xml_get_char(ctx)) != '>')
879           if (c == '\'' || c == '"')
880             while (xml_get_char(ctx) != c);
881     }
882   xml_dec(ctx);
883 }
884
885 /*** Validation of attribute values ***/
886
887 static uns
888 xml_check_tokens(char *value, uns first_cat, uns next_cat, uns seq)
889 {
890   char *p = value;
891   uns u;
892   while (1)
893     {
894       p = utf8_32_get(p, &u);
895       if (!(xml_char_cat(u) & first_cat))
896         return 0;
897       while (*p & ~0x20)
898         {
899           p = utf8_32_get(p, &u);
900           if (!(xml_char_cat(u) & next_cat))
901             return 0;
902         }
903       if (!*p)
904         return 1;
905       if (!seq)
906         return 0;
907       p++;
908     }
909 }
910
911 static uns
912 xml_is_name(struct xml_context *ctx, char *value)
913 {
914   /* Name ::= NameStartChar (NameChar)* */
915   return xml_check_tokens(value, ctx->cat_sname, ctx->cat_name, 0);
916 }
917
918 static uns
919 xml_is_names(struct xml_context *ctx, char *value)
920 {
921   /* Names ::= Name (#x20 Name)* */
922   return xml_check_tokens(value, ctx->cat_sname, ctx->cat_name, 1);
923 }
924
925 static uns
926 xml_is_nmtoken(struct xml_context *ctx, char *value)
927 {
928   /* Nmtoken ::= (NameChar)+ */
929   return xml_check_tokens(value, ctx->cat_name, ctx->cat_name, 0);
930 }
931
932 static uns
933 xml_is_nmtokens(struct xml_context *ctx, char *value)
934 {
935   /* Nmtokens ::= Nmtoken (#x20 Nmtoken)* */
936   return xml_check_tokens(value, ctx->cat_name, ctx->cat_name, 1);
937 }
938
939 static void
940 xml_err_attr_format(struct xml_context *ctx, struct xml_dtd_attr *dtd, char *type)
941 {
942   xml_error(ctx, "Attribute %s in <%s> does not match the production of %s", dtd->name, dtd->elem->name, type);
943 }
944
945 void
946 xml_validate_attr(struct xml_context *ctx, struct xml_dtd_attr *dtd, char *value)
947 {
948   if (dtd->type == XML_ATTR_CDATA)
949     return;
950   xml_normalize_white(ctx, value);
951   switch (dtd->type)
952     {
953       case XML_ATTR_ID:
954         if (!xml_is_name(ctx, value))
955           xml_err_attr_format(ctx, dtd, "NAME");
956         //FIXME: add to a hash table
957         break;
958       case XML_ATTR_IDREF:
959         if (!xml_is_name(ctx, value))
960           xml_err_attr_format(ctx, dtd, "NAME");
961         // FIXME: find in hash table (beware forward references)
962         break;
963       case XML_ATTR_IDREFS:
964         if (!xml_is_names(ctx, value))
965           xml_err_attr_format(ctx, dtd, "NAMES");
966         // FIXME: find
967         break;
968       case XML_ATTR_ENTITY:
969         // FIXME
970         break;
971       case XML_ATTR_ENTITIES:
972         // FIXME
973         break;
974       case XML_ATTR_NMTOKEN:
975         if (!xml_is_nmtoken(ctx, value))
976           xml_err_attr_format(ctx, dtd, "NMTOKEN");
977         break;
978       case XML_ATTR_NMTOKENS:
979         if (!xml_is_nmtokens(ctx, value))
980           xml_err_attr_format(ctx, dtd, "NMTOKENS");
981         break;
982       case XML_ATTR_ENUM:
983         if (!xml_dtd_evals_find(ctx->dtd->tab_evals, dtd, value))
984           xml_error(ctx, "Attribute %s in <%s> contains an undefined enumeration value", dtd->name, dtd->elem->name);
985         break;
986       case XML_ATTR_NOTATION:
987         if (!xml_dtd_find_notn(ctx, value))
988           xml_error(ctx, "Attribute %s in <%s> contains an undefined notation", dtd->name, dtd->elem->name);
989         break;
990     }
991 }