xref: /freebsd/usr.bin/tip/libacu/biz22.c (revision 0f3bdf5d)
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 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)biz22.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 static char rcsid[] = "$OpenBSD: biz22.c,v 1.7 2001/10/24 18:38:58 millert Exp $";
42 #endif /* not lint */
43 
44 #include "tip.h"
45 
46 #define DISCONNECT_CMD	"\20\04"	/* disconnection string */
47 
48 static	void sigALRM();
49 static	int timeout = 0;
50 static	jmp_buf timeoutbuf;
51 
52 static	int cmd(), detect();
53 void	biz22_disconnect();
54 
55 /*
56  * Dial up on a BIZCOMP Model 1022 with either
57  * 	tone dialing (mod = "V")
58  *	pulse dialing (mod = "W")
59  */
60 static int
61 biz_dialer(num, mod)
62 	char *num, *mod;
63 {
64 	int connected = 0;
65 	char cbuf[40];
66 
67 	if (boolean(value(VERBOSE)))
68 		printf("\nstarting call...");
69 	/*
70 	 * Disable auto-answer and configure for tone/pulse
71 	 *  dialing
72 	 */
73 	if (cmd("\02K\r")) {
74 		printf("can't initialize bizcomp...");
75 		return (0);
76 	}
77 	(void)strcpy(cbuf, "\02.\r");
78 	cbuf[1] = *mod;
79 	if (cmd(cbuf)) {
80 		printf("can't set dialing mode...");
81 		return (0);
82 	}
83 	(void)snprintf(cbuf, sizeof(cbuf), "\02D%s\r", num);
84 	write(FD, cbuf, strlen(cbuf));
85 	if (!detect("7\r")) {
86 		printf("can't get dial tone...");
87 		return (0);
88 	}
89 	if (boolean(value(VERBOSE)))
90 		printf("ringing...");
91 	/*
92 	 * The reply from the BIZCOMP should be:
93 	 *	2 \r or 7 \r	failure
94 	 *	1 \r		success
95 	 */
96 	connected = detect("1\r");
97 #ifdef ACULOG
98 	if (timeout) {
99 		char line[80];
100 
101 		(void)sprintf(line, "%ld second dial timeout",
102 			number(value(DIALTIMEOUT)));
103 		logent(value(HOST), num, "biz1022", line);
104 	}
105 #endif
106 	if (timeout)
107 		biz22_disconnect();	/* insurance */
108 	return (connected);
109 }
110 
111 int
112 biz22w_dialer(num, acu)
113 	char *num, *acu;
114 {
115 
116 	return (biz_dialer(num, "W"));
117 }
118 
119 int
120 biz22f_dialer(num, acu)
121 	char *num, *acu;
122 {
123 
124 	return (biz_dialer(num, "V"));
125 }
126 
127 void
128 biz22_disconnect()
129 {
130 	write(FD, DISCONNECT_CMD, 4);
131 	sleep(2);
132 	tcflush(FD, TCIOFLUSH);
133 }
134 
135 void
136 biz22_abort()
137 {
138 
139 	write(FD, "\02", 1);
140 }
141 
142 static void
143 sigALRM()
144 {
145 
146 	timeout = 1;
147 	longjmp(timeoutbuf, 1);
148 }
149 
150 static int
151 cmd(s)
152 	char *s;
153 {
154 	sig_t f;
155 	char c;
156 
157 	write(FD, s, strlen(s));
158 	f = signal(SIGALRM, sigALRM);
159 	if (setjmp(timeoutbuf)) {
160 		biz22_abort();
161 		signal(SIGALRM, f);
162 		return (1);
163 	}
164 	alarm(number(value(DIALTIMEOUT)));
165 	read(FD, &c, 1);
166 	alarm(0);
167 	signal(SIGALRM, f);
168 	c &= 0177;
169 	return (c != '\r');
170 }
171 
172 static int
173 detect(s)
174 	char *s;
175 {
176 	sig_t f;
177 	char c;
178 
179 	f = signal(SIGALRM, sigALRM);
180 	timeout = 0;
181 	while (*s) {
182 		if (setjmp(timeoutbuf)) {
183 			biz22_abort();
184 			break;
185 		}
186 		alarm(number(value(DIALTIMEOUT)));
187 		read(FD, &c, 1);
188 		alarm(0);
189 		c &= 0177;
190 		if (c != *s++)
191 			return (0);
192 	}
193 	signal(SIGALRM, f);
194 	return (timeout == 0);
195 }
196