xref: /titanic_51/usr/src/cmd/refer/refer1.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 
17*11a8fa6cSceastha #include <stdlib.h>
187c478bd9Sstevel@tonic-gate #include <signal.h>
197c478bd9Sstevel@tonic-gate #include <locale.h>
207c478bd9Sstevel@tonic-gate #include "refer..c"
217c478bd9Sstevel@tonic-gate 
22*11a8fa6cSceastha extern void clfgrep();
23*11a8fa6cSceastha extern void doref();
24*11a8fa6cSceastha extern void dumpold();
25*11a8fa6cSceastha extern void err();
26*11a8fa6cSceastha extern void output();
27*11a8fa6cSceastha extern int prefix();
28*11a8fa6cSceastha extern void recopy();
29*11a8fa6cSceastha 
30*11a8fa6cSceastha void cleanup(void);
31*11a8fa6cSceastha void signals(void);
32*11a8fa6cSceastha 
33*11a8fa6cSceastha int
34*11a8fa6cSceastha main(int argc, char *argv[])	/* process command-line arguments */
357c478bd9Sstevel@tonic-gate {
367c478bd9Sstevel@tonic-gate 	char line[BUFSIZ], *s;
377c478bd9Sstevel@tonic-gate 	int nodeflt = 0;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
407c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
417c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	signals();
467c478bd9Sstevel@tonic-gate 	while (argc > 1 && argv[1][0] == '-') {
477c478bd9Sstevel@tonic-gate 		switch (argv[1][1]) {
487c478bd9Sstevel@tonic-gate 		case 'e':
497c478bd9Sstevel@tonic-gate 			endpush++;
507c478bd9Sstevel@tonic-gate 			break;
517c478bd9Sstevel@tonic-gate 		case 's':
527c478bd9Sstevel@tonic-gate 			sort++;
537c478bd9Sstevel@tonic-gate 			endpush = 1;
547c478bd9Sstevel@tonic-gate 			if (argv[1][2])
557c478bd9Sstevel@tonic-gate 				keystr = argv[1]+2;
567c478bd9Sstevel@tonic-gate 			break;
577c478bd9Sstevel@tonic-gate 		case 'l':
587c478bd9Sstevel@tonic-gate 			labels++;
597c478bd9Sstevel@tonic-gate 			s = argv[1]+2;
607c478bd9Sstevel@tonic-gate 			nmlen = atoi(s);
617c478bd9Sstevel@tonic-gate 			while (*s)
627c478bd9Sstevel@tonic-gate 				if (*s++ == ',')
637c478bd9Sstevel@tonic-gate 					break;
647c478bd9Sstevel@tonic-gate 			dtlen = atoi(s);
657c478bd9Sstevel@tonic-gate 			break;
667c478bd9Sstevel@tonic-gate 		case 'k':
677c478bd9Sstevel@tonic-gate 			keywant = (argv[1][2] ? argv[1][2] : 'L');
687c478bd9Sstevel@tonic-gate 			labels++;
697c478bd9Sstevel@tonic-gate 			break;
707c478bd9Sstevel@tonic-gate 		case 'n':
717c478bd9Sstevel@tonic-gate 			nodeflt = 1;
727c478bd9Sstevel@tonic-gate 			break;
737c478bd9Sstevel@tonic-gate 		case 'p':
747c478bd9Sstevel@tonic-gate 			argc--;
757c478bd9Sstevel@tonic-gate 			argv++;
767c478bd9Sstevel@tonic-gate 			*search++ = argv[1];
777c478bd9Sstevel@tonic-gate 			if (search-rdata > NSERCH)
78*11a8fa6cSceastha 				err(gettext(
79*11a8fa6cSceastha 				    "too many -p options (%d)"), NSERCH);
807c478bd9Sstevel@tonic-gate 			break;
817c478bd9Sstevel@tonic-gate 		case 'a':
827c478bd9Sstevel@tonic-gate 			authrev = atoi(argv[1]+2);
837c478bd9Sstevel@tonic-gate 			if (authrev <= 0)
847c478bd9Sstevel@tonic-gate 				authrev = 1000;
857c478bd9Sstevel@tonic-gate 			break;
867c478bd9Sstevel@tonic-gate 		case 'b':
877c478bd9Sstevel@tonic-gate 			bare = (argv[1][2] == '1') ? 1 : 2;
887c478bd9Sstevel@tonic-gate 			break;
897c478bd9Sstevel@tonic-gate 		case 'c':
907c478bd9Sstevel@tonic-gate 			smallcaps = argv[1]+2;
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 		case 'f':
937c478bd9Sstevel@tonic-gate 			refnum = atoi(argv[1]+2) - 1;
947c478bd9Sstevel@tonic-gate 			break;
957c478bd9Sstevel@tonic-gate 		case 'B':
967c478bd9Sstevel@tonic-gate 			biblio++;
977c478bd9Sstevel@tonic-gate 			bare = 2;
987c478bd9Sstevel@tonic-gate 			if (argv[1][2])
997c478bd9Sstevel@tonic-gate 				convert = argv[1]+2;
1007c478bd9Sstevel@tonic-gate 			break;
1017c478bd9Sstevel@tonic-gate 		case 'S':
1027c478bd9Sstevel@tonic-gate 			science++;
1037c478bd9Sstevel@tonic-gate 			labels = 1;
1047c478bd9Sstevel@tonic-gate 			break;
1057c478bd9Sstevel@tonic-gate 		case 'P':
1067c478bd9Sstevel@tonic-gate 			postpunct++;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate 		argc--;
1107c478bd9Sstevel@tonic-gate 		argv++;
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	if (getenv("REFER") != NULL)
1137c478bd9Sstevel@tonic-gate 		*search++ = getenv("REFER");
1147c478bd9Sstevel@tonic-gate 	else if (nodeflt == 0)
1157c478bd9Sstevel@tonic-gate 		*search++ = "/usr/lib/refer/papers/Ind";
1167c478bd9Sstevel@tonic-gate 	if (sort && !labels) {
1177c478bd9Sstevel@tonic-gate 		sprintf(ofile, "/tmp/rj%db", getpid());
1187c478bd9Sstevel@tonic-gate 		ftemp = fopen(ofile, "w");
1197c478bd9Sstevel@tonic-gate 		if (ftemp == NULL) {
1207c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Can't open scratch file\n"));
1217c478bd9Sstevel@tonic-gate 			exit(1);
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	if (endpush) {
1257c478bd9Sstevel@tonic-gate 		sprintf(tfile, "/tmp/rj%da", getpid());
1267c478bd9Sstevel@tonic-gate 		fo = fopen(tfile, "w");
1277c478bd9Sstevel@tonic-gate 		if (fo == NULL) {
1287c478bd9Sstevel@tonic-gate 			fo = ftemp;
1297c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Can't open scratch file"));
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 		sep = 002; /* separate records without confusing sort.. */
1327c478bd9Sstevel@tonic-gate 	} else
1337c478bd9Sstevel@tonic-gate 		fo = ftemp;
1347c478bd9Sstevel@tonic-gate 	do {
1357c478bd9Sstevel@tonic-gate 		if (argc > 1) {
1367c478bd9Sstevel@tonic-gate 			fclose(in);
1377c478bd9Sstevel@tonic-gate 			Iline = 0;
1387c478bd9Sstevel@tonic-gate 			in = fopen(Ifile = argv[1], "r");
1397c478bd9Sstevel@tonic-gate 			argc--;
1407c478bd9Sstevel@tonic-gate 			argv++;
1417c478bd9Sstevel@tonic-gate 			if (in == NULL) {
1427c478bd9Sstevel@tonic-gate 				err(gettext("Can't read %s"), Ifile);
1437c478bd9Sstevel@tonic-gate 				continue;
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 		while (input(line)) {
1477c478bd9Sstevel@tonic-gate 			Iline++;
1487c478bd9Sstevel@tonic-gate 			if (biblio && *line == '\n')
1497c478bd9Sstevel@tonic-gate 				doref(line);
1507c478bd9Sstevel@tonic-gate 			else if (biblio && Iline == 1 && *line == '%')
1517c478bd9Sstevel@tonic-gate 				doref(line);
1527c478bd9Sstevel@tonic-gate 			else if (!prefix(".[", line))
1537c478bd9Sstevel@tonic-gate 				output(line);
1547c478bd9Sstevel@tonic-gate 			else
1557c478bd9Sstevel@tonic-gate 				doref(line);
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 	} while (argc > 1);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (endpush && fo != NULL)
1607c478bd9Sstevel@tonic-gate 		dumpold();
1617c478bd9Sstevel@tonic-gate 	output("");
1627c478bd9Sstevel@tonic-gate 	if (sort && !labels)
1637c478bd9Sstevel@tonic-gate 		recopy(ofile);
1647c478bd9Sstevel@tonic-gate 	clfgrep();
1657c478bd9Sstevel@tonic-gate 	cleanup();
166*11a8fa6cSceastha 	return (0);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate extern void intr();
1707c478bd9Sstevel@tonic-gate 
171*11a8fa6cSceastha void
172*11a8fa6cSceastha signals(void)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1757c478bd9Sstevel@tonic-gate 		signal(SIGINT, intr);
1767c478bd9Sstevel@tonic-gate 	signal(SIGHUP, intr);
1777c478bd9Sstevel@tonic-gate 	signal(SIGPIPE, intr);
1787c478bd9Sstevel@tonic-gate 	signal(SIGTERM, intr);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
181*11a8fa6cSceastha void
182*11a8fa6cSceastha intr(void)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
1857c478bd9Sstevel@tonic-gate 	cleanup();
1867c478bd9Sstevel@tonic-gate 	exit(1);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
189*11a8fa6cSceastha void
190*11a8fa6cSceastha cleanup(void)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	if (tfile[0])
1937c478bd9Sstevel@tonic-gate 		unlink(tfile);
1947c478bd9Sstevel@tonic-gate 	if (gfile[0])
1957c478bd9Sstevel@tonic-gate 		unlink(gfile);
1967c478bd9Sstevel@tonic-gate 	if (ofile[0])
1977c478bd9Sstevel@tonic-gate 		unlink(ofile);
1987c478bd9Sstevel@tonic-gate 	if (hidenam[0])
1997c478bd9Sstevel@tonic-gate 		unlink(hidenam);
2007c478bd9Sstevel@tonic-gate }
201