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