From 1d6bd2767572e600d175f774885f231ab8a367be Mon Sep 17 00:00:00 2001 From: joris schaft Date: Tue, 28 May 2024 14:31:04 +0200 Subject: [PATCH 1/4] [ADD] history --- include/include.h | 4 +++- src/main.c | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/include.h b/include/include.h index 1a015d2..261a55c 100644 --- a/include/include.h +++ b/include/include.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/05/16 18:18:16 by mcolonna ### ########.fr */ +/* Updated: 2024/05/31 13:45:38 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,8 @@ # include # include "libft.h" # include "libtf.h" +# include +# include ///// ASK COMMAND ///// diff --git a/src/main.c b/src/main.c index 8efcceb..559b26b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/05/17 14:39:43 by mcolonna ### ########.fr */ +/* Updated: 2024/05/31 13:46:33 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,6 +52,7 @@ int main(const int argc, const char *argv[], char *const envp[]) { mc = mem_subclass(fatal_error, g_mc); command_str = ask_command(mc); + add_history(command_str); do_command(envp, command_str); } return (errorstatus); From 04f27542099c6503283e74734fbb27d0f1d1ab62 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 31 May 2024 13:43:27 +0200 Subject: [PATCH 2/4] fix Makefile for history --- Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 794a0f3..7428d0c 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ LIBRARIES_FILES = libtf/libtf.a libft/libft.a # to use with the flags -L and -l LIBRARIES_LINK = # to use with the flag -l -LINK = +LINK = readline # flags to add to the compiler MORE_FLAGS += $(addprefix -L,$(LIBRARIES_LINK)) $(addprefix -l,$(LIBRARIES_LINK) $(LINK)) $(addprefix -I,$(INCLUDES)) ##### END OF THE INTERESTING PART ##### @@ -22,9 +22,9 @@ MORE_FLAGS += $(addprefix -L,$(LIBRARIES_LINK)) $(addprefix -l,$(LIBRARIES_LINK) C_FILES = $(addsuffix .c,$(addprefix $(SRCS),$(CODE))) O_FILES = $(addsuffix .o,$(addprefix $(SRCS),$(CODE))) ifdef DEBUG -CC = cc -Wall -Wextra -Werror -g $(MORE_FLAGS) +CC = cc -Wall -Wextra -Werror -g else -CC = cc -Wall -Wextra -Werror $(MORE_FLAGS) +CC = cc -Wall -Wextra -Werror endif @@ -53,7 +53,7 @@ debug : $(NAME) : $(O_FILES) $(LIBRARIES) @echo "\e[30;47;1m $(NAME): linking... \e[0m" - $(CC) -o $(NAME) $(O_FILES) $(LIBRARIES_FILES) + $(CC) -o $(NAME) $(O_FILES) $(LIBRARIES_FILES) $(MORE_FLAGS) @echo "\e[30;47;1m $(NAME): linked! \e[0m" $(LIBRARIES) : @@ -66,6 +66,6 @@ endif @echo "\e[30;47;1m $(NAME): library $@ made! \e[0m" %.o : %.c - $(CC) $(MORE_FLAGS) -c $< -o $@ + $(CC) -c $< -o $@ $(MORE_FLAGS) .PHONY : all debug clean fclean re $(LIBRARIES) From b103e605c3989b8679fa5b9eab901e034190a496 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 31 May 2024 13:52:57 +0200 Subject: [PATCH 3/4] fix history --- src/ask_command.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ask_command.c b/src/ask_command.c index 5b7719a..622c927 100644 --- a/src/ask_command.c +++ b/src/ask_command.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 15:03:23 by mcolonna #+# #+# */ -/* Updated: 2024/05/01 12:46:34 by mcolonna ### ########.fr */ +/* Updated: 2024/05/31 13:52:07 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,8 @@ const char *ask_command(const t_memclass mc) { const char *r; - print_str(fatal_error, 1, "\e[1m\e[38;5;45m( ^.^)> \e[0m"); - r = read_line(fatal_error, mc, 0); + (void)mc; + r = readline("\e[1m\e[38;5;45m( ^.^)> \e[0m"); if (r) return (r); else From d3fdd3bbf67805407145d0de1883f812371cf242 Mon Sep 17 00:00:00 2001 From: joris schaft Date: Mon, 3 Jun 2024 14:32:45 +0200 Subject: [PATCH 4/4] [ADD] cd builtin --- Makefile | 2 +- include/include.h | 10 ++++++++- src/builtin.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++ src/exec_command.c | 7 +++++-- src/path.c | 2 +- 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/builtin.c diff --git a/Makefile b/Makefile index 7428d0c..281e713 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRCS = src/ # include directory INCLUDES = include/ libtf/ libft/ # .c files in src/ without the extension -CODE = main ask_command error path parse_command exec_command +CODE = main ask_command error path parse_command exec_command builtin # directories to 'make' LIBRARIES = libtf libft # .a files to include diff --git a/include/include.h b/include/include.h index 261a55c..4616cbe 100644 --- a/include/include.h +++ b/include/include.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/05/31 13:45:38 by mcolonna ### ########.fr */ +/* Updated: 2024/06/03 13:52:11 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,6 +97,14 @@ const char **get_path(const t_memclass mc, char *const envp[]); const char *search_path( const t_memclass mc, const char **path, const char *prog); +///// BUILTIN ///// + +// Check if the command is a builtin, return 1 if yes, -1 if no +int is_builtin(const char *str); + +// Execute the builtin command +int exec_builtin(t_call call); + ///// MAIN ///// extern t_memclass g_mc; diff --git a/src/builtin.c b/src/builtin.c new file mode 100644 index 0000000..b9e17a5 --- /dev/null +++ b/src/builtin.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jschaft +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ +/* Updated: 2024/06/03 14:18:50 by jschaft ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +int is_builtin(const char *str) +{ + if (ft_strncmp(str, "cd", 3) == 0) + return (1); + if (ft_strncmp(str, "exit", 5) == 0) + return (1); + if (ft_strncmp(str, "export", 7) == 0) + return (1); + if (ft_strncmp(str, "unset", 6) == 0) + return (1); + return (-1); +} + +static int cd_builtin(const char *path) +{ + int r; + + r = chdir(path); + if (r) + { + minishell_error("cd: unavailable path"); + return (-1); + } + return (1); +} + +int exec_builtin(t_call call) +{ + if (ft_strncmp(call.program, "cd", 3) == 0) + return (cd_builtin((const char*)call.argv[1])); +/* if (ft_strncmp(call.program, "exit", 5) == 0) + exit_builtin(); + if (ft_strncmp(call.program, "export", 7) == 0) + return (export_builtin(call.argv)); + if (ft_strncmp(call.program, "unset", 6) == 0) + return (unset_builtin());*/ + return (1); +} \ No newline at end of file diff --git a/src/exec_command.c b/src/exec_command.c index a4adfe9..15e7928 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/05/17 14:43:37 by mcolonna ### ########.fr */ +/* Updated: 2024/06/03 14:01:02 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,7 +102,10 @@ static int exec_each_call(t_exec_command_global *global, { inout[0] = global->pipes[i].pipe[0]; inout[1] = global->pipes[i + 1].pipe[1]; - r = execute_call(global, command.calls[i], inout, envp); + if (is_builtin(command.calls[i].program) == 1) + r = exec_builtin(command.calls[i]); + else + r = execute_call(global, command.calls[i], inout, envp); if (r) return (r); } diff --git a/src/path.c b/src/path.c index 78f0660..ef5ee59 100644 --- a/src/path.c +++ b/src/path.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ -/* Updated: 2024/05/01 17:53:38 by mcolonna ### ########.fr */ +/* Updated: 2024/06/03 11:39:59 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */