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