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