xref: /titanic_44/usr/src/cmd/refer/lookbib.c (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate #include <locale.h>
207c478bd9Sstevel@tonic-gate 
21*11a8fa6cSceastha static void instruct(void);
22*11a8fa6cSceastha static void map_lower(char *);
23*11a8fa6cSceastha 
24*11a8fa6cSceastha /* look in biblio for record matching keywords */
25*11a8fa6cSceastha int
main(int argc,char ** argv)26*11a8fa6cSceastha main(int argc, char **argv)
277c478bd9Sstevel@tonic-gate {
287c478bd9Sstevel@tonic-gate 	FILE *hfp, *fopen(), *popen();
297c478bd9Sstevel@tonic-gate 	char s[BUFSIZ], hunt[64];
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
347c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 	if (argc == 1 || argc > 2) {
397c478bd9Sstevel@tonic-gate 		fputs(gettext("Usage:  lookbib database\n\
407c478bd9Sstevel@tonic-gate \tfinds citations specified on standard input\n"), stderr);
417c478bd9Sstevel@tonic-gate 		exit(1);
427c478bd9Sstevel@tonic-gate 	}
437c478bd9Sstevel@tonic-gate 	sprintf(s, "%s.ia", argv[1]);
447c478bd9Sstevel@tonic-gate 	if (access(s, 0) == -1) {
457c478bd9Sstevel@tonic-gate 		sprintf(s, "%s", argv[1]);
467c478bd9Sstevel@tonic-gate 		if (access(s, 0) == -1) {
477c478bd9Sstevel@tonic-gate 			perror(s);
487c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("\tNeither index file %s.ia \
497c478bd9Sstevel@tonic-gate nor reference file %s found\n"), s, s);
507c478bd9Sstevel@tonic-gate 			exit(1);
517c478bd9Sstevel@tonic-gate 		}
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 	sprintf(hunt, "/usr/lib/refer/hunt %s", argv[1]);
547c478bd9Sstevel@tonic-gate 	if (isatty(fileno(stdin))) {
557c478bd9Sstevel@tonic-gate 		fprintf(stderr, gettext("Instructions? "));
567c478bd9Sstevel@tonic-gate 		fgets(s, BUFSIZ, stdin);
577c478bd9Sstevel@tonic-gate 		if (*s == 'y')
587c478bd9Sstevel@tonic-gate 			instruct();
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate again:
617c478bd9Sstevel@tonic-gate 	fprintf(stderr, "> ");
627c478bd9Sstevel@tonic-gate 	if (fgets(s, BUFSIZ, stdin)) {
637c478bd9Sstevel@tonic-gate 		if (*s == '\n')
647c478bd9Sstevel@tonic-gate 			goto again;
657c478bd9Sstevel@tonic-gate 		if (strlen(s) <= 3)
667c478bd9Sstevel@tonic-gate 			goto again;
677c478bd9Sstevel@tonic-gate 		if ((hfp = popen(hunt, "w")) == NULL) {
687c478bd9Sstevel@tonic-gate 			perror(gettext("lookbib: /usr/lib/refer/hunt"));
697c478bd9Sstevel@tonic-gate 			exit(1);
707c478bd9Sstevel@tonic-gate 		}
717c478bd9Sstevel@tonic-gate 		map_lower(s);
727c478bd9Sstevel@tonic-gate 		fputs(s, hfp);
737c478bd9Sstevel@tonic-gate 		pclose(hfp);
747c478bd9Sstevel@tonic-gate 		goto again;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("EOT\n"));
77*11a8fa6cSceastha 	return (0);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
80*11a8fa6cSceastha static void
map_lower(char * s)81*11a8fa6cSceastha map_lower(char *s)		/* map string s to lower case */
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	for (; *s; ++s)
847c478bd9Sstevel@tonic-gate 		if (isupper(*s))
857c478bd9Sstevel@tonic-gate 			*s = tolower(*s);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
88*11a8fa6cSceastha static void
instruct(void)89*11a8fa6cSceastha instruct(void)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	fputs(gettext(
927c478bd9Sstevel@tonic-gate "\nType keywords (such as author and date) after the > prompt.\n\
937c478bd9Sstevel@tonic-gate References with those keywords are printed if they exist;\n\
947c478bd9Sstevel@tonic-gate \tif nothing matches you are given another prompt.\n\
957c478bd9Sstevel@tonic-gate To quit lookbib, press CTRL-d after the > prompt.\n\n"), stderr);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate }
98