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