xref: /original-bsd/usr.bin/tip/acu.c (revision 33b18212)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)acu.c	5.6 (Berkeley) 09/02/88";
20 #endif /* not lint */
21 
22 #include "tip.h"
23 
24 static acu_t *acu = NOACU;
25 static int conflag;
26 static int acuabort();
27 static acu_t *acutype();
28 static jmp_buf jmpbuf;
29 /*
30  * Establish connection for tip
31  *
32  * If DU is true, we should dial an ACU whose type is AT.
33  * The phone numbers are in PN, and the call unit is in CU.
34  *
35  * If the PN is an '@', then we consult the PHONES file for
36  *   the phone numbers.  This file is /etc/phones, unless overriden
37  *   by an exported shell variable.
38  *
39  * The data base files must be in the format:
40  *	host-name[ \t]*phone-number
41  *   with the possibility of multiple phone numbers
42  *   for a single host acting as a rotary (in the order
43  *   found in the file).
44  */
45 char *
46 connect()
47 {
48 	register char *cp = PN;
49 	char *phnum, string[256];
50 	FILE *fd;
51 	int tried = 0;
52 
53 	if (!DU) {		/* regular connect message */
54 		if (CM != NOSTR)
55 			pwrite(FD, CM, size(CM));
56 		logent(value(HOST), "", DV, "call completed");
57 		return (NOSTR);
58 	}
59 	/*
60 	 * @ =>'s use data base in PHONES environment variable
61 	 *        otherwise, use /etc/phones
62 	 */
63 	signal(SIGINT, acuabort);
64 	signal(SIGQUIT, acuabort);
65 	if (setjmp(jmpbuf)) {
66 		signal(SIGINT, SIG_IGN);
67 		signal(SIGQUIT, SIG_IGN);
68 		printf("\ncall aborted\n");
69 		logent(value(HOST), "", "", "call aborted");
70 		if (acu != NOACU) {
71 			boolean(value(VERBOSE)) = FALSE;
72 			if (conflag)
73 				disconnect(NOSTR);
74 			else
75 				(*acu->acu_abort)();
76 		}
77 		return ("interrupt");
78 	}
79 	if ((acu = acutype(AT)) == NOACU)
80 		return ("unknown ACU type");
81 	if (*cp != '@') {
82 		while (*cp) {
83 			for (phnum = cp; *cp && *cp != ','; cp++)
84 				;
85 			if (*cp)
86 				*cp++ = '\0';
87 
88 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
89 				logent(value(HOST), phnum, acu->acu_name,
90 					"call completed");
91 				return (NOSTR);
92 			} else
93 				logent(value(HOST), phnum, acu->acu_name,
94 					"call failed");
95 			tried++;
96 		}
97 	} else {
98 		if ((fd = fopen(PH, "r")) == NOFILE) {
99 			printf("%s: ", PH);
100 			return ("can't open phone number file");
101 		}
102 		while (fgets(string, sizeof(string), fd) != NOSTR) {
103 			for (cp = string; !any(*cp, " \t\n"); cp++)
104 				;
105 			if (*cp == '\n') {
106 				fclose(fd);
107 				return ("unrecognizable host name");
108 			}
109 			*cp++ = '\0';
110 			if (strcmp(string, value(HOST)))
111 				continue;
112 			while (any(*cp, " \t"))
113 				cp++;
114 			if (*cp == '\n') {
115 				fclose(fd);
116 				return ("missing phone number");
117 			}
118 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
119 				;
120 			if (*cp)
121 				*cp++ = '\0';
122 
123 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
124 				fclose(fd);
125 				logent(value(HOST), phnum, acu->acu_name,
126 					"call completed");
127 				return (NOSTR);
128 			} else
129 				logent(value(HOST), phnum, acu->acu_name,
130 					"call failed");
131 			tried++;
132 		}
133 		fclose(fd);
134 	}
135 	if (!tried)
136 		logent(value(HOST), "", acu->acu_name, "missing phone number");
137 	else
138 		(*acu->acu_abort)();
139 	return (tried ? "call failed" : "missing phone number");
140 }
141 
142 disconnect(reason)
143 	char *reason;
144 {
145 	if (!conflag) {
146 		logent(value(HOST), "", DV, "call terminated");
147 		return;
148 	}
149 	if (reason == NOSTR) {
150 		logent(value(HOST), "", acu->acu_name, "call terminated");
151 		if (boolean(value(VERBOSE)))
152 			printf("\r\ndisconnecting...");
153 	} else
154 		logent(value(HOST), "", acu->acu_name, reason);
155 	(*acu->acu_disconnect)();
156 }
157 
158 static int
159 acuabort(s)
160 {
161 	signal(s, SIG_IGN);
162 	longjmp(jmpbuf, 1);
163 }
164 
165 static acu_t *
166 acutype(s)
167 	register char *s;
168 {
169 	register acu_t *p;
170 	extern acu_t acutable[];
171 
172 	for (p = acutable; p->acu_name != '\0'; p++)
173 		if (!strcmp(s, p->acu_name))
174 			return (p);
175 	return (NOACU);
176 }
177