termios.nim 9.6 KB

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