xref: /titanic_44/usr/src/cmd/tip/acu.c (revision 9dd0f810214fdc8e1af881a9a5c4b6927629ff9e)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1983 Regents of the University of California.
8  * All rights reserved.  The Berkeley software License Agreement
9  * specifies the terms and conditions for redistribution.
10  */
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #include "tip.h"
15 
16 extern acu_t	acutable[];
17 
18 static acu_t *acu = NOACU;
19 static int conflag;
20 static void acuabort(int);
21 static acu_t *acutype(char *);
22 static sigjmp_buf jmpbuf;
23 
24 /*
25  * Establish connection for tip
26  *
27  * If DU is true, we should dial an ACU whose type is AT.
28  * The phone numbers are in PN, and the call unit is in CU.
29  *
30  * If the PN is an '@', then we consult the PHONES file for
31  *   the phone numbers.  This file is /etc/phones, unless overriden
32  *   by an exported shell variable.
33  *
34  * The data base files must be in the format:
35  *	host-name[ \t]*phone-number
36  *   with the possibility of multiple phone numbers
37  *   for a single host acting as a rotary (in the order
38  *   found in the file).
39  */
40 char *
41 connect(void)
42 {
43 	char *cp = PN;
44 	char *phnum, string[256];
45 	int tried = 0;
46 
47 	if (!DU)
48 		return (NOSTR);
49 	/*
50 	 * @ =>'s use data base in PHONES environment variable
51 	 *	  otherwise, use /etc/phones
52 	 */
53 	if (sigsetjmp(jmpbuf, 1)) {
54 		(void) signal(SIGINT, SIG_IGN);
55 		(void) signal(SIGQUIT, SIG_IGN);
56 		(void) 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 		myperm();
66 		delock(uucplock);
67 		exit(1);
68 	}
69 	(void) signal(SIGINT, acuabort);
70 	(void) signal(SIGQUIT, acuabort);
71 	if ((acu = acutype(AT)) == NOACU)
72 		return ("unknown ACU type");
73 	if (*cp != '@') {
74 		while (*cp) {
75 			for (phnum = cp; *cp && *cp != '|'; cp++)
76 				;
77 			if (*cp)
78 				*cp++ = '\0';
79 
80 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
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 (phfd == NOFILE) {
91 			(void) printf("%s: ", PH);
92 			return ("can't open phone number file");
93 		}
94 		rewind(phfd);
95 		while (fgets(string, sizeof (string), phfd) != NOSTR) {
96 			if (string[0] == '#')
97 				continue;
98 			for (cp = string; !any(*cp, " \t\n"); cp++)
99 				;
100 			if (*cp == '\n')
101 				return ("unrecognizable host name");
102 			*cp++ = '\0';
103 			if (!equal(string, value(HOST)))
104 				continue;
105 			while (any(*cp, " \t"))
106 				cp++;
107 			if (*cp == '\n')
108 				return ("missing phone number");
109 			for (phnum = cp; *cp && *cp != '|' && *cp != '\n'; cp++)
110 				;
111 			*cp = '\0';
112 
113 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
114 				logent(value(HOST), phnum, acu->acu_name,
115 				    "call completed");
116 				return (NOSTR);
117 			} else
118 				logent(value(HOST), phnum, acu->acu_name,
119 				    "call failed");
120 			tried++;
121 		}
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 void
131 disconnect(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 			(void) printf("\r\ndisconnecting...");
139 	} else
140 		logent(value(HOST), "", acu->acu_name, reason);
141 	(*acu->acu_disconnect)();
142 }
143 
144 static void
145 acuabort(int s)
146 {
147 	(void) signal(s, SIG_IGN);
148 	siglongjmp(jmpbuf, 1);
149 }
150 
151 static acu_t *
152 acutype(char *s)
153 {
154 	acu_t *p;
155 
156 	if (s != NOSTR)
157 		for (p = acutable; p->acu_name != '\0'; p++)
158 			if (equal(s, p->acu_name))
159 				return (p);
160 	return (NOACU);
161 }
162