Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * @(#)biz31.c  8.1 (Berkeley) 6/6/93
34  */
35
36 #include "tipconf.h"
37 #include "tip.h"
38
39 #define MAXRETRY        3               /* sync up retry count */
40 #define DISCONNECT_CMD  "\21\25\11\24"  /* disconnection string */
41
42 static  void sigALRM();
43 static  int timeout = 0;
44 static  jmp_buf timeoutbuf;
45
46 /*
47  * Dial up on a BIZCOMP Model 1031 with either
48  *      tone dialing (mod = "f")
49  *      pulse dialing (mod = "w")
50  */
51 static int
52 biz_dialer(num, mod)
53         char *num, *mod;
54 {
55         register int connected = 0;
56
57         if (!bizsync(FD)) {
58                 logent(value(HOST), "", "biz", "out of sync");
59                 printf("bizcomp out of sync\n");
60                 delock(uucplock);
61                 exit(0);
62         }
63         if (boolean(value(VERBOSE)))
64                 printf("\nstarting call...");
65         echo("#\rk$\r$\n");                     /* disable auto-answer */
66         echo("$>$.$ #\r");                      /* tone/pulse dialing */
67         echo(mod);
68         echo("$\r$\n");
69         echo("$>$.$ #\re$ ");                   /* disconnection sequence */
70         echo(DISCONNECT_CMD);
71         echo("\r$\n$\r$\n");
72         echo("$>$.$ #\rr$ ");                   /* repeat dial */
73         echo(num);
74         echo("\r$\n");
75         if (boolean(value(VERBOSE)))
76                 printf("ringing...");
77         /*
78          * The reply from the BIZCOMP should be:
79          *      `^G NO CONNECTION\r\n^G\r\n'    failure
80          *      ` CONNECTION\r\n^G'             success
81          */
82         connected = detect(" ");
83 #if ACULOG
84         if (timeout) {
85                 char line[80];
86
87                 sprintf(line, "%d second dial timeout",
88                         number(value(DIALTIMEOUT)));
89                 logent(value(HOST), num, "biz", line);
90         }
91 #endif
92         if (!connected)
93                 flush(" NO CONNECTION\r\n\07\r\n");
94         else
95                 flush("CONNECTION\r\n\07");
96         if (timeout)
97                 biz31_disconnect();     /* insurance */
98         return (connected);
99 }
100
101 biz31w_dialer(num, acu)
102         char *num, *acu;
103 {
104
105         return (biz_dialer(num, "w"));
106 }
107
108 biz31f_dialer(num, acu)
109         char *num, *acu;
110 {
111
112         return (biz_dialer(num, "f"));
113 }
114
115 biz31_disconnect()
116 {
117
118         write(FD, DISCONNECT_CMD, 4);
119         sleep(2);
120         ioctl(FD, TIOCFLUSH);
121 }
122
123 biz31_abort()
124 {
125
126         write(FD, "\33", 1);
127 }
128
129 static int
130 echo(s)
131         register char *s;
132 {
133         char c;
134
135         while (c = *s++) switch (c) {
136
137         case '$':
138                 read(FD, &c, 1);
139                 s++;
140                 break;
141
142         case '#':
143                 c = *s++;
144                 write(FD, &c, 1);
145                 break;
146
147         default:
148                 write(FD, &c, 1);
149                 read(FD, &c, 1);
150         }
151 }
152
153 static void
154 sigALRM()
155 {
156
157         timeout = 1;
158         longjmp(timeoutbuf, 1);
159 }
160
161 static int
162 detect(s)
163         register char *s;
164 {
165         sig_t f;
166         char c;
167
168         f = signal(SIGALRM, sigALRM);
169         timeout = 0;
170         while (*s) {
171                 if (setjmp(timeoutbuf)) {
172                         printf("\07timeout waiting for reply\n");
173                         biz31_abort();
174                         break;
175                 }
176                 alarm(number(value(DIALTIMEOUT)));
177                 read(FD, &c, 1);
178                 alarm(0);
179                 if (c != *s++)
180                         break;
181         }
182         signal(SIGALRM, f);
183         return (timeout == 0);
184 }
185
186 static int
187 flush(s)
188         register char *s;
189 {
190         sig_t f;
191         char c;
192
193         f = signal(SIGALRM, sigALRM);
194         while (*s++) {
195                 if (setjmp(timeoutbuf))
196                         break;
197                 alarm(10);
198                 read(FD, &c, 1);
199                 alarm(0);
200         }
201         signal(SIGALRM, f);
202         timeout = 0;                    /* guard against disconnection */
203 }
204
205 /*
206  * This convoluted piece of code attempts to get
207  *  the bizcomp in sync.  If you don't have the capacity or nread
208  *  call there are gory ways to simulate this.
209  */
210 static int
211 bizsync(fd)
212 {
213 #ifdef FIOCAPACITY
214         struct capacity b;
215 #       define chars(b) ((b).cp_nbytes)
216 #       define IOCTL    FIOCAPACITY
217 #endif
218 #ifdef FIONREAD
219         long b;
220 #       define chars(b) (b)
221 #       define IOCTL    FIONREAD
222 #endif
223         register int already = 0;
224         char buf[10];
225
226 retry:
227         if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0 && chars(b) > 0)
228                 ioctl(fd, TIOCFLUSH);
229         write(fd, "\rp>\r", 4);
230         sleep(1);
231         if (ioctl(fd, IOCTL, (caddr_t)&b) >= 0) {
232                 if (chars(b) != 10) {
233         nono:
234                         if (already > MAXRETRY)
235                                 return (0);
236                         write(fd, DISCONNECT_CMD, 4);
237                         sleep(2);
238                         already++;
239                         goto retry;
240                 } else {
241                         read(fd, buf, 10);
242                         if (strncmp(buf, "p >\r\n\r\n>", 8))
243                                 goto nono;
244                 }
245         }
246         return (1);
247 }