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
|
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2000-2022, University of Amsterdam
VU University Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#define _ISOC99_SOURCE 1 /* fwprintf(), etc prototypes */
#define DTD_MINOR_ERRORS 1 /* get detailed errors */
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <stdio.h>
#include "dtd.h"
#include "catalog.h"
#include "model.h"
#include "util.h"
#include <errno.h>
#include "error.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <wctype.h>
#define streq(s1, s2) (strcmp(s1, s2) == 0)
#define MAX_ERRORS 50
#define MAX_WARNINGS 50
#define ENDSNUL ((size_t)-1)
/*******************************
* PARSER CONTEXT DATA *
*******************************/
#define PD_MAGIC 0x36472ba1 /* just a number */
typedef enum
{ SA_FILE = 0, /* Stop at end-of-file */
SA_INPUT, /* Do not complete input */
SA_ELEMENT, /* Stop after first element */
SA_CONTENT, /* Stop after close */
SA_DECL /* Stop after declaration */
} stopat;
typedef enum
{ EM_QUIET = 0, /* Suppress messages */
EM_PRINT, /* Print message */
EM_STYLE /* include style-messages */
} errormode;
typedef struct _env
{ term_t tail;
struct _env *parent;
} env;
typedef struct _parser_data
{ int magic; /* PD_MAGIC */
dtd_parser *parser; /* parser itself */
int warnings; /* #warnings seen */
int errors; /* #errors seen */
int max_errors; /* error limit */
int max_warnings; /* warning limit */
errormode error_mode; /* how to handle errors */
int positions; /* report file-positions */
term_t exception; /* pending exception from callback */
predicate_t on_begin; /* begin element */
predicate_t on_end; /* end element */
predicate_t on_cdata; /* cdata */
predicate_t on_entity; /* entity */
predicate_t on_pi; /* processing instruction */
predicate_t on_xmlns; /* xmlns */
predicate_t on_urlns; /* url --> namespace */
predicate_t on_error; /* errors */
predicate_t on_decl; /* declarations */
stopat stopat; /* Where to stop */
int stopped; /* Environment is complete */
IOSTREAM* source; /* Where we are reading from */
term_t list; /* output term (if any) */
term_t tail; /* tail of the list */
env *stack; /* environment stack */
int free_on_close; /* sgml_free parser on close */
} parser_data;
/*******************************
* CONSTANTS *
*******************************/
static functor_t FUNCTOR_and2;
static functor_t FUNCTOR_attribute_value1;
static functor_t FUNCTOR_bar2;
static functor_t FUNCTOR_comma2;
static functor_t FUNCTOR_default1;
static functor_t FUNCTOR_dialect1;
static functor_t FUNCTOR_keep_prefix1;
static functor_t FUNCTOR_document1;
static functor_t FUNCTOR_dtd1;
static functor_t FUNCTOR_dtd2;
static functor_t FUNCTOR_element3;
static functor_t FUNCTOR_entity1;
static functor_t FUNCTOR_equal2;
static functor_t FUNCTOR_file1;
static functor_t FUNCTOR_file4;
static functor_t FUNCTOR_dstream_position4;
static functor_t FUNCTOR_fixed1;
static functor_t FUNCTOR_line1;
static functor_t FUNCTOR_linepos1;
static functor_t FUNCTOR_list1;
static functor_t FUNCTOR_max_errors1;
static functor_t FUNCTOR_syntax_error1;
static functor_t FUNCTOR_error2;
static functor_t FUNCTOR_nameof1;
static functor_t FUNCTOR_notation1;
static functor_t FUNCTOR_omit2;
static functor_t FUNCTOR_opt1;
static functor_t FUNCTOR_plus1;
static functor_t FUNCTOR_rep1;
static functor_t FUNCTOR_sgml_parser1;
static functor_t FUNCTOR_parse1;
static functor_t FUNCTOR_source1;
static functor_t FUNCTOR_content_length1;
static functor_t FUNCTOR_call2;
static functor_t FUNCTOR_cdata1;
static functor_t FUNCTOR_charpos1;
static functor_t FUNCTOR_charpos2;
static functor_t FUNCTOR_ns2; /* :/2 */
static functor_t FUNCTOR_prefix2; /* ns/2. This is a bit confusing but ns2 was already taken */
static functor_t FUNCTOR_space1;
static functor_t FUNCTOR_pi1;
static functor_t FUNCTOR_sdata1;
static functor_t FUNCTOR_ndata1;
static functor_t FUNCTOR_number1;
static functor_t FUNCTOR_syntax_errors1;
static functor_t FUNCTOR_xml_no_ns1;
static functor_t FUNCTOR_minus2;
static functor_t FUNCTOR_positions1;
static functor_t FUNCTOR_position1;
static functor_t FUNCTOR_event_class1;
static functor_t FUNCTOR_doctype1;
static functor_t FUNCTOR_allowed1;
static functor_t FUNCTOR_context1;
static functor_t FUNCTOR_defaults1;
static functor_t FUNCTOR_shorttag1;
static functor_t FUNCTOR_case_sensitive_attributes1;
static functor_t FUNCTOR_case_preserving_attributes1;
static functor_t FUNCTOR_system_entities1;
static functor_t FUNCTOR_max_memory1;
static functor_t FUNCTOR_ignore_doctype1;
static functor_t FUNCTOR_qualify_attributes1;
static functor_t FUNCTOR_encoding1;
static functor_t FUNCTOR_xmlns1;
static functor_t FUNCTOR_xmlns2;
static atom_t ATOM_true;
static atom_t ATOM_false;
static atom_t ATOM_cdata;
static atom_t ATOM_rcdata;
static atom_t ATOM_pcdata;
static atom_t ATOM_empty;
static atom_t ATOM_any;
static atom_t ATOM_position;
static atom_t ATOM_atom;
static atom_t ATOM_string;
#define mkfunctor(n, a) PL_new_functor(PL_new_atom(n), a)
static void
initConstants()
{
FUNCTOR_sgml_parser1 = mkfunctor("sgml_parser", 1);
FUNCTOR_equal2 = mkfunctor("=", 2);
FUNCTOR_dtd1 = mkfunctor("dtd", 1);
FUNCTOR_element3 = mkfunctor("element", 3);
FUNCTOR_entity1 = mkfunctor("entity", 1);
FUNCTOR_document1 = mkfunctor("document", 1);
FUNCTOR_dtd2 = mkfunctor("dtd", 2);
FUNCTOR_omit2 = mkfunctor("omit", 2);
FUNCTOR_and2 = mkfunctor("&", 2);
FUNCTOR_comma2 = mkfunctor(",", 2);
FUNCTOR_bar2 = mkfunctor("|", 2);
FUNCTOR_opt1 = mkfunctor("?", 1);
FUNCTOR_rep1 = mkfunctor("*", 1);
FUNCTOR_plus1 = mkfunctor("+", 1);
FUNCTOR_default1 = mkfunctor("default", 1);
FUNCTOR_fixed1 = mkfunctor("fixed", 1);
FUNCTOR_list1 = mkfunctor("list", 1);
FUNCTOR_nameof1 = mkfunctor("nameof", 1);
FUNCTOR_notation1 = mkfunctor("notation", 1);
FUNCTOR_file1 = mkfunctor("file", 1);
FUNCTOR_file4 = mkfunctor("file", 4);
FUNCTOR_line1 = mkfunctor("line", 1);
FUNCTOR_linepos1 = mkfunctor("linepos", 1);
FUNCTOR_dialect1 = mkfunctor("dialect", 1);
FUNCTOR_keep_prefix1 = mkfunctor("keep_prefix", 1);
FUNCTOR_max_errors1 = mkfunctor("max_errors", 1);
FUNCTOR_ignore_doctype1 = mkfunctor("ignore_doctype", 1);
FUNCTOR_parse1 = mkfunctor("parse", 1);
FUNCTOR_source1 = mkfunctor("source", 1);
FUNCTOR_content_length1= mkfunctor("content_length", 1);
FUNCTOR_call2 = mkfunctor("call", 2);
FUNCTOR_cdata1 = mkfunctor("cdata", 1);
FUNCTOR_attribute_value1 = mkfunctor("attribute_value", 1);
FUNCTOR_charpos1 = mkfunctor("charpos", 1);
FUNCTOR_charpos2 = mkfunctor("charpos", 2);
FUNCTOR_ns2 = mkfunctor(":", 2);
FUNCTOR_prefix2 = mkfunctor("ns", 2);
FUNCTOR_space1 = mkfunctor("space", 1);
FUNCTOR_pi1 = mkfunctor("pi", 1);
FUNCTOR_sdata1 = mkfunctor("sdata", 1);
FUNCTOR_ndata1 = mkfunctor("ndata", 1);
FUNCTOR_number1 = mkfunctor("number", 1);
FUNCTOR_syntax_errors1 = mkfunctor("syntax_errors", 1);
FUNCTOR_syntax_error1 = mkfunctor("syntax_error", 1);
FUNCTOR_error2 = mkfunctor("error", 2);
FUNCTOR_xml_no_ns1 = mkfunctor("xml_no_ns", 1);
FUNCTOR_minus2 = mkfunctor("-", 2);
FUNCTOR_position1 = mkfunctor("position", 1);
FUNCTOR_positions1 = mkfunctor("positions", 1);
FUNCTOR_event_class1 = mkfunctor("event_class", 1);
FUNCTOR_doctype1 = mkfunctor("doctype", 1);
FUNCTOR_allowed1 = mkfunctor("allowed", 1);
FUNCTOR_context1 = mkfunctor("context", 1);
FUNCTOR_defaults1 = mkfunctor("defaults", 1);
FUNCTOR_shorttag1 = mkfunctor("shorttag", 1);
FUNCTOR_case_sensitive_attributes1 = mkfunctor("case_sensitive_attributes", 1);
FUNCTOR_case_preserving_attributes1 = mkfunctor("case_preserving_attributes", 1);
FUNCTOR_system_entities1 = mkfunctor("system_entities", 1);
FUNCTOR_max_memory1 = mkfunctor("max_memory", 1);
FUNCTOR_qualify_attributes1 = mkfunctor("qualify_attributes", 1);
FUNCTOR_encoding1 = mkfunctor("encoding", 1);
FUNCTOR_xmlns1 = mkfunctor("xmlns", 1);
FUNCTOR_xmlns2 = mkfunctor("xmlns", 2);
FUNCTOR_dstream_position4 = PL_new_functor(PL_new_atom("$stream_position"), 4);
ATOM_true = PL_new_atom("true");
ATOM_false = PL_new_atom("false");
ATOM_cdata = PL_new_atom("cdata");
ATOM_rcdata = PL_new_atom("rcdata");
ATOM_pcdata = PL_new_atom("#pcdata");
ATOM_empty = PL_new_atom("empty");
ATOM_any = PL_new_atom("any");
ATOM_atom = PL_new_atom("atom");
ATOM_string = PL_new_atom("string");
ATOM_position = PL_new_atom("#position");
}
static int on_data(dtd_parser *p, data_type type, int len, const wchar_t *data);
/*******************************
* ACCESS *
*******************************/
static int
unify_parser(term_t parser, dtd_parser *p)
{ return PL_unify_term(parser, PL_FUNCTOR, FUNCTOR_sgml_parser1,
PL_POINTER, p);
}
static int
get_parser(term_t parser, dtd_parser **p)
{ if ( PL_is_functor(parser, FUNCTOR_sgml_parser1) )
{ term_t a = PL_new_term_ref();
void *ptr;
_PL_get_arg(1, parser, a);
if ( PL_get_pointer(a, &ptr) )
{ dtd_parser *tmp = ptr;
if ( tmp->magic == SGML_PARSER_MAGIC )
{ *p = tmp;
return TRUE;
}
return sgml2pl_error(ERR_EXISTENCE, "sgml_parser", parser);
}
}
return sgml2pl_error(ERR_TYPE, "sgml_parser", parser);
}
static int
unify_dtd(term_t t, dtd *dtd)
{ if ( dtd->doctype )
return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_dtd2,
PL_POINTER, dtd,
PL_NWCHARS, (size_t)-1, dtd->doctype);
else
return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_dtd2,
PL_POINTER, dtd,
PL_VARIABLE);
}
static int
get_dtd(term_t t, dtd **dtdp)
{ if ( PL_is_functor(t, FUNCTOR_dtd2) )
{ term_t a = PL_new_term_ref();
void *ptr;
_PL_get_arg(1, t, a);
if ( PL_get_pointer(a, &ptr) )
{ dtd *tmp = ptr;
if ( tmp->magic == SGML_DTD_MAGIC )
{ *dtdp = tmp;
return TRUE;
}
return sgml2pl_error(ERR_EXISTENCE, "dtd", t);
}
}
return sgml2pl_error(ERR_TYPE, "dtd", t);
}
/*******************************
* NEW/FREE *
*******************************/
static foreign_t
pl_new_sgml_parser(term_t ref, term_t options)
{ term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(options);
term_t tmp = PL_new_term_ref();
dtd *dtd = NULL;
dtd_parser *p;
while ( PL_get_list(tail, head, tail) )
{ if ( PL_is_functor(head, FUNCTOR_dtd1) )
{ _PL_get_arg(1, head, tmp);
if ( PL_is_variable(tmp) ) /* dtd(X) */
{ dtd = new_dtd(NULL); /* no known doctype */
dtd->references++;
unify_dtd(tmp, dtd);
} else if ( !get_dtd(tmp, &dtd) )
return FALSE;
}
}
if ( !PL_get_nil(tail) )
return sgml2pl_error(ERR_TYPE, "list", tail);
p = new_dtd_parser(dtd);
return unify_parser(ref, p);
}
static foreign_t
pl_free_sgml_parser(term_t parser)
{ dtd_parser *p;
if ( get_parser(parser, &p) )
{ free_dtd_parser(p);
return TRUE;
}
return FALSE;
}
static foreign_t
pl_new_dtd(term_t doctype, term_t ref)
{ ichar *dt;
dtd *dtd;
if ( !PL_get_wchars(doctype, NULL, &dt, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( !(dtd=new_dtd(dt)) )
return FALSE;
dtd->references++;
return unify_dtd(ref, dtd);
}
static foreign_t
pl_free_dtd(term_t t)
{ dtd *dtd;
if ( get_dtd(t, &dtd) )
{ free_dtd(dtd);
return TRUE;
}
return FALSE;
}
/*******************************
* DATA EXCHANGE *
*******************************/
static int
put_atom_wchars(term_t t, wchar_t const *s)
{ PL_put_variable(t);
return PL_unify_wchars(t, PL_ATOM, ENDSNUL, s);
}
/*******************************
* PROPERTIES *
*******************************/
static foreign_t
pl_set_sgml_parser(term_t parser, term_t option)
{ dtd_parser *p;
if ( !get_parser(parser, &p) )
return FALSE;
if ( PL_is_functor(option, FUNCTOR_file1) )
{ term_t a = PL_new_term_ref();
wchar_t *file;
dtd_symbol *fs;
_PL_get_arg(1, option, a);
if ( !PL_get_wchars(a, NULL, &file, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
fs = dtd_add_symbol(p->dtd, file); /* symbol will be freed */
set_file_dtd_parser(p, IN_FILE, fs->name);
} else if ( PL_is_functor(option, FUNCTOR_line1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( !PL_get_integer_ex(a, &p->location.line) )
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_linepos1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( !PL_get_integer_ex(a, &p->location.linepos) )
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_charpos1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( !PL_get_long_ex(a, &p->location.charpos) )
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_position1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( PL_is_functor(a, FUNCTOR_dstream_position4) )
{ term_t arg = PL_new_term_ref();
if ( !PL_get_arg(1,a,arg) || !PL_get_long_ex(arg, &p->location.charpos) ||
!PL_get_arg(2,a,arg) || !PL_get_integer_ex(arg, &p->location.line) ||
!PL_get_arg(3,a,arg) || !PL_get_integer_ex(arg, &p->location.linepos))
return FALSE;
} else
return PL_type_error("stream_position", a);
} else if ( PL_is_functor(option, FUNCTOR_dialect1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "xml") )
set_dialect_dtd(p->dtd, p, DL_XML);
else if ( streq(s, "xmlns") )
set_dialect_dtd(p->dtd, p, DL_XMLNS);
else if ( streq(s, "sgml") )
set_dialect_dtd(p->dtd, p, DL_SGML);
else if ( streq(s, "html") || streq(s, "html4") )
set_dialect_dtd(p->dtd, p, DL_HTML);
else if ( streq(s, "html5") )
set_dialect_dtd(p->dtd, p, DL_HTML5);
else if ( streq(s, "xhtml") )
set_dialect_dtd(p->dtd, p, DL_XHTML);
else if ( streq(s, "xhtml5") )
set_dialect_dtd(p->dtd, p, DL_XHTML5);
else
return sgml2pl_error(ERR_DOMAIN, "sgml_dialect", a);
} else if ( PL_is_functor(option, FUNCTOR_space1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "preserve") )
p->dtd->space_mode = SP_PRESERVE;
else if ( streq(s, "default") )
p->dtd->space_mode = SP_DEFAULT;
else if ( streq(s, "remove") )
p->dtd->space_mode = SP_REMOVE;
else if ( streq(s, "sgml") )
p->dtd->space_mode = SP_SGML;
else if ( streq(s, "strict") )
p->dtd->space_mode = SP_STRICT;
else
return sgml2pl_error(ERR_DOMAIN, "space", a);
} else if ( PL_is_functor(option, FUNCTOR_defaults1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
if ( val )
p->flags &= ~SGML_PARSER_NODEFS;
else
p->flags |= SGML_PARSER_NODEFS;
} else if ( PL_is_functor(option, FUNCTOR_qualify_attributes1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
if ( val )
p->flags |= SGML_PARSER_QUALIFY_ATTS;
else
p->flags &= ~SGML_PARSER_QUALIFY_ATTS;
} else if ( PL_is_functor(option, FUNCTOR_shorttag1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
set_option_dtd(p->dtd, OPT_SHORTTAG, val);
} else if ( PL_is_functor(option, FUNCTOR_case_sensitive_attributes1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
set_option_dtd(p->dtd, OPT_CASE_SENSITIVE_ATTRIBUTES, val);
} else if ( PL_is_functor(option, FUNCTOR_case_preserving_attributes1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
set_option_dtd(p->dtd, OPT_CASE_PRESERVING_ATTRIBUTES, val);
} else if ( PL_is_functor(option, FUNCTOR_system_entities1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
set_option_dtd(p->dtd, OPT_SYSTEM_ENTITIES, val);
} else if ( PL_is_functor(option, FUNCTOR_max_memory1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_integer(a, &val) )
return sgml2pl_error(ERR_TYPE, "integer", a);
p->max_memory = val;
if ( p->buffer )
p->buffer->limit = val;
if ( p->cdata )
p->cdata->limit = val;
} else if ( PL_is_functor(option, FUNCTOR_ignore_doctype1 ) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool_ex(a, &val) )
return FALSE;
p->ignore_doctype = val;
} else if ( PL_is_functor(option, FUNCTOR_number1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "token") )
p->dtd->number_mode = NU_TOKEN;
else if ( streq(s, "integer") )
p->dtd->number_mode = NU_INTEGER;
else
return sgml2pl_error(ERR_DOMAIN, "number", a);
} else if ( PL_is_functor(option, FUNCTOR_encoding1) )
{ term_t a = PL_new_term_ref();
char *val;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &val) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( !xml_set_encoding(p, val) )
return sgml2pl_error(ERR_DOMAIN, "encoding", a);
} else if ( PL_is_functor(option, FUNCTOR_doctype1) )
{ term_t a = PL_new_term_ref();
ichar *s;
_PL_get_arg(1, option, a);
if ( PL_is_variable(a) )
{ p->enforce_outer_element = NULL;
} else
{ if ( !PL_get_wchars(a, NULL, &s, CVT_ATOM) )
return sgml2pl_error(ERR_TYPE, "atom_or_variable", a);
p->enforce_outer_element = dtd_add_symbol(p->dtd, s);
}
} else if ( PL_is_functor(option, FUNCTOR_xmlns1) )
{ term_t a = PL_new_term_ref();
ichar ns[1] = {0};
ichar *uri;
_PL_get_arg(1, option, a);
if ( !PL_get_wchars(a, NULL, &uri, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
xmlns_push(p, ns, uri);
} else if ( PL_is_functor(option, FUNCTOR_xmlns2) )
{ term_t a = PL_new_term_ref();
ichar *ns, *uri;
_PL_get_arg(1, option, a);
if ( !PL_get_wchars(a, NULL, &ns, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
_PL_get_arg(2, option, a);
if ( !PL_get_wchars(a, NULL, &uri, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
xmlns_push(p, ns, uri);
} else if ( PL_is_functor(option, FUNCTOR_keep_prefix1) )
{ term_t a = PL_new_term_ref();
int val;
_PL_get_arg(1, option, a);
if ( !PL_get_bool(a, &val) )
return sgml2pl_error(ERR_TYPE, "boolean", a);
set_option_dtd(p->dtd, OPT_KEEP_PREFIX, val);
} else
return sgml2pl_error(ERR_DOMAIN, "sgml_parser_option", option);
return TRUE;
}
static dtd_srcloc *
file_location(dtd_parser *p, dtd_srcloc *l)
{ while(l->parent && l->type != IN_FILE)
l = l->parent;
return l;
}
static foreign_t
pl_get_sgml_parser(term_t parser, term_t option)
{ dtd_parser *p;
if ( !get_parser(parser, &p) )
return FALSE;
if ( PL_is_functor(option, FUNCTOR_charpos1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_integer(a, file_location(p, &p->startloc)->charpos);
} else if ( PL_is_functor(option, FUNCTOR_line1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_integer(a, file_location(p, &p->startloc)->line);
} else if ( PL_is_functor(option, FUNCTOR_charpos2) )
{ term_t a = PL_new_term_ref();
if ( PL_get_arg(1, option, a) &&
PL_unify_integer(a, file_location(p, &p->startloc)->charpos) &&
PL_get_arg(2, option, a) &&
PL_unify_integer(a, file_location(p, &p->location)->charpos) )
return TRUE;
else
return FALSE;
} else if ( PL_is_functor(option, FUNCTOR_file1) )
{ dtd_srcloc *l = file_location(p, &p->location);
if ( l->type == IN_FILE && l->name.file )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_wchars(a, PL_ATOM, ENDSNUL, l->name.file);
}
} else if ( PL_is_functor(option, FUNCTOR_source1) )
{ parser_data *pd = p->closure;
if ( pd && pd->magic == PD_MAGIC && pd->source )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return PL_unify_stream(a, pd->source);
}
} else if ( PL_is_functor(option, FUNCTOR_dialect1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
switch(p->dtd->dialect)
{ case DL_SGML:
return PL_unify_atom_chars(a, "sgml");
case DL_HTML:
return PL_unify_atom_chars(a, "html");
case DL_HTML5:
return PL_unify_atom_chars(a, "html5");
case DL_XHTML:
return PL_unify_atom_chars(a, "xhtml");
case DL_XHTML5:
return PL_unify_atom_chars(a, "xhtml5");
case DL_XML:
return PL_unify_atom_chars(a, "xml");
case DL_XMLNS:
return PL_unify_atom_chars(a, "xmlns");
}
} else if ( PL_is_functor(option, FUNCTOR_event_class1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
switch(p->event_class)
{ case EV_EXPLICIT:
return PL_unify_atom_chars(a, "explicit");
case EV_OMITTED:
return PL_unify_atom_chars(a, "omitted");
case EV_SHORTTAG:
return PL_unify_atom_chars(a, "shorttag");
case EV_SHORTREF:
return PL_unify_atom_chars(a, "shortref");
}
} else if ( PL_is_functor(option, FUNCTOR_dtd1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
return unify_dtd(a, p->dtd);
} else if ( PL_is_functor(option, FUNCTOR_doctype1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, option, a);
if ( p->enforce_outer_element )
return PL_unify_wchars(a, PL_ATOM, ENDSNUL,
p->enforce_outer_element->name);
else
return TRUE; /* leave variable */
} else if ( PL_is_functor(option, FUNCTOR_allowed1) )
{ term_t tail, head, tmp;
sgml_environment *env = p->environments;
if ( !(tail = PL_new_term_ref()) ||
!(head = PL_new_term_ref()) ||
!(tmp = PL_new_term_ref()) )
return FALSE;
_PL_get_arg(1, option, tail);
if ( env )
{ for( ; env; env = env->parent)
{ dtd_element *buf[256]; /* MAX_VISITED! */
int n = sizeof(buf)/sizeof(dtd_element *); /* not yet used! */
int i;
state_allows_for(env->state, buf, &n);
for(i=0; i<n; i++)
{ int rc;
if ( buf[i] == CDATA_ELEMENT )
rc = PL_put_atom_chars(tmp, "#pcdata");
else
rc = put_atom_wchars(tmp, buf[i]->name->name);
if ( !rc ||
!PL_unify_list(tail, head, tail) ||
!PL_unify(head, tmp) )
return FALSE;
}
if ( !env->element->structure ||
!env->element->structure->omit_close )
break;
}
} else if ( p->enforce_outer_element )
{ put_atom_wchars(tmp, p->enforce_outer_element->name);
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, tmp) )
return FALSE;
}
return PL_unify_nil(tail);
} else if ( PL_is_functor(option, FUNCTOR_context1) )
{ term_t tail = PL_new_term_ref();
term_t head = PL_new_term_ref();
term_t tmp = PL_new_term_ref();
sgml_environment *env = p->environments;
_PL_get_arg(1, option, tail);
for( ; env; env = env->parent)
{ put_atom_wchars(tmp, env->element->name->name);
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, tmp) )
return FALSE;
}
return PL_unify_nil(tail);
} else
return sgml2pl_error(ERR_DOMAIN, "parser_option", option);
return FALSE;
}
static int
call_prolog(parser_data *pd, predicate_t pred, term_t av)
{ qid_t qid = PL_open_query(NULL, PL_Q_PASS_EXCEPTION, pred, av);
int rc = PL_next_solution(qid);
PL_close_query(qid);
if ( rc )
{ pd->exception = FALSE;
} else
{ if ( (pd->exception = PL_exception(0)) )
pd->stopped = TRUE;
}
return rc;
}
static void
end_frame(fid_t fid, term_t ex)
{ if ( ex )
PL_close_foreign_frame(fid);
else
PL_discard_foreign_frame(fid);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
put_url(dtd_parser *p, term_t t, const ichar *url)
Store the url-part of a name-space qualifier in term. We call
xml:xmlns(-Canonical, +Full) trying to resolve the specified
namespace to an internal canonical namespace.
We do a little caching as there will generally be only a very
small pool of urls in use. We assume the url-pointers we get
life for the time of the parser. It might be possible that
multiple url pointers point to the same url, but this only clobbers
the cache a little.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define URL_CACHE 4 /* # entries cached */
typedef struct
{ const ichar *url; /* URL pointer */
atom_t canonical;
} url_cache;
static url_cache cache[URL_CACHE];
static void
reset_url_cache()
{ int i;
url_cache *c = cache;
for(i=0; i<URL_CACHE; i++)
{ c[i].url = NULL;
if ( c[i].canonical )
PL_unregister_atom(c[i].canonical);
c[i].canonical = 0;
}
}
WUNUSED static int
put_url(dtd_parser *p, term_t t, const ichar *url)
{ parser_data *pd = p->closure;
fid_t fid;
int i;
if ( !pd->on_urlns )
return put_atom_wchars(t, url);
for(i=0; i<URL_CACHE; i++)
{ if ( cache[i].url == url ) /* cache hit */
{ if ( cache[i].canonical ) /* and a canonical value */
return PL_put_atom(t, cache[i].canonical);
else
return put_atom_wchars(t, url);
}
}
/* shift the cache */
i = URL_CACHE-1;
if ( cache[i].canonical )
PL_unregister_atom(cache[i].canonical);
for(i=URL_CACHE-1; i>0; i--)
cache[i] = cache[i-1];
cache[0].url = url;
cache[0].canonical = 0;
if ( (fid = PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(3);
atom_t a;
rc = (put_atom_wchars(av+0, url) &&
unify_parser(av+2, p));
if ( rc &&
PL_call_predicate(NULL, PL_Q_NORMAL, pd->on_urlns, av) &&
PL_get_atom(av+1, &a) )
{ PL_register_atom(a);
cache[0].canonical = a;
PL_put_atom(t, a);
} else if ( rc )
{ rc = put_atom_wchars(t, url);
}
PL_discard_foreign_frame(fid);
return rc;
}
return FALSE;
}
WUNUSED static int
put_attribute_name(dtd_parser *p, term_t t, dtd_symbol *nm)
{ const ichar *url, *local, *prefix;
if ( p->dtd->dialect == DL_XMLNS )
{ xmlns_resolve_attribute(p, nm, &local, &url, &prefix);
if ( url )
{ term_t av;
if ( p->dtd->keep_prefix)
{ /* creates ns(prefix,url):local */
PL_put_variable(t);
return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_ns2,
PL_FUNCTOR, FUNCTOR_prefix2,
PL_NWCHARS, ENDSNUL, prefix ? prefix : L"",
PL_NWCHARS, ENDSNUL, url,
PL_NWCHARS, ENDSNUL, local);
} else
{ return ( (av=PL_new_term_refs(2)) &&
put_url(p, av+0, url) &&
put_atom_wchars(av+1, local) &&
PL_cons_functor_v(t, FUNCTOR_ns2, av) );
}
} else
return put_atom_wchars(t, local);
} else
return put_atom_wchars(t, nm->name);
}
WUNUSED static int
put_element_name(dtd_parser *p, term_t t, dtd_element *e)
{ const ichar *url, *local, *prefix;
if ( p->dtd->dialect == DL_XMLNS )
{ assert(p->environments->element == e);
xmlns_resolve_element(p, &local, &url, &prefix);
if ( url )
{ term_t av;
if ( p->dtd->keep_prefix )
{ /* creates ns(prefix,url):local */
return PL_unify_term(t, PL_FUNCTOR, FUNCTOR_ns2,
PL_FUNCTOR, FUNCTOR_prefix2,
PL_NWCHARS, ENDSNUL, prefix ? prefix : L"",
PL_NWCHARS, ENDSNUL, url,
PL_NWCHARS, ENDSNUL, local);
} else
{ return ( (av=PL_new_term_refs(2)) &&
put_url(p, av+0, url) &&
put_atom_wchars(av+1, local) &&
PL_cons_functor_v(t, FUNCTOR_ns2, av) );
}
} else
return put_atom_wchars(t, local);
} else
return put_atom_wchars(t, e->name->name);
}
static ichar *
istrblank(const ichar *s)
{ for( ; *s; s++ )
{ if ( iswspace(*s) )
return (ichar *)s;
}
return NULL;
}
WUNUSED static int
unify_listval(dtd_parser *p,
term_t t, attrtype type, size_t len, const ichar *text)
{ if ( type == AT_NUMBERS && p->dtd->number_mode == NU_INTEGER )
{ wchar_t *e;
#if SIZEOF_LONG == 4 && defined(HAVE_WCSTOLL)
int64_t v = wcstoll(text, &e, 10);
if ( (size_t)(e-text) == len && errno != ERANGE )
return PL_unify_int64(t, v);
#else
long v = wcstol(text, &e, 10);
if ( (size_t)(e-text) == len && errno != ERANGE )
return PL_unify_integer(t, v);
#endif
/* TBD: Error!? */
}
return PL_unify_wchars(t, PL_ATOM, len, text);
}
WUNUSED static int
put_att_text(dtd_parser *p, term_t t, sgml_attribute *a)
{ if ( a->value.textW )
{ PL_put_variable(t);
return PL_unify_wchars(t, p->att_rep, a->value.number, a->value.textW);
} else
return FALSE;
}
WUNUSED static int
put_attribute_value(dtd_parser *p, term_t t, sgml_attribute *a)
{ switch(a->definition->type)
{ case AT_CDATA:
return put_att_text(p, t, a);
case AT_NUMBER:
{ if ( !put_att_text(p, t, a) )
return PL_put_integer(t, a->value.number);
return TRUE;
}
default: /* multi-valued attribute */
{ if ( a->definition->islist && a->value.textW )
{ term_t tail, head;
const ichar *val = a->value.textW;
const ichar *e;
PL_put_variable(t);
if ( !(head = PL_new_term_ref()) ||
!(tail = PL_copy_term_ref(t)) )
return FALSE;
for(e=istrblank(val); e; val = e+1, e=istrblank(val))
{ if ( e == val )
continue; /* skip spaces */
if ( !PL_unify_list(tail, head, tail) ||
!unify_listval(p, head, a->definition->type, e-val, val) )
return FALSE;
}
return ( PL_unify_list(tail, head, tail) &&
unify_listval(p, head, a->definition->type,
istrlen(val), val) &&
PL_unify_nil(tail) );
} else
return put_att_text(p, t, a);
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Produce a tag-location in the format
start_location=file:char-char
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
WUNUSED static int
put_tag_position(dtd_parser *p, term_t pos)
{ dtd_srcloc *l = &p->startloc;
if ( l->type == IN_FILE && l->name.file )
{ PL_put_variable(pos);
return PL_unify_term(pos,
PL_FUNCTOR, FUNCTOR_ns2,
PL_NWCHARS, wcslen(l->name.file), l->name.file,
PL_FUNCTOR, FUNCTOR_minus2,
PL_LONG, l->charpos,
PL_LONG, p->location.charpos);
}
return FALSE;
}
WUNUSED static int
unify_attribute_list(dtd_parser *p, term_t alist,
int argc, sgml_attribute *argv)
{ int i;
term_t tail = PL_copy_term_ref(alist);
term_t h = PL_new_term_ref();
term_t a = PL_new_term_refs(2);
parser_data *pd = p->closure;
for(i=0; i<argc; i++)
{ if ( !put_attribute_name(p, a+0, argv[i].definition->name) ||
!put_attribute_value(p, a+1, &argv[i]) ||
!PL_cons_functor_v(a, FUNCTOR_equal2, a) ||
!PL_unify_list(tail, h, tail) ||
!PL_unify(h, a) )
return FALSE;
}
if ( pd->positions && put_tag_position(p, a+1) )
{ PL_put_atom(a, ATOM_position);
if ( !PL_cons_functor_v(a, FUNCTOR_equal2, a) ||
!PL_unify_list(tail, h, tail) ||
!PL_unify(h, a) )
return FALSE;
}
if ( PL_unify_nil(tail) )
{ PL_reset_term_refs(tail);
return TRUE;
}
return FALSE;
}
static int
on_begin_(dtd_parser *p, dtd_element *e, int argc, sgml_attribute *argv)
{ parser_data *pd = p->closure;
if ( pd->stopped )
return TRUE;
if ( pd->tail )
{ term_t content = PL_new_term_ref(); /* element content */
term_t alist = PL_new_term_ref(); /* attribute list */
term_t et = PL_new_term_ref(); /* element structure */
term_t h = PL_new_term_ref();
if ( !h ||
!put_element_name(p, h, e) ||
!unify_attribute_list(p, alist, argc, argv) ||
!PL_unify_term(et,
PL_FUNCTOR, FUNCTOR_element3,
PL_TERM, h,
PL_TERM, alist,
PL_TERM, content) )
{ pd->exception = PL_exception(0);
return FALSE;
}
if ( PL_unify_list(pd->tail, h, pd->tail) &&
PL_unify(h, et) )
{ env *env = sgml_calloc(1, sizeof(*env));
env->tail = pd->tail;
env->parent = pd->stack;
pd->stack = env;
pd->tail = content;
PL_reset_term_refs(alist);
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
if ( pd->on_begin )
{ fid_t fid;
if ( (fid = PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(3);
rc = ( put_element_name(p, av+0, e) &&
unify_attribute_list(p, av+1, argc, argv) &&
unify_parser(av+2, p) &&
call_prolog(pd, pd->on_begin, av)
);
PL_discard_foreign_frame(fid);
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
return TRUE;
}
static int
on_end(dtd_parser *p, dtd_element *e)
{ parser_data *pd = p->closure;
if ( pd->stopped )
return TRUE;
if ( pd->on_end )
{ fid_t fid;
if ( (fid = PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(2);
PL_STRINGS_MARK();
rc = ( put_element_name(p, av+0, e) &&
unify_parser(av+1, p) &&
call_prolog(pd, pd->on_end, av)
);
PL_STRINGS_RELEASE();
PL_discard_foreign_frame(fid);
if ( rc )
goto ok;
}
if ( (pd->exception = PL_exception(0)) )
return FALSE;
}
ok:
if ( pd->tail && !pd->stopped )
{ if ( !PL_unify_nil(pd->tail) )
return FALSE;
PL_reset_term_refs(pd->tail); /* ? */
if ( pd->stack )
{ env *parent = pd->stack->parent;
pd->tail = pd->stack->tail;
sgml_free(pd->stack);
pd->stack = parent;
} else
{ if ( pd->stopat == SA_CONTENT )
pd->stopped = TRUE;
}
}
if ( pd->stopat == SA_ELEMENT && !p->environments->parent )
pd->stopped = TRUE;
return TRUE;
}
static int
on_entity_(dtd_parser *p, dtd_entity *e, int chr)
{ parser_data *pd = p->closure;
if ( pd->stopped )
return TRUE;
if ( pd->on_entity )
{ fid_t fid;
if ( (fid=PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(2);
if ( e )
rc = put_atom_wchars(av+0, e->name->name);
else
rc = PL_put_integer(av+0, chr);
if ( rc )
rc = ( unify_parser(av+1, p) &&
call_prolog(pd, pd->on_end, av)
);
PL_discard_foreign_frame(fid);
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
if ( pd->tail )
{ int rc;
term_t h = PL_new_term_ref();
if ( !h ||
!PL_unify_list(pd->tail, h, pd->tail) )
{ pd->exception = PL_exception(0);
return FALSE;
}
if ( e )
rc = PL_unify_term(h,
PL_FUNCTOR, FUNCTOR_entity1,
PL_CHARS, e->name->name);
else
rc = PL_unify_term(h,
PL_FUNCTOR, FUNCTOR_entity1,
PL_INT, chr);
PL_reset_term_refs(h);
if ( !rc )
pd->exception = PL_exception(0);
return rc;
}
return TRUE;
}
static int
on_data_(dtd_parser *p, data_type type, int len, const wchar_t *data)
{ parser_data *pd = p->closure;
if ( pd->on_cdata )
{ fid_t fid;
if ( (fid=PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(2);
rc = ( PL_unify_wchars(av+0, PL_ATOM, len, data) &&
unify_parser(av+1, p) &&
call_prolog(pd, pd->on_cdata, av) );
PL_discard_foreign_frame(fid);
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
if ( pd->tail && !pd->stopped )
{ term_t h = PL_new_term_ref();
if ( PL_unify_list(pd->tail, h, pd->tail) )
{ int rval = TRUE;
term_t a;
switch(type)
{ case EC_CDATA:
a = h;
break;
case EC_SDATA:
{ term_t d = PL_new_term_ref();
a = d;
rval = PL_unify_term(h, PL_FUNCTOR, FUNCTOR_sdata1, PL_TERM, d);
break;
}
case EC_NDATA:
{ term_t d = PL_new_term_ref();
a = d;
rval = PL_unify_term(h, PL_FUNCTOR, FUNCTOR_ndata1, PL_TERM, d);
break;
}
default:
rval = FALSE;
assert(0);
}
if ( rval )
rval = PL_unify_wchars(a, p->cdata_rep, len, data);
if ( rval )
{ PL_reset_term_refs(h);
return TRUE;
} else
{ pd->exception = PL_exception(0);
}
}
}
return FALSE;
}
static int
on_cdata(dtd_parser *p, data_type type, int len, const wchar_t *data)
{ return on_data(p, type, len, data);
}
static int
can_end_omitted(dtd_parser *p)
{ sgml_environment *env;
for(env=p->environments; env; env = env->parent)
{ dtd_element *e = env->element;
if ( !(e->structure && e->structure->omit_close) )
return FALSE;
}
return TRUE;
}
static int
on_error_(dtd_parser *p, dtd_error *error)
{ parser_data *pd = p->closure;
const char *severity;
if ( pd->stopped )
return TRUE;
if ( pd->stopat == SA_ELEMENT &&
(error->minor == ERC_NOT_OPEN || error->minor == ERC_NOT_ALLOWED) &&
can_end_omitted(p) )
{ end_document_dtd_parser(p);
sgml_cplocation(&p->location, &p->startloc);
pd->stopped = TRUE;
return TRUE;
}
switch(error->severity)
{ case ERS_STYLE:
if ( pd->error_mode != EM_STYLE )
return TRUE;
severity = "informational";
break;
case ERS_WARNING:
pd->warnings++;
severity = "warning";
break;
case ERS_ERROR:
default: /* make compiler happy */
pd->errors++;
severity = "error";
break;
}
if ( pd->on_error ) /* msg, parser */
{ fid_t fid;
if ( (fid=PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(3);
rc = ( PL_put_atom_chars(av+0, severity) &&
PL_unify_wchars(av+1, PL_ATOM,
wcslen(error->plain_message),
error->plain_message) &&
unify_parser(av+2, p) &&
call_prolog(pd, pd->on_error, av)
);
PL_discard_foreign_frame(fid);
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
} else if ( pd->error_mode != EM_QUIET )
{ fid_t fid;
if ( (fid=PL_open_foreign_frame()) )
{ int rc = TRUE;
dtd_srcloc *l = file_location(p, &p->startloc);
if ( pd->max_errors == 0 )
{ term_t ex = PL_new_term_ref();
term_t pos = PL_new_term_ref();
if ( l->name.file )
rc = PL_unify_term(pos,
PL_FUNCTOR, FUNCTOR_file4,
PL_NWCHARS, (size_t)-1, l->name.file,
PL_INT, l->line,
PL_INT, l->linepos,
PL_INT64, (int64_t)l->charpos);
if ( rc )
rc = PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_syntax_error1,
PL_NWCHARS, (size_t)-1, error->plain_message,
PL_TERM, pos);
if ( rc )
rc = PL_raise_exception(ex);
} else
{ predicate_t pred = PL_predicate("print_message", 2, "user");
term_t av = PL_new_term_refs(2);
term_t src = PL_new_term_ref();
term_t parser = PL_new_term_ref();
rc = ( unify_parser(parser, p) &&
PL_put_atom_chars(av+0, severity) );
if ( rc )
{ if ( l->name.file )
{ if ( l->type == IN_FILE )
rc = put_atom_wchars(src, l->name.file);
else
rc = put_atom_wchars(src, l->name.entity);
} else
{ PL_put_nil(src);
}
}
if ( rc )
rc = PL_unify_term(av+1,
PL_FUNCTOR_CHARS, "sgml", 4,
PL_TERM, parser,
PL_TERM, src,
PL_INT, l->line,
PL_NWCHARS, wcslen(error->plain_message),
error->plain_message);
if ( rc )
rc = PL_call_predicate(NULL, PL_Q_NODEBUG, pred, av);
PL_discard_foreign_frame(fid);
}
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
return TRUE;
}
static int
on_xmlns_(dtd_parser *p, dtd_symbol *ns, dtd_symbol *url)
{ parser_data *pd = p->closure;
if ( pd->stopped )
return TRUE;
if ( pd->on_xmlns )
{ fid_t fid;
term_t av;
if ( (fid = PL_open_foreign_frame()) &&
(av = PL_new_term_refs(3)) )
{ int rc;
if ( ns )
{ rc = put_atom_wchars(av+0, ns->name);
} else
{ PL_put_nil(av+0);
rc = TRUE;
}
if ( rc )
{ rc = ( put_atom_wchars(av+1, url->name) &&
unify_parser(av+2, p) &&
call_prolog(pd, pd->on_xmlns, av)
);
}
end_frame(fid, pd->exception);
PL_discard_foreign_frame(fid);
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
return TRUE;
}
static int
on_pi(dtd_parser *p, const ichar *pi)
{ parser_data *pd = p->closure;
if ( pd->stopped )
return TRUE;
if ( pd->on_pi )
{ fid_t fid;
if ( (fid=PL_open_foreign_frame()) )
{ int rc;
term_t av = PL_new_term_refs(2);
rc = ( put_atom_wchars(av+0, pi) &&
unify_parser(av+1, p) &&
call_prolog(pd, pd->on_pi, av)
);
PL_discard_foreign_frame(fid);
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
if ( pd->tail )
{ term_t h;
if ( !(h = PL_new_term_ref()) ||
!PL_unify_list(pd->tail, h, pd->tail) )
{ pd->exception = PL_exception(0);
return FALSE;
}
if ( !PL_unify_term(h,
PL_FUNCTOR, FUNCTOR_pi1,
PL_NWCHARS, wcslen(pi), pi) )
{ pd->exception = PL_exception(0);
return FALSE;
}
PL_reset_term_refs(h);
}
return TRUE;
}
static int
on_decl(dtd_parser *p, const ichar *decl)
{ parser_data *pd = p->closure;
if ( pd->stopped )
return TRUE;
if ( pd->on_decl )
{ fid_t fid;
term_t av;
if ( (fid = PL_open_foreign_frame()) &&
(av = PL_new_term_refs(2)) )
{ int rc;
rc = ( put_atom_wchars(av+0, decl) &&
unify_parser(av+1, p) &&
call_prolog(pd, pd->on_decl, av)
);
end_frame(fid, pd->exception);
PL_discard_foreign_frame(fid);
if ( rc )
return TRUE;
}
pd->exception = PL_exception(0);
return FALSE;
}
if ( pd->stopat == SA_DECL )
pd->stopped = TRUE;
return TRUE;
}
static int
on_begin(dtd_parser *p, dtd_element *e, int argc, sgml_attribute *argv)
{ int rc;
PL_STRINGS_MARK();
rc = on_begin_(p, e, argc, argv);
PL_STRINGS_RELEASE();
return rc;
}
static int
on_data(dtd_parser *p, data_type type, int len, const wchar_t *data)
{ int rc;
PL_STRINGS_MARK();
rc = on_data_(p, type, len, data);
PL_STRINGS_RELEASE();
return rc;
}
static int
on_entity(dtd_parser *p, dtd_entity *e, int chr)
{ int rc;
PL_STRINGS_MARK();
rc = on_entity_(p, e, chr);
PL_STRINGS_RELEASE();
return rc;
}
static int
on_error(dtd_parser *p, dtd_error *error)
{ int rc;
PL_STRINGS_MARK();
rc = on_error_(p, error);
PL_STRINGS_RELEASE();
return rc;
}
static int
on_xmlns(dtd_parser *p, dtd_symbol *ns, dtd_symbol *url)
{ int rc;
PL_STRINGS_MARK();
rc = on_xmlns_(p, ns, url);
PL_STRINGS_RELEASE();
return rc;
}
static int
write_parser(void *h, char *buf, int len)
{ parser_data *pd = h;
unsigned char *s = (unsigned char *)buf;
unsigned char *e = s+len;
if ( !pd->parser || pd->parser->magic != SGML_PARSER_MAGIC )
{ errno = EINVAL;
return -1;
}
if ( (pd->errors > pd->max_errors && pd->max_errors >= 0) || pd->stopped )
{ errno = EIO;
return -1;
}
for(; s<e; s++)
{ putchar_dtd_parser(pd->parser, *s);
if ( pd->exception )
break;
}
return len;
}
static int
close_parser(void *h)
{ parser_data *pd = h;
dtd_parser *p;
if ( !(p=pd->parser) || p->magic != SGML_PARSER_MAGIC )
{ errno = EINVAL;
return -1;
}
if ( pd->tail )
{ if ( !PL_unify_nil(pd->tail) )
return -1; /* resource error */
}
if ( p->dmode == DM_DTD )
p->dtd->implicit = FALSE; /* assume we loaded a DTD */
if ( pd->free_on_close )
free_dtd_parser(p);
else
p->closure = NULL;
sgml_free(pd);
return 0;
}
static IOFUNCTIONS sgml_stream_functions =
{ (Sread_function) NULL,
(Swrite_function) write_parser,
(Sseek_function) NULL,
(Sclose_function) close_parser,
NULL
};
static parser_data *
new_parser_data(dtd_parser *p)
{ parser_data *pd;
pd = sgml_calloc(1, sizeof(*pd));
pd->magic = PD_MAGIC;
pd->parser = p;
pd->max_errors = MAX_ERRORS;
pd->max_warnings = MAX_WARNINGS;
pd->error_mode = EM_PRINT;
pd->exception = FALSE;
p->closure = pd;
return pd;
}
static foreign_t
pl_open_dtd(term_t ref, term_t options, term_t stream)
{ dtd *dtd;
dtd_parser *p;
parser_data *pd;
IOSTREAM *s;
term_t tail = PL_copy_term_ref(options);
term_t option = PL_new_term_ref();
if ( !get_dtd(ref, &dtd) )
return FALSE;
p = new_dtd_parser(dtd);
p->dmode = DM_DTD;
pd = new_parser_data(p);
pd->free_on_close = TRUE;
while( PL_get_list(tail, option, tail) )
{ if ( PL_is_functor(option, FUNCTOR_dialect1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "xml") )
set_dialect_dtd(dtd, p, DL_XML);
else if ( streq(s, "xmlns") )
set_dialect_dtd(dtd, p, DL_XMLNS);
else if ( streq(s, "sgml") )
set_dialect_dtd(dtd, p, DL_SGML);
else
return sgml2pl_error(ERR_DOMAIN, "sgml_dialect", a);
}
}
if ( !PL_get_nil(tail) )
return sgml2pl_error(ERR_TYPE, "list", options);
s = Snew(pd, SIO_OUTPUT|SIO_FBUF, &sgml_stream_functions);
if ( !PL_open_stream(stream, s) )
return FALSE;
return TRUE;
}
static int
set_callback_predicates(parser_data *pd, term_t option)
{ term_t a = PL_new_term_ref();
char *fname;
atom_t pname;
predicate_t *pp = NULL; /* keep compiler happy */
int arity;
module_t m = NULL;
_PL_get_arg(2, option, a);
if ( !PL_strip_module(a, &m, a) )
return FALSE;
if ( !PL_get_atom(a, &pname) )
return sgml2pl_error(ERR_TYPE, "atom", a);
_PL_get_arg(1, option, a);
if ( !PL_get_atom_chars(a, &fname) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(fname, "begin") )
{ pp = &pd->on_begin; /* tag, attributes, parser */
arity = 3;
} else if ( streq(fname, "end") )
{ pp = &pd->on_end; /* tag, parser */
arity = 2;
} else if ( streq(fname, "cdata") )
{ pp = &pd->on_cdata; /* cdata, parser */
arity = 2;
} else if ( streq(fname, "entity") )
{ pp = &pd->on_entity; /* name, parser */
arity = 2;
} else if ( streq(fname, "pi") )
{ pp = &pd->on_pi; /* pi, parser */
arity = 2;
} else if ( streq(fname, "xmlns") )
{ pp = &pd->on_xmlns; /* ns, url, parser */
arity = 3;
} else if ( streq(fname, "urlns") )
{ pp = &pd->on_urlns; /* url, ns, parser */
arity = 3;
} else if ( streq(fname, "error") )
{ pp = &pd->on_error; /* severity, message, parser */
arity = 3;
} else if ( streq(fname, "decl") )
{ pp = &pd->on_decl; /* decl, parser */
arity = 2;
} else
return sgml2pl_error(ERR_DOMAIN, "sgml_callback", a);
*pp = PL_pred(PL_new_functor(pname, arity), m);
return TRUE;
}
static foreign_t
pl_sgml_parse(term_t parser, term_t options)
{ dtd_parser *p;
parser_data *pd;
parser_data *oldpd;
term_t head = PL_new_term_ref();
term_t tail = PL_copy_term_ref(options);
IOSTREAM *in = NULL;
IOSTREAM *release = NULL;
int recursive;
int has_content_length = FALSE;
int64_t content_length = 0; /* content_length(Len) */
int count = 0;
int rc = TRUE;
if ( !get_parser(parser, &p) )
return FALSE;
if ( p->closure ) /* recursive call */
{ recursive = TRUE;
oldpd = p->closure;
if ( oldpd->magic != PD_MAGIC || oldpd->parser != p )
return sgml2pl_error(ERR_MISC, "sgml",
"Parser associated with illegal data");
pd = sgml_calloc(1, sizeof(*pd));
*pd = *oldpd;
p->closure = pd;
in = pd->source;
} else
{ recursive = FALSE;
oldpd = NULL; /* keep compiler happy */
set_mode_dtd_parser(p, DM_DATA);
p->on_begin_element = on_begin;
p->on_end_element = on_end;
p->on_entity = on_entity;
p->on_pi = on_pi;
p->on_data = on_cdata;
p->on_error = on_error;
p->on_xmlns = on_xmlns;
p->on_decl = on_decl;
p->cdata_rep = PL_ATOM;
p->att_rep = PL_ATOM;
pd = new_parser_data(p);
}
while ( PL_get_list(tail, head, tail) )
{ if ( PL_is_functor(head, FUNCTOR_document1) )
{ pd->list = PL_new_term_ref();
_PL_get_arg(1, head, pd->list);
pd->tail = PL_copy_term_ref(pd->list);
pd->stack = NULL;
} else if ( PL_is_functor(head, FUNCTOR_content_length1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, head, a);
if ( !PL_get_int64(a, &content_length) )
return sgml2pl_error(ERR_TYPE, "integer", a);
has_content_length = TRUE;
} else if ( PL_is_functor(head, FUNCTOR_call2) )
{ if ( !set_callback_predicates(pd, head) )
return FALSE;
} else if ( PL_is_functor(head, FUNCTOR_xml_no_ns1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, head, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "error") )
p->xml_no_ns = NONS_ERROR;
else if ( streq(s, "quiet") )
p->xml_no_ns = NONS_QUIET;
else
return sgml2pl_error(ERR_DOMAIN, "xml_no_ns", a);
} else if ( PL_is_functor(head, FUNCTOR_parse1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, head, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "element") )
pd->stopat = SA_ELEMENT;
else if ( streq(s, "content") )
pd->stopat = SA_CONTENT;
else if ( streq(s, "file") )
pd->stopat = SA_FILE;
else if ( streq(s, "input") )
pd->stopat = SA_INPUT;
else if ( streq(s, "declaration") )
pd->stopat = SA_DECL;
else
return sgml2pl_error(ERR_DOMAIN, "parse", a);
} else if ( PL_is_functor(head, FUNCTOR_max_errors1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, head, a);
if ( !PL_get_integer(a, &pd->max_errors) )
return sgml2pl_error(ERR_TYPE, "integer", a);
} else if ( PL_is_functor(head, FUNCTOR_syntax_errors1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, head, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "quiet") )
pd->error_mode = EM_QUIET;
else if ( streq(s, "print") )
pd->error_mode = EM_PRINT;
else if ( streq(s, "style") )
pd->error_mode = EM_STYLE;
else
return sgml2pl_error(ERR_DOMAIN, "syntax_error", a);
} else if ( PL_is_functor(head, FUNCTOR_positions1) )
{ term_t a = PL_new_term_ref();
char *s;
_PL_get_arg(1, head, a);
if ( !PL_get_atom_chars(a, &s) )
return sgml2pl_error(ERR_TYPE, "atom", a);
if ( streq(s, "true") )
pd->positions = TRUE;
else if ( streq(s, "false") )
pd->positions = FALSE;
else
return sgml2pl_error(ERR_DOMAIN, "positions", a);
} else if ( PL_is_functor(head, FUNCTOR_cdata1) )
{ term_t arg = PL_new_term_ref();
atom_t a;
_PL_get_arg(1, head, arg);
if (!PL_get_atom_ex(arg, &a))
return FALSE;
if (a == ATOM_atom)
p->cdata_rep = PL_ATOM;
else if (a == ATOM_string)
p->cdata_rep = PL_STRING;
else
return sgml2pl_error(ERR_DOMAIN, "representation", a);
} else if ( PL_is_functor(head, FUNCTOR_attribute_value1) )
{ term_t arg = PL_new_term_ref();
atom_t a;
_PL_get_arg(1, head, arg);
if (!PL_get_atom_ex(arg, &a))
return FALSE;
if (a == ATOM_atom)
p->att_rep = PL_ATOM;
else if (a == ATOM_string)
p->att_rep = PL_STRING;
else
return sgml2pl_error(ERR_DOMAIN, "representation", a);
} else if ( PL_is_functor(head, FUNCTOR_source1) )
{ term_t a = PL_new_term_ref();
_PL_get_arg(1, head, a);
if ( !PL_get_stream(a, &in, SIO_INPUT) )
return FALSE;
release = in;
}/* else ignored option */
}
if ( !PL_get_nil_ex(tail) )
{ if ( release )
PL_release_stream(release);
return FALSE;
}
/* Parsing input from a stream */
#define CHECKERROR \
{ if ( pd->exception ) \
{ rc = FALSE; \
goto out; \
} \
if ( pd->errors > pd->max_errors && pd->max_errors >= 0 ) \
{ rc = sgml2pl_error(ERR_LIMIT, "max_errors", (long)pd->max_errors); \
goto out; \
} \
}
if ( pd->stopat == SA_CONTENT && p->empty_element )
goto out;
if ( in )
{ int eof = FALSE;
if ( in->encoding == ENC_OCTET )
p->encoded = TRUE; /* parser must decode */
else
p->encoded = FALSE; /* already decoded */
pd->stopped = FALSE;
if ( !recursive )
{ pd->source = in;
begin_document_dtd_parser(p);
}
while(!eof)
{ int c, ateof;
if ( (++count % 8192) == 0 && PL_handle_signals() < 0 )
{ rc = FALSE;
goto out;
}
if ( has_content_length )
{ if ( content_length <= 0 )
c = EOF;
else
c = Sgetcode(in);
ateof = (--content_length <= 0);
} else
{ c = Sgetcode(in);
ateof = Sfeof(in);
}
if ( ateof )
{ eof = TRUE;
if ( c == LF ) /* file ends in LF */
c = CR;
else if ( c != CR ) /* file ends in normal char */
{ if ( has_content_length && in->position )
{ int64_t bc0 = in->position->byteno;
putchar_dtd_parser(p, c);
content_length -= in->position->byteno-bc0;
} else
{ putchar_dtd_parser(p, c);
}
CHECKERROR;
if ( pd->stopped )
goto stopped;
c = CR;
}
} else if ( Sferror(in) )
{ rc = FALSE;
goto out;
}
if ( has_content_length && in->position )
{ int64_t bc0 = in->position->byteno;
putchar_dtd_parser(p, c);
content_length -= in->position->byteno-bc0;
} else
{ putchar_dtd_parser(p, c);
}
CHECKERROR;
if ( pd->stopped )
{ stopped:
pd->stopped = FALSE;
if ( pd->stopat != SA_CONTENT )
reset_document_dtd_parser(p); /* ensure a clean start */
goto out;
}
}
if ( !recursive && pd->stopat != SA_INPUT )
end_document_dtd_parser(p);
CHECKERROR;
out:
if ( release )
rc = PL_release_stream(release) && rc;
reset_url_cache();
if ( pd->tail && rc )
rc = PL_unify_nil(pd->tail);
if ( recursive )
{ p->closure = oldpd;
} else
{ p->closure = NULL;
}
pd->magic = 0; /* invalidate */
sgml_free(pd);
return rc;
}
reset_url_cache();
return TRUE;
}
/*******************************
* DTD PROPERTIES *
*******************************/
static int put_model(term_t t, dtd_model *m) WUNUSED;
/* doctype(DocType) */
static int
dtd_prop_doctype(dtd *dtd, term_t prop)
{ if ( dtd->doctype )
return PL_unify_wchars(prop, PL_ATOM, ENDSNUL, dtd->doctype);
return FALSE;
}
/* elements(ListOfElements) */
WUNUSED static int
make_model_list(term_t t, dtd_model *m, functor_t f)
{ if ( !m->next )
{ return put_model(t, m);
} else
{ term_t av;
if ( (av=PL_new_term_refs(2)) &&
put_model(av+0, m) &&
make_model_list(av+1, m->next, f) &&
PL_cons_functor_v(t, f, av) )
{ PL_reset_term_refs(av);
return TRUE;
}
return FALSE;
}
}
WUNUSED static int
put_model(term_t t, dtd_model *m)
{ int rc = TRUE;
functor_t f;
switch(m->type)
{ case MT_PCDATA:
rc = PL_put_atom(t, ATOM_pcdata);
goto card;
case MT_ELEMENT:
rc = put_atom_wchars(t, m->content.element->name->name);
goto card;
case MT_AND:
f = FUNCTOR_and2;
break;
case MT_SEQ:
f = FUNCTOR_comma2;
break;
case MT_OR:
f = FUNCTOR_bar2;
break;
case MT_UNDEF:
default:
assert(0);
f = 0;
break;
}
if ( rc )
{ if ( !m->content.group )
rc = PL_put_atom(t, ATOM_empty);
else
rc = make_model_list(t, m->content.group, f);
}
card:
if ( !rc )
return FALSE;
switch(m->cardinality)
{ case MC_ONE:
break;
case MC_OPT:
rc = PL_cons_functor_v(t, FUNCTOR_opt1, t);
break;
case MC_REP:
rc = PL_cons_functor_v(t, FUNCTOR_rep1, t);
break;
case MC_PLUS:
rc = PL_cons_functor_v(t, FUNCTOR_plus1, t);
break;
}
return rc;
}
WUNUSED static int
put_content(term_t t, dtd_edef *def)
{ switch(def->type)
{ case C_EMPTY:
return PL_put_atom(t, ATOM_empty);
case C_CDATA:
return PL_put_atom(t, ATOM_cdata);
case C_RCDATA:
return PL_put_atom(t, ATOM_rcdata);
case C_ANY:
return PL_put_atom(t, ATOM_any);
default:
if ( def->content )
return put_model(t, def->content);
return TRUE;
}
}
static int
dtd_prop_elements(dtd *dtd, term_t prop)
{ term_t tail = PL_copy_term_ref(prop);
term_t head = PL_new_term_ref();
term_t et = PL_new_term_ref();
dtd_element *e;
for( e=dtd->elements; e; e=e->next )
{ put_atom_wchars(et, e->name->name);
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, et) )
return FALSE;
}
return PL_unify_nil(tail);
}
static int
get_element(dtd *dtd, term_t name, dtd_element **elem)
{ ichar *s;
dtd_element *e;
dtd_symbol *id;
if ( !PL_get_wchars(name, NULL, &s, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( !(id=dtd_find_symbol(dtd, s)) ||
!(e=id->element) )
return FALSE;
*elem = e;
return TRUE;
}
static int
dtd_prop_element(dtd *dtd, term_t name, term_t omit, term_t content)
{ dtd_element *e;
dtd_edef *def;
term_t model = PL_new_term_ref();
if ( !get_element(dtd, name, &e) || !(def=e->structure) )
return FALSE;
if ( !PL_unify_term(omit, PL_FUNCTOR, FUNCTOR_omit2,
PL_ATOM, def->omit_open ? ATOM_true : ATOM_false,
PL_ATOM, def->omit_close ? ATOM_true : ATOM_false) )
return FALSE;
return ( put_content(model, def) &&
PL_unify(content, model) );
}
static int
dtd_prop_attributes(dtd *dtd, term_t ename, term_t atts)
{ dtd_element *e;
term_t tail = PL_copy_term_ref(atts);
term_t head = PL_new_term_ref();
term_t elem = PL_new_term_ref();
dtd_attr_list *al;
if ( !get_element(dtd, ename, &e) )
return FALSE;
for(al=e->attributes; al; al=al->next)
{ put_atom_wchars(elem, al->attribute->name->name);
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, elem) )
return FALSE;
}
return PL_unify_nil(tail);
}
typedef struct _plattrdef
{ attrtype type; /* AT_* */
const char * name; /* name */
int islist; /* list-type */
atom_t atom; /* name as atom */
} plattrdef;
static plattrdef plattrs[] =
{
{ AT_CDATA, "cdata", FALSE },
{ AT_ENTITY, "entity", FALSE },
{ AT_ENTITIES, "entity", TRUE },
{ AT_ID, "id", FALSE },
{ AT_IDREF, "idref", FALSE },
{ AT_IDREFS, "idref", TRUE },
{ AT_NAME, "name", FALSE },
{ AT_NAMES, "name", TRUE },
/*{ AT_NAMEOF, "nameof", FALSE },*/
{ AT_NMTOKEN, "nmtoken", FALSE },
{ AT_NMTOKENS, "nmtoken", TRUE },
{ AT_NUMBER, "number", FALSE },
{ AT_NUMBERS, "number", TRUE },
{ AT_NUTOKEN, "nutoken", FALSE },
{ AT_NUTOKENS, "nutoken", TRUE },
{ AT_NOTATION, "notation", FALSE },
{ AT_CDATA, NULL, FALSE }
};
static int
unify_attribute_type(term_t type, dtd_attr *a)
{ plattrdef *ad = plattrs;
for( ; ad->name; ad++ )
{ if ( ad->type == a->type )
{ if ( !ad->atom )
ad->atom = PL_new_atom(ad->name);
if ( ad->islist )
return PL_unify_term(type, PL_FUNCTOR, FUNCTOR_list1,
PL_ATOM, ad->atom);
else
return PL_unify_atom(type, ad->atom);
}
}
if ( a->type == AT_NAMEOF || a->type == AT_NOTATION )
{ dtd_name_list *nl;
term_t tail, head, elem;
if ( !(tail = PL_new_term_ref()) ||
!(head = PL_new_term_ref()) ||
!(elem = PL_new_term_ref()) ||
!PL_unify_functor(type,
a->type == AT_NAMEOF ?
FUNCTOR_nameof1 :
FUNCTOR_notation1) )
return FALSE;
_PL_get_arg(1, type, tail);
for(nl = a->typeex.nameof; nl; nl = nl->next)
{ if ( !put_atom_wchars(elem, nl->value->name) ||
!PL_unify_list(tail, head, tail) ||
!PL_unify(head, elem) )
return FALSE;
}
return PL_unify_nil(tail);
}
assert(0);
return FALSE;
}
static int
unify_attribute_default(term_t defval, dtd_attr *a)
{ int v;
switch(a->def)
{ case AT_REQUIRED:
return PL_unify_atom_chars(defval, "required");
case AT_CURRENT:
return PL_unify_atom_chars(defval, "current");
case AT_CONREF:
return PL_unify_atom_chars(defval, "conref");
case AT_IMPLIED:
return PL_unify_atom_chars(defval, "implied");
case AT_DEFAULT:
v = PL_unify_functor(defval, FUNCTOR_default1);
goto common;
case AT_FIXED:
v = PL_unify_functor(defval, FUNCTOR_fixed1);
common:
if ( v )
{ term_t tmp;
if ( !(tmp=PL_new_term_ref()) )
return FALSE;
_PL_get_arg(1, defval, tmp);
switch( a->type )
{ case AT_CDATA:
return PL_unify_wchars(tmp, PL_ATOM, ENDSNUL, a->att_def.cdata);
case AT_NAME:
case AT_NMTOKEN:
case AT_NAMEOF:
case AT_NOTATION:
return PL_unify_wchars(tmp, PL_ATOM, ENDSNUL, a->att_def.name->name);
case AT_NUMBER:
return PL_unify_integer(tmp, a->att_def.number);
default:
assert(0);
}
} else
return FALSE;
default:
assert(0);
return FALSE;
}
}
static int
dtd_prop_attribute(dtd *dtd, term_t ename, term_t aname,
term_t type, term_t def_value)
{ dtd_element *e;
ichar *achars;
dtd_symbol *asym;
dtd_attr_list *al;
if ( !get_element(dtd, ename, &e) )
return FALSE;
if ( !PL_get_wchars(aname, NULL, &achars, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( !(asym=dtd_find_symbol(dtd, achars)) )
return FALSE;
for(al=e->attributes; al; al=al->next)
{ if ( al->attribute->name == asym )
{ if ( unify_attribute_type(type, al->attribute) &&
unify_attribute_default(def_value, al->attribute) )
return TRUE;
return FALSE;
}
}
return FALSE;
}
static int
dtd_prop_entities(dtd *dtd, term_t list)
{ term_t tail = PL_copy_term_ref(list);
term_t head = PL_new_term_ref();
term_t et = PL_new_term_ref();
dtd_entity *e;
for( e=dtd->entities; e; e=e->next )
{ put_atom_wchars(et, e->name->name);
if ( !PL_unify_list(tail, head, tail) ||
!PL_unify(head, et) )
return FALSE;
}
return PL_unify_nil(tail);
}
static int
dtd_prop_entity(dtd *dtd, term_t ename, term_t value)
{ ichar *s;
dtd_entity *e;
dtd_symbol *id;
if ( !PL_get_wchars(ename, NULL, &s, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( !(id=dtd_find_symbol(dtd, s)) ||
!(e=id->entity) )
return FALSE;
switch(e->type)
{ case ET_SYSTEM:
return PL_unify_term(value, PL_FUNCTOR_CHARS, "system", 1,
PL_CHARS, e->exturl);
case ET_PUBLIC:
if ( e->exturl )
return PL_unify_term(value, PL_FUNCTOR_CHARS, "public", 2,
PL_CHARS, e->extid,
PL_CHARS, e->exturl);
else
return PL_unify_term(value, PL_FUNCTOR_CHARS, "public", 2,
PL_CHARS, e->extid,
PL_VARIABLE);
case ET_LITERAL:
default:
if ( e->value )
{ const char *wrap;
switch(e->content)
{ case EC_SGML: wrap = "sgml"; break;
case EC_STARTTAG: wrap = "start_tag"; break;
case EC_ENDTAG: wrap = "end_tag"; break;
case EC_CDATA: wrap = NULL; break;
case EC_SDATA: wrap = "sdata"; break;
case EC_NDATA: wrap = "ndata"; break;
case EC_PI: wrap = "pi"; break;
default: wrap = NULL; assert(0);
}
if ( wrap )
return PL_unify_term(value, PL_FUNCTOR_CHARS, wrap, 1,
PL_CHARS, e->value);
else
return PL_unify_wchars(value, PL_ATOM, wcslen(e->value), e->value);
}
}
assert(0);
return FALSE;
}
static int
dtd_prop_notations(dtd *dtd, term_t list)
{ dtd_notation *n;
term_t tail = PL_copy_term_ref(list);
term_t head = PL_new_term_ref();
for(n=dtd->notations; n; n=n->next)
{ if ( PL_unify_list(tail, head, tail) &&
PL_unify_wchars(head, PL_ATOM, wcslen(n->name->name), n->name->name) )
continue;
return FALSE;
}
return PL_unify_nil(tail);
}
static int
dtd_prop_notation(dtd *dtd, term_t nname, term_t desc)
{ char *s;
dtd_symbol *id;
if ( !PL_get_atom_chars(nname, &s) )
return sgml2pl_error(ERR_TYPE, "atom", nname);
if ( (id=dtd_find_symbol(dtd, (ichar *)s)) )
{ dtd_notation *n;
for(n=dtd->notations; n; n=n->next)
{ if ( n->name == id )
{ term_t tail = PL_copy_term_ref(desc);
term_t head = PL_new_term_ref();
if ( n->system )
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_term(head,
PL_FUNCTOR_CHARS, "system", 1,
PL_CHARS, n->system) )
return FALSE;
}
if ( n->public )
{ if ( !PL_unify_list(tail, head, tail) ||
!PL_unify_term(head,
PL_FUNCTOR_CHARS, "public", 1,
PL_CHARS, n->public) )
return FALSE;
}
return PL_unify_nil(tail);
}
}
}
return FALSE;
}
typedef struct _prop
{ int (*func)();
const char *name;
int arity;
functor_t functor;
} prop;
static prop dtd_props[] =
{ { dtd_prop_doctype, "doctype", 1 },
{ dtd_prop_elements, "elements", 1 },
{ dtd_prop_element, "element", 3 },
{ dtd_prop_attributes, "attributes", 2, },
{ dtd_prop_attribute, "attribute", 4, },
{ dtd_prop_entities, "entities", 1, },
{ dtd_prop_entity, "entity", 2, },
{ dtd_prop_notations, "notations", 1, },
{ dtd_prop_notation, "notation", 2, },
{ NULL }
};
static void
initprops()
{ static int done = FALSE;
if ( !done )
{ prop *p;
done = TRUE;
for(p=dtd_props; p->func; p++)
p->functor = PL_new_functor(PL_new_atom(p->name), p->arity);
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
dtd_property(DTD, doctype(DocType))
dtd_property(DTD, elements(ListOfNames))
dtd_property(DTD, element(Name, Omit, Model))
dtd_property(DTD, attributes(ElementName, ListOfAttributes)),
dtd_property(DTD, attribute(ElementName, AttributeName, Type, Default))
dtd_property(DTD, entities(ListOfEntityNames))
dtd_property(DTD, entity(Name, Type))
dtd_property(DTD, notations(ListOfNotationNames)
dtd_property(DTD, notation(Name, File))
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
typedef int (*fptr1)(dtd *dtd, term_t);
typedef int (*fptr2)(dtd *dtd, term_t, term_t);
typedef int (*fptr3)(dtd *dtd, term_t, term_t, term_t);
typedef int (*fptr4)(dtd *dtd, term_t, term_t, term_t, term_t);
static foreign_t
pl_dtd_property(term_t ref, term_t property)
{ dtd *dtd;
const prop *p;
initprops();
if ( !get_dtd(ref, &dtd) )
return FALSE;
for(p=dtd_props; p->func; p++)
{ if ( PL_is_functor(property, p->functor) )
{ term_t a = PL_new_term_refs(p->arity);
int i;
for(i=0; i<p->arity; i++)
_PL_get_arg(i+1, property, a+i);
switch(p->arity)
{ case 1:
return (*(fptr1)p->func)(dtd, a+0);
case 2:
return (*(fptr2)p->func)(dtd, a+0, a+1);
case 3:
return (*(fptr3)p->func)(dtd, a+0, a+1, a+2);
case 4:
return (*(fptr4)p->func)(dtd, a+0, a+1, a+2, a+3);
default:
assert(0);
return FALSE;
}
}
}
return sgml2pl_error(ERR_DOMAIN, "dtd_property", property);
}
/*******************************
* CATALOG *
*******************************/
static foreign_t
pl_sgml_register_catalog_file(term_t file, term_t where)
{ wchar_t *fn;
char *w;
catalog_location loc;
if ( !PL_get_wchars(file, NULL, &fn, CVT_ATOM|CVT_EXCEPTION) )
return FALSE;
if ( !PL_get_atom_chars(where, &w) )
return sgml2pl_error(ERR_TYPE, "atom", where);
if ( streq(w, "start") )
loc = CTL_START;
else if ( streq(w, "end") )
loc = CTL_END;
else
return sgml2pl_error(ERR_DOMAIN, "location", where);
return register_catalog_file(fn, loc);
}
/*******************************
* INSTALL *
*******************************/
extern install_t install_xml_quote(void);
extern install_t install_xsd(void);
#ifdef O_STATISTICS
extern void sgml_statistics(void);
#endif
install_t
install_sgml2pl(void)
{ initConstants();
init_ring();
PL_register_foreign("new_dtd", 2, pl_new_dtd, 0);
PL_register_foreign("free_dtd", 1, pl_free_dtd, 0);
PL_register_foreign("new_sgml_parser", 2, pl_new_sgml_parser, 0);
PL_register_foreign("free_sgml_parser", 1, pl_free_sgml_parser, 0);
PL_register_foreign("set_sgml_parser", 2, pl_set_sgml_parser, 0);
PL_register_foreign("get_sgml_parser", 2, pl_get_sgml_parser, 0);
PL_register_foreign("open_dtd", 3, pl_open_dtd, 0);
PL_register_foreign("sgml_parse", 2, pl_sgml_parse,
PL_FA_TRANSPARENT);
PL_register_foreign("_sgml_register_catalog_file", 2,
pl_sgml_register_catalog_file, 0);
PL_register_foreign("$dtd_property", 2, pl_dtd_property, 0);
install_xml_quote();
install_xsd();
#ifdef O_STATISTICS
atexit(sgml_statistics);
#endif
}
install_t
uninstall_sgml2pl(void)
{ stop_ring();
}
|