xref: /original-bsd/contrib/bib/src/listrefs.c (revision 93bede30)
1 #ifndef lint
2 static char sccsid[] = "@(#)listrefs.c	2.7	01/03/94";
3 #endif not lint
4 /*
5         Listrefs - list references for bib system
6 
7         Authored by: Tim Budd, University of Arizona, 1983.
8                 lookup routines written by gary levin 2/82
9 
10                 version 7/4/83
11 
12         Various modifications suggested by:
13                 David Cherveny - Duke University Medical Center
14                 Phil Garrison - UC Berkeley
15                 M. J. Hawley - Yale University
16 
17 
18 
19 
20                                                         */
21 # include <stdio.h>
22 # include <ctype.h>
23 # include "bib.h"
24 # include "streams.h"
25 # define MAXLIST 2000  /* maximum number of references that can be listed */
26 # define getch(c,fd) (c = getc(fd))
27 
28 FILE *tfd;
29 
30 #ifndef INCORE
31 FILE *rfd;                      /* reference file position */
32 char reffile[] = TMPREFFILE;    /* temporary file (see bib.h) */
33 #endif INCORE
34 struct refinfo refinfo[MAXLIST];      /* references temporary file, seek positions */
35 struct refinfo *refshash[HASHSIZE];
36 long int rend = 1;              /* last used position in reference file */
37 int numrefs = 0;               /* number of references */
38 extern int sort;                /* see if things are to be sorted */
39 extern char bibfname[];
40 extern int biblineno;
41 char *programName;
42 
43 #include <signal.h>
44 main(argc, argv)
45    int argc;
46    char **argv;
47 {  char defult[120];
48    int  i, rcomp();
49    void intr();
50 
51    InitDirectory(BMACLIB,N_BMACLIB);
52    InitDirectory(COMFILE,N_COMFILE);
53    InitDirectory(DEFSTYLE,N_DEFSTYLE);
54 
55    signal(SIGINT, intr);
56    programName = argv[0];
57    tfd = stdout;
58    strcpy(defult, BMACLIB);
59    strcat(defult,"/bib.list");
60 #ifndef INCORE
61    mktemp(reffile);
62    rfd = fopen(reffile,"w+");
63    if (rfd == NULL)
64       error("can't open temporary reference file");
65    putc('x', rfd);      /* put garbage in first position */
66 #endif not INCORE
67 
68    doargs(argc, argv, defult);
69 
70    if (sort)
71       qsort(refinfo, numrefs, sizeof(struct refinfo), rcomp);
72    makecites();
73    disambiguate();
74 
75    for (i = 0; i < numrefs; i++)
76       dumpref(i, stdout);
77 
78    cleanup(0);
79 }
80 void
81 intr()
82 {
83   cleanup(1);
84 }
85 cleanup(val)
86 {
87 #ifndef INCORE
88   fclose(rfd);
89   unlink(reffile);
90 #endif not INCORE
91   exit(val);
92 }
93 
94 /* rdtext - process a file */
95    rdtext(ifile)
96    FILE *ifile;
97 {  int c;
98    char *p, rec[REFSIZE];
99    int i;
100    int hash, lg;
101 
102    biblineno = 1;
103    for (;;) {
104       getch(c, ifile);
105       for (;;) {
106 	 /* skip leading newlines and comments */
107 	 if (c == '\n') getch(c, ifile);
108 	 else if (c == '#') while (getch(c, ifile) != '\n' && c != EOF) ;
109 	 else break;
110          biblineno++;
111 	 }
112       if (c == EOF)
113          return;
114 
115       p = rec;          /* read a reference */
116       for (;;) {
117          for (*p++ = c; getch(c, ifile) != '\n'; )
118             if (c == EOF)
119                error("ill formed reference file");
120             else
121                *p++ = c;
122 	 /* at end-of-line */
123 	 while (getch(c, ifile) == '#')
124 	    while (getch(c, ifile) != '\n' && c != EOF) ;
125          if (c == '\n' || c == EOF) { /* if empty or eof */
126             biblineno++;
127             *p++ = '\n';
128             break;
129             }
130          if (c == '.' || c == '%')
131             *p++ = '\n';
132          else
133             *p++ = ' ';
134          }
135 
136       *p = 0;
137       expand(rec);
138 
139       /* didn't match any existing reference, create new one */
140       if (numrefs >= MAXLIST)
141 	error("too many references, max of %d", MAXLIST);
142       hash = strhash(rec);
143       lg = strlen(rec) + 1;
144       refinfo[numrefs].ri_pos = rend;
145       refinfo[numrefs].ri_length = lg;
146       refinfo[numrefs].ri_hp = refshash[hash];
147       refinfo[numrefs].ri_n = numrefs;
148       refshash[hash] = &refinfo[numrefs];
149       wrref(&refinfo[numrefs], rec);
150       numrefs++;
151       }
152 }
153