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