forked from SWI-Prolog/packages-sgml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgml2pl.c
2694 lines (2175 loc) · 63.4 KB
/
sgml2pl.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): 1985-2013, University of Amsterdam
VU University Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _ISOC99_SOURCE 1 /* fwprintf(), etc prototypes */
#ifdef __WINDOWS__
#include <windows.h>
#endif
#define DTD_MINOR_ERRORS 1 /* get detailed errors */
#include <stdio.h>
#include "dtd.h"
#include "catalog.h"
#include "model.h"
#include "util.h"
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <errno.h>
#include "error.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <wctype.h>
#define streq(s1, s2) (strcmp(s1, s2) == 0)
#define MAX_ERRORS 50
#define MAX_WARNINGS 50
#define ENDSNUL ((size_t)-1)
install_t install( void );
install_t uninstall( void );
/*******************************
* PARSER CONTEXT DATA *
*******************************/
#define PD_MAGIC 0x36472ba1 /* just a number */
typedef enum
{ SA_FILE = 0, /* Stop at end-of-file */
SA_INPUT, /* Do not complete input */
SA_ELEMENT, /* Stop after first element */
SA_CONTENT, /* Stop after close */
SA_DECL /* Stop after declaration */
} stopat;
typedef enum
{ EM_QUIET = 0, /* Suppress messages */
EM_PRINT, /* Print message */
EM_STYLE /* include style-messages */
} errormode;
typedef struct _env
{ term_t tail;
struct _env *parent;
} env;
typedef struct _parser_data
{ int magic; /* PD_MAGIC */
dtd_parser *parser; /* parser itself */
int warnings; /* #warnings seen */
int errors; /* #errors seen */
int max_errors; /* error limit */
int max_warnings; /* warning limit */
errormode error_mode; /* how to handle errors */
int positions; /* report file-positions */
term_t exception; /* pending exception from callback */
predicate_t on_begin; /* begin element */
predicate_t on_end; /* end element */
predicate_t on_cdata; /* cdata */
predicate_t on_entity; /* entity */
predicate_t on_pi; /* processing instruction */
predicate_t on_xmlns; /* xmlns */
predicate_t on_urlns; /* url --> namespace */
predicate_t on_error; /* errors */
predicate_t on_decl; /* declarations */
stopat stopat; /* Where to stop */
int stopped; /* Environment is complete */
IOSTREAM* source; /* Where we are reading from */
term_t list; /* output term (if any) */
term_t tail; /* tail of the list */
env *stack; /* environment stack */
int free_on_close; /* sgml_free parser on close */
} parser_data;
/*******************************
* CONSTANTS *
*******************************/
static functor_t FUNCTOR_and2;
static functor_t FUNCTOR_bar2;
static functor_t FUNCTOR_comma2;
static functor_t FUNCTOR_default1;
static functor_t FUNCTOR_dialect1;
static functor_t FUNCTOR_document1;
static functor_t FUNCTOR_dtd1;
static functor_t FUNCTOR_dtd2;
static functor_t FUNCTOR_element3;
static functor_t FUNCTOR_entity1;
static functor_t FUNCTOR_equal2;
static functor_t FUNCTOR_file1;
static functor_t FUNCTOR_file4;
static functor_t FUNCTOR_dstream_position4;
static functor_t FUNCTOR_fixed1;
static functor_t FUNCTOR_line1;
static functor_t FUNCTOR_linepos1;
static functor_t FUNCTOR_list1;
static functor_t FUNCTOR_max_errors1;
static functor_t FUNCTOR_syntax_error1;
static functor_t FUNCTOR_error2;
static functor_t FUNCTOR_nameof1;
static functor_t FUNCTOR_notation1;
static functor_t FUNCTOR_omit2;
static functor_t FUNCTOR_opt1;
static functor_t FUNCTOR_plus1;
static functor_t FUNCTOR_rep1;
static functor_t FUNCTOR_sgml_parser1;
static functor_t FUNCTOR_parse1;
static functor_t FUNCTOR_source1;
static functor_t FUNCTOR_content_length1;
static functor_t FUNCTOR_call2;
static functor_t FUNCTOR_charpos1;
static functor_t FUNCTOR_charpos2;
static functor_t FUNCTOR_ns2; /* :/2 */
static functor_t FUNCTOR_space1;
static functor_t FUNCTOR_pi1;
static functor_t FUNCTOR_sdata1;
static functor_t FUNCTOR_ndata1;
static functor_t FUNCTOR_number1;
static functor_t FUNCTOR_syntax_errors1;
static functor_t FUNCTOR_xml_no_ns1;
static functor_t FUNCTOR_minus2;
static functor_t FUNCTOR_positions1;
static functor_t FUNCTOR_position1;
static functor_t FUNCTOR_event_class1;
static functor_t FUNCTOR_doctype1;
static functor_t FUNCTOR_allowed1;
static functor_t FUNCTOR_context1;
static functor_t FUNCTOR_defaults1;
static functor_t FUNCTOR_shorttag1;
static functor_t FUNCTOR_case_sensitive_attributes1;
static functor_t FUNCTOR_qualify_attributes1;
static functor_t FUNCTOR_encoding1;
static functor_t FUNCTOR_xmlns1;
static functor_t FUNCTOR_xmlns2;
static atom_t ATOM_true;
static atom_t ATOM_false;
static atom_t ATOM_cdata;
static atom_t ATOM_rcdata;
static atom_t ATOM_pcdata;
static atom_t ATOM_empty;
static atom_t ATOM_any;
static atom_t ATOM_position;
#define mkfunctor(n, a) PL_new_functor(PL_new_atom(n), a)
static void
initConstants(void)
{
FUNCTOR_sgml_parser1 = mkfunctor("sgml_parser", 1);
FUNCTOR_equal2 = mkfunctor("=", 2);
FUNCTOR_dtd1 = mkfunctor("dtd", 1);
FUNCTOR_element3 = mkfunctor("element", 3);
FUNCTOR_entity1 = mkfunctor("entity", 1);
FUNCTOR_document1 = mkfunctor("document", 1);
FUNCTOR_dtd2 = mkfunctor("dtd", 2);
FUNCTOR_omit2 = mkfunctor("omit", 2);
FUNCTOR_and2 = mkfunctor("&", 2);
FUNCTOR_comma2 = mkfunctor(",", 2);
FUNCTOR_bar2 = mkfunctor("|", 2);
FUNCTOR_opt1 = mkfunctor("?", 1);
FUNCTOR_rep1 = mkfunctor("*", 1);
FUNCTOR_plus1 = mkfunctor("+", 1);
FUNCTOR_default1 = mkfunctor("default", 1);
FUNCTOR_fixed1 = mkfunctor("fixed", 1);
FUNCTOR_list1 = mkfunctor("list", 1);
FUNCTOR_nameof1 = mkfunctor("nameof", 1);
FUNCTOR_notation1 = mkfunctor("notation", 1);
FUNCTOR_file1 = mkfunctor("file", 1);
FUNCTOR_file4 = mkfunctor("file", 4);
FUNCTOR_line1 = mkfunctor("line", 1);
FUNCTOR_linepos1 = mkfunctor("linepos", 1);
FUNCTOR_dialect1 = mkfunctor("dialect", 1);
FUNCTOR_max_errors1 = mkfunctor("max_errors", 1);
FUNCTOR_parse1 = mkfunctor("parse", 1);
FUNCTOR_source1 = mkfunctor("source", 1);
FUNCTOR_content_length1= mkfunctor("content_length", 1);
FUNCTOR_call2 = mkfunctor("call", 2);
FUNCTOR_charpos1 = mkfunctor("charpos", 1);
FUNCTOR_charpos2 = mkfunctor("charpos", 2);
FUNCTOR_ns2 = mkfunctor(":", 2);
FUNCTOR_space1 = mkfunctor("space", 1);
FUNCTOR_pi1 = mkfunctor("pi", 1);
FUNCTOR_sdata1 = mkfunctor("sdata", 1);
FUNCTOR_ndata1 = mkfunctor("ndata", 1);
FUNCTOR_number1 = mkfunctor("number", 1);
FUNCTOR_syntax_errors1 = mkfunctor("syntax_errors", 1);
FUNCTOR_syntax_error1 = mkfunctor("syntax_error", 1);
FUNCTOR_error2 = mkfunctor("error", 2);
FUNCTOR_xml_no_ns1 = mkfunctor("xml_no_ns", 1);
FUNCTOR_minus2 = mkfunctor("-", 2);
FUNCTOR_position1 = mkfunctor("position", 1);
FUNCTOR_positions1 = mkfunctor("positions", 1);
FUNCTOR_event_class1 = mkfunctor("event_class", 1);
FUNCTOR_doctype1 = mkfunctor("doctype", 1);
FUNCTOR_allowed1 = mkfunctor("allowed", 1);
FUNCTOR_context1 = mkfunctor("context", 1);
FUNCTOR_defaults1 = mkfunctor("defaults", 1);
FUNCTOR_shorttag1 = mkfunctor("shorttag", 1);
FUNCTOR_case_sensitive_attributes1 = mkfunctor("case_sensitive_attributes", 1);
FUNCTOR_qualify_attributes1 = mkfunctor("qualify_attributes", 1);
FUNCTOR_encoding1 = mkfunctor("encoding", 1);
FUNCTOR_xmlns1 = mkfunctor("xmlns", 1);
FUNCTOR_xmlns2 = mkfunctor("xmlns", 2);
FUNCTOR_dstream_position4 = PL_new_functor(PL_new_atom("$stream_position"), 4);
ATOM_true = PL_new_atom("true");
ATOM_false = PL_new_atom("false");
ATOM_cdata = PL_new_atom("cdata");
ATOM_rcdata = PL_new_atom("rcdata");
ATOM_pcdata = PL_new_atom("#pcdata");
ATOM_empty = PL_new_atom("empty");
ATOM_any = PL_new_atom("any");
ATOM_position = PL_new_atom("#position");
}
/*******************************
* ACCESS *
*******************************/
static int
unify_parser(term_t parser, dtd_parser *p)
{ return PL_unify_term(parser, PL_FUNCTOR, FUNCTOR_sgml_parser1,
PL_POINTER, p);
}
static int
get_parser(term_t parser, dtd_parser **p)
{ if ( PL_is_functor(parser, FUNCTOR_sgml_parser1) )
{ term_t a = PL_new_term_ref();
void *ptr;
_PL_get_arg(1, parser, a);
if ( PL_get_pointer(a, &ptr) )
{ dtd_parser *tmp = ptr;
if ( tmp->magic == SGML_PARSER_MAGIC )
{ *p = tmp;
return TRUE;
}
return sgml2pl_error(ERR_EXISTENCE, "sgml_parser", parser);
}
}
return sgml2pl_error(ERR_TYPE, "sgml_parser", parser);
}
static int
unify_dtd(term_t t, dtd *dtd)
{ if ( dtd->doctype )
return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_dtd2,
PL_POINTER, dtd,
PL_NWCHARS, (size_t)-1, dtd->doctype);
else
return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_dtd2,
PL_POINTER, dtd,
PL_VARIABLE);
}
static int
get_dtd(term_t t, dtd **dtdp)
{ if ( PL_is_functor(t, FUNCTOR_dtd2) )
{ term_t a = PL_new_term_ref();
void *ptr;
_PL_get_arg(1, t, a);
if ( PL_get_pointer(a, &ptr) )
{ dtd *tmp = ptr;
if ( tmp->magic == SGML_DTD_MAGIC )
{ *dtdp = tmp;
return TRUE;
}
return sgml2pl_error(ERR_EXISTENCE, "dtd", t);
}
}
return sgml2pl_error(ERR_TYPE, "dtd", t);
}
/*******************************
* NEW/FREE *
*******************************/
static foreign_t
pl_new_sgml_parser(term_t ref, term_t options)
{ term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(options);
term_t tmp = PL_new_term_ref();
dtd *dtd = NULL;
dtd_parser *p;
while ( PL_get_list(tail, head, tail) )
{ if ( PL_is_functor(head, FUNCTOR_dtd1) )
{ _PL_get_arg(1, head, tmp);
if ( PL_is_variable(tmp) ) /* dtd(X) */
{ dtd = new_dtd(NULL); /* no known doctype */
dtd->references++;
unify_dtd(tmp, dtd);
} else if ( !get_dtd(tmp, &dtd) )
return FALSE;
}
}
if ( !PL_get_nil(tail) )
return sgml2pl_error(ERR_TYPE, "list", tail);
p = new_dtd_parser(dtd);
return unify_parser(ref, p);
}
static foreign_t
pl_free_sgml_parser(term_t parser)
{ dtd_parser *p;
if ( get_parser(parser, &p) )
{ free_dtd_parser(p);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_new_dtd(term_t doctype, term_t ref)
{ ichar *dt;
dtd *dtd;
if ( !PL_get_wchars(doctype, NULL, &dt, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( !(dtd=new_dtd(dt)) )
return FALSE;
dtd->references++;
return unify_dtd(ref, dtd);
}
static foreign_t
pl_free_dtd(term_t t)
{ dtd *dtd;
if ( get_dtd(t, &dtd) )
{ free_dtd(dtd);
return TRUE;
}
return FALSE;
}
/*******************************
* DATA EXCHANGE *
*******************************/
static int
put_atom_wchars(term_t t, wchar_t const *s)
{ PL_put_variable(t);
return PL_unify_wchars(t, PL_ATOM, ENDSNUL, s);
}
/*******************************
* PROPERTIES *
*******************************/
static foreign_t
pl_set_sgml_parser(term_t parser, term_t option)
{ dtd_parser *p;
if ( !get_parser(parser, &p) )
return FALSE;
if ( PL_is_functor(option, FUNCTOR_file1) )
{ term_t a = PL_new_term_ref();
wchar_t *file;
dtd_symbol *fs;
_PL_get_arg(1, option, a);
if ( !PL_get_wchars(a, NULL, &file, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
fs = dtd_add_symbol(p->dtd, file); /* symbol will be freed */
set_file_dtd_parser(p, IN_FILE, fs->name);
} else if ( PL_is_functor(option, FUNCTOR_line1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( !PL_get_integer_ex(a, &p->location.line) )
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_linepos1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( !PL_get_integer_ex(a, &p->location.linepos) )
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_charpos1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( !PL_get_long_ex(a, &p->location.charpos) )
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_position1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( PL_is_functor(a, FUNCTOR_dstream_position4) )
{ term_t arg = PL_new_term_ref();
if ( !PL_get_arg(1,a,arg) || !PL_get_long_ex(arg, &p->location.charpos) ||
!PL_get_arg(2,a,arg) || !PL_get_integer_ex(arg, &p->location.line) ||
!PL_get_arg(3,a,arg) || !PL_get_integer_ex(arg, &p->location.linepos))
return FALSE;
} else
return PL_type_error("stream_position", a);
} else if ( PL_is_functor(option, FUNCTOR_dialect1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "xml") )
set_dialect_dtd(p->dtd, DL_XML);
else if ( streq(s, "xmlns") )
set_dialect_dtd(p->dtd, DL_XMLNS);
else if ( streq(s, "sgml") )
set_dialect_dtd(p->dtd, DL_SGML);
else if ( streq(s, "html") || streq(s, "html4") )
set_dialect_dtd(p->dtd, DL_HTML);
else if ( streq(s, "html5") )
set_dialect_dtd(p->dtd, DL_HTML5);
else
return sgml2pl_error(ERR_DOMAIN, "sgml_dialect", a);
} else if ( PL_is_functor(option, FUNCTOR_space1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "preserve") )
p->dtd->space_mode = SP_PRESERVE;
else if ( streq(s, "default") )
p->dtd->space_mode = SP_DEFAULT;
else if ( streq(s, "remove") )
p->dtd->space_mode = SP_REMOVE;
else if ( streq(s, "sgml") )
p->dtd->space_mode = SP_SGML;
else
return sgml2pl_error(ERR_DOMAIN, "space", a);
} else if ( PL_is_functor(option, FUNCTOR_defaults1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
if ( val )
p->flags &= ~SGML_PARSER_NODEFS;
else
p->flags |= SGML_PARSER_NODEFS;
} else if ( PL_is_functor(option, FUNCTOR_qualify_attributes1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
if ( val )
p->flags |= SGML_PARSER_QUALIFY_ATTS;
else
p->flags &= ~SGML_PARSER_QUALIFY_ATTS;
} else if ( PL_is_functor(option, FUNCTOR_shorttag1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
set_option_dtd(p->dtd, OPT_SHORTTAG, val);
} else if ( PL_is_functor(option, FUNCTOR_case_sensitive_attributes1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
set_option_dtd(p->dtd, OPT_CASE_SENSITIVE_ATTRIBUTES, val);
} else if ( PL_is_functor(option, FUNCTOR_number1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "token") )
p->dtd->number_mode = NU_TOKEN;
else if ( streq(s, "integer") )
p->dtd->number_mode = NU_INTEGER;
else
return sgml2pl_error(ERR_DOMAIN, "number", a);
} else if ( PL_is_functor(option, FUNCTOR_encoding1) )
{ term_t a = PL_new_term_ref();
char *val;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &val) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( !xml_set_encoding(p, val) )
return sgml2pl_error(ERR_DOMAIN, "encoding", a);
} else if ( PL_is_functor(option, FUNCTOR_doctype1) )
{ term_t a = PL_new_term_ref();
ichar *s;
_PL_get_arg(1, option, a);
if ( PL_is_variable(a) )
{ p->enforce_outer_element = NULL;
} else
{ if ( !PL_get_wchars(a, NULL, &s, CVT_ATOM) )
return sgml2pl_error(ERR_TYPE, "atom_or_variable", a);
p->enforce_outer_element = dtd_add_symbol(p->dtd, s);
}
} else if ( PL_is_functor(option, FUNCTOR_xmlns1) )
{ term_t a = PL_new_term_ref();
ichar ns[1] = {0};
ichar *uri;
_PL_get_arg(1, option, a);
if ( !PL_get_wchars(a, NULL, &uri, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
xmlns_push(p, ns, uri);
} else if ( PL_is_functor(option, FUNCTOR_xmlns2) )
{ term_t a = PL_new_term_ref();
ichar *ns, *uri;
_PL_get_arg(1, option, a);
if ( !PL_get_wchars(a, NULL, &ns, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
_PL_get_arg(2, option, a);
if ( !PL_get_wchars(a, NULL, &uri, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
xmlns_push(p, ns, uri);
} else
return sgml2pl_error(ERR_DOMAIN, "sgml_parser_option", option);
return TRUE;
}
static dtd_srcloc *
file_location(dtd_parser *p, dtd_srcloc *l)
{ while(l->parent && l->type != IN_FILE)
l = l->parent;
return l;
}
static foreign_t
pl_get_sgml_parser(term_t parser, term_t option)
{ dtd_parser *p;
if ( !get_parser(parser, &p) )
return FALSE;
if ( PL_is_functor(option, FUNCTOR_charpos1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_integer(a, file_location(p, &p->startloc)->charpos);
} else if ( PL_is_functor(option, FUNCTOR_line1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_integer(a, file_location(p, &p->startloc)->line);
} else if ( PL_is_functor(option, FUNCTOR_charpos2) )
{ term_t a = PL_new_term_ref();
if ( PL_get_arg(1, option, a) &&
PL_unify_integer(a, file_location(p, &p->startloc)->charpos) &&
PL_get_arg(2, option, a) &&
PL_unify_integer(a, file_location(p, &p->location)->charpos) )
return TRUE;
else
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_file1) )
{ dtd_srcloc *l = file_location(p, &p->location);
if ( l->type == IN_FILE && l->name.file )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_wchars(a, PL_ATOM, ENDSNUL, l->name.file);
}
} else if ( PL_is_functor(option, FUNCTOR_source1) )
{ parser_data *pd = p->closure;
if ( pd && pd->magic == PD_MAGIC && pd->source )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_stream(a, pd->source);
}
} else if ( PL_is_functor(option, FUNCTOR_dialect1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
switch(p->dtd->dialect)
{ case DL_SGML:
return PL_unify_atom_chars(a, "sgml");
case DL_HTML:
return PL_unify_atom_chars(a, "html");
case DL_HTML5:
return PL_unify_atom_chars(a, "html5");
case DL_XML:
return PL_unify_atom_chars(a, "xml");
case DL_XMLNS:
return PL_unify_atom_chars(a, "xmlns");
}
} else if ( PL_is_functor(option, FUNCTOR_event_class1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
switch(p->event_class)
{ case EV_EXPLICIT:
return PL_unify_atom_chars(a, "explicit");
case EV_OMITTED:
return PL_unify_atom_chars(a, "omitted");
case EV_SHORTTAG:
return PL_unify_atom_chars(a, "shorttag");
case EV_SHORTREF:
return PL_unify_atom_chars(a, "shortref");
}
} else if ( PL_is_functor(option, FUNCTOR_dtd1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return unify_dtd(a, p->dtd);
} else if ( PL_is_functor(option, FUNCTOR_doctype1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( p->enforce_outer_element )
return PL_unify_wchars(a, PL_ATOM, ENDSNUL,
p->enforce_outer_element->name);
else
return TRUE; /* leave variable */
} else if ( PL_is_functor(option, FUNCTOR_allowed1) )
{ term_t tail, head, tmp;
sgml_environment *env = p->environments;
if ( !(tail = PL_new_term_ref()) ||
!(head = PL_new_term_ref()) ||
!(tmp = PL_new_term_ref()) )
return FALSE;
_PL_get_arg(1, option, tail);
if ( env )
{ for( ; env; env = env->parent)
{ dtd_element *buf[256]; /* MAX_VISITED! */
int n = sizeof(buf)/sizeof(dtd_element *); /* not yet used! */
int i;
state_allows_for(env->state, buf, &n);
for(i=0; i<n; i++)
{ int rc;
if ( buf[i] == CDATA_ELEMENT )
rc = PL_put_atom_chars(tmp, "#pcdata");
else
rc = put_atom_wchars(tmp, buf[i]->name->name);
if ( !rc ||
!PL_unify_list(tail, head, tail) ||
!PL_unify(head, tmp) )
return FALSE;
}
if ( !env->element->structure ||
!env->element->structure->omit_close )
break;
}
} else if ( p->enforce_outer_element )
{ put_atom_wchars(tmp, p->enforce_outer_element->name);
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, tmp) )
return FALSE;
}
return PL_unify_nil(tail);
} else if ( PL_is_functor(option, FUNCTOR_context1) )
{ term_t tail = PL_new_term_ref();
term_t head = PL_new_term_ref();
term_t tmp = PL_new_term_ref();
sgml_environment *env = p->environments;
_PL_get_arg(1, option, tail);
for( ; env; env = env->parent)
{ put_atom_wchars(tmp, env->element->name->name);
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, tmp) )
return FALSE;
}
return PL_unify_nil(tail);
} else
return sgml2pl_error(ERR_DOMAIN, "parser_option", option);
return FALSE;
}
static int
call_prolog(parser_data *pd, predicate_t pred, term_t av)
{ qid_t qid = PL_open_query(NULL, PL_Q_PASS_EXCEPTION, pred, av);
int rc = PL_next_solution(qid);
PL_close_query(qid);
if ( rc )
{ pd->exception = FALSE;
} else
{ if ( (pd->exception = PL_exception(0)) )
pd->stopped = TRUE;
}
return rc;
}
static void
end_frame(fid_t fid, term_t ex)
{ if ( ex )
PL_close_foreign_frame(fid);
else
PL_discard_foreign_frame(fid);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
put_url(dtd_parser *p, term_t t, const ichar *url)
Store the url-part of a name-space qualifier in term. We call
xml:xmlns(-Canonical, +Full) trying to resolve the specified
namespace to an internal canonical namespace.
We do a little caching as there will generally be only a very
small pool of urls in use. We assume the url-pointers we get
life for the time of the parser. It might be possible that
multiple url pointers point to the same url, but this only clobbers
the cache a little.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define URL_CACHE 4 /* # entries cached */
typedef struct
{ const ichar *url; /* URL pointer */
atom_t canonical;
} url_cache;
static url_cache cache[URL_CACHE];
static void
reset_url_cache(void)
{ int i;
url_cache *c = cache;
for(i=0; i<URL_CACHE; i++)
{ c[i].url = NULL;
if ( c[i].canonical )
PL_unregister_atom(c[i].canonical);
c[i].canonical = 0;
}
}
WUNUSED static int
put_url(dtd_parser *p, term_t t, const ichar *url)
{ parser_data *pd = p->closure;
fid_t fid;
int i;
if ( !pd->on_urlns )
return put_atom_wchars(t, url);
for(i=0; i<URL_CACHE; i++)
{ if ( cache[i].url == url ) /* cache hit */
{ if ( cache[i].canonical ) /* and a canonical value */
return PL_put_atom(t, cache[i].canonical);
else
return put_atom_wchars(t, url);
}
}
/* shift the cache */
i = URL_CACHE-1;
if ( cache[i].canonical )
PL_unregister_atom(cache[i].canonical);
for(i=URL_CACHE-1; i>0; i--)
cache[i] = cache[i-1];
cache[0].url = url;
cache[0].canonical = 0;
if ( (fid = PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(3);
atom_t a;
rc = (put_atom_wchars(av+0, url) &&
unify_parser(av+2, p));
if ( rc &&
PL_call_predicate(NULL, PL_Q_NORMAL, pd->on_urlns, av) &&
PL_get_atom(av+1, &a) )
{ PL_register_atom(a);
cache[0].canonical = a;
PL_put_atom(t, a);
} else if ( rc )
{ rc = put_atom_wchars(t, url);
}
PL_discard_foreign_frame(fid);
return rc;
}
return FALSE;
}
WUNUSED static int
put_attribute_name(dtd_parser *p, term_t t, dtd_symbol *nm)
{ const ichar *url, *local;
if ( p->dtd->dialect == DL_XMLNS )
{ xmlns_resolve_attribute(p, nm, &local, &url);
if ( url )
{ term_t av;
return ( (av=PL_new_term_refs(2)) &&
put_url(p, av+0, url) &&
put_atom_wchars(av+1, local) &&
PL_cons_functor_v(t, FUNCTOR_ns2, av) );
} else
return put_atom_wchars(t, local);
} else
return put_atom_wchars(t, nm->name);
}
WUNUSED static int
put_element_name(dtd_parser *p, term_t t, dtd_element *e)
{ const ichar *url, *local;
if ( p->dtd->dialect == DL_XMLNS )
{ assert(p->environments->element == e);
xmlns_resolve_element(p, &local, &url);
if ( url )
{ term_t av;
return ( (av=PL_new_term_refs(2)) &&
put_url(p, av+0, url) &&
put_atom_wchars(av+1, local) &&
PL_cons_functor_v(t, FUNCTOR_ns2, av) );
} else
return put_atom_wchars(t, local);
} else
return put_atom_wchars(t, e->name->name);
}
static ichar *
istrblank(const ichar *s)
{ for( ; *s; s++ )
{ if ( iswspace(*s) )
return (ichar *)s;
}
return NULL;
}
static int
unify_listval(dtd_parser *p,
term_t t, attrtype type, size_t len, const ichar *text)
{ if ( type == AT_NUMBERS && p->dtd->number_mode == NU_INTEGER )
{ wchar_t *e;
#if SIZEOF_LONG == 4 && defined(HAVE_WCSTOLL)
int64_t v = wcstoll(text, &e, 10);
if ( (size_t)(e-text) == len && errno != ERANGE )
return PL_unify_int64(t, v);
#else
long v = wcstol(text, &e, 10);
if ( (size_t)(e-text) == len && errno != ERANGE )
return PL_unify_integer(t, v);
#endif
/* TBD: Error!? */
}
return PL_unify_wchars(t, PL_ATOM, len, text);
}
static int
put_att_text(term_t t, sgml_attribute *a)
{ if ( a->value.textW )
{ PL_put_variable(t);
return PL_unify_wchars(t, PL_ATOM, a->value.number, a->value.textW);
} else
return FALSE;
}
static int
put_attribute_value(dtd_parser *p, term_t t, sgml_attribute *a)
{ switch(a->definition->type)
{ case AT_CDATA:
return put_att_text(t, a);
case AT_NUMBER:
{ if ( !put_att_text(t, a) )
return PL_put_integer(t, a->value.number);
return TRUE;
}
default: /* multi-valued attribute */
{ if ( a->definition->islist && a->value.textW )
{ term_t tail, head;
const ichar *val = a->value.textW;
const ichar *e;
PL_put_variable(t);