xref: /csrg-svn/usr.bin/tip/aculib/biz31.c (revision 13277)
1*13277Ssam #ifndef lint
2*13277Ssam static char sccsid[] = "@(#)biz31.c	4.6 (Berkeley) 06/25/83";
3*13277Ssam #endif
4*13277Ssam 
53687Sroot #include "tip.h"
63687Sroot 
74408Ssam #if BIZ1031
83687Sroot #define MAXRETRY	3		/* sync up retry count */
9*13277Ssam #define DISCONNECT_CMD	"\21\25\11\24"	/* disconnection string */
103687Sroot 
11*13277Ssam static	int sigALRM();
12*13277Ssam static	int timeout = 0;
13*13277Ssam static	jmp_buf timeoutbuf;
143687Sroot 
153687Sroot /*
164408Ssam  * Dial up on a BIZCOMP Model 1031 with either
173687Sroot  * 	tone dialing (mod = "f")
183687Sroot  *	pulse dialing (mod = "w")
193687Sroot  */
203687Sroot static int
213687Sroot biz_dialer(num, mod)
224962Ssam 	char *num, *mod;
233687Sroot {
243687Sroot 	register int connected = 0;
253687Sroot 
263687Sroot 	if (!bizsync(FD)) {
273687Sroot 		logent(value(HOST), "", "biz", "out of sync");
283687Sroot 		printf("bizcomp out of sync\n");
293687Sroot 		delock(uucplock);
303687Sroot 		exit(0);
313687Sroot 	}
323687Sroot 	if (boolean(value(VERBOSE)))
333687Sroot 		printf("\nstarting call...");
343687Sroot 	echo("#\rk$\r$\n");			/* disable auto-answer */
353687Sroot 	echo("$>$.$ #\r");			/* tone/pulse dialing */
363687Sroot 	echo(mod);
373687Sroot 	echo("$\r$\n");
383687Sroot 	echo("$>$.$ #\re$ ");			/* disconnection sequence */
39*13277Ssam 	echo(DISCONNECT_CMD);
403687Sroot 	echo("\r$\n$\r$\n");
413687Sroot 	echo("$>$.$ #\rr$ ");			/* repeat dial */
423687Sroot 	echo(num);
433687Sroot 	echo("\r$\n");
443687Sroot 	if (boolean(value(VERBOSE)))
453687Sroot 		printf("ringing...");
463687Sroot 	/*
473687Sroot 	 * The reply from the BIZCOMP should be:
483687Sroot 	 *	`^G NO CONNECTION\r\n^G\r\n'	failure
493687Sroot 	 *	` CONNECTION\r\n^G'		success
503687Sroot 	 */
513687Sroot 	connected = detect(" ");
523687Sroot #ifdef ACULOG
533687Sroot 	if (timeout) {
543687Sroot 		char line[80];
553687Sroot 
563687Sroot 		sprintf(line, "%d second dial timeout",
573687Sroot 			number(value(DIALTIMEOUT)));
583687Sroot 		logent(value(HOST), num, "biz", line);
593687Sroot 	}
603687Sroot #endif
613687Sroot 	if (!connected)
623687Sroot 		flush(" NO CONNECTION\r\n\07\r\n");
633687Sroot 	else
643687Sroot 		flush("CONNECTION\r\n\07");
653687Sroot 	if (timeout)
664408Ssam 		biz31_disconnect();	/* insurance */
675132Ssam 	return (connected);
683687Sroot }
693687Sroot 
704408Ssam biz31w_dialer(num, acu)
714962Ssam 	char *num, *acu;
723687Sroot {
73*13277Ssam 
745132Ssam 	return (biz_dialer(num, "w"));
753687Sroot }
763687Sroot 
774408Ssam biz31f_dialer(num, acu)
784962Ssam 	char *num, *acu;
793687Sroot {
80*13277Ssam 
815132Ssam 	return (biz_dialer(num, "f"));
823687Sroot }
833687Sroot 
844408Ssam biz31_disconnect()
853687Sroot {
86*13277Ssam 
87*13277Ssam 	write(FD, DISCONNECT_CMD, 4);
883687Sroot 	sleep(2);
893687Sroot 	ioctl(FD, TIOCFLUSH);
903687Sroot }
913687Sroot 
924408Ssam biz31_abort()
933687Sroot {
94*13277Ssam 
953687Sroot 	write(FD, "\33", 1);
963687Sroot }
973687Sroot 
983687Sroot static int
993687Sroot echo(s)
1004962Ssam 	register char *s;
1013687Sroot {
1023687Sroot 	char c;
1033687Sroot 
1045132Ssam 	while (c = *s++) switch (c) {
1055132Ssam 
1065132Ssam 	case '$':
1075132Ssam 		read(FD, &c, 1);
1085132Ssam 		s++;
1095132Ssam 		break;
1105132Ssam 
1115132Ssam 	case '#':
1125132Ssam 		c = *s++;
1135132Ssam 		write(FD, &c, 1);
1145132Ssam 		break;
1155132Ssam 
1165132Ssam 	default:
1175132Ssam 		write(FD, &c, 1);
1185132Ssam 		read(FD, &c, 1);
1195132Ssam 	}
1203687Sroot }
1213687Sroot 
1223687Sroot static int
1233687Sroot sigALRM()
1243687Sroot {
125*13277Ssam 
1263687Sroot 	timeout = 1;
127*13277Ssam 	longjmp(timeoutbuf, 1);
1283687Sroot }
1293687Sroot 
1303687Sroot static int
1313687Sroot detect(s)
1324962Ssam 	register char *s;
1333687Sroot {
1343687Sroot 	char c;
135*13277Ssam 	int (*f)();
1363687Sroot 
137*13277Ssam 	f = signal(SIGALRM, sigALRM);
1383687Sroot 	timeout = 0;
1395132Ssam 	while (*s) {
140*13277Ssam 		if (setjmp(timeoutbuf)) {
141*13277Ssam 			printf("\07timeout waiting for reply\n");
142*13277Ssam 			biz31_abort();
143*13277Ssam 			break;
144*13277Ssam 		}
1453687Sroot 		alarm(number(value(DIALTIMEOUT)));
1463687Sroot 		read(FD, &c, 1);
1473687Sroot 		alarm(0);
1483687Sroot 		if (c != *s++)
149*13277Ssam 			break;
1503687Sroot 	}
151*13277Ssam 	signal(SIGALRM, f);
152*13277Ssam 	return (timeout == 0);
1533687Sroot }
1543687Sroot 
1553687Sroot static int
1563687Sroot flush(s)
1574962Ssam 	register char *s;
1583687Sroot {
1593687Sroot 	char c;
160*13277Ssam 	int (*f)();
1613687Sroot 
162*13277Ssam 	f = signal(SIGALRM, sigALRM);
1635132Ssam 	while (*s++) {
164*13277Ssam 		if (setjmp(timeoutbuf))
165*13277Ssam 			break;
1663687Sroot 		alarm(10);
1673687Sroot 		read(FD, &c, 1);
1683687Sroot 		alarm(0);
1693687Sroot 	}
170*13277Ssam 	signal(SIGALRM, f);
1713687Sroot 	timeout = 0;			/* guard against disconnection */
1723687Sroot }
1733687Sroot 
1743687Sroot /*
1753687Sroot  * This convoluted piece of code attempts to get
1763687Sroot  *  the bizcomp in sync.  If you don't have the capacity or nread
1773687Sroot  *  call there are gory ways to simulate this.
1783687Sroot  */
1793687Sroot static int
1803687Sroot bizsync(fd)
1813687Sroot {
1823687Sroot #ifdef FIOCAPACITY
1833687Sroot 	struct capacity b;
1843687Sroot #	define chars(b)	((b).cp_nbytes)
1853687Sroot #	define IOCTL	FIOCAPACITY
1863687Sroot #endif
1873687Sroot #ifdef FIONREAD
1883687Sroot 	long b;
1893687Sroot #	define chars(b)	(b)
1903687Sroot #	define IOCTL	FIONREAD
1913687Sroot #endif
1923687Sroot 	register int already = 0;
1933687Sroot 	char buf[10];
1943687Sroot 
1953687Sroot retry:
1963687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
1973687Sroot 		ioctl(fd, TIOCFLUSH);
1983687Sroot 	write(fd, "\rp>\r", 4);
1993687Sroot 	sleep(1);
2003687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
2013687Sroot 		if (chars(b) != 10) {
2023687Sroot 	nono:
2033687Sroot 			if (already > MAXRETRY)
2045132Ssam 				return (0);
205*13277Ssam 			write(fd, DISCONNECT_CMD, 4);
2063687Sroot 			sleep(2);
2073687Sroot 			already++;
2083687Sroot 			goto retry;
2093687Sroot 		} else {
2103687Sroot 			read(fd, buf, 10);
2113687Sroot 			if (strncmp(buf, "p >\r\n\r\n>", 8))
2123687Sroot 				goto nono;
2133687Sroot 		}
2143687Sroot 	}
2155132Ssam 	return (1);
2163687Sroot }
2173687Sroot #endif
218