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
  
1001
  
1002
  
1003
  
1004
  
1005
  
1006
  
1007
  
1008
  
1009
  
1010
  
1011
  
1012
  
1013
  
1014
  
1015
  
1016
  
1017
  
1018
  
1019
  
1020
  
1021
  
1022
  
1023
  
1024
  
1025
  
1026
  
1027
  
1028
  
1029
  
1030
  
1031
  
1032
  
1033
  
1034
  
1035
  
1036
  
1037
  
1038
  
1039
  
1040
  
1041
  
1042
  
1043
  
1044
  
1045
  
1046
  
1047
  
1048
  
1049
  
1050
  
1051
  
1052
  
1053
  
1054
  
1055
  
1056
  
1057
  
1058
  
1059
  
1060
  
1061
  
1062
  
1063
  
1064
  
1065
  
1066
  
1067
  
1068
  
1069
  
1070
  
1071
  
1072
  
1073
  
1074
  
1075
  
1076
  
1077
  
1078
  
1079
  
1080
  
1081
  
1082
  
1083
  
1084
  
1085
  
1086
  
1087
  
1088
  
1089
  
1090
  
1091
  
1092
  
1093
  
1094
  
1095
  
1096
  
1097
  
1098
  
1099
  
1100
  
1101
  
1102
  
1103
  
1104
  
1105
  
1106
  
1107
  
1108
  
1109
  
1110
  
1111
  
1112
  
1113
  
1114
  
1115
  
1116
  
1117
  
1118
  
1119
  
1120
  
1121
  
1122
  
1123
  
1124
  
1125
  
1126
  
1127
  
1128
  
1129
  
1130
  
1131
  
1132
  
1133
  
1134
  
1135
  
1136
  
1137
  
1138
  
1139
  
1140
  
1141
  
1142
  
1143
  
1144
  
1145
  
1146
  
1147
  
1148
  
1149
  
1150
  
1151
  
1152
  
1153
  
1154
  
1155
  
1156
  
1157
  
1158
  
1159
  
1160
  
1161
  
1162
  
1163
  
1164
  
1165
  
1166
  
1167
  
1168
  
1169
  
1170
  
1171
  
1172
  
1173
  
1174
  
1175
  
1176
  
1177
  
1178
  
1179
  
1180
  
1181
  
1182
  
1183
  
1184
  
1185
  
1186
  
1187
  
1188
  
1189
  
1190
  
1191
  
1192
  
1193
  
1194
  
1195
  
1196
  
1197
  
1198
  
1199
  
1200
  
1201
  
1202
  
1203
  
1204
  
1205
  
1206
  
1207
  
1208
  
1209
  
1210
  
1211
  
1212
  
1213
  
1214
  
1215
  
1216
  
1217
  
1218
  
1219
  
1220
  
1221
  
1222
  
1223
  
1224
  
1225
  
1226
  
1227
  
1228
  
1229
  
1230
  
1231
  
1232
  
1233
  
1234
  
1235
  
1236
  
1237
  
1238
  
1239
  
1240
  
1241
  
1242
  
1243
  
1244
  
1245
  
1246
  
1247
  
1248
  
1249
  
1250
  
1251
  
1252
  
1253
  
1254
  
1255
  
1256
  
1257
  
1258
  
1259
  
1260
  
1261
  
1262
  
1263
  
1264
  
1265
  
1266
  
1267
  
1268
  
1269
  
1270
  
1271
  
1272
  
1273
  
1274
  
1275
  
1276
  
1277
  
1278
  
1279
  
1280
  
1281
  
1282
  
1283
  
1284
  
1285
  
1286
  
1287
  
1288
  
1289
  
1290
  
1291
  
1292
  
1293
  
1294
  
1295
  
1296
  
1297
  
1298
  
1299
  
1300
  
1301
  
1302
  
1303
  
1304
  
1305
  
1306
  
1307
  
1308
  
1309
  
1310
  
1311
  
1312
  
1313
  
1314
  
1315
  
1316
  
1317
  
1318
  
1319
  
1320
  
1321
  
1322
  
1323
  
1324
  
1325
  
1326
  
1327
  
1328
  
1329
  
1330
  
1331
  
1332
  
1333
  
1334
  
1335
  
1336
  
1337
  
1338
  
1339
  
1340
  
1341
  
1342
  
1343
  
1344
  
1345
  
1346
  
1347
  
1348
  
1349
  
1350
  
1351
  
1352
  
1353
  
1354
  
1355
  
1356
  
1357
  
1358
  
1359
  
1360
  
1361
  
1362
  
1363
  
1364
  
1365
  
1366
  
1367
  
1368
  
1369
  
1370
  
1371
  
1372
  
1373
  
1374
  
1375
  
1376
  
1377
  
1378
  
1379
  
1380
  
1381
  
1382
  
1383
  
1384
  
1385
  
1386
  
1387
  
1388
  
1389
  
1390
  
1391
  
1392
  
1393
  
1394
  
1395
  
1396
  
1397
  
1398
  
1399
  
1400
  
1401
  
1402
  
1403
  
1404
  
1405
  
1406
  
1407
  
1408
  
1409
  
1410
  
1411
  
1412
  
1413
  
1414
  
1415
  
1416
  
1417
  
1418
  
1419
  
1420
  
1421
  
1422
  
1423
  
1424
  
1425
  
1426
  
1427
  
1428
  
1429
  
1430
  
1431
  
1432
  
1433
  
1434
  
1435
  
1436
  
1437
  
1438
  
1439
  
1440
  
1441
  
1442
  
1443
  
1444
  
1445
  
1446
  
1447
  
1448
  
1449
  
1450
  
1451
  
1452
  
1453
  
1454
  
1455
  
1456
  
1457
  
1458
  
1459
  
1460
  
1461
  
1462
  
1463
  
1464
  
1465
  
1466
  
1467
  
1468
  
1469
  
1470
  
1471
  
1472
  
1473
  
1474
  
1475
  
1476
  
1477
  
1478
  
1479
  
1480
  
1481
  
1482
  
1483
  
1484
  
1485
  
1486
  
1487
  
1488
  
1489
  
1490
  
1491
  
1492
  
1493
  
1494
  
1495
  
1496
  
1497
  
1498
  
1499
  
1500
  
1501
  
1502
  
1503
  
1504
  
1505
  
1506
  
1507
  
1508
  
1509
  
1510
  
1511
  
1512
  
1513
  
1514
  
1515
  
1516
  
1517
  
1518
  
1519
  
1520
  
1521
  
1522
  
1523
  
1524
  
1525
  
1526
  
1527
  
1528
  
1529
  
1530
  
1531
  
1532
  
1533
  
1534
  
1535
  
1536
  
1537
  
1538
  
1539
  
1540
  
1541
  
1542
  
1543
  
1544
  
1545
  
1546
  
1547
  
1548
  
1549
  
1550
  
1551
  
1552
  
1553
  
1554
  
1555
  
1556
  
1557
  
1558
  
1559
  
1560
  
1561
  
1562
  
1563
  
1564
  
1565
  
1566
  
1567
  
1568
  
1569
  
1570
  
1571
  
1572
  
1573
  
1574
  
1575
  
1576
  
1577
  
1578
  
1579
  
1580
  
1581
  
1582
  
1583
  
1584
  
1585
  
1586
  
1587
  
1588
  
1589
  
1590
  
1591
  
1592
  
1593
  
1594
  
1595
  
1596
  
1597
  
1598
  
1599
  
1600
  
1601
  
1602
  
1603
  
1604
  
1605
  
1606
  
1607
  
1608
  
1609
  
1610
  
1611
  
1612
  
1613
  
1614
  
1615
  
1616
  
1617
  
1618
  
1619
  
1620
  
1621
  
1622
  
1623
  
1624
  
1625
  
1626
  
1627
  
1628
  
1629
  
1630
  
1631
  
1632
  
1633
  
1634
  
1635
  
1636
  
1637
  
1638
  
1639
  
1640
  
1641
  
1642
  
1643
  
1644
  
1645
  
1646
  
1647
  
1648
  
1649
  
1650
  
1651
  
1652
  
1653
  
1654
  
1655
  
1656
  
1657
  
1658
  
1659
  
1660
  
1661
  
1662
  
1663
  
1664
  
1665
  
1666
  
1667
  
1668
  
1669
  
1670
  
1671
  
1672
  
1673
  
1674
  
1675
  
1676
  
1677
  
1678
  
1679
  
1680
  
1681
  
1682
  
1683
  
1684
  
1685
  
1686
  
1687
  
1688
  
1689
  
1690
  
1691
  
1692
  
1693
  
1694
  
1695
  
1696
  
1697
  
1698
  
1699
  
1700
  
1701
  
1702
  
1703
  
1704
  
1705
  
1706
  
1707
  
1708
  
1709
  
1710
  
1711
  
1712
  
1713
  
1714
  
1715
  
1716
  
1717
  
1718
  
1719
  
1720
  
1721
  
1722
  
1723
  
1724
  
1725
  
1726
  
1727
  
1728
  
1729
  
1730
  
1731
  
1732
  
1733
  
1734
  
1735
  
1736
  
1737
  
1738
  
1739
  
1740
  
1741
  
1742
  
1743
  
1744
  
1745
  
1746
  
1747
  
1748
  
1749
  
1750
  
1751
  
1752
  
1753
  
1754
  
1755
  
1756
  
1757
  
1758
  
1759
  
1760
  
1761
  
1762
  
1763
  
1764
  
1765
  
1766
  
1767
  
1768
  
1769
  
1770
  
1771
  
1772
  
1773
  
1774
  
1775
  
1776
  
1777
  
1778
  
1779
  
1780
  
1781
  
1782
  
1783
  
1784
  
1785
  
1786
  
1787
  
1788
  
1789
  
1790
  
1791
  
1792
  
1793
  
1794
  
1795
  
1796
  
1797
  
1798
  
1799
  
1800
  
1801
  
1802
  
1803
  
1804
  
1805
  
1806
  
1807
  
1808
  
1809
  
1810
  
1811
  
1812
  
1813
  
1814
  
1815
  
1816
  
1817
  
1818
  
1819
  
1820
  
1821
  
1822
  
1823
  
1824
  
1825
  
1826
  
1827
  
1828
  
1829
  
1830
  
1831
  
1832
  
1833
  
1834
  
1835
  
1836
  
1837
  
1838
  
1839
  
1840
  
1841
  
1842
  
1843
  
1844
  
1845
  
1846
  
1847
  
1848
  
1849
  
1850
  
1851
  
1852
  
1853
  
1854
  
1855
  
1856
  
1857
  
1858
  
1859
  
1860
  
1861
  
1862
  
1863
  
1864
  
1865
  
1866
  
1867
  
1868
  
1869
  
1870
  
1871
  
1872
  
1873
  
1874
  
1875
  
1876
  
1877
  
1878
  
1879
  
1880
  
1881
  
1882
  
1883
  
1884
  
1885
  
1886
  
1887
  
1888
  
1889
  
1890
  
1891
  
1892
  
1893
  
1894
  
1895
  
1896
  
1897
  
1898
  
1899
  
1900
  
1901
  
1902
  
1903
  
1904
  
1905
  
1906
  
1907
  
1908
  
1909
  
1910
  
1911
  
1912
  
1913
  
1914
  
1915
  
1916
  
1917
  
1918
  
1919
  
1920
  
1921
  
1922
  
1923
  
1924
  
1925
  
1926
  
1927
  
1928
  
1929
  
1930
  
1931
  
1932
  
1933
  
1934
  
1935
  
1936
  
1937
  
1938
  
1939
  
1940
  
1941
  
1942
  
1943
  
1944
  
1945
  
1946
  
1947
  
1948
  
1949
  
1950
  
1951
  
1952
  
1953
  
1954
  
1955
  
1956
  
1957
  
1958
  
1959
  
1960
  
1961
  
1962
  
1963
  
1964
  
1965
  
1966
  
1967
  
1968
  
1969
  
1970
  
1971
  
1972
  
1973
  
1974
  
1975
  
1976
  
1977
  
1978
  
1979
  
1980
  
1981
  
1982
  
1983
  
1984
  
1985
  
1986
  
1987
  
1988
  
1989
  
1990
  
1991
  
1992
  
1993
  
1994
  
1995
  
1996
  
1997
  
1998
  
1999
  
2000
  
2001
  
2002
  
2003
  
2004
  
2005
  
2006
  
2007
  
2008
  
2009
  
2010
  
2011
  
2012
  
2013
  
2014
  
2015
  
2016
  
2017
  
2018
  
2019
  
2020
  
2021
  
2022
  
2023
  
2024
  
2025
  
2026
  
2027
  
2028
  
2029
  
2030
  
2031
  
2032
  
2033
  
2034
  
2035
  
2036
  
2037
  
2038
  
2039
  
2040
  
2041
  
2042
  
2043
  
2044
  
2045
  
2046
  
2047
  
2048
  
2049
  
2050
  
2051
  
2052
  
2053
  
2054
  
2055
  
2056
  
2057
  
2058
  
2059
  
2060
  
2061
  
2062
  
2063
  
2064
  
2065
  
2066
  
2067
  
2068
  
2069
  
2070
  
2071
  
2072
  
2073
  
2074
  
2075
  
2076
  
2077
  
2078
  
2079
  
2080
  
2081
  
2082
  
2083
  
2084
  
2085
  
2086
  
2087
  
2088
  
2089
  
2090
  
2091
  
2092
  
2093
  
2094
  
2095
  
2096
  
2097
  
2098
  
2099
  
2100
  
2101
  
2102
  
2103
  
2104
  
2105
  
2106
  
2107
  
2108
  
2109
  
2110
  
2111
  
2112
  
2113
  
2114
  
2115
  
2116
  
2117
  
2118
  
2119
  
2120
  
2121
  
2122
  
2123
  
2124
  
2125
  
2126
  
2127
  
2128
  
2129
  
2130
  
2131
  
2132
  
2133
  
2134
  
2135
  
2136
  
2137
  
2138
  
2139
  
2140
  
2141
  
2142
  
2143
  
2144
  
2145
  
2146
  
2147
  
2148
  
2149
  
2150
  
2151
  
2152
  
2153
  
2154
  
2155
  
2156
  
2157
  
2158
  
2159
  
2160
  
2161
  
2162
  
2163
  
2164
  
2165
  
2166
  
2167
  
2168
  
2169
  
2170
  
2171
  
2172
  
2173
  
2174
  
2175
  
2176
  
2177
  
2178
  
2179
  
2180
  
2181
  
2182
  
2183
  
2184
  
2185
  
2186
  
2187
  
2188
  
2189
  
2190
  
2191
  
2192
  
2193
  
2194
  
2195
  
2196
  
2197
  
2198
  
2199
  
2200
  
2201
  
2202
  
2203
  
2204
  
2205
  
2206
  
2207
  
2208
  
2209
  
2210
  
2211
  
2212
  
2213
  
2214
  
2215
  
2216
  
2217
  
2218
  
2219
  
2220
  
2221
  
2222
  
2223
  
2224
  
2225
  
2226
  
2227
  
2228
  
2229
  
2230
  
2231
  
2232
  
2233
  
2234
  
2235
  
2236
  
2237
  
2238
  
2239
  
2240
  
2241
  
2242
  
2243
  
2244
  
2245
  
2246
  
2247
  
2248
  
2249
  
2250
  
2251
  
2252
  
2253
  
2254
  
2255
  
2256
  
2257
  
2258
  
2259
  
2260
  
2261
  
2262
  
2263
  
2264
  
2265
  
2266
  
2267
  
2268
  
2269
  
2270
  
2271
  
2272
  
2273
  
2274
  
2275
  
2276
  
2277
  
2278
  
2279
  
2280
  
2281
  
2282
  
2283
  
2284
  
2285
  
2286
  
2287
  
2288
  
2289
  
2290
  
2291
  
2292
  
2293
  
2294
  
2295
  
2296
  
2297
  
2298
  
2299
  
2300
  
2301
  
2302
  
2303
  
2304
  
2305
  
2306
  
2307
  
2308
  
2309
  
2310
  
2311
  
2312
  
2313
  
2314
  
2315
  
2316
  
2317
  
2318
  
2319
  
2320
  
2321
  
2322
  
2323
  
2324
  
2325
  
2326
  
2327
  
2328
  
2329
  
2330
  
2331
  
2332
  
2333
  
2334
  
2335
  
2336
  
2337
  
2338
  
2339
  
2340
  
2341
  
2342
  
2343
  
2344
  
2345
  
2346
  
2347
  
2348
  
2349
  
2350
  
2351
  
2352
  
2353
  
2354
  
2355
  
2356
  
2357
  
2358
  
2359
  
2360
  
2361
  
2362
  
2363
  
2364
  
2365
  
2366
  
2367
  
2368
  
2369
  
2370
  
2371
  
2372
  
2373
  
2374
  
2375
  
2376
  
2377
  
2378
  
2379
  
2380
  
2381
  
2382
  
2383
  
2384
  
2385
  
2386
  
2387
  
2388
  
2389
  
2390
  
2391
  
2392
  
2393
  
2394
  
2395
  
2396
  
2397
  
2398
  
2399
  
2400
  
2401
  
2402
  
2403
  
2404
  
2405
  
2406
  
2407
  
2408
  
2409
  
2410
  
2411
  
2412
  
2413
  
2414
  
2415
  
2416
  
2417
  
2418
  
2419
  
2420
  
2421
  
2422
  
2423
  
2424
  
2425
  
2426
  
2427
  
2428
  
2429
  
2430
  
2431
  
2432
  
2433
  
2434
  
2435
  
2436
  
2437
  
2438
  
2439
  
2440
  
2441
  
2442
  
2443
  
2444
  
2445
  
2446
  
2447
  
2448
  
2449
  
2450
  
2451
  
2452
  
2453
  
2454
  
2455
  
2456
  
2457
  
2458
  
2459
  
2460
  
2461
  
2462
  
2463
  
2464
  
2465
  
2466
  
2467
  
2468
  
2469
  
2470
  
2471
  
2472
  
2473
  
2474
  
2475
  
2476
  
2477
  
2478
  
2479
  
2480
  
2481
  
2482
  
2483
  
2484
  
2485
  
2486
  
2487
  
2488
  
2489
  
2490
  
2491
  
2492
  
2493
  
2494
  
2495
  
2496
  
2497
  
2498
  
2499
  
2500
  
2501
  
2502
  
2503
  
2504
  
2505
  
2506
  
2507
  
2508
  
2509
  
2510
  
2511
  
2512
  
2513
  
2514
  
2515
  
2516
  
2517
  
2518
  
2519
  
2520
  
2521
  
2522
  
2523
  
2524
  
2525
  
2526
  
2527
  
2528
  
2529
  
2530
  
2531
  
2532
  
2533
  
2534
  
2535
  
2536
  
2537
  
2538
  
2539
  
2540
  
2541
  
2542
  
2543
  
2544
  
2545
  
2546
  
2547
  
2548
  
2549
  
2550
  
2551
  
2552
  
2553
  
2554
  
2555
  
2556
  
2557
  
2558
  
2559
  
2560
  
2561
  
2562
  
2563
  
2564
  
2565
  
2566
  
2567
  
2568
  
2569
  
2570
  
2571
  
2572
  
2573
  
2574
  
2575
  
2576
  
2577
  
2578
  
2579
  
2580
  
2581
  
2582
  
2583
  
2584
  
2585
  
2586
  
2587
  
2588
  
2589
  
2590
  
2591
  
2592
  
2593
  
2594
  
2595
  
2596
  
2597
  
2598
  
2599
  
2600
  
2601
  
2602
  
2603
  
2604
  
2605
  
2606
  
2607
  
2608
  
2609
  
2610
  
2611
  
2612
  
2613
  
2614
  
2615
  
2616
  
2617
  
2618
  
2619
  
2620
  
2621
  
2622
  
2623
  
2624
  
2625
  
2626
  
2627
  
2628
  
2629
  
2630
  
2631
  
2632
  
2633
  
2634
  
2635
  
2636
  
2637
  
2638
  
2639
  
2640
  
2641
  
2642
  
2643
  
2644
  
2645
  
2646
  
2647
  
2648
  
2649
  
2650
  
2651
  
2652
  
2653
  
2654
  
2655
  
2656
  
2657
  
2658
  
2659
  
2660
  
2661
  
2662
  
2663
  
2664
  
2665
  
2666
  
2667
  
2668
  
2669
  
2670
  
2671
  
2672
  
2673
  
2674
  
2675
  
2676
  
2677
  
2678
  
2679
  
2680
  
2681
  
2682
  
2683
  
2684
  
2685
  
2686
  
2687
  
2688
  
2689
  
2690
  
2691
  
2692
  
2693
  
2694
  
2695
  
2696
  
2697
  
2698
  
2699
  
2700
  
2701
  
2702
  
2703
  
2704
  
2705
  
2706
  
2707
  
2708
  
2709
  
2710
  
2711
  
2712
  
2713
  
2714
  
2715
  
2716
  
2717
  
2718
  
2719
  
2720
  
2721
  
2722
  
2723
  
2724
  
2725
  
2726
  
2727
  
2728
  
2729
  
2730
  
2731
  
2732
  
2733
  
2734
  
2735
  
2736
  
2737
  
2738
  
2739
  
2740
  
2741
  
2742
  
2743
  
2744
  
2745
  
2746
  
2747
  
2748
  
2749
  
2750
  
2751
  
2752
  
2753
  
2754
  
2755
  
2756
  
2757
  
2758
  
2759
  
2760
  
2761
  
2762
  
2763
  
2764
  
2765
  
2766
  
2767
  
2768
  
2769
  
2770
  
2771
  
2772
  
2773
  
2774
  
2775
  
2776
  
2777
  
2778
  
2779
  
2780
  
2781
  
2782
  
2783
  
2784
  
2785
  
2786
  
2787
  
2788
  
2789
  
2790
  
2791
  
2792
  
2793
  
2794
  
2795
  
2796
  
2797
  
2798
  
2799
  
2800
  
2801
  
2802
  
2803
  
2804
  
2805
  
2806
  
2807
  
2808
  
2809
  
2810
  
2811
  
2812
  
2813
  
2814
  
2815
  
2816
  
2817
  
2818
  
2819
  
2820
  
2821
  
2822
  
2823
  
2824
  
2825
  
2826
  
2827
  
2828
  
2829
  
2830
  
2831
  
2832
  
2833
  
2834
  
2835
  
2836
  
2837
  
2838
  
2839
  
2840
  
2841
  
2842
  
2843
  
2844
  
2845
  
2846
  
2847
  
2848
  
2849
  
2850
  
2851
  
2852
  
2853
  
2854
  
2855
  
2856
  
2857
  
2858
  
2859
  
2860
  
2861
  
2862
  
2863
  
2864
  
2865
  
2866
  
2867
  
2868
  
2869
  
2870
  
2871
  
2872
  
2873
  
2874
  
2875
  
2876
  
2877
  
2878
  
2879
  
2880
  
2881
  
2882
  
2883
  
2884
  
2885
  
2886
  
2887
  
2888
  
2889
  
2890
  
2891
  
2892
  
2893
  
2894
  
2895
  
2896
  
2897
  
2898
  
2899
  
2900
  
2901
  
2902
  
2903
  
2904
  
2905
  
2906
  
2907
  
2908
  
2909
  
2910
  
2911
  
2912
  
2913
  
2914
  
2915
  
2916
  
2917
  
2918
  
2919
  
2920
  
2921
  
2922
  
2923
  
2924
  
2925
  
2926
  
2927
  
2928
  
2929
  
2930
  
2931
  
2932
  
2933
  
2934
  
2935
  
2936
  
2937
  
2938
  
2939
  
2940
  
2941
  
2942
  
2943
  
2944
  
2945
  
2946
  
2947
  
2948
  
2949
  
2950
  
2951
  
2952
  
2953
  
2954
  
2955
  
2956
  
2957
  
2958
  
2959
  
2960
  
2961
  
2962
  
2963
  
2964
  
2965
  
2966
  
2967
  
2968
  
2969
  
2970
  
2971
  
2972
  
2973
  
2974
  
2975
  
2976
  
2977
  
2978
  
2979
  
2980
  
2981
  
2982
  
2983
  
2984
  
2985
  
2986
  
2987
  
2988
  
2989
  
2990
  
2991
  
2992
  
2993
  
2994
  
2995
  
2996
  
2997
  
2998
  
2999
  
3000
  
3001
  
3002
  
3003
  
3004
  
3005
  
3006
  
3007
  
3008
  
3009
  
3010
  
3011
  
3012
  
3013
  
3014
  
3015
  
3016
  
3017
  
3018
  
3019
  
3020
  
3021
  
3022
  
3023
  
3024
  
3025
  
3026
  
3027
  
3028
  
3029
  
3030
  
3031
  
3032
  
3033
  
3034
  
3035
  
3036
  
3037
  
3038
  
3039
  
3040
  
3041
  
3042
  
3043
  
3044
  
3045
  
3046
  
3047
  
3048
  
3049
  
3050
  
3051
  
3052
  
3053
  
3054
  
3055
  
3056
  
3057
  
3058
  
3059
  
3060
  
3061
  
3062
  
3063
  
3064
  
3065
  
3066
  
3067
  
3068
  
3069
  
3070
  
3071
  
3072
  
3073
  
3074
  
3075
  
3076
  
3077
  
3078
  
3079
  
3080
  
3081
  
3082
  
3083
  
3084
  
3085
  
3086
  
3087
  
3088
  
3089
  
3090
  
3091
  
3092
  
3093
  
3094
  
3095
  
3096
  
3097
  
3098
  
3099
  
3100
  
3101
  
3102
  
3103
  
3104
  
3105
  
3106
  
3107
  
3108
  
3109
  
3110
  
3111
  
3112
  
3113
  
3114
  
3115
  
3116
  
3117
  
3118
  
3119
  
3120
  
3121
  
3122
  
3123
  
3124
  
3125
  
3126
  
3127
  
3128
  
3129
  
3130
  
3131
  
3132
  
3133
  
3134
  
3135
  
3136
  
3137
  
3138
  
3139
  
3140
  
3141
  
3142
  
3143
  
3144
  
3145
  
3146
  
3147
  
3148
  
3149
  
3150
  
3151
  
3152
  
3153
  
3154
  
3155
  
3156
  
3157
  
3158
  
3159
  
3160
  
3161
  
3162
  
3163
  
3164
  
3165
  
3166
  
3167
  
3168
  
3169
  
3170
  
3171
  
3172
  
3173
  
3174
  
3175
  
3176
  
3177
  
3178
  
3179
  
3180
  
3181
  
3182
  
3183
  
3184
  
3185
  
3186
  
3187
  
3188
  
3189
  
3190
  
3191
  
3192
  
3193
  
3194
  
3195
  
3196
  
3197
  
3198
  
3199
  
3200
  
3201
  
3202
  
3203
  
3204
  
3205
  
3206
  
3207
  
3208
  
3209
  
3210
  
3211
  
3212
  
3213
  
3214
  
3215
  
3216
  
3217
  
3218
  
3219
  
3220
  
3221
  
3222
  
3223
  
3224
  
3225
  
3226
  
3227
  
3228
  
3229
  
3230
  
3231
  
3232
  
3233
  
3234
  
3235
  
3236
  
3237
  
3238
  
3239
  
3240
  
3241
  
3242
  
3243
  
3244
  
3245
  
3246
  
3247
  
3248
  
3249
  
3250
  
3251
  
3252
  
3253
  
3254
  
3255
  
3256
  
3257
  
3258
  
3259
  
3260
  
3261
  
3262
  
3263
  
3264
  
3265
  
3266
  
3267
  
3268
  
3269
  
3270
  
3271
  
3272
  
3273
  
3274
  
3275
  
3276
  
3277
  
3278
  
3279
  
3280
  
3281
  
3282
  
3283
  
3284
  
3285
  
3286
  
3287
  
3288
  
3289
  
3290
  
3291
  
3292
  
3293
  
3294
  
3295
  
3296
  
3297
  
3298
  
3299
  
3300
  
3301
  
3302
  
3303
  
3304
  
3305
  
3306
  
3307
  
3308
  
3309
  
3310
  
3311
  
3312
  
3313
  
3314
  
3315
  
3316
  
3317
  
3318
  
3319
  
3320
  
3321
  
3322
  
3323
  
3324
  
3325
  
3326
  
3327
  
3328
  
3329
  
3330
  
3331
  
3332
  
3333
  
3334
  
3335
  
3336
  
3337
  
3338
  
3339
  
3340
  
3341
  
3342
  
3343
  
3344
  
3345
  
3346
  
3347
  
3348
  
3349
  
3350
  
3351
  
3352
  
3353
  
3354
  
3355
  
3356
  
3357
  
3358
  
3359
  
3360
  
3361
  
3362
  
3363
  
3364
  
3365
  
3366
  
3367
  
3368
  
3369
  
3370
  
3371
  
3372
  
3373
  
3374
  
3375
  
3376
  
3377
  
3378
  
3379
  
3380
  
3381
  
3382
  
3383
  
3384
  
3385
  
3386
  
3387
  
3388
  
3389
  
3390
  
3391
  
3392
  
3393
  
3394
  
3395
  
3396
  
3397
  
3398
  
3399
  
3400
  
3401
  
3402
  
3403
  
3404
  
3405
  
3406
  
3407
  
3408
  
3409
  
3410
  
3411
  
3412
  
3413
  
3414
  
3415
  
3416
  
3417
  
3418
  
3419
  
3420
  
3421
  
3422
  
3423
  
3424
  
3425
  
3426
  
3427
  
3428
  
3429
  
3430
  
3431
  
3432
  
3433
  
3434
  
3435
  
3436
  
3437
  
3438
  
3439
  
3440
  
3441
  
3442
  
3443
  
3444
  
3445
  
3446
  
3447
  
3448
  
3449
  
3450
  
3451
  
3452
  
3453
  
3454
  
3455
  
3456
  
3457
  
3458
  
3459
  
3460
  
3461
  
3462
  
3463
  
3464
  
3465
  
3466
  
3467
  
3468
  
3469
  
3470
  
3471
  
3472
  
3473
  
3474
  
3475
  
3476
  
3477
  
3478
  
3479
  
3480
  
3481
  
3482
  
3483
  
3484
  
3485
  
3486
  
3487
  
3488
  
3489
  
3490
  
3491
  
3492
  
3493
  
3494
  
3495
  
3496
  
3497
  
3498
  
3499
  
3500
  
3501
  
3502
  
3503
  
3504
  
3505
  
3506
  
3507
  
3508
  
3509
  
3510
  
3511
  
3512
  
3513
  
3514
  
3515
  
3516
  
3517
  
3518
  
3519
  
3520
  
3521
  
3522
  
3523
  
3524
  
3525
  
3526
  
3527
  
3528
  
3529
  
3530
  
3531
  
3532
  
3533
  
3534
  
3535
  
3536
  
3537
  
3538
  
3539
  
3540
  
3541
  
3542
  
3543
  
3544
  
3545
  
3546
  
3547
  
3548
  
3549
  
3550
  
3551
  
3552
  
3553
  
3554
  
3555
  
3556
  
3557
  
3558
  
3559
  
3560
  
3561
  
3562
  
3563
  
3564
  
3565
  
3566
  
3567
  
3568
  
3569
  
3570
  
3571
  
3572
  
3573
  
3574
  
3575
  
3576
  
3577
  
3578
  
3579
  
3580
  
3581
  
3582
  
3583
  
3584
  
3585
  
3586
  
3587
  
3588
  
3589
  
3590
  
3591
  
3592
  
3593
  
3594
  
3595
  
3596
  
3597
  
3598
  
3599
  
3600
  
3601
  
3602
  
3603
  
3604
  
3605
  
3606
  
3607
  
3608
  
3609
  
3610
  
3611
  
3612
  
3613
  
3614
  
3615
  
3616
  
