xref: /netbsd/usr.bin/tip/aculib/v831.c (revision bf9ec67e)
1 /*	$NetBSD: v831.c,v 1.8 1998/12/19 23:02:21 christos 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.8 1998/12/19 23:02:21 christos 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 
125         alarm(0);
126         longjmp(jmpbuf, 1);
127 }
128 
129 /*
130  * Insurance, for some reason we don't seem to be
131  *  hanging up...
132  */
133 void
134 v831_disconnect()
135 {
136 	struct termios	cntrl;
137 
138         sleep(2);
139 #ifdef DEBUG
140         printf("[disconnect: FD=%d]\n", FD);
141 #endif
142         if (FD > 0) {
143                 ioctl(FD, TIOCCDTR, 0);
144 		tcgetattr(FD, &cntrl);
145 		cfsetospeed(&cntrl, 0);
146 		cfsetispeed(&cntrl, 0);
147 		tcsetattr(FD, TCSAFLUSH, &cntrl);
148                 ioctl(FD, TIOCNXCL, NULL);
149         }
150         close(FD);
151 }
152 
153 void
154 v831_abort()
155 {
156 
157 #ifdef DEBUG
158         printf("[abort: AC=%d]\n", AC);
159 #endif
160         sleep(2);
161         if (child > 0)
162                 kill(child, SIGKILL);
163         if (AC > 0)
164                 ioctl(FD, TIOCNXCL, NULL);
165                 close(AC);
166         if (FD > 0)
167                 ioctl(FD, TIOCCDTR, 0);
168         close(FD);
169 }
170 
171 /*
172  * Sigh, this probably must be changed at each site.
173  */
174 struct vaconfig {
175 	char	*vc_name;
176 	char	vc_rack;
177 	char	vc_modem;
178 } vaconfig[] = {
179 	{ "/dev/cua0",'4','0' },
180 	{ "/dev/cua1",'4','1' },
181 	{ 0 }
182 };
183 
184 #define pc(x)	(c = x, write(AC,&c,1))
185 #define ABORT	01
186 #define SI	017
187 #define STX	02
188 #define ETX	03
189 
190 static int
191 dialit(phonenum, acu)
192 	char *phonenum;
193 	char *acu;
194 {
195         struct vaconfig *vp;
196 	struct termios cntrl;
197         char c;
198         int i;
199 
200         phonenum = sanitize(phonenum);
201 #ifdef DEBUG
202         printf ("(dial phonenum=%s)\n", phonenum);
203 #endif
204         if (*phonenum == '<' && phonenum[1] == 0)
205                 return ('Z');
206 	for (vp = vaconfig; vp->vc_name; vp++)
207 		if (strcmp(vp->vc_name, acu) == 0)
208 			break;
209 	if (vp->vc_name == 0) {
210 		printf("Unable to locate dialer (%s)\n", acu);
211 		return ('K');
212 	}
213 	tcgetattr(AC, &cntrl);
214 	cfsetospeed(&cntrl, B2400);
215 	cfsetispeed(&cntrl, B2400);
216 	cntrl.c_cflag |= PARODD | PARENB;
217 	cntrl.c_lflag &= ~(ISIG | ICANON);
218 	tcsetattr(AC, TCSANOW, &cntrl);
219 	tcflush(AC, TCIOFLUSH);
220         pc(STX);
221 	pc(vp->vc_rack);
222 	pc(vp->vc_modem);
223 	while (*phonenum && *phonenum != '<')
224 		pc(*phonenum++);
225         pc(SI);
226 	pc(ETX);
227         sleep(1);
228         i = read(AC, &c, 1);
229 #ifdef DEBUG
230         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
231 #endif
232         if (i != 1)
233 		c = 'M';
234         if (c == 'B' || c == 'G') {
235                 char cc, oc = c;
236 
237                 pc(ABORT);
238                 read(AC, &cc, 1);
239 #ifdef DEBUG
240                 printf("abort response=%c\n", cc);
241 #endif
242                 c = oc;
243                 v831_disconnect();
244         }
245         close(AC);
246 #ifdef DEBUG
247         printf("dialit: returns %c\n", c);
248 #endif
249         return (c);
250 }
251 
252 static char *
253 sanitize(s)
254 	char *s;
255 {
256         static char buf[128];
257         char *cp;
258 
259         for (cp = buf; *s && buf + sizeof buf - cp > 1; s++) {
260 		if (!isdigit((unsigned char)*s) && *s == '<' && *s != '_')
261 			continue;
262 		if (*s == '_')
263 			*s = '=';
264 		*cp++ = *s;
265 	}
266         *cp++ = 0;
267         return (buf);
268 }
269