new copyright; att/bsd/shared
[unix-history] / usr / src / old / refer / lookbib / lookbib.c
CommitLineData
a860ef9e
KB
1/*-
2 * %sccs.include.proprietary.c%
3 */
4
37132bf3 5#ifndef lint
a860ef9e
KB
6static char sccsid[] = "@(#)lookbib.c 4.7 (Berkeley) %G%";
7#endif /* not lint */
37132bf3
BT
8
9#include <stdio.h>
10#include <ctype.h>
ade4c407 11#include "pathnames.h"
37132bf3
BT
12
13main(argc, argv) /* look in biblio for record matching keywords */
14int argc;
15char **argv;
16{
17 FILE *fp, *hfp, *fopen(), *popen();
aa81268e 18 char s[BUFSIZ], hunt[64];
b12cd3d6 19 int instructions = 1;
37132bf3 20
b12cd3d6
RC
21 if (strcmp(argv[1],"-n") == 0)
22 {
23 argv++;
24 argc--;
25 instructions = 0;
26 }
37132bf3
BT
27 if (argc == 1 || argc > 2)
28 {
29 fputs("Usage: lookbib database\n",
30 stderr);
37132bf3
BT
31 fputs("\tfinds citations specified on standard input\n",
32 stderr);
33 exit(1);
34 }
35 if (!isatty(fileno(stdin)))
36 fp = stdin;
ade4c407 37 else if ((fp = fopen(_PATH_TTY, "r")) == NULL)
37132bf3 38 {
ade4c407 39 perror(_PATH_TTY);
37132bf3
BT
40 exit(1);
41 }
aa81268e 42 (void)sprintf(s, "%s.ia", argv[1]);
37132bf3 43 if (access(s, 0) == -1) {
aa81268e 44 (void)sprintf (s, "%s", argv[1]);
dcbf8695
GL
45 if (access(s, 0) == -1) {
46 perror(s);
c27492e3
BT
47 fprintf(stderr, "\tNeither index file %s.ia ", s);
48 fprintf(stderr, "nor reference file %s found\n", s);
dcbf8695
GL
49 exit(1);
50 }
37132bf3 51 }
ade4c407 52 (void)sprintf(hunt, "%s %s", _PATH_HUNT, argv[1]);
37132bf3 53
b12cd3d6 54 if (instructions && isatty(fileno(fp)))
37132bf3
BT
55 {
56 fprintf(stderr, "Instructions? ");
57 fgets(s, BUFSIZ, fp);
58 if (*s == 'y')
59 instruct();
60 }
61 again:
62 fprintf(stderr, "> ");
63 if (fgets(s, BUFSIZ, fp))
64 {
65 if (*s == '\n')
66 goto again;
67 if ((hfp = popen(hunt, "w")) == NULL)
68 {
ade4c407 69 perror("lookbib: hunt");
37132bf3
BT
70 exit(1);
71 }
72 map_lower(s);
73 fputs(s, hfp);
74 pclose(hfp);
75 goto again;
76 }
77 fclose(fp);
78 fprintf(stderr, "EOT\n");
79 exit(0);
80}
81
82map_lower(s) /* map string s to lower case */
83char *s;
84{
85 for ( ; *s; ++s)
86 if (isupper(*s))
87 *s = tolower(*s);
88}
89
90instruct()
91{
92 fputs("\nType keywords (such as author and date) after the > prompt.\n",
93 stderr);
94 fputs("References with those keywords are printed if they exist;\n",
95 stderr);
96 fputs("\tif nothing matches you are given another prompt.\n",
97 stderr);
98 fputs("To quit lookbib, press CTRL-d after the > prompt.\n\n",
99 stderr);
100}