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