xref: /csrg-svn/usr.bin/tip/aculib/biz31.c (revision 39253)
122415Sdist /*
235492Sbostic  * Copyright (c) 1983 The Regents of the University of California.
335492Sbostic  * All rights reserved.
435492Sbostic  *
535492Sbostic  * Redistribution and use in source and binary forms are permitted
635492Sbostic  * provided that the above copyright notice and this paragraph are
735492Sbostic  * duplicated in all such forms and that any documentation,
835492Sbostic  * advertising materials, and other materials related to such
935492Sbostic  * distribution and use acknowledge that the software was developed
1035492Sbostic  * by the University of California, Berkeley.  The name of the
1135492Sbostic  * University may not be used to endorse or promote products derived
1235492Sbostic  * from this software without specific prior written permission.
1335492Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435492Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535492Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622415Sdist  */
1722415Sdist 
1813277Ssam #ifndef lint
19*39253Sbostic static char sccsid[] = "@(#)biz31.c	5.3 (Berkeley) 10/03/89";
2035492Sbostic #endif /* not lint */
2113277Ssam 
223687Sroot #include "tip.h"
233687Sroot 
243687Sroot #define MAXRETRY	3		/* sync up retry count */
2513277Ssam #define DISCONNECT_CMD	"\21\25\11\24"	/* disconnection string */
263687Sroot 
27*39253Sbostic static	void sigALRM();
2813277Ssam static	int timeout = 0;
2913277Ssam static	jmp_buf timeoutbuf;
303687Sroot 
313687Sroot /*
324408Ssam  * Dial up on a BIZCOMP Model 1031 with either
333687Sroot  * 	tone dialing (mod = "f")
343687Sroot  *	pulse dialing (mod = "w")
353687Sroot  */
363687Sroot static int
373687Sroot biz_dialer(num, mod)
384962Ssam 	char *num, *mod;
393687Sroot {
403687Sroot 	register int connected = 0;
413687Sroot 
423687Sroot 	if (!bizsync(FD)) {
433687Sroot 		logent(value(HOST), "", "biz", "out of sync");
443687Sroot 		printf("bizcomp out of sync\n");
453687Sroot 		delock(uucplock);
463687Sroot 		exit(0);
473687Sroot 	}
483687Sroot 	if (boolean(value(VERBOSE)))
493687Sroot 		printf("\nstarting call...");
503687Sroot 	echo("#\rk$\r$\n");			/* disable auto-answer */
513687Sroot 	echo("$>$.$ #\r");			/* tone/pulse dialing */
523687Sroot 	echo(mod);
533687Sroot 	echo("$\r$\n");
543687Sroot 	echo("$>$.$ #\re$ ");			/* disconnection sequence */
5513277Ssam 	echo(DISCONNECT_CMD);
563687Sroot 	echo("\r$\n$\r$\n");
573687Sroot 	echo("$>$.$ #\rr$ ");			/* repeat dial */
583687Sroot 	echo(num);
593687Sroot 	echo("\r$\n");
603687Sroot 	if (boolean(value(VERBOSE)))
613687Sroot 		printf("ringing...");
623687Sroot 	/*
633687Sroot 	 * The reply from the BIZCOMP should be:
643687Sroot 	 *	`^G NO CONNECTION\r\n^G\r\n'	failure
653687Sroot 	 *	` CONNECTION\r\n^G'		success
663687Sroot 	 */
673687Sroot 	connected = detect(" ");
683687Sroot #ifdef ACULOG
693687Sroot 	if (timeout) {
703687Sroot 		char line[80];
713687Sroot 
723687Sroot 		sprintf(line, "%d second dial timeout",
733687Sroot 			number(value(DIALTIMEOUT)));
743687Sroot 		logent(value(HOST), num, "biz", line);
753687Sroot 	}
763687Sroot #endif
773687Sroot 	if (!connected)
783687Sroot 		flush(" NO CONNECTION\r\n\07\r\n");
793687Sroot 	else
803687Sroot 		flush("CONNECTION\r\n\07");
813687Sroot 	if (timeout)
824408Ssam 		biz31_disconnect();	/* insurance */
835132Ssam 	return (connected);
843687Sroot }
853687Sroot 
864408Ssam biz31w_dialer(num, acu)
874962Ssam 	char *num, *acu;
883687Sroot {
8913277Ssam 
905132Ssam 	return (biz_dialer(num, "w"));
913687Sroot }
923687Sroot 
934408Ssam biz31f_dialer(num, acu)
944962Ssam 	char *num, *acu;
953687Sroot {
9613277Ssam 
975132Ssam 	return (biz_dialer(num, "f"));
983687Sroot }
993687Sroot 
1004408Ssam biz31_disconnect()
1013687Sroot {
10213277Ssam 
10313277Ssam 	write(FD, DISCONNECT_CMD, 4);
1043687Sroot 	sleep(2);
1053687Sroot 	ioctl(FD, TIOCFLUSH);
1063687Sroot }
1073687Sroot 
1084408Ssam biz31_abort()
1093687Sroot {
11013277Ssam 
1113687Sroot 	write(FD, "\33", 1);
1123687Sroot }
1133687Sroot 
1143687Sroot static int
1153687Sroot echo(s)
1164962Ssam 	register char *s;
1173687Sroot {
1183687Sroot 	char c;
1193687Sroot 
1205132Ssam 	while (c = *s++) switch (c) {
1215132Ssam 
1225132Ssam 	case '$':
1235132Ssam 		read(FD, &c, 1);
1245132Ssam 		s++;
1255132Ssam 		break;
1265132Ssam 
1275132Ssam 	case '#':
1285132Ssam 		c = *s++;
1295132Ssam 		write(FD, &c, 1);
1305132Ssam 		break;
1315132Ssam 
1325132Ssam 	default:
1335132Ssam 		write(FD, &c, 1);
1345132Ssam 		read(FD, &c, 1);
1355132Ssam 	}
1363687Sroot }
1373687Sroot 
138*39253Sbostic static void
1393687Sroot sigALRM()
1403687Sroot {
14113277Ssam 
1423687Sroot 	timeout = 1;
14313277Ssam 	longjmp(timeoutbuf, 1);
1443687Sroot }
1453687Sroot 
1463687Sroot static int
1473687Sroot detect(s)
1484962Ssam 	register char *s;
1493687Sroot {
150*39253Sbostic 	sig_t f;
1513687Sroot 	char c;
1523687Sroot 
15313277Ssam 	f = signal(SIGALRM, sigALRM);
1543687Sroot 	timeout = 0;
1555132Ssam 	while (*s) {
15613277Ssam 		if (setjmp(timeoutbuf)) {
15713277Ssam 			printf("\07timeout waiting for reply\n");
15813277Ssam 			biz31_abort();
15913277Ssam 			break;
16013277Ssam 		}
1613687Sroot 		alarm(number(value(DIALTIMEOUT)));
1623687Sroot 		read(FD, &c, 1);
1633687Sroot 		alarm(0);
1643687Sroot 		if (c != *s++)
16513277Ssam 			break;
1663687Sroot 	}
16713277Ssam 	signal(SIGALRM, f);
16813277Ssam 	return (timeout == 0);
1693687Sroot }
1703687Sroot 
1713687Sroot static int
1723687Sroot flush(s)
1734962Ssam 	register char *s;
1743687Sroot {
175*39253Sbostic 	sig_t f;
1763687Sroot 	char c;
1773687Sroot 
17813277Ssam 	f = signal(SIGALRM, sigALRM);
1795132Ssam 	while (*s++) {
18013277Ssam 		if (setjmp(timeoutbuf))
18113277Ssam 			break;
1823687Sroot 		alarm(10);
1833687Sroot 		read(FD, &c, 1);
1843687Sroot 		alarm(0);
1853687Sroot 	}
18613277Ssam 	signal(SIGALRM, f);
1873687Sroot 	timeout = 0;			/* guard against disconnection */
1883687Sroot }
1893687Sroot 
1903687Sroot /*
1913687Sroot  * This convoluted piece of code attempts to get
1923687Sroot  *  the bizcomp in sync.  If you don't have the capacity or nread
1933687Sroot  *  call there are gory ways to simulate this.
1943687Sroot  */
1953687Sroot static int
1963687Sroot bizsync(fd)
1973687Sroot {
1983687Sroot #ifdef FIOCAPACITY
1993687Sroot 	struct capacity b;
2003687Sroot #	define chars(b)	((b).cp_nbytes)
2013687Sroot #	define IOCTL	FIOCAPACITY
2023687Sroot #endif
2033687Sroot #ifdef FIONREAD
2043687Sroot 	long b;
2053687Sroot #	define chars(b)	(b)
2063687Sroot #	define IOCTL	FIONREAD
2073687Sroot #endif
2083687Sroot 	register int already = 0;
2093687Sroot 	char buf[10];
2103687Sroot 
2113687Sroot retry:
2123687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
2133687Sroot 		ioctl(fd, TIOCFLUSH);
2143687Sroot 	write(fd, "\rp>\r", 4);
2153687Sroot 	sleep(1);
2163687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
2173687Sroot 		if (chars(b) != 10) {
2183687Sroot 	nono:
2193687Sroot 			if (already > MAXRETRY)
2205132Ssam 				return (0);
22113277Ssam 			write(fd, DISCONNECT_CMD, 4);
2223687Sroot 			sleep(2);
2233687Sroot 			already++;
2243687Sroot 			goto retry;
2253687Sroot 		} else {
2263687Sroot 			read(fd, buf, 10);
2273687Sroot 			if (strncmp(buf, "p >\r\n\r\n>", 8))
2283687Sroot 				goto nono;
2293687Sroot 		}
2303687Sroot 	}
2315132Ssam 	return (1);
2323687Sroot }
233