News for hackers. Stuff that matters.
Re:What happens when you press a key in your terminal? by schoen on Thursday July 21, 2022 @03:00AM (#32176587)
> I believe the reason cat gets interrupted when we press Ctrl+C is that the Linux kernel on the server side receives this \x03 character, recognizes that it means “interrupt”, and then sends a SIGINT to the process that owns the pseudoterminal’s process group. So it’s handled in the kernel and not in userspace.

Also interestingly, the use of \x03 for this purpose is a default, but it's not hardcoded. You can change it with the stty command.

For example, if you run

stty intr '^X'

then your interrupt character sequence will be Ctrl+X instead of Ctrl+C (!).

In order to make this change, the stty program actually has to call into the kernel with an ioctl call (TCSETS or a related ioctl, for "terminal control set settings").

You can learn more about this ioctl on Linux in the ioctl_tty(2) man page, and you can see the various settings that can be changed this way with

stty -a

(It's a little bit confusing which things are handled by the readline library and which things are handled by the kernel, as the kernel's ability to support some rudimentary line-editing features on an interactive terminal long predates libraries like readline. I think readline might actually disable kernel interpretation of some of these control characters when it starts accepting input, and then re-enable them when it stops, but I've never looked into that.)

Re:What happens when you press a key in your terminal? by formerly_proven on Thursday July 21, 2022 @04:06AM (#32176903)
> I think readline might actually disable kernel interpretation of some of these control characters when it starts accepting input, and then re-enable them when it stops, but I've never looked into that.

That's the canonical mode bit (ICANON). Canonical mode means the kernel/terminal is line-buffered and line-editing is handled by it; non-canonical mode means user input is pushed to the application immediately.

Re:What happens when you press a key in your terminal? by tonnydourado on Thursday July 21, 2022 @05:11AM (#32177202)
> For example, if you run

> stty intr '^X'

> then your interrupt character sequence will be Ctrl+X instead of Ctrl+C (!).

I have a feeling that if you actually do this in a real Linux system, a sysadmin will hunt you down and dismember you alive, but I might be wrong.

Re:What happens when you press a key in your terminal? by grymoire1 on Thursday July 21, 2022 @08:17AM (#32178429)
Nope. As someone who administered a VAX with 50 simultaneous users, it's not a problem. It only affect that process and that user during that session. A logout resets everything. A more interesting problem was trying to read or write to a serial port that was hardwired to use a different baud rate.

You could type "stty 9600 /dev/tty4"; cat file >/dev/tty4" and it wouldn't work because when stty exited, the system would reset the terminal baud rate.

The proper way to do this (assuming you weren't the sysadmin and couldn't modify the default per-terminal baud rate), was to type the following

(stty 9600; cat file)>/dev/tty4

Re:What happens when you press a key in your terminal? by jwilk on Thursday July 21, 2022 @06:19AM (#32177607)
Fun fact: it doesn't have to be Ctrl+something. If you feel paricularly evil today, you might try this:

  stty intr y
Re:What happens when you press a key in your terminal? by Brian_K_White on Thursday July 21, 2022 @07:41AM (#32178184)
It can be any single byte, except probably not 0x00.

It cannot be a multi-byte sequence, which means it cannot be any keyboard key.

On SCO systems intr/break is the Del key, which on a scoansi terminal emits ^? (I don't remember the ascii value just that ctrl-? is another way to produce it) So to break out of programs is the Del key instead of Ctrl-C.

But on a vtxx terminal like the linux console or xterm, even if you were perverse enough to want to, you can not assign break/intr to the Del key like that, because on a vtxx terminal the Del key emits a multi byte escape sequence, not a single byte.

hot keys like ctrl-c require multiple fingers, but what's produced is a single byte.

(you can actually modify both the console and xterm to change what a key emits, but then the resulting terminal no longer matches the definition of a linux or xterm terminal)

Re:What happens when you press a key in your terminal? by kps on Thursday July 21, 2022 @10:13AM (#32179645)
> Del key, which on a scoansi terminal emits ^?

^? = ASCII DEL[elete] = 0x7F. Terminals where the Delete key sends Delete are doing it right.

Why is DEL 0x7F, when other control codes are <0x20? Because the American Standard Code for Information Interchange descended from teletype codes, and teletypes often used paper tape ‘storage’ where a 1 bit was a hole. So teletype codes would normally have a delete function punch all holes, because that would obliterate any other possible character (and typical of punches, advance to the next position, making DEL semantically a forward delete operation).

> on a vtxx terminal the Del key emits a multi byte escape sequence

Only VTxxx where xxx ≥ 200. The VT100 series and earlier had ASCII Delete and Backspace keys, but in the VT2xx era DEC got some funny ideas and provided only a ⌫ key, which left us an enduring mess.

Re:What happens when you press a key in your terminal? by jwilk on Thursday July 21, 2022 @09:42AM (#32179276)
> It can be any single byte, except probably not 0x00.

At least on Linux, indeed you can't use 0x00 (aka ^@), because _POSIX_VDISABLE (the thing you use for disabling special characters) is 0.

Re:What happens when you press a key in your terminal? by jwilk on Thursday July 21, 2022 @08:29AM (#32178507)
^? = 0x7F
Re:What happens when you press a key in your terminal? by psydvl on Thursday July 21, 2022 @06:39AM (#32177733)
Is it fun enough to be 1st April joke?
Re:What happens when you press a key in your terminal? by codesnik on Thursday July 21, 2022 @08:01AM (#32178319)
joke aside, it is a per-tty setting, right?
Re:What happens when you press a key in your terminal? by BaseballPhysics on Thursday July 21, 2022 @10:13AM (#32179653)
Sure, which is why you put it in /etc/profile so you don't have to remember to run the command all the time!
Re:What happens when you press a key in your terminal? by jwilk on Thursday July 21, 2022 @09:13AM (#32178925)
Yes.
Re:What happens when you press a key in your terminal? by deadbunny on Thursday July 21, 2022 @05:28AM (#32177296)
Can confirm.
Re:What happens when you press a key in your terminal? by dundarious on Thursday July 21, 2022 @01:30PM (#32182224)
Anyone interested in the machinations of all of this terminal stuff should look at antirez’ kilo, a terminal text editor in under 1000 lines of code: https://github.com/antirez/kilo

There is a nice tutorial that walks through how one might write it from scratch: https://viewsourcecode.org/snaptoken/kilo/

Re:What happens when you press a key in your terminal? by dylan604 on Thursday July 21, 2022 @10:37AM (#32179922)
The timing of HN can be spooky sometimes. Earlier this week I was just wondering it I could switch the ctrl-c interrupt to a single key for easier keyboard smashing to stop an errant poorly thought out script. My choice would have been the spacebar as that is second nature muscle memory from all of my days in an edit bay where the spacebar acted as the All Stop and was engraved with the phrase Awww Shit! as that was the typical utterance from the editor just before using the key
Re:What happens when you press a key in your terminal? by account42 on Friday July 22, 2022 @07:47AM (#32190933)
stty is useful when an interactive program died before it had a chance to restore the terminal mode so you end up with nothing displayed when you type - `stty sane` will fix that up.