terminal.texi 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  1. @node Low-Level Terminal Interface, Syslog, Sockets, Top
  2. @c %MENU% How to change the characteristics of a terminal device
  3. @chapter Low-Level Terminal Interface
  4. This chapter describes functions that are specific to terminal devices.
  5. You can use these functions to do things like turn off input echoing;
  6. set serial line characteristics such as line speed and flow control; and
  7. change which characters are used for end-of-file, command-line editing,
  8. sending signals, and similar control functions.
  9. Most of the functions in this chapter operate on file descriptors.
  10. @xref{Low-Level I/O}, for more information about what a file
  11. descriptor is and how to open a file descriptor for a terminal device.
  12. @menu
  13. * Terminal Device Model:: The fundamental concepts of a terminal device
  14. * Is It a Terminal:: How to determine if a file is a terminal
  15. device, and what its name is.
  16. * I/O Queues:: About flow control and typeahead.
  17. * Canonical or Not:: Two basic styles of input processing.
  18. * Terminal Modes:: How to examine and modify flags controlling
  19. details of terminal I/O: echoing,
  20. signals, editing. Posix.
  21. * BSD Terminal Modes:: BSD compatible terminal mode setting
  22. * Line Control:: Sending break sequences, clearing
  23. terminal buffers @dots{}
  24. * Noncanon Example:: How to read single characters without echo.
  25. * getpass:: Prompting the user for a passphrase.
  26. * Pseudo-Terminals:: How to open a pseudo-terminal.
  27. @end menu
  28. @node Terminal Device Model
  29. @section Terminal Device Model
  30. @cindex terminal model
  31. A @dfn{terminal device}, abbreviated @code{tty} (for @dfn{teletype}), is
  32. a character device which implements a set of functionality appropriate
  33. for communications devices, and which can host an interactive login
  34. session. Conceptually, a terminal device implements an RS232
  35. asynchronous serial interface, but the actual hardware implementation
  36. may be entirely different, or it may be entirely virtual, notably
  37. @pxref{Pseudo-Terminals}.
  38. For a true conventional asynchronous serial port, such as RS232/V.24,
  39. RS422/V.11, RS423, or RS485, the functionality is generally as
  40. described, whereas for other devices, the meaning of serial port
  41. specific functionality such as modem control signals, BREAK, and line
  42. speed is device specific.
  43. The rest of this section is described in terms of a physical RS232
  44. interface.
  45. The RS232 specification assumes the host (Data Terminal Equipment, DTE)
  46. connects to a modem (Data Communications Equipment, DCE), regardless of
  47. if a physical modem is present or not.
  48. In addition to the serial data, the DTE provides a set of control
  49. signals to the DCE, and the DCE a set of status signals to the DTE. The
  50. full RS232 and V.24 specifications provide a large number of signals,
  51. but the ones that are typically implemented in contemporary hardware and
  52. are relevant to the terminal device interface are:
  53. @table @asis
  54. @item DTR - Data Terminal Ready (control)
  55. If asserted (true), the DTE is ready to accept/continue an incoming
  56. communications session. If deasserted (false), this is a
  57. @dfn{modem disconnect request} to the DCE. The DCE may, but is not
  58. required to, trigger a modem disconnect in response.
  59. @cindex modem disconnect request
  60. @item RTS - Request To Send (control)
  61. This signal is also referred to as Ready To Receive (RTR).
  62. If asserted, the DTE is ready to accept data. If deasserted, the DCE is
  63. requested to hold data temporarily without disconnecting. This is known
  64. as hardware or RTS/CTS @dfn{flow control} and can be handled
  65. automatically if the appropriate terminal mode flags are set.
  66. @item DSR - Data Set Ready (status)
  67. If asserted, the DCE is ready to communicate, but may or may not have a
  68. connection to a remote peer.
  69. @item DCD - Data Carrier Detect (status)
  70. If asserted, the DCE has a connection to the remote peer. If
  71. deasserted, this is a @dfn{modem disconnect} signal to the DTE. A modem
  72. disconnect may be triggered in response to the DTR control signal being
  73. deasserted, or it may be caused by an external event.
  74. @cindex modem disconnect
  75. @item CTS - Clear To Send (status)
  76. If asserted, the DCE is ready to accept data. If deasserted, the DTE is
  77. requested to hold data temporarily but should not interpret it as a
  78. disconnect. This is the DCE to DTE part of RTS/CTS flow
  79. control.
  80. @item RI - Ring Indicator (status)
  81. If asserted, this indicates that a remote peer is requesting to connect
  82. (``the phone is ringing''). Depending on how the DCE is configured, the
  83. DTE may need to assert the DTR control signal before the DCE will accept
  84. the incoming connection.
  85. @end table
  86. @node Is It a Terminal
  87. @section Identifying Terminals
  88. @cindex terminal identification
  89. @cindex identifying terminals
  90. The functions described in this chapter only work on files that
  91. correspond to terminal devices. You can find out whether a file
  92. descriptor is associated with a terminal by using the @code{isatty}
  93. function.
  94. @pindex unistd.h
  95. Prototypes for the functions in this section are declared in the header
  96. file @file{unistd.h}.
  97. @deftypefun int isatty (int @var{filedes})
  98. @standards{POSIX.1, unistd.h}
  99. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  100. @c isatty ok
  101. @c tcgetattr dup ok
  102. This function returns @code{1} if @var{filedes} is a file descriptor
  103. associated with an open terminal device, and @math{0} otherwise.
  104. @end deftypefun
  105. If a file descriptor is associated with a terminal, you can get its
  106. associated file name using the @code{ttyname} function. See also the
  107. @code{ctermid} function, described in @ref{Identifying the Terminal}.
  108. @deftypefun {char *} ttyname (int @var{filedes})
  109. @standards{POSIX.1, unistd.h}
  110. @safety{@prelim{}@mtunsafe{@mtasurace{:ttyname}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
  111. @c ttyname @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
  112. @c isatty dup ok
  113. @c fstat dup ok
  114. @c memcpy dup ok
  115. @c getttyname @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
  116. @c opendir @ascuheap @acsmem @acsfd
  117. @c readdir ok [protected by exclusive access]
  118. @c strcmp dup ok
  119. @c free dup @asulock @aculock @acsfd @acsmem
  120. @c malloc dup @asulock @aculock @acsfd @acsmem
  121. @c closedir @ascuheap @acsmem @acsfd
  122. @c mempcpy dup ok
  123. @c stat dup ok
  124. If the file descriptor @var{filedes} is associated with a terminal
  125. device, the @code{ttyname} function returns a pointer to a
  126. statically-allocated, null-terminated string containing the file name of
  127. the terminal file. The value is a null pointer if the file descriptor
  128. isn't associated with a terminal, or the file name cannot be determined.
  129. @end deftypefun
  130. @deftypefun int ttyname_r (int @var{filedes}, char *@var{buf}, size_t @var{len})
  131. @standards{POSIX.1, unistd.h}
  132. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{} @acsfd{}}}
  133. @c ttyname_r @ascuheap @acsmem @acsfd
  134. @c isatty dup ok
  135. @c fstat dup ok
  136. @c memcpy dup ok
  137. @c getttyname_r @ascuheap @acsmem @acsfd
  138. @c opendir @ascuheap @acsmem @acsfd
  139. @c readdir ok [protected by exclusive access]
  140. @c strcmp dup ok
  141. @c closedir @ascuheap @acsmem @acsfd
  142. @c stpncpy dup ok
  143. @c stat dup ok
  144. The @code{ttyname_r} function is similar to the @code{ttyname} function
  145. except that it places its result into the user-specified buffer starting
  146. at @var{buf} with length @var{len}.
  147. The normal return value from @code{ttyname_r} is @math{0}. Otherwise an
  148. error number is returned to indicate the error. The following
  149. @code{errno} error conditions are defined for this function:
  150. @table @code
  151. @item EBADF
  152. The @var{filedes} argument is not a valid file descriptor.
  153. @item ENOTTY
  154. The @var{filedes} is not associated with a terminal.
  155. @item ERANGE
  156. The buffer length @var{len} is too small to store the string to be
  157. returned.
  158. @item ENODEV
  159. The @var{filedes} is associated with a terminal device that is a slave
  160. pseudo-terminal, but the file name associated with that device could
  161. not be determined. This is a GNU extension.
  162. @end table
  163. @end deftypefun
  164. @node I/O Queues
  165. @section I/O Queues
  166. Many of the remaining functions in this section refer to the input and
  167. output queues of a terminal device. These queues implement a form of
  168. buffering @emph{within the kernel} independent of the buffering
  169. implemented by I/O streams (@pxref{I/O on Streams}).
  170. @cindex terminal input queue
  171. @cindex typeahead buffer
  172. The @dfn{terminal input queue} is also sometimes referred to as its
  173. @dfn{typeahead buffer}. It holds the characters that have been received
  174. from the terminal but not yet read by any process.
  175. The size of the input queue is described by the @code{MAX_INPUT} and
  176. @w{@code{_POSIX_MAX_INPUT}} parameters; see @ref{Limits for Files}. You
  177. are guaranteed a queue size of at least @code{MAX_INPUT}, but the queue
  178. might be larger, and might even dynamically change size. If input flow
  179. control is enabled by setting the @code{IXOFF} input mode bit
  180. (@pxref{Input Modes}), the terminal driver transmits STOP and START
  181. characters to the terminal when necessary to prevent the queue from
  182. overflowing. Otherwise, input may be lost if it comes in too fast from
  183. the terminal. In canonical mode, all input stays in the queue until a
  184. newline character is received, so the terminal input queue can fill up
  185. when you type a very long line. @xref{Canonical or Not}.
  186. @cindex terminal output queue
  187. The @dfn{terminal output queue} is like the input queue, but for output;
  188. it contains characters that have been written by processes, but not yet
  189. transmitted to the terminal. If output flow control is enabled by
  190. setting the @code{IXON} input mode bit (@pxref{Input Modes}), the
  191. terminal driver obeys START and STOP characters sent by the terminal to
  192. stop and restart transmission of output.
  193. @dfn{Clearing} the terminal input queue means discarding any characters
  194. that have been received but not yet read. Similarly, clearing the
  195. terminal output queue means discarding any characters that have been
  196. written but not yet transmitted.
  197. @node Canonical or Not
  198. @section Two Styles of Input: Canonical or Not
  199. POSIX systems support two basic modes of input: canonical and
  200. noncanonical.
  201. @cindex canonical input processing
  202. In @dfn{canonical input processing} mode, terminal input is processed in
  203. lines terminated by newline (@code{'\n'}), EOF, or EOL characters. No
  204. input can be read until an entire line has been typed by the user, and
  205. the @code{read} function (@pxref{I/O Primitives}) returns at most a
  206. single line of input, no matter how many bytes are requested.
  207. In canonical input mode, the operating system provides input editing
  208. facilities: some characters are interpreted specially to perform editing
  209. operations within the current line of text, such as ERASE and KILL.
  210. @xref{Editing Characters}.
  211. The constants @code{_POSIX_MAX_CANON} and @code{MAX_CANON} parameterize
  212. the maximum number of bytes which may appear in a single line of
  213. canonical input. @xref{Limits for Files}. You are guaranteed a maximum
  214. line length of at least @code{MAX_CANON} bytes, but the maximum might be
  215. larger, and might even dynamically change size.
  216. @cindex noncanonical input processing
  217. In @dfn{noncanonical input processing} mode, characters are not grouped
  218. into lines, and ERASE and KILL processing is not performed. The
  219. granularity with which bytes are read in noncanonical input mode is
  220. controlled by the MIN and TIME settings. @xref{Noncanonical Input}.
  221. Most programs use canonical input mode, because this gives the user a
  222. way to edit input line by line. The usual reason to use noncanonical
  223. mode is when the program accepts single-character commands or provides
  224. its own editing facilities.
  225. The choice of canonical or noncanonical input is controlled by the
  226. @code{ICANON} flag in the @code{c_lflag} member of @code{struct termios}.
  227. @xref{Local Modes}.
  228. @node Terminal Modes
  229. @section Terminal Modes
  230. @pindex termios.h
  231. This section describes the various terminal attributes that control how
  232. input and output are done. The functions, data structures, and symbolic
  233. constants are all declared in the header file @file{termios.h}.
  234. Don't confuse terminal attributes with file attributes. A device special
  235. file which is associated with a terminal has file attributes as described
  236. in @ref{File Attributes}. These are unrelated to the attributes of the
  237. terminal device itself, which are discussed in this section.
  238. @menu
  239. * Mode Data Types:: The data type @code{struct termios} and
  240. related types.
  241. * Mode Functions:: Functions to read and set the terminal
  242. attributes.
  243. * Setting Modes:: The right way to set terminal attributes
  244. reliably.
  245. * Input Modes:: Flags controlling low-level input handling.
  246. * Output Modes:: Flags controlling low-level output handling.
  247. * Control Modes:: Flags controlling serial port behavior.
  248. * Local Modes:: Flags controlling high-level input handling.
  249. * Line Speed:: How to read and set the terminal line speed.
  250. * Special Characters:: Characters that have special effects,
  251. and how to change them.
  252. * Noncanonical Input:: Controlling how long to wait for input.
  253. @end menu
  254. @node Mode Data Types
  255. @subsection Terminal Mode Data Types
  256. @cindex terminal mode data types
  257. The entire collection of attributes of a terminal is stored in a
  258. structure of type @code{struct termios}. This structure is used
  259. with the functions @code{tcgetattr} and @code{tcsetattr} to read
  260. and set the attributes.
  261. @deftp {Data Type} {struct termios}
  262. @standards{POSIX.1, termios.h}
  263. A @code{struct termios} records all the I/O attributes of a terminal. The
  264. structure includes at least the following members:
  265. @table @code
  266. @item tcflag_t c_iflag
  267. A bit mask specifying flags for input modes; see @ref{Input Modes}.
  268. @item tcflag_t c_oflag
  269. A bit mask specifying flags for output modes; see @ref{Output Modes}.
  270. @item tcflag_t c_cflag
  271. A bit mask specifying flags for control modes; see @ref{Control Modes}.
  272. @item tcflag_t c_lflag
  273. A bit mask specifying flags for local modes; see @ref{Local Modes}.
  274. @item cc_t c_cc[NCCS]
  275. An array specifying which characters are associated with various
  276. control functions; see @ref{Special Characters}.
  277. @end table
  278. The @code{struct termios} structure also contains members which
  279. encode input and output transmission speeds, but the representation is
  280. not specified. @xref{Line Speed}, for how to examine and store the
  281. speed values.
  282. @end deftp
  283. The following sections describe the details of the members of the
  284. @code{struct termios} structure.
  285. @deftp {Data Type} tcflag_t
  286. @standards{POSIX.1, termios.h}
  287. This is an unsigned integer type used to represent the various
  288. bit masks for terminal flags.
  289. @end deftp
  290. @deftp {Data Type} cc_t
  291. @standards{POSIX.1, termios.h}
  292. This is an unsigned integer type used to represent characters associated
  293. with various terminal control functions.
  294. @end deftp
  295. @deftypevr Macro int NCCS
  296. @standards{POSIX.1, termios.h}
  297. The value of this macro is the number of elements in the @code{c_cc}
  298. array.
  299. @end deftypevr
  300. @node Mode Functions
  301. @subsection Terminal Mode Functions
  302. @cindex terminal mode functions
  303. @deftypefun int tcgetattr (int @var{filedes}, struct termios *@var{termios-p})
  304. @standards{POSIX.1, termios.h}
  305. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  306. @c Converting the kernel-returned termios data structure to the userland
  307. @c format does not ensure atomic or consistent writing.
  308. This function is used to examine the attributes of the terminal
  309. device with file descriptor @var{filedes}. The attributes are returned
  310. in the structure that @var{termios-p} points to.
  311. If successful, @code{tcgetattr} returns @math{0}. A return value of @math{-1}
  312. indicates an error. The following @code{errno} error conditions are
  313. defined for this function:
  314. @table @code
  315. @item EBADF
  316. The @var{filedes} argument is not a valid file descriptor.
  317. @item ENOTTY
  318. The @var{filedes} is not associated with a terminal.
  319. @end table
  320. @end deftypefun
  321. @deftypefun int tcsetattr (int @var{filedes}, int @var{when}, const struct termios *@var{termios-p})
  322. @standards{POSIX.1, termios.h}
  323. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  324. @c Converting the incoming termios data structure to the kernel format
  325. @c does not ensure atomic or consistent reading.
  326. This function sets the attributes of the terminal device with file
  327. descriptor @var{filedes}. The new attributes are taken from the
  328. structure that @var{termios-p} points to.
  329. The @var{when} argument specifies how to deal with input and output
  330. already queued. It can be one of the following values:
  331. @vtable @code
  332. @item TCSANOW
  333. @standards{POSIX.1, termios.h}
  334. Make the change immediately.
  335. @item TCSADRAIN
  336. @standards{POSIX.1, termios.h}
  337. Make the change after waiting until all queued output has been written.
  338. You should usually use this option when changing parameters that affect
  339. output.
  340. @item TCSAFLUSH
  341. @standards{POSIX.1, termios.h}
  342. This is like @code{TCSADRAIN}, but also discards any queued input.
  343. @item TCSASOFT
  344. @standards{BSD, termios.h}
  345. This is a flag bit that you can add to any of the above alternatives.
  346. Its meaning is to inhibit alteration of the state of the terminal
  347. hardware. It is a BSD extension; it is only supported on BSD systems
  348. and @gnuhurdsystems{}.
  349. Using @code{TCSASOFT} is exactly the same as setting the @code{CIGNORE}
  350. bit in the @code{c_cflag} member of the structure @var{termios-p} points
  351. to. @xref{Control Modes}, for a description of @code{CIGNORE}.
  352. @end vtable
  353. If this function is called from a background process on its controlling
  354. terminal, normally all processes in the process group are sent a
  355. @code{SIGTTOU} signal, in the same way as if the process were trying to
  356. write to the terminal. The exception is if the calling process itself
  357. is ignoring or blocking @code{SIGTTOU} signals, in which case the
  358. operation is performed and no signal is sent. @xref{Job Control}.
  359. If successful, @code{tcsetattr} returns @math{0}. A return value of
  360. @math{-1} indicates an error. The following @code{errno} error
  361. conditions are defined for this function:
  362. @table @code
  363. @item EBADF
  364. The @var{filedes} argument is not a valid file descriptor.
  365. @item ENOTTY
  366. The @var{filedes} is not associated with a terminal.
  367. @item EINVAL
  368. Either the value of the @code{when} argument is not valid, or there is
  369. something wrong with the data in the @var{termios-p} argument.
  370. @end table
  371. @end deftypefun
  372. Although @code{tcgetattr} and @code{tcsetattr} specify the terminal
  373. device with a file descriptor, the attributes are those of the terminal
  374. device itself and not of the file descriptor. This means that the
  375. effects of changing terminal attributes are persistent; if another
  376. process opens the terminal file later on, it will see the changed
  377. attributes even though it doesn't have anything to do with the open file
  378. descriptor you originally specified in changing the attributes.
  379. Similarly, if a single process has multiple or duplicated file
  380. descriptors for the same terminal device, changing the terminal
  381. attributes affects input and output to all of these file
  382. descriptors. This means, for example, that you can't open one file
  383. descriptor or stream to read from a terminal in the normal
  384. line-buffered, echoed mode; and simultaneously have another file
  385. descriptor for the same terminal that you use to read from it in
  386. single-character, non-echoed mode. Instead, you have to explicitly
  387. switch the terminal back and forth between the two modes.
  388. @node Setting Modes
  389. @subsection Setting Terminal Modes Properly
  390. When you set terminal modes, you should call @code{tcgetattr} first to
  391. get the current modes of the particular terminal device, modify only
  392. those modes that you are really interested in, and store the result with
  393. @code{tcsetattr}.
  394. It's a bad idea to simply initialize a @code{struct termios} structure
  395. to a chosen set of attributes and pass it directly to @code{tcsetattr}.
  396. Your program may be run years from now, on systems that support members
  397. not documented in this manual. The way to avoid setting these members
  398. to unreasonable values is to avoid changing them.
  399. What's more, different terminal devices may require different mode
  400. settings in order to function properly. So you should avoid blindly
  401. copying attributes from one terminal device to another.
  402. When a member contains a collection of independent flags, as the
  403. @code{c_iflag}, @code{c_oflag} and @code{c_cflag} members do, even
  404. setting the entire member is a bad idea, because particular operating
  405. systems have their own flags. Instead, you should start with the
  406. current value of the member and alter only the flags whose values matter
  407. in your program, leaving any other flags unchanged.
  408. Here is an example of how to set one flag (@code{ISTRIP}) in the
  409. @code{struct termios} structure while properly preserving all the other
  410. data in the structure:
  411. @smallexample
  412. @group
  413. int
  414. set_istrip (int desc, int value)
  415. @{
  416. struct termios settings;
  417. int result;
  418. @end group
  419. @group
  420. result = tcgetattr (desc, &settings);
  421. if (result < 0)
  422. @{
  423. perror ("error in tcgetattr");
  424. return 0;
  425. @}
  426. @end group
  427. @group
  428. settings.c_iflag &= ~ISTRIP;
  429. if (value)
  430. settings.c_iflag |= ISTRIP;
  431. @end group
  432. @group
  433. result = tcsetattr (desc, TCSANOW, &settings);
  434. if (result < 0)
  435. @{
  436. perror ("error in tcsetattr");
  437. return 0;
  438. @}
  439. return 1;
  440. @}
  441. @end group
  442. @end smallexample
  443. @node Input Modes
  444. @subsection Input Modes
  445. This section describes the terminal attribute flags that control
  446. fairly low-level aspects of input processing: handling of parity errors,
  447. break signals, flow control, and @key{RET} and @key{LFD} characters.
  448. All of these flags are bits in the @code{c_iflag} member of the
  449. @code{struct termios} structure. The member is an integer, and you
  450. change flags using the operators @code{&}, @code{|} and @code{^}. Don't
  451. try to specify the entire value for @code{c_iflag}---instead, change
  452. only specific flags and leave the rest untouched (@pxref{Setting
  453. Modes}).
  454. @deftypevr Macro tcflag_t INPCK
  455. @standards{POSIX.1, termios.h}
  456. @cindex parity checking
  457. If this bit is set, input parity checking is enabled. If it is not set,
  458. no checking at all is done for parity errors on input; the
  459. characters are simply passed through to the application.
  460. Parity checking on input processing is independent of whether parity
  461. detection and generation on the underlying terminal hardware is enabled;
  462. see @ref{Control Modes}. For example, you could clear the @code{INPCK}
  463. input mode flag and set the @code{PARENB} control mode flag to ignore
  464. parity errors on input, but still generate parity on output.
  465. If this bit is set, what happens when a parity error is detected depends
  466. on whether the @code{IGNPAR} or @code{PARMRK} bits are set. If neither
  467. of these bits are set, a byte with a parity error is passed to the
  468. application as a @code{'\0'} character.
  469. @end deftypevr
  470. @deftypevr Macro tcflag_t IGNPAR
  471. @standards{POSIX.1, termios.h}
  472. If this bit is set, any byte with a framing or parity error is ignored.
  473. This is only useful if @code{INPCK} is also set.
  474. @end deftypevr
  475. @deftypevr Macro tcflag_t PARMRK
  476. @standards{POSIX.1, termios.h}
  477. If this bit is set, input bytes with parity or framing errors are marked
  478. when passed to the program. This bit is meaningful only when
  479. @code{INPCK} is set and @code{IGNPAR} is not set.
  480. The way erroneous bytes are marked is with two preceding bytes,
  481. @code{377} and @code{0}. Thus, the program actually reads three bytes
  482. for one erroneous byte received from the terminal.
  483. If a valid byte has the value @code{0377}, and @code{ISTRIP} (see below)
  484. is not set, the program might confuse it with the prefix that marks a
  485. parity error. So a valid byte @code{0377} is passed to the program as
  486. two bytes, @code{0377} @code{0377}, in this case.
  487. @end deftypevr
  488. @deftypevr Macro tcflag_t ISTRIP
  489. @standards{POSIX.1, termios.h}
  490. If this bit is set, valid input bytes are stripped to seven bits;
  491. otherwise, all eight bits are available for programs to read.
  492. @end deftypevr
  493. @deftypevr Macro tcflag_t IGNBRK
  494. @standards{POSIX.1, termios.h}
  495. If this bit is set, break conditions are ignored.
  496. @cindex break condition, detecting
  497. A @dfn{break condition} is defined in the context of asynchronous
  498. serial data transmission as a series of zero-value bits longer than a
  499. single byte.
  500. @end deftypevr
  501. @deftypevr Macro tcflag_t BRKINT
  502. @standards{POSIX.1, termios.h}
  503. If this bit is set and @code{IGNBRK} is not set, a break condition
  504. clears the terminal input and output queues and raises a @code{SIGINT}
  505. signal for the foreground process group associated with the terminal.
  506. If neither @code{BRKINT} nor @code{IGNBRK} are set, a break condition is
  507. passed to the application as a single @code{'\0'} character if
  508. @code{PARMRK} is not set, or otherwise as a three-character sequence
  509. @code{'\377'}, @code{'\0'}, @code{'\0'}.
  510. @end deftypevr
  511. @deftypevr Macro tcflag_t IGNCR
  512. @standards{POSIX.1, termios.h}
  513. If this bit is set, carriage return characters (@code{'\r'}) are
  514. discarded on input. Discarding carriage return may be useful on
  515. terminals that send both carriage return and linefeed when you type the
  516. @key{RET} key.
  517. @end deftypevr
  518. @deftypevr Macro tcflag_t ICRNL
  519. @standards{POSIX.1, termios.h}
  520. If this bit is set and @code{IGNCR} is not set, carriage return characters
  521. (@code{'\r'}) received as input are passed to the application as newline
  522. characters (@code{'\n'}).
  523. @end deftypevr
  524. @deftypevr Macro tcflag_t INLCR
  525. @standards{POSIX.1, termios.h}
  526. If this bit is set, newline characters (@code{'\n'}) received as input
  527. are passed to the application as carriage return characters (@code{'\r'}).
  528. @end deftypevr
  529. @deftypevr Macro tcflag_t IXOFF
  530. @standards{POSIX.1, termios.h}
  531. If this bit is set, start/stop control on input is enabled. In other
  532. words, the computer sends STOP and START characters as necessary to
  533. prevent input from coming in faster than programs are reading it. The
  534. idea is that the actual terminal hardware that is generating the input
  535. data responds to a STOP character by suspending transmission, and to a
  536. START character by resuming transmission. @xref{Start/Stop Characters}.
  537. @end deftypevr
  538. @deftypevr Macro tcflag_t IXON
  539. @standards{POSIX.1, termios.h}
  540. If this bit is set, start/stop control on output is enabled. In other
  541. words, if the computer receives a STOP character, it suspends output
  542. until a START character is received. In this case, the STOP and START
  543. characters are never passed to the application program. If this bit is
  544. not set, then START and STOP can be read as ordinary characters.
  545. @xref{Start/Stop Characters}.
  546. @c !!! mention this interferes with using C-s and C-q for programs like emacs
  547. @end deftypevr
  548. @deftypevr Macro tcflag_t IXANY
  549. @standards{BSD, termios.h}
  550. If this bit is set, any input character restarts output when output has
  551. been suspended with the STOP character. Otherwise, only the START
  552. character restarts output.
  553. This is a BSD extension; it exists only on BSD systems and
  554. @gnulinuxhurdsystems{}.
  555. @end deftypevr
  556. @deftypevr Macro tcflag_t IMAXBEL
  557. @standards{BSD, termios.h}
  558. If this bit is set, then filling up the terminal input buffer sends a
  559. BEL character (code @code{007}) to the terminal to ring the bell.
  560. This is a BSD extension.
  561. @end deftypevr
  562. @node Output Modes
  563. @subsection Output Modes
  564. This section describes the terminal flags and fields that control how
  565. output characters are translated and padded for display. All of these
  566. are contained in the @code{c_oflag} member of the @w{@code{struct termios}}
  567. structure.
  568. The @code{c_oflag} member itself is an integer, and you change the flags
  569. and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
  570. try to specify the entire value for @code{c_oflag}---instead, change
  571. only specific flags and leave the rest untouched (@pxref{Setting
  572. Modes}).
  573. @deftypevr Macro tcflag_t OPOST
  574. @standards{POSIX.1, termios.h}
  575. If this bit is set, output data is processed in some unspecified way so
  576. that it is displayed appropriately on the terminal device. This
  577. typically includes mapping newline characters (@code{'\n'}) onto
  578. carriage return and linefeed pairs.
  579. If this bit isn't set, the characters are transmitted as-is.
  580. @end deftypevr
  581. The following three bits are effective only if @code{OPOST} is set.
  582. @deftypevr Macro tcflag_t ONLCR
  583. @standards{POSIX.1, termios.h}
  584. If this bit is set, convert the newline character on output into a pair
  585. of characters, carriage return followed by linefeed.
  586. @end deftypevr
  587. @deftypevr Macro tcflag_t OXTABS
  588. @standards{BSD, termios.h (optional)}
  589. If this bit is set, convert tab characters on output into the appropriate
  590. number of spaces to emulate a tab stop every eight columns. This bit
  591. exists only on BSD systems and @gnuhurdsystems{}; on
  592. @gnulinuxsystems{} it is available as @code{XTABS}.
  593. @end deftypevr
  594. @deftypevr Macro tcflag_t ONOEOT
  595. @standards{BSD, termios.h (optional)}
  596. If this bit is set, discard @kbd{C-d} characters (code @code{004}) on
  597. output. These characters cause many dial-up terminals to disconnect.
  598. This bit exists only on BSD systems and @gnuhurdsystems{}.
  599. @end deftypevr
  600. @node Control Modes
  601. @subsection Control Modes
  602. This section describes the terminal flags and fields that control
  603. parameters usually associated with asynchronous serial data
  604. transmission. These flags may not make sense for other kinds of
  605. terminal ports (such as a network connection pseudo-terminal). All of
  606. these are contained in the @code{c_cflag} member of the @code{struct
  607. termios} structure.
  608. The @code{c_cflag} member itself is an integer, and you change the flags
  609. and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
  610. try to specify the entire value for @code{c_cflag}---instead, change
  611. only specific flags and leave the rest untouched (@pxref{Setting
  612. Modes}).
  613. @deftypevr Macro tcflag_t CLOCAL
  614. @standards{POSIX.1, termios.h}
  615. If this bit is set, it indicates that the terminal is connected
  616. ``locally'' and that the modem status lines (such as carrier detect)
  617. should be ignored.
  618. @cindex modem status lines
  619. @cindex carrier detect
  620. On many systems if this bit is not set and you call @code{open} without
  621. the @code{O_NONBLOCK} flag set, @code{open} blocks until a modem
  622. connection is established.
  623. If this bit is not set and a modem disconnect is detected, a
  624. @code{SIGHUP} signal is sent to the controlling process group for the
  625. terminal (if it has one). Normally, this causes the process to exit;
  626. see @ref{Signal Handling}. Reading from the terminal after a disconnect
  627. causes an end-of-file condition, and writing causes an @code{EIO} error
  628. to be returned. The terminal device must be closed and reopened to
  629. clear the condition.
  630. @cindex modem disconnect
  631. @end deftypevr
  632. @deftypevr Macro tcflag_t HUPCL
  633. @standards{POSIX.1, termios.h}
  634. If this bit is set, a modem disconnect request is generated when all
  635. processes that have the terminal device open have either closed the file
  636. or exited.
  637. @cindex modem disconnect request
  638. @end deftypevr
  639. @deftypevr Macro tcflag_t CREAD
  640. @standards{POSIX.1, termios.h}
  641. If this bit is set, input can be read from the terminal. Otherwise,
  642. input is discarded when it arrives.
  643. @end deftypevr
  644. @deftypevr Macro tcflag_t CSTOPB
  645. @standards{POSIX.1, termios.h}
  646. If this bit is set, two stop bits are used. Otherwise, only one stop bit
  647. is used.
  648. @end deftypevr
  649. @deftypevr Macro tcflag_t PARENB
  650. @standards{POSIX.1, termios.h}
  651. If this bit is set, generation and detection of a parity bit are enabled.
  652. @xref{Input Modes}, for information on how input parity errors are handled.
  653. If this bit is not set, no parity bit is added to output characters, and
  654. input characters are not checked for correct parity.
  655. @end deftypevr
  656. @deftypevr Macro tcflag_t PARODD
  657. @standards{POSIX.1, termios.h}
  658. This bit is only useful if @code{PARENB} is set. If @code{PARODD} is set,
  659. odd parity is used, otherwise even parity is used.
  660. @end deftypevr
  661. The control mode flags also includes a field for the number of bits per
  662. character. You can use the @code{CSIZE} macro as a mask to extract the
  663. value, like this: @code{settings.c_cflag & CSIZE}.
  664. @deftypevr Macro tcflag_t CSIZE
  665. @standards{POSIX.1, termios.h}
  666. This is a mask for the number of bits per character.
  667. @end deftypevr
  668. @deftypevr Macro tcflag_t CS5
  669. @standards{POSIX.1, termios.h}
  670. This specifies five bits per byte.
  671. @end deftypevr
  672. @deftypevr Macro tcflag_t CS6
  673. @standards{POSIX.1, termios.h}
  674. This specifies six bits per byte.
  675. @end deftypevr
  676. @deftypevr Macro tcflag_t CS7
  677. @standards{POSIX.1, termios.h}
  678. This specifies seven bits per byte.
  679. @end deftypevr
  680. @deftypevr Macro tcflag_t CS8
  681. @standards{POSIX.1, termios.h}
  682. This specifies eight bits per byte.
  683. @end deftypevr
  684. The following four bits are BSD extensions; these exist only on BSD
  685. systems and @gnuhurdsystems{}.
  686. @deftypevr Macro tcflag_t CCTS_OFLOW
  687. @standards{BSD, termios.h}
  688. If this bit is set, enable flow control of output based on the CTS wire
  689. (RS232 protocol).
  690. @end deftypevr
  691. @deftypevr Macro tcflag_t CRTS_IFLOW
  692. @standards{BSD, termios.h}
  693. If this bit is set, enable flow control of input based on the RTS wire
  694. (RS232 protocol).
  695. @end deftypevr
  696. @deftypevr Macro tcflag_t MDMBUF
  697. @standards{BSD, termios.h}
  698. If this bit is set, enable carrier-based flow control of output.
  699. @end deftypevr
  700. @deftypevr Macro tcflag_t CIGNORE
  701. @standards{BSD, termios.h}
  702. If this bit is set, it says to ignore the control modes and line speed
  703. values entirely. This is only meaningful in a call to @code{tcsetattr}.
  704. The @code{c_cflag} member and the line speed values returned by
  705. @code{cfgetispeed}, @code{cfgetospeed}, @code{cfgetibaud} and
  706. @code{cfsetibaud} will be unaffected by the call. @code{CIGNORE} is
  707. useful if you want to set all the software modes in the other members,
  708. but leave the hardware details in @code{c_cflag} unchanged. (This is
  709. how the @code{TCSASOFT} flag to @code{tcsetattr} works.)
  710. This bit is never set in the structure filled in by @code{tcgetattr}.
  711. @end deftypevr
  712. @node Local Modes
  713. @subsection Local Modes
  714. This section describes the flags for the @code{c_lflag} member of the
  715. @code{struct termios} structure. These flags generally control
  716. higher-level aspects of input processing than the input modes flags
  717. described in @ref{Input Modes}, such as echoing, signals, and the choice
  718. of canonical or noncanonical input.
  719. The @code{c_lflag} member itself is an integer, and you change the flags
  720. and fields using the operators @code{&}, @code{|}, and @code{^}. Don't
  721. try to specify the entire value for @code{c_lflag}---instead, change
  722. only specific flags and leave the rest untouched (@pxref{Setting
  723. Modes}).
  724. @deftypevr Macro tcflag_t ICANON
  725. @standards{POSIX.1, termios.h}
  726. This bit, if set, enables canonical input processing mode. Otherwise,
  727. input is processed in noncanonical mode. @xref{Canonical or Not}.
  728. @end deftypevr
  729. @deftypevr Macro tcflag_t ECHO
  730. @standards{POSIX.1, termios.h}
  731. If this bit is set, echoing of input characters back to the terminal
  732. is enabled.
  733. @cindex echo of terminal input
  734. @end deftypevr
  735. @deftypevr Macro tcflag_t ECHOE
  736. @standards{POSIX.1, termios.h}
  737. If this bit is set, echoing indicates erasure of input with the ERASE
  738. character by erasing the last character in the current line from the
  739. screen. Otherwise, the character erased is re-echoed to show what has
  740. happened (suitable for a printing terminal).
  741. This bit only controls the display behavior; the @code{ICANON} bit by
  742. itself controls actual recognition of the ERASE character and erasure of
  743. input, without which @code{ECHOE} is simply irrelevant.
  744. @end deftypevr
  745. @deftypevr Macro tcflag_t ECHOPRT
  746. @standards{BSD, termios.h}
  747. This bit, like @code{ECHOE}, enables display of the ERASE character in
  748. a way that is geared to a hardcopy terminal. When you type the ERASE
  749. character, a @samp{\} character is printed followed by the first
  750. character erased. Typing the ERASE character again just prints the next
  751. character erased. Then, the next time you type a normal character, a
  752. @samp{/} character is printed before the character echoes.
  753. This is a BSD extension, and exists only in BSD systems and
  754. @gnulinuxhurdsystems{}.
  755. @end deftypevr
  756. @deftypevr Macro tcflag_t ECHOK
  757. @standards{POSIX.1, termios.h}
  758. This bit enables special display of the KILL character by moving to a
  759. new line after echoing the KILL character normally. The behavior of
  760. @code{ECHOKE} (below) is nicer to look at.
  761. If this bit is not set, the KILL character echoes just as it would if it
  762. were not the KILL character. Then it is up to the user to remember that
  763. the KILL character has erased the preceding input; there is no
  764. indication of this on the screen.
  765. This bit only controls the display behavior; the @code{ICANON} bit by
  766. itself controls actual recognition of the KILL character and erasure of
  767. input, without which @code{ECHOK} is simply irrelevant.
  768. @end deftypevr
  769. @deftypevr Macro tcflag_t ECHOKE
  770. @standards{BSD, termios.h}
  771. This bit is similar to @code{ECHOK}. It enables special display of the
  772. KILL character by erasing on the screen the entire line that has been
  773. killed. This is a BSD extension, and exists only in BSD systems and
  774. @gnulinuxhurdsystems{}.
  775. @end deftypevr
  776. @deftypevr Macro tcflag_t ECHONL
  777. @standards{POSIX.1, termios.h}
  778. If this bit is set and the @code{ICANON} bit is also set, then the
  779. newline (@code{'\n'}) character is echoed even if the @code{ECHO} bit
  780. is not set.
  781. @end deftypevr
  782. @deftypevr Macro tcflag_t ECHOCTL
  783. @standards{BSD, termios.h}
  784. If this bit is set and the @code{ECHO} bit is also set, echo control
  785. characters with @samp{^} followed by the corresponding text character.
  786. Thus, control-A echoes as @samp{^A}. This is usually the preferred mode
  787. for interactive input, because echoing a control character back to the
  788. terminal could have some undesired effect on the terminal.
  789. This is a BSD extension, and exists only in BSD systems and
  790. @gnulinuxhurdsystems{}.
  791. @end deftypevr
  792. @deftypevr Macro tcflag_t ISIG
  793. @standards{POSIX.1, termios.h}
  794. This bit controls whether the INTR, QUIT, and SUSP characters are
  795. recognized. The functions associated with these characters are performed
  796. if and only if this bit is set. Being in canonical or noncanonical
  797. input mode has no effect on the interpretation of these characters.
  798. You should use caution when disabling recognition of these characters.
  799. Programs that cannot be interrupted interactively are very
  800. user-unfriendly. If you clear this bit, your program should provide
  801. some alternate interface that allows the user to interactively send the
  802. signals associated with these characters, or to escape from the program.
  803. @cindex interactive signals, from terminal
  804. @xref{Signal Characters}.
  805. @end deftypevr
  806. @deftypevr Macro tcflag_t IEXTEN
  807. @standards{POSIX.1, termios.h}
  808. POSIX.1 gives @code{IEXTEN} implementation-defined meaning,
  809. so you cannot rely on this interpretation on all systems.
  810. On BSD systems and @gnulinuxhurdsystems{}, it enables the LNEXT and
  811. DISCARD characters.
  812. @xref{Other Special}.
  813. @end deftypevr
  814. @deftypevr Macro tcflag_t NOFLSH
  815. @standards{POSIX.1, termios.h}
  816. Normally, the INTR, QUIT, and SUSP characters cause input and output
  817. queues for the terminal to be cleared. If this bit is set, the queues
  818. are not cleared.
  819. @end deftypevr
  820. @deftypevr Macro tcflag_t TOSTOP
  821. @standards{POSIX.1, termios.h}
  822. If this bit is set and the system supports job control, then
  823. @code{SIGTTOU} signals are generated by background processes that
  824. attempt to write to the terminal. @xref{Access to the Terminal}.
  825. @end deftypevr
  826. The following bits are BSD extensions; they exist only on BSD systems
  827. and @gnuhurdsystems{}.
  828. @deftypevr Macro tcflag_t ALTWERASE
  829. @standards{BSD, termios.h}
  830. This bit determines how far the WERASE character should erase. The
  831. WERASE character erases back to the beginning of a word; the question
  832. is, where do words begin?
  833. If this bit is clear, then the beginning of a word is a nonwhitespace
  834. character following a whitespace character. If the bit is set, then the
  835. beginning of a word is an alphanumeric character or underscore following
  836. a character which is none of those.
  837. @xref{Editing Characters}, for more information about the WERASE character.
  838. @end deftypevr
  839. @deftypevr Macro tcflag_t FLUSHO
  840. @standards{BSD, termios.h}
  841. This is the bit that toggles when the user types the DISCARD character.
  842. While this bit is set, all output is discarded. @xref{Other Special}.
  843. @end deftypevr
  844. @deftypevr Macro tcflag_t NOKERNINFO
  845. @standards{BSD, termios.h (optional)}
  846. Setting this bit disables handling of the STATUS character.
  847. @xref{Other Special}.
  848. @end deftypevr
  849. @deftypevr Macro tcflag_t PENDIN
  850. @standards{BSD, termios.h}
  851. If this bit is set, it indicates that there is a line of input that
  852. needs to be reprinted. Typing the REPRINT character sets this bit; the
  853. bit remains set until reprinting is finished. @xref{Editing Characters}.
  854. @end deftypevr
  855. @c EXTPROC is too obscure to document now. --roland
  856. @node Line Speed
  857. @subsection Line Speed
  858. @cindex line speed
  859. @cindex baud rate
  860. @cindex terminal line speed
  861. @cindex terminal line speed
  862. The terminal line speed tells the computer how fast to read and write
  863. data on the terminal.
  864. For standard asynchronous serial lines employing binary NRZ encoding
  865. such as RS232, RS422, RS423, or RS485, the terminal speed will equal the
  866. physical layer baud rate including asynchronous framing and parity bits.
  867. This needs to match the communication speed expected by the peer device,
  868. or communication will not work. Which particular speeds are supported
  869. by any particular interface is hardware specific.
  870. For other types of devices the meaning of the line speed is
  871. device-specific and may not even affect the actual data transmission
  872. speed at all (for example, if it is a pseudo-terminal or network
  873. connection), but some programs will use it to determine the amount of
  874. padding needed. It's best to specify a line speed value that matches
  875. the actual speed of the actual terminal, but you can safely experiment
  876. with different values to vary the amount of padding.
  877. As the terminal interface models an RS232 serial interface
  878. (@pxref{Terminal Device Model}), the term ``baud rate'' is frequently
  879. used as a direct alias for ``line speed''; this convention is followed
  880. in the following descriptions.
  881. There are actually two line speeds for each terminal, one for input and
  882. one for output. You can set them independently, but most often
  883. terminals use the same speed for both directions. If the hardware does
  884. not support different speeds for each direction, the output speed will
  885. be used for both input and output.
  886. Specifying an output speed of zero generates a modem disconnect request.
  887. For the @code{speed_t} interface, this is the constant @code{B0} which
  888. may or may not have the numeric value @math{0}.
  889. Specifying an input speed value of zero sets the input speed to equal
  890. the output speed. This is the numeric constant @math{0} (not
  891. necessarily the same as @code{B0}) for both the @code{speed_t} and
  892. @code{baud_t} interfaces. This use is deprecated.
  893. The line speed values are stored in the @code{struct termios} structure, but
  894. don't try to access them in the @code{struct termios} structure
  895. directly. Instead, you should use the functions defined by the
  896. interfaces below to access them.
  897. The line speed setting functions report errors only when attempting to
  898. set line rate values that the system simply cannot handle. If you
  899. specify a line speed value that is plausible for the system, then the
  900. functions will succeed. However, they do not check that a particular
  901. hardware device can actually support the specified value---in fact, they
  902. don't know which device you plan to set the line speed for until
  903. @code{tcsetattr} is called. If you use @code{tcsetattr} to set the
  904. speed of a particular device to a value that it cannot handle, either
  905. @code{tcsetattr} returns @math{-1} and sets @code{errno} to
  906. @code{EINVAL}, or the value is adjusted to the closest supported value,
  907. depending on the policy of the kernel driver. In the latter case, a
  908. subsequent call to @code{tcgetattr} may or may not reflect this
  909. adjustment.
  910. @Theglibc{} supports two interoperable interfaces for setting the line
  911. speed: the POSIX.1 @code{speed_t} interface, which requires the use of a
  912. set of enumerated constants, and the @code{baud_t} interface, a GNU
  913. extension, which is guaranteed to use plain numeric values.
  914. @subsubsection The @code{speed_t} interface
  915. @deftp {Data Type} speed_t
  916. @standards{POSIX.1, termios.h}
  917. The @code{speed_t} type is an unsigned integer data type used to
  918. represent line speeds.
  919. @strong{Portability note:} In the current version of @theglibc{}, the
  920. @code{speed_t} type is numerically indentical to the line speed rate.
  921. Other libraries and older versions of @theglibc{} may require speeds to
  922. be indicated by enumerated constants, which may not be numerically
  923. identical to the requested line speed. For portability, you must use
  924. one of the following symbols to represent the speed; their precise
  925. numeric values are system-dependent, but each name has a fixed meaning:
  926. @code{B110} stands for 110 bps, @code{B300} for 300 bps, and so on.
  927. There is no portable way to represent any speed but these.
  928. @end deftp
  929. @comment termios.h
  930. @comment POSIX.1
  931. @vindex B0
  932. @comment termios.h
  933. @comment POSIX.1
  934. @vindex B50
  935. @comment termios.h
  936. @comment POSIX.1
  937. @vindex B75
  938. @comment termios.h
  939. @comment POSIX.1
  940. @vindex B110
  941. @comment termios.h
  942. @comment POSIX.1
  943. @vindex B134
  944. @comment termios.h
  945. @comment POSIX.1
  946. @vindex B150
  947. @comment termios.h
  948. @comment POSIX.1
  949. @vindex B200
  950. @comment termios.h
  951. @comment POSIX.1
  952. @vindex B300
  953. @comment termios.h
  954. @comment POSIX.1
  955. @vindex B600
  956. @comment termios.h
  957. @comment POSIX.1
  958. @vindex B1200
  959. @comment termios.h
  960. @comment POSIX.1
  961. @vindex B1800
  962. @comment termios.h
  963. @comment POSIX.1
  964. @vindex B2400
  965. @comment termios.h
  966. @comment POSIX.1
  967. @vindex B4800
  968. @comment termios.h
  969. @comment POSIX.1
  970. @vindex B9600
  971. @comment termios.h
  972. @comment POSIX.1
  973. @vindex B19200
  974. @comment termios.h
  975. @comment POSIX.1
  976. @vindex B38400
  977. @smallexample
  978. B0 B50 B75 B110 B134 B150 B200 B300 B600 B1200
  979. B1800 B2400 B4800 B9600 B19200 B38400
  980. @end smallexample
  981. @Theglibc{} defines these additional constants:
  982. @comment termios.h
  983. @comment GNU
  984. @vindex B7200
  985. @comment termios.h
  986. @comment GNU
  987. @vindex B14400
  988. @comment termios.h
  989. @comment GNU
  990. @vindex B28800
  991. @comment termios.h
  992. @comment GNU
  993. @vindex B33600
  994. @comment termios.h
  995. @comment GNU
  996. @vindex B57600
  997. @comment termios.h
  998. @comment GNU
  999. @vindex B76800
  1000. @comment termios.h
  1001. @comment GNU
  1002. @vindex B115200
  1003. @comment termios.h
  1004. @comment GNU
  1005. @vindex B153600
  1006. @comment termios.h
  1007. @comment GNU
  1008. @vindex B230400
  1009. @comment termios.h
  1010. @comment GNU
  1011. @vindex B307200
  1012. @comment termios.h
  1013. @comment GNU
  1014. @vindex B460800
  1015. @comment termios.h
  1016. @comment GNU
  1017. @vindex B500000
  1018. @comment termios.h
  1019. @comment GNU
  1020. @vindex B576000
  1021. @comment termios.h
  1022. @comment GNU
  1023. @vindex B614400
  1024. @comment termios.h
  1025. @comment GNU
  1026. @vindex B921600
  1027. @comment termios.h
  1028. @comment GNU
  1029. @vindex B1000000
  1030. @comment termios.h
  1031. @comment GNU
  1032. @vindex B1152000
  1033. @comment termios.h
  1034. @comment GNU
  1035. @vindex B1500000
  1036. @comment termios.h
  1037. @comment GNU
  1038. @vindex B2000000
  1039. @comment termios.h
  1040. @comment GNU
  1041. @vindex B2500000
  1042. @comment termios.h
  1043. @comment GNU
  1044. @vindex B3000000
  1045. @comment termios.h
  1046. @comment GNU
  1047. @vindex B3500000
  1048. @comment termios.h
  1049. @comment GNU
  1050. @vindex B4000000
  1051. @comment termios.h
  1052. @comment GNU
  1053. @vindex B5000000
  1054. @comment termios.h
  1055. @comment GNU
  1056. @vindex B10000000
  1057. @smallexample
  1058. B7200 B14400 B28800 B33600 B57600 B76800 B115200 B153600 B230400 B307200
  1059. B460800 B500000 B576000 B614400 B921600 B1000000 B1152000 B1500000
  1060. B2000000 B2500000 B3000000 B3500000 B4000000 B5000000 B10000000
  1061. @end smallexample
  1062. @vindex EXTA
  1063. @vindex EXTB
  1064. BSD defines two additional speed symbols as aliases: @code{EXTA} is an
  1065. alias for @code{B19200} and @code{EXTB} is an alias for @code{B38400}.
  1066. These aliases are obsolete.
  1067. @deftypevr Macro speed_t SPEED_MAX
  1068. @standards{GNU, termios.h}
  1069. @Theglibc{} defines the constant @code{SPEED_MAX} for the largest valid
  1070. value of type @code{speed_t}. This value may be smaller than the
  1071. underlying C type can store.
  1072. For compatiblity with some other platforms the alias @code{__MAX_BAUD}
  1073. is defined for this constant.
  1074. @end deftypevr
  1075. @deftypefun speed_t cfgetospeed (const struct termios *@var{termios-p})
  1076. @standards{POSIX.1, termios.h}
  1077. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1078. @c Direct access to a single termios field. However, this may not be
  1079. @c true on other implementations; callers must ensure mutual exclusion
  1080. @c on such non-opaque types.
  1081. This function returns the output line speed stored in the structure
  1082. @code{*@var{termios-p}}.
  1083. @end deftypefun
  1084. @deftypefun speed_t cfgetispeed (const struct termios *@var{termios-p})
  1085. @standards{POSIX.1, termios.h}
  1086. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1087. This function returns the input line speed stored in the structure
  1088. @code{*@var{termios-p}}.
  1089. @end deftypefun
  1090. @deftypefun int cfsetospeed (struct termios *@var{termios-p}, speed_t @var{speed})
  1091. @standards{POSIX.1, termios.h}
  1092. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1093. @c Direct access to a single termios field, except on Linux, where two
  1094. @c fields are accessed, one of which (c_cflag) is read-modify-write. No
  1095. @c worries either way, callers must ensure mutual exclusion on such
  1096. @c non-opaque types.
  1097. This function stores @var{speed} in @code{*@var{termios-p}} as the
  1098. output line speed. If @var{speed} is @code{B0}, generates a modem
  1099. disconnect request.
  1100. If @var{speed} is neither a plausible line speed nor @code{B0},
  1101. @code{cfsetospeed} returns @math{-1} and sets @code{errno} to
  1102. @code{EINVAL}.
  1103. @end deftypefun
  1104. @deftypefun int cfsetispeed (struct termios *@var{termios-p}, speed_t @var{speed})
  1105. @standards{POSIX.1, termios.h}
  1106. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1107. This function stores @var{speed} in @code{*@var{termios-p}} as the input
  1108. speed. If @var{speed} is @math{0}, the input speed is set to equal the
  1109. output speed; note that POSIX.1 specifies this as the numeric value
  1110. @math{0} which is not required to equal the constant @code{B0}.
  1111. If @var{speed} is not a plausible line speed or @math{0},
  1112. @code{cfsetispeed} returns @math{-1} and sets @code{errno} to
  1113. @code{EINVAL}.
  1114. @strong{Portability note:} POSIX.1-2024 has deprecated setting of the
  1115. input speed to @math{0} to set the input line speed to equal the output
  1116. line speed. After calling @code{tcsetattr} followed by
  1117. @code{tcgetattr}, @code{cfgetispeed} may report the input line speed
  1118. either as @math{0} or the same as @code{cfgetospeed}.
  1119. @end deftypefun
  1120. @deftypefun int cfsetspeed (struct termios *@var{termios-p}, speed_t @var{speed})
  1121. @standards{BSD, termios.h}
  1122. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1123. @c There's no guarantee that the two calls are atomic, but since this is
  1124. @c not an opaque type, callers ought to ensure mutual exclusion to the
  1125. @c termios object.
  1126. @c cfsetspeed ok
  1127. @c cfsetispeed ok
  1128. @c cfsetospeed ok
  1129. This function stores @var{speed} in @code{*@var{termios-p}} as both the
  1130. input and output speeds.
  1131. If @var{baud} is not a plausible line speed, @code{cfsetbaud} returns
  1132. @math{-1} and sets @code{errno} to @code{EINVAL}.
  1133. This function is an extension which originated in 4.4 BSD.
  1134. @end deftypefun
  1135. @subsubsection The @code{baud_t} interface
  1136. @deftp {Data Type} baud_t
  1137. @standards{GNU, termios.h}
  1138. The @code{baud_t} type is a numeric data type used to represent line
  1139. baud rates. It will always represent the actual numeric value
  1140. corresponding to the line speed, unlike @code{speed_t}. In the current
  1141. version of @theglibc{} this is the same type as @code{speed_t}, but this
  1142. may not be the case in future versions or on other implementations; it
  1143. is specifically not guaranteed to be an integer type.
  1144. @end deftp
  1145. @deftypevr Macro baud_t BAUD_MAX
  1146. @standards{GNU, termios.h}
  1147. The constant @code{BAUD_MAX} is defined to the maximum valid value of
  1148. type @code{baud_t}. This value may be smaller than the underlying C
  1149. type can store.
  1150. @end deftypevr
  1151. @deftypefun baud_t cfgetobaud (const struct termios *@var{termios-p})
  1152. @standards{GNU, termios.h}
  1153. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1154. @c Direct access to a single termios field. However, this may not be
  1155. @c true on other implementations; callers must ensure mutual exclusion
  1156. @c on such non-opaque types.
  1157. This function returns the output line speed stored in the structure
  1158. @code{*@var{termios-p}} as a numeric value.
  1159. @end deftypefun
  1160. @deftypefun baud_t cfgetibaud (const struct termios *@var{termios-p})
  1161. @standards{GNU, termios.h}
  1162. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1163. This function returns the input line speed stored in the structure
  1164. @code{*@var{termios-p}} as a numeric value.
  1165. @end deftypefun
  1166. @deftypefun int cfsetobaud (struct termios *@var{termios-p}, baud_t @var{baud})
  1167. @standards{GNU, termios.h}
  1168. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1169. @c Direct access to a single termios field, except on Linux, where two
  1170. @c fields are accessed, one of which (c_cflag) is read-modify-write. No
  1171. @c worries either way, callers must ensure mutual exclusion on such
  1172. @c non-opaque types.
  1173. This function stores @var{baud} in @code{*@var{termios-p}} as the output
  1174. line speed. If @var{baud} is @math{0}, generates a modem disconnect.
  1175. If @var{speed} is not a plausible line speed, @code{cfsetspeed} returns
  1176. @math{-1} and sets @code{errno} to @code{EINVAL}.
  1177. @end deftypefun
  1178. @deftypefun int cfsetibaud (struct termios *@var{termios-p}, baud_t @var{baud})
  1179. @standards{GNU, termios.h}
  1180. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1181. This function stores @var{baud} in @code{*@var{termios-p}} as the input
  1182. line speed.
  1183. To simplify conversions from the @code{speed_t} interface, setting the
  1184. input line speed to @math{0} is interpreted as setting the input line
  1185. speed equal to the output line speed. The caveats described under
  1186. @code{cfsetispeed} apply equally to @code{cfsetibaud}. As for
  1187. @code{cfsetispeed}, this usage is deprecated.
  1188. If @var{baud} is not a plausible line speed or @math{0},
  1189. @code{cfsetibaud} returns @math{-1} and sets @code{errno} to
  1190. @code{EINVAL}.
  1191. @end deftypefun
  1192. @deftypefun int cfsetbaud (struct termios *@var{termios-p}, baud_t @var{baud})
  1193. @standards{GNU, termios.h}
  1194. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1195. @c There's no guarantee that the two calls are atomic, but since this is
  1196. @c not an opaque type, callers should ensure mutual exclusion to the
  1197. @c termios object.
  1198. @c cfsetbaud ok
  1199. @c cfsetibaud ok
  1200. @c cfsetobaud ok
  1201. This function stores @var{baud} in @code{*@var{termios-p}} as both the
  1202. input and output line speeds.
  1203. If @var{baud} is not a plausible line speed, @code{cfsetbaud} returns
  1204. @math{-1} and sets @code{errno} to @code{EINVAL}.
  1205. @end deftypefun
  1206. @node Special Characters
  1207. @subsection Special Characters
  1208. In canonical input, the terminal driver recognizes a number of special
  1209. characters which perform various control functions. These include the
  1210. ERASE character (usually @key{DEL}) for editing input, and other editing
  1211. characters. The INTR character (normally @kbd{C-c}) for sending a
  1212. @code{SIGINT} signal, and other signal-raising characters, may be
  1213. available in either canonical or noncanonical input mode. All these
  1214. characters are described in this section.
  1215. The particular characters used are specified in the @code{c_cc} member
  1216. of the @code{struct termios} structure. This member is an array; each
  1217. element specifies the character for a particular role. Each element has
  1218. a symbolic constant that stands for the index of that element---for
  1219. example, @code{VINTR} is the index of the element that specifies the INTR
  1220. character, so storing @code{'='} in @code{@var{termios}.c_cc[VINTR]}
  1221. specifies @samp{=} as the INTR character.
  1222. @vindex _POSIX_VDISABLE
  1223. On some systems, you can disable a particular special character function
  1224. by specifying the value @code{_POSIX_VDISABLE} for that role. This
  1225. value is unequal to any possible character code. @xref{Options for
  1226. Files}, for more information about how to tell whether the operating
  1227. system you are using supports @code{_POSIX_VDISABLE}.
  1228. @menu
  1229. * Editing Characters:: Special characters that terminate lines and
  1230. delete text, and other editing functions.
  1231. * Signal Characters:: Special characters that send or raise signals
  1232. to or for certain classes of processes.
  1233. * Start/Stop Characters:: Special characters that suspend or resume
  1234. suspended output.
  1235. * Other Special:: Other special characters for BSD systems:
  1236. they can discard output, and print status.
  1237. @end menu
  1238. @node Editing Characters
  1239. @subsubsection Characters for Input Editing
  1240. These special characters are active only in canonical input mode.
  1241. @xref{Canonical or Not}.
  1242. @deftypevr Macro int VEOF
  1243. @standards{POSIX.1, termios.h}
  1244. @cindex EOF character
  1245. This is the subscript for the EOF character in the special control
  1246. character array. @code{@var{termios}.c_cc[VEOF]} holds the character
  1247. itself.
  1248. The EOF character is recognized only in canonical input mode. It acts
  1249. as a line terminator in the same way as a newline character, but if the
  1250. EOF character is typed at the beginning of a line it causes @code{read}
  1251. to return a byte count of zero, indicating end-of-file. The EOF
  1252. character itself is discarded.
  1253. Usually, the EOF character is @kbd{C-d}.
  1254. @end deftypevr
  1255. @deftypevr Macro int VEOL
  1256. @standards{POSIX.1, termios.h}
  1257. @cindex EOL character
  1258. This is the subscript for the EOL character in the special control
  1259. character array. @code{@var{termios}.c_cc[VEOL]} holds the character
  1260. itself.
  1261. The EOL character is recognized only in canonical input mode. It acts
  1262. as a line terminator, just like a newline character. The EOL character
  1263. is not discarded; it is read as the last character in the input line.
  1264. @c !!! example: this is set to ESC by 4.3 csh with "set filec" so it can
  1265. @c complete partial lines without using cbreak or raw mode.
  1266. You don't need to use the EOL character to make @key{RET} end a line.
  1267. Just set the ICRNL flag. In fact, this is the default state of
  1268. affairs.
  1269. @end deftypevr
  1270. @deftypevr Macro int VEOL2
  1271. @standards{BSD, termios.h}
  1272. @cindex EOL2 character
  1273. This is the subscript for the EOL2 character in the special control
  1274. character array. @code{@var{termios}.c_cc[VEOL2]} holds the character
  1275. itself.
  1276. The EOL2 character works just like the EOL character (see above), but it
  1277. can be a different character. Thus, you can specify two characters to
  1278. terminate an input line, by setting EOL to one of them and EOL2 to the
  1279. other.
  1280. The EOL2 character is a BSD extension; it exists only on BSD systems
  1281. and @gnulinuxhurdsystems{}.
  1282. @end deftypevr
  1283. @deftypevr Macro int VERASE
  1284. @standards{POSIX.1, termios.h}
  1285. @cindex ERASE character
  1286. This is the subscript for the ERASE character in the special control
  1287. character array. @code{@var{termios}.c_cc[VERASE]} holds the
  1288. character itself.
  1289. The ERASE character is recognized only in canonical input mode. When
  1290. the user types the erase character, the previous character typed is
  1291. discarded. (If the terminal generates multibyte character sequences,
  1292. this may cause more than one byte of input to be discarded.) This
  1293. cannot be used to erase past the beginning of the current line of text.
  1294. The ERASE character itself is discarded.
  1295. @c !!! mention ECHOE here
  1296. Usually, the ERASE character is @key{DEL}.
  1297. @end deftypevr
  1298. @deftypevr Macro int VWERASE
  1299. @standards{BSD, termios.h}
  1300. @cindex WERASE character
  1301. This is the subscript for the WERASE character in the special control
  1302. character array. @code{@var{termios}.c_cc[VWERASE]} holds the character
  1303. itself.
  1304. The WERASE character is recognized only in canonical mode. It erases an
  1305. entire word of prior input, and any whitespace after it; whitespace
  1306. characters before the word are not erased.
  1307. The definition of a ``word'' depends on the setting of the
  1308. @code{ALTWERASE} mode; @pxref{Local Modes}.
  1309. If the @code{ALTWERASE} mode is not set, a word is defined as a sequence
  1310. of any characters except space or tab.
  1311. If the @code{ALTWERASE} mode is set, a word is defined as a sequence of
  1312. characters containing only letters, numbers, and underscores, optionally
  1313. followed by one character that is not a letter, number, or underscore.
  1314. The WERASE character is usually @kbd{C-w}.
  1315. This is a BSD extension.
  1316. @end deftypevr
  1317. @deftypevr Macro int VKILL
  1318. @standards{POSIX.1, termios.h}
  1319. @cindex KILL character
  1320. This is the subscript for the KILL character in the special control
  1321. character array. @code{@var{termios}.c_cc[VKILL]} holds the character
  1322. itself.
  1323. The KILL character is recognized only in canonical input mode. When the
  1324. user types the kill character, the entire contents of the current line
  1325. of input are discarded. The kill character itself is discarded too.
  1326. The KILL character is usually @kbd{C-u}.
  1327. @end deftypevr
  1328. @deftypevr Macro int VREPRINT
  1329. @standards{BSD, termios.h}
  1330. @cindex REPRINT character
  1331. This is the subscript for the REPRINT character in the special control
  1332. character array. @code{@var{termios}.c_cc[VREPRINT]} holds the character
  1333. itself.
  1334. The REPRINT character is recognized only in canonical mode. It reprints
  1335. the current input line. If some asynchronous output has come while you
  1336. are typing, this lets you see the line you are typing clearly again.
  1337. The REPRINT character is usually @kbd{C-r}.
  1338. This is a BSD extension.
  1339. @end deftypevr
  1340. @node Signal Characters
  1341. @subsubsection Characters that Cause Signals
  1342. These special characters may be active in either canonical or noncanonical
  1343. input mode, but only when the @code{ISIG} flag is set (@pxref{Local
  1344. Modes}).
  1345. @deftypevr Macro int VINTR
  1346. @standards{POSIX.1, termios.h}
  1347. @cindex INTR character
  1348. @cindex interrupt character
  1349. This is the subscript for the INTR character in the special control
  1350. character array. @code{@var{termios}.c_cc[VINTR]} holds the character
  1351. itself.
  1352. The INTR (interrupt) character raises a @code{SIGINT} signal for all
  1353. processes in the foreground job associated with the terminal. The INTR
  1354. character itself is then discarded. @xref{Signal Handling}, for more
  1355. information about signals.
  1356. Typically, the INTR character is @kbd{C-c}.
  1357. @end deftypevr
  1358. @deftypevr Macro int VQUIT
  1359. @standards{POSIX.1, termios.h}
  1360. @cindex QUIT character
  1361. This is the subscript for the QUIT character in the special control
  1362. character array. @code{@var{termios}.c_cc[VQUIT]} holds the character
  1363. itself.
  1364. The QUIT character raises a @code{SIGQUIT} signal for all processes in
  1365. the foreground job associated with the terminal. The QUIT character
  1366. itself is then discarded. @xref{Signal Handling}, for more information
  1367. about signals.
  1368. Typically, the QUIT character is @kbd{C-\}.
  1369. @end deftypevr
  1370. @deftypevr Macro int VSUSP
  1371. @standards{POSIX.1, termios.h}
  1372. @cindex SUSP character
  1373. @cindex suspend character
  1374. This is the subscript for the SUSP character in the special control
  1375. character array. @code{@var{termios}.c_cc[VSUSP]} holds the character
  1376. itself.
  1377. The SUSP (suspend) character is recognized only if the implementation
  1378. supports job control (@pxref{Job Control}). It causes a @code{SIGTSTP}
  1379. signal to be sent to all processes in the foreground job associated with
  1380. the terminal. The SUSP character itself is then discarded.
  1381. @xref{Signal Handling}, for more information about signals.
  1382. Typically, the SUSP character is @kbd{C-z}.
  1383. @end deftypevr
  1384. Few applications disable the normal interpretation of the SUSP
  1385. character. If your program does this, it should provide some other
  1386. mechanism for the user to stop the job. When the user invokes this
  1387. mechanism, the program should send a @code{SIGTSTP} signal to the
  1388. process group of the process, not just to the process itself.
  1389. @xref{Signaling Another Process}.
  1390. @deftypevr Macro int VDSUSP
  1391. @standards{BSD, termios.h}
  1392. @cindex DSUSP character
  1393. @cindex delayed suspend character
  1394. This is the subscript for the DSUSP character in the special control
  1395. character array. @code{@var{termios}.c_cc[VDSUSP]} holds the character
  1396. itself.
  1397. The DSUSP (suspend) character is recognized only if the implementation
  1398. supports job control (@pxref{Job Control}). It sends a @code{SIGTSTP}
  1399. signal, like the SUSP character, but not right away---only when the
  1400. program tries to read it as input. Not all systems with job control
  1401. support DSUSP; only BSD-compatible systems do (including @gnuhurdsystems{}).
  1402. @xref{Signal Handling}, for more information about signals.
  1403. Typically, the DSUSP character is @kbd{C-y}.
  1404. @end deftypevr
  1405. @node Start/Stop Characters
  1406. @subsubsection Special Characters for Flow Control
  1407. These special characters may be active in either canonical or noncanonical
  1408. input mode, but their use is controlled by the flags @code{IXON} and
  1409. @code{IXOFF} (@pxref{Input Modes}).
  1410. @deftypevr Macro int VSTART
  1411. @standards{POSIX.1, termios.h}
  1412. @cindex START character
  1413. This is the subscript for the START character in the special control
  1414. character array. @code{@var{termios}.c_cc[VSTART]} holds the
  1415. character itself.
  1416. The START character is used to support the @code{IXON} and @code{IXOFF}
  1417. input modes. If @code{IXON} is set, receiving a START character resumes
  1418. suspended output; the START character itself is discarded. If
  1419. @code{IXANY} is set, receiving any character at all resumes suspended
  1420. output; the resuming character is not discarded unless it is the START
  1421. character. If @code{IXOFF} is set, the system may also transmit START
  1422. characters to the terminal.
  1423. The usual value for the START character is @kbd{C-q}. You may not be
  1424. able to change this value---the hardware may insist on using @kbd{C-q}
  1425. regardless of what you specify.
  1426. @end deftypevr
  1427. @deftypevr Macro int VSTOP
  1428. @standards{POSIX.1, termios.h}
  1429. @cindex STOP character
  1430. This is the subscript for the STOP character in the special control
  1431. character array. @code{@var{termios}.c_cc[VSTOP]} holds the character
  1432. itself.
  1433. The STOP character is used to support the @code{IXON} and @code{IXOFF}
  1434. input modes. If @code{IXON} is set, receiving a STOP character causes
  1435. output to be suspended; the STOP character itself is discarded. If
  1436. @code{IXOFF} is set, the system may also transmit STOP characters to the
  1437. terminal, to prevent the input queue from overflowing.
  1438. The usual value for the STOP character is @kbd{C-s}. You may not be
  1439. able to change this value---the hardware may insist on using @kbd{C-s}
  1440. regardless of what you specify.
  1441. @end deftypevr
  1442. @node Other Special
  1443. @subsubsection Other Special Characters
  1444. @deftypevr Macro int VLNEXT
  1445. @standards{BSD, termios.h}
  1446. @cindex LNEXT character
  1447. This is the subscript for the LNEXT character in the special control
  1448. character array. @code{@var{termios}.c_cc[VLNEXT]} holds the character
  1449. itself.
  1450. The LNEXT character is recognized only when @code{IEXTEN} is set, but in
  1451. both canonical and noncanonical mode. It disables any special
  1452. significance of the next character the user types. Even if the
  1453. character would normally perform some editing function or generate a
  1454. signal, it is read as a plain character. This is the analogue of the
  1455. @kbd{C-q} command in Emacs. ``LNEXT'' stands for ``literal next.''
  1456. The LNEXT character is usually @kbd{C-v}.
  1457. This character is available on BSD systems and @gnulinuxhurdsystems{}.
  1458. @end deftypevr
  1459. @deftypevr Macro int VDISCARD
  1460. @standards{BSD, termios.h}
  1461. @cindex DISCARD character
  1462. This is the subscript for the DISCARD character in the special control
  1463. character array. @code{@var{termios}.c_cc[VDISCARD]} holds the character
  1464. itself.
  1465. The DISCARD character is recognized only when @code{IEXTEN} is set, but
  1466. in both canonical and noncanonical mode. Its effect is to toggle the
  1467. discard-output flag. When this flag is set, all program output is
  1468. discarded. Setting the flag also discards all output currently in the
  1469. output buffer. Typing any other character resets the flag.
  1470. This character is available on BSD systems and @gnulinuxhurdsystems{}.
  1471. @end deftypevr
  1472. @deftypevr Macro int VSTATUS
  1473. @standards{BSD, termios.h}
  1474. @cindex STATUS character
  1475. This is the subscript for the STATUS character in the special control
  1476. character array. @code{@var{termios}.c_cc[VSTATUS]} holds the character
  1477. itself.
  1478. The STATUS character's effect is to print out a status message about how
  1479. the current process is running.
  1480. The STATUS character is recognized only in canonical mode, and only if
  1481. @code{NOKERNINFO} is not set.
  1482. This character is available only on BSD systems and @gnuhurdsystems{}.
  1483. @end deftypevr
  1484. @node Noncanonical Input
  1485. @subsection Noncanonical Input
  1486. In noncanonical input mode, the special editing characters such as
  1487. ERASE and KILL are ignored. The system facilities for the user to edit
  1488. input are disabled in noncanonical mode, so that all input characters
  1489. (unless they are special for signal or flow-control purposes) are passed
  1490. to the application program exactly as typed. It is up to the
  1491. application program to give the user ways to edit the input, if
  1492. appropriate.
  1493. Noncanonical mode offers special parameters called MIN and TIME for
  1494. controlling whether and how long to wait for input to be available. You
  1495. can even use them to avoid ever waiting---to return immediately with
  1496. whatever input is available, or with no input.
  1497. The MIN and TIME are stored in elements of the @code{c_cc} array, which
  1498. is a member of the @w{@code{struct termios}} structure. Each element of
  1499. this array has a particular role, and each element has a symbolic
  1500. constant that stands for the index of that element. @code{VMIN} and
  1501. @code{VTIME} are the names for the indices in the array of the MIN and
  1502. TIME slots.
  1503. @deftypevr Macro int VMIN
  1504. @standards{POSIX.1, termios.h}
  1505. @cindex MIN termios slot
  1506. This is the subscript for the MIN slot in the @code{c_cc} array. Thus,
  1507. @code{@var{termios}.c_cc[VMIN]} is the value itself.
  1508. The MIN slot is only meaningful in noncanonical input mode; it
  1509. specifies the minimum number of bytes that must be available in the
  1510. input queue in order for @code{read} to return.
  1511. @end deftypevr
  1512. @deftypevr Macro int VTIME
  1513. @standards{POSIX.1, termios.h}
  1514. @cindex TIME termios slot
  1515. This is the subscript for the TIME slot in the @code{c_cc} array. Thus,
  1516. @code{@var{termios}.c_cc[VTIME]} is the value itself.
  1517. The TIME slot is only meaningful in noncanonical input mode; it
  1518. specifies how long to wait for input before returning, in units of 0.1
  1519. seconds.
  1520. @end deftypevr
  1521. The MIN and TIME values interact to determine the criterion for when
  1522. @code{read} should return; their precise meanings depend on which of
  1523. them are nonzero. There are four possible cases:
  1524. @itemize @bullet
  1525. @item
  1526. Both TIME and MIN are nonzero.
  1527. In this case, TIME specifies how long to wait after each input character
  1528. to see if more input arrives. After the first character received,
  1529. @code{read} keeps waiting until either MIN bytes have arrived in all, or
  1530. TIME elapses with no further input.
  1531. @code{read} always blocks until the first character arrives, even if
  1532. TIME elapses first. @code{read} can return more than MIN characters if
  1533. more than MIN happen to be in the queue.
  1534. @item
  1535. Both MIN and TIME are zero.
  1536. In this case, @code{read} always returns immediately with as many
  1537. characters as are available in the queue, up to the number requested.
  1538. If no input is immediately available, @code{read} returns a value of
  1539. zero.
  1540. @item
  1541. MIN is zero but TIME has a nonzero value.
  1542. In this case, @code{read} waits for time TIME for input to become
  1543. available; the availability of a single byte is enough to satisfy the
  1544. read request and cause @code{read} to return. When it returns, it
  1545. returns as many characters as are available, up to the number requested.
  1546. If no input is available before the timer expires, @code{read} returns a
  1547. value of zero.
  1548. @item
  1549. TIME is zero but MIN has a nonzero value.
  1550. In this case, @code{read} waits until at least MIN bytes are available
  1551. in the queue. At that time, @code{read} returns as many characters as
  1552. are available, up to the number requested. @code{read} can return more
  1553. than MIN characters if more than MIN happen to be in the queue.
  1554. @end itemize
  1555. What happens if MIN is 50 and you ask to read just 10 bytes?
  1556. Normally, @code{read} waits until there are 50 bytes in the buffer (or,
  1557. more generally, the wait condition described above is satisfied), and
  1558. then reads 10 of them, leaving the other 40 buffered in the operating
  1559. system for a subsequent call to @code{read}.
  1560. @strong{Portability note:} On some systems, the MIN and TIME slots are
  1561. actually the same as the EOF and EOL slots. This causes no serious
  1562. problem because the MIN and TIME slots are used only in noncanonical
  1563. input and the EOF and EOL slots are used only in canonical input, but it
  1564. isn't very clean. @Theglibc{} allocates separate slots for these
  1565. uses.
  1566. @deftypefun void cfmakeraw (struct termios *@var{termios-p})
  1567. @standards{BSD, termios.h}
  1568. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1569. @c There's no guarantee the changes are atomic, but since this is not an
  1570. @c opaque type, callers ought to ensure mutual exclusion to the termios
  1571. @c object.
  1572. This function provides an easy way to set up @code{*@var{termios-p}} for
  1573. what has traditionally been called ``raw mode'' in BSD. This uses
  1574. noncanonical input, and turns off most processing to give an unmodified
  1575. channel to the terminal.
  1576. It does exactly this:
  1577. @smallexample
  1578. @var{termios-p}->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
  1579. |INLCR|IGNCR|ICRNL|IXON);
  1580. @var{termios-p}->c_oflag &= ~OPOST;
  1581. @var{termios-p}->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  1582. @var{termios-p}->c_cflag &= ~(CSIZE|PARENB);
  1583. @var{termios-p}->c_cflag |= CS8;
  1584. @end smallexample
  1585. @end deftypefun
  1586. @node BSD Terminal Modes
  1587. @section BSD Terminal Modes
  1588. @cindex terminal modes, BSD
  1589. The usual way to get and set terminal modes is with the functions described
  1590. in @ref{Terminal Modes}. However, on some systems you can use the
  1591. BSD-derived functions in this section to do some of the same things. On
  1592. many systems, these functions do not exist. Even with @theglibc{},
  1593. the functions simply fail with @code{errno} = @code{ENOSYS} with many
  1594. kernels, including Linux.
  1595. The symbols used in this section are declared in @file{sgtty.h}.
  1596. @deftp {Data Type} {struct sgttyb}
  1597. @standards{BSD, termios.h}
  1598. This structure is an input or output parameter list for @code{gtty} and
  1599. @code{stty}.
  1600. @table @code
  1601. @item char sg_ispeed
  1602. Line speed for input
  1603. @item char sg_ospeed
  1604. Line speed for output
  1605. @item char sg_erase
  1606. Erase character
  1607. @item char sg_kill
  1608. Kill character
  1609. @item int sg_flags
  1610. Various flags
  1611. @end table
  1612. @end deftp
  1613. @deftypefun int gtty (int @var{filedes}, struct sgttyb *@var{attributes})
  1614. @standards{BSD, sgtty.h}
  1615. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1616. @c Direct ioctl, BSD only.
  1617. This function gets the attributes of a terminal.
  1618. @code{gtty} sets *@var{attributes} to describe the terminal attributes
  1619. of the terminal which is open with file descriptor @var{filedes}.
  1620. @end deftypefun
  1621. @deftypefun int stty (int @var{filedes}, const struct sgttyb *@var{attributes})
  1622. @standards{BSD, sgtty.h}
  1623. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1624. @c Direct ioctl, BSD only.
  1625. This function sets the attributes of a terminal.
  1626. @code{stty} sets the terminal attributes of the terminal which is open with
  1627. file descriptor @var{filedes} to those described by *@var{attributes}.
  1628. @end deftypefun
  1629. @node Line Control
  1630. @section Line Control Functions
  1631. @cindex terminal line control functions
  1632. These functions perform miscellaneous control actions on terminal
  1633. devices. As regards terminal access, they are treated like doing
  1634. output: if any of these functions is used by a background process on its
  1635. controlling terminal, normally all processes in the process group are
  1636. sent a @code{SIGTTOU} signal. The exception is if the calling process
  1637. itself is ignoring or blocking @code{SIGTTOU} signals, in which case the
  1638. operation is performed and no signal is sent. @xref{Job Control}.
  1639. @cindex break condition, generating
  1640. @deftypefun int tcsendbreak (int @var{filedes}, int @var{duration})
  1641. @standards{POSIX.1, termios.h}
  1642. @safety{@prelim{}@mtunsafe{@mtasurace{:tcattr(filedes)/bsd}}@asunsafe{}@acunsafe{@acucorrupt{/bsd}}}
  1643. @c On Linux, this calls just one out of two ioctls; on BSD, it's two
  1644. @c ioctls with a select (for the delay only) in between, the first
  1645. @c setting and the latter clearing the break status. The BSD
  1646. @c implementation may leave the break enabled if cancelled, and threads
  1647. @c and signals may cause the break to be interrupted before requested.
  1648. This function generates a break condition by transmitting a stream of
  1649. zero bits on the terminal associated with the file descriptor
  1650. @var{filedes}. The duration of the break is controlled by the
  1651. @var{duration} argument. If zero, the duration is between 0.25 and 0.5
  1652. seconds. The meaning of a nonzero value depends on the operating system.
  1653. This function does nothing if the terminal is not an asynchronous serial
  1654. data port.
  1655. The return value is normally zero. In the event of an error, a value
  1656. of @math{-1} is returned. The following @code{errno} error conditions
  1657. are defined for this function:
  1658. @table @code
  1659. @item EBADF
  1660. The @var{filedes} is not a valid file descriptor.
  1661. @item ENOTTY
  1662. The @var{filedes} is not associated with a terminal device.
  1663. @end table
  1664. @end deftypefun
  1665. @cindex flushing terminal output queue
  1666. @cindex terminal output queue, flushing
  1667. @deftypefun int tcdrain (int @var{filedes})
  1668. @standards{POSIX.1, termios.h}
  1669. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1670. @c Direct ioctl.
  1671. The @code{tcdrain} function waits until all queued
  1672. output to the terminal @var{filedes} has been transmitted.
  1673. This function is a cancellation point in multi-threaded programs. This
  1674. is a problem if the thread allocates some resources (like memory, file
  1675. descriptors, semaphores or whatever) at the time @code{tcdrain} is
  1676. called. If the thread gets canceled these resources stay allocated
  1677. until the program ends. To avoid this calls to @code{tcdrain} should be
  1678. protected using cancellation handlers.
  1679. @c ref pthread_cleanup_push / pthread_cleanup_pop
  1680. The return value is normally zero. In the event of an error, a value
  1681. of @math{-1} is returned. The following @code{errno} error conditions
  1682. are defined for this function:
  1683. @table @code
  1684. @item EBADF
  1685. The @var{filedes} is not a valid file descriptor.
  1686. @item ENOTTY
  1687. The @var{filedes} is not associated with a terminal device.
  1688. @item EINTR
  1689. The operation was interrupted by delivery of a signal.
  1690. @xref{Interrupted Primitives}.
  1691. @end table
  1692. @end deftypefun
  1693. @cindex clearing terminal input queue
  1694. @cindex terminal input queue, clearing
  1695. @deftypefun int tcflush (int @var{filedes}, int @var{queue})
  1696. @standards{POSIX.1, termios.h}
  1697. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1698. @c Direct ioctl.
  1699. The @code{tcflush} function is used to clear the input and/or output
  1700. queues associated with the terminal file @var{filedes}. The @var{queue}
  1701. argument specifies which queue(s) to clear, and can be one of the
  1702. following values:
  1703. @c Extra blank lines here make it look better.
  1704. @vtable @code
  1705. @item TCIFLUSH
  1706. Clear any input data received, but not yet read.
  1707. @item TCOFLUSH
  1708. Clear any output data written, but not yet transmitted.
  1709. @item TCIOFLUSH
  1710. Clear both queued input and output.
  1711. @end vtable
  1712. The return value is normally zero. In the event of an error, a value
  1713. of @math{-1} is returned. The following @code{errno} error conditions
  1714. are defined for this function:
  1715. @table @code
  1716. @item EBADF
  1717. The @var{filedes} is not a valid file descriptor.
  1718. @item ENOTTY
  1719. The @var{filedes} is not associated with a terminal device.
  1720. @item EINVAL
  1721. A bad value was supplied as the @var{queue} argument.
  1722. @end table
  1723. It is unfortunate that this function is named @code{tcflush}, because
  1724. the term ``flush'' is normally used for quite another operation---waiting
  1725. until all output is transmitted---and using it for discarding input or
  1726. output would be confusing. Unfortunately, the name @code{tcflush} comes
  1727. from POSIX and we cannot change it.
  1728. @end deftypefun
  1729. @cindex flow control, terminal
  1730. @cindex terminal flow control
  1731. @deftypefun int tcflow (int @var{filedes}, int @var{action})
  1732. @standards{POSIX.1, termios.h}
  1733. @safety{@prelim{}@mtunsafe{@mtasurace{:tcattr(filedes)/bsd}}@asunsafe{}@acsafe{}}
  1734. @c Direct ioctl on Linux. On BSD, the TCO* actions are a single ioctl,
  1735. @c whereas the TCI actions first call tcgetattr and then write to the fd
  1736. @c the c_cc character corresponding to the action; there's a window for
  1737. @c another thread to change the xon/xoff characters.
  1738. The @code{tcflow} function is used to perform operations relating to
  1739. XON/XOFF flow control on the terminal file specified by @var{filedes}.
  1740. The @var{action} argument specifies what operation to perform, and can
  1741. be one of the following values:
  1742. @vtable @code
  1743. @item TCOOFF
  1744. Suspend transmission of output.
  1745. @item TCOON
  1746. Restart transmission of output.
  1747. @item TCIOFF
  1748. Transmit a STOP character.
  1749. @item TCION
  1750. Transmit a START character.
  1751. @end vtable
  1752. For more information about the STOP and START characters, see @ref{Special
  1753. Characters}.
  1754. The return value is normally zero. In the event of an error, a value
  1755. of @math{-1} is returned. The following @code{errno} error conditions
  1756. are defined for this function:
  1757. @table @code
  1758. @vindex EBADF
  1759. @item EBADF
  1760. The @var{filedes} is not a valid file descriptor.
  1761. @vindex ENOTTY
  1762. @item ENOTTY
  1763. The @var{filedes} is not associated with a terminal device.
  1764. @vindex EINVAL
  1765. @item EINVAL
  1766. A bad value was supplied as the @var{action} argument.
  1767. @end table
  1768. @end deftypefun
  1769. @node Noncanon Example
  1770. @section Noncanonical Mode Example
  1771. Here is an example program that shows how you can set up a terminal
  1772. device to read single characters in noncanonical input mode, without
  1773. echo.
  1774. @smallexample
  1775. @include termios.c.texi
  1776. @end smallexample
  1777. This program is careful to restore the original terminal modes before
  1778. exiting or terminating with a signal. It uses the @code{atexit}
  1779. function (@pxref{Cleanups on Exit}) to make sure this is done
  1780. by @code{exit}.
  1781. @ignore
  1782. @c !!!! the example doesn't handle any signals!
  1783. The signals handled in the example are the ones that typically occur due
  1784. to actions of the user. It might be desirable to handle other signals
  1785. such as SIGSEGV that can result from bugs in the program.
  1786. @end ignore
  1787. The shell is supposed to take care of resetting the terminal modes when
  1788. a process is stopped or continued; see @ref{Job Control}. But some
  1789. existing shells do not actually do this, so you may wish to establish
  1790. handlers for job control signals that reset terminal modes. The above
  1791. example does so.
  1792. @node getpass
  1793. @section Reading Passphrases
  1794. When reading in a passphrase, it is desirable to avoid displaying it on
  1795. the screen, to help keep it secret. The following function handles this
  1796. in a convenient way.
  1797. @deftypefun {char *} getpass (const char *@var{prompt})
  1798. @standards{BSD, unistd.h}
  1799. @safety{@prelim{}@mtunsafe{@mtasuterm{}}@asunsafe{@ascuheap{} @asulock{} @asucorrupt{}}@acunsafe{@acuterm{} @aculock{} @acucorrupt{}}}
  1800. @c This function will attempt to create a stream for terminal I/O, but
  1801. @c will fallback to stdio/stderr. It attempts to change the terminal
  1802. @c mode in a thread-unsafe way, write out the prompt, read the passphrase,
  1803. @c then restore the terminal mode. It has a cleanup to close the stream
  1804. @c in case of (synchronous) cancellation, but not to restore the
  1805. @c terminal mode.
  1806. @code{getpass} outputs @var{prompt}, then reads a string in from the
  1807. terminal without echoing it. It tries to connect to the real terminal,
  1808. @file{/dev/tty}, if possible, to encourage users not to put plaintext
  1809. passphrases in files; otherwise, it uses @code{stdin} and @code{stderr}.
  1810. @code{getpass} also disables the INTR, QUIT, and SUSP characters on the
  1811. terminal using the @code{ISIG} terminal attribute (@pxref{Local Modes}).
  1812. The terminal is flushed before and after @code{getpass}, so that
  1813. characters of a mistyped passphrase are not accidentally visible.
  1814. In other C libraries, @code{getpass} may only return the first
  1815. @code{PASS_MAX} bytes of a passphrase. @Theglibc{} has no limit, so
  1816. @code{PASS_MAX} is undefined.
  1817. The prototype for this function is in @file{unistd.h}. @code{PASS_MAX}
  1818. would be defined in @file{limits.h}.
  1819. @end deftypefun
  1820. This precise set of operations may not suit all possible situations. In
  1821. this case, it is recommended that users write their own @code{getpass}
  1822. substitute. For instance, a very simple substitute is as follows:
  1823. @smallexample
  1824. @include mygetpass.c.texi
  1825. @end smallexample
  1826. The substitute takes the same parameters as @code{getline}
  1827. (@pxref{Line Input}); the user must print any prompt desired.
  1828. @node Pseudo-Terminals
  1829. @section Pseudo-Terminals
  1830. @cindex pseudo-terminals
  1831. A @dfn{pseudo-terminal} is a special interprocess communication channel
  1832. that acts like a terminal. One end of the channel is called the
  1833. @dfn{master} side or @dfn{master pseudo-terminal device}, the other side
  1834. is called the @dfn{slave} side. Data written to the master side is
  1835. received by the slave side as if it was the result of a user typing at
  1836. an ordinary terminal, and data written to the slave side is sent to the
  1837. master side as if it was written on an ordinary terminal.
  1838. Pseudo terminals are the way programs like @code{xterm} and @code{emacs}
  1839. implement their terminal emulation functionality.
  1840. @menu
  1841. * Allocation:: Allocating a pseudo terminal.
  1842. * Pseudo-Terminal Pairs:: How to open both sides of a
  1843. pseudo-terminal in a single operation.
  1844. @end menu
  1845. @node Allocation
  1846. @subsection Allocating Pseudo-Terminals
  1847. @cindex allocating pseudo-terminals
  1848. @pindex stdlib.h
  1849. This subsection describes functions for allocating a pseudo-terminal,
  1850. and for making this pseudo-terminal available for actual use. These
  1851. functions are declared in the header file @file{stdlib.h}.
  1852. @deftypefun int posix_openpt (int @var{flags})
  1853. @standards{POSIX.1, stdlib.h}
  1854. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
  1855. @code{posix_openpt} returns a new file descriptor for the next
  1856. available master pseudo-terminal. In the case of an error, it returns
  1857. a value of @math{-1} instead, and sets @code{errno} to indicate
  1858. the error. @xref{Opening and Closing Files} for possible values
  1859. of @code{errno}.
  1860. @var{flags} is a bit mask created from a bitwise OR of zero or more
  1861. of the following flags:
  1862. @table @code
  1863. @item O_RDWR
  1864. Open the device for both reading and writing. It is usual to specify
  1865. this flag.
  1866. @item O_NOCTTY
  1867. Do not make the device the controlling terminal for the process.
  1868. @end table
  1869. These flags are defined in @file{fcntl.h}. @xref{Access Modes}.
  1870. For this function to be available, @code{_XOPEN_SOURCE} must be defined
  1871. to a value greater than @samp{600}. @xref{Feature Test Macros}.
  1872. @end deftypefun
  1873. @deftypefun int getpt (void)
  1874. @standards{GNU, stdlib.h}
  1875. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
  1876. @c On BSD, tries to open multiple potential pty names, returning on the
  1877. @c first success. On Linux, try posix_openpt first, then fallback to
  1878. @c the BSD implementation. The posix implementation opens the ptmx
  1879. @c device, checks with statfs that /dev/pts is a devpts or that /dev is
  1880. @c a devfs, and returns the fd; static variables devpts_mounted and
  1881. @c have_no_dev_ptmx are safely initialized so as to avoid repeated
  1882. @c tests.
  1883. @code{getpt} is similar to @code{posix_openpt}. This function is a
  1884. GNU extension and should not be used in portable programs.
  1885. The @code{getpt} function returns a new file descriptor for the next
  1886. available master pseudo-terminal. The normal return value from
  1887. @code{getpt} is a non-negative integer file descriptor. In the case of
  1888. an error, a value of @math{-1} is returned instead. The following
  1889. @code{errno} conditions are defined for this function:
  1890. @table @code
  1891. @item ENOENT
  1892. There are no free master pseudo-terminals available.
  1893. @end table
  1894. @end deftypefun
  1895. @deftypefun int grantpt (int @var{filedes})
  1896. @standards{SVID, stdlib.h}
  1897. @standards{XPG4.2, stdlib.h}
  1898. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1899. @c grantpt @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1900. @c unix/grantpt:pts_name @acsuheap @acsmem
  1901. @c ptsname_internal dup ok (but this is Linux-only!)
  1902. @c memchr dup ok
  1903. @c realloc dup @acsuheap @acsmem
  1904. @c malloc dup @acsuheap @acsmem
  1905. @c free dup @acsuheap @acsmem
  1906. @c fcntl dup ok
  1907. @c getuid dup ok
  1908. @c chown dup ok
  1909. @c sysconf(_SC_GETGR_R_SIZE_MAX) ok
  1910. @c getgrnam_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1911. @c getgid dup ok
  1912. @c chmod dup ok
  1913. @c fork dup @aculock
  1914. @c [child]
  1915. @c setrlimit
  1916. @c dup2
  1917. @c CLOSE_ALL_FDS
  1918. @c execle
  1919. @c _exit
  1920. @c waitpid dup ok
  1921. @c WIFEXITED dup ok
  1922. @c WEXITSTATUS dup ok
  1923. @c free dup @ascuheap @acsmem
  1924. The @code{grantpt} function changes the ownership and access permission
  1925. of the slave pseudo-terminal device corresponding to the master
  1926. pseudo-terminal device associated with the file descriptor
  1927. @var{filedes}. The owner is set from the real user ID of the calling
  1928. process (@pxref{Process Persona}), and the group is set to a special
  1929. group (typically @dfn{tty}) or from the real group ID of the calling
  1930. process. The access permission is set such that the file is both
  1931. readable and writable by the owner and only writable by the group.
  1932. On some systems this function is implemented by invoking a special
  1933. @code{setuid} root program (@pxref{How Change Persona}). As a
  1934. consequence, installing a signal handler for the @code{SIGCHLD} signal
  1935. (@pxref{Job Control Signals}) may interfere with a call to
  1936. @code{grantpt}.
  1937. The normal return value from @code{grantpt} is @math{0}; a value of
  1938. @math{-1} is returned in case of failure. The following @code{errno}
  1939. error conditions are defined for this function:
  1940. @table @code
  1941. @item EBADF
  1942. The @var{filedes} argument is not a valid file descriptor.
  1943. @item EINVAL
  1944. The @var{filedes} argument is not associated with a master pseudo-terminal
  1945. device.
  1946. @item EACCES
  1947. The slave pseudo-terminal device corresponding to the master associated
  1948. with @var{filedes} could not be accessed.
  1949. @end table
  1950. @end deftypefun
  1951. @deftypefun int unlockpt (int @var{filedes})
  1952. @standards{SVID, stdlib.h}
  1953. @standards{XPG4.2, stdlib.h}
  1954. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{/bsd}}@acunsafe{@acsmem{} @acsfd{}}}
  1955. @c unlockpt @ascuheap/bsd @acsmem @acsfd
  1956. @c /bsd
  1957. @c ptsname_r dup @ascuheap @acsmem @acsfd
  1958. @c revoke ok (syscall)
  1959. @c /linux
  1960. @c ioctl dup ok
  1961. The @code{unlockpt} function unlocks the slave pseudo-terminal device
  1962. corresponding to the master pseudo-terminal device associated with the
  1963. file descriptor @var{filedes}. On many systems, the slave can only be
  1964. opened after unlocking, so portable applications should always call
  1965. @code{unlockpt} before trying to open the slave.
  1966. The normal return value from @code{unlockpt} is @math{0}; a value of
  1967. @math{-1} is returned in case of failure. The following @code{errno}
  1968. error conditions are defined for this function:
  1969. @table @code
  1970. @item EBADF
  1971. The @var{filedes} argument is not a valid file descriptor.
  1972. @item EINVAL
  1973. The @var{filedes} argument is not associated with a master pseudo-terminal
  1974. device.
  1975. @end table
  1976. @end deftypefun
  1977. @deftypefun {char *} ptsname (int @var{filedes})
  1978. @standards{SVID, stdlib.h}
  1979. @standards{XPG4.2, stdlib.h}
  1980. @safety{@prelim{}@mtunsafe{@mtasurace{:ptsname}}@asunsafe{@ascuheap{/bsd}}@acunsafe{@acsmem{} @acsfd{}}}
  1981. @c ptsname @mtasurace:ptsname @ascuheap/bsd @acsmem @acsfd
  1982. @c ptsname_r dup @ascuheap/bsd @acsmem @acsfd
  1983. If the file descriptor @var{filedes} is associated with a
  1984. master pseudo-terminal device, the @code{ptsname} function returns a
  1985. pointer to a statically-allocated, null-terminated string containing the
  1986. file name of the associated slave pseudo-terminal file. This string
  1987. might be overwritten by subsequent calls to @code{ptsname}.
  1988. @end deftypefun
  1989. @deftypefun int ptsname_r (int @var{filedes}, char *@var{buf}, size_t @var{len})
  1990. @standards{POSIX.1-2024, stdlib.h}
  1991. @safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{/bsd}}@acunsafe{@acsmem{} @acsfd{}}}
  1992. @c ptsname_r @ascuheap/bsd @acsmem @acsfd
  1993. @c /hurd
  1994. @c term_get_peername ok
  1995. @c strlen dup ok
  1996. @c memcpy dup ok
  1997. @c /bsd
  1998. @c isatty dup ok
  1999. @c strlen dup ok
  2000. @c ttyname_r dup @ascuheap @acsmem @acsfd
  2001. @c stat dup ok
  2002. @c /linux
  2003. @c ptsname_internal ok
  2004. @c isatty dup ok
  2005. @c ioctl dup ok
  2006. @c strlen dup ok
  2007. @c itoa_word dup ok
  2008. @c stpcpy dup ok
  2009. @c memcpy dup ok
  2010. @c fxstat64 dup ok
  2011. @c MASTER_P ok
  2012. @c major ok
  2013. @c gnu_dev_major ok
  2014. @c minor ok
  2015. @c gnu_dev_minor ok
  2016. @c minor dup ok
  2017. @c xstat64 dup ok
  2018. @c S_ISCHR dup ok
  2019. @c SLAVE_P ok
  2020. @c major dup ok
  2021. @c minor dup ok
  2022. The @code{ptsname_r} function is similar to the @code{ptsname} function
  2023. except that it places its result into the user-specified buffer starting
  2024. at @var{buf} with length @var{len}.
  2025. This function was originally a GNU extension, but was added in
  2026. POSIX.1-2024.
  2027. @end deftypefun
  2028. Typical usage of these functions is illustrated by the following example:
  2029. @smallexample
  2030. int
  2031. open_pty_pair (int *amaster, int *aslave)
  2032. @{
  2033. int master, slave;
  2034. char *name;
  2035. master = posix_openpt (O_RDWR | O_NOCTTY);
  2036. if (master < 0)
  2037. return 0;
  2038. if (grantpt (master) < 0 || unlockpt (master) < 0)
  2039. goto close_master;
  2040. name = ptsname (master);
  2041. if (name == NULL)
  2042. goto close_master;
  2043. slave = open (name, O_RDWR);
  2044. if (slave == -1)
  2045. goto close_master;
  2046. *amaster = master;
  2047. *aslave = slave;
  2048. return 1;
  2049. close_slave:
  2050. close (slave);
  2051. close_master:
  2052. close (master);
  2053. return 0;
  2054. @}
  2055. @end smallexample
  2056. @node Pseudo-Terminal Pairs
  2057. @subsection Opening a Pseudo-Terminal Pair
  2058. @cindex opening a pseudo-terminal pair
  2059. These functions, derived from BSD, are available in the separate
  2060. @file{libutil} library, and declared in @file{pty.h}.
  2061. @deftypefun int openpty (int *@var{amaster}, int *@var{aslave}, char *@var{name}, const struct termios *@var{termp}, const struct winsize *@var{winp})
  2062. @standards{BSD, pty.h}
  2063. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2064. @c openpty @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2065. @c getpt @acsfd
  2066. @c grantpt @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2067. @c unlockpt dup @ascuheap/bsd @acsmem @acsfd
  2068. @c openpty:pts_name @acsuheap @acsmem @acsfd
  2069. @c ptsname_r dup @ascuheap/bsd @acsmem @acsfd
  2070. @c realloc dup @acsuheap @acsmem
  2071. @c malloc dup @acsuheap @acsmem
  2072. @c free dup @acsuheap @acsmem
  2073. @c open dup @acsfd
  2074. @c free dup @acsuheap @acsmem
  2075. @c tcsetattr dup ok
  2076. @c ioctl dup ok
  2077. @c strcpy dup ok
  2078. @c close dup @acsfd
  2079. This function allocates and opens a pseudo-terminal pair, returning the
  2080. file descriptor for the master in @var{*amaster}, and the file
  2081. descriptor for the slave in @var{*aslave}. If the argument @var{name}
  2082. is not a null pointer, the file name of the slave pseudo-terminal
  2083. device is stored in @code{*name}. If @var{termp} is not a null pointer,
  2084. the terminal attributes of the slave are set to the ones specified in
  2085. the structure that @var{termp} points to (@pxref{Terminal Modes}).
  2086. Likewise, if @var{winp} is not a null pointer, the screen size of
  2087. the slave is set to the values specified in the structure that
  2088. @var{winp} points to.
  2089. The normal return value from @code{openpty} is @math{0}; a value of
  2090. @math{-1} is returned in case of failure. The following @code{errno}
  2091. conditions are defined for this function:
  2092. @table @code
  2093. @item ENOENT
  2094. There are no free pseudo-terminal pairs available.
  2095. @end table
  2096. @strong{Warning:} Using the @code{openpty} function with @var{name} not
  2097. set to @code{NULL} is @strong{very dangerous} because it provides no
  2098. protection against overflowing the string @var{name}. You should use
  2099. the @code{ttyname} function on the file descriptor returned in
  2100. @var{*slave} to find out the file name of the slave pseudo-terminal
  2101. device instead.
  2102. @end deftypefun
  2103. @deftypefun int forkpty (int *@var{amaster}, char *@var{name}, const struct termios *@var{termp}, const struct winsize *@var{winp})
  2104. @standards{BSD, pty.h}
  2105. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2106. @c forkpty @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2107. @c openpty dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2108. @c fork dup @aculock
  2109. @c close dup @acsfd
  2110. @c /child
  2111. @c close dup @acsfd
  2112. @c login_tty dup @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
  2113. @c _exit dup ok
  2114. @c close dup @acsfd
  2115. This function is similar to the @code{openpty} function, but in
  2116. addition, forks a new process (@pxref{Creating a Process}) and makes the
  2117. newly opened slave pseudo-terminal device the controlling terminal
  2118. (@pxref{Controlling Terminal}) for the child process.
  2119. If the operation is successful, there are then both parent and child
  2120. processes and both see @code{forkpty} return, but with different values:
  2121. it returns a value of @math{0} in the child process and returns the child's
  2122. process ID in the parent process.
  2123. If the allocation of a pseudo-terminal pair or the process creation
  2124. failed, @code{forkpty} returns a value of @math{-1} in the parent
  2125. process.
  2126. @strong{Warning:} The @code{forkpty} function has the same problems with
  2127. respect to the @var{name} argument as @code{openpty}.
  2128. @end deftypefun