xref: /csrg-svn/contrib/bib/src/listrefs.c (revision 60507)
113111Srrh #ifndef lint
2*60507Sbostic static char sccsid[] = "@(#)listrefs.c	2.6	05/27/93";
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;
41*60507Sbostic char *programName;
4215062Sgarrison 
4315905Srrh #include <signal.h>
4412912Sgarrison main(argc, argv)
4512912Sgarrison    int argc;
4612912Sgarrison    char **argv;
4715062Sgarrison {  char defult[120];
4815905Srrh    int  i, rcomp(), intr();
4915062Sgarrison 
50*60507Sbostic    InitDirectory(BMACLIB,N_BMACLIB);
51*60507Sbostic    InitDirectory(COMFILE,N_COMFILE);
52*60507Sbostic    InitDirectory(DEFSTYLE,N_DEFSTYLE);
5317249Srrh 
5415905Srrh    signal(SIGINT, intr);
55*60507Sbostic    programName = argv[0];
5612912Sgarrison    tfd = stdout;
5715062Sgarrison    strcpy(defult, BMACLIB);
5815062Sgarrison    strcat(defult,"/bib.list");
5915905Srrh #ifndef INCORE
6015062Sgarrison    mktemp(reffile);
6115062Sgarrison    rfd = fopen(reffile,"w+");
6215062Sgarrison    if (rfd == NULL)
6315062Sgarrison       error("can't open temporary reference file");
6415062Sgarrison    putc('x', rfd);      /* put garbage in first position */
6515905Srrh #endif not INCORE
6615062Sgarrison 
6715062Sgarrison    doargs(argc, argv, defult);
6815062Sgarrison 
6915062Sgarrison    if (sort)
7015905Srrh       qsort(refinfo, numrefs, sizeof(struct refinfo), rcomp);
7115905Srrh    makecites();
7215062Sgarrison    disambiguate();
7315062Sgarrison 
7415905Srrh    for (i = 0; i < numrefs; i++)
7515062Sgarrison       dumpref(i, stdout);
7615062Sgarrison 
7715905Srrh    cleanup(0);
7812912Sgarrison }
7915905Srrh intr()
8015905Srrh {
8115905Srrh   cleanup(1);
8215905Srrh }
8315905Srrh cleanup(val)
8415905Srrh {
8515905Srrh #ifndef INCORE
8615905Srrh   fclose(rfd);
8715905Srrh   unlink(reffile);
8815905Srrh #endif not INCORE
8915905Srrh   exit(val);
9015905Srrh }
9112912Sgarrison 
9212912Sgarrison /* rdtext - process a file */
9312912Sgarrison    rdtext(ifile)
9412912Sgarrison    FILE *ifile;
95*60507Sbostic {  int c;
96*60507Sbostic    char *p, rec[REFSIZE];
9715062Sgarrison    int i;
9815905Srrh    int hash, lg;
9912912Sgarrison 
10015062Sgarrison    biblineno = 1;
10112912Sgarrison    for (;;) {
102*60507Sbostic       getch(c, ifile);
103*60507Sbostic       for (;;) {
104*60507Sbostic 	 /* skip leading newlines and comments */
105*60507Sbostic 	 if (c == '\n') getch(c, ifile);
106*60507Sbostic 	 else if (c == '#') while (getch(c, ifile) != '\n' && c != EOF) ;
107*60507Sbostic 	 else break;
108*60507Sbostic          biblineno++;
109*60507Sbostic 	 }
11015062Sgarrison       if (c == EOF)
11115062Sgarrison          return;
11212912Sgarrison 
11315062Sgarrison       p = rec;          /* read a reference */
11415062Sgarrison       for (;;) {
11515062Sgarrison          for (*p++ = c; getch(c, ifile) != '\n'; )
11615062Sgarrison             if (c == EOF)
11715062Sgarrison                error("ill formed reference file");
11815062Sgarrison             else
11915062Sgarrison                *p++ = c;
120*60507Sbostic 	 /* at end-of-line */
121*60507Sbostic 	 while (getch(c, ifile) == '#')
122*60507Sbostic 	    while (getch(c, ifile) != '\n' && c != EOF) ;
123*60507Sbostic          if (c == '\n' || c == EOF) { /* if empty or eof */
12415062Sgarrison             biblineno++;
12515062Sgarrison             *p++ = '\n';
12615062Sgarrison             break;
12712912Sgarrison             }
12815062Sgarrison          if (c == '.' || c == '%')
12915062Sgarrison             *p++ = '\n';
13015062Sgarrison          else
13115062Sgarrison             *p++ = ' ';
13215062Sgarrison          }
13312912Sgarrison 
13412912Sgarrison       *p = 0;
13515062Sgarrison       expand(rec);
13612912Sgarrison 
13715905Srrh       /* didn't match any existing reference, create new one */
13815905Srrh       if (numrefs >= MAXLIST)
13915905Srrh 	error("too many references, max of %d", MAXLIST);
14015905Srrh       hash = strhash(rec);
14115905Srrh       lg = strlen(rec) + 1;
14215905Srrh       refinfo[numrefs].ri_pos = rend;
14315905Srrh       refinfo[numrefs].ri_length = lg;
14415905Srrh       refinfo[numrefs].ri_hp = refshash[hash];
14515905Srrh       refinfo[numrefs].ri_n = numrefs;
14615905Srrh       refshash[hash] = &refinfo[numrefs];
14715905Srrh       wrref(&refinfo[numrefs], rec);
14815905Srrh       numrefs++;
14912912Sgarrison       }
15012912Sgarrison }
151