xref: /freebsd/usr.bin/tip/libacu/biz22.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
1 /*	$OpenBSD: biz22.c,v 1.7 2001/10/24 18:38:58 millert Exp $	*/
2 /*	$NetBSD: biz22.c,v 1.6 1997/02/11 09:24:11 mrg Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)biz22.c	8.1 (Berkeley) 6/6/93";
43 static char rcsid[] = "$OpenBSD: biz22.c,v 1.7 2001/10/24 18:38:58 millert Exp $";
44 #endif
45 #endif /* not lint */
46 
47 #include "tip.h"
48 
49 #define DISCONNECT_CMD	"\20\04"	/* disconnection string */
50 
51 static	void sigALRM();
52 static	int timeout = 0;
53 static	jmp_buf timeoutbuf;
54 
55 static	int cmd(), detect();
56 void	biz22_disconnect();
57 
58 /*
59  * Dial up on a BIZCOMP Model 1022 with either
60  * 	tone dialing (mod = "V")
61  *	pulse dialing (mod = "W")
62  */
63 static int
64 biz_dialer(num, mod)
65 	char *num, *mod;
66 {
67 	int connected = 0;
68 	char cbuf[40];
69 
70 	if (boolean(value(VERBOSE)))
71 		printf("\nstarting call...");
72 	/*
73 	 * Disable auto-answer and configure for tone/pulse
74 	 *  dialing
75 	 */
76 	if (cmd("\02K\r")) {
77 		printf("can't initialize bizcomp...");
78 		return (0);
79 	}
80 	(void)strcpy(cbuf, "\02.\r");
81 	cbuf[1] = *mod;
82 	if (cmd(cbuf)) {
83 		printf("can't set dialing mode...");
84 		return (0);
85 	}
86 	(void)snprintf(cbuf, sizeof(cbuf), "\02D%s\r", num);
87 	write(FD, cbuf, strlen(cbuf));
88 	if (!detect("7\r")) {
89 		printf("can't get dial tone...");
90 		return (0);
91 	}
92 	if (boolean(value(VERBOSE)))
93 		printf("ringing...");
94 	/*
95 	 * The reply from the BIZCOMP should be:
96 	 *	2 \r or 7 \r	failure
97 	 *	1 \r		success
98 	 */
99 	connected = detect("1\r");
100 #ifdef ACULOG
101 	if (timeout) {
102 		char line[80];
103 
104 		(void)sprintf(line, "%ld second dial timeout",
105 			number(value(DIALTIMEOUT)));
106 		logent(value(HOST), num, "biz1022", line);
107 	}
108 #endif
109 	if (timeout)
110 		biz22_disconnect();	/* insurance */
111 	return (connected);
112 }
113 
114 int
115 biz22w_dialer(num, acu)
116 	char *num, *acu;
117 {
118 
119 	return (biz_dialer(num, "W"));
120 }
121 
122 int
123 biz22f_dialer(num, acu)
124 	char *num, *acu;
125 {
126 
127 	return (biz_dialer(num, "V"));
128 }
129 
130 void
131 biz22_disconnect()
132 {
133 	write(FD, DISCONNECT_CMD, 4);
134 	sleep(2);
135 	tcflush(FD, TCIOFLUSH);
136 }
137 
138 void
139 biz22_abort()
140 {
141 
142 	write(FD, "\02", 1);
143 }
144 
145 static void
146 sigALRM()
147 {
148 
149 	timeout = 1;
150 	longjmp(timeoutbuf, 1);
151 }
152 
153 static int
154 cmd(s)
155 	char *s;
156 {
157 	sig_t f;
158 	char c;
159 
160 	write(FD, s, strlen(s));
161 	f = signal(SIGALRM, sigALRM);
162 	if (setjmp(timeoutbuf)) {
163 		biz22_abort();
164 		signal(SIGALRM, f);
165 		return (1);
166 	}
167 	alarm(number(value(DIALTIMEOUT)));
168 	read(FD, &c, 1);
169 	alarm(0);
170 	signal(SIGALRM, f);
171 	c &= 0177;
172 	return (c != '\r');
173 }
174 
175 static int
176 detect(s)
177 	char *s;
178 {
179 	sig_t f;
180 	char c;
181 
182 	f = signal(SIGALRM, sigALRM);
183 	timeout = 0;
184 	while (*s) {
185 		if (setjmp(timeoutbuf)) {
186 			biz22_abort();
187 			break;
188 		}
189 		alarm(number(value(DIALTIMEOUT)));
190 		read(FD, &c, 1);
191 		alarm(0);
192 		c &= 0177;
193 		if (c != *s++)
194 			return (0);
195 	}
196 	signal(SIGALRM, f);
197 	return (timeout == 0);
198 }
199