File: biblook.c

package info (click to toggle)
bibindex 2.8-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 220 kB
  • ctags: 368
  • sloc: ansic: 3,274; makefile: 175; sh: 100
file content (1875 lines) | stat: -rw-r--r-- 50,353 bytes parent folder | download
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
/* ================================================================= *\

   biblook -- look up references in a bibindexed BibTeX file

   This program was specifically developed for use with the
   computational geometry bibliographic database.  The database
   can 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 biblook biblook.c

   Usage: biblook bibfile [savefile]

   -----------------------------------------------------------------

   HOW IT WORKS:

   The user can enter any of the following commands:

   f[ind] [not] <field> <words>
	Find the entries containing the given words in any field
	with a prefix matching the <field> argument.  For example,
	`a' matches both `author' and `address', and `au' matches
	`author' only.  If the <field> argument is `-' (or any
	string with no letters or numbers), match any field.

	If `not' appears before the <field>, the sense of the search
	is reversed.  The symbols `~' and `!' can be used in place
	of `not'.

	Each word is a contiguous sequence of letters and digits.
	Case is ignored; accents should be omitted; apostrophes are
	not required.  Single characters and a few common words are
	also ignored.  Any word ending with an asterisk is treated
	as a prefix.  Thus, `point*' matches `point', `points',
	`pointer', etc.

   and [not] <field> <words>
   or [not] <field> <words>
	Intersect (resp. union) the results of the given search
	with the previous search.  Several of these commands may be
	combined on a single line.  Commands are handled in the order
	in which they appear; there is no precedence.  Unlike other
	commands, and like `not', these must be spelled out
	completely.  `&' can be used in place of `and', and `|' can
	be used in place of `or'.

   d[isplay]
	Display the results of the previous search.

   s[ave] [<filename>]
	Save the results of the previous results into the specified
	file.  If <filename> is omitted, the previous save file is
	used.  If no save file has ever been specified, results are
	saved in the file specified on the command line.  If no such
	file is specified, `save.bib' is used.  If the save file
	exists, results are appended to it.

   w[hatis] <abbrev>
	Display the definition of the abbreviation <abbrev>.

   q[uit]/EOF
	Quit.

   Several commands can be combined on a single line by separating
   them with semicolons.  For example, the following command displays
   all STOC papers cowritten by Erdo"s without `Voronoi diagrams' in
   the title:

   f b stoc* | b symp* theory comp* & au erdos & ~t voronoi diagrams ; d

   -----------------------------------------------------------------
   Version history

   1.0 <jge> 3/29/92	Initial version complete
   1.1 <jge> 4/3/92	Fixed GetToken bug.
			Prompts and feedback messages sent to stderr
			instead of stdout, so results can be
			redirected to a file.

   2.0 <jge> 6/17/92	Major change in file format and commands.
	1. Allow searching on any field or all fields.
	2. More extensive boolean queries (and, or, not)
	3. New command to save results to a file
	4. New command to display results, rather than displaying
	   them automatically.
	5. Allow searching for prefixes
	6. Pipe display results through $PAGER or /usr/ucb/more
   2.1 <jge> 7/8/92	Minor bug fixes.
   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.
   2.4 Nelson H. F. Beebe <beebe@math.utah.edu> [01-Jun-1993]
	1. Remove some mixed-mode arithmetic.
	2. Add cast to return value of fork().
	3. Correct use and type of numoffsets so that code works
	   if Index_t is "unsigned int" or "unsigned long".
   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 bibindex: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 <jge> 8/29/93
	1. Simplified "help" output so it fits in one screen.
	2. Made help command act like a COMMAND -- "find help" no
	   longer displays the help message followed by an error
	   message.
	3. Fixed error with not -- "f not" followed by "f a erdos"
	   no longer finds all entries NOT written by Erdos!
	4. Added "safemalloc" routine from bibindex.
	5. Added "whatis" command to look up @strings.  Since @strings
	   are now indexed as if "@string" were a field, it's now
	   trivial to look up all abbreviations containing a given set
	   of words.
	6. Index lists are now read in only when they are required for
	   a search.  Only the CACHESIZE most recently accessed lists
	   are kept; older ones are freed.
	7. Added support for BIBLOOK environment variable, which
	   stores a search path for bib/bix files.  Defaults to
	   BIBINPUTS, or just the current directory if neither of
	   those exist.
       Nelson H. F. Beebe <beebe@math.utah.edu> [12-Sep-1993]
	8. Restore long form of help, offering short form first,
	   then long form if help is asked for a second time.  The
	   "telnet biblio" service on siggraph.org needs the long
	   form, since remote users cannot be expected to have a
	   biblook manual page.  Telling users to go read the manual
	   is unfriendly.
	9. Change "/usr/ucb/more" and "more" to compile-time settable
	   MOREPATH and MORE, and set Makefile to use "less" when available.
       10. Change type of numfields from char to unsigned char, and
           use sizeof numfields instead of sizeof(type of numfields).
       11. Change all exit() calls to use Standard C EXIT_xxx symbols.
       12. Put (void) casts on printf() family calls to avoid compiler
           warnings about discarded returned values.
       13. 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).
       14. Change &abbrevlocs to abbrevlocs in GetTables().  This error
           went undetected on 23 O/S-compiler combinations, but was
	   caught on DECstation ULTRIX with both gcc and g++, the only
	   one of these systems which is little-endian.  Network byte
	   order is big-endian, so ConvertToHostOrder() is a no-op
	   on such systems.
       Bill Jones <jones@cs.usask.ca> 93/09/29
       15. CACHESIZE of 8192 seems to keep runtime size within ~2.5 MB.
       16. MAXRESULTS limit on set display can be enabled if wanted.
       17. bixfile kept open all the time.
   2.7 Bill Jones <jones@cs.usask.ca> 94/02/10
	1. Changes for MSDOS use, from Guenter Rote.
   2.8 Bill Jones <jones@cs.usask.ca> 95/01/19
	1. Renovations for Index_t compression, and for 64-bit-long
	   portability typedefs and casts -- see bibindex for more details.

\* ================================================================= */


#include "biblook.h"

static char bibfile[FILENAME_MAX+1];
static char bixfile[FILENAME_MAX+1];

/* ======================= UTILITY FUNCTIONS ======================= */

