
The TTY Demystified - natex
http://www.linusakesson.net/programming/tty/
======
noirman
Previously on Hacker News: <http://news.ycombinator.com/item?id=4062981>

------
pooriaazimi
Very informative article.

What I _love_ is how they come up with different names for different signals
that kill/suspend processes:

`SIGHUP`, `SIGINT`, `SIGQUIT`, `SIGABRT`, `SIGKILL`, `SIGTERM` and `SIGSTOP`!

7 different combinations of SIG and a verb that means
"terminate/kill/stop/close/quit/suspend", and it's not _that_ confusing (once
you get your head around it). Must have been quite a challenge!

~~~
emmelaich
Although sigkill should've been named sigstop and sigstop should've been
sigpause. Ah, hindsight is a wonderful thing.

~~~
jlgreco
Eh, do you stop your car at an intersection, or do you pause it?

------
zx2c4
The article makes an interesting point

"There's an important difference here: Writing to a TTY which is stopped due
to flow control, or due to lack of kernel buffer space, will block your
process, whereas writing to a TTY from a background job will cause a SIGTTOU
to suspend the entire process group. I don't know why the designers of UNIX
had to go all the way to invent SIGTTOU and SIGTTIN instead of relying on
blocking I/O, but my best guess is that the TTY driver, being in charge of job
control, was designed to monitor and manipulate whole jobs; never the
individual processes within them."

Can anyone provide any more insight about this?

------
ChuckMcM
This is a great article, at some point it will be a story like how the rail
spacing in England is a function of the ruts created by the Roman chariots
(unclear if that is actually the case (see snopes etc)), Having written tty
driver code for FiNE (Fine-is-not-EMACS) there was a lot stuff related to just
how fast the terminal on the other end of the serial channel could do stuff.
(we had tunables for insert line, scroll etc).

~~~
jrabone
Great article, and your point about tunables is spot on; in the guts of
several curses implementations there's an evil little select() call which is
trying to work out if you meant "cursor down 1 line" or the four seperate
characters ESC, [, 1, A by doing millisecond-precise timings.

IMHO I think this all needs to be replaced with something sane. VT100 is
obsolete, and we can now spare a few more BPS for a better terminal layer.
Wasn't so long ago that if you left capslock on, something (getty?) on the
Linux console would assume your terminal was one of those olde-worlde 6-bit
ones, and stop using lowercase letters...

~~~
dllthomas
> IMHO I think this all needs to be replaced with something sane.

It seems that we're in the process of replacing it with web tech, which may or
may not meet your definition of "something sane"...

~~~
cturner
Web isn't a replacement. It doesn't and won't ever have consistent keyboard
handling across platforms and browser upgrades. As a result, it has evolved to
being mouse-centric, and is inadequate for high-volume users.

There are many web systems being developed as business front-ends today that
would suck less were they menu and form-driven terminal apps with a couple of
web screens for reports. (or a websockets arrangement where the terminal could
render certain things to an associated browser when appropriate)

~~~
Jamiecon
Where I work, our > 15 year old ticket retail system was developed on a
platform called Dataflex [0], which provides a character mode user interface.
We have since migrated the datastore from the embedded ISAM database to
Postgres, and built eCommerce and other web applications around the original
schema.

Our administrative and call centre staff continue to use the original
character mode interface to interact with the data and do their daily work.
The software works and we have no reason to redevelop it anyway, but
seriously, you should see the speed with which experienced staff move around
the system. Editors can input hundreds of events an hour and call centre staff
are amazingly efficient - they can practically use the thing blindfold.

There's a lot to be said for consistent keyboard controls and a distraction
free interface in the workplace. Line of business applications from the last
couple of decades, mostly mouse driven, are often tragically inefficient.

[0] <http://en.wikipedia.org/wiki/Dataflex>

~~~
yourapostasy
Somewhat sadly, it doesn't look like businesses are commissioning such
character terminal interfaces any longer. Certainly not in sufficient volumes
to support the few boutique shops that published and maintained text window
libraries that some developers would use to insulate themselves from the
insanity of supporting multiple terminal types on different systems. As far as
I can tell from some Google searches, Vermont Creative Software's Vermont
Views and Oakland Group, Inc.'s C-Scape are both no longer sold or even have a
web presence. If it is true both went out of business, it would be a pity that
such codebases are languishing in bit rot hell and could possibly disappear
from the historical record. There were and are no comparable open source text
window libraries with the polish these products had.

~~~
cturner
(Follow-up post after re-read.)

Actually, maybe these things have features that I wouldn't know of being only
a curses person. I'd be interested to know more if you wrote up a blog post.
Email cratuki at the google mail server.

~~~
yourapostasy
I'm prone to Yegge-length expositions in a blog, so the HN format might be
better. These libraries could be thought of kind of like extensions to curses.
In some cases, they actually depended on curses, in others, they did the work
curses did and then went beyond that. You could typically draw into a defined
buffer that represented a window, then have that window blasted onto the
terminal replete with the character ASCII art that decorated the window
borders without having to work out the arithmetic drudgery to put the right
spaces and characters to draw the borders.

More elaborate libraries let you overlap the windows and would optimally
redraw them when you moved the windows. They simply let you avoid having to
roll your own text user interface starting from curses and building from
there. Open source equivalents never approached the comprehensiveness or
polish of these commercial libraries, though the open source ones are pretty
good. Now that the commercial libraries appear to have dropped off the face of
the earth, the open source ones seem to be all that is left. CDK [1] and NDK++
[2] on Unix and DFLAT [3] on DOS are examples of the open source libraries. I
just felt the commercial libraries were interesting pieces of software history
that should be preserved.

I personally feel that the kind of high-speed manual data entry optimization
that these libraries facilitated is likely on its way out with advances in
machine learning and the self-service customer culture of ecommerce. Certainly
nothing prevents someone from using a standard web browser to present a user
interface that responds entirely from keypresses, should the need for a highly
optimized data entry-oriented Javascript library become necessary (something
I've looked for but haven't found either for sale or open source). You would
need to come up with a way to buffer up all the keypresses locally on the
browser and ensure they all make it to the server, but it's doable.

If someone really had a burning desire to have some kind of in-house
application presented through a character terminal interface via PuTTY in
today's world, then they can still fall back to curses I suppose. Though I
sometimes wonder if it would be faster to just write something in Emacs Lisp
and autostart that when the user logs in.

[1] <http://invisible-island.net/cdk/cdk.html>

[2] <http://ndk-xx.sourceforge.net/>

[3] [http://www.ibiblio.org/pub/micro/pc-
stuff/freedos/files/deve...](http://www.ibiblio.org/pub/micro/pc-
stuff/freedos/files/devel/libs/dflat/)

------
Zenst
Nice read, the part at the end "Finally, stty sane will restore your TTY
device configuration to something reasonable" Realy should be what you learn
from your fist instance of using a terminal (to many bad memorys of yesteryear
systems over RS232 and uucp and modems, stop me now).

But everybody learns what they need to learn, even if it is setting the
backspace/del keys to something visualy complient.

Knowing a tty and VT commands you can do some realy simple clever stuff, like
find certain people logged in and there tty and cat append information to
there screens and if they have terminals with extra sub lines you can sends a
string that will memorise the screen cursor position, reposition cursor to the
lower lines and display your text and then restore the cursor position. A very
easy and simple way to instantly alert logged on users in certain groups. Can
do all that in shell script easily. So knowing how things work and there
capabilities and even there history and background so you understand why they
are the way they are and from that appreciete there limitations. Then you can
start doing some realy fun things. Again, nice read as verifys what you know
and will fill in some worthy blanks.

~~~
kps
Often when TTY settings are messed up by a full-screen program, you need to
type «Control-J stty sane Control-J». It's necessary to use Control-J (i.e.
newline) explicitly instead of pressing Return or Enter, because those
generally send CR (Carriage Return, Control-M) and CR-to-newline translation
is off.