3617
  
3618
  
3619
  
3620
  
3621
  
3622
  
3623
  
3624
  
3625
  
3626
  
3627
  
3628
  
3629
  
3630
  
3631
  
3632
  
3633
  
3634
  
3635
  
3636
  
3637
  
3638
  
3639
  
3640
  
3641
  
3642
  
3643
  
3644
  
3645
  
3646
  
3647
  
3648
  
3649
  
3650
  
3651
  
3652
  
3653
  
3654
  
3655
  
3656
  
3657
  
3658
  
3659
  
3660
  
3661
  
3662
  
3663
  
3664
  
3665
  
3666
  
3667
  
3668
  
3669
  
3670
  
3671
  
3672
  
3673
  
3674
  
3675
  
3676
  
3677
  
3678
  
3679
  
3680
  
3681
  
3682
  
3683
  
3684
  
3685
  
3686
  
3687
  
3688
  
3689
  
3690
  
3691
  
3692
  
3693
  
3694
  
3695
  
3696
  
3697
  
3698
  
3699
  
3700
  
3701
  
3702
  
3703
  
3704
  
3705
  
3706
  
3707
  
3708
  
3709
  
3710
  
3711
  
3712
  
3713
  
3714
  
3715
  
3716
  
3717
  
3718
  
3719
  
3720
  
3721
  
3722
  
3723
  
3724
  
3725
  
3726
  
3727
  
3728
  
3729
  
3730
  
3731
  
3732
  
3733
  
3734
  
3735
  
3736
  
3737
  
3738
  
3739
  
3740
  
3741
  
3742
  
3743
  
3744
  
3745
  
3746
  
3747
  
3748
  
3749
  
3750
  
3751
  
3752
  
3753
  
3754
  
3755
  
3756
  
3757
  
3758
  
3759
  
3760
  
3761
  
3762
  
3763
  
3764
  
3765
  
3766
  
3767
  
3768
  
3769
  
3770
  
3771
  
3772
  
3773
  
3774
  
3775
  
3776
  
3777
  
3778
  
3779
  
3780
  
3781
  
3782
  
3783
  
3784
  
3785
  
3786
  
3787
  
3788
  
3789
  
3790
  
3791
  
3792
  
3793
  
3794
  
3795
  
3796
  
3797
  
3798
  
3799
  
3800
  
3801
  
3802
  
3803
  
3804
  
3805
  
3806
  
3807
  
3808
  
3809
  
3810
  
3811
  
3812
  
3813
  
3814
  
3815
  
3816
  
3817
  
3818
  
3819
  
3820
  
3821
  
3822
  
3823
  
3824
  
3825
  
3826
  
3827
  
3828
  
3829
  
3830
  
3831
  
3832
  
3833
  
3834
  
3835
  
3836
  
3837
  
3838
  
3839
  
3840
  
3841
  
3842
  
3843
  
3844
  
3845
  
3846
  
3847
  
3848
  
3849
  
3850
  
3851
  
3852
  
3853
  
3854
  
3855
  
3856
  
3857
  
3858
  
3859
  
3860
  
3861
  
3862
  
3863
  
3864
  
3865
  
3866
  
3867
  
3868
  
3869
  
3870
  
3871
  
3872
  
3873
  
3874
  
3875
  
3876
  
3877
  
3878
  
3879
  
3880
  
3881
  
3882
  
3883
  
3884
  
3885
  
3886
  
3887
  
3888
  
3889
  
3890
  
3891
  
3892
  
3893
  
3894
  
3895
  
3896
  
3897
  
3898
  
3899
  
3900
  
3901
  
3902
  
3903
  
3904
  
3905
  
3906
  
3907
  
3908
  
3909
  
3910
  
3911
  
3912
  
3913
  
3914
  
3915
  
3916
  
3917
  
3918
  
3919
  
3920
  
3921
  
3922
  
3923
  
3924
  
3925
  
3926
  
3927
  
3928
  
3929
  
3930
  
3931
  
3932
  
3933
  
3934
  
3935
  
3936
  
3937
  
3938
  
3939
  
3940
  
3941
  
3942
  
3943
  
3944
  
3945
  
3946
  
3947
  
3948
  
3949
  
3950
  
3951
  
3952
  
3953
  
3954
  
3955
  
3956
  
3957
  
3958
  
3959
  
3960
  
3961
  
3962
  
3963
  
3964
  
3965
  
3966
  
3967
  
3968
  
3969
  
3970
  
3971
  
3972
  
3973
  
3974
  
3975
  
3976
  
3977
  
3978
  
3979
  
3980
  
3981
  
3982
  
3983
  
3984
  
3985
  
3986
  
3987
  
3988
  
3989
  
3990
  
3991
  
3992
  
3993
  
3994
  
3995
  
3996
  
3997
  
3998
  
3999
  
4000
  
4001
  
4002
  
4003
  
4004
  
4005
  
4006
  
4007
  
4008
  
4009
  
4010
  
4011
  
4012
  
4013
  
4014
  
4015
  
4016
  
4017
  
4018
  
4019
  
4020
  
4021
  
4022
  
4023
  
4024
  
4025
  
4026
  
4027
  
4028
  
4029
  
4030
  
4031
  
4032
  
4033
  
4034
  
4035
  
4036
  
4037
  
4038
  
4039
  
4040
  
4041
  
4042
  
4043
  
4044
  
4045
  
4046
  
4047
  
4048
  
4049
  
4050
  
4051
  
4052
  
4053
  
4054
  
4055
  
4056
  
4057
  
4058
  
4059
  
4060
  
4061
  
4062
  
4063
  
4064
  
4065
  
4066
  
4067
  
4068
  
4069
  
4070
  
4071
  
4072
  
4073
  
4074
  
4075
  
4076
  
4077
  
4078
  
4079
  
4080
  
4081
  
4082
  
4083
  
4084
  
4085
  
4086
  
4087
  
4088
  
4089
  
4090
  
4091
  
4092
  
4093
  
4094
  
4095
  
4096
  
4097
  
4098
  
4099
  
4100
  
4101
  
4102
  
4103
  
4104
  
4105
  
4106
  
4107
  
4108
  
4109
  
4110
  
4111
  
4112
  
4113
  
4114
  
4115
  
4116
  
4117
  
4118
  
4119
  
4120
  
4121
  
4122
  
4123
  
4124
  
4125
  
4126
  
4127
  
4128
  
4129
  
4130
  
4131
  
4132
  
4133
  
4134
  
4135
  
4136
  
4137
  
4138
  
4139
  
4140
  
4141
  
4142
  
4143
  
4144
  
4145
  
4146
  
4147
  
4148
  
4149
  
4150
  
4151
  
4152
  
4153
  
4154
  
4155
  
4156
  
4157
  
4158
  
4159
  
4160
  
4161
  
4162
  
4163
  
4164
  
4165
  
4166
  
4167
  
4168
  
4169
  
4170
  
4171
  
4172
  
4173
  
4174
  
4175
  
4176
  
4177
  
4178
  
4179
  
4180
  
4181
  
4182
  
4183
  
4184
  
4185
  
4186
  
4187
  
4188
  
4189
  
4190
  
4191
  
4192
  
4193
  
4194
  
4195
  
4196
  
4197
  
4198
  
4199
  
4200
  
4201
  
4202
  
4203
  
4204
  
4205
  
4206
  
4207
  
4208
  
4209
  
4210
  
4211
  
4212
  
4213
  
4214
  
4215
  
4216
  
4217
  
4218
  
4219
  
4220
  
4221
  
4222
  
4223
  
4224
  
4225
  
4226
  
4227
  
4228
  
4229
  
4230
  
4231
  
4232
  
4233
  
4234
  
4235
  
4236
  
4237
  
4238
  
4239
  
4240
  
4241
  
4242
  
4243
  
4244
  
4245
  
4246
  
4247
  
4248
  
4249
  
4250
  
4251
  
4252
  
4253
  
4254
  
4255
  
4256
  
4257
  
4258
  
4259
  
4260
  
4261
  
4262
  
4263
  
4264
  
4265
  
4266
  
4267
  
4268
  
4269
  
4270
  
4271
  
4272
  
4273
  
4274
  
4275
  
4276
  
4277
  
4278
  
4279
  
4280
  
4281
  
4282
  
4283
  
4284
  
4285
  
4286
  
4287
  
4288
  
4289
  
4290
  
4291
  
4292
  
4293
  
4294
  
4295
  
4296
  
4297
  
4298
  
4299
  
4300
  
4301
  
4302
  
4303
  
4304
  
4305
  
4306
  
4307
  
4308
  
4309
  
4310
  
4311
  
4312
  
4313
  
4314
  
4315
  
4316
  
4317
  
4318
  
4319
  
4320
  
4321
  
4322
  
4323
  
4324
  
4325
  
4326
  
4327
  
4328
  
4329
  
4330
  
4331
  
4332
  
4333
  
4334
  
4335
  
4336
  
4337
  
4338
  
4339
  
4340
  
4341
  
4342
  
4343
  
4344
  
4345
  
4346
  
4347
  
4348
  
4349
  
4350
  
4351
  
4352
  
4353
  
4354
  
4355
  
4356
  
4357
  
4358
  
4359
  
4360
  
4361
  
4362
  
4363
  
4364
  
4365
  
4366
  
4367
  
4368
  
4369
  
4370
  
4371
  
4372
  
4373
  
4374
  
4375
  
4376
  
4377
  
4378
  
4379
  
4380
  
4381
  
4382
  
4383
  
4384
  
4385
  
4386
  
4387
  
4388
  
4389
  
4390
  
4391
  
4392
  
4393
  
4394
  
4395
  
4396
  
4397
  
4398
  
4399
  
4400
  
4401
  
4402
  
4403
  
4404
  
4405
  
4406
  
4407
  
4408
  
4409
  
4410
  
4411
  
4412
  
4413
  
4414
  
4415
  
4416
  
4417
  
4418
  
4419
  
4420
  
4421
  
4422
  
4423
  
4424
  
4425
  
4426
  
4427
  
4428
  
4429
  
4430
  
4431
  
4432
  
4433
  
4434
  
4435
  
4436
  
4437
  
4438
  
4439
  
4440
  
4441
  
4442
  
4443
  
4444
  
4445
  
4446
  
4447
  
4448
  
4449
  
4450
  
4451
  
4452
  
4453
  
4454
  
4455
  
4456
  
4457
  
4458
  
4459
  
4460
  
4461
  
4462
  
4463
  
4464
  
4465
  
4466
  
4467
  
4468
  
4469
  
4470
  
4471
  
4472
  
4473
  
4474
  
4475
  
4476
  
4477
  
4478
  
4479
  
4480
  
4481
  
4482
  
4483
  
4484
  
4485
  
4486
  
4487
  
4488
  
4489
  
4490
  
4491
  
4492
  
4493
  
4494
  
4495
  
4496
  
4497
  
4498
  
4499
  
4500
  
4501
  
4502
  
4503
  
4504
  
4505
  
4506
  
4507
  
4508
  
4509
  
4510
  
4511
  
4512
  
4513
  
4514
  
4515
  
4516
  
4517
  
4518
  
4519
  
4520
  
4521
  
4522
  
4523
  
4524
  
4525
  
4526
  
4527
  
4528
  
4529
  
4530
  
4531
  
4532
  
4533
  
4534
  
4535
  
4536
  
4537
  
4538
  
4539
  
4540
  
4541
  
4542
  
4543
  
4544
  
4545
  
4546
  
4547
  
4548
  
4549
  
4550
  
4551
  
4552
  
4553
  
4554
  
4555
  
4556
  
4557
  
4558
  
4559
  
4560
  
4561
  
4562
  
4563
  
4564
  
4565
  
4566
  
4567
  
4568
  
4569
  
4570
  
4571
  
4572
  
4573
  
4574
  
4575
  
4576
  
4577
  
4578
  
4579
  
4580
  
4581
  
4582
  
4583
  
4584
  
4585
  
4586
  
4587
  
4588
  
4589
  
4590
  
4591
  
4592
  
4593
  
4594
  
4595
  
4596
  
4597
  
4598
  
4599
  
4600
  
4601
  
4602
  
4603
  
4604
  
4605
  
4606
  
4607
  
4608
  
4609
  
4610
  
4611
  
4612
  
4613
  
4614
  
4615
  
4616
  
4617
  
4618
  
4619
  
4620
  
4621
  
4622
  
4623
  
4624
  
4625
  
4626
  
4627
  
4628
  
4629
  
4630
  
4631
  
4632
  
4633
  
4634
  
4635
  
4636
  
4637
  
4638
  
4639
  
4640
  
4641
  
4642
  
4643
  
4644
  
4645
  
4646
  
4647
  
4648
  
4649
  
4650
  
4651
  
4652
  
4653
  
4654
  
4655
  
4656
  
4657
  
4658
  
4659
  
4660
  
4661
  
4662
  
4663
  
4664
  
4665
  
4666
  
4667
  
4668
  
4669
  
4670
  
4671
  
4672
  
4673
  
4674
  
4675
  
4676
  
4677
  
4678
  
4679
  
4680
  
4681
  
4682
  
4683
  
4684
  
4685
  
4686
  
4687
  
4688
  
4689
  
4690
  
4691
  
4692
  
4693
  
4694
  
4695
  
4696
  
4697
  
4698
  
4699
  
4700
  
4701
  
4702
  
4703
  
4704
  
4705
  
4706
  
4707
  
4708
  
4709
  
4710
  
4711
  
4712
  
4713
  
4714
  
4715
  
4716
  
4717
  
4718
  
4719
  
4720
  
4721
  
4722
  
4723
  
4724
  
4725
  
4726
  
4727
  
4728
  
4729
  
4730
  
4731
  
4732
  
4733
  
4734
  
4735
  
4736
  
4737
  
4738
  
4739
  
4740
  
4741
  
4742
  
4743
  
4744
  
4745
  
4746
  
4747
  
4748
  
4749
  
4750
  
4751
  
4752
  
4753
  
4754
  
4755
  
4756
  
4757
  
4758
  
4759
  
4760
  
4761
  
4762
  
4763
  
4764
  
4765
  
4766
  
4767
  
4768
  
4769
  
4770
  
4771
  
4772
  
4773
  
4774
  
4775
  
4776
  
4777
  
4778
  
4779
  
4780
  
4781
  
4782
  
4783
  
4784
  
4785
  
4786
  
4787
  
4788
  
4789
  
4790
  
4791
  
4792
  
4793
  
4794
  
4795
  
4796
  
4797
  
4798
  
4799
  
4800
  
4801
  
4802
  
4803
  
4804
  
4805
  
4806
  
4807
  
4808
  
4809
  
4810
  
4811
  
4812
  
4813
  
4814
  
4815
  
4816
  
4817
  
4818
  
4819
  
4820
  
4821
  
4822
  
4823
  
4824
  
4825
  
4826
  
4827
  
4828
  
4829
  
4830
  
4831
  
4832
  
4833
  
4834
  
4835
  
4836
  
4837
  
4838
  
4839
  
4840
  
4841
  
4842
  
4843
  
4844
  
4845
  
4846
  
4847
  
4848
  
4849
  
4850
  
4851
  
4852
  
4853
  
4854
  
4855
  
4856
  
4857
  
4858
  
4859
  
4860
  
4861
  
4862
  
4863
  
4864
  
4865
  
4866
  
4867
  
4868
  
4869
  
4870
  
4871
  
4872
  
4873
  
4874
  
4875
  
4876
  
4877
  
4878
  
4879
  
4880
  
4881
  
4882
  
4883
  
4884
  
4885
  
4886
  
4887
  
4888
  
4889
  
4890
  
4891
  
4892
  
4893
  
4894
  
4895
  
4896
  
4897
  
4898
  
4899
  
4900
  
4901
  
4902
  
4903
  
4904
  
4905
  
4906
  
4907
  
4908
  
4909
  
4910
  
4911
  
4912
  
4913
  
4914
  
4915
  
4916
  
4917
  
4918
  
4919
  
4920
  
4921
  
4922
  
4923
  
4924
  
4925
  
4926
  
4927
  
4928
  
4929
  
4930
  
4931
  
4932
  
4933
  
4934
  
4935
  
4936
  
4937
  
4938
  
4939
  
4940
  
4941
  
4942
  
4943
  
4944
  
4945
  
4946
  
4947
  
4948
  
4949
  
4950
  
4951
  
4952
  
4953
  
4954
  
4955
  
4956
  
4957
  
4958
  
4959
  
4960
  
4961
  
4962
  
4963
  
4964
  
4965
  
4966
  
4967
  
4968
  
4969
  
4970
  
4971
  
4972
  
4973
  
4974
  
4975
  
4976
  
4977
  
4978
  
4979
  
4980
  
4981
  
4982
  
4983
  
4984
  
4985
  
4986
  
4987
  
4988
  
4989
  
4990
  
4991
  
4992
  
4993
  
4994
  
4995
  
4996
  
4997
  
4998
  
4999
  
5000
  
5001
  
5002
  
5003
  
5004
  
5005
  
5006
  
5007
  
5008
  
5009
  
5010
  
5011
  
5012
  
5013
  
5014
  
5015
  
5016
  
5017
  
5018
  
5019
  
5020
  
5021
  
5022
  
5023
  
5024
  
5025
  
5026
  
5027
  
5028
  
5029
  
5030
  
5031
  
5032
  
5033
  
5034
  
5035
  
5036
  
5037
  
5038
  
5039
  
5040
  
5041
  
5042
  
5043
  
5044
  
5045
  
5046
  
5047
  
5048
  
5049
  
5050
  
5051
  
5052
  
5053
  
5054
  
5055
  
5056
  
5057
  
5058
  
5059
  
5060
  
5061
  
5062
  
5063
  
5064
  
5065
  
5066
  
5067
  
5068
  
5069
  
5070
  
5071
  
5072
  
5073
  
5074
  
5075
  
5076
  
5077
  
5078
  
5079
  
5080
  
5081
  
5082
  
5083
  
5084
  
5085
  
5086
  
5087
  
5088
  
5089
  
5090
  
5091
  
5092
  
5093
  
5094
  
5095
  
5096
  
5097
  
5098
  
5099
  
5100
  
5101
  
5102
  
5103
  
5104
  
5105
  
5106
  
5107
  
5108
  
5109
  
5110
  
5111
  
5112
  
5113
  
5114
  
5115
  
5116
  
5117
  
5118
  
5119
  
5120
  
5121
  
5122
  
5123
  
5124
  
5125
  
5126
  
5127
  
5128
  
5129
  
5130
  
5131
  
5132
  
5133
  
5134
  
5135
  
5136
  
5137
  
5138
  
5139
  
5140
  
5141
  
5142
  
5143
  
5144
  
5145
  
5146
  
5147
  
5148
  
5149
  
5150
  
5151
  
5152
  
5153
  
5154
  
5155
  
5156
  
5157
  
5158
  
5159
  
5160
  
5161
  
5162
  
5163
  
5164
  
5165
  
5166
  
5167
  
5168
  
5169
  
5170
  
5171
  
5172
  
5173
  
5174
  
5175
  
5176
  
5177
  
5178
  
5179
  
5180
  
5181
  
5182
  
5183
  
5184
  
5185
  
5186
  
5187
  
5188
  
5189
  
5190
  
5191
  
5192
  
5193
  
5194
  
5195
  
5196
  
5197
  
5198
  
5199
  
5200
  
5201
  
5202
  
5203
  
5204
  
5205
  
5206
  
5207
  
5208
  
5209
  
5210
  
5211
  
5212
  
5213
  
5214
  
5215
  
5216
  
5217
  
5218
  
5219
  
5220
  
5221
  
5222
  
5223
  
5224
  
5225
  
5226
  
5227
  
5228
  
5229
  
5230
  
5231
  
5232
  
5233
  
5234
  
5235
  
5236
  
5237
  
5238
  
5239
  
5240
  
5241
  
5242
  
5243
  
5244
  
5245
  
5246
  
5247
  
5248
  
5249
  
5250
  
5251
  
5252
  
5253
  
5254
  
5255
  
5256
  
5257
  
5258
  
5259
  
5260
  
5261
  
5262
  
5263
  
5264
  
5265
  
5266
  
5267
  
5268
  
5269
  
5270
  
5271
  
5272
  
5273
  
5274
  
5275
  
5276
  
5277
  
5278
  
5279
  
5280
  
5281
  
5282
  
5283
  
5284
  
5285
  
5286
  
5287
  
5288
  
5289
  
5290
  
5291
  
5292
  
5293
  
5294
  
5295
  
5296
  
5297
  
5298
  
5299
  
5300
  
5301
  
5302
  
5303
  
5304
  
5305
  
5306
  
5307
  
5308
  
5309
  
5310
  
5311
  
5312
  
5313
  
5314
  
5315
  
5316
  
5317
  
5318
  
5319
  
5320
  
5321
  
5322
  
5323
  
5324
  
5325
  
5326
  
5327
  
5328
  
5329
  
5330
  
5331
  
5332
  
5333
  
5334
  
5335
  
5336
  
5337
  
5338
  
5339
  
5340
  
5341
  
5342
  
5343
  
5344
  
5345
  
5346
  
5347
  
5348
  
5349
  
5350
  
5351
  
5352
  
5353
  
5354
  
5355
  
5356
  
5357
  
5358
  
5359
  
5360
  
5361
  
5362
  
5363
  
5364
  
5365
  
5366
  
5367
  
5368
  
5369
  
5370
  
5371
  
5372
  
5373
  
5374
  
5375
  
5376
  
5377
  
5378
  
5379
  
5380
  
5381
  
5382
  
5383
  
5384
  
5385
  
5386
  
5387
  
5388
  
5389
  
5390
  
5391
  
5392
  
5393
  
5394
  
5395
  
5396
  
5397
  
5398
  
5399
  
5400
  
5401
  
5402
  
5403
  
5404
  
5405
  
5406
  
5407
  
5408
  
5409
  
5410
  
5411
  
5412
  
5413
  
5414
  
5415
  
5416
  
5417
  
5418
  
5419
  
5420
  
5421
  
5422
  
5423
  
5424
  
5425
  
5426
  
5427
  
5428
  
5429
  
5430
  
5431
  
5432
  
5433
  
5434
  
5435
  
5436
  
5437
  
5438
  
5439
  
5440
  
5441
  
5442
  
5443
  
5444
  
5445
  
5446
  
5447
  
5448
  
5449
  
5450
  
5451
  
5452
  
5453
  
5454
  
5455
  
5456
  
5457
  
5458
  
5459
  
5460
  
5461
  
5462
  
5463
  
5464
  
5465
  
5466
  
5467
  
5468
  
5469
  
5470
  
5471
  
5472
  
5473
  
5474
  
5475
  
5476
  
5477
  
5478
  
5479
  
5480
  
5481
  
5482
  
5483
  
5484
  
5485
  
5486
  
5487
  
5488
  
5489
  
5490
  
5491
  
5492
  
5493
  
5494
  
5495
  
5496
  
5497
  
5498
  
5499
  
5500
  
5501
  
5502
  
5503
  
5504
  
5505
  
5506
  
5507
  
5508
  
5509
  
5510
  
5511
  
5512
  
5513
  
5514
  
5515
  
5516
  
5517
  
5518
  
5519
  
5520
  
5521
  
5522
  
5523
  
5524
  
5525
  
5526
  
5527
  
5528
  
5529
  
5530
  
5531
  
5532
  
5533
  
5534
  
5535
  
5536
  
5537
  
5538
  
5539
  
5540
  
5541
  
5542
  
5543
  
5544
  
5545
  
5546
  
5547
  
5548
  
5549
  
5550
  
5551
  
5552
  
5553
  
5554
  
5555
  
5556
  
5557
  
5558
  
5559
  
5560
  
5561
  
5562
  
5563
  
5564
  
5565
  
5566
  
5567
  
5568
  
5569
  
5570
  
5571
  
5572
  
5573
  
5574
  
5575
  
5576
  
5577
  
5578
  
5579
  
5580
  
5581
  
5582
  
5583
  
5584
  
5585
  
5586
  
5587
  
5588
  
5589
  
5590
  
5591
  
5592
  
5593
  
5594
  
5595
  
5596
  
5597
  
5598
  
5599
  
5600
  
5601
  
5602
  
5603
  
5604
  
5605
  
5606
  
5607
  
5608
  
5609
  
5610
  
5611
  
5612
  
5613
  
5614
  
5615
  
5616
  
5617
  
5618
  
5619
  
5620
  
5621
  
5622
  
5623
  
5624
  
5625
  
5626
  
5627
  
5628
  
5629
  
5630
  
5631
  
5632
  
5633
  
5634
  
5635
  
5636
  
5637
  
5638
  
5639
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "config.h" 
 
#include "pike_macros.h" 
#include "object.h" 
#include "constants.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "threads.h" 
#include "array.h" 
#include "pike_error.h" 
#include "operators.h" 
#include "builtin_functions.h" 
#include "module_support.h" 
#include "mapping.h" 
#include "stralloc.h" 
#include "program_id.h" 
#include "block_allocator.h" 
#include <ctype.h> 
 
#include "parser.h" 
 
 
#define sp Pike_sp 
 
extern struct program *parser_html_program; 
 
/* #define SCAN_DEBUG */ 
/* #define DEBUG */ 
 
#ifdef DEBUG 
#undef DEBUG 
#define DEBUG(X) do if (THIS->flags & FLAG_DEBUG_MODE) {fprintf X;} while (0) 
#define DEBUG_MARK_SPOT debug_mark_spot 
#define HTML_DEBUG 
#else 
#define DEBUG(X) do {} while(0) 
#define DEBUG_MARK_SPOT(TEXT,FEED,C) do {} while(0) 
#endif 
 
#if 0 
#define free(X) fprintf(stderr,"free line %d: %p\n",__LINE__,X); free(X) 
#endif 
 
#define MAX_FEED_STACK_DEPTH 10 
 
/* Define this to get configurable markup chars (i.e. the '<' '>' '&' 
 * '"' etc). It's currently undefined since there are no user access 
 * functions for it, and it slows the parser down. Besides, I honestly 
 * can't see the point with making these things changeable. */ 
/* #define CONFIGURABLE_MARKUP */ 
 
/*! @module Parser 
 */ 
 
/*! @class HTML 
 *! This is a simple parser for SGML structured markups. It's not 
 *! really HTML, but it's useful for that purpose. 
 *! 
 *! The simple way to use it is to give it some information about 
 *! available tags and containers, and what callbacks those are to 
 *! call. 
 *! 
 *! The object is easily reused, by calling the @[clone()] function. 
 *! 
 *! @seealso 
 *!  @[add_tag], @[add_container], @[finish] 
 */ 
 
struct location 
{ 
   int byteno;     /* current byte, first=0 */ 
   int lineno;     /* line number, first=1 */ 
   int linestart;  /* byte current line started at */ 
}; 
 
static const struct location init_pos = {0, 1, 0}; 
 
struct piece 
{ 
   struct pike_string *s; 
   struct piece *next; 
}; 
 
static struct block_allocator piece_allocator 
    = BA_INIT_PAGES(sizeof(struct piece), 2); 
 
static INLINE struct piece * alloc_piece(void) { 
    struct piece * p = ba_alloc(&piece_allocator); 
    p->next = NULL; 
    return p; 
} 
 
static INLINE void really_free_piece(struct piece * p) { 
    free_string(p->s); 
    ba_free(&piece_allocator, p); 
} 
 
struct out_piece 
{ 
   struct svalue v; 
   struct out_piece *next; 
}; 
 
static struct block_allocator out_piece_allocator 
    = BA_INIT_PAGES(sizeof(struct out_piece), 2); 
 
static INLINE struct out_piece * alloc_out_piece(void) { 
    struct out_piece * p = ba_alloc(&out_piece_allocator); 
    p->next = NULL; 
    return p; 
} 
static INLINE void really_free_out_piece(struct out_piece * p) { 
    free_svalue(&p->v); 
    ba_free(&out_piece_allocator, p); 
} 
 
struct feed_stack 
{ 
   int ignore_data, parse_tags; 
 
   struct feed_stack *prev; 
 
   /* this is a feed-stack, ie 
      these contains the result of callbacks, 
      if they are to be parsed. 
 
      The bottom stack element is also the global feed 
      (parser_html_storage.top). */ 
 
   /* current position; if not local feed, use global feed */ 
   struct piece *local_feed; 
   ptrdiff_t c; 
 
   struct location pos; 
}; 
 
static struct block_allocator feed_stack_allocator 
    = BA_INIT_PAGES(sizeof(struct feed_stack), 1); 
static INLINE struct feed_stack * alloc_feed_stack(void) { 
    struct feed_stack * p = ba_alloc(&feed_stack_allocator); 
    p->local_feed = NULL; 
    return p; 
} 
static INLINE void really_free_feed_stack(struct feed_stack * p) { 
    while (p->local_feed) { 
        struct piece *f=p->local_feed; 
        p->local_feed=f->next; 
        really_free_piece (f); 
    } 
    ba_free(&feed_stack_allocator, p); 
} 
 
enum types { 
  TYPE_TAG,                     /* empty tag callback */ 
  TYPE_CONT,                    /* container tag callback */ 
  TYPE_ENTITY,                  /* entity */ 
  TYPE_QTAG,                    /* quote tag callback */ 
  TYPE_DATA                     /* data callback */ 
}; 
 
enum contexts { 
  CTX_DATA,                     /* in normal text data */ 
  CTX_TAG,                      /* in a tag, before an arg name */ 
  CTX_SPLICE_ARG,               /* in a splice arg */ 
  CTX_TAG_ARG,                  /* before an unquoted tag arg */ 
  CTX_TAG_QUOTED_ARG/*+ n*/,        /* in a tag arg quoted with quote n */ 
}; 
 
/* flag: case sensitive tag matching */ 
#define FLAG_CASE_INSENSITIVE_TAG     0x00000001 
 
/* flag: arg quote may have tag_end to end quote and tag */ 
#define FLAG_LAZY_END_ARG_QUOTE               0x00000002 
 
/* flag: the chars in lazy_entity_ends ends the search for entity ends */ 
#define FLAG_LAZY_ENTITY_END          0x00000004 
 
/* flag: match '<' and '>' for in-tag-tags (<foo <bar>>) */ 
#define FLAG_MATCH_TAG                        0x00000008 
 
/* flag: treat unknown tags and entities as text */ 
#define FLAG_IGNORE_UNKNOWN           0x00000020 
 
/* flag: debug output */ 
#define FLAG_DEBUG_MODE                       0x00000040 
 
/* flag: xml tag syntax */ 
#define FLAG_XML_TAGS                 0x00000080 
 
/* flag: strict html or xml syntax, corresponding to flags 0 or 3 to 
 * the xml_tag_syntax() function */ 
#define FLAG_STRICT_TAGS              0x00000100 
 
/* flag: allow whitespace before the tag name */ 
#define FLAG_WS_BEFORE_TAG_NAME               0x00000200 
 
/* flag: parse tags */ 
#define FLAG_PARSE_TAGS                       0x00000400 
 
/* flag: understand nestled entities (send it all to entity callback) */ 
/*       disabled by FLAG_LAZY_ENTITY_END */ 
#define FLAG_NESTLING_ENTITY_END      0x00000800 
 
/* flag: ignore --...-- in tag parsing (as in <!--...-->) */ 
#define FLAG_IGNORE_COMMENTS            0x00001000 
 
/* flag: reparse plain strings used as tag/container/entity callbacks */ 
#define FLAG_REPARSE_STRINGS          0x00002000 
 
/* flag: attribute quote stapling. (q=f"o'o"'b"a'r == q="fo'ob&quot;ar") */ 
#define FLAG_QUOTE_STAPLING           0x00004000 
 
