tip(1): Fix some -Wmissing-parameter-type warnings.
[dragonfly.git] / usr.bin / tip / libacu / ventel.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  * @(#)ventel.c 8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/tip/libacu/ventel.c,v 1.3 1999/08/28 01:06:31 peter Exp $
35  */
36
37 /*
38  * Routines for calling up on a Ventel Modem
39  * The Ventel is expected to be strapped for local echo (just like uucp)
40  */
41 #include "tipconf.h"
42 #include "tip.h"
43 #include <err.h>
44
45 #define MAXRETRY        5
46
47 static  void sigALRM();
48 static int gobble(char, char[]);
49 static int vensync(int);
50 static void echo(char *);
51 static  int timeout = 0;
52 static  jmp_buf timeoutbuf;
53
54 /*
55  * some sleep calls have been replaced by this macro
56  * because some ventel modems require two <cr>s in less than
57  * a second in order to 'wake up'... yes, it is dirty...
58  */
59 #define delay(num,denom) busyloop(CPUSPEED*num/denom)
60 #define CPUSPEED 1000000        /* VAX 780 is 1MIPS */
61 #define DELAY(n)        { long N = (n); while (--N > 0); }
62 void busyloop(int n) { DELAY(n); }
63
64 ven_dialer(num, acu)
65         char *num;
66         char *acu;
67 {
68         char *cp;
69         int connected = 0;
70         char *msg, *index(), line[80];
71
72         /*
73          * Get in synch with a couple of carriage returns
74          */
75         if (!vensync(FD)) {
76                 printf("can't synchronize with ventel\n");
77 #if ACULOG
78                 logent(value(HOST), num, "ventel", "can't synch up");
79 #endif
80                 return (0);
81         }
82         if (boolean(value(VERBOSE)))
83                 printf("\ndialing...");
84         fflush(stdout);
85         acu_hupcl ();
86         echo("#k$\r$\n$D$I$A$L$:$ ");
87         for (cp = num; *cp; cp++) {
88                 delay(1, 10);
89                 write(FD, cp, 1);
90         }
91         delay(1, 10);
92         write(FD, "\r", 1);
93         gobble('\n', line);
94         if (gobble('\n', line))
95                 connected = gobble('!', line);
96         acu_flush ();
97 #if ACULOG
98         if (timeout) {
99                 sprintf(line, "%d second dial timeout",
100                         number(value(DIALTIMEOUT)));
101                 logent(value(HOST), num, "ventel", line);
102         }
103 #endif
104         if (timeout)
105                 ven_disconnect();       /* insurance */
106         if (connected || timeout || !boolean(value(VERBOSE)))
107                 return (connected);
108         /* call failed, parse response for user */
109         cp = index(line, '\r');
110         if (cp)
111                 *cp = '\0';
112         for (cp = line; cp = index(cp, ' '); cp++)
113                 if (cp[1] == ' ')
114                         break;
115         if (cp) {
116                 while (*cp == ' ')
117                         cp++;
118                 msg = cp;
119                 while (*cp) {
120                         if (isupper(*cp))
121                                 *cp = tolower(*cp);
122                         cp++;
123                 }
124                 printf("%s...", msg);
125         }
126         return (connected);
127 }
128
129 ven_disconnect()
130 {
131
132         close(FD);
133 }
134
135 ven_abort()
136 {
137
138         write(FD, "\03", 1);
139         close(FD);
140 }
141
142 static void
143 echo(s)
144         char *s;
145 {
146         char c;
147
148         while (c = *s++) switch (c) {
149
150         case '$':
151                 read(FD, &c, 1);
152                 s++;
153                 break;
154
155         case '#':
156                 c = *s++;
157                 write(FD, &c, 1);
158                 break;
159
160         default:
161                 write(FD, &c, 1);
162                 read(FD, &c, 1);
163         }
164 }
165
166 static void
167 sigALRM()
168 {
169         printf("\07timeout waiting for reply\n");
170         timeout = 1;
171         longjmp(timeoutbuf, 1);
172 }
173
174 static int
175 gobble(match, response)
176         char match;
177         char response[];
178 {
179         char *cp = response;
180         sig_t f;
181         char c;
182
183         f = signal(SIGALRM, sigALRM);
184         timeout = 0;
185         do {
186                 if (setjmp(timeoutbuf)) {
187                         signal(SIGALRM, f);
188                         *cp = '\0';
189                         return (0);
190                 }
191                 alarm(number(value(DIALTIMEOUT)));
192                 read(FD, cp, 1);
193                 alarm(0);
194                 c = (*cp++ &= 0177);
195 #ifdef notdef
196                 if (boolean(value(VERBOSE)))
197                         putchar(c);
198 #endif
199         } while (c != '\n' && c != match);
200         signal(SIGALRM, SIG_DFL);
201         *cp = '\0';
202         return (c == match);
203 }
204
205 #define min(a,b)        ((a)>(b)?(b):(a))
206 /*
207  * This convoluted piece of code attempts to get
208  * the ventel in sync.  If you don't have FIONREAD
209  * there are gory ways to simulate this.
210  */
211 static int
212 vensync(int fd)
213 {
214         int already = 0, nread;
215         char buf[60];
216
217         /*
218          * Toggle DTR to force anyone off that might have left
219          * the modem connected, and insure a consistent state
220          * to start from.
221          *
222          * If you don't have the ioctl calls to diddle directly
223          * with DTR, you can always try setting the baud rate to 0.
224          */
225         ioctl(FD, TIOCCDTR, 0);
226         sleep(1);
227         ioctl(FD, TIOCSDTR, 0);
228         while (already < MAXRETRY) {
229                 /*
230                  * After reseting the modem, send it two \r's to
231                  * autobaud on. Make sure to delay between them
232                  * so the modem can frame the incoming characters.
233                  */
234                 write(fd, "\r", 1);
235                 delay(1,10);
236                 write(fd, "\r", 1);
237                 sleep(2);
238                 if (ioctl(fd, FIONREAD, (caddr_t)&nread) < 0) {
239                         warn("ioctl");
240                         continue;
241                 }
242                 while (nread > 0) {
243                         read(fd, buf, min(nread, 60));
244                         if ((buf[nread - 1] & 0177) == '$')
245                                 return (1);
246                         nread -= min(nread, 60);
247                 }
248                 sleep(1);
249                 already++;
250         }
251         return (0);
252 }
253