/* ----------------------------------------------------------------- *\
|  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, "Error:  %s %s\n", msg1, msg2);
    exit(EXIT_FAILURE);
}

/* ----------------------------------------------------------------- *\
|  void pdie(const char *msg1, const char *msg2)
|
|  Print a custom error message and a system error message, then die.
\* ----------------------------------------------------------------- */
void pdie(const char *msg1, const char *msg2)
{
    char msg[256];
    (void)sprintf(msg, "%s %s", msg1, msg2);
    perror(msg);
    exit(EXIT_FAILURE);
}

/* ----------------------------------------------------------------- *\
|  void safefread(void *ptr, size_t size, size_t num, FILE *fp)
|
|  Read from the file, but die if there's an error.
\* ----------------------------------------------------------------- */
void safefread(void *ptr, size_t size, size_t num, FILE *fp)
{
    if (fread(ptr, size, num, fp) < num)
	pdie("Error reading", bixfile);
}

/* ----------------------------------------------------------------- *\
|  char safegetc(FILE *fp)
|
|  Get the next character safely.  Used by routines that assume that
|  they won't run into the end of file.
\* ----------------------------------------------------------------- */
char safegetc(FILE *fp)
{
    if (feof(fp))
	pdie("Error reading", bibfile);
    return getc(fp);
}

/* ----------------------------------------------------------------- *\
|  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)
	pdie(msg1, msg2);

    return tmp;
}

/* ----------------------------------------------------------------- *\
|  void ConvertToHostOrder(int n, int s, void *xx)
|
|  Convert n elements of size s to host-byteorder
\* ----------------------------------------------------------------- */
static void ConvertToHostOrder(size_t n, size_t s, void *xx)
{
    uint16 *x = (uint16 *)xx;
    uint32 *y = (uint32 *)xx;

    if (s == sizeof(uint16))
    {
	while (n-- > 0)
	{
	    *x = ntohs(*x);
	    x++;
	}
    }
    else			/* assume s == sizeof(uint32) */
    {
	while (n-- > 0)
	{
	    *y = ntohl(*y);
	    y++;
	}
    }
}

/* ----------------------------------------------------------------- *\
|  void UncompressRefs(Index_t *list, char *p, Index_t length)
|
|  Uncompress a sequence of Index_t.  See bibindex for algorithm.
\* ----------------------------------------------------------------- */
void UncompressRefs(Index_t *list, char *p, Index_t length)
{
    Index_t prevref = (Index_t)-1;
    Index_t diff;
    char bits, highbit;
    int shift;

    while (length-- > 0)
    {
	diff = 0;
	shift = 0;
	do {
	    bits = *p++;
	    highbit = bits & CHAR_HIGHBIT;
	    bits &= ~CHAR_HIGHBIT;
	    diff |= bits << shift;
	    shift += CHAR_BIT - 1;
	} while (highbit);
	*list = prevref + diff;
	prevref = *list++;
    }
}


/* ============================== CACHE ============================ *\

   In the interest of saving memory, starting with version 2.6,
   index lists are now cached.  The cache is a priority queue,
   implemented as a simple binary heap, stored in an array.  The cache
   contains pointers to the real lists, and each list contains its
   rank in the heap.  The heap is prioritized by least recent use.

   Making the rank field an unsigned char almost certainly doesn't
   save anything, since the data will almost certainly have to be
   word- or long-aligned.  [2.6 [NHFB]: change rank type to int, and
   reorder struct to have largest items first, because of
   memory-alignment requirements of many architectures.]

\* ================================================================= */

#define CACHESIZE 8192

typedef struct
{
    long          offset;	/* offset into index file  */
    Index_s       length;	/* length of the list	   */
    Index_s       bytes;	/* length when compressed  */
    char         *list; 	/* compressed list or NULL */
    int		  rank; 	/* back pointer into cache */
} CachedList;

typedef struct
{
    long	stamp;	/* time stamp */
    CachedList *clist;  /* the cached list */
} CacheElement;

CacheElement cache[CACHESIZE];
long curstamp;		/* current "time stamp" */
int cachenum;		/* number of elements in the cache */

/* ----------------------------------------------------------------- *\
|  void InitCache(VOID)
|
|  Initialize the cache.
\* ----------------------------------------------------------------- */
void InitCache(VOID)
{
    int i;

    for (i=0; i<CACHESIZE; i++)
    {
	cache[i].stamp = -1;
	cache[i].clist = NULL;
    }
    curstamp = 0;
}

/* ----------------------------------------------------------------- *\
|  void InitCachedList(CachedList *clist, FILE *ifp)
|
|  Initialize the cached list.  Read the lengths of the list from the
|  file, record the position of the list within the file, and skip
|  over the list itself.
\* ----------------------------------------------------------------- */
void InitCachedList(CachedList *clist, FILE *ifp)
{
    Index_s num;

    clist->list = NULL;

    safefread((void *) &num, sizeof(Index_s), 1, ifp);
    ConvertToHostOrder(1, sizeof (Index_s), &num);
    clist->length = num;
    safefread((void *) &num, sizeof(Index_s), 1, ifp);
    ConvertToHostOrder(1, sizeof (Index_s), &num);
    clist->bytes = num;

    clist->offset = ftell(ifp);
    if (fseek(ifp, (long)num, SEEK_CUR) != 0)
	pdie("Error reading", bixfile);
}

/* ----------------------------------------------------------------- *\
|  void FreeCache(void)
|
|  Free everything in the cache.
\* ----------------------------------------------------------------- */
void FreeCache(VOID)
{
    int i;

    for (i=0; i<cachenum; i++)
	free(cache[i].clist->list);
}

/* ----------------------------------------------------------------- *\
|  void CheckStamp(void)
|
|  Avoid emacs's timestamp wraparound bug!!
\* ----------------------------------------------------------------- */
void CheckStamp(VOID)
{
    int i;

    if (curstamp < 0)
    {
	for (i=0; i<cachenum; i++)
	    cache[i].stamp = i;		/* Changes time stamp order! */
	curstamp = cachenum;
	(void)printf("You've been running biblook a long time, haven't you?\n");
    }
}

/* ----------------------------------------------------------------- *\
|  void HeapBubble(int which)
|
|  Bubble the given element down into its place in the heap.
\* ----------------------------------------------------------------- */
void HeapBubble(int which)
{
    register int i;
    CacheElement tmp;

    i = which;
    tmp = cache[i];

    while (2*i+1 < cachenum)		/* while I still have children */
    {
	if ((2*i+2 == cachenum) ||
	    (cache[2*i+1].stamp < cache[2*i+2].stamp))
	{
	    cache[i] = cache[2*i+1];	/* go to left child */
	    cache[i].clist->rank = i;
	    i = 2*i+1;
	}
	else
	{
	    cache[i] = cache[2*i+2];	/* go to right child */
	    cache[i].clist->rank = i;
	    i = 2*i+2;
	}
    }

    cache[i] = tmp;
    cache[i].clist->rank = i;
}