/* default characters */ 
#define DEF_WS                ' ', '\n', '\r', '\t', '\v' 
#define DEF_TAG_START       '<' 
#define DEF_TAG_END '>' 
#define DEF_TAG_FIN '/' 
#define DEF_ENT_START       '&' 
#define DEF_ENT_END ';' 
#define DEF_ARGQ_STARTS     '\"', '\'' 
#define DEF_ARGQ_STOPS      '\"', '\'' 
#define DEF_EQ              '=' 
#define DEF_LAZY_EENDS      DEF_WS, DEF_TAG_START, DEF_TAG_END, DEF_TAG_FIN, \ 
                        DEF_ENT_START, DEF_ENT_END, DEF_ARGQ_STARTS, DEF_EQ 
#define DEF_TAG_COMMENT     '-' /* --..-- */ 
 
static const p_wchar2 argq_start[] = {DEF_ARGQ_STARTS}; 
static const p_wchar2 argq_stop[] = {DEF_ARGQ_STOPS}; 
static const p_wchar2 lazy_entity_ends[] = {DEF_LAZY_EENDS}; 
static const p_wchar2 whitespace[] = {DEF_WS}; 
 
#ifdef CONFIGURABLE_MARKUP 
 
#define MAX_ARGQ 8 
 
#define TAG_START(this) (this->tag_start) 
#define TAG_END(this) (this->tag_end) 
#define TAG_FIN(this) (this->tag_fin) 
#define ENTITY_START(this) (this->entity_start) 
#define ENTITY_END(this) (this->entity_end) 
#define NARGQ(this) (this->nargq) 
#define ARGQ_START(this) (this->argq_start) 
#define ARGQ_STOP(this) (this->argq_stop) 
#define ARG_EQ(this) (this->arg_eq) 
#define LAZY_ENTITY_ENDS(this) (this->lazy_entity_ends) 
#define N_LAZY_ENTITY_ENDS(this) (this->n_lazy_entity_ends) 
#define TAG_COMMENT(this) DEF_TAG_COMMENT 
#define WS(this) (this->ws) 
#define N_WS(this) (this->n_ws) 
 
#else 
 
#define MAX_ARGQ NELEM (argq_start) 
 
static const p_wchar2 tag_start = DEF_TAG_START, tag_end = DEF_TAG_END, 
  tag_fin = DEF_TAG_FIN, entity_start = DEF_ENT_START, entity_end = DEF_ENT_END, 
  arg_eq = DEF_EQ; 
#define TAG_START(this) tag_start 
#define TAG_END(this) tag_end 
#define TAG_FIN(this) tag_fin 
#define ENTITY_START(this) entity_start 
#define ENTITY_END(this) entity_end 
#define NARGQ(this) NELEM (argq_start) 
#define ARGQ_START(this) argq_start 
#define ARGQ_STOP(this) argq_stop 
#define ARG_EQ(this) arg_eq 
#define LAZY_ENTITY_ENDS(this) lazy_entity_ends 
#define N_LAZY_ENTITY_ENDS(this) NELEM (lazy_entity_ends) 
#define TAG_COMMENT(this) DEF_TAG_COMMENT 
#define WS(this) whitespace 
#define N_WS(this) NELEM (whitespace) 
 
#endif 
 
struct calc_chars           /* Calculated char sets. */ 
{ 
   /* whitespace + end argument ('=', '<', '>' and '/') + 
    * start quote + start entity + tag comment */ 
#ifdef CONFIGURABLE_MARKUP 
   p_wchar2 *arg_break_chars; 
#else 
   p_wchar2 arg_break_chars[N_WS(0) + 4 + NARGQ(0) + 2]; 
#endif 
   size_t n_arg_break_chars; 
 
   /* end argument ('=', '>' and '/') + start quote */ 
   p_wchar2 look_for_start[MAX_ARGQ+5]; 
   size_t num_look_for_start; 
 
   /* end(s) of _this_ arg quote */ 
   p_wchar2 look_for_end[MAX_ARGQ][MAX_ARGQ+4]; 
   size_t num_look_for_end[MAX_ARGQ]; 
}; 
 
#ifdef CONFIGURABLE_MARKUP 
 
#define ARG_BREAK_CHARS(this) (this->cc.arg_break_chars) 
#define N_ARG_BREAK_CHARS(this) (this->cc.n_arg_break_chars) 
#define LOOK_FOR_START(this) (this->cc.look_for_start) 
#define NUM_LOOK_FOR_START(this) (this->cc.num_look_for_start) 
#define LOOK_FOR_END(this) (this->cc.look_for_end) 
#define NUM_LOOK_FOR_END(this) (this->cc.num_look_for_end) 
 
#define TAG_END_STRING(this) make_shared_binary_string2 (&TAG_END (this), 1) 
#define TAG_FIN_STRING(this) make_shared_binary_string2 (&TAG_FIN (this), 1) 
 
#else 
 
/* All combinations of the flags that affect the calculated char sets: 
 * 
 * o  (this->flags & (FLAG_STRICT_TAGS|FLAG_XML_TAGS)) == FLAG_STRICT_TAGS 
 * o  this->flags & FLAG_LAZY_END_ARG_QUOTE 
 * o  this->flags & FLAG_IGNORE_COMMENTS 
 */ 
static struct calc_chars char_variants[ 1 << 3]; 
 
#define ARG_BREAK_CHARS(this) (this->cc->arg_break_chars) 
#define N_ARG_BREAK_CHARS(this) (this->cc->n_arg_break_chars) 
#define LOOK_FOR_START(this) (this->cc->look_for_start) 
#define NUM_LOOK_FOR_START(this) (this->cc->num_look_for_start) 
#define LOOK_FOR_END(this) (this->cc->look_for_end) 
#define NUM_LOOK_FOR_END(this) (this->cc->num_look_for_end) 
 
static struct pike_string *tag_end_string, *tag_fin_string; 
#define TAG_END_STRING(this) (add_ref (tag_end_string), tag_end_string) 
#define TAG_FIN_STRING(this) (add_ref (tag_fin_string), tag_fin_string) 
 
#endif 
 
struct parser_html_storage 
{ 
/*--- current state -----------------------------------------------*/ 
 
   /* Where new fed info is added. */ 
   struct piece *feed_end; 
 
   /* Feed chain waiting to be passed to the data callback. */ 
   struct piece *data_cb_feed, *data_cb_feed_end; 
   struct location data_cb_pos; 
 
   /* resulting data */ 
   struct out_piece *out,*out_end; 
 
   /* Upper bound for the string shift in the output queue. -1 when in 
    * mixed mode. */ 
   int out_max_shift; 
 
   /* Total length of all strings in the output queue, or the number 
    * of values in it when in mixed mode. */ 
   ptrdiff_t out_length; 
 
   /* The current context in the output queue. */ 
   enum contexts out_ctx; 
 
   /* parser stack */ 
   struct feed_stack *stack; 
   struct feed_stack top; 
   int stack_count; 
   int max_stack_depth; 
 
   /* current range (ie, current tag/entity/whatever) */ 
   /* start is also used to flag callback recursion */ 
   struct piece *start,*end; 
   ptrdiff_t cstart,cend; 
 
   /* the type of the current thing being parsed */ 
   enum types type; 
 
/*--- user configurable -------------------------------------------*/ 
 
  /* extra arguments */ 
   struct array *extra_args; 
 
   /* named stuff */ 
   struct mapping *maptag; 
   struct mapping *mapcont; 
   struct mapping *mapentity; 
   struct mapping *mapqtag; 
 
   /* mapqtag is indexed on the first two chars in each tag name. The 
    * values are concatenated tuples of ({name, to_do, end}), where 
    * every name that's a prefix of some other name comes after it. */ 
 
   /* string to use for magic splice argument */ 
   struct pike_string *splice_arg; 
 
   /* callback functions */ 
   struct svalue callback__tag; 
   struct svalue callback__data; 
   struct svalue callback__entity; 
 
   /* flag bitfield; see FLAG_* above */ 
   int flags; 
 
#ifdef CONFIGURABLE_MARKUP 
   p_wchar2 tag_start,tag_end,tag_fin; /* tag_fin is the '/'s in <t/><c></c> */ 
   p_wchar2 entity_start,entity_end; 
   size_t nargq; 
   p_wchar2 argq_start[MAX_ARGQ],argq_stop[MAX_ARGQ]; 
   p_wchar2 arg_eq; /* = as in foo=bar */ 
 
   p_wchar2 *lazy_entity_ends; 
   size_t n_lazy_entity_ends; 
 
   p_wchar2 *ws; 
   size_t n_ws; 
 
   struct calc_chars cc; 
#else 
   struct calc_chars *cc; 
#endif 
}; 
 
/* P_WAIT was already used by MSVC++ :(  /Hubbe */ 
typedef enum { STATE_DONE=0, STATE_WAIT, STATE_REREAD, STATE_REPARSE } newstate; 
 
#ifdef THIS 
#undef THIS /* Needed for NT */ 
#endif 
 
#define THIS ((struct parser_html_storage*)(Pike_fp->current_storage)) 
#define THISOBJ (Pike_fp->current_object) 
 
static void tag_name(struct parser_html_storage *this, 
                     struct piece *feed, ptrdiff_t c, int skip_tag_start); 
static void tag_args(struct parser_html_storage *this, 
                     struct piece *feed, ptrdiff_t c,struct svalue *def, 
                     int skip_name, int to_tag_end); 
 
static int quote_tag_lookup (struct parser_html_storage *this, 
                             struct piece *feed, ptrdiff_t c, 
                             struct piece **destp, ptrdiff_t *d_p, 
                             int finished, struct svalue **mapqentry); 
 
/****** debug helper ********************************/ 
 
/* Avoid loss of precision warnings. */ 
#ifdef __ECL 
static inline long TO_LONG(ptrdiff_t x) 
{ 
  return DO_NOT_WARN((long)x); 
} 
#else /* !__ECL */ 
#define TO_LONG(x)  ((long)(x)) 
#endif /* __ECL */ 
 
#ifdef HTML_DEBUG 
static void debug_mark_spot(char *desc,struct piece *feed,int c) 
{ 
   ptrdiff_t l, i, i0, m; 
   char buf[40]; 
 
   if (!(THIS->flags & FLAG_DEBUG_MODE)) return; 
 
   l=strlen(desc)+1; 
   if (l<40) l=40; 
   m=75-l; if (m<10) m=10; 
   fprintf(stderr,"%-*s `",DO_NOT_WARN((int)l),desc); 
   i=c-m/2; 
   if (i+m>=feed->s->len) i=feed->s->len-m; 
   if (i<0) i=0; 
   for (i0=i;i<feed->s->len && i-i0<m;i++) 
   { 
      p_wchar2 ch=index_shared_string(feed->s,i); 
      /* if (i==c) { fprintf(stderr,"^"); continue; }*/ 
      if (ch<32 || (ch>126 && ch<160) || ch>255) 
         fprintf(stderr,"."); 
      else 
         fprintf(stderr,"%c",ch); 
   } 
 
   sprintf(buf,"(%ld) %p:%d/%ld    ^", 
           DO_NOT_WARN((long)i0), 
           (void *)feed, c, 
           DO_NOT_WARN((long)feed->s->len)); 
   fprintf(stderr,"`\n%*s\n", 
           DO_NOT_WARN((int)(l+c-i0+3)), 
           buf); 
} 
 
static void debug_print_chars (const p_wchar2 *chars, size_t numchars) 
{ 
  size_t i; 
  for (i=0; i<numchars; i++) 
    if (i > 30) { 
      fprintf (stderr, "... (number of chars suspiciously large: " 
               "%"PRINTSIZET"u)", numchars); 
      break; 
    } 
    else if (chars[i]<33 || (chars[i]>126 && chars[i]<160) 
             || chars[i]>255) 
      fprintf(stderr,"%d ",chars[i]); 
    else 
      fprintf(stderr,"'%c' ",chars[i]); 
  fprintf(stderr,"\n"); 
} 
 
static void debug_print_search_chars (struct parser_html_storage *this) 
{ 
  size_t i; 
  fprintf (stderr, "  arg_break_chars: "); 
  debug_print_chars (ARG_BREAK_CHARS (this), N_ARG_BREAK_CHARS (this)); 
  fprintf (stderr, "  look_for_start:  "); 
  debug_print_chars (LOOK_FOR_START (this), NUM_LOOK_FOR_START (this)); 
  for (i = 0; i < NARGQ (this); i++) { 
    fprintf (stderr, "  look_for_end[%"PRINTSIZET"u]: ", i); 
    debug_print_chars (LOOK_FOR_END (this)[i], NUM_LOOK_FOR_END (this)[i]); 
  } 
} 
#endif 
 
/****** init & exit *********************************/ 
 
static INLINE void reset_stack_head (struct parser_html_storage *this) 
{ 
   this->top.ignore_data=0; 
   this->top.parse_tags=this->flags & FLAG_PARSE_TAGS; 
   this->top.pos=init_pos; 
   this->top.c=0; 
} 
 
static void reset_feed(struct parser_html_storage *this) 
{ 
   struct feed_stack *st; 
 
   /* kill top feed */ 
 
   while (this->top.local_feed) 
   { 
      struct piece *f=this->top.local_feed; 
      this->top.local_feed=f->next; 
      really_free_piece (f); 
   } 
   this->feed_end=NULL; 
 
   /* kill data cb feed */ 
 
   while (this->data_cb_feed) 
   { 
      struct piece *f=this->data_cb_feed; 
      this->data_cb_feed=f->next; 
      really_free_piece (f); 
   } 
   this->data_cb_feed_end=NULL; 
 
   /* kill out-feed */ 
 
   while (this->out) 
   { 
      struct out_piece *f=this->out; 
      this->out=f->next; 
      really_free_out_piece (f); 
   } 
   if (this->out_max_shift > 0) this->out_max_shift = 0; 
   this->out_length = 0; 
   this->out_ctx = CTX_DATA; 
 
   /* Free stack and init new stack head. */ 
 
   while (1) { 
     st=this->stack; 
     if (!st->prev) break; 
     this->stack=st->prev; 
     really_free_feed_stack (st); 
   } 
   this->stack_count=0; 
   reset_stack_head (this); 
} 
 
static void calculate_chars ( 
#ifdef CONFIGURABLE_MARKUP 
  struct parser_html_storage *this 
#define CC (&this->cc) 
#define FLAGS (this->flags) 
#else 
  struct calc_chars *CC, int FLAGS 
#endif 
) 
{ 
   size_t n,i,j,k; 
   int check_fin = (FLAGS & (FLAG_STRICT_TAGS|FLAG_XML_TAGS)) != FLAG_STRICT_TAGS; 
   p_wchar2 *ws_or_endarg; 
   ptrdiff_t n_ws_or_endarg; 
 
   /* prepare look for start of argument quote or end of tag */ 
   CC->look_for_start[0] = TAG_END (this); 
   CC->look_for_start[1] = ARG_EQ (this); 
   CC->look_for_start[2] = TAG_START (this); 
   if (check_fin) { 
     CC->look_for_start[3] = TAG_FIN (this); 
     n=4; 
   } 
   else n=3; 
   for (i=0; i < NARGQ (this); i++) 
   { 
      for (j=0; j<n; j++) 
         if (CC->look_for_start[j] == ARGQ_START (this)[i]) goto found_start; 
      CC->look_for_start[n++] = ARGQ_START (this)[i]; 
found_start: 
      ; 
   } 
   CC->num_look_for_start=n; 
#ifdef PIKE_DEBUG 
   if (n > NELEM (CC->look_for_start)) 
     Pike_fatal ("Static size of calc_chars.look_for_start is " 
                 "%"PRINTSIZET"d but %"PRINTSIZET"d required.\n", 
                 NELEM (CC->look_for_start), n); 
#endif 
 
   for (k=0; k < NARGQ (this); k++) 
   { 
      n=0; 
      CC->look_for_end[k][n++] = ARGQ_STOP (this)[k]; 
      CC->look_for_end[k][n++] = ENTITY_START (this); 
      for (i=0; i < NARGQ (this); i++) 
         if (ARGQ_START (this)[k] == ARGQ_START (this)[i]) 
         { 
            for (j=0; j < NARGQ (this); j++) 
               if (CC->look_for_end[k][j] == ARGQ_START (this)[i]) 
                  goto found_end; 
            CC->look_for_end[k][n++] = ARGQ_START (this)[i]; 
   found_end: 
            ; 
         } 
      if (FLAGS & FLAG_LAZY_END_ARG_QUOTE) { 
         CC->look_for_end[k][n++] = TAG_END (this); 
         if (check_fin) CC->look_for_end[k][n++] = TAG_FIN (this); 
      } 
 
      CC->num_look_for_end[k]=n; 
#ifdef PIKE_DEBUG 
      if (n > NELEM (CC->look_for_end[k])) 
        Pike_fatal ("Static size of calc_chars.look_for_end[k] is " 
                    "%"PRINTSIZET"d but %"PRINTSIZET"d required.\n", 
                    NELEM (CC->look_for_end[k]), n); 
#endif 
   } 
#ifdef PIKE_DEBUG 
   if (k > NELEM (CC->look_for_end)) 
     Pike_fatal ("Static size of calc_chars.look_for_end is " 
                 "%"PRINTSIZET"d but %"PRINTSIZET"d required.\n", 
                 NELEM (CC->look_for_end), k); 
#endif 
 
   n = check_fin ? 4 : 3; 
   n_ws_or_endarg = (ptrdiff_t)(N_WS(this) + n); 
   ws_or_endarg=alloca(sizeof(p_wchar2)*n_ws_or_endarg); 
   if (!ws_or_endarg) Pike_error ("Out of stack.\n"); 
   memcpy(ws_or_endarg+n, WS (this), N_WS (this) * sizeof(p_wchar2)); 
   ws_or_endarg[0] = ARG_EQ (this); 
   ws_or_endarg[1] = TAG_END (this); 
   ws_or_endarg[2] = TAG_START (this); 
   if (check_fin) ws_or_endarg[3] = TAG_FIN (this); 
 
   CC->n_arg_break_chars= 
      n_ws_or_endarg + NARGQ (this) + 1 + 
      ((FLAGS & FLAG_IGNORE_COMMENTS)?1:0); 
 
#ifdef CONFIGURABLE_MARKUP 
   if (CC->arg_break_chars) 
   { 
      free(CC->arg_break_chars); 
      CC->arg_break_chars=NULL; 
   } 
   CC->arg_break_chars= 
      (p_wchar2*)xalloc(sizeof(p_wchar2)*(CC->n_arg_break_chars)); 
#else 
#ifdef PIKE_DEBUG 
   if (CC->n_arg_break_chars > NELEM (CC->arg_break_chars)) 
     Pike_fatal ("Static size of calc_chars.arg_break_chars is " 
                 "%"PRINTSIZET"d but %"PRINTSIZET"d required.\n", 
                 NELEM (CC->arg_break_chars), CC->n_arg_break_chars); 
#endif 
#endif 
 
   memcpy(CC->arg_break_chars, ws_or_endarg, 
          n_ws_or_endarg*sizeof(p_wchar2)); 
 
   memcpy(CC->arg_break_chars+n_ws_or_endarg, 
          ARGQ_START (this), NARGQ (this) * sizeof(p_wchar2)); 
 
   CC->arg_break_chars[CC->n_arg_break_chars-1] = ENTITY_START (this); 
   if ((FLAGS & FLAG_IGNORE_COMMENTS)) 
      CC->arg_break_chars[CC->n_arg_break_chars-2] = TAG_COMMENT (this); 
} 
#undef CC 
#undef FLAGS 
 
#ifdef CONFIGURABLE_MARKUP 
#define init_calc_chars() 
#define exit_calc_chars() 
#define recalculate_argq(this) \ 
  calculate_chars (this); \ 
  DEBUG ((stderr, "Recalculated search chars:\n"); \ 
         debug_print_search_chars (this)) 
#else 
#define recalculate_argq(this) \ 
  this->cc = select_variant (this->flags); \ 
  DEBUG ((stderr, "Using precalculated search chars:\n"); \ 
         debug_print_search_chars (this)) 
 
static INLINE void init_calc_chars(void) 
{ 
  int i; 
  for (i = 0; i < 1 << 3; i++) 
    calculate_chars (char_variants + i, 
                     ((i & 1) ? FLAG_STRICT_TAGS : 0) | 
                     ((i & 2) ? FLAG_LAZY_END_ARG_QUOTE : 0) | 
                     ((i & 4) ? FLAG_IGNORE_COMMENTS : 0)); 
 
  tag_end_string = make_shared_binary_string2 (&TAG_END (0), 1); 
  tag_fin_string = make_shared_binary_string2 (&TAG_FIN (0), 1); 
} 
 
static INLINE void exit_calc_chars(void) 
{ 
  free_string (tag_end_string); 
  free_string (tag_fin_string); 
} 
 
static INLINE struct calc_chars *select_variant (int flags) 
{ 
  return char_variants + 
    (((flags & (FLAG_STRICT_TAGS|FLAG_XML_TAGS)) == FLAG_STRICT_TAGS ? 1 : 0) | 
     (flags & FLAG_LAZY_END_ARG_QUOTE ? 2 : 0) | 
     (flags & FLAG_IGNORE_COMMENTS ? 4 : 0)); 
} 
#endif 
 
static void init_html_struct(struct object *UNUSED(o)) 
{ 
#ifdef HTML_DEBUG 
   THIS->flags=0; 
#endif 
   DEBUG((stderr,"init_html_struct %p\n",THIS)); 
 
   /* state */ 
   THIS->start=NULL; 
 
#ifdef CONFIGURABLE_MARKUP 
   /* default set */ 
   THIS->tag_start=DEF_TAG_START; 
   THIS->tag_end=DEF_TAG_END; 
   THIS->tag_fin=DEF_TAG_FIN; 
   THIS->entity_start=DEF_ENT_START; 
   THIS->entity_end=DEF_ENT_END; 
   THIS->nargq=NELEM(argq_start); 
   memcpy(THIS->argq_start,argq_start,sizeof(argq_start)); 
   memcpy(THIS->argq_stop,argq_stop,sizeof(argq_stop)); 
   THIS->arg_eq=DEF_EQ; 
 
   /* allocated stuff */ 
   THIS->lazy_entity_ends=NULL; 
   THIS->ws=NULL; 
   THIS->cc.arg_break_chars=NULL; 
#endif 
 
   THIS->flags=FLAG_MATCH_TAG|FLAG_PARSE_TAGS; 
 
   /* initialize feed */ 
   THIS->data_cb_feed = NULL; 
   THIS->out=NULL; 
   THIS->out_length = THIS->out_max_shift = 0; 
   THIS->stack = &THIS->top; 
   THIS->top.prev = NULL; 
   THIS->top.local_feed = NULL; 
   reset_feed(THIS); 
 
   /* settings */ 
   THIS->max_stack_depth=MAX_FEED_STACK_DEPTH; 
 
   /* this may now throw */ 
 
#ifdef CONFIGURABLE_MARKUP 
   THIS->lazy_entity_ends=(p_wchar2*)xalloc(sizeof(lazy_entity_ends)); 
   memcpy(THIS->lazy_entity_ends,lazy_entity_ends,sizeof(lazy_entity_ends)); 
   THIS->n_lazy_entity_ends=NELEM(lazy_entity_ends); 
 
   THIS->ws=(p_wchar2*)xalloc(sizeof(whitespace)); 
   memcpy(THIS->ws,whitespace,sizeof(whitespace)); 
   THIS->n_ws=NELEM(whitespace); 
#endif 
 
   THIS->maptag=allocate_mapping(32); 
   THIS->mapcont=allocate_mapping(32); 
   THIS->mapentity=allocate_mapping(32); 
   THIS->mapqtag=allocate_mapping(8); 
 
   recalculate_argq(THIS); 
} 
 
static void exit_html_struct(struct object *UNUSED(o)) 
{ 
   DEBUG((stderr,"exit_html_struct %p\n",THIS)); 
 
   reset_feed(THIS); 
 
#ifdef CONFIGURABLE_MARKUP 
   if (THIS->lazy_entity_ends) free(THIS->lazy_entity_ends); 
   if (THIS->ws) free(THIS->ws); 
   if (THIS->cc.arg_break_chars) free(THIS->cc.arg_break_chars); 
#endif 
 
   DEBUG((stderr,"exit_html_struct %p done\n",THIS)); 
} 
 
/****** setup callbacks *****************************/ 
 
/*! @decl Parser.HTML _set_tag_callback(function|string|array to_call) 
 *! @decl Parser.HTML _set_entity_callback(function|string|array to_call) 
 *! @decl Parser.HTML _set_data_callback(function|string|array to_call) 
 *! 
 *! These functions set up the parser object to call the given 
 *! callbacks upon tags, entities and/or data. The callbacks will 
 *! @i{only@} be called if there isn't another tag/container/entity 
 *! handler for these. 
 *! 
 *! The callback function will be called with the parser object as 
 *! first argument, and the active string as second. Note that no 
 *! parsing of the contents has been done. Both endtags and normal 
 *! tags are called; there is no container parsing. 
 *! 
 *! The return values from the callbacks are handled in the same way 
 *! as the return values from callbacks registered with @[add_tag] and 
 *! similar functions. 
 *! 
 *! The data callback will be called as seldom as possible with the 
 *! longest possible string, as long as it doesn't get called out of 
 *! order with any other callback. It will never be called with a zero 
 *! length string. 
 *! 
 *! If a string or array is given instead of a function, it 
 *! will act as the return value from the function. Arrays 
 *! or empty strings is probably preferable to avoid recursion. 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 */ 
 
