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