xref: /csrg-svn/usr.bin/tip/aculib/biz31.c (revision 5132)
1*5132Ssam /*	biz31.c	4.4	81/11/29	*/
23687Sroot #include "tip.h"
33687Sroot 
44408Ssam #if BIZ1031
53687Sroot #define MAXRETRY	3		/* sync up retry count */
63687Sroot #define DISCONNECT	"\21\25\11\24"	/* disconnection string */
73687Sroot 
83687Sroot static int sigALRM();
93687Sroot static int timeout = 0;
103687Sroot 
113687Sroot /*
124408Ssam  * Dial up on a BIZCOMP Model 1031 with either
133687Sroot  * 	tone dialing (mod = "f")
143687Sroot  *	pulse dialing (mod = "w")
153687Sroot  */
163687Sroot static int
173687Sroot biz_dialer(num, mod)
184962Ssam 	char *num, *mod;
193687Sroot {
203687Sroot 	register int connected = 0;
213687Sroot 
223687Sroot 	if (!bizsync(FD)) {
233687Sroot 		logent(value(HOST), "", "biz", "out of sync");
243687Sroot 		printf("bizcomp out of sync\n");
253687Sroot 		delock(uucplock);
263687Sroot 		exit(0);
273687Sroot 	}
283687Sroot 	if (boolean(value(VERBOSE)))
293687Sroot 		printf("\nstarting call...");
303687Sroot 	echo("#\rk$\r$\n");			/* disable auto-answer */
313687Sroot 	echo("$>$.$ #\r");			/* tone/pulse dialing */
323687Sroot 	echo(mod);
333687Sroot 	echo("$\r$\n");
343687Sroot 	echo("$>$.$ #\re$ ");			/* disconnection sequence */
353687Sroot 	echo(DISCONNECT);
363687Sroot 	echo("\r$\n$\r$\n");
373687Sroot 	echo("$>$.$ #\rr$ ");			/* repeat dial */
383687Sroot 	echo(num);
393687Sroot 	echo("\r$\n");
403687Sroot 	if (boolean(value(VERBOSE)))
413687Sroot 		printf("ringing...");
423687Sroot 	/*
433687Sroot 	 * The reply from the BIZCOMP should be:
443687Sroot 	 *	`^G NO CONNECTION\r\n^G\r\n'	failure
453687Sroot 	 *	` CONNECTION\r\n^G'		success
463687Sroot 	 */
473687Sroot 	connected = detect(" ");
483687Sroot #ifdef ACULOG
493687Sroot 	if (timeout) {
503687Sroot 		char line[80];
513687Sroot 
523687Sroot 		sprintf(line, "%d second dial timeout",
533687Sroot 			number(value(DIALTIMEOUT)));
543687Sroot 		logent(value(HOST), num, "biz", line);
553687Sroot 	}
563687Sroot #endif
573687Sroot 	if (!connected)
583687Sroot 		flush(" NO CONNECTION\r\n\07\r\n");
593687Sroot 	else
603687Sroot 		flush("CONNECTION\r\n\07");
613687Sroot 	if (timeout)
624408Ssam 		biz31_disconnect();	/* insurance */
63*5132Ssam 	return (connected);
643687Sroot }
653687Sroot 
664408Ssam biz31w_dialer(num, acu)
674962Ssam 	char *num, *acu;
683687Sroot {
69*5132Ssam 	return (biz_dialer(num, "w"));
703687Sroot }
713687Sroot 
724408Ssam biz31f_dialer(num, acu)
734962Ssam 	char *num, *acu;
743687Sroot {
75*5132Ssam 	return (biz_dialer(num, "f"));
763687Sroot }
773687Sroot 
784408Ssam biz31_disconnect()
793687Sroot {
803687Sroot 	write(FD, DISCONNECT, 4);
813687Sroot 	sleep(2);
823687Sroot 	ioctl(FD, TIOCFLUSH);
833687Sroot }
843687Sroot 
854408Ssam biz31_abort()
863687Sroot {
873687Sroot 	write(FD, "\33", 1);
883687Sroot 	timeout = 1;
893687Sroot }
903687Sroot 
913687Sroot static int
923687Sroot echo(s)
934962Ssam 	register char *s;
943687Sroot {
953687Sroot 	char c;
963687Sroot 
97*5132Ssam 	while (c = *s++) switch (c) {
98*5132Ssam 
99*5132Ssam 	case '$':
100*5132Ssam 		read(FD, &c, 1);
101*5132Ssam 		s++;
102*5132Ssam 		break;
103*5132Ssam 
104*5132Ssam 	case '#':
105*5132Ssam 		c = *s++;
106*5132Ssam 		write(FD, &c, 1);
107*5132Ssam 		break;
108*5132Ssam 
109*5132Ssam 	default:
110*5132Ssam 		write(FD, &c, 1);
111*5132Ssam 		read(FD, &c, 1);
112*5132Ssam 	}
1133687Sroot }
1143687Sroot 
1153687Sroot static int
1163687Sroot sigALRM()
1173687Sroot {
1183687Sroot 	signal(SIGALRM, SIG_IGN);
1193687Sroot 	printf("\07timeout waiting for reply\n");
1203687Sroot 	timeout = 1;
1213687Sroot }
1223687Sroot 
1233687Sroot static int
1243687Sroot detect(s)
1254962Ssam 	register char *s;
1263687Sroot {
1273687Sroot 	char c;
1283687Sroot 
1294408Ssam 	signal(SIGALRM, biz31_abort);
1303687Sroot 	timeout = 0;
131*5132Ssam 	while (*s) {
1323687Sroot 		alarm(number(value(DIALTIMEOUT)));
1333687Sroot 		read(FD, &c, 1);
1343687Sroot 		alarm(0);
1353687Sroot 		if (timeout)
136*5132Ssam 			return (0);
1373687Sroot 		if (c != *s++)
138*5132Ssam 			return (0);
1393687Sroot 	}
1403687Sroot 	signal(SIGALRM, SIG_DFL);
141*5132Ssam 	return (1);
1423687Sroot }
1433687Sroot 
1443687Sroot static int
1453687Sroot flush(s)
1464962Ssam 	register char *s;
1473687Sroot {
1483687Sroot 	char c;
1493687Sroot 
1503687Sroot 	signal(SIGALRM, sigALRM);
1513687Sroot 	timeout = 0;
152*5132Ssam 	while (*s++) {
1533687Sroot 		alarm(10);
1543687Sroot 		read(FD, &c, 1);
1553687Sroot 		alarm(0);
1563687Sroot 		if (timeout)
1573687Sroot 			break;
1583687Sroot 	}
1593687Sroot 	signal(SIGALRM, SIG_DFL);
1603687Sroot 	timeout = 0;			/* guard against disconnection */
161*5132Ssam 	return (1);
1623687Sroot }
1633687Sroot 
1643687Sroot /*
1653687Sroot  * This convoluted piece of code attempts to get
1663687Sroot  *  the bizcomp in sync.  If you don't have the capacity or nread
1673687Sroot  *  call there are gory ways to simulate this.
1683687Sroot  */
1693687Sroot static int
1703687Sroot bizsync(fd)
1713687Sroot {
1723687Sroot #ifdef FIOCAPACITY
1733687Sroot 	struct capacity b;
1743687Sroot #	define chars(b)	((b).cp_nbytes)
1753687Sroot #	define IOCTL	FIOCAPACITY
1763687Sroot #endif
1773687Sroot #ifdef FIONREAD
1783687Sroot 	long b;
1793687Sroot #	define chars(b)	(b)
1803687Sroot #	define IOCTL	FIONREAD
1813687Sroot #endif
1823687Sroot 	register int already = 0;
1833687Sroot 	char buf[10];
1843687Sroot 
1853687Sroot retry:
1863687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
1873687Sroot 		ioctl(fd, TIOCFLUSH);
1883687Sroot 	write(fd, "\rp>\r", 4);
1893687Sroot 	sleep(1);
1903687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
1913687Sroot 		if (chars(b) != 10) {
1923687Sroot 	nono:
1933687Sroot 			if (already > MAXRETRY)
194*5132Ssam 				return (0);
1953687Sroot 			write(fd, DISCONNECT, 4);
1963687Sroot 			sleep(2);
1973687Sroot 			already++;
1983687Sroot 			goto retry;
1993687Sroot 		} else {
2003687Sroot 			read(fd, buf, 10);
2013687Sroot 			if (strncmp(buf, "p >\r\n\r\n>", 8))
2023687Sroot 				goto nono;
2033687Sroot 		}
2043687Sroot 	}
205*5132Ssam 	return (1);
2063687Sroot }
2073687Sroot #endif
208