static void html__set_tag_callback(INT32 args) 
{ 
   if (!args) Pike_error("_set_tag_callback: too few arguments\n"); 
   dmalloc_touch_svalue(sp-args); 
   assign_svalue(&(THIS->callback__tag),sp-args); 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void html__set_data_callback(INT32 args) 
{ 
   if (!args) Pike_error("_set_data_callback: too few arguments\n"); 
   dmalloc_touch_svalue(sp-args); 
   assign_svalue(&(THIS->callback__data),sp-args); 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void html__set_entity_callback(INT32 args) 
{ 
   if (!args) Pike_error("_set_entity_callback: too few arguments\n"); 
   dmalloc_touch_svalue(sp-args); 
   assign_svalue(&(THIS->callback__entity),sp-args); 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
/*! @decl Parser.HTML add_tag(string name,mixed to_do) 
 *! @decl Parser.HTML add_container(string name,mixed to_do) 
 *! @decl Parser.HTML add_entity(string entity,mixed to_do) 
 *! @decl Parser.HTML add_quote_tag(string name,mixed to_do,string end) 
 *! @decl Parser.HTML add_tags(mapping(string:mixed) tags) 
 *! @decl Parser.HTML add_containers(mapping(string:mixed) containers) 
 *! @decl Parser.HTML add_entities(mapping(string:mixed) entities) 
 *! 
 *! Registers the actions to take when parsing various things. Tags, 
 *! containers, entities are as usual. @[add_quote_tag()] adds a special 
 *! kind of tag that reads any data until the next occurrence of the 
 *! end string immediately before a tag end. 
 *! 
 *! @param to_do 
 *!   This argument can be any of the following. 
 *! 
 *!   @mixed 
 *!     @type function 
 *!       The function will be called as a callback function. It will 
 *!       get the following arguments, depending on the type of 
 *!       callback. 
 *!     @pre{ 
 *! mixed tag_callback(Parser.HTML parser,mapping args,mixed ... extra) 
 *! mixed container_callback(Parser.HTML parser,mapping args,string content,mixed ... extra) 
 *! mixed entity_callback(Parser.HTML parser,mixed ... extra) 
 *! mixed quote_tag_callback(Parser.HTML parser,string content,mixed ... extra) 
 *!     @} 
 *! 
 *!     @type string 
 *!       This tag/container/entity is then replaced by the string. 
 *!       The string is normally not reparsed, i.e. it's equivalent to 
 *!       writing a function that returns the string in an array (but 
 *!       a lot faster). If @[reparse_strings] is set the string will 
 *!       be reparsed, though. 
 *! 
 *!     @type array 
 *!       The first element is a function as above. It will receive 
 *!       the rest of the array as extra arguments. If extra arguments 
 *!       are given by @[set_extra()], they will appear after 
 *!       the ones in this array. 
 *! 
 *!     @type int(0..) 
 *!       If there is a tag/container/entity with the given name in 
 *!       the parser, it's removed. 
 *! 
 *!   @endmixed 
 *! 
 *! The callback function can return: 
 *! 
 *!   @mixed 
 *! 
 *!     @type string 
 *!       This string will be pushed on the parser stack and be 
 *!       parsed. Be careful not to return anything in this way that 
 *!       could lead to a infinite recursion. 
 *! 
 *!     @type array 
 *!       The element(s) of the array is the result of the function. 
 *!       This will not be parsed. This is useful for avoiding 
 *!       infinite recursion. The array can be of any size, this means 
 *!       the empty array is the most effective to return if you don't 
 *!       care about the result. If the parser is operating in 
 *!       @[mixed_mode], the array can contain anything. Otherwise 
 *!       only strings are allowed. 
 *! 
 *!     @type int(0..0) 
 *!       This means "don't do anything", ie the item that generated 
 *!       the callback is left as it is, and the parser continues. 
 *! 
 *!     @type int(1..1) 
 *!       Reparse the last item again. This is useful to parse a tag 
 *!       as a container, or vice versa: just add or remove callbacks 
 *!       for the tag and return this to jump to the right callback. 
 *!   @endmixed 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 *! 
 *! @seealso 
 *!   @[tags], @[containers], @[entities] 
 *! 
 */ 
 
static void html_add_tag(INT32 args) 
{ 
   check_all_args("add_tag",args,BIT_STRING, 
                  BIT_INT|BIT_STRING|BIT_ARRAY|BIT_FUNCTION|BIT_OBJECT|BIT_PROGRAM,0); 
   if (TYPEOF(sp[1-args]) == T_ARRAY) { 
     struct array *a = sp[1-args].u.array; 
     if (!a->size || 
         (TYPEOF(a->item[0]) != T_FUNCTION && TYPEOF(a->item[0]) != T_OBJECT && 
          TYPEOF(a->item[0]) != T_PROGRAM)) 
       SIMPLE_BAD_ARG_ERROR("add_tag", 1, "array with function as first element"); 
   } 
   else if (TYPEOF(sp[1-args]) == T_INT && sp[1-args].u.integer) 
     SIMPLE_BAD_ARG_ERROR("add_tag", 1, "zero, string, array or function"); 
 
   if (THIS->maptag->refs>1) 
   { 
      push_mapping(THIS->maptag); 
      THIS->maptag=copy_mapping(THIS->maptag); 
      pop_stack(); 
   } 
 
   if (THIS->flags & FLAG_CASE_INSENSITIVE_TAG) { 
     stack_swap(); 
     f_lower_case(1); 
     stack_swap(); 
   } 
 
   if (UNSAFE_IS_ZERO(sp-1)) 
     map_delete(THIS->maptag,sp-2); 
   else 
     mapping_insert(THIS->maptag,sp-2,sp-1); 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void html_add_container(INT32 args) 
{ 
   check_all_args("add_container",args,BIT_STRING, 
                  BIT_INT|BIT_STRING|BIT_ARRAY|BIT_FUNCTION|BIT_OBJECT|BIT_PROGRAM,0); 
   if (TYPEOF(sp[1-args]) == T_ARRAY) { 
     struct array *a = sp[1-args].u.array; 
     if (!a->size || 
         (TYPEOF(a->item[0]) != T_FUNCTION && TYPEOF(a->item[0]) != T_OBJECT && 
          TYPEOF(a->item[0]) != T_PROGRAM)) 
       SIMPLE_BAD_ARG_ERROR("add_container", 1, "array with function as first element"); 
   } 
   else if (TYPEOF(sp[1-args]) == T_INT && sp[1-args].u.integer) 
     SIMPLE_BAD_ARG_ERROR("add_tag", 1, "zero, string, array or function"); 
 
   if (args > 2) { 
     pop_n_elems(args-2); 
     args = 2; 
   } 
 
   if (THIS->mapcont->refs>1) 
   { 
      push_mapping(THIS->mapcont); 
      THIS->mapcont=copy_mapping(THIS->mapcont); 
      pop_stack(); 
   } 
 
   if (THIS->flags & FLAG_CASE_INSENSITIVE_TAG) { 
     stack_swap(); 
     f_lower_case(1); 
     stack_swap(); 
   } 
 
   if (UNSAFE_IS_ZERO(sp-1)) 
     map_delete(THIS->mapcont,sp-2); 
   else 
     mapping_insert(THIS->mapcont,sp-2,sp-1); 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void html_add_entity(INT32 args) 
{ 
   check_all_args("add_entity",args,BIT_STRING, 
                  BIT_INT|BIT_STRING|BIT_ARRAY|BIT_FUNCTION|BIT_OBJECT|BIT_PROGRAM,0); 
   if (TYPEOF(sp[1-args]) == T_ARRAY) { 
     struct array *a = sp[1-args].u.array; 
     if (!a->size || 
         (TYPEOF(a->item[0]) != T_FUNCTION && TYPEOF(a->item[0]) != T_OBJECT && 
          TYPEOF(a->item[0]) != T_PROGRAM)) 
       SIMPLE_BAD_ARG_ERROR("add_entity", 1, "array with function as first element"); 
   } 
   else if (TYPEOF(sp[1-args]) == T_INT && sp[1-args].u.integer) 
     SIMPLE_BAD_ARG_ERROR("add_tag", 1, "zero, string, array or function"); 
 
   if (THIS->mapentity->refs>1) 
   { 
      push_mapping(THIS->mapentity); 
      THIS->mapentity=copy_mapping(THIS->mapentity); 
      pop_stack(); 
   } 
 
   if (UNSAFE_IS_ZERO(sp-1)) 
     map_delete(THIS->mapentity,sp-2); 
   else 
     mapping_insert(THIS->mapentity,sp-2,sp-1); 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void html_add_quote_tag(INT32 args) 
{ 
  int remove; 
  struct mapping *map; 
  struct pike_string *name; 
  struct pike_string *prefix; 
  struct svalue *val; 
  ONERROR uwp; 
 
  check_all_args("add_quote_tag",args,BIT_STRING, 
                 BIT_INT|BIT_STRING|BIT_ARRAY|BIT_FUNCTION|BIT_OBJECT|BIT_PROGRAM, 
                 BIT_STRING|BIT_VOID,0); 
   if (TYPEOF(sp[1-args]) == T_ARRAY) { 
     struct array *a = sp[1-args].u.array; 
     if (!a->size || 
         (TYPEOF(a->item[0]) != T_FUNCTION && TYPEOF(a->item[0]) != T_OBJECT && 
          TYPEOF(a->item[0]) != T_PROGRAM)) 
       SIMPLE_BAD_ARG_ERROR("add_quote_tag", 1, "array with function as first element"); 
   } 
   else if (TYPEOF(sp[1-args]) == T_INT && sp[1-args].u.integer) 
     SIMPLE_BAD_ARG_ERROR("add_tag", 1, "zero, string, array or function"); 
 
  remove = UNSAFE_IS_ZERO (sp+1-args); 
  if (!remove && args < 3) 
    SIMPLE_TOO_FEW_ARGS_ERROR ("add_quote_tag", 3); 
 
  if (THIS->mapqtag->refs>1) 
  { 
    push_mapping(THIS->mapqtag); 
    /* Note: Deferred copy; the arrays might still have only one ref. */ 
    THIS->mapqtag=copy_mapping(THIS->mapqtag); 
    pop_stack(); 
  } 
  map = THIS->mapqtag; 
 
  if (!remove) { 
    struct pike_string *end = TAG_END_STRING (THIS); 
    pop_n_elems (args-3); 
    args = 3; 
    push_string (end); 
    f_add (2); 
  } 
 
  name = dmalloc_touch (struct pike_string *, sp[-args].u.string); 
  if (name->len > 2) 
    prefix = string_slice (name, 0, 2); 
  else 
    copy_shared_string (prefix, name); 
  SET_ONERROR (uwp, do_free_string, prefix); 
 
  val = low_mapping_string_lookup (map, prefix); 
  if (val) { 
    int i; 
    struct array *arr; 
#ifdef PIKE_DEBUG 
    if (TYPEOF(*val) != T_ARRAY) 
      Pike_fatal ("Expected array as value in mapqtag.\n"); 
#endif 
    arr = val->u.array; 
 
    for (i = 0; i < arr->size; i += 3) { 
      struct pike_string *curname; 
#ifdef PIKE_DEBUG 
      if (TYPEOF(arr->item[i]) != T_STRING) 
        Pike_fatal ("Expected string as name in mapqtag.\n"); 
#endif 
      curname = dmalloc_touch (struct pike_string *, arr->item[i].u.string); 
 
      if (curname == name) { 
        if (remove) 
          if (arr->size == 3) { 
            struct svalue tmp; 
            SET_SVAL(tmp, T_STRING, 0, string, prefix); 
            map_delete (map, &tmp); 
          } 
          else { 
            if (arr->refs > 1 || mapping_data_is_shared (map)) { 
              push_array (arr = copy_array (arr)); 
              mapping_string_insert (map, prefix, sp - 1); 
              pop_stack(); 
            } 
            free_svalues (arr->item+i, 3, BIT_MIXED); 
            memmove (arr->item+i, arr->item+i+3, (arr->size-i-3) * sizeof(struct svalue)); 
            arr->size -= 3; 
          } 
        else { 
          if (arr->refs > 1 || mapping_data_is_shared (map)) { 
            push_array (arr = copy_array (arr)); 
            mapping_string_insert (map, prefix, sp - 1); 
            pop_stack(); 
          } 
          assign_svalue (arr->item+i+1, sp-2); 
          assign_svalue (arr->item+i+2, sp-1); 
        } 
        pop_n_elems (args); 
        goto done; 
      } 
 
      else if (curname->len < name->len && !remove) { 
        struct pike_string *cmp = string_slice (name, 0, curname->len); 
        if (cmp == curname) { /* Found a shorter prefix to name; insert before. */ 
          free_string (cmp); 
          if (arr->refs > 1 || mapping_data_is_shared (map)) { 
            arr = copy_array (arr); 
            push_array (arr = resize_array (arr, arr->size+3)); 
            mapping_string_insert (map, prefix, sp - 1); 
            pop_stack(); 
          } 
          else 
            arr = val->u.array = resize_array (arr, arr->size+3); 
          memmove (arr->item+i+3, arr->item+i, 
                  (arr->size-i-3) * sizeof(struct svalue)); 
          memcpy (arr->item+i, sp-=3, 3 * sizeof(struct svalue)); 
          goto done; 
        } 
        else free_string (cmp); 
      } 
    } 
 
    if (!remove) { 
      if (arr->refs > 1 || mapping_data_is_shared (map)) { 
        arr = copy_array (arr); 
        push_array (arr = resize_array (arr, arr->size+3)); 
        mapping_string_insert (map, prefix, sp - 1); 
        pop_stack(); 
      } 
      else 
        arr = val->u.array = resize_array (arr, arr->size+3); 
      memcpy (arr->item+arr->size-3, sp-=3, 3 * sizeof(struct svalue)); 
    } 
 
  done: ; 
  } 
 
  else if (!remove) { 
    f_aggregate (3); 
    mapping_string_insert (map, prefix, sp-1); 
    pop_stack(); 
  } 
 
  free_string (prefix); 
  UNSET_ONERROR (uwp); 
 
  ref_push_object(THISOBJ); 
} 
 
static void html_add_tags(INT32 args) 
{ 
   INT32 e; 
   struct keypair *k; 
   struct mapping_data *md; 
   check_all_args("add_tags",args,BIT_MAPPING,0); 
 
   md = sp[-1].u.mapping->data; 
 
   NEW_MAPPING_LOOP(md) 
      { 
         push_svalue(&k->ind); 
         push_svalue(&k->val); 
         html_add_tag(2); 
         pop_stack(); 
      } 
 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void html_add_containers(INT32 args) 
{ 
   INT32 e; 
   struct keypair *k; 
   struct mapping_data *md; 
   check_all_args("add_containers",args,BIT_MAPPING,0); 
 
   md = sp[-1].u.mapping->data; 
 
   NEW_MAPPING_LOOP(md) 
      { 
         push_svalue(&k->ind); 
         push_svalue(&k->val); 
         html_add_container(2); 
         pop_stack(); 
      } 
 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void html_add_entities(INT32 args) 
{ 
   INT32 e; 
   struct keypair *k; 
   struct mapping_data *md; 
   check_all_args("add_entities",args,BIT_MAPPING,0); 
 
   md = sp[-1].u.mapping->data; 
 
   NEW_MAPPING_LOOP(md) 
      { 
         push_svalue(&k->ind); 
         push_svalue(&k->val); 
         html_add_entity(2); 
         pop_stack(); 
      } 
 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
/*! @decl Parser.HTML clear_tags() 
 *! @decl Parser.HTML clear_containers() 
 *! @decl Parser.HTML clear_entities() 
 *! @decl Parser.HTML clear_quote_tags() 
 *! 
 *! Removes all registered definitions in the different categories. 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 *! 
 *! @seealso 
 *!   @[add_tag], @[add_tags], @[add_container], @[add_containers], 
 *!   @[add_entity], @[add_entities] 
 */ 
 
static void html_clear_tags (INT32 args) 
{ 
  pop_n_elems(args); 
  free_mapping (THIS->maptag); 
  THIS->maptag = allocate_mapping (32); 
  ref_push_object (THISOBJ); 
} 
 
static void html_clear_containers (INT32 args) 
{ 
  pop_n_elems(args); 
  free_mapping (THIS->mapcont); 
  THIS->mapcont = allocate_mapping (32); 
  ref_push_object (THISOBJ); 
} 
 
static void html_clear_entities (INT32 args) 
{ 
  pop_n_elems(args); 
  free_mapping (THIS->mapentity); 
  THIS->mapentity = allocate_mapping (32); 
  ref_push_object (THISOBJ); 
} 
 
static void html_clear_quote_tags (INT32 args) 
{ 
  pop_n_elems(args); 
  free_mapping (THIS->mapqtag); 
  THIS->mapqtag = allocate_mapping (32); 
  ref_push_object (THISOBJ); 
} 
 
/*! @decl mapping(string:mixed) tags() 
 *! @decl mapping(string:mixed) containers() 
 *! @decl mapping(string:mixed) entities() 
 *! 
 *! Returns the current callback settings. When matching is done case 
 *! insensitively, all names will be returned in lowercase. 
 *! 
 *! Implementation note: These run in constant time since they return 
 *! copy-on-write mappings. 
 *! 
 *! @seealso 
 *!   @[add_tag], @[add_tags], @[add_container], @[add_containers], 
 *!   @[add_entity], @[add_entities] 
 */ 
 
static void html_tags(INT32 args) 
{ 
   pop_n_elems(args); 
   push_mapping(copy_mapping(THIS->maptag)); /* deferred copy */ 
} 
 
static void html_containers(INT32 args) 
{ 
   pop_n_elems(args); 
   push_mapping(copy_mapping(THIS->mapcont)); /* deferred copy */ 
} 
 
static void html_entities(INT32 args) 
{ 
   pop_n_elems(args); 
   push_mapping(copy_mapping(THIS->mapentity)); /* deferred copy */ 
} 
 
/*! @decl mapping(string:array(mixed|string)) quote_tags() 
 *! 
 *! Returns the current callback settings. The values are arrays 
 *! ({callback, end_quote}). When matching is done case insensitively, 
 *! all names will be returned in lowercase. 
 *! 
 *! Implementation note: @[quote_tags()] allocates a new mapping for 
 *! every call and thus, unlike e.g. @[tags()] runs in linear time. 
 *! 
 *! @seealso 
 *!   @[add_quote_tag] 
 */ 
 
static void html_quote_tags(INT32 args) 
{ 
   struct mapping *res = allocate_mapping (32); 
   INT32 e; 
   struct keypair *k; 
   struct mapping_data *md = THIS->mapqtag->data; 
   pop_n_elems(args); 
   NEW_MAPPING_LOOP(md) { 
     int i; 
     struct array *arr = k->val.u.array; 
     for (i = 0; i < arr->size; i += 3) { 
       struct pike_string *end; 
       push_svalue (arr->item+i+1); 
#ifdef PIKE_DEBUG 
       if (TYPEOF(arr->item[i+2]) != T_STRING) 
         Pike_fatal ("Expected string as end in mapqtag.\n"); 
#endif 
       end = arr->item[i+2].u.string; 
       push_string (string_slice (end, 0, end->len-1)); 
       f_aggregate (2); 
       mapping_insert (res, arr->item+i, sp-1); 
       pop_stack(); 
     } 
   } 
   push_mapping (res); 
} 
 
/* ---------------------------------------- */ 
/* helper function to figure out what to do */ 
 
static INLINE void recheck_scan(struct parser_html_storage *this, 
                                int *scan_entity) 
{ 
   if (TYPEOF(this->callback__entity) != T_INT || 
       m_sizeof(this->mapentity)) 
      *scan_entity=1; 
   else 
      *scan_entity=0; 
} 
 
/* -------------- */ 
/* feed to output */ 
 
static void put_out_feed(struct parser_html_storage *this, struct svalue *v) 
{ 
   struct out_piece *f; 
 
#ifdef PIKE_DEBUG 
   if (TYPEOF(*v) != T_STRING && this->out_max_shift >= 0) 
     Pike_fatal ("Putting a non-string into output queue in non-mixed mode.\n"); 
#endif 
 
   f = alloc_out_piece(); 
   assign_svalue_no_free(&f->v,v); 
 
   f->next=NULL; 
 
   if (this->out==NULL) 
     this->out=this->out_end=f; 
   else 
   { 
     this->out_end->next=f; 
     this->out_end=f; 
   } 
 
   if (this->out_max_shift >= 0) { 
     this->out_max_shift = MAXIMUM (this->out_max_shift, v->u.string->size_shift); 
     this->out_length += v->u.string->len; 
   } 
   else 
     this->out_length++; 
} 
 
/* ---------------------------- */ 
/* feed range in feed to output */ 
 
static void put_out_feed_range(struct parser_html_storage *this, 
                               struct piece *head, 
                               ptrdiff_t c_head, 
                               struct piece *tail, 
                               ptrdiff_t c_tail) 
{ 
   DEBUG((stderr,"put out feed range " 
          "%p:%"PRINTPTRDIFFT"d - %p:%"PRINTPTRDIFFT"d\n", 
          head,c_head,tail,c_tail)); 
   /* fit it in range (this allows other code to ignore eof stuff) */ 
   if (c_tail>tail->s->len) c_tail=tail->s->len; 
 
   if (head != tail && c_head) { 
     if (head->s->len-c_head)       /* Ignore empty strings. */ 
     { 
       push_string(string_slice(head->s,c_head,head->s->len-c_head)); 
       put_out_feed(this,sp-1); 
       pop_stack(); 
     } 
     c_head=0; 
     head=head->next; 
   } 
 
   while (1) 
   { 
      if (head==tail) 
      { 
         if (c_tail-c_head) /* Ignore empty strings. */ 
         { 
           push_string(string_slice(head->s,c_head,c_tail-c_head)); 
           put_out_feed(this,sp-1); 
           pop_stack(); 
         } 
         return; 
      } 
#ifdef PIKE_DEBUG 
      if (!head) 
        Pike_fatal("internal error: tail not found in feed (put_out_feed_range)\n"); 
#endif 
      /* Should never have empty strings in the feed. */ 
      ref_push_string(head->s); 
      put_out_feed(this,sp-1); 
      pop_stack(); 
      head=head->next; 
   } 
   UNREACHABLE(); 
} 
 
/* ------------------------ */ 
/* push feed range on stack */ 
 
static INLINE int low_push_feed_range(struct piece *head, 
                                      ptrdiff_t c_head, 
                                      struct piece *tail, 
                                      ptrdiff_t c_tail) 
{ 
   int n=0; 
   /* fit it in range (this allows other code to ignore eof stuff) */ 
   if (c_tail>tail->s->len) c_tail=tail->s->len; 
 
   if (head != tail && c_head) { 
     if (head->s->len - c_head) { 
       push_string(string_slice(head->s,c_head,head->s->len-c_head)); 
       n++; 
     } 
     c_head = 0; 
     head = head->next; 
   } 
 
   while (1) 
   { 
      if (head==tail) 
      { 
         if (c_head < c_tail) { 
           push_string(string_slice(head->s,c_head,c_tail-c_head)); 
           n++; 
         } 
         break; 
      } 
#ifdef PIKE_DEBUG 
      if (!head) 
        Pike_fatal("internal error: tail not found in feed (push_feed_range)\n"); 
#endif 
      ref_push_string(head->s); 
      n++; 
      if (n==32) 
      { 
         f_add(32); 
         n=1; 
      } 
      head=head->next; 
   } 
 
   if (!n) { 
      DEBUG((stderr,"push empty\n")); 
      return 0; 
   } else if (n>1) 
      f_add(n); 
   DEBUG((stderr,"push len=%"PRINTPTRDIFFT"d\n",sp[-1].u.string->len)); 
   return 1; 
} 
 
static INLINE void push_feed_range(struct piece *head, 
                                   ptrdiff_t c_head, 
                                   struct piece *tail, 
                                   ptrdiff_t c_tail) 
{ 
  if (!low_push_feed_range(head, c_head, tail, c_tail)) 
    ref_push_string(empty_pike_string); 
} 
 
/* -------------------------------------------------------- */ 
/* go forward by adjusting for a positive displacement in c */ 
 
#define FORWARD_CHAR(feed, c, dest, dp) do {                          \ 
  (dest) = (struct piece *) (feed);                                     \ 
  (dp) = (ptrdiff_t) (c) + 1;                                           \ 
  while ((dp) == (dest)->s->len && (dest)->next) {                      \ 
    (dp) = 0;                                                           \ 
    (dest) = (dest)->next;                                              \ 
  }                                                                     \ 
} while (0) 
 
static INLINE int n_pos_forward (struct piece *feed, ptrdiff_t c, 
                                 struct piece **dest, ptrdiff_t *dp) 
{ 
  while (feed->s->len <= c) { 
    if (!feed->next) { 
      *dest = feed; 
      *dp = feed->s->len; 
      return 0; 
    } 
    c -= feed->s->len; 
    feed = feed->next; 
  } 
  *dest = feed; 
  *dp = c; 
  return 1; 
} 
 
/* ------------------------- */ 
/* compare positions in feed */ 
 
static INLINE int cmp_feed_pos(struct piece *piece_a, ptrdiff_t pos_a, 
                               struct piece *piece_b, ptrdiff_t pos_b) 
{ 
  struct piece *a_save = piece_a; 
 
  if (piece_a == piece_b) 
    return pos_a < pos_b ? -1 : (pos_a > pos_b ? 1 : 0); 
 
  while (piece_a && piece_a != piece_b) 
    piece_a = piece_a->next; 
  if (piece_a) return -1; 
 
  while (piece_b && piece_b != a_save) 
    piece_b = piece_b->next; 
  if (piece_b) return 1; 
 
  return 17;                        /* Not in the same feed! */ 
} 
 
/* ------------------------------------ */ 
/* skip feed range and count lines, etc */ 
 
/* count lines, etc */ 
static INLINE void skip_piece_range(struct location *loc, 
                                    struct piece *p, 
                                    ptrdiff_t start, 
                                    ptrdiff_t stop) 
{ 
   int b=loc->byteno; 
   switch (p->s->size_shift) 
   { 
#define LOOP(TYPE)                                                    \ 
     {                                                                  \ 
       TYPE *s=(TYPE *)p->s->str + start;                               \ 
       for (;start<stop;start++)                                        \ 
       {                                                                \ 
         if (*(s++)=='\n')                                              \ 
         {                                                              \ 
           loc->linestart=b;                                            \ 
           loc->lineno++;                                               \ 
         }                                                              \ 
         b++;                                                           \ 
       }                                                                \ 
     } 
     case 0: LOOP (p_wchar0); break; 
     case 1: LOOP (p_wchar1); break; 
     case 2: LOOP (p_wchar2); break; 
#undef LOOP 
#ifdef PIKE_DEBUG 
     default: Pike_fatal("unknown width of string\n"); 
#endif 
   } 
   loc->byteno=b; 
} 
 
static void skip_feed_range(struct feed_stack *st, 
                            struct piece **headp, 
                            ptrdiff_t *c_headp, 
                            struct piece *tail, 
                            ptrdiff_t c_tail) 
{ 
   struct piece *head=*headp; 
   ptrdiff_t c_head=*c_headp; 
 
   if (!*headp) 
   { 
      DEBUG((stderr,"skip_feed_range: already at end<tm>\n")); 
      return; 
   } 
 
   DEBUG_MARK_SPOT("skip_feed_range from",*headp,*c_headp); 
   DEBUG_MARK_SPOT("                  to",tail,c_tail); 
 
   while (head) 
   { 
      if (head==tail && c_tail<tail->s->len) 
      { 
         skip_piece_range(&(st->pos),head,c_head,c_tail); 
         *headp = head; 
         *c_headp=c_tail; 
         return; 
      } 
      skip_piece_range(&(st->pos),head,c_head,head->s->len); 
      { 
        struct piece *next = head->next; 
        really_free_piece (head); 
        head = next; 
      } 
      c_head=0; 
   } 
 
   *headp = head; 
   *c_headp = 0; 
} 
 
static p_wchar2 next_character(struct piece *feed, 
                               ptrdiff_t pos) 
{ 
   FORWARD_CHAR(feed,pos,feed,pos); 
   if (pos==feed->s->len) return 0; /* in lack of anything better */ 
   return index_shared_string(feed->s,pos); 
} 
 
/* ------------------------------ */ 
/* scan forward for certain chars */ 
 
static int scan_forward(struct piece *feed, 
                        ptrdiff_t c, 
                        struct piece **destp, 
                        ptrdiff_t *d_p, 
                        const p_wchar2 *look_for, 
                        ptrdiff_t num_look_for) /* negative = skip those */ 
{ 
   int rev=0; 
 
   if (num_look_for<0) num_look_for=-num_look_for,rev=1; 
 
#ifdef HTML_DEBUG 
   if (THIS->flags & FLAG_DEBUG_MODE) { 
      DEBUG_MARK_SPOT("scan_forward",feed,c); 
      fprintf(stderr,"    n=%"PRINTPTRDIFFT"d%s; ",num_look_for,rev?"; rev":""); 
      debug_print_chars (look_for, num_look_for); 
   } 
#define SCAN_DEBUG_MARK_SPOT(A,B,C) DEBUG_MARK_SPOT(A,B,C) 
#else 
#define SCAN_DEBUG_MARK_SPOT(A,B,C) do {} while (0); 
#endif 
 
   while (c >= feed->s->len) { 
     c -= feed->s->len; 
     if (!feed->next) { 
       *destp = feed; 
       *d_p = feed->s->len; 
       SCAN_DEBUG_MARK_SPOT("scan_forward end (start offset beyond feed end)", 
                            *destp,*d_p); 
       return 0; 
     } 
     feed = feed->next; 
   } 
 
   switch (num_look_for) 
   { 
      case 0: /* optimize, skip to end */ 
         while (feed->next) 
            feed=feed->next; 
         *destp=feed; 
         *d_p=feed->s->len; 
         SCAN_DEBUG_MARK_SPOT("scan_forward end (nothing to look for)", 
                              *destp,*d_p); 
         return 0; /* not found :-) */ 
 
      case 1: 
         if (!rev) { 
            while (feed) 
            { 
               ptrdiff_t ce = feed->s->len - c; 
               p_wchar2 f=(p_wchar2)*look_for; 
               SCAN_DEBUG_MARK_SPOT("scan_forward piece loop (1)",feed,c); 
               switch (feed->s->size_shift) 
               { 
#define LOOP(TYPE)                                                    \ 
                  {                                                     \ 
                     TYPE *s=((TYPE *)feed->s->str)+c;                  \ 
                     while (ce--)                                       \ 
                        if ((p_wchar2)*(s++)==f)                        \ 
                        {                                               \ 
                           c=feed->s->len-ce;                           \ 
                           goto found;                                  \ 
                        }                                               \ 
                  } 
                case 0: LOOP (p_wchar0); break; 
                  case 1: LOOP (p_wchar1); break; 
                  case 2: LOOP (p_wchar2); break; 
#undef LOOP 
#ifdef PIKE_DEBUG 
                default: 
                     Pike_fatal("unknown width of string\n"); 
#endif 
             } 
               if (!feed->next) break; 
               c=0; 
               feed=feed->next; 
            } 
            break; 
         } 
         /* FALL THROUGH */ 
 
      default:  /* num_look_for > 1 */ 
         while (feed) 
         { 
            ptrdiff_t ce = feed->s->len - c; 
            SCAN_DEBUG_MARK_SPOT("scan_forward piece loop (>1)",feed,c); 
            switch (feed->s->size_shift) 
            { 
#define LOOP(TYPE)                                                    \ 
               {                                                        \ 
                  int n;                                                \ 
                  TYPE *s=((TYPE *)feed->s->str)+c;                     \ 
                  while (ce--)                                          \ 
                  {                                                     \ 
                     for (n=0; n<num_look_for; n++)                     \ 
                        if (((p_wchar2)*s)==look_for[n])                \ 
                        {                                               \ 
                           c=feed->s->len-ce;                           \ 
                           if (!rev) goto found; else break;            \ 
                        }                                               \ 
                     if (rev && n==num_look_for) goto found;            \ 
                     s++;                                               \ 
                  }                                                     \ 
               } 
             case 0: LOOP (p_wchar0); break; 
               case 1: LOOP (p_wchar1); break; 
               case 2: LOOP (p_wchar2); break; 
#undef LOOP 
#ifdef PIKE_DEBUG 
             default: 
                  Pike_fatal("unknown width of string\n"); 
#endif 
          } 
            if (!feed->next) break; 
            c=0; 
            feed=feed->next; 
         } 
         break; 
   } 
 
   SCAN_DEBUG_MARK_SPOT("scan_forward not found",feed,feed->s->len); 
   *destp=feed; 
   *d_p=feed->s->len; 
   return 0; /* not found */ 
 
found: 
   c-=!rev; 
   while (c == feed->s->len && feed->next) { 
     c = 0; 
     feed = feed->next; 
   } 
   *destp=feed; 
   *d_p=c; 
   SCAN_DEBUG_MARK_SPOT("scan_forward found",feed,c); 
   return 1; 
} 
 
static int scan_for_string (struct parser_html_storage *UNUSED(this), 
                            struct piece *feed, 
                            ptrdiff_t c, 
                            struct piece **destp, 
                            ptrdiff_t *d_p, 
                            struct pike_string *str) 
{ 
  if (!str->len) { 
    *destp = feed; 
    *d_p = c; 
    return 1; 
  } 
 
#define LOOP(SHIFT) {                                                 \ 
    p_wchar2 look_for = INDEX_CHARP(str->str, 0, SHIFT);                \ 
    for (;;) {                                                          \ 
      PIKE_CONCAT(p_wchar, SHIFT) *p, *e;                               \ 
      struct piece *dst;                                                \ 
      ptrdiff_t cdst;                                                   \ 
      if (!scan_forward (feed, c, &feed, &c, &look_for, 1)) {           \ 
        *destp = feed;                                                  \ 
        *d_p = c;                                                       \ 
        return 0;                                                       \ 
      }                                                                 \ 
                                                                        \ 
      p = ((PIKE_CONCAT(p_wchar, SHIFT) *) str->str) + 1;               \ 
      e = ((PIKE_CONCAT(p_wchar, SHIFT) *) str->str) + str->len;        \ 
      dst = feed;                                                       \ 
      cdst = c + 1;                                                     \ 
      for (; p < e; p++, cdst++) {                                      \ 
        while (cdst == dst->s->len) {                                   \ 
          if (dst->next) dst = dst->next;                               \ 
          else {                                                        \ 
            *destp = dst;                                               \ 
            *d_p = dst->s->len;                                         \ 
            return 0;                                                   \ 
          }                                                             \ 
          cdst = 0;                                                     \ 
        }                                                               \ 
        if ((p_wchar2) *p !=                                            \ 
            INDEX_CHARP (dst->s->str, cdst, dst->s->size_shift))        \ 
          goto PIKE_CONCAT(cont, SHIFT);                                \ 
      }                                                                 \ 
                                                                        \ 
      *destp = feed;                                                    \ 
      *d_p = c;                                                         \ 
      return 1;                                                         \ 
                                                                        \ 
    PIKE_CONCAT(cont, SHIFT):                                           \ 
      c++;                                                              \ 
    }                                                                   \ 
  } 
 
  switch (str->size_shift) { 
    case 0: LOOP(0); break; 
    case 1: LOOP(1); break; 
    case 2: LOOP(2); break; 
#ifdef PIKE_DEBUG 
    default: Pike_fatal ("Unknown width of string.\n"); 
#endif 
  } 
 
#undef LOOP 
 
  return 0; 
} 
 
enum scan_arg_do {SCAN_ARG_ONLY, SCAN_ARG_PUSH, SCAN_ARG_ENT_BREAK}; 
 
static int scan_forward_arg(struct parser_html_storage *this, 
                            struct piece *feed, 
                            ptrdiff_t c, 
                            struct piece **destp, 
                            ptrdiff_t *d_p, 
                            enum scan_arg_do what, 
                            int scan_name, /* Break on "=" if outside quotes. */ 
                            int finished, 
                            int *quote) 
/* Returns 1 if end is found, 2 if broken for entity, 0 otherwise. If 
 * 2 is returned, *quote is set to the index in this->argq_start for 
 * the current quote we're in, or -1 if outside quotes. *quote is also 
 * read on entry to continue inside quotes. */ 
{ 
   p_wchar2 ch=0; 
   int res; 
   size_t i; 
   int n=0; 
   int q=0; 
 
   DEBUG_MARK_SPOT("scan_forward_arg: start",feed,c); 
 
   if (quote && *quote >= 0) { 
     *destp = feed; 
     *d_p = c-1; 
     i = *quote; 
     goto in_quote_cont;    /* jump right into the loop */ 
   } 
 
   for (;;) 
   { 
      /* we are here: */ 
      /* < f'o'"o" = bar > */ 
      /*   ^     ->        */ 
      /*      this is the end */ 
 
      DEBUG_MARK_SPOT("scan_forward_arg: loop",feed,c); 
 
      res=scan_forward(feed,c,destp,d_p, 
                       ARG_BREAK_CHARS (this), 
                       N_ARG_BREAK_CHARS (this)); 
 
retryloop: 
      if (res) 
      { 
         ch=index_shared_string(destp[0]->s,*d_p); 
         if ((this->flags & FLAG_IGNORE_COMMENTS) && 
             ch=='-') /* possible --...-- to be ignored */ 
         { 
            if (next_character(*destp,*d_p)=='-') 
            { 
               static const p_wchar2 minus='-'; 
 
               if (what == SCAN_ARG_PUSH) 
                  n += low_push_feed_range(feed,c,*destp,*d_p); 
 
         /* skip to next -- */ 
 
               DEBUG_MARK_SPOT("scan_forward_arg: " 
                               "found --",*destp,*d_p); 
               FORWARD_CHAR (*destp, *d_p, *destp, *d_p); 
               for (;;) 
               { 
                  res=scan_forward(*destp,d_p[0]+1,destp,d_p,&minus,1); 
                  if (!res) { 
                     if (!finished) 
                     { 
                        DEBUG_MARK_SPOT("scan_forward_arg: " 
                                        "wait for --",*destp,*d_p); 
                        if (what == SCAN_ARG_PUSH) pop_n_elems(n); 
                        return 0; 
                     } 
                     else 
                     { 
                        DEBUG_MARK_SPOT("scan_forward_arg: " 
                                        "forced end in --...--",destp[0],*d_p); 
                        break; 
                     } 
                  } 
                  if (next_character(*destp,*d_p)=='-') 
                  { 
                     DEBUG_MARK_SPOT("scan_forward_arg: " 
                                     "found -- end",*destp,*d_p); 
                     d_p[0]++; 
                     goto next; 
                  } 
                  DEBUG_MARK_SPOT("scan_forward_arg: " 
                                  "not -- end, cont",*destp,*d_p); 
               } 
               break; 
            } 
            else 
            { 
               DEBUG_MARK_SPOT("scan_forward_arg: " 
                               "found -, not -",*destp,*d_p); 
 
               res=scan_forward(*destp,d_p[0]+1,destp,d_p, 
                                ARG_BREAK_CHARS (this), 
                                N_ARG_BREAK_CHARS (this)); 
               goto retryloop; 
            } 
         } 
      } 
 
      if (what == SCAN_ARG_PUSH) n += low_push_feed_range(feed,c,*destp,*d_p); 
 
      if (!res) { 
         if (!finished) 
         { 
            DEBUG_MARK_SPOT("scan_forward_arg: wait",feed,c); 
            if (what == SCAN_ARG_PUSH) pop_n_elems(n); 
            return 0; 
         } 
         else 
         { 
            DEBUG_MARK_SPOT("scan_forward_arg: forced end",destp[0],*d_p); 
            break; 
         } 
      } 
 
      if (ch == ARG_EQ (this)) 
      { 
        if (scan_name) { 
          DEBUG_MARK_SPOT("scan_forward_arg: end by arg_eq", 
                          destp[0],*d_p); 
          break; 
        } 
        else { 
          DEBUG_MARK_SPOT("scan_forward_arg: = ignored in arg val", 
                          destp[0],*d_p); 
          if (what == SCAN_ARG_PUSH) n += low_push_feed_range(*destp,*d_p,*destp,*d_p+1); 
          goto next; 
        } 
      } 
 
      if (ch == TAG_END (this)) { 
         if ((this->flags & FLAG_MATCH_TAG) && q--) 
         { 
            DEBUG_MARK_SPOT("scan_forward_arg: inner tag end", 
                            destp[0],*d_p); 
            if (what == SCAN_ARG_PUSH) n += low_push_feed_range(*destp,*d_p,*destp,*d_p+1); 
            goto next; 
         } 
         else 
         { 
            DEBUG_MARK_SPOT("scan_forward_arg: end by tag end", 
                            destp[0],*d_p); 
            break; 
         } 
      } 
 
      if ((this->flags & FLAG_MATCH_TAG) && ch == TAG_START (this)) 
      { 
         DEBUG_MARK_SPOT("scan_forward_arg: inner tag start", 
                         destp[0],*d_p); 
         q++; 
         if (what == SCAN_ARG_PUSH) n += low_push_feed_range(*destp,*d_p,*destp,*d_p+1); 
         goto next; 
      } 
 
      if (ch == ENTITY_START (this)) { 
        DEBUG_MARK_SPOT("scan_forward_arg: entity start char", 
                        destp[0],*d_p); 
        if (what == SCAN_ARG_ENT_BREAK) { 
          if (quote) *quote = -1; 
          return 2; 
        } 
        else { 
          if (what == SCAN_ARG_PUSH) n += low_push_feed_range(*destp,*d_p,*destp,*d_p+1); 
          goto next; 
        } 
      } 
 
      /* scan for (possible) end(s) of this argument quote */ 
 
      for (i=0; i < NARGQ (this); i++) 
         if (ch == ARGQ_START (this)[i]) break; 
      if (i == NARGQ (this)) 
      { 
         if (ch == TAG_FIN (this)) { 
            FORWARD_CHAR (*destp, *d_p, feed, c); 
            if (((this->flags & FLAG_MATCH_TAG) && q) || 
                index_shared_string (feed->s, c) != TAG_END (this)) { 
               DEBUG_MARK_SPOT("scan_forward_arg: tag fin char", 
                               destp[0],*d_p); 
               if (what == SCAN_ARG_PUSH) n += low_push_feed_range (*destp, *d_p, feed, c); 
               goto next; 
            } 
            else { 
               DEBUG_MARK_SPOT("scan_forward_arg: end by tag fin", 
                               destp[0],*d_p); 
               break; 
            } 
         } 
         else { 
            DEBUG_MARK_SPOT("scan_forward_arg: end by ws", 
                            destp[0],*d_p); 
            break; 
         } 
      } 
 
in_quote_cont: 
      DEBUG_MARK_SPOT("scan_forward_arg: quoted",destp[0],*d_p); 
      while (1) { 
        res=scan_forward(feed=*destp,c=d_p[0]+1,destp,d_p, 
                         LOOK_FOR_END (this)[i], NUM_LOOK_FOR_END (this)[i]); 
        if (what == SCAN_ARG_PUSH) n += low_push_feed_range(feed,c,*destp,*d_p); 
 
        if (!res) { 
          if (!finished) 
          { 
            DEBUG_MARK_SPOT("scan_forward_arg: wait (quote)",feed,c); 
            if (what == SCAN_ARG_PUSH) pop_n_elems(n); 
            return 0; 
          } 
          else 
          { 
            DEBUG_MARK_SPOT("scan_forward_arg: forced end (quote)", 
                            destp[0], *d_p); 
            if (this->flags & FLAG_QUOTE_STAPLING) break; 
            goto end; 
          } 
        } 
 
        if (index_shared_string(destp[0]->s,*d_p) == ENTITY_START (this)) { 
          DEBUG_MARK_SPOT("scan_forward_arg: entity start char", 
                          destp[0],*d_p); 
          if (what == SCAN_ARG_ENT_BREAK) { 
            if (quote) *quote = i; 
            return 2; 
          } 
          else { 
            if (what == SCAN_ARG_PUSH) n += low_push_feed_range(*destp,*d_p,*destp,*d_p+1); 
            continue; 
          } 
        } 
        if (this->flags & FLAG_QUOTE_STAPLING) break; 
        FORWARD_CHAR (*destp, *d_p, *destp, *d_p); 
        DEBUG_MARK_SPOT("scan_forward_arg: End of quoted string", 
                        destp[0],*d_p); 
        goto end; 
      } 
 
next: 
      if (n > 20) { 
        if (this->flags & FLAG_QUOTE_STAPLING) { 
          fprintf(stderr, "Parser.HTML: Warning: Old-style attribute stapling.\n"); 
        } 
        f_add(n); 
        n = 1; 
      } 
 
      feed=*destp; 
      c=d_p[0]+1; 
   } 
end: 
   if (what == SCAN_ARG_PUSH) 
   { 
      if (n>1) { 
        if (this->flags & FLAG_QUOTE_STAPLING) { 
          fprintf(stderr, 
                  "Parser.HTML: Warning: Old-style attribute stapling.\n"); 
        } 
        f_add(n); 
      } else if (!n) ref_push_string(empty_pike_string); 
   } 
   return 1; 
} 
 
static int scan_for_end_of_tag(struct parser_html_storage *this, 
                               struct piece *feed, 
                               ptrdiff_t c, 
                               struct piece **destp, 
                               ptrdiff_t *d_p, 
                               int finished, 
                               int match_tag, 
                               int *got_fin) 
/* match_tag == 0: don't match inner tags, > 0: match inner tags, < 0: 
 * consider it an unbalanced tag and break. got_fin is assigned 
 * nonzero if there's a '/' before the tag end, zero otherwise. */ 
{ 
   p_wchar2 ch; 
   int res; 
   size_t i; 
   int q=0; 
 
   /* maybe these should be cached */ 
 
   /* bla bla <tag foo 'bar' "gazonk" > */ 
   /*          ^                      ^ */ 
   /*       here now             scan here */ 
 
   DEBUG((stderr,"scan for end of tag: %p:%"PRINTPTRDIFFT"d\n",feed,c)); 
 
   if (got_fin) *got_fin = 0; 
 
   for (;;) 
   { 
      /* scan for start of argument quote or end of tag */ 
 
      res=scan_forward(feed,c,destp,d_p, 
                       LOOK_FOR_START (this), NUM_LOOK_FOR_START (this)); 
      if (!res) { 
         if (!finished) 
         { 
            DEBUG((stderr,"scan for end of tag: " 
                   "wait at %p:%"PRINTPTRDIFFT"d\n",feed,c)); 
            return 0; /* not found - no end of tag, yet */ 
         } 
         else 
         { 
            DEBUG((stderr,"scan for end of tag: " 
                   "forced end at %p:%"PRINTPTRDIFFT"d\n", destp[0],*d_p)); 
            return 1; /* end of tag, sure... */ 
         } 
      } 
 
      ch=index_shared_string(destp[0]->s,*d_p); 
      if (ch == ARG_EQ (this)) 
      { 
         DEBUG_MARK_SPOT("scan for end of tag: arg_eq", 
                         destp[0],*d_p); 
         feed=*destp; 
         c=d_p[0]+1; 
         continue; 
      } 
 
      if (ch == TAG_FIN (this)) { 
        DEBUG_MARK_SPOT("scan for end of tag: tag_fin", 
                         destp[0],*d_p); 
        FORWARD_CHAR (*destp, *d_p, feed, c); 
        if (match_tag && q) continue; 
        else { 
          ch = index_shared_string (feed->s, c); 
          if (ch == TAG_END (this)) { 
            if (got_fin) *got_fin = 1; 
            *destp = feed; 
            *d_p = c; 
            DEBUG_MARK_SPOT("scan for end of tag: end by tag_fin + tag_end", 
                            destp[0],*d_p); 
            return 1; 
          } 
          else continue; 
        } 
      } 
 
      if (ch == TAG_END (this)) { 
        if (match_tag && q--) 
         { 
            DEBUG_MARK_SPOT("scan for end of tag: inner tag end", 
                            destp[0],*d_p); 
            feed=*destp; 
            c=d_p[0]+1; 
            continue; /* Aja baja /hubbe: //scan more  */ 
         } 
         else 
         { 
            DEBUG_MARK_SPOT("scan for end of tag: end by tag end", 
                            destp[0],*d_p); 
            return 1; 
         } 
      } 
 
      if (ch == TAG_START (this)) { 
        if (match_tag > 0) 
        { 
          DEBUG_MARK_SPOT("scan for end of tag: inner tag start", 
                          destp[0],*d_p); 
          q++; 
          feed=*destp; 
          c=d_p[0]+1; 
          continue; 
        } 
        else if (match_tag < 0) 
        { 
          DEBUG_MARK_SPOT("scan for end of tag: unbalanced tag start", 
                          destp[0],*d_p); 
          return 1; 
        } 
        else 
        { 
          DEBUG_MARK_SPOT("scan for end of tag: ignored inner tag start", 
                          destp[0],*d_p); 
          feed=*destp; 
          c=d_p[0]+1; 
          continue; 
        } 
      } 
 
      /* scan for (possible) end(s) of this argument quote */ 
 
      for (i=0; i < NARGQ (this); i++) 
        if (ch == ARGQ_START (this)[i]) { 
 
          do { 
            res = scan_forward(*destp, d_p[0]+1, destp, d_p, 
                               LOOK_FOR_END (this)[i], 
                               NUM_LOOK_FOR_END (this)[i]); 
            if (!res) { 
              if (!finished) 
              { 
                DEBUG((stderr,"scan for end of tag: " 
                       "wait at %p:%"PRINTPTRDIFFT"d\n", destp[0],*d_p)); 
                return 0; /* not found - no end of tag, yet */ 
              } 
              else 
              { 
                DEBUG((stderr,"scan for end of tag: " 
                       "forced end at %p:%"PRINTPTRDIFFT"d\n", feed,c)); 
                return 1; /* end of tag, sure... */ 
              } 
            } 
          } 
          while (index_shared_string(destp[0]->s, *d_p) == ENTITY_START (this)); 
 
          feed=*destp; 
          c=d_p[0]+1; 
 
          break; 
        } 
 
   } 
} 
 
/* ---------------------------------- */ 
 
struct chr_buf_len2 
{ 
  p_wchar2 str[2]; 
  struct piece *p[2]; 
  ptrdiff_t c[2]; 
}; 
 
static int quote_tag_lookup (struct parser_html_storage *this, 
                             struct piece *feed, ptrdiff_t c, 
                             struct piece **destp, ptrdiff_t *d_p, 
                             int finished, struct svalue **mapqentry) 
/* Checks for a quote tag starting at the given position. If one is 
 * found, scans to the end of it and sets *mapqentry to the first 
 * svalue in the ({name, to_do, end}) tuple in this->mapqtag. */ 
{ 
  struct chr_buf_len2 buf; 
  struct piece *dst = feed; 
  ptrdiff_t cdst = c; 
  size_t checklen; 
  *mapqentry = NULL; 
 
  DEBUG_MARK_SPOT ("quote tag lookup", feed, c); 
 
  for (checklen = 0; checklen < NELEM (buf.str); checklen++) { 
    while (cdst == dst->s->len) { 
      if (!(dst = dst->next)) { 
        /* This is not entirely correct; we should continue if mapqtag 
         * doesn't contain any longer indices whose prefixes match 
         * what we got so far. But it's not much of a problem as long 
         * as we're indexing on only two chars or less. */ 
        DEBUG ((stderr, "quote tag lookup: wait at prefix\n")); 
        return finished;    /* 0 => wait, 1 => no go - *mapqentry still NULL */ 
      } 
      cdst = 0; 
    } 
    buf.str[checklen] = index_shared_string (dst->s, cdst); 
    buf.p[checklen] = dst; 
    buf.c[checklen] = ++cdst; 
  } 
 
  for (; checklen; checklen--) { 
    struct pike_string *indstr = make_shared_binary_string2 (buf.str, checklen); 
    struct svalue *val = low_mapping_string_lookup (this->mapqtag, indstr); 
    free_string (indstr); 
 
    if (val) { 
      int i; 
      struct array *arr; 
      DEBUG ((stderr, "quote tag lookup: found entry %c%c at %"PRINTSIZET"u\n", 
              isprint (buf.str[0]) ? buf.str[0] : '.', 
              isprint (buf.str[1]) ? buf.str[1] : '.', checklen)); 
#ifdef PIKE_DEBUG 
      if (TYPEOF(*val) != T_ARRAY) 
        Pike_fatal ("Expected array as value in mapqtag.\n"); 
#endif 
      arr = val->u.array; 
 
      for (i = 0; i < arr->size; i += 3) { 
        struct pike_string *tag; 
#ifdef PIKE_DEBUG 
      if (TYPEOF(arr->item[i]) != T_STRING) 
          Pike_fatal ("Expected string as name in mapqtag.\n"); 
#endif 
      tag = arr->item[i].u.string; 
        dst = buf.p[checklen-1]; 
        cdst = buf.c[checklen-1]; 
 
#define LOOP(TYPE) {                                                  \ 
          TYPE *p = (TYPE *) tag->str + checklen;                       \ 
          TYPE *e = (TYPE *) tag->str + tag->len;                       \ 
          for (; p < e; p++, cdst++) {                                  \ 
            while (cdst == dst->s->len) {                               \ 
              if (!(dst = dst->next)) {                                 \ 
                DEBUG ((stderr, "quote tag lookup: wait\n"));           \ 
                return finished; /* 0/1 => see above */                 \ 
              }                                                         \ 
              cdst = 0;                                                 \ 
            }                                                           \ 
            if ((p_wchar2) *p !=                                        \ 
                INDEX_CHARP (dst->s->str, cdst, dst->s->size_shift))    \ 
              goto cont;                                                \ 
          }                                                             \ 
        } 
 
        switch (tag->size_shift) { 
          case 0: LOOP (p_wchar0); break; 
          case 1: LOOP (p_wchar1); break; 
          case 2: LOOP (p_wchar2); break; 
#ifdef PIKE_DEBUG 
        default: Pike_fatal ("Unknown width of string.\n"); 
#endif 
      } 
 
#undef LOOP 
 
        DEBUG_MARK_SPOT ("quote tag lookup: matched quote tag", dst, cdst); 
        *mapqentry = arr->item+i; 
        *destp = dst; 
        *d_p = cdst; 
        return 1; 
 
      cont: ; 
      } 
    } 
#ifdef HTML_DEBUG 
    else 
      DEBUG ((stderr, "quote tag lookup: no entry %c%c at %"PRINTSIZET"u\n", 
              isprint (buf.str[0]) ? buf.str[0] : '.', 
              isprint (buf.str[1]) ? buf.str[1] : '.', checklen)); 
#endif 
  } 
 
  DEBUG ((stderr, "quote tag lookup: no quote tag\n")); 
  return 1;                 /* 1 => no go - *mapqentry still NULL */ 
} 
 
static INLINE void low_add_local_feed (struct parser_html_storage *this, 
                                       struct piece *feed) 
{ 
  /* Note: Calling code assumes that this alloc never fails with an 
   * exception or a zero value (i.e. it exits the process immediately 
   * instead). */ 
  struct feed_stack *new = alloc_feed_stack(); 
 
  new->local_feed=feed; 
  new->ignore_data=0; 
  new->parse_tags=this->stack->parse_tags && this->out_ctx == CTX_DATA; 
  new->pos=init_pos; 
  new->prev=this->stack; 
  new->c=0; 
  this->stack=new; 
  this->stack_count++; 
} 
 
static INLINE void add_local_feed (struct parser_html_storage *this, 
                                   struct pike_string *str) 
{ 
  struct piece *feed = alloc_piece(); 
  copy_shared_string(feed->s,str); 
  low_add_local_feed (this, feed); 
} 
 
/* ---------------------------------------------------------------- */ 
/* this is called to get data from callbacks and do the right thing */ 
 
static newstate handle_result(struct parser_html_storage *this, 
                              struct feed_stack *st, 
                              struct piece **head, 
                              ptrdiff_t *c_head, 
                              struct piece *tail, 
                              ptrdiff_t c_tail, 
                              int skip) 
{ 
   int i; 
 
   /* on sp[-1]: 
      string: push it to feed stack 
      int: noop, output range 
      array(string): output string */ 
 
   switch (TYPEOF(sp[-1])) 
   { 
      case T_STRING: /* push it to feed stack */ 
 
         /* first skip this in local feed*/ 
         if (skip) skip_feed_range(st,head,c_head,tail,c_tail); 
 
         if (sp[-1].u.string->len) { 
           DEBUG((stderr,"handle_result: pushing string " 
                  "(len=%"PRINTPTRDIFFT"d) on feedstack\n", 
                  sp[-1].u.string->len)); 
           add_local_feed (this, sp[-1].u.string); 
           pop_stack(); 
           return STATE_REREAD; /* please reread stack head */ 
         } 
         else { 
           DEBUG((stderr,"handle_result: not pushing empty string on feedstack\n")); 
           pop_stack(); 
           if (this->stack != st) /* got more feed recursively - reread */ 
             return STATE_REREAD; 
           return STATE_DONE; /* continue */ 
         } 
 
      case T_INT: 
         switch (sp[-1].u.integer) 
         { 
            case 0: 
               if ((this->type == TYPE_TAG || 
                    this->type == TYPE_CONT) && 
                   (TYPEOF(this->callback__entity) != T_INT || 
                    m_sizeof (this->mapentity))) { 
                 /* If it's a tag and we got entities, just output the 
                  * tag starter and name and switch to CTX_TAG to 
                  * parse any entities in the tag. */ 
                 struct piece *pos; 
                 ptrdiff_t cpos; 
                 if (this->flags & FLAG_WS_BEFORE_TAG_NAME) 
                   scan_forward (*head, *c_head + 1, &pos, &cpos, 
                                 WS(this), 
                                 -(ptrdiff_t)N_WS(this)); 
                 else pos = *head, cpos = *c_head + 1; 
                 scan_forward_arg (this, pos, cpos, &pos, &cpos, 
                                   SCAN_ARG_ONLY, 1, 1, NULL); 
                 put_out_feed_range(this,*head,*c_head,pos,cpos); 
                 if (skip) skip_feed_range(st,head,c_head,pos,cpos); 
                 this->out_ctx = CTX_TAG; 
                 return STATE_REREAD; 
               } 
               else if (*head) { 
                 /* just output the whole range */ 
                 put_out_feed_range(this,*head,*c_head,tail,c_tail); 
                 if (skip) skip_feed_range(st,head,c_head,tail,c_tail); 
               } 
               pop_stack(); 
               if (this->stack != st) /* got more feed recursively - reread */ 
                  return STATE_REREAD; 
               return STATE_DONE; /* continue */ 
            case 1: 
               /* reparse the last thing */ 
               pop_stack(); 
               return STATE_REPARSE; 
         } 
         Pike_error("Parser.HTML: illegal result from callback: %"PRINTPIKEINT"d, " 
                    "not 0 (skip) or 1 (reparse)\n", 
                    sp[-1].u.integer); 
 
      case T_ARRAY: 
         /* output element(s) */ 
         for (i=0; i<sp[-1].u.array->size; i++) 
         { 
            if (THIS->out_max_shift >= 0 && 
                TYPEOF(sp[-1].u.array->item[i]) != T_STRING) 
               Pike_error("Parser.HTML: illegal result from callback: " 
                          "element in array not string\n"); 
            push_svalue(sp[-1].u.array->item+i); 
            put_out_feed(this,sp-1); 
            pop_stack(); 
         } 
         if (skip) skip_feed_range(st,head,c_head,tail,c_tail); 
         pop_stack(); 
         if (this->stack != st) /* got more feed recursively - reread */ 
            return STATE_REREAD; 
         return STATE_DONE; /* continue */ 
 
      default: 
         Pike_error("Parser.HTML: illegal result from callback: " 
                    "not 0, string or array\n"); 
   } 
   UNREACHABLE(return STATE_DONE); 
} 
 
static void clear_start(struct parser_html_storage *this) 
{ 
   this->start=NULL; 
} 
 
static void do_callback(struct parser_html_storage *this, 
                        struct object *thisobj, 
                        struct svalue *callback_function, 
                        struct piece *start, 
                        ptrdiff_t cstart, 
                        struct piece *end, 
                        ptrdiff_t cend) 
{ 
   ONERROR uwp; 
 
   if (TYPEOF(*callback_function) != T_FUNCTION && 
       TYPEOF(*callback_function) != T_PROGRAM) 
   { 
      push_svalue(callback_function); 
      this->start=NULL; 
      return; 
   } 
 
   this->start=start; 
   this->cstart=cstart; 
   this->end=end; 
   this->cend=cend; 
 
   SET_ONERROR(uwp,clear_start,this); 
 
   ref_push_object(thisobj); 
   push_feed_range(start,cstart,end,cend); 
 
   if (this->extra_args) 
   { 
      add_ref(this->extra_args); 
      push_array_items(this->extra_args); 
 
      DEBUG((stderr,"_-callback args=%d\n",2+this->extra_args->size)); 
 
      dmalloc_touch_svalue(callback_function); 
      apply_svalue(callback_function,2+this->extra_args->size); 
   } 
   else 
   { 
      DEBUG((stderr,"_-callback args=%d\n",2)); 
 
      dmalloc_touch_svalue(callback_function); 
      apply_svalue(callback_function,2); 
   } 
 
   UNSET_ONERROR(uwp); 
   this->start=NULL; 
} 
 
struct uwp_pos 
{ 
  struct parser_html_storage *this; 
  struct location orig_pos; 
}; 
 
static void restore_pos (struct uwp_pos *uwp_pos) 
{ 
  uwp_pos->this->top.pos = uwp_pos->orig_pos; 
} 
 
static newstate data_callback (struct parser_html_storage *this, 
                               struct object *thisobj, 
                               struct feed_stack *st) 
{ 
  newstate res; 
  ptrdiff_t cstart = 0, cend; 
  struct uwp_pos uwp_pos; 
  ONERROR uwp; 
 
#ifdef PIKE_DEBUG 
  if (TYPEOF(this->callback__data) == T_INT || !this->data_cb_feed) 
    Pike_fatal ("data_callback called in bogus state.\n"); 
#endif 
 
  cend = this->data_cb_feed_end->s->len; 
 
  DEBUG((stderr, "%*d calling _data callback %p:0..%p:%"PRINTPTRDIFFT"d\n", 
         this->stack_count, this->stack_count, 
         this->data_cb_feed, this->data_cb_feed_end, cend)); 
 
  uwp_pos.this = this; 
  uwp_pos.orig_pos = this->top.pos; 
  SET_ONERROR (uwp, restore_pos, &uwp_pos); 
  this->top.pos = this->data_cb_pos; 
 
  this->type = TYPE_DATA; 
  do_callback (this, thisobj, 
               &(this->callback__data), 
               this->data_cb_feed, 0, this->data_cb_feed_end, cend); 
 
  UNSET_ONERROR (uwp); 
  this->top.pos = uwp_pos.orig_pos; 
 
  res = handle_result (this, st, 
                       &this->data_cb_feed, &cstart, this->data_cb_feed_end, cend, 
                       0); 
 
  if (res == STATE_REPARSE) { 
    /* Known bug: Should really bring back the original feed to handle 
     * this correctly. */ 
    low_add_local_feed (this, this->data_cb_feed); 
    this->data_cb_feed = NULL; 
    return STATE_REREAD; 
  } 
  else { 
#ifdef PIKE_DEBUG 
    if (res != STATE_DONE && res != STATE_REREAD) 
      Pike_fatal ("Unexpected state after data callback.\n"); 
#endif 
    do { 
      struct piece *next = this->data_cb_feed->next; 
      really_free_piece (this->data_cb_feed); 
      this->data_cb_feed = next; 
    } while (this->data_cb_feed); 
  } 
 
  return res; 
} 
 
#define CALL_CALLBACK(TYPESTR, PREPARE_SPEC) {                                \ 
   int args;                                                            \ 
   ONERROR uwp;                                                         \ 
   newstate res;                                                        \ 
                                                                        \ 
   if (TYPEOF(*v) == T_STRING) {                                        \ 
     if (this->flags & FLAG_REPARSE_STRINGS) {                          \ 
       add_local_feed (this, v->u.string);                              \ 
       skip_feed_range(st,cutstart,ccutstart,cutend,ccutend);           \ 
       return STATE_REREAD;                                             \ 
     }                                                                  \ 
     else {                                                             \ 
       if (TYPEOF(this->callback__data) != T_INT && this->data_cb_feed) { \ 
         res = data_callback (this, thisobj, st);                       \ 
         return res ? res : STATE_REREAD;                               \ 
       }                                                                \ 
       put_out_feed(this,v);                                            \ 
       skip_feed_range(st,cutstart,ccutstart,cutend,ccutend);           \ 
       return STATE_DONE;                                               \ 
     }                                                                  \ 
   }                                                                    \ 
                                                                        \ 
   if (TYPEOF(this->callback__data) != T_INT && this->data_cb_feed) {   \ 
     res = data_callback (this, thisobj, st);                           \ 
     return res ? res : STATE_REREAD;                                   \ 
   }                                                                    \ 
                                                                        \ 
   switch (TYPEOF(*v))                                                  \ 
   {                                                                    \ 
      case T_FUNCTION:                                                  \ 
      case T_OBJECT:                                                    \ 
         push_svalue(v);                                                \ 
         break;                                                         \ 
      case T_ARRAY:                                                     \ 
         if (v->u.array->size)                                          \ 
         {                                                              \ 
            push_svalue(v->u.array->item);                              \ 
            break;                                                      \ 
         }                                                              \ 
      default:                                                          \ 
         Pike_error("Parser.HTML: illegal type found "                  \ 
                    "when trying to call " TYPESTR " callback\n");      \ 
   }                                                                    \ 
                                                                        \ 
   this->start=*cutstart;                                               \ 
   this->cstart=*ccutstart;                                             \ 
   this->end=cutend;                                                    \ 
   this->cend=ccutend;                                                  \ 
                                                                        \ 
   SET_ONERROR(uwp,clear_start,this);                                   \ 
                                                                        \ 
   ref_push_object(thisobj);                                            \ 
   PREPARE_SPEC                                                         \ 
                                                                        \ 
   if (TYPEOF(*v)==T_ARRAY && v->u.array->size>1)                       \ 
   {                                                                    \ 
      assign_svalues_no_free(sp,v->u.array->item+1,                     \ 
                             v->u.array->size-1,                        \ 
                             v->u.array->type_field);                   \ 
      sp+=v->u.array->size-1;                                           \ 
      args+=v->u.array->size-1;                                         \ 
   }                                                                    \ 
                                                                        \ 
   if (this->extra_args)                                                \ 
   {                                                                    \ 
      add_ref(this->extra_args);                                        \ 
      push_array_items(this->extra_args);                               \ 
      args+=this->extra_args->size;                                     \ 
   }                                                                    \ 
                                                                        \ 
   DEBUG((stderr,TYPESTR " callback args=%d\n",args));                  \ 
   f_call_function(args);                                               \ 
                                                                        \ 
   UNSET_ONERROR(uwp);                                                  \ 
   this->start=NULL;                                                    \ 
                                                                        \ 
   return handle_result(this,st,cutstart,ccutstart,cutend,ccutend,1);   \ 
} 
 
static newstate entity_callback(struct parser_html_storage *this, 
                                struct object *thisobj, 
                                struct svalue *v, 
                                struct feed_stack *st, 
                                struct piece **cutstart, ptrdiff_t *ccutstart, 
                                struct piece *cutend, ptrdiff_t ccutend) 
{ 
  CALL_CALLBACK ("entity", { 
    args=2; 
  }); 
} 
 
static newstate tag_callback(struct parser_html_storage *this, 
                             struct object *thisobj, 
                             struct svalue *v, 
                             struct feed_stack *st, 
                             struct piece **cutstart, ptrdiff_t *ccutstart, 
                             struct piece *cutend, ptrdiff_t ccutend) 
{ 
  CALL_CALLBACK ("tag", { 
    tag_args(this,this->start,this->cstart,NULL,1,1); 
    args=3; 
    this->type=TYPE_TAG; 
  }); 
} 
 
static newstate container_callback(struct parser_html_storage *this, 
                                   struct object *thisobj, 
                                   struct svalue *v, 
                                   struct piece *startc, ptrdiff_t cstartc, 
                                   struct piece *endc, ptrdiff_t cendc, 
                                   struct feed_stack *st, 
                                   struct piece **cutstart, ptrdiff_t *ccutstart, 
                                   struct piece *cutend, ptrdiff_t ccutend) 
{ 
  CALL_CALLBACK ("container", { 
    tag_args(this,this->start,this->cstart,NULL,1,1); 
    push_feed_range(startc,cstartc,endc,cendc); 
    args=4; 
    this->type=TYPE_CONT; 
  }); 
} 
 
static newstate quote_tag_callback(struct parser_html_storage *this, 
                                   struct object *thisobj, 
                                   struct svalue *v, 
                                   struct piece *startc, ptrdiff_t cstartc, 
                                   struct piece *endc, ptrdiff_t cendc, 
                                   struct feed_stack *st, 
                                   struct piece **cutstart, ptrdiff_t *ccutstart, 
                                   struct piece *cutend, ptrdiff_t ccutend) 
{ 
  CALL_CALLBACK ("quote tag", { 
    push_feed_range(startc,cstartc,endc,cendc); 
    args=3; 
    this->type=TYPE_QTAG; 
  }); 
} 
 
static newstate find_end_of_container(struct parser_html_storage *this, 
                                      struct svalue *tagname, 
                                      struct piece *feed, ptrdiff_t c, 
                                      struct piece **e1, ptrdiff_t *ce1, 
                                      struct piece **e2, ptrdiff_t *ce2, 
                                      int finished) 
/* e1/ce1 is set to the first position of the end tag. e2/ce2 is set 
 * to the first position after the end tag. Both are set to the end of 
 * the feed if finished is set and an end tag wasn't found. */ 
{ 
   struct piece *s1,*s2,*s3; 
   ptrdiff_t c1,c2,c3; 
   newstate res; 
 
   DEBUG_MARK_SPOT("find_end_of_container",feed,c); 
 
   for (;;) 
   { 
      int found, endtag, got_fin; 
 
      DEBUG_MARK_SPOT("find_end_of_cont : loop",feed,c); 
      if (!scan_forward(feed,c,&s1,&c1,&TAG_START(this),1)) 
      { 
         if (!finished) 
         { 
            DEBUG_MARK_SPOT("find_end_of_cont : wait",s1,c1); 
            return STATE_WAIT; /* please wait */ 
         } 
         else 
         { 
            DEBUG_MARK_SPOT("find_end_of_cont : forced end",s1,c1); 
            *e1=*e2=s1; 
            *ce1=*ce2=c1; 
            return STATE_DONE; /* end of tag, sure... */ 
         } 
      } 
      DEBUG_MARK_SPOT("find_end_of_cont : found tag",s1,c1); 
 
      /* scan ws to start of tag name */ 
      if (this->flags & FLAG_WS_BEFORE_TAG_NAME) { 
        if (!scan_forward(s1,c1+1,&s2,&c2,WS(this), 
                          -(ptrdiff_t)N_WS(this))) { 
          DEBUG_MARK_SPOT("find_end_of_cont : wait at pre tag ws",s2,c2); 
          return STATE_WAIT; /* come again */ 
        } 
      } 
      else FORWARD_CHAR (s1, c1, s2, c2); 
 
      if (m_sizeof(this->mapqtag)) { 
        /* must check nested quote tags, since they affect the syntax itself */ 
        struct svalue *v; 
        if (!quote_tag_lookup(this,s2,c2,&s3,&c3,finished,&v)) { 
          DEBUG_MARK_SPOT("find_end_of_cont : wait at quote tag",s2,c2); 
          return STATE_WAIT; /* come again */ 
        } 
        if (v) { 
          DEBUG_MARK_SPOT("find_end_of_cont : quote tag",s2,c2); 
          if (!scan_for_string(this,s3,c3,&s3,&c3,v[2].u.string)) { 
            if (finished) { 
              DEBUG_MARK_SPOT("find_end_of_cont : forced end in quote tag",s1,c1); 
              *e1 = *e2 = s3; 
              *ce1 = *ce2 = c3; 
              return STATE_DONE; 
            } 
            else { 
              DEBUG_MARK_SPOT("find_end_of_cont : wait in quote tag",s1,c1); 
              return STATE_WAIT; 
            } 
          } 
          n_pos_forward(s3,c3+v[2].u.string->len,&feed,&c); 
          DEBUG_MARK_SPOT("find_end_of_cont : quote tag end",feed,c); 
          continue; 
        } 
      } 
 
      /* tag or end tag? */ 
      endtag = index_shared_string (s2->s, c2) == TAG_FIN (this); 
      if (endtag) c2++; 
 
      /* scan tag name as argument and push */ 
      if (!scan_forward_arg(this,s2,c2,&s3,&c3,SCAN_ARG_PUSH,1,finished,NULL)) 
      { 
        DEBUG_MARK_SPOT("find_end_of_cont : wait at tag name",s2,c2); 
        return STATE_WAIT; /* come again */ 
      } 
#ifdef HTML_DEBUG 
      if (endtag) 
        DEBUG_MARK_SPOT("find_end_of_cont : got end tag",s2,c2); 
      else 
        DEBUG_MARK_SPOT("find_end_of_cont : got tag",s2,c2); 
#endif 
 
      if (this->flags & FLAG_CASE_INSENSITIVE_TAG) 
        f_lower_case(1); 
      found = is_eq(sp-1,tagname); 
 
      /* When we should treat unknown tags as text, we have to parse 
       * and lookup just the tag name before continuing. This is an 
       * awkward situation, since there's no telling whether the 
       * parser used for the contents will define the same tags. */ 
      if (!found && (THIS->flags & FLAG_IGNORE_UNKNOWN)) { 
        if (endtag) { 
          struct pike_string *s; 
          push_string (TAG_FIN_STRING (this)); 
          s = add_shared_strings (sp[-1].u.string, sp[-2].u.string); 
          pop_stack(); 
          push_string (s); 
        } 
        else 
          stack_dup(); 
        if (!((m_sizeof(this->maptag) && 
               (low_mapping_lookup(this->maptag,sp-1) || 
               ((THIS->flags & (FLAG_XML_TAGS|FLAG_STRICT_TAGS)) == 
                (FLAG_XML_TAGS|FLAG_STRICT_TAGS) && 
                low_mapping_lookup(this->maptag,sp-2)))) || 
              (m_sizeof(this->mapcont) && 
               (low_mapping_lookup(this->mapcont,sp-1) || 
                low_mapping_lookup(this->mapcont,sp-2))))) { 
          DEBUG((stderr,"find_end_of_cont : treating unknown tag as text\n")); 
          feed = s3; 
          c = c3; 
          pop_n_elems (2); 
          continue; 
        } 
        pop_stack(); 
      } 
 
      if (tagname->u.string->len > sp[-1].u.string->len && 
          c3 < s3->s->len && index_shared_string (s3->s,c3) == TAG_START (this)) { 
        struct pike_string *cmp = 
          string_slice (tagname->u.string, 0, sp[-1].u.string->len); 
        if (cmp == sp[-1].u.string) { 
          DEBUG_MARK_SPOT("find_end_of_cont : skipping open endtag prefix",s2,c2); 
          free_string (cmp); 
          feed = s3; 
          c = c3; 
          pop_stack(); 
          continue; 
        } 
        free_string (cmp); 
      } 
 
      pop_stack(); 
 
      if (!scan_for_end_of_tag(this,s3,c3,&s2,&c2,finished, 
                               found && endtag ? -1 : this->flags & FLAG_MATCH_TAG, 
                               &got_fin)) 
      { 
        DEBUG_MARK_SPOT("find_end_of_cont : wait at tag end",s2,c2); 
         return STATE_WAIT; 
      } 
 
      if (found) { 
        if (!endtag) { /* push a new level */ 
          if (got_fin && this->flags & FLAG_XML_TAGS) { 
            DEBUG_MARK_SPOT("find_end_of_cont : skipping empty element tag",s2,c2+1); 
            feed = s2; 
            c = c2+1; 
            continue; 
          } 
 
          DEBUG_MARK_SPOT("find_end_of_cont : push",s2,c2+1); 
          res=find_end_of_container(this,tagname, 
                                    s2,++c2,e1,ce1,&feed,&c,finished); 
          DEBUG_MARK_SPOT("find_end_of_cont : push end",feed,c); 
          if (res) 
          { 
            DEBUG((stderr,"find_end_of_cont : " 
                   "(pushed) return %d %p:%"PRINTPTRDIFFT"d\n", 
                   res,s1,c1)); 
            return res; 
          } 
        } 
 
        else { 
          if (index_shared_string (s2->s, c2) == TAG_START (this)) { 
            DEBUG_MARK_SPOT("find_end_of_cont : skipping open endtag",s2,c2); 
            feed = s2; 
            c = c2; 
            continue; 
          } 
 
          DEBUG_MARK_SPOT("find_end_of_cont : got cont end   --> ",s1,c1); 
          DEBUG_MARK_SPOT("find_end_of_cont : got endtag end --> ",s2,c2); 
 
          *e1=s1; 
          *ce1=c1; 
          *e2=s2; 
          *ce2=c2+1; 
 
          return STATE_DONE; 
        } 
      } 
 
      else { 
        feed=s2; 
        c=c2; 
      } 
   } 
} 
 
/* ---------------------------------------------------------------- */ 
 
static newstate do_try_feed(struct parser_html_storage *this, 
                            struct object *thisobj, 
                            struct feed_stack *st, 
                            struct piece **feed, 
                            int finished, 
                            int ignore_tag_cb) 
{ 
   p_wchar2 look_for[2 /* entity or tag */]; 
   p_wchar2 ch; 
   int n; 
   struct piece *dst = *feed; 
   ptrdiff_t cdst = st->c; 
   newstate res; 
   int flags = this->flags; 
   enum contexts ctx = this->out_ctx; 
 
   int scan_entity; 
 
   recheck_scan(this,&scan_entity); 
 
   for (;;) 
   { 
    switch (ctx) { 
 
      case CTX_DATA: 
      DEBUG((stderr,"%*d do_try_feed scan in data " 
             "scan_entity=%d ignore_data=%d\n", 
             this->stack_count,this->stack_count, 
             scan_entity,st->ignore_data)); 
 
#ifdef PIKE_DEBUG 
      if (*feed && feed[0]->s->len < st->c) 
         Pike_fatal("len (%ld) < st->c (%ld)\n", 
               TO_LONG(feed[0]->s->len), TO_LONG(st->c)); 
      if (*feed && cmp_feed_pos (*feed, st->c, dst, cdst) > 0) 
        Pike_fatal ("Going backwards from %p:%ld to %p:%ld.\n", 
               (void *)(*feed), TO_LONG(st->c), (void *)dst, TO_LONG(cdst)); 
#endif 
 
      /* do we need to check data? */ 
      if (!st->ignore_data && *feed) 
      { 
         DEBUG_MARK_SPOT("do_try_feed scan for data from", *feed, st->c); 
 
         /* we are to get data first */ 
         /* look for tag or entity */ 
         n=0; 
         if (scan_entity) look_for[n++] = ENTITY_START (this); 
         if (st->parse_tags) look_for[n++] = TAG_START (this); 
         scan_forward(dst,cdst,&dst,&cdst,look_for,n); 
         DEBUG_MARK_SPOT("do_try_feed scan for data to", dst, cdst); 
 
         if (*feed != dst || st->c != cdst) { /* Found some data. */ 
           ignore_tag_cb = 0; 
 
           if (TYPEOF(this->callback__data) != T_INT) { 
             struct piece *f; 
             DEBUG((stderr, "put data cb feed range " 
                    "%p:%"PRINTPTRDIFFT"d - %p:%"PRINTPTRDIFFT"d\n", 
                    *feed, st->c, dst, cdst)); 
             push_feed_range (*feed, st->c, dst, cdst); 
             f = alloc_piece(); 
             f->s = (--sp)->u.string; 
             if (this->data_cb_feed) { 
               this->data_cb_feed_end->next = f; 
               this->data_cb_feed_end = f; 
             } 
             else { 
               this->data_cb_feed = this->data_cb_feed_end = f; 
               this->data_cb_pos = st->pos; 
             } 
           } 
           else 
             put_out_feed_range(this, *feed, st->c, dst, cdst); 
 
           skip_feed_range(st,feed,&(st->c),dst,cdst); 
         } 
         else if (!dst->next && dst->s->len == cdst) { 
           /* Skip a last empty piece. */ 
           really_free_piece (*feed); 
           *feed = NULL; 
         } 
      } 
      /* at end, entity or tag */ 
 
      if (!*feed || cdst == dst->s->len) 
      { 
         DEBUG((stderr,"%*d do_try_feed end\n", 
                this->stack_count,this->stack_count)); 
         return STATE_DONE; /* done */ 
      } 
 
#ifdef PIKE_DEBUG 
      if (*feed != dst || st->c != cdst) 
        Pike_fatal ("Internal position confusion: feed: %p:%ld, dst: %p:%ld.\n", 
               (void *)(*feed), TO_LONG(st->c), (void *)dst, TO_LONG(cdst)); 
#endif 
 
      ch=index_shared_string(dst->s,cdst); 
      if (ch == TAG_START (this)) /* tag */ 
      { 
         struct piece *tagend = NULL; 
         ptrdiff_t ctagend; 
 
         DEBUG((stderr,"%*d do_try_feed scan tag %p:%"PRINTPTRDIFFT"d\n", 
                this->stack_count,this->stack_count, 
                *feed,st->c)); 
#ifdef PIKE_DEBUG 
       if (!st->parse_tags) 
           Pike_fatal ("What am I doing parsing tags now?\n"); 
#endif 
 
         /* skip ws to start of tag name */ 
         if (flags & FLAG_WS_BEFORE_TAG_NAME) { 
           if (!scan_forward(*feed,st->c+1,&dst,&cdst, 
                             WS(this), 
                             -(ptrdiff_t)N_WS(this)) && !finished) 
           { 
             st->ignore_data=1; 
             return STATE_WAIT; /* come again */ 
           } 
         } 
         else { 
           FORWARD_CHAR (*feed, st->c, dst, cdst); 
           if (cdst >= dst->s->len && !finished) return STATE_WAIT; 
         } 
 
         if (m_sizeof(this->mapqtag)) { 
           struct piece *e1,*e2; /* <![CDATA[ ... ]]>  */ 
           ptrdiff_t ce1,ce2;    /*       e1 ^    ^ e2 */ 
           struct svalue *entry; 
 
           if (!quote_tag_lookup(this,dst,cdst,&e1,&ce1,finished,&entry)) { 
             st->ignore_data=1; 
             return STATE_WAIT; /* come again */ 
           } 
 
           if (entry) {             /* quote tag */ 
             if (scan_for_string(this,e1,ce1,&e2,&ce2,entry[2].u.string)) 
               n_pos_forward(e2,ce2+entry[2].u.string->len,&dst,&cdst); 
             else { 
               if (!finished) { 
                 st->ignore_data=1; 
                 return STATE_WAIT; /* come again */ 
               } 
               dst = e2; 
               cdst = ce2; 
             } 
 
             push_svalue(entry+1); 
             if ((res=quote_tag_callback(this,thisobj,sp-1, 
                                         e1,ce1,e2,ce2, 
                                         st,feed,&(st->c),dst,cdst))) 
             { 
               DEBUG((stderr,"%*d quote tag callback return %d " 
                      "%p:%"PRINTPTRDIFFT"d\n", this->stack_count, 
                      this->stack_count, res,*feed,st->c)); 
               st->ignore_data=(res==STATE_WAIT); 
               pop_stack(); 
               return res; 
             } 
 
             DEBUG((stderr,"%*d quote tag callback done %p:%"PRINTPTRDIFFT"d\n", 
                    this->stack_count,this->stack_count, 
                    *feed,st->c)); 
 
             pop_stack(); 
             recheck_scan(this,&scan_entity); 
             goto done; 
           } 
         } 
 
         /* scan tag name as argument and push */ 
         if (!scan_forward_arg(this,dst,cdst,&dst,&cdst,SCAN_ARG_PUSH,1,finished,NULL)) 
         { 
           st->ignore_data=1; 
           return STATE_WAIT; /* come again */ 
         } 
 
         if (m_sizeof(this->maptag) || 
             m_sizeof(this->mapcont)) 
         { 
            int empty_tag; 
            struct svalue *tag, *cont; 
            struct piece *e1,*e2; /* <tag> ... </tag>     */ 
            ptrdiff_t ce1,ce2;    /*        e1 ^     ^ e2 */ 
 
            if (!scan_for_end_of_tag(this,dst,cdst,&tagend,&ctagend,finished, 
                                     flags & FLAG_MATCH_TAG, &empty_tag)) 
            { 
              st->ignore_data=1; 
              pop_stack(); 
              return STATE_WAIT; /* come again */ 
            } 
 
            if (flags & FLAG_CASE_INSENSITIVE_TAG) 
              f_lower_case(1); 
 
            tag = NULL, cont = NULL; 
            if (flags & FLAG_XML_TAGS) { /* decide what to do from tag syntax */ 
              if (empty_tag) {      /* <foo/> */ 
                tag=low_mapping_lookup(this->maptag,sp-1); 
                if (!tag) cont=low_mapping_lookup(this->mapcont,sp-1); 
              } 
              else {                /* <foo> */ 
                cont=low_mapping_lookup(this->mapcont,sp-1); 
                if (!cont) { 
                  tag=low_mapping_lookup(this->maptag,sp-1); 
                  if (!tag || !(flags & FLAG_STRICT_TAGS)) empty_tag = 1; 
                } 
              } 
            } 
            else {          /* decide what to do from registered callbacks */ 
              tag=low_mapping_lookup(this->maptag,sp-1); 
              if (!tag && (cont=low_mapping_lookup(this->mapcont,sp-1))) 
                empty_tag = 0; 
              else empty_tag = 1; 
            } 
 
            if (empty_tag) {        /* no content */ 
              e1 = e2 = tagend; 
              ce1 = ce2 = ctagend + 1; 
              pop_stack(); 
            } 
            else { 
               /* this is the hardest part : find the corresponding end tag */ 
#ifdef PIKE_DEBUG 
             if (!tag && !cont) Pike_fatal ("You push that stone yourself!\n"); 
#endif 
 
               if ((res=find_end_of_container(this, 
                                              sp-1, 
                                              tagend,ctagend+1, 
                                              &e1,&ce1,&e2,&ce2, 
                                              finished))) 
               { 
                  DEBUG((stderr,"%*d find end of cont return %d " 
                         "%p:%"PRINTPTRDIFFT"d\n", this->stack_count, 
                         this->stack_count, res,*feed,st->c)); 
                  st->ignore_data=(res==STATE_WAIT); 
                  pop_stack(); 
                  return res; 
               } 
               pop_stack(); 
            } 
 
            if (tag) /* tag callback */ 
            { 
               /* low-level tag call */ 
               if ((res=tag_callback(this,thisobj,tag, 
                                     st,feed,&(st->c),dst=e2,cdst=ce2))) 
               { 
                  DEBUG((stderr,"%*d tag callback return %d " 
                         "%p:%"PRINTPTRDIFFT"d\n", this->stack_count, 
                         this->stack_count, res,*feed,st->c)); 
                  st->ignore_data=(res==STATE_WAIT); 
                  return res; 
               } 
 
               DEBUG((stderr,"%*d tag callback done %p:%"PRINTPTRDIFFT"d\n", 
                      this->stack_count,this->stack_count, 
                      *feed,st->c)); 
 
               recheck_scan(this,&scan_entity); 
               goto done; 
            } 
 
            else if (cont) /* container callback */ 
            { 
               /* low-level container call */ 
               if ((res=container_callback(this,thisobj,cont, 
                                           tagend,ctagend+1,e1,ce1, 
                                           st,feed,&(st->c),dst=e2,cdst=ce2))) 
               { 
                  DEBUG((stderr,"%*d container callback return %d " 
                         "%p:%"PRINTPTRDIFFT"d\n", this->stack_count, 
                         this->stack_count, res,*feed,st->c)); 
                  st->ignore_data=(res==STATE_WAIT); 
                  return res; 
               } 
 
               recheck_scan(this,&scan_entity); 
               goto done; 
            } 
         } 
         else pop_stack(); 
 
         dmalloc_touch_svalue(&(this->callback__tag)); 
 
         if (TYPEOF(this->callback__tag) != T_INT && !ignore_tag_cb) 
         { 
            if (!tagend && 
                !scan_for_end_of_tag(this,dst,cdst,&tagend,&ctagend,finished, 
                                     flags & FLAG_MATCH_TAG,NULL)) 
            { 
               st->ignore_data=1; 
               return STATE_WAIT; /* come again */ 
            } 
 
            if (TYPEOF(this->callback__data) != T_INT && this->data_cb_feed) { 
              res = data_callback (this, thisobj, st); 
              return res ? res : STATE_REREAD; 
            } 
 
            DEBUG((stderr,"%*d calling _tag callback " 
                   "%p:%"PRINTPTRDIFFT"d..%p:%"PRINTPTRDIFFT"d\n", 
                   this->stack_count,this->stack_count, 
                   *feed,st->c+1,tagend,ctagend)); 
 
            /* low-level tag call */ 
            this->type = TYPE_TAG; 
            do_callback(this,thisobj, 
                        &(this->callback__tag), 
                        *feed,st->c,dst=tagend,cdst=ctagend+1); 
 
            if ((res=handle_result(this,st,feed,&(st->c),dst,cdst,1))) 
            { 
               DEBUG((stderr,"%*d do_try_feed return %d %p:%"PRINTPTRDIFFT"d\n", 
                      this->stack_count,this->stack_count, res,*feed,st->c)); 
               st->ignore_data=(res==STATE_WAIT); 
               return res; 
            } 
            recheck_scan(this,&scan_entity); 
         } 
         else if (flags & FLAG_IGNORE_UNKNOWN) { 
           /* Send it to callback__data. */ 
           dst=*feed; 
           cdst=st->c+1; 
           goto done; 
         } 
         else 
           /* Begin parsing inside the tag for entities in arguments. */ 
           ctx = CTX_TAG; 
      } 
      else                  /* entity */ 
      { 
#ifdef PIKE_DEBUG 
      if (ch != ENTITY_START (this)) 
          Pike_fatal ("Oups! How did I end up here? There's no entity around.\n"); 
#endif 
      goto parse_entity;  /* same code as in tag arguments */ 
      } 
      break; 
 
      case CTX_TAG: { 
        struct piece *s1 = dst, *s2, *s3; 
        ptrdiff_t c1 = cdst, c2, c3; 
        int pushed = 0; 
 
        if (TYPEOF(this->callback__data) != T_INT && this->data_cb_feed) { 
          res = data_callback (this, thisobj, st); 
          return res ? res : STATE_REREAD; 
        } 
 
        /* NOTE: This somewhat duplicates tag_args(). */ 
 
        DEBUG((stderr,"%*d do_try_feed scan in tag\n", 
               this->stack_count,this->stack_count)); 
 
        /* Skip ws. */ 
        if (!scan_forward (s1, c1, &s2, &c2, WS(this), 
                           -(ptrdiff_t)N_WS(this))) 
          return STATE_WAIT; 
 
        while (1) { 
          if (s1 != s2 || c1 != c2) { 
            if (this->splice_arg && pushed) { /* Check for splice_arg name. */ 
              if (pushed > 1) { 
                f_add (pushed); 
                pushed = 1; 
              } 
              if (sp[-1].u.string == this->splice_arg) { 
                DEBUG_MARK_SPOT ("do_try_feed arg ent scan: at empty splice arg", 
                                 s1, c1); 
                break; 
              } 
              else { 
                pop_stack(); 
                pushed = 0; 
              } 
            } 
 
            dst = s2, cdst = c2; /* New arg name. */ 
          } 
 
          /* Scan arg name. */ 
          DEBUG_MARK_SPOT ("do_try_feed at arg name", s2, c2); 
          if (!scan_forward_arg (this, s2, c2, &s3, &c3, 
                                 this->splice_arg ? SCAN_ARG_PUSH : SCAN_ARG_ONLY, 
                                 1, finished, NULL)) { 
            pop_n_elems (pushed); 
            return STATE_WAIT; 
          } 
          if (this->splice_arg) pushed++; 
 
          /* Skip ws. */ 
          scan_forward (s3, c3, &s1, &c1, WS(this), 
                        -(ptrdiff_t)N_WS(this)); 
          if (c1 == s1->s->len) { 
            pop_n_elems (pushed); 
            return STATE_WAIT; 
          } 
 
          DEBUG_MARK_SPOT ("do_try_feed after arg name", s1, c1); 
          ch = index_shared_string (s1->s, c1); 
 
          if (ch == ARG_EQ (this)) { 
            c1++; 
            DEBUG_MARK_SPOT ("do_try_feed arg ent scan: before tag arg", s1, c1); 
            ctx = CTX_TAG_ARG; 
            break; 
          } 
 
          else if (ch == TAG_FIN (this) && this->splice_arg && 
                   s3 == s1 && c3 == c1) { 
            DEBUG_MARK_SPOT ("do_try_feed arg ent scan: at tag fin", s1, c1); 
            /* Got to build the arg name. '/' may be in the middle of it. */ 
            FORWARD_CHAR (s1, c1, s1, c1); 
            if ((flags & (FLAG_STRICT_TAGS|FLAG_XML_TAGS)) != FLAG_STRICT_TAGS) { 
              /* But then again, maybe it isn't.. */ 
              if (c1 == s1->s->len) { 
                pop_n_elems (pushed); 
                return STATE_WAIT; 
              } 
              ch = index_shared_string (s1->s, c1); 
              if (ch == TAG_END (this)) { 
                c1++; 
                DEBUG_MARK_SPOT ("do_try_feed arg ent scan: at tag end", s1, c1); 
                ctx = CTX_DATA; 
                break; 
              } 
            } 
            push_string (TAG_FIN_STRING (this)); 
            pushed++; 
          } 
 
          else if (ch == TAG_END (this)) { 
            c1++; 
            DEBUG_MARK_SPOT ("do_try_feed arg ent scan: at tag end", s1, c1); 
            ctx = CTX_DATA; 
            break; 
          } 
 
          else if (s2 == s3 && c2 == c3) { 
            /* scan_forward_arg() didn't skip -- bogus character. */ 
            DEBUG_MARK_SPOT ("do_try_feed bogus char", s1, c1); 
            c1++; 
            dst = s1, cdst = c1; /* New arg name. */ 
            pop_n_elems (pushed); 
            pushed = 0; 
          } 
 
          else if (s3 != s1 || c3 != c1) { /* Has already skipped ws. */ 
            s2 = s1, c2 = c1; 
            s1 = s3, c1 = c3; 
            continue; 
          } 
 
          /* Skip ws. */ 
          if (!scan_forward (s1, c1, &s2, &c2, WS(this), 
                             -(ptrdiff_t)N_WS(this))) { 
            pop_n_elems (pushed); 
            return STATE_WAIT; 
          } 
        } 
 
        if (pushed) { 
          if (pushed > 1) f_add (pushed); 
          if (sp[-1].u.string == this->splice_arg) { /* Got a splice arg. */ 
            pop_stack(); 
 
            if (ctx == CTX_TAG_ARG) { /* Got an arg value to splice. */ 
              if (!scan_forward (s1, c1, &s1, &c1, WS(this), 
                                 -(ptrdiff_t)N_WS(this)) || 
                  !scan_forward_arg (this, s1, c1, &s2, &c2, 
                                     SCAN_ARG_PUSH, 0, finished, NULL)) { 
                DEBUG((stderr,"%*d do_try_feed wait in splice arg at " 
                       "%p:%"PRINTPTRDIFFT"d\n", this->stack_count, 
                       this->stack_count,s1,c1)); 
 
                /* Put out collected data up to the splice arg name. */ 
                put_out_feed_range(this,*feed,st->c,dst,cdst); 
                skip_feed_range(st,feed,&(st->c),dst,cdst); 
                this->out_ctx = CTX_TAG_ARG; 
                return STATE_WAIT; 
              } 
 
              DEBUG((stderr,"%*d do_try_feed got splice arg " 
                     "%p:%"PRINTPTRDIFFT"d..%p:%"PRINTPTRDIFFT"d\n", 
                     this->stack_count,this->stack_count,s1,c1,s2,c2)); 
 
              /* Put out collected data, but skip the splice arg itself. */ 
              put_out_feed_range(this,*feed,st->c,dst,cdst); 
              skip_feed_range(st,feed,&(st->c),s2,c2); 
 
              /* Put the arg value in a nested feed and parse it in 
               * the CTX_SPLICE_ARG context. */ 
              add_local_feed (this, sp[-1].u.string); 
              this->out_ctx = CTX_SPLICE_ARG; 
              return STATE_REREAD; 
            } 
 
            else { 
              /* Just a splice arg without value. Ignore it. */ 
              put_out_feed_range(this,*feed,st->c,dst,cdst); 
              if (ctx == CTX_DATA) 
                put_out_feed_range(this,s3,c3,s1,c1); /* Put trailing tag end. */ 
              skip_feed_range(st,feed,&(st->c),dst=s1,cdst=c1); 
              this->out_ctx = ctx; 
              break; 
            } 
          } 
          else pop_stack(); 
        } 
 
        put_out_feed_range(this,*feed,st->c,dst=s1,cdst=c1); 
        skip_feed_range(st,feed,&(st->c),dst,cdst); 
        this->out_ctx = ctx; 
        break; 
      } 
 
      case CTX_SPLICE_ARG: 
#ifdef PIKE_DEBUG 
      if (this->data_cb_feed) 
          Pike_fatal ("Shouldn't have data cb feed in splice arg context.\n"); 
#endif 
 
        if (!*feed) { 
          DEBUG((stderr,"%*d do_try_feed end in splice tag\n", 
                 this->stack_count,this->stack_count)); 
          this->out_ctx = CTX_TAG; 
          return STATE_DONE; 
        } 
 
        if (!scan_entity) { /* Just put out everything. */ 
          DEBUG((stderr,"%*d do_try_feed put out whole splice tag\n", 
                 this->stack_count,this->stack_count)); 
          while (dst->next) dst = dst->next; 
          put_out_feed_range(this,*feed,st->c,dst,cdst=dst->s->len); 
          skip_feed_range(st,feed,&(st->c),dst,cdst); 
          this->out_ctx = CTX_TAG; 
          return STATE_DONE; 
        } 
 
        /* Search for entities. Let anything through otherwise. */ 
 
        DEBUG((stderr,"%*d do_try_feed entity scan in splice tag\n", 
               this->stack_count,this->stack_count)); 
 
        res = scan_forward (dst, cdst, &dst, &cdst, &ENTITY_START (this), 1); 
        put_out_feed_range(this,*feed,st->c,dst,cdst); 
        skip_feed_range(st,feed,&(st->c),dst,cdst); 
 
        if (!res) { 
          this->out_ctx = CTX_TAG; 
          return STATE_DONE; 
        } 
        goto parse_entity; 
 
      default: { 
        int quote; 
 
#ifdef PIKE_DEBUG 
      if (this->data_cb_feed) 
          Pike_fatal ("Shouldn't have data cb feed in tag arg context.\n"); 
#endif 
 
        if (!*feed) { 
          DEBUG((stderr,"%*d do_try_feed end in tag arg\n", 
                 this->stack_count,this->stack_count)); 
          this->out_ctx = ctx; 
          return STATE_DONE; 
        } 
 
        DEBUG((stderr,"%*d do_try_feed scan in tag arg\n", 
               this->stack_count,this->stack_count)); 
 
        /* Skip ws. */ 
        if (!scan_forward (dst, cdst, &dst, &cdst, WS(this), 
                           -(ptrdiff_t)N_WS(this))) 
          return STATE_WAIT; 
 
      continue_in_arg: 
        quote = ctx - CTX_TAG_QUOTED_ARG; 
 
        DEBUG_MARK_SPOT ("do_try_feed at arg val", dst, cdst); 
        if (!(res = scan_forward_arg (this, dst, cdst, &dst, &cdst, 
                                      scan_entity ? SCAN_ARG_ENT_BREAK : SCAN_ARG_ONLY, 
                                      0, finished, &quote))) 
          return STATE_WAIT; 
 
        if (res == 1) {             /* arg end */ 
          ctx = CTX_TAG; 
          break; 
        } 
 
        /* entity in tag argument */ 
        DEBUG_MARK_SPOT ("do_try_feed arg ent scan: at entity start", dst, cdst); 
        put_out_feed_range(this,*feed,st->c,dst,cdst); 
        skip_feed_range(st,feed,&(st->c),dst,cdst); 
        ctx = this->out_ctx = CTX_TAG_QUOTED_ARG + quote; 
      } 
 
        /* FALL THROUGH */ 
    parse_entity: { 
        p_wchar2 end_found; 
 
        DEBUG((stderr,"%*d do_try_feed scan entity %p:%"PRINTPTRDIFFT"d\n", 
               this->stack_count,this->stack_count, *feed,st->c)); 
 
#ifdef PIKE_DEBUG 
      if (!scan_entity) Pike_fatal ("Shouldn't parse entities now.\n"); 
        if (*feed != dst || st->c != cdst) 
          Pike_fatal ("Internal position confusion: feed: %p:%ld, dst: %p:%ld\n", 
                 (void *)(*feed), TO_LONG(st->c), (void *)dst, TO_LONG(cdst)); 
#endif 
      /* just search for end of entity */ 
 
        if (flags & FLAG_LAZY_ENTITY_END) 
          end_found = 
            scan_forward(dst,cdst+1,&dst,&cdst, 
                         LAZY_ENTITY_ENDS (this), N_LAZY_ENTITY_ENDS (this)) ? 
            index_shared_string(dst->s,cdst) : 0; 
        else if (flags & FLAG_NESTLING_ENTITY_END) 
        { 
           p_wchar2 look_for[2]; 
           int level=1; 
 
           look_for[0] = ENTITY_START (this); 
           look_for[1] = ENTITY_END (this); 
 
           for (;;) 
           { 
              if (!scan_forward(dst,cdst+1,&dst,&cdst,look_for,2)) 
              { 
                 end_found=0; 
                 break; 
              } 
              if (index_shared_string(dst->s,cdst) == ENTITY_END (this)) 
              { 
                 if (!--level) 
                 { 
                    end_found = ENTITY_END (this); 
                    break; 
                 } 
              } 
              else 
                 level++; 
           } 
        } 
        else 
          end_found = 
            scan_forward(dst,cdst+1,&dst,&cdst,&ENTITY_END(this),1) ? 
            ENTITY_END (this) : 0; 
 
        if (end_found != ENTITY_END (this)) { 
          if (finished) {   /* got no entity end */ 
            cdst=st->c+1; 
            goto done; 
          } 
          else { 
            st->ignore_data=1; /* no data first at next call */ 
            return STATE_WAIT; /* no end, call again */ 
          } 
        } 
 
        if (m_sizeof(this->mapentity)) 
        { 
          struct svalue *v; 
 
          push_feed_range(*feed,st->c+1,dst,cdst); 
          cdst++; 
 
          v=low_mapping_lookup(this->mapentity,sp-1); 
          pop_stack(); 
          if (v) /* entity we want, do a callback */ 
          { 
            /* low-level entity call */ 
            this->type = TYPE_ENTITY; 
            if ((res=entity_callback(this,thisobj,v, 
                                     st,feed,&(st->c),dst,cdst))) 
            { 
              DEBUG((stderr,"%*d entity callback return %d " 
                     "%p:%"PRINTPTRDIFFT"d\n", this->stack_count, 
                     this->stack_count, res,*feed,st->c)); 
              st->ignore_data=(res==STATE_WAIT); 
              return res; 
            } 
 
            DEBUG((stderr,"%*d entity callback done %p:%"PRINTPTRDIFFT"d\n", 
                   this->stack_count,this->stack_count, *feed,st->c)); 
 
            recheck_scan(this,&scan_entity); 
 
            switch (ctx) { 
              case CTX_DATA: 
              case CTX_SPLICE_ARG: 
                goto done; 
              default: 
                if (*feed) 
                  goto continue_in_arg; /* Shouldn't skip ws here. */ 
                goto done; 
            } 
          } 
        } 
        else cdst++; 
 
        dmalloc_touch_svalue(&(this->callback__entity)); 
 
        if (TYPEOF(this->callback__entity) != T_INT && !ignore_tag_cb) 
        { 
          if (TYPEOF(this->callback__data) != T_INT && this->data_cb_feed) { 
            res = data_callback (this, thisobj, st); 
            return res ? res : STATE_REREAD; 
          } 
 
          DEBUG((stderr,"%*d calling _entity callback " 
                 "%p:%"PRINTPTRDIFFT"d..%p:%"PRINTPTRDIFFT"d\n", 
                 this->stack_count,this->stack_count, *feed,st->c,dst,cdst)); 
 
          /* low-level entity call */ 
          this->type = TYPE_ENTITY; 
          do_callback(this,thisobj, 
                      &(this->callback__entity), 
                      *feed,st->c,dst,cdst); 
 
          if ((res=handle_result(this,st,feed,&(st->c),dst,cdst,1))) 
          { 
            DEBUG((stderr,"%*d do_try_feed return %d %p:%"PRINTPTRDIFFT"d\n", 
                   this->stack_count,this->stack_count, res,*feed,st->c)); 
            st->ignore_data=(res==STATE_WAIT); 
            return res; 
          } 
          recheck_scan(this,&scan_entity); 
 
          switch (ctx) { 
            case CTX_DATA: 
            case CTX_SPLICE_ARG: 
              goto done; 
            default: 
              if (*feed) 
                goto continue_in_arg; /* Shouldn't skip ws here. */ 
              goto done; 
          } 
        } 
        else if (flags & FLAG_IGNORE_UNKNOWN) { 
          dst=*feed; 
          cdst=st->c+1; 
        } 
        else if (*feed) 
        { 
          if (TYPEOF(this->callback__data) != T_INT && this->data_cb_feed && 
              (res = data_callback (this, thisobj, st))) 
            return res; 
          put_out_feed_range(this,*feed,st->c,dst,cdst); 
          skip_feed_range(st,feed,&(st->c),dst,cdst); 
        } 
      } 
    } 
 
done: 
 
      st->ignore_data=0; 
      ignore_tag_cb=0; 
   } 
} 
 
/****** try_feed - internal main ********************/ 
 
static void try_feed(int finished) 
{ 
   /* 
      o if tag_stack: 
          o pop & parse that 
          o ev put on stack 
   */ 
 
   struct feed_stack *st; 
   int ignore_tag_cb = 0; 
 
   if (THIS->start) return;     /* called from a callback - avoid recursion */ 
 
   for (;;) 
   { 
      newstate res; 
      struct piece **feed; 
      st=THIS->stack; 
      feed = &st->local_feed; 
      if (*feed) 
        res = do_try_feed(THIS,THISOBJ, 
                          st, 
                          feed, 
                          finished||(st->prev!=NULL), 
                          ignore_tag_cb); 
      else 
        res = STATE_WAIT;       /* Got no data. Try waiting. */ 
      ignore_tag_cb = 0; 
      switch (res) 
      { 
         case STATE_WAIT: /* incomplete, call again */ 
            st = THIS->stack; 
 
            if (!finished && st->prev == NULL) 
              return; 
 
            /* finished anyway, just output last bit */ 
            feed = &st->local_feed; 
            if (*feed) { 
              struct piece *end; 
              DEBUG((stderr, "%*d try_feed finishing from STATE_WAIT\n", 
                     THIS->stack_count, THIS->stack_count)); 
              for (end = *feed; end->next; end = end->next) {} 
              if (*feed != end || st->c != end->s->len) { 
                /* This is an incomplete tag or entity and not data. 
                 * Hence it's output directly regardless of the data cb. */ 
                put_out_feed_range (THIS, *feed, st->c, end, end->s->len); 
                skip_feed_range (st, feed, &(st->c), end, end->s->len); 
              } 
            } 
 
            /* FALL THROUGH */ 
         case STATE_DONE: /* done, pop stack */ 
            if (!THIS->top.local_feed) THIS->feed_end=NULL; 
 
            st=THIS->stack->prev; 
            if (!st) { 
               if (TYPEOF(THIS->callback__data) != T_INT && 
                   THIS->data_cb_feed && 
                   (res = data_callback (THIS, THISOBJ, st))) 
                 goto state_reread; 
               if (finished) reset_stack_head (THIS); 
               return; /* all done, but keep last stack elem */ 
            } 
 
#ifdef PIKE_DEBUG 
          if (THIS->stack->local_feed) 
               Pike_fatal("internal weirdness in Parser.HTML: feed left\n"); 
#endif 
 
            really_free_feed_stack (THIS->stack); 
            THIS->stack=st; 
            THIS->stack_count--; 
            break; 
 
         case STATE_REPARSE: /* user requested another go at the current data */ 
            if (st == THIS->stack) ignore_tag_cb = 1; 
            /* FALL THROUGH */ 
 
         state_reread: 
         case STATE_REREAD: /* reread stack head */ 
            if (THIS->stack_count>THIS->max_stack_depth) 
               Pike_error("Parser.HTML: too deep recursion\n"); 
            break; 
      } 
   } 
   UNREACHABLE(); 
} 
 
/****** feed ****************************************/ 
 
 
static void low_feed(struct pike_string *ps) 
{ 
   struct piece *f; 
 
   if (!ps->len) return; 
 
   f = alloc_piece(); 
   copy_shared_string(f->s,ps); 
 
   if (THIS->feed_end==NULL) 
   { 
#ifdef HTML_DEBUG 
      if (THIS->flags & FLAG_DEBUG_MODE) { 
        fprintf (stderr, "Initial search chars:\n"); 
        debug_print_search_chars (THIS); 
      } 
#endif 
      DEBUG_MARK_SPOT("new feed", f, 0); 
      THIS->top.local_feed=THIS->feed_end=f; 
   } 
   else 
   { 
      DEBUG_MARK_SPOT("attached to feed end", f, 0); 
      THIS->feed_end->next=f; 
      THIS->feed_end=f; 
   } 
} 
 
/*! @decl Parser.HTML feed() 
 *! @decl Parser.HTML feed(string s, void|int do_parse) 
 *! 
 *! Feed new data to the @[Parser.HTML] object. This will start a scan 
 *! and may result in callbacks. Note that it's possible that all data 
 *! fed isn't processed - to do that, call @[finish()]. 
 *! 
 *! If the function is called without arguments, no data is fed, but 
 *! the parser is run. If the string argument is followed by a 
 *! @expr{0@}, @expr{->feed(s,0);@}, the string is fed, but the parser 
 *! isn't run. 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 *! 
 *! @seealso 
 *!   @[finish], @[read], @[feed_insert] 
 */ 
 
static void html_feed(INT32 args) 
{ 
   DEBUG((stderr,"feed %"PRINTPTRDIFFT"d chars\n", 
          (args && TYPEOF(sp[-args]) == T_STRING)? 
          sp[-args].u.string->len:-1)); 
 
   if (args) 
   { 
      if (TYPEOF(sp[-args]) == T_STRING) 
         low_feed(sp[-args].u.string); 
      else if (TYPEOF(sp[-args]) != T_INT || sp[-args].u.integer) 
         SIMPLE_BAD_ARG_ERROR("feed",1,"string"); 
   } 
 
   if (args<2 || TYPEOF(sp[1-args]) != T_INT || sp[1-args].u.integer) 
   { 
      pop_n_elems(args); 
      try_feed(0); 
   } 
   else 
      pop_n_elems(args); 
 
   ref_push_object(THISOBJ); 
} 
 
/*! @decl Parser.HTML feed_insert(string s) 
 *! This pushes a string on the parser stack. 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 *! 
 *! @note 
 *!   Don't use! 
 */ 
 
static void html_feed_insert(INT32 args) 
{ 
   if (!args) 
      SIMPLE_TOO_FEW_ARGS_ERROR("feed_insert",1); 
 
   if (TYPEOF(sp[-args]) != T_STRING) 
      SIMPLE_BAD_ARG_ERROR("feed_insert",1,"string"); 
 
   DEBUG((stderr,"html_feed_insert: " 
          "pushing string (len=%"PRINTPTRDIFFT"d) on feedstack\n", 
          sp[-args].u.string->len)); 
 
   add_local_feed (THIS, sp[-args].u.string); 
 
   if (args<2 || TYPEOF(sp[1-args]) != T_INT || sp[1-args].u.integer) 
   { 
      pop_n_elems(args); 
      try_feed(0); 
   } 
   else 
      pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
/*! @decl Parser.HTML finish() 
 *! @decl Parser.HTML finish(string s) 
 *! Finish a parser pass. A string may be sent here, similar to 
 *! feed(). 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 */ 
 
static void html_finish(INT32 args) 
{ 
   if (args) 
   { 
      if (TYPEOF(sp[-args]) == T_STRING) 
         low_feed(sp[-args].u.string); 
      else if (TYPEOF(sp[-args]) != T_INT || sp[-args].u.integer) 
         SIMPLE_BAD_ARG_ERROR("finish",1,"string"); 
   } 
   try_feed(1); 
   ref_push_object(THISOBJ); 
} 
 
/*! @decl string|array(mixed) read() 
 *! @decl string|array(mixed) read(int max_elems) 
 *! Read parsed data from the parser object. 
 *! 
 *! @returns 
 *!   Returns a string of parsed data if the parser isn't in 
 *!   @[mixed_mode], an array of arbitrary data otherwise. 
 */ 
 
static void html_read(INT32 args) 
{ 
   ptrdiff_t n = THIS->out_length; 
 
   if (args) { 
      if (TYPEOF(sp[-args]) == T_INT && sp[-args].u.integer >= 0) 
        n = MINIMUM (sp[-args].u.integer, n); 
      else 
        SIMPLE_BAD_ARG_ERROR ("read", 1, "nonnegative integer"); 
   } 
 
   pop_n_elems(args); 
 
   if (THIS->out_max_shift < 0) 
   { 
      /* collect up to n items */ 
      struct array *res; 
      ptrdiff_t i; 
      int type_field = 0; 
      res = allocate_array (n); 
      for (i = 0; i < n; i++) 
      { 
         struct out_piece *z = THIS->out; 
         type_field |= 1 << TYPEOF(z->v); 
         move_svalue (&ITEM(res)[i], &z->v); 
         mark_free_svalue (&z->v); 
         THIS->out = z->next; 
         really_free_out_piece (z); 
      } 
      res->type_field = type_field; 
      push_array (res); 
      THIS->out_length -= n; 
#ifdef PIKE_DEBUG 
      if (!THIS->out_length != !THIS->out) 
        Pike_fatal ("Inconsistency in output queue length.\n"); 
#endif 
   } 
   else 
   { 
     /* collect up to n characters */ 
     if (!THIS->out || THIS->out->v.u.string->len < n) { 
       struct string_builder buf; 
       ptrdiff_t l = 0; 
       init_string_builder_alloc (&buf, n, THIS->out_max_shift); 
       while (l < n) { 
         struct out_piece *z = THIS->out; 
#ifdef PIKE_DEBUG 
       if (TYPEOF(z->v) != T_STRING) 
            Pike_fatal ("Got nonstring in parsed data\n"); 
#endif 
       if (z->v.u.string->len>n) 
         { 
            struct pike_string *ps; 
            string_builder_append (&buf, MKPCHARP_STR (z->v.u.string), n); 
            ps=string_slice(z->v.u.string,n,z->v.u.string->len-n); 
            free_string(z->v.u.string); 
            z->v.u.string=ps; 
            break; 
         } 
         l+=z->v.u.string->len; 
         string_builder_shared_strcat (&buf, z->v.u.string); 
         THIS->out=z->next; 
         really_free_out_piece (z); 
       } 
       push_string (finish_string_builder (&buf)); 
     } 
     else 
       /* Optimize the cases when there are no output pieces to concatenate. */ 
       if (THIS->out->v.u.string->len == n) { 
         struct out_piece *z = THIS->out; 
         push_string (z->v.u.string); 
         mark_free_svalue (&z->v); 
         THIS->out = z->next; 
         really_free_out_piece (z); 
       } 
       else {                       /* THIS->out->v.u.string->len > n */ 
         struct pike_string *ps; 
         push_string(string_slice(THIS->out->v.u.string,0,n)); 
         ps=string_slice(THIS->out->v.u.string,n,THIS->out->v.u.string->len-n); 
         free_string(THIS->out->v.u.string); 
         THIS->out->v.u.string=ps; 
       } 
     THIS->out_length -= n; 
     if (!THIS->out_length) THIS->out_max_shift = 0; 
#ifdef PIKE_DEBUG 
     if (!THIS->out_length) { 
       struct out_piece *z; 
       for (z = THIS->out; z; z = z->next) 
         if (TYPEOF(z->v) != T_STRING || z->v.u.string->len) 
           Pike_fatal ("Inconsistency in output queue.\n"); 
     } 
     else if (!THIS->out) 
       Pike_fatal ("Inconsistency in output queue length.\n"); 
#endif 
   } 
} 
 
/*! @decl Parser.HTML write_out(mixed ... args) 
 *! Send data to the output stream, i.e. it won't be parsed and it 
 *! won't be sent to the data callback, if any. 
 *! 
 *! Any data is allowed when the parser is running in @[mixed_mode]. 
 *! Only strings are allowed otherwise. 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 */ 
 
static void html_write_out(INT32 args) 
{ 
   int i; 
   for (i = args; i; i--) 
   { 
      if (THIS->out_max_shift >= 0 && TYPEOF(sp[-i]) != T_STRING) 
         Pike_error("write_out: not a string argument\n"); 
      put_out_feed(THIS,sp-i); 
   } 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
/** query *******************************************/ 
 
/*! @decl array(int) at() 
 *! @decl int at_line() 
 *! @decl int at_char() 
 *! @decl int at_column() 
 *! 
 *! Returns the current position. Characters and columns count from 
 *! @expr{0@}, lines count from @expr{1@}. 
 *! 
 *! @[at()] gives an array with the following layout. 
 *! @array 
 *!   @elem int 0 
 *!     Line. 
 *!   @elem int 1 
 *!     Character. 
 *!   @elem int 2 
 *!     Column. 
 *! @endarray 
 */ 
 
static void html_at_line(INT32 args) 
{ 
   pop_n_elems(args); 
   push_int(THIS->top.pos.lineno); 
} 
 
static void html_at_char(INT32 args) 
{ 
   pop_n_elems(args); 
   push_int(THIS->top.pos.byteno); 
} 
 
static void html_at_column(INT32 args) 
{ 
   pop_n_elems(args); 
   push_int(THIS->top.pos.byteno-THIS->top.pos.linestart); 
} 
 
static void html_at(INT32 args) 
{ 
   pop_n_elems(args); 
   push_int(THIS->top.pos.lineno); 
   push_int(THIS->top.pos.byteno); 
   push_int(THIS->top.pos.byteno-THIS->top.pos.linestart); 
   f_aggregate(3); 
} 
 
/*! @decl string current() 
 *! 
 *! Gives the current range of data, ie the whole tag/entity/etc being 
 *! parsed in the current callback. Returns zero if there's no current 
 *! range, i.e. when the function is not called in a callback. 
 */ 
 
static void html_current(INT32 args) 
{ 
   pop_n_elems(args); 
   if (THIS->start) 
   { 
      push_feed_range(THIS->start,THIS->cstart,THIS->end,THIS->cend); 
   } 
   else 
      push_int(0); 
} 
 
/*! @decl string tag_name() 
 *! 
 *! Gives the name of the current tag, or zero. If used from an entity 
 *! callback, it gives the string inside the entity. 
 *! 
 */ 
 
/*! @decl mapping(string:mixed) tag_args(void|mixed default_value) 
 *! 
 *! Gives the arguments of the current tag, parsed to a convenient 
 *! mapping consisting of key:value pairs. If the current thing isn't 
 *! a tag, it gives zero. @[default_value] is used for arguments which 
 *! have no value in the tag. If @[default_value] isn't given, the 
 *! value is set to the same string as the key. 
 */ 
 
/*! @decl string tag_content() 
 *! 
 *! Gives the content of the current tag, if it's a container or quote 
 *! tag. Otherwise returns zero. 
 */ 
 
/*! @decl array tag(void|mixed default_value) 
 *! 
 *! Returns the equivalent of the following calls. 
 *! @array 
 *!   @elem string 0 
 *!     @expr{tag_name()@} 
 *!   @elem mapping(string:mixed) 1 
 *!     @expr{tag_args(default_value)@} 
 *!   @elem string 2 
 *!     @expr{tag_content()@} 
 *! @endarray 
 */ 
 
static void tag_name(struct parser_html_storage *this,struct piece *feed, 
                     ptrdiff_t c, int skip_tag_start) 
{ 
   struct piece *s1=NULL,*s2=NULL; 
   ptrdiff_t c1=0,c2=0; 
   int pushed = 0; 
 
   if (skip_tag_start) { 
     p_wchar2 ch=index_shared_string(feed->s,c); 
     if (c < feed->s->len && ch == TAG_START (this)) 
       FORWARD_CHAR(feed,c,feed,c); 
   } 
 
   if (c < feed->s->len && 
       index_shared_string (feed->s, c) == TAG_FIN (this)) { 
     c++; 
     push_string (TAG_FIN_STRING (this)); 
     pushed = 1; 
   } 
 
   /* scan ws to start of tag name */ 
   if (this->flags & FLAG_WS_BEFORE_TAG_NAME) 
     scan_forward(feed,c,&s1,&c1, WS(this), 
                  -(ptrdiff_t)N_WS(this)); 
   else 
     s1 = feed, c1 = c; 
 
   /* scan as argument and push*/ 
   scan_forward_arg(this,s1,c1,&s2,&c2,SCAN_ARG_PUSH,1,1,NULL); 
   if (pushed) f_add (2); 
} 
 
static INLINE void tag_push_default_arg(struct svalue *def) 
{ 
   if (def) push_svalue(def); 
   else stack_dup(); 
} 
 
static void tag_args(struct parser_html_storage *this,struct piece *feed,ptrdiff_t c, 
                     struct svalue *def, int skip_name, int to_tag_end) 
{ 
   struct piece *s1=NULL,*s2=NULL,*s3; 
   int flags = this->flags; 
   ptrdiff_t c1=0,c2=0,c3; 
   int n=0; 
#ifdef PIKE_DEBUG 
   struct piece *prev_s = NULL; 
   ptrdiff_t prev_c = 0; 
#endif 
 
   /* NOTE: This somewhat duplicates the CTX_TAG clause in 
    * do_try_feed(). */ 
 
   p_wchar2 ch=index_shared_string(feed->s,c); 
   if (ch == TAG_START (this)) c++; 
 
   if (skip_name) { 
     /* scan past tag name */ 
     if (flags & FLAG_WS_BEFORE_TAG_NAME) 
       scan_forward(feed,c,&s1,&c1, WS(this), 
                    -(ptrdiff_t)N_WS(this)); 
     else 
       s1 = feed, c1 = c; 
     scan_forward_arg(this,s1,c1,&s2,&c2,SCAN_ARG_ONLY,1,1,NULL); 
   } 
   else s2 = feed, c2 = c; 
 
   for (;;) 
   { 
      /* skip whitespace */ 
      scan_forward(s2,c2,&s1,&c1,WS(this), 
                   -(ptrdiff_t)N_WS(this)); 
 
new_arg: 
#ifdef PIKE_DEBUG 
      if (prev_s && cmp_feed_pos (prev_s, prev_c, s1, c1) >= 0) 
        Pike_fatal ("Not going forward in tag args loop (from %p:%ld to %p:%ld).\n", 
               (void *)prev_s, PTRDIFF_T_TO_LONG(prev_c), 
               (void *)s1, PTRDIFF_T_TO_LONG(c1)); 
      prev_s = s1, prev_c = c1; 
#endif 
 
      DEBUG_MARK_SPOT("html_tag_args arg start",s1,c1); 
 
      /* end of tag? */ 
      if (c1==s1->s->len) { /* end<tm> */ 
        DEBUG_MARK_SPOT("html_tag_args hard tag end (1)",s1,c1); 
        break; 
      } 
      ch=index_shared_string(s1->s,c1); 
      if ((flags & (FLAG_STRICT_TAGS|FLAG_XML_TAGS)) != FLAG_STRICT_TAGS && 
          ch == TAG_FIN (this)) { 
        FORWARD_CHAR(s1, c1, s3, c3); 
        if ((c3==s3->s->len || index_shared_string(s3->s,c3) == TAG_END (this)) && 
            to_tag_end) { 
          DEBUG_MARK_SPOT("html_tag_args empty element tag end",s3,c3); 
          break; 
        } 
        else if (n && s1==s2 && c1==c2) { /* previous arg value didn't really end */ 
          DEBUG_MARK_SPOT("html_tag_args arg val continues",s3,c3); 
          push_string (TAG_FIN_STRING (this)); 
          scan_forward_arg (this,s3,c3,&s2,&c2,SCAN_ARG_PUSH,0,1,NULL); 
          f_add (3); 
          continue; 
        } 
      } 
      else if (ch == TAG_END (this) && to_tag_end) { /* end */ 
        DEBUG_MARK_SPOT("html_tag_args soft tag end (1)",s1,c1); 
        break; 
      } 
 
      /* scan this argument name and push*/ 
      scan_forward_arg(this,s1,c1,&s2,&c2,SCAN_ARG_PUSH,1,1,NULL); 
      if (flags & FLAG_CASE_INSENSITIVE_TAG) 
        f_lower_case(1); 
      n++; 
 
      if (!(n & 127)) 
        custom_check_stack(256, "Out of stack after parsing %d tag arguments.\n", n); 
 
      do { 
        /* scan for '=', '>' or next argument */ 
        /* skip whitespace */ 
        scan_forward(s2,c2,&s3,&c3,WS(this), 
                     -(ptrdiff_t)N_WS(this)); 
        if (c3==s3->s->len) /* end<tm> */ 
        { 
          DEBUG_MARK_SPOT("html_tag_args hard tag end (2)",s3,c3); 
          tag_push_default_arg(def); 
          goto done; 
        } 
 
        ch=index_shared_string(s3->s,c3); 
 
        if (ch == TAG_FIN (this) && s2==s3 && c2==c3) { 
          struct piece *s4; 
          ptrdiff_t c4; 
          /* a '/' that might have been part of the argument name */ 
          FORWARD_CHAR (s3, c3, s4, c4); 
          ch = index_shared_string (s4->s,c4); 
          if (ch == TAG_END (this) && to_tag_end) break; 
          DEBUG_MARK_SPOT("html_tag_args arg name continues",s4,c4); 
          push_string (TAG_FIN_STRING (this)); 
          scan_forward_arg (this,s4,c4,&s2,&c2,SCAN_ARG_PUSH,1,1,NULL); 
          if (flags & FLAG_CASE_INSENSITIVE_TAG) 
            f_lower_case(1); 
          f_add (3); 
        } 
        else break; 
      } while (1); 
 
      if (ch == TAG_END (this) && to_tag_end) /* end */ 
      { 
         DEBUG_MARK_SPOT("html_tag_args soft tag end (2)",s3,c3); 
         tag_push_default_arg(def); 
         break; 
      } 
 
      if (ch != ARG_EQ (this)) { 
        if (c1!=c3 || s1!=s3)       /* end of _this_ argument */ 
        { 
          DEBUG_MARK_SPOT("html_tag_args empty arg end",s3,c3); 
          s1 = s3, c1 = c3; 
          tag_push_default_arg(def); 
          goto new_arg; 
        } 
        else {                      /* a stray bogus character */ 
          DEBUG_MARK_SPOT("html_tag_args bogus char",s3,c3); 
          s2 = s3, c2 = c3 + 1; 
          pop_stack(); 
          n--; 
          continue; 
        } 
      } 
 
      /* left: case of '=' */ 
      c3++; /* skip it */ 
 
      /* skip whitespace */ 
      scan_forward(s3,c3,&s2,&c2,WS(this), 
                   -(ptrdiff_t)N_WS(this)); 
 
      DEBUG_MARK_SPOT("html_tag_args value start",s2,c2); 
 
      /* scan the argument value */ 
      scan_forward_arg(this,s2,c2,&s1,&c1,SCAN_ARG_PUSH,0,1,NULL); 
 
      DEBUG_MARK_SPOT("html_tag_args value end",s1,c1); 
 
      /* next argument in the loop */ 
      s2 = s1; 
      c2 = c1; 
   } 
 
done: 
   f_aggregate_mapping(n*2); 
} 
 
static void html_tag_name(INT32 args) 
{ 
   /* get rid of arguments */ 
   pop_n_elems(args); 
 
   if (!THIS->start) Pike_error ("Parser.HTML: There's no current range.\n"); 
   switch (THIS->type) { 
     case TYPE_TAG: 
     case TYPE_CONT: 
       tag_name(THIS,THIS->start,THIS->cstart,1); 
       break; 
     case TYPE_ENTITY: 
       if (THIS->cend == 0) { 
         push_feed_range(THIS->start,THIS->cstart+1,THIS->end,THIS->cend); 
         if (sp[-1].u.string->len && 
             index_shared_string(sp[-1].u.string,sp[-1].u.string->len-1) == 
             ENTITY_END (THIS)) { 
           struct pike_string *s=string_slice(sp[-1].u.string,0,sp[-1].u.string->len-1); 
           pop_stack(); 
           push_string(s); 
         } 
       } 
       else { 
         ptrdiff_t end = THIS->cend; 
         if (index_shared_string(THIS->end->s,end-1) == ENTITY_END (THIS)) end--; 
         push_feed_range(THIS->start,THIS->cstart+1,THIS->end,end); 
       } 
       break; 
     case TYPE_QTAG: { 
       struct svalue *v; 
       struct piece *beg; 
       ptrdiff_t cbeg; 
       if (THIS->flags & FLAG_WS_BEFORE_TAG_NAME) 
         scan_forward (THIS->start, THIS->cstart+1, &beg, &cbeg, 
                       WS(THIS), -(ptrdiff_t)N_WS(THIS)); 
       else 
         beg = THIS->start, cbeg = THIS->cstart + 1; 
       quote_tag_lookup (THIS, beg, cbeg, &beg, &cbeg, 1, &v); 
       if (!v) push_int (0); 
       else push_svalue (v); 
       break; 
     } 
     default: 
       push_int(0); 
   } 
} 
 
static void html_tag_args(INT32 args) 
{ 
   struct svalue def; 
   check_all_args("tag_args",args,BIT_MIXED|BIT_VOID,0); 
   if (args) assign_svalue_no_free(&def,sp-args); 
   pop_n_elems(args); 
 
   if (!THIS->start) Pike_error ("Parser.HTML: There's no current range.\n"); 
 
   switch (THIS->type) { 
     case TYPE_TAG: 
     case TYPE_CONT: 
       if (args) 
       { 
         tag_args(THIS,THIS->start,THIS->cstart,&def,1,1); 
         free_svalue(&def); 
       } 
       else tag_args(THIS,THIS->start,THIS->cstart,NULL,1,1); 
       break; 
     default: 
       push_int(0); 
   } 
} 
 
static void html_tag_content(INT32 args) 
{ 
  struct piece *beg = THIS->start; 
  ptrdiff_t cbeg = THIS->cstart+1; 
 
  pop_n_elems(args); 
 
  if (!THIS->start) Pike_error ("Parser.HTML: There's no current range.\n"); 
 
  if (THIS->flags & FLAG_WS_BEFORE_TAG_NAME && 
      !scan_forward (beg, cbeg, &beg, &cbeg, WS(THIS), 
                     -(ptrdiff_t)N_WS(THIS))) { 
    push_int(0); 
    return; 
  } 
 
  switch (THIS->type) { 
    case TYPE_CONT: { 
      struct piece *end, *dummy; 
      ptrdiff_t cend, cdummy; 
      if (scan_forward_arg (THIS, beg, cbeg, &beg, &cbeg, SCAN_ARG_PUSH, 1, 1, NULL)) { 
        if (scan_for_end_of_tag (THIS, beg, cbeg, &beg, &cbeg, 1, 
                                 THIS->flags & FLAG_MATCH_TAG, NULL) && 
            !find_end_of_container (THIS, sp-1, beg, cbeg+1, 
                                    &end, &cend, &dummy, &cdummy, 1)) { 
          pop_stack(); 
          if (cmp_feed_pos (end, cend, THIS->end, THIS->cend) < 0) 
            push_feed_range (beg, cbeg+1, end, cend); 
          else 
            push_int(0); 
        } 
        else { 
          pop_stack(); 
          push_int(0); 
        } 
      } 
      else push_int(0); 
      break; 
    } 
    case TYPE_QTAG: { 
      struct svalue *v; 
      struct piece *end; 
      ptrdiff_t cend; 
      if (quote_tag_lookup (THIS, beg, cbeg, &beg, &cbeg, 1, &v) && 
          scan_for_string (THIS, beg, cbeg, &end, &cend, v[2].u.string)) 
        push_feed_range (beg, cbeg, end, cend); 
      else 
        push_int(0); 
      break; 
    } 
    default: 
      push_int(0); 
  } 
} 
 
static void html_tag(INT32 args) 
{ 
   check_all_args("tag",args,BIT_MIXED|BIT_VOID,0); 
 
   html_tag_args(args); 
   html_tag_name(0); 
   stack_swap(); 
   html_tag_content(0); 
   f_aggregate(3); 
} 
 
/*! @decl string context() 
 *! Returns the current output context as a string. 
 *! 
 *! @string 
 *!   @value "data" 
 *!     In top level data. This is always returned when called from 
 *!     tag or container callbacks. 
 *!   @value "arg" 
 *!     In an unquoted argument. 
 *!   @value "splice_arg" 
 *!     In a splice argument. 
 *! @endstring 
 *! 
 *! The return value can also be a single character string, in which 
 *! case the context is a quoted argument. The string contains the 
 *! starting quote character. 
 *! 
 *! This function is typically only useful in entity callbacks, which 
 *! can be called both from text and argument values of different 
 *! sorts. 
 *! 
 *! @seealso 
 *!   @[splice_arg] 
 */ 
 
static void html_context(INT32 args) 
{ 
  pop_n_elems (args); 
  switch (THIS->out_ctx) { 
    case CTX_DATA: push_constant_text ("data"); break; 
    case CTX_TAG: push_constant_text ("tag"); break; /* should never happen */ 
    case CTX_SPLICE_ARG: push_constant_text ("splice_arg"); break; 
    case CTX_TAG_ARG: push_constant_text ("arg"); break; 
    default: 
      push_string (make_shared_binary_string2 ( 
                     ARGQ_START (THIS) + (THIS->out_ctx - CTX_TAG_QUOTED_ARG), 1)); 
  } 
} 
 
/*! @decl string parse_tag_name(string tag) 
 *! 
 *! Parses the tag name from a tag string without the surrounding 
 *! brackets, i.e. a string on the form @expr{"tagname some='tag' 
 *! args"@}. 
 *! @returns 
 *!   Returns the tag name or an empty string if none. 
 */ 
 
static void html_parse_tag_name(INT32 args) 
{ 
   struct piece feed; 
   check_all_args("parse_tag_name",args,BIT_STRING,0); 
   feed.s=sp[-args].u.string; 
   feed.next=NULL; 
   tag_name(THIS,&feed,0,0); 
   stack_pop_n_elems_keep_top(args); 
} 
 
/*! @decl mapping parse_tag_args(string tag) 
 *! 
 *! Parses the tag arguments from a tag string without the name and 
 *! surrounding brackets, i.e. a string on the form @expr{"some='tag' 
 *! args"@}. 
 *! 
 *! @returns 
 *!   Returns a mapping containing the tag arguments. 
 *! 
 *! @seealso 
 *!   @[tag_args] 
 */ 
 
static void html_parse_tag_args(INT32 args) 
{ 
   struct piece feed; 
   check_all_args("parse_tag_args",args,BIT_STRING,0); 
   feed.s=sp[-args].u.string; 
   feed.next=NULL; 
   tag_args(THIS,&feed,0,NULL,0,0); 
   stack_pop_n_elems_keep_top(args); 
} 
 
/** debug *******************************************/ 
 
/*! @decl mapping _inspect() 
 *! 
 *! This is a low-level way of debugging a parser. This gives a 
 *! mapping of the internal state of the Parser.HTML object. 
 *! 
 *! The format and contents of this mapping may change without further 
 *! notice. 
 */ 
 
static void html__inspect(INT32 args) 
{ 
   int n=0,m,o,p; 
   struct piece *f; 
   struct out_piece *of; 
   struct feed_stack *st; 
 
   pop_n_elems(args); 
 
   push_static_text("feed"); 
   m=0; 
 
   st=THIS->stack; 
   while (st) 
   { 
      o=0; 
      p=0; 
 
      push_static_text("feed"); 
 
      f=st->local_feed; 
 
      while (f) 
      { 
         ref_push_string(f->s); 
         p++; 
         f=f->next; 
      } 
 
      f_aggregate(p); 
      o++; 
 
      push_static_text("position"); 
      push_int(DO_NOT_WARN(st->c)); 
      o++; 
 
      push_static_text("byteno"); 
      push_int(st->pos.byteno); 
      o++; 
 
      push_static_text("lineno"); 
      push_int(st->pos.lineno); 
      o++; 
 
      push_static_text("linestart"); 
      push_int(st->pos.linestart); 
      o++; 
 
      f_aggregate_mapping(o*2); 
      st=st->prev; 
 
      m++; 
   } 
 
   f_aggregate(m); 
   n++; 
 
   push_static_text("data_cb_feed"); 
   for (f = THIS->data_cb_feed, p = 0; f; f = f->next) { 
     ref_push_string (f->s); 
     p++; 
   } 
   f_aggregate(p); 
   n++; 
 
   push_static_text("outfeed"); 
   p=0; 
   of=THIS->out; 
   while (of) 
   { 
      push_svalue(&of->v); 
      p++; 
      of=of->next; 
   } 
   f_aggregate(p); 
   n++; 
 
   push_static_text("tags"); 
   ref_push_mapping(THIS->maptag); 
   n++; 
 
   push_static_text("containers"); 
   ref_push_mapping(THIS->mapcont); 
   n++; 
 
   push_static_text("entities"); 
   ref_push_mapping(THIS->mapentity); 
   n++; 
 
   push_static_text("quote_tags"); 
   ref_push_mapping(THIS->mapqtag); 
   n++; 
 
   push_static_text("splice_arg"); 
   if (THIS->splice_arg) 
     ref_push_string(THIS->splice_arg); 
   else 
     push_int(0); 
   n++; 
 
   push_static_text("callback__tag"); 
   push_svalue(&(THIS->callback__tag)); 
   n++; 
 
   push_static_text("callback__entity"); 
   push_svalue(&(THIS->callback__entity)); 
   n++; 
 
   push_static_text("callback__data"); 
   push_svalue(&(THIS->callback__data)); 
   n++; 
 
   push_static_text("flags"); 
   push_int(THIS->flags); 
   n++; 
 
   f_aggregate_mapping(n*2); 
} 
 
/** create, clone ***********************************/ 
 
static void html_create(INT32 args) 
{ 
   pop_n_elems(args); 
} 
 
/*! @decl Parser.HTML clone(mixed ... args) 
 *! 
 *! Clones the @[Parser.HTML] object. A new object of the same class 
 *! is created, filled with the parse setup from the old object. 
 *! 
 *! This is the simpliest way of flushing a parse feed/output. 
 *! 
 *! The arguments to clone is sent to the new object, simplifying work 
 *! for custom classes that inherits @[Parser.HTML]. 
 *! 
 *! @returns 
 *!   Returns the new object. 
 *! 
 *! @note 
 *!   create is called _before_ the setup is copied. 
 */ 
 
static void html_clone(INT32 args) 
{ 
   struct object *o; 
   struct parser_html_storage *p; 
 
   DEBUG((stderr,"parse_html_clone object %p\n",THISOBJ)); 
 
   push_object(o=clone_object_from_object(THISOBJ,args)); 
 
   p=get_storage(o,parser_html_program); 
 
   if (p->maptag) free_mapping(p->maptag); 
   add_ref(p->maptag=THIS->maptag); 
   if (p->mapcont) free_mapping(p->mapcont); 
   add_ref(p->mapcont=THIS->mapcont); 
   if (p->mapentity) free_mapping(p->mapentity); 
   add_ref(p->mapentity=THIS->mapentity); 
   if (p->mapqtag) free_mapping(p->mapqtag); 
   add_ref(p->mapqtag=THIS->mapqtag); 
 
   if (p->splice_arg) free_string(p->splice_arg); 
   if (THIS->splice_arg) 
     add_ref(p->splice_arg=THIS->splice_arg); 
   else 
     p->splice_arg=NULL; 
 
   dmalloc_touch_svalue(&p->callback__tag); 
   dmalloc_touch_svalue(&p->callback__data); 
   dmalloc_touch_svalue(&p->callback__entity); 
   dmalloc_touch_svalue(&THIS->callback__tag); 
   dmalloc_touch_svalue(&THIS->callback__data); 
   dmalloc_touch_svalue(&THIS->callback__entity); 
   assign_svalue(&p->callback__tag,&THIS->callback__tag); 
   assign_svalue(&p->callback__data,&THIS->callback__data); 
   assign_svalue(&p->callback__entity,&THIS->callback__entity); 
 
   if (p->extra_args) free_array(p->extra_args); 
   if (THIS->extra_args) 
      add_ref(p->extra_args=THIS->extra_args); 
   else 
      p->extra_args=NULL; 
 
   p->flags=THIS->flags; 
   if(THIS->out_max_shift==-1) p->out_max_shift=THIS->out_max_shift; 
   p->max_stack_depth=THIS->max_stack_depth; 
   p->top.parse_tags = THIS->flags & FLAG_PARSE_TAGS; 
 
#ifdef CONFIGURABLE_MARKUP 
   p->tag_start=THIS->tag_start; 
   p->tag_end=THIS->tag_end; 
   p->entity_start=THIS->entity_start; 
   p->entity_end=THIS->entity_end; 
   p->arg_eq=THIS->arg_eq; 
 
   p->nargq=THIS->nargq; 
   for (i=0; i<p->nargq; i++) 
   { 
      p->argq_start[i]=THIS->argq_start[i]; 
      p->argq_stop[i]=THIS->argq_stop[i]; 
   } 
 
   p->n_lazy_entity_ends=THIS->n_lazy_entity_ends; 
   newstr=(p_wchar2*)xalloc(sizeof(p_wchar2)*p->n_lazy_entity_ends); 
   memcpy(newstr,THIS->lazy_entity_ends,sizeof(p_wchar2)*p->n_lazy_entity_ends); 
   if (p->lazy_entity_ends) free(p->lazy_entity_ends); 
   p->lazy_entity_ends=newstr; 
 
   p->n_ws=THIS->n_ws; 
   newstr=(p_wchar2*)xalloc(sizeof(p_wchar2)*p->n_ws); 
   memcpy(newstr,THIS->ws,sizeof(p_wchar2)*p->n_ws); 
   if (p->ws) free(p->ws); 
   p->ws=newstr; 
#endif 
 
   DEBUG((stderr,"done clone\n")); 
 
   /* all copied, object on stack */ 
} 
 
/****** setup ***************************************/ 
 
/*! @decl Parser.HTML set_extra(mixed ...args) 
 *! 
 *! Sets the extra arguments passed to all tag, container and entity 
 *! callbacks. 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 */ 
 
static void html_set_extra(INT32 args) 
{ 
   if (THIS->extra_args) { 
     free_array(THIS->extra_args); 
     THIS->extra_args=NULL; 
   } 
   if (args) { 
     f_aggregate(args); 
     THIS->extra_args=sp[-1].u.array; 
     sp--; 
   } 
   dmalloc_touch_svalue(sp); 
   ref_push_object(THISOBJ); 
} 
 
/*! @decl array get_extra() 
 *! Gets the extra arguments set by @[set_extra()]. 
 *! 
 *! @returns 
 *!   Returns the object being called. 
 */ 
 
static void html_get_extra(INT32 args) 
{ 
  pop_n_elems(args); 
  if (THIS->extra_args) 
    ref_push_array (THIS->extra_args); 
  else 
    ref_push_array (&empty_array); 
} 
 
/*! @decl string splice_arg(void|string name) 
 *! 
 *! If given a string, it sets the splice argument name to it. It 
 *! returns the old splice argument name. 
 *! 
 *! If a splice argument name is set, it's parsed in all tags, both 
 *! those with callbacks and those without. Wherever it occurs, its 
 *! value (after being parsed for entities in the normal way) is 
 *! inserted directly into the tag. E.g: 
 *! 
 *! @pre{<foo arg1="val 1" splice="arg2='val 2' arg3" arg4>@} 
 *! 
 *! becomes 
 *! 
 *! @pre{<foo arg1="val 1" arg2='val 2' arg3 arg4>@} 
 *! 
 *! if @expr{"splice"@} is set as the splice argument name. 
 */ 
 
static void html_splice_arg (INT32 args) 
{ 
  struct pike_string *old = THIS->splice_arg; 
   check_all_args("splice_arg",args,BIT_VOID|BIT_STRING|BIT_INT,0); 
   if (args) { 
     if (TYPEOF(sp[-args]) == T_STRING) 
       add_ref (THIS->splice_arg = sp[-args].u.string); 
     else if (sp[-args].u.integer) 
       SIMPLE_BAD_ARG_ERROR ("splice_arg", 1, "string or zero"); 
     else 
       THIS->splice_arg = NULL; 
   } 
   pop_n_elems(args); 
   if (old) push_string (old); 
   else push_int (0); 
} 
 
/*! @decl int case_insensitive_tag(void|int value) 
 *! 
 *! All tags and containers are matched case insensitively, and 
 *! argument names are converted to lowercase. Tags added with 
 *! @[add_quote_tag()] are not affected, though. Switching to case 
 *! insensitive mode and back won't preserve the case of registered 
 *! tags and containers. 
 */ 
 
/*! @decl int ignore_tags(void|int value) 
 *! 
 *! Do not look for tags at all. Normally tags are matched even when 
 *! there's no callbacks for them at all. When this is set, the tag 
 *! delimiters @expr{'<'@} and @expr{'>'@} will be treated as any 
 *! normal character. 
 */ 
 
/*! @decl int ignore_unknown(void|int value) 
 *! 
 *! Treat unknown tags and entities as text data, continuing parsing 
 *! for tags and entities inside them. 
 *! 
 *! @note 
 *!   When functions are specified with @[_set_tag_callback()] or 
 *!   @[_set_entity_callback()], all tags or entities, respectively, 
 *!   are considered known. However, if one of those functions return 
 *!   1 and ignore_unknown is set, they are treated as text data 
 *!   instead of making another call to the same function again. 
 */ 
 
/*! @decl int lazy_argument_end(void|int value) 
 *! 
 *! A @expr{'>'@} in a tag argument closes both the argument and the 
 *! tag, even if the argument is quoted. 
 */ 
 
/*! @decl int lazy_entity_end(void|int value) 
 *! 
 *! Normally, the parser search indefinitely for the entity end 
 *! character (i.e. @expr{';'@}). When this flag is set, the 
 *! characters @expr{'&'@}, @expr{'<'@}, @expr{'>'@}, @expr{'"'@}, 
 *! @expr{'''@}, and any whitespace breaks the search for the entity 
 *! end, and the entity text is then ignored, i.e. treated as 
 *! data. 
 */ 
 
/*! @decl int match_tag(void|int value) 
 *! 
 *! Unquoted nested tag starters and enders will be balanced when 
 *! parsing tags. This is the default. 
 */ 
 
/*! @decl int max_parse_depth(void|int value) 
 *! 
 *! Maximum recursion depth during parsing. Recursion occurs when a 
 *! tag/container/entity/quote tag callback function returns a string 
 *! to be reparsed. The default value is @expr{10@}. 
 */ 
 
/*! @decl int mixed_mode(void|int value) 
 *! 
 *! Allow callbacks to return arbitrary data in the arrays, which will 
 *! be concatenated in the output. 
 */ 
 
/*! @decl int reparse_strings(void|int value) 
 *! 
 *! When a plain string is used as a tag/container/entity/quote tag 
 *! callback, it's not reparsed if this flag is unset. Setting it 
 *! causes all such strings to be reparsed. 
 */ 
 
/*! @decl int ws_before_tag_name(void|int value) 
 *! 
 *! Allow whitespace between the tag start character and the tag 
 *! name. 
 */ 
 
/*! @decl int xml_tag_syntax(void|int value) 
 *! 
 *! Whether or not to use XML syntax to tell empty tags and container 
 *! tags apart. 
 *! 
 *! @int 
 *!   @value 0 
 *!     Use HTML syntax only. If there's a @expr{'/'@} last in a tag, 
 *!     it's just treated as any other argument. 
 *!   @value 1 
 *!     Use HTML syntax, but ignore a @expr{'/'@} if it comes last in 
 *!     a tag. This is the default. 
 *!   @value 2 
 *!     Use XML syntax, but when a tag that does not end with 
 *!     @expr{'/>'@} is found which only got a non-container tag 
 *!     callback, treat it as a non-container (i.e. don't start to 
 *!     seek for the container end). 
 *!   @value 3 
 *!     Use XML syntax only. If a tag got both container and 
 *!     non-container callbacks, the non-container callback is called 
 *!     when the empty element form (i.e. the one ending with 
 *!     @expr{'/>'@}) is used, and the container callback otherwise. 
 *!     If only a container callback exists, it gets the empty string 
 *!     as content when there's none to be parsed. If only a 
 *!     non-container callback exists, it will be called (without the 
 *!     content argument) for both kinds of tags. 
 *! @endint 
 */ 
 
static void html_ignore_tags(INT32 args) 
{ 
   int o=!(THIS->flags & FLAG_PARSE_TAGS); 
   check_all_args("ignore_tags",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags &= ~FLAG_PARSE_TAGS; 
     else THIS->flags |= FLAG_PARSE_TAGS; 
     THIS->top.parse_tags = THIS->flags & FLAG_PARSE_TAGS; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_case_insensitive_tag(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_CASE_INSENSITIVE_TAG); 
   check_all_args("case_insensitive_tag",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_CASE_INSENSITIVE_TAG; 
     else THIS->flags &= ~FLAG_CASE_INSENSITIVE_TAG; 
   } 
   pop_n_elems(args); 
 
   if (args && (THIS->flags & FLAG_CASE_INSENSITIVE_TAG) && !o) { 
     INT32 e; 
     struct keypair *k; 
     struct mapping_data *md; 
 
     md = THIS->maptag->data; 
     NEW_MAPPING_LOOP(md) { 
       push_svalue(&k->ind); 
       f_lower_case(1); 
       push_svalue(&k->val); 
     } 
     f_aggregate_mapping(m_sizeof(THIS->maptag) * 2); 
     free_mapping(THIS->maptag); 
     THIS->maptag=(--sp)->u.mapping; 
 
     md = THIS->mapcont->data; 
     NEW_MAPPING_LOOP(md) { 
       push_svalue(&k->ind); 
       f_lower_case(1); 
       push_svalue(&k->val); 
     } 
     f_aggregate_mapping(m_sizeof(THIS->mapcont) * 2); 
     free_mapping(THIS->mapcont); 
     THIS->mapcont=(--sp)->u.mapping; 
   } 
 
   push_int(o); 
} 
 
static void html_lazy_argument_end(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_LAZY_END_ARG_QUOTE); 
   check_all_args("lazy_argument_end",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_LAZY_END_ARG_QUOTE; 
     else THIS->flags &= ~FLAG_LAZY_END_ARG_QUOTE; 
     recalculate_argq(THIS); 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_lazy_entity_end(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_LAZY_ENTITY_END); 
   check_all_args("lazy_entity_end",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_LAZY_ENTITY_END; 
     else THIS->flags &= ~FLAG_LAZY_ENTITY_END; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
/*! @decl int nestling_entity_end(void|int value) 
 *! 
 */ 
 
static void html_nestling_entity_end(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_NESTLING_ENTITY_END); 
   check_all_args("nestling_entity_end",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_NESTLING_ENTITY_END; 
     else THIS->flags &= ~FLAG_NESTLING_ENTITY_END; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_match_tag(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_MATCH_TAG); 
   check_all_args("match_tag",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_MATCH_TAG; 
     else THIS->flags &= ~FLAG_MATCH_TAG; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_mixed_mode(INT32 args) 
{ 
   int o=THIS->out_max_shift < 0; 
   check_all_args("mixed_mode",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) { 
       if (!o) { 
         struct out_piece *f; 
         size_t c; 
         THIS->out_max_shift = -1; 
         /* Got to count the entries in the output queue. */ 
         for (f = THIS->out, c = 0; f; f = f->next) c++; 
         THIS->out_length = c; 
       } 
     } 
     else 
       if (o) { 
         struct out_piece *f; 
         int max_shift = 0; 
         size_t length; 
         for (f = THIS->out, length = 0; f; f = f->next) { 
           if (TYPEOF(f->v) != T_STRING) 
             Pike_error ("Cannot switch from mixed mode " 
                         "with nonstrings in the output queue.\n"); 
           if (f->v.u.string->size_shift > max_shift) 
             max_shift = f->v.u.string->size_shift; 
           length += f->v.u.string->len; 
         } 
         THIS->out_max_shift = max_shift; 
         THIS->out_length = length; 
       } 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_reparse_strings(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_REPARSE_STRINGS); 
   check_all_args("reparse_strings",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_REPARSE_STRINGS; 
     else THIS->flags &= ~FLAG_REPARSE_STRINGS; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_ignore_unknown(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_IGNORE_UNKNOWN); 
   check_all_args("ignore_unknown",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_IGNORE_UNKNOWN; 
     else THIS->flags &= ~FLAG_IGNORE_UNKNOWN; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_xml_tag_syntax(INT32 args) 
{ 
   int o=THIS->flags; 
   check_all_args("xml_tag_syntax",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     THIS->flags &= ~(FLAG_XML_TAGS|FLAG_STRICT_TAGS); 
     switch (sp[-args].u.integer) { 
       case 0: THIS->flags |= FLAG_STRICT_TAGS; break; 
       case 1: break; 
       case 2: THIS->flags |= FLAG_XML_TAGS; break; 
       case 3: THIS->flags |= FLAG_XML_TAGS|FLAG_STRICT_TAGS; break; 
       default: 
         SIMPLE_BAD_ARG_ERROR ("xml_tag_syntax", 1, "integer 0..3"); 
     } 
     recalculate_argq(THIS); 
   } 
   pop_n_elems(args); 
   if (o & FLAG_XML_TAGS) 
     o = o & FLAG_STRICT_TAGS ? 3 : 2; 
   else 
     o = o & FLAG_STRICT_TAGS ? 0 : 1; 
   push_int(o); 
} 
 
static void html_ws_before_tag_name(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_WS_BEFORE_TAG_NAME); 
   check_all_args("ws_before_tag_name",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_WS_BEFORE_TAG_NAME; 
     else THIS->flags &= ~FLAG_WS_BEFORE_TAG_NAME; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
static void html_max_stack_depth(INT32 args) 
{ 
   int o=THIS->max_stack_depth; 
   check_all_args("max_stack_depth",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     THIS->max_stack_depth = sp[-args].u.integer; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
#ifdef HTML_DEBUG 
static void html_debug_mode(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_DEBUG_MODE); 
   check_all_args("debug_mode",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_DEBUG_MODE; 
     else THIS->flags &= ~FLAG_DEBUG_MODE; 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
#endif 
 
/*! @decl int ignore_comments(void|int value) 
 *! 
 */ 
 
static void html_ignore_comments(INT32 args) 
{ 
   int o=!!(THIS->flags & FLAG_IGNORE_COMMENTS); 
   check_all_args("debug_mode",args,BIT_VOID|BIT_INT,0); 
   if (args) { 
     if (sp[-args].u.integer) THIS->flags |= FLAG_IGNORE_COMMENTS; 
     else THIS->flags &= ~FLAG_IGNORE_COMMENTS; 
     recalculate_argq(THIS); 
   } 
   pop_n_elems(args); 
   push_int(o); 
} 
 
/*! @decl int quote_stapling(int|void enable) 
 *! 
 *! Enable old-style attribute quoting by stapling. 
 *! 
 *! @param enable 
 *!   Enable/disable the mode. Defaults to 
 *!   keeping the old setting. 
 *! 
 *! @returns 
 *!   Returns the prior setting. 
 *! 
 *! @note 
 *!   Any use of this mode is discouraged, and is 
 *!   only provided for compatibility with versions 
 *!   of Pike prior to 8.0. 
 *! 
 *! @note 
 *!   Note also that this mode will output runtime 
 *!   warnings whenever the mode has had an effect 
 *!   on the parsing. 
 */ 
static void html_quote_stapling(INT32 args) 
{ 
  int of = !!(THIS->flags & FLAG_QUOTE_STAPLING); 
  int f = of; 
  get_all_args("quote_stapling", args, ".%d", &f); 
  if (f) { 
    THIS->flags |= FLAG_QUOTE_STAPLING; 
  } else { 
    THIS->flags &= ~FLAG_QUOTE_STAPLING; 
  } 
  push_int(of); 
} 
 
/*! @endclass 
 */ 
 
/*! @endmodule 
 */ 
 
 
/****** module init *********************************/ 
 
#define tCbret tOr4(tZero,tInt1,tStr,tArr(tMixed)) 
#define tCbfunc(X) tOr(tFunc(tNone,tCbret),tFunc(tObjImpl_PARSER_HTML X,tCbret)) 
#define tTodo(X) tOr4(tZero,tStr,tCbfunc(X),tArray) 
#define tTagargs tMap(tStr,tStr) 
 
#define t_Todo tOr3(tStr,tArr(tMixed),tFuncV(tObjImpl_PARSER_HTML tStr,tMix,tCbret)) 
 
void init_parser_html(void) 
{ 
   size_t offset; 
 
   offset = ADD_STORAGE(struct parser_html_storage); 
 
   PIKE_MAP_VARIABLE(" maptag", offset + OFFSETOF(parser_html_storage, maptag), 
                tMap(tStr,tTodo(tTagargs)), 
                T_MAPPING, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" mapcont", offset + OFFSETOF(parser_html_storage, mapcont), 
                tMap(tStr,tTodo(tTagargs tStr)), 
                T_MAPPING, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" mapentity", offset + OFFSETOF(parser_html_storage, mapentity), 
                tMap(tStr,tTodo(tNone)), 
                T_MAPPING, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" mapqtag", offset + OFFSETOF(parser_html_storage, mapqtag), 
                tMap(tStr,tTodo(tStr)), 
                T_MAPPING, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" callback__tag", offset + OFFSETOF(parser_html_storage, callback__tag), 
                tFuncV(tObjImpl_PARSER_HTML tStr,tMix,tCbret), 
                T_MIXED, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" callback__data", offset + OFFSETOF(parser_html_storage, callback__data), 
                tFuncV(tObjImpl_PARSER_HTML tStr,tMix,tCbret), 
                T_MIXED, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" callback__entity", offset + OFFSETOF(parser_html_storage, callback__entity), 
                tFuncV(tObjImpl_PARSER_HTML tStr,tMix,tCbret), 
                T_MIXED, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" splice_arg", offset + OFFSETOF(parser_html_storage, splice_arg), 
                tString, 
                T_STRING, ID_PROTECTED|ID_PRIVATE); 
   PIKE_MAP_VARIABLE(" extra_args", offset + OFFSETOF(parser_html_storage, extra_args), 
                tArray, 
                T_ARRAY, ID_PROTECTED|ID_PRIVATE); 
 
   set_init_callback(init_html_struct); 
   set_exit_callback(exit_html_struct); 
 
   ADD_FUNCTION("create",html_create,tFunc(tNone,tVoid),ID_PROTECTED); 
   ADD_FUNCTION("clone",html_clone,tFuncV(tNone,tMixed,tObjImpl_PARSER_HTML),0); 
 
   /* feed control */ 
 
   ADD_FUNCTION("feed",html_feed, 
                tOr(tFunc(tNone,tObjImpl_PARSER_HTML), 
                    tFunc(tStr tOr(tVoid,tInt),tObjImpl_PARSER_HTML)),0); 
   ADD_FUNCTION("finish",html_finish, 
                tFunc(tOr(tVoid,tStr),tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("read",html_read, 
                tFunc(tOr(tVoid,tInt),tOr(tStr,tArr(tMixed))),0); 
 
   ADD_FUNCTION("write_out",html_write_out, 
                tFuncV(tNone,tOr(tStr,tMixed),tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("feed_insert",html_feed_insert, 
                tFunc(tStr,tObjImpl_PARSER_HTML),0); 
 
   /* query */ 
 
   ADD_FUNCTION("current",html_current,tFunc(tNone,tStr),0); 
 
   ADD_FUNCTION("at",html_at,tFunc(tNone,tArr(tInt)),0); 
   ADD_FUNCTION("at_line",html_at_line,tFunc(tNone,tInt),0); 
   ADD_FUNCTION("at_char",html_at_char,tFunc(tNone,tInt),0); 
   ADD_FUNCTION("at_column",html_at_column,tFunc(tNone,tInt),0); 
 
   ADD_FUNCTION("tag_name",html_tag_name,tFunc(tNone,tStr),0); 
   ADD_FUNCTION("tag_args",html_tag_args, 
                tFunc(tOr(tVoid,tSetvar(1,tMixed)), 
                      tMap(tStr,tOr(tStr,tVar(1)))),0); 
   ADD_FUNCTION("tag_content",html_tag_content,tFunc(tNone,tStr),0); 
   ADD_FUNCTION("tag",html_tag, 
                tFunc(tOr(tVoid,tSetvar(1,tMixed)), 
                      tArr(tOr(tStr,tMap(tStr,tOr(tStr,tVar(1)))))),0); 
   ADD_FUNCTION("context",html_context, 
                tFunc(tNone,tStr),0); 
 
   /* callback setup */ 
 
   ADD_FUNCTION("add_tag",html_add_tag, 
                tFunc(tStr tTodo(tTagargs),tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("add_container",html_add_container, 
                tFunc(tStr tTodo(tTagargs tStr),tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("add_entity",html_add_entity, 
                tFunc(tStr tTodo(""),tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("add_quote_tag",html_add_quote_tag, 
                tOr(tFunc(tStr tTodo(tStr) tStr,tObjImpl_PARSER_HTML), 
                    tFunc(tStr tZero,tObjImpl_PARSER_HTML)),0); 
 
   ADD_FUNCTION("add_tags",html_add_tags, 
                tFunc(tMap(tStr,tTodo( tTagargs )),tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("add_containers",html_add_containers, 
                tFunc(tMap(tStr,tTodo( tTagargs tStr )),tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("add_entities",html_add_entities, 
                tFunc(tMap(tStr,tTodo( "" )),tObjImpl_PARSER_HTML),0); 
 
   ADD_FUNCTION("clear_tags",html_clear_tags, 
                tFunc(tNone,tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("clear_containers",html_clear_containers, 
                tFunc(tNone,tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("clear_entities",html_clear_entities, 
                tFunc(tNone,tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("clear_quote_tags",html_clear_quote_tags, 
                tFunc(tNone,tObjImpl_PARSER_HTML),0); 
 
   ADD_FUNCTION("tags",html_tags, 
                tFunc(tNone,tMap(tStr,tTodo( tTagargs ))),0); 
   ADD_FUNCTION("containers",html_containers, 
                tFunc(tNone,tMap(tStr,tTodo( tTagargs tStr ))),0); 
   ADD_FUNCTION("entities",html_entities, 
                tFunc(tNone,tMap(tStr,tTodo( "" ))),0); 
   ADD_FUNCTION("quote_tags",html_quote_tags, 
                tFunc(tNone,tMap(tStr,tTodo( tStr ))),0); 
 
   /* setup */ 
 
   ADD_FUNCTION("set_extra",html_set_extra, 
                tFuncV(tNone,tMix,tObjImpl_PARSER_HTML),0); 
   ADD_FUNCTION("get_extra",html_get_extra, 
                tFunc(tNone,tArray),0); 
 
   ADD_FUNCTION("splice_arg",html_splice_arg, 
                tFunc(tOr(tVoid,tStr),tStr),0); 
 
   ADD_FUNCTION("ignore_tags",html_ignore_tags, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("max_stack_depth",html_max_stack_depth, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("case_insensitive_tag",html_case_insensitive_tag, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("lazy_argument_end",html_lazy_argument_end, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("lazy_entity_end",html_lazy_entity_end, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("nestling_entity_end",html_nestling_entity_end, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("match_tag",html_match_tag, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("mixed_mode",html_mixed_mode, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("reparse_strings",html_reparse_strings, 
                tFunc(tOr(tVoid,tInt),tInt),0); 
   ADD_FUNCTION("ignore_unknown",html_ignore_unknown, 
                tFunc(tOr(tVoid,tInt),tInt),0);