/* ----------------------------------------------------------------- *\
|  void Access(CachedList *clist, FILE *ifp)
|
|  Make sure clist is in memory.  Make sure the given element is in
|  its rightful place in the cache.  If it's already there, just move
|  it.  Otherwise, insert it into the heap, deleting the oldest
|  element if the cache is already full.
\* ----------------------------------------------------------------- */
void Access(CachedList *clist, FILE *ifp)
{
    if (clist->list == NULL)
    {
	if (fseek(ifp, clist->offset, SEEK_SET) != 0)
	    pdie("Error reading", bixfile);

	clist->list = (char *)safemalloc(clist->bytes,
		"Can't allocate index list.", "");
	safefread((void *)clist->list, sizeof(char), clist->bytes, ifp);

	if (cachenum == CACHESIZE)		/* if cache is full... */
	{
	    free(cache[0].clist->list);		/* delete oldest element */
	    cache[0].clist->list = NULL;

	    cache[0] = cache[CACHESIZE-1];
	    cache[CACHESIZE-1].clist = NULL;

	    cachenum--;
	    HeapBubble(0);
	}

	cache[cachenum].stamp = curstamp++;
	cache[cachenum].clist = clist;
	clist->rank = cachenum++;
    }
    else
    {
	cache[clist->rank].stamp = curstamp++;
	HeapBubble(clist->rank);
    }

    CheckStamp();
}


/* ========================== INDEX TABLES ========================= */

typedef struct
{
    Word       theword;
    CachedList refs;
} Index, *IndexPtr;

typedef struct
{
    Word     thefield;
    Index_t  numwords;
    IndexPtr words;
} IndexTable;

Index_s numfields;
IndexTable *fieldtable;

Index_t numabbrevs;
Word *abbrevs;
Index_t *abbrevlocs;

Index_t numoffsets;
Off_t *offsets;

/* ----------------------------------------------------------------- *\
|  void ReadWord(FILE *ifp, Word word)
|
|  Read a "pascal" string into the given buffer
\* ----------------------------------------------------------------- */
void ReadWord(FILE *ifp, Word word)
{
    unsigned char length;

    safefread((void *)&length, sizeof(unsigned char), 1, ifp);
    if (length > MAXWORD)
	die("Index file is corrupt", "(word too long).");

    safefread((void *)word, sizeof(char), length, ifp);
    word[length] = 0;

#if DEBUG
    (void)printf("word:%02d = [%s]\n", (int)length, word);
#endif /* DEBUG */

}

/* ----------------------------------------------------------------- *\
|  void GetOneTable(FILE *ifp, IndexTable *table)
|
|  Get one index table from the file
\* ----------------------------------------------------------------- */
void GetOneTable(FILE *ifp, IndexTable *table)
{
    Index_t i;

    safefread((void *)&table->numwords, sizeof(Index_t), 1, ifp);
    ConvertToHostOrder(1, sizeof(Index_t), &table->numwords);
    table->words = (IndexPtr)safemalloc(table->numwords*sizeof(Index),
			      "Can't create index table for", table->thefield);

    for (i=0; i<table->numwords; i++)
    {
	ReadWord(ifp, table->words[i].theword);
	InitCachedList(&(table->words[i].refs), ifp);
    }
}

FILE *bixfp;

/* ----------------------------------------------------------------- *\
|  void GetTables(VOID)
|
|  Get the tables from the index file.
\* ----------------------------------------------------------------- */
void GetTables(VOID)
{
    int version, i;
    Index_t k;

    if (fscanf(bixfp, "bibindex %d %*[^\n]%*c", &version) < 1)
	die(bixfile, "is not a bibindex file!");
    if (version < FILE_VERSION)
	die(bixfile, "is the wrong version.\n\tPlease rerun bibindex.");
    if (version > FILE_VERSION)
	die(bixfile, "is the wrong version.\n\tPlease recompile biblook.");

    InitCache();

    safefread((void *) &numoffsets, sizeof(Index_t), 1, bixfp);
    ConvertToHostOrder(1, sizeof(Index_t), &numoffsets);
    offsets = (Off_t *) safemalloc(numoffsets * sizeof(Off_t),
				  "Can't create offset table", "");

    safefread((void *) offsets, sizeof(Off_t), numoffsets, bixfp);
    ConvertToHostOrder(numoffsets, sizeof(Off_t), offsets);

    safefread((void *) &numfields, sizeof numfields, 1, bixfp);
    ConvertToHostOrder(1, sizeof numfields, &numfields);
    fieldtable = (IndexTable *) safemalloc(numfields * sizeof(IndexTable),
					   "Can't create field table", "");

    for (i=0; i < (int)numfields; i++)
	ReadWord(bixfp, fieldtable[i].thefield);
    for (i=0; i < (int)numfields; i++)
	GetOneTable(bixfp, fieldtable+i);

    safefread((void *) &numabbrevs, sizeof(Index_t), 1, bixfp);
    ConvertToHostOrder(1, sizeof(Index_t), &numabbrevs);

    abbrevs = (Word *) safemalloc(numabbrevs * sizeof(Word),
				  "Can't create abbreviation table", "");
    for (k=0; k<numabbrevs; k++)
	ReadWord(bixfp, abbrevs[k]);

    abbrevlocs =
	(Index_t *) safemalloc(numabbrevs * sizeof(Index_t),
			       "Can't create abbrev offset table", "");
    safefread((void *) abbrevlocs, sizeof(Index_t), numabbrevs, bixfp);
    ConvertToHostOrder(numabbrevs, sizeof(Index_t), abbrevlocs);
}

/* ----------------------------------------------------------------- *\
|  void FreeTables(void)
|
|  Free the index tables.
\* ----------------------------------------------------------------- */
void FreeTables(VOID)
{
    register int i;

    FreeCache();	/* free all index lists in memory */

    for (i=0; i < (int)numfields; i++)
	free(fieldtable[i].words);

    free(fieldtable);
    free(offsets);
}


