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