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