[HN Gopher] What is TTY?
       ___________________________________________________________________
        
       What is TTY?
        
       Author : arunc
       Score  : 170 points
       Date   : 2022-08-13 13:39 UTC (9 hours ago)
        
 (HTM) web link (www.sobyte.net)
 (TXT) w3m dump (www.sobyte.net)
        
       | np_tedious wrote:
       | Further evidence that I am so clueless about the workings of many
       | tools I use every day
        
         | GrabbinD33ze69 wrote:
         | I mean modern computing is built upon many layers of
         | abstraction, where you only need to understand the
         | technicalities of the thing you're specifically working on, and
         | the tech you don't have time to focus on will seem like "magic"
         | due to abstraction.
        
       | cebert wrote:
       | I would hope this wasn't a software engineering interview
       | question. I've been in industry a while, and would have no idea
       | what to say if asked about this in an interview.
        
       | teddyh wrote:
       | I prefer this: https://www.linusakesson.net/programming/tty/
       | 
       | In fact, the image with the "hardware" and "software" diagrams
       | seems to have been copied directly from that link. I did not see
       | any attribution or acknowledgement.
        
       | bluetomcat wrote:
       | It was probably a good abstraction for its time. The driver for
       | the particular terminal was abstracted away in the OS, meaning
       | that programs only had to handle a pre-defined set of signals and
       | IO.
       | 
       | It is, however, a needlessly complex abstraction as of now. The
       | lack of a real terminal device means that the OS maintains such
       | pseudo devices for the sole purpose of being able to spawn
       | multiple virtual terminals.
        
       | davegauer wrote:
       | This is an excellent and entertaining intro to the world of TTYs
       | and I think the first question is a particularly clever way to
       | introduce it:
       | 
       | > The answer is Ctrl+W. Because Ctrl+W is provided by something
       | called TTY, and the other three are provided by the shell.
       | 
       | My only criticism is a pet peeve of mine: screenshots of
       | terminals. I know it's more work to copy/paste and make sure
       | formatting is correct, and you lose the ANSI colors. But it just
       | feels so wasteful and inaccessible (<img alt="tty">).
        
         | Ennea wrote:
         | You could preserve the colors with some CSS, even :) I have the
         | sudden urge to write a small tool/some CSS that can
         | automatically convert bits of terminal output into ready-to-use
         | fancy HTML+CSS.
        
           | rascul wrote:
           | Something like aha? (It's probably in your distro's repo)
           | 
           | https://github.com/theZiz/aha/
        
             | Ennea wrote:
             | I knew that somebody would already have something ready
             | that does this :) Thanks!
        
         | rascul wrote:
         | You can use the script(1) utility (from util-linux) to record a
         | session and it will save the ANSI colors. This only works if
         | you do it ahead of time, though. And if you're putting it in a
         | pastebin, the color codes will get in the way.
        
           | 5e92cb50239222b wrote:
           | There's also https://asciinema.org . It makes recordings
           | which can be replayed just like video (colors and all), but
           | weigh almost nothing and let you select and copy text. There
           | are lots of examples on the main page.
        
           | blueflow wrote:
           | There exists tooling to convert these typescripts into, for
           | example, HTML output to include on your website. I use the
           | script(1) util as part of my dayjob to create a bit of a
           | trail when there are questions later.
           | 
           | Some terminal emulators also support dumping the buffer as
           | HTML.
        
             | rascul wrote:
             | > There exists tooling to convert these typescripts into,
             | for example, HTML output to include on your website.
             | 
             | Indeed, I mentioned aha in another comment.
             | 
             | > Some terminal emulators also support dumping the buffer
             | as HTML.
             | 
             | That's a nifty feature that I haven't run into yet. Seems
             | like it could be quite useful.
        
               | formerly_proven wrote:
               | Konsole does HTML export and the HTML is simple/editable
               | (only inline styles, insignificant whitespace removed,
               | output as-shown, not as-generated, so e.g. a progress bar
               | is just a single line of text, not however many times it
               | was updated).
        
       | rascul wrote:
       | Kind of related, Textmode Window Environment is interesting.
       | 
       | https://github.com/cosmos72/twin
       | 
       | A couple screenshots are on the old SourceForge site.
       | 
       | https://sourceforge.net/projects/twin/
        
       | commonlisper wrote:
       | > Ctrl+B to move a letter forward
       | 
       | Did they mean Ctrl+B to move a letter backward?
       | 
       | Ctrl+F moves a letter forward.
        
       | arpa wrote:
       | recently i tasked myself into creating a bash TUI
       | library/framework, thinking it would be as easy as taking good
       | old pascal in late 1990s and just drawing on screen using ascii.
       | Boy was I mistaken!
        
         | rascul wrote:
         | Something like dialog or whiptail? Dialog uses ncurses and
         | whiptail uses s-lang.
        
           | arpa wrote:
           | yes, but no underlying libs, just pure bash... "How hard can
           | that be"...
        
             | kevin_thibedeau wrote:
             | PC TUIs have the advantage of direct access to the
             | character buffer. Doing it indirectly with control codes is
             | much more involved when it comes to tracking terminal
             | state.
        
       | zokier wrote:
       | > Because Ctrl+W is provided by something called TTY, and the
       | other three are provided by the shell
       | 
       | This is more complicated (or simpler?) than what the article
       | explains. Or I might be really confused. But if you strace bash
       | while typing in stuff, you see clearly that it is reading in
       | characters one by one, including ^W, and handling them itself. I
       | don't see TTY doing much magic here at all:
       | read(0, "h", 1)                         = 1         write(2, "h",
       | 1)                        = 1         read(0, "e", 1)
       | = 1         write(2, "e", 1)                        = 1
       | read(0, "l", 1)                         = 1         write(2, "l",
       | 1)                        = 1         read(0, "l", 1)
       | = 1         write(2, "l", 1)                        = 1
       | read(0, "o", 1)                         = 1         write(2, "o",
       | 1)                        = 1         read(0, " ", 1)
       | = 1         write(2, " ", 1)                        = 1
       | read(0, "w", 1)                         = 1         write(2, "w",
       | 1)                        = 1         read(0, "o", 1)
       | = 1         write(2, "o", 1)                        = 1
       | read(0, "r", 1)                         = 1         write(2, "r",
       | 1)                        = 1         read(0, "l", 1)
       | = 1         write(2, "l", 1)                        = 1
       | read(0, "d", 1)                         = 1         write(2, "d",
       | 1)                        = 1         read(0, "\x17", 1)
       | = 1     <-- bash reading in ^W!         write(2,
       | "\x08\x08\x08\x08\x08\x1b\x5b\x4b", 8) = 8
        
         | zokier wrote:
         | stracing bash more reveals another thing; when executing
         | command like stty then before forking bash calls TCSETSW ioctl
         | to set tty settings, and after child exists its calls TCSETSW
         | again to set its own values, so stty does not actually show
         | what mode the tty is in while bash is handling the input:
         | ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig icanon
         | echo ...}) = 0         clone(child_stack=NULL,
         | flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
         | child_tidptr=0x7fe653abef10) = 5175         wait4(-1,
         | [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED,
         | NULL) = 5175         --- SIGCHLD {si_signo=SIGCHLD,
         | si_code=CLD_EXITED, si_pid=5175, si_uid=1000, si_status=0,
         | si_utime=0, si_stime=0} ---         wait4(-1, 0x7fff156cbad0,
         | WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child
         | processes)         ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400
         | opost isig -icanon -echo ...}) = 0
         | 
         | https://man7.org/linux/man-pages/man4/tty_ioctl.4.html
        
         | icedchai wrote:
         | I believe this is handled by the tty "canonical" (line input)
         | mode. If you run "stty -a" you'll see a number of special
         | characters defined, like werase = ^W
         | 
         | If you set this to something else (stty werase ^O) you'll see
         | it takes effect immediately.
        
           | zokier wrote:
           | but as it turns out bash does not use canonical mode, and I
           | suspect that applies to many other interactive repls/shells
           | too; probably something readline handles
        
             | icedchai wrote:
             | You are correct for bash. If you run "cat" (from "bash"),
             | you'll see that it no longer handles control-A or control-E
             | (handled by readline...), but control-W is still handled by
             | the tty.
        
       | throwaway09223 wrote:
       | bash is a bad example for these types of interview questions
       | because, as others have noted, bash disables many of these tty
       | features and handles them in-process.
       | 
       | Another example: The author touches on line discipline, but then
       | makes the mistake of saying "The command is actually stored in
       | the TTY after it is typed and before the Enter key is pressed"
       | This isn't true in bash, or on any modern shells that support
       | line editing features like tab completion.
       | 
       | If you're going to use this for an interview question, I suggest
       | using cat(1) instead -- it will behave as the author suggests.
        
       ___________________________________________________________________
       (page generated 2022-08-13 23:00 UTC)