-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgml.c
2262 lines (2071 loc) · 57.2 KB
/
sgml.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
/*
System: Structured text retrieval tool sgrep.
Module: sgml.c
Author: Pekka Kilpeläinen & Jani Jaakkola
Description: SGML/XML/HTML scanner, word tokenizer, character decoder
Version history: Original version December 1998 by JJ & PK
Copyright: University of Helsinki, Dept. of Computer Science
*/
#define SGREP_LIBRARY
#include <string.h>
#include <ctype.h>
#include "sgrep.h"
/* These come from the XML-specs */
#define SGML_NameChars ("-a-zA-Z0-9._:")
#define SGML_NameStartChars ("a-zA-Z_:")
#define DEFAULT_WordChars ("a-zA-Z")
#define IN_CLIST(LIST,CHAR) ( \
( (LIST)->bitmap[(CHAR)/sizeof(unsigned long int)] ) & \
( (1UL) << ((CHAR)%sizeof(unsigned long int)) ) )
#define ADD_CLIST(LIST,CHAR) ( \
((LIST)->bitmap[(CHAR)/sizeof(unsigned long int)]) |= \
( (1UL) << ((CHAR)%sizeof(unsigned long int)) ) )
typedef struct CharacterListStruct {
unsigned long int bitmap[65536/sizeof(unsigned long int)];
SgrepData *sgrep;
} CharacterList;
/* FIXME: check for string length */
#define MAX_TERM_SIZE 256
#define TERM_PUSH(STRING,CHAR) do { \
if (string_len(STRING)<MAX_TERM_SIZE) {string_push((STRING),(CHAR)); } \
} while (0);
/*
* datatypes for the SGML-scanner
*/
/*
* Implementation of a simple SGML scanner starts here
* (simple finite automaton)
*/
/* FIXME: index.c has a different name for same constant */
enum SGMLState { SGML_PCDATA, SGML_WORD,
SGML_ENTITY_OPEN,SGML_ENTITY,SGML_PCDATA_ENTITY,
SGML_WORD_ENTITY,
SGML_CHARACTER_REFERENCE_OPEN,
SGML_DECIMAL_CHARACTER_REFERENCE,
SGML_HEX_CHARACTER_REFERENCE,
SGML_CHARACTER_REFERENCE_CLOSE,
SGML_STAGO, SGML_GI, SGML_W_ATTNAME,
SGML_ATTNAME,
SGML_W_ATTEQUAL, SGML_W_ATTVALUE,
SGML_ATTVALUE, SGML_ATTVALUE_DQUOTED,
SGML_ATTVALUE_SQUOTED, SGML_ATTRIBUTE_END,
SGML_STAGC, SGML_END_TAG, SGML_W_ETAGC,
SGML_DECLARATION_START,SGML_DOCTYPE_DECLARATION,
SGML_COMMENT_START, SGML_COMMENT, SGML_COMMENT_WORD,
SGML_COMMENT_END1, SGML_COMMENT_END2,
SGML_PI, SGML_PI_END,
SGML_MARKED_SECTION_START, SGML_MARKED_SECTION_START2,
SGML_CDATA_MARKED_SECTION, SGML_CDATA_MARKED_SECTION_WORD,
SGML_CDATA_MARKED_SECTION_END1,
SGML_CDATA_MARKED_SECTION_END2,
SGML_DOCTYPE, SGML_DOCTYPE_EXTERNAL,
SGML_DOCTYPE_PUBLIC, SGML_DOCTYPE_SYSTEM,
SGML_WAITING_LITERAL,
SGML_LITERAL_START,SGML_LITERAL_SQUOTED,SGML_LITERAL_DQUOTED,
SGML_DOCTYPE_PUBLIC_ID_START, SGML_DOCTYPE_PUBLIC_ID,
SGML_DOCTYPE_SYSTEM_ID_START, SGML_DOCTYPE_SYSTEM_ID,
SGML_DOCTYPE_INTERNAL_START,
SGML_DOCTYPE_INTERNAL,
SGML_INTERNAL_DECLARATION_START1,
SGML_INTERNAL_DECLARATION_START2,
SGML_PEREFERENCE,
SGML_INTERNAL_DECLARATION_NAME,
SGML_ENTITY_DECLARATION,
SGML_ENTITY_DECLARATION_NAME,
SGML_ENTITY_DEFINITION,
SGML_ENTITY_DEFINITION_TYPE,
SGML_LITERAL_ENTITY,
SGML_GENERAL_ENTITY_DEFINITION_DQUOTED,
SGML_GENERAL_ENTITY_DEFINITION_SQUOTED,
SGML_ENTITY_DEFINITION_PUBLIC_ID,
SGML_WAITING_ENTITY_DEFINITION_SYSTEM_ID,
SGML_ENTITY_DEFINITION_SYSTEM_ID,
SGML_ENTITY_DEFINITION_NDATA,
SGML_ENTITY_DEFINITION_NDATA2,
SGML_ENTITY_DEFINITION_NDATA_NAME,
SGML_ENTITY_DEFINITION_END,
SGML_ELEMENT_TYPE_DECLARATION,
SGML_ATTLIST_DECLARATION,
SGML_NOTATION_DECLARATION,
SGML_DOCTYPE_END,
SGML_RESERVED_WORD
};
typedef struct ElementStackStruct {
char *gi;
int start;
int end;
struct ElementStackStruct *prev;
} ElementStack;
enum EncoderState {
EIGHT_BIT,
UTF8_1, UTF8_2, UTF8_3_1, UTF8_3_2,
UTF16_1,
UTF16_BIG_START,UTF16_BIG,
UTF16_SMALL_START,UTF16_SMALL
};
typedef struct EncoderStruct {
enum EncoderState estate;
int char1;
int char2;
int prev;
} Encoder;
struct SGMLScannerStruct {
SgrepData *sgrep;
FileList *file_list;
int file_num;
/* The Encoder */
Encoder encoder;
/* Scanner "configuration" */
ScannerType type;
CharacterList *name_start_chars;
CharacterList *name_chars;
CharacterList *word_chars;
CharacterList *indexed_chars;
int ignore_case;
int include_system_entities;
/* Maintaining the element stack */
int maintain_element_stack;
ElementStack *top;
RegionList *element_list;
/* Scanner state */
int parse_errors;
struct PHRASE_NODE *phrase_list;
int words;
int word_end;
SgrepString *word;
/* Start and end tag */
int tags;
SgrepString *gi;
/* Attributes */
int anames;
SgrepString *aname;
int avals;
SgrepString *aval;
/* Comments */
int comments;
int comment_words;
SgrepString *comment_word;
int markeds;
int doctypes;
int doctype_declarations;
int internal_declarations;
int entity_has_systemid;
int entity_is_ndata;
int entitys; /* Start of entity reference */
int character_reference;
SgrepString *name;
int name2s;
SgrepString *name2;
int literals;
SgrepString *literal;
int publici;
int systemi;
SgrepString *pi;
enum SGMLState state;
/* This isn't a real stack of context free language parser;
* it's just a trick for some state reuse */
#define SGML_SCANNER_STACK_SIZE 10
enum SGMLState state_stack[SGML_SCANNER_STACK_SIZE];
int state_stack_ptr;
void (*entry)(struct SGMLScannerStruct *state,
const char *str, int start, int end);
void *data;
int failed;
};
/*
* The XML character class definitions. These come straight from the
* XML spesification
*/
const char *XML_BaseChar=
"\\#x0041-\\#x005A\\#x0061-\\#x007A\\#x00C0-\\#x00D6"
"\\#x00D8-\\#x00F6\\#x00F8-\\#x00FF\\#x0100-\\#x0131"
"\\#x0134-\\#x013E\\#x0141-\\#x0148\\#x014A-\\#x017E"
"\\#x0180-\\#x01C3\\#x01CD-\\#x01F0\\#x01F4-\\#x01F5"
"\\#x01FA-\\#x0217\\#x0250-\\#x02A8\\#x02BB-\\#x02C1\\#x0386"
"\\#x0388-\\#x038A\\#x038C\\#x038E-\\#x03A1\\#x03A3-\\#x03CE"
"\\#x03D0-\\#x03D6\\#x03DA\\#x03DC\\#x03DE\\#x03E0"
"\\#x03E2-\\#x03F3\\#x0401-\\#x040C\\#x040E-\\#x044F"
"\\#x0451-\\#x045C\\#x045E-\\#x0481\\#x0490-\\#x04C4"
"\\#x04C7-\\#x04C8\\#x04CB-\\#x04CC\\#x04D0-\\#x04EB"
"\\#x04EE-\\#x04F5\\#x04F8-\\#x04F9\\#x0531-\\#x0556\\#x0559"
"\\#x0561-\\#x0586\\#x05D0-\\#x05EA\\#x05F0-\\#x05F2"
"\\#x0621-\\#x063A\\#x0641-\\#x064A\\#x0671-\\#x06B7"
"\\#x06BA-\\#x06BE\\#x06C0-\\#x06CE\\#x06D0-\\#x06D3\\#x06D5"
"\\#x06E5-\\#x06E6\\#x0905-\\#x0939\\#x093D\\#x0958-\\#x0961"
"\\#x0985-\\#x098C\\#x098F-\\#x0990\\#x0993-\\#x09A8"
"\\#x09AA-\\#x09B0\\#x09B2\\#x09B6-\\#x09B9\\#x09DC-\\#x09DD"
"\\#x09DF-\\#x09E1\\#x09F0-\\#x09F1\\#x0A05-\\#x0A0A"
"\\#x0A0F-\\#x0A10\\#x0A13-\\#x0A28\\#x0A2A-\\#x0A30"
"\\#x0A32-\\#x0A33\\#x0A35-\\#x0A36\\#x0A38-\\#x0A39"
"\\#x0A59-\\#x0A5C\\#x0A5E\\#x0A72-\\#x0A74\\#x0A85-\\#x0A8B"
"\\#x0A8D\\#x0A8F-\\#x0A91\\#x0A93-\\#x0AA8\\#x0AAA-\\#x0AB0"
"\\#x0AB2-\\#x0AB3\\#x0AB5-\\#x0AB9\\#x0ABD\\#x0AE0"
"\\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C\\#x114E\\#x1150"
"\\#x1154-\\#x1155\\#x1159\\#x115F-\\#x1161\\#x1163\\#x1165"
"\\#x1167\\#x1169\\#x116D-\\#x116E\\#x1172-\\#x1173\\#x1175"
"\\#x119E\\#x11A8\\#x11AB\\#x11AE-\\#x11AF\\#x11B7-\\#x11B8"
"\\#x11BA\\#x11BC-\\#x11C2\\#x11EB\\#x11F0\\#x11F9"
"\\#x1E00-\\#x1E9B\\#x1EA0-\\#x1EF9\\#x1F00-\\#x1F15"
"\\#x1F18-\\#x1F1D\\#x1F20-\\#x1F45\\#x1F48-\\#x1F4D"
"\\#x1F50-\\#x1F57\\#x1F59\\#x1F5B\\#x1F5D\\#x1F5F-\\#x1F7D"
"\\#x1F80-\\#x1FB4\\#x1FB6-\\#x1FBC\\#x1FBE\\#x1FC2-\\#x1FC4"
"\\#x1FC6-\\#x1FCC\\#x1FD0-\\#x1FD3\\#x1FD6-\\#x1FDB"
"\\#x1FE0-\\#x1FEC\\#x1FF2-\\#x1FF4\\#x1FF6-\\#x1FFC\\#x2126"
"\\#x212A-\\#x212B\\#x212E\\#x2180-\\#x2182\\#x3041-\\#x3094"
"\\#x30A1-\\#x30FA\\#x3105-\\#x312C\\#xAC00-\\#xD7A3"
;
const char *XML_Ideographic=
"\\#x4E00-\\#x9FA5\\#x3007\\#x3021-\\#x3029"
;
const char *XML_CombiningChar=
"\\#x0300-\\#x0345\\#x0360-\\#x0361\\#x0483-\\#x0486"
"\\#x0591-\\#x05A1\\#x05A3-\\#x05B9\\#x05BB-\\#x05BD\\#x05BF"
"\\#x05C1-\\#x05C2\\#x05C4\\#x064B-\\#x0652\\#x0670"
"\\#x06D6-\\#x06DC\\#x06DD-\\#x06DF\\#x06E0-\\#x06E4"
"\\#x06E7-\\#x06E8\\#x06EA-\\#x06ED\\#x0901-\\#x0903\\#x093C"
"\\#x093E-\\#x094C\\#x094D\\#x0951-\\#x0954\\#x0962-\\#x0963"
"\\#x0981-\\#x0983\\#x09BC\\#x09BE\\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-\\#x06F9"
"\\#x0966-\\#x096F\\#x09E6-\\#x09EF\\#x0A66-\\#x0A6F"
"\\#x0AE6-\\#x0AEF\\#x0B66-\\#x0B6F\\#x0BE7-\\#x0BEF"
"\\#x0C66-\\#x0C6F\\#x0CE6-\\#x0CEF\\#x0D66-\\#x0D6F"
"\\#x0E50-\\#x0E59\\#x0ED0-\\#x0ED9\\#x0F20-\\#x0F29"
;
const char *XML_Extender=
"\\#x00B7\\#x02D0\\#x02D1\\#x0387\\#x0640\\#x0E46\\#x0EC6"
"\\#x3005\\#x3031-\\#x3035\\#x309D-\\#x309E\\#x30FC-\\#x30FE"
;
/*
* Creates a new empty character list
*/
CharacterList *new_character_list(SgrepData *sgrep) {
CharacterList *a;
a=sgrep_new(CharacterList);
memset(a,0,sizeof(CharacterList));
a->sgrep=sgrep;
return a;
}
/*
* Parses a given character list string adding them to a CharacterList
*/
void character_list_add(CharacterList *a,const char *l) {
int i;
const unsigned char *list=(const unsigned char *)l;
int previous;
int expand_from;
int current;
SGREPDATA(a);
previous=-1;
expand_from=-1;
i=0;
while(list[i]) {
current=list[i];
i++;
if (current=='\\') {
if (list[i]=='-') {
/* Escape also \- */
i++;
current='-';
} else {
current=expand_backslash_escape(sgrep,list,&i);
}
} else if (current=='-' && i>1 && expand_from==-1) {
/* Mark the requirement to expand in next iteration */
expand_from=previous;
continue;
}
if (expand_from>=0 && current>=0) {
/* A region */
int j;
for(j=expand_from;j<=current;j++) {
ADD_CLIST(a,j);
}
} else if (current>=0) {
ADD_CLIST(a,current);
}
expand_from=-1;
previous=current;
current=-1;
}
if (expand_from>=0) {
sgrep_error(sgrep,"Character list '%s' contains a region with no endpoint\n",
list);
}
}
void pop_elements_to(SGMLScanner *state,
struct ElementStackStruct *p);
/* FIXME: needs hashing for speed */
void sgml_add_entry_to_gclist(SGMLScanner *state,
const char *phrase,int start, int end) {
struct PHRASE_NODE *n;
for(n=state->phrase_list;n!=NULL;n=n->next) {
if (n->phrase->s[n->phrase->length-1]=='*') {
/* Wildcard */
if (strncmp(n->phrase->s,phrase,n->phrase->length-1)==0) {
add_region(n->regions,start,end);
}
} else if (strcmp(n->phrase->s,phrase)==0) {
add_region(n->regions,start,end);
}
}
}
void sgml_add_entry_to_index(SGMLScanner *state,
const char *phrase,
int start, int end) {
if (phrase[0]=='@') {
add_region(state->element_list,start,end);
} else {
if (add_region_to_index((struct IndexWriterStruct *)state->data,
phrase,start,end)==SGREP_ERROR) {
state->failed=1;
}
}
}
void reset_encoder(SGMLScanner *sgmls, Encoder *encoder) {
SGREPDATA(sgmls);
switch(sgrep->default_encoding) {
case ENCODING_GUESS:
switch(sgmls->type) {
case SGML_SCANNER:
encoder->estate=EIGHT_BIT;
break;
case XML_SCANNER:
encoder->estate=UTF8_1;
break;
case TEXT_SCANNER:
encoder->estate=EIGHT_BIT;
break;
}
break;
case ENCODING_8BIT:
encoder->estate=EIGHT_BIT;
break;
case ENCODING_UTF8:
encoder->estate=UTF8_1;
break;
case ENCODING_UTF16:
encoder->estate=UTF8_1;
break;
}
encoder->prev=-1;
}
SGMLScanner *new_sgml_scanner_common(SgrepData *sgrep, FileList *file_list) {
SGMLScanner *scanner;
scanner=sgrep_new(SGMLScanner);
scanner->sgrep=sgrep;
scanner->file_list=file_list;
scanner->file_num=-1;
scanner->state_stack_ptr=0;
scanner->maintain_element_stack=1;
scanner->top=NULL;
scanner->element_list=NULL;
scanner->word_chars=new_character_list(sgrep);
switch(sgrep->scanner_type) {
case SGML_SCANNER:
scanner->name_start_chars=new_character_list(sgrep);
character_list_add(scanner->name_start_chars,SGML_NameStartChars);
scanner->name_chars=new_character_list(sgrep);
character_list_add(scanner->name_chars,SGML_NameChars);
break;
case XML_SCANNER:
/* NameStart characters */
scanner->name_start_chars=new_character_list(sgrep);
character_list_add(scanner->name_start_chars,XML_BaseChar);
character_list_add(scanner->name_start_chars,XML_Ideographic);
character_list_add(scanner->name_start_chars,SGML_NameStartChars);
/* Name characters */
scanner->name_chars=new_character_list(sgrep);
character_list_add(scanner->name_chars,XML_BaseChar);
character_list_add(scanner->name_chars,XML_Ideographic);
character_list_add(scanner->name_chars,SGML_NameChars);
break;
case TEXT_SCANNER:
scanner->name_start_chars=NULL;
scanner->name_chars=NULL;
break;
}
if (sgrep->word_chars) {
character_list_add(scanner->word_chars,sgrep->word_chars);
} else {
character_list_add(scanner->word_chars,XML_BaseChar);
character_list_add(scanner->word_chars,XML_Ideographic);
}
scanner->parse_errors=0;
scanner->type=sgrep->scanner_type;
scanner->ignore_case=sgrep->ignore_case;
scanner->include_system_entities=sgrep->include_system_entities;
scanner->state=SGML_PCDATA;
scanner->gi=new_string(sgrep,MAX_TERM_SIZE);
scanner->word=new_string(sgrep,MAX_TERM_SIZE);
TERM_PUSH(scanner->word,'w');
scanner->name2=new_string(sgrep,MAX_TERM_SIZE);
scanner->comment_word=new_string(sgrep,MAX_TERM_SIZE);
scanner->name=new_string(sgrep,MAX_TERM_SIZE);
scanner->literal=new_string(sgrep,MAX_TERM_SIZE);
string_cat(scanner->literal,"xxx");
scanner->aname=new_string(sgrep,MAX_TERM_SIZE);
TERM_PUSH(scanner->aname,'a');
scanner->aval=new_string(sgrep,MAX_TERM_SIZE);
TERM_PUSH(scanner->aval,'v');
scanner->pi=new_string(sgrep,MAX_TERM_SIZE);
TERM_PUSH(scanner->pi,'?');
scanner->failed=0;
reset_encoder(scanner,&scanner->encoder);
return scanner;
}
SGMLScanner *new_sgml_phrase_scanner(SgrepData *sgrep,
FileList *file_list,
struct PHRASE_NODE *list) {
SGMLScanner *scanner;
scanner=new_sgml_scanner_common(sgrep,file_list);
scanner->phrase_list=list;
scanner->entry=sgml_add_entry_to_gclist;
scanner->data=NULL;
return scanner;
}
SGMLScanner *new_sgml_index_scanner(SgrepData *sgrep,
FileList *file_list,
struct IndexWriterStruct *writer) {
SGMLScanner *scanner;
scanner=new_sgml_scanner_common(sgrep,file_list);
scanner->phrase_list=NULL;
scanner->element_list=new_region_list(sgrep);
list_set_sorted(scanner->element_list,NOT_SORTED);
scanner->element_list->nested=1;
scanner->entry=sgml_add_entry_to_index;
scanner->data=writer;
return scanner;
}
void delete_sgml_scanner(SGMLScanner *s) {
SgrepData *sgrep=s->sgrep;
/* Empty the element stack if there is one */
pop_elements_to(s,NULL);
if (s->element_list) {
delete_region_list(s->element_list);
}
delete_string(s->word);
delete_string(s->name2);
delete_string(s->comment_word);
delete_string(s->gi);
delete_string(s->aname);
delete_string(s->aval);
delete_string(s->name);
delete_string(s->literal);
delete_string(s->pi);
if (s->name_start_chars) sgrep_free(s->name_start_chars);
if (s->name_chars) sgrep_free(s->name_chars);
sgrep_free(s->word_chars);
sgrep_free(s);
}
void push_state(SGMLScanner *scanner, enum SGMLState state) {
assert(scanner->state_stack_ptr<SGML_SCANNER_STACK_SIZE);
scanner->state_stack[scanner->state_stack_ptr++]=state;
}
enum SGMLState pop_state(SGMLScanner *scanner) {
assert(scanner->state_stack_ptr>0);
return scanner->state_stack[--scanner->state_stack_ptr];
}
#define SGML_ENTRY(QUERY,NAME,RAW_NAME,START,END) \
do { if (sgrep->sgml_debug) sgrep_error(sgrep,"%s(\"%s\"):%s:(%d,%d)\n",(QUERY),(NAME),(RAW_NAME),(START),(END)); \
if ((START)<=(END)) state->entry(state,(char *)(RAW_NAME),(START),(END)); } while (0)
void pop_elements_to(SGMLScanner *state,ElementStack *p) {
ElementStack *q;
SGREPDATA(state);
assert(p==NULL || state->top);
/* Element is in the stack */
q=state->top;
while(p!=q) {
/* All elements in the stack not having an end tag
* are considered as empty. Sad but true */
state->top=q->prev;
SGML_ENTRY("elements","","@elements",q->start,q->end);
/* fprintf(stderr,"<%s/>\n",q->gi); */
sgrep_free(q->gi);
sgrep_free(q);
q=state->top;
}
}
/*
* If you think that this function is dull to read, I can assure that is was
* even duller to write
*/
void parse_xml_declaration(SGMLScanner *scanner) {
SGREPDATA(scanner);
const char *version="version";
const char *encoding="encoding";
const unsigned char *ptr=string_to_char(scanner->pi)+4;
SgrepString *encoding_name;
int quote_ch;
encoding_name=new_string(sgrep,MAX_TERM_SIZE);
/* Whitespace */
while(*ptr && isspace(*ptr)) ptr++;
/* "version" */
while(*ptr && *ptr==*version) {
ptr++;
version++;
}
if (*version) goto error;
/* Whitespace */
while(*ptr && isspace(*ptr)) ptr++;
/* "=" */
if (*ptr!='=') goto error;
ptr++;
/* Whitespace */
while(*ptr && isspace(*ptr)) ptr++;
/* quote */
if (*ptr!='\'' && *ptr!='\"') goto error;
quote_ch=*(ptr++);
/* Ignores version */
while(*ptr && *ptr!=quote_ch) ptr++;
if (*ptr!=quote_ch) goto error;
ptr++;
/* Whitespace */
while(*ptr && isspace(*ptr)) ptr++;
if (!*ptr) {
delete_string(encoding_name);
return;
}
/* "encoding" */
while(*ptr && *ptr==*encoding) {
ptr++;
encoding++;
}
if (*encoding) goto error;
/* Whitespace */
while(*ptr && isspace(*ptr)) ptr++;
/* "=" */
if (*ptr!='=') goto error;
ptr++;
/* Whitespace */
while(*ptr && isspace(*ptr)) ptr++;
/* quote */
if (*ptr!='\'' && *ptr!='\"') goto error;
quote_ch=*(ptr++);
/* Scan the encoding */
while(*ptr && *ptr!=quote_ch) {
string_push(encoding_name,*ptr);
ptr++;
}
if (*ptr!=quote_ch) goto error;
ptr++;
/* Ignore the rest of declaration, whatever there is */
if (sgrep->sgml_debug) {
sgrep_progress(sgrep,"encoding=%s\n",string_to_char(encoding_name));
}
/* If we have been given the encoding, ignore the encoding parameter
*/
if (sgrep->default_encoding==ENCODING_GUESS) {
string_tolower(encoding_name,0);
if (strcmp(string_to_char(encoding_name),"iso-8859-1")==0 ||
strcmp(string_to_char(encoding_name),"us-ascii")==0) {
scanner->encoder.estate=EIGHT_BIT;
} else if (strcmp(string_to_char(encoding_name),"utf-8")==0) {
scanner->encoder.estate=UTF8_1;
} else if (strcmp(string_to_char(encoding_name),"utf-16")==0) {
if (scanner->encoder.estate==UTF8_1 ||
scanner->encoder.estate==EIGHT_BIT) {
sgrep_error(sgrep,"File '%s': utf-16 encoding given in 8-bit encoding declaration?",
flist_name(scanner->file_list,scanner->file_num));
}
} else {
sgrep_error(sgrep,"File '%s':Unknown encoding '%s'. Using default.\n",
flist_name(scanner->file_list,scanner->file_num),
string_to_char(encoding_name));
reset_encoder(scanner,&scanner->encoder);
}
}
delete_string(encoding_name);
return;
error:
delete_string(encoding_name);
scanner->parse_errors++;
sgrep_error(sgrep,"File '%s':Parse error in XML-declaration.\n",
flist_name(scanner->file_list,scanner->file_num));
}
void sgml_found(SGMLScanner *state,enum SGMLState s,int end_index) {
SGREPDATA(state);
end_index--;
switch(s) {
case SGML_WORD:
case SGML_WORD_ENTITY:
case SGML_CDATA_MARKED_SECTION_WORD:
assert(string_to_char(state->word)[0]=='w');
if (state->ignore_case) {
string_tolower(state->word,1);
}
SGML_ENTRY("word",
string_escaped(state->word)+1,
string_to_char(state->word),
state->words,end_index);
break;
case SGML_ENTITY:
assert(string_to_char(state->name)[0]=='&');
SGML_ENTRY("entity",
string_escaped(state->name)+1,
string_to_char(state->name),
state->entitys,end_index);
break;
case SGML_STAGC:
if (state->type!=XML_SCANNER) {
string_toupper(state->gi,1);
}
SGML_ENTRY("stag",
string_escaped(state->gi)+1,
string_to_char(state->gi),
state->tags,end_index);
if (state->maintain_element_stack) {
/* Push to element stack */
ElementStack *e=sgrep_new(ElementStack);
e->gi=sgrep_strdup(string_to_char(state->gi)+1);
e->start=state->tags;
e->end=end_index;
e->prev=state->top;
state->top=e;
}
break;
case SGML_ATTVALUE:
case SGML_ATTVALUE_DQUOTED:
case SGML_ATTVALUE_SQUOTED:
SGML_ENTRY("attvalue",
string_escaped(state->aval)+1,
string_to_char(state->aval),
state->avals,end_index);
break;
case SGML_ATTRIBUTE_END:
if (state->type!=XML_SCANNER) {
string_toupper(state->aname,1);
}
SGML_ENTRY("attribute",
string_escaped(state->aname)+1,
string_to_char(state->aname),
state->anames,end_index);
break;
case SGML_W_ETAGC:
if (state->type!=XML_SCANNER) {
string_toupper(state->gi,1);
}
SGML_ENTRY("etag",
string_escaped(state->gi)+1,
string_to_char(state->gi),
state->tags,end_index);
if (state->maintain_element_stack) {
/* First check that the element is on the stack */
ElementStack *p=state->top;
while(p && strcmp(string_to_char(state->gi)+1,p->gi)!=0) {
p=p->prev;
}
if (p) {
/* Take elements until p is in top */
pop_elements_to(state,p);
/* Pop p */
state->top=p->prev;
SGML_ENTRY("elements","","@elements",p->start,end_index);
/* fprintf(stderr,"<%s>..</%s>\n",p->gi,p->gi);*/
sgrep_free(p->gi);
sgrep_free(p);
}
}
break;
case SGML_COMMENT_WORD:
if (state->ignore_case) {
string_tolower(state->comment_word,1);
}
SGML_ENTRY("comment_word",
string_escaped(state->comment_word)+1,
string_to_char(state->comment_word),
state->comment_words,end_index);
break;
case SGML_COMMENT_END2:
SGML_ENTRY("comment",(const unsigned char *)"",
(const unsigned char *)"-",
state->comments,end_index);
break;
case SGML_PI:
case SGML_PI_END:
SGML_ENTRY("pi",
string_escaped(state->pi)+1,
string_to_char(state->pi),
state->tags,end_index);
if (state->type==XML_SCANNER &&
toupper(string_to_char(state->pi)[1])=='X' &&
toupper(string_to_char(state->pi)[2])=='M' &&
toupper(string_to_char(state->pi)[3])=='L') {
parse_xml_declaration(state);
}
break;
case SGML_CDATA_MARKED_SECTION_END2:
SGML_ENTRY("cdata",(const unsigned char *)"",
(const unsigned char *)"[CDATA",
state->markeds,end_index);
break;
case SGML_DOCTYPE:
assert(string_to_char(state->name)[0]=='d' &&
string_to_char(state->name)[1]=='n');
if (state->type!=XML_SCANNER) {
string_toupper(state->name,2);
}
SGML_ENTRY("doctype",
string_escaped(state->name)+2,
string_to_char(state->name),
state->doctypes, end_index);
/* Empty the element stack */
pop_elements_to(state,NULL);
break;
case SGML_DOCTYPE_PUBLIC_ID:
state->literal->s[1]='d';
state->literal->s[2]='p';
SGML_ENTRY("doctype_pid",
string_escaped(state->literal)+3,
string_to_char(state->literal)+1,
state->literals,end_index);
break;
case SGML_DOCTYPE_SYSTEM_ID:
state->literal->s[1]='d';
state->literal->s[2]='s';
SGML_ENTRY("doctype_sid",
string_escaped(state->literal)+3,
string_to_char(state->literal)+1,
state->literals,end_index);
break;
case SGML_DOCTYPE_END:
SGML_ENTRY("prologs",(const unsigned char *)"d",
(const unsigned char *)"d!",
state->doctype_declarations, end_index);
break;
/* FIXME: add the literal instead of it's name */
case SGML_GENERAL_ENTITY_DEFINITION_DQUOTED:
case SGML_GENERAL_ENTITY_DEFINITION_SQUOTED:
assert(string_to_char(state->name)[0]=='!' &&
string_to_char(state->name)[1]=='e');
state->name->s[2]='l';
SGML_ENTRY("entity_literal",
string_escaped(state->name)+3,
string_to_char(state->name),
state->literals,end_index);
break;
case SGML_ENTITY_DEFINITION_END:
assert(string_to_char(state->name)[0]=='!' &&
string_to_char(state->name)[1]=='e');
state->name->s[2]='d';
SGML_ENTRY("entity_declaration",
string_escaped(state->name)+3,
string_to_char(state->name),
state->internal_declarations,end_index);
/* Check if this is an system entity reference we should include */
if (state->entity_has_systemid &&
(!state->entity_is_ndata) &&
state->include_system_entities) {
const char *url=string_to_char(state->literal)+3;
if (!flist_exists(state->file_list, url)) {
if (flist_add_relative(state->file_list,
state->file_num,
url)==SGREP_OK) {
sgrep_progress(sgrep,
"Including system entity '%s'\n",
url);
/* fprintf(stderr,"File list size %d\n",
flist_files(state->scan_buffer->file_list)); */
} else {
sgrep_progress(sgrep,"Cannot include system entity '%s'\n",
url);
}
}
}
break;
case SGML_ENTITY_DEFINITION_PUBLIC_ID:
state->literal->s[0]='!';
state->literal->s[1]='e';
state->literal->s[2]='p';
SGML_ENTRY("entity_pid",
string_escaped(state->literal)+3,
string_to_char(state->literal),
state->literals,end_index);
break;
case SGML_ENTITY_DEFINITION_SYSTEM_ID:
state->literal->s[0]='!';
state->literal->s[1]='e';
state->literal->s[2]='s';
SGML_ENTRY("entity_sid",
string_escaped(state->literal)+3,
string_to_char(state->literal),
state->literals,end_index);
break;
case SGML_ENTITY_DEFINITION_NDATA_NAME:
if (state->type!=XML_SCANNER) {
string_toupper(state->name2,3);
}
SGML_ENTRY("entity_ndata",
string_escaped(state->name2)+3,
string_to_char(state->name2),
state->name2s,end_index);
break;
case SGML_ELEMENT_TYPE_DECLARATION:
case SGML_ATTLIST_DECLARATION:
case SGML_NOTATION_DECLARATION:
/* Not used */
break;
default:
sgrep_error(sgrep,"SGML huh?\n");
break;
}
}
/* FIXME: remember to reset encoding */
void sgml_flush(SGMLScanner *sgmls) {
SGREPDATA(sgmls);
/* sgrep_progress(sgrep,"sgml_flush()\n"); */
pop_elements_to(sgmls,NULL);
if (sgmls->element_list && sgmls->entry==sgml_add_entry_to_index) {
ListIterator l;
Region r;
struct IndexWriterStruct *writer=
(struct IndexWriterStruct *)sgmls->data;
/* sgrep_progress(sgrep,"Adding element list to index\n"); */
start_region_search(sgmls->element_list,&l);
get_region(&l,&r);
while(r.start!=-1) {
add_region_to_index(writer,"@elements",r.start,r.end);
get_region(&l,&r);
}
delete_region_list(sgmls->element_list);
sgmls->element_list=new_region_list(sgrep);
list_set_sorted(sgmls->element_list,NOT_SORTED);
sgmls->element_list->nested=1;
}
reset_encoder(sgmls,&sgmls->encoder);
sgmls->state=SGML_PCDATA;
}
/*
* This could be made faster with macro magics like in James Clarks expat.
* I hope that no one notices.
*/
int sgml_scan(SGMLScanner *scanner,
const unsigned char *buf,
int len,
int start,int file_num) {
#define POS (start+i)
#define NEXT_CH do { encoder->prev=POS; ch=-1; } while(0)
#define SGML_FOUND(SCANNER,END) do { \
sgml_found((SCANNER),state,(END)); if ((SCANNER)->failed) return SGREP_ERROR; \
} while(0)
int i;
int ch=-1;
Encoder *encoder=&scanner->encoder;
enum SGMLState state=scanner->state;
SGREPDATA(scanner);
if (encoder->prev==-1) encoder->prev=start;