xref: /csrg-svn/contrib/bib/src/listrefs.c (revision 65357)
113111Srrh #ifndef lint
2*65357Sbostic static char sccsid[] = "@(#)listrefs.c	2.7	01/03/94";
313111Srrh #endif not lint
415062Sgarrison /*
515062Sgarrison         Listrefs - list references for bib system
613111Srrh 
715062Sgarrison         Authored by: Tim Budd, University of Arizona, 1983.
815062Sgarrison                 lookup routines written by gary levin 2/82
915062Sgarrison 
1015062Sgarrison                 version 7/4/83
1115062Sgarrison 
1215062Sgarrison         Various modifications suggested by:
1315062Sgarrison                 David Cherveny - Duke University Medical Center
1415062Sgarrison                 Phil Garrison - UC Berkeley
1515062Sgarrison                 M. J. Hawley - Yale University
1615062Sgarrison 
1715062Sgarrison 
1815062Sgarrison 
1915062Sgarrison 
2012912Sgarrison                                                         */
2112912Sgarrison # include <stdio.h>
2212912Sgarrison # include <ctype.h>
2312912Sgarrison # include "bib.h"
2412912Sgarrison # include "streams.h"
2515062Sgarrison # define MAXLIST 2000  /* maximum number of references that can be listed */
2615062Sgarrison # define getch(c,fd) (c = getc(fd))
2712912Sgarrison 
2812912Sgarrison FILE *tfd;
2912912Sgarrison 
3015905Srrh #ifndef INCORE
3115062Sgarrison FILE *rfd;                      /* reference file position */
3215062Sgarrison char reffile[] = TMPREFFILE;    /* temporary file (see bib.h) */
3315905Srrh #endif INCORE
3415905Srrh struct refinfo refinfo[MAXLIST];      /* references temporary file, seek positions */
3515905Srrh struct refinfo *refshash[HASHSIZE];
3615062Sgarrison long int rend = 1;              /* last used position in reference file */
3716939Srrh int numrefs = 0;               /* number of references */
3815062Sgarrison extern int sort;                /* see if things are to be sorted */
3915062Sgarrison extern char bibfname[];
4015062Sgarrison extern int biblineno;
4160507Sbostic char *programName;
4215062Sgarrison 
4315905Srrh #include <signal.h>
main(argc,argv)4412912Sgarrison main(argc, argv)
4512912Sgarrison    int argc;
4612912Sgarrison    char **argv;
4715062Sgarrison {  char defult[120];
48*65357Sbostic    int  i, rcomp();
49*65357Sbostic    void intr();
5015062Sgarrison 
5160507Sbostic    InitDirectory(BMACLIB,N_BMACLIB);
5260507Sbostic    InitDirectory(COMFILE,N_COMFILE);
5360507Sbostic    InitDirectory(DEFSTYLE,N_DEFSTYLE);
5417249Srrh 
5515905Srrh    signal(SIGINT, intr);
5660507Sbostic    programName = argv[0];
5712912Sgarrison    tfd = stdout;
5815062Sgarrison    strcpy(defult, BMACLIB);
5915062Sgarrison    strcat(defult,"/bib.list");
6015905Srrh #ifndef INCORE
6115062Sgarrison    mktemp(reffile);
6215062Sgarrison    rfd = fopen(reffile,"w+");
6315062Sgarrison    if (rfd == NULL)
6415062Sgarrison       error("can't open temporary reference file");
6515062Sgarrison    putc('x', rfd);      /* put garbage in first position */
6615905Srrh #endif not INCORE
6715062Sgarrison 
6815062Sgarrison    doargs(argc, argv, defult);
6915062Sgarrison 
7015062Sgarrison    if (sort)
7115905Srrh       qsort(refinfo, numrefs, sizeof(struct refinfo), rcomp);
7215905Srrh    makecites();
7315062Sgarrison    disambiguate();
7415062Sgarrison 
7515905Srrh    for (i = 0; i < numrefs; i++)
7615062Sgarrison       dumpref(i, stdout);
7715062Sgarrison 
7815905Srrh    cleanup(0);
7912912Sgarrison }
80*65357Sbostic void
intr()8115905Srrh intr()
8215905Srrh {
8315905Srrh   cleanup(1);
8415905Srrh }
cleanup(val)8515905Srrh cleanup(val)
8615905Srrh {
8715905Srrh #ifndef INCORE
8815905Srrh   fclose(rfd);
8915905Srrh   unlink(reffile);
9015905Srrh #endif not INCORE
9115905Srrh   exit(val);
9215905Srrh }
9312912Sgarrison 
9412912Sgarrison /* rdtext - process a file */
rdtext(ifile)9512912Sgarrison    rdtext(ifile)
9612912Sgarrison    FILE *ifile;
9760507Sbostic {  int c;
9860507Sbostic    char *p, rec[REFSIZE];
9915062Sgarrison    int i;
10015905Srrh    int hash, lg;
10112912Sgarrison 
10215062Sgarrison    biblineno = 1;
10312912Sgarrison    for (;;) {
10460507Sbostic       getch(c, ifile);
10560507Sbostic       for (;;) {
10660507Sbostic 	 /* skip leading newlines and comments */
10760507Sbostic 	 if (c == '\n') getch(c, ifile);
10860507Sbostic 	 else if (c == '#') while (getch(c, ifile) != '\n' && c != EOF) ;
10960507Sbostic 	 else break;
11060507Sbostic          biblineno++;
11160507Sbostic 	 }
11215062Sgarrison       if (c == EOF)
11315062Sgarrison          return;
11412912Sgarrison 
11515062Sgarrison       p = rec;          /* read a reference */
11615062Sgarrison       for (;;) {
11715062Sgarrison          for (*p++ = c; getch(c, ifile) != '\n'; )
11815062Sgarrison             if (c == EOF)
11915062Sgarrison                error("ill formed reference file");
12015062Sgarrison             else
12115062Sgarrison                *p++ = c;
12260507Sbostic 	 /* at end-of-line */
12360507Sbostic 	 while (getch(c, ifile) == '#')
12460507Sbostic 	    while (getch(c, ifile) != '\n' && c != EOF) ;
12560507Sbostic          if (c == '\n' || c == EOF) { /* if empty or eof */
12615062Sgarrison             biblineno++;
12715062Sgarrison             *p++ = '\n';
12815062Sgarrison             break;
12912912Sgarrison             }
13015062Sgarrison          if (c == '.' || c == '%')
13115062Sgarrison             *p++ = '\n';
13215062Sgarrison          else
13315062Sgarrison             *p++ = ' ';
13415062Sgarrison          }
13512912Sgarrison 
13612912Sgarrison       *p = 0;
13715062Sgarrison       expand(rec);
13812912Sgarrison 
13915905Srrh       /* didn't match any existing reference, create new one */
14015905Srrh       if (numrefs >= MAXLIST)
14115905Srrh 	error("too many references, max of %d", MAXLIST);
14215905Srrh       hash = strhash(rec);
14315905Srrh       lg = strlen(rec) + 1;
14415905Srrh       refinfo[numrefs].ri_pos = rend;
14515905Srrh       refinfo[numrefs].ri_length = lg;
14615905Srrh       refinfo[numrefs].ri_hp = refshash[hash];
14715905Srrh       refinfo[numrefs].ri_n = numrefs;
14815905Srrh       refshash[hash] = &refinfo[numrefs];
14915905Srrh       wrref(&refinfo[numrefs], rec);
15015905Srrh       numrefs++;
15112912Sgarrison       }
15212912Sgarrison }
153