xref: /original-bsd/contrib/bib/src/listrefs.c (revision c180b88f)
1 #ifndef lint
2 static char sccsid[] = "@(#)listrefs.c	2.6	05/27/93";
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(), intr();
49 
50    InitDirectory(BMACLIB,N_BMACLIB);
51    InitDirectory(COMFILE,N_COMFILE);
52    InitDirectory(DEFSTYLE,N_DEFSTYLE);
53 
54    signal(SIGINT, intr);
55    programName = argv[0];
56    tfd = stdout;
57    strcpy(defult, BMACLIB);
58    strcat(defult,"/bib.list");
59 #ifndef INCORE
60    mktemp(reffile);
61    rfd = fopen(reffile,"w+");
62    if (rfd == NULL)
63       error("can't open temporary reference file");
64    putc('x', rfd);      /* put garbage in first position */
65 #endif not INCORE
66 
67    doargs(argc, argv, defult);
68 
69    if (sort)
70       qsort(refinfo, numrefs, sizeof(struct refinfo), rcomp);
71    makecites();
72    disambiguate();
73 
74    for (i = 0; i < numrefs; i++)
75       dumpref(i, stdout);
76 
77    cleanup(0);
78 }
79 intr()
80 {
81   cleanup(1);
82 }
83 cleanup(val)
84 {
85 #ifndef INCORE
86   fclose(rfd);
87   unlink(reffile);
88 #endif not INCORE
89   exit(val);
90 }
91 
92 /* rdtext - process a file */
93    rdtext(ifile)
94    FILE *ifile;
95 {  int c;
96    char *p, rec[REFSIZE];
97    int i;
98    int hash, lg;
99 
100    biblineno = 1;
101    for (;;) {
102       getch(c, ifile);
103       for (;;) {
104 	 /* skip leading newlines and comments */
105 	 if (c == '\n') getch(c, ifile);
106 	 else if (c == '#') while (getch(c, ifile) != '\n' && c != EOF) ;
107 	 else break;
108          biblineno++;
109 	 }
110       if (c == EOF)
111          return;
112 
113       p = rec;          /* read a reference */
114       for (;;) {
115          for (*p++ = c; getch(c, ifile) != '\n'; )
116             if (c == EOF)
117                error("ill formed reference file");
118             else
119                *p++ = c;
120 	 /* at end-of-line */
121 	 while (getch(c, ifile) == '#')
122 	    while (getch(c, ifile) != '\n' && c != EOF) ;
123          if (c == '\n' || c == EOF) { /* if empty or eof */
124             biblineno++;
125             *p++ = '\n';
126             break;
127             }
128          if (c == '.' || c == '%')
129             *p++ = '\n';
130          else
131             *p++ = ' ';
132          }
133 
134       *p = 0;
135       expand(rec);
136 
137       /* didn't match any existing reference, create new one */
138       if (numrefs >= MAXLIST)
139 	error("too many references, max of %d", MAXLIST);
140       hash = strhash(rec);
141       lg = strlen(rec) + 1;
142       refinfo[numrefs].ri_pos = rend;
143       refinfo[numrefs].ri_length = lg;
144       refinfo[numrefs].ri_hp = refshash[hash];
145       refinfo[numrefs].ri_n = numrefs;
146       refshash[hash] = &refinfo[numrefs];
147       wrref(&refinfo[numrefs], rec);
148       numrefs++;
149       }
150 }
151