termios.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 __libc_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. weak_alias(__libc_tcdrain, tcdrain)
  42. #endif
  43. #ifdef L_tcflow
  44. /* Suspend or restart transmission on FD. */
  45. int tcflow ( int fd, int action)
  46. {
  47. return ioctl(fd, TCXONC, action);
  48. }
  49. #endif
  50. #ifdef L_tcflush
  51. /* Flush pending data on FD. */
  52. int tcflush ( int fd, int queue_selector)
  53. {
  54. return ioctl(fd, TCFLSH, queue_selector);
  55. }
  56. #endif
  57. #ifdef L_tcsendbreak
  58. /* Send zero bits on FD. */
  59. int tcsendbreak( int fd, int duration)
  60. {
  61. /*
  62. * The break lasts 0.25 to 0.5 seconds if DURATION is zero, and an
  63. * implementation-defined period if DURATION is nonzero. We define a
  64. * positive DURATION to be number of milliseconds to break.
  65. */
  66. if (duration <= 0)
  67. return ioctl(fd, TCSBRK, 0);
  68. /*
  69. * ioctl can't send a break of any other duration for us. This could be
  70. * changed to use trickery (e.g. lower speed and send a '\0') to send
  71. * the break, but for now just return an error.
  72. */
  73. __set_errno(EINVAL);
  74. return -1;
  75. }
  76. #endif
  77. #ifdef L_tcsetpgrp
  78. /* Set the foreground process group ID of FD set PGRP_ID. */
  79. int tcsetpgrp ( int fd, pid_t pgrp_id)
  80. {
  81. return ioctl (fd, TIOCSPGRP, &pgrp_id);
  82. }
  83. #endif
  84. #ifdef L_tcgetpgrp
  85. /* Return the foreground process group ID of FD. */
  86. pid_t tcgetpgrp ( int fd)
  87. {
  88. int pgrp;
  89. if (ioctl (fd, TIOCGPGRP, &pgrp) < 0)
  90. return (pid_t) -1;
  91. return (pid_t) pgrp;
  92. }
  93. #endif
  94. /* This is a gross hack around a kernel bug. If the cfsetispeed functions is
  95. * called with the SPEED argument set to zero this means use the same speed as
  96. * for output. But we don't have independent input and output speeds and
  97. * therefore cannot record this.
  98. *
  99. * We use an unused bit in the `c_iflag' field to keep track of this use of
  100. * `cfsetispeed'. The value here must correspond to the one used in
  101. * `tcsetattr.c'. */
  102. #define IBAUD0 020000000000
  103. #ifdef L_cfgetospeed
  104. /* Return the output baud rate stored in *TERMIOS_P. */
  105. speed_t cfgetospeed ( const struct termios *termios_p)
  106. {
  107. return termios_p->c_cflag & (CBAUD | CBAUDEX);
  108. }
  109. #endif
  110. #ifdef L_cfgetispeed
  111. /* Return the input baud rate stored in *TERMIOS_P.
  112. * Although for Linux there is no difference between input and output
  113. * speed, the numerical 0 is a special case for the input baud rate. It
  114. * should set the input baud rate to the output baud rate. */
  115. speed_t cfgetispeed (const struct termios *termios_p)
  116. {
  117. return ((termios_p->c_iflag & IBAUD0)
  118. ? 0 : termios_p->c_cflag & (CBAUD | CBAUDEX));
  119. }
  120. #endif
  121. #ifdef L_cfsetospeed
  122. /* Set the output baud rate stored in *TERMIOS_P to SPEED. */
  123. int cfsetospeed (struct termios *termios_p, speed_t speed)
  124. {
  125. if ((speed & ~CBAUD) != 0
  126. && (speed < B57600 || speed > B460800))
  127. {
  128. __set_errno(EINVAL);
  129. return -1;
  130. }
  131. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  132. termios_p->c_cflag |= speed;
  133. return 0;
  134. }
  135. #endif
  136. #ifdef L_cfsetispeed
  137. /* Set the input baud rate stored in *TERMIOS_P to SPEED.
  138. * Although for Linux there is no difference between input and output
  139. * speed, the numerical 0 is a special case for the input baud rate. It
  140. * should set the input baud rate to the output baud rate. */
  141. int cfsetispeed ( struct termios *termios_p, speed_t speed)
  142. {
  143. if ((speed & ~CBAUD) != 0
  144. && (speed < B57600 || speed > B460800))
  145. {
  146. __set_errno(EINVAL);
  147. return -1;
  148. }
  149. if (speed == 0)
  150. termios_p->c_iflag |= IBAUD0;
  151. else
  152. {
  153. termios_p->c_iflag &= ~IBAUD0;
  154. termios_p->c_cflag &= ~(CBAUD | CBAUDEX);
  155. termios_p->c_cflag |= speed;
  156. }
  157. return 0;
  158. }
  159. #endif
  160. #ifdef L_cfsetspeed
  161. struct speed_struct
  162. {
  163. speed_t value;
  164. speed_t internal;
  165. };
  166. static const struct speed_struct speeds[] =
  167. {
  168. #ifdef B0
  169. { 0, B0 },
  170. #endif
  171. #ifdef B50
  172. { 50, B50 },
  173. #endif
  174. #ifdef B75
  175. { 75, B75 },
  176. #endif
  177. #ifdef B110
  178. { 110, B110 },
  179. #endif
  180. #ifdef B134
  181. { 134, B134 },
  182. #endif
  183. #ifdef B150
  184. { 150, B150 },
  185. #endif
  186. #ifdef B200
  187. { 200, B200 },
  188. #endif
  189. #ifdef B300
  190. { 300, B300 },
  191. #endif
  192. #ifdef B600
  193. { 600, B600 },
  194. #endif
  195. #ifdef B1200
  196. { 1200, B1200 },
  197. #endif
  198. #ifdef B1200
  199. { 1200, B1200 },
  200. #endif
  201. #ifdef B1800
  202. { 1800, B1800 },
  203. #endif
  204. #ifdef B2400
  205. { 2400, B2400 },
  206. #endif
  207. #ifdef B4800
  208. { 4800, B4800 },
  209. #endif
  210. #ifdef B9600
  211. { 9600, B9600 },
  212. #endif
  213. #ifdef B19200
  214. { 19200, B19200 },
  215. #endif
  216. #ifdef B38400
  217. { 38400, B38400 },
  218. #endif
  219. #ifdef B57600
  220. { 57600, B57600 },
  221. #endif
  222. #ifdef B76800
  223. { 76800, B76800 },
  224. #endif
  225. #ifdef B115200
  226. { 115200, B115200 },
  227. #endif
  228. #ifdef B153600
  229. { 153600, B153600 },
  230. #endif
  231. #ifdef B230400
  232. { 230400, B230400 },
  233. #endif
  234. #ifdef B307200
  235. { 307200, B307200 },
  236. #endif
  237. #ifdef B460800
  238. { 460800, B460800 },
  239. #endif
  240. };
  241. /* Set both the input and output baud rates stored in *TERMIOS_P to SPEED. */
  242. int cfsetspeed (struct termios *termios_p, speed_t speed)
  243. {
  244. size_t cnt;
  245. for (cnt = 0; cnt < sizeof (speeds) / sizeof (speeds[0]); ++cnt)
  246. if (speed == speeds[cnt].internal)
  247. {
  248. cfsetispeed (termios_p, speed);
  249. cfsetospeed (termios_p, speed);
  250. return 0;
  251. }
  252. else if (speed == speeds[cnt].value)
  253. {
  254. cfsetispeed (termios_p, speeds[cnt].internal);
  255. cfsetospeed (termios_p, speeds[cnt].internal);
  256. return 0;
  257. }
  258. __set_errno (EINVAL);
  259. return -1;
  260. }
  261. #endif
  262. #ifdef L_cfmakeraw
  263. /* Copyright (C) 1992, 1996, 1997 Free Software Foundation, Inc.
  264. This file is part of the GNU C Library.
  265. */
  266. #include <termios.h>
  267. /* Set *T to indicate raw mode. */
  268. void
  269. cfmakeraw (struct termios *t)
  270. {
  271. t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  272. t->c_oflag &= ~OPOST;
  273. t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  274. t->c_cflag &= ~(CSIZE|PARENB);
  275. t->c_cflag |= CS8;
  276. t->c_cc[VMIN] = 1; /* read returns when one char is available. */
  277. t->c_cc[VTIME] = 0;
  278. }
  279. #endif