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
|
#include <config.h>
#include "xctype.h"
#include "xstdlib.h"
#include "xstring.h"
#include "xunistd.h"
RCSID("$Id: do.c,v 1.6 1996/05/03 20:13:38 beebe Exp beebe $")
#include "ch.h"
#include "delete.h"
#include "keybrd.h"
#include "pattern.h"
#include "token.h"
#include "toklst.h"
#include "yesorno.h"
#include "match.h" /* must come AFTER yesorno.h */
#define APPEND_CHAR(s,n,c) (s[n] = (char)c, s[n+1] = (char)'\0')
/* append c and NUL to s[] */
#if !defined(BIBCLEAN_SUFFIX)
#define BIBCLEAN_EXT "BIBCLEANEXT" /* environment variable */
#endif
#define EMPTY_STRING(s) (s[0] = (char)'\0', s)
/* for return (EMPTY_STRING(foo))*/
#define GETDEFAULT(envname,default) \
((getenv(envname) != (char *)NULL) ? getenv(envname) : default)
#if !defined(INITFILE_EXT)
#define INITFILE_EXT ".ini" /* file extension for initialization files */
#endif
#define isfieldvalueseparator(c) (((c) == '=') || ((c) == ':'))
#define KEEP_PREAMBLE_SPACES() ((in_preamble == YES) && \
(keep_preamble_spaces == YES))
#define KEEP_STRING_SPACES() ((in_string == YES) && \
(keep_string_spaces == YES))
#if defined(MAX)
#undef MAX
#endif
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#if !defined(MAX_LINE)
#define MAX_LINE 10240 /* maximum line length in initialization file */
#endif /* !defined(MAX_LINE) */
#define NOOP /* dummy statement */
#if OS_PCDOS
#define OPEN_MODE_R "rb" /* bibliography files are read in binary mode */
/* so that we can handle Ctl-Z properly */
#else
#define OPEN_MODE_R "r"
#endif
#define SKIP_SPACE(p) while (isspace((unsigned char)*p)) ++p
#define TABLE_CHUNKS 25 /* how many table entries to allocate at once */
#define TOLOWER(c) (isupper(((unsigned char)c)) ? \
tolower(((unsigned char)c)) : (((unsigned char)c)))
typedef struct s_name_pair
{
const char *old_name;
const char *new_name;
} NAME_PAIR;
typedef struct s_option_function_entry
{
const char *name; /* option name */
size_t min_match; /* minimum length string match */
void (*function)(VOID); /* function to call when option matched */
} OPTION_FUNCTION_ENTRY;
typedef struct s_position
{
const char *filename;
long byte_position;
long last_column_position;
long column_position;
long line_number;
} POSITION;
typedef struct s_pattern_table
{
MATCH_PATTERN *patterns;
int current_size;
int maximum_size;
} PATTERN_TABLE;
typedef struct s_pattern_names
{
const char *name;
PATTERN_TABLE *table;
} PATTERN_NAMES;
typedef struct s_io_pair
{
POSITION input;
POSITION output;
} IO_PAIR;
NAME_PAIR month_pair[] =
{
{"January", "jan"},
{"February", "feb"},
{"March", "mar"},
{"April", "apr"},
{"May", "may"},
{"June", "jun"},
{"July", "jul"},
{"August", "aug"},
{"September", "sep"},
{"October", "oct"},
{"November", "nov"},
{"December", "dec"},
{"Jan.", "jan"},
{"Feb.", "feb"},
{"Mar.", "mar"},
{"Apr.", "apr"},
{"Jun.", "jun"},
{"Jul.", "jul"},
{"Aug.", "aug"},
{"Sep.", "sep"},
{"Sept.", "sep"},
{"Oct.", "oct"},
{"Nov.", "nov"},
{"Dec.", "dec"},
{"Jan", "jan"},
{"Feb", "feb"},
{"Mar", "mar"},
{"Apr", "apr"},
{"Jun", "jun"},
{"Jul", "jul"},
{"Aug", "aug"},
{"Sep", "sep"},
{"Sept", "sep"},
{"Oct", "oct"},
{"Nov", "nov"},
{"Dec", "dec"},
{(const char*)NULL, (const char*)NULL},
};
extern YESorNO align_equals; /* NO: left-adjust equals */
extern int at_level; /* @ nesting level */
extern int brace_level; /* curly brace nesting level */
extern YESorNO check_values; /* NO: suppress value checks */
extern int close_char; /* BibTeX entry closing; may */
/* be right paren or brace */
extern char current_entry_name[]; /* entry name */
extern char current_field[]; /* field name */
extern char current_key[]; /* string value */
extern char current_value[]; /* string value */
extern YESorNO delete_empty_values; /* YES: delete empty values */
extern YESorNO discard_next_comma; /* YES: deleting field/value */
extern YESorNO eofile; /* set to YES at end-of-file */
extern int field_indentation;
extern FILE *fpin; /* input file pointer */
extern YESorNO in_preamble; /* YES: in @Preamble{...} */
extern YESorNO in_string; /* YES: in @String{...} */
extern YESorNO in_value; /* YES: in value string */
extern YESorNO is_parbreak; /* get_next_non_blank() sets */
extern YESorNO keep_linebreaks; /* YES: keep linebreaks in values */
extern YESorNO keep_parbreaks; /* YES: keep parbreaks in values */
extern YESorNO keep_preamble_spaces; /* YES: keep spaces in @Preamble{} */
extern YESorNO keep_spaces; /* YES: keep spaces in values */
extern YESorNO keep_string_spaces; /* YES: keep spaces in @String{} */
extern int non_white_chars; /* used to test for legal @ */
extern YESorNO parbreaks; /* NO: parbreaks forbidden */
/* in strings and entries */
extern PATTERN_NAMES pattern_names[];
extern YESorNO prettyprint; /* NO: do lexical analysis */
extern char *program_name; /* set to argv[0] */
extern YESorNO print_patterns; /* YES: print value patterns */
extern YESorNO read_initialization_files;/* -[no-]read-init-files sets */
extern YESorNO remove_OPT_prefixes; /* YES: remove OPT prefix */
extern YESorNO rflag; /* YES: resynchronizing */
extern YESorNO Scribe; /* Scribe format input */
extern char shared_string[];
extern FILE *stdlog; /* usually stderr */
extern IO_PAIR token_start; /* used for # line output */
extern IO_PAIR the_entry; /* used in error messages */
extern IO_PAIR the_file; /* used in error messages */
extern IO_PAIR the_value; /* used in error messages */
extern int value_indentation;
extern YESorNO wrapping; /* NO: verbatim output */
void do_files ARGS((int argc_, char *argv_[]));
void do_other ARGS((void));
extern YESorNO apply_function ARGS((const char *option_,
OPTION_FUNCTION_ENTRY table_[]));
extern void check_length ARGS((size_t n_));
extern void check_key ARGS((void));
extern void check_chapter ARGS((void));
extern void check_CODEN ARGS((void));
extern void check_ISBN ARGS((void));
extern void check_ISSN ARGS((void));
extern void check_month ARGS((void));
extern void check_number ARGS((void));
extern void check_pages ARGS((void));
extern void check_other ARGS((void));
extern void check_volume ARGS((void));
extern void check_year ARGS((void));
extern void do_args ARGS((int argc_, char *argv_[]));
extern void do_initfile ARGS((const char *pathlist_,const char *name_));
extern void error ARGS((const char *msg_));
extern void fatal ARGS((const char *msg_));
extern char *findfile ARGS((const char *pathlist_, const char *name_));
extern void fix_month ARGS((void));
extern void fix_namelist ARGS((void));
extern void fix_pages ARGS((void));
extern void fix_title ARGS((void));
extern void free_pattern_table_entries ARGS((PATTERN_TABLE *pt_));
extern int get_char ARGS((void));
extern int get_linebreak ARGS((void));
extern int get_next_non_blank ARGS((void));
extern char *initialization_file_name;
extern int isidchar ARGS((int c_));
extern int isoptionprefix ARGS((int c));
extern void out_at ARGS((void));
extern void out_c ARGS((int c_));
extern void out_flush ARGS((void));
extern void out_newline ARGS((void));
extern void out_s ARGS((const char *s_));
extern void out_spaces ARGS((int n_));
extern void out_string ARGS((token_t type_, const char *token_));
extern void out_token ARGS((token_t type_, const char *token_));
extern void out_with_error ARGS((const char *s_,const char *msg_));
extern void out_with_parbreak_error ARGS((char *s_));
extern void put_back ARGS((int c_));
extern FILE *tfopen ARGS((const char *filename_, const char *mode_));
extern void warning ARGS((const char *msg_));
YESorNO German_style = NO; /* YES: " inside braced string
value obeys german.sty style */
static long space_count = 0L; /* count of spaces in do_optional_space() */
static char Scribe_close_delims[] = "}])>'\"`";
static char Scribe_open_delims[] = "{[(<'\"`";
static void add_one_pattern ARGS((PATTERN_TABLE *pt_,
const char *fieldname_, const char *pattern_,
const char *msg_));
static void add_pattern ARGS((const char *fieldname_, const char *pattern_,
const char *msg_));
static void append_value ARGS((const char *s_));
static void do_at ARGS((void));
static void do_BibTeX_entry ARGS((void));
static void do_BibTeX_value ARGS((void));
static void do_BibTeX_value_1 ARGS((void));
static void do_BibTeX_value_2 ARGS((void));
static void do_close_brace ARGS((void));
static void do_comma ARGS((void));
static void do_entry_name ARGS((void));
static void do_equals ARGS((void));
static void do_escapes ARGS((char *s_));
static void do_field ARGS((void));
static YESorNO do_field_value_pair ARGS((void));
static void do_fileinit ARGS((const char *bibfilename_));
static void do_group ARGS((void));
static void do_key_name ARGS((void));
static void do_new_pattern ARGS((char *s_));
static void do_newline ARGS((void));
static void do_one_file ARGS((FILE *fp_));
static void do_open_brace ARGS((void));
static void do_optional_inline_comment ARGS((void));
static void do_optional_space ARGS((void));
static void do_preamble ARGS((void));
static void do_preamble_2 ARGS((void));
static void do_Scribe_block_comment ARGS((void));
static void do_Scribe_close_delimiter ARGS((void));
static void do_Scribe_comment ARGS((void));
static void do_Scribe_entry ARGS((void));
static void do_Scribe_open_delimiter ARGS((void));
static void do_Scribe_separator ARGS((void));
static void do_Scribe_value ARGS((void));
static void do_single_arg ARGS((char *s_));
static void do_space ARGS((void));
static void do_string ARGS((void));
static void do_string_2 ARGS((void));
static void enlarge_table ARGS((PATTERN_TABLE *table_));
static void flush_inter_entry_space ARGS((void));
static char *get_braced_string ARGS((void));
static char *get_digit_string ARGS((void));
static char *get_identifier_string ARGS((void));
static char *get_inline_comment ARGS((void));
static char *get_line ARGS((FILE *fp_));
static char *get_optional_space ARGS((void));
static int get_parbreak ARGS((void));
static char *get_quoted_string ARGS((void));
static char *get_Scribe_delimited_string ARGS((void));
static char *get_Scribe_identifier_string ARGS((void));
static char *get_Scribe_string ARGS((void));
static char *get_simple_string ARGS((void));
static char *get_token ARGS((char *s_, char **nextp_,
const char *terminators_));
static void new_entry ARGS((void));
static void new_io_pair ARGS((IO_PAIR *pair_));
static void new_position ARGS((POSITION *position_));
static void out_close_brace ARGS((void));
static void out_comma ARGS((void));
static void out_complex_value ARGS((void));
static void out_equals ARGS((void));
static void out_field ARGS((void));
static void out_open_brace ARGS((void));
static void out_other ARGS((const char *s_));
static void out_value ARGS((void));
static void prt_pattern ARGS((const char *fieldname_, const char *pattern_,
const char *msg_));
static void put_back_string ARGS((const char *s_));
static void trim_value ARGS((void));
#if defined(HAVE_STDC)
static void
add_one_pattern(PATTERN_TABLE *pt, const char *fieldname, const char *pattern,
const char *message)
#else /* K&R style */
static void
add_one_pattern(pt,fieldname,pattern,message)
PATTERN_TABLE *pt;
const char *fieldname;
const char *pattern;
const char *message;
#endif
{
int m; /* index into pt->patterns[] */
if (STREQUAL(pattern,"")) /* then clear pattern table */
free_pattern_table_entries(pt);
else /* otherwise add new pattern */
{
if (pt->current_size == pt->maximum_size) /* then table full */
enlarge_table(pt);
for (m = 0; m < pt->current_size; ++m)
{
/* Make sure this is not a duplicate; if it is, and its message */
/* is the same, then we just ignore the request. Duplicates */
/* are possible when the user and system search paths overlap. */
if (STREQUAL(pattern,pt->patterns[m].pattern))
{ /* duplicate pattern found */
if (((pt->patterns[m].message) != (char*)NULL)
&& (message != (char*)NULL) &&
(STREQUAL(message,pt->patterns[m].message)))
return; /* messages duplicate too */
pt->patterns[m].message =
(message == (char*)NULL) ? message :
(const char*)Strdup(message);
/* replace message string */
prt_pattern(fieldname,pattern,message);
return;
}
}
/* We have a new and distinct pattern and message, so save them */
pt->patterns[pt->current_size].pattern = Strdup(pattern);
pt->patterns[pt->current_size++].message =
(message == (char*)NULL) ? message : (const char*)Strdup(message);
}
prt_pattern(fieldname,pattern,message);
}
#if defined(HAVE_STDC)
static void
add_pattern(const char *fieldname, const char *pattern, const char *message)
#else /* K&R style */
static void
add_pattern(fieldname,pattern,message)
const char *fieldname;
const char *pattern;
const char *message;
#endif
{
int k; /* index into pattern_names[] */
for (k = 0; pattern_names[k].name != (const char*)NULL; ++k)
{ /* find the correct pattern table */
if (stricmp(pattern_names[k].name,fieldname) == 0)
{ /* then found the required table */
add_one_pattern(pattern_names[k].table,fieldname,pattern,message);
return;
}
}
/* If we get here, then the pattern name is not in the built-in list,
so create a new entry in pattern_names[] if space remains */
if (k >= (int)((MAX_PATTERN_NAMES - 1)))
{ /* too many pattern types */
(void)fprintf(stdlog,
"%s Out of memory for pattern name [%s] -- pattern ignored\n",
WARNING_PREFIX, fieldname);
}
else
{ /* sufficient table space remains */
pattern_names[k].name = Strdup(fieldname); /* add new table entry */
pattern_names[k].table = (PATTERN_TABLE*)malloc(sizeof(PATTERN_TABLE));
if (pattern_names[k].table == (PATTERN_TABLE*)NULL)
fatal("Out of memory for pattern tables");
pattern_names[k].table->patterns = (MATCH_PATTERN*)NULL;
pattern_names[k].table->current_size = 0;
pattern_names[k].table->maximum_size = 0;
add_one_pattern(pattern_names[k].table,fieldname,pattern,message);
pattern_names[k+1].name = (char*)NULL; /* mark new end of table */
pattern_names[k+1].table = (PATTERN_TABLE*)NULL;
}
}
#if defined(HAVE_STDC)
static void
append_value(const char *s)
#else /* K&R style */
static void
append_value(s)
const char *s;
#endif
{
size_t n_cv = strlen(current_value);
size_t n_s = strlen(s);
if ((n_cv + n_s) < MAX_TOKEN)
(void)strcpy(¤t_value[n_cv],s);
else /* string too long; concatenate into parts */
{
out_s(current_value);
(void)strcpy(current_value,s);
out_with_error(" # ","Value too long for field ``%f''");
}
}
static void
do_at(VOID) /* parse @name{...} */
{
int c;
token_start = the_file; /* remember location of token start */
c = get_char();
the_entry = the_file;
if ((non_white_chars == 1) && (c == '@'))
{
at_level++;
out_at();
if (brace_level != 0)
{
error(
"@ begins line, but brace level is not zero after entry ``@%e{%k,''");
brace_level = 0;
}
}
else if (c != EOF)
{
out_c(c);
out_with_error("", "Expected @name{...} after entry ``@%e{%k,''");
}
}
static void
do_BibTeX_entry(VOID)
{
/*************************************************************
Parse a BibTeX entry, one of:
@entry-name{key,field=value,field=value,...,}
@string{name=value}
@preamble{...}
*************************************************************/
new_entry();
do_at();
if ((rflag == YES) || (eofile == YES)) return;
do_optional_space();
if (prettyprint == YES)
out_c(DELETE_WHITESPACE); /* discard any space that we found */
do_entry_name();
if (rflag == YES) return;
if (STREQUAL(current_entry_name,"Include"))
do_group();
else if (STREQUAL(current_entry_name,"Preamble"))
do_preamble();
else if (STREQUAL(current_entry_name,"String"))
do_string();
else /* expect @name{key, field = value, ... } */
{
do_optional_space();
if (prettyprint == YES)
out_c(DELETE_WHITESPACE); /* discard any space that we found */
do_open_brace();
if (rflag == YES) return;
do_optional_space();
do_key_name();
if (rflag == YES) return;
do_optional_space();
do_comma();
if (rflag == YES) return;
do_optional_space();
while (do_field_value_pair() == YES)
{
do_optional_space();
do_comma(); /* this supplies any missing optional comma */
if ((rflag == YES) || (eofile == YES)) return;
do_optional_space();
}
if (rflag == YES) return;
do_optional_space();
do_close_brace();
}
flush_inter_entry_space();
}
/***********************************************************************
BibTeX field values can take several forms, as illustrated by this
simple BNF grammar:
BibTeX-value-string:
simple-string |
simple-string # BibTeX-value-string
simple-string:
"quoted string" |
{braced-string} |
digit-sequence |
alpha-sequence |
***********************************************************************/
static void
do_BibTeX_value(VOID) /* process BibTeX value string */
{
if (prettyprint == YES)
do_BibTeX_value_1();
else
do_BibTeX_value_2();
}
static void
do_BibTeX_value_1(VOID) /* process BibTeX value string */
{ /* for prettyprinted output */
/* In order to support string value checking, we need to collect
complete values, including intervening inline comments, which
we bracket by magic delimiters so they can be ignored during
pattern matching, and restored on output. Space between
values is simply discarded. */
int c;
the_value = the_file;
current_value[0] = '\0';
append_value(get_simple_string());
do_optional_inline_comment();
while ((c = get_char()) == '#')
{
if (KEEP_PREAMBLE_SPACES())
append_value("#");
else if (KEEP_STRING_SPACES())
append_value("#");
else
append_value(" # ");
do_optional_inline_comment();
append_value(get_simple_string());
do_optional_inline_comment();
}
put_back(c);
out_value();
}
static void
do_BibTeX_value_2(VOID) /* process BibTeX value string */
{ /* for lexical analysis output */
int c;
the_value = the_file;
(void)strcpy(current_value,get_simple_string());
out_string((current_value[0] == '"') ? TOKEN_VALUE : TOKEN_ABBREV,
current_value);
do_optional_space();
while ((c = get_char()) == '#')
{
out_string(TOKEN_SPACE," ");
out_string(TOKEN_SHARP,"#");
out_string(TOKEN_SPACE," ");
do_optional_space();
(void)strcpy(current_value,get_simple_string());
out_string((current_value[0] == '"') ? TOKEN_VALUE : TOKEN_ABBREV,
current_value);
do_optional_space();
}
put_back(c);
}
static void
do_close_brace(VOID) /* parse level 1 closing brace or parenthesis */
{
int c;
c = get_char();
if (c == EOF)
return;
else if (c == close_char)
{
if (c == ')')
brace_level--; /* get_char() could not do this for us */
out_close_brace(); /* standardize parenthesis to brace */
if (brace_level != 0)
out_with_error("",
"Non-zero brace level after @name{...} processed. Last key = ``%k''");
}
else /* raise error and try to resynchronize */
{
out_c(c);
out_with_error("",
"Expected closing brace or parenthesis in entry ``@%e{%k,''");
}
}
static void
do_comma(VOID)
{
int c;
/* Parse a comma, or an optional comma before a closing brace or
parenthesis; an omitted legal comma is supplied explicitly.
A newline is output after the comma so that field = value
pairs appear on separate lines. */
the_value = the_file;
c = get_char();
if (c == EOF)
NOOP;
else if (c == ',')
{
if (discard_next_comma == NO)
{
out_comma();
out_newline();
}
}
else if (c == close_char)
{ /* supply missing comma for last field = value pair*/
if (c == ')')
brace_level--; /* get_char() could not do this for us */
if (brace_level == 0) /* reached end of bibliography entry */
{
if (c == ')')
brace_level++; /* put_back() could not do this for us */
put_back(c);
if (discard_next_comma == NO)
{
out_comma();
out_newline();
}
}
else /* no comma, and still in bibliography entry */
{
out_c(c);
out_with_error("","Non-zero brace level after @name{...} \
processed. Last entry = ``@%e{%k,''");
}
}
else /* raise error and try to resynchronize */
{
out_c(c);
out_with_error("", "Expected comma after last field ``%f''");
}
discard_next_comma = NO;
}
static void
do_entry_name(VOID) /* process BibTeX entry name */
{
int c;
size_t k;
int n;
static NAME_PAIR entry_pair[] =
{ /* entry name case change table */
{ "Deathesis", "DEAthesis" },
{ "Inbook", "InBook" },
{ "Incollection", "InCollection" },
{ "Inproceedings", "InProceedings" },
{ "Mastersthesis", "MastersThesis" },
{ "Phdthesis", "PhdThesis" },
{ "Techreport", "TechReport" },
};
token_start = the_file; /* remember location of token start */
for (k = 0; ((c = get_char()) != EOF) && isidchar(c); ++k)
{ /* store capitalized entry name */
if ((k == 0) && !Isalpha(c))
error("Non-alphabetic character begins an entry name");
if ((k == 0) && Islower(c))
c = toupper(c);
else if ((k > 0) && Isupper(c))
c = tolower(c);
if ((parbreaks == NO) && (is_parbreak == YES))
{
APPEND_CHAR(current_entry_name,k,c);
out_with_parbreak_error(current_entry_name);
return;
}
if (k >= MAX_TOKEN)
{
APPEND_CHAR(current_entry_name,k,c);
out_with_error(current_entry_name, "@entry_name too long");
return;
}
current_entry_name[k] = (char)c;
}
current_entry_name[k] = (char)'\0';
if (c != EOF)
put_back(c);
/* Substitute a few entry names that look better in upper case */
for (n = 0; n < (int)(sizeof(entry_pair)/sizeof(entry_pair[0])); ++n)
if (STREQUAL(current_entry_name,entry_pair[n].old_name))
(void)strcpy(current_entry_name,entry_pair[n].new_name);
if (prettyprint == YES)
out_s(current_entry_name);
else if (STREQUAL(current_entry_name,"Include"))
out_token(TOKEN_INCLUDE, current_entry_name);
else if (STREQUAL(current_entry_name,"Preamble"))
out_token(TOKEN_PREAMBLE, current_entry_name);
else if (STREQUAL(current_entry_name,"String"))
out_token(TOKEN_STRING, current_entry_name);
else
out_token(TOKEN_ENTRY, current_entry_name);
check_length(k);
}
static void
do_equals(VOID) /* process = in field = value */
{
int c;
the_value = the_file;
token_start = the_file; /* remember location of token start */
c = get_char();
if (c == EOF)
NOOP;
else if (c == '=')
out_equals();
else
{
out_c(c);
out_with_error("", "Expected \"=\" after field ``%f''");
}
out_spaces((int)(value_indentation - the_file.output.column_position));
/* supply leading indentation */
}
#if defined(HAVE_STDC)
static void
do_escapes(char *s)
#else /* K&R style */
static void
do_escapes(s)
char *s;
#endif
{ /* reduce escape sequences in s[] */
int base; /* number base for strtol() */
char *endptr; /* pointer returned by strtol() */
char *p; /* pointer into output s[] */
if (s == (char*)NULL) /* nothing to do if no string */
return;
for (p = s ; *s ; ++s)
{
if (*s == '\\') /* have escaped character */
{
base = 8; /* base is tentatively octal */
switch (*++s)
{
case 'a': *p++ = CTL('G'); break;
case 'b': *p++ = CTL('H'); break;
case 'f': *p++ = CTL('L'); break;
case 'n': *p++ = CTL('J'); break;
case 'r': *p++ = CTL('M'); break;
case 't': *p++ = CTL('I'); break;
case 'v': *p++ = CTL('K'); break;
case '0':
if (TOLOWER(s[1]) == 'x') /* 0x means hexadecimal */
base = 16;
/* FALL THROUGH */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
*p++ = (char)strtol((const char*)s,&endptr,base);
s = endptr - 1; /* point to last used character */
break;
default: /* \x becomes x for all other x */
*p++ = *s;
break;
}
}
else /* not escaped, so just copy it */
*p++ = *s;
}
*p = '\0'; /* terminate final string */
}
#if defined(HAVE_STDC)
static void
do_fileinit(const char *bibfilename) /* process one initialization file */
#else /* K&R style */
static void
do_fileinit(bibfilename) /* process one initialization file */
const char *bibfilename;
#endif
{
char *p;
const char *ext;
ext = GETDEFAULT(BIBCLEAN_EXT,INITFILE_EXT);
if (strrchr(bibfilename,'.') != (char*)NULL) /* then have file extension */
{ /* convert foo.bib to foo.ini and then process it as an init file */
if ((p = (char*)malloc(strlen(bibfilename) + strlen(ext) + 1))
!= (char*)NULL)
{
(void)strcpy(p,bibfilename);
(void)strcpy(strrchr(p,'.'),ext);
do_initfile((char*)NULL,p);
FREE(p);
}
}
}
static void
do_field(VOID) /* process BibTeX field name */
{
int c;
size_t k;
int n;
static NAME_PAIR field_pair[] =
{ /* field name case change table */
{ "ansi-standard-number", "ANSI-standard-number" },
{ "coden", "CODEN" },
/* 5- and 6-letter standard journal identifier */
{ "ieee-standard-number", "IEEE-standard-number" },
{ "isbn", "ISBN" },
/* International Standard Book Number */
{ "iso-standard-number", "ISO-standard-number" },
{ "issn", "ISSN" },
/* International Standard Serials Number */
{ "lccn", "LCCN" },
/* Library of Congress catalog number */
{ "mrclass", "MRclass" }, /* Math Reviews class */
{ "mrnumber", "MRnumber" }, /* Math Reviews number */
{ "uri", "URI" },
/* WWW: Uniform Resource Identifier */
{ "url", "URL" },
/* WWW: Uniform Resource Locator */
{ "urn", "URN" },
/* WWW: Uniform Resource Name */
{ "xxansi-standard-number", "xxANSI-standard-number" },
{ "xxcoden", "xxCODEN" },
{ "xxieee-standard-number", "xxIEEE-standard-number" },
{ "xxisbn", "xxISBN" },
{ "xxiso-standard-number", "xxISO-standard-number" },
{ "xxissn", "xxISSN" },
{ "xxmrclass", "xxMRclass" },
{ "xxmrnumber", "xxMRnumber" },
{ "xxuri", "xxURI" },
{ "xxuri", "xxURI" },
{ "xxurl", "xxURL" },
{ "xxurn", "xxURN" },
};
the_value = the_file;
token_start = the_file; /* remember location of token start */
for (k = 0, c = get_char(); (c != EOF) && isidchar(c);
c = get_char(), k++)
{
if (k >= MAX_TOKEN)
{
APPEND_CHAR(current_field,k,c);
out_with_error(current_field, "Entry field name too long");
return;
}
else if ((k == 0) && !Isalpha(c))
error("Non-alphabetic character begins a field name");
current_field[k] = (char)(((in_string == NO) && Isupper(c))
? tolower(c) : c);
}
if (c != EOF)
put_back(c);
current_field[k] = (char)'\0';
if (in_string == NO) /* @String{...} contents untouched */
{
/* Substitute a few field names that look better in upper case */
for (n = 0; n < (int)(sizeof(field_pair)/sizeof(field_pair[0])); ++n)
{
if (STREQUAL(current_field,field_pair[n].old_name))
{
(void)strcpy(current_field,field_pair[n].new_name);
break;
}
}
if (strncmp("opt",current_field,3) == 0)
{ /* Emacs bibtex.el expects OPT */
(void)strncpy(current_field,"OPT",3);
}
}
if (k > 0)
out_field();
check_length(k);
}
static YESorNO
do_field_value_pair(VOID) /* process field = value pair */
{
if (eofile == YES) return (NO);
do_field();
if ((rflag == YES) || (eofile == YES) || (current_field[0] == '\0'))
return (NO);
space_count = 0L; /* examined in do_Scribe_separator() */
do_optional_space(); /* and set here */
if (Scribe == YES)
do_Scribe_separator();
else
do_equals();
if ((rflag == YES) || (eofile == YES)) return (NO);
do_optional_space();
if (Scribe == YES)
do_Scribe_value();
else
do_BibTeX_value();
if ((rflag == YES) || (eofile == YES)) return (NO);
return (YES);
}
#if defined(HAVE_STDC)
void
do_files(int argc, char *argv[])
#else /* K&R style */
void
do_files(argc,argv)
int argc;
char *argv[];
#endif
{
FILE *fp;
int k = argc; /* index into argv[] */
/* set to argc to remove optimizer complaints about unused argument */
if (argv[1] == (char*)NULL) /* no files specified, so use stdin */
{
the_file.input.filename = "stdin";
do_one_file(stdin);
}
else /* else use command-line files left in argv[] */
{
for (k = 1; argv[k] != (char*)NULL; ++k)
{
if (STREQUAL(argv[k],"-"))
{
/* A filename of "-" is conventionally interpreted in
the UNIX world as a synonym for stdin, since that
system otherwise lacks true filenames for stdin,
stdout, and stdlog. We process stdin with
do_one_file(), but never close it so that subsequent
read attempts will silently, and harmlessly, fail
at end-of-file. */
the_file.input.filename = "stdin";
do_one_file(stdin);
}
else if ((fp = tfopen(argv[k], OPEN_MODE_R)) == (FILE*)NULL)
{
(void)fprintf(stdlog,
"\n%s Ignoring open failure on file [%s]\n",
ERROR_PREFIX, argv[k]);
perror("perror() says");
}
else /* open succeeded, so process file */
{
if (k > 1) /* supply blank line between */
out_newline(); /* entries at file boundaries */
the_file.input.filename = argv[k];
if (read_initialization_files == YES)
do_fileinit(the_file.input.filename);
do_one_file(fp);
(void)fclose(fp); /* close to save file resources */
}
}
}
}
static void
do_group(VOID) /* copy a braced group verbatim */
{
int c;
char *s = shared_string; /* memory-saving device */
size_t k; /* index into s[] */
do_optional_space();
if (prettyprint == YES)
{
out_c(DELETE_WHITESPACE); /* discard any space that we found */
do_open_brace();
if (rflag == YES) return;
out_c(DELETE_WHITESPACE); /* discard any space that we found */
while ((c = get_char()) != EOF)
{
if ((brace_level == 1) && (close_char == ')') && (c == close_char))
{ /* end of @entry(...) */
brace_level = 0;
c = '}';
}
if ((non_white_chars == 1) && (c == '@'))
error("@ begins line, but brace level is not zero after \
entry ``@%e{%k,''");
if ((brace_level == 0) && (c == '}'))
{
out_c(DELETE_WHITESPACE); /* discard any space that we found */
out_close_brace();
}
else
out_c(c);
if (brace_level == 0)
break;
}
}
else /* prettyprint == NO */
{ /* output entire braced group as one literal*/
token_start = the_file; /* remember location of token start */
c = get_char();
if (c == '{')
close_char = '}';
else if (c == '(')
{
close_char = ')';
brace_level++; /* get_char() could not do this for us */
}
else /* raise error and try to resynchronize */
{
s[0] = (char)c;
s[1] = '\0';
out_token(TOKEN_LITERAL,s);
error(
"Expected open brace or parenthesis. Last entry = ``@%e{%k,''");
return;
}
s[0] = '{'; /* standardize to outer braces */
for (k = 1; c != EOF;)
{
c = get_char();
if (k >= MAX_TOKEN)
{
error("Braced literal string too long for entry ``%e''");
s[k] = '\0';
out_token(TOKEN_LITERAL, s);
return;
}
s[k++] = (char)c;
if ((c == close_char) && (c == ')'))
brace_level--; /* get_char() could not do this for us */
if (brace_level == 0)
break; /* here's the normal loop exit */
}
s[k-1] = '}'; /* standardize to outer braces */
s[k] = '\0'; /* terminate string */
out_token(TOKEN_LITERAL, s);
}
}
#if defined(HAVE_STDC)
void
do_initfile(const char *pathlist, const char *name)
#else /* K&R style */
void
do_initfile(pathlist,name)
const char *pathlist;
const char *name;
#endif
{
FILE *fp;
char *p;
if ((initialization_file_name = findfile(pathlist,name)) == (char*)NULL)
return; /* silently ignore missing files */
if ((fp = tfopen(initialization_file_name,"r")) == (FILE*)NULL)
return; /* silently ignore missing files */
while ((p = get_line(fp)) != (char *)NULL)
{ /* process init file lines */
SKIP_SPACE(p);
if (isoptionprefix(*p))
do_single_arg(p); /* then expect -option [value] */
else
do_new_pattern(p); /* else expect field = "value" */
}
(void)fclose(fp);
}
static void
do_key_name(VOID) /* process BibTeX citation key */
{
int c;
size_t k;
token_start = the_file; /* remember location of token start */
for (k = 0, c = get_char(); (c != EOF) && (c != ',') && !Isspace(c);
c = get_char(), k++)
{
if (k >= MAX_TOKEN)
{
APPEND_CHAR(current_key,k,c);
out_with_error(current_key, "Citation key too long");
return;
}
current_key[k] = (char)c;
}
current_key[k] = (char)'\0';
if (c != EOF)
put_back(c);
if (check_values == YES)
check_key();
out_string(TOKEN_KEY, current_key);
check_length(k);
}
#if defined(HAVE_STDC)
static void
do_new_pattern(char *s)
#else /* K&R style */
static void
do_new_pattern(s)
char *s;
#endif
{
char *field;
char *p = s;
YESorNO saw_space;
char *value;
/*******************************************************************
We expect s[] to contain
field = "value"
field : "value"
field "value"
field = "value" "message"
field : "value" "message"
field "value" "message"
Empty lines are silently ignored.
*******************************************************************/
field = get_token(p,&p,"=: \t\v\f");
if (field == (char*)NULL)
return; /* then we have an empty line */
if (p != (char*)NULL) /* then we have more text */
{
saw_space = Isspace(*p) ? YES : NO;
SKIP_SPACE(p);
if ((saw_space == YES) || isfieldvalueseparator(*p))
{
if (isfieldvalueseparator(*p))
++p; /* then move past separator */
SKIP_SPACE(p);
if (*p == '"') /* then have quoted value */
{
value = get_token(p,&p," \t\v\f");
if (value != (char*)NULL)
{
SKIP_SPACE(p);
if (*p == '"') /* then have quoted message */
{
add_pattern(field,value,get_token(p,&p," \t\v\f"));
return;
}
else if ((*p == '\0') || (*p == COMMENT_PREFIX))
{ /* have end of string s[] */
add_pattern(field,value,(char*)NULL);
return;
}
}
}
}
}
(void)fprintf(stdlog,"%s Bad line [%s] in initialization file [%s]\n",
ERROR_PREFIX, s, initialization_file_name);
exit(EXIT_FAILURE);
}
static void
do_newline(VOID)
{
int c;
/* Newlines are standardized by bibclean inside bibliographic entries, */
/* so we only output a newline here if we are outside such an entry. */
c = get_char();
if (c == '\n')
{
if (brace_level == 0)
out_newline();
else if (KEEP_PREAMBLE_SPACES())
out_newline();
else if (KEEP_STRING_SPACES())
out_newline();
}
else
put_back(c);
}
#if defined(HAVE_STDC)
static void
do_one_file(FILE *fp) /* process one input file on fp */
#else /* K&R style */
static void
do_one_file(fp) /* process one input file on fp */
FILE *fp;
#endif
{
fpin = fp; /* save file pointer globally for get_char() */
new_io_pair(&the_file);
eofile = NO;
new_entry();
while (eofile == NO)
{
do_optional_space();
do_other();
if (Scribe == YES)
do_Scribe_entry();
else
do_BibTeX_entry();
}
if (prettyprint == YES)
{
out_c(DELETE_WHITESPACE); /* discard all trailing space */
out_c('\n'); /* supply final newline */
}
out_flush(); /* flush all buffered output */
if (brace_level != 0)
error("Non-zero brace level at end-of-file");
}
static void
do_open_brace(VOID) /* process open brace or parenthesis */
{
int c;
c = get_char();
if (c == EOF)
return;
else if (c == '{')
{
close_char = '}';
out_open_brace();
}
else if (c == '(')
{
close_char = ')';
brace_level++; /* get_char() could not do this for us */
out_open_brace(); /* standardize parenthesis to brace */
}
else /* raise error and try to resynchronize */
{
out_c(c);
out_with_error("",
"Expected open brace or parenthesis. Last entry = ``@%e{%k,''");
}
}
static void
do_optional_inline_comment(VOID)
{
size_t n;
char *s;
for (;;)
{
s = get_optional_space();
switch ((int)s[0])
{
case BIBTEX_COMMENT_PREFIX:
n = strlen(s);
Memmove(s+1,s,n);
s[0] = BIBTEX_HIDDEN_DELIMITER;
s[n+1] = BIBTEX_HIDDEN_DELIMITER;
s[n+2] = '\0';
append_value(s);
break;
case '\n': /* newline or */
case ' ': /* horizontal space token */
case '\f':
case '\r':
case '\t':
case '\v':
if (KEEP_PREAMBLE_SPACES())
append_value(s);
else if (KEEP_STRING_SPACES())
append_value(s);
break; /* else discard white space */
default: /* no more space or inline comments */
return; /* here's the loop exit */
}
}
}
static void
do_optional_space(VOID)
{ /* skip over optional horizontal space, newline, and in-line comments */
YESorNO save_wrapping;
char *s;
for (;;)
{
s = get_optional_space();
switch (s[0])
{
case '\n': /* newline token */
space_count++;
put_back((int)s[0]);
do_newline();
break;
case ' ': /* horizontal space token */
case '\f':
case '\r':
case '\t':
case '\v':
space_count++;
put_back((int)s[0]);
do_space();
break;
case BIBTEX_COMMENT_PREFIX: /* in-line comment token */
save_wrapping = wrapping;
wrapping = NO; /* inline comments are never line wrapped */
out_string(TOKEN_INLINE,s);
wrapping = save_wrapping;
break;
default: /* not optional space */
return; /* here's the loop exit */
}
}
}
void
do_other(VOID) /* copy non-BibTeX text verbatim */
{
int c; /* current input character */
size_t k; /* index into s[] */
YESorNO save_wrapping;
char *s = shared_string; /* memory-saving device */
save_wrapping = wrapping;
wrapping = NO;
/* For the purposes of lexical analysis (-no-prettyprint), we
collect complete lines, rather than single characters. */
for (k = 0, s[0] = (char)'\0'; (c = get_char()) != EOF; )
{
if ((c == '@') && (non_white_chars == 1))
{ /* new entry found */
put_back(c);
break;
}
if (k >= MAX_TOKEN)
{ /* buffer full, empty it and start a new one */
APPEND_CHAR(s,k,c);
out_other(s);
k = 0;
}
else if (c == '\n') /* end of line */
{
s[k] = (char)'\0';
out_other(s); /* output line contents */
out_newline(); /* and then separate newline token */
k = 0;
}
else if (Isspace(s[0])) /* then collecting whitespace */
{
if (Isspace(c)) /* still collecting whitespace */
s[k++] = (char)c;
else /* end of whitespace */
{
s[k] = (char)'\0';
out_other(s); /* output whitespace token */
k = 0;
s[k++] = (char)c; /* and start new one */
}
}
else
s[k++] = (char)c;
}
s[k] = (char)'\0';
out_other(s);
wrapping = save_wrapping;
}
static void
do_preamble(VOID)
{
in_preamble = YES;
do_preamble_2();
in_preamble = NO;
}
static void
do_preamble_2(VOID)
{
do_optional_space();
if (prettyprint == YES)
out_c(DELETE_WHITESPACE); /* discard any space that we found */
do_open_brace();
if (rflag == YES) return;
do_optional_space();
do_BibTeX_value();
if (rflag == YES) return;
do_optional_space();
do_close_brace();
}
static void
do_Scribe_block_comment(VOID)
{
int b_level = 0; /* brace level */
int c;
int k;
char *p;
char s[3+1]; /* to hold "end" */
p = get_Scribe_string(); /* expect to get "comment" */
if (stricmp(p,"\"comment\"") == 0)
{ /* found start of @Begin{comment} */
for (k = 6; k > 0; --k)
out_c(DELETE_CHAR); /* delete "@Begin" from output */
/* that was output by do_entry_name() */
out_s("@Comment{"); /* convert to BibTeX `comment' */
while ((c = get_char()) != EOF)
{
switch (c)
{
case '@': /* lookahead for "@End" */
s[0] = (char)get_char();
s[1] = (char)get_char();
s[2] = (char)get_char();
s[3] = (char)'\0';
if (stricmp(s,"end") == 0)
{ /* then we have @End */
p = get_Scribe_string(); /* so get what follows */
if (stricmp(p,"\"Comment\"") == 0)
{
out_close_brace();/* found @End{comment}, so finish
conversion to @Comment{...} */
return; /* block comment conversion done! */
}
else /* false alarm, just stuff lookahead */
{ /* back into input stream */
put_back_string(p);
put_back_string(s);
}
}
else /* lookahead was NOT "@End" */
put_back_string(s);
break;
case '{':
b_level++;
break;
case '}':
if (b_level <= 0)
out_open_brace(); /* keep output braces balanced */
else
b_level--;
break;
} /* end switch(c) */
out_c(c); /* copy one comment character */
} /* end while ((c = ...)) */
}
else /* was not @Begin{comment} after all */
put_back_string(p);
}
static void
do_Scribe_close_delimiter(VOID)
{
int c;
static char fmt[] = "Expected Scribe close delimiter `%c' [8#%03o], but \
found `%c' [8#%03o] instead for field ``%%f''";
char msg[sizeof(fmt)];
c = get_char();
if ((parbreaks == NO) && (is_parbreak == YES))
{
APPEND_CHAR(msg,0,c);
out_with_parbreak_error(msg);
return;
}
if (c == EOF)
return;
else if (c == close_char)
{
if (c == ')')
brace_level--; /* get_char() could not do this for us */
out_close_brace(); /* standardize parenthesis to brace */
}
else /* raise error and try to resynchronize */
{
out_c(c);
(void)sprintf(msg, fmt, close_char, BYTE_VAL(close_char),
(int)(Isprint(c) ? c : '?'), BYTE_VAL(c));
out_with_error("", msg);
}
}
static void
do_Scribe_comment(VOID)
{
int c;
int b_level = 0; /* brace level */
/* BibTeX does not yet have a comment syntax, so we just output the
Scribe comment in braces, ensuring that internal braces are balanced. */
do_optional_space();
do_Scribe_open_delimiter(); /* this outputs an opening brace */
if (rflag == YES) return;
for (c = get_char(); (c != EOF) && (c != close_char); c = get_char())
{
if (c == '{')
b_level++;
else if (c == '}')
{
b_level--;
if (b_level < 0)
{
out_open_brace(); /* force matching internal braces */
b_level++;
}
}
out_c(c);
}
for (; b_level > 0; b_level--)
out_close_brace(); /* force matching internal braces */
out_close_brace();
}
static void
do_Scribe_entry(VOID)
{
/*************************************************************
Parse a Scribe entry, one of:
@entry-name{key,field=value,field=value,...,}
@string{name=value}
@comment{...}
@begin{comment}...@end{comment}
The = separator in field/value pairs may also be a space or
a slash.
Any of the seven Scribe delimiters can be used to surround
the value(s) following @name, and to surround values of
field value pairs.
*************************************************************/
int save_close_char;
new_entry();
do_at();
if ((rflag == YES) || (eofile == YES)) return;
do_optional_space();
if (prettyprint == YES)
out_c(DELETE_WHITESPACE); /* discard any space that we found */
do_entry_name();
if (rflag == YES) return;
if (STREQUAL(current_entry_name,"Comment"))
do_Scribe_comment();
else if (STREQUAL(current_entry_name,"Begin"))
do_Scribe_block_comment();
else if (STREQUAL(current_entry_name,"String"))
do_string();
else
{
do_optional_space();
if (prettyprint == YES)
out_c(DELETE_WHITESPACE); /* discard any space that we found */
do_Scribe_open_delimiter();
if (rflag == YES) return;
save_close_char = close_char;
brace_level = 1; /* get_char() cannot do this for us */
do_optional_space();
do_key_name();
if (rflag == YES) return;
do_optional_space();
do_comma();
if (rflag == YES) return;
do_optional_space();
while (do_field_value_pair() == YES)
{
do_optional_space();
do_comma(); /* this supplies any missing optional comma */
if ((rflag == YES) || (eofile == YES)) return;
do_optional_space();
}
if (rflag == YES) return;
do_optional_space();
close_char = save_close_char;
do_Scribe_close_delimiter();
}
flush_inter_entry_space();
}
static void
do_Scribe_open_delimiter(VOID) /* process open delimiter */
{
int c;
char *p;
c = get_char();
if (c == EOF)
return;
else
{
p = strchr(Scribe_open_delims,c);
if (p == (char*)NULL)
{
out_c(c);
out_with_error("",
"Expected Scribe open delimiter, one of { [ ( < ' \" ` for field ``%f''");
return;
}
close_char = Scribe_close_delims[(int)(p - Scribe_open_delims)];
out_open_brace(); /* standardize open delimiter to brace */
}
}
static void
do_Scribe_separator(VOID)
{
int c;
YESorNO saw_space = NO;
the_value = the_file;
saw_space = (space_count > 0L) ? YES : NO;
c = get_char();
if ((parbreaks == NO) && (is_parbreak == YES))
{
char msg[2];
APPEND_CHAR(msg,0,c);
out_with_parbreak_error(msg);
return;
}
if (c == EOF)
NOOP;
else if ((c == '=') || (c == '/'))
out_equals();
else if (saw_space == YES) /* have field value with no binary operator */
{
out_equals(); /* supply the missing = operator */
put_back(c); /* this is first character of value string */
}
else /* looks like run-together fieldvalue */
{
out_c(c);
out_with_error("",
"Expected Scribe separator \"=\", \"/\", or \" \" for field ``%f''");
}
out_spaces((int)(value_indentation - the_file.output.column_position));
/* supply leading indentation */
}
/***********************************************************************
Scribe field values can take several forms, as illustrated by this
simple BNF grammar:
Scribe-value-string:
<open-delimiter><not-open-or-close-delimiter>*<close-delimiter> |
<digit><letter-or-digit-or-dot>*
***********************************************************************/
static void
do_Scribe_value(VOID) /* process Scribe value string */
{
the_value = the_file;
(void)strcpy(current_value,get_Scribe_string());
if ((rflag == YES) || (eofile == YES))
out_s(current_value);
else
out_value();
}
#if defined(HAVE_STDC)
static void
do_single_arg(char *s)
#else /* K&R style */
static void
do_single_arg(s)
char *s;
#endif
{ /* expect -option or -option value */
char *temp_argv[4]; /* "program" "-option" "value" NULL */
int temp_argc; /* temporary argument count */
temp_argv[0] = program_name; /* 0th argument always program name */
temp_argv[1] = get_token(s,&s," \t\v\f"); /* option */
temp_argv[2] = get_token(s,&s," \t\v\f"); /* value */
temp_argv[3] = (char *)NULL;
temp_argc = (temp_argv[2] == (char*)NULL) ? 2 : 3;
do_args(temp_argc,temp_argv);
}
static void
do_space(VOID)
{
int c;
char *s = shared_string; /* memory-saving device */
size_t k; /* index into s[] */
token_start = the_file; /* remember location of token start */
c = get_char();
s[0] = '\0';
for (k = 0; (c != EOF) && Isspace(c) && (c != '\n'); )
{
if (k >= MAX_TOKEN)
{ /* split long comments into multiple ones */
s[k] = '\0';
if (prettyprint == NO)
out_token(TOKEN_SPACE,s);
else if (keep_spaces == YES)
out_s(s);
else if (KEEP_PREAMBLE_SPACES())
out_s(s);
else if (KEEP_STRING_SPACES())
out_s(s);
/* else discard: spaces are standardized during prettyprinting */
k = 0;
}
s[k++] = (char)c;
c = get_char();
}
s[k] = '\0'; /* terminate token string */
if (prettyprint == NO)
out_token(TOKEN_SPACE,s);
else if (keep_spaces == YES)
out_s(s);
else if (KEEP_PREAMBLE_SPACES())
out_s(s);
else if (KEEP_STRING_SPACES())
out_s(s);
/* else discard: spaces are standardized during prettyprinting */
put_back(c); /* restore lookahead */
}
static void
do_string(VOID) /* process @String{abbrev = "value"} */
{
in_string = YES;
do_string_2();
in_string = NO;
}
static void
do_string_2(VOID) /* process @String{abbrev = "value"} */
{
do_optional_space();
if (prettyprint == YES)
out_c(DELETE_WHITESPACE); /* discard any space that we found */
do { /* one trip loop */
do_open_brace();
if (rflag == YES) break;
do_optional_space();
if (do_field_value_pair() == NO)
break;
if (rflag == YES) break;
do_optional_space();
do_close_brace();
if (rflag == YES) break;
} while (0);
}
#if defined(HAVE_STDC)
static void
enlarge_table(PATTERN_TABLE *table)
#else /* K&R style */
static void
enlarge_table(table)
PATTERN_TABLE *table;
#endif
{
if (table->maximum_size == 0)
table->patterns = (MATCH_PATTERN*)malloc(sizeof(MATCH_PATTERN) *
TABLE_CHUNKS);
else
table->patterns = (MATCH_PATTERN*)realloc((char*)table->patterns,
sizeof(MATCH_PATTERN) * (table->maximum_size + TABLE_CHUNKS));
/* NB: Sun C++ requires (char*) cast */
if (table->patterns == (MATCH_PATTERN*)NULL)
fatal("Out of memory for pattern table space");
table->maximum_size += TABLE_CHUNKS;
}
static void
flush_inter_entry_space(VOID) /* standardize to 1 blank line between entries */
{
int c;
if (keep_spaces == YES)
return;
put_back((c = get_next_non_blank()));
if (c != EOF)
out_newline();
out_newline();
}
static char *
get_braced_string(VOID)
{
int b_level = 0; /* brace level */
int c; /* current input character */
size_t k; /* index into s[] */
size_t n; /* index into t[] */
char *s = shared_string; /* memory-saving device */
char t[MAX_TOKEN_SIZE]; /* working area for braced string */
in_value = YES;
for (c = get_char(), k = 0, *s = '\0'; c != EOF; )
{
if ((parbreaks == NO) && (is_parbreak == YES))
{
put_back(c); /* so this comes AFTER error message */
out_with_parbreak_error(s);
in_value = NO;
return (EMPTY_STRING(s));
}
else if (k >= MAX_TOKEN)
{
APPEND_CHAR(s,k,c);
out_with_error(s, "BibTeX string too long for field ``%f''");
in_value = NO;
return (EMPTY_STRING(s));
}
else
{
if (c == '\n')
c = get_parbreak();
else if (c == '\f')
{
if (keep_linebreaks == NO)
c = (keep_parbreaks == YES) ? PARBREAK : ' ';
}
else if (Isspace(c))
c = ' '; /* change whitespace to real space */
else if (c == '{')
b_level++;
else if (c == '}')
b_level--;
else if (c == '\\') /* \\<NEWLINE> -> \\<LINEBREAK> */
{
s[k++] = (char)c;
c = get_char();
if (c == '\\') /* found \\ */
{
s[k++] = (char)c;
c = get_linebreak();
}
else /* \<NON-BACKSLASH> -> \ */
{
put_back(c);
c = '\0';
}
}
if (c != '\0')
s[k++] = (char)c;
if (b_level == 0)
break; /* here's the loop exit */
c = Isspace(c) ? get_next_non_blank() : get_char();
}
}
s[k] = (char)'\0';
/* Now convert braced string to quoted string */
for (b_level = 0, k = 0, n = 0; s[k] && (n < (MAX_TOKEN_SIZE-2)); ++k)
{
if (s[k] == '{')
b_level++;
else if (s[k] == '}')
b_level--;
if ((s[k] == '"') && (b_level == 1)) /* k > 0 if this is true */
{ /* so we can omit that check */
if (German_style == YES)
{
switch (s[k+1])
{
case '"':
case '-':
case '<':
case '>':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case '\'':
case '`':
case 'a':
case 'c':
case 'e':
case 'f':
case 'i':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 's':
case 't':
case 'u':
case '|':
t[n++] = (char)'{';
t[n++] = (char)'"';
t[n++] = s[++k];
t[n++] = (char)'}';
break;
default: /* should never happen */
t[n++] = (char)'{';
t[n++] = (char)'"';
t[n++] = (char)'}';
warning(
"Unexpected quote usage in German-style braced string");
break;
}
}
else /* German_style == NO */
{
if ((s[k-1] == '\\') &&
(s[k+1] == '{') &&
((s[k+2] != '\0') && (s[k+2] != '{') && (s[k+2] != '}')) &&
(s[k+3] == '}')) /* change \"{x}y to {\"{x}}y */
{ /* BUG FIX for overlooked case: this branch added [08-Mar-1995] */
n--; /* now t[n] == '\\' */
k--; /* now s[k] == '\\' */
t[n++] = (char)'{';
t[n++] = s[k++]; /* copy 5-char string \"{x} */
t[n++] = s[k++];
t[n++] = s[k++];
t[n++] = s[k++];
t[n++] = s[k];
t[n++] = (char)'}';
}
else if (s[k-1] == '\\') /* change \"xy to {\"x}y */
{
n--; /* overwriting t[n] == '\\' */
t[n++] = (char)'{';
t[n++] = (char)'\\';
t[n++] = (char)'"';
t[n++] = s[++k];
t[n++] = (char)'}';
}
else /* change x" to x{"} */
{
t[n++] = (char)'{';
t[n++] = (char)'"';
t[n++] = (char)'}';
}
}
}
else
t[n++] = s[k];
}
t[0] = (char)'"'; /* change initial and final */
APPEND_CHAR(t,n-1,'"'); /* braces to quotes */
check_length(n);
if (c == EOF)
error("End-of-file in braced string");
in_value = NO;
return (strcpy(s,t));
}
static char *
get_digit_string(VOID)
{
int c; /* current input character */
size_t k; /* index into s[] */
char *s = shared_string; /* memory-saving device */
k = 0;
s[k++] = (char)'"'; /* convert to quoted string */
for (c = get_char(); (c != EOF) && Isdigit(c); )
{
if (k >= MAX_TOKEN)
{
APPEND_CHAR(s,k,c);
out_with_error(s, "BibTeX string too long for field ``%f''");
return (EMPTY_STRING(s));
}
else
{
s[k++] = (char)c;
c = get_char();
}
}
put_back(c); /* we read past end of digit string */
s[k++] = (char)'"'; /* supply terminating quote */
s[k] = (char)'\0';
check_length(k);
return (s);
}
static char *
get_identifier_string(VOID)
{
int c; /* current input character */
size_t k; /* index into s[] */
char *s = shared_string; /* memory-saving device */
for (c = get_char(), k = 0; (c != EOF) && isidchar(c); )
{
if (k >= MAX_TOKEN)
{
APPEND_CHAR(s,k,c);
out_with_error(s, "BibTeX string too long for field ``%f''");
return (EMPTY_STRING(s));
}
else
{
s[k++] = (char)c;
c = get_char();
}
}
put_back(c); /* we read past end of identifier string */
s[k] = (char)'\0';
check_length(k);
return (s);
}
static char *
get_inline_comment(VOID)
{
int c; /* current input character */
size_t k; /* index into s[] */
int newlines;
char *s = shared_string; /* memory-saving device */
s[0] = '\0';
c = get_char();
if ((Scribe == NO) && (c == BIBTEX_COMMENT_PREFIX))
{
token_start = the_file; /* remember location of token start */
for (s[0] = BIBTEX_COMMENT_PREFIX, c = get_char(),
k = 1, newlines = 0; (c != EOF); )
{ /* collect up to newline, plus following horizontal space */
if ((newlines == 1) && !Isspace(c))
break; /* here's a loop exit */
if (k >= MAX_TOKEN)
{ /* split long comments into multiple ones */
s[k++] = '\n';
put_back(c); /* restore lookahead */
c = BIBTEX_COMMENT_PREFIX; /* we put this back too later */
break; /* here's a loop exit */
}
if (c == '\n')
newlines++;
if (newlines > 1)
break; /* here's a loop exit */
s[k++] = (char)c;
c = get_char();
}
s[k] = '\0'; /* terminate token string */
}
put_back(c); /* restore lookahead */
return (s);
}
#if defined(HAVE_STDC)
static char *
get_line(FILE *fp)
#else /* K&R style */
static char *
get_line(fp)
FILE *fp;
#endif
{ /* return a complete line to the caller, discarding backslash-newlines */
/* on consecutive lines, and discarding the final newline. At EOF, */
/* return (char*)NULL instead. */
static char line[MAX_LINE];
static char *p;
static char *more;
more = &line[0];
line[0] = (char)'\0'; /* must set in case we hit EOF */
while (fgets(more,(int)(&line[MAX_LINE] - more),fp) != (char *)NULL)
{
p = strchr(more,'\n');
if (p != (char*)NULL) /* did we get a complete line? */
{ /* yes */
*p = '\0'; /* zap final newline */
if ((p > &line[0]) && (*(p-1) == '\\'))
/* then have backslash-newline */
more = p - 1; /* so get another line */
else /* otherwise have normal newline */
break; /* so return the current line */
}
else /* no, return partial line */
break;
}
return ((line[0] == '\0' && feof(fp)) ? (char*)NULL : &line[0]);
}
static char *
get_optional_space(VOID)
{
int c; /* current input character */
char *s = shared_string; /* memory-saving device */
/* Space tokens are returned as single-character values, because */
/* do_optional_space() pushes them back into the input stream before */
/* calling do_newline() and do_space() for further processing. However, */
/* inline comments are returned as multiple-character values */
c = get_char();
switch (c)
{
case '\n': /* newline token */
case ' ': /* horizontal space token */
case '\f':
case '\r':
case '\t':
case '\v':
s[0] = (char)c;
s[1] = '\0';
break;
case BIBTEX_COMMENT_PREFIX: /* in-line comment token */
put_back(c);
s = get_inline_comment();
break;
default: /* not optional space */
put_back(c);
s[0] = '\0';
break;
}
return (s);
}
static int
get_parbreak(VOID)
{
/* This function is called when the current input character is a
newline. If keep_linebreaks is YES, it returns a LINEBREAK. If
keep_parbreaks is NO, then it returns a space. Otherwise, it
looks ahead to the next non-blank character, and if a paragraph
break was seen, the return value is a PARBREAK, and otherwise,
a space, or a newline if keep_linebreaks is YES. */
if ((in_value == YES) && (keep_linebreaks == YES))
return (LINEBREAK);
else if (KEEP_PREAMBLE_SPACES())
return (LINEBREAK);
else if (KEEP_STRING_SPACES())
return (LINEBREAK);
else if ((in_value == YES) && (keep_parbreaks == YES))
{
put_back('\n'); /* so get_next_non_blank() finds it */
put_back(get_next_non_blank());
return ((is_parbreak == YES) ? PARBREAK :
((keep_linebreaks == YES) ? (int)'\n' : (int)' '));
}
else
return ((int)' ');
}
static char *
get_quoted_string(VOID)
{
int b_level = 0; /* brace level */
int c; /* current input character */
size_t k; /* index into s[] */
char *s = shared_string; /* memory-saving device */
in_value = YES;
for (c = get_char(), k = 0, *s = '\0'; c != EOF; )
{
if ((parbreaks == NO) && (is_parbreak == YES))
{
put_back(c); /* so this comes AFTER error message */
out_with_parbreak_error(s);
in_value = NO;
return (EMPTY_STRING(s));
}
else if (k >= MAX_TOKEN)
{
APPEND_CHAR(s,k,c);
out_with_error(s, "BibTeX string too long for field ``%f''");
in_value = NO;
return (EMPTY_STRING(s));
}
else
{
if (c == '\n')
c = get_parbreak();
else if (c == '\f')
{
if (keep_linebreaks == NO)
c = (keep_parbreaks == YES) ? PARBREAK : ' ';
}
else if (Isspace(c))
c = ' '; /* change whitespace to real space */
else if (c == '{')
b_level++;
else if (c == '}')
b_level--;
else if (c == '\\') /* \\<NEWLINE> -> \\<LINEBREAK> */
{
s[k++] = (char)c;
c = get_char();
if (c == '\\') /* found \\ */
{
s[k++] = (char)c;
c = get_linebreak();
}
else /* \<NON-BACKSLASH> -> \ */
{
put_back(c);
c = '\0';
}
}
if (c != '\0')
s[k++] = (char)c;
if ((c == '"') && (k > 1) && (b_level == 0))
{
if (s[k-2] == '\\')
{
/* convert \"x inside string at brace-level 0 to {\"x}: */
/* illegal, but hand-entered bibliographies have it */
c = get_char();
if (c != EOF)
{
k = k - 2;
s[k++] = (char)'{';
s[k++] = (char)'\\';
s[k++] = (char)'"';
s[k++] = (char)c;
s[k++] = (char)'}';
}
}
else
break; /* here's the loop exit */
}
c = Isspace(c) ? get_next_non_blank() : get_char();
}
}
s[k] = (char)'\0';
check_length(k);
if (c == EOF)
error("End-of-file in quoted string");
in_value = NO;
return (s);
}
static char *
get_Scribe_delimited_string(VOID)
{
int c;
int close_delim;
size_t k;
int last_c = EOF;
char *p;
char *s = shared_string; /* memory-saving device */
c = get_char();
p = strchr(Scribe_open_delims,c); /* maybe delimited string? */
if (p == (char*)NULL)
{
APPEND_CHAR(s,0,c);
out_with_error(s,"Expected Scribe value string for field ``%f''");
return (EMPTY_STRING(s));
}
/* We have a delimited string */
close_delim = Scribe_close_delims[(int)(p - Scribe_open_delims)];
c = get_next_non_blank(); /* get first character in string */
/* ignoring leading space */
for (k = 0, s[k++] = (char)'"';
(c != EOF) &&
!((last_c != '\\') && (c == close_delim)) &&
(k < MAX_TOKEN);
k++)
{
if ((parbreaks == NO) && (is_parbreak == YES))
{
s[k] = '\0'; /* ensure string termination */
put_back(c); /* so this comes AFTER error message */
out_with_parbreak_error(s);
return (EMPTY_STRING(s));
}
if (c == '"') /* protect quotes inside string */
{
if (s[k-1] == '\\')
{ /* then TeX accent in Scribe string */
last_c = c;
c = get_char();
if (c == '{') /* change \"{ to {\" */
{
s[k-1] = (char)'{';
s[k] = (char)'\\';
s[++k] = (char)'"';
}
else /* change \". to {\".} (. = any) */
{
s[k-1] = (char)'{';
s[k] = (char)'\\';
s[++k] = (char)'"';
s[++k] = (char)c;
s[++k] = (char)'}';
}
}
else
{
s[k] = (char)'{';
s[++k] = (char)'"';
s[++k] = (char)'}';
}
}
else if (c == '\n')
s[k] = (char)get_parbreak();
else if (c == '\f')
s[k] = (keep_linebreaks == YES) ? (char)'\f' :
((keep_parbreaks == YES) ? (char)PARBREAK : (char)' ');
else if (Isspace(c))
s[k] = (char)' '; /* change whitespace to real space */
else if (c == '\\') /* \\<NEWLINE> -> \\<LINEBREAK> */
{
s[k] = (char)c;
c = get_char();
if (c == '\\') /* found \\ */
{
s[++k] = (char)c;
c = get_linebreak();
if (c != '\0')
s[++k] = (char)c;
}
else /* \<NON-BACKSLASH> -> \ */
put_back(c);
}
else
s[k] = (char)c;
last_c = c;
c = Isspace(c) ? get_next_non_blank() : get_char();
}
APPEND_CHAR(s,k,'"'); /* append close delimiter */
if (k >= MAX_TOKEN)
{
out_with_error(s, "Scribe string too long for field ``%f''");
return (EMPTY_STRING(s));
}
check_length(k);
return (s);
}
static char *
get_Scribe_identifier_string(VOID) /* read undelimited identifier */
{ /* and return quoted equivalent */
int c;
size_t k;
char *s = shared_string; /* memory-saving device */
c = get_char();
for (k = 0, s[k++] = (char)'"'; isidchar(c) && (k < MAX_TOKEN);
k++, c = get_char())
{
s[k] = (char)c;
}
put_back(c); /* put back lookahead */
APPEND_CHAR(s,k,'"');
if (k >= MAX_TOKEN)
{
out_with_error(s, "Scribe number string too long for field ``%f''");
return (EMPTY_STRING(s));
}
++k;
check_length(k);
return (s);
}
static char *
get_Scribe_string(VOID) /* read Scribe string */
{
int c;
do_optional_space();
c = get_char(); /* peek ahead one character */
put_back(c);
return (isidchar(c) ?
get_Scribe_identifier_string() :
get_Scribe_delimited_string());
}
static char *
get_simple_string(VOID) /* read simple BibTeX string */
{
int c; /* current input character */
char *s = shared_string; /* memory-saving device */
*s = '\0';
c = get_next_non_blank(); /* peek ahead to next non-blank */
if (c == EOF)
return (EMPTY_STRING(s));
else if ((parbreaks == NO) && (is_parbreak == YES))
{
put_back(c); /* so this comes AFTER error message */
out_with_parbreak_error(s);
return (EMPTY_STRING(s));
}
put_back(c); /* put back lookahead */
token_start = the_file; /* remember location of string start */
if (c == '{')
return (get_braced_string());
else if (Isdigit(c))
return (get_digit_string());
else if (c == '"')
return (get_quoted_string());
else if (Isalpha(c))
return (get_identifier_string());
else
{
out_with_error("", "Expected BibTeX value string for field ``%f''");
return (EMPTY_STRING(s));
}
}
#if defined(HAVE_STDC)
static char *
get_token(char *s, char **nextp, const char *terminators)
#else /* K&R style */
static char *
get_token(s,nextp,terminators)
char *s;
char **nextp;
const char *terminators;
#endif
{
char *t = s;
char *token;
/*******************************************************************
Ignoring leading space, find the next token in s[], stopping at
end-of-string, or one of the characters in terminators[],
whichever comes first. Replace the terminating character in s[]
by a NUL. Set *nextp to point to the next character in s[], or to
(char*)NULL if end-of-string was reached. Return (char*)NULL if
no token was found, or else a pointer to its start in s[]. The
job is terminated with an error message if a syntax error is
detected.
Quoted strings are correctly recognized as valid tokens, and
returned with their surrounding quotes removed, and embedded
escape sequences expanded. The comment character is recognized
outside quoted strings, but not inside.
*******************************************************************/
if (t != (char*)NULL)
SKIP_SPACE(t);
if ((t == (char*)NULL) || (*t == '\0') || (*t == COMMENT_PREFIX))
{ /* initial sanity check */
t = (char*)NULL; /* save for *nextp later */
token = (char*)NULL;
}
else if (*t == '"') /* then collect quoted string */
{
token = ++t; /* drop leading quote */
for ( ; *t && (*t != '"'); ++t)
{ /* find ending quote */
/* step over escape sequences; it doesn't matter if we have */
/* \123, since we are only looking for the ending quote */
if (*t == '\\')
++t;
}
if (*t == '"') /* then found valid string */
{
*t++ = '\0'; /* terminate token */
do_escapes(token); /* and expand escape sequences */
}
else
{
(void)fprintf(stdlog,
"%s Bad line [%s] in initialization file [%s]\n",
ERROR_PREFIX, s, initialization_file_name);
exit(EXIT_FAILURE);
}
}
else /* else collect unquoted string */
{
for (token = t; *t && (*t != COMMENT_PREFIX) &&
(strchr(terminators,*t) == (char*)NULL); ++t)
NOOP; /* scan over token */
if ((*t == '\0') || (*t == COMMENT_PREFIX)) /* then hit end of s[] */
t = (char*)NULL; /* save for *nextp later */
else /* else still inside s[] */
*t++ = '\0'; /* terminate token */
}
*nextp = t; /* set continuation position */
return (token);
}
static void
new_entry(VOID) /* initialize for new BibTeX @name{...} */
{
at_level = 0;
brace_level = 0;
is_parbreak = NO;
non_white_chars = 0;
rflag = NO; /* already synchronized */
current_entry_name[0] = '\0'; /* empty current_xxx[] strings */
current_field[0] = '\0';
current_key[0] = '\0';
current_value[0] = '\0';
}
#if defined(HAVE_STDC)
static void
new_io_pair(IO_PAIR *pair)
#else /* K&R style */
static void
new_io_pair(pair)
IO_PAIR *pair;
#endif
{
new_position(&pair->input);
new_position(&pair->output);
}
#if defined(HAVE_STDC)
static void
new_position(POSITION *position)
#else /* K&R style */
static void
new_position(position)
POSITION *position;
#endif
{
position->byte_position = 0L;
position->last_column_position = 0L;
position->column_position = 0L;
position->line_number = 1L;
}
static void
out_close_brace(VOID)
{
out_string(TOKEN_RBRACE, "}");
}
static void
out_comma(VOID)
{
YESorNO save_wrapping;
save_wrapping = wrapping;
wrapping = NO;
out_string(TOKEN_COMMA, ",");
wrapping = save_wrapping;
}
static void
out_complex_value(VOID)
{
char *s;
char *p;
/* A complex value may contain concatenated simple strings with */
/* intervening inline comments delimited by BIBTEX_HIDDEN_DELIMITER. */
/* We split it apart and output separate tokens. */
for (s = ¤t_value[0]; *s; )
{
p = strchr(s,BIBTEX_HIDDEN_DELIMITER);
if (p == (char*)NULL)
{
out_string((*s == '"') ? TOKEN_VALUE : TOKEN_ABBREV,s);
check_length(strlen(s));
return;
}
*p = '\0';
out_string((*s == '"') ? TOKEN_VALUE : TOKEN_ABBREV,s);
check_length(strlen(s));
s = p + 1;
p = strchr(s,BIBTEX_HIDDEN_DELIMITER);
if (p == (char*)NULL) /* should never happen, but recover safely */
p = strchr(s,'\0'); /* if it does */
*p = '\0';
out_string(TOKEN_INLINE,s);
check_length(strlen(s));
s = p + 1;
}
}
static void
out_equals(VOID)
{
if (prettyprint == YES)
{
if (KEEP_PREAMBLE_SPACES())
out_c('=');
else if (KEEP_STRING_SPACES())
out_c('=');
else
{
out_c(' ');
if (align_equals == YES)
out_spaces((int)(value_indentation -
the_file.output.column_position - 2));
out_c('='); /* standardize to = */
out_c(' '); /* always surround = by spaces */
}
}
else
out_token(TOKEN_EQUALS,"=");
}
static void
out_field(VOID)
{
if (prettyprint == YES)
{
if (in_string == NO)
out_spaces(field_indentation);
out_s(current_field);
}
else
out_token((in_string == YES) ? TOKEN_ABBREV : TOKEN_FIELD,
current_field);
}
static void
out_open_brace(VOID)
{
out_string(TOKEN_LBRACE, "{");
}
#if defined(HAVE_STDC)
static void
out_other(const char *s) /* output a non-BibTeX string */
#else /* K&R style */
static void
out_other(s)
const char *s;
#endif
{
if (prettyprint == YES)
out_s(s);
else
{
if (Isspace(s[0])) /* do_other() guarantees whole token is whitespace */
out_token(TOKEN_SPACE, s);
else if (s[0] == BIBTEX_COMMENT_PREFIX)
out_token(TOKEN_INLINE, s);
else
out_token(TOKEN_LITERAL, s);
}
}
static void
out_value(VOID)
{
static OPTION_FUNCTION_ENTRY checks[] =
{
{"author", 6, check_other},
{"chapter", 7, check_chapter},
{"CODEN", 5, check_CODEN},
{"ISBN", 4, check_ISBN},
{"ISSN", 4, check_ISSN},
{"month", 5, check_month},
{"number", 6, check_number},
{"pages", 5, check_pages},
{"volume", 6, check_volume},
{"year", 4, check_year},
{(const char*)NULL, 0, (void (*)(VOID))NULL},
};
static OPTION_FUNCTION_ENTRY fixes[] =
{
{"author", 6, fix_namelist},
{"editor", 6, fix_namelist},
{"month", 5, fix_month},
{"pages", 5, fix_pages},
{"title", 5, fix_title},
{(const char*)NULL, 0, (void (*)(VOID))NULL},
};
trim_value();
if ((in_preamble == NO) && (in_string == NO))
{
(void)apply_function(current_field,fixes);
if ((check_values == YES) && !STREQUAL(current_value,"\"\""))
{
if (apply_function(current_field,checks) == NO)
check_other();
}
if ((remove_OPT_prefixes == YES) &&
(strncmp(current_field,"OPT",3) == 0) &&
(strlen(current_field) > (size_t)3) &&
(strlen(current_value) > (size_t)2)) /* 2, not 0: quotes are included! */
{
out_c(DELETE_LINE);
Memmove(current_field,¤t_field[3],
(size_t)(strlen(current_field)-3+1));
/* reduce "OPTname" to "name" */
out_field();
out_equals();
out_spaces((int)(value_indentation -
the_file.output.column_position));
}
else if ((delete_empty_values == YES) &&
(STREQUAL(current_value,"\"\"")))
{ /* 2, not 0, because quotes are included! */
out_c(DELETE_LINE);
discard_next_comma = YES;
return;
}
}
out_complex_value();
}
#if defined(HAVE_STDC)
static void
prt_pattern(const char *fieldname, const char *pattern, const char *message)
#else /* K&R style */
static void
prt_pattern(fieldname,pattern,message)
const char *fieldname;
const char *pattern;
const char *message;
#endif
{
if (print_patterns == YES)
{
if ((pattern == (const char*)NULL) || (*pattern == '\0'))
(void)fprintf(stdlog,
"\nfile=[%s] field=[%-12s] existing patterns discarded\n\n",
initialization_file_name, fieldname);
else if (message == (char*)NULL)
(void)fprintf(stdlog,
"file=[%s] field=[%-12s] pattern=[%s]\n",
initialization_file_name, fieldname, pattern);
else
(void)fprintf(stdlog,
"file=[%s] field=[%-12s] pattern=[%s] message[%s]\n",
initialization_file_name, fieldname, pattern, message);
}
}
#if defined(HAVE_STDC)
static void
put_back_string(const char *s) /* put string value back onto input stream */
#else /* K&R style */
static void
put_back_string(s) /* put string value back onto input stream */
const char *s;
#endif
{
const char *p;
for (p = strchr(s,'\0') - 1; p >= s; p--)
put_back(*p);
}
static void
trim_value(VOID)
{ /* trim leading and trailing space from current_value[] */
size_t k;
size_t n = strlen(current_value);
if ((current_value[0] == '"') && Isspace(current_value[1]))
{ /* then quoted string value with leading space*/
for (k = 1; (k < n) && Isspace(current_value[k]); ++k)
NOOP;
Memmove(¤t_value[1], ¤t_value[k], (size_t)(n + 1 - k));
/* copy includes trailing NULL */
n = strlen(current_value);
}
if (current_value[n-1] == '"')
{
for (k = n; (k > 1) && Isspace(current_value[k-2]); --k)
NOOP;
if (current_value[k-2] == '\\') /* maybe have \<space> or \\<space> */
{
if ((k > 2) && (current_value[k-3] != '\\'))
--k; /* discard final \<space> */
}
current_value[k-1] = (char)'"';
current_value[k] = (char)'\0';
}
}
|