termios.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. termios.h
  9. Abstract:
  10. This header contains definitions for terminal control.
  11. Author:
  12. Evan Green 23-Jun-2013
  13. --*/
  14. #ifndef _TERMIOS_H
  15. #define _TERMIOS_H
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <sys/types.h>
  20. //
  21. // ---------------------------------------------------------------- Definitions
  22. //
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. //
  27. // Define input control flags.
  28. //
  29. //
  30. // Set this flag to ignore break conditions.
  31. //
  32. #define IGNBRK 0x00000001
  33. //
  34. // Set this flag to signal an interrupt on break.
  35. //
  36. #define BRKINT 0x00000002
  37. //
  38. // Set this flag to ignore characters with parity errors.
  39. //
  40. #define IGNPAR 0x00000004
  41. //
  42. // Set this flag to mark parity errors.
  43. //
  44. #define PARMRK 0x00000008
  45. //
  46. // Set this flag to enable input parity checking.
  47. //
  48. #define INPCK 0x00000010
  49. //
  50. // Set this flag to strip characters.
  51. //
  52. #define ISTRIP 0x00000020
  53. //
  54. // Set this flag to map newlines (\n) to carraige returns (\r) on input.
  55. //
  56. #define INLCR 0x00000040
  57. //
  58. // Set this flag to ignore carraige returns.
  59. //
  60. #define IGNCR 0x00000080
  61. //
  62. // Set this flag to map carraige return (\r) characters to newlines (\n) on
  63. // input.
  64. //
  65. #define ICRNL 0x00000100
  66. //
  67. // Set this flag to enable start/stop output control.
  68. //
  69. #define IXON 0x00000200
  70. //
  71. // Set this flag to enable start/stop input control.
  72. //
  73. #define IXOFF 0x00000400
  74. //
  75. // Set this flag to enable any character to restart output.
  76. //
  77. #define IXANY 0x00000800
  78. //
  79. // Set this flag to cause a bell character to be sent to the output if the
  80. // input buffer is full. If this flag is not set and a new character is
  81. // received when the input queue is full, then the entire current input and
  82. // output queue is discarded.
  83. //
  84. #define IMAXBEL 0x00001000
  85. //
  86. // Define output control flags.
  87. //
  88. //
  89. // Set this flag to post-process output.
  90. //
  91. #define OPOST 0x00000001
  92. //
  93. // Set this flag to map newlines (\n) or CR-NL (\r\n) on output.
  94. //
  95. #define ONLCR 0x00000002
  96. //
  97. // Set this flag to map carraige returns (\r) to newlines (\n) on output.
  98. //
  99. #define OCRNL 0x00000004
  100. //
  101. // Set this flag to avoid carraige return output at column 0.
  102. //
  103. #define ONOCR 0x00000008
  104. //
  105. // Set this flag to have newline perform carraige return functionality.
  106. //
  107. #define ONLRET 0x00000010
  108. //
  109. // Set this flag to use fill characters for delay.
  110. //
  111. #define OFILL 0x00000020
  112. //
  113. // Set this flag to enable newline delays, type 0 or mode 1 (0.1 seconds).
  114. //
  115. #define NLDLY (NL0 | NL1)
  116. #define NL0 0x00000000
  117. #define NL1 0x00000040
  118. //
  119. // Set this flag to select carraige return delays, types 0 through 3.
  120. // Type 1 delays for an amount dependent on column position. Type 2 is about
  121. // 0.1 seconds, and type 3 is about 0.15 seconds. If OFILL is set, type 1
  122. // transmits two fill characters and type 2 transmits four fill characters.
  123. //
  124. #define CRDLY (CR0 | CR1 | CR2 | CR3)
  125. #define CR0 0x00000000
  126. #define CR1 0x00000080
  127. #define CR2 0x00000100
  128. #define CR3 0x00000180
  129. //
  130. // Set this flag to enable tab delays, types 0 through 3.
  131. // Type 1 is dependent on column positions, type 2 is 0.1 seconds, and type 3
  132. // is "expand tabs to spaces". If OFILL is set, any delay transmits two fill
  133. // characters.
  134. //
  135. #define TABDLY (TAB0 | TAB1 | TAB2 | TAB3)
  136. #define TAB0 0x00000000
  137. #define TAB1 0x00000200
  138. #define TAB2 0x00000400
  139. #define TAB3 0x00000600
  140. //
  141. // Set this flag to enable backspace delays, type 0 or type 1. Type 1 is
  142. // 0.05 seconds or one fill character.
  143. //
  144. #define BSDLY (BS0 | BS1)
  145. #define BS0 0x00000000
  146. #define BS1 0x00000800
  147. //
  148. // Set this flag to enable vertical tab delays, type 0 or 1. Type 1 lasts two
  149. // seconds.
  150. //
  151. #define VTDLY (VT0 | VT1)
  152. #define VT0 0x00000000
  153. #define VT1 0x00001000
  154. //
  155. // Set this flag to enable form fee delays, type 0 or 1. Type 1 lasts two
  156. // seconds.
  157. //
  158. #define FFDLY (FF0 | FF1)
  159. #define FF0 0x00000000
  160. #define FF1 0x00002000
  161. //
  162. // Set this flag to set the fill character to ASCII DEL (127). If this flag is
  163. // not set, the fill character is NUL (0).
  164. //
  165. #define OFDEL 0x00004000
  166. //
  167. // Define valid baud rates.
  168. //
  169. //
  170. // Hang up.
  171. //
  172. #define B0 0
  173. //
  174. // The remainder correspond to their exact baud rates, except for B134 which
  175. // corresponds to a baud rate of 134.5. Because I know you cared a lot about
  176. // that one.
  177. //
  178. #define B50 50
  179. #define B75 75
  180. #define B110 110
  181. #define B134 134
  182. #define B150 150
  183. #define B200 200
  184. #define B300 300
  185. #define B600 600
  186. #define B1200 1200
  187. #define B1800 1800
  188. #define B2400 2400
  189. #define B4800 4800
  190. #define B9600 9600
  191. #define B19200 19200
  192. #define B38400 38400
  193. #define B57600 57600
  194. #define B115200 115200
  195. #define B230400 230400
  196. #define B460800 460800
  197. #define B500000 500000
  198. #define B576000 576000
  199. #define B921600 921600
  200. #define B1000000 1000000
  201. #define B1152000 1152000
  202. #define B1500000 1500000
  203. #define B2000000 2000000
  204. #define B2500000 2500000
  205. #define B3000000 3000000
  206. #define B3500000 3500000
  207. #define B4000000 4000000
  208. //
  209. // Define control mode flags.
  210. //
  211. //
  212. // Set this field to set the number of bits per character.
  213. //
  214. #define CSIZE (CS5 | CS6 | CS7 | CS8)
  215. #define CS5 0x00000000
  216. #define CS6 0x00000001
  217. #define CS7 0x00000002
  218. #define CS8 0x00000003
  219. //
  220. // Set this bit to send two stop bits (without it set one stop bit is sent).
  221. //
  222. #define CSTOPB 0x00000004
  223. //
  224. // Set this bit to enable the receiver.
  225. //
  226. #define CREAD 0x00000008
  227. //
  228. // Set this bit to enable a parity bit.
  229. //
  230. #define PARENB 0x00000010
  231. //
  232. // Set this bit to enable odd parity (without this bit set even parity is used).
  233. //
  234. #define PARODD 0x00000020
  235. //
  236. // Set this bit to send a hangup signal when the terminal is closed.
  237. //
  238. #define HUPCL 0x00000040
  239. //
  240. // Set this bit to ignore modem status lines (and do not send a hangup signal).
  241. //
  242. #define CLOCAL 0x00000080
  243. //
  244. // Define local mode flags.
  245. //
  246. //
  247. // Set this bit enable automatically echoing input characters to the output.
  248. //
  249. #define ECHO 0x00000001
  250. //
  251. // Set this bit to enable echoing the erase character.
  252. //
  253. #define ECHOE 0x00000002
  254. //
  255. // Set this bit to enable echoing the kill character.
  256. //
  257. #define ECHOK 0x00000004
  258. //
  259. // Set this bit to enable echoing newline (\n) characters.
  260. //
  261. #define ECHONL 0x00000008
  262. //
  263. // Set this bit to enable canonical mode, which enables a handful of
  264. // automatic line processing features.
  265. //
  266. #define ICANON 0x00000010
  267. //
  268. // Set this bit to enable extended processing. With extended processing,
  269. // the erase, kill, and end of file characters can be preceded by a backslash
  270. // to remove their special meaning.
  271. //
  272. #define IEXTEN 0x00000020
  273. //
  274. // Set this bit to enable extended input character processing.
  275. //
  276. #define ISIG 0x00000040
  277. //
  278. // Set this bit to disable flushing after an interrupt or quit character.
  279. //
  280. #define NOFLSH 0x00000080
  281. //
  282. // Set this bit to send a SIGTTOU signal for background output.
  283. //
  284. #define TOSTOP 0x00000100
  285. //
  286. // Set this bit to enable echoing the kill character as visually erasing the
  287. // entire line. If this is not set, then the ECHOK flag controls behavior.
  288. //
  289. #define ECHOKE 0x00000200
  290. //
  291. // Set this bit to enable echoing control characters with '^' followed by their
  292. // corresponding text character.
  293. //
  294. #define ECHOCTL 0x00000400
  295. //
  296. // Define constants used to determine when an attribute change will take
  297. // effect.
  298. //
  299. //
  300. // Define the value used to enact changes immediately.
  301. //
  302. #define TCSANOW 0
  303. //
  304. // Define the value used to enact changes when the output has drained.
  305. //
  306. #define TCSADRAIN 1
  307. //
  308. // Define the value used to enact changes when the output has drained, also
  309. // flush pending input.
  310. //
  311. #define TCSAFLUSH 2
  312. //
  313. // Define flush actions.
  314. //
  315. //
  316. // Use this value to flush pending input.
  317. //
  318. #define TCIFLUSH 0x2
  319. //
  320. // Use this value to flush untransmitted output.
  321. //
  322. #define TCOFLUSH 0x4
  323. //
  324. // Use this value to flush both pending input and untransmitted output.
  325. //
  326. #define TCIOFLUSH (TCIFLUSH | TCOFLUSH)
  327. //
  328. // Define flow control constants.
  329. //
  330. //
  331. // Use this constant to transmit a STOP character, intended to suspend input
  332. // data.
  333. //
  334. #define TCIOFF 0
  335. //
  336. // Use this constant to transmit a START character intended to restart input
  337. // data.
  338. //
  339. #define TCION 1
  340. //
  341. // Use this constant to suspend output.
  342. //
  343. #define TCOOFF 2
  344. //
  345. // Use this constant to restart output.
  346. //
  347. #define TCOON 3
  348. //
  349. // Define the indices of various control characters in the array.
  350. //
  351. //
  352. // Define the End Of File character index.
  353. //
  354. #define VEOF 0
  355. //
  356. // Define the End of Line character index.
  357. //
  358. #define VEOL 1
  359. //
  360. // Define the erase character index.
  361. //
  362. #define VERASE 2
  363. //
  364. // Define the interrupt character index.
  365. //
  366. #define VINTR 3
  367. //
  368. // Define the kill character index.
  369. //
  370. #define VKILL 4
  371. //
  372. // Define the minimum character count to flush index.
  373. //
  374. #define VMIN 5
  375. //
  376. // Define the quit character index.
  377. //
  378. #define VQUIT 6
  379. //
  380. // Define the start flow control character index.
  381. //
  382. #define VSTART 7
  383. //
  384. // Define the stop flow control character index.
  385. //
  386. #define VSTOP 8
  387. //
  388. // Define the suspend character index.
  389. //
  390. #define VSUSP 9
  391. //
  392. // Define the time before flushing anyway index.
  393. //
  394. #define VTIME 10
  395. //
  396. // Define the array size for control characters.
  397. //
  398. #define NCCS 11
  399. //
  400. // ------------------------------------------------------ Data Type Definitions
  401. //
  402. //
  403. // Define the type used for terminal special characters.
  404. //
  405. typedef char cc_t;
  406. //
  407. // Define the type used for baud rates.
  408. //
  409. typedef int speed_t;
  410. //
  411. // Define the type used for terminal modes.
  412. //
  413. typedef long tcflag_t;
  414. /*++
  415. Structure Description:
  416. This structure stores terminal behavior and control data.
  417. Members:
  418. c_iflag - Stores the input mode flags.
  419. c_oflag - Stores the output mode flags.
  420. c_cflag - Stores the control mode flags.
  421. c_lflag - Stores the local control flags.
  422. c_cc - Stores the characters to use for control characters.
  423. c_ispeed - Stores the input baud rate.
  424. c_ospeed - Stores the output baud rate.
  425. --*/
  426. struct termios {
  427. tcflag_t c_iflag;
  428. tcflag_t c_oflag;
  429. tcflag_t c_cflag;
  430. tcflag_t c_lflag;
  431. cc_t c_cc[NCCS];
  432. speed_t c_ispeed;
  433. speed_t c_ospeed;
  434. };
  435. //
  436. // -------------------------------------------------------------------- Globals
  437. //
  438. //
  439. // -------------------------------------------------------- Function Prototypes
  440. //
  441. LIBC_API
  442. int
  443. tcgetattr (
  444. int FileDescriptor,
  445. struct termios *Settings
  446. );
  447. /*++
  448. Routine Description:
  449. This routine gets the current terminal settings.
  450. Arguments:
  451. FileDescriptor - Supplies the file descriptor for the terminal.
  452. Settings - Supplies a pointer where the terminal settings will be returned
  453. on success.
  454. Return Value:
  455. 0 on success.
  456. -1 on failure, and the errno variable will be set to provide more
  457. information.
  458. --*/
  459. LIBC_API
  460. int
  461. tcsetattr (
  462. int FileDescriptor,
  463. int When,
  464. const struct termios *NewSettings
  465. );
  466. /*++
  467. Routine Description:
  468. This routine sets the given terminal's attributes.
  469. Arguments:
  470. FileDescriptor - Supplies the file descriptor for the terminal.
  471. When - Supplies more information about when the new settings should take
  472. effect. See TCSA* definitions.
  473. NewSettings - Supplies a pointer to the new settings.
  474. Return Value:
  475. 0 on success.
  476. -1 on failure, and the errno variable will be set to provide more
  477. information.
  478. --*/
  479. LIBC_API
  480. speed_t
  481. cfgetispeed (
  482. const struct termios *Settings
  483. );
  484. /*++
  485. Routine Description:
  486. This routine gets the input baud rate from the given terminal settings.
  487. Arguments:
  488. Settings - Supplies a pointer to the terminal settings retrieved with a
  489. call to tcgetattr.
  490. Return Value:
  491. Returns the input speed, in baud.
  492. --*/
  493. LIBC_API
  494. speed_t
  495. cfgetospeed (
  496. const struct termios *Settings
  497. );
  498. /*++
  499. Routine Description:
  500. This routine gets the output baud rate from the given terminal settings.
  501. Arguments:
  502. Settings - Supplies a pointer to the terminal settings retrieved with a
  503. call to tcgetattr.
  504. Return Value:
  505. Returns the output speed, in baud.
  506. --*/
  507. LIBC_API
  508. int
  509. cfsetispeed (
  510. struct termios *Settings,
  511. speed_t NewBaudRate
  512. );
  513. /*++
  514. Routine Description:
  515. This routine sets the input baud rate from the given terminal settings.
  516. Arguments:
  517. Settings - Supplies a pointer to the terminal settings retrieved with a
  518. call to tcgetattr.
  519. NewBaudRate - Supplies the new input baud rate to set.
  520. Return Value:
  521. 0 on success.
  522. -1 on failure, and the errno will be set to EINVAL to indicate the given
  523. baud rate is invalid or not achievable.
  524. --*/
  525. LIBC_API
  526. int
  527. cfsetospeed (
  528. struct termios *Settings,
  529. speed_t NewBaudRate
  530. );
  531. /*++
  532. Routine Description:
  533. This routine sets the output baud rate from the given terminal settings.
  534. Arguments:
  535. Settings - Supplies a pointer to the terminal settings retrieved with a
  536. call to tcgetattr.
  537. NewBaudRate - Supplies the new output baud rate to set.
  538. Return Value:
  539. 0 on success.
  540. -1 on failure, and the errno will be set to EINVAL to indicate the given
  541. baud rate is invalid or not achievable.
  542. --*/
  543. LIBC_API
  544. void
  545. cfmakeraw (
  546. struct termios *Settings
  547. );
  548. /*++
  549. Routine Description:
  550. This routine sets the given settings to "raw" mode, disabling all line
  551. processing features and making the terminal as basic as possible.
  552. Arguments:
  553. Settings - Supplies a pointer to the terminal settings to adjust to raw
  554. mode.
  555. Return Value:
  556. None.
  557. --*/
  558. LIBC_API
  559. int
  560. tcflush (
  561. int FileDescriptor,
  562. int Selector
  563. );
  564. /*++
  565. Routine Description:
  566. This routine discards data written to the the given terminal, data received
  567. but not yet read from the terminal, or both.
  568. Attempts to use this function from a process which is a member of the
  569. background process group on the given terminal will cause the process group
  570. to be sent a SIGTTOU. If the calling process is blocking or ignoring
  571. SIGTTOU, the process shall be allowed to perform the operation, and no
  572. signal is sent.
  573. Arguments:
  574. FileDescriptor - Supplies the file descriptor of the terminal to flush.
  575. Selector - Supplies the type of flush to perform. Valid values are
  576. TCIFLUSH to flush data received but not read, TCOFLUSH to flush data
  577. written but not transmitted, and TCIOFLUSH to flush both types.
  578. Return Value:
  579. 0 on success.
  580. -1 on failure, and the errno will be set to contain more information.
  581. --*/
  582. LIBC_API
  583. int
  584. tcdrain (
  585. int FileDescriptor
  586. );
  587. /*++
  588. Routine Description:
  589. This routine waits until all output written to the terminal at the given
  590. file descriptor is written.
  591. Attempts to use this function from a process which is a member of the
  592. background process group on the given terminal will cause the process group
  593. to be sent a SIGTTOU. If the calling process is blocking or ignoring
  594. SIGTTOU, the process shall be allowed to perform the operation, and no
  595. signal is sent.
  596. Arguments:
  597. FileDescriptor - Supplies the file descriptor of the terminal to drain.
  598. Return Value:
  599. 0 on success.
  600. -1 on failure, and the errno will be set to contain more information.
  601. --*/
  602. LIBC_API
  603. int
  604. tcflow (
  605. int FileDescriptor,
  606. int Action
  607. );
  608. /*++
  609. Routine Description:
  610. This routine suspends or restarts transmission of data on the given
  611. terminal.
  612. Attempts to use this function from a process which is a member of the
  613. background process group on the given terminal will cause the process group
  614. to be sent a SIGTTOU. If the calling process is blocking or ignoring
  615. SIGTTOU, the process shall be allowed to perform the operation, and no
  616. signal is sent.
  617. Arguments:
  618. FileDescriptor - Supplies the file descriptor of the terminal.
  619. Action - Supplies the action to perform. Valid values are:
  620. TCOOFF - Suspends output.
  621. TCOON - Resumes suspended output.
  622. TCIOFF - Causes the system to transmit a STOP character, which is
  623. intended to cause the terminal device to stop transmitting data to
  624. this system.
  625. TCION - Causes the system to transmit a START character, which is
  626. intended to restart the sending of data to this terminal.
  627. Return Value:
  628. 0 on success.
  629. -1 on failure, and the errno will be set to contain more information.
  630. --*/
  631. LIBC_API
  632. int
  633. tcsendbreak (
  634. int FileDescriptor,
  635. int Duration
  636. );
  637. /*++
  638. Routine Description:
  639. This routine sends a continuous stream of zero-valued bits for a specific
  640. duration if the given terminal is using asynchronous serial data
  641. transmission. If the terminal is not using asynchronous serial data
  642. transmission, this routine returns without performing any action.
  643. Attempts to use this function from a process which is a member of the
  644. background process group on the given terminal will cause the process group
  645. to be sent a SIGTTOU. If the calling process is blocking or ignoring
  646. SIGTTOU, the process shall be allowed to perform the operation, and no
  647. signal is sent.
  648. Arguments:
  649. FileDescriptor - Supplies the file descriptor of the terminal.
  650. Duration - Supplies a value that if zero causes the duration to be between
  651. 0.25 and 0.5 seconds. If duration is not zero, it sends zero-valued
  652. bits for an implementation-defined length of time.
  653. Return Value:
  654. 0 on success.
  655. -1 on failure, and the errno will be set to contain more information.
  656. --*/
  657. LIBC_API
  658. pid_t
  659. tcgetsid (
  660. int FileDescriptor
  661. );
  662. /*++
  663. Routine Description:
  664. This routine gets the process group ID of the session for which the
  665. terminal specified by the given file descriptor is the controlling terminal.
  666. Arguments:
  667. FileDescriptor - Supplies the file descriptor of the terminal.
  668. Return Value:
  669. Returns the process group ID associated with the terminal on success.
  670. -1 on failure, and errno will be set to contain more information.
  671. --*/
  672. #ifdef __cplusplus
  673. }
  674. #endif
  675. #endif