diff --git a/Makefile b/Makefile index 794a0f3..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 @@ -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) diff --git a/dev/TODO b/dev/TODO index d1c50ca..c96d23e 100644 --- a/dev/TODO +++ b/dev/TODO @@ -19,5 +19,8 @@ - fix - `$ |` throws 2 errors - empty variables aren't freed + - need to free value returned by readline + - the builtins should get all arguments and check if they're good (also search TODO and FIXME in the files) +(also check if it works at the cluster (might need libreadline-dev)) diff --git a/include/include.h b/include/include.h index f2c372f..1ac41be 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/06/05 17:45:33 by mcolonna ### ########.fr */ +/* Updated: 2024/06/06 16:36:19 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,8 @@ # include # include "libft.h" # include "libtf.h" +# include +# include ///// ASK COMMAND ///// @@ -102,6 +104,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. +bool 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/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 diff --git a/src/builtin.c b/src/builtin.c new file mode 100644 index 0000000..11fdbdb --- /dev/null +++ b/src/builtin.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jschaft +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ +/* Updated: 2024/06/06 16:34:51 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +// Check if str is the name of a builtin. +// Return true if it is, false if not. +bool is_builtin(const char *str) +{ + return ( + ft_strncmp(str, "cd", 3) == 0 + || ft_strncmp(str, "exit", 5) == 0 + || ft_strncmp(str, "export", 7) == 0 + || ft_strncmp(str, "unset", 6) == 0); +} + +// Execute the builtin cd for the specified path. +// Return 0 if success, errno if error. +static int cd_builtin(const char *path) +{ + int r; + + r = chdir(path); + if (r) + { + perror("cd"); + return (errno); + } + return (0); +} + +// Execute the specified builtin. +// The builtin must exist. +// Return the error value returned by the builtin. +// TODO all the builtins +int exec_builtin(t_call call) +{ + if (ft_strncmp(call.program, "cd", 3) == 0) + return (cd_builtin(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); +} diff --git a/src/exec_command.c b/src/exec_command.c index a4adfe9..d14bc1a 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/06 16:31:21 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,14 +102,17 @@ 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)) + r = exec_builtin(command.calls[i]); + else + r = execute_call(global, command.calls[i], inout, envp); if (r) return (r); } return (r); } -// TODO line jump if command not found +// FIXME line jump if command not found int execute_command(t_memclass mc, t_command command, char *const envp[]) { t_exec_command_global global; diff --git a/src/main.c b/src/main.c index 86bebf5..cf3523a 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/06/05 17:45:43 by mcolonna ### ########.fr */ +/* Updated: 2024/06/06 16:21:46 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,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, &variables, command_str); } return (errorstatus);