xref: /original-bsd/usr.bin/tip/acu.c (revision 7a8f01dc)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)acu.c	5.7 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 #include "tip.h"
13 
14 static acu_t *acu = NOACU;
15 static int conflag;
16 static int acuabort();
17 static acu_t *acutype();
18 static 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  */
35 char *
36 connect()
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)
45 			pwrite(FD, CM, size(CM));
46 		logent(value(HOST), "", DV, "call completed");
47 		return (NOSTR);
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)
63 				disconnect(NOSTR);
64 			else
65 				(*acu->acu_abort)();
66 		}
67 		return ("interrupt");
68 	}
69 	if ((acu = acutype(AT)) == NOACU)
70 		return ("unknown ACU type");
71 	if (*cp != '@') {
72 		while (*cp) {
73 			for (phnum = cp; *cp && *cp != ','; cp++)
74 				;
75 			if (*cp)
76 				*cp++ = '\0';
77 
78 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
79 				logent(value(HOST), phnum, acu->acu_name,
80 					"call completed");
81 				return (NOSTR);
82 			} else
83 				logent(value(HOST), phnum, acu->acu_name,
84 					"call failed");
85 			tried++;
86 		}
87 	} else {
88 		if ((fd = fopen(PH, "r")) == NOFILE) {
89 			printf("%s: ", PH);
90 			return ("can't open phone number file");
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);
97 				return ("unrecognizable host name");
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);
106 				return ("missing phone number");
107 			}
108 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
109 				;
110 			if (*cp)
111 				*cp++ = '\0';
112 
113 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
114 				fclose(fd);
115 				logent(value(HOST), phnum, acu->acu_name,
116 					"call completed");
117 				return (NOSTR);
118 			} else
119 				logent(value(HOST), phnum, acu->acu_name,
120 					"call failed");
121 			tried++;
122 		}
123 		fclose(fd);
124 	}
125 	if (!tried)
126 		logent(value(HOST), "", acu->acu_name, "missing phone number");
127 	else
128 		(*acu->acu_abort)();
129 	return (tried ? "call failed" : "missing phone number");
130 }
131 
132 disconnect(reason)
133 	char *reason;
134 {
135 	if (!conflag) {
136 		logent(value(HOST), "", DV, "call terminated");
137 		return;
138 	}
139 	if (reason == NOSTR) {
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);
145 	(*acu->acu_disconnect)();
146 }
147 
148 static int
149 acuabort(s)
150 {
151 	signal(s, SIG_IGN);
152 	longjmp(jmpbuf, 1);
153 }
154 
155 static acu_t *
156 acutype(s)
157 	register char *s;
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))
164 			return (p);
165 	return (NOACU);
166 }
167