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