xref: /titanic_50/usr/src/cmd/refer/refer1.c (revision de3d2ce46fc25c7b67ccbae4afe5f15e5357568f)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 /*
10  * Copyright (c) 1980 Regents of the University of California.
11  * All rights reserved. The Berkeley software License Agreement
12  * specifies the terms and conditions for redistribution.
13  */
14 
15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
16 
17 #include <stdlib.h>
18 #include <signal.h>
19 #include <locale.h>
20 #include "refer..c"
21 
22 extern void clfgrep();
23 extern void doref();
24 extern void dumpold();
25 extern void err();
26 extern void output();
27 extern int prefix();
28 extern void recopy();
29 
30 void cleanup(void);
31 void signals(void);
32 
33 int
34 main(int argc, char *argv[])	/* process command-line arguments */
35 {
36 	char line[BUFSIZ], *s;
37 	int nodeflt = 0;
38 
39 	(void) setlocale(LC_ALL, "");
40 #if !defined(TEXT_DOMAIN)
41 #define	TEXT_DOMAIN "SYS_TEST"
42 #endif
43 	(void) textdomain(TEXT_DOMAIN);
44 
45 	signals();
46 	while (argc > 1 && argv[1][0] == '-') {
47 		switch (argv[1][1]) {
48 		case 'e':
49 			endpush++;
50 			break;
51 		case 's':
52 			sort++;
53 			endpush = 1;
54 			if (argv[1][2])
55 				keystr = argv[1]+2;
56 			break;
57 		case 'l':
58 			labels++;
59 			s = argv[1]+2;
60 			nmlen = atoi(s);
61 			while (*s)
62 				if (*s++ == ',')
63 					break;
64 			dtlen = atoi(s);
65 			break;
66 		case 'k':
67 			keywant = (argv[1][2] ? argv[1][2] : 'L');
68 			labels++;
69 			break;
70 		case 'n':
71 			nodeflt = 1;
72 			break;
73 		case 'p':
74 			argc--;
75 			argv++;
76 			*search++ = argv[1];
77 			if (search-rdata > NSERCH)
78 				err(gettext(
79 				    "too many -p options (%d)"), NSERCH);
80 			break;
81 		case 'a':
82 			authrev = atoi(argv[1]+2);
83 			if (authrev <= 0)
84 				authrev = 1000;
85 			break;
86 		case 'b':
87 			bare = (argv[1][2] == '1') ? 1 : 2;
88 			break;
89 		case 'c':
90 			smallcaps = argv[1]+2;
91 			break;
92 		case 'f':
93 			refnum = atoi(argv[1]+2) - 1;
94 			break;
95 		case 'B':
96 			biblio++;
97 			bare = 2;
98 			if (argv[1][2])
99 				convert = argv[1]+2;
100 			break;
101 		case 'S':
102 			science++;
103 			labels = 1;
104 			break;
105 		case 'P':
106 			postpunct++;
107 			break;
108 		}
109 		argc--;
110 		argv++;
111 	}
112 	if (getenv("REFER") != NULL)
113 		*search++ = getenv("REFER");
114 	else if (nodeflt == 0)
115 		*search++ = "/usr/lib/refer/papers/Ind";
116 	if (sort && !labels) {
117 		sprintf(ofile, "/tmp/rj%db", getpid());
118 		ftemp = fopen(ofile, "w");
119 		if (ftemp == NULL) {
120 			fprintf(stderr, gettext("Can't open scratch file\n"));
121 			exit(1);
122 		}
123 	}
124 	if (endpush) {
125 		sprintf(tfile, "/tmp/rj%da", getpid());
126 		fo = fopen(tfile, "w");
127 		if (fo == NULL) {
128 			fo = ftemp;
129 			fprintf(stderr, gettext("Can't open scratch file"));
130 		}
131 		sep = 002; /* separate records without confusing sort.. */
132 	} else
133 		fo = ftemp;
134 	do {
135 		if (argc > 1) {
136 			fclose(in);
137 			Iline = 0;
138 			in = fopen(Ifile = argv[1], "r");
139 			argc--;
140 			argv++;
141 			if (in == NULL) {
142 				err(gettext("Can't read %s"), Ifile);
143 				continue;
144 			}
145 		}
146 		while (input(line)) {
147 			Iline++;
148 			if (biblio && *line == '\n')
149 				doref(line);
150 			else if (biblio && Iline == 1 && *line == '%')
151 				doref(line);
152 			else if (!prefix(".[", line))
153 				output(line);
154 			else
155 				doref(line);
156 		}
157 	} while (argc > 1);
158 
159 	if (endpush && fo != NULL)
160 		dumpold();
161 	output("");
162 	if (sort && !labels)
163 		recopy(ofile);
164 	clfgrep();
165 	cleanup();
166 	return (0);
167 }
168 
169 extern void intr();
170 
171 void
172 signals(void)
173 {
174 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
175 		signal(SIGINT, intr);
176 	signal(SIGHUP, intr);
177 	signal(SIGPIPE, intr);
178 	signal(SIGTERM, intr);
179 }
180 
181 void
182 intr(void)
183 {
184 	signal(SIGINT, SIG_IGN);
185 	cleanup();
186 	exit(1);
187 }
188 
189 void
190 cleanup(void)
191 {
192 	if (tfile[0])
193 		unlink(tfile);
194 	if (gfile[0])
195 		unlink(gfile);
196 	if (ofile[0])
197 		unlink(ofile);
198 	if (hidenam[0])
199 		unlink(hidenam);
200 }
201