mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
get_command: if input is not a tty, use get next line
This simplifies tests
This commit is contained in:
parent
dff68d5a8b
commit
486dec7bb7
2 changed files with 19 additions and 14 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue