xref: /netbsd-src/usr.bin/tip/aculib/biz31.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*	$NetBSD: biz31.c,v 1.3 1994/12/08 09:31:33 jtc Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)biz31.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: biz31.c,v 1.3 1994/12/08 09:31:33 jtc Exp $";
41 #endif /* not lint */
42 
43 #include "tip.h"
44 
45 #define MAXRETRY	3		/* sync up retry count */
46 #define DISCONNECT_CMD	"\21\25\11\24"	/* disconnection string */
47 
48 static	void sigALRM();
49 static	int timeout = 0;
50 static	jmp_buf timeoutbuf;
51 
52 /*
53  * Dial up on a BIZCOMP Model 1031 with either
54  * 	tone dialing (mod = "f")
55  *	pulse dialing (mod = "w")
56  */
57 static int
58 biz_dialer(num, mod)
59 	char *num, *mod;
60 {
61 	register int connected = 0;
62 
63 	if (!bizsync(FD)) {
64 		logent(value(HOST), "", "biz", "out of sync");
65 		printf("bizcomp out of sync\n");
66 		delock(uucplock);
67 		exit(0);
68 	}
69 	if (boolean(value(VERBOSE)))
70 		printf("\nstarting call...");
71 	echo("#\rk$\r$\n");			/* disable auto-answer */
72 	echo("$>$.$ #\r");			/* tone/pulse dialing */
73 	echo(mod);
74 	echo("$\r$\n");
75 	echo("$>$.$ #\re$ ");			/* disconnection sequence */
76 	echo(DISCONNECT_CMD);
77 	echo("\r$\n$\r$\n");
78 	echo("$>$.$ #\rr$ ");			/* repeat dial */
79 	echo(num);
80 	echo("\r$\n");
81 	if (boolean(value(VERBOSE)))
82 		printf("ringing...");
83 	/*
84 	 * The reply from the BIZCOMP should be:
85 	 *	`^G NO CONNECTION\r\n^G\r\n'	failure
86 	 *	` CONNECTION\r\n^G'		success
87 	 */
88 	connected = detect(" ");
89 #ifdef ACULOG
90 	if (timeout) {
91 		char line[80];
92 
93 		sprintf(line, "%d second dial timeout",
94 			number(value(DIALTIMEOUT)));
95 		logent(value(HOST), num, "biz", line);
96 	}
97 #endif
98 	if (!connected)
99 		flush(" NO CONNECTION\r\n\07\r\n");
100 	else
101 		flush("CONNECTION\r\n\07");
102 	if (timeout)
103 		biz31_disconnect();	/* insurance */
104 	return (connected);
105 }
106 
107 biz31w_dialer(num, acu)
108 	char *num, *acu;
109 {
110 
111 	return (biz_dialer(num, "w"));
112 }
113 
114 biz31f_dialer(num, acu)
115 	char *num, *acu;
116 {
117 
118 	return (biz_dialer(num, "f"));
119 }
120 
121 biz31_disconnect()
122 {
123 
124 	write(FD, DISCONNECT_CMD, 4);
125 	sleep(2);
126 	ioctl(FD, TIOCFLUSH);
127 }
128 
129 biz31_abort()
130 {
131 
132 	write(FD, "\33", 1);
133 }
134 
135 static int
136 echo(s)
137 	register char *s;
138 {
139 	char c;
140 
141 	while (c = *s++) switch (c) {
142 
143 	case '$':
144 		read(FD, &c, 1);
145 		s++;
146 		break;
147 
148 	case '#':
149 		c = *s++;
150 		write(FD, &c, 1);
151 		break;
152 
153 	default:
154 		write(FD, &c, 1);
155 		read(FD, &c, 1);
156 	}
157 }
158 
159 static void
160 sigALRM()
161 {
162 
163 	timeout = 1;
164 	longjmp(timeoutbuf, 1);
165 }
166 
167 static int
168 detect(s)
169 	register char *s;
170 {
171 	sig_t f;
172 	char c;
173 
174 	f = signal(SIGALRM, sigALRM);
175 	timeout = 0;
176 	while (*s) {
177 		if (setjmp(timeoutbuf)) {
178 			printf("\07timeout waiting for reply\n");
179 			biz31_abort();
180 			break;
181 		}
182 		alarm(number(value(DIALTIMEOUT)));
183 		read(FD, &c, 1);
184 		alarm(0);
185 		if (c != *s++)
186 			break;
187 	}
188 	signal(SIGALRM, f);
189 	return (timeout == 0);
190 }
191 
192 static int
193 flush(s)
194 	register char *s;
195 {
196 	sig_t f;
197 	char c;
198 
199 	f = signal(SIGALRM, sigALRM);
200 	while (*s++) {
201 		if (setjmp(timeoutbuf))
202 			break;
203 		alarm(10);
204 		read(FD, &c, 1);
205 		alarm(0);
206 	}
207 	signal(SIGALRM, f);
208 	timeout = 0;			/* guard against disconnection */
209 }
210 
211 /*
212  * This convoluted piece of code attempts to get
213  *  the bizcomp in sync.  If you don't have the capacity or nread
214  *  call there are gory ways to simulate this.
215  */
216 static int
217 bizsync(fd)
218 {
219 #ifdef FIOCAPACITY
220 	struct capacity b;
221 #	define chars(b)	((b).cp_nbytes)
222 #	define IOCTL	FIOCAPACITY
223 #endif
224 #ifdef FIONREAD
225 	long b;
226 #	define chars(b)	(b)
227 #	define IOCTL	FIONREAD
228 #endif
229 	register int already = 0;
230 	char buf[10];
231 
232 retry:
233 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
234 		ioctl(fd, TIOCFLUSH);
235 	write(fd, "\rp>\r", 4);
236 	sleep(1);
237 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
238 		if (chars(b) != 10) {
239 	nono:
240 			if (already > MAXRETRY)
241 				return (0);
242 			write(fd, DISCONNECT_CMD, 4);
243 			sleep(2);
244 			already++;
245 			goto retry;
246 		} else {
247 			read(fd, buf, 10);
248 			if (strncmp(buf, "p >\r\n\r\n>", 8))
249 				goto nono;
250 		}
251 	}
252 	return (1);
253 }
254