~~~
Zenst
Very true I instinctly do a ctrl-c first, but once you learn to not cat
binary's/unsanatised data to your terminal you learn to add strings or use OD
instead.

------
stox
The concept of a teletype predated ASCII by decades. I would not use this
article for any historical references.

------
cientifico
How many times this article appear at the hacker new front page?

~~~
johncoltrane
Didn't spot it the previous times.

~~~
tisme
<http://news.ycombinator.com/item?id=658155>

<http://news.ycombinator.com/item?id=4062981>

------
cm3
This shows why Plan 9 as part of the plan to fix inconsistencies and problems
in UNIX wrote a terminal which doesn't even support escape sequences for
coloring. I'm not a Plan 9 expert but it's actually for the better. Try
launching some program like the sudoku game or acme from 9term inside Plan 9
compared to doing the same in plan9port's 9term or a plain xterm. On Plan 9
your 9term will become the program you launch.

~~~
derleth
> wrote a terminal which doesn't even support escape sequences for coloring

Color can be a powerful way to organize large amounts of information quickly.
What's the Plan 9 way to do that?

~~~
cm3
I don't know enough about Plan 9 to answer that.

------
joedoe55555
Ok, so how do I build a teletype that I can connect via USB to my Mac? (And
will vi work? :D)

~~~
johncoltrane
While not exactly a teletype, Justin Ouelette's VT220 is cool:

<http://jstn.cc/post/8692501831>

<http://jstn.cc/post/13476503553>

<http://jstn.cc/post/10831555077>

[http://www.fastcodesign.com/1665509/digital-archaeology-
hack...](http://www.fastcodesign.com/1665509/digital-archaeology-hacker-
gets-2011-mac-os-to-run-on-1983-computer)

<http://justinouellette.com/>

~~~
jff
What's most amazing to me is that this got so much press. Maybe OS X is
ridiculously hard to set up, but I've been doing this on and off with my own
VT 220 for the last 5 years on Linux. It's just... not that big of a deal.
It's not archaeology, it's just pointing getty at /dev/ttyS0.

I've done it with an ADM-3A from the 70s too, it's just. Not. That. Tough.

<http://imgur.com/bRnST> here's me, years back, hacking on a VT 220

~~~
derleth
On the hardware level, does the VT 220 connect via a standard nine-pin serial
port?

~~~
kps
"standard nine-pin serial port"

Them's fightin' words, boy.

DEC had a DE9 serial port connector whose pinout is _not_ the same as the
later IBM PC DE9 serial ports.

------
sharkweek
Fascinating read -- when I was a bit... less mature, I used to use this
service for many a prank call, which at one point apparently accounted for
85-90% of the calls according to some operators:
[http://en.wikipedia.org/wiki/Telecommunications_relay_servic...](http://en.wikipedia.org/wiki/Telecommunications_relay_service#Fraudulent_uses_in_the_United_States)

~~~
derleth
If you can tell me how to use /dev/ttyS0 to make a prank call, I'm all ears.
;)

~~~
jrockway
Simply use a serial cable to connect your computer to a prank-calling device.
Done!

