kcollect - Remove system calls from graph
[dragonfly.git] / usr.bin / 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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)acu.c    8.1 (Berkeley) 6/6/93
30  * $FreeBSD: src/usr.bin/tip/tip/acu.c,v 1.5 1999/08/28 01:06:31 peter Exp $
31  */
32
33 #include "tip.h"
34
35 acu_t* unidialer_getmodem (const char *modem_name);
36
37 static acu_t *acu = NULL;
38 static int conflag;
39 static void acuabort(int);
40 static acu_t *acutype(char *);
41 static jmp_buf jmpbuf;
42 /*
43  * Establish connection for tip
44  *
45  * If DU is true, we should dial an ACU whose type is AT.
46  * The phone numbers are in PN, and the call unit is in CU.
47  *
48  * If the PN is an '@', then we consult the PHONES file for
49  *   the phone numbers.  This file is /etc/phones, unless overridden
50  *   by an exported shell variable.
51  *
52  * The data base files must be in the format:
53  *      host-name[ \t]*phone-number
54  *   with the possibility of multiple phone numbers
55  *   for a single host acting as a rotary (in the order
56  *   found in the file).
57  */
58 char *
59 connect(void)
60 {
61         char *cp = PN;
62         char *phnum, string[256];
63         FILE *fd;
64         int tried = 0;
65
66         if (!DU) {              /* regular connect message */
67                 if (CM != NULL)
68                         xpwrite(FD, CM, size(CM));
69                 logent(value(HOST), "", DV, "call completed");
70                 return (NULL);
71         }
72         /*
73          * @ =>'s use data base in PHONES environment variable
74          *        otherwise, use /etc/phones
75          */
76         signal(SIGINT, acuabort);
77         signal(SIGQUIT, acuabort);
78         if (setjmp(jmpbuf)) {
79                 signal(SIGINT, SIG_IGN);
80                 signal(SIGQUIT, SIG_IGN);
81                 printf("\ncall aborted\n");
82                 logent(value(HOST), "", "", "call aborted");
83                 if (acu != NULL) {
84                         boolean(value(VERBOSE)) = FALSE;
85                         if (conflag)
86                                 disconnect(NULL);
87                         else
88                                 (*acu->acu_abort)();
89                 }
90                 return ("interrupt");
91         }
92         if ((acu = acutype(AT)) == NULL)
93                 return ("unknown ACU type");
94         if (*cp != '@') {
95                 while (*cp) {
96                         for (phnum = cp; *cp && *cp != ','; cp++)
97                                 ;
98                         if (*cp)
99                                 *cp++ = '\0';
100
101                         if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
102                                 if (CM != NULL)
103                                         xpwrite(FD, CM, size(CM));
104                                 logent(value(HOST), phnum, acu->acu_name,
105                                         "call completed");
106                                 return (NULL);
107                         } else
108                                 logent(value(HOST), phnum, acu->acu_name,
109                                         "call failed");
110                         tried++;
111                 }
112         } else {
113                 if ((fd = fopen(PH, "r")) == NULL) {
114                         printf("%s: ", PH);
115                         return ("can't open phone number file");
116                 }
117                 while (fgets(string, sizeof(string), fd) != NULL) {
118                         for (cp = string; !any(*cp, " \t\n"); cp++)
119                                 ;
120                         if (*cp == '\n') {
121                                 fclose(fd);
122                                 return ("unrecognizable host name");
123                         }
124                         *cp++ = '\0';
125                         if (strcmp(string, value(HOST)))
126                                 continue;
127                         while (any(*cp, " \t"))
128                                 cp++;
129                         if (*cp == '\n') {
130                                 fclose(fd);
131                                 return ("missing phone number");
132                         }
133                         for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
134                                 ;
135                         if (*cp)
136                                 *cp++ = '\0';
137
138                         if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
139                                 fclose(fd);
140                                 if (CM != NULL)
141                                         xpwrite(FD, CM, size(CM));
142                                 logent(value(HOST), phnum, acu->acu_name,
143                                         "call completed");
144                                 return (NULL);
145                         } else
146                                 logent(value(HOST), phnum, acu->acu_name,
147                                         "call failed");
148                         tried++;
149                 }
150                 fclose(fd);
151         }
152         if (!tried)
153                 logent(value(HOST), "", acu->acu_name, "missing phone number");
154         else
155                 (*acu->acu_abort)();
156         return (tried ? "call failed" : "missing phone number");
157 }
158
159 void
160 disconnect(char *reason)
161 {
162         if (!conflag) {
163                 logent(value(HOST), "", DV, "call terminated");
164                 return;
165         }
166         if (reason == NULL) {
167                 logent(value(HOST), "", acu->acu_name, "call terminated");
168                 if (boolean(value(VERBOSE)))
169                         printf("\r\ndisconnecting...");
170         } else
171                 logent(value(HOST), "", acu->acu_name, reason);
172         (*acu->acu_disconnect)();
173 }
174
175 static void
176 acuabort(int s)
177 {
178         signal(s, SIG_IGN);
179         longjmp(jmpbuf, 1);
180 }
181
182 static acu_t *
183 acutype(char *s)
184 {
185         return unidialer_getmodem (s);
186 }