xref: /illumos-gate/usr/src/cmd/tip/acu.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
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 #include "tip.h"
13 
14 extern acu_t	acutable[];
15 
16 static acu_t *acu = NOACU;
17 static int conflag;
18 static void acuabort(int);
19 static acu_t *acutype(char *);
20 static sigjmp_buf jmpbuf;
21 
22 /*
23  * Establish connection for tip
24  *
25  * If DU is true, we should dial an ACU whose type is AT.
26  * The phone numbers are in PN, and the call unit is in CU.
27  *
28  * If the PN is an '@', then we consult the PHONES file for
29  *   the phone numbers.  This file is /etc/phones, unless overriden
30  *   by an exported shell variable.
31  *
32  * The data base files must be in the format:
33  *	host-name[ \t]*phone-number
34  *   with the possibility of multiple phone numbers
35  *   for a single host acting as a rotary (in the order
36  *   found in the file).
37  */
38 char *
39 connect(void)
40 {
41 	char *cp = PN;
42 	char *phnum, string[256];
43 	int tried = 0;
44 
45 	if (!DU)
46 		return (NOSTR);
47 	/*
48 	 * @ =>'s use data base in PHONES environment variable
49 	 *	  otherwise, use /etc/phones
50 	 */
51 	if (sigsetjmp(jmpbuf, 1)) {
52 		(void) signal(SIGINT, SIG_IGN);
53 		(void) signal(SIGQUIT, SIG_IGN);
54 		(void) printf("\ncall aborted\n");
55 		logent(value(HOST), "", "", "call aborted");
56 		if (acu != NOACU) {
57 			boolean(value(VERBOSE)) = FALSE;
58 			if (conflag)
59 				disconnect(NOSTR);
60 			else
61 				(*acu->acu_abort)();
62 		}
63 		myperm();
64 		delock(uucplock);
65 		exit(1);
66 	}
67 	(void) signal(SIGINT, acuabort);
68 	(void) signal(SIGQUIT, acuabort);
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 (phfd == NOFILE) {
89 			(void) printf("%s: ", PH);
90 			return ("can't open phone number file");
91 		}
92 		rewind(phfd);
93 		while (fgets(string, sizeof (string), phfd) != NOSTR) {
94 			if (string[0] == '#')
95 				continue;
96 			for (cp = string; !any(*cp, " \t\n"); cp++)
97 				;
98 			if (*cp == '\n')
99 				return ("unrecognizable host name");
100 			*cp++ = '\0';
101 			if (!equal(string, value(HOST)))
102 				continue;
103 			while (any(*cp, " \t"))
104 				cp++;
105 			if (*cp == '\n')
106 				return ("missing phone number");
107 			for (phnum = cp; *cp && *cp != '|' && *cp != '\n'; cp++)
108 				;
109 			*cp = '\0';
110 
111 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
112 				logent(value(HOST), phnum, acu->acu_name,
113 				    "call completed");
114 				return (NOSTR);
115 			} else
116 				logent(value(HOST), phnum, acu->acu_name,
117 				    "call failed");
118 			tried++;
119 		}
120 	}
121 	if (!tried)
122 		logent(value(HOST), "", acu->acu_name, "missing phone number");
123 	else
124 		(*acu->acu_abort)();
125 	return (tried ? "call failed" : "missing phone number");
126 }
127 
128 void
129 disconnect(char *reason)
130 {
131 	if (!conflag)
132 		return;
133 	if (reason == NOSTR) {
134 		logent(value(HOST), "", acu->acu_name, "call terminated");
135 		if (boolean(value(VERBOSE)))
136 			(void) printf("\r\ndisconnecting...");
137 	} else
138 		logent(value(HOST), "", acu->acu_name, reason);
139 	(*acu->acu_disconnect)();
140 }
141 
142 static void
143 acuabort(int s)
144 {
145 	(void) signal(s, SIG_IGN);
146 	siglongjmp(jmpbuf, 1);
147 }
148 
149 static acu_t *
150 acutype(char *s)
151 {
152 	acu_t *p;
153 
154 	if (s != NOSTR)
155 		for (p = acutable; p->acu_name != NULL; p++)
156 			if (equal(s, p->acu_name))
157 				return (p);
158 	return (NOACU);
159 }
160