termios.nim 9.5 KB

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