summaryrefslogtreecommitdiff
path: root/biblio/bibtex/utils/bibindex/bibindex.c
blob: af7870896a3aaef78cde14277155f44a8803c6c3 (plain)
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
/* ================================================================= *\

   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 reach 75% 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
	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

   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.

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

#include "biblook.h"

static long line_number = 1L;		/* for debug messages */
static long initial_line_number = 1L;


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

/* ----------------------------------------------------------------- *\
|  void die(const char *msg1, const char *msg2) -- print an error message and die
\* ----------------------------------------------------------------- */
void die(const char *msg1, const char *msg2)
{
    fprintf(stderr,
    "\nError:\tin BibTeX entry starting at line %ld, \
error detected at line %ld:\n\t%s %s\n",
	    initial_line_number, line_number, msg1, msg2);
    exit(1);
}

/* ----------------------------------------------------------------- *\
|  char safegetc(FILE *fp, const char *where)
|
|  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 *where)
{
    char c;

    if (feof(fp))
	die("Unexpected end of file in", where);

    c = getc(fp);
    if (c == '\n')
	line_number++;
    return (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 = malloc(howmuch);
    if (tmp == NULL)
	die(msg1, msg2);

    return tmp;
}


/* ====================== HASH TABLE FUNCTIONS ===================== *\

   The hash tables start small and double whenever they reach 75%
   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	127	 	/* intentionally way too much */
				/* NB: limited by char type of numfields */

#define INIT_HASH_SIZE	256
#define HASH_CONST   	1482907		/* prime close to 2^{20.5} */

typedef struct		/* Hash table entry */
{
    Word    theword;	/* the hashed word */
    Index_t  number;	/* number of references in the list */
    Index_t  size;	/* real size of reference list */
    Index_t  *refs;	/* actual list of references */
} HashCell, *HashPtr;

typedef struct		/* Extensible hash table */
{
    Word    thefield;	/* the field type */
    Index_t  number;	/* number of words in the hash table */
    Index_t  size;	/* real size of the hash table */
    HashPtr words;	/* the actual hash table */
} ExHashTable;

static ExHashTable fieldtable[MAXFIELDS];	/* the field tables */
static char        numfields;			/* number of fields */


/* ----------------------------------------------------------------- *\
|  void InitTables(void)
|
|  Initialize the field tables
\* ----------------------------------------------------------------- */
void InitTables(VOID)
{
    register unsigned short i;

    numfields = 0;
    for (i=0; i<MAXFIELDS; i++)
    {
	fieldtable[i].thefield[0] = 0;
	fieldtable[i].number = 0;
	fieldtable[i].size = 0;
	fieldtable[i].words = NULL;
    }
}

/* ----------------------------------------------------------------- *\
|  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->thefield);
    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;
    }
}

/* ----------------------------------------------------------------- *\
|  void FreeTables(void)
|
|  Free the tables
\* ----------------------------------------------------------------- */
void FreeTables(VOID)
{
    register unsigned short i;
    Index_t j;

    for (i=0; i < (unsigned short)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);
	}
    }
}


/* ----------------------------------------------------------------- *\
|  ExHashTable *GetHashTable(char *field)
|
|  Get the hash table associated with the given field.  If the table
|  is unclaimed, claim it and initialize it.
\* ----------------------------------------------------------------- */
ExHashTable *GetHashTable(char *field)
{
    register unsigned long hash=0;	/* primary hash value	*/
    register unsigned long skip=1;	/* secondary hash value */
    register int i;

    for (i=0; field[i]; i++)
    {
	hash = (hash*HASH_CONST + field[i]) % MAXFIELDS;
	skip += 2*hash;
    }

    while (fieldtable[hash].thefield[0] &&	     /* cell not empty and  */
	   strcmp(fieldtable[hash].thefield, field)) /* not the right field */
    {
	hash = (hash+skip) % MAXFIELDS;
    }

    if (!fieldtable[hash].thefield[0])
    {
	strcpy(fieldtable[hash].thefield, field);
	InitOneField(fieldtable+hash);
	numfields++;
	if (numfields >= MAXFIELDS)	/* NB: NOT > because that produces */
					/* an infinite while() loop above on */
					/* next entry! */
	    die("too many field names",field);
    }

    return fieldtable+hash;
}

