xref: /netbsd/usr.bin/tip/acu.c (revision bf9ec67e)
1 /*	$NetBSD: acu.c,v 1.7 1998/07/12 09:59:29 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)acu.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: acu.c,v 1.7 1998/07/12 09:59:29 mrg Exp $");
42 #endif /* not lint */
43 
44 #include "tip.h"
45 
46 static acu_t *acu = NULL;
47 static int conflag;
48 static jmp_buf jmpbuf;
49 
50 static void	acuabort __P((int));
51 static acu_t   *acutype __P((char *));
52 
53 /*
54  * Establish connection for tip
55  *
56  * If DU is true, we should dial an ACU whose type is AT.
57  * The phone numbers are in PN, and the call unit is in CU.
58  *
59  * If the PN is an '@', then we consult the PHONES file for
60  *   the phone numbers.  This file is /etc/phones, unless overriden
61  *   by an exported shell variable.
62  *
63  * The data base files must be in the format:
64  *	host-name[ \t]*phone-number
65  *   with the possibility of multiple phone numbers
66  *   for a single host acting as a rotary (in the order
67  *   found in the file).
68  */
69 char *
70 connect()
71 {
72 	char *cp = PN;
73 	char *phnum, string[256];
74 	FILE *fd;
75 	int tried = 0;
76 
77 #if __GNUC__		/* XXX pacify gcc */
78 	(void)&cp;
79 	(void)&tried;
80 #endif
81 
82 	if (!DU) {		/* regular connect message */
83 		if (CM != NULL)
84 			xpwrite(FD, CM, strlen(CM));
85 		logent(value(HOST), "", DV, "call completed");
86 		return (NULL);
87 	}
88 	/*
89 	 * @ =>'s use data base in PHONES environment variable
90 	 *        otherwise, use /etc/phones
91 	 */
92 	signal(SIGINT, acuabort);
93 	signal(SIGQUIT, acuabort);
94 	if (setjmp(jmpbuf)) {
95 		signal(SIGINT, SIG_IGN);
96 		signal(SIGQUIT, SIG_IGN);
97 		printf("\ncall aborted\n");
98 		logent(value(HOST), "", "", "call aborted");
99 		if (acu != NULL) {
100 			setboolean(value(VERBOSE), FALSE);
101 			if (conflag)
102 				disconnect(NULL);
103 			else
104 				(*acu->acu_abort)();
105 		}
106 		return ("interrupt");
107 	}
108 	if ((acu = acutype(AT)) == NULL)
109 		return ("unknown ACU type");
110 	if (*cp != '@') {
111 		while (*cp) {
112 			for (phnum = cp; *cp && *cp != ','; cp++)
113 				;
114 			if (*cp)
115 				*cp++ = '\0';
116 
117 			if ((conflag = (*acu->acu_dialer)(phnum, CU)) != 0) {
118 				if (CM != NULL)
119 					xpwrite(FD, CM, strlen(CM));
120 				logent(value(HOST), phnum, acu->acu_name,
121 					"call completed");
122 				return (NULL);
123 			} else
124 				logent(value(HOST), phnum, acu->acu_name,
125 					"call failed");
126 			tried++;
127 		}
128 	} else {
129 		if ((fd = fopen(PH, "r")) == NULL) {
130 			printf("%s: ", PH);
131 			return ("can't open phone number file");
132 		}
133 		while (fgets(string, sizeof(string), fd) != NULL) {
134 			for (cp = string; !any(*cp, " \t\n"); cp++)
135 				;
136 			if (*cp == '\n') {
137 				fclose(fd);
138 				return ("unrecognizable host name");
139 			}
140 			*cp++ = '\0';
141 			if (strcmp(string, value(HOST)))
142 				continue;
143 			while (any(*cp, " \t"))
144 				cp++;
145 			if (*cp == '\n') {
146 				fclose(fd);
147 				return ("missing phone number");
148 			}
149 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
150 				;
151 			if (*cp)
152 				*cp++ = '\0';
153 
154 			if ((conflag = (*acu->acu_dialer)(phnum, CU)) != 0) {
155 				fclose(fd);
156 				if (CM != NULL)
157 					xpwrite(FD, CM, strlen(CM));
158 				logent(value(HOST), phnum, acu->acu_name,
159 					"call completed");
160 				return (NULL);
161 			} else
162 				logent(value(HOST), phnum, acu->acu_name,
163 					"call failed");
164 			tried++;
165 		}
166 		fclose(fd);
167 	}
168 	if (!tried)
169 		logent(value(HOST), "", acu->acu_name, "missing phone number");
170 	else
171 		(*acu->acu_abort)();
172 	return (tried ? "call failed" : "missing phone number");
173 }
174 
175 void
176 disconnect(reason)
177 	char *reason;
178 {
179 
180 	if (!conflag) {
181 		logent(value(HOST), "", DV, "call terminated");
182 		return;
183 	}
184 	if (reason == NULL) {
185 		logent(value(HOST), "", acu->acu_name, "call terminated");
186 		if (boolean(value(VERBOSE)))
187 			printf("\r\ndisconnecting...");
188 	} else
189 		logent(value(HOST), "", acu->acu_name, reason);
190 	(*acu->acu_disconnect)();
191 }
192 
193 static void
194 acuabort(s)
195 	int s;
196 {
197 
198 	signal(s, SIG_IGN);
199 	longjmp(jmpbuf, 1);
200 }
201 
202 static acu_t *
203 acutype(s)
204 	char *s;
205 {
206 	acu_t *p;
207 
208 	for (p = acutable; p->acu_name != '\0'; p++)
209 		if (!strcmp(s, p->acu_name))
210 			return (p);
211 	return (NULL);
212 }
213