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