xref: /dragonfly/usr.bin/tip/acu.c (revision 0db87cb7)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  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  * @(#)acu.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.bin/tip/tip/acu.c,v 1.5 1999/08/28 01:06:31 peter Exp $
35  */
36 
37 #include "tipconf.h"
38 #include "tip.h"
39 
40 #if UNIDIALER
41 acu_t* unidialer_getmodem (const char *modem_name);
42 #endif
43 
44 static acu_t *acu = NULL;
45 static int conflag;
46 static void acuabort(int);
47 static acu_t *acutype(char *);
48 static jmp_buf jmpbuf;
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 char *
66 connect(void)
67 {
68 	char *cp = PN;
69 	char *phnum, string[256];
70 	FILE *fd;
71 	int tried = 0;
72 
73 	if (!DU) {		/* regular connect message */
74 		if (CM != NULL)
75 			xpwrite(FD, CM, size(CM));
76 		logent(value(HOST), "", DV, "call completed");
77 		return (NULL);
78 	}
79 	/*
80 	 * @ =>'s use data base in PHONES environment variable
81 	 *        otherwise, use /etc/phones
82 	 */
83 	signal(SIGINT, acuabort);
84 	signal(SIGQUIT, acuabort);
85 	if (setjmp(jmpbuf)) {
86 		signal(SIGINT, SIG_IGN);
87 		signal(SIGQUIT, SIG_IGN);
88 		printf("\ncall aborted\n");
89 		logent(value(HOST), "", "", "call aborted");
90 		if (acu != NULL) {
91 			boolean(value(VERBOSE)) = FALSE;
92 			if (conflag)
93 				disconnect(NULL);
94 			else
95 				(*acu->acu_abort)();
96 		}
97 		return ("interrupt");
98 	}
99 	if ((acu = acutype(AT)) == NULL)
100 		return ("unknown ACU type");
101 	if (*cp != '@') {
102 		while (*cp) {
103 			for (phnum = cp; *cp && *cp != ','; cp++)
104 				;
105 			if (*cp)
106 				*cp++ = '\0';
107 
108 			if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
109 				if (CM != NULL)
110 					xpwrite(FD, CM, size(CM));
111 				logent(value(HOST), phnum, acu->acu_name,
112 					"call completed");
113 				return (NULL);
114 			} else
115 				logent(value(HOST), phnum, acu->acu_name,
116 					"call failed");
117 			tried++;
118 		}
119 	} else {
120 		if ((fd = fopen(PH, "r")) == NULL) {
121 			printf("%s: ", PH);
122 			return ("can't open phone number file");
123 		}
124 		while (fgets(string, sizeof(string), fd) != NULL) {
125 			for (cp = string; !any(*cp, " \t\n"); cp++)
126 				;
127 			if (*cp == '\n') {
128 				fclose(fd);
129 				return ("unrecognizable host name");
130 			}
131 			*cp++ = '\0';
132 			if (strcmp(string, value(HOST)))
133 				continue;
134 			while (any(*cp, " \t"))
135 				cp++;
136 			if (*cp == '\n') {
137 				fclose(fd);
138 				return ("missing phone number");
139 			}
140 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
141 				;
142 			if (*cp)
143 				*cp++ = '\0';
144 
145 			if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
146 				fclose(fd);
147 				if (CM != NULL)
148 					xpwrite(FD, CM, size(CM));
149 				logent(value(HOST), phnum, acu->acu_name,
150 					"call completed");
151 				return (NULL);
152 			} else
153 				logent(value(HOST), phnum, acu->acu_name,
154 					"call failed");
155 			tried++;
156 		}
157 		fclose(fd);
158 	}
159 	if (!tried)
160 		logent(value(HOST), "", acu->acu_name, "missing phone number");
161 	else
162 		(*acu->acu_abort)();
163 	return (tried ? "call failed" : "missing phone number");
164 }
165 
166 void
167 disconnect(char *reason)
168 {
169 	if (!conflag) {
170 		logent(value(HOST), "", DV, "call terminated");
171 		return;
172 	}
173 	if (reason == NULL) {
174 		logent(value(HOST), "", acu->acu_name, "call terminated");
175 		if (boolean(value(VERBOSE)))
176 			printf("\r\ndisconnecting...");
177 	} else
178 		logent(value(HOST), "", acu->acu_name, reason);
179 	(*acu->acu_disconnect)();
180 }
181 
182 static void
183 acuabort(int s)
184 {
185 	signal(s, SIG_IGN);
186 	longjmp(jmpbuf, 1);
187 }
188 
189 static acu_t *
190 acutype(char *s)
191 {
192 #if UNIDIALER
193 	return unidialer_getmodem (s);
194 #else
195 	return (NULL);
196 #endif
197 }
198