xref: /csrg-svn/usr.bin/tip/aculib/v831.c (revision 13280)
1 #ifndef lint
2 static char sccsid[] = "@(#)v831.c	4.4 (Berkeley) 06/25/83";
3 #endif
4 
5 #ifdef V831
6 /*
7  * Routines for dialing up on Vadic 831
8  */
9 #include <sys/time.h>
10 
11 #include "tip.h"
12 
13 int	v831_abort();
14 static	int alarmtr();
15 extern	errno;
16 
17 static jmp_buf jmpbuf;
18 static int child = -1;
19 
20 v831_dialer(num, acu)
21         char *num, *acu;
22 {
23         int status, pid, connected = 1;
24         register int timelim;
25 
26         if (boolean(value(VERBOSE)))
27                 printf("\nstarting call...");
28 #ifdef DEBUG
29         printf ("(acu=%s)\n", acu);
30 #endif
31         if ((AC = open(acu, O_RDWR)) < 0) {
32                 if (errno == EBUSY)
33                         printf("line busy...");
34                 else
35                         printf("acu open error...");
36                 return (0);
37         }
38         if (setjmp(jmpbuf)) {
39                 kill(child, SIGKILL);
40                 close(AC);
41                 return (0);
42         }
43         signal(SIGALRM, alarmtr);
44         timelim = 5 * strlen(num);
45         alarm(timelim < 30 ? 30 : timelim);
46         if ((child = fork()) == 0) {
47                 /*
48                  * ignore this stuff for aborts
49                  */
50                 signal(SIGALRM, SIG_IGN);
51 		signal(SIGINT, SIG_IGN);
52                 signal(SIGQUIT, SIG_IGN);
53                 sleep(2);
54                 exit(dialit(num, acu) != 'A');
55         }
56         /*
57          * open line - will return on carrier
58          */
59         if ((FD = open(DV, O_RDWR)) < 0) {
60 #ifdef DEBUG
61                 printf("(after open, errno=%d)\n", errno);
62 #endif
63                 if (errno == EIO)
64                         printf("lost carrier...");
65                 else
66                         printf("dialup line open failed...");
67                 alarm(0);
68                 kill(child, SIGKILL);
69                 close(AC);
70                 return (0);
71         }
72         alarm(0);
73 #ifdef notdef
74         ioctl(AC, TIOCHPCL, 0);
75 #endif
76         signal(SIGALRM, SIG_DFL);
77         while ((pid = wait(&status)) != child && pid != -1)
78                 ;
79         if (status) {
80                 close(AC);
81                 return (0);
82         }
83         return (1);
84 }
85 
86 static
87 alarmtr()
88 {
89 
90         alarm(0);
91         longjmp(jmpbuf, 1);
92 }
93 
94 /*
95  * Insurance, for some reason we don't seem to be
96  *  hanging up...
97  */
98 v831_disconnect()
99 {
100         struct sgttyb cntrl;
101 
102         sleep(2);
103 #ifdef DEBUG
104         printf("[disconnect: FD=%d]\n", FD);
105 #endif
106         if (FD > 0) {
107                 ioctl(FD, TIOCCDTR, 0);
108                 ioctl(FD, TIOCGETP, &cntrl);
109                 cntrl.sg_ispeed = cntrl.sg_ospeed = 0;
110                 ioctl(FD, TIOCSETP, &cntrl);
111                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
112         }
113         close(FD);
114 }
115 
116 v831_abort()
117 {
118 
119 #ifdef DEBUG
120         printf("[abort: AC=%d]\n", AC);
121 #endif
122         sleep(2);
123         if (child > 0)
124                 kill(child, SIGKILL);
125         if (AC > 0)
126                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
127                 close(AC);
128         if (FD > 0)
129                 ioctl(FD, TIOCCDTR, 0);
130         close(FD);
131 }
132 #endif
133 
134 /*
135  * Sigh, this probably must be changed at each site.
136  */
137 struct vaconfig {
138 	char	*vc_name;
139 	char	vc_rack;
140 	char	vc_modem;
141 } vaconfig[] = {
142 	{ "/dev/cua0",'4','0' },
143 	{ "/dev/cua1",'4','1' },
144 	{ 0 }
145 };
146 
147 #define pc(x)	(c = x, write(AC,&c,1))
148 #define ABORT	01
149 #define SI	017
150 #define STX	02
151 #define ETX	03
152 
153 static
154 dialit(phonenum, acu)
155 	register char *phonenum;
156 	char *acu;
157 {
158         register struct vaconfig *vp;
159 	struct sgttyb cntrl;
160         char c, *sanitize();
161         int i, two = 2;
162 
163         phonenum = sanitize(phonenum);
164 #ifdef DEBUG
165         printf ("(dial phonenum=%s)\n", phonenum);
166 #endif
167         if (*phonenum == '<' && phonenum[1] == 0)
168                 return ('Z');
169 	for (vp = vaconfig; vp->vc_name; vp++)
170 		if (strcmp(vp->vc_name, acu) == 0)
171 			break;
172 	if (vp->vc_name == 0) {
173 		printf("Unable to locate dialer (%s)\n", acu);
174 		return ('K');
175 	}
176         ioctl(AC, TIOCGETP, &cntrl);
177         cntrl.sg_ispeed = cntrl.sg_ospeed = B2400;
178         cntrl.sg_flags = RAW | EVENP | ODDP;
179         ioctl(AC, TIOCSETP, &cntrl);
180 	ioctl(AC, TIOCFLUSH, &two);
181         pc(STX);
182 	pc(vp->vc_rack);
183 	pc(vp->vc_modem);
184 	while (*phonenum && *phonenum != '<')
185 		pc(*phonenum++);
186         pc(SI);
187 	pc(ETX);
188         sleep(1);
189         i = read(AC, &c, 1);
190 #ifdef DEBUG
191         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
192 #endif
193         if (i != 1)
194 		c = 'M';
195         if (c == 'B' || c == 'G') {
196                 char cc, oc = c;
197 
198                 pc(ABORT);
199                 read(AC, &cc, 1);
200 #ifdef DEBUG
201                 printf("abort response=%c\n", cc);
202 #endif
203                 c = oc;
204                 v831_disconnect();
205         }
206         close(AC);
207 #ifdef DEBUG
208         printf("dialit: returns %c\n", c);
209 #endif
210         return (c);
211 }
212 
213 static char *
214 sanitize(s)
215 	register char *s;
216 {
217         static char buf[128];
218         register char *cp;
219 
220         for (cp = buf; *s; s++) {
221 		if (!isdigit(*s) && *s == '<' && *s != '_')
222 			continue;
223 		if (*s == '_')
224 			*s = '=';
225 		*cp++ = *s;
226 	}
227         *cp++ = 0;
228         return (buf);
229 }
230