xref: /original-bsd/usr.bin/tip/acu.c (revision 7468e195)
103f490e8Sdist /*
2*7468e195Sbostic  * Copyright (c) 1983, 1993
3*7468e195Sbostic  *	The Regents of the University of California.  All rights reserved.
455947aa5Sbostic  *
5068cc2b8Sbostic  * %sccs.include.redist.c%
603f490e8Sdist  */
703f490e8Sdist 
8b0a04719Ssam #ifndef lint
9*7468e195Sbostic static char sccsid[] = "@(#)acu.c	8.1 (Berkeley) 06/06/93";
1055947aa5Sbostic #endif /* not lint */
1183937133Sroot 
12b0a04719Ssam #include "tip.h"
13b0a04719Ssam 
1483937133Sroot static acu_t *acu = NOACU;
1583937133Sroot static int conflag;
16cef0edb6Sbostic static void acuabort();
1783937133Sroot static acu_t *acutype();
1883937133Sroot static jmp_buf jmpbuf;
1983937133Sroot /*
2083937133Sroot  * Establish connection for tip
2183937133Sroot  *
2283937133Sroot  * If DU is true, we should dial an ACU whose type is AT.
2383937133Sroot  * The phone numbers are in PN, and the call unit is in CU.
2483937133Sroot  *
2583937133Sroot  * If the PN is an '@', then we consult the PHONES file for
2683937133Sroot  *   the phone numbers.  This file is /etc/phones, unless overriden
2783937133Sroot  *   by an exported shell variable.
2883937133Sroot  *
2983937133Sroot  * The data base files must be in the format:
3083937133Sroot  *	host-name[ \t]*phone-number
3183937133Sroot  *   with the possibility of multiple phone numbers
3283937133Sroot  *   for a single host acting as a rotary (in the order
3383937133Sroot  *   found in the file).
3483937133Sroot  */
3583937133Sroot char *
connect()3683937133Sroot connect()
3783937133Sroot {
3883937133Sroot 	register char *cp = PN;
3983937133Sroot 	char *phnum, string[256];
4083937133Sroot 	FILE *fd;
4183937133Sroot 	int tried = 0;
4283937133Sroot 
4383937133Sroot 	if (!DU) {		/* regular connect message */
4483937133Sroot 		if (CM != NOSTR)
45dca4632aSdonn 			pwrite(FD, CM, size(CM));
4683070de0Smarc 		logent(value(HOST), "", DV, "call completed");
4783937133Sroot 		return (NOSTR);
4883937133Sroot 	}
4983937133Sroot 	/*
5083937133Sroot 	 * @ =>'s use data base in PHONES environment variable
5183937133Sroot 	 *        otherwise, use /etc/phones
5283937133Sroot 	 */
5383937133Sroot 	signal(SIGINT, acuabort);
5483937133Sroot 	signal(SIGQUIT, acuabort);
5583937133Sroot 	if (setjmp(jmpbuf)) {
5683937133Sroot 		signal(SIGINT, SIG_IGN);
5783937133Sroot 		signal(SIGQUIT, SIG_IGN);
5883937133Sroot 		printf("\ncall aborted\n");
5983937133Sroot 		logent(value(HOST), "", "", "call aborted");
6083937133Sroot 		if (acu != NOACU) {
6183937133Sroot 			boolean(value(VERBOSE)) = FALSE;
6283937133Sroot 			if (conflag)
635a9fa380Ssam 				disconnect(NOSTR);
6483937133Sroot 			else
6583937133Sroot 				(*acu->acu_abort)();
6683937133Sroot 		}
6708fd8f39Skarels 		return ("interrupt");
6883937133Sroot 	}
6983937133Sroot 	if ((acu = acutype(AT)) == NOACU)
7083937133Sroot 		return ("unknown ACU type");
7183937133Sroot 	if (*cp != '@') {
7283937133Sroot 		while (*cp) {
73dca4632aSdonn 			for (phnum = cp; *cp && *cp != ','; cp++)
7483937133Sroot 				;
75bb76d836Sshannon 			if (*cp)
7683937133Sroot 				*cp++ = '\0';
7783937133Sroot 
7883937133Sroot 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
79a57ca4bdShibler 				if (CM != NOSTR)
80a57ca4bdShibler 					pwrite(FD, CM, size(CM));
8183937133Sroot 				logent(value(HOST), phnum, acu->acu_name,
8283937133Sroot 					"call completed");
8383937133Sroot 				return (NOSTR);
8483937133Sroot 			} else
8583937133Sroot 				logent(value(HOST), phnum, acu->acu_name,
8604196026Ssam 					"call failed");
8783937133Sroot 			tried++;
8883937133Sroot 		}
8983937133Sroot 	} else {
9083937133Sroot 		if ((fd = fopen(PH, "r")) == NOFILE) {
9183937133Sroot 			printf("%s: ", PH);
9283937133Sroot 			return ("can't open phone number file");
9383937133Sroot 		}
9483937133Sroot 		while (fgets(string, sizeof(string), fd) != NOSTR) {
9583937133Sroot 			for (cp = string; !any(*cp, " \t\n"); cp++)
9683937133Sroot 				;
9783937133Sroot 			if (*cp == '\n') {
9883937133Sroot 				fclose(fd);
9983937133Sroot 				return ("unrecognizable host name");
10083937133Sroot 			}
10183937133Sroot 			*cp++ = '\0';
10283937133Sroot 			if (strcmp(string, value(HOST)))
10383937133Sroot 				continue;
10483937133Sroot 			while (any(*cp, " \t"))
10583937133Sroot 				cp++;
10683937133Sroot 			if (*cp == '\n') {
10783937133Sroot 				fclose(fd);
10883937133Sroot 				return ("missing phone number");
10983937133Sroot 			}
110d1bcc19dSdonn 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
11183937133Sroot 				;
112d1bcc19dSdonn 			if (*cp)
113d1bcc19dSdonn 				*cp++ = '\0';
11483937133Sroot 
11586181237Ssam 			if (conflag = (*acu->acu_dialer)(phnum, CU)) {
11683937133Sroot 				fclose(fd);
117a57ca4bdShibler 				if (CM != NOSTR)
118a57ca4bdShibler 					pwrite(FD, CM, size(CM));
11983937133Sroot 				logent(value(HOST), phnum, acu->acu_name,
12083937133Sroot 					"call completed");
12183937133Sroot 				return (NOSTR);
12283937133Sroot 			} else
12383937133Sroot 				logent(value(HOST), phnum, acu->acu_name,
12404196026Ssam 					"call failed");
12583937133Sroot 			tried++;
12683937133Sroot 		}
12783937133Sroot 		fclose(fd);
12883937133Sroot 	}
12983937133Sroot 	if (!tried)
13083937133Sroot 		logent(value(HOST), "", acu->acu_name, "missing phone number");
131a8864971Sralph 	else
132a8864971Sralph 		(*acu->acu_abort)();
13304196026Ssam 	return (tried ? "call failed" : "missing phone number");
13483937133Sroot }
13583937133Sroot 
disconnect(reason)1365a9fa380Ssam disconnect(reason)
1375a9fa380Ssam 	char *reason;
13883937133Sroot {
13983070de0Smarc 	if (!conflag) {
14083070de0Smarc 		logent(value(HOST), "", DV, "call terminated");
14183937133Sroot 		return;
14283070de0Smarc 	}
14309aaf4ebSsam 	if (reason == NOSTR) {
14483937133Sroot 		logent(value(HOST), "", acu->acu_name, "call terminated");
14583937133Sroot 		if (boolean(value(VERBOSE)))
14683937133Sroot 			printf("\r\ndisconnecting...");
1475a9fa380Ssam 	} else
1485a9fa380Ssam 		logent(value(HOST), "", acu->acu_name, reason);
14983937133Sroot 	(*acu->acu_disconnect)();
15083937133Sroot }
15183937133Sroot 
152cef0edb6Sbostic static void
acuabort(s)15383937133Sroot acuabort(s)
15483937133Sroot {
15583937133Sroot 	signal(s, SIG_IGN);
15638f7c0adSsam 	longjmp(jmpbuf, 1);
15783937133Sroot }
15883937133Sroot 
15983937133Sroot static acu_t *
acutype(s)16083937133Sroot acutype(s)
16183937133Sroot 	register char *s;
16283937133Sroot {
16383937133Sroot 	register acu_t *p;
16483937133Sroot 	extern acu_t acutable[];
16583937133Sroot 
16683937133Sroot 	for (p = acutable; p->acu_name != '\0'; p++)
16783937133Sroot 		if (!strcmp(s, p->acu_name))
16883937133Sroot 			return (p);
16983937133Sroot 	return (NOACU);
17083937133Sroot }
171