termios.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright (C) 1992, 1993, 1996, 1997, 1998 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA.
  15. About the only thing remaining here fromthe original Linux-8086 C library
  16. version by Robert de Bath <robert@mayday.compulink.co.uk>, is the general
  17. layout. All else has been recently stolen from GNU libc, since that was
  18. much more current.
  19. */
  20. #include <errno.h>
  21. #include <stddef.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. #include <termios.h>
  26. #ifdef L_isatty
  27. /* Return 1 if FD is a terminal, 0 if not. */
  28. int isatty(int fd)
  29. {
  30. struct termios term;
  31. return (tcgetattr (fd, &term) == 0);
  32. }
  33. #endif
  34. #ifdef L_tcdrain
  35. /* Wait for pending output to be written on FD. */
  36. int tcdrain (int fd)
  37. {
  38. /* With an argument of 1, TCSBRK waits for the output to drain. */
  39. return ioctl(fd, TCSBRK, 1);
  40. }
  41. #endif
  42. #ifdef L_tcflow
  43. /* Suspend or restart transmission on FD. */
  44. int tcflow ( int fd, int action)
  45. {
  46. return ioctl(fd, TCXONC, action);
  47. }
  48. #endif
  49. #ifdef L_tcflush
  50. /* Flush pending data on FD. */
  51. int tcflush ( int fd, int queue_selector)
  52. {
  53. return ioctl(fd, TCFLSH, queue_selector);
  54. }
  55. #endif
  56. #ifdef L_tcsendbreak
  57. /* Send zero bits on FD. */
  58. int tcsendbreak( int fd, int duration)
  59. {
  60. /*
  61. * The break lasts 0.25 to 0.5 seconds if DURATION is zero, and an
  62. * implementation-defined period if DURATION is nonzero. We define a
  63. * positive DURATION to be number of milliseconds to break.
  64. */
  65. if (duration <= 0)
  66. return ioctl(fd, TCSBRK, 0);
  67. /*
  68. * ioctl can't send a break of any other duration for us. This could be
  69. * changed to use trickery (e.g. lower speed and send a '\0') to send
  70. * the break, but for now just return an error.
  71. */
  72. __set_errno(EINVAL);
  73. return -1;
  74. }
  75. #endif
  76. #ifdef L_tcsetpgrp
  77. /* Set the foreground process group ID of FD set PGRP_ID. */
  78. int tcsetpgrp ( int fd, pid_t pgrp_id)
  79. {
  80. return ioctl (fd, TIOCSPGRP, &pgrp_id);
  81. }
  82. #endif
  83. #ifdef L_tcgetpgrp
  84. /* Return the foreground process group ID of FD. */
  85. pid_t tcgetpgrp ( int fd)
  86. {
  87. int pgrp;
  88. if (ioctl (fd, TIOCGPGRP, &pgrp) < 0)
  89. return (pid_t) -1;
  90. return (pid_t) pgrp;
  91. }
  92. #endif
  93. /* This is a gross hack around a kernel bug. If the cfsetispeed functions is
  94. * called with the SPEED argument set to zero this means use the same speed as
  95. * for output. But we don't have independent input and output speeds and
  96. * therefore cannot record this.
  97. *
  98. * We use an unused bit in the `c_iflag' field to keep track of this use of
  99. * `cfsetispeed'. The value here must correspond to the one used in
  100. * `tcsetattr.c'. */
  101. #define IBAUD0 020000000000
  102. #ifdef L_cfgetospeed
  103. /* Return the output baud rate stored in *TERMIOS_P. */
  104. speed_t cfgetospeed ( const struct termios *termios_p)
  105. {
  106. return termios_p->c_cflag & (CBAUD | CBAUDEX);
  107. }
  108. #endif
  109. #ifdef L_cfgetispeed
  110. /* Return the input baud rate stored in *TERMIOS_P.
  111. * Although for Linux there is no difference between input and output
  112. * speed, the numerical 0 is a special case for the input baud rate. It
  113. * should set the input baud rate to the output baud rate. */
  114. speed_t cfgetispeed (const struct termios *termios_p)
  115. {
  116. return ((termios_p->c_iflag & IBAUD0)
  117. ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
  118. }
  119. #endif
  120. #ifdef L_cfsetospeed
  121. /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
  122. int cfsetospeed (struct termios *termios_p, speed_t speed)
  123. {
  124. if ((speed & ~CBAUD) != 0
  125. && (speed < B57600 || speed > B460800))
  126. {
  127. __set_errno(EINVAL);
  128. return -1;
  129. }
  130. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  131. termios_p->c_cflag |= speed;
  132. return 0;
  133. }
  134. #endif
  135. #ifdef L_cfsetispeed
  136. /* Set the input baud rate stored in *TERMIOS_P to SPEED.
  137. * Although for Linux there is no difference between input and output
  138. * speed, the numerical 0 is a special case for the input baud rate. It
  139. * should set the input baud rate to the output baud rate. */
  140. int cfsetispeed ( struct termios *termios_p, speed_t speed)
  141. {
  142. if ((speed & ~CBAUD) != 0
  143. && (speed < B57600 || speed > B460800))
  144. {
  145. __set_errno(EINVAL);
  146. return -1;
  147. }
  148. if (speed == 0)
  149. termios_p->c_iflag |= IBAUD0;
  150. else
  151. {
  152. termios_p->c_iflag &= ~IBAUD0;
  153. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  154. termios_p->c_cflag |= speed;
  155. }
  156. return 0;
  157. }
  158. #endif
  159. #ifdef L_tcspeed
  160. struct speed_struct
  161. {
  162. speed_t value;
  163. speed_t internal;
  164. };
  165. static const struct speed_struct speeds[] =
  166. {
  167. #ifdef B0
  168. { 0, B0 },
  169. #endif
  170. #ifdef B50
  171. { 50, B50 },
  172. #endif
  173. #ifdef B75
  174. { 75, B75 },
  175. #endif
  176. #ifdef B110
  177. { 110, B110 },
  178. #endif
  179. #ifdef B134
  180. { 134, B134 },
  181. #endif
  182. #ifdef B150
  183. { 150, B150 },
  184. #endif
  185. #ifdef B200
  186. { 200, B200 },
  187. #endif
  188. #ifdef B300
  189. { 300, B300 },
  190. #endif
  191. #ifdef B600
  192. { 600, B600 },
  193. #endif
  194. #ifdef B1200
  195. { 1200, B1200 },
  196. #endif
  197. #ifdef B1200
  198. { 1200, B1200 },
  199. #endif
  200. #ifdef B1800
  201. { 1800, B1800 },
  202. #endif
  203. #ifdef B2400
  204. { 2400, B2400 },
  205. #endif
  206. #ifdef B4800
  207. { 4800, B4800 },
  208. #endif
  209. #ifdef B9600
  210. { 9600, B9600 },
  211. #endif
  212. #ifdef B19200
  213. { 19200, B19200 },
  214. #endif
  215. #ifdef B38400
  216. { 38400, B38400 },
  217. #endif
  218. #ifdef B57600
  219. { 57600, B57600 },
  220. #endif
  221. #ifdef B76800
  222. { 76800, B76800 },
  223. #endif
  224. #ifdef B115200
  225. { 115200, B115200 },
  226. #endif
  227. #ifdef B153600
  228. { 153600, B153600 },
  229. #endif
  230. #ifdef B230400
  231. { 230400, B230400 },
  232. #endif
  233. #ifdef B307200
  234. { 307200, B307200 },
  235. #endif
  236. #ifdef B460800
  237. { 460800, B460800 },
  238. #endif
  239. };
  240. /* Set both the input and output baud rates stored in *TERMIOS_P to SPEED. */
  241. int cfsetspeed (struct termios *termios_p, speed_t speed)
  242. {
  243. size_t cnt;
  244. for (cnt = 0; cnt < sizeof (speeds) / sizeof (speeds[0]); ++cnt)
  245. if (speed == speeds[cnt].internal)
  246. {
  247. cfsetispeed (termios_p, speed);
  248. cfsetospeed (termios_p, speed);
  249. return 0;
  250. }
  251. else if (speed == speeds[cnt].value)
  252. {
  253. cfsetispeed (termios_p, speeds[cnt].internal);
  254. cfsetospeed (termios_p, speeds[cnt].internal);
  255. return 0;
  256. }
  257. __set_errno (EINVAL);
  258. return -1;
  259. }
  260. #endif
  261. #ifdef L_cfmakeraw
  262. /* Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
  263. This file is part of the GNU C Library.
  264. */
  265. #include <termios.h>
  266. /* Set *T to indicate raw mode. */
  267. void
  268. cfmakeraw (struct termios *t)
  269. {
  270. t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  271. t->c_oflag &= ~OPOST;
  272. t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  273. t->c_cflag &= ~(CSIZE|PARENB);
  274. t->c_cflag |= CS8;
  275. t->c_cc[VMIN] = 1; /* read returns when one char is available. */
  276. t->c_cc[VTIME] = 0;
  277. }
  278. #endif