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