xref: /netbsd-src/usr.bin/tip/aculib/v3451.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: v3451.c,v 1.3 1994/12/08 09:31:48 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[] = "@(#)v3451.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: v3451.c,v 1.3 1994/12/08 09:31:48 jtc Exp $";
41 #endif /* not lint */
42 
43 /*
44  * Routines for calling up on a Vadic 3451 Modem
45  */
46 #include "tip.h"
47 
48 static	jmp_buf Sjbuf;
49 
50 v3451_dialer(num, acu)
51 	register char *num;
52 	char *acu;
53 {
54 	sig_t func;
55 	int ok;
56 	int slow = number(value(BAUDRATE)) < 1200, rw = 2;
57 	char phone[50];
58 #ifdef ACULOG
59 	char line[80];
60 #endif
61 	static int expect();
62 	static void vawrite();
63 
64 	/*
65 	 * Get in synch
66 	 */
67 	vawrite("I\r", 1 + slow);
68 	vawrite("I\r", 1 + slow);
69 	vawrite("I\r", 1 + slow);
70 	vawrite("\005\r", 2 + slow);
71 	if (!expect("READY")) {
72 		printf("can't synchronize with vadic 3451\n");
73 #ifdef ACULOG
74 		logent(value(HOST), num, "vadic", "can't synch up");
75 #endif
76 		return (0);
77 	}
78 	ioctl(FD, TIOCHPCL, 0);
79 	sleep(1);
80 	vawrite("D\r", 2 + slow);
81 	if (!expect("NUMBER?")) {
82 		printf("Vadic will not accept dial command\n");
83 #ifdef ACULOG
84 		logent(value(HOST), num, "vadic", "will not accept dial");
85 #endif
86 		return (0);
87 	}
88 	strcpy(phone, num);
89 	strcat(phone, "\r");
90 	vawrite(phone, 1 + slow);
91 	if (!expect(phone)) {
92 		printf("Vadic will not accept phone number\n");
93 #ifdef ACULOG
94 		logent(value(HOST), num, "vadic", "will not accept number");
95 #endif
96 		return (0);
97 	}
98 	func = signal(SIGINT,SIG_IGN);
99 	/*
100 	 * You cannot interrupt the Vadic when its dialing;
101 	 * even dropping DTR does not work (definitely a
102 	 * brain damaged design).
103 	 */
104 	vawrite("\r", 1 + slow);
105 	vawrite("\r", 1 + slow);
106 	if (!expect("DIALING:")) {
107 		printf("Vadic failed to dial\n");
108 #ifdef ACULOG
109 		logent(value(HOST), num, "vadic", "failed to dial");
110 #endif
111 		return (0);
112 	}
113 	if (boolean(value(VERBOSE)))
114 		printf("\ndialing...");
115 	ok = expect("ON LINE");
116 	signal(SIGINT, func);
117 	if (!ok) {
118 		printf("call failed\n");
119 #ifdef ACULOG
120 		logent(value(HOST), num, "vadic", "call failed");
121 #endif
122 		return (0);
123 	}
124 	ioctl(FD, TIOCFLUSH, &rw);
125 	return (1);
126 }
127 
128 v3451_disconnect()
129 {
130 
131 	close(FD);
132 }
133 
134 v3451_abort()
135 {
136 
137 	close(FD);
138 }
139 
140 static void
141 vawrite(cp, delay)
142 	register char *cp;
143 	int delay;
144 {
145 
146 	for (; *cp; sleep(delay), cp++)
147 		write(FD, cp, 1);
148 }
149 
150 static
151 expect(cp)
152 	register char *cp;
153 {
154 	char buf[300];
155 	register char *rp = buf;
156 	int timeout = 30, online = 0;
157 	static int notin();
158 	static void alarmtr();
159 
160 	if (strcmp(cp, "\"\"") == 0)
161 		return (1);
162 	*rp = 0;
163 	/*
164 	 * If we are waiting for the Vadic to complete
165 	 * dialing and get a connection, allow more time
166 	 * Unfortunately, the Vadic times out 24 seconds after
167 	 * the last digit is dialed
168 	 */
169 	online = strcmp(cp, "ON LINE") == 0;
170 	if (online)
171 		timeout = number(value(DIALTIMEOUT));
172 	signal(SIGALRM, alarmtr);
173 	if (setjmp(Sjbuf))
174 		return (0);
175 	alarm(timeout);
176 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
177 		if (online && notin("FAILED CALL", buf) == 0)
178 			return (0);
179 		if (read(FD, rp, 1) < 0) {
180 			alarm(0);
181 			return (0);
182 		}
183 		if (*rp &= 0177)
184 			rp++;
185 		*rp = '\0';
186 	}
187 	alarm(0);
188 	return (1);
189 }
190 
191 static void
192 alarmtr()
193 {
194 	longjmp(Sjbuf, 1);
195 }
196 
197 static int
198 notin(sh, lg)
199 	char *sh, *lg;
200 {
201 	static int prefix();
202 
203 	for (; *lg; lg++)
204 		if (prefix(sh, lg))
205 			return (0);
206 	return (1);
207 }
208 
209 static
210 prefix(s1, s2)
211 	register char *s1, *s2;
212 {
213 	register char c;
214 
215 	while ((c = *s1++) == *s2++)
216 		if (c == '\0')
217 			return (1);
218 	return (c == '\0');
219 }
220