xref: /titanic_53/usr/src/cmd/tip/aculib/v831.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from UCB 4.5 6/25/83 */
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate /*
13*7c478bd9Sstevel@tonic-gate  * Routines for dialing up on Vadic 831
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate #include "tip.h"
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate int	v831_abort();
20*7c478bd9Sstevel@tonic-gate static	void alarmtr();
21*7c478bd9Sstevel@tonic-gate extern	errno;
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate static sigjmp_buf jmpbuf;
24*7c478bd9Sstevel@tonic-gate static int child = -1;
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate v831_dialer(num, acu)
27*7c478bd9Sstevel@tonic-gate 	char *num, *acu;
28*7c478bd9Sstevel@tonic-gate {
29*7c478bd9Sstevel@tonic-gate 	int status, pid, connected = 1;
30*7c478bd9Sstevel@tonic-gate 	register int timelim;
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 	if (boolean(value(VERBOSE)))
33*7c478bd9Sstevel@tonic-gate 		printf("\nstarting call...");
34*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
35*7c478bd9Sstevel@tonic-gate 	printf("(acu=%s)\n", acu);
36*7c478bd9Sstevel@tonic-gate #endif
37*7c478bd9Sstevel@tonic-gate 	if ((AC = open(acu, O_RDWR)) < 0) {
38*7c478bd9Sstevel@tonic-gate 		if (errno == EBUSY)
39*7c478bd9Sstevel@tonic-gate 			printf("line busy...");
40*7c478bd9Sstevel@tonic-gate 		else
41*7c478bd9Sstevel@tonic-gate 			printf("acu open error...");
42*7c478bd9Sstevel@tonic-gate 		return (0);
43*7c478bd9Sstevel@tonic-gate 	}
44*7c478bd9Sstevel@tonic-gate 	if (sigsetjmp(jmpbuf, 1)) {
45*7c478bd9Sstevel@tonic-gate 		kill(child, SIGKILL);
46*7c478bd9Sstevel@tonic-gate 		close(AC);
47*7c478bd9Sstevel@tonic-gate 		return (0);
48*7c478bd9Sstevel@tonic-gate 	}
49*7c478bd9Sstevel@tonic-gate 	signal(SIGALRM, alarmtr);
50*7c478bd9Sstevel@tonic-gate 	timelim = 5 * strlen(num);
51*7c478bd9Sstevel@tonic-gate 	alarm(timelim < 30 ? 30 : timelim);
52*7c478bd9Sstevel@tonic-gate 	if ((child = fork()) == 0) {
53*7c478bd9Sstevel@tonic-gate 		/*
54*7c478bd9Sstevel@tonic-gate 		 * ignore this stuff for aborts
55*7c478bd9Sstevel@tonic-gate 		 */
56*7c478bd9Sstevel@tonic-gate 		signal(SIGALRM, SIG_IGN);
57*7c478bd9Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
58*7c478bd9Sstevel@tonic-gate 		signal(SIGQUIT, SIG_IGN);
59*7c478bd9Sstevel@tonic-gate 		sleep(2);
60*7c478bd9Sstevel@tonic-gate 		exit(dialit(num, acu) != 'A');
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 	/*
63*7c478bd9Sstevel@tonic-gate 	 * open line - will return on carrier
64*7c478bd9Sstevel@tonic-gate 	 */
65*7c478bd9Sstevel@tonic-gate 	if ((FD = open(DV, O_RDWR)) < 0) {
66*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
67*7c478bd9Sstevel@tonic-gate 		printf("(after open, errno=%d)\n", errno);
68*7c478bd9Sstevel@tonic-gate #endif
69*7c478bd9Sstevel@tonic-gate 		if (errno == EIO)
70*7c478bd9Sstevel@tonic-gate 			printf("lost carrier...");
71*7c478bd9Sstevel@tonic-gate 		else
72*7c478bd9Sstevel@tonic-gate 			printf("dialup line open failed...");
73*7c478bd9Sstevel@tonic-gate 		alarm(0);
74*7c478bd9Sstevel@tonic-gate 		kill(child, SIGKILL);
75*7c478bd9Sstevel@tonic-gate 		close(AC);
76*7c478bd9Sstevel@tonic-gate 		return (0);
77*7c478bd9Sstevel@tonic-gate 	}
78*7c478bd9Sstevel@tonic-gate 	alarm(0);
79*7c478bd9Sstevel@tonic-gate 	signal(SIGALRM, SIG_DFL);
80*7c478bd9Sstevel@tonic-gate 	while ((pid = wait(&status)) != child && pid != -1)
81*7c478bd9Sstevel@tonic-gate 		;
82*7c478bd9Sstevel@tonic-gate 	if (status) {
83*7c478bd9Sstevel@tonic-gate 		close(AC);
84*7c478bd9Sstevel@tonic-gate 		return (0);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 	return (1);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static void
90*7c478bd9Sstevel@tonic-gate alarmtr()
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	alarm(0);
94*7c478bd9Sstevel@tonic-gate 	siglongjmp(jmpbuf, 1);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate /*
98*7c478bd9Sstevel@tonic-gate  * Insurance, for some reason we don't seem to be
99*7c478bd9Sstevel@tonic-gate  *  hanging up...
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate v831_disconnect()
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	struct termios cntrl;
104*7c478bd9Sstevel@tonic-gate 	int dtr = TIOCM_DTR;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	sleep(2);
107*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
108*7c478bd9Sstevel@tonic-gate 	printf("[disconnect: FD=%d]\n", FD);
109*7c478bd9Sstevel@tonic-gate #endif
110*7c478bd9Sstevel@tonic-gate 	if (FD > 0) {
111*7c478bd9Sstevel@tonic-gate 		ioctl(FD, TIOCMBIC, &dtr);
112*7c478bd9Sstevel@tonic-gate 		ioctl(FD, TCGETS, &cntrl);
113*7c478bd9Sstevel@tonic-gate 		cfsetospeed(&cntrl, B0);
114*7c478bd9Sstevel@tonic-gate 		cntrl.c_cflag &= ~XCLUDE;
115*7c478bd9Sstevel@tonic-gate 		ioctl(FD, TCSETSF, &cntrl);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 	close(FD);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate v831_abort()
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	int dtr = TIOCM_DTR;
123*7c478bd9Sstevel@tonic-gate 	struct termios buf;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
126*7c478bd9Sstevel@tonic-gate 	printf("[abort: AC=%d]\n", AC);
127*7c478bd9Sstevel@tonic-gate #endif
128*7c478bd9Sstevel@tonic-gate 	sleep(2);
129*7c478bd9Sstevel@tonic-gate 	if (child > 0)
130*7c478bd9Sstevel@tonic-gate 		kill(child, SIGKILL);
131*7c478bd9Sstevel@tonic-gate 	if (AC > 0) {
132*7c478bd9Sstevel@tonic-gate 		ioctl(FD, TCGETS, &buf);
133*7c478bd9Sstevel@tonic-gate 		buf.c_cflag &= ~XCLUDE;
134*7c478bd9Sstevel@tonic-gate 		ioctl(FD, TCSETSF, &buf);
135*7c478bd9Sstevel@tonic-gate 		close(AC);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	if (FD > 0)
138*7c478bd9Sstevel@tonic-gate 		ioctl(FD, TIOCMBIC, &dtr);
139*7c478bd9Sstevel@tonic-gate 	close(FD);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate /*
143*7c478bd9Sstevel@tonic-gate  * Sigh, this probably must be changed at each site.
144*7c478bd9Sstevel@tonic-gate  */
145*7c478bd9Sstevel@tonic-gate struct vaconfig {
146*7c478bd9Sstevel@tonic-gate 	char	*vc_name;
147*7c478bd9Sstevel@tonic-gate 	char	vc_rack;
148*7c478bd9Sstevel@tonic-gate 	char	vc_modem;
149*7c478bd9Sstevel@tonic-gate } vaconfig[] = {
150*7c478bd9Sstevel@tonic-gate 	{ "/dev/cua0", '4', '0' },
151*7c478bd9Sstevel@tonic-gate 	{ "/dev/cua1", '4', '1' },
152*7c478bd9Sstevel@tonic-gate 	{ 0 }
153*7c478bd9Sstevel@tonic-gate };
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate #define	pc(x)	(c = x, write(AC, &c, 1))
156*7c478bd9Sstevel@tonic-gate #define	ABORT	01
157*7c478bd9Sstevel@tonic-gate #define	SI	017
158*7c478bd9Sstevel@tonic-gate #define	STX	02
159*7c478bd9Sstevel@tonic-gate #define	ETX	03
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate static
162*7c478bd9Sstevel@tonic-gate dialit(phonenum, acu)
163*7c478bd9Sstevel@tonic-gate 	register char *phonenum;
164*7c478bd9Sstevel@tonic-gate 	char *acu;
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	register struct vaconfig *vp;
167*7c478bd9Sstevel@tonic-gate 	struct termios cntrl;
168*7c478bd9Sstevel@tonic-gate 	char c, *sanitize();
169*7c478bd9Sstevel@tonic-gate 	int i;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	phonenum = sanitize(phonenum);
172*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
173*7c478bd9Sstevel@tonic-gate 	printf("(dial phonenum=%s)\n", phonenum);
174*7c478bd9Sstevel@tonic-gate #endif
175*7c478bd9Sstevel@tonic-gate 	if (*phonenum == '<' && phonenum[1] == 0)
176*7c478bd9Sstevel@tonic-gate 		return ('Z');
177*7c478bd9Sstevel@tonic-gate 	for (vp = vaconfig; vp->vc_name; vp++)
178*7c478bd9Sstevel@tonic-gate 		if (strcmp(vp->vc_name, acu) == 0)
179*7c478bd9Sstevel@tonic-gate 			break;
180*7c478bd9Sstevel@tonic-gate 	if (vp->vc_name == 0) {
181*7c478bd9Sstevel@tonic-gate 		printf("Unable to locate dialer (%s)\n", acu);
182*7c478bd9Sstevel@tonic-gate 		return ('K');
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 	ioctl(AC, TCGETS, &cntrl);
185*7c478bd9Sstevel@tonic-gate 	cfsetospeed(&cntrl, B0);
186*7c478bd9Sstevel@tonic-gate 	cfsetispeed(&cntrl, B0);
187*7c478bd9Sstevel@tonic-gate 	cntrl.c_cflag &= ~(CSIZE|PARENB|PARODD);
188*7c478bd9Sstevel@tonic-gate 	cfsetospeed(&cntrl, B2400);
189*7c478bd9Sstevel@tonic-gate 	cntrl.c_cflag |= CS8;
190*7c478bd9Sstevel@tonic-gate 	cntrl.c_iflag &= IXOFF|IXANY;
191*7c478bd9Sstevel@tonic-gate 	cntrl.c_lflag &= ~(ICANON|ISIG);
192*7c478bd9Sstevel@tonic-gate 	cntrl.c_oflag = 0;
193*7c478bd9Sstevel@tonic-gate 	cntrl.c_cc[VMIN] = cntrl.c_cc[VTIME] = 0;
194*7c478bd9Sstevel@tonic-gate 	ioctl(AC, TCSETSF, &cntrl);
195*7c478bd9Sstevel@tonic-gate 	ioctl(AC, TCFLSH, TCOFLUSH);
196*7c478bd9Sstevel@tonic-gate 	pc(STX);
197*7c478bd9Sstevel@tonic-gate 	pc(vp->vc_rack);
198*7c478bd9Sstevel@tonic-gate 	pc(vp->vc_modem);
199*7c478bd9Sstevel@tonic-gate 	while (*phonenum && *phonenum != '<')
200*7c478bd9Sstevel@tonic-gate 		pc(*phonenum++);
201*7c478bd9Sstevel@tonic-gate 	pc(SI);
202*7c478bd9Sstevel@tonic-gate 	pc(ETX);
203*7c478bd9Sstevel@tonic-gate 	sleep(1);
204*7c478bd9Sstevel@tonic-gate 	i = read(AC, &c, 1);
205*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
206*7c478bd9Sstevel@tonic-gate 	printf("read %d chars, char=%c, errno %d\n", i, c, errno);
207*7c478bd9Sstevel@tonic-gate #endif
208*7c478bd9Sstevel@tonic-gate 	if (i != 1)
209*7c478bd9Sstevel@tonic-gate 		c = 'M';
210*7c478bd9Sstevel@tonic-gate 	if (c == 'B' || c == 'G') {
211*7c478bd9Sstevel@tonic-gate 		char cc, oc = c;
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 		pc(ABORT);
214*7c478bd9Sstevel@tonic-gate 		read(AC, &cc, 1);
215*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
216*7c478bd9Sstevel@tonic-gate 		printf("abort response=%c\n", cc);
217*7c478bd9Sstevel@tonic-gate #endif
218*7c478bd9Sstevel@tonic-gate 		c = oc;
219*7c478bd9Sstevel@tonic-gate 		v831_disconnect();
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate 	close(AC);
222*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
223*7c478bd9Sstevel@tonic-gate 	printf("dialit: returns %c\n", c);
224*7c478bd9Sstevel@tonic-gate #endif
225*7c478bd9Sstevel@tonic-gate 	return (c);
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate static char *
229*7c478bd9Sstevel@tonic-gate sanitize(s)
230*7c478bd9Sstevel@tonic-gate 	register char *s;
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate 	static char buf[128];
233*7c478bd9Sstevel@tonic-gate 	register char *cp;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	for (cp = buf; *s; s++) {
236*7c478bd9Sstevel@tonic-gate 		if (!isdigit(*s) && *s == '<' && *s != '_')
237*7c478bd9Sstevel@tonic-gate 			continue;
238*7c478bd9Sstevel@tonic-gate 		if (*s == '_')
239*7c478bd9Sstevel@tonic-gate 			*s = '=';
240*7c478bd9Sstevel@tonic-gate 		*cp++ = *s;
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate 	*cp++ = 0;
243*7c478bd9Sstevel@tonic-gate 	return (buf);
244*7c478bd9Sstevel@tonic-gate }
245