termios.nim 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. {.deadCodeElim: on.}
  10. import posix
  11. type
  12. Speed* = cuint
  13. Cflag* = cuint
  14. {.deprecated: [Tcflag: Cflag].}
  15. const
  16. NCCS* = when defined(macosx): 20 else: 32
  17. when defined(linux) and defined(amd64):
  18. type
  19. Termios* {.importc: "struct termios", header: "<termios.h>".} = object
  20. c_iflag*: Cflag # input mode flags
  21. c_oflag*: Cflag # output mode flags
  22. c_cflag*: Cflag # control mode flags
  23. c_lflag*: Cflag # local mode flags
  24. c_line*: cuchar
  25. c_cc*: array[NCCS, cuchar] # control characters
  26. c_ispeed*: Speed
  27. c_ospeed*: Speed
  28. else:
  29. type
  30. Termios* {.importc: "struct termios", header: "<termios.h>".} = object
  31. c_iflag*: Cflag # input mode flags
  32. c_oflag*: Cflag # output mode flags
  33. c_cflag*: Cflag # control mode flags
  34. c_lflag*: Cflag # local mode flags
  35. c_cc*: array[NCCS, cuchar] # control characters
  36. # cc characters
  37. var
  38. VINTR* {.importc, header: "<termios.h>".}: cint
  39. VQUIT* {.importc, header: "<termios.h>".}: cint
  40. VERASE* {.importc, header: "<termios.h>".}: cint
  41. VKILL* {.importc, header: "<termios.h>".}: cint
  42. VEOF* {.importc, header: "<termios.h>".}: cint
  43. VTIME* {.importc, header: "<termios.h>".}: cint
  44. VMIN* {.importc, header: "<termios.h>".}: cint
  45. VSTART* {.importc, header: "<termios.h>".}: cint
  46. VSTOP* {.importc, header: "<termios.h>".}: cint
  47. VSUSP* {.importc, header: "<termios.h>".}: cint
  48. VEOL* {.importc, header: "<termios.h>".}: cint
  49. # iflag bits
  50. var
  51. IGNBRK* {.importc, header: "<termios.h>".}: Cflag
  52. BRKINT* {.importc, header: "<termios.h>".}: Cflag
  53. IGNPAR* {.importc, header: "<termios.h>".}: Cflag
  54. PARMRK* {.importc, header: "<termios.h>".}: Cflag
  55. INPCK* {.importc, header: "<termios.h>".}: Cflag
  56. ISTRIP* {.importc, header: "<termios.h>".}: Cflag
  57. INLCR* {.importc, header: "<termios.h>".}: Cflag
  58. IGNCR* {.importc, header: "<termios.h>".}: Cflag
  59. ICRNL* {.importc, header: "<termios.h>".}: Cflag
  60. IUCLC* {.importc, header: "<termios.h>".}: Cflag
  61. IXON* {.importc, header: "<termios.h>".}: Cflag
  62. IXANY* {.importc, header: "<termios.h>".}: Cflag
  63. IXOFF* {.importc, header: "<termios.h>".}: Cflag
  64. # oflag bits
  65. var
  66. OPOST* {.importc, header: "<termios.h>".}: Cflag
  67. ONLCR* {.importc, header: "<termios.h>".}: Cflag
  68. OCRNL* {.importc, header: "<termios.h>".}: Cflag
  69. ONOCR* {.importc, header: "<termios.h>".}: Cflag
  70. ONLRET* {.importc, header: "<termios.h>".}: Cflag
  71. OFILL* {.importc, header: "<termios.h>".}: Cflag
  72. OFDEL* {.importc, header: "<termios.h>".}: Cflag
  73. NLDLY* {.importc, header: "<termios.h>".}: Cflag
  74. NL0* {.importc, header: "<termios.h>".}: Cflag
  75. NL1* {.importc, header: "<termios.h>".}: Cflag
  76. CRDLY* {.importc, header: "<termios.h>".}: Cflag
  77. CR0* {.importc, header: "<termios.h>".}: Cflag
  78. CR1* {.importc, header: "<termios.h>".}: Cflag
  79. CR2* {.importc, header: "<termios.h>".}: Cflag
  80. CR3* {.importc, header: "<termios.h>".}: Cflag
  81. TABDLY* {.importc, header: "<termios.h>".}: Cflag
  82. TAB0* {.importc, header: "<termios.h>".}: Cflag
  83. TAB1* {.importc, header: "<termios.h>".}: Cflag
  84. TAB2* {.importc, header: "<termios.h>".}: Cflag
  85. TAB3* {.importc, header: "<termios.h>".}: Cflag
  86. BSDLY* {.importc, header: "<termios.h>".}: Cflag
  87. BS0* {.importc, header: "<termios.h>".}: Cflag
  88. BS1* {.importc, header: "<termios.h>".}: Cflag
  89. FFDLY* {.importc, header: "<termios.h>".}: Cflag
  90. FF0* {.importc, header: "<termios.h>".}: Cflag
  91. FF1* {.importc, header: "<termios.h>".}: Cflag
  92. VTDLY* {.importc, header: "<termios.h>".}: Cflag
  93. VT0* {.importc, header: "<termios.h>".}: Cflag
  94. VT1* {.importc, header: "<termios.h>".}: Cflag
  95. # cflag bit meaning
  96. var
  97. B0* {.importc, header: "<termios.h>".}: Speed
  98. B50* {.importc, header: "<termios.h>".}: Speed
  99. B75* {.importc, header: "<termios.h>".}: Speed
  100. B110* {.importc, header: "<termios.h>".}: Speed
  101. B134* {.importc, header: "<termios.h>".}: Speed
  102. B150* {.importc, header: "<termios.h>".}: Speed
  103. B200* {.importc, header: "<termios.h>".}: Speed
  104. B300* {.importc, header: "<termios.h>".}: Speed
  105. B600* {.importc, header: "<termios.h>".}: Speed
  106. B1200* {.importc, header: "<termios.h>".}: Speed
  107. B1800* {.importc, header: "<termios.h>".}: Speed
  108. B2400* {.importc, header: "<termios.h>".}: Speed
  109. B4800* {.importc, header: "<termios.h>".}: Speed
  110. B9600* {.importc, header: "<termios.h>".}: Speed
  111. B19200* {.importc, header: "<termios.h>".}: Speed
  112. B38400* {.importc, header: "<termios.h>".}: Speed
  113. EXTA* {.importc, header: "<termios.h>".}: Speed
  114. EXTB* {.importc, header: "<termios.h>".}: Speed
  115. CSIZE* {.importc, header: "<termios.h>".}: Cflag
  116. CS5* {.importc, header: "<termios.h>".}: Cflag
  117. CS6* {.importc, header: "<termios.h>".}: Cflag
  118. CS7* {.importc, header: "<termios.h>".}: Cflag
  119. CS8* {.importc, header: "<termios.h>".}: Cflag
  120. CSTOPB* {.importc, header: "<termios.h>".}: Cflag
  121. CREAD* {.importc, header: "<termios.h>".}: Cflag
  122. PARENB* {.importc, header: "<termios.h>".}: Cflag
  123. PARODD* {.importc, header: "<termios.h>".}: Cflag
  124. HUPCL* {.importc, header: "<termios.h>".}: Cflag
  125. CLOCAL* {.importc, header: "<termios.h>".}: Cflag
  126. # lflag bits
  127. var
  128. ISIG* {.importc, header: "<termios.h>".}: Cflag
  129. ICANON* {.importc, header: "<termios.h>".}: Cflag
  130. ECHO* {.importc, header: "<termios.h>".}: Cflag
  131. ECHOE* {.importc, header: "<termios.h>".}: Cflag
  132. ECHOK* {.importc, header: "<termios.h>".}: Cflag
  133. ECHONL* {.importc, header: "<termios.h>".}: Cflag
  134. NOFLSH* {.importc, header: "<termios.h>".}: Cflag
  135. TOSTOP* {.importc, header: "<termios.h>".}: Cflag
  136. IEXTEN* {.importc, header: "<termios.h>".}: Cflag
  137. # tcflow() and TCXONC use these
  138. var
  139. TCOOFF* {.importc, header: "<termios.h>".}: cint
  140. TCOON* {.importc, header: "<termios.h>".}: cint
  141. TCIOFF* {.importc, header: "<termios.h>".}: cint
  142. TCION* {.importc, header: "<termios.h>".}: cint
  143. # tcflush() and TCFLSH use these
  144. var
  145. TCIFLUSH* {.importc, header: "<termios.h>".}: cint
  146. TCOFLUSH* {.importc, header: "<termios.h>".}: cint
  147. TCIOFLUSH* {.importc, header: "<termios.h>".}: cint
  148. # tcsetattr uses these
  149. var
  150. TCSANOW* {.importc, header: "<termios.h>".}: cint
  151. TCSADRAIN* {.importc, header: "<termios.h>".}: cint
  152. TCSAFLUSH* {.importc, header: "<termios.h>".}: cint
  153. # Compare a character C to a value VAL from the `cc' array in a
  154. # `struct termios'. If VAL is _POSIX_VDISABLE, no character can match it.
  155. template cceq*(val, c): untyped =
  156. c == val and val != POSIX_VDISABLE
  157. # Return the output baud rate stored in *TERMIOS_P.
  158. proc cfGetOspeed*(termios: ptr Termios): Speed {.importc: "cfgetospeed",
  159. header: "<termios.h>".}
  160. # Return the input baud rate stored in *TERMIOS_P.
  161. proc cfGetIspeed*(termios: ptr Termios): Speed {.importc: "cfgetispeed",
  162. header: "<termios.h>".}
  163. # Set the output baud rate stored in *TERMIOS_P to SPEED.
  164. proc cfSetOspeed*(termios: ptr Termios; speed: Speed): cint {.
  165. importc: "cfsetospeed", header: "<termios.h>".}
  166. # Set the input baud rate stored in *TERMIOS_P to SPEED.
  167. proc cfSetIspeed*(termios: ptr Termios; speed: Speed): cint {.
  168. importc: "cfsetispeed", header: "<termios.h>".}
  169. # Set both the input and output baud rates in *TERMIOS_OP to SPEED.
  170. proc tcGetAttr*(fd: cint; termios: ptr Termios): cint {.
  171. importc: "tcgetattr", header: "<termios.h>".}
  172. # Set the state of FD to *TERMIOS_P.
  173. # Values for OPTIONAL_ACTIONS (TCSA*) are in <bits/termios.h>.
  174. proc tcSetAttr*(fd: cint; optional_actions: cint; termios: ptr Termios): cint {.
  175. importc: "tcsetattr", header: "<termios.h>".}
  176. # Set *TERMIOS_P to indicate raw mode.
  177. proc tcSendBreak*(fd: cint; duration: cint): cint {.importc: "tcsendbreak",
  178. header: "<termios.h>".}
  179. # Wait for pending output to be written on FD.
  180. #
  181. # This function is a cancellation point and therefore not marked with
  182. # .
  183. proc tcDrain*(fd: cint): cint {.importc: "tcdrain", header: "<termios.h>".}
  184. # Flush pending data on FD.
  185. # Values for QUEUE_SELECTOR (TC{I,O,IO}FLUSH) are in <bits/termios.h>.
  186. proc tcFlush*(fd: cint; queue_selector: cint): cint {.importc: "tcflush",
  187. header: "<termios.h>".}
  188. # Suspend or restart transmission on FD.
  189. # Values for ACTION (TC[IO]{OFF,ON}) are in <bits/termios.h>.
  190. proc tcFlow*(fd: cint; action: cint): cint {.importc: "tcflow",
  191. header: "<termios.h>".}
  192. # Get process group ID for session leader for controlling terminal FD.
  193. # Window size ioctl. Should work on on any Unix that xterm has been ported to.
  194. var TIOCGWINSZ*{.importc, header: "<sys/ioctl.h>".}: culong
  195. type IOctl_WinSize* = object
  196. ws_row*, ws_col*, ws_xpixel*, ws_ypixel*: cushort
  197. proc ioctl*(fd: cint, request: culong, reply: ptr IOctl_WinSize): int {.
  198. importc: "ioctl", header: "<stdio.h>", varargs.}