/* ----------------------------------------------------------------- *\
|  Index_t FindIndex(IndexTable table, char *word, char prefix)
|
|  Find the index of a word in a table.  Return INDEX_NAN if the word
|  isn't there.  If prefix is true, return the index of the first
|  matching word.
\* ----------------------------------------------------------------- */
Index_t FindIndex(IndexTable table, char *word, char prefix)
{
    register IndexPtr words = table.words;
    register int hi, lo, mid;	/* must be signed */
    register int cmp;

    hi = table.numwords-1;
    lo = 0;

    while (hi>=lo)
    {
	mid = (hi+lo)/2;
	cmp = strcmp(word, words[mid].theword);

	if (cmp == 0)
	    return (Index_t)mid;
	else if (cmp < 0)
	    hi = mid-1;
	else if (cmp > 0)
	    lo = mid+1;
    }

    if (prefix && !strncmp(word, words[lo].theword, strlen(word)))
	return (Index_t)lo;
    else
	return (Index_t)INDEX_NAN;
}


/* ----------------------------------------------------------------- *\
|  Index_t FindAbbrev(char *word)
|
|  Find the index of an abbrev in the abbrev table.  Return INDEX_NAN
|  if the abbrev isn't there.
\* ----------------------------------------------------------------- */
Index_t FindAbbrev(register char *word)
{
    register int hi, lo, mid;	/* must be signed */
    register int cmp;

    hi = numabbrevs-1;
    lo = 0;

    while (hi>=lo)
    {
	mid = (hi+lo)/2;
	cmp = strcmp(word, abbrevs[mid]);

	if (cmp == 0)
	    return (Index_t)mid;
	else if (cmp < 0)
	    hi = mid-1;
	else if (cmp > 0)
	    lo = mid+1;
    }

    return (Index_t)INDEX_NAN;
}


/* =================== SET MANIPULATION ROUTINES =================== */

static Index_t setsize;
typedef unsigned long Set_t, *Set;
static Set_t setmask;		   /* used to erase extra bits */

#define SETSCALE (sizeof(Set_t)*8)

/* ----------------------------------------------------------------- *\
|  Set NewSet(void)
|
|  Get a new variable to hold sets of integers in the range
|  [0, numoffsets].  Set setsize and setmask.
\* ----------------------------------------------------------------- */
Set NewSet(VOID)
{
    setsize = (numoffsets + SETSCALE - 1)/SETSCALE;	/* HACK */
    setmask = ((Set_t)1<<(numoffsets%SETSCALE)) - 1;		/* KLUDGE */

    return (Set) safemalloc(setsize * SETSCALE,
			    "Can't create new result list", "");
}

/* ----------------------------------------------------------------- *\
|  void EmptySet(Set theset)
|
|  Empty the set.
\* ----------------------------------------------------------------- */
void EmptySet(Set theset)
{
    register Index_t i;
    for (i=0; i<setsize; i++)
	theset[i] = 0;
}

/* ----------------------------------------------------------------- *\
|  void SetUnion(Set src1, Set src2, Set result)
|
|  Get the union of two sets
\* ----------------------------------------------------------------- */
void SetUnion(Set src1, Set src2, Set result)
{
    register Index_t i;
    for (i=0; i<setsize; i++)
	result[i] = src1[i] | src2[i];
}

/* ----------------------------------------------------------------- *\
|  void SetIntersection(Set src1, Set src2, Set result)
|
|  Get the intersection of two sets
\* ----------------------------------------------------------------- */
void SetIntersection(Set src1, Set src2, Set result)
{
    register Index_t i;
    for (i=0; i<setsize; i++)
	result[i] = src1[i] & src2[i];
}

/* ----------------------------------------------------------------- *\
|  void SetComplement(Set src, Set result)
|
|  Get the complement of a set
\* ----------------------------------------------------------------- */
void SetComplement(Set src, Set result)
{
    register Index_t i;
    for (i=0; i<setsize; i++)
	result[i] = ~src[i];
    /* Bug fixed at version 2.5: setmask == 0 if numoffsets is  */
    /* a multiple of setsize, so the set contains no partial words, */
    /* The clearing of trailing bits must then be omitted. */
    if (setmask)
	result[setsize-1] &= setmask;	/* clear those last few bits */
}

/* ----------------------------------------------------------------- *\
|  void CopySet(Set src, Set result)
|
|  Copy one set into another
\* ----------------------------------------------------------------- */
void CopySet(Set src, Set result)
{
    register Index_t i;
    for (i=0; i<setsize; i++)
	result[i] = src[i];
}


/* ----------------------------------------------------------------- *\
|  int CountSet(Set theset)
|
|  Get the cardinality of the set
\* ----------------------------------------------------------------- */
int CountSet(Set theset)
{
    register Index_t i, count;
    register unsigned int j;

    count = 0;
    for (i=0; i<setsize; i++)
	for (j=0; j<SETSCALE; j++)
	    if (theset[i] & ((Set_t)1<<j))
		count++;

    return count;
}

/* ----------------------------------------------------------------- *\
|  void BuildSet(Set theset, Index_t *thelist, Index_t length)
|
|  Build a set out of a list of integers
\* ----------------------------------------------------------------- */
void BuildSet(Set theset, Index_t *thelist, Index_t length)
{
    register Index_t i;

    EmptySet(theset);
    for (i=0; i<length; i++)
	theset[thelist[i]/SETSCALE] |= (Set_t)1 << (thelist[i] % SETSCALE);
}

/* ----------------------------------------------------------------- *\
|  void DoForSet(Set theset, void (*action)(int, void *), void *arg)
|
|  Do something to every element in a set
\* ----------------------------------------------------------------- */
void DoForSet(Set theset, void (*action)(int, void *), void *arg)
{
    register Index_t i;
    register unsigned int j;

    for (i=0; i<setsize; i++)
	for (j=0; j<SETSCALE; j++)
	    if (theset[i] & ((Set_t)1<<j))
		(*action)((int)(SETSCALE*i + j), arg);
}


/* ======================== SEARCH ROUTINES ======================== */

Set results, oldresults, oneword, onefield;
short firstfield, lastfield;		/* indices into fieldtable */

/* ----------------------------------------------------------------- *\
|  void InitSearch(void)
|
|  Initialize the search lists
\* ----------------------------------------------------------------- */
void InitSearch(VOID)
{
    results = NewSet();
    oldresults = NewSet();
    oneword = NewSet();
    onefield = NewSet();
    firstfield = lastfield = -1;
}

