Remanent prompt staying in a readline history when using rl_add_history

40 Views Asked by At

I am currently recreating a mini version of bash in C. I am having troubles with the readline function: when I type a pipe command such as cat /etc/os-release | grep ubuntu I have some characters that stay on the screen (cat /etc/os-) but not in the readline buffer (if I press they are not here)

Also, when I check my .minishell_history, I can see that these remanent characters are not stored in any case, they just appear on the screen and disappear when you resize the window by example

Example if I run:

minishell> ls | wc

and then

minishell> cat /etc/os-release | grep ubuntu

When I try to go up in the history this is what is displayed:

minishell> cat /etc/os-ls | wc

But when I press enter, the normal ls | wc is executed and only ls | wc is stored in my .minishell_history file.

Here is the code related to history:

int main(int argc, char **argv, char **env)
{
    t_data              data;

    (void)argv;
    if (argc != 1)
    {
        errno = EINVAL;
        perror("main");
        exit(EXIT_FAILURE);
    }
    ft_memset(&data, 0, sizeof(t_data));
    if (init_data(&data, env))
        exit_minishell(&data, EXIT_FAILURE);
    launch_minishell(&data);
    return (EXIT_SUCCESS);
}

void    launch_minishell(t_data *data)
{
    while (1)
    {
        signals_interactive();
        data->argv = readline(ENTRY_PROMPT);
        if (!data->argv)
            exit_minishell(data, EXIT_SUCCESS);
        signals_non_interactive();
        add_history(data->argv);
        if (lexer(data) || parser(data) || executer(data))
        {
            free_memory_between_commands(data);
            continue ;
        }
        free_memory_between_commands(data);
    }
}

void    exit_minishell(t_data *data, int exit_code)
{
    ft_printf_colour(RED_BOLD, "Exiting minishell \n\n");
    if (ft_getenv("HISTFILE=", data))
        write_history(ft_getenv("HISTFILE=", data) + 9);
    free_all_memory(data);
    rl_clear_history();
    exit(exit_code);
}

int init_data(t_data *data, char **env)
{
    char    *history_path;

    if (init_env(data, env) || init_path(data))
        return (EXIT_FAILURE);
    if (ft_getenv("HOME=", data))
    {
        history_path = ft_concat(2, ft_getenv("HOME=", data) + 5, \
                    "/.minishell_history");
        if (history_path)
        {
            add_variable_to_env("HISTFILE=", history_path, data);
            read_history(history_path);
            free_and_set_to_null(1, history_path);
        }
    }
    init_io(data);
    data->wstatus = 0;
    return (EXIT_SUCCESS);
}

Thanks in advance for the help!

0

There are 0 best solutions below