xref: /csrg-svn/usr.bin/tip/aculib/v831.c (revision 13153)
1*13153Sralph /*	v831.c	4.2	83/06/15	*/
213130Sralph 
3*13153Sralph #ifdef V831
413130Sralph /*
513130Sralph  * Routines for dialing up on Vadic 831
613130Sralph  */
7*13153Sralph #include <sys/file.h>
8*13153Sralph #include <sys/time.h>
9*13153Sralph 
1013130Sralph #include <setjmp.h>
1113130Sralph #include <errno.h>
1213130Sralph #include <sgtty.h>
1313130Sralph 
14*13153Sralph #include "tip.h"
1513130Sralph 
16*13153Sralph static char *sccsid = "@(#)v831.c	4.2 06/15/83";
1713130Sralph 
18*13153Sralph int	v831_abort();
19*13153Sralph int	alarmtr();
20*13153Sralph extern	errno;
2113130Sralph 
2213130Sralph static jmp_buf jmpbuf;
2313130Sralph static int child = -1;
2413130Sralph 
2513130Sralph v831_dialer(num, acu)
26*13153Sralph         char *num, *acu;
2713130Sralph {
28*13153Sralph         int status, pid, connected = 1;
29*13153Sralph         register int timelim;
3013130Sralph 
31*13153Sralph         if (boolean(value(VERBOSE)))
32*13153Sralph                 printf("\nstarting call...");
3313130Sralph #ifdef DEBUG
34*13153Sralph         printf ("(acu=%s)\n", acu);
3513130Sralph #endif
36*13153Sralph         if ((AC = open(acu, FRDWR)) < 0) {
37*13153Sralph                 if (errno == EBUSY)
38*13153Sralph                         printf("line busy...");
39*13153Sralph                 else
40*13153Sralph                         printf("acu open error...");
41*13153Sralph                 return (0);
42*13153Sralph         }
43*13153Sralph         if (setjmp(jmpbuf)) {
44*13153Sralph                 kill(child, SIGKILL);
45*13153Sralph                 close(AC);
46*13153Sralph                 return (0);
47*13153Sralph         }
48*13153Sralph         signal(SIGALRM, alarmtr);
49*13153Sralph         timelim = 5 * strlen(num);
50*13153Sralph         alarm(timelim < 30 ? 30 : timelim);
51*13153Sralph         if ((child = fork()) == 0) {
52*13153Sralph                 /*
53*13153Sralph                  * ignore this stuff for aborts
54*13153Sralph                  */
55*13153Sralph                 signal(SIGALRM, SIG_IGN);
5613130Sralph 		signal(SIGINT, SIG_IGN);
57*13153Sralph                 signal(SIGQUIT, SIG_IGN);
58*13153Sralph                 sleep(2);
59*13153Sralph                 exit(dialit(num, acu) != 'A');
60*13153Sralph         }
61*13153Sralph         /*
62*13153Sralph          * open line - will return on carrier
63*13153Sralph          */
64*13153Sralph         if ((FD = open(DV, 2)) < 0) {
6513130Sralph #ifdef DEBUG
66*13153Sralph                 printf("(after open, errno=%d)\n", errno);
6713130Sralph #endif
68*13153Sralph                 if (errno == EIO)
69*13153Sralph                         printf("lost carrier...");
70*13153Sralph                 else
71*13153Sralph                         printf("dialup line open failed...");
72*13153Sralph                 alarm(0);
73*13153Sralph                 kill(child, SIGKILL);
74*13153Sralph                 close(AC);
75*13153Sralph                 return (0);
76*13153Sralph         }
77*13153Sralph         alarm(0);
78*13153Sralph #ifdef notdef
79*13153Sralph         ioctl(AC, TIOCHPCL, 0);
80*13153Sralph #endif
81*13153Sralph         signal(SIGALRM, SIG_DFL);
82*13153Sralph         while ((pid = wait(&status)) != child && pid != -1)
83*13153Sralph                 ;
84*13153Sralph         if (status) {
85*13153Sralph                 close(AC);
86*13153Sralph                 return (0);
87*13153Sralph         }
88*13153Sralph         return (1);
8913130Sralph }
9013130Sralph 
9113130Sralph alarmtr()
9213130Sralph {
93*13153Sralph         alarm(0);
94*13153Sralph         longjmp(jmpbuf, 1);
9513130Sralph }
9613130Sralph 
9713130Sralph /*
9813130Sralph  * Insurance, for some reason we don't seem to be
9913130Sralph  *  hanging up...
10013130Sralph  */
10113130Sralph v831_disconnect()
10213130Sralph {
103*13153Sralph         struct sgttyb cntrl;
104*13153Sralph 
105*13153Sralph         sleep(2);
10613130Sralph #ifdef VMUNIX
10713130Sralph #ifdef DEBUG
108*13153Sralph         printf("[disconnect: FD=%d]\n", FD);
10913130Sralph #endif
110*13153Sralph         if (FD > 0) {
111*13153Sralph                 ioctl(FD, TIOCCDTR, 0);
112*13153Sralph                 ioctl(FD, TIOCGETP, &cntrl);
113*13153Sralph                 cntrl.sg_ispeed = cntrl.sg_ospeed = 0;
114*13153Sralph                 ioctl(FD, TIOCSETP, &cntrl);
115*13153Sralph                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
116*13153Sralph         }
11713130Sralph #endif
118*13153Sralph         close(FD);
11913130Sralph }
12013130Sralph 
12113130Sralph v831_abort()
12213130Sralph {
12313130Sralph #ifdef DEBUG
124*13153Sralph         printf("[abort: AC=%d]\n", AC);
12513130Sralph #endif
126*13153Sralph         sleep(2);
127*13153Sralph         if (child > 0)
128*13153Sralph                 kill(child, SIGKILL);
129*13153Sralph         if (AC > 0)
130*13153Sralph                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
131*13153Sralph                 close(AC);
13213130Sralph #ifdef VMUNIX
133*13153Sralph         if (FD > 0)
134*13153Sralph                 ioctl(FD, TIOCCDTR, 0);
13513130Sralph #endif
136*13153Sralph         close(FD);
13713130Sralph }
13813130Sralph #endif
13913130Sralph 
140*13153Sralph /*
141*13153Sralph  * Sigh, this probably must be changed at each site.
142*13153Sralph  */
143*13153Sralph struct vaconfig {
144*13153Sralph 	char	*vc_name;
145*13153Sralph 	char	vc_rack;
146*13153Sralph 	char	vc_modem;
147*13153Sralph } vaconfig[] = {
148*13153Sralph 	{ "/dev/cua0",'4','0' },
149*13153Sralph 	{ "/dev/cua1",'4','1' },
150*13153Sralph 	{ 0 }
151*13153Sralph };
152*13153Sralph 
153*13153Sralph #define pc(x)	(c = x, write(AC,&c,1))
154*13153Sralph #define ABORT	01
155*13153Sralph #define SI	017
156*13153Sralph #define STX	02
157*13153Sralph #define ETX	03
158*13153Sralph 
159*13153Sralph dialit(phonenum, acu)
160*13153Sralph 	register char *phonenum;
161*13153Sralph 	char *acu;
16213130Sralph {
163*13153Sralph         register struct vaconfig *vp;
164*13153Sralph 	struct sgttyb cntrl;
165*13153Sralph         char c, *sanitize();
166*13153Sralph         int i, two = 2;
16713130Sralph 
168*13153Sralph         phonenum = sanitize(phonenum);
16913130Sralph #ifdef DEBUG
170*13153Sralph         printf ("(dial phonenum=%s)\n", phonenum);
17113130Sralph #endif
172*13153Sralph         if (*phonenum == '<' && phonenum[1] == 0)
173*13153Sralph                 return ('Z');
174*13153Sralph 	for (vp = vaconfig; vp->vc_name; vp++)
175*13153Sralph 		if (strcmp(vp->vc_name, acu) == 0)
176*13153Sralph 			break;
177*13153Sralph 	if (vp->vc_name == 0) {
17813130Sralph 		printf("Unable to locate dialer (%s)\n", acu);
179*13153Sralph 		return ('K');
18013130Sralph 	}
181*13153Sralph         ioctl(AC, TIOCGETP, &cntrl);
182*13153Sralph         cntrl.sg_ispeed = cntrl.sg_ospeed = B2400;
183*13153Sralph         cntrl.sg_flags = RAW | EVENP | ODDP;
184*13153Sralph         ioctl(AC, TIOCSETP, &cntrl);
185*13153Sralph 	ioctl(AC, TIOCFLUSH, &two);
186*13153Sralph         pc(STX);
187*13153Sralph 	pc(vp->vc_rack);
188*13153Sralph 	pc(vp->vc_modem);
189*13153Sralph 	while (*phonenum && *phonenum != '<')
190*13153Sralph 		pc(*phonenum++);
191*13153Sralph         pc(SI);
192*13153Sralph 	pc(ETX);
193*13153Sralph         sleep(1);
194*13153Sralph         i = read(AC, &c, 1);
19513130Sralph #ifdef DEBUG
196*13153Sralph         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
19713130Sralph #endif
198*13153Sralph         if (i != 1)
199*13153Sralph 		c = 'M';
200*13153Sralph         if (c == 'B' || c == 'G') {
201*13153Sralph                 char cc, oc = c;
20213130Sralph 
203*13153Sralph                 pc(ABORT);
204*13153Sralph                 read(AC, &cc, 1);
20513130Sralph #ifdef DEBUG
206*13153Sralph                 printf("abort response=%c\n", cc);
20713130Sralph #endif
208*13153Sralph                 c = oc;
209*13153Sralph                 v831_disconnect();
210*13153Sralph         }
211*13153Sralph         close(AC);
21213130Sralph #ifdef DEBUG
213*13153Sralph         printf("dialit: returns %c\n", c);
21413130Sralph #endif
215*13153Sralph         return (c);
21613130Sralph }
217*13153Sralph 
218*13153Sralph static char *
219*13153Sralph sanitize(s)
220*13153Sralph 	register char *s;
22113130Sralph {
222*13153Sralph         static char buf[128];
223*13153Sralph         register char *cp;
224*13153Sralph 
225*13153Sralph         for (cp = buf; *s; s++) {
226*13153Sralph 		if (!isdigit(*s) && *s == '<' && *s != '_')
227*13153Sralph 			continue;
228*13153Sralph 		if (*s == '_')
229*13153Sralph 			*s = '=';
230*13153Sralph 		*cp++ = *s;
23113130Sralph 	}
232*13153Sralph         *cp++ = 0;
233*13153Sralph         return (buf);
23413130Sralph }
235