From 486dec7bb7e977c9856beeefc3d167ea389abe9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 19 Feb 2025 17:02:25 +0100 Subject: [PATCH] get_command: if input is not a tty, use get next line This simplifies tests --- spec/minishell_spec.sh | 6 ++---- src/minishell.c | 27 +++++++++++++++++---------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/spec/minishell_spec.sh b/spec/minishell_spec.sh index 145f2d2..27e4b48 100644 --- a/spec/minishell_spec.sh +++ b/spec/minishell_spec.sh @@ -6,8 +6,6 @@ It "echos output" When call ./minishell The output line 1 should eq "hello" - The output line 2 should eq "hello" - The output line 3 should eq "there" - The output line 4 should eq "there" - The output line 5 should be undefined + The output line 2 should eq "there" + The output line 3 should be undefined End diff --git a/src/minishell.c b/src/minishell.c index f848cfd..ebba968 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ -/* Updated: 2025/02/11 16:22:29 by khais ### ########.fr */ +/* Updated: 2025/02/19 17:02:17 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,17 +17,21 @@ #include #include #include +#include "libft.h" /* -** get the prompt to show to the user when getting a command -** -** if the terminal is a tty, the prompt is '$ ', else it is '' +** remove one '\n' from the end of the string, if it exists */ -static const char *get_prompt(void) +static char *strip_newline(char *str) { - if (isatty(STDIN_FILENO)) - return ("$ "); - return (""); + size_t last_char_idx; + + if (str == NULL) + return (NULL); + last_char_idx = ft_strlen(str) - 1; + if (str[last_char_idx] == '\n') + str[last_char_idx] = '\0'; + return (str); } /* @@ -40,7 +44,10 @@ static char *get_command(void) { char *line; - line = readline(get_prompt()); + if (isatty(STDIN_FILENO)) + line = readline("$ "); + else + line = strip_newline(get_next_line(STDIN_FILENO)); if (line != NULL && line[0] != '\0') add_history(line); return (line); @@ -56,7 +63,7 @@ int main(int argc, char *argv[], char **envp) line = get_command(); while (line != NULL) { - printf("%s\n", line); // FIXME + ft_printf("%s\n", line); free(line); line = get_command(); }