148298Sbostic /*-
248298Sbostic * %sccs.include.proprietary.c%
348298Sbostic */
448298Sbostic
512260Stut #ifndef lint
6*53815Sbostic static char sccsid[] = "@(#)lookbib.c 4.8 (Berkeley) 06/02/92";
748298Sbostic #endif /* not lint */
812260Stut
912260Stut #include <stdio.h>
1012260Stut #include <ctype.h>
1137895Sbostic #include "pathnames.h"
1212260Stut
main(argc,argv)1312260Stut main(argc, argv) /* look in biblio for record matching keywords */
1412260Stut int argc;
1512260Stut char **argv;
1612260Stut {
1712260Stut FILE *fp, *hfp, *fopen(), *popen();
1832537Sbostic char s[BUFSIZ], hunt[64];
1916047Sralph int instructions = 1;
2012260Stut
21*53815Sbostic if (argc > 1 && strcmp(argv[1],"-n") == 0)
2216047Sralph {
2316047Sralph argv++;
2416047Sralph argc--;
2516047Sralph instructions = 0;
2616047Sralph }
2712260Stut if (argc == 1 || argc > 2)
2812260Stut {
2912260Stut fputs("Usage: lookbib database\n",
3012260Stut stderr);
3112260Stut fputs("\tfinds citations specified on standard input\n",
3212260Stut stderr);
3312260Stut exit(1);
3412260Stut }
3512260Stut if (!isatty(fileno(stdin)))
3612260Stut fp = stdin;
3737895Sbostic else if ((fp = fopen(_PATH_TTY, "r")) == NULL)
3812260Stut {
3937895Sbostic perror(_PATH_TTY);
4012260Stut exit(1);
4112260Stut }
4232537Sbostic (void)sprintf(s, "%s.ia", argv[1]);
4312260Stut if (access(s, 0) == -1) {
4432537Sbostic (void)sprintf (s, "%s", argv[1]);
4513240Sgarrison if (access(s, 0) == -1) {
4613240Sgarrison perror(s);
4713433Stut fprintf(stderr, "\tNeither index file %s.ia ", s);
4813433Stut fprintf(stderr, "nor reference file %s found\n", s);
4913240Sgarrison exit(1);
5013240Sgarrison }
5112260Stut }
5237895Sbostic (void)sprintf(hunt, "%s %s", _PATH_HUNT, argv[1]);
5312260Stut
5416047Sralph if (instructions && isatty(fileno(fp)))
5512260Stut {
5612260Stut fprintf(stderr, "Instructions? ");
5712260Stut fgets(s, BUFSIZ, fp);
5812260Stut if (*s == 'y')
5912260Stut instruct();
6012260Stut }
6112260Stut again:
6212260Stut fprintf(stderr, "> ");
6312260Stut if (fgets(s, BUFSIZ, fp))
6412260Stut {
6512260Stut if (*s == '\n')
6612260Stut goto again;
6712260Stut if ((hfp = popen(hunt, "w")) == NULL)
6812260Stut {
6937895Sbostic perror("lookbib: hunt");
7012260Stut exit(1);
7112260Stut }
7212260Stut map_lower(s);
7312260Stut fputs(s, hfp);
7412260Stut pclose(hfp);
7512260Stut goto again;
7612260Stut }
7712260Stut fclose(fp);
7812260Stut fprintf(stderr, "EOT\n");
7912260Stut exit(0);
8012260Stut }
8112260Stut
map_lower(s)8212260Stut map_lower(s) /* map string s to lower case */
8312260Stut char *s;
8412260Stut {
8512260Stut for ( ; *s; ++s)
8612260Stut if (isupper(*s))
8712260Stut *s = tolower(*s);
8812260Stut }
8912260Stut
instruct()9012260Stut instruct()
9112260Stut {
9212260Stut fputs("\nType keywords (such as author and date) after the > prompt.\n",
9312260Stut stderr);
9412260Stut fputs("References with those keywords are printed if they exist;\n",
9512260Stut stderr);
9612260Stut fputs("\tif nothing matches you are given another prompt.\n",
9712260Stut stderr);
9812260Stut fputs("To quit lookbib, press CTRL-d after the > prompt.\n\n",
9912260Stut stderr);
10012260Stut }
101