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