always die, we can't survive some of the compile errors
[unix-history] / usr / src / usr.bin / tip / acu.c
CommitLineData
d0aeaf5a 1/*
c9686c12
KB
2 * Copyright (c) 1983 The Regents of the University of California.
3 * All rights reserved.
4 *
cb956e54 5 * %sccs.include.redist.c%
d0aeaf5a
DF
6 */
7
05862919 8#ifndef lint
010bfa9a 9static char sccsid[] = "@(#)acu.c 5.8 (Berkeley) %G%";
c9686c12 10#endif /* not lint */
05862919 11
f6a65350 12#include "tip.h"
f6a65350
BJ
13
14static acu_t *acu = NOACU;
15static int conflag;
010bfa9a 16static void acuabort();
f6a65350
BJ
17static acu_t *acutype();
18static jmp_buf jmpbuf;
19/*
20 * Establish connection for tip
21 *
22 * If DU is true, we should dial an ACU whose type is AT.
23 * The phone numbers are in PN, and the call unit is in CU.
24 *
25 * If the PN is an '@', then we consult the PHONES file for
26 * the phone numbers. This file is /etc/phones, unless overriden
27 * by an exported shell variable.
28 *
29 * The data base files must be in the format:
30 * host-name[ \t]*phone-number
31 * with the possibility of multiple phone numbers
32 * for a single host acting as a rotary (in the order
33 * found in the file).
34 */
35char *
36connect()
37{
38 register char *cp = PN;
39 char *phnum, string[256];
40 FILE *fd;
41 int tried = 0;
42
43 if (!DU) { /* regular connect message */
44 if (CM != NOSTR)
8a0ce7f1 45 pwrite(FD, CM, size(CM));
7bc815c0 46 logent(value(HOST), "", DV, "call completed");
3f48242d 47 return (NOSTR);
f6a65350
BJ
48 }
49 /*
50 * @ =>'s use data base in PHONES environment variable
51 * otherwise, use /etc/phones
52 */
53 signal(SIGINT, acuabort);
54 signal(SIGQUIT, acuabort);
55 if (setjmp(jmpbuf)) {
56 signal(SIGINT, SIG_IGN);
57 signal(SIGQUIT, SIG_IGN);
58 printf("\ncall aborted\n");
59 logent(value(HOST), "", "", "call aborted");
60 if (acu != NOACU) {
61 boolean(value(VERBOSE)) = FALSE;
62 if (conflag)
e2326c44 63 disconnect(NOSTR);
f6a65350
BJ
64 else
65 (*acu->acu_abort)();
66 }
45d9e9af 67 return ("interrupt");
f6a65350
BJ
68 }
69 if ((acu = acutype(AT)) == NOACU)
3f48242d 70 return ("unknown ACU type");
f6a65350
BJ
71 if (*cp != '@') {
72 while (*cp) {
8a0ce7f1 73 for (phnum = cp; *cp && *cp != ','; cp++)
f6a65350 74 ;
51f98017
BS
75 if (*cp)
76 *cp++ = '\0';
f6a65350
BJ
77
78 if (conflag = (*acu->acu_dialer)(phnum, CU)) {
79 logent(value(HOST), phnum, acu->acu_name,
80 "call completed");
3f48242d 81 return (NOSTR);
f6a65350
BJ
82 } else
83 logent(value(HOST), phnum, acu->acu_name,
03bdc5a9 84 "call failed");
f6a65350
BJ
85 tried++;
86 }
87 } else {
88 if ((fd = fopen(PH, "r")) == NOFILE) {
89 printf("%s: ", PH);
3f48242d 90 return ("can't open phone number file");
f6a65350
BJ
91 }
92 while (fgets(string, sizeof(string), fd) != NOSTR) {
93 for (cp = string; !any(*cp, " \t\n"); cp++)
94 ;
95 if (*cp == '\n') {
96 fclose(fd);
3f48242d 97 return ("unrecognizable host name");
f6a65350
BJ
98 }
99 *cp++ = '\0';
100 if (strcmp(string, value(HOST)))
101 continue;
102 while (any(*cp, " \t"))
103 cp++;
104 if (*cp == '\n') {
105 fclose(fd);
3f48242d 106 return ("missing phone number");
f6a65350 107 }
5c8b4e1e 108 for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
f6a65350 109 ;
5c8b4e1e
DS
110 if (*cp)
111 *cp++ = '\0';
f6a65350 112
bc6f2b39 113 if (conflag = (*acu->acu_dialer)(phnum, CU)) {
f6a65350
BJ
114 fclose(fd);
115 logent(value(HOST), phnum, acu->acu_name,
116 "call completed");
3f48242d 117 return (NOSTR);
f6a65350
BJ
118 } else
119 logent(value(HOST), phnum, acu->acu_name,
03bdc5a9 120 "call failed");
f6a65350
BJ
121 tried++;
122 }
123 fclose(fd);
124 }
125 if (!tried)
126 logent(value(HOST), "", acu->acu_name, "missing phone number");
92ee31b0
RC
127 else
128 (*acu->acu_abort)();
03bdc5a9 129 return (tried ? "call failed" : "missing phone number");
f6a65350
BJ
130}
131
e2326c44
SL
132disconnect(reason)
133 char *reason;
f6a65350 134{
7bc815c0
MT
135 if (!conflag) {
136 logent(value(HOST), "", DV, "call terminated");
f6a65350 137 return;
7bc815c0 138 }
631a2290 139 if (reason == NOSTR) {
e2326c44
SL
140 logent(value(HOST), "", acu->acu_name, "call terminated");
141 if (boolean(value(VERBOSE)))
142 printf("\r\ndisconnecting...");
143 } else
144 logent(value(HOST), "", acu->acu_name, reason);
f6a65350
BJ
145 (*acu->acu_disconnect)();
146}
147
010bfa9a 148static void
f6a65350
BJ
149acuabort(s)
150{
151 signal(s, SIG_IGN);
3a779b68 152 longjmp(jmpbuf, 1);
f6a65350
BJ
153}
154
155static acu_t *
156acutype(s)
d8feebc2 157 register char *s;
f6a65350
BJ
158{
159 register acu_t *p;
160 extern acu_t acutable[];
161
162 for (p = acutable; p->acu_name != '\0'; p++)
163 if (!strcmp(s, p->acu_name))
3f48242d
SL
164 return (p);
165 return (NOACU);
f6a65350 166}