Home | History | Annotate | Line # | Download | only in aculib
      1 /*	$NetBSD: biz31.c,v 1.12 2006/12/14 17:09:43 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[] = "@(#)biz31.c	8.1 (Berkeley) 6/6/93";
     36 #endif
     37 __RCSID("$NetBSD: biz31.c,v 1.12 2006/12/14 17:09:43 christos Exp $");
     38 #endif /* not lint */
     39 
     40 #include "tip.h"
     41 
     42 #define MAXRETRY	3		/* sync up retry count */
     43 #define DISCONNECT_CMD	"\21\25\11\24"	/* disconnection string */
     44 
     45 static	void sigALRM(int);
     46 static	int timeout = 0;
     47 static	jmp_buf timeoutbuf;
     48 
     49 static void echo(const char *);
     50 static int detect(const char *);
     51 static void flush(const char *);
     52 static int bizsync(int);
     53 
     54 /*
     55  * Dial up on a BIZCOMP Model 1031 with either
     56  * 	tone dialing (mod = "f")
     57  *	pulse dialing (mod = "w")
     58  */
     59 static int
     60 biz_dialer(char *num, const char *mod)
     61 {
     62 	int connected = 0;
     63 
     64 	if (!bizsync(FD)) {
     65 		logent(value(HOST), "", "biz", "out of sync");
     66 		(void)printf("bizcomp out of sync\n");
     67 		exit(0);
     68 	}
     69 	if (boolean(value(VERBOSE)))
     70 		(void)printf("\nstarting call...");
     71 	echo("#\rk$\r$\n");			/* disable auto-answer */
     72 	echo("$>$.$ #\r");			/* tone/pulse dialing */
     73 	echo(mod);
     74 	echo("$\r$\n");
     75 	echo("$>$.$ #\re$ ");			/* disconnection sequence */
     76 	echo(DISCONNECT_CMD);
     77 	echo("\r$\n$\r$\n");
     78 	echo("$>$.$ #\rr$ ");			/* repeat dial */
     79 	echo(num);
     80 	echo("\r$\n");
     81 	if (boolean(value(VERBOSE)))
     82 		(void)printf("ringing...");
     83 	/*
     84 	 * The reply from the BIZCOMP should be:
     85 	 *	`^G NO CONNECTION\r\n^G\r\n'	failure
     86 	 *	` CONNECTION\r\n^G'		success
     87 	 */
     88 	connected = detect(" ");
     89 	if (!connected)
     90 		flush(" NO CONNECTION\r\n\07\r\n");
     91 	else
     92 		flush("CONNECTION\r\n\07");
     93 	if (timeout)
     94 		biz31_disconnect();	/* insurance */
     95 	return (connected);
     96 }
     97 
     98 int
     99 /*ARGSUSED*/
    100 biz31w_dialer(char *num, char *acu __unused)
    101 {
    102 
    103 	return (biz_dialer(num, "w"));
    104 }
    105 
    106 int
    107 /*ARGSUSED*/
    108 biz31f_dialer(char *num, char *acu __unused)
    109 {
    110 
    111 	return (biz_dialer(num, "f"));
    112 }
    113 
    114 void
    115 biz31_disconnect(void)
    116 {
    117 
    118 	(void)write(FD, DISCONNECT_CMD, 4);
    119 	(void)sleep(2);
    120 	(void)tcflush(FD, TCIOFLUSH);
    121 }
    122 
    123 void
    124 biz31_abort(void)
    125 {
    126 
    127 	(void)write(FD, "\33", 1);
    128 }
    129 
    130 static void
    131 echo(const char *s)
    132 {
    133 	char c;
    134 
    135 	while ((c = *s++) != '\0')
    136 		switch (c) {
    137 		case '$':
    138 			(void)read(FD, &c, 1);
    139 			s++;
    140 			break;
    141 
    142 		case '#':
    143 			c = *s++;
    144 			(void)write(FD, &c, 1);
    145 			break;
    146 
    147 		default:
    148 			(void)write(FD, &c, 1);
    149 			(void)read(FD, &c, 1);
    150 		}
    151 }
    152 
    153 static void
    154 /*ARGSUSED*/
    155 sigALRM(int signo __unused)
    156 {
    157 
    158 	timeout = 1;
    159 	longjmp(timeoutbuf, 1);
    160 }
    161 
    162 static int
    163 detect(const char *s)
    164 {
    165 	sig_t f;
    166 	char c;
    167 
    168 	f = signal(SIGALRM, sigALRM);
    169 	timeout = 0;
    170 	while (*s) {
    171 		if (setjmp(timeoutbuf)) {
    172 			(void)printf("\07timeout waiting for reply\n");
    173 			biz31_abort();
    174 			break;
    175 		}
    176 		(void)alarm((unsigned)number(value(DIALTIMEOUT)));
    177 		(void)read(FD, &c, 1);
    178 		(void)alarm(0);
    179 		if (c != *s++)
    180 			break;
    181 	}
    182 	(void)signal(SIGALRM, f);
    183 	return (timeout == 0);
    184 }
    185 
    186 static void
    187 flush(const char *s)
    188 {
    189 	sig_t f;
    190 	char c;
    191 
    192 	f = signal(SIGALRM, sigALRM);
    193 	while (*s++) {
    194 		if (setjmp(timeoutbuf))
    195 			break;
    196 		(void)alarm(10);
    197 		(void)read(FD, &c, 1);
    198 		(void)alarm(0);
    199 	}
    200 	(void)signal(SIGALRM, f);
    201 	timeout = 0;			/* guard against disconnection */
    202 }
    203 
    204 /*
    205  * This convoluted piece of code attempts to get
    206  *  the bizcomp in sync.  If you don't have the capacity or nread
    207  *  call there are gory ways to simulate this.
    208  */
    209 static int
    210 bizsync(int fd)
    211 {
    212 #ifdef FIOCAPACITY
    213 	struct capacity b;
    214 #	define chars(b)	((b).cp_nbytes)
    215 #	define IOCTL	FIOCAPACITY
    216 #endif
    217 #ifdef FIONREAD
    218 	long b;
    219 #	define chars(b)	(b)
    220 #	define IOCTL	FIONREAD
    221 #endif
    222 	int already = 0;
    223 	char buf[10];
    224 
    225 retry:
    226 	if (ioctl(fd, IOCTL, &b) >= 0 && chars(b) > 0)
    227 		(void)tcflush(FD, TCIOFLUSH);
    228 	(void)write(fd, "\rp>\r", 4);
    229 	(void)sleep(1);
    230 	if (ioctl(fd, IOCTL, &b) >= 0) {
    231 		if (chars(b) != 10) {
    232 	nono:
    233 			if (already > MAXRETRY)
    234 				return (0);
    235 			(void)write(fd, DISCONNECT_CMD, 4);
    236 			(void)sleep(2);
    237 			already++;
    238 			goto retry;
    239 		} else {
    240 			(void)read(fd, buf, 10);
    241 			if (strncmp(buf, "p >\r\n\r\n>", 8))
    242 				goto nono;
    243 		}
    244 	}
    245 	return (1);
    246 }
    247