termios.c 6.2 KB

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