xref: /titanic_44/usr/src/cmd/refer/refer5.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 "refer..c"
19 #define SAME 0
20 #define NFLAB 3000		/* number of bytes to record all labels */
21 #define NLABC 1000		/* max number of labels */
22 
23 static char sig[MXSIG];
24 static char bflab[NFLAB];
25 static char *labtab[NLABC];
26 static char *lbp = bflab;
27 static char labc[NLABC];
28 static char stbuff[50];
29 static int  prevsig;
30 
31 putsig (nf, flds, nref, nstline, endline, toindex)   /* choose signal style */
32 char *flds[], *nstline, *endline;
33 {
34 	char t[100], t1[MXSIG], t2[100], format[10], *sd, *stline;
35 	int addon, another = 0;
36 	static FILE *fhide = 0;
37 	int i;
38 	char tag;
39 
40 	if (labels) {
41 		if (nf == 0)	/* old */
42 			sprintf(t, "%s%c", labtab[nref], labc[nref]);
43 		else {
44 			*t = 0;
45 			if (keywant)
46 				fpar(nf, flds, t, keywant, 1, 0);
47 			if (science && t[0] == 0) {
48 				if (fpar(nf, flds, t, 'A', 1, 0) != 0) {
49 					if (fpar(nf, flds, t2, 'D', 1, 0) != 0) {
50 						strcat(t, ", ");
51 						strcat(t, t2);
52 					}
53 				}
54 			} else if (t[0] == 0) {
55 				sprintf(format,
56 					nmlen>0 ? "%%.%ds%%s" : "%%s%%s",
57 					nmlen);
58 				/* format is %s%s for default labels */
59 				/* or %.3s%s eg if wanted */
60 				if (fpar(nf, flds, t2, 'D', 1, 0)) {
61 					sd = t2;
62 					if (dtlen > 0) {
63 						int n = strlen(sd) - dtlen;
64 						if (n > 0)
65 							sd += n;
66 					}
67 				} else {
68 					sd = "";
69 				}
70 				t1[0] = 0;
71 				fpar(nf, flds, t1, 'A', 1, 0);
72 				sprintf(t, format, t1, sd);
73 			}
74 			if (keywant) {
75 				addon = 0;
76 				for (sd = t; *sd; sd++)
77 					;
78 				if (*--sd == '-') {
79 					addon = 1;
80 					*sd = 0;
81 				}
82 			}
83 			if ((!keywant || addon) && !science) {
84 			    addch(t, keylet(t, nref));
85 			}
86 			else {
87 			    tokeytab (t,nref);
88 			}
89 		}
90 	}
91 	else {
92 		if (sort)
93 			sprintf(t, "%c%d%c", FLAG, nref, FLAG);
94 		else
95 			sprintf(t, "%d", nref);
96 	}
97 	another = (sd = lookat()) ? prefix(".[", sd) : 0;
98 	if (another && (strcmp(".[\n", sd) != SAME))
99 		fprintf(stderr, (char *)gettext("File %s line %d: punctuation ignored from: %s"),
100 			Ifile, Iline, sd);
101 	if ((strlen(sig) + strlen(t)) > MXSIG)
102 		err(gettext("sig overflow (%d)"), MXSIG);
103 	strcat(sig, t);
104 #if EBUG
105 	fprintf(stderr, "sig is now %s leng %d\n",sig,strlen(sig));
106 #endif
107 	trimnl(nstline);
108 	trimnl(endline);
109 	stline = stbuff;
110 	if (prevsig == 0) {
111 		strcpy (stline, nstline);
112 		prevsig=1;
113 	}
114 	if (stline[2] || endline[2]) {
115 		stline += 2;
116 		endline += 2;
117 	}
118 	else {
119 		stline  = "\\*([.";
120 		endline = "\\*(.]";
121 	}
122 	if (science) {
123 		stline = " (";
124 		endline = ")";
125 	}
126 	if (bare == 0) {
127 		if (!another) {
128 			sprintf(t1, "%s%s%s\n", stline, sig, endline);
129 			if (strlen(t1) > MXSIG)
130 				err(gettext("t1 overflow (%d)"), MXSIG);
131 			append(t1);
132 			flout();
133 			sig[0] = 0;
134 			prevsig = 0;
135 			if (fo == fhide) {
136 				int ch;
137 				fclose(fhide);
138 				fhide = fopen(hidenam, "r");
139 				fo = ftemp;
140 				while ((ch = getc(fhide)) != EOF)
141 					putc(ch, fo);
142 				fclose(fhide);
143 				unlink(hidenam);
144 			}
145 		}
146 		else {
147 			if (labels) {
148 				strcat(sig, ",\\|");
149 			} else {
150 				/*
151 				 * Seperate reference numbers with AFLAG
152 				 * for later sorting and condensing.
153 				 */
154 				t1[0] = AFLAG;
155 				t1[1] = '\0';
156 				strcat(sig, t1);
157 			}
158 			if (fo == ftemp) {	/* hide if need be */
159 				sprintf(hidenam, "/tmp/rj%dc", getpid());
160 #if EBUG
161 				fprintf(stderr, "hiding in %s\n", hidenam);
162 #endif
163 				fhide = fopen(hidenam, "w");
164 				if (fhide == NULL)
165 					err(gettext("Can't get scratch file %s"),
166 						hidenam);
167 				fo = fhide;
168 			}
169 		}
170 	}
171 	if (bare < 2)
172 		if (nf > 0 && toindex)
173 			fprintf(fo,".ds [F %s%c",t,sep);
174 	if (bare > 0)
175 		flout();
176 #if EBUG
177 	fprintf(stderr, "sig is now %s\n",sig);
178 #endif
179 }
180 
181 char *
182 fpar (nf, flds, out, c, seq, prepend)
183 char *flds[], *out;
184 {
185 	char *p, *s;
186 	int i, fnd = 0;
187 
188 	for(i = 0; i < nf; i++)
189 		if (flds[i][1] == c && ++fnd >= seq) {
190 			/* for titles use first word otherwise last */
191 			if (c == 'T' || c == 'J') {
192 				p = flds[i]+3;
193 				if (prefix("A ", p))
194 					p += 2;
195 				if (prefix("An ", p))
196 					p += 3;
197 				if (prefix("The ", p))
198 					p += 4;
199 				mycpy2(out, p, 20);
200 				return(out);
201 			}
202 			/* if its not 'L' then use just the last word */
203 			s = p = flds[i]+2;
204 			if (c != 'L') {
205 			    for(; *p; p++);
206 			    while (p > s && *p != ' ')
207 				    p--;
208 			}
209 			/* special wart for authors */
210 			if (c == 'A' && (p[-1] == ',' || p[1] =='(')) {
211 				p--;
212 				while (p > s && *p != ' ')
213 					p--;
214 				mycpy(out, p+1);
215 			}
216 			else
217 				strcpy(out, p+1);
218 			if (c == 'A' && prepend)
219 				initadd(out, flds[i]+2, p);
220 			return(out);
221 		}
222 	return(0);
223 }
224 
225 putkey(nf, flds, nref, keystr)
226 char *flds[], *keystr;
227 {
228 	char t1[50], *sf;
229 	int ctype, i, count;
230 
231 	fprintf(fo, ".\\\"");
232 	if (nf <= 0)
233 		fprintf(fo, "%s%c%c", labtab[nref], labc[nref], sep);
234 	else {
235 		while (ctype = *keystr++) {
236 			count = atoi(keystr);
237 			if (*keystr=='+')
238 				count=999;
239 			if (count <= 0)
240 				count = 1;
241 			for(i = 1; i <= count; i++) {
242 				sf = fpar(nf, flds, t1, ctype, i, 1);
243 				if (sf == 0)
244 					break;
245 				sf = artskp(sf);
246 				fprintf(fo, "%s%c", sf, '-');
247 			}
248 		}
249 		fprintf(fo, "%c%d%c%c", FLAG, nref, FLAG, sep);
250 	}
251 }
252 
253 
254 tokeytab (t, nref)
255 char *t;
256 {
257 	strcpy(labtab[nref]=lbp, t);
258 	while (*lbp++)
259 		;
260 }
261 
262 keylet(t, nref)
263 char *t;
264 {
265 	int i;
266 	int x = 'a' - 1;
267 
268 	for(i = 1; i < nref; i++) {
269 		if (strcmp(labtab[i], t) == 0)
270 			x = labc[i];
271 	}
272 	tokeytab (t, nref);
273 	if (lbp-bflab > NFLAB)
274 		err(gettext("bflab overflow (%d)"), NFLAB);
275 	if (nref > NLABC)
276 		err(gettext("nref in labc overflow (%d)"), NLABC);
277 #if EBUG
278 	fprintf(stderr, "lbp up to %d of %d\n", lbp-bflab, NFLAB);
279 #endif
280 	return(labc[nref] = x+1);
281 }
282 
283 mycpy(s, t)
284 char *s, *t;
285 {
286 	while (*t && *t != ',' && *t != ' ')
287 		*s++ = *t++;
288 	*s = 0;
289 }
290 
291 mycpy2(s, t, n)
292 char *s, *t;
293 {
294 	int c;
295 
296 	while (n-- && (c= *t++) > 0) {
297 		if (c == ' ')
298 			c = '-';
299 		*s++ = c;
300 	}
301 	*s = 0;
302 }
303 
304 initadd(to, from, stop)
305 char *to, *from, *stop;
306 {
307 	int c, nalph = 1;
308 
309 	while (*to)
310 		to++;
311 	while (from < stop) {
312 		c = *from++;
313 		if (!isalpha(c)) {
314 			if (nalph)
315 				*to++ = '.';
316 			nalph = 0;
317 			continue;
318 		}
319 		if (nalph++ == 0)
320 			*to++ = c;
321 	}
322 	*to = 0;
323 }
324 
325 static char *articles[] = {
326 	"the ", "an ", "a ", 0
327 };
328 
329 char *
330 artskp(s)	/* skips over initial "a ", "an ", "the " in s */
331 char *s;
332 {
333 
334 	char **p, *r1, *r2;
335 
336 	for (p = articles; *p; p++) {
337 		r2 = s;
338 		for (r1 = *p; ((*r1 ^ *r2) & ~040 ) == 0; r1++)
339 			r2++;
340 		if (*r1 == 0 && *r2 != 0)
341 			return(r2);
342 	}
343 	return(s);
344 }
345