get_command: if input is not a tty, use get next line

This simplifies tests
This commit is contained in:
Khaïs COLIN 2025-02-19 17:02:25 +01:00
parent dff68d5a8b
commit 486dec7bb7
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 19 additions and 14 deletions

View file

@ -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

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#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();
}