Home > Blockchain >  How are ctrl x inputs processed?
How are ctrl x inputs processed?

Time:10-30

I have searched so much, but I simply cannot find any specific info on how CTRL inputs work in terminals, and in interconnection to STDIN

My setup:

  • unblocking input with no generation of terminal signals
  • reading STDIN with read(2)

What I think I know:

  • in termbox2 source code I saw that CTRL A equates to the number 1, which in turn equates to the ASCII code for "Start of Heading".

Now, what I am utterly confused by is how all this interconnects? Do CTRL letter inputs literally put a number between 1 and 26 into STDIN? Is ^A notation just a representation of the number 1? Does the terminal listen for CTRL C, or number 4, or for the ASCII "End of Transmission"? Or how about CTRL G which is apparently 7 and ASCII BELL character, which can also be written as '\a' in code. And where can I find which numbers are generated by CTRL combinations, except in obscure source codes?

I'm sure there is a very simple explanation for all this, but I only know bits and pieces of the puzzle, and can't connect it.

Please help!

CodePudding user response:

how all this interconnects?

There's a "terminal program" (or just "terminal"), e.g. termbox2, and there's a TTY device, e.g. /dev/pts/2.

Ctrl letter is detected as key presses by the terminal. The terminal converts it to its ASCII control code (e.g. Ctrl A becomes 1, Ctrl G becomes 7), and writes this code to the TTY device.

The TTY driver sees a control code in the TTY input stream. If the terminal is in "cooked mode", and the control code is actionable by the TTY, it will pull this byte out of the TTY input stream and act on it. If the terminal is in raw mode, the control code just stays in the stream.

"Act on it" could mean, for example, translating ^Z (ASCII 26) to a SIGTSTP to the foreground-running process.

Now, if the terminal is configured to echo input to the terminal (e.g. normal typing, but not for passwords), then any bytes which weren't pulled out of the TTY input stream will also be sent to the TTY output stream.

So the terminal sees a byte with the value 1 in the TTY output stream.
Normally, if it sees a regular byte like e (ASCII 101), it just prints it.
ASCII 7 (BELL) has no defined display, but it is known to be generated by Ctrl G, so the terminal tries to be as informative as possible and prints it as ^G.

Do CTRL letter inputs literally put a number between 1 and 26 into STDIN?

Yes, if the control code isn't intercepted by the TTY driver.

Is ^A notation just a representation of the number 1?

Yes.

Does the terminal listen for CTRL C, or number 4, or for the ASCII "End of Transmission"?

The terminal program listens for the keys Ctrl and C.
The terminal driver, which takes inputs from the terminal program, listens for the number 4.

Or how about CTRL G which is apparently 7 and ASCII BELL character, which can also be written as '\a' in code.

The terminal translates the combination of Ctrl and G pressed to together to the number 7.
The C compiler translates \a to the number 7.

And where can I find which numbers are generated by CTRL combinations, except in obscure source codes?

Wikipedia.


Extra curricular: this post really goes into the gory details of TTYs.

  •  Tags:  
  • c
  • Related