termios.nim 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.} # dce option deprecated
  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. B57600* {.importc, header: "<termios.h>".}: Speed
  114. B115200* {.importc, header: "<termios.h>".}: Speed
  115. B230400* {.importc, header: "<termios.h>".}: Speed
  116. B460800* {.importc, header: "<termios.h>".}: Speed
  117. B500000* {.importc, header: "<termios.h>".}: Speed
  118. B576000* {.importc, header: "<termios.h>".}: Speed
  119. B921600* {.importc, header: "<termios.h>".}: Speed
  120. B1000000* {.importc, header: "<termios.h>".}: Speed
  121. B1152000* {.importc, header: "<termios.h>".}: Speed
  122. B1500000* {.importc, header: "<termios.h>".}: Speed
  123. B2000000* {.importc, header: "<termios.h>".}: Speed
  124. B2500000* {.importc, header: "<termios.h>".}: Speed
  125. B3000000* {.importc, header: "<termios.h>".}: Speed
  126. B3500000* {.importc, header: "<termios.h>".}: Speed
  127. B4000000* {.importc, header: "<termios.h>".}: Speed
  128. EXTA* {.importc, header: "<termios.h>".}: Speed
  129. EXTB* {.importc, header: "<termios.h>".}: Speed
  130. CSIZE* {.importc, header: "<termios.h>".}: Cflag
  131. CS5* {.importc, header: "<termios.h>".}: Cflag
  132. CS6* {.importc, header: "<termios.h>".}: Cflag
  133. CS7* {.importc, header: "<termios.h>".}: Cflag
  134. CS8* {.importc, header: "<termios.h>".}: Cflag
  135. CSTOPB* {.importc, header: "<termios.h>".}: Cflag
  136. CREAD* {.importc, header: "<termios.h>".}: Cflag
  137. PARENB* {.importc, header: "<termios.h>".}: Cflag
  138. PARODD* {.importc, header: "<termios.h>".}: Cflag
  139. HUPCL* {.importc, header: "<termios.h>".}: Cflag
  140. CLOCAL* {.importc, header: "<termios.h>".}: Cflag
  141. # lflag bits
  142. var
  143. ISIG* {.importc, header: "<termios.h>".}: Cflag
  144. ICANON* {.importc, header: "<termios.h>".}: Cflag
  145. ECHO* {.importc, header: "<termios.h>".}: Cflag
  146. ECHOE* {.importc, header: "<termios.h>".}: Cflag
  147. ECHOK* {.importc, header: "<termios.h>".}: Cflag
  148. ECHONL* {.importc, header: "<termios.h>".}: Cflag
  149. NOFLSH* {.importc, header: "<termios.h>".}: Cflag
  150. TOSTOP* {.importc, header: "<termios.h>".}: Cflag
  151. IEXTEN* {.importc, header: "<termios.h>".}: Cflag
  152. # tcflow() and TCXONC use these
  153. var
  154. TCOOFF* {.importc, header: "<termios.h>".}: cint
  155. TCOON* {.importc, header: "<termios.h>".}: cint
  156. TCIOFF* {.importc, header: "<termios.h>".}: cint
  157. TCION* {.importc, header: "<termios.h>".}: cint
  158. # tcflush() and TCFLSH use these
  159. var
  160. TCIFLUSH* {.importc, header: "<termios.h>".}: cint
  161. TCOFLUSH* {.importc, header: "<termios.h>".}: cint
  162. TCIOFLUSH* {.importc, header: "<termios.h>".}: cint
  163. # tcsetattr uses these
  164. var
  165. TCSANOW* {.importc, header: "<termios.h>".}: cint
  166. TCSADRAIN* {.importc, header: "<termios.h>".}: cint
  167. TCSAFLUSH* {.importc, header: "<termios.h>".}: cint
  168. # Compare a character C to a value VAL from the `cc' array in a
  169. # `struct termios'. If VAL is _POSIX_VDISABLE, no character can match it.
  170. template cceq*(val, c): untyped =
  171. c == val and val != POSIX_VDISABLE
  172. # Return the output baud rate stored in *TERMIOS_P.
  173. proc cfGetOspeed*(termios: ptr Termios): Speed {.importc: "cfgetospeed",
  174. header: "<termios.h>".}
  175. # Return the input baud rate stored in *TERMIOS_P.
  176. proc cfGetIspeed*(termios: ptr Termios): Speed {.importc: "cfgetispeed",
  177. header: "<termios.h>".}
  178. # Set the output baud rate stored in *TERMIOS_P to SPEED.
  179. proc cfSetOspeed*(termios: ptr Termios; speed: Speed): cint {.
  180. importc: "cfsetospeed", header: "<termios.h>".}
  181. # Set the input baud rate stored in *TERMIOS_P to SPEED.
  182. proc cfSetIspeed*(termios: ptr Termios; speed: Speed): cint {.
  183. importc: "cfsetispeed", header: "<termios.h>".}
  184. # Set both the input and output baud rates in *TERMIOS_OP to SPEED.
  185. proc tcGetAttr*(fd: cint; termios: ptr Termios): cint {.
  186. importc: "tcgetattr", header: "<termios.h>".}
  187. # Set the state of FD to *TERMIOS_P.
  188. # Values for OPTIONAL_ACTIONS (TCSA*) are in <bits/termios.h>.
  189. proc tcSetAttr*(fd: cint; optional_actions: cint; termios: ptr Termios): cint {.
  190. importc: "tcsetattr", header: "<termios.h>".}
  191. # Set *TERMIOS_P to indicate raw mode.
  192. proc tcSendBreak*(fd: cint; duration: cint): cint {.importc: "tcsendbreak",
  193. header: "<termios.h>".}
  194. # Wait for pending output to be written on FD.
  195. #
  196. # This function is a cancellation point and therefore not marked with
  197. # .
  198. proc tcDrain*(fd: cint): cint {.importc: "tcdrain", header: "<termios.h>".}
  199. # Flush pending data on FD.
  200. # Values for QUEUE_SELECTOR (TC{I,O,IO}FLUSH) are in <bits/termios.h>.
  201. proc tcFlush*(fd: cint; queue_selector: cint): cint {.importc: "tcflush",
  202. header: "<termios.h>".}
  203. # Suspend or restart transmission on FD.
  204. # Values for ACTION (TC[IO]{OFF,ON}) are in <bits/termios.h>.
  205. proc tcFlow*(fd: cint; action: cint): cint {.importc: "tcflow",
  206. header: "<termios.h>".}
  207. # Get process group ID for session leader for controlling terminal FD.
  208. # Window size ioctl. Should work on on any Unix that xterm has been ported to.
  209. var TIOCGWINSZ*{.importc, header: "<sys/ioctl.h>".}: culong
  210. type IOctl_WinSize* = object
  211. ws_row*, ws_col*, ws_xpixel*, ws_ypixel*: cushort
  212. proc ioctl*(fd: cint, request: culong, reply: ptr IOctl_WinSize): int {.
  213. importc: "ioctl", header: "<stdio.h>", varargs.}