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