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