Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / tip / libacu / biz31.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)biz31.c     8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37
38 #include "tipconf.h"
39 #include "tip.h"
40
41 #define MAXRETRY        3               /* sync up retry count */
42 #define DISCONNECT_CMD  "\21\25\11\24"  /* disconnection string */
43
44 static  void sigALRM();
45 static  int timeout = 0;
46 static  jmp_buf timeoutbuf;
47
48 /*
49  * Dial up on a BIZCOMP Model 1031 with either
50  *      tone dialing (mod = "f")
51  *      pulse dialing (mod = "w")
52  */
53 static int
54 biz_dialer(num, mod)
55         char *num, *mod;
56 {
57         register int connected = 0;
58
59         if (!bizsync(FD)) {
60                 logent(value(HOST), "", "biz", "out of sync");
61                 printf("bizcomp out of sync\n");
62                 delock(uucplock);
63                 exit(0);
64         }
65         if (boolean(value(VERBOSE)))
66                 printf("\nstarting call...");
67         echo("#\rk$\r$\n");                     /* disable auto-answer */
68         echo("$>$.$ #\r");                      /* tone/pulse dialing */
69         echo(mod);
70         echo("$\r$\n");
71         echo("$>$.$ #\re$ ");                   /* disconnection sequence */
72         echo(DISCONNECT_CMD);
73         echo("\r$\n$\r$\n");
74         echo("$>$.$ #\rr$ ");                   /* repeat dial */
75         echo(num);
76         echo("\r$\n");
77         if (boolean(value(VERBOSE)))
78                 printf("ringing...");
79         /*
80          * The reply from the BIZCOMP should be:
81          *      `^G NO CONNECTION\r\n^G\r\n'    failure
82          *      ` CONNECTION\r\n^G'             success
83          */
84         connected = detect(" ");
85 #if ACULOG
86         if (timeout) {
87                 char line[80];
88
89                 sprintf(line, "%d second dial timeout",
90                         number(value(DIALTIMEOUT)));
91                 logent(value(HOST), num, "biz", line);
92         }
93 #endif
94         if (!connected)
95                 flush(" NO CONNECTION\r\n\07\r\n");
96         else
97                 flush("CONNECTION\r\n\07");
98         if (timeout)
99                 biz31_disconnect();     /* insurance */
100         return (connected);
101 }
102
103 biz31w_dialer(num, acu)
104         char *num, *acu;
105 {
106
107         return (biz_dialer(num, "w"));
108 }
109
110 biz31f_dialer(num, acu)
111         char *num, *acu;
112 {
113
114         return (biz_dialer(num, "f"));
115 }
116
117 biz31_disconnect()
118 {
119
120         write(FD, DISCONNECT_CMD, 4);
121         sleep(2);
122         ioctl(FD, TIOCFLUSH);
123 }
124
125 biz31_abort()
126 {
127
128         write(FD, "\33", 1);
129 }
130
131 static int
132 echo(s)
133         register char *s;
134 {
135         char c;
136
137         while (c = *s++) switch (c) {
138
139         case '$':
140                 read(FD, &c, 1);
141                 s++;
142                 break;
143
144         case '#':
145                 c = *s++;
146                 write(FD, &c, 1);
147                 break;
148
149         default:
150                 write(FD, &c, 1);
151                 read(FD, &c, 1);
152         }
153 }
154
155 static void
156 sigALRM()
157 {
158
159         timeout = 1;
160         longjmp(timeoutbuf, 1);
161 }
162
163 static int
164 detect(s)
165         register char *s;
166 {
167         sig_t f;
168         char c;
169
170         f = signal(SIGALRM, sigALRM);
171         timeout = 0;
172         while (*s) {
173                 if (setjmp(timeoutbuf)) {
174                         printf("\07timeout waiting for reply\n");
175                         biz31_abort();
176                         break;
177                 }
178                 alarm(number(value(DIALTIMEOUT)));
179                 read(FD, &c, 1);
180                 alarm(0);
181                 if (c != *s++)
182                         break;
183         }
184         signal(SIGALRM, f);
185         return (timeout == 0);
186 }
187
188 static int
189 flush(s)
190         register char *s;
191 {
192         sig_t f;
193         char c;
194
195         f = signal(SIGALRM, sigALRM);
196         while (*s++) {
197                 if (setjmp(timeoutbuf))
198                         break;
199                 alarm(10);
200                 read(FD, &c, 1);
201                 alarm(0);
202         }
203         signal(SIGALRM, f);
204         timeout = 0;                    /* guard against disconnection */
205 }
206
207 /*
208  * This convoluted piece of code attempts to get
209  *  the bizcomp in sync.  If you don't have the capacity or nread
210  *  call there are gory ways to simulate this.
211  */
212 static int
213 bizsync(fd)
214 {
215 #ifdef FIOCAPACITY
216         struct capacity b;
217 #       define chars(b) ((b).cp_nbytes)
218 #       define IOCTL    FIOCAPACITY
219 #endif
220 #ifdef FIONREAD
221         long b;
222 #       define chars(b) (b)
223 #       define IOCTL    FIONREAD
224 #endif
225         register int already = 0;
226         char buf[10];
227
228 retry:
229         if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
230                 ioctl(fd, TIOCFLUSH);
231         write(fd, "\rp>\r", 4);
232         sleep(1);
233         if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
234                 if (chars(b) != 10) {
235         nono:
236                         if (already > MAXRETRY)
237                                 return (0);
238                         write(fd, DISCONNECT_CMD, 4);
239                         sleep(2);
240                         already++;
241                         goto retry;
242                 } else {
243                         read(fd, buf, 10);
244                         if (strncmp(buf, "p >\r\n\r\n>", 8))
245                                 goto nono;
246                 }
247         }
248         return (1);
249 }