xref: /csrg-svn/usr.bin/tip/aculib/v831.c (revision 62317)
119817Sdist /*
2*62317Sbostic  * Copyright (c) 1983, 1993
3*62317Sbostic  *	The Regents of the University of California.  All rights reserved.
435492Sbostic  *
542770Sbostic  * %sccs.include.redist.c%
619817Sdist  */
719817Sdist 
813280Ssam #ifndef lint
9*62317Sbostic static char sccsid[] = "@(#)v831.c	8.1 (Berkeley) 06/06/93";
1035492Sbostic #endif /* not lint */
1113130Sralph 
1213130Sralph /*
1313130Sralph  * Routines for dialing up on Vadic 831
1413130Sralph  */
1513153Sralph #include "tip.h"
1613130Sralph 
1713153Sralph int	v831_abort();
1846867Sbostic static	void alarmtr();
1946867Sbostic extern	int errno;
2013130Sralph 
2113130Sralph static jmp_buf jmpbuf;
2213130Sralph static int child = -1;
2313130Sralph 
v831_dialer(num,acu)2413130Sralph v831_dialer(num, acu)
2513153Sralph         char *num, *acu;
2613130Sralph {
2713153Sralph         int status, pid, connected = 1;
2813153Sralph         register int timelim;
2946867Sbostic 	static int dialit();
3013130Sralph 
3113153Sralph         if (boolean(value(VERBOSE)))
3213153Sralph                 printf("\nstarting call...");
3313130Sralph #ifdef DEBUG
3413153Sralph         printf ("(acu=%s)\n", acu);
3513130Sralph #endif
3613186Ssam         if ((AC = open(acu, O_RDWR)) < 0) {
3713153Sralph                 if (errno == EBUSY)
3813153Sralph                         printf("line busy...");
3913153Sralph                 else
4013153Sralph                         printf("acu open error...");
4113153Sralph                 return (0);
4213153Sralph         }
4313153Sralph         if (setjmp(jmpbuf)) {
4413153Sralph                 kill(child, SIGKILL);
4513153Sralph                 close(AC);
4613153Sralph                 return (0);
4713153Sralph         }
4813153Sralph         signal(SIGALRM, alarmtr);
4913153Sralph         timelim = 5 * strlen(num);
5013153Sralph         alarm(timelim < 30 ? 30 : timelim);
5113153Sralph         if ((child = fork()) == 0) {
5213153Sralph                 /*
5313153Sralph                  * ignore this stuff for aborts
5413153Sralph                  */
5513153Sralph                 signal(SIGALRM, SIG_IGN);
5613130Sralph 		signal(SIGINT, SIG_IGN);
5713153Sralph                 signal(SIGQUIT, SIG_IGN);
5813153Sralph                 sleep(2);
5913153Sralph                 exit(dialit(num, acu) != 'A');
6013153Sralph         }
6113153Sralph         /*
6213153Sralph          * open line - will return on carrier
6313153Sralph          */
6413186Ssam         if ((FD = open(DV, O_RDWR)) < 0) {
6513130Sralph #ifdef DEBUG
6613153Sralph                 printf("(after open, errno=%d)\n", errno);
6713130Sralph #endif
6813153Sralph                 if (errno == EIO)
6913153Sralph                         printf("lost carrier...");
7013153Sralph                 else
7113153Sralph                         printf("dialup line open failed...");
7213153Sralph                 alarm(0);
7313153Sralph                 kill(child, SIGKILL);
7413153Sralph                 close(AC);
7513153Sralph                 return (0);
7613153Sralph         }
7713153Sralph         alarm(0);
7813153Sralph #ifdef notdef
7913153Sralph         ioctl(AC, TIOCHPCL, 0);
8013153Sralph #endif
8113153Sralph         signal(SIGALRM, SIG_DFL);
8213153Sralph         while ((pid = wait(&status)) != child && pid != -1)
8313153Sralph                 ;
8413153Sralph         if (status) {
8513153Sralph                 close(AC);
8613153Sralph                 return (0);
8713153Sralph         }
8813153Sralph         return (1);
8913130Sralph }
9013130Sralph 
9146867Sbostic static void
alarmtr()9213130Sralph alarmtr()
9313130Sralph {
9413153Sralph         alarm(0);
9513153Sralph         longjmp(jmpbuf, 1);
9613130Sralph }
9713130Sralph 
9813130Sralph /*
9913130Sralph  * Insurance, for some reason we don't seem to be
10013130Sralph  *  hanging up...
10113130Sralph  */
v831_disconnect()10213130Sralph v831_disconnect()
10313130Sralph {
10413153Sralph         struct sgttyb cntrl;
10513153Sralph 
10613153Sralph         sleep(2);
10713130Sralph #ifdef DEBUG
10813153Sralph         printf("[disconnect: FD=%d]\n", FD);
10913130Sralph #endif
11013153Sralph         if (FD > 0) {
11113153Sralph                 ioctl(FD, TIOCCDTR, 0);
11213153Sralph                 ioctl(FD, TIOCGETP, &cntrl);
11313153Sralph                 cntrl.sg_ispeed = cntrl.sg_ospeed = 0;
11413153Sralph                 ioctl(FD, TIOCSETP, &cntrl);
11513153Sralph                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
11613153Sralph         }
11713153Sralph         close(FD);
11813130Sralph }
11913130Sralph 
v831_abort()12013130Sralph v831_abort()
12113130Sralph {
12213186Ssam 
12313130Sralph #ifdef DEBUG
12413153Sralph         printf("[abort: AC=%d]\n", AC);
12513130Sralph #endif
12613153Sralph         sleep(2);
12713153Sralph         if (child > 0)
12813153Sralph                 kill(child, SIGKILL);
12913153Sralph         if (AC > 0)
13013153Sralph                 ioctl(FD, TIOCNXCL, (struct sgttyb *)NULL);
13113153Sralph                 close(AC);
13213153Sralph         if (FD > 0)
13313153Sralph                 ioctl(FD, TIOCCDTR, 0);
13413153Sralph         close(FD);
13513130Sralph }
13613130Sralph 
13713153Sralph /*
13813153Sralph  * Sigh, this probably must be changed at each site.
13913153Sralph  */
14013153Sralph struct vaconfig {
14113153Sralph 	char	*vc_name;
14213153Sralph 	char	vc_rack;
14313153Sralph 	char	vc_modem;
14413153Sralph } vaconfig[] = {
14513153Sralph 	{ "/dev/cua0",'4','0' },
14613153Sralph 	{ "/dev/cua1",'4','1' },
14713153Sralph 	{ 0 }
14813153Sralph };
14913153Sralph 
15013153Sralph #define pc(x)	(c = x, write(AC,&c,1))
15113153Sralph #define ABORT	01
15213153Sralph #define SI	017
15313153Sralph #define STX	02
15413153Sralph #define ETX	03
15513153Sralph 
15646867Sbostic static int
dialit(phonenum,acu)15713153Sralph dialit(phonenum, acu)
15813153Sralph 	register char *phonenum;
15913153Sralph 	char *acu;
16013130Sralph {
16113153Sralph         register struct vaconfig *vp;
16213153Sralph 	struct sgttyb cntrl;
16346867Sbostic         char c;
16413153Sralph         int i, two = 2;
16546867Sbostic 	static char *sanitize();
16613130Sralph 
16713153Sralph         phonenum = sanitize(phonenum);
16813130Sralph #ifdef DEBUG
16913153Sralph         printf ("(dial phonenum=%s)\n", phonenum);
17013130Sralph #endif
17113153Sralph         if (*phonenum == '<' && phonenum[1] == 0)
17213153Sralph                 return ('Z');
17313153Sralph 	for (vp = vaconfig; vp->vc_name; vp++)
17413153Sralph 		if (strcmp(vp->vc_name, acu) == 0)
17513153Sralph 			break;
17613153Sralph 	if (vp->vc_name == 0) {
17713130Sralph 		printf("Unable to locate dialer (%s)\n", acu);
17813153Sralph 		return ('K');
17913130Sralph 	}
18013153Sralph         ioctl(AC, TIOCGETP, &cntrl);
18113153Sralph         cntrl.sg_ispeed = cntrl.sg_ospeed = B2400;
18213153Sralph         cntrl.sg_flags = RAW | EVENP | ODDP;
18313153Sralph         ioctl(AC, TIOCSETP, &cntrl);
18413153Sralph 	ioctl(AC, TIOCFLUSH, &two);
18513153Sralph         pc(STX);
18613153Sralph 	pc(vp->vc_rack);
18713153Sralph 	pc(vp->vc_modem);
18813153Sralph 	while (*phonenum && *phonenum != '<')
18913153Sralph 		pc(*phonenum++);
19013153Sralph         pc(SI);
19113153Sralph 	pc(ETX);
19213153Sralph         sleep(1);
19313153Sralph         i = read(AC, &c, 1);
19413130Sralph #ifdef DEBUG
19513153Sralph         printf("read %d chars, char=%c, errno %d\n", i, c, errno);
19613130Sralph #endif
19713153Sralph         if (i != 1)
19813153Sralph 		c = 'M';
19913153Sralph         if (c == 'B' || c == 'G') {
20013153Sralph                 char cc, oc = c;
20113130Sralph 
20213153Sralph                 pc(ABORT);
20313153Sralph                 read(AC, &cc, 1);
20413130Sralph #ifdef DEBUG
20513153Sralph                 printf("abort response=%c\n", cc);
20613130Sralph #endif
20713153Sralph                 c = oc;
20813153Sralph                 v831_disconnect();
20913153Sralph         }
21013153Sralph         close(AC);
21113130Sralph #ifdef DEBUG
21213153Sralph         printf("dialit: returns %c\n", c);
21313130Sralph #endif
21413153Sralph         return (c);
21513130Sralph }
21613153Sralph 
21713153Sralph static char *
sanitize(s)21813153Sralph sanitize(s)
21913153Sralph 	register char *s;
22013130Sralph {
22113153Sralph         static char buf[128];
22213153Sralph         register char *cp;
22313153Sralph 
22413153Sralph         for (cp = buf; *s; s++) {
22513153Sralph 		if (!isdigit(*s) && *s == '<' && *s != '_')
22613153Sralph 			continue;
22713153Sralph 		if (*s == '_')
22813153Sralph 			*s = '=';
22913153Sralph 		*cp++ = *s;
23013130Sralph 	}
23113153Sralph         *cp++ = 0;
23213153Sralph         return (buf);
23313130Sralph }
234