/* ----------------------------------------------------------------- *\
|  void FreeSearch(void)
|
|  Free the search list
\* ----------------------------------------------------------------- */
void FreeSearch(VOID)
{
    free(results);
    free(oldresults);
    free(oneword);
    free(onefield);
}

/* ----------------------------------------------------------------- *\
|  void ClearResults(void)
|
|  Clear the current and old results
\* ----------------------------------------------------------------- */
void ClearResults(VOID)
{
    EmptySet(results);
    SetComplement(results, results);
    CopySet(results, oldresults);
}

/* ----------------------------------------------------------------- *\
|  void SaveResults(void)
|
|  Save and clear the current results
\* ----------------------------------------------------------------- */
void SaveResults(VOID)
{
    CopySet(results, oldresults);
    EmptySet(results);
    SetComplement(results, results);
}

/* ----------------------------------------------------------------- *\
|  void CombineResults(char invert, char intersect)
|
|  Combine current results with old results
\* ----------------------------------------------------------------- */
void CombineResults(char invert, char intersect)
{
    if (invert)
	SetComplement(results, results);
    if (intersect)
	SetIntersection(results, oldresults, results);
    else
	SetUnion(results, oldresults, results);
}


/* ----------------------------------------------------------------- *\
|  char SetUpField(char *field)
|
|  Set up the search fields.  Return the number of searchable fields.
\* ----------------------------------------------------------------- */
char SetUpField(char *field)
{
    int i, len;

    firstfield = -1;
    len = strlen(field);

    for (i=0; i < (int)numfields; i++)
    {
	if (!strncmp(field, fieldtable[i].thefield, len))
	{
	    if (firstfield == -1)
		firstfield = i;
	    lastfield = i;
	}
    }

    if (firstfield == -1)
    {
	(void)printf("\tNo searchable fields matching \"%s\".\n", field);
	return 0;
    }
    else
	return lastfield - firstfield + 1;

}

const char *badwords[] = BADWORDS;
/* ----------------------------------------------------------------- *\
|  void FindWord(char *word, char prefix)
|
|  Find a word in the currently active field and update `results'.
|  If the prefix flag is set, find all words having the given prefix.
\* ----------------------------------------------------------------- */
void FindWord(register char *word, char prefix)
{
    register IndexPtr words;
    Index_t win, len;
    int i;

    if (!prefix)
    {
	if (!word[0])
	{
	    (void)printf("\t[ignoring empty string]\n");
	    return;
	}
	if (!word[1])
	{
	    (void)printf("\t[ignoring single letter \"%s\"]\n", word);
	    return;
	}
#if !IGNORENONE
	for (i=0; badwords[i]; i++)
	{
	    if (!strcmp(badwords[i], word))
	    {
		(void)printf("\t[ignoring common word \"%s\"]\n", word);
		return;
	    }
	}
#endif
    }

    EmptySet(oneword);
    len = strlen(word);

    for (i=firstfield; i<=lastfield; i++)
    {
	words = fieldtable[i].words;
	win = FindIndex(fieldtable[i], word, prefix);

	if (win != INDEX_NAN)
	{
	    do {
		CachedList *clist = &(words[win].refs);
		Index_t *p;

		Access(clist, bixfp);
		p = (Index_t *)safemalloc(clist->length * sizeof(Index_t),
			"Can't allocate entry list.", "");
		UncompressRefs(p, clist->list, clist->length);
		BuildSet(onefield, p, clist->length);
		SetUnion(oneword, onefield, oneword);
		free(p);
	    } while (prefix && ++win < fieldtable[i].numwords &&
		    !strncmp(words[win].theword, word, len));
	}
    }

    SetIntersection(oneword, results, results);
}


/* ============================= OUTPUT ============================ */
FILE *bibfp;

/* ----------------------------------------------------------------- *\
|  void ReportResults(void)
|
|  Report the results of the previous search.
\* ----------------------------------------------------------------- */
void ReportResults(VOID)
{
    int numresults;

    numresults = CountSet(results);

    if (numresults == 0)
	(void)printf("\tNo matches found.\n");
    else if (numresults == 1)
	(void)printf("\t1 match found.\n");
    else
	(void)printf("\t%d matches found.\n", numresults);
}

/* ----------------------------------------------------------------- *\
|  void PrintEntry(int entry, FILE *ofp)
|
|  Print the entry.
\* ----------------------------------------------------------------- */
void PrintEntry(int entry, FILE *ofp)
{
    char ch;
    char braces;
    char quotes;

    if (entry >= (int)numoffsets)	/* extra bits might be set */
	return;

    putc('\n', ofp);
    if (fseek(bibfp, offsets[entry], 0))
	die("Index file is corrupt.", "");

    ch = safegetc(bibfp);

    while (ch != '@')
    {
	putc(ch, ofp);
	ch = safegetc(bibfp);
    }
	while ((ch != '{') && (ch != '('))
    {
	putc(ch, ofp);
	ch = safegetc(bibfp);
    }

    braces = quotes = 0;

    putc(ch, ofp);
    ch = safegetc(bibfp);
    while (braces || quotes || ((ch != '}') && (ch != ')')))
    {
	if (ch == '{')
	    braces++;
	else if (ch == '}')
	    braces--;
	else if ((ch == '"') && !braces)
	    quotes = !quotes;
	putc(ch, ofp);
	ch = safegetc(bibfp);
    }

    putc(ch, ofp);
    putc('\n', ofp);
}

