termios.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* Copyright (C) 1996 Robert de Bath <robert@mayday.compulink.co.uk> This
  2. * file is part of the Linux-8086 C library and is distributed under the
  3. * GNU Library General Public License.
  4. */
  5. /* Note: This is based loosely on the Glib termios routines. */
  6. #ifndef __MSDOS__
  7. #include <errno.h>
  8. #include <stddef.h>
  9. #include <sys/ioctl.h>
  10. #include <termios.h>
  11. #include <unistd.h>
  12. #ifdef L_isatty
  13. int isatty(int fd)
  14. {
  15. struct termios term;
  16. int rv, err = errno;
  17. rv = (ioctl(fd, TCGETS, &term) == 0);
  18. if (rv == 0 && errno == ENOSYS)
  19. rv = (fd < 3);
  20. errno = err;
  21. return rv;
  22. }
  23. #endif
  24. #ifdef L_tcgetattr
  25. int tcgetattr(fd, term)
  26. int fd;
  27. struct termios *term;
  28. {
  29. return ioctl(fd, TCGETS, term);
  30. }
  31. #endif
  32. #ifdef L_tcsetattr
  33. int tcsetattr(fildes, optional_actions, termios_p)
  34. int fildes;
  35. int optional_actions;
  36. struct termios *termios_p;
  37. {
  38. switch (optional_actions) {
  39. case TCSANOW:
  40. return ioctl(fildes, TCSETS, termios_p);
  41. case TCSADRAIN:
  42. return ioctl(fildes, TCSETSW, termios_p);
  43. case TCSAFLUSH:
  44. return ioctl(fildes, TCSETSF, termios_p);
  45. default:
  46. errno = EINVAL;
  47. return -1;
  48. }
  49. }
  50. #endif
  51. #ifdef L_tcdrain
  52. /* Wait for pending output to be written on FD. */
  53. int tcdrain(fd)
  54. int fd;
  55. {
  56. /* With an argument of 1, TCSBRK just waits for output to drain. */
  57. return ioctl(fd, TCSBRK, 1);
  58. }
  59. #endif
  60. #ifdef L_tcflow
  61. int tcflow(fd, action)
  62. int fd;
  63. int action;
  64. {
  65. return ioctl(fd, TCXONC, action);
  66. }
  67. #endif
  68. #ifdef L_tcflush
  69. /* Flush pending data on FD. */
  70. int tcflush(fd, queue_selector)
  71. int fd;
  72. int queue_selector;
  73. {
  74. return ioctl(fd, TCFLSH, queue_selector);
  75. }
  76. #endif
  77. #ifdef L_tcsendbreak
  78. /* Send zero bits on FD. */
  79. int tcsendbreak(fd, duration)
  80. int fd;
  81. int duration;
  82. {
  83. /*
  84. * The break lasts 0.25 to 0.5 seconds if DURATION is zero, and an
  85. * implementation-defined period if DURATION is nonzero. We define a
  86. * positive DURATION to be number of milliseconds to break.
  87. */
  88. if (duration <= 0)
  89. return ioctl(fd, TCSBRK, 0);
  90. /*
  91. * ioctl can't send a break of any other duration for us. This could be
  92. * changed to use trickery (e.g. lower speed and send a '\0') to send
  93. * the break, but for now just return an error.
  94. */
  95. errno = EINVAL;
  96. return -1;
  97. }
  98. #endif
  99. #ifdef L_tcsetpgrp
  100. /* Set the foreground process group ID of FD set PGRP_ID. */
  101. int tcsetpgrp(fd, pgrp_id)
  102. int fd;
  103. pid_t pgrp_id;
  104. {
  105. return ioctl(fd, TIOCSPGRP, &pgrp_id);
  106. }
  107. #endif
  108. #ifdef L_tcgetpgrp
  109. /* Return the foreground process group ID of FD. */
  110. pid_t tcgetpgrp(fd)
  111. int fd;
  112. {
  113. int pgrp;
  114. if (ioctl(fd, TIOCGPGRP, &pgrp) < 0)
  115. return (pid_t) - 1;
  116. return (pid_t) pgrp;
  117. }
  118. #endif
  119. #ifdef L_cfgetospeed
  120. speed_t cfgetospeed(tp)
  121. struct termios *tp;
  122. {
  123. return (tp->c_cflag & CBAUD);
  124. }
  125. #endif
  126. #ifdef L_cfgetispeed
  127. speed_t cfgetispeed(tp)
  128. struct termios *tp;
  129. {
  130. return (tp->c_cflag & CBAUD);
  131. }
  132. #endif
  133. #ifdef L_cfsetospeed
  134. int cfsetospeed(tp, speed)
  135. struct termios *tp;
  136. speed_t speed;
  137. {
  138. #ifdef CBAUDEX
  139. if ((speed & ~CBAUD) ||
  140. ((speed & CBAUDEX) && (speed < B57600 || speed > B115200)))
  141. return 0;
  142. #else
  143. if (speed & ~CBAUD)
  144. return 0;
  145. #endif
  146. tp->c_cflag &= ~CBAUD;
  147. tp->c_cflag |= speed;
  148. return 0;
  149. }
  150. #endif
  151. #ifdef L_cfsetispeed
  152. int cfsetispeed(tp, speed)
  153. struct termios *tp;
  154. speed_t speed;
  155. {
  156. return cfsetospeed(tp, speed);
  157. }
  158. #endif
  159. #if 0
  160. /* Not POSIX standard, not worth the bother to keep it up */
  161. #ifdef L_tcspeed
  162. static struct {
  163. int number;
  164. speed_t code;
  165. } tcspeeds[] = {
  166. #ifdef B50
  167. {
  168. 50, B50},
  169. #endif
  170. #ifdef B75
  171. {
  172. 75, B75},
  173. #endif
  174. #ifdef B110
  175. {
  176. 110, B110},
  177. #endif
  178. #ifdef B134
  179. {
  180. 134, B134},
  181. #endif
  182. #ifdef B150
  183. {
  184. 150, B150},
  185. #endif
  186. #ifdef B200
  187. {
  188. 200, B200},
  189. #endif
  190. #ifdef B300
  191. {
  192. 300, B300},
  193. #endif
  194. #ifdef B600
  195. {
  196. 600, B600},
  197. #endif
  198. #ifdef B1200
  199. {
  200. 1200, B1200},
  201. #endif
  202. #ifdef B1800
  203. {
  204. 1800, B1800},
  205. #endif
  206. #ifdef B2400
  207. {
  208. 2400, B2400},
  209. #endif
  210. #ifdef B4800
  211. {
  212. 4800, B4800},
  213. #endif
  214. #ifdef B9600
  215. {
  216. 9600, B9600},
  217. #endif
  218. #ifdef B19200
  219. {
  220. 19200, B19200},
  221. #endif
  222. #ifdef B38400
  223. {
  224. 38400, B38400},
  225. #endif
  226. #ifdef B57600
  227. {
  228. 57600, B57600},
  229. #endif
  230. #ifdef B115200
  231. {
  232. 115200, B115200},
  233. #endif
  234. #ifdef B230400
  235. {
  236. 230400, B230400},
  237. #endif
  238. #ifdef B460800
  239. {
  240. 460800, B460800},
  241. #endif
  242. #ifdef B0
  243. {
  244. 0, B0},
  245. #endif
  246. {
  247. 0, 0}
  248. };
  249. int tcspeed_to_number(code)
  250. speed_t code;
  251. {
  252. int i;
  253. code &= CBAUD;
  254. for (i = 0; tcspeeds[i].code; i++)
  255. if (tcspeeds[i].code == code)
  256. return tcspeeds[i].number;
  257. return 0;
  258. }
  259. speed_t tcspeed_from_number(number)
  260. int number;
  261. {
  262. int i;
  263. for (i = 0; tcspeeds[i].code; i++)
  264. if (tcspeeds[i].number == number)
  265. return tcspeeds[i].code;
  266. return B0;
  267. }
  268. #endif
  269. #ifdef L_cfgetospeedn
  270. int cfgetospeedn(tp)
  271. struct termios *tp;
  272. {
  273. return tcspeed_to_number(cfgetospeed(tp));
  274. }
  275. #endif
  276. #ifdef L_cfgetispeedn
  277. int cfgetispeedn(tp)
  278. struct termios *tp;
  279. {
  280. return tcspeed_to_number(cfgetispeed(tp));
  281. }
  282. #endif
  283. #ifdef L_cfsetospeedn
  284. int cfsetospeedn(tp, speed)
  285. struct termios *tp;
  286. int speed;
  287. {
  288. return cfsetospeed(tp, tcspeed_from_number(speed));
  289. }
  290. #endif
  291. #ifdef L_cfsetispeedn
  292. int cfsetispeedn(tp, speed)
  293. struct termios *tp;
  294. int speed;
  295. {
  296. return cfsetispeedn(tp, tcspeed_from_number(speed));
  297. }
  298. #endif
  299. #endif
  300. /* From linux libc-4.6.27 again */
  301. #ifdef L_cfmakeraw
  302. /* Copyright (C) 1992 Free Software Foundation, Inc.
  303. This file is part of the GNU C Library.*/
  304. void cfmakeraw(t)
  305. struct termios *t;
  306. {
  307. /* I changed it to the current form according to the suggestions
  308. * from Bruce Evans. Thanks Bruce. Please report the problems to
  309. * H.J. Lu (hlu@eecs.wsu.edu).
  310. */
  311. /*
  312. * I took out the bits commented out by #if 1...#else - RHP
  313. */
  314. /* VMIN = 0 means non-blocking for Linux */
  315. t->c_cc[VMIN] = 1;
  316. t->c_cc[VTIME] = 1;
  317. /* clear some bits with &= ~(bits), set others with |= */
  318. t->c_cflag &= ~(CSIZE | PARENB | CSTOPB);
  319. t->c_cflag |= (CS8 | HUPCL | CREAD);
  320. t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INPCK | ISTRIP);
  321. t->c_iflag &= ~(INLCR | IGNCR | ICRNL | IXON | IXOFF);
  322. t->c_iflag |= (BRKINT | IGNPAR);
  323. t->c_oflag &=
  324. ~(OPOST | OLCUC | OCRNL | ONOCR | ONLRET | OFILL | OFDEL);
  325. t->c_oflag &= ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY);
  326. t->c_oflag |= (ONLCR | NL0 | CR0 | TAB3 | BS0 | VT0 | FF0);
  327. t->c_lflag &=
  328. ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL);
  329. t->c_lflag &= ~(NOFLSH | XCASE);
  330. t->c_lflag &= ~(ECHOPRT | ECHOCTL | ECHOKE);
  331. }
  332. #endif
  333. #endif