xref: /netbsd/usr.bin/tip/aculib/biz31.c (revision bf9ec67e)
1 /*	$NetBSD: biz31.c,v 1.7 1998/07/12 09:14:19 mrg 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)biz31.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: biz31.c,v 1.7 1998/07/12 09:14:19 mrg Exp $");
42 #endif /* not lint */
43 
44 #include "tip.h"
45 
46 #define MAXRETRY	3		/* sync up retry count */
47 #define DISCONNECT_CMD	"\21\25\11\24"	/* disconnection string */
48 
49 static	void sigALRM();
50 static	int timeout = 0;
51 static	jmp_buf timeoutbuf;
52 
53 /*
54  * Dial up on a BIZCOMP Model 1031 with either
55  * 	tone dialing (mod = "f")
56  *	pulse dialing (mod = "w")
57  */
58 static int
59 biz_dialer(num, mod)
60 	char *num, *mod;
61 {
62 	int connected = 0;
63 
64 	if (!bizsync(FD)) {
65 		logent(value(HOST), "", "biz", "out of sync");
66 		printf("bizcomp out of sync\n");
67 		delock(uucplock);
68 		exit(0);
69 	}
70 	if (boolean(value(VERBOSE)))
71 		printf("\nstarting call...");
72 	echo("#\rk$\r$\n");			/* disable auto-answer */
73 	echo("$>$.$ #\r");			/* tone/pulse dialing */
74 	echo(mod);
75 	echo("$\r$\n");
76 	echo("$>$.$ #\re$ ");			/* disconnection sequence */
77 	echo(DISCONNECT_CMD);
78 	echo("\r$\n$\r$\n");
79 	echo("$>$.$ #\rr$ ");			/* repeat dial */
80 	echo(num);
81 	echo("\r$\n");
82 	if (boolean(value(VERBOSE)))
83 		printf("ringing...");
84 	/*
85 	 * The reply from the BIZCOMP should be:
86 	 *	`^G NO CONNECTION\r\n^G\r\n'	failure
87 	 *	` CONNECTION\r\n^G'		success
88 	 */
89 	connected = detect(" ");
90 #ifdef ACULOG
91 	if (timeout) {
92 		char line[80];
93 
94 		(void)snprintf(line, sizeof line, "%d second dial timeout",
95 			number(value(DIALTIMEOUT)));
96 		logent(value(HOST), num, "biz", line);
97 	}
98 #endif
99 	if (!connected)
100 		flush(" NO CONNECTION\r\n\07\r\n");
101 	else
102 		flush("CONNECTION\r\n\07");
103 	if (timeout)
104 		biz31_disconnect();	/* insurance */
105 	return (connected);
106 }
107 
108 biz31w_dialer(num, acu)
109 	char *num, *acu;
110 {
111 
112 	return (biz_dialer(num, "w"));
113 }
114 
115 biz31f_dialer(num, acu)
116 	char *num, *acu;
117 {
118 
119 	return (biz_dialer(num, "f"));
120 }
121 
122 biz31_disconnect()
123 {
124 
125 	write(FD, DISCONNECT_CMD, 4);
126 	sleep(2);
127 	tcflush(FD, TCIOFLUSH);
128 }
129 
130 biz31_abort()
131 {
132 
133 	write(FD, "\33", 1);
134 }
135 
136 static int
137 echo(s)
138 	char *s;
139 {
140 	char c;
141 
142 	while (c = *s++) switch (c) {
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 	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 	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 	int already = 0;
230 	char buf[10];
231 
232 retry:
233 	if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
234 		tcflush(FD, TCIOFLUSH);
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