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
|
/*
Copyright (C) 1992, 1993 Jeff Erickson
Copyright (C) 1993, 1994, 1995, 1998, 2000 Bill Jones
Copyright (C) 2000 Rafael Laboissiere
This file is part of biblook.
biblook is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
biblook is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with biblook; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* ================================================================= *\
bibindex -- a program to index bibtex files, used in conjunction
with biblook
This program was specifically developed for use with the
computational geometry bibliographic database, and assumes the
rules thereof. The database may be obtained by anonymous ftp
from cs.usask.ca in the file "pub/geometry/geombib.tar.Z".
Version 1.0 written by Jeff Erickson <jeff@ics.uci.edu>, 27 Mar 92
Version 2.0 written by Jeff Erickson <jeff@ics.uci.edu>, 17 Jun 92
This program is in the public domain. You may use it or modify
it to your heart's content, at your own risk. Bouquets, brickbats,
and bug fixes may be sent to Jeff Erickson, jeffe@cs.berkeley.edu.
%Make% gcc -O -o bibindex bibindex.c
Usage: bibindex bibfile [-i field ...]
-----------------------------------------------------------------
HOW IT WORKS:
The bibtex file is read field by field. The file offset beginning
each record and each record's citation key are recorded. A list of
words is extracted from each field. These words are placed into
tables, which remember which records contain them in their
respective fields. Once the file has been completely read, the
hash tables are compacted and sorted.
The hash tables are extensible, since we have to maintain one for
each possible field type, and static tables would be way too big.
Initially, each table holds 1K entries, and the tables are doubled
whenever they near full capacity. Each table entry is at least 24
bytes. If the resulting hash tables use too much memory, the
entries should be changed to pointers, allocated on the fly.
The entry lists associated with each word are implemented as
extensible arrays. Initially, each list holds eight entries. If a
new entry is inserted into a full list, the list is doubled first.
The index file has the following format (loosely):
version info
# entries
array of offsets into bib file -- one per entry
# field types (incl. "@string")
array of field names -- one per field type
array of -- one per field type
# words
array of -- one per word
word -- in alphabetical order
# locations
array of entry #s -- one per location [compressed]
# abbreviations
array of abbreviations -- in alphabetical order
array of offsets into bib file -- one per abbreviation
There are advantages and disadvantages of having multiple hash
tables instead of a single table. I am starting with the premise
that the lookup program should be very fast. Consequently, I can't
make it determine which fields contain a given word. Doing so
would require putting ALL of the entry-parsing code into the lookup
program. It would also mean potentially parsing a lot of
extraneous entries to find relatively common words in relatively
uncommon places (eg, "title edelsbrunner").
If there were a single word table, each word list would have to
include bitmasks to indicate the appropriate fields along with the
entry numbers. Assuming between 16 and 32 field types (the CG bib
uses about 24), this would triple the size of each entry. On the
average, each word occurs in less than two field types. The
bitmask approach would also require knowledge of the field names in
advance; the multiple table approach does not.
-----------------------------------------------------------------
VERSION HISTORY:
1.0 <jge> 3/26/92 Initial version completed
1.1 <jge> 3/27/92 Words restricted to letters only; special
rules added for apostrophes, so that words
like "didn't" and "O'Rourke" can be handled
correctly.
1.2 <jge> 3/30/92 Faster hash function; now compressing hash
tables before sorting them. Execution time on
the CG bib reduced by a factor of thirty-five.
1.3 <jge> 4/2/92 Toyed with the hash function some more, trying
to reduce collisions, with limited success.
1.4 <jge> 4/17/92 Added exit(0) at the end of main() [I thought
that was supposed to be automatic!]
2.0 <jge> 6/12/92 First major revision completed.
1. Major change in file format -- word tables for every
field instead of just author, title, and keywords.
2. Word hash tables made extensible.
3. Fixed bug in GetNextWord that would cause the function
to return inside nested braces.
4. Fixed bug in MungeField that would kill everything in an
entry after an abbreviation. Abbreviations now go into
the appropriate hash table with the other words.
5. Made GetNextWord consider numbers and math expressions
to be words.
6. Added WriteWord, resulting in 40% savings in disk space.
7. Added -i flag and black holes. Ignoring "oldlabel"
gives another 15% savings (6/92 version of CGbib).
2.1 <jge> 7/9/92 Minor bug fixes.
2.2 Nelson H. F. Beebe <beebe@math.utah.edu> 03-Oct-1992
Testing with >100K lines of .bib files led to these changes:
1. Add support for complete BibTeX keyword name syntax with
iskeychar() function.
2. Add support for trailing comma after last key = "value" entry.
3. Record input line number for display in error messages.
4. Record initial line number of each BibTeX entry for
display in error messages to better localize error.
5. Add test for word buffer overflow in MungeField() to prevent
run-time core dumps, and increase supported word size from
15 to 31 (a word-length histogram of a 116921-word dictionary
found words up to 28 characters long, with 1% longer than 15).
File version increased to 2 because of word size change.
6. Add typecasts in calls to qsort() and comparisons of
unsigned short with short, change main() from void to int,
remove variable from initializer of msg[2], and add void to
IndexBibFile() definition to allow compilation with C++ as
well as C for better compile-time checking.
7. In MungeEntry(), do an ungetc() after key name collection.
Otherwise, a key="value" pair without a blank before the =
will not be recognized because the = read on lookahead has
not been put back.
8. Revise table compression code in OutputTables() and
code in FreeTables() to avoid duplicate frees, which is
a fatal error on many systems, and specified to result
in undefined behavior by the 1989 ANSI/ISO C Standard.
9. Define bcopy() as a macro to invoke standard memcpy()
instead.
10. Include time.h, unistd.h, and malloc.h to get proper
function declarations for library routines needed here.
11. Add DEBUG_MALLOC support.
12. Change several char* types to const char*.
13. Correct some typographical errors in comment.
14. Write UNIX manual pages for bibindex and biblook.
15. Allow command-line to specify a filename with .bib extension.
16. Add help support to biblook.
17. Correct error in FreeTables() in biblook.c; inner loop
incremented i instead of j.
2.3 Bill Jones <jones@cs.usask.ca> 93/01/29
1. Declarations common to bibindex.c and biblook.c factored out
to new file biblook.h.
2. Index type of (signed) short overflows early; created typedef
Index_t, defined as unsigned short.
3. Changed hash tables to extend at 75% capacity rather than 50%.
2.4 Nelson H. F. Beebe <beebe@math.utah.edu> [01-Jun-1993]
1. Remove some mixed-mode arithmetic.
2. Increase MAXFIELDS from 64 to 127 to deal with TeX User Group
bibliography collection
3. Correct error in GetHashTable(); older versions got into an
infinite loop if MAXFIELDS field names were already stored, and
a new one was encountered.
2.5 Erik Schoenfelder <schoenfr@ibr.cs.tu-bs.de> [14-Aug-1993]
1. Add support for network byte order I/O, so that index files
can be shared between big-endian and little-endian systems.
This option is selected when HAVE_NETINET_IN_H is defined
at compile time (default on UNIX).
Nelson H. F. Beebe <beebe@math.utah.edu> [14-Aug-1993]
2. Add typecast (int) in OutputTables() array reference to eliminate
compiler warnings.
3. Correct code in biblook:SetComplement() to check for zero
setmask; otherwise, a .bib file with a number of entries
which is an exact multiple of setsize (commonly 32) will
result in failing searches for the last setsize entries!
2.6 Jeff Erickson <jeffe@cs.berkeley.edu> 8/19/93
1. Made MungeWord() use iskeychar() instead of referring to
NONABBREV directly, and made iskeychar() use NONABBREV (now
called NONKEYCHARS). No reason to spell it out twice!
2. Correctly distinguish between abbreviations and numbers
not in quotes.
3. 75% calculation for hash table capacity uses unsigned longs
instead of Index_t's, which might overflow if Index_t is
unsigned short.
4. Added support for abbreviations (@strings). Abbrevs are
stored in special "field" table. HashCell data structure
modified to keep words in the expansion of any abbrev, along
with the offset at which the abbrev is defined. Whenever an
abbrev is encountered, both the abbrev and its expansion are
indexed. Warnings for multiple definitions of the same
abbrev or use of an undefined abbrev. Automatic inclusion
of standard abbrevs.
5. Abbrevs stored in index file to support biblook "whatis"
command. File version changed to 3.
6. Support for # concatenation added to MungeField.
7. Brackets ignored in GetNextWord, so optional abbreviations
like "J[ohn]" are handled correctly.
8. Elements of compound words, like "lower-bound" and "Fejes
Toth", are indexed both together and separately.
9. Added support for BIBINDEXFLAGS environment variable, which
stores default set of command-line arguments
10. No longer barfs on @comment and @preamble entries.
11. Changed format of screen output.
Nelson H. F. Beebe <beebe@math.utah.edu> [12-Sep-1993]
12. Add some code fixes so compilation under C++ works again.
13. Restore extended list of BADWORDS (defined in biblook.h).
Profiling showed that the sequential search through the
extended badwords[] table in IsRealWord() is a major
bottleneck with large input files. Therefore, replace slow
sequential search by fast hash lookup, with new functions
InHashCell() and StandardBadWords(), and new initialization
code in InitTables(), FreeTables(), and GetHashCell(),
14. Fix bug in GetNextWord() that produced string overflow in
word, and corrupt .bix file. Add corresponding sanity
check in WriteWord().
15. [Withdrawn]
16. [Withdrawn]
17. Apply some spelling corrections to strings and comments.
18. Prevent zero divide in statistics output in OutputTables().
19. Use FILENAME_MAX+1 (defined in biblook.h) for size of filename
strings.
20. [Withdrawn]
21. Change type of numfields from char to unsigned char, and
use sizeof numfields instead of sizeof(type of numfields).
22. Change all exit() calls to use Standard C EXIT_xxx symbols.
23. Put (void) casts on printf() family calls to avoid compiler
warnings about discarded returned values.
24. Change type of loop index variables from "short" to "int",
since shorts carry a significant penalty on modern
RISC architectures.
25. Change safemalloc() to request non-zero size when zero size
is requested; some systems reject a malloc(0) request (e.g.
IBM RS/6000 AIX 3.2).
Bill Jones <jones@cs.usask.ca> 93/09/29
26. MAXFIELDS set to 128, to resolve hash conflicts.
27. New type String for assembling compound words, MAXSTRING of
4095 as allowed by bibclean 2.07. strncpy and kin used in
hash table.
28. Hash tables extend at 7/8 capacity.
2.7 Bill Jones <jones@cs.usask.ca> 94/02/10
1. size_t used for sizes of tables/lists to escape overflow
when Index_t is short.
2. More robust behaviour in the face of word or expansion
buffer overflow.
3. Changes for MSDOS use, from Guenter Rote.
2.8 Jeff Erickson <jeffe@cs.berkeley.edu> 10 Jan 95
1. Changed some die()s to warn()s and added some error
recovery code to be more like bibtex.
2. Changed parsing of @comment to conform with bibtex, even
though it's totally drain-bamaged.
3. Changed iskeychar to conform with bibtex implementation
rather than bibtex documentation. Unfortunately, this
means that some things are indexed that can't be looked at
in biblook, since biblook ignores most non-alphanumeric
input.
4. Warn the user again at the end if there were any warnings.
If there was a warning, there's probably extra garbage in
the index tables.
Bill Jones <jones@cs.usask.ca> 95/01/19
5. Renovations to remove need for short/long Index_t tradeoff.
Index_t now always 4 bytes internally, but entry reference
lists written to disk in compressed form. File version 4.
6. Changes to hashing code. New HashWord collects code common to
GetHashCell and InHashCell. Calculation now truncates at
sizeof(Word), fixes bug where words differing beyond storage
truncation point had distinct hash entries. Performance tweak,
% can be done once, outside inner loop. Overflow bumped to
15/16 to save memory; profiling shows collisions still
miniscule on average.
7. Typedefs and casts necessitated by Alpha, where long is 64
bits, to keep index file quantities 2 or 4 bytes as intended.
(Thanks to Markus Stoll.)
8. Non-USASCII chars ignored, since their char classification
will differ with ISO locale.
9. BADWORDS trimmed to a top 20ish English list.
2.9 Bill Jones <jones@cs.usask.ca> 98/03/30
1. Pattern matching support, from Sariel Har-Peled, in biblook.
\* ================================================================= */
#include "biblook.h"
/* ======================= UTILITY FUNCTIONS ======================= */
static long line_number = 1L; /* for debug messages */
static long initial_line_number = 1L;
static int warnings = 0; /* How many warnings so far? */
/* ----------------------------------------------------------------- *\
| void die(const char *msg1, const char *msg2)
|
| Print an error message and die.
\* ----------------------------------------------------------------- */
void die(const char *msg1, const char *msg2)
{
(void)fprintf(stderr, "\nError:\t in BibTeX entry starting at line %ld, ",
initial_line_number);
(void)fprintf(stderr, "error detected at line %ld:\n",
line_number);
(void)fprintf(stderr, "\t%s %s\n", msg1, msg2);
exit(EXIT_FAILURE);
}
/* ----------------------------------------------------------------- *\
| void diechar(const char *msg1, const char msg2)
|
| Print an error message and die. Called when second message is a
| single character
\* ----------------------------------------------------------------- */
void diechar(const char *msg1, const char msg2)
{
char tmp[2];
tmp[0] = msg2;
tmp[1] = 0;
die(msg1, tmp);
}
/* ----------------------------------------------------------------- *\
| void warn(const char *msg1, const char *msg2)
|
| Print an error message and return.
\* ----------------------------------------------------------------- */
void warn(const char *msg1, const char *msg2)
{
(void)fprintf(stderr, "\nWarning: %s %s (at line %ld)\n",
msg1, msg2, line_number);
warnings++;
}
/* ----------------------------------------------------------------- *\
| void warnchar(const char *msg1, const char msg2)
|
| Print an error message and return. Called when second message is a
| single character
\* ----------------------------------------------------------------- */
void warnchar(const char *msg1, const char msg2)
{
char tmp[2];
tmp[0] = msg2;
tmp[1] = 0;
warn(msg1, tmp);
}
/* ----------------------------------------------------------------- *\
| char safegetc(FILE *fp, const char *what)
|
| Get the next character safely. Used by routines that assume that
| they won't run into the end of file.
\* ----------------------------------------------------------------- */
char safegetc(FILE *fp, const char *what)
{
register int c;
if ((c = getc(fp)) == '\n')
++line_number;
else if (c == EOF)
die("Unexpected end of file", what);
return ((char)c);
}
/* ----------------------------------------------------------------- *\
| void *safemalloc(unsigned howmuch, const char *msg1, const char *msg2)
|
| Allocate memory safely. Used by routines that assume they won't
| run out of memory.
\* ----------------------------------------------------------------- */
void *safemalloc(unsigned howmuch, const char *msg1, const char *msg2)
{
register void *tmp = NULL;
tmp = (void*)malloc((howmuch > 0) ? howmuch : 1);
/* some malloc's fail with zero size request */
if (tmp == NULL)
die(msg1, msg2);
return tmp;
}
/* ====================== HASH TABLE FUNCTIONS ===================== *\
The hash tables start small and double whenever they reach 15/16
capacity. Hashing is performed by going through the string one
character at a time, multiplying by a constant and adding in the
new character value each time. The constant is defined to give
the same even spread (about size/sqrt(2)) between successive
multiples, as long as the hash table size is a power of two.
Collisions are resolved by double hashing. Since the hash table
size is always a power of two, the secondary hash value has to be
odd to avoid loops.
The field tables are non-extensible hash tables, otherwise handled
the same way. It is probably well worth the effort to fine tune
the field table hash function in order to avoid collisions.
The field tables associated with ignored fields are black holes.
Everything is the same, except that InsertEntry doesn't actually
DO anything.
\* ================================================================= */
#define MAXFIELDS 256 /* small power of 2 */
#define INIT_HASH_SIZE 256 /* power of 2, >= MAXFIELDS */
#define HASH_CONST 1482907 /* prime close to 2^{20.5} */
typedef struct /* Hash table entry for index/abbrev tables */
{
Word theword; /* the hashed word/abbreviation */
Index_s number; /* number of refs/words in the list */
size_t size; /* real size of reference/word list */
/* need sizeof(size_t) > sizeof(Index_s) */
/* --- Index tables only --- */
Index_t *refs; /* actual list of references */
/* --- Abbreviation table only --- */
Index_t entry; /* entry containing definition */
Word *words; /* list of words in expansion */
} HashCell, *HashPtr;
typedef struct /* Extensible hash table */
{
Word thekey; /* field name or "@string" */
Index_t number; /* number of words in the table */
size_t size; /* real size of the table */
HashPtr words; /* index hash table */
} ExHashTable;
static ExHashTable fieldtable[MAXFIELDS]; /* the field tables */
static Index_s numfields; /* number of fields */
static ExHashTable abbrevtable[1]; /* the abbrev table */
static ExHashTable badwordtable[1]; /* the badword table */
/* ----------------------------------------------------------------- *\
| void InitOneField(ExHashTable *htable)
|
| Initialize one field's hash table
\* ----------------------------------------------------------------- */
void InitOneField(register ExHashTable *htable)
{
Index_t i;
htable->number = 0;
htable->size = INIT_HASH_SIZE;
htable->words = (HashPtr) safemalloc(INIT_HASH_SIZE*sizeof(HashCell),
"Can't create hash table for",
htable->thekey);
for (i=0; i<INIT_HASH_SIZE; i++)
{
htable->words[i].theword[0] = 0;
htable->words[i].number = 0;
htable->words[i].size = 0;
htable->words[i].refs = NULL;
htable->words[i].entry = INDEX_NAN;
htable->words[i].words = NULL;
}
}
/* ----------------------------------------------------------------- *\
| void InitTables(void)
|
| Initialize the field tables
\* ----------------------------------------------------------------- */
void InitTables(VOID)
{
register unsigned int i;
numfields = 0;
for (i=0; i<MAXFIELDS; i++)
{
fieldtable[i].thekey[0] = 0;
fieldtable[i].number = 0;
fieldtable[i].size = 0;
fieldtable[i].words = NULL;
}
strcpy(abbrevtable->thekey, "abbreviations");
abbrevtable->number = 0;
abbrevtable->size = 0;
abbrevtable->words = NULL;
InitOneField(abbrevtable);
strcpy(badwordtable->thekey, "bad words");
badwordtable->number = 0;
badwordtable->size = 0;
badwordtable->words = NULL;
InitOneField(badwordtable);
}
/* ----------------------------------------------------------------- *\
| void FreeTables(void)
|
| Free the tables
\* ----------------------------------------------------------------- */
void FreeTables(VOID)
{
register unsigned int i;
Index_t j;
for (i=0; i < (unsigned int)numfields; i++)
{
if (fieldtable[i].words)
{
for (j=0; j < fieldtable[i].number; j++)
if (fieldtable[i].words[j].refs)
free(fieldtable[i].words[j].refs);
free(fieldtable[i].words);
}
}
if (abbrevtable->words)
{
for (j=0; j < abbrevtable->number; j++)
if (abbrevtable->words[j].words)
free(abbrevtable->words[j].words);
free(abbrevtable->words);
}
if (badwordtable->words)
{
for (j=0; j < badwordtable->number; j++)
if (badwordtable->words[j].words)
free(badwordtable->words[j].words);
free(badwordtable->words);
}
}
/* ----------------------------------------------------------------- *\
| ExHashTable *GetHashTable(const char *field)
|
| Get the hash table associated with the given key -- field name or
| "@string". If the table is unclaimed, claim it and initialize it.
\* ----------------------------------------------------------------- */
ExHashTable *GetHashTable(const char *field)
{
register unsigned long hash=0; /* primary hash value */
register unsigned long skip=1; /* secondary hash value */
register int i;
register ExHashTable *cell;
for (i=0; field[i] && (i < ((int)sizeof(Word)) - 1); i++)
{
hash = hash*HASH_CONST + field[i];
skip += 2*hash;
}
hash &= MAXFIELDS - 1; /* MAXFIELDS power of 2 */
/* cell not empty, and not the right word */
while (cell = fieldtable+hash, cell->thekey[0] &&
strncmp(cell->thekey, field, sizeof(Word)-1))
hash = (hash+skip) & (MAXFIELDS-1);
if (!cell->thekey[0])
{
strncpy(cell->thekey, field, sizeof(Word));
if (strlen(field) > sizeof(Word)-1) {
cell->thekey[sizeof(Word)-1] = 0;
warn("truncated field name:", cell->thekey);
}
InitOneField(cell);
numfields++;
if (numfields >= MAXFIELDS) /* NB: NOT > because that produces */
/* an infinite while() loop above on */
/* next entry! */
die("too many field names", field);
}
return cell;
}
/* ----------------------------------------------------------------- *\
| void InitBlackHole(char *field)
|
| Initialize a black hole for the given field
\* ----------------------------------------------------------------- */
void InitBlackHole(char *field)
{
ExHashTable *hole;
hole = GetHashTable(field);
if (hole->words != NULL)
{
free(hole->words);
hole->words = NULL;
}
}
/* ----------------------------------------------------------------- *\
| int IsBlackHole(ExHashTable *htable)
|
| Is the given hash table a black hole?
\* ----------------------------------------------------------------- */
#define IsBlackHole(htable) ((htable)->words == NULL)
/* ----------------------------------------------------------------- *\
| HashPtr HashWord(ExHashTable *htable, const char *word)
|
| Hashing computation and table search.
\* ----------------------------------------------------------------- */
HashPtr HashWord(ExHashTable *htable, register const char *word)
{
register unsigned long hash=0; /* primary hash value */
register unsigned long skip=1; /* secondary hash value */
register int i;
register HashPtr cell, table;
table = htable->words;
for (i=0; word[i] && (i < ((int)sizeof(Word)) - 1); i++)
{
hash = (hash*HASH_CONST + word[i]);
skip += 2*hash;
}
hash &= htable->size - 1; /* size power of 2 */
/* cell not empty, and not the right word */
while (cell = table+hash, cell->theword[0] &&
strncmp(cell->theword, word, sizeof(Word)-1))
hash = (hash+skip) & (htable->size - 1);
return cell;
}
/* ----------------------------------------------------------------- *\
| HashPtr GetHashCell(ExHashTable *htable, const char *word)
|
| Get the hash table cell associated with the given word/abbrev.
| If the cell is unclaimed, claim it, initialize it, and update
| the table's word count.
\* ----------------------------------------------------------------- */
HashPtr GetHashCell(ExHashTable *htable, const char *word)
{
register HashPtr cell;
cell = HashWord(htable, word);
if (!cell->theword[0]) /* if cell isn't initialized yet... */
{
strncpy(cell->theword, word, sizeof(Word));
if (strlen(word) > sizeof(Word)-1) {
cell->theword[sizeof(Word)-1] = 0;
warn("truncated word:", cell->theword);
}
cell->size = 4;
if (htable == abbrevtable)
{
cell->words = (Word *) safemalloc(cell->size * sizeof(Word),
"Can't store expansion for",
word);
}
else if (htable == badwordtable)
{
cell->words = (Word *) safemalloc(cell->size * sizeof(Word),
"Can't store ignorable word",
word);
}
else
{
cell->refs = (Index_t *) safemalloc(cell->size * sizeof(Index_t),
"Can't create entry list for",
word);
}
htable->number++;
}
return cell;
}
/* ----------------------------------------------------------------- *\
| int InHashCell(ExHashTable *htable, const char *word)
|
| Return 1 if word is in table, and 0 otherwise.
\* ----------------------------------------------------------------- */
int InHashCell(ExHashTable *htable, const char *word)
{
return (HashWord(htable, word)->theword[0] ? 1 : 0);
}
/* ----------------------------------------------------------------- *\
| void ExtendHashTable(ExHashTable *htable)
|
| Double the size of the hash table and rehash everything.
\* ----------------------------------------------------------------- */
void ExtendHashTable(ExHashTable *htable)
{
register HashPtr newcell;
register HashPtr oldtable;
size_t i;
Index_t oldsize;
oldsize = htable->size;
oldtable = htable->words;
htable->number = 0;
htable->size *= 2;
if (htable->size <= 0)
die("hash type overflow:", htable->thekey);
htable->words = (HashPtr) safemalloc(sizeof(HashCell)*htable->size,
"Can't extend hash table for",
htable->thekey);
for (i=0; i < htable->size; i++)
{
htable->words[i].theword[0] = 0;
htable->words[i].number = 0;
htable->words[i].size = 0;
htable->words[i].refs = NULL;
htable->words[i].entry = INDEX_NAN;
htable->words[i].words = NULL;
}
for (i=0; i<oldsize; i++)
{
if (oldtable[i].theword[0])
{
newcell = GetHashCell(htable, oldtable[i].theword);
*newcell = oldtable[i];
}
}
free(oldtable);
}
/* ----------------------------------------------------------------- *\
| void InsertEntry(ExHashTable *htable, char *word, Index_t entry)
|
| Insert the word/entry pair into the hash table, unless it's
| already there. Assumes htable is not the abbreviation table.
\* ----------------------------------------------------------------- */
void InsertEntry(ExHashTable *htable, char *word, Index_t entry)
{
register HashPtr cell;
Index_t *newlist;
if (IsBlackHole(htable))
return;
if (htable->number * (unsigned long)16 >
htable->size * (unsigned long)15) /* unsigned long, NOT Index_t */
ExtendHashTable(htable);
cell = GetHashCell(htable, word);
if (cell->number && (cell->refs[cell->number-1] == entry))
return;
if (cell->number == cell->size) /* expand the array */
{
cell->size *= 2;
if (cell->size <= 0)
die("hash type overflow:", htable->thekey);
newlist = (Index_t *) safemalloc(cell->size * sizeof(Index_t),
"Can't extend entry list for",
word);
bcopy(cell->refs, newlist, cell->number * sizeof(Index_t));
free(cell->refs);
cell->refs = newlist;
}
cell->refs[cell->number++] = entry;
}
/* ----------------------------------------------------------------- *\
| void InsertExpansion(HashPtr cell, const char *theword);
|
| Insert a new word into the given abbreviation's expansion.
| Doesn't bother checking for duplicate words [yet].
\* ----------------------------------------------------------------- */
void InsertExpansion(register HashPtr cell, const char *theword)
{
Word *newlist;
/* This should never happen... */
if (cell->number == cell->size) /* expand the array */
{
cell->size *= 2;
if (cell->size <= 0)
die("hash type overflow:", "abbreviations");
newlist = (Word *) safemalloc(cell->size * sizeof(Word),
"Can't extend expansion list for",
cell->theword);
bcopy(cell->words, newlist, cell->number * sizeof(Word));
free(cell->words);
cell->words = newlist;
}
strncpy(cell->words[cell->number], theword, sizeof(Word));
if (strlen(theword) > sizeof(Word)-1) {
(cell->words[cell->number])[sizeof(Word)-1] = 0;
warn("truncated expansion:", cell->words[cell->number]);
}
cell->number++;
}
/* ----------------------------------------------------------------- *\
| Standard bibtex abbreviations. The journal list is taken from
| "plain.bst", but the journal abbreviations conform (wherever
| possible) to the computational geometry bibliography's standard.
| In particular, these may disagree with the abbreviations used in
| standard ".bst" files. See the file "authority" for details
| on the abbreviation standard. Journal abbreviations are
| included automatically iff the macro STD_J_ABBREVS is defined.
\* ----------------------------------------------------------------- */
typedef struct
{
Word abbrev;
short number;
Word words[10];
} StdAbbrev;
#ifdef STD_J_ABBREVS
#define NUM_STD_ABBR 32
#else
#define NUM_STD_ABBR 12
#endif
const StdAbbrev std_abbr[] =
{
{"jan", 1, {"january"}},
{"feb", 1, {"february"}},
{"mar", 1, {"march"}},
{"apr", 1, {"april"}},
{"may", 1, {"may"}},
{"jun", 1, {"june"}},
{"jul", 1, {"july"}},
{"aug", 1, {"august"}},
{"sep", 2, {"sept", "september"}},
{"oct", 1, {"october"}},
{"nov", 1, {"november"}},
{"dec", 1, {"december"}},
#ifdef LOGABBREVS
{"acmcs", 3, {"acm", "comput", "surv"}},
{"acta", 2, {"acta", "inform"}},
{"cacm", 2, {"commun", "acm"}},
{"ibmjrd", 3, {"ibm", "res", "develop"}},
{"ibmsj", 2, {"ibm", "syst"}},
{"ieeese", 4, {"ieee", "trans", "softw", "eng"}},
{"ieeetc", 3, {"ieee", "trans", "comput"}},
{"ieeetcad",7, {"ieee", "trans", "comput", "aided", "design",
"integrated", "circuits"},
{"ipl", 3, {"inform", "process", "lett"}},
{"jacm", 1, {"acm"}},
{"jcss", 3, {"comput", "syst", "sci"}},
{"scp", 3, {"sci", "comput", "program"}},
{"sicomp", 2, {"siam", "comput"}},
{"tocs", 4, {"acm", "trans", "comput", "syst"}},
{"tods", 4, {"acm", "trans", "database", "syst"}},
{"tog", 3, {"acm", "trans", "graph"}},
{"toms", 4, {"acm", "trans", "math", "softw"}},
{"toois", 5, {"acm", "trans", "office", "inform", "syst"}},
{"toplas", 5, {"acm", "trans", "program", "lang", "syst"}},
{"tcs", 3, {"theoret", "comput", "sci"}},
#endif
};
/* ----------------------------------------------------------------- *\
| void StandardAbbrevs(void)
|
| Put the standard abbreviations into the abbreviation table.
\* ----------------------------------------------------------------- */
void StandardAbbrevs(VOID)
{
register HashPtr thecell;
register int i,j;
for (i=0; i<NUM_STD_ABBR; i++)
{
thecell = GetHashCell(abbrevtable, std_abbr[i].abbrev);
thecell->entry = INDEX_BUILTIN;
for (j=0; j<std_abbr[i].number; j++)
InsertExpansion(thecell, std_abbr[i].words[j]);
}
}
const char *badwords[] = BADWORDS;
/* ----------------------------------------------------------------- *\
| void StandardBadWords(void)
|
| Put the standard ignorable words into the badword table.
\* ----------------------------------------------------------------- */
void StandardBadWords(VOID)
{
register int i;
for (i=0; badwords[i] != (const char*)NULL; i++)
(void)GetHashCell(badwordtable, badwords[i]);
}
/* ============================= INPUT ============================= *\
I'd like this to work with more than just the CG bib, so I can't
assume very much about the input. In particular, all white space
(blanks, tabs, and newlines) is identical most of the time. On
the other hand, it would be nice to include any "comments" that
obviously go with an entry as part of that entry. Consequently,
two newlines in a row (possibly separated by other white space) is
considered a break between entries. This will give us bogus
entries for stray "comments", but we can take care that easily
enough -- an entry is only real if it contains a @ character.
\* ================================================================= */
/* ----------------------------------------------------------------- *\
| unsigned long FindNextEntry(FILE *ifp)
|
| Return the file offset to the next entry in the bib file. On exit,
| the file pointer is left just after the "@". The entry officially
| begins after the most recent blank line, the end of the previous
| entry, or the beginning of the file. Returns "-1" if there is no
| next entry. It is the CALLER's responsibility to determine the type
| of entry (normal or @string or @comment or @preamble or error).
\* ----------------------------------------------------------------- */
unsigned long FindNextEntry(FILE *ifp)
{
char ch;
char blank = 0; /* 1 if current line is blank so far */
unsigned long offset;
offset = ftell(ifp);
ch = getc(ifp);
if (ch == '\n')
{
line_number++;
offset++;
}
initial_line_number = line_number; /* record for errors */
for (;;)
{
if (feof(ifp))
return (unsigned long) -1;
if (ch == '@') /* got an entry */
return offset;
else if (ch == '\n')
{
if (blank)
{
offset = ftell(ifp);
initial_line_number = line_number;
}
blank = 1;
}
else if (!isspace(ch))
blank = 0;
ch = getc(ifp);
if (ch == '\n')
line_number++;
}
}
/* ----------------------------------------------------------------- *\
| int GetNextWord(FILE *ifp, char *word)
|
| Get the next word in the current field. A word is any contiguous
| set of letters and numbers, AFTER the following steps:
| 1. Letters are folded to lower case. Thus, "Voronoi" is
| returned as "voronoi"
| 2. All TeX commands, except those in math expressions, are
| removed, but their arguments are left behind. Thus,
| "Erd{\H o}s" is returned as "erdos".
| 3. All other non-word characters are removed. Non-word
| characters inside {{possibly} nested} braces or dollar
| signs do not delimit words, so they may cause unexpected
| results. Thus, "{this example}" is returned as
| "thisexample". (But see below.)
| 4. TeX commands in math expressions are considered normal
| text. Thus, "$O(n\log^2 n)$" is returned as "onlog2n"
| instead of "onn". (But see below.) This occasionally
| gives unexpected or unreadable results.
| 5. Apostrophes, brackets, and hyphens are ignored (but see
| below). Thus, "{\'O}'D{\'u}nlaing" as "odunlaing",
| "J[ohn]" as "john", and "half-space" as "halfspace".
|
| Compound words are words with several components, like "half-space"
| or "{van Dam}". Compound words are written into the word buffer
| with their components separated by null characters. There are
| three ways to get compound words:
| 1. Hyphens separate components. Thus, "half-space" is really
| returned as "half\0space". However, "18--21" is returned
| as two separate words.
| 2. Within braces, white space delimits components. Thus,
| "{van Dam}" is really returned as "van\0dam".
| 3. Within math expressions, components are delimited by ANY
| string of non-alphanumeric characters. For example,
| "$\Omega(n\log n)$" is really returned as
| "omega\0n\0log\0n".
|
| Returns 0 if no word was found, 1 if a simple word was found, and
| the number of components if a compound word was found.
|
| The file pointer is left at the character immediately following the
| word. The input is assumed to be syntactically correct: unbalanced
| braces, math delimiters, or quotation marks will cause errors.
\* ----------------------------------------------------------------- */
int GetNextWord(FILE *ifp, register char *word)
{
register char ch = ' ';
char braces = 0; /* levels of indented braces */
char math = 0; /* 1 if in math expression */
char incmd = 0; /* 1 if reading TeX command */
char done = 0; /* 1 if word is complete */
char btwn = 1; /* 1 if "between" components */
int nchars = 0; /* how many characters in word? */
int nwords = 0; /* how many words have I seen? */
#if DEBUG
char *start_word = word; /* DEBUG */
#endif /* DEBUG */
while (!done)
{
ch = safegetc(ifp, "reading next word");
#if DEBUG
if ((int)(word - start_word) > (MAXSTRING - 3))
{
(void)printf("WARNING: word = %-.*s\n", MAXWORD, start_word);
}
#endif /* DEBUG */
if (!isascii(ch)) /* high bit set */
{
char buf[32];
(void)sprintf(buf, "%c (\\%03o)", (unsigned char) ch, (unsigned char) ch);
warn("nonascii char, ignoring: ", buf);
}
else if (isalpha(ch)) /* letters */
{
if (!incmd) /* may be part of TeX command */
{
if (btwn)
{
nwords++;
btwn = 0;
}
if (++nchars <= MAXSTRING) /* ignore overflow */
*word++ = tolower(ch);
}
}
else if (isdigit(ch)) /* digits */
{
incmd = 0;
if (btwn)
{
nwords++;
btwn = 0;
}
if (++nchars <= MAXSTRING)
*word++ = ch;
}
else if (math) /* other char in math mode */
{
if (ch == '$') /* end math mode */
{
math = 0;
braces--;
}
else if (!btwn)
{
if (++nchars <= MAXSTRING)
*word++ = 0;
btwn = 1;
}
}
else if (ch == '\\') /* beginning of TeX command */
{
ch = safegetc(ifp, "reading next word");
if (isalpha(ch))
incmd = 1;
}
else if (ch == '{') /* left brace */
{
incmd = 0;
braces++;
}
else if (ch == '}') /* right brace */
{
incmd = 0;
if (!braces)
{
ungetc(ch, ifp);
done = 1;
}
else
braces--;
}
else if (ch == '"') /* double quote */
{
incmd = 0;
if (!braces)
{
ungetc(ch, ifp);
done = 1;
}
}
else if (ch == '$') /* begin math mode */
{
incmd = 0;
math = 1;
braces++;
}
else if (ch == '-' && !btwn) /* single hyphens */
{
if (++nchars <= MAXSTRING)
*word++ = 0;
btwn = 1;
}
else if (isspace(ch) && braces) /* white space */
{
if (incmd)
incmd = 0;
else if (!btwn)
{
if (++nchars <= MAXSTRING)
*word++ = 0;
btwn = 1;
}
}
else if (incmd) /* other characters */
incmd = 0;
else if ((ch == '\'') || (ch == '[') || (ch == ']'))
/* do nothing */;
else if ((nwords != 0) && !braces)
done = 1;
#if DEBUG
if ((int)(word - start_word) > (MAXSTRING - 3))
{
(void)printf("WARNING: word = %-.*s\n", MAXWORD, start_word);
}
#endif /* DEBUG */
#if 0
(void)printf("%c {%d} $%d$ \\%d -%d- #%2d/%d\n",
ch, braces, math, incmd, btwn, nchars, nwords);
#endif
/* Two situations can produce an unusually long `word' that
exceeds MAXSTRING characters:
(a) a long compound word
(b) a long braced string (e.g. braces added to protect
a complete title from lower-casing in certain
bibliography styles)
In either case, we bail out early, making sure to properly
terminate the string. */
if (nchars > MAXSTRING)
{
done = 1; /* bail out early if buffer full */
*word = 0;
}
}
if (!btwn)
*word = 0;
#if DEBUG
if (strlen(start_word) > (size_t)MAXSTRING)
{
(void)printf("ERROR: word:%d = [%s]\n", strlen(start_word), start_word);
}
#endif /* DEBUG */
return nwords;
}
/* ----------------------------------------------------------------- *\
| int iskeychar(char c, int first_char)
|
| Return 1 if c is a valid keyword character, and 0 otherwise.
| The rules are different for the first character, so first_char
| must be set non-zero to select that case.
|
| The code in bibtex.web in the section on character set dependencies
| creates a character classification array, id_class[], which permits
| the following characters (verified by experiment too) in field
| names:
| A-Z a-z 0-9
| ! $ & * + - . / : ; < > ? @ [ \ ] ^ _ ` | ~ <DELete>
| In other words, every printing character except the following.
| " # % ' ( ) , = { }
| [bibtex.web 33]
|
| Despite claims in the documentation that say the first character
| must be a letter, it can really be anything but a digit.
| [bibtex.web 90]
\* ----------------------------------------------------------------- */
int iskeychar(char c, int first_char)
{
if (isalpha(c))
return 1;
else if (isdigit(c))
return !first_char;
else if (iscntrl(c))
return 0;
else
return (strchr(NONKEYCHARS, (int)c) == (char *)NULL);
}
/* ----------------------------------------------------------------- *\
| int IsRealWord(char *theword)
|
| Is this a word I really want to index?
\* ----------------------------------------------------------------- */
int IsRealWord(register char *theword)
{
#if 0
register int i;
#endif
if (!theword[0] || !theword[1]) /* empty or 1 character */
return 0;
#if IGNORENONE
return 1;
#endif
#if 0
for (i=0; badwords[i]; i++)
if (!strcmp(theword, badwords[i]))
return 0;
return 1;
#else
return (!InHashCell(badwordtable,theword));
#endif
}
/* ----------------------------------------------------------------- *\
| char MungeField(FILE *ifp, void (*action)(char *, void *, void *)),
| void *arg1, void *arg2)
|
| Munge the current field. For every word in the field, call
| (*action), passing in the word and the args. If the word is an
| abbreviation, call (*action) on the words in its expansion.
|
| On entrance, the file pointer is just after the field name. On
| exit, the file pointer is on the comma or closing character for
| the entry.
|
| If a parsing error is encountered, warn the user and return 0. If
| the field is munged successfully, return 1.
\* ----------------------------------------------------------------- */
char MungeField(FILE *ifp, void (*action)(char *, void *, void *),
void *arg1, void *arg2)
{
register char ch;
register int i, nwords;
register char *tmp, *tmp2;
Index_t k;
String nextword; /* big, to survive over-embraced titles */
HashPtr abbrevcell;
ch = safegetc(ifp, "looking for =");
while (isspace(ch))
ch = safegetc(ifp, "looking for =");
if (ch != '=')
{
warnchar("= expected after field name: ", ch);
return 0;
}
for (;;)
{
ch = safegetc(ifp, "looking for open quote/brace");
while (isspace(ch))
ch = safegetc(ifp, "looking for open quote/brace");
if ((ch == '{') || (ch == '"'))
{
nwords = GetNextWord(ifp, nextword);
while (nwords != 0)
{
if (nwords != 1) /* compound word */
{
tmp = nextword;
for (i=0; i<nwords; i++)
{
if (IsRealWord(tmp))
(*action)(tmp, arg1, arg2);
tmp += strlen(tmp) + 1;
}
tmp = nextword + strlen(nextword);
tmp2 = tmp + 1;
while (nwords > 1) /* recombine the components */
{
if (!*tmp2)
{
tmp2++;
nwords--;
}
else
*tmp++ = *tmp2++;
}
*tmp = 0;
}
if (IsRealWord(nextword))
(*action)(nextword, arg1, arg2);
nwords = GetNextWord(ifp, nextword);
}
ch = safegetc(ifp, "reading close quote/brace");
ch = safegetc(ifp, "looking for comma or close brace");
}
else if (isdigit(ch)) /* number */
{
for (i=0; isdigit(ch); i++)
{
if (i >= MAXWORD)
{
nextword[MAXWORD] = 0;
die("word buffer overflow", nextword);
}
nextword[i] = tolower(ch);
ch = safegetc(ifp, "reading number");
}
nextword[i] = 0;
nextword[MAXWORD] = 0;
(*action)(nextword, arg1, arg2);
}
else if (iskeychar(ch, 1)) /* abbreviation */
{
for (i=0; iskeychar(ch, i==0); i++)
{
if (i >= MAXWORD)
{
nextword[MAXWORD] = 0;
die("word buffer overflow", nextword);
}
nextword[i] = tolower(ch);
ch = safegetc(ifp, "reading abbreviation");
}
nextword[i] = 0;
nextword[MAXWORD] = 0;
(*action)(nextword, arg1, arg2);
/* --- Munge the abbreviation's expansion, too --- */
abbrevcell = GetHashCell(abbrevtable, nextword);
if (abbrevcell->entry == INDEX_NAN)
warn("Undefined abbreviation:", nextword);
for (k=0; k<abbrevcell->number; k++)
(*action)(abbrevcell->words[k], arg1, arg2);
}
else
{
warnchar("Illegal character after =:", ch);
return 0;
}
while (isspace(ch))
ch = safegetc(ifp, "looking for comma, close brace, or #");
if ((ch == ',') || (ch == '}') || (ch == ')'))
{
ungetc(ch, ifp);
return 1;
}
else if (ch == '#')
continue;
else
{
warnchar("Expected } or , or # after field string:", ch);
return 0;
}
}
}
/* ----------------------------------------------------------------- *\
| void MF_InsertExpansion(char *word, ExHashTable *htable, HashPtr cell)
|
| Version of InsertEntry for passing to MungeField
\* ----------------------------------------------------------------- */
void MF_InsertExpansion(char *word, ExHashTable *htable, HashPtr cell)
{
InsertEntry(htable, word, cell->entry);
InsertExpansion(cell, word);
}
/* ----------------------------------------------------------------- *\
| void MF_InsertEntry(char *word, ExHashTable *htable, Index_t *entry)
|
| Version of InsertEntry for passing to MungeField
\* ----------------------------------------------------------------- */
void MF_InsertEntry(char *word, ExHashTable *htable, Index_t *entry)
{
InsertEntry(htable, word, *entry);
}
/* ----------------------------------------------------------------- *\
| void MungeAbbrev(FILE *ifp, Index_t entry)
|
| Wander though the abbreviation, putting the words into the
| abbreviation table. Looks a lot like MungeField, doesn't it?
|
| If a parsing error is encountered, warn the user and return
| immediately. This makes us ignore everything up to the next @,
| which is more or less what bibtex does in the same situation.
\* ----------------------------------------------------------------- */
void MungeAbbrev(FILE *ifp, Index_t entry)
{
register char ch;
register int i;
Word theabbrev;
HashPtr thecell;
ExHashTable *htable;
ch = safegetc(ifp, "looking for abbreviation");
while (isspace(ch))
ch = safegetc(ifp, "looking for abbreviation");
if (!iskeychar(ch, 1))
{
warnchar("Illegal character starting abbreviation:", ch);
(void)fprintf(stderr, "\t I'm skipping the rest of this entry.\n");
return;
}
for (i=0; iskeychar(ch, i==0); i++)
{
if (i >= MAXWORD)
{
theabbrev[i] = 0;
die("abbreviation buffer overflow", theabbrev);
}
theabbrev[i] = tolower(ch);
ch = safegetc(ifp, "reading abbreviation");
}
ungetc(ch, ifp); /* put back lookahead char */
theabbrev[i] = 0;
if (abbrevtable->number * (unsigned long)8 >
abbrevtable->size * (unsigned long)7)
ExtendHashTable(abbrevtable);
htable = GetHashTable("@string");
thecell = GetHashCell(abbrevtable, theabbrev);
if (thecell->entry != INDEX_NAN)
warn("Multiply-defined abbreviation:", theabbrev);
thecell->entry = entry;
MungeField(ifp, (void (*)(char *, void *, void *))MF_InsertExpansion,
(void *)htable, (void *)thecell);
ch = safegetc(ifp, "trying to read close brace");
}
/* ----------------------------------------------------------------- *\
| void MungeRealEntry(FILE *ifp, Index_t entry)
|
| Wander though the entry, mungeing each field. On entry, the file
| pointer is just after the opening brace/paren.
|
| If a parsing error is encountered, warn the user and return
| immediately. This makes us ignore everything up to the next @,
| which is exactly what bibtex does in the same situation.
\* ----------------------------------------------------------------- */
void MungeRealEntry(FILE *ifp, Index_t entry)
{
register char ch;
Word thefield;
int i;
ExHashTable *htable;
ch = safegetc(ifp, "looking for citekey");
while (isspace(ch))
ch = safegetc(ifp, "looking for citekey");
/* Pretty much anything can go in a bibtex key, including braces, */
/* parens, quotes, and even chars that are illegal ANYWHERE else! */
while (ch != ',')
ch = safegetc(ifp, "reading citekey");
while (ch == ',')
{
ch = safegetc(ifp, "looking for field descriptor");
while (isspace(ch))
ch = safegetc(ifp, "looking for field descriptor");
if ((ch == '}') || (ch == ')')) /* allow trailing comma after */
return; /* last key = "value" entry */
if (!iskeychar(ch, 1))
{
warnchar("Illegal character starting field descriptor:", ch);
(void)fprintf(stderr, "\t I'm skipping the rest of this entry.\n");
return;
}
for (i=0; iskeychar(ch, i==0); i++)
{
if (i >= MAXWORD)
{
thefield[i] = 0;
die("field name buffer overflow", thefield);
}
thefield[i] = tolower(ch);
ch = safegetc(ifp, "reading field descriptor");
}
ungetc(ch, ifp); /* put back lookahead char */
thefield[i] = 0;
htable = GetHashTable(thefield);
if (!MungeField(ifp,
(void (*)(char*, void*, void*))MF_InsertEntry,
(void *)htable, (void *)&entry))
{
(void)fprintf(stderr, "\t I'm skipping the rest of this entry.\n");
return;
}
ch = safegetc(ifp, "trying to read comma or close brace");
}
}
/* ----------------------------------------------------------------- *\
| void SkipEntry(FILE *ifp)
|
| Skip the current entry, assuming that we're starting at the
| beginning. Currently only used to skip @preamble's.
\* ----------------------------------------------------------------- */
void SkipEntry(FILE *ifp)
{
char ch;
int braces;
int quotes;
quotes = 0;
braces = 0;
ch = safegetc(ifp, "skipping false entry");
while (quotes || braces || ((ch != '}') && (ch != ')')))
{
if (ch == '{')
braces++;
else if (ch == '}')
braces--;
else if ((ch == '"') && !braces)
quotes = !quotes;
ch = safegetc(ifp, "skipping false entry");
}
}
/* ----------------------------------------------------------------- *\
| int MungeEntry(FILE *ifp, Index_t entry)
|
| Determine whether the current entry is real or @comment or
| @preamble, and munge the entry appropriately. Return 1 if the
| entry was real, or 0 if it was a "comment" or if there was a
| parsing error AT THIS LEVEL.
|
| If a parsing error is encountered, warn the user and return 0
| immediately. This makes us ignore everything up to the next @,
| which is exactly what bibtex does in the same situation.
\* ----------------------------------------------------------------- */
int MungeEntry(FILE *ifp, Index_t entry)
{
register char ch;
Word therecord;
int i;
ch = safegetc(ifp, "looking for entry type");
while (isspace(ch))
ch = safegetc(ifp, "looking for entry type");
if (!isalpha(ch))
{
warnchar("Letter expected after @:", ch);
(void)fprintf(stderr, "\t I'm skipping the rest of this entry.\n");
return 0;
}
for (i=0; iskeychar(ch, i==0); i++)
{
if (i >= MAXWORD)
{
therecord[i] = 0;
die("record name buffer overflow", therecord);
}
therecord[i] = tolower(ch);
ch = safegetc(ifp, "recording entry type");
}
therecord[i] = 0;
while (isspace(ch))
ch = safegetc(ifp, "looking for open brace");
if ((ch != '(') && (ch != '{'))
{
warnchar("{ or ( expected after entry type:", ch);
(void)fprintf(stderr, "\t I'm skipping the rest of this entry.\n");
return 0;
}
if (!strcmp(therecord, "string"))
{
MungeAbbrev(ifp, entry);
return 1;
}
else if (!strcmp(therecord, "comment"))
{
/* Do nothing! [bibtex.web 241] */
return 0;
}
else if (!strcmp(therecord, "preamble"))
{
SkipEntry(ifp);
return 0;
}
else
{
MungeRealEntry(ifp, entry);
return 1;
}
}
/* ============================= OUTPUT ============================ */
/* ----------------------------------------------------------------- *\
| void WriteWord(FILE *ofp, Word word)
|
| Output the word in "Pascal" string format -- length byte followed
| by characters. This saves some disk space over writing MAXWORD+1
| bytes in all cases.
\* ----------------------------------------------------------------- */
void WriteWord(FILE *ofp, Word word)
{
unsigned char length = (unsigned char)strlen(word);
/* Apply a sanity check: had this been here in the first place,
a nasty bug introduced with compound word support would have
been caught many hours sooner. */
if (length > MAXWORD) {
warn("punting on overflowed word buffer: ", word);
length = MAXWORD;
word[MAXWORD] = 0;
}
fwrite((void *) &length, sizeof(unsigned char), 1, ofp);
fwrite((void *) word, sizeof(char), (size_t)length, ofp);
}
/* ----------------------------------------------------------------- *\
| void NetOrderFwrite(void *vbuf, int s, int n, FILE *fp)
|
| Write n elements of size s in network byteorder
\* ----------------------------------------------------------------- */
static void NetOrderFwrite(void *vbuf, size_t s, size_t n, FILE *fp)
{
char *buf = (char *)vbuf;
while (n-- > 0)
{
size_t rc;
if (s == sizeof(uint16))
{
uint16 x = htons(* (uint16 *) buf);
rc = fwrite(&x, s, 1, fp);
}
else /* assume s == sizeof(uint32) */
{
uint32 x = htonl(* (uint32 *) buf);
rc = fwrite(&x, s, 1, fp);
}
if (rc != 1)
{
perror("bibindex: cannot write; reason");
exit(EXIT_FAILURE);
}
/* advance and continue: */
buf += s;
}
}
/* ----------------------------------------------------------------- *\
| void SortTable(ExHashTable* htable)
|
| Compress and sort a hash table. KLUDGE -- Passing strcmp to qsort
| assumes intimate knowledge of HashCell.
\* ----------------------------------------------------------------- */
void SortTable(register ExHashTable* htable)
{
register HashPtr words;
size_t m, n;
words = htable->words;
for (m=0, n=0; m<htable->size; m++)
{
if (words[m].theword[0])
{
if (m > n)
{
words[n] = words[m]; /* copy mth table to nth */
words[m].number = 0; /* then clear mth table */
words[m].size = 0; /* to avoid duplicate free() later */
words[m].refs = (Index_t*)0;
}
n++;
}
}
qsort(words, (size_t)htable->number, sizeof(HashCell),
(int (*)(const void*,const void*))strcmp);
}
/* ----------------------------------------------------------------- *\
| Index_s CompressRefs(char *p, Index_t *list, Index_t length)
|
| Compress a sequence of Index_t. Assumes and exploits redundancy
| where sequence is monotonic increasing, with interterm difference
| typically small. Tests on a 68 MB concatenation of bibliographies
| gave sample probabilities for difference bitlengths as
|
| + 1 2 3 4 5 6 7 8
| 0 .22279 .06392 .06831 .07519 .07839 .07693 .07075 .06177
| 8 .05325 .04293 .03429 .02824 .02399 .02756 .02232 .02607
| 16 .01580 .00749
|
| with expected value 6.177 bits. To compress, we encode difference
| values in a low-to-high nibble scheme where the low n bits are
| difference data and the high (n+1)st bit indicates the value continues
| to the next nibble. From the data above, we can calculate the
| relative storage used by n between 1 and 7 bits as
|
| 1.0910 0.8964 0.8693 0.8824 0.9149 0.9506 1.0000
|
| so n of 3 looks optimal, but we use 7 bits here for convenience.
| Difference range of this scheme is thus
|
| with 1 bytes 127 entries
| 2 16383+127
| 3 2M+16383+127
| 4 256M+2M+16383+127
|
| and testing on biblios from 1.7 to 68 MB showed average bytes per
| difference as low as 1.25, but generally close to 1.40.
\* ----------------------------------------------------------------- */
Index_s CompressRefs(char *p, Index_t *list, Index_t length)
{
Index_t prevref = (Index_t)-1;
Index_t diff;
char bits;
char *p0 = p;
while (length-- > 0)
{
diff = *list - prevref;
while (diff > 0)
{
bits = diff & ~CHAR_HIGHBIT;
if ((diff >>= (CHAR_BIT - 1)))
bits |= CHAR_HIGHBIT;
*p++ = bits;
}
prevref = *list++;
}
return p - p0;
}
/* ----------------------------------------------------------------- *\
| Index_s WriteIndices(Index_t *list, Index_t length, FILE *ofp)
|
| Compress and write an array of Index_t
\* ----------------------------------------------------------------- */
Index_s WriteIndices(Index_t *list, Index_t length, FILE *ofp)
{
char *p;
Index_s n;
p = (char*)safemalloc(length * sizeof(Index_t),
"can't allocate index list", "");
n = CompressRefs(p, list, length);
NetOrderFwrite((void*)&n, sizeof(Index_s), 1, ofp);
if (fwrite(p, sizeof(char), n, ofp) != n)
{
perror("bibindex: cannot write indices; reason");
exit(EXIT_FAILURE);
}
free(p);
return n;
}
/* ----------------------------------------------------------------- *\
| void OutputTables(FILE *ofp)
|
| Compress and output the tables, with lots of user feedback.
| KLUDGE -- Passing strcmp to qsort assumes intimate knowledge of
| ExHashTable.
\* ----------------------------------------------------------------- */
void OutputTables(FILE *ofp)
{
register HashPtr words;
register int i, k;
long count, numwords, numrefs;
Index_t m;
numwords = numrefs = 0;
(void)printf("Writing index tables...");
fflush(stdout);
numfields = 0; /* recount, ignoring black holes */
for (i=0; i<MAXFIELDS; i++)
{
if (fieldtable[i].words)
{
if (i > (int)numfields)
{
fieldtable[(int)numfields] = fieldtable[i];/* copy ith table */
fieldtable[i].number = 0; /* then clear it */
fieldtable[i].size = 0; /* to avoid duplicate free() later */
fieldtable[i].words = NULL;
}
numfields++;
}
}
qsort(fieldtable, (size_t)numfields, sizeof(ExHashTable),
(int (*)(const void*,const void*))strcmp);
NetOrderFwrite((void *) &numfields, sizeof numfields, 1, ofp);
for (i=0; i < (int)numfields; i++)
WriteWord(ofp, fieldtable[i].thekey);
(void)printf("%d fields\n", (int)numfields);
for (k=0; k < (int)numfields; k++)
{
(void)printf("%3d. %-12s ", k+1, fieldtable[k].thekey);
fflush(stdout);
SortTable(&(fieldtable[k]));
NetOrderFwrite((void *) &(fieldtable[k].number),
sizeof(Index_t), 1, ofp);
count = 0;
words = fieldtable[k].words;
for (m=0; m<fieldtable[k].number; m++)
{
WriteWord(ofp, words[m].theword);
NetOrderFwrite((void *)&(words[m].number), sizeof(Index_s), 1, ofp);
WriteIndices(words[m].refs, words[m].number, ofp);
count += words[m].number;
}
(void)printf("%6ld words,%8ld refs,%7.2f refs/word\n",
(long)fieldtable[k].number, count,
(double)count/((fieldtable[k].number == 0) ?
1.0 : (double)fieldtable[k].number));
numwords += fieldtable[k].number;
numrefs += count;
}
(void)printf("--- TOTAL --- %7ld words,%8ld refs,%7.2f refs/word\n",
numwords, numrefs,
(double)numrefs/(double)((numwords == 0) ? 1 : numwords));
putchar('\n');
(void)printf("Writing abbrev table...");
fflush(stdout);
SortTable(abbrevtable);
NetOrderFwrite((void *)&(abbrevtable->number), sizeof(Index_t), 1, ofp);
words = abbrevtable->words;
for (m=0; m<abbrevtable->number; m++)
WriteWord(ofp, words[m].theword);
for (m=0; m<abbrevtable->number; m++)
NetOrderFwrite((void *)&(words[m].entry), sizeof(Index_t), 1, ofp);
(void)printf("%d+%d abbreviations\n",
abbrevtable->number - NUM_STD_ABBR, NUM_STD_ABBR);
}
/* ========================== MAIN PROGRAM ========================= */
/* ----------------------------------------------------------------- *\
| IndexBibFile(FILE *ifp, FILE *ofp, char *filename)
|
| Index a bibtex file. Input comes from ifp; output goes to ofp.
| Filename is the name of the bibliography, with no prefix.
\* ----------------------------------------------------------------- */
void IndexBibFile(FILE *ifp, FILE *ofp, char *filename)
{
Index_t count=0;
long curoffset;
Off_t *offsets;
Off_t *oldoff;
size_t offsize;
time_t now = time(0);
(void)printf("Indexing %s.bib.", filename);
fflush(stdout);
offsize = 128; /* MINIMUM OFFSET LIST SIZE */
offsets = (Off_t *) malloc(offsize * sizeof(Off_t));
while (!feof(ifp))
{
curoffset = FindNextEntry(ifp);
if (curoffset == (Off_t)-1)
break;
if (MungeEntry(ifp, count))
{
offsets[count++] = (Off_t)curoffset;
if (count == offsize) /* expand full offset array */
{
oldoff = offsets;
offsize *= 2;
offsets = (Off_t *) malloc(offsize * sizeof(Off_t));
bcopy(oldoff, offsets, count * sizeof(Off_t));
free(oldoff);
}
if (!(count % 200)) /* feedback */
{
if (count % 1000)
putchar('.');
else
(void)printf("%d.", count);
fflush(stdout);
}
}
}
(void)printf("done.\n");
(void)fprintf(ofp, "bibindex %d %d %d %s", FILE_VERSION, MAJOR_VERSION,
MINOR_VERSION, ctime(&now));
(void)printf("Writing offset table...");
fflush(stdout);
NetOrderFwrite((void *) &count, sizeof(Index_t), 1, ofp);
NetOrderFwrite((void *) offsets, sizeof(Off_t), count, ofp);
free(offsets);
(void)printf("%d entries\n", count);
OutputTables(ofp);
if (warnings)
{
(void)printf("\nWarning: %d problems were encountered.\n", warnings);
(void)printf("\t Biblook may give unexpected results.\n\n");
}
(void)printf("All done!\n");
}
/* ----------------------------------------------------------------- *\
| The main program
\* ----------------------------------------------------------------- */
int main(int argc, char **argv)
{
FILE *ifp;
FILE *ofp;
char infile[FILENAME_MAX+1];
char outfile[FILENAME_MAX+1];
char *p, *opts;
int i, inopt;
#if DEBUG_MALLOC
malloc_debug(2);
#endif /* DEBUG_MALLOC */
if (argc < 2)
die("Usage: bibindex bib [-i field...]", "");
if (((p = strrchr(argv[1],'.')) != (char*)NULL) &&
(strcmp(p, ".bib") == 0))
{
*p = '\0'; /* remove any .bib extension */
}
(void)sprintf(infile, "%s.bib", argv[1]);
(void)sprintf(outfile, "%s.bix", argv[1]);
ifp = fopen(infile, "r");
if (!ifp)
die("Can't read", infile);
#if MSDOS
ofp = fopen(outfile, "wb");
#else
ofp = fopen(outfile, "w");
#endif
if (!ofp)
die("Can't write", outfile);
InitTables();
StandardAbbrevs();
StandardBadWords();
if ((argc > 2) && (!strcmp(argv[2], "-i")))
{
for (i=3; i<argc; i++)
InitBlackHole(argv[i]);
}
else
{
opts = (char*)getenv("BIBINDEXFLAGS");
if (opts != NULL)
{
p = opts;
inopt = 0;
while (*p != 0)
{
if (isspace(*p))
{
if (inopt)
{
inopt = 0;
*p = 0;
if (strcmp(opts, "-i"))
InitBlackHole(opts);
opts = p+1;
}
}
else
inopt = 1;
p++;
}
if (inopt && strcmp(opts, "-i"))
InitBlackHole(opts);
}
}
IndexBibFile(ifp, ofp, argv[1]);
FreeTables();
fclose(ifp);
fclose(ofp);
exit(EXIT_SUCCESS); /* Argh! */
return (0); /* keep compilers happy */
}
|