xref: /titanic_53/usr/src/cmd/refer/refer2.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #include "refer..c"
19*7c478bd9Sstevel@tonic-gate #include <locale.h>
20*7c478bd9Sstevel@tonic-gate #define NFLD 30
21*7c478bd9Sstevel@tonic-gate #define TLEN 512
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate extern FILE *in;
24*7c478bd9Sstevel@tonic-gate char one[ANSLEN];
25*7c478bd9Sstevel@tonic-gate int onelen = ANSLEN;
26*7c478bd9Sstevel@tonic-gate static char dr [100] = "";
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate doref(line1)
29*7c478bd9Sstevel@tonic-gate char *line1;
30*7c478bd9Sstevel@tonic-gate {
31*7c478bd9Sstevel@tonic-gate 	char buff[QLEN], dbuff[3*QLEN];
32*7c478bd9Sstevel@tonic-gate 	char answer[ANSLEN], temp[TLEN], line[BUFSIZ];
33*7c478bd9Sstevel@tonic-gate 	char *p, **sr, *flds[NFLD], *r;
34*7c478bd9Sstevel@tonic-gate 	int stat, nf, nr, query = 0, alph, digs;
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate    again:
37*7c478bd9Sstevel@tonic-gate 	buff[0] = dbuff[0] = NULL;
38*7c478bd9Sstevel@tonic-gate 	if (biblio && Iline == 1 && line1[0] == '%')
39*7c478bd9Sstevel@tonic-gate 		strcat(dbuff, line1);
40*7c478bd9Sstevel@tonic-gate 	while (input(line)) {		/* get query */
41*7c478bd9Sstevel@tonic-gate 		Iline++;
42*7c478bd9Sstevel@tonic-gate 		if (prefix(".]", line))
43*7c478bd9Sstevel@tonic-gate 			break;
44*7c478bd9Sstevel@tonic-gate 		if (biblio && line[0] == '\n')
45*7c478bd9Sstevel@tonic-gate 			break;
46*7c478bd9Sstevel@tonic-gate 		if (biblio && line[0] == '%' && line[1] == *convert)
47*7c478bd9Sstevel@tonic-gate 			break;
48*7c478bd9Sstevel@tonic-gate 		if (control(line[0]))
49*7c478bd9Sstevel@tonic-gate 			query = 1;
50*7c478bd9Sstevel@tonic-gate 		strcat(query ? dbuff : buff, line);
51*7c478bd9Sstevel@tonic-gate 		if (strlen(buff) > QLEN)
52*7c478bd9Sstevel@tonic-gate 			err(gettext("query too long (%d)"), strlen(buff));
53*7c478bd9Sstevel@tonic-gate 		if (strlen(dbuff) > 3 * QLEN)
54*7c478bd9Sstevel@tonic-gate 			err(gettext("record at line %d too long"), Iline-1);
55*7c478bd9Sstevel@tonic-gate 	}
56*7c478bd9Sstevel@tonic-gate 	if (biblio && line[0] == '\n' && feof(in))
57*7c478bd9Sstevel@tonic-gate 		return;
58*7c478bd9Sstevel@tonic-gate 	if (strcmp(buff, "$LIST$\n")==0) {
59*7c478bd9Sstevel@tonic-gate 		assert (dbuff[0] == 0);
60*7c478bd9Sstevel@tonic-gate 		dumpold();
61*7c478bd9Sstevel@tonic-gate 		return;
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate 	answer[0] = 0;
64*7c478bd9Sstevel@tonic-gate 	for (p = buff; *p; p++) {
65*7c478bd9Sstevel@tonic-gate 		if (isupper(*p))
66*7c478bd9Sstevel@tonic-gate 			*p |= 040;
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 	alph = digs = 0;
69*7c478bd9Sstevel@tonic-gate 	for (p = buff; *p; p++) {
70*7c478bd9Sstevel@tonic-gate 		if (isalpha(*p))
71*7c478bd9Sstevel@tonic-gate 			alph++;
72*7c478bd9Sstevel@tonic-gate 		else
73*7c478bd9Sstevel@tonic-gate 			if (isdigit(*p))
74*7c478bd9Sstevel@tonic-gate 				digs++;
75*7c478bd9Sstevel@tonic-gate 			else {
76*7c478bd9Sstevel@tonic-gate 				*p = 0;
77*7c478bd9Sstevel@tonic-gate 				if ((alph+digs < 3) || common(p-alph)) {
78*7c478bd9Sstevel@tonic-gate 					r = p-alph;
79*7c478bd9Sstevel@tonic-gate 					while (r < p)
80*7c478bd9Sstevel@tonic-gate 						*r++ = ' ';
81*7c478bd9Sstevel@tonic-gate 				}
82*7c478bd9Sstevel@tonic-gate 				if (alph == 0 && digs > 0) {
83*7c478bd9Sstevel@tonic-gate 					r = p-digs;
84*7c478bd9Sstevel@tonic-gate 					if (digs != 4 || atoi(r)/100 != 19) {
85*7c478bd9Sstevel@tonic-gate 						while (r < p)
86*7c478bd9Sstevel@tonic-gate 							*r++ = ' ';
87*7c478bd9Sstevel@tonic-gate 					}
88*7c478bd9Sstevel@tonic-gate 				}
89*7c478bd9Sstevel@tonic-gate 				*p = ' ';
90*7c478bd9Sstevel@tonic-gate 				alph = digs = 0;
91*7c478bd9Sstevel@tonic-gate 			}
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	one[0] = 0;
94*7c478bd9Sstevel@tonic-gate 	if (buff[0]) {	/* do not search if no query */
95*7c478bd9Sstevel@tonic-gate 		for (sr = rdata; sr < search; sr++) {
96*7c478bd9Sstevel@tonic-gate 			temp[0] = 0;
97*7c478bd9Sstevel@tonic-gate 			corout(buff, temp, "hunt", *sr, TLEN);
98*7c478bd9Sstevel@tonic-gate 			assert(strlen(temp) < TLEN);
99*7c478bd9Sstevel@tonic-gate 			if (strlen(temp)+strlen(answer) > BUFSIZ)
100*7c478bd9Sstevel@tonic-gate 				err(gettext("Accumulated answers too large"),0);
101*7c478bd9Sstevel@tonic-gate 			strcat(answer, temp);
102*7c478bd9Sstevel@tonic-gate 			if (strlen(answer)>BUFSIZ)
103*7c478bd9Sstevel@tonic-gate 				err(gettext("answer too long (%d)"), strlen(answer));
104*7c478bd9Sstevel@tonic-gate 			if (newline(answer) > 0)
105*7c478bd9Sstevel@tonic-gate 				break;
106*7c478bd9Sstevel@tonic-gate 		}
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	assert(strlen(one) < ANSLEN);
109*7c478bd9Sstevel@tonic-gate 	assert(strlen(answer) < ANSLEN);
110*7c478bd9Sstevel@tonic-gate 	if (buff[0])
111*7c478bd9Sstevel@tonic-gate 		switch (newline(answer)) {
112*7c478bd9Sstevel@tonic-gate 		case 0:
113*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("No such paper: %s\n"), buff);
114*7c478bd9Sstevel@tonic-gate 			return;
115*7c478bd9Sstevel@tonic-gate 		default:
116*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("Too many hits: %s\n"), trimnl(buff));
117*7c478bd9Sstevel@tonic-gate 			choices(answer);
118*7c478bd9Sstevel@tonic-gate 			p = buff;
119*7c478bd9Sstevel@tonic-gate 			while (*p != '\n')
120*7c478bd9Sstevel@tonic-gate 				p++;
121*7c478bd9Sstevel@tonic-gate 			*++p = 0;
122*7c478bd9Sstevel@tonic-gate 		case 1:
123*7c478bd9Sstevel@tonic-gate 			if (endpush)
124*7c478bd9Sstevel@tonic-gate 				if (nr = chkdup(answer)) {
125*7c478bd9Sstevel@tonic-gate 					if (bare < 2) {
126*7c478bd9Sstevel@tonic-gate 						nf = tabs(flds, one);
127*7c478bd9Sstevel@tonic-gate 						nf += tabs(flds+nf, dbuff);
128*7c478bd9Sstevel@tonic-gate 						assert(nf < NFLD);
129*7c478bd9Sstevel@tonic-gate 						putsig(nf,flds,nr,line1,line,0);
130*7c478bd9Sstevel@tonic-gate 					}
131*7c478bd9Sstevel@tonic-gate 					return;
132*7c478bd9Sstevel@tonic-gate 				}
133*7c478bd9Sstevel@tonic-gate 			if (one[0] == 0)
134*7c478bd9Sstevel@tonic-gate 				corout(answer, one, "deliv", dr, QLEN);
135*7c478bd9Sstevel@tonic-gate 			break;
136*7c478bd9Sstevel@tonic-gate 		}
137*7c478bd9Sstevel@tonic-gate 	assert(strlen(buff) < QLEN);
138*7c478bd9Sstevel@tonic-gate 	assert(strlen(one) < ANSLEN);
139*7c478bd9Sstevel@tonic-gate 	nf = tabs(flds, one);
140*7c478bd9Sstevel@tonic-gate 	nf += tabs(flds+nf, dbuff);
141*7c478bd9Sstevel@tonic-gate 	assert(nf < NFLD);
142*7c478bd9Sstevel@tonic-gate 	refnum++;
143*7c478bd9Sstevel@tonic-gate 	if (sort)
144*7c478bd9Sstevel@tonic-gate 		putkey(nf, flds, refnum, keystr);
145*7c478bd9Sstevel@tonic-gate 	if (bare < 2)
146*7c478bd9Sstevel@tonic-gate 		putsig(nf, flds, refnum, line1, line, 1);
147*7c478bd9Sstevel@tonic-gate 	else
148*7c478bd9Sstevel@tonic-gate 		flout();
149*7c478bd9Sstevel@tonic-gate 	putref(nf, flds);
150*7c478bd9Sstevel@tonic-gate 	if (biblio && line[0] == '\n')
151*7c478bd9Sstevel@tonic-gate 		goto again;
152*7c478bd9Sstevel@tonic-gate 	if (biblio && line[0] == '%' && line[1] == *convert)
153*7c478bd9Sstevel@tonic-gate 		fprintf(fo, "%s%c%s", convert+1, sep, line+3);
154*7c478bd9Sstevel@tonic-gate }
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate newline(s)
157*7c478bd9Sstevel@tonic-gate char *s;
158*7c478bd9Sstevel@tonic-gate {
159*7c478bd9Sstevel@tonic-gate 	int k = 0, c;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	while (c = *s++)
162*7c478bd9Sstevel@tonic-gate 		if (c == '\n')
163*7c478bd9Sstevel@tonic-gate 		k++;
164*7c478bd9Sstevel@tonic-gate 	return(k);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate choices(buff)
168*7c478bd9Sstevel@tonic-gate char *buff;
169*7c478bd9Sstevel@tonic-gate {
170*7c478bd9Sstevel@tonic-gate 	char ob[BUFSIZ], *p, *r, *q, *t;
171*7c478bd9Sstevel@tonic-gate 	int nl;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	for (r = p = buff; *p; p++) {
174*7c478bd9Sstevel@tonic-gate 		if (*p == '\n') {
175*7c478bd9Sstevel@tonic-gate 			*p++ = 0;
176*7c478bd9Sstevel@tonic-gate 		corout(r, ob, "deliv", dr, BUFSIZ);
177*7c478bd9Sstevel@tonic-gate 			nl = 1;
178*7c478bd9Sstevel@tonic-gate 			for (q = ob; *q; q++) {
179*7c478bd9Sstevel@tonic-gate 				if (nl && (q[0]=='.'||q[0]=='%') && q[1]=='T') {
180*7c478bd9Sstevel@tonic-gate 					q += 3;
181*7c478bd9Sstevel@tonic-gate 					for (t = q; *t && *t != '\n'; t++)
182*7c478bd9Sstevel@tonic-gate 						;
183*7c478bd9Sstevel@tonic-gate 				*t = 0;
184*7c478bd9Sstevel@tonic-gate 					fprintf(stderr, "%.70s\n", q);
185*7c478bd9Sstevel@tonic-gate 					q = 0;
186*7c478bd9Sstevel@tonic-gate 				break;
187*7c478bd9Sstevel@tonic-gate 			}
188*7c478bd9Sstevel@tonic-gate 				nl = *q == '\n';
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 			if (q)
191*7c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("??? at %s\n"),r);
192*7c478bd9Sstevel@tonic-gate 			r=p;
193*7c478bd9Sstevel@tonic-gate 		}
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate }
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate control(c)
198*7c478bd9Sstevel@tonic-gate {
199*7c478bd9Sstevel@tonic-gate 	if (c == '.')
200*7c478bd9Sstevel@tonic-gate 		return(1);
201*7c478bd9Sstevel@tonic-gate 	if (c == '%')
202*7c478bd9Sstevel@tonic-gate 		return(1);
203*7c478bd9Sstevel@tonic-gate 	return(0);
204*7c478bd9Sstevel@tonic-gate }
205