diff --git a/Makefile b/Makefile index 8202be5..cd543f6 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ srcs = \ src/env/env_manip.c \ src/env/envp.c \ src/ft_errno.c \ + src/get_command.c \ src/parser/matchers/identifier.c \ src/parser/matchers/metacharacter.c \ diff --git a/src/get_command.c b/src/get_command.c new file mode 100644 index 0000000..a6aea96 --- /dev/null +++ b/src/get_command.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_command.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 18:03:11 by khais #+# #+# */ +/* Updated: 2025/02/19 18:04:06 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +// stdio must be included before readline +#include +#include +#include +#include +#include +#include +#include "libft.h" + +/* +** remove one '\n' from the end of the string, if it exists +*/ +static char *strip_newline(char *str) +{ + 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); +} + +/* +** get a command line using readline. +** +** returned buffer must be freed by caller. +** will add command to history if appropriate. +*/ +char *get_command(void) +{ + char *line; + + 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); +} diff --git a/src/get_command.h b/src/get_command.h new file mode 100644 index 0000000..0772ecd --- /dev/null +++ b/src/get_command.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_command.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 18:02:19 by khais #+# #+# */ +/* Updated: 2025/02/19 18:03:06 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_COMMAND_H +# define GET_COMMAND_H + +char *get_command(void); + +#endif diff --git a/src/minishell.c b/src/minishell.c index ebba968..2b44091 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -6,52 +6,12 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ -/* Updated: 2025/02/19 17:02:17 by khais ### ########.fr */ +/* Updated: 2025/02/19 18:03:54 by khais ### ########.fr */ /* */ /* ************************************************************************** */ -// stdio must be included before readline -#include -#include -#include -#include -#include -#include #include "libft.h" - -/* -** remove one '\n' from the end of the string, if it exists -*/ -static char *strip_newline(char *str) -{ - 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); -} - -/* -** get a command line using readline. -** -** returned buffer must be freed by caller. -** will add command to history if appropriate. -*/ -static char *get_command(void) -{ - char *line; - - 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); -} +#include "get_command.h" int main(int argc, char *argv[], char **envp) {