xref: /netbsd-src/usr.bin/tip/aculib/v831.c (revision 4d7e773266e3c3f48566c86c0ad52d51c6454fd1)
1 /*	$NetBSD: v831.c,v 1.5 1996/12/29 10:42:01 cgd 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[] = "@(#)v831.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: v831.c,v 1.5 1996/12/29 10:42:01 cgd Exp $";
41 #endif /* not lint */
42 
43 /*
44  * Routines for dialing up on Vadic 831
45  */
46 #include "tip.h"
47 #include <termios.h>
48 
49 int	v831_abort();
50 static	void alarmtr();
51 static	int dialit();
52 static	char *sanitize();
53 extern	int errno;
54 
55 static jmp_buf jmpbuf;
56 static int child = -1;
57 
58 v831_dialer(num, acu)
59         char *num, *acu;
60 {
61         int status, pid, connected = 1;
62         register int timelim;
63 
64         if (boolean(value(VERBOSE)))
65                 printf("\nstarting call...");
66 #ifdef DEBUG
67         printf ("(acu=%s)\n", acu);
68 #endif
69         if ((AC = open(acu, O_RDWR)) < 0) {
70                 if (errno == EBUSY)
71                         printf("line busy...");
72                 else
73                         printf("acu open error...");
74                 return (0);
75         }
76         if (setjmp(jmpbuf)) {
77                 kill(child, SIGKILL);
78                 close(AC);
79                 return (0);
80         }
81         signal(SIGALRM, alarmtr);
82         timelim = 5 * strlen(num);
83         alarm(timelim < 30 ? 30 : timelim);
84         if ((child = fork()) == 0) {
85                 /*
86                  * ignore this stuff for aborts
87                  */
88                 signal(SIGALRM, SIG_IGN);
89 		signal(SIGINT, SIG_IGN);
90                 signal(SIGQUIT, SIG_IGN);
91                 sleep(2);
92                 exit(dialit(num, acu) != 'A');
93         }
94         /*
95          * open line - will return on carrier
96          */
97         if ((FD = open(DV, O_RDWR)) < 0) {
98 #ifdef DEBUG
99                 printf("(after open, errno=%d)\n", errno);
100 #endif
101                 if (errno == EIO)
102                         printf("lost carrier...");
103                 else
104                         printf("dialup line open failed...");
105                 alarm(0);
106                 kill(child, SIGKILL);
107                 close(AC);
108                 return (0);
109         }
110         alarm(0);
111         signal(SIGALRM, SIG_DFL);
112         while ((pid = wait(&status)) != child && pid != -1)
113                 ;
114         if (status) {
115                 close(AC);
116                 return (0);
117         }
118         return (1);
119 }
120 
121 static void
122 alarmtr()
123 {
124         alarm(0);
125         longjmp(jmpbuf, 1);
126 }
127 
128 /*
129  * Insurance, for some reason we don't seem to be
130  *  hanging up...
131  */
132 v831_disconnect()
133 {
134 	struct termios	cntrl;
135 
136         sleep(2);
137 #ifdef DEBUG
138         printf("[disconnect: FD=%d]\n", FD);
139 #endif
140         if (FD > 0) {
141                 ioctl(FD, TIOCCDTR, 0);
142 		tcgetattr(FD, &cntrl);
143 		cfsetospeed(&cntrl, 0);
144 		cfsetispeed(&cntrl, 0);
145 		tcsetattr(FD, TCSAFLUSH, &cntrl);
146                 ioctl(FD, TIOCNXCL, NULL);
147         }
148         close(FD);
149 }
150 
151 v831_abort()
152 {
153 
154 #ifdef DEBUG
155         printf("[abort: AC=%d]\n", AC);
156 #endif
157         sleep(2);
158         if (child > 0)
159                 kill(child, SIGKILL);
160         if (AC > 0)
161                 ioctl(FD, TIOCNXCL, NULL);
162                 close(AC);
163         if (FD > 0)
164                 ioctl(FD, TIOCCDTR, 0);
165         close(FD);
166 }
167 
168 /*
169  * Sigh, this probably must be changed at each site.
170  */
171 struct vaconfig {
172 	char	*vc_name;
173 	char	vc_rack;
174 	char	vc_modem;
175 } vaconfig[] = {
176 	{ "/dev/cua0",'4','0' },
177 	{ "/dev/cua1",'4','1' },
178 	{ 0 }
179 };
180 
181 #define pc(x)	(c = x, write(AC,&c,1))
182 #define ABORT	01
183 #define SI	017
184 #define STX	02
185 #define ETX	03
186 
187 static int
188 dialit(phonenum, acu)
189 	register char *phonenum;
190 	char *acu;
191 {
192         register struct vaconfig *vp;
193 	struct termios cntrl;
194         char c;
195         int i, two = 2;
196 
197         phonenum = sanitize(phonenum);
198 #ifdef DEBUG
199         printf ("(dial phonenum=%s)\n", phonenum);
200 #endif
201         if (*phonenum == '<' && phonenum[1] == 0)
202                 return ('Z');
203 	for (vp = vaconfig; vp->vc_name; vp++)
204 		if (strcmp(vp->vc_name, acu) == 0)
205 			break;
206 	if (vp->vc_name == 0) {
207 		printf("Unable to locate dialer (%s)\n", acu);
208 		return ('K');
209 	}
210 	tcgetattr(AC, &cntrl);
211 	cfsetospeed(&cntrl, B2400);
212 	cfsetispeed(&cntrl, B2400);
213 	cntrl.c_cflag |= PARODD | PARENB;
214 	cntrl.c_lflag &= ~(ISIG | ICANON);
215 	tcsetattr(AC, TCSANOW, &cntrl);
216 	tcflush(AC, TCIOFLUSH);
217         pc(STX);
218 	pc(vp->vc_rack);
219 	pc(vp->vc_modem);
220 	while (*phonenum && *phonenum != '<')
221 		pc(*phonenum++);
222         pc(SI);
223 	pc(ETX);
224         sleep(1);
225         i = read(AC, &c, 1);
226 #ifdef DEBUG
227         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
228 #endif
229         if (i != 1)
230 		c = 'M';
231         if (c == 'B' || c == 'G') {
232                 char cc, oc = c;
233 
234                 pc(ABORT);
235                 read(AC, &cc, 1);
236 #ifdef DEBUG
237                 printf("abort response=%c\n", cc);
238 #endif
239                 c = oc;
240                 v831_disconnect();
241         }
242         close(AC);
243 #ifdef DEBUG
244         printf("dialit: returns %c\n", c);
245 #endif
246         return (c);
247 }
248 
249 static char *
250 sanitize(s)
251 	register char *s;
252 {
253         static char buf[128];
254         register char *cp;
255 
256         for (cp = buf; *s; s++) {
257 		if (!isdigit(*s) && *s == '<' && *s != '_')
258 			continue;
259 		if (*s == '_')
260 			*s = '=';
261 		*cp++ = *s;
262 	}
263         *cp++ = 0;
264         return (buf);
265 }
266