From c0068a5ec78292e0527fb338f58009aa990a39c8 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Sun, 10 Oct 2021 08:57:33 +0200 Subject: [PATCH] wip builtins: env, exit --- Makefile | 6 +++-- headers/minishell.h | 3 ++- headers/minishell_prototypes.h | 8 ++++-- headers/minishell_structs.h | 3 ++- headers/minishell_user_macro.h | 20 +++++++++++++++ srcs/builtins/env.c | 21 ++++++++++++++++ srcs/builtins/exit.c | 46 ++++++++++++++++++++++++++++++++++ srcs/init.c | 11 ++++---- srcs/main.c | 16 +++++------- 9 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 headers/minishell_user_macro.h create mode 100644 srcs/builtins/env.c create mode 100644 srcs/builtins/exit.c diff --git a/Makefile b/Makefile index 462765e..b53fe46 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CC = clang CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -g VPATH = $(DIR_SRCS) -DIR_SRCS = srcs +DIR_SRCS = srcs srcs/builtins INCLUDES = -I$(HEADERS_D) -I$(LIBFT_D) @@ -13,6 +13,7 @@ HEADERS_D = ./headers HEADERS = minishell.h \ minishell_structs.h minishell_prototypes.h \ minishell_macro.h minishell_term_colors.h \ + minishell_user_macro.h LIBS = -L $(LIBFT_D) -lft \ -lreadline -ltermcap @@ -20,7 +21,8 @@ LIBS = -L $(LIBFT_D) -lft \ LIBFT_D = ./libft LIBFT = $(LIBFT_D)/libft.a -SRCS = main.c init.c generic.c +SRCS = main.c init.c generic.c \ + env.c exit.c DIR_OBJS = builds OBJS = $(SRCS:%.c=$(DIR_OBJS)/%.o) diff --git a/headers/minishell.h b/headers/minishell.h index 6e96b8a..31dbb1c 100644 --- a/headers/minishell.h +++ b/headers/minishell.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/03 19:14:46 by lperrey #+# #+# */ -/* Updated: 2021/10/08 03:04:26 by lperrey ### ########.fr */ +/* Updated: 2021/10/10 03:37:29 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ # include "minishell_structs.h" # include "minishell_macro.h" # include "minishell_term_colors.h" +# include "minishell_user_macro.h" # include "minishell_prototypes.h" /* diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index fed789d..3167dc4 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/10/08 09:28:29 by lperrey ### ########.fr */ +/* Updated: 2021/10/10 08:55:48 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,11 @@ # define MINISHELL_PROTOTYPES_H // Init -int init(t_all *c); +int init(t_all *c, char *envp[]); + +// Builtins +int builtin_env(int argc, char *argv[], t_all *c); +int builtin_exit(int argc, char *argv[], t_all *c); // Generic char *ft_strjoinfree(char *s1, char *s2); diff --git a/headers/minishell_structs.h b/headers/minishell_structs.h index 9116c43..6bb2bc6 100644 --- a/headers/minishell_structs.h +++ b/headers/minishell_structs.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */ -/* Updated: 2021/10/08 18:59:02 by lperrey ### ########.fr */ +/* Updated: 2021/10/10 05:39:09 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ typedef struct s_all { + char **envp; char *prompt_base; char *prompt; } t_all; diff --git a/headers/minishell_user_macro.h b/headers/minishell_user_macro.h new file mode 100644 index 0000000..877191b --- /dev/null +++ b/headers/minishell_user_macro.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* minishell_user_macro.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/10 03:36:37 by lperrey #+# #+# */ +/* Updated: 2021/10/10 04:55:46 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MINISHELL_USER_MACRO_H +# define MINISHELL_USER_MACRO_H + +# include "minishell_macro.h" + +# define USER_PROMPT PROMPT_EURO + +#endif diff --git a/srcs/builtins/env.c b/srcs/builtins/env.c new file mode 100644 index 0000000..0332bd7 --- /dev/null +++ b/srcs/builtins/env.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/10 05:01:26 by lperrey #+# #+# */ +/* Updated: 2021/10/10 07:37:29 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int builtin_env(int argc, char *argv[], t_all *c) // WIP +{ + (void)argc; + (void)argv; + ft_putendl_arr_fd(c->envp, 1); + return (0); +} diff --git a/srcs/builtins/exit.c b/srcs/builtins/exit.c new file mode 100644 index 0000000..ff88c1e --- /dev/null +++ b/srcs/builtins/exit.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lperrey +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2021/10/10 05:01:22 by lperrey #+# #+# */ +/* Updated: 2021/10/10 08:50:37 by lperrey ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int builtin_exit(int argc, char *argv[], t_all *c) // WIP +{ + unsigned char status; + int i; + + status = 0; + if (argc > 2) + return (ft_reti_print(1, "exit: too many arguments\n", 2)); + if (argc == 2) + { + i = 0; + while (argv[1][i]) + { + if ((argv[1][0] == '-' || argv[1][0] == '+') && argv[1][1] != '\0') + i++; + while (ft_isdigit(argv[1][i])) + i++; + if (argv[1][i] != '\0') + { + ft_putstr_fd("exit: ", 2); + ft_putstr_fd(argv[1], 2); + return (ft_reti_print(2, " numeric argument required\n", 2)); + } + } + status = ft_atoi(argv[1]); + } + // TODO : remplacer exit(status) par + (void)c; + exit(status); + return (0); + // return (free_exit(&c, status)); +} diff --git a/srcs/init.c b/srcs/init.c index 8f050dd..207d317 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ -/* Updated: 2021/10/08 19:09:12 by lperrey ### ########.fr */ +/* Updated: 2021/10/10 08:54:17 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,10 @@ static char *init_prompt_base(void); static char *init_prompt(char *prompt_base); -int init(t_all *c) +int init(t_all *c, char *envp[]) { ft_bzero(c, sizeof *c); + c->envp = envp; c->prompt_base = init_prompt_base(); if (!c->prompt_base) return (ft_reti_perror(0, "init_prompt_base() fail")); @@ -36,12 +37,12 @@ static char *init_prompt_base(void) if (!tmp) tmp = getenv("LOGNAME"); if (!tmp) - tmp = "NoName"; + tmp = "NoUser"; prompt_base = ft_strjoin(TERM_LIGHT_GREEN, tmp); prompt_base = ft_strjoinfree_s1(prompt_base, "@"); tmp = getenv("NAME"); if (!tmp) - tmp = "NoHost"; + tmp = "NoName"; prompt_base = ft_strjoinfree_s1(prompt_base, tmp); prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE); return (prompt_base); @@ -52,6 +53,6 @@ static char *init_prompt(char *prompt_base) char *prompt; prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0)); - prompt = ft_strjoinfree_s1(prompt, TERM_RESET PROMPT_EURO); + prompt = ft_strjoinfree_s1(prompt, TERM_RESET USER_PROMPT); return (prompt); } diff --git a/srcs/main.c b/srcs/main.c index 7e61322..34f6d74 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/08 19:11:03 by lperrey ### ########.fr */ +/* Updated: 2021/10/10 08:50:20 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,13 +24,13 @@ void shell_loop(t_all *c) line_input = readline(c->prompt); if (line_input && *line_input) { - if (!ft_strncmp(line_input, "exit", 5)) - exit(EXIT_SUCCESS); + if (!ft_strncmp(line_input, "env", 4)) // temp placeholder + builtin_env(0, NULL, c); + else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder + builtin_exit(0, NULL, c); else printf("echo: %s\n", line_input); } - //rl_redisplay(); - //rl_on_new_line(); } } @@ -63,13 +63,9 @@ int main(int argc, char *argv[], char *envp[]) { t_all c; - ft_putendl_arr_fd(envp, 1); - //char *path = getenv("PATH"); (void)argc; (void)argv; - (void)envp; - - if (!init(&c)) + if (!init(&c, envp)) exit(EXIT_FAILURE); shell_loop(&c); return (0);