-
Notifications
You must be signed in to change notification settings - Fork 9
/
parser.c
5786 lines (4739 loc) · 133 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2000-2020, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#define _ISOC99_SOURCE 1 /* fwprintf(), etc prototypes */
#define _CRT_SECURE_NO_WARNINGS 1
#define DTD_IMPLEMENTATION 1
#include <stdio.h>
#include <wchar.h>
#include "dtd.h"
#include "model.h"
#include "util.h"
#include "catalog.h"
#include "parser.h"
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include "utf8.h"
#include <errno.h>
#include <wctype.h>
#include "xml_unicode.h"
#define DEBUG(g) ((void)0)
#define ZERO_TERM_LEN (-1) /* terminated by nul */
#ifdef __WINDOWS__
#define inline __inline
#define swprintf _snwprintf
#endif
/*******************************
* LOCAL TYPES *
*******************************/
typedef struct locbuf
{ dtd_srcloc start; /* p->startloc */
dtd_srcloc here; /* p->location */
} locbuf;
#define FASTATTRIBUTES 64
typedef struct sgml_attribute_list
{ sgml_attribute *attributes; /* The attributes */
size_t count; /* Number of attributes */
size_t allocated; /* #Allocated attributes */
sgml_attribute local[FASTATTRIBUTES];/* Handle fast up to here */
} sgml_attribute_list;
/*******************************
* PROTOYPES *
*******************************/
static const ichar * itake_name(dtd_parser *p,
const ichar *in, dtd_symbol **id);
static const ichar * itake_entity_name(dtd_parser *p, const ichar *in,
dtd_symbol **id);
static const ichar * itake_namegroup(dtd_parser *p, const ichar *decl,
dtd_symbol **names, int *n);
static const ichar * iskip_layout(dtd *dtd, const ichar *in);
static dtd_parser * clone_dtd_parser(dtd_parser *p);
static void free_model(dtd_model *m);
static int process_entity_declaration(dtd_parser *p,
const ichar *decl);
static void free_notations(dtd_notation *n);
static void free_shortrefs(dtd_shortref *sr);
static int process_cdata(dtd_parser *p, int last);
static int process_entity(dtd_parser *p, const ichar *name);
static int emit_cdata(dtd_parser *p, int last);
static dtd_space_mode istr_to_space_mode(const ichar *val);
static void update_space_mode(dtd_parser *p, dtd_element *e,
size_t natts, sgml_attribute *atts);
static dtd_model * make_model(dtd_parser *p, const ichar *decl,
const ichar **end);
static void for_elements_in_model(dtd_model *m,
void (*f)(dtd_element *e,
void *closure),
void *closure);
int putchar_dtd_parser(dtd_parser *p, int chr);
void free_dtd_parser(dtd_parser *p);
static const ichar * isee_character_entity(dtd *dtd, const ichar *in,
int *chr);
static int add_default_attributes(dtd_parser *p, dtd_element *e,
sgml_attribute_list *atts);
static int prepare_cdata(dtd_parser *p);
static void init_attribute_list(sgml_attribute_list *atts);
static void clear_attribute_list(sgml_attribute_list *atts);
static sgml_attribute * new_attribute(sgml_attribute_list *atts);
/*******************************
* MACROS *
*******************************/
#define WITH_CLASS(p, c, g) \
{ sgml_event_class _oc = p->event_class; \
p->event_class = c; \
g; \
p->event_class = _oc; \
}
/*******************************
* STATISTICS *
*******************************/
#ifdef O_STATISTICS
int edefs_created = 0;
int edefs_freed = 0;
int edefs_implicit = 0;
int edefs_atts = 0;
int edefs_decl = 0;
int dtd_created = 0;
int dtd_freed = 0;
void
sgml_statistics(void)
{ fprintf(stderr, "EDEFS: created %d; freed %d\n", edefs_created, edefs_freed);
fprintf(stderr, "EDEFS: implicit %d; atts %d; decl %d\n",
edefs_implicit, edefs_atts, edefs_decl);
fprintf(stderr, "DTDs: created: %d; freed: %d\n", dtd_created, dtd_freed);
}
#define STAT(g) g
#else
#define STAT(g) ((void)0)
#endif
/*******************************
* SRC LOCATION *
*******************************/
static void /* TBD: also handle startloc */
push_location(dtd_parser *p, locbuf *save)
{ save->here = p->location;
save->start = p->startloc;
p->location.parent = &save->here;
p->startloc.parent = &save->start;
}
static void
pop_location(dtd_parser *p, locbuf *saved)
{ p->location = saved->here;
p->startloc = saved->start;
}
static inline void
_sgml_cplocation(dtd_srcloc *d, dtd_srcloc *loc)
{ d->type = loc->type;
d->name.file = loc->name.file;
d->line = loc->line;
d->linepos = loc->linepos;
d->charpos = loc->charpos;
/* but not the parent! */
}
void
sgml_cplocation(dtd_srcloc *d, dtd_srcloc *loc)
{ _sgml_cplocation(d, loc);
}
#define sgml_cplocation(d,s) _sgml_cplocation(d, s)
static void
inc_location(dtd_srcloc *l, int chr)
{ if ( chr == '\n' )
{ l->linepos = 0;
l->line++;
} else if ( chr == '\t' )
{ l->linepos |= 7;
}
l->linepos++;
l->charpos++;
}
static void
dec_location(dtd_srcloc *l, int chr)
{ if ( chr == '\n' )
{ l->linepos = 2; /* not good! */
l->line--;
}
l->linepos--;
l->charpos--;
}
/*******************************
* CLASSIFICATION PRIMITIVES *
*******************************/
static inline int
HasClass(dtd *dtd, wint_t chr, int mask)
{ if ( chr <= 0xff )
return (dtd->charclass->class[(chr)] & (mask));
else
{ switch(mask)
{ case CH_NAME:
return ( xml_basechar(chr) ||
xml_digit(chr) ||
xml_ideographic(chr) ||
xml_combining_char(chr) ||
xml_extender(chr)
);
case CH_NMSTART:
return ( xml_basechar(chr) ||
xml_ideographic(chr) );
case CH_WHITE:
return FALSE; /* only ' ' and '\t' */
case CH_BLANK:
return iswspace(chr);
case CH_DIGIT:
return xml_digit(chr);
case CH_RS:
case CH_RE:
return FALSE;
default:
assert(0);
return FALSE;
}
}
}
static const ichar *
isee_func(dtd *dtd, const ichar *in, charfunc func)
{ if ( dtd->charfunc->func[func] == *in )
return ++in;
return NULL;
}
/*******************************
* SYMBOLS *
*******************************/
static dtd_symbol_table *
new_symbol_table()
{ dtd_symbol_table *t = sgml_calloc(1, sizeof(*t));
t->size = SYMBOLHASHSIZE;
t->entries = sgml_calloc(t->size, sizeof(dtd_symbol*));
return t;
}
static void
free_symbol_table(dtd_symbol_table *t)
{ int i;
for(i=0; i<t->size; i++)
{ dtd_symbol *s, *next;
for(s=t->entries[i]; s; s=next)
{ next = s->next;
sgml_free((ichar*)s->name);
sgml_free(s);
}
}
sgml_free(t->entries);
sgml_free(t);
}
dtd_symbol *
dtd_find_symbol(dtd *dtd, const ichar *name)
{ dtd_symbol_table *t = dtd->symbols;
if ( dtd->case_sensitive )
{ int k = istrhash(name, t->size);
dtd_symbol *s;
for(s=t->entries[k]; s; s = s->next)
{ if ( istreq(s->name, name) )
return s;
}
} else
{ int k = istrcasehash(name, t->size);
dtd_symbol *s;
for(s=t->entries[k]; s; s = s->next)
{ if ( istrcaseeq(s->name, name) )
return s;
}
}
return NULL;
}
static dtd_symbol *
dtd_find_entity_symbol(dtd *dtd, const ichar *name)
{ dtd_symbol_table *t = dtd->symbols;
if ( dtd->ent_case_sensitive )
{ int k = istrhash(name, t->size);
dtd_symbol *s;
for(s=t->entries[k]; s; s = s->next)
{ if ( istreq(s->name, name) )
return s;
}
} else
{ int k = istrcasehash(name, t->size);
dtd_symbol *s;
for(s=t->entries[k]; s; s = s->next)
{ if ( istrcaseeq(s->name, name) )
return s;
}
}
return NULL;
}
dtd_symbol *
dtd_add_symbol(dtd *dtd, const ichar *name)
{ dtd_symbol_table *t = dtd->symbols;
int k = istrhash(name, t->size);
dtd_symbol *s;
for(s=t->entries[k]; s; s = s->next)
{ if ( istreq(s->name, name) )
return s;
}
s = sgml_calloc(1, sizeof(*s));
s->name = istrdup(name);
s->next = t->entries[k];
t->entries[k] = s;
return s;
}
/*******************************
* ENTITIES *
*******************************/
static void
free_entity_list(dtd_entity *e)
{ dtd_entity *next;
for( ; e; e=next)
{ next = e->next;
if ( e->value ) sgml_free(e->value);
if ( e->extid ) sgml_free(e->extid);
if ( e->exturl ) sgml_free(e->exturl);
if ( e->baseurl ) sgml_free(e->baseurl);
sgml_free(e);
}
}
static dtd_entity *
find_pentity(dtd *dtd, dtd_symbol *id)
{ dtd_entity *e;
for(e = dtd->pentities; e; e=e->next)
{ if ( e->name == id )
return e;
}
return NULL;
}
/* returned path must be freed when done */
static ichar *
entity_file(dtd *dtd, dtd_entity *e)
{ switch(e->type)
{ case ET_SYSTEM:
case ET_PUBLIC:
{ const ichar *f;
f = find_in_catalogue(e->catalog_location,
e->name->name,
e->extid,
e->exturl,
IS_XML_DIALECT(dtd->dialect));
if ( f ) /* owned by catalog */
{ ichar *file;
if ( is_absolute_path(f) || is_url(f) || !e->baseurl )
file = istrdup(f);
else
file = localpath(e->baseurl, f);
return file;
}
}
default:
return NULL;
}
}
static const ichar *
entity_value(dtd_parser *p, dtd_entity *e, int *len)
{ ichar *file;
if ( !e->value && (file=entity_file(p->dtd, e)) )
{ int normalise = (e->content == EC_SGML || e->content == EC_CDATA);
size_t l;
e->value = load_sgml_file_to_charp(file, normalise, &l);
e->length = (long)l;
sgml_free(file);
}
if ( len )
*len = e->length;
return e->value;
}
static int
expand_pentities(dtd_parser *p, const ichar *in, int ilen, ichar *out, int len)
{ dtd *dtd = p->dtd;
int pero = dtd->charfunc->func[CF_PERO]; /* % */
int ero = dtd->charfunc->func[CF_ERO]; /* & */
const ichar *s;
const ichar *end;
if ( ilen == ZERO_TERM_LEN )
{ end = in + wcslen(in);
} else
{ end = &in[ilen];
}
while(in < end)
{ if ( *in == pero )
{ dtd_symbol *id;
if ( (s = itake_entity_name(p, in+1, &id)) )
{ dtd_entity *e = find_pentity(dtd, id);
const ichar *eval;
int l;
in = s;
if ( (s=isee_func(dtd, s, CF_ERC)) ) /* ; is not obligatory? */
in = s;
if ( !e )
return gripe(p, ERC_EXISTENCE, L"parameter entity", id->name);
if ( !(eval = entity_value(p, e, NULL)) )
return FALSE;
if ( !expand_pentities(p, eval, ZERO_TERM_LEN, out, len) )
return FALSE;
l = (int)istrlen(out); /* could be better */
out += l;
len -= l;
continue;
}
}
if ( --len <= 0 )
{ gripe(p, ERC_REPRESENTATION, L"Declaration too long");
return FALSE;
}
if ( *in == ero && in[1] == '#' ) /* &# */
{ int chr;
if ( (s=isee_character_entity(dtd, in, &chr)) )
{ if ( chr == 0 )
{ gripe(p, ERC_SYNTAX_ERROR, L"Illegal character entity", in);
} else
{ *out++ = chr;
in = s;
continue;
}
}
}
*out++ = *in++;
}
*out = '\0';
return TRUE;
}
static int
char_entity_value(const ichar *decl)
{ if ( *decl == '#' )
{ const ichar *s = decl+1;
ichar *end;
long v;
/* do octal too? */
if ( s[0] == 'x' || s[0] == 'X' )
v = wcstoul(s+1, &end, 16);
else
v = wcstoul(s, &end, 10);
if ( *end == '\0' )
{ return (int)v;
} else if ( istreq(s, L"RS") )
{ return '\n';
} else if ( istreq(s, L"RE") )
{ return '\r';
} else if ( istreq(s, L"TAB") )
{ return '\t';
} else if ( istreq(s, L"SPACE") )
{ return ' ';
}
}
return -1;
}
static const ichar *
isee_character_entity(dtd *dtd, const ichar *in, int *chr)
{ const ichar *s;
if ( (s=isee_func(dtd, in, CF_ERO)) && *s == '#' )
{ ichar e[32];
ichar *o = e;
int v;
*o++ = *s++;
while(o < e+sizeof(e)/sizeof(ichar)-1 && HasClass(dtd, *s, CH_NAME))
*o++ = *s++;
if ( isee_func(dtd, s, CF_ERC)) /* skip ; */
s++;
*o = '\0';
if ( (v=char_entity_value(e)) >= 0 )
{ *chr = v;
return s;
}
}
return NULL;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand entities in a string. Used to expand CDATA attribute values.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
expand_entities(dtd_parser *p, const ichar *in, int len, ocharbuf *out)
{ const ichar *s;
const ichar *end = &in[len];
dtd *dtd = p->dtd;
int ero = dtd->charfunc->func[CF_ERO]; /* & */
while(in < end)
{ if ( *in == ero )
{ const ichar *estart = in; /* for recovery */
int chr;
if ( (s=isee_character_entity(dtd, in, &chr)) )
{ if ( chr == 0 )
gripe(p, ERC_SYNTAX_ERROR, L"Illegal character entity", in);
add_ocharbuf(out, chr);
in = s;
continue;
}
if ( HasClass(dtd, in[1], CH_NMSTART) )
{ dtd_symbol *id;
dtd_entity *e;
const ichar *eval;
if ( !(in = itake_name(p, in+1, &id)) )
{ in = estart;
goto recover;
}
if ( isee_func(dtd, in, CF_ERC) || *in == '\n' )
in++;
if ( !(e = id->entity) && !(e=dtd->default_entity) )
{ gripe(p, ERC_EXISTENCE, L"entity", id->name);
in = estart;
goto recover;
}
if ( !(eval = entity_value(p, e, NULL)) )
{ gripe(p, ERC_NO_VALUE, e->name->name);
in = estart;
goto recover;
}
if ( e->content == EC_SGML )
{ if ( !expand_entities(p, eval, (int)istrlen(eval), out) )
return FALSE;
} else
{ const ichar *s;
for(s=eval; *s; s++)
add_ocharbuf(out, *s);
}
continue;
}
if ( IS_XML_DIALECT(dtd->dialect) )
gripe(p, ERC_SYNTAX_ERROR, L"Illegal entity", estart);
}
recover:
if ( *in == CR && in[1] == LF )
in++;
if ( HasClass(dtd, *in, CH_BLANK) )
{ add_ocharbuf(out, ' ');
in++;
} else
{ add_ocharbuf(out, *in++);
}
}
terminate_ocharbuf(out);
return TRUE;
}
/*******************************
* ELEMENTS *
*******************************/
static dtd_element *
find_element(dtd *dtd, dtd_symbol *id)
{ dtd_element *e;
if ( id->element )
return id->element; /* must check */
e = sgml_calloc(1, sizeof(*e));
e->space_mode = SP_INHERIT;
e->undefined = TRUE;
e->name = id;
id->element = e;
e->next = dtd->elements;
dtd->elements = e;
return e;
}
static dtd_edef *
new_element_definition(dtd *dtd)
{ dtd_edef *def = sgml_calloc(1, sizeof(*def));
STAT(edefs_created++);
return def;
}
static dtd_element *
def_element(dtd *dtd, dtd_symbol *id)
{ dtd_element *e = find_element(dtd, id);
if ( !e->structure )
{ e->structure = new_element_definition(dtd);
e->structure->references = 1;
e->structure->type = C_EMPTY;
}
return e;
}
static void
free_name_list(dtd_name_list *nl)
{ dtd_name_list *next;
for( ; nl; nl=next)
{ next = nl->next;
sgml_free(nl);
}
}
#define REFS_VIRGIN (-42)
static void
free_attribute(dtd_attr *a)
{ if ( a->references == REFS_VIRGIN || --a->references == 0 )
{ switch(a->type)
{ case AT_NAMEOF:
case AT_NOTATION:
free_name_list(a->typeex.nameof);
default:
;
}
switch(a->def)
{ case AT_DEFAULT:
case AT_FIXED:
{ if ( a->islist )
sgml_free(a->att_def.list);
else if ( a->type == AT_CDATA && a->att_def.cdata )
sgml_free(a->att_def.cdata);
}
default:
;
}
sgml_free(a);
}
}
static void
free_attribute_list(dtd_attr_list *l)
{ dtd_attr_list *next;
for(; l; l=next)
{ next = l->next;
free_attribute(l->attribute);
sgml_free(l);
}
}
static void
free_element_list(dtd_element_list *l)
{ dtd_element_list *next;
for( ; l; l=next)
{ next = l->next;
sgml_free(l);
}
}
static void
free_element_definition(dtd_edef *def)
{ if ( --def->references == 0 )
{ STAT(edefs_freed++);
if ( def->content )
free_model(def->content);
free_element_list(def->included);
free_element_list(def->excluded);
free_state_engine(def->initial_state);
sgml_free(def);
}
}
static void
free_elements(dtd_element *e)
{ dtd_element *next;
for( ; e; e=next)
{ next = e->next;
if ( e->structure )
free_element_definition(e->structure);
free_attribute_list(e->attributes);
sgml_free(e);
}
}
/*******************************
* ATTRIBUTES *
*******************************/
static dtd_attr *
find_attribute(dtd_element *e, dtd_symbol *name)
{ dtd_attr_list *a;
for(a=e->attributes; a; a=a->next)
{ if ( a->attribute->name == name )
return a->attribute;
}
return NULL;
}
/*******************************
* PARSE PRIMITIVES *
*******************************/
static const ichar *
iskip_layout(dtd *dtd, const ichar *in)
{ ichar cmt = dtd->charfunc->func[CF_CMT]; /* also skips comment */
for( ; *in; in++ )
{ if ( HasClass(dtd, *in, CH_BLANK) )
continue;
if ( in[0] == cmt && in[1] == cmt )
{ in += 2;
for( ; *in; in++ )
{ if ( in[0] == cmt && in[1] == cmt )
break;
}
in++;
continue;
}
return in;
}
return in;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
See whether we are looking at identifier "id". "id" must be lowercase!
This is only used for reserved words, and parsed case-insentive in both
XML and SGML modes.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static const ichar *
isee_identifier(dtd *dtd, const ichar *in, char *id)
{ in = iskip_layout(dtd, in);
/* match */
while (*id && (wint_t)*id == towlower(*in) )
id++, in++;
if ( *id == 0 && !HasClass(dtd, *in, CH_NAME) )
return iskip_layout(dtd, in);
return NULL;
}
static const ichar *
itake_name(dtd_parser *p, const ichar *in, dtd_symbol **id)
{ ichar buf[MAXNMLEN];
ichar *o = buf;
ichar *e = &buf[MAXNMLEN]-1;
dtd *dtd = p->dtd;
in = iskip_layout(dtd, in);
if ( !HasClass(dtd, *in, CH_NMSTART) )
return NULL;
if ( dtd->case_sensitive )
{ while( HasClass(dtd, *in, CH_NAME) && o < e )
*o++ = *in++;
} else
{ while( HasClass(dtd, *in, CH_NAME) && o < e )
*o++ = towlower(*in++);
}
if ( o == e )
{ gripe(p, ERC_REPRESENTATION, L"NAME too long");
return NULL;
}
*o++ = '\0';
*id = dtd_add_symbol(dtd, buf);
return iskip_layout(dtd, in);
}
static const ichar *
itake_entity_name(dtd_parser *p, const ichar *in, dtd_symbol **id)
{ ichar buf[MAXNMLEN];
ichar *o = buf;
ichar *e = &buf[MAXNMLEN]-1;
dtd *dtd = p->dtd;
in = iskip_layout(dtd, in);
if ( !HasClass(dtd, *in, CH_NMSTART) )
return NULL;
if ( dtd->ent_case_sensitive )
{ while( HasClass(dtd, *in, CH_NAME) && o < e )
*o++ = *in++;
} else
{ while( HasClass(dtd, *in, CH_NAME) && o < e )
*o++ = towlower(*in++);
}
if ( o == e )
{ gripe(p, ERC_REPRESENTATION, L"Entity NAME too long");
return NULL;
}
*o++ = '\0';
*id = dtd_add_symbol(dtd, buf);
return in;
}
static const ichar *
itake_nmtoken(dtd_parser *p, const ichar *in, dtd_symbol **id)
{ ichar buf[MAXNMLEN];
ichar *o = buf;
ichar *e = &buf[MAXNMLEN]-1;
dtd *dtd = p->dtd;
in = iskip_layout(dtd, in);
if ( !HasClass(dtd, *in, CH_NAME) )
return NULL;
if ( dtd->case_sensitive )
{ while( HasClass(dtd, *in, CH_NAME) && o < e )
*o++ = *in++;
} else
{ while( HasClass(dtd, *in, CH_NAME) && o < e )
*o++ = towlower(*in++);
}
if ( o == e )
{ gripe(p, ERC_REPRESENTATION, L"NMTOKEN too long");
return NULL;
}
*o = '\0';
*id = dtd_add_symbol(dtd, buf);
return iskip_layout(dtd, in);
}
static const ichar *
itake_nutoken(dtd_parser *p, const ichar *in, dtd_symbol **id)
{ ichar buf[MAXNMLEN];
ichar *o = buf;
ichar *e = &buf[MAXNMLEN]-1;
dtd *dtd = p->dtd;
in = iskip_layout(dtd, in);
if ( !HasClass(dtd, *in, CH_DIGIT) )
return NULL;
if ( dtd->case_sensitive )
{ while( HasClass(dtd, *in, CH_NAME) && o < e )
*o++ = *in++;
} else