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