xref: /csrg-svn/usr.bin/tip/aculib/biz31.c (revision 4962)
1*4962Ssam /*	biz31.c	4.3	81/11/20	*/
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)
18*4962Ssam 	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 */
633687Sroot 	return(connected);
643687Sroot }
653687Sroot 
664408Ssam biz31w_dialer(num, acu)
67*4962Ssam 	char *num, *acu;
683687Sroot {
693687Sroot 	return(biz_dialer(num, "w"));
703687Sroot }
713687Sroot 
724408Ssam biz31f_dialer(num, acu)
73*4962Ssam 	char *num, *acu;
743687Sroot {
753687Sroot 	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)
93*4962Ssam 	register char *s;
943687Sroot {
953687Sroot 	char c;
963687Sroot 
973687Sroot 	while (c = *s++)
983687Sroot 		switch(c)
993687Sroot 		{
1003687Sroot 			case '$':
1013687Sroot 				read(FD, &c, 1);
1023687Sroot 				s++;
1033687Sroot 				break;
1043687Sroot 			case '#':
1053687Sroot 				c = *s++;
1063687Sroot 				write(FD, &c, 1);
1073687Sroot 				break;
1083687Sroot 			default:
1093687Sroot 				write(FD, &c, 1);
1103687Sroot 				read(FD, &c, 1);
1113687Sroot 		}
1123687Sroot }
1133687Sroot 
1143687Sroot static int
1153687Sroot sigALRM()
1163687Sroot {
1173687Sroot 	signal(SIGALRM, SIG_IGN);
1183687Sroot 	printf("\07timeout waiting for reply\n");
1193687Sroot 	timeout = 1;
1203687Sroot }
1213687Sroot 
1223687Sroot static int
1233687Sroot detect(s)
124*4962Ssam 	register char *s;
1253687Sroot {
1263687Sroot 	char c;
1273687Sroot 
1284408Ssam 	signal(SIGALRM, biz31_abort);
1293687Sroot 	timeout = 0;
1303687Sroot 	while (*s)
1313687Sroot 	{
1323687Sroot 		alarm(number(value(DIALTIMEOUT)));
1333687Sroot 		read(FD, &c, 1);
1343687Sroot 		alarm(0);
1353687Sroot 		if (timeout)
1363687Sroot 			return(0);
1373687Sroot 		if (c != *s++)
1383687Sroot 			return(0);
1393687Sroot 	}
1403687Sroot 	signal(SIGALRM, SIG_DFL);
1413687Sroot 	return(1);
1423687Sroot }
1433687Sroot 
1443687Sroot static int
1453687Sroot flush(s)
146*4962Ssam 	register char *s;
1473687Sroot {
1483687Sroot 	char c;
1493687Sroot 
1503687Sroot 	signal(SIGALRM, sigALRM);
1513687Sroot 	timeout = 0;
1523687Sroot 	while (*s++)
1533687Sroot 	{
1543687Sroot 		alarm(10);
1553687Sroot 		read(FD, &c, 1);
1563687Sroot 		alarm(0);
1573687Sroot 		if (timeout)
1583687Sroot 			break;
1593687Sroot 	}
1603687Sroot 	signal(SIGALRM, SIG_DFL);
1613687Sroot 	timeout = 0;			/* guard against disconnection */
1623687Sroot 	return(1);
1633687Sroot }
1643687Sroot 
1653687Sroot /*
1663687Sroot  * This convoluted piece of code attempts to get
1673687Sroot  *  the bizcomp in sync.  If you don't have the capacity or nread
1683687Sroot  *  call there are gory ways to simulate this.
1693687Sroot  */
1703687Sroot static int
1713687Sroot bizsync(fd)
1723687Sroot {
1733687Sroot #ifdef FIOCAPACITY
1743687Sroot 	struct capacity b;
1753687Sroot #	define chars(b)	((b).cp_nbytes)
1763687Sroot #	define IOCTL	FIOCAPACITY
1773687Sroot #endif
1783687Sroot #ifdef FIONREAD
1793687Sroot 	long b;
1803687Sroot #	define chars(b)	(b)
1813687Sroot #	define IOCTL	FIONREAD
1823687Sroot #endif
1833687Sroot 	register int already = 0;
1843687Sroot 	char buf[10];
1853687Sroot 
1863687Sroot retry:
1873687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
1883687Sroot 		ioctl(fd, TIOCFLUSH);
1893687Sroot 	write(fd, "\rp>\r", 4);
1903687Sroot 	sleep(1);
1913687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
1923687Sroot 		if (chars(b) != 10) {
1933687Sroot 	nono:
1943687Sroot 			if (already > MAXRETRY)
1953687Sroot 				return(0);
1963687Sroot 			write(fd, DISCONNECT, 4);
1973687Sroot 			sleep(2);
1983687Sroot 			already++;
1993687Sroot 			goto retry;
2003687Sroot 		} else {
2013687Sroot 			read(fd, buf, 10);
2023687Sroot 			if (strncmp(buf, "p >\r\n\r\n>", 8))
2033687Sroot 				goto nono;
2043687Sroot 		}
2053687Sroot 	}
2063687Sroot 	return(1);
2073687Sroot }
2083687Sroot #endif
209