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