xref: /original-bsd/old/refer/lookbib/lookbib.c (revision 93ab02a6)
1 /*-
2  * %sccs.include.proprietary.c%
3  */
4 
5 #ifndef lint
6 static char sccsid[] = "@(#)lookbib.c	4.7 (Berkeley) 04/18/91";
7 #endif /* not lint */
8 
9 #include <stdio.h>
10 #include <ctype.h>
11 #include "pathnames.h"
12 
13 main(argc, argv)	/* look in biblio for record matching keywords */
14 int argc;
15 char **argv;
16 {
17 	FILE *fp, *hfp, *fopen(), *popen();
18 	char s[BUFSIZ], hunt[64];
19 	int instructions = 1;
20 
21 	if (strcmp(argv[1],"-n") == 0)
22 	{
23 		argv++;
24 		argc--;
25 		instructions = 0;
26 	}
27 	if (argc == 1 || argc > 2)
28 	{
29 		fputs("Usage:  lookbib database\n",
30 			stderr);
31 		fputs("\tfinds citations specified on standard input\n",
32 			stderr);
33 		exit(1);
34 	}
35 	if (!isatty(fileno(stdin)))
36 		fp = stdin;
37 	else if ((fp = fopen(_PATH_TTY, "r")) == NULL)
38 	{
39 		perror(_PATH_TTY);
40 		exit(1);
41 	}
42 	(void)sprintf(s, "%s.ia", argv[1]);
43 	if (access(s, 0) == -1) {
44 		(void)sprintf (s, "%s", argv[1]);
45 		if (access(s, 0) == -1) {
46 			perror(s);
47 			fprintf(stderr, "\tNeither index file %s.ia ", s);
48 			fprintf(stderr, "nor reference file %s found\n", s);
49 			exit(1);
50 		}
51 	}
52 	(void)sprintf(hunt, "%s %s", _PATH_HUNT, argv[1]);
53 
54 	if (instructions && isatty(fileno(fp)))
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 		{
69 			perror("lookbib: hunt");
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 
82 map_lower(s)		/* map string s to lower case */
83 char *s;
84 {
85 	for ( ; *s; ++s)
86 		if (isupper(*s))
87 			*s = tolower(*s);
88 }
89 
90 instruct()
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 }
101