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