xref: /titanic_41/usr/src/cmd/refer/refer2.c (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include "refer..c"
187c478bd9Sstevel@tonic-gate #include <locale.h>
197c478bd9Sstevel@tonic-gate #define	NFLD 30
207c478bd9Sstevel@tonic-gate #define	TLEN 512
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate extern FILE *in;
237c478bd9Sstevel@tonic-gate char one[ANSLEN];
247c478bd9Sstevel@tonic-gate int onelen = ANSLEN;
257c478bd9Sstevel@tonic-gate static char dr [100] = "";
267c478bd9Sstevel@tonic-gate 
27*11a8fa6cSceastha extern int prefix();
28*11a8fa6cSceastha extern int common();
29*11a8fa6cSceastha extern void dumpold();
30*11a8fa6cSceastha extern void putref();
31*11a8fa6cSceastha extern void flout();
32*11a8fa6cSceastha extern int tabs();
33*11a8fa6cSceastha extern int chkdup();
34*11a8fa6cSceastha extern void putsig();
35*11a8fa6cSceastha extern void putkey();
36*11a8fa6cSceastha extern void err();
37*11a8fa6cSceastha extern int corout();
38*11a8fa6cSceastha 
39*11a8fa6cSceastha void choices(char *);
40*11a8fa6cSceastha int control(char);
41*11a8fa6cSceastha static int newline(char *);
42*11a8fa6cSceastha 
43*11a8fa6cSceastha void
doref(char * line1)44*11a8fa6cSceastha doref(char *line1)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	char buff[QLEN], dbuff[3*QLEN];
477c478bd9Sstevel@tonic-gate 	char answer[ANSLEN], temp[TLEN], line[BUFSIZ];
487c478bd9Sstevel@tonic-gate 	char *p, **sr, *flds[NFLD], *r;
497c478bd9Sstevel@tonic-gate 	int stat, nf, nr, query = 0, alph, digs;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate again:
527c478bd9Sstevel@tonic-gate 	buff[0] = dbuff[0] = NULL;
537c478bd9Sstevel@tonic-gate 	if (biblio && Iline == 1 && line1[0] == '%')
547c478bd9Sstevel@tonic-gate 		strcat(dbuff, line1);
557c478bd9Sstevel@tonic-gate 	while (input(line)) {		/* get query */
567c478bd9Sstevel@tonic-gate 		Iline++;
577c478bd9Sstevel@tonic-gate 		if (prefix(".]", line))
587c478bd9Sstevel@tonic-gate 			break;
597c478bd9Sstevel@tonic-gate 		if (biblio && line[0] == '\n')
607c478bd9Sstevel@tonic-gate 			break;
617c478bd9Sstevel@tonic-gate 		if (biblio && line[0] == '%' && line[1] == *convert)
627c478bd9Sstevel@tonic-gate 			break;
637c478bd9Sstevel@tonic-gate 		if (control(line[0]))
647c478bd9Sstevel@tonic-gate 			query = 1;
657c478bd9Sstevel@tonic-gate 		strcat(query ? dbuff : buff, line);
667c478bd9Sstevel@tonic-gate 		if (strlen(buff) > QLEN)
677c478bd9Sstevel@tonic-gate 			err(gettext("query too long (%d)"), strlen(buff));
687c478bd9Sstevel@tonic-gate 		if (strlen(dbuff) > 3 * QLEN)
697c478bd9Sstevel@tonic-gate 			err(gettext("record at line %d too long"), Iline-1);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 	if (biblio && line[0] == '\n' && feof(in))
727c478bd9Sstevel@tonic-gate 		return;
737c478bd9Sstevel@tonic-gate 	if (strcmp(buff, "$LIST$\n") == 0) {
747c478bd9Sstevel@tonic-gate 		assert(dbuff[0] == 0);
757c478bd9Sstevel@tonic-gate 		dumpold();
767c478bd9Sstevel@tonic-gate 		return;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 	answer[0] = 0;
797c478bd9Sstevel@tonic-gate 	for (p = buff; *p; p++) {
807c478bd9Sstevel@tonic-gate 		if (isupper(*p))
817c478bd9Sstevel@tonic-gate 			*p |= 040;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 	alph = digs = 0;
847c478bd9Sstevel@tonic-gate 	for (p = buff; *p; p++) {
857c478bd9Sstevel@tonic-gate 		if (isalpha(*p))
867c478bd9Sstevel@tonic-gate 			alph++;
877c478bd9Sstevel@tonic-gate 		else
887c478bd9Sstevel@tonic-gate 			if (isdigit(*p))
897c478bd9Sstevel@tonic-gate 				digs++;
907c478bd9Sstevel@tonic-gate 			else {
917c478bd9Sstevel@tonic-gate 				*p = 0;
927c478bd9Sstevel@tonic-gate 				if ((alph+digs < 3) || common(p-alph)) {
937c478bd9Sstevel@tonic-gate 					r = p-alph;
947c478bd9Sstevel@tonic-gate 					while (r < p)
957c478bd9Sstevel@tonic-gate 						*r++ = ' ';
967c478bd9Sstevel@tonic-gate 				}
977c478bd9Sstevel@tonic-gate 				if (alph == 0 && digs > 0) {
987c478bd9Sstevel@tonic-gate 					r = p-digs;
997c478bd9Sstevel@tonic-gate 					if (digs != 4 || atoi(r)/100 != 19) {
1007c478bd9Sstevel@tonic-gate 						while (r < p)
1017c478bd9Sstevel@tonic-gate 							*r++ = ' ';
1027c478bd9Sstevel@tonic-gate 					}
1037c478bd9Sstevel@tonic-gate 				}
1047c478bd9Sstevel@tonic-gate 				*p = ' ';
1057c478bd9Sstevel@tonic-gate 				alph = digs = 0;
1067c478bd9Sstevel@tonic-gate 			}
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	one[0] = 0;
1097c478bd9Sstevel@tonic-gate 	if (buff[0]) {	/* do not search if no query */
1107c478bd9Sstevel@tonic-gate 		for (sr = rdata; sr < search; sr++) {
1117c478bd9Sstevel@tonic-gate 			temp[0] = 0;
1127c478bd9Sstevel@tonic-gate 			corout(buff, temp, "hunt", *sr, TLEN);
1137c478bd9Sstevel@tonic-gate 			assert(strlen(temp) < TLEN);
1147c478bd9Sstevel@tonic-gate 			if (strlen(temp)+strlen(answer) > BUFSIZ)
115*11a8fa6cSceastha 				err(gettext(
116*11a8fa6cSceastha 				    "Accumulated answers too large"), 0);
1177c478bd9Sstevel@tonic-gate 			strcat(answer, temp);
1187c478bd9Sstevel@tonic-gate 			if (strlen(answer) > BUFSIZ)
119*11a8fa6cSceastha 				err(gettext("answer too long (%d)"),
120*11a8fa6cSceastha 				    strlen(answer));
1217c478bd9Sstevel@tonic-gate 			if (newline(answer) > 0)
1227c478bd9Sstevel@tonic-gate 				break;
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	assert(strlen(one) < ANSLEN);
1267c478bd9Sstevel@tonic-gate 	assert(strlen(answer) < ANSLEN);
1277c478bd9Sstevel@tonic-gate 	if (buff[0])
1287c478bd9Sstevel@tonic-gate 		switch (newline(answer)) {
1297c478bd9Sstevel@tonic-gate 		case 0:
1307c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("No such paper: %s\n"), buff);
1317c478bd9Sstevel@tonic-gate 			return;
1327c478bd9Sstevel@tonic-gate 		default:
133*11a8fa6cSceastha 			fprintf(stderr, gettext(
134*11a8fa6cSceastha 			    "Too many hits: %s\n"), trimnl(buff));
1357c478bd9Sstevel@tonic-gate 			choices(answer);
1367c478bd9Sstevel@tonic-gate 			p = buff;
1377c478bd9Sstevel@tonic-gate 			while (*p != '\n')
1387c478bd9Sstevel@tonic-gate 				p++;
1397c478bd9Sstevel@tonic-gate 			*++p = 0;
1407c478bd9Sstevel@tonic-gate 		case 1:
1417c478bd9Sstevel@tonic-gate 			if (endpush)
1427c478bd9Sstevel@tonic-gate 				if (nr = chkdup(answer)) {
1437c478bd9Sstevel@tonic-gate 					if (bare < 2) {
1447c478bd9Sstevel@tonic-gate 						nf = tabs(flds, one);
1457c478bd9Sstevel@tonic-gate 						nf += tabs(flds+nf, dbuff);
1467c478bd9Sstevel@tonic-gate 						assert(nf < NFLD);
147*11a8fa6cSceastha 						putsig(nf, flds, nr, line1,
148*11a8fa6cSceastha 						    line, 0);
1497c478bd9Sstevel@tonic-gate 					}
1507c478bd9Sstevel@tonic-gate 					return;
1517c478bd9Sstevel@tonic-gate 				}
1527c478bd9Sstevel@tonic-gate 			if (one[0] == 0)
1537c478bd9Sstevel@tonic-gate 				corout(answer, one, "deliv", dr, QLEN);
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	assert(strlen(buff) < QLEN);
1577c478bd9Sstevel@tonic-gate 	assert(strlen(one) < ANSLEN);
1587c478bd9Sstevel@tonic-gate 	nf = tabs(flds, one);
1597c478bd9Sstevel@tonic-gate 	nf += tabs(flds+nf, dbuff);
1607c478bd9Sstevel@tonic-gate 	assert(nf < NFLD);
1617c478bd9Sstevel@tonic-gate 	refnum++;
1627c478bd9Sstevel@tonic-gate 	if (sort)
1637c478bd9Sstevel@tonic-gate 		putkey(nf, flds, refnum, keystr);
1647c478bd9Sstevel@tonic-gate 	if (bare < 2)
1657c478bd9Sstevel@tonic-gate 		putsig(nf, flds, refnum, line1, line, 1);
1667c478bd9Sstevel@tonic-gate 	else
1677c478bd9Sstevel@tonic-gate 		flout();
1687c478bd9Sstevel@tonic-gate 	putref(nf, flds);
1697c478bd9Sstevel@tonic-gate 	if (biblio && line[0] == '\n')
1707c478bd9Sstevel@tonic-gate 		goto again;
1717c478bd9Sstevel@tonic-gate 	if (biblio && line[0] == '%' && line[1] == *convert)
1727c478bd9Sstevel@tonic-gate 		fprintf(fo, "%s%c%s", convert+1, sep, line+3);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
175*11a8fa6cSceastha static int
newline(char * s)176*11a8fa6cSceastha newline(char *s)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	int k = 0, c;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	while (c = *s++)
1817c478bd9Sstevel@tonic-gate 		if (c == '\n')
1827c478bd9Sstevel@tonic-gate 		k++;
1837c478bd9Sstevel@tonic-gate 	return (k);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
186*11a8fa6cSceastha void
choices(char * buff)187*11a8fa6cSceastha choices(char *buff)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	char ob[BUFSIZ], *p, *r, *q, *t;
1907c478bd9Sstevel@tonic-gate 	int nl;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	for (r = p = buff; *p; p++) {
1937c478bd9Sstevel@tonic-gate 		if (*p == '\n') {
1947c478bd9Sstevel@tonic-gate 			*p++ = 0;
1957c478bd9Sstevel@tonic-gate 		corout(r, ob, "deliv", dr, BUFSIZ);
1967c478bd9Sstevel@tonic-gate 			nl = 1;
1977c478bd9Sstevel@tonic-gate 			for (q = ob; *q; q++) {
198*11a8fa6cSceastha 				if (nl && (q[0] == '.' || q[0] == '%') &&
199*11a8fa6cSceastha 				    q[1] == 'T') {
2007c478bd9Sstevel@tonic-gate 					q += 3;
2017c478bd9Sstevel@tonic-gate 					for (t = q; *t && *t != '\n'; t++)
2027c478bd9Sstevel@tonic-gate 						;
2037c478bd9Sstevel@tonic-gate 					*t = 0;
2047c478bd9Sstevel@tonic-gate 					fprintf(stderr, "%.70s\n", q);
2057c478bd9Sstevel@tonic-gate 					q = 0;
2067c478bd9Sstevel@tonic-gate 					break;
2077c478bd9Sstevel@tonic-gate 				}
2087c478bd9Sstevel@tonic-gate 				nl = *q == '\n';
2097c478bd9Sstevel@tonic-gate 			}
2107c478bd9Sstevel@tonic-gate 			if (q)
2117c478bd9Sstevel@tonic-gate 				fprintf(stderr, gettext("??? at %s\n"), r);
2127c478bd9Sstevel@tonic-gate 			r = p;
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
217*11a8fa6cSceastha int
control(char c)218*11a8fa6cSceastha control(char c)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	if (c == '.')
2217c478bd9Sstevel@tonic-gate 		return (1);
2227c478bd9Sstevel@tonic-gate 	if (c == '%')
2237c478bd9Sstevel@tonic-gate 		return (1);
2247c478bd9Sstevel@tonic-gate 	return (0);
2257c478bd9Sstevel@tonic-gate }
226