Merge branch 'vendor/FILE'
[dragonfly.git] / usr.bin / tip / libacu / v3451.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  * @(#)v3451.c  8.1 (Berkeley) 6/6/93
34  */
35
36 /*
37  * Routines for calling up on a Vadic 3451 Modem
38  */
39 #include "tipconf.h"
40 #include "tip.h"
41
42 static int expect(char *);
43 static void vawrite(char *, int);
44 static int notin(char *, char *);
45 static void alarmtr(int);
46 static int prefix(char *, char *);
47
48 static  jmp_buf Sjbuf;
49
50 v3451_dialer(num, acu)
51         char *num;
52         char *acu;
53 {
54         sig_t func;
55         int ok;
56         int slow = number(value(BAUDRATE)) < 1200, rw = 2;
57         char phone[50];
58 #if ACULOG
59         char line[80];
60 #endif
61
62         /*
63          * Get in synch
64          */
65         vawrite("I\r", 1 + slow);
66         vawrite("I\r", 1 + slow);
67         vawrite("I\r", 1 + slow);
68         vawrite("\005\r", 2 + slow);
69         if (!expect("READY")) {
70                 printf("can't synchronize with vadic 3451\n");
71 #if ACULOG
72                 logent(value(HOST), num, "vadic", "can't synch up");
73 #endif
74                 return (0);
75         }
76         acu_hupcl ();
77         sleep(1);
78         vawrite("D\r", 2 + slow);
79         if (!expect("NUMBER?")) {
80                 printf("Vadic will not accept dial command\n");
81 #if ACULOG
82                 logent(value(HOST), num, "vadic", "will not accept dial");
83 #endif
84                 return (0);
85         }
86         strcpy(phone, num);
87         strcat(phone, "\r");
88         vawrite(phone, 1 + slow);
89         if (!expect(phone)) {
90                 printf("Vadic will not accept phone number\n");
91 #if ACULOG
92                 logent(value(HOST), num, "vadic", "will not accept number");
93 #endif
94                 return (0);
95         }
96         func = signal(SIGINT,SIG_IGN);
97         /*
98          * You cannot interrupt the Vadic when its dialing;
99          * even dropping DTR does not work (definitely a
100          * brain damaged design).
101          */
102         vawrite("\r", 1 + slow);
103         vawrite("\r", 1 + slow);
104         if (!expect("DIALING:")) {
105                 printf("Vadic failed to dial\n");
106 #if ACULOG
107                 logent(value(HOST), num, "vadic", "failed to dial");
108 #endif
109                 return (0);
110         }
111         if (boolean(value(VERBOSE)))
112                 printf("\ndialing...");
113         ok = expect("ON LINE");
114         signal(SIGINT, func);
115         if (!ok) {
116                 printf("call failed\n");
117 #if ACULOG
118                 logent(value(HOST), num, "vadic", "call failed");
119 #endif
120                 return (0);
121         }
122         ioctl(FD, TIOCFLUSH, &rw);
123         return (1);
124 }
125
126 v3451_disconnect()
127 {
128
129         close(FD);
130 }
131
132 v3451_abort()
133 {
134
135         close(FD);
136 }
137
138 static void
139 vawrite(cp, delay)
140         char *cp;
141         int delay;
142 {
143
144         for (; *cp; sleep(delay), cp++)
145                 write(FD, cp, 1);
146 }
147
148 static
149 expect(cp)
150         char *cp;
151 {
152         char buf[300];
153         char *rp = buf;
154         int timeout = 30, online = 0;
155
156         if (strcmp(cp, "\"\"") == 0)
157                 return (1);
158         *rp = 0;
159         /*
160          * If we are waiting for the Vadic to complete
161          * dialing and get a connection, allow more time
162          * Unfortunately, the Vadic times out 24 seconds after
163          * the last digit is dialed
164          */
165         online = strcmp(cp, "ON LINE") == 0;
166         if (online)
167                 timeout = number(value(DIALTIMEOUT));
168         signal(SIGALRM, alarmtr);
169         if (setjmp(Sjbuf))
170                 return (0);
171         alarm(timeout);
172         while (notin(cp, buf) && rp < buf + sizeof (buf) - 1) {
173                 if (online && notin("FAILED CALL", buf) == 0)
174                         return (0);
175                 if (read(FD, rp, 1) < 0) {
176                         alarm(0);
177                         return (0);
178                 }
179                 if (*rp &= 0177)
180                         rp++;
181                 *rp = '\0';
182         }
183         alarm(0);
184         return (1);
185 }
186
187 static void
188 alarmtr(int signo __unused)
189 {
190         longjmp(Sjbuf, 1);
191 }
192
193 static int
194 notin(sh, lg)
195         char *sh, *lg;
196 {
197         for (; *lg; lg++)
198                 if (prefix(sh, lg))
199                         return (0);
200         return (1);
201 }
202
203 static
204 prefix(s1, s2)
205         char *s1, *s2;
206 {
207         char c;
208
209         while ((c = *s1++) == *s2++)
210                 if (c == '\0')
211                         return (1);
212         return (c == '\0');
213 }