I'm working on multiple C programs like a shell and a text editor that require to be run without the ECHO and ICANON flags. I disabled these using termios.h and managed to write my own gets function that can relay returned strings to my program and do special things for escape characters. The only think I can't do is print a backspace. If, for example, I use this code:
void mgets(char *str)
{
int c, i = 0;
while((c = getchar()) != '\n')
if(c == 27)
// the user hit ESC, ignore it for now
else if(c == '\b')
puts("\b \b") // this is where it SHOULD backspace
// else if it's a regular character:
else {
str+i = c; i++; // write that to the string...
putchar(c); // ...and echo it to the screen
}
}
It all works great but the program just doesn't respond when I backspace. if I change my if statement a bit…
if(c == '\b')
printf("You hit a backspace!");
But it's still unresponsive. I know that puts("\b \b") works so the only conclusion is that my backspace key isn't being detected as a '\b'. What can I do? Please help? Thanks in advance!
Best Answer
Before you change the termios settings, save them. That information is the "same" as what you can see with
stty -a
.For example
The value shown after
erase
is the character that you're referring to as "backspace". Depending on the system conventions that might be^H
(8, or ASCII backspace) or^?
(127, or ASCII DEL).In the termios settings, e.g.,
then
is the same erase character (with some caveats when the data has never been set:
stty
would show "undef").Unlike some of the other settings, the meaning of the erase character is still relevant in raw/cbreak modes, since it is a setting that tells the terminal driver what your terminal is expected to send. It doesn't hurt to test for both possibilities, in case your
stty
settings were incorrect...Further reading: