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