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