0

So I'm trying to reproduce bash, and when I press control + \ in bash it does nothing. So I'm trying to reproduce that behavior this is my code.

# include <signal.h>
# include <readline/readline.h>
# include <readline/history.h>
# include <stdlib.h>
# include <unistd.h>

void    sig_handler(int sig)
{
    write(2, "\b \b", 2);
}

int main(void)
{
    signal(SIGQUIT, sig_handler);
    while (1)
    {
        char *line = readline("test> ");
        if (!line)
            break;
        free(line);
    }
    return (0);
}

When I press control + \ , it works the first time but then when I press it again it starts showing the ^ character, so how am I supposed to do this? I'm on macOS btw.

to compile this code run gcc main.c -lreadline

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sami
  • 75
  • 1
  • 9
  • `stty -echoctl` achieves the effect. You need to read the documentation for [`tcgetattr()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetattr.html), `tcsetattr()` and the [``](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/termios.h.html) header to achieve the same effect. There may also be a method in the GNU readline library to do the job; I'm not familiar enough with it to know for sure. The `` link lists `ECHOCTL` as 'reserved for XSI-compliant systems', meaning that it isn't directly supported by POSIX, but was standardized elsewhere. – Jonathan Leffler Mar 03 '22 at 15:28
  • 1
    Note, too, that macOS doesn't use the GNU readline library — it uses an emulation of it. – Jonathan Leffler Mar 03 '22 at 15:30

0 Answers0