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