xref: /original-bsd/usr.bin/tip/aculib/v3451.c (revision abfc216a)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)v3451.c	5.5 (Berkeley) 03/02/91";
10 #endif /* not lint */
11 
12 /*
13  * Routines for calling up on a Vadic 3451 Modem
14  */
15 #include "tip.h"
16 
17 static	jmp_buf Sjbuf;
18 
19 v3451_dialer(num, acu)
20 	register char *num;
21 	char *acu;
22 {
23 	sig_t func;
24 	int ok;
25 	int slow = number(value(BAUDRATE)) < 1200, rw = 2;
26 	char phone[50];
27 #ifdef ACULOG
28 	char line[80];
29 #endif
30 	static int expect();
31 	static void vawrite();
32 
33 	/*
34 	 * Get in synch
35 	 */
36 	vawrite("I\r", 1 + slow);
37 	vawrite("I\r", 1 + slow);
38 	vawrite("I\r", 1 + slow);
39 	vawrite("\005\r", 2 + slow);
40 	if (!expect("READY")) {
41 		printf("can't synchronize with vadic 3451\n");
42 #ifdef ACULOG
43 		logent(value(HOST), num, "vadic", "can't synch up");
44 #endif
45 		return (0);
46 	}
47 	ioctl(FD, TIOCHPCL, 0);
48 	sleep(1);
49 	vawrite("D\r", 2 + slow);
50 	if (!expect("NUMBER?")) {
51 		printf("Vadic will not accept dial command\n");
52 #ifdef ACULOG
53 		logent(value(HOST), num, "vadic", "will not accept dial");
54 #endif
55 		return (0);
56 	}
57 	strcpy(phone, num);
58 	strcat(phone, "\r");
59 	vawrite(phone, 1 + slow);
60 	if (!expect(phone)) {
61 		printf("Vadic will not accept phone number\n");
62 #ifdef ACULOG
63 		logent(value(HOST), num, "vadic", "will not accept number");
64 #endif
65 		return (0);
66 	}
67 	func = signal(SIGINT,SIG_IGN);
68 	/*
69 	 * You cannot interrupt the Vadic when its dialing;
70 	 * even dropping DTR does not work (definitely a
71 	 * brain damaged design).
72 	 */
73 	vawrite("\r", 1 + slow);
74 	vawrite("\r", 1 + slow);
75 	if (!expect("DIALING:")) {
76 		printf("Vadic failed to dial\n");
77 #ifdef ACULOG
78 		logent(value(HOST), num, "vadic", "failed to dial");
79 #endif
80 		return (0);
81 	}
82 	if (boolean(value(VERBOSE)))
83 		printf("\ndialing...");
84 	ok = expect("ON LINE");
85 	signal(SIGINT, func);
86 	if (!ok) {
87 		printf("call failed\n");
88 #ifdef ACULOG
89 		logent(value(HOST), num, "vadic", "call failed");
90 #endif
91 		return (0);
92 	}
93 	ioctl(FD, TIOCFLUSH, &rw);
94 	return (1);
95 }
96 
97 v3451_disconnect()
98 {
99 
100 	close(FD);
101 }
102 
103 v3451_abort()
104 {
105 
106 	close(FD);
107 }
108 
109 static void
110 vawrite(cp, delay)
111 	register char *cp;
112 	int delay;
113 {
114 
115 	for (; *cp; sleep(delay), cp++)
116 		write(FD, cp, 1);
117 }
118 
119 static
120 expect(cp)
121 	register char *cp;
122 {
123 	char buf[300];
124 	register char *rp = buf;
125 	int timeout = 30, online = 0;
126 	static int notin();
127 	static void alarmtr();
128 
129 	if (strcmp(cp, "\"\"") == 0)
130 		return (1);
131 	*rp = 0;
132 	/*
133 	 * If we are waiting for the Vadic to complete
134 	 * dialing and get a connection, allow more time
135 	 * Unfortunately, the Vadic times out 24 seconds after
136 	 * the last digit is dialed
137 	 */
138 	online = strcmp(cp, "ON LINE") == 0;
139 	if (online)
140 		timeout = number(value(DIALTIMEOUT));
141 	signal(SIGALRM, alarmtr);
142 	if (setjmp(Sjbuf))
143 		return (0);
144 	alarm(timeout);
145 	while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
146 		if (online && notin("FAILED CALL", buf) == 0)
147 			return (0);
148 		if (read(FD, rp, 1) < 0) {
149 			alarm(0);
150 			return (0);
151 		}
152 		if (*rp &= 0177)
153 			rp++;
154 		*rp = '\0';
155 	}
156 	alarm(0);
157 	return (1);
158 }
159 
160 static void
161 alarmtr()
162 {
163 	longjmp(Sjbuf, 1);
164 }
165 
166 static int
167 notin(sh, lg)
168 	char *sh, *lg;
169 {
170 	static int prefix();
171 
172 	for (; *lg; lg++)
173 		if (prefix(sh, lg))
174 			return (0);
175 	return (1);
176 }
177 
178 static
179 prefix(s1, s2)
180 	register char *s1, *s2;
181 {
182 	register char c;
183 
184 	while ((c = *s1++) == *s2++)
185 		if (c == '\0')
186 			return (1);
187 	return (c == '\0');
188 }
189