Home | History | Annotate | Line # | Download | only in aculib
biz22.c revision 1.8
      1 /*	$NetBSD: biz22.c,v 1.8 2003/08/07 11:16:21 agc 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[] = "@(#)biz22.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: biz22.c,v 1.8 2003/08/07 11:16:21 agc Exp $");
     38 #endif /* not lint */
     39 
     40 #include "tip.h"
     41 
     42 #define DISCONNECT_CMD	"\20\04"	/* disconnection string */
     43 
     44 static	int btimeout = 0;
     45 static	jmp_buf timeoutbuf;
     46 
     47 static	int	biz_dialer __P((char *, char *));
     48 static	int	cmd __P((char *));
     49 static	int	detect __P((char *));
     50 static	void	sigALRM __P((int));
     51 
     52 /*
     53  * Dial up on a BIZCOMP Model 1022 with either
     54  * 	tone dialing (mod = "V")
     55  *	pulse dialing (mod = "W")
     56  */
     57 static int
     58 biz_dialer(num, mod)
     59 	char *num, *mod;
     60 {
     61 	int connected = 0;
     62 	char cbuf[40];
     63 
     64 	if (boolean(value(VERBOSE)))
     65 		printf("\nstarting call...");
     66 	/*
     67 	 * Disable auto-answer and configure for tone/pulse
     68 	 *  dialing
     69 	 */
     70 	if (cmd("\02K\r")) {
     71 		printf("can't initialize bizcomp...");
     72 		return (0);
     73 	}
     74 	(void)strncpy(cbuf, "\02.\r", sizeof(cbuf) - 1);
     75 	cbuf[1] = *mod;
     76 	if (cmd(cbuf)) {
     77 		printf("can't set dialing mode...");
     78 		return (0);
     79 	}
     80 	(void)snprintf(cbuf, sizeof cbuf, "\02D%s\r", num);
     81 	write(FD, cbuf, strlen(cbuf));
     82 	if (!detect("7\r")) {
     83 		printf("can't get dial tone...");
     84 		return (0);
     85 	}
     86 	if (boolean(value(VERBOSE)))
     87 		printf("ringing...");
     88 	/*
     89 	 * The reply from the BIZCOMP should be:
     90 	 *	2 \r or 7 \r	failure
     91 	 *	1 \r		success
     92 	 */
     93 	connected = detect("1\r");
     94 #ifdef ACULOG
     95 	if (btimeout) {
     96 		char line[80];
     97 
     98 		(void)snprintf(line, sizeof line, "%d second dial timeout",
     99 			(int)number(value(DIALTIMEOUT)));
    100 		logent(value(HOST), num, "biz1022", line);
    101 	}
    102 #endif
    103 	if (btimeout)
    104 		biz22_disconnect();	/* insurance */
    105 	return (connected);
    106 }
    107 
    108 int
    109 biz22w_dialer(num, acu)
    110 	char *num, *acu;
    111 {
    112 
    113 	return (biz_dialer(num, "W"));
    114 }
    115 
    116 int
    117 biz22f_dialer(num, acu)
    118 	char *num, *acu;
    119 {
    120 
    121 	return (biz_dialer(num, "V"));
    122 }
    123 
    124 void
    125 biz22_disconnect()
    126 {
    127 
    128 	write(FD, DISCONNECT_CMD, 4);
    129 	sleep(2);
    130 	tcflush(FD, TCIOFLUSH);
    131 }
    132 
    133 void
    134 biz22_abort()
    135 {
    136 
    137 	write(FD, "\02", 1);
    138 }
    139 
    140 static void
    141 sigALRM(dummy)
    142 	int dummy;
    143 {
    144 
    145 	btimeout = 1;
    146 	longjmp(timeoutbuf, 1);
    147 }
    148 
    149 static int
    150 cmd(s)
    151 	char *s;
    152 {
    153 	sig_t f;
    154 	char c;
    155 
    156 	write(FD, s, strlen(s));
    157 	f = signal(SIGALRM, sigALRM);
    158 	if (setjmp(timeoutbuf)) {
    159 		biz22_abort();
    160 		signal(SIGALRM, f);
    161 		return (1);
    162 	}
    163 	alarm(number(value(DIALTIMEOUT)));
    164 	read(FD, &c, 1);
    165 	alarm(0);
    166 	signal(SIGALRM, f);
    167 	c &= 0177;
    168 	return (c != '\r');
    169 }
    170 
    171 static int
    172 detect(s)
    173 	char *s;
    174 {
    175 	sig_t f;
    176 	char c;
    177 
    178 #if __GNUC__	/* XXX pacify gcc */
    179 	(void)&s;
    180 #endif
    181 
    182 	f = signal(SIGALRM, sigALRM);
    183 	btimeout = 0;
    184 	while (*s) {
    185 		if (setjmp(timeoutbuf)) {
    186 			biz22_abort();
    187 			break;
    188 		}
    189 		alarm(number(value(DIALTIMEOUT)));
    190 		read(FD, &c, 1);
    191 		alarm(0);
    192 		c &= 0177;
    193 		if (c != *s++)
    194 			return (0);
    195 	}
    196 	signal(SIGALRM, f);
    197 	return (btimeout == 0);
    198 }
    199