/* ----------------------------------------------------------------- *\
|  void InitBlackHole(char *field)
|
|  Intitialize a black hole for the given field.
\* ----------------------------------------------------------------- */
void InitBlackHole(char *field)
{
    ExHashTable *hole;

    hole = GetHashTable(field);
    free(hole->words);
    hole->words = NULL;
}

/* ----------------------------------------------------------------- *\
|  HashPtr GetHashCell(ExHashTable *table, char *word)
|
|  Get the hash table cell associated with the given word.  If the cell
|  is unclaimed, claim it, initialize it, and update the table's word
|  count.
\* ----------------------------------------------------------------- */
HashPtr GetHashCell(ExHashTable *htable, char *word)
{
    register HashPtr table, cell;
    register unsigned long hash=0;	/* primary hash value	*/
    register unsigned long skip=1;	/* secondary hash value */
    register int i;

    table = htable->words;

    for (i=0; word[i]; i++)
    {
	hash = (hash*HASH_CONST + word[i]) % htable->size;
	skip += 2*hash;
    }

    while (table[hash].theword[0] &&		/* cell not empty and */
	   strcmp(table[hash].theword, word))	/* not the right word */
    {
	hash = (hash+skip) % htable->size;
    }
    cell = table+hash;

    if (!cell->theword[0])
    {
	strcpy(cell->theword, word);
	cell->size = 8;
	cell->refs = (Index_t *) safemalloc(cell->size * sizeof(Index_t),
					     "Can't create entry list for",
					     word);
	htable->number++;
    }
    return cell;
}

/* ----------------------------------------------------------------- *\
|  void ExtendHashTable(ExHashTable *htable)
|
|  Double the size of the hash table and rehash everything.
\* ----------------------------------------------------------------- */
void ExtendHashTable(ExHashTable *htable)
{
    register HashPtr newcell;
    register HashPtr oldtable;
    Index_t i;
    Index_t oldsize;

    oldsize  = htable->size;
    oldtable = htable->words;

    htable->number = 0;
    htable->size *= 2;
    htable->words = (HashPtr) safemalloc(sizeof(HashCell)*htable->size,
					 "Can't extend hash table for",
					 htable->thefield);

    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;
    }

    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.
\* ----------------------------------------------------------------- */
void InsertEntry(ExHashTable *htable, char *word, Index_t entry)
{
    register HashPtr cell;
    Index_t *newlist;

    if (htable->words == NULL)		/* Is it a black hole? */
	return;

    if ((Index_t)(htable->number * 4) > (Index_t)(htable->size * 3))
	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;
	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;
    if (cell->number <= 0)
	die("hash type overflow", htable->thefield);
}


/* ================================================================= */

