/usr.bin/tip/libacu/biz22.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 187 lines · 123 code · 19 blank · 45 comment · 14 complexity · 6b305245babeba800cc2be2e13f902ae MD5 · raw file

  1. /* $OpenBSD: biz22.c,v 1.13 2006/03/17 19:17:13 moritz Exp $ */
  2. /* $NetBSD: biz22.c,v 1.6 1997/02/11 09:24:11 mrg Exp $ */
  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. #include <sys/cdefs.h>
  32. __FBSDID("$FreeBSD$");
  33. #ifndef lint
  34. #if 0
  35. static char sccsid[] = "@(#)biz22.c 8.1 (Berkeley) 6/6/93";
  36. static const char rcsid[] = "$OpenBSD: biz22.c,v 1.13 2006/03/17 19:17:13 moritz Exp $";
  37. #endif
  38. #endif /* not lint */
  39. #include "tip.h"
  40. #define DISCONNECT_CMD "\20\04" /* disconnection string */
  41. static int dialtimeout = 0;
  42. static jmp_buf timeoutbuf;
  43. static int biz_dialer(char *, char *);
  44. static void sigALRM(int);
  45. static int cmd(char *);
  46. static int detect(char *);
  47. /*
  48. * Dial up on a BIZCOMP Model 1022 with either
  49. * tone dialing (mod = "V")
  50. * pulse dialing (mod = "W")
  51. */
  52. static int
  53. biz_dialer(char *num, char *mod)
  54. {
  55. int connected = 0;
  56. char cbuf[40];
  57. if (boolean(value(VERBOSE)))
  58. printf("\nstarting call...");
  59. /*
  60. * Disable auto-answer and configure for tone/pulse
  61. * dialing
  62. */
  63. if (cmd("\02K\r")) {
  64. printf("can't initialize bizcomp...");
  65. return (0);
  66. }
  67. (void)strlcpy(cbuf, "\02.\r", sizeof cbuf);
  68. cbuf[1] = *mod;
  69. if (cmd(cbuf)) {
  70. printf("can't set dialing mode...");
  71. return (0);
  72. }
  73. (void)snprintf(cbuf, sizeof(cbuf), "\02D%s\r", num);
  74. write(FD, cbuf, strlen(cbuf));
  75. if (!detect("7\r")) {
  76. printf("can't get dial tone...");
  77. return (0);
  78. }
  79. if (boolean(value(VERBOSE)))
  80. printf("ringing...");
  81. /*
  82. * The reply from the BIZCOMP should be:
  83. * 2 \r or 7 \r failure
  84. * 1 \r success
  85. */
  86. connected = detect("1\r");
  87. #ifdef ACULOG
  88. if (dialtimeout) {
  89. char line[80];
  90. (void)snprintf(line, sizeof line, "%ld second dial timeout",
  91. number(value(DIALTIMEOUT)));
  92. logent(value(HOST), num, "biz1022", line);
  93. }
  94. #endif
  95. if (dialtimeout)
  96. biz22_disconnect(); /* insurance */
  97. return (connected);
  98. }
  99. int
  100. biz22w_dialer(char *num, char *acu)
  101. {
  102. return (biz_dialer(num, "W"));
  103. }
  104. int
  105. biz22f_dialer(char *num, char *acu)
  106. {
  107. return (biz_dialer(num, "V"));
  108. }
  109. void
  110. biz22_disconnect(void)
  111. {
  112. write(FD, DISCONNECT_CMD, sizeof(DISCONNECT_CMD)-1);
  113. sleep(2);
  114. tcflush(FD, TCIOFLUSH);
  115. }
  116. void
  117. biz22_abort(void)
  118. {
  119. write(FD, "\02", 1);
  120. }
  121. /*ARGSUSED*/
  122. static void
  123. sigALRM(int signo)
  124. {
  125. dialtimeout = 1;
  126. longjmp(timeoutbuf, 1);
  127. }
  128. static int
  129. cmd(char *s)
  130. {
  131. sig_t f;
  132. char c;
  133. write(FD, s, strlen(s));
  134. f = signal(SIGALRM, sigALRM);
  135. if (setjmp(timeoutbuf)) {
  136. biz22_abort();
  137. signal(SIGALRM, f);
  138. return (1);
  139. }
  140. alarm(number(value(DIALTIMEOUT)));
  141. read(FD, &c, 1);
  142. alarm(0);
  143. signal(SIGALRM, f);
  144. c &= 0177;
  145. return (c != '\r');
  146. }
  147. static int
  148. detect(char *s)
  149. {
  150. sig_t f;
  151. char c;
  152. f = signal(SIGALRM, sigALRM);
  153. dialtimeout = 0;
  154. while (*s) {
  155. if (setjmp(timeoutbuf)) {
  156. biz22_abort();
  157. break;
  158. }
  159. alarm(number(value(DIALTIMEOUT)));
  160. read(FD, &c, 1);
  161. alarm(0);
  162. c &= 0177;
  163. if (c != *s++)
  164. return (0);
  165. }
  166. signal(SIGALRM, f);
  167. return (dialtimeout == 0);
  168. }