<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Made of Bugs &#187; apis</title>
	<atom:link href="http://blog.nelhage.com/tag/apis/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nelhage.com</link>
	<description>It's software. It's made of bugs.</description>
	<lastBuildDate>Thu, 18 Aug 2011 21:57:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>A Brief Introduction to termios: termios(3) and stty</title>
		<link>http://blog.nelhage.com/2009/12/a-brief-introduction-to-termios-termios3-and-stty/</link>
		<comments>http://blog.nelhage.com/2009/12/a-brief-introduction-to-termios-termios3-and-stty/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 05:47:17 +0000</pubDate>
		<dc:creator>nelhage</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[apis]]></category>
		<category><![CDATA[termios]]></category>
		<category><![CDATA[tty]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://blog.nelhage.com/?p=27</guid>
		<description><![CDATA[(This is part two of a multi-part introduction to termios and terminal emulation on UNIX. Read part 1 if you&#8217;re new here) In this entry, we&#8217;ll look at the interfaces that are used to control the behavior of the &#8220;termios&#8221; box sitting between the master and slave pty. The behaviors I described last time are [...]]]></description>
			<content:encoded><![CDATA[<p>(This is part two of a multi-part introduction to termios and terminal
emulation on UNIX. Read <a href="http://blog.nelhage.com/archives/14">part 1</a> if you&#8217;re new here)</p>

<p>In this entry, we&#8217;ll look at the interfaces that are used to control
the behavior of the &#8220;termios&#8221; box sitting between the master and
slave pty. The behaviors I described last time are fine if you have a
completely dumb program talking to the terminal, but if the program
over on the right is using curses (like emacs or vim), or even just
readline (like bash), it will want to disable or customize some of the
behaviors.</p>

<p>The primary programmatic interface to termios is the <code>struct termios</code>
and two functions:</p>

<pre><code>   int tcgetattr(int fd, struct termios *termios_p);
   int tcsetattr(int fd, int optional_actions,
                 const struct termios *termios_p);
</code></pre>

<p>which retrieve and set the <code>struct termios</code> associated with a given terminal device.
They are all documented in <code>termios(3)</code> (If you&#8217;re
unfamiliar with the convention, that means document <code>termios</code> in section
<code>3</code> of the unix <code>man</code> pages &#8212; <code>man 3 termios</code> on a command-line will
get it for you).</p>

<p>So what&#8217;s inside <code>struct termios</code>? POSIX
specifies that this structure contains at least the following fields:</p>

<pre><code>       tcflag_t c_iflag;      /* input modes */
       tcflag_t c_oflag;      /* output modes */
       tcflag_t c_cflag;      /* control modes */
       tcflag_t c_lflag;      /* local modes */
       cc_t     c_cc[NCCS];   /* control chars */
</code></pre>

<p>Each &#8220;flag&#8221; field contains a number of flags (implemented as a bitmask) that can be individually enabled or disabled.
<code>c_iflag</code> and <code>c_oflag</code> contain flags that affect the processing
of input and output, respectively. <code>c_cflag</code> we will mostly ignore, as
it contains settings that relate to the control of modems and serial
lines that are mostly irrelevant these days. <code>c_lflag</code> is perhaps the most interesting of the flag values. It
contains flags that control the broad-scale behavior of the
<code>tty</code>. I&#8217;ll look at just a few of the interesting bits in each:</p>

<h3>local modes</h3>

<ul>
<li><p><code>ICANON</code> &#8211; Perhaps the most important bit in <code>c_lflag</code> is the <code>ICANON</code>
bit. Enabling it enables &#8220;canonical&#8221; mode &#8212; also known as
&#8220;line editing&#8221; mode. When <code>ICANON</code> is set, the terminal buffers a line
at a time, and enables line editing. Without <code>ICANON</code>, input is made
available to programs immediately (this is also known as &#8220;cbreak&#8221;
mode). </p></li>
<li><p><code>ECHO</code> in <code>c_lflag</code> controls whether input is immediately re-echoed as
output. It is independent of <code>ICANON</code>, although they are often turned on and off together. When <code>passwd</code> prompts for your password, your terminal is in canonical mode, but <code>ECHO</code> is disabled.</p></li>
<li><p><code>ISIG</code> in <code>c_lflag</code> controls whether <code>^C</code> and <code>^Z</code> (and friends) generate signals or
not. When unset, they are passed directly through as characters,
without generating signals to the application.</p></li>
</ul>

<h3>input and output modes</h3>

<p>There are also a few flags in <code>c_iflag</code> and <code>c_oflag</code> worth mentioning.</p>

<ul>
<li><p><code>IXON</code> in <code>c_iflag</code> enables the &#8220;flow control&#8221; mediated by
<code>^S</code> and <code>^Q</code> (by default). With <code>IXON</code>, once <code>^S</code> has been received
by the master pty, the slave will not accept any output (<code>write</code>s to it will hang) until <code>^Q</code> is received by the master pty.</p></li>
<li><p><code>IUTF8</code> in <code>c_iflag</code> is an interesting hack. In canonical mode, backspace needs to
delete the previous character in the input buffer. In non-ASCII encodings, a single character may be several bytes long, but the terminal still only sees a byte stream, and has no explicit information about the encoding or character boundaries on either end.
<code>IUTF8</code> tells termios that the input stream is utf-8 encoded, which permits the
correct handling of backspace. If <code>IUTF8</code> is unset, and you enter a
multibyte character and then press backspace, only the final byte will
be deleted, leaving you with a corrupt utf-8 stream.</p></li>
<li><p><code>OLCUC</code> in <code>c_oflag</code> &#8220;Map[s] lowercase characters to uppercase on
output.&#8221; Just in CASE YOU NEED YOUR TERMINAL TO LOOK MORE LIKE
SHOUTING.</p></li>
</ul>

<p>There are many more flags, controlling such details as newline translation and how character erase works. The full list is documented in <code>termios(3)</code>.</p>

<h3>c_cc</h3>

<p>Next up is <code>c_cc</code>. This field sets the various control characters used to interact with the
terminal. Characters like <code>^C</code> and <code>^Z</code> and delete that have special meanings to termios are not hard coded anywhere, but rather defined via the <code>c_cc</code> array.</p>

<p><code>c_cc</code> is indexed by various constants for the various control characters, and
the value at any index is the character that should have that effect. Some of the notable ones are:</p>

<ul>
<li><p><code>VINTR</code> &#8212; Generate a <code>SIGINT</code> (<code>^C</code> by default).</p></li>
<li><p><code>VSUSP</code> &#8212; Generate a <code>SIGTSTP</code> (stop the program) (<code>^Z</code> by default).</p></li>
<li><p><code>VERASE</code> &#8212; Erase the previous character. This tends to be one of
<code>^H</code> and <code>^?</code> (ASCII <code>0x7f</code>) by default &#8212; if you&#8217;ve ever pressed
&#8220;backspace&#8221; and been greeted by a <code>^H</code>, your terminal and your
<code>struct termios</code> disagree on the value of <code>VERASE</code>.</p></li>
<li><p><code>VEOF</code> &#8212; End of file. Sends the current line to the program
without waiting for end-of-line, or, as the first character on the line, causes the next <code>read</code> call by the slave to return return end-of-file. (<code>^D</code> by default)</p></li>
<li><p><code>VSTOP</code> and <code>VSTART</code> &#8212; <code>^S</code> and <code>^Q</code> by default, stop and start
output.</p></li>
</ul>

<p>Setting any of these to NULL (0) disables that special control character. Many of the <code>c_cc</code> elements are only relevant when certain modes are active &#8212; <code>VINTR</code> and <code>VSUSP</code>, for instance, only matter if <code>ISIG</code> is enabled in <code>c_lflag</code>, and <code>VSTOP</code> and <code>VSTART</code> are ignored unless <code>IXON</code> is set.</p>

<p>(A brief note on the representation of control characters &#8212; The characters <code>^A</code> through <code>^Z</code>, pronounced &#8220;Ctrl-FOO&#8221;, are represented by the bytes with values 1 through 26. So when I say that <code>c_cc[VINTR]</code> is equal to <code>^C</code> by default,
that&#8217;s actually just the number <code>3</code> &#8212; your terminal took the keypresses and just translated them into the byte <code>3</code> on the wire.)</p>

<h2>stty</h2>

<p>While <code>termios(3)</code> is the standard programmatic interface to control
termios, a much more convenient interface for experimentation is the
<code>stty</code> program, which is just a thin wrapper around <code>tcgetattr</code> and
<code>tcsetattr</code> designed to be usable from shell scripts or directly from the shell.</p>

<p><code>stty</code> gets or sets options on a terminal device. By default, it operates on the one connected to its standard out, but you can pass it an arbitrary device using the <code>-F</code> option.</p>

<p>Without aguments, <code>stty</code> prints in what way its terminal&#8217;s settings differ from an internal set of &#8220;sane&#8221;
defaults. <code>stty -a</code> causes it to print the value of every flag in the
<code>struct termios</code> in a human-readable format.</p>

<p>You can toggle flags using <tt>stty <i>flag</i></tt> to enable a flag, and <tt>stty
-<i>flag</i></tt> to disable it. So for instance, <code>stty -isig</code> will disable
signal generation &#8212; run a program after doing this, and you&#8217;ll find
yourself unable to <code>^C</code> it. In general it uses the same names as the C constants, except in lowercase, but check the man page if in doubt.</p>

<p><code>stty</code> can also change the value of the control characters in
<code>c_cc</code>, using <tt>stty <i>symbolic-name</i> <i>character</i></tt>. If you wanted <code>^G</code> to be the interrupt character, instead of <code>^C</code>, a simple <code>stty intr ^G</code> would suffice. You can spell <code>0</code> as <code>undef</code> to disable a given control character. So, if you hate flow
control and want to totally disable it, you could try <code>stty -ixon stop
undef</code> &#8212; disable <code>IXON</code>, and then also disable the <code>VSTOP</code>
character for good measure. (You might still be foiled by screen or some other layer doing its own flow control, unfortunately).</p>

<p><code>stty</code>&#8216;s <code>-F</code> option can be great for peeking at what some other program is
doing to its terminal. If you run <code>tty</code> in a shell, it will print the
path to that shell&#8217;s terminal device (usually of the form
<tt>/dev/pts/<i>N</i></tt>, at least under Linux). Now,
from a different shell, you can run <tt>stty -a -F /dev/pts/<i>N</i></tt> to see how
the first shell&#8217;s terminal is configured.  You can then run programs
in the first shell, and repeat the <code>stty</code> incant in shell two to see
what settings are getting set. For example, if I run <code>stty -F /dev/pts/10</code> right now (while I have a <code>bash</code> talking to a <code>gnome-terminal</code> via that pty), I see:</p>

<pre><code>$ stty -F /dev/pts/10
speed 38400 baud; line = 0;
eol = M-^?; eol2 = M-^?; swtch = M-^?; lnext = &lt;undef&gt;; min = 1; time = 0;
-icrnl iutf8
-icanon -echo
</code></pre>

<p>So we can see that bash/readline has disabled CR→LF translation on input (<code>icrnl</code>), disabled canonical mode and echo, but turned on UTF-8 mode (because bash detected a utf-8 locale). Note that if I run <code>stty</code> directly in that shell, I see something slightly different:</p>

<pre><code>$ stty
speed 38400 baud; line = 0;
eol = M-^?; eol2 = M-^?; swtch = M-^?;
iutf8
</code></pre>

<p>This is because bash maintains its own set of termios settings (for readline), and saves and restores settings around running programs, so that settings in place while running a program are different from those in place while you&#8217;re typing at the bash prompt. </p>

<p>In the next post, we&#8217;ll look at signal generation from <code>ISIG</code> and how it interacts with job control in your shell.</p>

<h2>Addendum: <code>ioctl(2)</code></h2>

<p>This last section is a brief aside, which has very little to do with termios specifically, so you should feel free to skip it. But read on if you&#8217;re curious about some of the low-level details of how the APIs work.</p>

<p>If you&#8217;re familiar with man page conventions, you may have noticed
that the <code>termios</code> functions are in man page section 3, which means
that they&#8217;re provided by system libraries, and are not system
calls. But at the same time, I told you last time that termios is
implemented inside the kernel &#8212; so how are the libraries talking to
the kernel, if not through syscalls?</p>

<p>The answer is a single odd little catch-all system call, known as
<code>ioctl</code>. Historically, one of the &#8220;big ideas&#8221; of UNIX was that
&#8220;everything is a file&#8221; &#8212; you could communicate with devices just like
you could files, by opening files in <code>/dev/</code>. But a file on UNIX is also just a stream of bytes, without any
OS-imposed structure. And for a device, you may often need to send
out-of-band control data &#8212; e.g. to set the baud and parity bit settings on a
serial port. And adding new system calls for every new device type would
be untenable for a number of reasons.</p>

<p>So the answer was one new new system call, <code>ioctl</code> (pronounced as any
of &#8220;I-O-cuddle&#8221;, &#8220;I-octal&#8221; or &#8220;I-O-C-T-L&#8221;). <code>ioctl</code> is prototyped as:</p>

<pre><code>int ioctl(int fd, int request, ...);
</code></pre>

<p>It takes a file descriptor, a numeric &#8220;request&#8221; code, and an unspecified number of
other arguments. <code>ioctl</code> looks up whatever device (or file
system, network protcol, or whatever) is backing that file descriptor, and hands it the &#8220;request&#8221; and the arguments
to do with as they will. </p>

<p>So any device that needs extra control channels can define some <code>ioctl</code> numbers and parameters and document them somewhere, and they become the interface to control that device. So, for instance (at least on Linux), an <code>ioctl</code> on a tty device with  a &#8220;request&#8221; of <code>TCGETS</code> (defined in <code>termios.h</code>) takes a parameter that is a pointer to a <code>struct termios</code>, and copies the in-kernel settings for that tty to the provided struct. So somewhere in libc, <code>tcgetattr(fd, p)</code> is just defined to do an <code>ioctl(fd, TCGETS, p)</code>. Similar ioctls are defined for <code>tcsetattr</code> and all the functions in <code>termios(3)</code>. On Linux, at least, the morbidly curious can find out all the gory details in <code>tty_ioctl(4)</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nelhage.com/2009/12/a-brief-introduction-to-termios-termios3-and-stty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

