Home | History | Annotate | Line # | Download | only in tip
      1 /*	$NetBSD: acu.c,v 1.18 2021/09/11 20:28:06 andvar Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)acu.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: acu.c,v 1.18 2021/09/11 20:28:06 andvar Exp $");
     38 #endif /* not lint */
     39 
     40 #include "tip.h"
     41 
     42 static acu_t *acu = NULL;
     43 static int conflag;
     44 static jmp_buf jmpbuf;
     45 
     46 __dead static void	acuabort(int);
     47 static acu_t   *acutype(char *);
     48 
     49 /*
     50  * Establish connection for tip
     51  *
     52  * If DU is true, we should dial an ACU whose type is AT.
     53  * The phone numbers are in PN, and the call unit is in CU.
     54  *
     55  * If the PN is an '@', then we consult the PHONES file for
     56  *   the phone numbers.  This file is /etc/phones, unless overridden.
     57  *   by an exported shell variable.
     58  *
     59  * The data base files must be in the format:
     60  *	host-name[ \t]*phone-number
     61  *   with the possibility of multiple phone numbers
     62  *   for a single host acting as a rotary (in the order
     63  *   found in the file).
     64  */
     65 const char *
     66 tip_connect(void)
     67 {
     68 	char * volatile cp;
     69 	char *phnum, string[256];
     70 	FILE *fd;
     71 	int volatile tried;
     72 
     73 	cp = PN;
     74 	tried = 0;
     75 
     76 	if (!DU) {		/* regular connect message */
     77 		if (CM != NULL)
     78 			xpwrite(FD, CM, strlen(CM));
     79 		return (NULL);
     80 	}
     81 	/*
     82 	 * @ =>'s use data base in PHONES environment variable
     83 	 *        otherwise, use /etc/phones
     84 	 */
     85 	(void)signal(SIGINT, acuabort);
     86 	(void)signal(SIGQUIT, acuabort);
     87 	if (setjmp(jmpbuf)) {
     88 		(void)signal(SIGINT, SIG_IGN);
     89 		(void)signal(SIGQUIT, SIG_IGN);
     90 		(void)printf("\ncall aborted\n");
     91 		if (acu != NULL) {
     92 			setboolean(value(VERBOSE), FALSE);
     93 			if (conflag)
     94 				disconnect(NULL);
     95 			else
     96 				(*acu->acu_abort)();
     97 		}
     98 		return ("interrupt");
     99 	}
    100 	if ((acu = acutype(AT)) == NULL)
    101 		return ("unknown ACU type");
    102 	if (*cp != '@') {
    103 		while (*cp) {
    104 			for (phnum = cp; *cp && *cp != ','; cp++)
    105 				;
    106 			if (*cp)
    107 				*cp++ = '\0';
    108 
    109 			if ((conflag = (*acu->acu_dialer)(phnum, CU)) != 0) {
    110 				if (CM != NULL)
    111 					xpwrite(FD, CM, strlen(CM));
    112 				return (NULL);
    113 			} else
    114 			tried++;
    115 		}
    116 	} else {
    117 		if ((fd = fopen(PH, "r")) == NULL) {
    118 			(void)printf("%s: ", PH);
    119 			return ("can't open phone number file");
    120 		}
    121 		while (fgets(string, sizeof(string), fd) != NULL) {
    122 			for (cp = string; !any(*cp, " \t\n"); cp++)
    123 				;
    124 			if (*cp == '\n') {
    125 				(void)fclose(fd);
    126 				return ("unrecognizable host name");
    127 			}
    128 			*cp++ = '\0';
    129 			if (strcmp(string, value(HOST)))
    130 				continue;
    131 			while (any(*cp, " \t"))
    132 				cp++;
    133 			if (*cp == '\n') {
    134 				(void)fclose(fd);
    135 				return ("missing phone number");
    136 			}
    137 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
    138 				;
    139 			if (*cp)
    140 				*cp++ = '\0';
    141 
    142 			if ((conflag = (*acu->acu_dialer)(phnum, CU)) != 0) {
    143 				(void)fclose(fd);
    144 				if (CM != NULL)
    145 					xpwrite(FD, CM, strlen(CM));
    146 				return (NULL);
    147 			} else
    148 			tried++;
    149 		}
    150 		(void)fclose(fd);
    151 	}
    152 	if (!tried) {
    153 	}
    154 	else
    155 		(*acu->acu_abort)();
    156 	return (tried ? "call failed" : "missing phone number");
    157 }
    158 
    159 void
    160 disconnect(const char *reason)
    161 {
    162 
    163 	if (!conflag) {
    164 		return;
    165 	}
    166 	if (reason == NULL) {
    167 		if (boolean(value(VERBOSE)))
    168 			(void)printf("\r\ndisconnecting...");
    169 	} else
    170 	(*acu->acu_disconnect)();
    171 }
    172 
    173 static void
    174 acuabort(int s)
    175 {
    176 
    177 	(void)signal(s, SIG_IGN);
    178 	longjmp(jmpbuf, 1);
    179 }
    180 
    181 static acu_t *
    182 acutype(char *s)
    183 {
    184 	acu_t *p;
    185 
    186 	for (p = acutable; p->acu_name != NULL; p++)
    187 		if (!strcmp(s, p->acu_name))
    188 			return (p);
    189 	return (NULL);
    190 }
    191