Merge manage variables with builtins & history

This commit is contained in:
mcolonna 2024-06-06 16:37:28 +02:00
commit 44daaaf097
7 changed files with 87 additions and 14 deletions

View file

@ -5,7 +5,7 @@ SRCS = src/
# include directory # include directory
INCLUDES = include/ libtf/ libft/ INCLUDES = include/ libtf/ libft/
# .c files in src/ without the extension # .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' # directories to 'make'
LIBRARIES = libtf libft LIBRARIES = libtf libft
# .a files to include # .a files to include
@ -13,7 +13,7 @@ LIBRARIES_FILES = libtf/libtf.a libft/libft.a
# to use with the flags -L and -l # to use with the flags -L and -l
LIBRARIES_LINK = LIBRARIES_LINK =
# to use with the flag -l # to use with the flag -l
LINK = LINK = readline
# flags to add to the compiler # flags to add to the compiler
MORE_FLAGS += $(addprefix -L,$(LIBRARIES_LINK)) $(addprefix -l,$(LIBRARIES_LINK) $(LINK)) $(addprefix -I,$(INCLUDES)) MORE_FLAGS += $(addprefix -L,$(LIBRARIES_LINK)) $(addprefix -l,$(LIBRARIES_LINK) $(LINK)) $(addprefix -I,$(INCLUDES))
##### END OF THE INTERESTING PART ##### ##### 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))) C_FILES = $(addsuffix .c,$(addprefix $(SRCS),$(CODE)))
O_FILES = $(addsuffix .o,$(addprefix $(SRCS),$(CODE))) O_FILES = $(addsuffix .o,$(addprefix $(SRCS),$(CODE)))
ifdef DEBUG ifdef DEBUG
CC = cc -Wall -Wextra -Werror -g $(MORE_FLAGS) CC = cc -Wall -Wextra -Werror -g
else else
CC = cc -Wall -Wextra -Werror $(MORE_FLAGS) CC = cc -Wall -Wextra -Werror
endif endif
@ -53,7 +53,7 @@ debug :
$(NAME) : $(O_FILES) $(LIBRARIES) $(NAME) : $(O_FILES) $(LIBRARIES)
@echo "\e[30;47;1m $(NAME): linking... \e[0m" @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" @echo "\e[30;47;1m $(NAME): linked! \e[0m"
$(LIBRARIES) : $(LIBRARIES) :
@ -66,6 +66,6 @@ endif
@echo "\e[30;47;1m $(NAME): library $@ made! \e[0m" @echo "\e[30;47;1m $(NAME): library $@ made! \e[0m"
%.o : %.c %.o : %.c
$(CC) $(MORE_FLAGS) -c $< -o $@ $(CC) -c $< -o $@ $(MORE_FLAGS)
.PHONY : all debug clean fclean re $(LIBRARIES) .PHONY : all debug clean fclean re $(LIBRARIES)

View file

@ -19,5 +19,8 @@
- fix - fix
- `$ |` throws 2 errors - `$ |` throws 2 errors
- empty variables aren't freed - 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 search TODO and FIXME in the files)
(also check if it works at the cluster (might need libreadline-dev))

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ /* 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 <sys/wait.h> # include <sys/wait.h>
# include "libft.h" # include "libft.h"
# include "libtf.h" # include "libtf.h"
# include <readline/history.h>
# include <readline/readline.h>
///// ASK COMMAND ///// ///// ASK COMMAND /////
@ -102,6 +104,14 @@ const char **get_path(const t_memclass mc, char *const envp[]);
const char *search_path( const char *search_path(
const t_memclass mc, const char **path, const char *prog); 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 ///// ///// MAIN /////
extern t_memclass g_mc; extern t_memclass g_mc;

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 15:03:23 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; const char *r;
print_str(fatal_error, 1, "\e[1m\e[38;5;45m( ^.^)> \e[0m"); (void)mc;
r = read_line(fatal_error, mc, 0); r = readline("\e[1m\e[38;5;45m( ^.^)> \e[0m");
if (r) if (r)
return (r); return (r);
else else

56
src/builtin.c Normal file
View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 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[0] = global->pipes[i].pipe[0];
inout[1] = global->pipes[i + 1].pipe[1]; 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) if (r)
return (r); return (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[]) int execute_command(t_memclass mc, t_command command, char *const envp[])
{ {
t_exec_command_global global; t_exec_command_global global;

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ /* 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); mc = mem_subclass(fatal_error, g_mc);
command_str = ask_command(mc); command_str = ask_command(mc);
add_history(command_str);
do_command(envp, &variables, command_str); do_command(envp, &variables, command_str);
} }
return (errorstatus); return (errorstatus);