xref: /original-bsd/usr.bin/tip/acu.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)acu.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "tip.h"
13 
14 static acu_t *acu = NOACU;
15 static int conflag;
16 static void 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 				if (CM != NOSTR)
80 					pwrite(FD, CM, size(CM));
81 				logent(value(HOST), phnum, acu->acu_name,
82 					"call completed");
83 				return (NOSTR);
84 			} else
85 				logent(value(HOST), phnum, acu->acu_name,
86 					"call failed");
87 			tried++;
88 		}
89 	} else {
90 		if ((fd = fopen(PH, "r")) == NOFILE) {
91 			printf("%s: ", PH);
92 			return ("can't open phone number file");
93 		}
94 		while (fgets(string, sizeof(string), fd) != NOSTR) {
95 			for (cp = string; !any(*cp, " \t\n"); cp++)
96 				;
97 			if (*cp == '\n') {
98 				fclose(fd);
99 				return ("unrecognizable host name");
100 			}
101 			*cp++ = '\0';
102 			if (strcmp(string, value(HOST)))
103 				continue;
104 			while (any(*cp, " \t"))
105 				cp++;
106 			if (*cp == '\n') {
107 				fclose(fd);
108 				return ("missing phone number");
109 			}
110 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
111 				;
112 			if (*cp)
113 				*cp++ = '\0';
114 
115 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
116 				fclose(fd);
117 				if (CM != NOSTR)
118 					pwrite(FD, CM, size(CM));
119 				logent(value(HOST), phnum, acu->acu_name,
120 					"call completed");
121 				return (NOSTR);
122 			} else
123 				logent(value(HOST), phnum, acu->acu_name,
124 					"call failed");
125 			tried++;
126 		}
127 		fclose(fd);
128 	}
129 	if (!tried)
130 		logent(value(HOST), "", acu->acu_name, "missing phone number");
131 	else
132 		(*acu->acu_abort)();
133 	return (tried ? "call failed" : "missing phone number");
134 }
135 
136 disconnect(reason)
137 	char *reason;
138 {
139 	if (!conflag) {
140 		logent(value(HOST), "", DV, "call terminated");
141 		return;
142 	}
143 	if (reason == NOSTR) {
144 		logent(value(HOST), "", acu->acu_name, "call terminated");
145 		if (boolean(value(VERBOSE)))
146 			printf("\r\ndisconnecting...");
147 	} else
148 		logent(value(HOST), "", acu->acu_name, reason);
149 	(*acu->acu_disconnect)();
150 }
151 
152 static void
153 acuabort(s)
154 {
155 	signal(s, SIG_IGN);
156 	longjmp(jmpbuf, 1);
157 }
158 
159 static acu_t *
160 acutype(s)
161 	register char *s;
162 {
163 	register acu_t *p;
164 	extern acu_t acutable[];
165 
166 	for (p = acutable; p->acu_name != '\0'; p++)
167 		if (!strcmp(s, p->acu_name))
168 			return (p);
169 	return (NOACU);
170 }
171