xref: /freebsd/usr.bin/tip/libacu/v831.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*	$OpenBSD: v831.c,v 1.11 2006/03/17 19:17:13 moritz Exp $	*/
2 /*	$NetBSD: v831.c,v 1.5 1996/12/29 10:42:01 cgd Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 1983, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #endif /* not lint */
38 
39 /*
40  * Routines for dialing up on Vadic 831
41  */
42 #include "tip.h"
43 #include <termios.h>
44 
45 static jmp_buf jmpbuf;
46 static pid_t child = -1;
47 
48 static void	alarmtr(int);
49 static int	dialit(char *, char *);
50 static char *	sanitize(char *);
51 
52 int
53 v831_dialer(char *num, char *acu)
54 {
55         int status;
56         int timelim;
57 	pid_t pid;
58 
59         if (boolean(value(VERBOSE)))
60                 printf("\nstarting call...");
61 #ifdef DEBUG
62         printf ("(acu=%s)\n", acu);
63 #endif
64         if ((AC = open(acu, O_RDWR)) < 0) {
65                 if (errno == EBUSY)
66                         printf("line busy...");
67                 else
68                         printf("acu open error...");
69                 return (0);
70         }
71         if (setjmp(jmpbuf)) {
72                 kill(child, SIGKILL);
73                 close(AC);
74                 return (0);
75         }
76         signal(SIGALRM, alarmtr);
77         timelim = 5 * strlen(num);
78         alarm(timelim < 30 ? 30 : timelim);
79         if ((child = fork()) == 0) {
80                 /*
81                  * ignore this stuff for aborts
82                  */
83                 signal(SIGALRM, SIG_IGN);
84 		signal(SIGINT, SIG_IGN);
85                 signal(SIGQUIT, SIG_IGN);
86                 sleep(2);
87                 exit(dialit(num, acu) != 'A');
88         }
89         /*
90          * open line - will return on carrier
91          */
92         if ((FD = open(DV, O_RDWR)) < 0) {
93 #ifdef DEBUG
94                 printf("(after open, errno=%d)\n", errno);
95 #endif
96                 if (errno == EIO)
97                         printf("lost carrier...");
98                 else
99                         printf("dialup line open failed...");
100                 alarm(0);
101                 kill(child, SIGKILL);
102                 close(AC);
103                 return (0);
104         }
105         alarm(0);
106         signal(SIGALRM, SIG_DFL);
107         while ((pid = wait(&status)) != child && pid != -1)
108                 ;
109         if (status) {
110                 close(AC);
111                 return (0);
112         }
113         return (1);
114 }
115 
116 /*ARGSUSED*/
117 static void
118 alarmtr(int signo)
119 {
120         alarm(0);
121         longjmp(jmpbuf, 1);
122 }
123 
124 /*
125  * Insurance, for some reason we don't seem to be
126  *  hanging up...
127  */
128 void
129 v831_disconnect(void)
130 {
131 	struct termios	cntrl;
132 
133         sleep(2);
134 #ifdef DEBUG
135         printf("[disconnect: FD=%d]\n", FD);
136 #endif
137         if (FD > 0) {
138                 ioctl(FD, TIOCCDTR, 0);
139 		tcgetattr(FD, &cntrl);
140 		cfsetospeed(&cntrl, 0);
141 		cfsetispeed(&cntrl, 0);
142 		tcsetattr(FD, TCSAFLUSH, &cntrl);
143                 ioctl(FD, TIOCNXCL, NULL);
144         }
145         close(FD);
146 }
147 
148 void
149 v831_abort(void)
150 {
151 #ifdef DEBUG
152         printf("[abort: AC=%d]\n", AC);
153 #endif
154         sleep(2);
155         if (child > 0)
156                 kill(child, SIGKILL);
157         if (FD > 0)
158                 ioctl(FD, TIOCNXCL, NULL);
159         close(AC);
160         if (FD > 0)
161                 ioctl(FD, TIOCCDTR, 0);
162         close(FD);
163 }
164 
165 /*
166  * Sigh, this probably must be changed at each site.
167  */
168 struct vaconfig {
169 	char	*vc_name;
170 	char	vc_rack;
171 	char	vc_modem;
172 } vaconfig[] = {
173 	{ "/dev/cua0",'4','0' },
174 	{ "/dev/cua1",'4','1' },
175 	{ NULL, '\0', '\0' }
176 };
177 
178 #define pc(x)	(c = x, write(AC,&c,1))
179 #define ABORT	01
180 #define SI	017
181 #define STX	02
182 #define ETX	03
183 
184 static int
185 dialit(char *phonenum, char *acu)
186 {
187         struct vaconfig *vp;
188 	struct termios cntrl;
189         char c;
190         int i;
191 
192         phonenum = sanitize(phonenum);
193 #ifdef DEBUG
194         printf ("(dial phonenum=%s)\n", phonenum);
195 #endif
196         if (*phonenum == '<' && phonenum[1] == 0)
197                 return ('Z');
198 	for (vp = vaconfig; vp->vc_name; vp++)
199 		if (strcmp(vp->vc_name, acu) == 0)
200 			break;
201 	if (vp->vc_name == 0) {
202 		printf("Unable to locate dialer (%s)\n", acu);
203 		return ('K');
204 	}
205 	tcgetattr(AC, &cntrl);
206 	cfsetospeed(&cntrl, B2400);
207 	cfsetispeed(&cntrl, B2400);
208 	cntrl.c_cflag |= PARODD | PARENB;
209 	cntrl.c_lflag &= ~(ISIG | ICANON);
210 	tcsetattr(AC, TCSANOW, &cntrl);
211 	tcflush(AC, TCIOFLUSH);
212         pc(STX);
213 	pc(vp->vc_rack);
214 	pc(vp->vc_modem);
215 	while (*phonenum && *phonenum != '<')
216 		pc(*phonenum++);
217         pc(SI);
218 	pc(ETX);
219         sleep(1);
220         i = read(AC, &c, 1);
221 #ifdef DEBUG
222         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
223 #endif
224         if (i != 1)
225 		c = 'M';
226         if (c == 'B' || c == 'G') {
227                 char cc, oc = c;
228 
229                 pc(ABORT);
230                 read(AC, &cc, 1);
231 #ifdef DEBUG
232                 printf("abort response=%c\n", cc);
233 #endif
234                 c = oc;
235                 v831_disconnect();
236         }
237         close(AC);
238 #ifdef DEBUG
239         printf("dialit: returns %c\n", c);
240 #endif
241         return (c);
242 }
243 
244 static char *
245 sanitize(char *s)
246 {
247         static char buf[128];
248         char *cp;
249 
250         for (cp = buf; *s; s++) {
251 		if (!isdigit(*s) && *s == '<' && *s != '_')
252 			continue;
253 		if (*s == '_')
254 			*s = '=';
255 		*cp++ = *s;
256 	}
257         *cp++ = 0;
258         return (buf);
259 }
260