/* ----------------------------------------------------------------- *\
|  void PrintResults(char *filename)
|
|  Print the current search results into the given file.  If the
|  filename is NULL, pipe the output through $PAGER.
\* ----------------------------------------------------------------- */
void PrintResults(char *filename)
{
    int numresults;
    FILE *ofp;
    char *pager;
    char *the_tmpfile = (char*)NULL;
#if unix
    int childpid;
#else
    char *command, *command_fname;
#endif

    numresults = CountSet(results);
    if (numresults == 0)
	(void)printf("\tNothing to display!\n");
#if MAXRESULTS
    else if (numresults > MAXRESULTS)
	(void)printf("\tI can't display that many results!\n");
#endif
    else
    {
	if (filename)
	{
	    ofp = fopen(filename, "a");
	    if (!ofp)
	    {
		(void)printf("\tCan't open %s: ", filename);
		perror(NULL);
		return;
	    }
	}
	else
	{
	    the_tmpfile = (char*)tempnam(NULL, "bibl.");
	    ofp = fopen(the_tmpfile, "w");
	    if (!ofp)
	    {
		perror("\tCan't open temp file");
		return;
	    }
	}

	if (filename)
	{
	    time_t now = time(0);
	    (void)fprintf(ofp, "%% Retrieved by biblook %d.%d at %s",
		    MAJOR_VERSION, MINOR_VERSION, ctime(&now));
	}

	DoForSet(results, (void (*)(int, void *)) PrintEntry,
		 (void *) ofp);

	fclose(ofp);
	if (filename)
	    (void)printf("\tResults saved in \"%s\"\n", filename);
	else
	{
	    pager = (char*)getenv("PAGER");

#if unix
	    if ((childpid = (int)fork()) != 0)
		waitpid(childpid, (int *) 0, 0);
	    else if (pager)
	    {
		execlp(pager, pager, the_tmpfile, (char *) 0);
		perror(pager);		/* should never get here! */
		exit(EXIT_SUCCESS);
	    }
	    else
	    {
		/* try absolute path first */
		execl(MOREPATH, MORE, the_tmpfile, (char *) 0);
		/* next try to find it in PATH list */
		execlp(MORE, MORE, the_tmpfile, (char *) 0);
		/* no pager available, so give up */
		perror(MOREPATH);
		exit(EXIT_SUCCESS);
	    }
#elif MSDOS
	    if (pager)
	    {
		command = (char *) malloc(strlen(pager) + 2 + strlen(the_tmpfile));
		strcpy(command, pager);
		strcat(command, " ");
	    }
	    else
	    {
		command = (char *) malloc(7 + strlen(the_tmpfile));
		strcpy(command, "MORE <");
	    }

	    command_fname = command + strlen(command);
	    strcat(command, the_tmpfile);
	    for (; *command_fname != '\0' ; command_fname++)
		if (*command_fname == '/')
		    *command_fname = '\\';

	    if (system(command))
		perror( (pager? pager : "MORE") );
	    free(command);
#else  /* ? other systems ? */
#endif

	    unlink(the_tmpfile);
	    free(the_tmpfile);	    	/* malloc'ed by tempnam() */
	    putchar('\n');
	}
    }
}

/* ----------------------------------------------------------------- *\
|  void DisplayAbbrev(char *theabbrev)
|
|  Display the definition of the given abbreviation.
\* ----------------------------------------------------------------- */
void DisplayAbbrev(char *theabbrev)
{
    Index_t the_index = FindAbbrev(theabbrev);

    if (the_index == INDEX_NAN)
	(void)printf("\tThe abbreviation \"%s\" is not defined.\n", theabbrev);
    else if (abbrevlocs[the_index] == INDEX_BUILTIN)
	(void)printf("\tThe abbreviation \"%s\" is builtin.\n", theabbrev);
    else
    {
	PrintEntry((int)abbrevlocs[the_index], stdout);
	putchar('\n');
    }
}


/* ======================== USER INTERFACE ========================= */

typedef enum {
    T_Find, T_Whatis, T_Display, T_Save, T_Quit, T_Word,
    T_And, T_Or, T_Not, T_Semi, T_Return, T_Help
} Token;

/* ----------------------------------------------------------------- *\
|  Token GetToken(char *tokenstr)
|
|  Get the next input token.
\* ----------------------------------------------------------------- */
Token GetToken(char *tokenstr)
{
    static char line[256];
    static short pos;
    static char neednew = 1;
    short tlen = 0;

    *tokenstr = 0;

    if (neednew)
    {
	(void)printf("biblook: ");
	if (!fgets(line, 254, stdin))
	    return T_Quit;

	pos = 0;
	neednew = 0;
    }

    while ((line[pos] == ' ') || (line[pos] == '\t'))
	pos++;

    switch (line[pos])
    {
	case '\n':
	    pos++;
	    neednew = 1;
	    return T_Return;

	case '&':
	    pos++;
	    return T_And;

	case '|':
	    pos++;
	    return T_Or;

	case '~':
	case '!':
	    pos++;
	    return T_Not;

	case ';':
	    pos++;
	    return T_Semi;

	case '?':
	    pos++;
	    return T_Help;

	default:
	    tokenstr[tlen++] = tolower(line[pos++]);
	    while (!isspace(line[pos]) && (line[pos] != ';') &&
		   (line[pos] != '&') && (line[pos] != '|'))
	    {
		tokenstr[tlen++] = tolower(line[pos++]);
	    }
	    tokenstr[tlen] = 0;

	    /* I really ought to use a hash table here. */

	    if (!strncmp(tokenstr, "find", tlen))
		return T_Find;
	    else if (!strncmp(tokenstr, "display", tlen))
		return T_Display;
	    else if (!strncmp(tokenstr, "help", tlen))
		return T_Help;
	    else if (!strncmp(tokenstr, "save", tlen))
		return T_Save;
	    else if (!strncmp(tokenstr, "whatis", tlen))
		return T_Whatis;
	    else if (!strncmp(tokenstr, "quit", tlen))
		return T_Quit;
	    else if (!strcmp(tokenstr, "and"))
		return T_And;
	    else if (!strcmp(tokenstr, "or"))
		return T_Or;
	    else if (!strcmp(tokenstr, "not"))
		return T_Not;
	    else
		return T_Word;
    }
}


/* ----------------------------------------------------------------- *\
|  char Strip(char *string)
|
|  Strip all but alphanumeric characters out of the string.  Return
|  true if the original string ended with the prefix character '*'.
\* ----------------------------------------------------------------- */
char Strip(char *string)
{
    char prefix = 0;
    char *src = string;

    while (*src)
    {
	prefix = (*src == '*');
	if (isalnum(*src))
	    *string++ = *src;
	src++;
    }
    *string = 0;
    return prefix;
}

/* ----------------------------------------------------------------- *\
|  void CmdError(void)
|
|  Print syntax error message
\* ----------------------------------------------------------------- */
void CmdError(VOID)
{
    (void)printf("\t?? Syntax error ??\n");
}

static const char* shorthelplines[] =
{
    "------------------------------------------------------------",
    "help			Print this message",
    "find <field> <words>	Find entries with <words> in <field>",
    "and  <field> <words>	Narrow search",
    "or   <field> <words>	Widen search",
    "display			Display search results",
    "save <file>		Save search results to <file>",
    "whatis <abbrev>		Find and display an abbreviation",
    "quit			Quit biblook",
    "------------------------------------------------------------",
    "Type `help' or `?' again for more details.",
    (const char *) NULL,
};

