xref: /csrg-svn/usr.bin/tip/aculib/biz31.c (revision 3687)
1*3687Sroot /*	biz31.c	4.1	81/05/09	*/
2*3687Sroot #include "tip.h"
3*3687Sroot 
4*3687Sroot #if BIZCOMP
5*3687Sroot #define MAXRETRY	3		/* sync up retry count */
6*3687Sroot #define DISCONNECT	"\21\25\11\24"	/* disconnection string */
7*3687Sroot 
8*3687Sroot static int sigALRM();
9*3687Sroot static int timeout = 0;
10*3687Sroot 
11*3687Sroot /*
12*3687Sroot  * Dial up on a BIZCOMP with either
13*3687Sroot  * 	tone dialing (mod = "f")
14*3687Sroot  *	pulse dialing (mod = "w")
15*3687Sroot  */
16*3687Sroot static int
17*3687Sroot biz_dialer(num, mod)
18*3687Sroot char *num, *mod;
19*3687Sroot {
20*3687Sroot 	register int connected = 0;
21*3687Sroot 
22*3687Sroot 	if (!bizsync(FD)) {
23*3687Sroot 		logent(value(HOST), "", "biz", "out of sync");
24*3687Sroot 		printf("bizcomp out of sync\n");
25*3687Sroot 		delock(uucplock);
26*3687Sroot 		exit(0);
27*3687Sroot 	}
28*3687Sroot 	if (boolean(value(VERBOSE)))
29*3687Sroot 		printf("\nstarting call...");
30*3687Sroot 	echo("#\rk$\r$\n");			/* disable auto-answer */
31*3687Sroot 	echo("$>$.$ #\r");			/* tone/pulse dialing */
32*3687Sroot 	echo(mod);
33*3687Sroot 	echo("$\r$\n");
34*3687Sroot 	echo("$>$.$ #\re$ ");			/* disconnection sequence */
35*3687Sroot 	echo(DISCONNECT);
36*3687Sroot 	echo("\r$\n$\r$\n");
37*3687Sroot 	echo("$>$.$ #\rr$ ");			/* repeat dial */
38*3687Sroot 	echo(num);
39*3687Sroot 	echo("\r$\n");
40*3687Sroot 	if (boolean(value(VERBOSE)))
41*3687Sroot 		printf("ringing...");
42*3687Sroot 	/*
43*3687Sroot 	 * The reply from the BIZCOMP should be:
44*3687Sroot 	 *	`^G NO CONNECTION\r\n^G\r\n'	failure
45*3687Sroot 	 *	` CONNECTION\r\n^G'		success
46*3687Sroot 	 */
47*3687Sroot 	connected = detect(" ");
48*3687Sroot #ifdef ACULOG
49*3687Sroot 	if (timeout) {
50*3687Sroot 		char line[80];
51*3687Sroot 
52*3687Sroot 		sprintf(line, "%d second dial timeout",
53*3687Sroot 			number(value(DIALTIMEOUT)));
54*3687Sroot 		logent(value(HOST), num, "biz", line);
55*3687Sroot 	}
56*3687Sroot #endif
57*3687Sroot 	if (!connected)
58*3687Sroot 		flush(" NO CONNECTION\r\n\07\r\n");
59*3687Sroot 	else
60*3687Sroot 		flush("CONNECTION\r\n\07");
61*3687Sroot 	if (timeout)
62*3687Sroot 		biz_disconnect();	/* insurance */
63*3687Sroot 	return(connected);
64*3687Sroot }
65*3687Sroot 
66*3687Sroot bizw_dialer(num, acu)
67*3687Sroot char *num, *acu;
68*3687Sroot {
69*3687Sroot 	return(biz_dialer(num, "w"));
70*3687Sroot }
71*3687Sroot 
72*3687Sroot bizf_dialer(num, acu)
73*3687Sroot char *num, *acu;
74*3687Sroot {
75*3687Sroot 	return(biz_dialer(num, "f"));
76*3687Sroot }
77*3687Sroot 
78*3687Sroot biz_disconnect()
79*3687Sroot {
80*3687Sroot 	write(FD, DISCONNECT, 4);
81*3687Sroot 	sleep(2);
82*3687Sroot 	ioctl(FD, TIOCFLUSH);
83*3687Sroot }
84*3687Sroot 
85*3687Sroot biz_abort()
86*3687Sroot {
87*3687Sroot 	write(FD, "\33", 1);
88*3687Sroot 	timeout = 1;
89*3687Sroot }
90*3687Sroot 
91*3687Sroot static int
92*3687Sroot echo(s)
93*3687Sroot register char *s;
94*3687Sroot {
95*3687Sroot 	char c;
96*3687Sroot 
97*3687Sroot 	while (c = *s++)
98*3687Sroot 		switch(c)
99*3687Sroot 		{
100*3687Sroot 			case '$':
101*3687Sroot 				read(FD, &c, 1);
102*3687Sroot 				s++;
103*3687Sroot 				break;
104*3687Sroot 			case '#':
105*3687Sroot 				c = *s++;
106*3687Sroot 				write(FD, &c, 1);
107*3687Sroot 				break;
108*3687Sroot 			default:
109*3687Sroot 				write(FD, &c, 1);
110*3687Sroot 				read(FD, &c, 1);
111*3687Sroot 		}
112*3687Sroot }
113*3687Sroot 
114*3687Sroot static int
115*3687Sroot sigALRM()
116*3687Sroot {
117*3687Sroot 	signal(SIGALRM, SIG_IGN);
118*3687Sroot 	printf("\07timeout waiting for reply\n");
119*3687Sroot 	timeout = 1;
120*3687Sroot }
121*3687Sroot 
122*3687Sroot static int
123*3687Sroot detect(s)
124*3687Sroot register char *s;
125*3687Sroot {
126*3687Sroot 	char c;
127*3687Sroot 
128*3687Sroot 	signal(SIGALRM, biz_abort);
129*3687Sroot 	timeout = 0;
130*3687Sroot 	while (*s)
131*3687Sroot 	{
132*3687Sroot 		alarm(number(value(DIALTIMEOUT)));
133*3687Sroot 		read(FD, &c, 1);
134*3687Sroot 		alarm(0);
135*3687Sroot 		if (timeout)
136*3687Sroot 			return(0);
137*3687Sroot 		if (c != *s++)
138*3687Sroot 			return(0);
139*3687Sroot 	}
140*3687Sroot 	signal(SIGALRM, SIG_DFL);
141*3687Sroot 	return(1);
142*3687Sroot }
143*3687Sroot 
144*3687Sroot static int
145*3687Sroot flush(s)
146*3687Sroot register char *s;
147*3687Sroot {
148*3687Sroot 	char c;
149*3687Sroot 
150*3687Sroot 	signal(SIGALRM, sigALRM);
151*3687Sroot 	timeout = 0;
152*3687Sroot 	while (*s++)
153*3687Sroot 	{
154*3687Sroot 		alarm(10);
155*3687Sroot 		read(FD, &c, 1);
156*3687Sroot 		alarm(0);
157*3687Sroot 		if (timeout)
158*3687Sroot 			break;
159*3687Sroot 	}
160*3687Sroot 	signal(SIGALRM, SIG_DFL);
161*3687Sroot 	timeout = 0;			/* guard against disconnection */
162*3687Sroot 	return(1);
163*3687Sroot }
164*3687Sroot 
165*3687Sroot /*
166*3687Sroot  * This convoluted piece of code attempts to get
167*3687Sroot  *  the bizcomp in sync.  If you don't have the capacity or nread
168*3687Sroot  *  call there are gory ways to simulate this.
169*3687Sroot  */
170*3687Sroot static int
171*3687Sroot bizsync(fd)
172*3687Sroot {
173*3687Sroot #ifdef FIOCAPACITY
174*3687Sroot 	struct capacity b;
175*3687Sroot #	define chars(b)	((b).cp_nbytes)
176*3687Sroot #	define IOCTL	FIOCAPACITY
177*3687Sroot #endif
178*3687Sroot #ifdef FIONREAD
179*3687Sroot 	long b;
180*3687Sroot #	define chars(b)	(b)
181*3687Sroot #	define IOCTL	FIONREAD
182*3687Sroot #endif
183*3687Sroot 	register int already = 0;
184*3687Sroot 	char buf[10];
185*3687Sroot 
186*3687Sroot retry:
187*3687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
188*3687Sroot 		ioctl(fd, TIOCFLUSH);
189*3687Sroot 	write(fd, "\rp>\r", 4);
190*3687Sroot 	sleep(1);
191*3687Sroot 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
192*3687Sroot 		if (chars(b) != 10) {
193*3687Sroot 	nono:
194*3687Sroot 			if (already > MAXRETRY)
195*3687Sroot 				return(0);
196*3687Sroot 			write(fd, DISCONNECT, 4);
197*3687Sroot 			sleep(2);
198*3687Sroot 			already++;
199*3687Sroot 			goto retry;
200*3687Sroot 		} else {
201*3687Sroot 			read(fd, buf, 10);
202*3687Sroot 			if (strncmp(buf, "p >\r\n\r\n>", 8))
203*3687Sroot 				goto nono;
204*3687Sroot 		}
205*3687Sroot 	}
206*3687Sroot 	return(1);
207*3687Sroot }
208*3687Sroot #endif
209