/* ----------------------------------------------------------------- *\
|  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 = strlen(word);
    fwrite((void *) &length, sizeof(char), 1, ofp);
    fwrite((void *) word, sizeof(char), length, ofp);
}

/* ----------------------------------------------------------------- *\
|  void OutputTables(FILE *ofp)
|
|  Compress and output the tables, with lots of user feedback.
|  KLUDGE -- Passing strcmp to qsort assumes intimate knowledge of
|  the HashCell and ExhashTable structs.
\* ----------------------------------------------------------------- */
void OutputTables(FILE *ofp)
{
    register HashPtr words;
    register int i, k, count;
    Index_t m, n;

    printf("Writing index tables...");
    fflush(stdout);

    numfields = 0;		/* recount, ignoring black holes */
    for (i=0; i<MAXFIELDS; i++)
    {
	if (fieldtable[i].words)
	{
	    if (i > numfields)
	    {
		fieldtable[numfields] = fieldtable[i]; /* copy i-th table */
	        fieldtable[i].number = 0; /* then clear i-th table */
	        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);

    fwrite((void *) &numfields, sizeof(char), 1, ofp);
    for (i=0; i<numfields; i++)
	WriteWord(ofp, fieldtable[i].thefield);

    printf("[%d fields]\n", numfields);

    for (k=0; k<numfields; k++)
    {
	printf("%2d: %s...", k+1, fieldtable[k].thefield);
	fflush(stdout);

	words = fieldtable[k].words;

	for (m=0, n=0; m<fieldtable[k].size; m++)
	{
	    if (words[m].theword[0])
	    {
		if (m > n)
		{
		    words[n] = words[m]; /* copy m-th table to n-th */
		    words[m].number = 0; /* then clear m-th table */
		    words[m].size = 0;	/* to avoid duplicate free() later */
		    words[m].refs = (Index_t*)0;
		}
		n++;
	    }
	}
	qsort(words, (size_t)fieldtable[k].number, sizeof(HashCell),
	      (int (*)(const void*,const void*))strcmp);

	fwrite((void *) &(fieldtable[k].number), sizeof(Index_t), 1, ofp);

	count = 0;
	for (m=0; m<fieldtable[k].number; m++)
	{
	    WriteWord(ofp, words[m].theword);
	    fwrite((void *) &(words[m].number), sizeof(Index_t), 1, ofp);
	    fwrite((void *) words[m].refs, sizeof(Index_t),
		   words[m].number, ofp);
	    count += words[m].number;
	}

	printf("[%d words, %d references]\n", fieldtable[k].number, count);
    }
}


/* ============================= 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 at the first comma.  Entries containing no
|  commas, such as @string, are skipped.  The entry begins after the
|  most recent blank line, the end of the last entry, or the beginning
|  of the file.  Returns "-1" if there is no next entry.
\* ----------------------------------------------------------------- */
unsigned long FindNextEntry(FILE *ifp)
{
    char ch;
    char blank = 0;	  	/* 1 if current line is blank so far */
    char braces = 0;
    char quotes = 0;
    unsigned long offset;

    offset = ftell(ifp);
    ch = getc(ifp);
    if (ch == '\n')
	line_number++;

    while (1)
    {
	if (feof(ifp))
	    return (unsigned long) -1;

	if (ch == '@')			/* got a potential entry */
	{
	    initial_line_number = line_number; /* record start of entry */
	    blank = 0;
	    while ((ch != '{') && (ch != '('))
		ch = safegetc(ifp, "FindNextEntry 1");	/* EOF is an */
							/* error here */
	    quotes = 0;
	    braces = 0;
	    ch = safegetc(ifp, "FindNextEntry 2");

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

		ch = safegetc(ifp, "FindNextEntry 3");
	    }
	    if (ch == ',')
	    {
		ungetc(ch, ifp);
		return offset;
	    }
	    else
		offset = ftell(ifp);
	}
	else if (ch == '\n')
	{
	    if (blank)
		offset = ftell(ifp);
	    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}ss" 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".
|	4. TeX commands in math expressions are considered normal
|	   text.  Thus, "$O(n\log^2 n)$" is returned as "onlog2n"
|	   instead of "onn".  This occasionally gives unexpected or
|	   unreadable results.  For example, "$\bigcup_1^n[a_i,b_i]$"
|	   is returned as "bigcup1naibi".
|	5. Apostrophes do not delimit words.  Thus, "didn't" is
|	   returned as "didnt", and "{\'O}'D{\'u}nlaing" is returned
|	   as "odunlaing".
|
|  Returns 1 if a word was actually found, 0 otherwise.
|
|  The file pointer is left at the character immediately following the
|  word.  The input is assumed to be syntactically correct: unbalanced
|  braces, math delimiters, and quotation marks will cause errors.
\* ----------------------------------------------------------------- */
int GetNextWord(FILE *ifp, char *word)
{
    char braces = 0;		/* levels of indented braces */
    char math = 0;		/* 1 if in math expression */
    char inword = 0;		/* 1 if word has started */
    char incmd = 0;		/* 1 if reading TeX command */
    char done = 0;		/* 1 if word is complete */
    char ch = ' ';
    int nchars = 0;		/* how many characters stored in word */

    while (!done)
    {
	ch = safegetc(ifp, "GetNextWord 1");

	if (isalpha(ch))		/* letters */
	{
	    if (!incmd)			/* may be part of TeX command */
	    {
		inword = 1;
		if (++nchars <= MAXWORD) /* ignore overflow in word buffer */
		    *word++ = tolower(ch);
	    }
	}
	else if (isdigit(ch))
	{
	    incmd = 0;
	    inword = 1;
	    if (++nchars <= MAXWORD) /* ignore overflow in word buffer */
	        *word++ = ch;
	}
	else if (ch == '\'')		/* ignore apostrophes entirely */
	    incmd = 0;
	else if (ch == '\\') 		/* beginning of TeX command */
	{
	    if (!math)			/* if math, pretend it's text */
	    {
		ch = safegetc(ifp, "GetNextWord 2");
		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 == '$')		/* math */
	{
	    incmd = 0;
	    math = !math;
	    if (math)		/* treat $s as braces */
		braces++;
	    else
		braces--;
	}
	else if (incmd)			/* other characters */
	    incmd = 0;
	else if (inword && !braces)
	    done = 1;
    }

    *word = 0;
    return inword;
}

const char *badwords[] = BADWORDS;

/* ----------------------------------------------------------------- *\
|  void MungeField(FILE *ifp, ExHashTable *htable, Index_t entry)
|
|  Munge the current field.  Dump all words into the given hash table,
|  with the given entry number.  On exit, the file pointer is on the
|  comma or closing character for the entry.
\* ----------------------------------------------------------------- */
void MungeField(FILE *ifp, ExHashTable *htable, Index_t entry)
{
    int i;
    char ch, bad;
    char nextword[MAXWORD+1];

    ch = safegetc(ifp, "MungeField");
    while (isspace(ch))
	ch = safegetc(ifp, "MungeField");

    if (ch != '=')
	die("= expected in bibliography field", htable->thefield);

    ch = safegetc(ifp, "MungeField");
    while (isspace(ch))
	ch = safegetc(ifp, "MungeField");

    if (ch != '{' && ch != '"') 	/* handle abbreviations */
    {
	for (i=0; !strchr(NONABBREV, ch); i++)
	{
	    if (i >= MAXWORD)
		die("word buffer overflow", htable->thefield);
	    nextword[i] = tolower(ch);
	    ch = safegetc(ifp, "MungeField");
	}
	nextword[i] = 0;
	nextword[MAXWORD] = 0;
	InsertEntry(htable, nextword, entry);
    }
    else
    {
	while (GetNextWord(ifp, nextword))
	{
	    if (nextword[1] == 0)	/* ignore single letters */
		continue;
	    for (bad=0, i=0; !bad && badwords[i]; i++)
		if (!strcmp(nextword, badwords[i]))
		    bad = 1;
	    if (!bad)
	    {
		nextword[MAXWORD] = 0;		/* truncate to fit */
		InsertEntry(htable, nextword, entry);
	    }
	}
	ch = safegetc(ifp, "MungeField");	/* close quote/brace */
	ch = safegetc(ifp, "MungeField");	/* comma (maybe) */
    }

    while (isspace(ch))
	ch = safegetc(ifp, "MungeField");
    ungetc(ch, ifp);	  	       /* comma or close paren/brace */
}

/* ----------------------------------------------------------------- *\
|  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>
|
\* ----------------------------------------------------------------- */

int iskeychar(char c,int first_char)
{
    if (first_char)
        return (isalpha(c) ? 1 : 0);
    else if (isalpha(c) || isdigit(c))
	return (1);
    else if (strchr("!$&+-./:;<>?@[\\]^_`|~\177",(int)c) != (char*)NULL)
	return (1);
    else
	return (0);
}

/* ----------------------------------------------------------------- *\
|  void MungeEntry(FILE *ifp, Index_t entry)
|
|  Wander through the entry, extracting words and depositing them in
|  the appropriate hash tables.
\* ----------------------------------------------------------------- */
void MungeEntry(FILE *ifp, Index_t entry)
{
    register char ch;
    Word thefield;
    short i;

    ch = safegetc(ifp, "MungeEntry");
    while (ch == ',')
    {
	ch = safegetc(ifp, "MungeEntry");
	while (isspace(ch))
	    ch = safegetc(ifp, "MungeEntry");

	if ((ch == '}') || (ch == ')'))	/* allow trailing comma after */
	    return;			/* last key = "value" entry */

	if (!isalpha(ch))
	{
	    char msg[2];
	    msg[0] = ch;
	    msg[1] = 0;
	    die("Nonalpha character in field descriptor:", msg);
	}

	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, "MungeEntry");
	}
	ungetc(ch, ifp);		/* put back lookahead char */
	thefield[i] = 0;

	MungeField(ifp, GetHashTable(thefield), entry);

	ch = safegetc(ifp, "MungeEntry");
    }
}


/* ============================= OUTPUT ============================ */

/* ----------------------------------------------------------------- *\
|  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;
    long *offsets;
    long *oldoff;
    Index_t offsize;
    time_t now = time(0);

    printf("Indexing %s.bib...", filename);
    fflush(stdout);

    offsize = 128;		/* MINIMUM OFFSET LIST SIZE */
    offsets = (long *) malloc(offsize * sizeof(long));

    while (!feof(ifp))
    {
	curoffset = FindNextEntry(ifp);
	if (curoffset == (unsigned long) -1)
	    break;

	offsets[count] = curoffset;
	MungeEntry(ifp, count++);

	if (count == offsize)		/* expand full offset array */
	{
	    oldoff = offsets;
	    offsize *= 2;
	    offsets = (long *) malloc(offsize * sizeof(long));
	    bcopy(oldoff, offsets, count * sizeof(long));
	    free(oldoff);
	}

	if (!(count % 500))		/* feedback */
	{
	    printf("%d...", count);
	    fflush(stdout);
	}
    }

    printf("done.\n");

    fprintf(ofp, "bibindex %d %d %d %s", FILE_VERSION, MAJOR_VERSION,
	    MINOR_VERSION, ctime(&now));

    printf("Writing file offsets...");
    fflush(stdout);
    fwrite((void *) &count, sizeof(Index_t), 1, ofp);
    fwrite((void *) offsets, sizeof(long), count, ofp);
    free(offsets);
    printf("[%d entries]\n", count);

    OutputTables(ofp);

    printf("All done!\n");
}

/* ----------------------------------------------------------------- *\
|  The main program
\* ----------------------------------------------------------------- */

int main(int argc, char **argv)
{
    FILE *ifp;
    FILE *ofp;
    char infile[256];
    char outfile[256];
    char *p;
    int i;

#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 */

    sprintf(infile, "%s.bib", argv[1]);
    sprintf(outfile, "%s.bix", argv[1]);

    ifp = fopen(infile, "r");
    if (!ifp)
	die("Can't read", infile);

    ofp = fopen(outfile, "w");
    if (!ofp)
	die("Can't write", outfile);

    InitTables();

    if ((argc > 2) && (!strcmp(argv[2], "-i")))
	for (i=3; i<argc; i++)
	    InitBlackHole(argv[i]);

    IndexBibFile(ifp, ofp, argv[1]);

    FreeTables();
    fclose(ifp);
    fclose(ofp);

    exit(0);		/* Argh! */
    return (0);		/* keep compilers happy */
}