xref: /netbsd-src/usr.bin/tip/aculib/v831.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: v831.c,v 1.3 1994/12/08 09:31:50 jtc 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.3 1994/12/08 09:31:50 jtc Exp $";
41 #endif /* not lint */
42 
43 /*
44  * Routines for dialing up on Vadic 831
45  */
46 #include "tip.h"
47 
48 int	v831_abort();
49 static	void alarmtr();
50 extern	int errno;
51 
52 static jmp_buf jmpbuf;
53 static int child = -1;
54 
55 v831_dialer(num, acu)
56         char *num, *acu;
57 {
58         int status, pid, connected = 1;
59         register int timelim;
60 	static int dialit();
61 
62         if (boolean(value(VERBOSE)))
63                 printf("\nstarting call...");
64 #ifdef DEBUG
65         printf ("(acu=%s)\n", acu);
66 #endif
67         if ((AC = open(acu, O_RDWR)) < 0) {
68                 if (errno == EBUSY)
69                         printf("line busy...");
70                 else
71                         printf("acu open error...");
72                 return (0);
73         }
74         if (setjmp(jmpbuf)) {
75                 kill(child, SIGKILL);
76                 close(AC);
77                 return (0);
78         }
79         signal(SIGALRM, alarmtr);
80         timelim = 5 * strlen(num);
81         alarm(timelim < 30 ? 30 : timelim);
82         if ((child = fork()) == 0) {
83                 /*
84                  * ignore this stuff for aborts
85                  */
86                 signal(SIGALRM, SIG_IGN);
87 		signal(SIGINT, SIG_IGN);
88                 signal(SIGQUIT, SIG_IGN);
89                 sleep(2);
90                 exit(dialit(num, acu) != 'A');
91         }
92         /*
93          * open line - will return on carrier
94          */
95         if ((FD = open(DV, O_RDWR)) < 0) {
96 #ifdef DEBUG
97                 printf("(after open, errno=%d)\n", errno);
98 #endif
99                 if (errno == EIO)
100                         printf("lost carrier...");
101                 else
102                         printf("dialup line open failed...");
103                 alarm(0);
104                 kill(child, SIGKILL);
105                 close(AC);
106                 return (0);
107         }
108         alarm(0);
109 #ifdef notdef
110         ioctl(AC, TIOCHPCL, 0);
111 #endif
112         signal(SIGALRM, SIG_DFL);
113         while ((pid = wait(&status)) != child && pid != -1)
114                 ;
115         if (status) {
116                 close(AC);
117                 return (0);
118         }
119         return (1);
120 }
121 
122 static void
123 alarmtr()
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 v831_disconnect()
134 {
135         struct sgttyb 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                 ioctl(FD, TIOCGETP, &cntrl);
144                 cntrl.sg_ispeed = cntrl.sg_ospeed = 0;
145                 ioctl(FD, TIOCSETP, &cntrl);
146                 ioctl(FD, TIOCNXCL, (struct sgttyb *)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, (struct sgttyb *)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 sgttyb cntrl;
194         char c;
195         int i, two = 2;
196 	static char *sanitize();
197 
198         phonenum = sanitize(phonenum);
199 #ifdef DEBUG
200         printf ("(dial phonenum=%s)\n", phonenum);
201 #endif
202         if (*phonenum == '<' && phonenum[1] == 0)
203                 return ('Z');
204 	for (vp = vaconfig; vp->vc_name; vp++)
205 		if (strcmp(vp->vc_name, acu) == 0)
206 			break;
207 	if (vp->vc_name == 0) {
208 		printf("Unable to locate dialer (%s)\n", acu);
209 		return ('K');
210 	}
211         ioctl(AC, TIOCGETP, &cntrl);
212         cntrl.sg_ispeed = cntrl.sg_ospeed = B2400;
213         cntrl.sg_flags = RAW | EVENP | ODDP;
214         ioctl(AC, TIOCSETP, &cntrl);
215 	ioctl(AC, TIOCFLUSH, &two);
216         pc(STX);
217 	pc(vp->vc_rack);
218 	pc(vp->vc_modem);
219 	while (*phonenum && *phonenum != '<')
220 		pc(*phonenum++);
221         pc(SI);
222 	pc(ETX);
223         sleep(1);
224         i = read(AC, &c, 1);
225 #ifdef DEBUG
226         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
227 #endif
228         if (i != 1)
229 		c = 'M';
230         if (c == 'B' || c == 'G') {
231                 char cc, oc = c;
232 
233                 pc(ABORT);
234                 read(AC, &cc, 1);
235 #ifdef DEBUG
236                 printf("abort response=%c\n", cc);
237 #endif
238                 c = oc;
239                 v831_disconnect();
240         }
241         close(AC);
242 #ifdef DEBUG
243         printf("dialit: returns %c\n", c);
244 #endif
245         return (c);
246 }
247 
248 static char *
249 sanitize(s)
250 	register char *s;
251 {
252         static char buf[128];
253         register char *cp;
254 
255         for (cp = buf; *s; s++) {
256 		if (!isdigit(*s) && *s == '<' && *s != '_')
257 			continue;
258 		if (*s == '_')
259 			*s = '=';
260 		*cp++ = *s;
261 	}
262         *cp++ = 0;
263         return (buf);
264 }
265