dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / tip / aculib / biz31.c
blob5dba92d65e6489752bed710f9bab385ae13fc43e
1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
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.
12 #pragma ident "%Z%%M% %I% %E% SMI"
14 #include "tip.h"
16 #define MAXRETRY 3 /* sync up retry count */
17 #define DISCONNECT_CMD "\21\25\11\24" /* disconnection string */
19 static int detect(char *);
20 static int bizsync(int);
21 static void echo(char *);
22 static void flush(char *);
23 static void sigALRM(void);
24 static int timeout = 0;
25 static sigjmp_buf timeoutbuf;
27 void biz31_disconnect(void);
30 * Dial up on a BIZCOMP Model 1031 with either
31 * tone dialing (mod = "f")
32 * pulse dialing (mod = "w")
34 static int
35 biz_dialer(char *num, char *mod)
37 int connected = 0;
39 if (!bizsync(FD)) {
40 logent(value(HOST), "", "biz", "out of sync");
41 (void) printf("bizcomp out of sync\n");
42 delock(uucplock);
43 exit(0);
45 if (boolean(value(VERBOSE)))
46 (void) printf("\nstarting call...");
47 echo("#\rk$\r$\n"); /* disable auto-answer */
48 echo("$>$.$ #\r"); /* tone/pulse dialing */
49 echo(mod);
50 echo("$\r$\n");
51 echo("$>$.$ #\re$ "); /* disconnection sequence */
52 echo(DISCONNECT_CMD);
53 echo("\r$\n$\r$\n");
54 echo("$>$.$ #\rr$ "); /* repeat dial */
55 echo(num);
56 echo("\r$\n");
57 if (boolean(value(VERBOSE)))
58 (void) printf("ringing...");
60 * The reply from the BIZCOMP should be:
61 * `^G NO CONNECTION\r\n^G\r\n' failure
62 * ` CONNECTION\r\n^G' success
64 connected = detect(" ");
65 #ifdef ACULOG
66 if (timeout) {
67 char line[80];
69 (void) sprintf(line, "%d second dial timeout",
70 number(value(DIALTIMEOUT)));
71 logent(value(HOST), num, "biz", line);
73 #endif
74 if (!connected)
75 flush(" NO CONNECTION\r\n\07\r\n");
76 else
77 flush("CONNECTION\r\n\07");
78 if (timeout)
79 biz31_disconnect(); /* insurance */
80 return (connected);
83 /* ARGSUSED */
84 int
85 biz31w_dialer(char *num, char *acu)
88 return (biz_dialer(num, "w"));
91 /* ARGSUSED */
92 int
93 biz31f_dialer(char *num, char *acu)
96 return (biz_dialer(num, "f"));
99 void
100 biz31_disconnect(void)
103 (void) write(FD, DISCONNECT_CMD, 4);
104 (void) sleep(2);
105 (void) ioctl(FD, TCFLSH, TCOFLUSH);
108 void
109 biz31_abort(void)
112 (void) write(FD, "\33", 1);
115 static void
116 echo(char *s)
118 char c;
120 while (c = *s++) {
121 switch (c) {
122 case '$':
123 (void) read(FD, &c, 1);
124 s++;
125 break;
127 case '#':
128 c = *s++;
129 (void) write(FD, &c, 1);
130 break;
132 default:
133 (void) write(FD, &c, 1);
134 (void) read(FD, &c, 1);
139 static void
140 sigALRM(void)
143 timeout = 1;
144 siglongjmp(timeoutbuf, 1);
147 static int
148 detect(char *s)
150 char c;
151 sig_handler_t f;
153 f = signal(SIGALRM, (sig_handler_t)sigALRM);
154 timeout = 0;
155 while (*s) {
156 if (sigsetjmp(timeoutbuf, 1)) {
157 (void) printf("\07timeout waiting for reply\n");
158 biz31_abort();
159 break;
161 (void) alarm(number(value(DIALTIMEOUT)));
162 (void) read(FD, &c, 1);
163 (void) alarm(0);
164 if (c != *s++)
165 break;
167 (void) signal(SIGALRM, f);
168 return (timeout == 0);
171 static void
172 flush(char *s)
174 char c;
175 sig_handler_t f;
177 f = signal(SIGALRM, (sig_handler_t)sigALRM);
178 while (*s++) {
179 if (sigsetjmp(timeoutbuf, 1))
180 break;
181 (void) alarm(10);
182 (void) read(FD, &c, 1);
183 (void) alarm(0);
185 (void) signal(SIGALRM, f);
186 timeout = 0; /* guard against disconnection */
190 * This convoluted piece of code attempts to get
191 * the bizcomp in sync. If you don't have the capacity or nread
192 * call there are gory ways to simulate this.
194 static int
195 bizsync(int fd)
197 #ifdef FIOCAPACITY
198 struct capacity b;
199 #define chars(b) ((b).cp_nbytes)
200 #define IOCTL FIOCAPACITY
201 #endif
202 #ifdef FIONREAD
203 long b;
204 #define chars(b) (b)
205 #define IOCTL FIONREAD
206 #endif
207 int already = 0;
208 char buf[10];
210 retry:
211 if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
212 (void) ioctl(fd, TCFLSH, TCIOFLUSH);
213 (void) write(fd, "\rp>\r", 4);
214 (void) sleep(1);
215 if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
216 if (chars(b) != 10) {
217 nono:
218 if (already > MAXRETRY)
219 return (0);
220 (void) write(fd, DISCONNECT_CMD, 4);
221 (void) sleep(2);
222 already++;
223 goto retry;
224 } else {
225 (void) read(fd, buf, 10);
226 if (strncmp(buf, "p >\r\n\r\n>", 8))
227 goto nono;
230 return (1);