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