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