xref: /illumos-gate/usr/src/cmd/refer/refer2.c (revision 7bc3049f)
111a8fa6cSceastha /*
211a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
311a8fa6cSceastha  * Use is subject to license terms.
411a8fa6cSceastha  */
511a8fa6cSceastha 
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 #include "refer..c"
167c478bd9Sstevel@tonic-gate #include <locale.h>
177c478bd9Sstevel@tonic-gate #define	NFLD 30
187c478bd9Sstevel@tonic-gate #define	TLEN 512
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate extern FILE *in;
217c478bd9Sstevel@tonic-gate char one[ANSLEN];
227c478bd9Sstevel@tonic-gate int onelen = ANSLEN;
237c478bd9Sstevel@tonic-gate static char dr [100] = "";
247c478bd9Sstevel@tonic-gate 
2511a8fa6cSceastha extern int prefix();
2611a8fa6cSceastha extern int common();
2711a8fa6cSceastha extern void dumpold();
2811a8fa6cSceastha extern void putref();
2911a8fa6cSceastha extern void flout();
3011a8fa6cSceastha extern int tabs();
3111a8fa6cSceastha extern int chkdup();
3211a8fa6cSceastha extern void putsig();
3311a8fa6cSceastha extern void putkey();
3411a8fa6cSceastha extern void err();
3511a8fa6cSceastha extern int corout();
3611a8fa6cSceastha 
3711a8fa6cSceastha void choices(char *);
3811a8fa6cSceastha int control(char);
3911a8fa6cSceastha static int newline(char *);
4011a8fa6cSceastha 
4111a8fa6cSceastha void
doref(char * line1)4211a8fa6cSceastha doref(char *line1)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	char buff[QLEN], dbuff[3*QLEN];
457c478bd9Sstevel@tonic-gate 	char answer[ANSLEN], temp[TLEN], line[BUFSIZ];
467c478bd9Sstevel@tonic-gate 	char *p, **sr, *flds[NFLD], *r;
477c478bd9Sstevel@tonic-gate 	int stat, nf, nr, query = 0, alph, digs;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate again:
50*7bc3049fSToomas Soome 	buff[0] = dbuff[0] = '\0';
517c478bd9Sstevel@tonic-gate 	if (biblio && Iline == 1 && line1[0] == '%')
527c478bd9Sstevel@tonic-gate 		strcat(dbuff, line1);
537c478bd9Sstevel@tonic-gate 	while (input(line)) {		/* get query */
547c478bd9Sstevel@tonic-gate 		Iline++;
557c478bd9Sstevel@tonic-gate 		if (prefix(".]", line))
567c478bd9Sstevel@tonic-gate 			break;
577c478bd9Sstevel@tonic-gate 		if (biblio && line[0] == '\n')
587c478bd9Sstevel@tonic-gate 			break;
597c478bd9Sstevel@tonic-gate 		if (biblio && line[0] == '%' && line[1] == *convert)
607c478bd9Sstevel@tonic-gate 			break;
617c478bd9Sstevel@tonic-gate 		if (control(line[0]))
627c478bd9Sstevel@tonic-gate 			query = 1;
637c478bd9Sstevel@tonic-gate 		strcat(query ? dbuff : buff, line);
647c478bd9Sstevel@tonic-gate 		if (strlen(buff) > QLEN)
657c478bd9Sstevel@tonic-gate 			err(gettext("query too long (%d)"), strlen(buff));
667c478bd9Sstevel@tonic-gate 		if (strlen(dbuff) > 3 * QLEN)
677c478bd9Sstevel@tonic-gate 			err(gettext("record at line %d too long"), Iline-1);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 	if (biblio && line[0] == '\n' && feof(in))
707c478bd9Sstevel@tonic-gate 		return;
717c478bd9Sstevel@tonic-gate 	if (strcmp(buff, "$LIST$\n") == 0) {
727c478bd9Sstevel@tonic-gate 		assert(dbuff[0] == 0);
737c478bd9Sstevel@tonic-gate 		dumpold();
747c478bd9Sstevel@tonic-gate 		return;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	answer[0] = 0;
777c478bd9Sstevel@tonic-gate 	for (p = buff; *p; p++) {
787c478bd9Sstevel@tonic-gate 		if (isupper(*p))
797c478bd9Sstevel@tonic-gate 			*p |= 040;
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	alph = digs = 0;
827c478bd9Sstevel@tonic-gate 	for (p = buff; *p; p++) {
837c478bd9Sstevel@tonic-gate 		if (isalpha(*p))
847c478bd9Sstevel@tonic-gate 			alph++;
857c478bd9Sstevel@tonic-gate 		else
867c478bd9Sstevel@tonic-gate 			if (isdigit(*p))
877c478bd9Sstevel@tonic-gate 				digs++;
887c478bd9Sstevel@tonic-gate 			else {
897c478bd9Sstevel@tonic-gate 				*p = 0;
907c478bd9Sstevel@tonic-gate 				if ((alph+digs < 3) || common(p-alph)) {
917c478bd9Sstevel@tonic-gate 					r = p-alph;
927c478bd9Sstevel@tonic-gate 					while (r < p)
937c478bd9Sstevel@tonic-gate 						*r++ = ' ';
947c478bd9Sstevel@tonic-gate 				}
957c478bd9Sstevel@tonic-gate 				if (alph == 0 && digs > 0) {
967c478bd9Sstevel@tonic-gate 					r = p-digs;
977c478bd9Sstevel@tonic-gate 					if (digs != 4 || atoi(r)/100 != 19) {
987c478bd9Sstevel@tonic-gate 						while (r < p)
997c478bd9Sstevel@tonic-gate 							*r++ = ' ';
1007c478bd9Sstevel@tonic-gate 					}
1017c478bd9Sstevel@tonic-gate 				}
1027c478bd9Sstevel@tonic-gate 				*p = ' ';
1037c478bd9Sstevel@tonic-gate 				alph = digs = 0;
1047c478bd9Sstevel@tonic-gate 			}
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	one[0] = 0;
1077c478bd9Sstevel@tonic-gate 	if (buff[0]) {	/* do not search if no query */
1087c478bd9Sstevel@tonic-gate 		for (sr = rdata; sr < search; sr++) {
1097c478bd9Sstevel@tonic-gate 			temp[0] = 0;
1107c478bd9Sstevel@tonic-gate 			corout(buff, temp, "hunt", *sr, TLEN);
1117c478bd9Sstevel@tonic-gate 			assert(strlen(temp) < TLEN);
1127c478bd9Sstevel@tonic-gate 			if (strlen(temp)+strlen(answer) > BUFSIZ)
11311a8fa6cSceastha 				err(gettext(
11411a8fa6cSceastha 				    "Accumulated answers too large"), 0);
1157c478bd9Sstevel@tonic-gate 			strcat(answer, temp);
1167c478bd9Sstevel@tonic-gate 			if (strlen(answer) > BUFSIZ)
11711a8fa6cSceastha 				err(gettext("answer too long (%d)"),
11811a8fa6cSceastha 				    strlen(answer));
1197c478bd9Sstevel@tonic-gate 			if (newline(answer) > 0)
1207c478bd9Sstevel@tonic-gate 				break;
1217c478bd9Sstevel@tonic-gate 		}
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	assert(strlen(one) < ANSLEN);
1247c478bd9Sstevel@tonic-gate 	assert(strlen(answer) < ANSLEN);
1257c478bd9Sstevel@tonic-gate 	if (buff[0])
1267c478bd9Sstevel@tonic-gate 		switch (newline(answer)) {
1277c478bd9Sstevel@tonic-gate 		case 0:
1287c478bd9Sstevel@tonic-gate 			fprintf(stderr, gettext("No such paper: %s\n"), buff);
1297c478bd9Sstevel@tonic-gate 			return;
1307c478bd9Sstevel@tonic-gate 		default:
13111a8fa6cSceastha 			fprintf(stderr, gettext(
13211a8fa6cSceastha 			    "Too many hits: %s\n"), trimnl(buff));
1337c478bd9Sstevel@tonic-gate 			choices(answer);
1347c478bd9Sstevel@tonic-gate 			p = buff;
1357c478bd9Sstevel@tonic-gate 			while (*p != '\n')
1367c478bd9Sstevel@tonic-gate 				p++;
1377c478bd9Sstevel@tonic-gate 			*++p = 0;
1380d7511a1SToomas Soome 			/* FALLTHROUGH */
1390d7511a1SToomas Soome 
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);
14711a8fa6cSceastha 						putsig(nf, flds, nr, line1,
14811a8fa6cSceastha 						    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 
17511a8fa6cSceastha static int
newline(char * s)17611a8fa6cSceastha 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 
18611a8fa6cSceastha void
choices(char * buff)18711a8fa6cSceastha 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++) {
19811a8fa6cSceastha 				if (nl && (q[0] == '.' || q[0] == '%') &&
19911a8fa6cSceastha 				    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 
21711a8fa6cSceastha int
control(char c)21811a8fa6cSceastha 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