summaryrefslogtreecommitdiff
path: root/usr/src/cmd/refer/lookbib.c
blob: d66184e08eb8551a26b84a772b5e403e062271e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * Copyright (c) 1980 Regents of the University of California.
 * All rights reserved. The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <ctype.h>
#include <locale.h>

static void instruct(void);
static void map_lower(char *);

/* look in biblio for record matching keywords */
int
main(int argc, char **argv)
{
	FILE *hfp, *fopen(), *popen();
	char s[BUFSIZ], hunt[64];

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	if (argc == 1 || argc > 2) {
		fputs(gettext("Usage:  lookbib database\n\
\tfinds citations specified on standard input\n"), stderr);
		exit(1);
	}
	sprintf(s, "%s.ia", argv[1]);
	if (access(s, 0) == -1) {
		sprintf(s, "%s", argv[1]);
		if (access(s, 0) == -1) {
			perror(s);
			fprintf(stderr, gettext("\tNeither index file %s.ia \
nor reference file %s found\n"), s, s);
			exit(1);
		}
	}
	sprintf(hunt, "/usr/lib/refer/hunt %s", argv[1]);
	if (isatty(fileno(stdin))) {
		fprintf(stderr, gettext("Instructions? "));
		fgets(s, BUFSIZ, stdin);
		if (*s == 'y')
			instruct();
	}
again:
	fprintf(stderr, "> ");
	if (fgets(s, BUFSIZ, stdin)) {
		if (*s == '\n')
			goto again;
		if (strlen(s) <= 3)
			goto again;
		if ((hfp = popen(hunt, "w")) == NULL) {
			perror(gettext("lookbib: /usr/lib/refer/hunt"));
			exit(1);
		}
		map_lower(s);
		fputs(s, hfp);
		pclose(hfp);
		goto again;
	}
	fprintf(stderr, gettext("EOT\n"));
	return (0);
}

static void
map_lower(char *s)		/* map string s to lower case */
{
	for (; *s; ++s)
		if (isupper(*s))
			*s = tolower(*s);
}

static void
instruct(void)
{
	fputs(gettext(
"\nType keywords (such as author and date) after the > prompt.\n\
References with those keywords are printed if they exist;\n\
\tif nothing matches you are given another prompt.\n\
To quit lookbib, press CTRL-d after the > prompt.\n\n"), stderr);

}