Reputation: 632
I can parse input, traverse up and down in the history but my backspace does not work. Why is that?
void printPrompt(void)
{ // prints working directory prompt
char cwd[256];
getcwd(cwd, 255);
printf("%s> ", cwd);
fflush(stdout);
}
Upvotes: 0
Views: 200
Reputation: 7698
The backspace is working. The problem is the redisplay of the line after the backspace. You should probably print a carriage return (\r) and then reprint the line including the prompt.
Edit: I'm not certain that my suggestion is 100% portable across terminals either. You may need to use something like curses to handle the portability issues.
Upvotes: 0
Reputation: 9850
You're trying to print out a DEL
(dec 127) when you should be sending a BS
(dec 8). You'll also probably then want to send a space and another BS
to clear out that character (BS
just moves the cursor). So, when you get buf[0] == 8
, print out 8
, 32
, 8
.
else if (buf[0] == 8)
{ // Backspace
if (charsRead > 0)
{
line[charsRead - 1] = '\0';
charsRead--;
printf("%c%c%c", 8, 32, 8); // CHANGE HERE
}
}
Upvotes: 5