static const char* longhelplines[] =
{
    "biblook permits rapid lookup in a BibTeX bibliography data",
    "base, using a compact binary index file prepared by bibindex(1).",
    "",
    "Available commands:",
    "? or h[elp]",
    "     Display this help message.",
    "",
    "f[ind] [not] <field> <words>",
    "     Find the entries containing the given words in any",
    "     field with a prefix matching the <field> argument.  For",
    "     example, `a' matches both `author' and `address', and",
    "     `au' matches `author' only.  If the <field> argument is",
    "     `-' (or any string with no letters or numbers), match",
    "     any field.",
    "",
    "     If `not' appears before the <field>, the sense of the",
    "     search is reversed.  The symbols `~' and `!' can be",
    "     used in place of `not'.",
    "",
    "     Each word is a contiguous sequence of letters and",
    "     digits.  Case is ignored; accents should be omitted;",
    "     apostrophes are not required.  Single characters and a",
    "     few common words are also ignored.  Any word ending",
    "     with an asterisk is treated as a prefix.  Thus,",
    "     `point*' matches `point', `points', `pointer', etc.",
    "",
    "and [not] <field> <words>",
    "or [not] <field> <words>",
    "     Intersect (resp. union) the results of the given search",
    "     with the previous search.  Several of these commands",
    "     may be combined on a single line.  Commands are handled",
    "     in the order in which they appear; there is no pre-",
    "     cedence.  Unlike other commands, and like `not', these",
    "     must be spelled out completely.  `&' can be used in",
    "     place of `and', and `|' can be used in place of `or'.",
    "",
    "d[isplay]",
    "     Display the results of the previous search.",
    "",
    "s[ave] [<filename>]",
    "     Save the results of the previous results into the",
    "     specified file.  If <filename> is omitted, the previous",
    "     save file is used.  If no save file has ever been",
    "     specified, results are saved in the file specified on",
    "     the command line.  If no such file is specified,",
    "     `save.bib' is used.  If the save file exists, results",
    "     are appended to it.",
    "",
    "w[hatis] <abbrev>",
    "     Display the definition of the abbreviation <abbrev>.",
    "",
    "q[uit]/EOF",
    "     Quit.",
    "",
    "Several commands can be combined on a single line by",
    "separating them with semicolons.  For example, the following",
    "command displays all STOC papers cowritten by Erdo\"s",
    "without `Voronoi diagrams' in the title:",
    "",
    "f b stoc* | b symp* theory comp* & au erdos & ~t voronoi diagrams ; d",
    "",
    (const char*)NULL,
};

/* ----------------------------------------------------------------- *\
|  void GiveHelp(int verbose)
|
|  Print a help message.  Lines are stored as separate strings to
|  avoid hitting compiler limits.
\* ----------------------------------------------------------------- */
void GiveHelp(int verbose)
{
    int k;
#if 0
    /* This fails on IBM RS/6000 AIX 3.2 cc with bogus error:
       "Operands must be pointers to compatible types." */
    const char **help = verbose ? &longhelplines[0] : &shorthelplines[0];
#else
    const char **help;

    if (verbose)
        help = &longhelplines[0];
    else
	help = &shorthelplines[0];
#endif

    for (k = 0; help[k]; ++k)
	(void)printf("\t%s\n", help[k]);
}

/* ----------------------------------------------------------------- *\
|  States for Lookup()
\* ----------------------------------------------------------------- */
typedef enum {
    Wait,	/* nothing yet 			*/
    Find,	/* "find"			*/
    FindN,	/* "find not"			*/
    FindF,	/* "find [not] <field>"		*/
    FindW,	/* "find [not] <field> <words>"	*/
    Display,	/* "display"			*/
    Save,	/* "save"			*/
    SaveF,	/* "save <file>"		*/
    Whatis,	/* "whatis"			*/
    WhatisA,	/* "whatis <abbrev>"		*/
    Help,	/* "help"			*/
    Error	/* anything else		*/
} CmdState;

