1 /* $MirOS: src/usr.bin/oldroff/refer/refer1.c,v 1.2 2008/11/08 23:04:46 tg Exp $ */
2 
3 /*-
4  * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
5  *     The Regents of the University of California.
6  * Copyright (C) Caldera International Inc.  2001-2002.
7  * Copyright (c) 2003, 2004
8  *	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms,
12  * with or without modification, are permitted provided
13  * that the following conditions are met:
14  *
15  * Redistributions of source code and documentation must retain
16  * the above copyright notice, this list of conditions and the
17  * following disclaimer.  Redistributions in binary form must
18  * reproduce the above copyright notice, this list of conditions
19  * and the following disclaimer in the documentation and/or other
20  * materials provided with the distribution.
21  *
22  * All advertising materials mentioning features or use of this
23  * software must display the following acknowledgement:
24  *   This product includes software developed or owned by
25  *   Caldera International, Inc.
26  *
27  * Neither the name of Caldera International, Inc. nor the names
28  * of other contributors may be used to endorse or promote products
29  * derived from this software without specific prior written permission.
30  *
31  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
32  * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
33  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
36  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
41  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  */
44 
45 #ifndef lint
46 static char sccsid[] = "@(#)refer1.c	4.6 (Berkeley) 4/18/91";
47 #endif /* not lint */
48 
49 #include <signal.h>
50 #include "refer..c"
51 #include "pathnames.h"
52 
main(argc,argv)53 main(argc,argv)		/* process command-line arguments */
54 char *argv[];
55 {
56 	char line[BUFSIZ], *s;
57 	int nodeflt = 0;
58 
59 	signals();
60 	while (argv[1][0] == '-') {
61 		switch(argv[1][1]) {
62 		case 'e':
63 			endpush++;
64 			break;
65 		case 's':
66 			sort++;
67 			endpush = 1;
68 			if (argv[1][2])
69 				keystr = argv[1]+2;
70 			break;
71 		case 'l':
72 			labels++;
73 			s = argv[1]+2;
74 			nmlen = atoi(s);
75 			while (*s)
76 				if (*s++ == ',')
77 					break;
78 			dtlen = atoi(s);
79 			break;
80 		case 'k':
81 			keywant = (argv[1][2] ? argv[1][2] : 'L');
82 			labels++;
83 			break;
84 		case 'n':
85 			nodeflt = 1;
86 			break;
87 		case 'p':
88 			argc--;
89 			argv++;
90 			*search++ = argv[1];
91 			if (search-rdata > NSERCH)
92 				err("too many -p options (%d)", NSERCH);
93 			break;
94 		case 'a':
95 			authrev = atoi(argv[1]+2);
96 			if (authrev<=0)
97 				authrev = 1000;
98 			break;
99 		case 'b':
100 			bare = (argv[1][2] == '1') ? 1 : 2;
101 			break;
102 		case 'c':
103 			smallcaps = argv[1]+2;
104 			break;
105 		case 'f':
106 			refnum = atoi(argv[1]+2) - 1;
107 			break;
108 		case 'B':
109 			biblio++;
110 			bare = 2;
111 			if (argv[1][2])
112 				convert = argv[1]+2;
113 			break;
114 		case 'S':
115 			science++;
116 			labels = 1;
117 			break;
118 		case 'P':
119 			postpunct++;
120 			break;
121 		}
122 		argc--;
123 		argv++;
124 	}
125 	if (getenv("REFER") != NULL)
126 		*search++ = getenv("REFER");
127 	else if (nodeflt == 0)
128 		*search++ = _PATH_IND;
129 	if (!labels) {
130 		sprintf(ofile, "%s/rj%db", _PATH_TMP, getpid());
131 		ftemp = fopen(ofile, "w");
132 		if (ftemp == NULL) {
133 			fprintf(stderr, "Can't open scratch file\n");
134 			exit(1);
135 		}
136 	}
137 	if (endpush) {
138 		sprintf(tfile, "%s/rj%da", _PATH_TMP, getpid());
139 		fo = fopen(tfile, "w");
140 		if (fo == NULL) {
141 			fo = ftemp;
142 			fprintf(stderr, "Can't open scratch file");
143 		}
144 		sep = 002; /* separate records without confusing sort..*/
145 	} else
146 		fo = ftemp;
147 	do {
148 		if (argc > 1) {
149 			fclose(in);
150 			Iline = 0;
151 			in = fopen(Ifile = argv[1], "r");
152 			argc--;
153 			argv++;
154 			if (in == NULL) {
155 				err("Can't read %s", Ifile);
156 				continue;
157 			}
158 		}
159 		while (input(line)) {
160 			Iline++;
161 			if (biblio && *line == '\n')
162 				doref(line);
163 			else if (biblio && Iline == 1 && *line == '%')
164 				doref(line);
165 			else if (!prefix(".[", line))
166 				output(line);
167 			else
168 				doref(line);
169 		}
170 	} while (argc > 1);
171 
172 	if (endpush && fo != NULL)
173 		dumpold();
174 	output("");
175 	if (!labels)
176 		recopy(ofile);
177 	clfgrep();
178 	cleanup();
179 	exit(0);
180 }
181 
signals()182 signals()
183 {
184 	void intr();
185 
186 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
187 		signal(SIGINT, intr);
188 	signal(SIGHUP, intr);
189 	signal(SIGPIPE, intr);
190 	signal(SIGTERM, intr);
191 }
192 
193 void
intr()194 intr()
195 {
196 	signal(SIGINT, SIG_IGN);
197 	cleanup();
198 	exit(1);
199 }
200 
cleanup()201 cleanup()
202 {
203 	if (tfile[0])
204 		unlink(tfile);
205 	if (gfile[0])
206 		unlink(gfile);
207 	if (ofile[0])
208 		unlink(ofile);
209 	if (hidenam[0])
210 		unlink(hidenam);
211 }
212