cosmetics
[unix-history] / usr / src / usr.bin / tip / acu.c
CommitLineData
d8feebc2 1/* acu.c 4.4 81/11/20 */
f6a65350
BJ
2#include "tip.h"
3#include <setjmp.h>
4
5static acu_t *acu = NOACU;
6static int conflag;
7static int acuabort();
8static acu_t *acutype();
9static jmp_buf jmpbuf;
10/*
11 * Establish connection for tip
12 *
13 * If DU is true, we should dial an ACU whose type is AT.
14 * The phone numbers are in PN, and the call unit is in CU.
15 *
16 * If the PN is an '@', then we consult the PHONES file for
17 * the phone numbers. This file is /etc/phones, unless overriden
18 * by an exported shell variable.
19 *
20 * The data base files must be in the format:
21 * host-name[ \t]*phone-number
22 * with the possibility of multiple phone numbers
23 * for a single host acting as a rotary (in the order
24 * found in the file).
25 */
26char *
27connect()
28{
29 register char *cp = PN;
30 char *phnum, string[256];
31 FILE *fd;
32 int tried = 0;
33
34 if (!DU) { /* regular connect message */
35 if (CM != NOSTR)
36 write(FD, cp, size(CM));
37 return(NOSTR);
38 }
39 /*
40 * @ =>'s use data base in PHONES environment variable
41 * otherwise, use /etc/phones
42 */
43 signal(SIGINT, acuabort);
44 signal(SIGQUIT, acuabort);
45 if (setjmp(jmpbuf)) {
46 signal(SIGINT, SIG_IGN);
47 signal(SIGQUIT, SIG_IGN);
48 printf("\ncall aborted\n");
49 logent(value(HOST), "", "", "call aborted");
50 if (acu != NOACU) {
51 boolean(value(VERBOSE)) = FALSE;
52 if (conflag)
53 disconnect();
54 else
55 (*acu->acu_abort)();
56 }
57 delock(uucplock);
58 exit(1);
59 }
60 if ((acu = acutype(AT)) == NOACU)
61 return("unknown ACU type");
62 if (*cp != '@') {
63 while (*cp) {
64 for (phnum = cp; any(*cp, "0123456789-*="); cp++)
65 ;
66 *cp++ = '\0';
67
68 if (conflag = (*acu->acu_dialer)(phnum, CU)) {
69 logent(value(HOST), phnum, acu->acu_name,
70 "call completed");
71 return(NOSTR);
72 } else
73 logent(value(HOST), phnum, acu->acu_name,
74 "no answer");
75 tried++;
76 }
77 } else {
78 if ((fd = fopen(PH, "r")) == NOFILE) {
79 printf("%s: ", PH);
80 return("can't open phone number file");
81 }
82 while (fgets(string, sizeof(string), fd) != NOSTR) {
83 for (cp = string; !any(*cp, " \t\n"); cp++)
84 ;
85 if (*cp == '\n') {
86 fclose(fd);
87 return("unrecognizable host name");
88 }
89 *cp++ = '\0';
90 if (strcmp(string, value(HOST)))
91 continue;
92 while (any(*cp, " \t"))
93 cp++;
94 if (*cp == '\n') {
95 fclose(fd);
96 return("missing phone number");
97 }
98 for (phnum = cp; any(*cp, "0123456789-*="); cp++)
99 ;
100 *cp = '\0';
101
bc6f2b39 102 if (conflag = (*acu->acu_dialer)(phnum, CU)) {
f6a65350
BJ
103 fclose(fd);
104 logent(value(HOST), phnum, acu->acu_name,
105 "call completed");
106 return(NOSTR);
107 } else
108 logent(value(HOST), phnum, acu->acu_name,
109 "no answer");
110 tried++;
111 }
112 fclose(fd);
113 }
114 if (!tried)
115 logent(value(HOST), "", acu->acu_name, "missing phone number");
116 return(tried ? "no answer" : "missing phone number");
117}
118
119disconnect()
120{
121 if (!conflag)
122 return;
123 logent(value(HOST), "", acu->acu_name, "call terminated");
124 if (boolean(value(VERBOSE)))
125 printf("\r\ndisconnecting...");
126 (*acu->acu_disconnect)();
127}
128
129static int
130acuabort(s)
131{
132 signal(s, SIG_IGN);
3a779b68 133 longjmp(jmpbuf, 1);
f6a65350
BJ
134}
135
136static acu_t *
137acutype(s)
d8feebc2 138 register char *s;
f6a65350
BJ
139{
140 register acu_t *p;
141 extern acu_t acutable[];
142
143 for (p = acutable; p->acu_name != '\0'; p++)
144 if (!strcmp(s, p->acu_name))
145 return(p);
146 return(NOACU);
147}