/* ----------------------------------------------------------------- *\
|  void Lookup(const char *defsave)
|
|  Execute commands until the user quits.  Defsave is the default
|  save file name.  This is one big finite state machine.  It's long
|  and boring, but that's interface code for ya!
\* ----------------------------------------------------------------- */
void Lookup(const char *defsave)
{
    char tokenstr[256];
    char savestr[256];
    CmdState state = Wait;
    static CmdState last_state = Wait;

    Token thetoken;
    char intersect = 1;		/* 1 = intersect, 0 = union */
    char invert = 0;		/* 1 = invert */
    char prefix;		/* 1 = word is really a prefix */

    ClearResults();
    strcpy(savestr, defsave);

    for (;;)
    {
	thetoken = GetToken(tokenstr);

	if ((thetoken == T_Quit) && !tokenstr[0])
	    return;

	switch (state)
	{
	    case Wait:
		switch (thetoken)
		{
		    case T_Quit:
			return;
		    case T_Find:
			state = Find;
			invert = 0;
			ClearResults();
			break;
		    case T_And:
			state = Find;
			invert = 0;
			SaveResults();
			break;
		    case T_Or:
			state = Find;
			invert = 0;
			intersect = 0;
			SaveResults();
			break;
		    case T_Display:
			state = Display;
			break;
		    case T_Save:
			state = Save;
			break;
		    case T_Help:
			state = Help;
			break;
		    case T_Whatis:
			state = Whatis;
			break;
		    case T_Return:
		    case T_Semi:
			break;
		    default:
			state = Error;
			CmdError();
			break;
		}
		break;

	    case Find:
		if (thetoken == T_Not)
		{
		    last_state = state;
		    state = FindN;
		    invert = 1;
		}
		else
		{
		    if (tokenstr[0])
		    {
			state = FindF;
			Strip(tokenstr);
			if (!SetUpField(tokenstr))
			    state = Error;
			else
			    last_state = Find;
		    }
		    else
		    {
			state = (thetoken == T_Return) ? Wait : Error;
			CmdError();
		    }
		}
		break;

	    case FindN:
		if (tokenstr[0])
		{
		    state = FindF;
		    Strip(tokenstr);
		    if (!SetUpField(tokenstr))
			state = Error;
		    else
			last_state = FindN;

		}
		else
		{
		    state = (thetoken == T_Return) ? Wait : Error;
		    CmdError();
		}
		break;

	    case FindF:
		if (tokenstr[0])
		{
		    last_state = state;
		    state = FindW;
		    prefix = Strip(tokenstr);
		    FindWord(tokenstr, prefix);
		}
		else
		{
		    state = (thetoken == T_Return) ? Wait : Error;
		    CmdError();
		}
		break;

	    case FindW:
		switch (thetoken)
		{
		    case T_And:
		        last_state = state;
			state = Find;
			CombineResults(invert, intersect);
			SaveResults();
			invert = 0;
			intersect = 1;
			break;
		    case T_Or:
			last_state = state;
			state = Find;
			CombineResults(invert, intersect);
			SaveResults();
			invert = 0;
			intersect = 0;
			break;
		    case T_Semi:
			last_state = state;
			state = Wait;
			CombineResults(invert, intersect);
			invert = 0;
			intersect = 1;
			break;
		    case T_Return:
			last_state = state;
			state = Wait;
			CombineResults(invert, intersect);
			ReportResults();
			invert = 0;
			intersect = 1;
			break;
		    default:
			if (tokenstr[0])
			{
			    last_state = state;
			    state = FindW;
			    prefix = Strip(tokenstr);
			    FindWord(tokenstr, prefix);
			}
			else
			{
			    state = Error;
			    CmdError();
			}
			break;
		}
		break;

	    case Display:
		if ((thetoken == T_Semi) || (thetoken == T_Return))
		{
		    last_state = state;
		    state = Wait;
		    PrintResults(NULL);
		}
		else
		{
		    state = Error;
		    CmdError();
		}
		break;

	    case Save:
		if (tokenstr[0])
		{
		    last_state = state;
		    state = SaveF;
		    strcpy(savestr, tokenstr);
		}
		else if ((thetoken == T_Semi) || (thetoken == T_Return))
		{
		    state = Wait;
		    PrintResults(savestr);
		}
		else
		{
		    state = Error;
		    CmdError();
		}
		break;

	    case SaveF:
		if ((thetoken == T_Semi) || (thetoken == T_Return))
		{
		    last_state = state;
		    state = Wait;
		    PrintResults(savestr);
		}
		else
		{
		    state = Error;
		    CmdError();
		}
		break;

	    case Whatis:
		if (tokenstr[0])
		{
		    last_state = state;
		    state = WhatisA;
		    strcpy(savestr, tokenstr);
		}
		else
		{
		    state = (thetoken == T_Return) ? Wait : Error;
		    CmdError();
		}
		break;

	    case WhatisA:
		if ((thetoken == T_Semi) || (thetoken == T_Return))
		{
		    last_state = state;
		    state = Wait;
		    DisplayAbbrev(savestr);
		}
		else
		{
		    state = Error;
		    CmdError();
		}
		break;

	    case Help:
		if ((thetoken == T_Semi) || (thetoken == T_Return))
		{
		    state = Wait;
		    GiveHelp(last_state == Help);
		    last_state = Help;
		}
		break;

	    case Error:
		switch (thetoken)
		{
		    case T_Quit:
			return;
		    case T_Return:
			state = Wait;
			break;
		    default:
			break;
		}
		break;
	}				/* end switch(state) */
    }					/* end for(;;) */
}


/* ================================================================= *\
|  The main program
\* ================================================================= */
int main(int argc, char **argv)
{
    register char *path, *tmp;
    char gotit;
    struct stat bibstat, bixstat;
    char *p;

    (void)printf("biblook version %d.%d  file version %d\n",
	   (int)MAJOR_VERSION, (int)MINOR_VERSION, (int)FILE_VERSION);
    (void)printf("Type ? or h for help\n");

    if ((argc != 2) && (argc != 3))
    {
	(void)fprintf(stderr, "Usage: biblook bib [savefile]\n");
	exit(EXIT_FAILURE);
    }

    if (((p = strrchr(argv[1],'.')) != (char*)NULL) &&
	(strcmp(p, ".bib") == 0))
    {
	*p = '\0';			/* remove any .bib extension */
    }

    /* ---- Search BIBLOOKPATH or BIBINPUTS for the files ---- */

    path = (char *)getenv("BIBLOOKPATH");
    if (path == NULL)
	path = (char *)getenv("BIBINPUTS");
    if ((path == NULL) || (argv[1][0] == '/'))
    {
	(void)sprintf(bibfile, "%s.bib", argv[1]);
	(void)sprintf(bixfile, "%s.bix", argv[1]);
    }
    else
    {
	tmp = path;
	gotit = 0;

	while ((tmp != NULL) && !gotit)
	{
	    tmp = strchr(path, ':');
	    if (tmp != NULL)
		*tmp = 0;

	    if (strcmp(path, "."))
	    {
		(void)sprintf(bibfile, "%s/%s.bib", path, argv[1]);
		(void)sprintf(bixfile, "%s/%s.bix", path, argv[1]);
	    }
	    else
	    {
		(void)sprintf(bibfile, "%s.bib", argv[1]);
		(void)sprintf(bixfile, "%s.bix", argv[1]);
	    }

	    if (stat(bibfile, &bibstat) != 0)
	    {
		if (errno != ENOENT)
		    pdie("Can't open", bibfile);
	    }
	    else
		gotit = 1;

	    if (tmp != NULL)
		path = tmp + 1;
	}

	if (!gotit)
	{
	    (void)sprintf(bibfile, "%s.bib", argv[1]);
	    pdie("Can't find", bibfile);
	}
    }

    /* ---- Now that we've found the files, open them and do the job ---- */

    if (stat(bibfile, &bibstat) != 0)
	pdie("Can't open", bibfile);

    if (stat(bixfile, &bixstat) != 0)
	pdie("Can't open", bixfile);

    if (bibstat.st_mtime > bixstat.st_mtime)
	die(bixfile, "is out of date.\n\tPlease rerun bibindex.");

    bibfp = fopen(bibfile, "r");
    if (!bibfp)
	pdie("Can't read", bibfile);
#if MSDOS
    bixfp = fopen(bixfile, "rb");
#else
    bixfp = fopen(bixfile, "r");
#endif
    if (!bixfp)
	pdie("Can't read", bixfile);

    GetTables();
    InitSearch();

    if (argc == 3)
	Lookup(argv[2]);
    else
	Lookup("save.bib");

    FreeSearch();
    FreeTables();

    fclose(bibfp);
    fclose(bixfp);
    exit(EXIT_SUCCESS);
    return(0);
}