Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / tip / tip / acu.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 #if 0
36 static char sccsid[] = "@(#)acu.c       8.1 (Berkeley) 6/6/93";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/usr.bin/tip/tip/acu.c,v 1.5 1999/08/28 01:06:31 peter Exp $";
40 #endif /* not lint */
41
42 #include "tipconf.h"
43 #include "tip.h"
44
45 #if UNIDIALER
46 acu_t* unidialer_getmodem (const char *modem_name);
47 #endif
48
49 static acu_t *acu = NOACU;
50 static int conflag;
51 static void acuabort();
52 static acu_t *acutype();
53 static jmp_buf jmpbuf;
54 /*
55  * Establish connection for tip
56  *
57  * If DU is true, we should dial an ACU whose type is AT.
58  * The phone numbers are in PN, and the call unit is in CU.
59  *
60  * If the PN is an '@', then we consult the PHONES file for
61  *   the phone numbers.  This file is /etc/phones, unless overriden
62  *   by an exported shell variable.
63  *
64  * The data base files must be in the format:
65  *      host-name[ \t]*phone-number
66  *   with the possibility of multiple phone numbers
67  *   for a single host acting as a rotary (in the order
68  *   found in the file).
69  */
70 char *
71 connect()
72 {
73         register char *cp = PN;
74         char *phnum, string[256];
75         FILE *fd;
76         int tried = 0;
77
78         if (!DU) {              /* regular connect message */
79                 if (CM != NOSTR)
80                         xpwrite(FD, CM, size(CM));
81                 logent(value(HOST), "", DV, "call completed");
82                 return (NOSTR);
83         }
84         /*
85          * @ =>'s use data base in PHONES environment variable
86          *        otherwise, use /etc/phones
87          */
88         signal(SIGINT, acuabort);
89         signal(SIGQUIT, acuabort);
90         if (setjmp(jmpbuf)) {
91                 signal(SIGINT, SIG_IGN);
92                 signal(SIGQUIT, SIG_IGN);
93                 printf("\ncall aborted\n");
94                 logent(value(HOST), "", "", "call aborted");
95                 if (acu != NOACU) {
96                         boolean(value(VERBOSE)) = FALSE;
97                         if (conflag)
98                                 disconnect(NOSTR);
99                         else
100                                 (*acu->acu_abort)();
101                 }
102                 return ("interrupt");
103         }
104         if ((acu = acutype(AT)) == NOACU)
105                 return ("unknown ACU type");
106         if (*cp != '@') {
107                 while (*cp) {
108                         for (phnum = cp; *cp && *cp != ','; cp++)
109                                 ;
110                         if (*cp)
111                                 *cp++ = '\0';
112
113                         if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
114                                 if (CM != NOSTR)
115                                         xpwrite(FD, CM, size(CM));
116                                 logent(value(HOST), phnum, acu->acu_name,
117                                         "call completed");
118                                 return (NOSTR);
119                         } else
120                                 logent(value(HOST), phnum, acu->acu_name,
121                                         "call failed");
122                         tried++;
123                 }
124         } else {
125                 if ((fd = fopen(PH, "r")) == NOFILE) {
126                         printf("%s: ", PH);
127                         return ("can't open phone number file");
128                 }
129                 while (fgets(string, sizeof(string), fd) != NOSTR) {
130                         for (cp = string; !any(*cp, " \t\n"); cp++)
131                                 ;
132                         if (*cp == '\n') {
133                                 fclose(fd);
134                                 return ("unrecognizable host name");
135                         }
136                         *cp++ = '\0';
137                         if (strcmp(string, value(HOST)))
138                                 continue;
139                         while (any(*cp, " \t"))
140                                 cp++;
141                         if (*cp == '\n') {
142                                 fclose(fd);
143                                 return ("missing phone number");
144                         }
145                         for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
146                                 ;
147                         if (*cp)
148                                 *cp++ = '\0';
149
150                         if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
151                                 fclose(fd);
152                                 if (CM != NOSTR)
153                                         xpwrite(FD, CM, size(CM));
154                                 logent(value(HOST), phnum, acu->acu_name,
155                                         "call completed");
156                                 return (NOSTR);
157                         } else
158                                 logent(value(HOST), phnum, acu->acu_name,
159                                         "call failed");
160                         tried++;
161                 }
162                 fclose(fd);
163         }
164         if (!tried)
165                 logent(value(HOST), "", acu->acu_name, "missing phone number");
166         else
167                 (*acu->acu_abort)();
168         return (tried ? "call failed" : "missing phone number");
169 }
170
171 void
172 disconnect(reason)
173         char *reason;
174 {
175         if (!conflag) {
176                 logent(value(HOST), "", DV, "call terminated");
177                 return;
178         }
179         if (reason == NOSTR) {
180                 logent(value(HOST), "", acu->acu_name, "call terminated");
181                 if (boolean(value(VERBOSE)))
182                         printf("\r\ndisconnecting...");
183         } else
184                 logent(value(HOST), "", acu->acu_name, reason);
185         (*acu->acu_disconnect)();
186 }
187
188 static void
189 acuabort(s)
190 {
191         signal(s, SIG_IGN);
192         longjmp(jmpbuf, 1);
193 }
194
195 static acu_t *
196 acutype(s)
197         register char *s;
198 {
199         register acu_t *p;
200         extern acu_t acutable[];
201
202         for (p = acutable; p->acu_name != '\0'; p++)
203                 if (!strcmp(s, p->acu_name))
204                         return (p);
205
206         #if UNIDIALER
207         return unidialer_getmodem (s);
208         #else
209         return (NOACU);
210         #endif
211 }