xref: /netbsd-src/usr.bin/tip/aculib/v831.c (revision 81b108b45f75f89f1e3ffad9fb6f074e771c0935)
1 /*	$NetBSD: v831.c,v 1.4 1995/10/29 00:50:02 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[] = "@(#)v831.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: v831.c,v 1.4 1995/10/29 00:50:02 pk 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 extern	int errno;
52 
53 static jmp_buf jmpbuf;
54 static int child = -1;
55 
56 v831_dialer(num, acu)
57         char *num, *acu;
58 {
59         int status, pid, connected = 1;
60         register int timelim;
61 	static int dialit();
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()
122 {
123         alarm(0);
124         longjmp(jmpbuf, 1);
125 }
126 
127 /*
128  * Insurance, for some reason we don't seem to be
129  *  hanging up...
130  */
131 v831_disconnect()
132 {
133 	struct termios	cntrl;
134 
135         sleep(2);
136 #ifdef DEBUG
137         printf("[disconnect: FD=%d]\n", FD);
138 #endif
139         if (FD > 0) {
140                 ioctl(FD, TIOCCDTR, 0);
141 		tcgetattr(FD, &cntrl);
142 		cfsetospeed(&cntrl, 0);
143 		cfsetispeed(&cntrl, 0);
144 		tcsetattr(FD, TCSAFLUSH, &cntrl);
145                 ioctl(FD, TIOCNXCL, NULL);
146         }
147         close(FD);
148 }
149 
150 v831_abort()
151 {
152 
153 #ifdef DEBUG
154         printf("[abort: AC=%d]\n", AC);
155 #endif
156         sleep(2);
157         if (child > 0)
158                 kill(child, SIGKILL);
159         if (AC > 0)
160                 ioctl(FD, TIOCNXCL, NULL);
161                 close(AC);
162         if (FD > 0)
163                 ioctl(FD, TIOCCDTR, 0);
164         close(FD);
165 }
166 
167 /*
168  * Sigh, this probably must be changed at each site.
169  */
170 struct vaconfig {
171 	char	*vc_name;
172 	char	vc_rack;
173 	char	vc_modem;
174 } vaconfig[] = {
175 	{ "/dev/cua0",'4','0' },
176 	{ "/dev/cua1",'4','1' },
177 	{ 0 }
178 };
179 
180 #define pc(x)	(c = x, write(AC,&c,1))
181 #define ABORT	01
182 #define SI	017
183 #define STX	02
184 #define ETX	03
185 
186 static int
187 dialit(phonenum, acu)
188 	register char *phonenum;
189 	char *acu;
190 {
191         register struct vaconfig *vp;
192 	struct termios cntrl;
193         char c;
194         int i, two = 2;
195 	static char *sanitize();
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