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