xref: /illumos-gate/usr/src/cmd/tip/aculib/biz22.c (revision 8d489c7a)
1 /*
2  * Copyright 2000 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1983 Regents of the University of California.
8  * All rights reserved. The Berkeley software License Agreement
9  * specifies the terms and conditions for redistribution.
10  */
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13 
14 #include "tip.h"
15 
16 #define	DISCONNECT_CMD	"\20\04"	/* disconnection string */
17 
18 static void	sigALRM(void);
19 static int	cmd(char *);
20 static int	detect(char *);
21 
22 static	int timeout = 0;
23 static	sigjmp_buf timeoutbuf;
24 
25 void	biz22_disconnect(void);
26 
27 /*
28  * Dial up on a BIZCOMP Model 1022 with either
29  * 	tone dialing (mod = "V")
30  *	pulse dialing (mod = "W")
31  */
32 static int
biz_dialer(char * num,char * mod)33 biz_dialer(char *num, char *mod)
34 {
35 	int connected = 0;
36 	char cbuf[40];
37 
38 	if (boolean(value(VERBOSE)))
39 		(void) printf("\nstarting call...");
40 	/*
41 	 * Disable auto-answer and configure for tone/pulse
42 	 *  dialing
43 	 */
44 	if (cmd("\02K\r")) {
45 		(void) printf("can't initialize bizcomp...");
46 		return (0);
47 	}
48 	(void) strcpy(cbuf, "\02.\r");
49 	cbuf[1] = *mod;
50 	if (cmd(cbuf)) {
51 		(void) printf("can't set dialing mode...");
52 		return (0);
53 	}
54 	(void) strcpy(cbuf, "\02D");
55 	(void) strlcat(cbuf, num, sizeof (cbuf));
56 	(void) strlcat(cbuf, "\r", sizeof (cbuf));
57 	(void) write(FD, cbuf, strlen(cbuf));
58 	if (!detect("7\r")) {
59 		(void) printf("can't get dial tone...");
60 		return (0);
61 	}
62 	if (boolean(value(VERBOSE)))
63 		(void) printf("ringing...");
64 	/*
65 	 * The reply from the BIZCOMP should be:
66 	 *	2 \r or 7 \r	failure
67 	 *	1 \r		success
68 	 */
69 	connected = detect("1\r");
70 #ifdef ACULOG
71 	if (timeout) {
72 		char line[80];
73 
74 		(void) sprintf(line, "%d second dial timeout",
75 		    number(value(DIALTIMEOUT)));
76 		logent(value(HOST), num, "biz1022", line);
77 	}
78 #endif
79 	if (timeout)
80 		biz22_disconnect();	/* insurance */
81 	return (connected);
82 }
83 
84 /* ARGSUSED */
85 int
biz22w_dialer(char * num,char * acu)86 biz22w_dialer(char *num, char *acu)
87 {
88 
89 	return (biz_dialer(num, "W"));
90 }
91 
92 /* ARGSUSED */
93 int
biz22f_dialer(char * num,char * acu)94 biz22f_dialer(char *num, char *acu)
95 {
96 
97 	return (biz_dialer(num, "V"));
98 }
99 
100 void
biz22_disconnect(void)101 biz22_disconnect(void)
102 {
103 
104 	(void) write(FD, DISCONNECT_CMD, 4);
105 	(void) sleep(2);
106 	(void) ioctl(FD, TCFLSH, TCOFLUSH);
107 }
108 
109 void
biz22_abort(void)110 biz22_abort(void)
111 {
112 
113 	(void) write(FD, "\02", 1);
114 }
115 
116 static void
sigALRM(void)117 sigALRM(void)
118 {
119 
120 	timeout = 1;
121 	siglongjmp(timeoutbuf, 1);
122 }
123 
124 static int
cmd(char * s)125 cmd(char *s)
126 {
127 	char c;
128 	sig_handler_t f;
129 
130 	(void) write(FD, s, strlen(s));
131 	f = signal(SIGALRM, (sig_handler_t)sigALRM);
132 	if (sigsetjmp(timeoutbuf, 1)) {
133 		biz22_abort();
134 		(void) signal(SIGALRM, f);
135 		return (1);
136 	}
137 	(void) alarm(number(value(DIALTIMEOUT)));
138 	(void) read(FD, &c, 1);
139 	(void) alarm(0);
140 	(void) signal(SIGALRM, f);
141 	c &= 0177;
142 	return (c != '\r');
143 }
144 
145 static int
detect(char * s)146 detect(char *s)
147 {
148 	char c;
149 	sig_handler_t f;
150 
151 	f = signal(SIGALRM, (sig_handler_t)sigALRM);
152 	timeout = 0;
153 	while (*s) {
154 		if (sigsetjmp(timeoutbuf, 1)) {
155 			biz22_abort();
156 			break;
157 		}
158 		(void) alarm(number(value(DIALTIMEOUT)));
159 		(void) read(FD, &c, 1);
160 		(void) alarm(0);
161 		c &= 0177;
162 		if (c != *s++)
163 			return (0);
164 	}
165 	(void) signal(SIGALRM, f);
166 	return (timeout == 0);
167 }
168