10 ms·
How Unix erases things when you type a backspace while entering text
- basicplus2 10y agoWhen I first started programming a pdpd11 using basicplus2 I wrote a program that had a built in password which consisted of the password which included backspaces and *'s which obliterated the first half of the printed password so when it was printed out you could not read the password. Primitive but effective. Even when viewing it on the screen it was quick enough you could not read the password
- corysama 10y agoWay back on Solaris I once managed to accidentally name a file backspace. Somehow I managed to finger-flub and hit "mv filename <crtl-h> <enter>" or something like that. Had to use a GUI interface to delete that file.
- vram22 10y agoNot at a Unix machine right, now, but this might work: rm \BKSP where the BKSP represents the backspace key. If it worked, it did because the backslash escapes the usual meaning of the backspace key, which is to erase the preceding character. Also, related (though may not work for above case), to delete other files with funny characters in the name (like a leading dash): rm -- -filename where the -- means (end of the options), so even a leading dash after -- is not treated it as an option. Best to use the -i option too, with such cases, for interactive confirmation of the remove.
- ChuckMcM 10y agoWhat is perhaps more interesting is the evolution and thinking about how it is done so easily on modern systems. I think I've rewritten the basic 'cooked' mode probably a few times the latest being for a simulation of it on embedded systems[1]. The basic code is easy and it fails on the interesting edge cases (the most common one being if you auto CR/LF at the end of the screen few readline functions back up to the previous line.) These days however if you have "lots" of memory and rendering a screen takes less time than rendering a single character did back in the day. You can just delete the character out of the buffer and re-render the entire screen. No need to count, your screen can do funky things like set your prompt different colors have tabs at an arbitrary number of spaces etc. Basically you can start at the top and render the entire 40 - 50 lines of text, poof in less an few mS. That was unthinkable when even at 9600 baud it took a couple of seconds to re-render the enter screen. [1] https://github.com/ChuckM/stm32f469i/blob/master/demos/util/console.c#L158 https://github.com/ChuckM/stm32f469i/blob/master/demos/util/...
- userbinator 10y agoYou can just delete the character out of the buffer and re-render the entire screen. I've used apps which I suspect do this (not necessarily terminal emulators), and I'd strongly recommend against it; it may not be noticeable if your input rate isn't high, but if you lean on a key and let the keyboard auto-repeat, the rest of the screen/window, which hasn't changed, starts to noticeably flicker annoyingly.
- pacaro 10y agoThese apps are getting more wrong than rendering too much on a single character change if there is visible flicker. I wrote software that rendered its entire UI on any change into a DIB and blitted to the DC twenty years ago with no flicker
- FreeFull 10y agoMaybe for some reason they clear the screen first before redrawing? That would certainly cause a flicker.
- chillaxtian 10y agoi'm confused as to why the article implies the kernel is serving the shell. isn't the shell usually a program running in userspace? that interprets commands, and prints characters on the screen, etc, in userspace? i didn't think the kernel was involved at all, except to receive exec syscalls.
- jclulow 10y agoIt's true that modern shells like "bash" are receiving a stream of bytes in what is called the "raw" mode of the tty subsystem. For programs that don't switch to raw mode, though, the tty is generally in the "cooked" mode that has existed for most of the life of UNIX and similar platforms. In cooked mode, the kernel is interpreting your input and provides basic line editing facilities -- only once you "commit" a line, generally by pressing something like carriage return or enter, does the kernel make the entire line available on the tty file descriptor. That's why the author mentions "cat" as a way to witness the kernel's line editing behaviour: it generally leaves the terminal in "cooked" mode.
- sdkmvx 10y agoThe shell needs to print a prompt and read input. This is I/O, which involves the kernel. The shell is (when running interactively) attached to some terminal. These days it's usually a pseudo-terminal with a terminal emulator like xterm on the other end. (With some exceptions) the read syscall on a terminal does not return until there's a newline. The kernel only implements simple line editing. You can kill the whole like (^U) or erase the last character (^H i.e. backspace). Traditionally kill and erase were @ and # which you may see in some old documentation. On a hardcopy terminal you could type daat##te and it would run date. If the right flags are set, instead of outputting the erase character, Unix will back up, print a space, and back up again as described. However the shell supports more line editing. I bet you can press the left arrow and edit the middle of the line. I bet you can press the up arrow and get the last line typed. I bet you can press ^A and get to the beginning of the line. The kernel doesn't do any of this, and you can't do it in cat. What happens is the shell turns off line editing. Then it gets all input unprocessed, and can decide what to do on it's own. This is why Chris Siebenmann put the PS in there.
- 10y ago
- userbinator 10y agoThe whole TTY layer actually contains a surprising amount of functionality, handling line editing being only one of them; this is one of the best articles I've seen about the rest of that: http://www.linusakesson.net/programming/tty/ http://www.linusakesson.net/programming/tty/ (It's also been mentioned multiple times on HN.)
- mkl 10y agoThis is interesting, because it reveals the reason Windows Services for Linux gets some terminal things wrong - it's just emulating a Linux kernel, and they haven't done this bit the same. Running cat > /dev/null then pressing tab then backspace works properly on Linux (including SSH from a terminal on Windows), but doesn't work on WSL (or Cygwin).
- mastax 10y agoIt is possible to get it working properly, but it does take some work: https://conemu.github.io/en/BashOnWindows.html https://conemu.github.io/en/BashOnWindows.html
- stiglitz 10y ago[nit] Windows Subsystem for Linux
- 0x0 10y agoIn related news, Windows (NT/2000/XP/2003) had a BSOD crasher in terminal tab+backspace handling for decades. https://www.owasp.org/index.php/The_CSRSS_Backspace_Bug_still_works_in_windows_2003_sp1 https://www.owasp.org/index.php/The_CSRSS_Backspace_Bug_stil...
- JdeBP 10y agoThe original article still exists, at http://jdebp.eu./FGA/csrss-backspace-bug.html http://jdebp.eu./FGA/csrss-backspace-bug.html
- amelius 10y agoThis seems complicated and error prone. Although I can see the advantages of this approach, it really makes me wonder whether the designers chose the right abstractions. Also, the fact that I can type "cat binary-file" and mess up my terminal settings tells me that they didn't clearly separated concerns.
- MaulingMonkey 10y ago> This seems complicated and error prone. Although I can see the advantages of this approach, it really makes me wonder whether the designers chose the right abstractions. Keep in mind that many of the original tty devices were teleprinters, and the physical actions of the printer head upon being commanded to backspace would involve moving the actual printer head back a character - potentially using an eraser head - and then potentially moving the pointer head back again once more if that advanced the head. It's perhaps complicated an error prone, but it's also the optimal movement of the printer head. You do not want to bake "complicated and error prone" into the peripherals of the time. You had no firmware. I'm not sure if you even had a proper ROM, necessarily. So you do it on the software side. There's no abstraction here per se - but a 1:1 mapping of commands to common sets of physical hardware actions. That's not even a bad thing - what exactly would the abstraction gain you back then? You might abstract the escape codes to use for a given terminal via a terminfo or termcap database, to support multiple devices, but the core actions were always the same. Hopefully you're not still using teleprinters in production - but someone is. And the VDUs that replaced teleprinters reused the same control mechanisms - keeps the complexity and thus unit price down, easier to retrofit / make backwards compatible with software expecting teleprinters, etc... I know at least one place still using fairly dumb VDUs in production, and these sequences are the simplest fastest commands you can send them to effectively implement "backspace". If you ape the same logic in your latest and greatest single-page javascript webpage "app", and you're not writing a terminal emulator, it might be the wrong abstraction. It's sane for ttys though. > Also, the fact that I can type "cat binary-file" and mess up my terminal settings tells me that they didn't clearly separated concerns. It's a gun with the potential to be a footgun. Benefits, drawbacks. Writing a shell script that wants to read passwords? cat a file which disables input echoing. After invoking readline, cat a different file to re-enable it. "Feature". Want to cobble together a quick script to use your laser printer to print barcodes? Create a binary file that creates the barcode you want, cat it, pipe that through sed a few times to replace the number, and then pipe straight to the appropriate /dev/ttyXY. I've actually seen this, fixed this, and updated this. Figuring out the correct keyboard inputs to give vi was 'fun'. "Feature". If you don't want to mess up your terminal, "view" or "vi" is likely the correct command - not cat.
- Jaruzel 10y agoYou know when you are getting old when you read: > When you hit backspace, the kernel tty line discipline rubs out your previous character by printing (in the simple case) Ctrl-H, a space, and then another Ctrl-H. And you think 'Yup, I knew that.' ... (In my case, I just about recall re-implementing it when mucking about with serial consoles in my youth - On low baud connections, you could actually see it do it - backspace - space - backspace )
- kps 10y ago> printing (in the simple case) Ctrl-H, a space, and then another Ctrl-H. For the really simple case, run 'stty -echoe'. (And perhaps run something like 'cat', since most shells run in raw mode and do their own editing by default.)
- rasjani 10y agoI didn't read the article and all the comments but this is the sort of thing once again reminds that there's really nothing new in most of the new technologies :) Lets take Dom updates for example. Do you update the whole tree and rerender the whole screen or only the changed parts. Problem is essentially the "same" albeit tech and corner cases have their own set of differences. Next, consider that maybe your browser is just more fancier terminal and webserver is just evolution of old mainframes. Well, that's just my way of thinking but because way of thinking and having started my career writing unix applications, I still can maintain my interest in latest 'trends' and at least feel that I have something to contribute when trend seems to gear towards hiring younger and younger devs.
- rocky1138 10y agoBut what if I want a string that includes the backspace character?