Annotation of src/usr.bin/tip/acu.c, Revision 1.5
1.5 ! deraadt 1: /* $OpenBSD: acu.c,v 1.4 1997/09/01 23:24:23 deraadt Exp $ */
1.3 millert 2: /* $NetBSD: acu.c,v 1.4 1996/12/29 10:34:03 cgd Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)acu.c 8.1 (Berkeley) 6/6/93";
40: #endif
1.5 ! deraadt 41: static char rcsid[] = "$OpenBSD: acu.c,v 1.4 1997/09/01 23:24:23 deraadt Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #include "tip.h"
45:
46: static acu_t *acu = NOACU;
47: static int conflag;
48: static void acuabort();
49: static acu_t *acutype();
50: static jmp_buf jmpbuf;
51: /*
52: * Establish connection for tip
53: *
54: * If DU is true, we should dial an ACU whose type is AT.
55: * The phone numbers are in PN, and the call unit is in CU.
56: *
57: * If the PN is an '@', then we consult the PHONES file for
58: * the phone numbers. This file is /etc/phones, unless overriden
59: * by an exported shell variable.
60: *
61: * The data base files must be in the format:
62: * host-name[ \t]*phone-number
63: * with the possibility of multiple phone numbers
64: * for a single host acting as a rotary (in the order
65: * found in the file).
66: */
67: char *
68: connect()
69: {
70: register char *cp = PN;
71: char *phnum, string[256];
72: FILE *fd;
1.4 deraadt 73: volatile int tried = 0;
1.1 deraadt 74:
75: if (!DU) { /* regular connect message */
76: if (CM != NOSTR)
1.5 ! deraadt 77: parwrite(FD, CM, size(CM));
1.1 deraadt 78: logent(value(HOST), "", DV, "call completed");
79: return (NOSTR);
80: }
81: /*
82: * @ =>'s use data base in PHONES environment variable
83: * otherwise, use /etc/phones
84: */
85: signal(SIGINT, acuabort);
86: signal(SIGQUIT, acuabort);
87: if (setjmp(jmpbuf)) {
88: signal(SIGINT, SIG_IGN);
89: signal(SIGQUIT, SIG_IGN);
90: printf("\ncall aborted\n");
91: logent(value(HOST), "", "", "call aborted");
92: if (acu != NOACU) {
1.3 millert 93: setboolean(value(VERBOSE), FALSE);
1.1 deraadt 94: if (conflag)
95: disconnect(NOSTR);
96: else
97: (*acu->acu_abort)();
98: }
99: return ("interrupt");
100: }
101: if ((acu = acutype(AT)) == NOACU)
102: return ("unknown ACU type");
103: if (*cp != '@') {
104: while (*cp) {
105: for (phnum = cp; *cp && *cp != ','; cp++)
106: ;
107: if (*cp)
108: *cp++ = '\0';
109:
1.4 deraadt 110: if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
1.1 deraadt 111: if (CM != NOSTR)
1.5 ! deraadt 112: parwrite(FD, CM, size(CM));
1.1 deraadt 113: logent(value(HOST), phnum, acu->acu_name,
114: "call completed");
115: return (NOSTR);
116: } else
117: logent(value(HOST), phnum, acu->acu_name,
118: "call failed");
119: tried++;
120: }
121: } else {
122: if ((fd = fopen(PH, "r")) == NOFILE) {
123: printf("%s: ", PH);
124: return ("can't open phone number file");
125: }
126: while (fgets(string, sizeof(string), fd) != NOSTR) {
127: for (cp = string; !any(*cp, " \t\n"); cp++)
128: ;
129: if (*cp == '\n') {
130: fclose(fd);
131: return ("unrecognizable host name");
132: }
133: *cp++ = '\0';
134: if (strcmp(string, value(HOST)))
135: continue;
136: while (any(*cp, " \t"))
137: cp++;
138: if (*cp == '\n') {
139: fclose(fd);
140: return ("missing phone number");
141: }
142: for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
143: ;
144: if (*cp)
145: *cp++ = '\0';
146:
1.4 deraadt 147: if ((conflag = (*acu->acu_dialer)(phnum, CU))) {
1.1 deraadt 148: fclose(fd);
149: if (CM != NOSTR)
1.5 ! deraadt 150: parwrite(FD, CM, size(CM));
1.1 deraadt 151: logent(value(HOST), phnum, acu->acu_name,
152: "call completed");
153: return (NOSTR);
154: } else
155: logent(value(HOST), phnum, acu->acu_name,
156: "call failed");
157: tried++;
158: }
159: fclose(fd);
160: }
161: if (!tried)
162: logent(value(HOST), "", acu->acu_name, "missing phone number");
163: else
164: (*acu->acu_abort)();
165: return (tried ? "call failed" : "missing phone number");
166: }
167:
1.4 deraadt 168: void
1.1 deraadt 169: disconnect(reason)
170: char *reason;
171: {
172: if (!conflag) {
173: logent(value(HOST), "", DV, "call terminated");
174: return;
175: }
176: if (reason == NOSTR) {
177: logent(value(HOST), "", acu->acu_name, "call terminated");
178: if (boolean(value(VERBOSE)))
179: printf("\r\ndisconnecting...");
180: } else
181: logent(value(HOST), "", acu->acu_name, reason);
182: (*acu->acu_disconnect)();
183: }
184:
185: static void
186: acuabort(s)
187: {
188: signal(s, SIG_IGN);
189: longjmp(jmpbuf, 1);
190: }
191:
192: static acu_t *
193: acutype(s)
194: register char *s;
195: {
196: register acu_t *p;
197: extern acu_t acutable[];
198:
199: for (p = acutable; p->acu_name != '\0'; p++)
200: if (!strcmp(s, p->acu_name))
201: return (p);
202: return (NOACU);
203: }