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