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