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