Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Prevent infinite input loop when testing with readline in non-interactive mode #365

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
# define PT_ROW_NUM 192
# define UNDEFINED_STATE -1

/* User Input */
# define MAX_INPUT_RETRIES 5

/* Export */
# define EXPORT_PREFIX "export "

Expand Down
4 changes: 2 additions & 2 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
/* User input utils */
bool read_input(
char **line,
t_sh *shell,
char *prompt,
bool add_to_history,
bool is_interactive);
bool add_to_history);

/* Token list utils */
t_tok *init_token(t_tok_typ type, char *data);
Expand Down
2 changes: 1 addition & 1 deletion source/backend/redirection/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static t_hd_st read_heredoc(t_sh *shell, t_list **line_list, char *here_end)
while (true)
{
line = NULL;
if (!read_input(&line, HEREDOC_PROMPT, false, shell->is_interactive))
if (!read_input(&line, shell, HEREDOC_PROMPT, false))
return (free(line), HD_ERROR);
if (shell->exit_code == TERM_BY_SIGNAL + SIGINT)
return (free(line), HD_ABORT);
Expand Down
2 changes: 1 addition & 1 deletion source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main(void)
print_welcome_msg(&shell);
while (true)
{
if (!read_input(&shell.input_line, PROMPT, true, shell.is_interactive))
if (!read_input(&shell.input_line, &shell, PROMPT, true))
continue ;
if (!shell.input_line)
exec_exit(&shell, NULL);
Expand Down
2 changes: 1 addition & 1 deletion source/shell/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void close_std_io(void);
void clean_and_exit_shell(t_sh *shell, int exit_code, char *msg)
{
if (msg)
print_error("%s\n", msg);
print_error("%s: %s\n", PROGRAM_NAME, msg);
clean_shell(shell);
safe_close_all_pipes(shell);
free_get_next_line();
Expand Down
36 changes: 27 additions & 9 deletions source/utils/user_input_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,53 @@
/* */
/* ************************************************************************** */

#include "defines.h"
#include "clean.h"
#include "utils.h"

static bool limit_retries(t_sh *shell, bool success);

bool read_input(
char **line,
t_sh *shell,
char *prompt,
bool add_to_history,
bool is_interactive)
bool add_to_history)
{
char *tmp;

errno = SUCCESS;
if (is_interactive)
if (shell->is_interactive)
*line = readline(prompt);
else
{
tmp = get_next_line(STDIN_FILENO);
if (errno != SUCCESS)
return (free(tmp), false);
return (free(tmp), limit_retries(shell, false));
if (tmp)
{
*line = ft_strtrim(tmp, "\n");
free(tmp);
}
}
if (errno == EINTR)
if (errno == EINTR || errno == ENOTTY)
errno = SUCCESS;
else if (errno != SUCCESS)
return (false);
if (add_to_history && *line && **line)
return (limit_retries(shell, false));
if (add_to_history && shell->is_interactive && *line && **line)
add_history(*line);
return (errno == SUCCESS);
return (limit_retries(shell, errno == SUCCESS));
}

static bool limit_retries(t_sh *shell, bool success)
{
static int retries;

if (success == true)
{
retries = 0;
return (true);
}
else if (++retries > MAX_INPUT_RETRIES)
clean_and_exit_shell(shell, GENERAL_ERROR, "read input failed");
print_error("%s: read input failed, trying again...\n", PROGRAM_NAME);
return (false);
}