xref: /original-bsd/old/refer/refer/refer5.c (revision 43bfbc1c)
1 #ifndef lint
2 static char *sccsid = "@(#)refer5.c	4.7 (Berkeley) 04/23/86";
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 		if (flds[i][1] == c && ++fnd >= seq) {
170 			/* for titles use first word otherwise last */
171 			if (c == 'T' || c == 'J') {
172 				p = flds[i]+3;
173 				if (prefix("A ", p))
174 					p += 2;
175 				if (prefix("An ", p))
176 					p += 3;
177 				if (prefix("The ", p))
178 					p += 4;
179 				mycpy2(out, p, 20);
180 				return(out);
181 			}
182 			/* if its not 'L' then use just the last word */
183 			s = p = flds[i]+2;
184 			if (c != 'L') {
185 			    for(; *p; p++);
186 			    while (p > s && *p != ' ')
187 				    p--;
188 			}
189 			/* special wart for authors */
190 			if (c == 'A' && (p[-1] == ',' || p[1] =='(')) {
191 				p--;
192 				while (p > s && *p != ' ')
193 					p--;
194 				mycpy(out, p+1);
195 			}
196 			else
197 				strcpy(out, p+1);
198 			if (c == 'A' && prepend)
199 				initadd(out, flds[i]+2, p);
200 			return(out);
201 		}
202 	return(0);
203 }
204 
205 putkey(nf, flds, nref, keystr)
206 char *flds[], *keystr;
207 {
208 	char t1[50], *sf;
209 	int ctype, i, count;
210 
211 	fprintf(fo, ".\\\"");
212 	if (nf <= 0)
213 		fprintf(fo, "%s%c%c", labtab[nref], labc[nref], sep);
214 	else {
215 		while (ctype = *keystr++) {
216 			count = atoi(keystr);
217 			if (*keystr=='+')
218 				count=999;
219 			if (count <= 0)
220 				count = 1;
221 			for(i = 1; i <= count; i++) {
222 				sf = fpar(nf, flds, t1, ctype, i, 1);
223 				if (sf == 0)
224 					break;
225 				sf = artskp(sf);
226 				fprintf(fo, "%s%c", sf, '-');
227 			}
228 		}
229 		fprintf(fo, "%c%d%c%c", FLAG, nref, FLAG, sep);
230 	}
231 }
232 
233 
234 tokeytab (t, nref)
235 char *t;
236 {
237 	strcpy(labtab[nref]=lbp, t);
238 	while (*lbp++)
239 		;
240 }
241 
242 keylet(t, nref)
243 char *t;
244 {
245 	int i;
246 	int x = 'a' - 1;
247 
248 	for(i = 1; i < nref; i++) {
249 		if (strcmp(labtab[i], t) == 0)
250 			x = labc[i];
251 	}
252 	tokeytab (t, nref);
253 	if (lbp-bflab > NFLAB)
254 		err("bflab overflow (%d)", NFLAB);
255 	if (nref > NLABC)
256 		err("nref in labc overflow (%d)", NLABC);
257 #if EBUG
258 	fprintf(stderr, "lbp up to %d of %d\n", lbp-bflab, NFLAB);
259 #endif
260 	return(labc[nref] = x+1);
261 }
262 
263 mycpy(s, t)
264 char *s, *t;
265 {
266 	while (*t && *t != ',' && *t != ' ')
267 		*s++ = *t++;
268 	*s = 0;
269 }
270 
271 mycpy2(s, t, n)
272 char *s, *t;
273 {
274 	int c;
275 
276 	while (n-- && (c= *t++) > 0) {
277 		if (c == ' ')
278 			c = '-';
279 		*s++ = c;
280 	}
281 	*s = 0;
282 }
283 
284 initadd(to, from, stop)
285 char *to, *from, *stop;
286 {
287 	int c, nalph = 1;
288 
289 	while (*to)
290 		to++;
291 	while (from < stop) {
292 		c = *from++;
293 		if (!isalpha(c)) {
294 			if (nalph)
295 				*to++ = '.';
296 			nalph = 0;
297 			continue;
298 		}
299 		if (nalph++ == 0)
300 			*to++ = c;
301 	}
302 	*to = 0;
303 }
304 
305 static char *articles[] = {
306 	"the ", "an ", "a ", 0
307 };
308 
309 char *
310 artskp(s)	/* skips over initial "a ", "an ", "the " in s */
311 char *s;
312 {
313 
314 	char **p, *r1, *r2;
315 
316 	for (p = articles; *p; p++) {
317 		r2 = s;
318 		for (r1 = *p; ((*r1 ^ *r2) & ~040 ) == 0; r1++)
319 			r2++;
320 		if (*r1 == 0 && *r2 != 0)
321 			return(r2);
322 	}
323 	return(s);
324 }
325