Home | History | Annotate | Line # | Download | only in tip
acu.c revision 1.10
      1 /*	$NetBSD: acu.c,v 1.10 2006/04/02 19:16:22 tls 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.10 2006/04/02 19:16:22 tls 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 static void	acuabort __P((int));
     47 static acu_t   *acutype __P((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 overriden
     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 connect()
     67 {
     68 	char *cp = PN;
     69 	char *phnum, string[256];
     70 	FILE *fd;
     71 	int tried = 0;
     72 
     73 #if __GNUC__		/* XXX pacify gcc */
     74 	(void)&cp;
     75 	(void)&tried;
     76 #endif
     77 
     78 	if (!DU) {		/* regular connect message */
     79 		if (CM != NULL)
     80 			xpwrite(FD, CM, strlen(CM));
     81 #ifdef ACULOG
     82 		logent(value(HOST), "", DV, "call completed");
     83 #endif
     84 		return (NULL);
     85 	}
     86 	/*
     87 	 * @ =>'s use data base in PHONES environment variable
     88 	 *        otherwise, use /etc/phones
     89 	 */
     90 	signal(SIGINT, acuabort);
     91 	signal(SIGQUIT, acuabort);
     92 	if (setjmp(jmpbuf)) {
     93 		signal(SIGINT, SIG_IGN);
     94 		signal(SIGQUIT, SIG_IGN);
     95 		printf("\ncall aborted\n");
     96 #ifdef ACULOG
     97 		logent(value(HOST), "", "", "call aborted");
     98 #endif
     99 		if (acu != NULL) {
    100 			setboolean(value(VERBOSE), FALSE);
    101 			if (conflag)
    102 				disconnect(NULL);
    103 			else
    104 				(*acu->acu_abort)();
    105 		}
    106 		return ("interrupt");
    107 	}
    108 	if ((acu = acutype(AT)) == NULL)
    109 		return ("unknown ACU type");
    110 	if (*cp != '@') {
    111 		while (*cp) {
    112 			for (phnum = cp; *cp && *cp != ','; cp++)
    113 				;
    114 			if (*cp)
    115 				*cp++ = '\0';
    116 
    117 			if ((conflag = (*acu->acu_dialer)(phnum, CU)) != 0) {
    118 				if (CM != NULL)
    119 					xpwrite(FD, CM, strlen(CM));
    120 #ifdef ACULOG
    121 				logent(value(HOST), phnum, acu->acu_name,
    122 					"call completed");
    123 #endif
    124 				return (NULL);
    125 			} else
    126 #ifdef ACULOG
    127 				logent(value(HOST), phnum, acu->acu_name,
    128 					"call failed");
    129 #endif
    130 			tried++;
    131 		}
    132 	} else {
    133 		if ((fd = fopen(PH, "r")) == NULL) {
    134 			printf("%s: ", PH);
    135 			return ("can't open phone number file");
    136 		}
    137 		while (fgets(string, sizeof(string), fd) != NULL) {
    138 			for (cp = string; !any(*cp, " \t\n"); cp++)
    139 				;
    140 			if (*cp == '\n') {
    141 				fclose(fd);
    142 				return ("unrecognizable host name");
    143 			}
    144 			*cp++ = '\0';
    145 			if (strcmp(string, value(HOST)))
    146 				continue;
    147 			while (any(*cp, " \t"))
    148 				cp++;
    149 			if (*cp == '\n') {
    150 				fclose(fd);
    151 				return ("missing phone number");
    152 			}
    153 			for (phnum = cp; *cp && *cp != ',' && *cp != '\n'; cp++)
    154 				;
    155 			if (*cp)
    156 				*cp++ = '\0';
    157 
    158 			if ((conflag = (*acu->acu_dialer)(phnum, CU)) != 0) {
    159 				fclose(fd);
    160 				if (CM != NULL)
    161 					xpwrite(FD, CM, strlen(CM));
    162 #ifdef ACULOG
    163 				logent(value(HOST), phnum, acu->acu_name,
    164 					"call completed");
    165 #endif
    166 				return (NULL);
    167 			} else
    168 #ifdef ACULOG
    169 				logent(value(HOST), phnum, acu->acu_name,
    170 					"call failed");
    171 #endif
    172 			tried++;
    173 		}
    174 		fclose(fd);
    175 	}
    176 	if (!tried) {
    177 #ifdef ACULOG
    178 		logent(value(HOST), "", acu->acu_name, "missing phone number");
    179 #endif
    180 	}
    181 	else
    182 		(*acu->acu_abort)();
    183 	return (tried ? "call failed" : "missing phone number");
    184 }
    185 
    186 void
    187 disconnect(reason)
    188 	const char *reason;
    189 {
    190 
    191 	if (!conflag) {
    192 #ifdef ACULOG
    193 		logent(value(HOST), "", DV, "call terminated");
    194 #endif
    195 		return;
    196 	}
    197 	if (reason == NULL) {
    198 #ifdef ACULOG
    199 		logent(value(HOST), "", acu->acu_name, "call terminated");
    200 #endif
    201 		if (boolean(value(VERBOSE)))
    202 			printf("\r\ndisconnecting...");
    203 	} else
    204 #ifdef ACULOG
    205 		logent(value(HOST), "", acu->acu_name, reason);
    206 #endif
    207 	(*acu->acu_disconnect)();
    208 }
    209 
    210 static void
    211 acuabort(s)
    212 	int s;
    213 {
    214 
    215 	signal(s, SIG_IGN);
    216 	longjmp(jmpbuf, 1);
    217 }
    218 
    219 static acu_t *
    220 acutype(s)
    221 	char *s;
    222 {
    223 	acu_t *p;
    224 
    225 	for (p = acutable; p->acu_name != '\0'; p++)
    226 		if (!strcmp(s, p->acu_name))
    227 			return (p);
    228 	return (NULL);
    229 }
    230