From 487a66394061f2d14a2fa421302966b5442d643f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 27 Jul 2020 18:55:07 +0200 Subject: I couldn't see shit --- src/b_builtins.h | 25 ++++++++ src/b_cd.c | 73 +++++++++++++++++++++ src/b_cd.h | 23 +++++++ src/b_echo.c | 116 +++++++++++++++++++++++++++++++++ src/b_echo.h | 23 +++++++ src/b_env.c | 37 +++++++++++ src/b_env.h | 23 +++++++ src/b_exit.c | 43 +++++++++++++ src/b_exit.h | 24 +++++++ src/b_export.c | 80 +++++++++++++++++++++++ src/b_export.h | 22 +++++++ src/b_pwd.c | 25 ++++++++ src/b_pwd.h | 23 +++++++ src/b_type.c | 120 ++++++++++++++++++++++++++++++++++ src/b_type.h | 23 +++++++ src/b_unset.c | 25 ++++++++ src/b_unset.h | 23 +++++++ src/d_define.h | 49 ++++++++++++++ src/d_enum.h | 41 ++++++++++++ src/e_builtins.c | 89 ++++++++++++++++++++++++++ src/e_builtins.h | 24 +++++++ src/e_externs.c | 79 +++++++++++++++++++++++ src/e_externs.h | 21 ++++++ src/e_externs_next.c | 105 ++++++++++++++++++++++++++++++ src/e_externs_next.h | 23 +++++++ src/e_externs_pipes.c | 147 ++++++++++++++++++++++++++++++++++++++++++ src/e_externs_pipes.h | 20 ++++++ src/e_lcom.c | 59 +++++++++++++++++ src/e_lcom.h | 21 ++++++ src/e_pipes.c | 49 ++++++++++++++ src/e_pipes.h | 20 ++++++ src/f_chdir.c | 27 ++++++++ src/f_chdir.h | 22 +++++++ src/f_errno.c | 26 ++++++++ src/f_errno.h | 21 ++++++ src/f_fail.c | 58 +++++++++++++++++ src/f_fail.h | 30 +++++++++ src/f_redir.c | 31 +++++++++ src/f_redir.h | 21 ++++++ src/ft_b_builtins.h | 25 -------- src/ft_b_cd.c | 73 --------------------- src/ft_b_cd.h | 22 ------- src/ft_b_echo.c | 116 --------------------------------- src/ft_b_echo.h | 22 ------- src/ft_b_env.c | 36 ----------- src/ft_b_env.h | 22 ------- src/ft_b_exit.c | 43 ------------- src/ft_b_exit.h | 22 ------- src/ft_b_export.c | 79 ----------------------- src/ft_b_export.h | 20 ------ src/ft_b_pwd.c | 25 -------- src/ft_b_pwd.h | 22 ------- src/ft_b_type.c | 120 ---------------------------------- src/ft_b_type.h | 22 ------- src/ft_b_unset.c | 24 ------- src/ft_b_unset.h | 22 ------- src/ft_d_define.h | 49 -------------- src/ft_d_enum.h | 41 ------------ src/ft_e_builtins.c | 89 -------------------------- src/ft_e_builtins.h | 23 ------- src/ft_e_externs.c | 79 ----------------------- src/ft_e_externs.h | 21 ------ src/ft_e_externs_next.c | 105 ------------------------------ src/ft_e_externs_next.h | 23 ------- src/ft_e_externs_pipes.c | 147 ------------------------------------------ src/ft_e_externs_pipes.h | 20 ------ src/ft_e_lcom.c | 59 ----------------- src/ft_e_lcom.h | 21 ------ src/ft_e_pipes.c | 49 -------------- src/ft_e_pipes.h | 20 ------ src/ft_f_chdir.c | 27 -------- src/ft_f_chdir.h | 22 ------- src/ft_f_errno.c | 26 -------- src/ft_f_errno.h | 21 ------ src/ft_f_fail.c | 58 ----------------- src/ft_f_fail.h | 30 --------- src/ft_f_redir.c | 31 --------- src/ft_f_redir.h | 21 ------ src/ft_m_argv.c | 43 ------------- src/ft_m_argv.h | 24 ------- src/ft_m_comm.c | 38 ----------- src/ft_m_comm.h | 23 ------- src/ft_m_funptr.c | 37 ----------- src/ft_m_funptr.h | 20 ------ src/ft_m_loop.c | 126 ------------------------------------ src/ft_m_loop.h | 22 ------- src/ft_m_prompt.c | 30 --------- src/ft_m_prompt.h | 20 ------ src/ft_m_redirs.c | 51 --------------- src/ft_m_redirs.h | 21 ------ src/ft_p_lcom.c | 158 --------------------------------------------- src/ft_p_lcom.h | 26 -------- src/ft_p_lcom_next.c | 97 ---------------------------- src/ft_p_lcom_next.h | 25 -------- src/ft_p_line.c | 50 --------------- src/ft_p_line.h | 21 ------ src/ft_s_destroy.c | 28 -------- src/ft_s_destroy.h | 20 ------ src/ft_s_init.c | 85 ------------------------ src/ft_s_init.h | 22 ------- src/ft_s_lcom.c | 145 ----------------------------------------- src/ft_s_lcom.h | 23 ------- src/ft_s_lpipes.c | 105 ------------------------------ src/ft_s_lpipes.h | 25 -------- src/ft_s_lvars.c | 126 ------------------------------------ src/ft_s_lvars.h | 29 --------- src/ft_s_struct.h | 68 -------------------- src/ft_u_utils.c | 28 -------- src/ft_u_utils.h | 20 ------ src/ft_u_vars.c | 163 ----------------------------------------------- src/ft_u_vars.h | 21 ------ src/m_argv.c | 43 +++++++++++++ src/m_argv.h | 24 +++++++ src/m_comm.c | 38 +++++++++++ src/m_comm.h | 23 +++++++ src/m_funptr.c | 37 +++++++++++ src/m_funptr.h | 20 ++++++ src/m_loop.c | 126 ++++++++++++++++++++++++++++++++++++ src/m_loop.h | 22 +++++++ src/m_minishell.c | 60 +++++++++++++++++ src/m_minishell.h | 16 +++++ src/m_prompt.c | 30 +++++++++ src/m_prompt.h | 20 ++++++ src/m_redirs.c | 51 +++++++++++++++ src/m_redirs.h | 21 ++++++ src/minishell.c | 61 ------------------ src/minishell.h | 16 ----- src/p_lcom.c | 158 +++++++++++++++++++++++++++++++++++++++++++++ src/p_lcom.h | 26 ++++++++ src/p_lcom_next.c | 97 ++++++++++++++++++++++++++++ src/p_lcom_next.h | 25 ++++++++ src/p_line.c | 50 +++++++++++++++ src/p_line.h | 21 ++++++ src/s_destroy.c | 28 ++++++++ src/s_destroy.h | 20 ++++++ src/s_init.c | 85 ++++++++++++++++++++++++ src/s_init.h | 23 +++++++ src/s_lcom.c | 145 +++++++++++++++++++++++++++++++++++++++++ src/s_lcom.h | 23 +++++++ src/s_lpipes.c | 105 ++++++++++++++++++++++++++++++ src/s_lpipes.h | 25 ++++++++ src/s_lvars.c | 126 ++++++++++++++++++++++++++++++++++++ src/s_lvars.h | 29 +++++++++ src/s_struct.h | 68 ++++++++++++++++++++ src/u_utils.c | 28 ++++++++ src/u_utils.h | 20 ++++++ src/u_vars.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++ src/u_vars.h | 21 ++++++ 148 files changed, 3528 insertions(+), 3514 deletions(-) create mode 100644 src/b_builtins.h create mode 100644 src/b_cd.c create mode 100644 src/b_cd.h create mode 100644 src/b_echo.c create mode 100644 src/b_echo.h create mode 100644 src/b_env.c create mode 100644 src/b_env.h create mode 100644 src/b_exit.c create mode 100644 src/b_exit.h create mode 100644 src/b_export.c create mode 100644 src/b_export.h create mode 100644 src/b_pwd.c create mode 100644 src/b_pwd.h create mode 100644 src/b_type.c create mode 100644 src/b_type.h create mode 100644 src/b_unset.c create mode 100644 src/b_unset.h create mode 100644 src/d_define.h create mode 100644 src/d_enum.h create mode 100644 src/e_builtins.c create mode 100644 src/e_builtins.h create mode 100644 src/e_externs.c create mode 100644 src/e_externs.h create mode 100644 src/e_externs_next.c create mode 100644 src/e_externs_next.h create mode 100644 src/e_externs_pipes.c create mode 100644 src/e_externs_pipes.h create mode 100644 src/e_lcom.c create mode 100644 src/e_lcom.h create mode 100644 src/e_pipes.c create mode 100644 src/e_pipes.h create mode 100644 src/f_chdir.c create mode 100644 src/f_chdir.h create mode 100644 src/f_errno.c create mode 100644 src/f_errno.h create mode 100644 src/f_fail.c create mode 100644 src/f_fail.h create mode 100644 src/f_redir.c create mode 100644 src/f_redir.h delete mode 100644 src/ft_b_builtins.h delete mode 100644 src/ft_b_cd.c delete mode 100644 src/ft_b_cd.h delete mode 100644 src/ft_b_echo.c delete mode 100644 src/ft_b_echo.h delete mode 100644 src/ft_b_env.c delete mode 100644 src/ft_b_env.h delete mode 100644 src/ft_b_exit.c delete mode 100644 src/ft_b_exit.h delete mode 100644 src/ft_b_export.c delete mode 100644 src/ft_b_export.h delete mode 100644 src/ft_b_pwd.c delete mode 100644 src/ft_b_pwd.h delete mode 100644 src/ft_b_type.c delete mode 100644 src/ft_b_type.h delete mode 100644 src/ft_b_unset.c delete mode 100644 src/ft_b_unset.h delete mode 100644 src/ft_d_define.h delete mode 100644 src/ft_d_enum.h delete mode 100644 src/ft_e_builtins.c delete mode 100644 src/ft_e_builtins.h delete mode 100644 src/ft_e_externs.c delete mode 100644 src/ft_e_externs.h delete mode 100644 src/ft_e_externs_next.c delete mode 100644 src/ft_e_externs_next.h delete mode 100644 src/ft_e_externs_pipes.c delete mode 100644 src/ft_e_externs_pipes.h delete mode 100644 src/ft_e_lcom.c delete mode 100644 src/ft_e_lcom.h delete mode 100644 src/ft_e_pipes.c delete mode 100644 src/ft_e_pipes.h delete mode 100644 src/ft_f_chdir.c delete mode 100644 src/ft_f_chdir.h delete mode 100644 src/ft_f_errno.c delete mode 100644 src/ft_f_errno.h delete mode 100644 src/ft_f_fail.c delete mode 100644 src/ft_f_fail.h delete mode 100644 src/ft_f_redir.c delete mode 100644 src/ft_f_redir.h delete mode 100644 src/ft_m_argv.c delete mode 100644 src/ft_m_argv.h delete mode 100644 src/ft_m_comm.c delete mode 100644 src/ft_m_comm.h delete mode 100644 src/ft_m_funptr.c delete mode 100644 src/ft_m_funptr.h delete mode 100644 src/ft_m_loop.c delete mode 100644 src/ft_m_loop.h delete mode 100644 src/ft_m_prompt.c delete mode 100644 src/ft_m_prompt.h delete mode 100644 src/ft_m_redirs.c delete mode 100644 src/ft_m_redirs.h delete mode 100644 src/ft_p_lcom.c delete mode 100644 src/ft_p_lcom.h delete mode 100644 src/ft_p_lcom_next.c delete mode 100644 src/ft_p_lcom_next.h delete mode 100644 src/ft_p_line.c delete mode 100644 src/ft_p_line.h delete mode 100644 src/ft_s_destroy.c delete mode 100644 src/ft_s_destroy.h delete mode 100644 src/ft_s_init.c delete mode 100644 src/ft_s_init.h delete mode 100644 src/ft_s_lcom.c delete mode 100644 src/ft_s_lcom.h delete mode 100644 src/ft_s_lpipes.c delete mode 100644 src/ft_s_lpipes.h delete mode 100644 src/ft_s_lvars.c delete mode 100644 src/ft_s_lvars.h delete mode 100644 src/ft_s_struct.h delete mode 100644 src/ft_u_utils.c delete mode 100644 src/ft_u_utils.h delete mode 100644 src/ft_u_vars.c delete mode 100644 src/ft_u_vars.h create mode 100644 src/m_argv.c create mode 100644 src/m_argv.h create mode 100644 src/m_comm.c create mode 100644 src/m_comm.h create mode 100644 src/m_funptr.c create mode 100644 src/m_funptr.h create mode 100644 src/m_loop.c create mode 100644 src/m_loop.h create mode 100644 src/m_minishell.c create mode 100644 src/m_minishell.h create mode 100644 src/m_prompt.c create mode 100644 src/m_prompt.h create mode 100644 src/m_redirs.c create mode 100644 src/m_redirs.h delete mode 100644 src/minishell.c delete mode 100644 src/minishell.h create mode 100644 src/p_lcom.c create mode 100644 src/p_lcom.h create mode 100644 src/p_lcom_next.c create mode 100644 src/p_lcom_next.h create mode 100644 src/p_line.c create mode 100644 src/p_line.h create mode 100644 src/s_destroy.c create mode 100644 src/s_destroy.h create mode 100644 src/s_init.c create mode 100644 src/s_init.h create mode 100644 src/s_lcom.c create mode 100644 src/s_lcom.h create mode 100644 src/s_lpipes.c create mode 100644 src/s_lpipes.h create mode 100644 src/s_lvars.c create mode 100644 src/s_lvars.h create mode 100644 src/s_struct.h create mode 100644 src/u_utils.c create mode 100644 src/u_utils.h create mode 100644 src/u_vars.c create mode 100644 src/u_vars.h (limited to 'src') diff --git a/src/b_builtins.h b/src/b_builtins.h new file mode 100644 index 0000000..f7e7e99 --- /dev/null +++ b/src/b_builtins.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_builtins.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_BUILTINS_H +#define B_BUILTINS_H + +#include "b_cd.h" +#include "b_echo.h" +#include "b_env.h" +#include "b_exit.h" +#include "b_export.h" +#include "b_pwd.h" +#include "b_type.h" +#include "b_unset.h" + +#endif diff --git a/src/b_cd.c b/src/b_cd.c new file mode 100644 index 0000000..bcc8475 --- /dev/null +++ b/src/b_cd.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "f_fail.h" +#include "s_destroy.h" +#include "s_struct.h" +#include "u_utils.h" +#include "u_vars.h" + +static void + set_path(char **path, + char *args[], + t_msh *msh) +{ + if (!(*path = ft_strdup(*args))) + { + s_destroy(msh); + fail_alloc(msh); + } + if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) + { + if (!(*path = ft_strsubst(*path, + "~", subst_var_value("$HOME", msh)))) + { + s_destroy(msh); + fail_alloc(msh); + } + } +} + +uint8_t + b_cd(char *args[], + t_msh *msh) +{ + const uint64_t argc = get_argc((const char**)args); + char *path; + + if (argc >= 2) + { + fail_too_many_args("cd", msh); + return (1); + } + else if (argc == 0) + { + if (!(path = subst_var_value("$HOME", msh))) + return (1); + } + else + set_path(&path, args, msh); + if (chdir(path) != 0) + { + fail_chd("cd", path, msh); + ft_memdel((void*)&path); + return (1); + } + ft_memdel((void*)&msh->cwd); + msh->cwd = getcwd(NULL, 0); + ft_memdel((void*)&path); + return (0); +} diff --git a/src/b_cd.h b/src/b_cd.h new file mode 100644 index 0000000..629cf32 --- /dev/null +++ b/src/b_cd.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_cd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_CD_H +#define B_CD_H + +#include + +#include "s_struct.h" + +uint8_t b_cd(char *args[], + t_msh *msh); + +#endif diff --git a/src/b_echo.c b/src/b_echo.c new file mode 100644 index 0000000..a02dad5 --- /dev/null +++ b/src/b_echo.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_echo.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "s_struct.h" +#include "u_utils.h" + +/* TODO: echo "quoted text", echo 'quoted text', done*/ +/* echo kill\nbackslash\nbut\nnot\nn, echo "quoted\nnew\nlines", done */ +/* full buffer */ + +void + e_put(char *str, + uint8_t op) +{ + size_t i; + + i = -1; + if (op == 0) + while (str[++i]) + { + if (str[i] == '\\') + i++; + ft_putchar(str[i]); + } +} + +char + *e_initb(char *ptr[], + char *str) +{ + size_t len; + uint8_t i; + + len = 0; + i = -1; + while (ptr[++i]) + { + len += ft_strlen(ptr[i]); + } + if (!(str = (char*)malloc(len * sizeof(char)))) + return (NULL); + return (str); +} + +/***********************************************/ +/* void */ +/* e_fill(char *ptr[], char **str) */ +/* { */ +/* char *bs; */ +/* */ +/* ft_sprintf(*str, "%s", *ptr); */ +/* if (*str[0] == '\"' || *str[0] == '\'') */ +/* { */ +/* *str = ft_strtrim(*str, "\"\'"); */ +/* ft_printf("%s", *str); */ +/* } */ +/* else if ((bs = ft_strrchr(*str, '\\'))) */ +/* { */ +/* e_put(*str, 0); */ +/* } */ +/* else */ +/* ft_printf("%s", *str); */ +/* } */ +/***********************************************/ + +uint8_t + b_echo(char *args[], + t_msh *msh) +{ + const uint64_t argc = get_argc((const char **)args); + char **ptr; + char *str; + int8_t nopt; + + (void)msh; + ptr = args; + nopt = 0; + str = NULL; + str = e_initb(ptr, str); + if (argc >= 1) + { + if (ft_strncmp(ptr[0], "-n", 2) == 0) + { + nopt = 1; + ptr += 1; + } + if (argc - nopt >= 1) + { + ft_printf("%s", *ptr); + ptr++; + while (*ptr) + { + ft_printf(" %s", *ptr); + ptr++; + } + } + } + free(str); + if (nopt == 0) + ft_printf("\n"); + return (0); +} diff --git a/src/b_echo.h b/src/b_echo.h new file mode 100644 index 0000000..3481480 --- /dev/null +++ b/src/b_echo.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_echo.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_ECHO_H +#define B_ECHO_H + +#include + +#include "s_struct.h" + +uint8_t b_echo(char *args[], + t_msh *msh); + +#endif diff --git a/src/b_env.c b/src/b_env.c new file mode 100644 index 0000000..08adaf5 --- /dev/null +++ b/src/b_env.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "f_fail.h" +#include "s_struct.h" + +uint8_t + b_env(char *args[], + t_msh *msh) +{ + char **ptr; + + if (args && args[0]) + { + fail_no_options("env", msh); + return (127); + } + ptr = msh->envp; + while (*ptr) + { + ft_printf("%s\n", *ptr); + ptr++; + } + return (0); +} diff --git a/src/b_env.h b/src/b_env.h new file mode 100644 index 0000000..bd338ae --- /dev/null +++ b/src/b_env.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_env.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_ENV_H +#define B_ENV_H + +#include + +#include "s_struct.h" + +uint8_t b_env(char *args[], + t_msh *msh); + +#endif diff --git a/src/b_exit.c b/src/b_exit.c new file mode 100644 index 0000000..75f16bf --- /dev/null +++ b/src/b_exit.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_exit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "f_fail.h" +#include "s_lcom.h" +#include "s_destroy.h" +#include "s_struct.h" +#include "u_utils.h" + +uint8_t + b_exit(char *args[], + t_msh *msh) +{ + uint8_t ret; + const uint64_t argc = get_argc((const char**)args); + + if (argc > 1) + { + fail_too_many_args("exit", msh); + return (1); + } + if (argc == 1) + { + ret = ft_atoi(args[0]); + /* TODO: non numeric args[0] */ + } + ft_dprintf(STDERR_FILENO, "exit\n"); + return (0); +} diff --git a/src/b_exit.h b/src/b_exit.h new file mode 100644 index 0000000..d437e6a --- /dev/null +++ b/src/b_exit.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_exit.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_EXIT_H +#define B_EXIT_H + +#include + +#include "s_struct.h" + +uint8_t + b_exit(char *args[], + t_msh *msh); + +#endif diff --git a/src/b_export.c b/src/b_export.c new file mode 100644 index 0000000..5e7ee14 --- /dev/null +++ b/src/b_export.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_export.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "b_env.h" +#include "f_fail.h" +#include "s_struct.h" +#include "u_utils.h" + +static int8_t + check_valid_identifier(const char *arg) +{ + char *ptr; + + ptr = (char*)arg; + if (ft_isalpha(ptr[0])) + { + return (1); + } + return (0); +} + +static int8_t + check_equals(const char *arg) +{ + char *ptr; + + ptr = (char*)arg; + while (*ptr) + { + if (*ptr == '=') + return (1); + ptr++; + } + return (0); +} + +uint8_t + b_export(char *args[], + t_msh *msh) +{ + const uint64_t argc = get_argc((const char**)args); + char **ptr; + int8_t next; + uint8_t r; + + if (argc == 0) + { + return (b_env(NULL, msh)); + } + ptr = args; + r = 0; + while (*ptr) + { + next = 0; + if (!check_valid_identifier(*ptr)) + { + fail_identifier("export", *ptr, msh); + next = 1; + r = 1; + } + if (next == 0 && !check_equals(*ptr)) + next = 1; + ptr++; + } + /* TODO: finish export */ + return (r); +} diff --git a/src/b_export.h b/src/b_export.h new file mode 100644 index 0000000..037f9d8 --- /dev/null +++ b/src/b_export.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_export.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_EXPORT_H +#define B_EXPORT_H + +#include "s_struct.h" + +uint8_t + b_export(char *args[], + t_msh *msh); + +#endif diff --git a/src/b_pwd.c b/src/b_pwd.c new file mode 100644 index 0000000..78ef128 --- /dev/null +++ b/src/b_pwd.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_pwd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "s_struct.h" + +uint8_t + b_pwd(char *args[], + t_msh *msh) +{ + (void)args; + ft_printf("%s\n", msh->cwd); + return (0); +} diff --git a/src/b_pwd.h b/src/b_pwd.h new file mode 100644 index 0000000..e165284 --- /dev/null +++ b/src/b_pwd.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_pwd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_PWD_H +#define B_PWD_H + +#include + +#include "s_struct.h" + +uint8_t b_pwd(char *args[], + t_msh *msh); + +#endif diff --git a/src/b_type.c b/src/b_type.c new file mode 100644 index 0000000..ccaca43 --- /dev/null +++ b/src/b_type.c @@ -0,0 +1,120 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_type.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "f_fail.h" +#include "s_lcom.h" +#include "s_struct.h" +#include "e_externs_next.h" +#include "u_utils.h" + +static int8_t + absolute_path_exists(char com[]) +{ + int32_t fd; + DIR *dir; + + if ((dir = opendir(com)) != NULL) + { + closedir(dir); + return (0); + } + if ((fd = open(com, O_RDONLY)) != -1) + { + close(fd); + return (1); + } + return (0); +} + +static char + *type_get_path(char com[], + t_msh *msh) +{ + char **envpath; + char *fullpath; + + envpath = NULL; + fullpath = NULL; + if (ft_ischarset("/.", com[0])) + { + if (absolute_path_exists(com)) + { + if (!(fullpath = ft_strdup(com))) + { + lcom_clear(&msh->curr); + fail_alloc(msh); + } + return (fullpath); + } + return (NULL); + } + else if ((envpath = get_env_path(msh)) != NULL) + { + fullpath = search_in_path(com, envpath, msh); + ft_delwords(envpath); + } + return (fullpath); +} + +static uint8_t + chk_nonbuilt(char **ptr, + t_msh *msh) +{ + char *fullpath; + int32_t ret; + + ret = 0; + fullpath = type_get_path(*ptr, msh); + if (fullpath) + ft_printf("%s is %s\n", *ptr, fullpath); + else + { + ft_printf("minishell: type: %s: not found\n", *ptr); + ret = 1; + } + ft_memdel((void*)&fullpath); + return (ret); +} + +uint8_t + b_type(char *args[], + t_msh *msh) +{ + char **ptr; + char **p_bu; + int32_t ret; + + ptr = args; + if (!*ptr) + return (0); + ret = 0; + while (*ptr) + { + p_bu = msh->bu_ref; + while (*p_bu && ft_strncmp(*ptr, *p_bu, ft_strlen(*p_bu) + 1)) + p_bu++; + if (*p_bu != NULL) + ft_printf("%s is a shell builtin\n", *ptr); + else + { + ret = chk_nonbuilt(ptr, msh); + } + ptr++; + } + return (ret); +} diff --git a/src/b_type.h b/src/b_type.h new file mode 100644 index 0000000..5828e8d --- /dev/null +++ b/src/b_type.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_type.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_TYPE_H +#define B_TYPE_H + +#include + +#include "s_struct.h" + +uint8_t b_type(char *args[], + t_msh *msh); + +#endif diff --git a/src/b_unset.c b/src/b_unset.c new file mode 100644 index 0000000..92db749 --- /dev/null +++ b/src/b_unset.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_unset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#include "s_struct.h" + +uint8_t + b_unset(char *args[], + t_msh *msh) +{ + (void)args; + (void)msh; + /* TODO: do unset */ + return (0); +} diff --git a/src/b_unset.h b/src/b_unset.h new file mode 100644 index 0000000..24e17f7 --- /dev/null +++ b/src/b_unset.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_unset.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_UNSET_H +#define B_UNSET_H + +#include + +#include "s_struct.h" + +uint8_t b_unset(char *args[], + t_msh *msh); + +#endif diff --git a/src/d_define.h b/src/d_define.h new file mode 100644 index 0000000..175e256 --- /dev/null +++ b/src/d_define.h @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* d_define.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef D_DEFINE_H +#define D_DEFINE_H + +#include "d_enum.h" + +/* +** ====== CLASSICS ====== +*/ + +#define FT_PS_ONE "minishell ~> " +#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type" +#define FT_BUILTINS_COUNT 8 +#define FT_HISTFILE "minishell_history" + +/* +** ====== OPTIONS ====== +*/ + +#define FT_OPT_INTERACT "-i" +#define FT_OPT_COMMAND "-c" + +/* +** ====== VARS ====== +*/ + +#define FT_ZER_VAR "$0" +#define FT_RET_VAR "$?" +#define FT_PID_VAR "$$" + +/* +** ====== FAIL MSG ====== +*/ + +#define FT_FAIL_NO_OPTIONS "no options required" +#define FT_FAIL_TOO_MANY_ARGS "too many arguments" + +#endif diff --git a/src/d_enum.h b/src/d_enum.h new file mode 100644 index 0000000..427e0a0 --- /dev/null +++ b/src/d_enum.h @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* d_enum.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef D_ENUM_H +#define D_ENUM_H + +/* +** ret vals: +** --------- +** 0: cool +** 1: alloc err +*/ + +typedef enum +{ + FALSE, + TRUE +} t_bool; + +enum +{ + FT_RET_FINE, + FT_RET_ALLOC +}; + +enum +{ + FT_WRITE_END, + FT_READ_END +}; + +#endif diff --git a/src/e_builtins.c b/src/e_builtins.c new file mode 100644 index 0000000..e35416a --- /dev/null +++ b/src/e_builtins.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_builtins.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "m_redirs.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" + +static void + e_builtin_child(const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh) +{ + int32_t ret; + + dup_redirs(ptr, msh); + ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); + lcom_clear(&msh->curr); + s_destroy(msh); + exit(ret); +} + +static void + e_builtin_parent(pid_t pid, + const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh) +{ + int32_t status; + int32_t ret; + + while (wait(&status) != pid) + ; + ret = WEXITSTATUS(status); + if (bu_id != 6) + msh->ret = ret; + if (bu_id == 1 && msh->ret == 0) + { + msh->bu_ptr[bu_id](ptr->argv + 1, msh); + /* TODO: export $PWD */ + } + else if (bu_id == 6 && ret == 0) + { + if (ptr->argv[1]) + ret = ft_atoi(ptr->argv[1]); + else + ret = msh->ret; + lcom_clear(&msh->curr); + s_destroy(msh); + exit(ret); + } +} + +void + e_builtin(const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh) +{ + pid_t pid; + + /* TODO: find a way to handle exit | bu_id = 6 */ + if ((pid = fork()) == 0) + { + e_builtin_child(ptr, bu_id, msh); + } + else if (pid < 0) + { + /* TODO: handle fork failed */ + } + else + { + e_builtin_parent(pid, ptr, bu_id, msh); + } +} diff --git a/src/e_builtins.h b/src/e_builtins.h new file mode 100644 index 0000000..1d0804d --- /dev/null +++ b/src/e_builtins.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_builtins.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef E_BUILTINS_H +#define E_BUILTINS_H + +#include + +#include "s_struct.h" + +void e_builtin(const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh); + +#endif diff --git a/src/e_externs.c b/src/e_externs.c new file mode 100644 index 0000000..d7c4854 --- /dev/null +++ b/src/e_externs.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_externs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "e_externs_next.h" +#include "m_redirs.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" + +static void + e_extern_child(const char *fullpath, + t_lcom *ptr, + t_msh *msh) +{ + dup_redirs(ptr, msh); + execve(fullpath, ptr->argv, msh->envp); + /* TODO: handle execve failed */ +} + +static void + exec_path(const char fullpath[], + t_lcom *ptr, + t_msh *msh) +{ + pid_t pid; + int32_t status; + + if ((pid = fork()) == 0) + { + e_extern_child(fullpath, ptr, msh); + } + else if (pid < 0) + { + /* TODO: handle fork failed */ + } + else + { + while (wait(&status) != pid) + ; + msh->ret = WEXITSTATUS(status); + } +} + +void + e_extern(t_lcom *ptr, + t_msh *msh) +{ + char **envpath; + char *fullpath; + + if (ft_ischarset("/.", ptr->com[0])) + { + exec_path(ptr->com, ptr, msh); + return ; + } + else if ((envpath = get_env_path(msh)) != NULL) + { + fullpath = search_in_path(ptr->com, envpath, msh); + ft_delwords(envpath); + } + /* TODO: deal if not found etc */ + exec_path(fullpath, ptr, msh); + ft_memdel((void*)&fullpath); +} diff --git a/src/e_externs.h b/src/e_externs.h new file mode 100644 index 0000000..9bbb907 --- /dev/null +++ b/src/e_externs.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_externs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef E_EXTERNS_H +#define E_EXTERNS_H + +#include "s_struct.h" + +void e_extern(t_lcom *ptr, + t_msh *msh); + +#endif diff --git a/src/e_externs_next.c b/src/e_externs_next.c new file mode 100644 index 0000000..ba3c2d5 --- /dev/null +++ b/src/e_externs_next.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_externs_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "f_fail.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" + +static char + *get_fullpath(const char p_path[], + const char d_name[], + t_msh *msh) +{ + char *fullpath; + const size_t path_len = ft_strlen(p_path); + const size_t name_len = ft_strlen(d_name); + + if (!(fullpath = (char*)malloc((path_len + name_len + 2) * sizeof(char)))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + fail_alloc(msh); + } + (void)ft_memcpy(fullpath, p_path, path_len); + *(fullpath + (path_len)) = '/'; + (void)ft_memcpy(fullpath + path_len + 1, d_name, name_len); + *(fullpath + (path_len + name_len + 1)) = '\0'; + return (fullpath); +} + +char + *search_in_path(const char com[], + char *envpath[], + t_msh *msh) +{ + /* TODO: norme */ + struct dirent *ent; + char **p_path; + char *fullpath; + DIR *dir; + + p_path = envpath; + while (*p_path) + { + if ((dir = opendir(*p_path)) != NULL) + { + while ((ent = readdir(dir)) != NULL) + { + /* TODO: check for not bins (dirs, etc) */ + if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0) + { + fullpath = get_fullpath(*p_path, ent->d_name, msh); + closedir(dir); + return (fullpath); + } + } + closedir(dir); + } + p_path++; + } + return (NULL); +} + +char + **get_env_path(t_msh *msh) +{ + char **p_env; + char **envpath; + char *envline; + + p_env = msh->envp; + while (*p_env && ft_strncmp("PATH", *p_env, 4) != 0) + { + p_env++; + } + if (*p_env == NULL) + return (NULL); + envline = ft_strchr(*p_env, '='); + envline += 1; + if (*envline != '\0') + { + if (!(envpath = ft_split(envline, ':'))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + fail_alloc(msh); + } + return (envpath); + } + return (NULL); +} diff --git a/src/e_externs_next.h b/src/e_externs_next.h new file mode 100644 index 0000000..da1cfdf --- /dev/null +++ b/src/e_externs_next.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_externs_next.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef E_EXTERNS_NEXT_H +#define E_EXTERNS_NEXT_H + +#include "s_struct.h" + +char **get_env_path(t_msh *msh); +char *search_in_path(const char com[], + char *envpath[], + t_msh *msh); + +#endif diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c new file mode 100644 index 0000000..c8f8404 --- /dev/null +++ b/src/e_externs_pipes.c @@ -0,0 +1,147 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_externs_pipes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include +#include + +#include "d_define.h" +#include "e_externs_next.h" +#include "f_fail.h" +#include "m_redirs.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" + +static void + e_extern_child(const char *fullpath, + t_lcom *ptr, + t_msh *msh) +{ + dup_redirs(ptr, msh); + execve(fullpath, ptr->argv, msh->envp); + /* TODO: handle execve failed */ +} + +static size_t + e_get_pipes_count(struct s_lpipes *ptr) +{ + struct s_lpipes *rptr; + size_t pipes; + + rptr = ptr; + pipes = 0; + while (rptr->next != NULL) + { + rptr = rptr->next; + pipes++; + } + return (pipes); +} + +static void + exec_path(const char *fullpath[], + struct s_lpipes *head, + t_msh *msh) +{ + size_t pipes; + size_t i; + size_t j; + int fd[256][2]; + int pid; + int status; + + /* TODO: norm, error mgmnt */ + pipes = e_get_pipes_count(head); + i = 0; + while (i < pipes) + { + pipe(fd[i]); + i++; + } + i = 0; + while (i <= pipes) + { + if ((pid = fork()) == 0) + { + if (i != 0) + dup2(fd[i - 1][FT_WRITE_END], STDIN_FILENO); + if (i != pipes) + dup2(fd[i][FT_READ_END], STDOUT_FILENO); + j = 0; + while (j < pipes) + { + close(fd[j][FT_WRITE_END]); + close(fd[j][FT_READ_END]); + j++; + } + e_extern_child(fullpath[i], head->one, msh); + } + head = head->next; + i++; + } + i = 0; + while (i < pipes) + { + close(fd[i][FT_WRITE_END]); + close(fd[i][FT_READ_END]); + i++; + } + /* while (wait(&status) != pid) */ + /* ; */ + /* msh->ret = WEXITSTATUS(status); */ + waitpid(pid, &status, 0); + msh->ret = WEXITSTATUS(status); +} + +void + e_externs_pipes(struct s_lpipes *ptr, + t_msh *msh) +{ + struct s_lpipes *head; + struct s_lpipes *rptr; + char **envpath; + char **fullpath; + size_t i; + size_t pipes; + + head = ptr; + rptr = ptr; + pipes = e_get_pipes_count(head); + if (!(fullpath = (char **)malloc((pipes + 2) * sizeof(char *)))) + fail_alloc(msh); + fullpath[pipes + 1] = NULL; + i = 0; + while (rptr != NULL) + { + if (ft_ischarset("/.", rptr->one->com[0])) + { + if (!(fullpath[i] = ft_strdup(rptr->one->com))) + fail_alloc(msh); + } + else if ((envpath = get_env_path(msh)) != NULL) + { + fullpath[i] = search_in_path(rptr->one->com, envpath, msh); + ft_delwords(envpath); + } + /* TODO: deal if not found etc */ + i++; + rptr = rptr->next; + } + i = 0; + exec_path((const char**)fullpath, head, msh); + ft_delwords(fullpath); +} diff --git a/src/e_externs_pipes.h b/src/e_externs_pipes.h new file mode 100644 index 0000000..6c242fb --- /dev/null +++ b/src/e_externs_pipes.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_externs_pipes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef E_EXTERNS_PIPES_H +#define E_EXTERNS_PIPES_H + +#include "s_struct.h" + +void e_externs_pipes(struct s_lpipes *ptr, t_msh *msh); + +#endif diff --git a/src/e_lcom.c b/src/e_lcom.c new file mode 100644 index 0000000..e8159fd --- /dev/null +++ b/src/e_lcom.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_lcom.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "e_builtins.h" +#include "e_externs.h" +#include "e_pipes.h" +#include "s_lpipes.h" +#include "s_struct.h" + +static uint8_t + get_builtin_id(const char com[], + t_msh *msh) +{ + uint8_t i; + + i = 0; + while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i], + ft_strlen(msh->bu_ref[i]) + 1) != 0) + { + i++; + } + return (i); +} + +void + e_lcom(t_msh *msh) +{ + t_lcom *ptr; + uint8_t bu_id; + + ptr = msh->curr; + while (ptr != NULL) + { + if (ptr->pipes) + { + e_pipes(ptr, msh); + } + else if (ptr->com) + { + if ((bu_id = get_builtin_id(ptr->com, msh)) < FT_BUILTINS_COUNT) + e_builtin(ptr, bu_id, msh); + else + e_extern(ptr, msh); + } + ptr = ptr->next; + } +} diff --git a/src/e_lcom.h b/src/e_lcom.h new file mode 100644 index 0000000..ab0368f --- /dev/null +++ b/src/e_lcom.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_lcom.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef E_LCOM_H +#define E_LCOM_H + +#include +#include "s_struct.h" + +void e_lcom(t_msh *msh); + +#endif diff --git a/src/e_pipes.c b/src/e_pipes.c new file mode 100644 index 0000000..29ee5d4 --- /dev/null +++ b/src/e_pipes.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_pipes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "e_builtins.h" +#include "e_externs_pipes.h" +#include "s_lpipes.h" +#include "s_struct.h" + +static uint8_t + get_builtin_id(const char com[], + t_msh *msh) +{ + uint8_t i; + + i = 0; + while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i], + ft_strlen(msh->bu_ref[i]) + 1) != 0) + { + i++; + } + return (i); +} + +void + e_pipes(t_lcom *ptr, + t_msh *msh) +{ + uint8_t bu_id; + + if ((bu_id = get_builtin_id(ptr->pipes->one->com, msh)) + < FT_BUILTINS_COUNT) + e_builtin(ptr->pipes->one, bu_id, msh); + else + e_externs_pipes(ptr->pipes, msh); + lpipes_clear(&ptr->pipes); +} diff --git a/src/e_pipes.h b/src/e_pipes.h new file mode 100644 index 0000000..d4de639 --- /dev/null +++ b/src/e_pipes.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* e_pipes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef E_PIPES_H +#define E_PIPES_H + +#include "s_struct.h" + +void e_pipes(t_lcom *ptr, t_msh *msh); + +#endif diff --git a/src/f_chdir.c b/src/f_chdir.c new file mode 100644 index 0000000..6bb0497 --- /dev/null +++ b/src/f_chdir.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_chdir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "s_struct.h" + +void + fail_chd(const char concern[], + const char path[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s: %s\n", + msh->shname, concern, path, strerror(errno)); +} diff --git a/src/f_chdir.h b/src/f_chdir.h new file mode 100644 index 0000000..be9773b --- /dev/null +++ b/src/f_chdir.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_chdir.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef F_CHDIR_H +#define F_CHDIR_H + +#include "s_struct.h" + +void fail_chd(const char concern[], + const char pathp[], + t_msh *msh); + +#endif diff --git a/src/f_errno.c b/src/f_errno.c new file mode 100644 index 0000000..360cf9f --- /dev/null +++ b/src/f_errno.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_errno.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "s_struct.h" + +void + f_dump_errno(const char concern[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", + msh->shname, concern, strerror(errno)); +} diff --git a/src/f_errno.h b/src/f_errno.h new file mode 100644 index 0000000..75a6159 --- /dev/null +++ b/src/f_errno.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_errno.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef F_ERRNO_H +#define F_ERRNO_H + +#include "s_struct.h" + +void f_dump_errno(const char concern[], + t_msh *msh); + +#endif diff --git a/src/f_fail.c b/src/f_fail.c new file mode 100644 index 0000000..aae00fb --- /dev/null +++ b/src/f_fail.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_fail.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "d_define.h" +#include "s_struct.h" + +static void + write_fail(const char concern[], + const char msg[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->shname, concern, msg); +} + +void + fail_no_options(const char concern[], + t_msh *msh) +{ + write_fail(concern, FT_FAIL_NO_OPTIONS, msh); +} + +void + fail_identifier(const char concern[], + const char identifier[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: `%s': not a valid identifier\n", + msh->shname, concern, identifier); +} + +void + fail_too_many_args(const char concern[], + t_msh *msh) +{ + write_fail(concern, FT_FAIL_TOO_MANY_ARGS, msh); +} + +void + fail_alloc(t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno)); + exit(FT_RET_ALLOC); +} diff --git a/src/f_fail.h b/src/f_fail.h new file mode 100644 index 0000000..9699eee --- /dev/null +++ b/src/f_fail.h @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_fail.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef F_FAIL_H +#define F_FAIL_H + +#include "f_chdir.h" +#include "f_errno.h" +#include "f_redir.h" +#include "s_struct.h" + +void fail_no_options(const char concern[], + t_msh *msh); +void fail_too_many_args(const char concern[], + t_msh *msh); +void fail_identifier(const char concern[], + const char identifier[], + t_msh *msh); +void fail_alloc(t_msh *msh); + +#endif diff --git a/src/f_redir.c b/src/f_redir.c new file mode 100644 index 0000000..3561272 --- /dev/null +++ b/src/f_redir.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "s_lcom.h" +#include "s_destroy.h" + +void + f_redir(const char path[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", + msh->shname, path, strerror(errno)); + lcom_clear(&msh->curr); + s_destroy(msh); + exit(1); +} diff --git a/src/f_redir.h b/src/f_redir.h new file mode 100644 index 0000000..25f53cf --- /dev/null +++ b/src/f_redir.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_redir.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef F_REDIR_H +#define F_REDIR_H + +#include "s_struct.h" + +void f_redir(const char path[], + t_msh *msh); + +#endif diff --git a/src/ft_b_builtins.h b/src/ft_b_builtins.h deleted file mode 100644 index ed76f70..0000000 --- a/src/ft_b_builtins.h +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_builtins.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_BUILTINS_H -#define FT_B_BUILTINS_H - -#include "ft_b_cd.h" -#include "ft_b_echo.h" -#include "ft_b_env.h" -#include "ft_b_exit.h" -#include "ft_b_export.h" -#include "ft_b_pwd.h" -#include "ft_b_type.h" -#include "ft_b_unset.h" - -#endif diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c deleted file mode 100644 index fbd0282..0000000 --- a/src/ft_b_cd.c +++ /dev/null @@ -1,73 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_cd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "ft_f_fail.h" -#include "ft_s_destroy.h" -#include "ft_s_struct.h" -#include "ft_u_utils.h" -#include "ft_u_vars.h" - -static void - ft_set_path(char **path, - char *args[], - t_msh *msh) -{ - if (!(*path = ft_strdup(*args))) - { - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) - { - if (!(*path = ft_strsubst(*path, - "~", ft_subst_var_value("$HOME", msh)))) - { - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - } -} - -uint8_t - ft_b_cd(char *args[], - t_msh *msh) -{ - const uint64_t argc = ft_get_argc((const char**)args); - char *path; - - if (argc >= 2) - { - ft_fail_too_many_args("cd", msh); - return (1); - } - else if (argc == 0) - { - if (!(path = ft_subst_var_value("$HOME", msh))) - return (1); - } - else - ft_set_path(&path, args, msh); - if (chdir(path) != 0) - { - ft_fail_chd("cd", path, msh); - ft_memdel((void*)&path); - return (1); - } - ft_memdel((void*)&msh->cwd); - msh->cwd = getcwd(NULL, 0); - ft_memdel((void*)&path); - return (0); -} diff --git a/src/ft_b_cd.h b/src/ft_b_cd.h deleted file mode 100644 index e789cc0..0000000 --- a/src/ft_b_cd.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_cd.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_CD_H -#define FT_B_CD_H - -#include -#include "ft_s_struct.h" - -uint8_t ft_b_cd(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_b_echo.c b/src/ft_b_echo.c deleted file mode 100644 index de5431f..0000000 --- a/src/ft_b_echo.c +++ /dev/null @@ -1,116 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_echo.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "ft_s_struct.h" -#include "ft_u_utils.h" - -/* TODO: echo "quoted text", echo 'quoted text', done*/ -/* echo kill\nbackslash\nbut\nnot\nn, echo "quoted\nnew\nlines", done */ -/* full buffer */ - -void - ft_e_put(char *str, - uint8_t op) -{ - size_t i; - - i = -1; - if (op == 0) - while (str[++i]) - { - if (str[i] == '\\') - i++; - ft_putchar(str[i]); - } -} - -char - *ft_e_initb(char *ptr[], - char *str) -{ - size_t len; - uint8_t i; - - len = 0; - i = -1; - while (ptr[++i]) - { - len += ft_strlen(ptr[i]); - } - if (!(str = (char*)malloc(len * sizeof(char)))) - return (NULL); - return (str); -} - -/***********************************************/ -/* void */ -/* ft_e_fill(char *ptr[], char **str) */ -/* { */ -/* char *bs; */ -/* */ -/* ft_sprintf(*str, "%s", *ptr); */ -/* if (*str[0] == '\"' || *str[0] == '\'') */ -/* { */ -/* *str = ft_strtrim(*str, "\"\'"); */ -/* ft_printf("%s", *str); */ -/* } */ -/* else if ((bs = ft_strrchr(*str, '\\'))) */ -/* { */ -/* ft_e_put(*str, 0); */ -/* } */ -/* else */ -/* ft_printf("%s", *str); */ -/* } */ -/***********************************************/ - -uint8_t - ft_b_echo(char *args[], - t_msh *msh) -{ - const uint64_t argc = ft_get_argc((const char **)args); - char **ptr; - char *str; - int8_t nopt; - - (void)msh; - ptr = args; - nopt = 0; - str = NULL; - str = ft_e_initb(ptr, str); - if (argc >= 1) - { - if (ft_strncmp(ptr[0], "-n", 2) == 0) - { - nopt = 1; - ptr += 1; - } - if (argc - nopt >= 1) - { - ft_printf("%s", *ptr); - ptr++; - while (*ptr) - { - ft_printf(" %s", *ptr); - ptr++; - } - } - } - free(str); - if (nopt == 0) - ft_printf("\n"); - return (0); -} diff --git a/src/ft_b_echo.h b/src/ft_b_echo.h deleted file mode 100644 index 7fd7463..0000000 --- a/src/ft_b_echo.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_echo.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_ECHO_H -#define FT_B_ECHO_H - -#include -#include "ft_s_struct.h" - -uint8_t ft_b_echo(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_b_env.c b/src/ft_b_env.c deleted file mode 100644 index 14d05a2..0000000 --- a/src/ft_b_env.c +++ /dev/null @@ -1,36 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_env.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include "ft_f_fail.h" -#include "ft_s_struct.h" - -uint8_t - ft_b_env(char *args[], - t_msh *msh) -{ - char **ptr; - - if (args && args[0]) - { - ft_fail_no_options("env", msh); - return (127); - } - ptr = msh->envp; - while (*ptr) - { - ft_printf("%s\n", *ptr); - ptr++; - } - return (0); -} diff --git a/src/ft_b_env.h b/src/ft_b_env.h deleted file mode 100644 index 192c5d9..0000000 --- a/src/ft_b_env.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_env.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_ENV_H -#define FT_B_ENV_H - -#include -#include "ft_s_struct.h" - -uint8_t ft_b_env(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_b_exit.c b/src/ft_b_exit.c deleted file mode 100644 index 7a7e5e8..0000000 --- a/src/ft_b_exit.c +++ /dev/null @@ -1,43 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_exit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "ft_f_fail.h" -#include "ft_s_lcom.h" -#include "ft_s_destroy.h" -#include "ft_s_struct.h" -#include "ft_u_utils.h" - -uint8_t - ft_b_exit(char *args[], - t_msh *msh) -{ - uint8_t ret; - const uint64_t argc = ft_get_argc((const char**)args); - - if (argc > 1) - { - ft_fail_too_many_args("exit", msh); - return (1); - } - if (argc == 1) - { - ret = ft_atoi(args[0]); - /* TODO: non numeric args[0] */ - } - ft_dprintf(STDERR_FILENO, "exit\n"); - return (0); -} diff --git a/src/ft_b_exit.h b/src/ft_b_exit.h deleted file mode 100644 index 5dd2a07..0000000 --- a/src/ft_b_exit.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_exit.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_EXIT_H -#define FT_B_EXIT_H - -#include - -uint8_t - ft_b_exit(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_b_export.c b/src/ft_b_export.c deleted file mode 100644 index fa2de45..0000000 --- a/src/ft_b_export.c +++ /dev/null @@ -1,79 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_export.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include "ft_b_env.h" -#include "ft_f_fail.h" -#include "ft_s_struct.h" -#include "ft_u_utils.h" - -static int8_t - ft_check_valid_identifier(const char *arg) -{ - char *ptr; - - ptr = (char*)arg; - if (ft_isalpha(ptr[0])) - { - return (1); - } - return (0); -} - -static int8_t - ft_check_equals(const char *arg) -{ - char *ptr; - - ptr = (char*)arg; - while (*ptr) - { - if (*ptr == '=') - return (1); - ptr++; - } - return (0); -} - -uint8_t - ft_b_export(char *args[], - t_msh *msh) -{ - const uint64_t argc = ft_get_argc((const char**)args); - char **ptr; - int8_t next; - uint8_t r; - - if (argc == 0) - { - return (ft_b_env(NULL, msh)); - } - ptr = args; - r = 0; - while (*ptr) - { - next = 0; - if (!ft_check_valid_identifier(*ptr)) - { - ft_fail_identifier("export", *ptr, msh); - next = 1; - r = 1; - } - if (next == 0 && !ft_check_equals(*ptr)) - next = 1; - ptr++; - } - /* TODO: finish export */ - return (r); -} diff --git a/src/ft_b_export.h b/src/ft_b_export.h deleted file mode 100644 index 3a28562..0000000 --- a/src/ft_b_export.h +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_export.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_EXPORT_H -#define FT_B_EXPORT_H - -uint8_t - ft_b_export(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_b_pwd.c b/src/ft_b_pwd.c deleted file mode 100644 index d47600f..0000000 --- a/src/ft_b_pwd.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_pwd.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "ft_s_struct.h" - -uint8_t - ft_b_pwd(char *args[], - t_msh *msh) -{ - (void)args; - ft_printf("%s\n", msh->cwd); - return (0); -} diff --git a/src/ft_b_pwd.h b/src/ft_b_pwd.h deleted file mode 100644 index 653c9e4..0000000 --- a/src/ft_b_pwd.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_pwd.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_PWD_H -#define FT_B_PWD_H - -#include -#include "ft_s_struct.h" - -uint8_t ft_b_pwd(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_b_type.c b/src/ft_b_type.c deleted file mode 100644 index 4338b32..0000000 --- a/src/ft_b_type.c +++ /dev/null @@ -1,120 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_type.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -#include "ft_f_fail.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" -#include "ft_e_externs_next.h" -#include "ft_u_utils.h" - -static int8_t - ft_absolute_path_exists(char com[]) -{ - int32_t fd; - DIR *dir; - - if ((dir = opendir(com)) != NULL) - { - closedir(dir); - return (0); - } - if ((fd = open(com, O_RDONLY)) != -1) - { - close(fd); - return (1); - } - return (0); -} - -static char - *ft_type_get_path(char com[], - t_msh *msh) -{ - char **envpath; - char *fullpath; - - envpath = NULL; - fullpath = NULL; - if (ft_ischarset("/.", com[0])) - { - if (ft_absolute_path_exists(com)) - { - if (!(fullpath = ft_strdup(com))) - { - ft_lcom_clear(&msh->curr); - ft_fail_alloc(msh); - } - return (fullpath); - } - return (NULL); - } - else if ((envpath = ft_get_env_path(msh)) != NULL) - { - fullpath = ft_search_in_path(com, envpath, msh); - ft_delwords(envpath); - } - return (fullpath); -} - -static uint8_t - ft_chk_nonbuilt(char **ptr, - t_msh *msh) -{ - char *fullpath; - int32_t ret; - - ret = 0; - fullpath = ft_type_get_path(*ptr, msh); - if (fullpath) - ft_printf("%s is %s\n", *ptr, fullpath); - else - { - ft_printf("minishell: type: %s: not found\n", *ptr); - ret = 1; - } - ft_memdel((void*)&fullpath); - return (ret); -} - -uint8_t - ft_b_type(char *args[], - t_msh *msh) -{ - char **ptr; - char **p_bu; - int32_t ret; - - ptr = args; - if (!*ptr) - return (0); - ret = 0; - while (*ptr) - { - p_bu = msh->bu_ref; - while (*p_bu && ft_strncmp(*ptr, *p_bu, ft_strlen(*p_bu) + 1)) - p_bu++; - if (*p_bu != NULL) - ft_printf("%s is a shell builtin\n", *ptr); - else - { - ret = ft_chk_nonbuilt(ptr, msh); - } - ptr++; - } - return (ret); -} diff --git a/src/ft_b_type.h b/src/ft_b_type.h deleted file mode 100644 index c3d5dd6..0000000 --- a/src/ft_b_type.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_type.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_TYPE_H -#define FT_B_TYPE_H - -#include -#include "ft_s_struct.h" - -uint8_t ft_b_type(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_b_unset.c b/src/ft_b_unset.c deleted file mode 100644 index a382aca..0000000 --- a/src/ft_b_unset.c +++ /dev/null @@ -1,24 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_unset.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include "ft_s_struct.h" - -uint8_t - ft_b_unset(char *args[], - t_msh *msh) -{ - (void)args; - (void)msh; - /* TODO: do unset */ - return (0); -} diff --git a/src/ft_b_unset.h b/src/ft_b_unset.h deleted file mode 100644 index 5ab1b2b..0000000 --- a/src/ft_b_unset.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_b_unset.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_B_UNSET_H -#define FT_B_UNSET_H - -#include -#include "ft_s_struct.h" - -uint8_t ft_b_unset(char *args[], - t_msh *msh); - -#endif diff --git a/src/ft_d_define.h b/src/ft_d_define.h deleted file mode 100644 index 4152e0d..0000000 --- a/src/ft_d_define.h +++ /dev/null @@ -1,49 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_d_define.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_D_DEFINE_H -#define FT_D_DEFINE_H - -#include "ft_d_enum.h" - -/* -** ====== CLASSICS ====== -*/ - -#define FT_PS_ONE "minishell ~> " -#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type" -#define FT_BUILTINS_COUNT 8 -#define FT_HISTFILE "minishell_history" - -/* -** ====== OPTIONS ====== -*/ - -#define FT_OPT_INTERACT "-i" -#define FT_OPT_COMMAND "-c" - -/* -** ====== VARS ====== -*/ - -#define FT_ZER_VAR "$0" -#define FT_RET_VAR "$?" -#define FT_PID_VAR "$$" - -/* -** ====== FAIL MSG ====== -*/ - -#define FT_FAIL_NO_OPTIONS "no options required" -#define FT_FAIL_TOO_MANY_ARGS "too many arguments" - -#endif diff --git a/src/ft_d_enum.h b/src/ft_d_enum.h deleted file mode 100644 index 680ff00..0000000 --- a/src/ft_d_enum.h +++ /dev/null @@ -1,41 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_d_enum.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_D_ENUM_H -#define FT_D_ENUM_H - -/* -** ret vals: -** --------- -** 0: cool -** 1: alloc err -*/ - -typedef enum -{ - FALSE, - TRUE -} t_bool; - -enum -{ - FT_RET_FINE, - FT_RET_ALLOC -}; - -enum -{ - FT_WRITE_END, - FT_READ_END -}; - -#endif diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c deleted file mode 100644 index 382f85d..0000000 --- a/src/ft_e_builtins.c +++ /dev/null @@ -1,89 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_builtins.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -#include "ft_m_redirs.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - -static void - ft_e_builtin_child(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh) -{ - int32_t ret; - - ft_dup_redirs(ptr, msh); - ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - exit(ret); -} - -static void - ft_e_builtin_parent(pid_t pid, - const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh) -{ - int32_t status; - int32_t ret; - - while (wait(&status) != pid) - ; - ret = WEXITSTATUS(status); - if (bu_id != 6) - msh->ret = ret; - if (bu_id == 1 && msh->ret == 0) - { - msh->bu_ptr[bu_id](ptr->argv + 1, msh); - /* TODO: export $PWD */ - } - else if (bu_id == 6 && ret == 0) - { - if (ptr->argv[1]) - ret = ft_atoi(ptr->argv[1]); - else - ret = msh->ret; - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - exit(ret); - } -} - -void - ft_e_builtin(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh) -{ - pid_t pid; - - /* TODO: find a way to handle exit | bu_id = 6 */ - if ((pid = fork()) == 0) - { - ft_e_builtin_child(ptr, bu_id, msh); - } - else if (pid < 0) - { - /* TODO: handle fork failed */ - } - else - { - ft_e_builtin_parent(pid, ptr, bu_id, msh); - } -} diff --git a/src/ft_e_builtins.h b/src/ft_e_builtins.h deleted file mode 100644 index 21908b8..0000000 --- a/src/ft_e_builtins.h +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_builtins.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_E_BUILTINS_H -#define FT_E_BUILTINS_H - -#include -#include "ft_s_struct.h" - -void ft_e_builtin(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh); - -#endif diff --git a/src/ft_e_externs.c b/src/ft_e_externs.c deleted file mode 100644 index 9302d18..0000000 --- a/src/ft_e_externs.c +++ /dev/null @@ -1,79 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_externs.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -#include "ft_e_externs_next.h" -#include "ft_m_redirs.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - -static void - ft_e_extern_child(const char *fullpath, - t_lcom *ptr, - t_msh *msh) -{ - ft_dup_redirs(ptr, msh); - execve(fullpath, ptr->argv, msh->envp); - /* TODO: handle execve failed */ -} - -static void - ft_exec_path(const char fullpath[], - t_lcom *ptr, - t_msh *msh) -{ - pid_t pid; - int32_t status; - - if ((pid = fork()) == 0) - { - ft_e_extern_child(fullpath, ptr, msh); - } - else if (pid < 0) - { - /* TODO: handle fork failed */ - } - else - { - while (wait(&status) != pid) - ; - msh->ret = WEXITSTATUS(status); - } -} - -void - ft_e_extern(t_lcom *ptr, - t_msh *msh) -{ - char **envpath; - char *fullpath; - - if (ft_ischarset("/.", ptr->com[0])) - { - ft_exec_path(ptr->com, ptr, msh); - return ; - } - else if ((envpath = ft_get_env_path(msh)) != NULL) - { - fullpath = ft_search_in_path(ptr->com, envpath, msh); - ft_delwords(envpath); - } - /* TODO: deal if not found etc */ - ft_exec_path(fullpath, ptr, msh); - ft_memdel((void*)&fullpath); -} diff --git a/src/ft_e_externs.h b/src/ft_e_externs.h deleted file mode 100644 index 5e02353..0000000 --- a/src/ft_e_externs.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_externs.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_E_EXTERNS_H -#define FT_E_EXTERNS_H - -#include "ft_s_struct.h" - -void ft_e_extern(t_lcom *ptr, - t_msh *msh); - -#endif diff --git a/src/ft_e_externs_next.c b/src/ft_e_externs_next.c deleted file mode 100644 index 36bf5e6..0000000 --- a/src/ft_e_externs_next.c +++ /dev/null @@ -1,105 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_externs_next.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "ft_f_fail.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - -static char - *ft_get_fullpath(const char p_path[], - const char d_name[], - t_msh *msh) -{ - char *fullpath; - const size_t path_len = ft_strlen(p_path); - const size_t name_len = ft_strlen(d_name); - - if (!(fullpath = (char*)malloc((path_len + name_len + 2) * sizeof(char)))) - { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - ft_memcpy(fullpath, p_path, path_len); - *(fullpath + (path_len)) = '/'; - ft_memcpy(fullpath + path_len + 1, d_name, name_len); - *(fullpath + (path_len + name_len + 1)) = '\0'; - return (fullpath); -} - -char - *ft_search_in_path(const char com[], - char *envpath[], - t_msh *msh) -{ - /* TODO: norme */ - struct dirent *ent; - char **p_path; - char *fullpath; - DIR *dir; - - p_path = envpath; - while (*p_path) - { - if ((dir = opendir(*p_path)) != NULL) - { - while ((ent = readdir(dir)) != NULL) - { - /* TODO: check for not bins (dirs, etc) */ - if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0) - { - fullpath = ft_get_fullpath(*p_path, ent->d_name, msh); - closedir(dir); - return (fullpath); - } - } - closedir(dir); - } - p_path++; - } - return (NULL); -} - -char - **ft_get_env_path(t_msh *msh) -{ - char **p_env; - char **envpath; - char *envline; - - p_env = msh->envp; - while (*p_env && ft_strncmp("PATH", *p_env, 4) != 0) - { - p_env++; - } - if (*p_env == NULL) - return (NULL); - envline = ft_strchr(*p_env, '='); - envline += 1; - if (*envline != '\0') - { - if (!(envpath = ft_split(envline, ':'))) - { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - return (envpath); - } - return (NULL); -} diff --git a/src/ft_e_externs_next.h b/src/ft_e_externs_next.h deleted file mode 100644 index bc6272c..0000000 --- a/src/ft_e_externs_next.h +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_externs_next.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_E_EXTERNS_NEXT_H -#define FT_E_EXTERNS_NEXT_H - -#include "ft_s_struct.h" - -char **ft_get_env_path(t_msh *msh); -char *ft_search_in_path(const char com[], - char *envpath[], - t_msh *msh); - -#endif diff --git a/src/ft_e_externs_pipes.c b/src/ft_e_externs_pipes.c deleted file mode 100644 index c1c57b5..0000000 --- a/src/ft_e_externs_pipes.c +++ /dev/null @@ -1,147 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_externs_pipes.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include -#include -#include - -#include "ft_d_define.h" -#include "ft_e_externs_next.h" -#include "ft_f_fail.h" -#include "ft_m_redirs.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - -static void - ft_e_extern_child(const char *fullpath, - t_lcom *ptr, - t_msh *msh) -{ - ft_dup_redirs(ptr, msh); - execve(fullpath, ptr->argv, msh->envp); - /* TODO: handle execve failed */ -} - -static size_t -ft_e_get_pipes_count(struct s_lpipes *ptr) -{ - struct s_lpipes *rptr; - size_t pipes; - - rptr = ptr; - pipes = 0; - while (rptr->next != NULL) - { - rptr = rptr->next; - pipes++; - } - return (pipes); -} - -static void - ft_exec_path(const char *fullpath[], - struct s_lpipes *head, - t_msh *msh) -{ - size_t pipes; - size_t i; - size_t j; - int fd[256][2]; - int pid; - int status; - - /* TODO: norm, error mgmnt */ - pipes = ft_e_get_pipes_count(head); - i = 0; - while (i < pipes) - { - pipe(fd[i]); - i++; - } - i = 0; - while (i <= pipes) - { - if ((pid = fork()) == 0) - { - if (i != 0) - dup2(fd[i - 1][FT_WRITE_END], STDIN_FILENO); - if (i != pipes) - dup2(fd[i][FT_READ_END], STDOUT_FILENO); - j = 0; - while (j < pipes) - { - close(fd[j][FT_WRITE_END]); - close(fd[j][FT_READ_END]); - j++; - } - ft_e_extern_child(fullpath[i], head->one, msh); - } - head = head->next; - i++; - } - i = 0; - while (i < pipes) - { - close(fd[i][FT_WRITE_END]); - close(fd[i][FT_READ_END]); - i++; - } - /* while (wait(&status) != pid) */ - /* ; */ - /* msh->ret = WEXITSTATUS(status); */ - waitpid(pid, &status, 0); - msh->ret = WEXITSTATUS(status); -} - -void - ft_e_externs_pipes(struct s_lpipes *ptr, - t_msh *msh) -{ - struct s_lpipes *head; - struct s_lpipes *rptr; - char **envpath; - char **fullpath; - size_t i; - size_t pipes; - - head = ptr; - rptr = ptr; - pipes = ft_e_get_pipes_count(head); - if (!(fullpath = (char **)malloc((pipes + 2) * sizeof(char *)))) - ft_fail_alloc(msh); - fullpath[pipes + 1] = NULL; - i = 0; - while (rptr != NULL) - { - if (ft_ischarset("/.", rptr->one->com[0])) - { - if (!(fullpath[i] = ft_strdup(rptr->one->com))) - ft_fail_alloc(msh); - } - else if ((envpath = ft_get_env_path(msh)) != NULL) - { - fullpath[i] = ft_search_in_path(rptr->one->com, envpath, msh); - ft_delwords(envpath); - } - /* TODO: deal if not found etc */ - i++; - rptr = rptr->next; - } - i = 0; - ft_exec_path((const char**)fullpath, head, msh); - ft_delwords(fullpath); -} diff --git a/src/ft_e_externs_pipes.h b/src/ft_e_externs_pipes.h deleted file mode 100644 index 435c795..0000000 --- a/src/ft_e_externs_pipes.h +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_externs_pipes.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_E_EXTERNS_PIPES_H -#define FT_E_EXTERNS_PIPES_H - -#include "ft_s_struct.h" - -void ft_e_externs_pipes(struct s_lpipes *ptr, t_msh *msh); - -#endif diff --git a/src/ft_e_lcom.c b/src/ft_e_lcom.c deleted file mode 100644 index 54db581..0000000 --- a/src/ft_e_lcom.c +++ /dev/null @@ -1,59 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_lcom.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "ft_e_builtins.h" -#include "ft_e_externs.h" -#include "ft_e_pipes.h" -#include "ft_s_lpipes.h" -#include "ft_s_struct.h" - -static uint8_t - ft_get_builtin_id(const char com[], - t_msh *msh) -{ - uint8_t i; - - i = 0; - while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i], - ft_strlen(msh->bu_ref[i]) + 1) != 0) - { - i++; - } - return (i); -} - -void - ft_e_lcom(t_msh *msh) -{ - t_lcom *ptr; - uint8_t bu_id; - - ptr = msh->curr; - while (ptr != NULL) - { - if (ptr->pipes) - { - ft_e_pipes(ptr, msh); - } - else if (ptr->com) - { - if ((bu_id = ft_get_builtin_id(ptr->com, msh)) < FT_BUILTINS_COUNT) - ft_e_builtin(ptr, bu_id, msh); - else - ft_e_extern(ptr, msh); - } - ptr = ptr->next; - } -} diff --git a/src/ft_e_lcom.h b/src/ft_e_lcom.h deleted file mode 100644 index 5244001..0000000 --- a/src/ft_e_lcom.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_lcom.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_E_LCOM_H -#define FT_E_LCOM_H - -#include -#include "ft_s_struct.h" - -void ft_e_lcom(t_msh *msh); - -#endif diff --git a/src/ft_e_pipes.c b/src/ft_e_pipes.c deleted file mode 100644 index 581aec2..0000000 --- a/src/ft_e_pipes.c +++ /dev/null @@ -1,49 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_pipes.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "ft_e_builtins.h" -#include "ft_e_externs_pipes.h" -#include "ft_s_lpipes.h" -#include "ft_s_struct.h" - -static uint8_t - ft_get_builtin_id(const char com[], - t_msh *msh) -{ - uint8_t i; - - i = 0; - while (msh->bu_ref[i] && ft_strncmp(com, msh->bu_ref[i], - ft_strlen(msh->bu_ref[i]) + 1) != 0) - { - i++; - } - return (i); -} - -void - ft_e_pipes(t_lcom *ptr, - t_msh *msh) -{ - uint8_t bu_id; - - if ((bu_id = ft_get_builtin_id(ptr->pipes->one->com, msh)) - < FT_BUILTINS_COUNT) - ft_e_builtin(ptr->pipes->one, bu_id, msh); - else - ft_e_externs_pipes(ptr->pipes, msh); - ft_lpipes_clear(&ptr->pipes); -} diff --git a/src/ft_e_pipes.h b/src/ft_e_pipes.h deleted file mode 100644 index f801b21..0000000 --- a/src/ft_e_pipes.h +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_pipes.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_E_PIPES_H -#define FT_E_PIPES_H - -#include "ft_s_struct.h" - -void ft_e_pipes(t_lcom *ptr, t_msh *msh); - -#endif diff --git a/src/ft_f_chdir.c b/src/ft_f_chdir.c deleted file mode 100644 index 1386626..0000000 --- a/src/ft_f_chdir.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_chdir.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "ft_s_struct.h" - -void - ft_fail_chd(const char concern[], - const char path[], - t_msh *msh) -{ - ft_dprintf(STDERR_FILENO, "%s: %s: %s: %s\n", - msh->shname, concern, path, strerror(errno)); -} diff --git a/src/ft_f_chdir.h b/src/ft_f_chdir.h deleted file mode 100644 index fae82d7..0000000 --- a/src/ft_f_chdir.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_chdir.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_F_CHDIR_H -#define FT_F_CHDIR_H - -#include "ft_s_struct.h" - -void ft_fail_chd(const char concern[], - const char pathp[], - t_msh *msh); - -#endif diff --git a/src/ft_f_errno.c b/src/ft_f_errno.c deleted file mode 100644 index 22ee7a9..0000000 --- a/src/ft_f_errno.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_errno.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "ft_s_struct.h" - -void - ft_f_dump_errno(const char concern[], - t_msh *msh) -{ - ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", - msh->shname, concern, strerror(errno)); -} diff --git a/src/ft_f_errno.h b/src/ft_f_errno.h deleted file mode 100644 index 86fce83..0000000 --- a/src/ft_f_errno.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_errno.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_F_ERRNO_H -#define FT_F_ERRNO_H - -#include "ft_s_struct.h" - -void ft_f_dump_errno(const char concern[], - t_msh *msh); - -#endif diff --git a/src/ft_f_fail.c b/src/ft_f_fail.c deleted file mode 100644 index 3f43bae..0000000 --- a/src/ft_f_fail.c +++ /dev/null @@ -1,58 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_fail.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -#include "ft_d_define.h" -#include "ft_s_struct.h" - -static void - ft_write_fail(const char concern[], - const char msg[], - t_msh *msh) -{ - ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", msh->shname, concern, msg); -} - -void - ft_fail_no_options(const char concern[], - t_msh *msh) -{ - ft_write_fail(concern, FT_FAIL_NO_OPTIONS, msh); -} - -void - ft_fail_identifier(const char concern[], - const char identifier[], - t_msh *msh) -{ - ft_dprintf(STDERR_FILENO, "%s: %s: `%s': not a valid identifier\n", - msh->shname, concern, identifier); -} - -void - ft_fail_too_many_args(const char concern[], - t_msh *msh) -{ - ft_write_fail(concern, FT_FAIL_TOO_MANY_ARGS, msh); -} - -void - ft_fail_alloc(t_msh *msh) -{ - ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno)); - exit(FT_RET_ALLOC); -} diff --git a/src/ft_f_fail.h b/src/ft_f_fail.h deleted file mode 100644 index b176cbb..0000000 --- a/src/ft_f_fail.h +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_fail.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_F_FAIL_H -#define FT_F_FAIL_H - -#include "ft_f_chdir.h" -#include "ft_f_errno.h" -#include "ft_f_redir.h" -#include "ft_s_struct.h" - -void ft_fail_no_options(const char concern[], - t_msh *msh); -void ft_fail_too_many_args(const char concern[], - t_msh *msh); -void ft_fail_identifier(const char concern[], - const char identifier[], - t_msh *msh); -void ft_fail_alloc(t_msh *msh); - -#endif diff --git a/src/ft_f_redir.c b/src/ft_f_redir.c deleted file mode 100644 index 2cd7d58..0000000 --- a/src/ft_f_redir.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_redir.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -#include "ft_s_lcom.h" -#include "ft_s_destroy.h" - -void - ft_f_redir(const char path[], - t_msh *msh) -{ - ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", - msh->shname, path, strerror(errno)); - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - exit(1); -} diff --git a/src/ft_f_redir.h b/src/ft_f_redir.h deleted file mode 100644 index 2b99da7..0000000 --- a/src/ft_f_redir.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_f_redir.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_F_REDIR_H -#define FT_F_REDIR_H - -#include "ft_s_struct.h" - -void ft_f_redir(const char path[], - t_msh *msh); - -#endif diff --git a/src/ft_m_argv.c b/src/ft_m_argv.c deleted file mode 100644 index 7b91fbb..0000000 --- a/src/ft_m_argv.c +++ /dev/null @@ -1,43 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_argv.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "ft_d_define.h" -#include "ft_m_comm.h" -#include "ft_m_loop.h" -#include "ft_s_struct.h" - -uint8_t - ft_m_argv(int argc, - char *const argv[], - t_msh *msh) -{ - /* TODO: better argv handling */ - if (argc == 1) - { - msh->ret = ft_m_loop(msh); - return (msh->ret); - } - if (!ft_strncmp(*(argv + 1), FT_OPT_COMMAND, 3)) - { - if (*(argv + 2) == NULL) - { - ft_dprintf(2, "%s: %s: option requires an argument\n", - msh->shname, FT_OPT_COMMAND); - return (2); - } - msh->ret = ft_m_comm(*(argv + 2), msh); - } - return (msh->ret); -} diff --git a/src/ft_m_argv.h b/src/ft_m_argv.h deleted file mode 100644 index 783c7e4..0000000 --- a/src/ft_m_argv.h +++ /dev/null @@ -1,24 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_argv.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_M_ARGV_H -#define FT_M_ARGV_H - -#include - -#include "ft_s_struct.h" - -uint8_t ft_m_argv(int argc, - char *const argv[], - t_msh *msh); - -#endif diff --git a/src/ft_m_comm.c b/src/ft_m_comm.c deleted file mode 100644 index 5fca253..0000000 --- a/src/ft_m_comm.c +++ /dev/null @@ -1,38 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_comm.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "ft_d_enum.h" -#include "ft_e_lcom.h" -#include "ft_m_prompt.h" -#include "ft_p_line.h" -#include "ft_s_lcom.h" - -uint8_t - ft_m_comm(const char line[], - t_msh *msh) -{ - if (line[0] != '\0') - { - ft_p_line((char*)line, msh); - ft_e_lcom(msh); - ft_lcom_clear(&msh->curr); - } - else - { - ft_memdel((void*)&line); - } - return (msh->ret); -} - diff --git a/src/ft_m_comm.h b/src/ft_m_comm.h deleted file mode 100644 index 5b22f3a..0000000 --- a/src/ft_m_comm.h +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_comm.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_M_COMM_H -#define FT_M_COMM_H - -#include - -#include "ft_s_struct.h" - -uint8_t ft_m_comm(const char line[], - t_msh *msh); - -#endif diff --git a/src/ft_m_funptr.c b/src/ft_m_funptr.c deleted file mode 100644 index 305dfc9..0000000 --- a/src/ft_m_funptr.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_funptr.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "ft_d_enum.h" -#include "ft_b_builtins.h" -#include "ft_f_fail.h" -#include "ft_m_funptr.h" -#include "ft_s_struct.h" - -void - ft_init_buptr(t_msh *msh) -{ - msh->bu_ptr[0] = ft_b_echo; - msh->bu_ptr[1] = ft_b_cd; - msh->bu_ptr[2] = ft_b_pwd; - msh->bu_ptr[3] = ft_b_export; - msh->bu_ptr[4] = ft_b_unset; - msh->bu_ptr[5] = ft_b_env; - msh->bu_ptr[6] = ft_b_exit; - msh->bu_ptr[7] = ft_b_type; - if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|'))) - { - ft_fail_alloc(msh); - } -} diff --git a/src/ft_m_funptr.h b/src/ft_m_funptr.h deleted file mode 100644 index 889c0af..0000000 --- a/src/ft_m_funptr.h +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_funptr.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_M_FUNPTR_H -#define FT_M_FUNPTR_H - -#include "ft_s_struct.h" - -void ft_init_buptr(t_msh *msh); - -#endif diff --git a/src/ft_m_loop.c b/src/ft_m_loop.c deleted file mode 100644 index 3f40fba..0000000 --- a/src/ft_m_loop.c +++ /dev/null @@ -1,126 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_loop.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "ft_e_lcom.h" -#include "ft_m_prompt.h" -#include "ft_p_line.h" -#include "ft_s_lcom.h" - - -static char - *ft_strjoin_m(char *s1, - char *s2, - uint8_t mode) -{ - size_t i; - size_t j; - size_t size1; - size_t size2; - char *dst; - - i = -1; - j = -1; - size1 = ft_strlen(s1); - size2 = ft_strlen(s2) + 1; - if (!(dst = (char*)malloc((size1 + size2 + 2) * sizeof(char)))) - return (NULL); - while (++i < size1) - dst[i] = s1[i]; - (mode == 0) ? dst[i++] = '\n' : 0; - while (++j < size2) - dst[i + j] = s2[j]; - dst[i + j] = '\0'; - (mode == 0) ? ft_memdel((void*)&s1) : ft_memdel((void*)&s2); - return (dst); -} - -void - ft_m_loop_cont(t_msh *msh, - char *line, - char *quote, - int8_t gnl) -{ - char *prog; - char *buf; - char *fin; - - if (!(prog = ft_calloc(ft_strlen(line) + 1, sizeof(char))) || - !(buf = ft_strdup(line + (quote - line) + 1))) - return ; - ft_memcpy(prog, line, (quote - line)); - while (gnl > 0 && ((!(quote = ft_strrchr(buf, '\"'))) && - (!(quote = ft_strrchr(buf, '\''))))) - { - ft_m_cont_prompt(); - gnl = get_next_line(STDIN_FILENO, &line); - buf = ft_strjoin_m(buf, line, 0); - ft_memdel((void*)&line); - } - fin = ft_strtrim(buf, "\"'"); - ft_memdel((void*)&buf); - fin = (ft_strrchr(fin, '\'')) ? ft_strsubst(fin, "\'", "") : fin; - fin = ft_strjoin_m(prog, fin, 1); - ft_p_line(fin, msh); - ft_memdel((void*)&prog); - ft_memdel((void*)&fin); - ft_e_lcom(msh); - ft_lcom_clear(&msh->curr); -} - -uint8_t - ft_m_loop(t_msh *msh) -{ - char *line; - char *quote; - int8_t gnl; - int32_t pipefd[2]; - - gnl = 1; - pipefd[0] = STDOUT_FILENO; - pipefd[1] = STDIN_FILENO; - while (gnl > 0) - { - ft_m_prompt(msh); - gnl = get_next_line(STDIN_FILENO, &line); - if (line[0] != '\0') - { - if (!(quote = ft_strchr(line, '\'')) && !(quote = ft_strchr(line, '\"'))) - { - ft_p_line(line, msh); - ft_memdel((void*)&line); - ft_e_lcom(msh); - ft_lcom_clear(&msh->curr); - } - else - { - ft_m_loop_cont(msh, line, quote, 1); - if (line != NULL) - ft_memdel((void*)&line); - } - /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ - /* TODO: GNL 25 leak on "msh ~> exit" | gl hf */ - /* TODO: "msh ~> some command \": re GNL into ft_nrealloc */ - /* TODO: the chad pipes | */ - /* TODO: a histfile would be nice */ - } - else - { - ft_memdel((void*)&line); - } - } - return (msh->ret); -} diff --git a/src/ft_m_loop.h b/src/ft_m_loop.h deleted file mode 100644 index 4dd1873..0000000 --- a/src/ft_m_loop.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_loop.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_M_LOOP -#define FT_M_LOOP - -#include - -#include "ft_s_struct.h" - -uint8_t ft_m_loop(t_msh *msh); - -#endif diff --git a/src/ft_m_prompt.c b/src/ft_m_prompt.c deleted file mode 100644 index 4c4cc41..0000000 --- a/src/ft_m_prompt.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_prompt.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "ft_m_prompt.h" -#include "ft_s_struct.h" - -void - ft_m_cont_prompt(void) -{ - ft_dprintf(STDIN_FILENO, "> "); - /* TODO :ft_printf("%s", msh->ps_two); */ -} - -void - ft_m_prompt(t_msh *msh) -{ - ft_dprintf(STDIN_FILENO, "%s", msh->ps_one); -} diff --git a/src/ft_m_prompt.h b/src/ft_m_prompt.h deleted file mode 100644 index 2725539..0000000 --- a/src/ft_m_prompt.h +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_prompt.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_M_PROMPT_H -#define FT_M_PROMPT_H - -#include "ft_s_struct.h" - -void ft_m_prompt(t_msh *msh); -void ft_m_cont_prompt(void); -#endif diff --git a/src/ft_m_redirs.c b/src/ft_m_redirs.c deleted file mode 100644 index 701a2a4..0000000 --- a/src/ft_m_redirs.c +++ /dev/null @@ -1,51 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_redirs.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "ft_f_fail.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - -void - ft_dup_redirs(const t_lcom *ptr, - t_msh *msh) -{ - int32_t fd; - - if (ptr->redir == -1) - { - if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) - ft_f_redir(ptr->rdrpath, msh); - dup2(fd, STDIN_FILENO); - close(fd); - } - if (ptr->redir == 1) - { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) - ft_f_redir(ptr->rdrpath, msh); - dup2(fd, ptr->rdrfd); - close(fd); - } - if (ptr->redir == 2) - { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) - ft_f_redir(ptr->rdrpath, msh); - dup2(fd, ptr->rdrfd); - close(fd); - } -} diff --git a/src/ft_m_redirs.h b/src/ft_m_redirs.h deleted file mode 100644 index 9021cb8..0000000 --- a/src/ft_m_redirs.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_m_redirs.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_M_REDIRS_H -#define FT_M_REDIRS_H - -#include "ft_s_struct.h" - -void ft_dup_redirs(const t_lcom *ptr, - t_msh *msh); - -#endif diff --git a/src/ft_p_lcom.c b/src/ft_p_lcom.c deleted file mode 100644 index a8449ae..0000000 --- a/src/ft_p_lcom.c +++ /dev/null @@ -1,158 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_p_lcom.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include -#include - -#include "ft_d_define.h" -#include "ft_f_fail.h" -#include "ft_s_lcom.h" -#include "ft_s_lpipes.h" -#include "ft_s_struct.h" - -static void - ft_rdr_err_check(char *ptr, - t_lcom **link) -{ - if ((*link)->redir == -1 && ft_ischarset("><", *(ptr + 1))) - { - /* TODO: syntax err */ - } - else if ((*link)->redir == 1 && ft_ischarset("<", *(ptr + 1))) - { - /* TODO: syntax err */ - } - else if ((*link)->redir == 2 && ft_ischarset("<>", *(ptr + 1))) - { - /* TODO: syntax err */ - } -} - -static int8_t - ft_get_rdrpath(char *ptr, - t_lcom **link) -{ - char *p_rdrpath; - - ptr += ((*link)->redir == 2) ? (2) : (1); - if (!((*link)->rdrpath = - (char*)malloc((ft_strlen(ptr) + 1) * sizeof(char)))) - { - return (-1); - } - p_rdrpath = (*link)->rdrpath; - while (*ptr) - { - if (*ptr != ' ') - { - *p_rdrpath = *ptr; - p_rdrpath++; - } - ptr++; - } - *p_rdrpath = '\0'; - return (0); -} - -static void - ft_get_rdrfd(const char *ptr, - t_lcom **link) -{ - while (ft_isdigit(*ptr)) - { - ptr--; - } - if (*ptr != ' ') - (*link)->rdrfd = STDOUT_FILENO; - else - { - ptr += 1; - (*link)->rdrfd = ft_atoi(ptr); - } -} - -int8_t - ft_get_redir(const char word[], - t_lcom **link) -{ - /* TODO: norme */ - char *ptr; - - ptr = (char *)word; - while (*ptr) - { - if (*ptr == '<') - { - (*link)->redir = -1; - break ; - } - if (*ptr == '>') - { - (*link)->redir = (*(ptr + 1) == '>') ? (2) : (1); - break ; - } - ptr++; - /* TODO: handle correctly multiples "msh ~> echo qwe > qwe > asd >> zxc > qweasdzxc" */ - /* hint: bash only handles the last one */ - /* TODO: handle "msh ~> cat < Makefile >qwe" | gl hf */ - } - if ((*link)->redir > 0) - { - if (ft_isdigit(*(ptr - 1))) - ft_get_rdrfd(ptr - 1, link); - else - (*link)->rdrfd = STDOUT_FILENO; - ft_rdr_err_check(ptr, link); - if (ft_get_rdrpath(ptr, link) != 0) - return (-1); - } - return (0); -} - -int8_t - ft_p_lcom(const char line[], - const uint64_t count, - t_msh *msh) -{ - /* TODO: norme */ - uint64_t i; - t_lcom *link; - char **words; - t_bool next; - - i = 0; - if (!(words = ft_split(line, ';'))) - return (-1); - while (i <= count && words[i]) - { - next = FALSE; - /* TODO: split pipes here */ - if (ft_strchr(words[i], '|')) - { - if (!(link = ft_lcom_new(NULL, msh))) - return (-1); - if (!(ft_split_pipes(words[i], link, msh))) - return (-1); - next = TRUE; - } - if (next == FALSE && !(link = ft_lcom_new(words[i], msh))) - return (-1); - ft_lcom_add_back(&msh->curr, link); - i++; - } - ft_delwords(words); - return (0); -} diff --git a/src/ft_p_lcom.h b/src/ft_p_lcom.h deleted file mode 100644 index 256fe75..0000000 --- a/src/ft_p_lcom.h +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_p_lcom.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_P_LCOM_H -#define FT_P_LCOM_H - -#include - -#include "ft_s_struct.h" - -int8_t ft_get_redir(const char word[], - t_lcom **link); -int8_t ft_p_lcom(const char line[], - const uint64_t count, - t_msh *msh); - -#endif diff --git a/src/ft_p_lcom_next.c b/src/ft_p_lcom_next.c deleted file mode 100644 index 8313b08..0000000 --- a/src/ft_p_lcom_next.c +++ /dev/null @@ -1,97 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_p_lcom_next.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "ft_s_struct.h" -#include "ft_u_vars.h" - -static int8_t - ft_subst_those_vars(int64_t i, - char **p_words, - t_msh *msh) -{ - size_t varlen; - char *s_varname; - char *varval; - - varval = NULL; - s_varname = NULL; - varlen = i + 1; - while ((*p_words)[varlen] != '\0' && - !ft_ischarset("$=/#@%^*+{}[],.?-", (*p_words)[varlen])) - varlen += 1; - if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) - return (-1); - varval = ft_subst_var_value(s_varname, msh); - *p_words = ft_strsubst(*p_words, s_varname, varval); - ft_memdel((void*)&s_varname); - ft_memdel((void*)&varval); - return (0); -} - -char - **ft_subst_vars(char *words[], - t_msh *msh) -{ - char **p_words; - int64_t i; - - p_words = words; - i = 0; - while (*p_words) - { - while ((i = ft_strlchr((*p_words), '$')) != -1) - { - if (*(*p_words) + i - 1 != '\\') - { - if (ft_subst_those_vars(i, p_words, msh) != 0) - return (NULL); - } - } - p_words += 1; - } - return (words); -} - - -char - **ft_subst_args(const char word[], - int8_t redir) -{ - char **words; - char *subst; - size_t i; - - if (redir == 0) - { - if (!(words = ft_split(word, ' '))) - return (NULL); - return (words); - } - i = 0; - while (word[i] && !ft_ischarset("<>", word[i])) - i++; - while (redir > 0 && ft_isdigit(word[i])) - i--; - if (!(subst = ft_substr(word, 0, i))) - return (NULL); - if (!(words = ft_split(subst, ' '))) - { - ft_memdel((void*)&subst); - return (NULL); - } - ft_memdel((void*)&subst); - return (words); -} diff --git a/src/ft_p_lcom_next.h b/src/ft_p_lcom_next.h deleted file mode 100644 index 54eefd2..0000000 --- a/src/ft_p_lcom_next.h +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_p_lcom_next.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_P_LCOM_NEXT_H -#define FT_P_LCOM_NEXT_H - -#include - -#include "ft_s_struct.h" - -char **ft_subst_vars(char *words[], - t_msh *msh); -char **ft_subst_args(const char word[], - int8_t redir); - -#endif diff --git a/src/ft_p_line.c b/src/ft_p_line.c deleted file mode 100644 index 166b880..0000000 --- a/src/ft_p_line.c +++ /dev/null @@ -1,50 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_p_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include - -#include "ft_d_enum.h" -#include "ft_f_fail.h" -#include "ft_p_lcom.h" -#include "ft_p_line.h" -#include "ft_s_destroy.h" -#include "ft_s_struct.h" - -void - ft_p_line(char line[], - t_msh *msh) -{ - char *ptr; - uint64_t count; - - count = 0; - ptr = line; - while (*ptr != '\0') - { - - if (*ptr == ';') - { - count += 1; - } - ptr++; - } - if (*(ptr - 1) == ';') - { - count -= 1; - } - if (ft_p_lcom(line, count, msh) < 0) - { - ft_s_destroy(msh); - ft_fail_alloc(msh); - } -} diff --git a/src/ft_p_line.h b/src/ft_p_line.h deleted file mode 100644 index 076be42..0000000 --- a/src/ft_p_line.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_p_line.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_P_LINE_H -#define FT_P_LINE_H - -#include "ft_s_struct.h" - -void ft_p_line(char *line, - t_msh *msh); - -#endif diff --git a/src/ft_s_destroy.c b/src/ft_s_destroy.c deleted file mode 100644 index 22009c8..0000000 --- a/src/ft_s_destroy.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_destroy.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include - -#include "ft_s_destroy.h" -#include "ft_s_lvars.h" - -void - ft_s_destroy(t_msh *msh) -{ - ft_memdel((void*)&msh->ps_one); - ft_memdel((void*)&msh->cwd); - ft_memdel((void*)&msh->shname); - ft_delwords(msh->bu_ref); - ft_delwords(msh->envp); - ft_lvars_clear(&msh->vars); - ft_memdel((void*)&msh); -} diff --git a/src/ft_s_destroy.h b/src/ft_s_destroy.h deleted file mode 100644 index 68a5c09..0000000 --- a/src/ft_s_destroy.h +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_destroy.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_S_DESTROY_H -#define FT_S_DESTROY_H - -#include "ft_s_struct.h" - -void ft_s_destroy(t_msh *msh); - -#endif diff --git a/src/ft_s_init.c b/src/ft_s_init.c deleted file mode 100644 index 88a3817..0000000 --- a/src/ft_s_init.c +++ /dev/null @@ -1,85 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_init.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -#include "ft_d_define.h" -#include "ft_m_funptr.h" -#include "ft_s_init.h" - -static char - **ft_dupenv_del(char **nenvp, - uint64_t i) -{ - while (i > 0) - { - ft_memdel((void*)&nenvp[i]); - i--; - } - ft_memdel((void*)&nenvp); - return (NULL); -} - -static char - **ft_dupenv(char *const envp[]) -{ - uint64_t i; - char **nenvp; - - i = 0; - while (envp[i]) - { - i++; - } - if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*)))) - { - return (NULL); - } - i = 0; - while (envp[i]) - { - if (!(nenvp[i] = ft_strdup(envp[i]))) - return (ft_dupenv_del(nenvp, i)); - i++; - } - nenvp[i] = NULL; - return (nenvp); -} - -t_msh - *ft_init_msh(char *const argv[], - char *const envp[]) -{ - t_msh *msh; - - if (!(msh = (t_msh*)malloc(sizeof(t_msh)))) - return (NULL); - if (!(msh->ps_one = ft_strdup(FT_PS_ONE))) - return (NULL); - if (!(msh->shname = ft_strdup(argv[0]))) - return (NULL); - /* TODO: shname: care about "./", try with symlinks */ - msh->cwd = NULL; - msh->cwd = getcwd(NULL, 0); - /* TODO: handle getcwd failed */ - msh->envp = NULL; - if (!(msh->envp = ft_dupenv(envp))) - return (NULL); - msh->ret = 0; - ft_init_buptr(msh); - msh->curr = NULL; - msh->vars = NULL; - return (msh); -} diff --git a/src/ft_s_init.h b/src/ft_s_init.h deleted file mode 100644 index bd8edc7..0000000 --- a/src/ft_s_init.h +++ /dev/null @@ -1,22 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_init.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_S_INIT_H -#define FT_S_INIT_H - -#include -#include "ft_s_struct.h" - -t_msh *ft_init_msh(char *const argv[], - char *const envp[]); - -#endif diff --git a/src/ft_s_lcom.c b/src/ft_s_lcom.c deleted file mode 100644 index d56ba7f..0000000 --- a/src/ft_s_lcom.c +++ /dev/null @@ -1,145 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_lcom.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "ft_p_lcom.h" -#include "ft_p_lcom_next.h" -#include "ft_s_struct.h" - -static int8_t - ft_fill_lcom(char *words[], - t_lcom **lcom) -{ - /* TODO: norme */ - uint64_t i; - uint64_t j; - - i = 0; - if (words[0]) - { - if (!((*lcom)->com = (char*)malloc((ft_strlen(words[0]) + 1) * - sizeof(char)))) - return (-1); - ft_strlcpy((*lcom)->com, words[0], ft_strlen(words[0]) + 1); - } - else - return (0); - while(words[i]) - { - /* TODO: cut fd number "msh ~> echo a 2>file" */ - /* ^ */ - if (ft_ischarset("<>", words[i][0])) - break ; - i++; - } - if (!((*lcom)->argv = (char**)malloc((i + 1) * sizeof(char*)))) - return (-1); - j = 0; - while (i > 0 && j < i) - { - if (!((*lcom)->argv[j] = - (char*)malloc((ft_strlen(words[j]) + 1) * sizeof(char)))) - return (-1); - ft_strlcpy((*lcom)->argv[j], words[j], - ft_strlen(words[j]) + 1); - j++; - } - (*lcom)->argv[j] = 0; - return (0); -} - -t_lcom - *ft_lcom_last(t_lcom *lcom) -{ - while (lcom->next != NULL) - lcom = lcom->next; - return (lcom); -} - -void - ft_lcom_add_back(t_lcom **alcom, - t_lcom *new) -{ - t_lcom *tmp; - - if (!*alcom) - *alcom = new; - else - { - tmp = ft_lcom_last(*alcom); - tmp->next = new; - } -} - -void - ft_lcom_clear(t_lcom **lcom) -{ - t_lcom *tmp; - t_lcom *renext; - - if (!lcom) - return ; - tmp = *lcom; - while (tmp) - { - renext = tmp->next; - ft_memdel((void*)&tmp->com); - if (tmp->argv) - ft_delwords(tmp->argv); - if (tmp->redir != 0) - ft_memdel((void*)&tmp->rdrpath); - ft_memdel((void*)&tmp); - tmp = renext; - } - *lcom = NULL; -} - -t_lcom - *ft_lcom_new(const char word[], - t_msh *msh) -{ - /* TODO: norme */ - t_lcom *link; - char **words; - - if (!(link = (t_lcom*)malloc(sizeof(t_lcom)))) - return (NULL); - link->redir = 0; - link->com = NULL; - link->argv = NULL; - link->rdrfd = 0; - link->rdrpath = NULL; - link->pipes = NULL; - if (!word) - { - link->next = NULL; - return (link); - } - link->pipes = NULL; - if (ft_get_redir(word, &link) != 0) - return (NULL); - if (!(words = ft_subst_args(word, link->redir))) - return (NULL); - if (!(words = ft_subst_vars(words, msh))) - return (NULL); - if (ft_fill_lcom(words, &link) < 0) - { - ft_delwords(words); - return (NULL); - } - link->next = NULL; - ft_delwords(words); - return (link); -} diff --git a/src/ft_s_lcom.h b/src/ft_s_lcom.h deleted file mode 100644 index ea78d6f..0000000 --- a/src/ft_s_lcom.h +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_lcom.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_S_LCOM_H -#define FT_S_LCOM_H - -#include "ft_s_struct.h" - -void ft_lcom_add_back(t_lcom **alcom, t_lcom *new); -void ft_lcom_clear(t_lcom **lcom); -t_lcom *ft_lcom_new(const char word[], t_msh *msh); -t_lcom *ft_lcom_last(t_lcom *lcom); - -#endif diff --git a/src/ft_s_lpipes.c b/src/ft_s_lpipes.c deleted file mode 100644 index 8bd34de..0000000 --- a/src/ft_s_lpipes.c +++ /dev/null @@ -1,105 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_lpipes.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "ft_s_lcom.h" -#include "ft_s_lpipes.h" -#include "ft_s_struct.h" - -struct s_lpipes - *ft_lpipes_last(struct s_lpipes *lpipes) -{ - while (lpipes->next != NULL) - lpipes = lpipes->next; - return (lpipes); -} - -void - ft_lpipes_add_back(struct s_lpipes **alpipes, - struct s_lpipes *new) -{ - struct s_lpipes *tmp; - - if (!*alpipes) - *alpipes = new; - else - { - tmp = ft_lpipes_last(*alpipes); - tmp->next = new; - } -} - -void - ft_lpipes_clear(struct s_lpipes **lpipes) -{ - struct s_lpipes *tmp; - struct s_lpipes *renext; - - if (!lpipes) - return ; - tmp = *lpipes; - while (tmp) - { - renext = tmp->next; - ft_lcom_clear(&tmp->one); - ft_memdel((void*)&tmp); - tmp = renext; - } - *lpipes = NULL; -} - -struct s_lpipes - *ft_lpipes_new(const char pipedword[], - t_msh *msh) -{ - struct s_lpipes *link; - - if (!(link = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) - return (NULL); - link->one = NULL; - if (!(link->one = ft_lcom_new(pipedword, msh))) - { - return (NULL); - } - link->next = NULL; - return (link); -} - -struct s_lpipes - *ft_split_pipes(const char word[], - t_lcom *lcom, - t_msh *msh) -{ - struct s_lpipes *lpipes; - char **words; - size_t i; - - if (!(words = ft_split(word, '|'))) - return (NULL); - i = 0; - if (!(lpipes = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) - return (NULL); - while (words[i]) - { - if (!(lpipes = ft_lpipes_new(words[i], msh))) - { - return (NULL); - } - ft_lpipes_add_back(&lcom->pipes, lpipes); - i++; - } - ft_delwords(words); - return (lpipes); -} diff --git a/src/ft_s_lpipes.h b/src/ft_s_lpipes.h deleted file mode 100644 index 50fd693..0000000 --- a/src/ft_s_lpipes.h +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_lpipes.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_S_LPIPES_H -#define FT_S_LPIPES_H - -#include "ft_s_struct.h" - -struct s_lpipes *ft_lpipes_last(struct s_lpipes *lpipes); -void ft_lpipes_add_back(struct s_lpipes **alpipes, - struct s_lpipes *new); -void ft_lpipes_clear(struct s_lpipes **lpipes); -struct s_lpipes *ft_lpipes_new(const char pipedword[], t_msh *msh); -struct s_lpipes *ft_split_pipes(const char word[], t_lcom *lcom, t_msh *msh); - -#endif diff --git a/src/ft_s_lvars.c b/src/ft_s_lvars.c deleted file mode 100644 index fb3a0f4..0000000 --- a/src/ft_s_lvars.c +++ /dev/null @@ -1,126 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_lvars.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -#include "ft_s_struct.h" - -void - ft_lvars_rebind(t_lvars **lvars, - const char name[], - const char newval[]) -{ - t_lvars *tmp; - - tmp = *lvars; - while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) - { - tmp = tmp->next; - } - if (tmp == NULL) - { - return ; - } - ft_memdel((void*)&tmp->val); - if (!(tmp->val = ft_strdup(newval))) - { - ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); - exit(FT_RET_ALLOC); - } -} - -void - ft_lvars_delone(t_lvars **lvars, - const char name[]) -{ - t_lvars *tmp; - t_lvars *prev; - - tmp = *lvars; - if (tmp != NULL && !ft_strncmp(tmp->name, name, ft_strlen(name))) - { - *lvars = tmp->next; - ft_memdel((void*)&tmp->name); - ft_memdel((void*)&tmp->val); - ft_memdel((void*)&tmp); - return ; - } - while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) - { - prev = tmp; - tmp = tmp->next; - } - if (tmp == NULL) - return ; - prev->next = tmp->next; - ft_memdel((void*)&tmp->name); - ft_memdel((void*)&tmp->val); - ft_memdel((void*)&tmp); -} - -void - ft_lvars_add_front(t_lvars **alvars, - t_lvars *new) -{ - if (!alvars || !new) - { - return ; - } - new->next = *alvars; - *alvars = new; -} - -void - ft_lvars_clear(t_lvars **lvars) -{ - t_lvars *tmp; - t_lvars *renext; - - if (!lvars) - return ; - tmp = *lvars; - while (tmp) - { - renext = tmp->next; - ft_memdel((void*)&tmp->name); - ft_memdel((void*)&tmp->val); - ft_memdel((void*)&tmp); - tmp = renext; - } - *lvars = NULL; -} - -t_lvars - *ft_lvars_new(const char name[], - const char val[]) -{ - t_lvars *link; - - if (!(link = (t_lvars*)malloc(sizeof(t_lvars)))) - { - return (NULL); - } - if (!(link->name = ft_strdup(name))) - { - return (NULL); - } - if (!(link->val = ft_strdup(val))) - { - return (NULL); - } - link->next = NULL; - return (link); -} diff --git a/src/ft_s_lvars.h b/src/ft_s_lvars.h deleted file mode 100644 index 0f46d7d..0000000 --- a/src/ft_s_lvars.h +++ /dev/null @@ -1,29 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_lvars.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_S_LVARS_H -#define FT_S_LVARS_H - -#include "ft_s_struct.h" - -void ft_lvars_rebind(t_lvars **lvars, - const char name[], - const char newval[]); -void ft_lvars_delone(t_lvars **lvars, - const char name[]); -void ft_lvars_add_front(t_lvars **alvars, - t_lvars *new); -void ft_lvars_clear(t_lvars **lvars); -t_lvars *ft_lvars_new(const char name[], - const char val[]); - -#endif diff --git a/src/ft_s_struct.h b/src/ft_s_struct.h deleted file mode 100644 index 3a8df0a..0000000 --- a/src/ft_s_struct.h +++ /dev/null @@ -1,68 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_s_struct.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_S_STRUCT_H -#define FT_S_STRUCT_H - -#include - -#include "ft_d_define.h" - -/* -** redir(int8_t) index -** -------------------- -** -1: < -** 1: > -** 2: >> -** 0: means no redirection -*/ - -typedef struct s_lpipes *t_lpipes; - -typedef struct s_lvars -{ - char *name; - char *val; - struct s_lvars *next; -} t_lvars; - -typedef struct s_lcom -{ - char *com; - char **argv; - int8_t redir; - int32_t rdrfd; - char *rdrpath; - struct s_lpipes *pipes; - struct s_lcom *next; -} t_lcom; - -struct s_lpipes -{ - struct s_lcom *one; - struct s_lpipes *next; -}; - -typedef struct s_msh -{ - char **envp; - char *ps_one; - char *cwd; - uint8_t ret; - char *shname; - char **bu_ref; - uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); - struct s_lcom *curr; - struct s_lvars *vars; -} t_msh; - -#endif diff --git a/src/ft_u_utils.c b/src/ft_u_utils.c deleted file mode 100644 index b7ab7d8..0000000 --- a/src/ft_u_utils.c +++ /dev/null @@ -1,28 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_u_utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -uint64_t - ft_get_argc(const char *args[]) -{ - uint64_t argc; - - argc = 0; - while (args[argc]) - { - argc++; - } - return (argc); -} diff --git a/src/ft_u_utils.h b/src/ft_u_utils.h deleted file mode 100644 index 1a3b324..0000000 --- a/src/ft_u_utils.h +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_u_utils.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_U_UTILS_H -#define FT_U_UTILS_H - -#include - -uint64_t ft_get_argc(const char *args[]); - -#endif diff --git a/src/ft_u_vars.c b/src/ft_u_vars.c deleted file mode 100644 index cb020e3..0000000 --- a/src/ft_u_vars.c +++ /dev/null @@ -1,163 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_u_vars.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include - -#include "ft_d_define.h" -#include "ft_f_fail.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - - -static char - *ft_set_rva(const char varname[], - t_msh *msh) -{ - char *rvarname; - - if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char)))) - { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - ft_memcpy((char*)rvarname, (const char*)varname + 1, - ft_strlen(varname + 1)); - *(rvarname + ft_strlen(varname + 1)) = '='; - *(rvarname + ft_strlen(varname + 1) + 1) = '\0'; - return (rvarname); -} - -static char - *ft_dup_env(char *p_env, - char *rvarname, - t_msh *msh) -{ - char *varval; - - if (!(varval = ft_strdup(p_env))) - { - ft_memdel((void*)&rvarname); - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - ft_memdel((void*)&rvarname); - return (varval); -} - -static char - *ft_get_frm_env(char rvarname[], - t_msh *msh) -{ - char **p_env; - char *pp_env; - char *varval; - - p_env = msh->envp; - while (*p_env) - { - if (!ft_strncmp(rvarname, *p_env, ft_strlen(rvarname))) - { - pp_env = *p_env; - while (*pp_env != '\0' && *pp_env != '=') - pp_env += 1; - if (*pp_env == '=') - pp_env += 1; - varval = ft_dup_env(pp_env, rvarname, msh); - return (varval); - } - p_env += 1; - } - return (NULL); -} - -static char - *ft_get_special_var(const char varname[], - t_msh *msh) -{ - char *varval; - - if (!ft_strncmp(varname, FT_RET_VAR, 3)) - { - if (!(varval = ft_uitoa(msh->ret))) - { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - return (varval); - } - else if (!ft_strncmp(varname, FT_ZER_VAR, 3)) - { - if (!(varval = ft_strdup(msh->shname))) - { - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - ft_fail_alloc(msh); - } - return (varval); - } - return (NULL); -} - -static char - *ft_get_cstm_vr(const char varname[], - t_msh *msh) -{ - (void)varname; - (void)msh; - return (NULL); -} - -/* -** char * -** ft_subst_var_value(const char varname[], const t_msh *msh); -** -** DESCRIPTION -** The ft_subst_var_value() function returns -** a heap-allocated, null-terminated string -** that may later be free'd containing the -** value of the variable varname[] including -** the '$' prefix. NULL is returned if varname[] -** wasn't found. -*/ - -char - *ft_subst_var_value(const char varname[], - t_msh *msh) -{ - /* TODO: check behaviour on empty vars -> "QWE=" */ - /* TODO: add support for global variables -> "$hey $nigga..." */ - char *varval; - char *rvarname; - - varval = NULL; - if ((varval = ft_get_special_var(varname, msh)) != NULL) - { - return (varval); - } - else if ((varval = ft_get_cstm_vr(varname, msh)) != NULL) - { - return (varval); - } - else - { - rvarname = ft_set_rva(varname, msh); - varval = ft_get_frm_env(rvarname, msh); - return (varval); - } - return (NULL); -} diff --git a/src/ft_u_vars.h b/src/ft_u_vars.h deleted file mode 100644 index 82aa668..0000000 --- a/src/ft_u_vars.h +++ /dev/null @@ -1,21 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_u_vars.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_U_VARS_H -#define FT_U_VARS_H - -#include "ft_s_struct.h" - -char *ft_subst_var_value(const char varname[], - t_msh *msh); - -#endif diff --git a/src/m_argv.c b/src/m_argv.c new file mode 100644 index 0000000..6a79bd7 --- /dev/null +++ b/src/m_argv.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_argv.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" +#include "m_comm.h" +#include "m_loop.h" +#include "s_struct.h" + +uint8_t + m_argv(int argc, + char *const argv[], + t_msh *msh) +{ + /* TODO: better argv handling */ + if (argc == 1) + { + msh->ret = m_loop(msh); + return (msh->ret); + } + if (!ft_strncmp(*(argv + 1), FT_OPT_COMMAND, 3)) + { + if (*(argv + 2) == NULL) + { + ft_dprintf(2, "%s: %s: option requires an argument\n", + msh->shname, FT_OPT_COMMAND); + return (2); + } + msh->ret = m_comm(*(argv + 2), msh); + } + return (msh->ret); +} diff --git a/src/m_argv.h b/src/m_argv.h new file mode 100644 index 0000000..cdda5d5 --- /dev/null +++ b/src/m_argv.h @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_argv.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_ARGV_H +#define M_ARGV_H + +#include + +#include "s_struct.h" + +uint8_t m_argv(int argc, + char *const argv[], + t_msh *msh); + +#endif diff --git a/src/m_comm.c b/src/m_comm.c new file mode 100644 index 0000000..66e0ac2 --- /dev/null +++ b/src/m_comm.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_comm.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_enum.h" +#include "e_lcom.h" +#include "m_prompt.h" +#include "p_line.h" +#include "s_lcom.h" + +uint8_t + m_comm(const char line[], + t_msh *msh) +{ + if (line[0] != '\0') + { + p_line((char*)line, msh); + e_lcom(msh); + lcom_clear(&msh->curr); + } + else + { + ft_memdel((void*)&line); + } + return (msh->ret); +} + diff --git a/src/m_comm.h b/src/m_comm.h new file mode 100644 index 0000000..a64d01e --- /dev/null +++ b/src/m_comm.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_comm.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_COMM_H +#define M_COMM_H + +#include + +#include "s_struct.h" + +uint8_t m_comm(const char line[], + t_msh *msh); + +#endif diff --git a/src/m_funptr.c b/src/m_funptr.c new file mode 100644 index 0000000..5a02b2e --- /dev/null +++ b/src/m_funptr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_funptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_enum.h" +#include "b_builtins.h" +#include "f_fail.h" +#include "m_funptr.h" +#include "s_struct.h" + +void + init_buptr(t_msh *msh) +{ + msh->bu_ptr[0] = b_echo; + msh->bu_ptr[1] = b_cd; + msh->bu_ptr[2] = b_pwd; + msh->bu_ptr[3] = b_export; + msh->bu_ptr[4] = b_unset; + msh->bu_ptr[5] = b_env; + msh->bu_ptr[6] = b_exit; + msh->bu_ptr[7] = b_type; + if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|'))) + { + fail_alloc(msh); + } +} diff --git a/src/m_funptr.h b/src/m_funptr.h new file mode 100644 index 0000000..1a0f198 --- /dev/null +++ b/src/m_funptr.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_funptr.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_FUNPTR_H +#define M_FUNPTR_H + +#include "s_struct.h" + +void init_buptr(t_msh *msh); + +#endif diff --git a/src/m_loop.c b/src/m_loop.c new file mode 100644 index 0000000..fe3fbcd --- /dev/null +++ b/src/m_loop.c @@ -0,0 +1,126 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_loop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "e_lcom.h" +#include "m_prompt.h" +#include "p_line.h" +#include "s_lcom.h" + + +static char + *strjoin_m(char *s1, + char *s2, + uint8_t mode) +{ + size_t i; + size_t j; + size_t size1; + size_t size2; + char *dst; + + i = -1; + j = -1; + size1 = ft_strlen(s1); + size2 = ft_strlen(s2) + 1; + if (!(dst = (char*)malloc((size1 + size2 + 2) * sizeof(char)))) + return (NULL); + while (++i < size1) + dst[i] = s1[i]; + (mode == 0) ? dst[i++] = '\n' : 0; + while (++j < size2) + dst[i + j] = s2[j]; + dst[i + j] = '\0'; + (mode == 0) ? ft_memdel((void*)&s1) : ft_memdel((void*)&s2); + return (dst); +} + +void + m_loop_cont(t_msh *msh, + char *line, + char *quote, + int8_t gnl) +{ + char *prog; + char *buf; + char *fin; + + if (!(prog = ft_calloc(ft_strlen(line) + 1, sizeof(char))) || + !(buf = ft_strdup(line + (quote - line) + 1))) + return ; + ft_memcpy(prog, line, (quote - line)); + while (gnl > 0 && ((!(quote = ft_strrchr(buf, '\"'))) && + (!(quote = ft_strrchr(buf, '\''))))) + { + m_cont_prompt(); + gnl = get_next_line(STDIN_FILENO, &line); + buf = strjoin_m(buf, line, 0); + ft_memdel((void*)&line); + } + fin = ft_strtrim(buf, "\"'"); + ft_memdel((void*)&buf); + fin = (ft_strrchr(fin, '\'')) ? ft_strsubst(fin, "\'", "") : fin; + fin = strjoin_m(prog, fin, 1); + p_line(fin, msh); + ft_memdel((void*)&prog); + ft_memdel((void*)&fin); + e_lcom(msh); + lcom_clear(&msh->curr); +} + +uint8_t + m_loop(t_msh *msh) +{ + char *line; + char *quote; + int8_t gnl; + int32_t pipefd[2]; + + gnl = 1; + pipefd[0] = STDOUT_FILENO; + pipefd[1] = STDIN_FILENO; + while (gnl > 0) + { + m_prompt(msh); + gnl = get_next_line(STDIN_FILENO, &line); + if (line[0] != '\0') + { + if (!(quote = ft_strchr(line, '\'')) && !(quote = ft_strchr(line, '\"'))) + { + p_line(line, msh); + ft_memdel((void*)&line); + e_lcom(msh); + lcom_clear(&msh->curr); + } + else + { + m_loop_cont(msh, line, quote, 1); + if (line != NULL) + ft_memdel((void*)&line); + } + /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ + /* TODO: GNL 25 leak on "msh ~> exit" | gl hf */ + /* TODO: "msh ~> some command \": re GNL into ft_nrealloc */ + /* TODO: the chad pipes | */ + /* TODO: a histfile would be nice */ + } + else + { + ft_memdel((void*)&line); + } + } + return (msh->ret); +} diff --git a/src/m_loop.h b/src/m_loop.h new file mode 100644 index 0000000..1d665e3 --- /dev/null +++ b/src/m_loop.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_loop.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_LOOP +#define M_LOOP + +#include + +#include "s_struct.h" + +uint8_t m_loop(t_msh *msh); + +#endif diff --git a/src/m_minishell.c b/src/m_minishell.c new file mode 100644 index 0000000..1e065b4 --- /dev/null +++ b/src/m_minishell.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_minishell.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "f_fail.h" +#include "m_argv.h" +#include "s_struct.h" +#include "s_init.h" +#include "s_destroy.h" + +int + main(int argc, + char *const argv[], + char *const envp[]) +{ + t_msh *msh; + int32_t ret; + + /* TODO: increment $SHLVL */ + /* TODO: also set $SHELL | maybe not bash does not set $SHELL */ + /* only zsh does */ + /* TODO: handle general variables | $var */ + if (!(msh = init_msh(argv, envp))) + { + ft_dprintf(2, "%s\n", strerror(errno)); + return (FT_RET_ALLOC); + } + ret = m_argv(argc, argv, msh); + s_destroy(msh); + return (ret); +} + +/* +** ====== INFO ====== +** Files prefixes info +** ------------------- +** b_ -> builtins related +** d_ -> defines related +** e_ -> exec related +** f_ -> failure related +** m_ -> core minishell related +** p_ -> parse related +** s_ -> structs related +** u_ -> utils related +** v_ -> general purpose variables related +*/ diff --git a/src/m_minishell.h b/src/m_minishell.h new file mode 100644 index 0000000..06ecfed --- /dev/null +++ b/src/m_minishell.h @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_minishell.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_MINISHELL_H +#define M_MINISHELL_H + +#endif diff --git a/src/m_prompt.c b/src/m_prompt.c new file mode 100644 index 0000000..34aae09 --- /dev/null +++ b/src/m_prompt.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_prompt.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "m_prompt.h" +#include "s_struct.h" + +void + m_cont_prompt(void) +{ + ft_dprintf(STDIN_FILENO, "> "); + /* TODO :ft_printf("%s", msh->ps_two); */ +} + +void + m_prompt(t_msh *msh) +{ + ft_dprintf(STDIN_FILENO, "%s", msh->ps_one); +} diff --git a/src/m_prompt.h b/src/m_prompt.h new file mode 100644 index 0000000..f929298 --- /dev/null +++ b/src/m_prompt.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_prompt.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_PROMPT_H +#define M_PROMPT_H + +#include "s_struct.h" + +void m_prompt(t_msh *msh); +void m_cont_prompt(void); +#endif diff --git a/src/m_redirs.c b/src/m_redirs.c new file mode 100644 index 0000000..2414dcd --- /dev/null +++ b/src/m_redirs.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_redirs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "f_fail.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" + +void + dup_redirs(const t_lcom *ptr, + t_msh *msh) +{ + int32_t fd; + + if (ptr->redir == -1) + { + if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) + f_redir(ptr->rdrpath, msh); + dup2(fd, STDIN_FILENO); + close(fd); + } + if (ptr->redir == 1) + { + if ((fd = open(ptr->rdrpath, + O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) + f_redir(ptr->rdrpath, msh); + dup2(fd, ptr->rdrfd); + close(fd); + } + if (ptr->redir == 2) + { + if ((fd = open(ptr->rdrpath, + O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) + f_redir(ptr->rdrpath, msh); + dup2(fd, ptr->rdrfd); + close(fd); + } +} diff --git a/src/m_redirs.h b/src/m_redirs.h new file mode 100644 index 0000000..295ea4d --- /dev/null +++ b/src/m_redirs.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* m_redirs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef M_REDIRS_H +#define M_REDIRS_H + +#include "s_struct.h" + +void dup_redirs(const t_lcom *ptr, + t_msh *msh); + +#endif diff --git a/src/minishell.c b/src/minishell.c deleted file mode 100644 index ee79f8f..0000000 --- a/src/minishell.c +++ /dev/null @@ -1,61 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* minishell.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include - -#include "ft_f_fail.h" -#include "ft_m_argv.h" -#include "ft_s_struct.h" -#include "ft_s_init.h" -#include "ft_s_destroy.h" - -int - main(int argc, - char *const argv[], - char *const envp[]) -{ - t_msh *msh; - int32_t ret; - - /* TODO: increment $SHLVL */ - /* TODO: also set $SHELL | maybe not bash does not set $SHELL */ - /* only zsh does */ - /* TODO: handle general variables | $var */ - if (!(msh = ft_init_msh(argv, envp))) - { - ft_dprintf(2, "%s\n", strerror(errno)); - return (FT_RET_ALLOC); - } - ret = ft_m_argv(argc, argv, msh); - ft_s_destroy(msh); - return (ret); -} - -/* -** ====== INFO ====== -** Files prefixes info -** ------------------- -** ft_ -> 42 -** b_ -> builtins related -** d_ -> defines related -** e_ -> exec related -** f_ -> failure related -** m_ -> core minishell related -** p_ -> parse related -** s_ -> structs related -** u_ -> utils related -** v_ -> general purpose variables related -*/ diff --git a/src/minishell.h b/src/minishell.h deleted file mode 100644 index a30d8bc..0000000 --- a/src/minishell.h +++ /dev/null @@ -1,16 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* minishell.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef MINISHELL_H -#define MINISHELL_H - -#endif diff --git a/src/p_lcom.c b/src/p_lcom.c new file mode 100644 index 0000000..f47e2c1 --- /dev/null +++ b/src/p_lcom.c @@ -0,0 +1,158 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_lcom.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include + +#include "d_define.h" +#include "f_fail.h" +#include "s_lcom.h" +#include "s_lpipes.h" +#include "s_struct.h" + +static void + rdr_err_check(char *ptr, + t_lcom **link) +{ + if ((*link)->redir == -1 && ft_ischarset("><", *(ptr + 1))) + { + /* TODO: syntax err */ + } + else if ((*link)->redir == 1 && ft_ischarset("<", *(ptr + 1))) + { + /* TODO: syntax err */ + } + else if ((*link)->redir == 2 && ft_ischarset("<>", *(ptr + 1))) + { + /* TODO: syntax err */ + } +} + +static int8_t + get_rdrpath(char *ptr, + t_lcom **link) +{ + char *p_rdrpath; + + ptr += ((*link)->redir == 2) ? (2) : (1); + if (!((*link)->rdrpath = + (char*)malloc((ft_strlen(ptr) + 1) * sizeof(char)))) + { + return (-1); + } + p_rdrpath = (*link)->rdrpath; + while (*ptr) + { + if (*ptr != ' ') + { + *p_rdrpath = *ptr; + p_rdrpath++; + } + ptr++; + } + *p_rdrpath = '\0'; + return (0); +} + +static void + get_rdrfd(const char *ptr, + t_lcom **link) +{ + while (ft_isdigit(*ptr)) + { + ptr--; + } + if (*ptr != ' ') + (*link)->rdrfd = STDOUT_FILENO; + else + { + ptr += 1; + (*link)->rdrfd = ft_atoi(ptr); + } +} + +int8_t + get_redir(const char word[], + t_lcom **link) +{ + /* TODO: norme */ + char *ptr; + + ptr = (char *)word; + while (*ptr) + { + if (*ptr == '<') + { + (*link)->redir = -1; + break ; + } + if (*ptr == '>') + { + (*link)->redir = (*(ptr + 1) == '>') ? (2) : (1); + break ; + } + ptr++; + /* TODO: handle correctly multiples "msh ~> echo qwe > qwe > asd >> zxc > qweasdzxc" */ + /* hint: bash only handles the last one */ + /* TODO: handle "msh ~> cat < Makefile >qwe" | gl hf */ + } + if ((*link)->redir > 0) + { + if (ft_isdigit(*(ptr - 1))) + get_rdrfd(ptr - 1, link); + else + (*link)->rdrfd = STDOUT_FILENO; + rdr_err_check(ptr, link); + if (get_rdrpath(ptr, link) != 0) + return (-1); + } + return (0); +} + +int8_t + p_lcom(const char line[], + const uint64_t count, + t_msh *msh) +{ + /* TODO: norme */ + uint64_t i; + t_lcom *link; + char **words; + t_bool next; + + i = 0; + if (!(words = ft_split(line, ';'))) + return (-1); + while (i <= count && words[i]) + { + next = FALSE; + /* TODO: split pipes here */ + if (ft_strchr(words[i], '|')) + { + if (!(link = lcom_new(NULL, msh))) + return (-1); + if (!(split_pipes(words[i], link, msh))) + return (-1); + next = TRUE; + } + if (next == FALSE && !(link = lcom_new(words[i], msh))) + return (-1); + lcom_add_back(&msh->curr, link); + i++; + } + ft_delwords(words); + return (0); +} diff --git a/src/p_lcom.h b/src/p_lcom.h new file mode 100644 index 0000000..4e37399 --- /dev/null +++ b/src/p_lcom.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_lcom.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_LCOM_H +#define P_LCOM_H + +#include + +#include "s_struct.h" + +int8_t get_redir(const char word[], + t_lcom **link); +int8_t p_lcom(const char line[], + const uint64_t count, + t_msh *msh); + +#endif diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c new file mode 100644 index 0000000..94ac9bf --- /dev/null +++ b/src/p_lcom_next.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_lcom_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "s_struct.h" +#include "u_vars.h" + +static int8_t + subst_those_vars(int64_t i, + char **p_words, + t_msh *msh) +{ + size_t varlen; + char *s_varname; + char *varval; + + varval = NULL; + s_varname = NULL; + varlen = i + 1; + while ((*p_words)[varlen] != '\0' && + !ft_ischarset("$=/#@%^*+{}[],.?-", (*p_words)[varlen])) + varlen += 1; + if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) + return (-1); + varval = subst_var_value(s_varname, msh); + *p_words = ft_strsubst(*p_words, s_varname, varval); + ft_memdel((void*)&s_varname); + ft_memdel((void*)&varval); + return (0); +} + +char + **subst_vars(char *words[], + t_msh *msh) +{ + char **p_words; + int64_t i; + + p_words = words; + i = 0; + while (*p_words) + { + while ((i = ft_strlchr((*p_words), '$')) != -1) + { + if (*(*p_words) + i - 1 != '\\') + { + if (subst_those_vars(i, p_words, msh) != 0) + return (NULL); + } + } + p_words += 1; + } + return (words); +} + + +char + **subst_args(const char word[], + int8_t redir) +{ + char **words; + char *subst; + size_t i; + + if (redir == 0) + { + if (!(words = ft_split(word, ' '))) + return (NULL); + return (words); + } + i = 0; + while (word[i] && !ft_ischarset("<>", word[i])) + i++; + while (redir > 0 && ft_isdigit(word[i])) + i--; + if (!(subst = ft_substr(word, 0, i))) + return (NULL); + if (!(words = ft_split(subst, ' '))) + { + ft_memdel((void*)&subst); + return (NULL); + } + ft_memdel((void*)&subst); + return (words); +} diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h new file mode 100644 index 0000000..3113b6b --- /dev/null +++ b/src/p_lcom_next.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_lcom_next.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_LCOM_NEXT_H +#define P_LCOM_NEXT_H + +#include + +#include "s_struct.h" + +char **subst_vars(char *words[], + t_msh *msh); +char **subst_args(const char word[], + int8_t redir); + +#endif diff --git a/src/p_line.c b/src/p_line.c new file mode 100644 index 0000000..4a58537 --- /dev/null +++ b/src/p_line.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_enum.h" +#include "f_fail.h" +#include "p_lcom.h" +#include "p_line.h" +#include "s_destroy.h" +#include "s_struct.h" + +void + p_line(char line[], + t_msh *msh) +{ + char *ptr; + uint64_t count; + + count = 0; + ptr = line; + while (*ptr != '\0') + { + + if (*ptr == ';') + { + count += 1; + } + ptr++; + } + if (*(ptr - 1) == ';') + { + count -= 1; + } + if (p_lcom(line, count, msh) < 0) + { + s_destroy(msh); + fail_alloc(msh); + } +} diff --git a/src/p_line.h b/src/p_line.h new file mode 100644 index 0000000..7e8a321 --- /dev/null +++ b/src/p_line.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef P_LINE_H +#define P_LINE_H + +#include "s_struct.h" + +void p_line(char *line, + t_msh *msh); + +#endif diff --git a/src/s_destroy.c b/src/s_destroy.c new file mode 100644 index 0000000..f9d56eb --- /dev/null +++ b/src/s_destroy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +#include "s_destroy.h" +#include "s_lvars.h" + +void + s_destroy(t_msh *msh) +{ + ft_memdel((void*)&msh->ps_one); + ft_memdel((void*)&msh->cwd); + ft_memdel((void*)&msh->shname); + ft_delwords(msh->bu_ref); + ft_delwords(msh->envp); + lvars_clear(&msh->vars); + ft_memdel((void*)&msh); +} diff --git a/src/s_destroy.h b/src/s_destroy.h new file mode 100644 index 0000000..66aea91 --- /dev/null +++ b/src/s_destroy.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_destroy.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef S_DESTROY_H +#define S_DESTROY_H + +#include "s_struct.h" + +void s_destroy(t_msh *msh); + +#endif diff --git a/src/s_init.c b/src/s_init.c new file mode 100644 index 0000000..454cb67 --- /dev/null +++ b/src/s_init.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +#include "d_define.h" +#include "m_funptr.h" +#include "s_init.h" + +static char + **dupenv_del(char **nenvp, + uint64_t i) +{ + while (i > 0) + { + ft_memdel((void*)&nenvp[i]); + i--; + } + ft_memdel((void*)&nenvp); + return (NULL); +} + +static char + **dupenv(char *const envp[]) +{ + uint64_t i; + char **nenvp; + + i = 0; + while (envp[i]) + { + i++; + } + if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*)))) + { + return (NULL); + } + i = 0; + while (envp[i]) + { + if (!(nenvp[i] = ft_strdup(envp[i]))) + return (dupenv_del(nenvp, i)); + i++; + } + nenvp[i] = NULL; + return (nenvp); +} + +t_msh + *init_msh(char *const argv[], + char *const envp[]) +{ + t_msh *msh; + + if (!(msh = (t_msh*)malloc(sizeof(t_msh)))) + return (NULL); + if (!(msh->ps_one = ft_strdup(FT_PS_ONE))) + return (NULL); + if (!(msh->shname = ft_strdup(argv[0]))) + return (NULL); + /* TODO: shname: care about "./", try with symlinks */ + msh->cwd = NULL; + msh->cwd = getcwd(NULL, 0); + /* TODO: handle getcwd failed */ + msh->envp = NULL; + if (!(msh->envp = dupenv(envp))) + return (NULL); + msh->ret = 0; + init_buptr(msh); + msh->curr = NULL; + msh->vars = NULL; + return (msh); +} diff --git a/src/s_init.h b/src/s_init.h new file mode 100644 index 0000000..8ffe954 --- /dev/null +++ b/src/s_init.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_init.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef S_INIT_H +#define S_INIT_H + +#include + +#include "s_struct.h" + +t_msh *init_msh(char *const argv[], + char *const envp[]); + +#endif diff --git a/src/s_lcom.c b/src/s_lcom.c new file mode 100644 index 0000000..31b14d6 --- /dev/null +++ b/src/s_lcom.c @@ -0,0 +1,145 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lcom.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "p_lcom.h" +#include "p_lcom_next.h" +#include "s_struct.h" + +static int8_t + fill_lcom(char *words[], + t_lcom **lcom) +{ + /* TODO: norme */ + uint64_t i; + uint64_t j; + + i = 0; + if (words[0]) + { + if (!((*lcom)->com = (char*)malloc((ft_strlen(words[0]) + 1) * + sizeof(char)))) + return (-1); + ft_strlcpy((*lcom)->com, words[0], ft_strlen(words[0]) + 1); + } + else + return (0); + while(words[i]) + { + /* TODO: cut fd number "msh ~> echo a 2>file" */ + /* ^ */ + if (ft_ischarset("<>", words[i][0])) + break ; + i++; + } + if (!((*lcom)->argv = (char**)malloc((i + 1) * sizeof(char*)))) + return (-1); + j = 0; + while (i > 0 && j < i) + { + if (!((*lcom)->argv[j] = + (char*)malloc((ft_strlen(words[j]) + 1) * sizeof(char)))) + return (-1); + ft_strlcpy((*lcom)->argv[j], words[j], + ft_strlen(words[j]) + 1); + j++; + } + (*lcom)->argv[j] = 0; + return (0); +} + +t_lcom + *lcom_last(t_lcom *lcom) +{ + while (lcom->next != NULL) + lcom = lcom->next; + return (lcom); +} + +void + lcom_add_back(t_lcom **alcom, + t_lcom *new) +{ + t_lcom *tmp; + + if (!*alcom) + *alcom = new; + else + { + tmp = lcom_last(*alcom); + tmp->next = new; + } +} + +void + lcom_clear(t_lcom **lcom) +{ + t_lcom *tmp; + t_lcom *renext; + + if (!lcom) + return ; + tmp = *lcom; + while (tmp) + { + renext = tmp->next; + ft_memdel((void*)&tmp->com); + if (tmp->argv) + ft_delwords(tmp->argv); + if (tmp->redir != 0) + ft_memdel((void*)&tmp->rdrpath); + ft_memdel((void*)&tmp); + tmp = renext; + } + *lcom = NULL; +} + +t_lcom + *lcom_new(const char word[], + t_msh *msh) +{ + /* TODO: norme */ + t_lcom *link; + char **words; + + if (!(link = (t_lcom*)malloc(sizeof(t_lcom)))) + return (NULL); + link->redir = 0; + link->com = NULL; + link->argv = NULL; + link->rdrfd = 0; + link->rdrpath = NULL; + link->pipes = NULL; + if (!word) + { + link->next = NULL; + return (link); + } + link->pipes = NULL; + if (get_redir(word, &link) != 0) + return (NULL); + if (!(words = subst_args(word, link->redir))) + return (NULL); + if (!(words = subst_vars(words, msh))) + return (NULL); + if (fill_lcom(words, &link) < 0) + { + ft_delwords(words); + return (NULL); + } + link->next = NULL; + ft_delwords(words); + return (link); +} diff --git a/src/s_lcom.h b/src/s_lcom.h new file mode 100644 index 0000000..10fcf7c --- /dev/null +++ b/src/s_lcom.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lcom.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef S_LCOM_H +#define S_LCOM_H + +#include "s_struct.h" + +void lcom_add_back(t_lcom **alcom, t_lcom *new); +void lcom_clear(t_lcom **lcom); +t_lcom *lcom_new(const char word[], t_msh *msh); +t_lcom *lcom_last(t_lcom *lcom); + +#endif diff --git a/src/s_lpipes.c b/src/s_lpipes.c new file mode 100644 index 0000000..cb78df3 --- /dev/null +++ b/src/s_lpipes.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lpipes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "s_lcom.h" +#include "s_lpipes.h" +#include "s_struct.h" + +struct s_lpipes + *lpipes_last(struct s_lpipes *lpipes) +{ + while (lpipes->next != NULL) + lpipes = lpipes->next; + return (lpipes); +} + +void + lpipes_add_back(struct s_lpipes **alpipes, + struct s_lpipes *new) +{ + struct s_lpipes *tmp; + + if (!*alpipes) + *alpipes = new; + else + { + tmp = lpipes_last(*alpipes); + tmp->next = new; + } +} + +void + lpipes_clear(struct s_lpipes **lpipes) +{ + struct s_lpipes *tmp; + struct s_lpipes *renext; + + if (!lpipes) + return ; + tmp = *lpipes; + while (tmp) + { + renext = tmp->next; + lcom_clear(&tmp->one); + ft_memdel((void*)&tmp); + tmp = renext; + } + *lpipes = NULL; +} + +struct s_lpipes + *lpipes_new(const char pipedword[], + t_msh *msh) +{ + struct s_lpipes *link; + + if (!(link = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) + return (NULL); + link->one = NULL; + if (!(link->one = lcom_new(pipedword, msh))) + { + return (NULL); + } + link->next = NULL; + return (link); +} + +struct s_lpipes + *split_pipes(const char word[], + t_lcom *lcom, + t_msh *msh) +{ + struct s_lpipes *lpipes; + char **words; + size_t i; + + if (!(words = ft_split(word, '|'))) + return (NULL); + i = 0; + if (!(lpipes = (struct s_lpipes*)malloc(sizeof(struct s_lpipes)))) + return (NULL); + while (words[i]) + { + if (!(lpipes = lpipes_new(words[i], msh))) + { + return (NULL); + } + lpipes_add_back(&lcom->pipes, lpipes); + i++; + } + ft_delwords(words); + return (lpipes); +} diff --git a/src/s_lpipes.h b/src/s_lpipes.h new file mode 100644 index 0000000..1fc6eec --- /dev/null +++ b/src/s_lpipes.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lpipes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef S_LPIPES_H +#define S_LPIPES_H + +#include "s_struct.h" + +struct s_lpipes *lpipes_last(struct s_lpipes *lpipes); +void lpipes_add_back(struct s_lpipes **alpipes, + struct s_lpipes *new); +void lpipes_clear(struct s_lpipes **lpipes); +struct s_lpipes *lpipes_new(const char pipedword[], t_msh *msh); +struct s_lpipes *split_pipes(const char word[], t_lcom *lcom, t_msh *msh); + +#endif diff --git a/src/s_lvars.c b/src/s_lvars.c new file mode 100644 index 0000000..f2a1151 --- /dev/null +++ b/src/s_lvars.c @@ -0,0 +1,126 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lvars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "s_struct.h" + +void + lvars_rebind(t_lvars **lvars, + const char name[], + const char newval[]) +{ + t_lvars *tmp; + + tmp = *lvars; + while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) + { + tmp = tmp->next; + } + if (tmp == NULL) + { + return ; + } + ft_memdel((void*)&tmp->val); + if (!(tmp->val = ft_strdup(newval))) + { + ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); + exit(FT_RET_ALLOC); + } +} + +void + lvars_delone(t_lvars **lvars, + const char name[]) +{ + t_lvars *tmp; + t_lvars *prev; + + tmp = *lvars; + if (tmp != NULL && !ft_strncmp(tmp->name, name, ft_strlen(name))) + { + *lvars = tmp->next; + ft_memdel((void*)&tmp->name); + ft_memdel((void*)&tmp->val); + ft_memdel((void*)&tmp); + return ; + } + while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) + { + prev = tmp; + tmp = tmp->next; + } + if (tmp == NULL) + return ; + prev->next = tmp->next; + ft_memdel((void*)&tmp->name); + ft_memdel((void*)&tmp->val); + ft_memdel((void*)&tmp); +} + +void + lvars_add_front(t_lvars **alvars, + t_lvars *new) +{ + if (!alvars || !new) + { + return ; + } + new->next = *alvars; + *alvars = new; +} + +void + lvars_clear(t_lvars **lvars) +{ + t_lvars *tmp; + t_lvars *renext; + + if (!lvars) + return ; + tmp = *lvars; + while (tmp) + { + renext = tmp->next; + ft_memdel((void*)&tmp->name); + ft_memdel((void*)&tmp->val); + ft_memdel((void*)&tmp); + tmp = renext; + } + *lvars = NULL; +} + +t_lvars + *lvars_new(const char name[], + const char val[]) +{ + t_lvars *link; + + if (!(link = (t_lvars*)malloc(sizeof(t_lvars)))) + { + return (NULL); + } + if (!(link->name = ft_strdup(name))) + { + return (NULL); + } + if (!(link->val = ft_strdup(val))) + { + return (NULL); + } + link->next = NULL; + return (link); +} diff --git a/src/s_lvars.h b/src/s_lvars.h new file mode 100644 index 0000000..6bc0a99 --- /dev/null +++ b/src/s_lvars.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_lvars.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef S_LVARS_H +#define S_LVARS_H + +#include "s_struct.h" + +void lvars_rebind(t_lvars **lvars, + const char name[], + const char newval[]); +void lvars_delone(t_lvars **lvars, + const char name[]); +void lvars_add_front(t_lvars **alvars, + t_lvars *new); +void lvars_clear(t_lvars **lvars); +t_lvars *lvars_new(const char name[], + const char val[]); + +#endif diff --git a/src/s_struct.h b/src/s_struct.h new file mode 100644 index 0000000..8c72012 --- /dev/null +++ b/src/s_struct.h @@ -0,0 +1,68 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_struct.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef S_STRUCT_H +#define S_STRUCT_H + +#include + +#include "d_define.h" + +/* +** redir(int8_t) index +** -------------------- +** -1: < +** 1: > +** 2: >> +** 0: means no redirection +*/ + +typedef struct s_lpipes *t_lpipes; + +typedef struct s_lvars +{ + char *name; + char *val; + struct s_lvars *next; +} t_lvars; + +typedef struct s_lcom +{ + char *com; + char **argv; + int8_t redir; + int32_t rdrfd; + char *rdrpath; + struct s_lpipes *pipes; + struct s_lcom *next; +} t_lcom; + +struct s_lpipes +{ + struct s_lcom *one; + struct s_lpipes *next; +}; + +typedef struct s_msh +{ + char **envp; + char *ps_one; + char *cwd; + uint8_t ret; + char *shname; + char **bu_ref; + uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*); + struct s_lcom *curr; + struct s_lvars *vars; +} t_msh; + +#endif diff --git a/src/u_utils.c b/src/u_utils.c new file mode 100644 index 0000000..c95cbb0 --- /dev/null +++ b/src/u_utils.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +uint64_t + get_argc(const char *args[]) +{ + uint64_t argc; + + argc = 0; + while (args[argc]) + { + argc++; + } + return (argc); +} diff --git a/src/u_utils.h b/src/u_utils.h new file mode 100644 index 0000000..a02e7e6 --- /dev/null +++ b/src/u_utils.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U_UTILS_H +#define U_UTILS_H + +#include + +uint64_t get_argc(const char *args[]); + +#endif diff --git a/src/u_vars.c b/src/u_vars.c new file mode 100644 index 0000000..3f3a1a5 --- /dev/null +++ b/src/u_vars.c @@ -0,0 +1,163 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_vars.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +#include "d_define.h" +#include "f_fail.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" + + +static char + *set_rva(const char varname[], + t_msh *msh) +{ + char *rvarname; + + if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char)))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + fail_alloc(msh); + } + ft_memcpy((char*)rvarname, (const char*)varname + 1, + ft_strlen(varname + 1)); + *(rvarname + ft_strlen(varname + 1)) = '='; + *(rvarname + ft_strlen(varname + 1) + 1) = '\0'; + return (rvarname); +} + +static char + *dup_env(char *p_env, + char *rvarname, + t_msh *msh) +{ + char *varval; + + if (!(varval = ft_strdup(p_env))) + { + ft_memdel((void*)&rvarname); + lcom_clear(&msh->curr); + s_destroy(msh); + fail_alloc(msh); + } + ft_memdel((void*)&rvarname); + return (varval); +} + +static char + *get_frm_env(char rvarname[], + t_msh *msh) +{ + char **p_env; + char *pp_env; + char *varval; + + p_env = msh->envp; + while (*p_env) + { + if (!ft_strncmp(rvarname, *p_env, ft_strlen(rvarname))) + { + pp_env = *p_env; + while (*pp_env != '\0' && *pp_env != '=') + pp_env += 1; + if (*pp_env == '=') + pp_env += 1; + varval = dup_env(pp_env, rvarname, msh); + return (varval); + } + p_env += 1; + } + return (NULL); +} + +static char + *get_special_var(const char varname[], + t_msh *msh) +{ + char *varval; + + if (!ft_strncmp(varname, FT_RET_VAR, 3)) + { + if (!(varval = ft_uitoa(msh->ret))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + fail_alloc(msh); + } + return (varval); + } + else if (!ft_strncmp(varname, FT_ZER_VAR, 3)) + { + if (!(varval = ft_strdup(msh->shname))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + fail_alloc(msh); + } + return (varval); + } + return (NULL); +} + +static char + *get_cstm_vr(const char varname[], + t_msh *msh) +{ + (void)varname; + (void)msh; + return (NULL); +} + +/* +** char * +** subst_var_value(const char varname[], const t_msh *msh); +** +** DESCRIPTION +** The subst_var_value() function returns +** a heap-allocated, null-terminated string +** that may later be free'd containing the +** value of the variable varname[] including +** the '$' prefix. NULL is returned if varname[] +** wasn't found. +*/ + +char + *subst_var_value(const char varname[], + t_msh *msh) +{ + /* TODO: check behaviour on empty vars -> "QWE=" */ + /* TODO: add support for global variables -> "$hey $nigga..." */ + char *varval; + char *rvarname; + + varval = NULL; + if ((varval = get_special_var(varname, msh)) != NULL) + { + return (varval); + } + else if ((varval = get_cstm_vr(varname, msh)) != NULL) + { + return (varval); + } + else + { + rvarname = set_rva(varname, msh); + varval = get_frm_env(rvarname, msh); + return (varval); + } + return (NULL); +} diff --git a/src/u_vars.h b/src/u_vars.h new file mode 100644 index 0000000..686a9b5 --- /dev/null +++ b/src/u_vars.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_vars.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U_VARS_H +#define U_VARS_H + +#include "s_struct.h" + +char *subst_var_value(const char varname[], + t_msh *msh); + +#endif -- cgit v1.2.3 From 004eef9a3fd3403d6634ca658336d9a1e9766685 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 27 Jul 2020 21:19:29 +0200 Subject: In progress --- src/b_cd.c | 4 ++-- src/p_lcom_next.c | 2 +- src/u_vars.c | 10 +++++----- src/u_vars.h | 2 +- src/u_vars_next.c | 30 ++++++++++++++++++++++++++++++ src/u_vars_next.h | 21 +++++++++++++++++++++ 6 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/u_vars_next.c create mode 100644 src/u_vars_next.h (limited to 'src') diff --git a/src/b_cd.c b/src/b_cd.c index bcc8475..c325201 100644 --- a/src/b_cd.c +++ b/src/b_cd.c @@ -33,7 +33,7 @@ static void if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) { if (!(*path = ft_strsubst(*path, - "~", subst_var_value("$HOME", msh)))) + "~", get_var_value("$HOME", msh)))) { s_destroy(msh); fail_alloc(msh); @@ -55,7 +55,7 @@ uint8_t } else if (argc == 0) { - if (!(path = subst_var_value("$HOME", msh))) + if (!(path = get_var_value("$HOME", msh))) return (1); } else diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 94ac9bf..a4fbb61 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -34,7 +34,7 @@ static int8_t varlen += 1; if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) return (-1); - varval = subst_var_value(s_varname, msh); + varval = get_var_value(s_varname, msh); *p_words = ft_strsubst(*p_words, s_varname, varval); ft_memdel((void*)&s_varname); ft_memdel((void*)&varval); diff --git a/src/u_vars.c b/src/u_vars.c index 3f3a1a5..2b7fc4a 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -123,11 +123,11 @@ static char } /* -** char * -** subst_var_value(const char varname[], const t_msh *msh); +** char* +** get_var_value(const char varname[], t_msh *msh); ** ** DESCRIPTION -** The subst_var_value() function returns +** The get_var_value() function returns ** a heap-allocated, null-terminated string ** that may later be free'd containing the ** value of the variable varname[] including @@ -136,8 +136,8 @@ static char */ char - *subst_var_value(const char varname[], - t_msh *msh) + *get_var_value(const char varname[], + t_msh *msh) { /* TODO: check behaviour on empty vars -> "QWE=" */ /* TODO: add support for global variables -> "$hey $nigga..." */ diff --git a/src/u_vars.h b/src/u_vars.h index 686a9b5..cad825c 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,7 +15,7 @@ #include "s_struct.h" -char *subst_var_value(const char varname[], +char *get_var_value(const char varname[], t_msh *msh); #endif diff --git a/src/u_vars_next.c b/src/u_vars_next.c new file mode 100644 index 0000000..5626376 --- /dev/null +++ b/src/u_vars_next.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_vars_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "s_struct.h" +/* +** void +** subst_var_value(const char varname[], t_msh *msh); +** +** DESCRIPTION +** The subst_var_value() changes the msh->envp value +** of the variable varname[]. If varname[] wasn't found +** in msh->envp, varname[] is searched in msh->vars. +*/ + +void + subst_var_value(const char varname[], + t_msh *msh) +{ + (void)varname; + (void)msh; +} diff --git a/src/u_vars_next.h b/src/u_vars_next.h new file mode 100644 index 0000000..1360190 --- /dev/null +++ b/src/u_vars_next.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_vars_next.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef U_VARS_NEXT_H +#define U_VARS_NEXT_H + +#include "s_struct.h" + +void subst_var_value(const char varname[], + t_msh *msh) + +#endif -- cgit v1.2.3 From 280ecd341f1ec88abe325fa69b197bca3e43d2f1 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 27 Jul 2020 22:38:54 +0200 Subject: Now can subst env vars --- src/b_cd.c | 4 ++-- src/m_minishell.c | 2 ++ src/p_lcom_next.c | 2 +- src/u_vars.c | 6 +++--- src/u_vars.h | 2 +- src/u_vars_next.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ src/u_vars_next.h | 5 +++-- 7 files changed, 61 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/b_cd.c b/src/b_cd.c index c325201..c892f7e 100644 --- a/src/b_cd.c +++ b/src/b_cd.c @@ -33,7 +33,7 @@ static void if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) { if (!(*path = ft_strsubst(*path, - "~", get_var_value("$HOME", msh)))) + "~", u_get_var_value("$HOME", msh)))) { s_destroy(msh); fail_alloc(msh); @@ -55,7 +55,7 @@ uint8_t } else if (argc == 0) { - if (!(path = get_var_value("$HOME", msh))) + if (!(path = u_get_var_value("$HOME", msh))) return (1); } else diff --git a/src/m_minishell.c b/src/m_minishell.c index 1e065b4..66426b7 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -21,6 +21,7 @@ #include "s_struct.h" #include "s_init.h" #include "s_destroy.h" +#include "u_vars_next.h" int main(int argc, @@ -39,6 +40,7 @@ int ft_dprintf(2, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } + /* TODO: DELET THIS */ ret = m_argv(argc, argv, msh); s_destroy(msh); return (ret); diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index a4fbb61..a0ca8d1 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -34,7 +34,7 @@ static int8_t varlen += 1; if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) return (-1); - varval = get_var_value(s_varname, msh); + varval = u_get_var_value(s_varname, msh); *p_words = ft_strsubst(*p_words, s_varname, varval); ft_memdel((void*)&s_varname); ft_memdel((void*)&varval); diff --git a/src/u_vars.c b/src/u_vars.c index 2b7fc4a..cd9d3f2 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -124,10 +124,10 @@ static char /* ** char* -** get_var_value(const char varname[], t_msh *msh); +** u_get_var_value(const char varname[], t_msh *msh); ** ** DESCRIPTION -** The get_var_value() function returns +** The u_get_var_value() function returns ** a heap-allocated, null-terminated string ** that may later be free'd containing the ** value of the variable varname[] including @@ -136,7 +136,7 @@ static char */ char - *get_var_value(const char varname[], + *u_get_var_value(const char varname[], t_msh *msh) { /* TODO: check behaviour on empty vars -> "QWE=" */ diff --git a/src/u_vars.h b/src/u_vars.h index cad825c..fb8b924 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,7 +15,7 @@ #include "s_struct.h" -char *get_var_value(const char varname[], +char *u_get_var_value(const char varname[], t_msh *msh); #endif diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 5626376..8134de3 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -10,21 +10,64 @@ /* */ /* ************************************************************************** */ +#include +#include +#include + #include "s_struct.h" + +static int64_t + u_get_var_line(const char varname[], + t_msh *msh) +{ + char **env_ptr; + int64_t i; + + env_ptr = msh->envp; + i = 0; + while (*env_ptr && + ft_strncmp(*env_ptr, varname, ft_strlen(varname)) != 0) + { + env_ptr++; + i++; + } + if (*env_ptr) + return (i); + else + return (-1); +} + /* ** void -** subst_var_value(const char varname[], t_msh *msh); +** u_subst_var_value(const char varname[], const char newval[], t_msh *msh); ** ** DESCRIPTION -** The subst_var_value() changes the msh->envp value -** of the variable varname[]. If varname[] wasn't found +** The subst_var_value() changes the value of msh->envp +** variable varname[] with newval[]. If varname[] wasn't found ** in msh->envp, varname[] is searched in msh->vars. */ void - subst_var_value(const char varname[], + u_subst_var_value(const char varname[], + const char newval[], t_msh *msh) { - (void)varname; - (void)msh; + char new_line_fmt[2048]; + int64_t env_line; + + if ((env_line = u_get_var_line(varname + 1, msh)) > -1) + { + ft_memdel((void*)&msh->envp[env_line]); + ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); + if (!(msh->envp[env_line] = + (char*)malloc((ft_strlen(new_line_fmt) + 1) * sizeof(char)))) + { + /* TODO: Handle this fail */ + } + (void)ft_strlcpy(msh->envp[env_line], new_line_fmt, ft_strlen(new_line_fmt) + 1); + } + else + { + /* TODO: Search in custom vars */ + } } diff --git a/src/u_vars_next.h b/src/u_vars_next.h index 1360190..118451f 100644 --- a/src/u_vars_next.h +++ b/src/u_vars_next.h @@ -15,7 +15,8 @@ #include "s_struct.h" -void subst_var_value(const char varname[], - t_msh *msh) +void u_subst_var_value(const char varname[], + const char newval[], + t_msh *msh); #endif -- cgit v1.2.3 From 8c912b19fb1fb9a31956a8a9468277f2ee7bed7e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 27 Jul 2020 22:44:47 +0200 Subject: env vars setting is ok --- src/u_vars.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/u_vars.c b/src/u_vars.c index cd9d3f2..fafc2fa 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -115,7 +115,7 @@ static char static char *get_cstm_vr(const char varname[], - t_msh *msh) + t_msh *msh) { (void)varname; (void)msh; -- cgit v1.2.3 From 6dc6bf6f46b736299826bfdd63192bdee16ba875 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 18:08:50 +0200 Subject: Less retarded stuff --- src/b_exit.c | 2 + src/d_define.h | 1 - src/d_enum.h | 13 ++++++ src/e_builtins.c | 117 +++++++++++++++++++++++++++++------------------------- src/m_minishell.c | 1 - 5 files changed, 77 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/b_exit.c b/src/b_exit.c index 75f16bf..777bbb9 100644 --- a/src/b_exit.c +++ b/src/b_exit.c @@ -28,6 +28,7 @@ uint8_t uint8_t ret; const uint64_t argc = get_argc((const char**)args); + ret = 0; if (argc > 1) { fail_too_many_args("exit", msh); @@ -39,5 +40,6 @@ uint8_t /* TODO: non numeric args[0] */ } ft_dprintf(STDERR_FILENO, "exit\n"); + exit(ret); return (0); } diff --git a/src/d_define.h b/src/d_define.h index 175e256..6f93751 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -21,7 +21,6 @@ #define FT_PS_ONE "minishell ~> " #define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type" -#define FT_BUILTINS_COUNT 8 #define FT_HISTFILE "minishell_history" /* diff --git a/src/d_enum.h b/src/d_enum.h index 427e0a0..ec35ca3 100644 --- a/src/d_enum.h +++ b/src/d_enum.h @@ -38,4 +38,17 @@ enum FT_READ_END }; +enum +{ + FT_ID_ECHO, + FT_ID_CD, + FT_ID_PWD, + FT_ID_EXPORT, + FT_ID_UNSET, + FT_ID_ENV, + FT_ID_EXIT, + FT_ID_TYPE, + FT_BUILTINS_COUNT +}; + #endif diff --git a/src/e_builtins.c b/src/e_builtins.c index e35416a..ca512a3 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -21,69 +21,76 @@ #include "s_lcom.h" #include "s_struct.h" -static void - e_builtin_child(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh) -{ - int32_t ret; +/* static void */ +/* e_builtin_child(const t_lcom *ptr, */ +/* uint8_t bu_id, */ +/* t_msh *msh) */ +/* { */ +/* int32_t ret; */ - dup_redirs(ptr, msh); - ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); - lcom_clear(&msh->curr); - s_destroy(msh); - exit(ret); -} +/* dup_redirs(ptr, msh); */ +/* ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); */ +/* lcom_clear(&msh->curr); */ +/* s_destroy(msh); */ +/* exit(ret); */ +/* } */ -static void - e_builtin_parent(pid_t pid, - const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh) -{ - int32_t status; - int32_t ret; +/* static void */ +/* e_builtin_parent(pid_t pid, */ +/* const t_lcom *ptr, */ +/* uint8_t bu_id, */ +/* t_msh *msh) */ +/* { */ +/* int32_t status; */ +/* int32_t ret; */ - while (wait(&status) != pid) - ; - ret = WEXITSTATUS(status); - if (bu_id != 6) - msh->ret = ret; - if (bu_id == 1 && msh->ret == 0) - { - msh->bu_ptr[bu_id](ptr->argv + 1, msh); - /* TODO: export $PWD */ - } - else if (bu_id == 6 && ret == 0) - { - if (ptr->argv[1]) - ret = ft_atoi(ptr->argv[1]); - else - ret = msh->ret; - lcom_clear(&msh->curr); - s_destroy(msh); - exit(ret); - } -} +/* while (wait(&status) != pid) */ +/* ; */ +/* ret = WEXITSTATUS(status); */ +/* if (bu_id != FT_ID_EXIT) */ +/* msh->ret = ret; */ +/* if (bu_id == FT_ID_CD && msh->ret == 0) */ +/* { */ +/* msh->bu_ptr[bu_id](ptr->argv + 1, msh); */ +/* /\* TODO: export $PWD *\/ */ +/* } */ +/* else if (bu_id == FT_ID_EXIT && ret == 0) */ +/* { */ +/* if (ptr->argv[1]) */ +/* ret = ft_atoi(ptr->argv[1]); */ +/* else */ +/* ret = msh->ret; */ +/* lcom_clear(&msh->curr); */ +/* s_destroy(msh); */ +/* exit(ret); */ +/* } */ +/* } */ void e_builtin(const t_lcom *ptr, uint8_t bu_id, t_msh *msh) { - pid_t pid; + int32_t ret; + + dup_redirs(ptr, msh); + ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); + lcom_clear(&msh->curr); + msh->ret = ret; + /* pid_t pid; */ - /* TODO: find a way to handle exit | bu_id = 6 */ - if ((pid = fork()) == 0) - { - e_builtin_child(ptr, bu_id, msh); - } - else if (pid < 0) - { - /* TODO: handle fork failed */ - } - else - { - e_builtin_parent(pid, ptr, bu_id, msh); - } + /* /\* TODO: find a way to handle exit | bu_id = 6 *\/ */ + /* e_builtin_child(ptr, bu_id, msh); */ + /* if ((pid = fork()) == 0) */ + /* { */ + /* e_builtin_child(ptr, bu_id, msh); */ + /* } */ + /* else if (pid < 0) */ + /* { */ + /* /\* TODO: handle fork failed *\/ */ + /* } */ + /* else */ + /* { */ + /* e_builtin_parent(pid, ptr, bu_id, msh); */ + /* } */ } diff --git a/src/m_minishell.c b/src/m_minishell.c index 66426b7..78700a1 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -40,7 +40,6 @@ int ft_dprintf(2, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } - /* TODO: DELET THIS */ ret = m_argv(argc, argv, msh); s_destroy(msh); return (ret); -- cgit v1.2.3 From f441e00ea60a5e485d853d5dd3ac99a8535a40cc Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 18:22:32 +0200 Subject: In progress --- src/b_export.c | 10 ++++++++++ src/b_export_next.c | 12 ++++++++++++ src/b_export_next.h | 18 ++++++++++++++++++ src/u_vars_next.c | 3 ++- 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/b_export_next.c create mode 100644 src/b_export_next.h (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 5e7ee14..95ccf40 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -64,6 +64,7 @@ uint8_t r = 0; while (*ptr) { + ft_printf("export: [%s]\n", *ptr); next = 0; if (!check_valid_identifier(*ptr)) { @@ -72,7 +73,16 @@ uint8_t r = 1; } if (next == 0 && !check_equals(*ptr)) + { next = 1; + /* TODO: export already set variable */ + /* examples: */ + /* ~> QWE=qwe */ + /* ~> export QWE */ + /* -------------- */ + /* # non-existant variable QWE */ + /* ~> export QWE */ + } ptr++; } /* TODO: finish export */ diff --git a/src/b_export_next.c b/src/b_export_next.c new file mode 100644 index 0000000..44d2b9b --- /dev/null +++ b/src/b_export_next.c @@ -0,0 +1,12 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_export_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + diff --git a/src/b_export_next.h b/src/b_export_next.h new file mode 100644 index 0000000..4a8781b --- /dev/null +++ b/src/b_export_next.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_export_next.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef B_EXPORT_NEXT_H +#define B_EXPORT_NEXT_H + +#include "s_struct.h" + +#endif diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 8134de3..8e02c26 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -64,7 +64,8 @@ void { /* TODO: Handle this fail */ } - (void)ft_strlcpy(msh->envp[env_line], new_line_fmt, ft_strlen(new_line_fmt) + 1); + (void)ft_strlcpy(msh->envp[env_line], + new_line_fmt, ft_strlen(new_line_fmt) + 1); } else { -- cgit v1.2.3 From 4fe88a37306072c5eacf24ab4dd1913cc9c30d3c Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 18:30:35 +0200 Subject: SHLVL boyy --- src/m_minishell.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/m_minishell.c b/src/m_minishell.c index 78700a1..c5901db 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -21,6 +21,7 @@ #include "s_struct.h" #include "s_init.h" #include "s_destroy.h" +#include "u_vars.h" #include "u_vars_next.h" int @@ -28,10 +29,10 @@ int char *const argv[], char *const envp[]) { - t_msh *msh; int32_t ret; + char *str; + t_msh *msh; - /* TODO: increment $SHLVL */ /* TODO: also set $SHELL | maybe not bash does not set $SHELL */ /* only zsh does */ /* TODO: handle general variables | $var */ @@ -40,6 +41,8 @@ int ft_dprintf(2, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } + u_subst_var_value("$SHLVL", str = ft_itoa(ft_atoi(u_get_var_value("$SHLVL", msh)) + 1), msh); + ft_memdel((void*)&str); ret = m_argv(argc, argv, msh); s_destroy(msh); return (ret); -- cgit v1.2.3 From 40b5a588c89890ea83d0e4fcaa9f3fdf539cb064 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 18:36:31 +0200 Subject: Norm update --- src/m_minishell.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/m_minishell.c b/src/m_minishell.c index c5901db..8a76598 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -41,7 +41,8 @@ int ft_dprintf(2, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } - u_subst_var_value("$SHLVL", str = ft_itoa(ft_atoi(u_get_var_value("$SHLVL", msh)) + 1), msh); + u_subst_var_value("$SHLVL", + str = ft_itoa(ft_atoi(u_get_var_value("$SHLVL", msh)) + 1), msh); ft_memdel((void*)&str); ret = m_argv(argc, argv, msh); s_destroy(msh); -- cgit v1.2.3 From 8797f947d58371ccbef5c34f4ebf64c103a38942 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 18:54:50 +0200 Subject: Removed todo --- src/m_minishell.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/m_minishell.c b/src/m_minishell.c index 8a76598..6e656a6 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -33,8 +33,6 @@ int char *str; t_msh *msh; - /* TODO: also set $SHELL | maybe not bash does not set $SHELL */ - /* only zsh does */ /* TODO: handle general variables | $var */ if (!(msh = init_msh(argv, envp))) { -- cgit v1.2.3 From ced3be528b52b59ad613f282e37d653c16ef85d9 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 19:17:05 +0200 Subject: I'm retarded --- src/m_minishell.c | 2 ++ src/u_vars.c | 22 ++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/m_minishell.c b/src/m_minishell.c index 6e656a6..e14dbdc 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -23,6 +23,7 @@ #include "s_destroy.h" #include "u_vars.h" #include "u_vars_next.h" +#include "s_lvars.h" int main(int argc, @@ -42,6 +43,7 @@ int u_subst_var_value("$SHLVL", str = ft_itoa(ft_atoi(u_get_var_value("$SHLVL", msh)) + 1), msh); ft_memdel((void*)&str); + lvars_add_front(&msh->vars, lvars_new("var", "value")); ret = m_argv(argc, argv, msh); s_destroy(msh); return (ret); diff --git a/src/u_vars.c b/src/u_vars.c index fafc2fa..9978df5 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -20,7 +20,6 @@ #include "s_lcom.h" #include "s_struct.h" - static char *set_rva(const char varname[], t_msh *msh) @@ -117,9 +116,24 @@ static char *get_cstm_vr(const char varname[], t_msh *msh) { - (void)varname; - (void)msh; - return (NULL); + t_lvars *ptr; + char *varval; + + ptr = msh->vars; + while (ptr != NULL && + ft_strncmp(varname + 1, ptr->name, ft_strlen(varname + 1)) != 0) + { + ptr = ptr->next; + } + if (ptr != NULL) + { + varval = ft_strdup(ptr->val); + return (varval); + } + else + { + return (NULL); + } } /* -- cgit v1.2.3 From e18c67950ddd57b65933cb46cbd7d77e74f00faa Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 19:19:38 +0200 Subject: Secured strdup --- src/u_vars.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/u_vars.c b/src/u_vars.c index 9978df5..377da77 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -127,7 +127,12 @@ static char } if (ptr != NULL) { - varval = ft_strdup(ptr->val); + if (!(varval = ft_strdup(ptr->val))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + fail_alloc(msh); + } return (varval); } else -- cgit v1.2.3 From af663bf8cbc9e97d9818959de69a1d8a6c423cbe Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 19:45:42 +0200 Subject: Work in progress, freed leak --- src/m_minishell.c | 12 ++++++++---- src/p_lcom.c | 1 - src/u_vars_next.c | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/m_minishell.c b/src/m_minishell.c index e14dbdc..9306955 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -31,7 +31,8 @@ int char *const envp[]) { int32_t ret; - char *str; + char *str_one; + char *str_two; t_msh *msh; /* TODO: handle general variables | $var */ @@ -41,9 +42,12 @@ int return (FT_RET_ALLOC); } u_subst_var_value("$SHLVL", - str = ft_itoa(ft_atoi(u_get_var_value("$SHLVL", msh)) + 1), msh); - ft_memdel((void*)&str); - lvars_add_front(&msh->vars, lvars_new("var", "value")); + str_one =ft_itoa( + ft_atoi(str_two = u_get_var_value("$SHLVL", msh)) + 1), msh); + ft_memdel((void*)&str_one); + ft_memdel((void*)&str_two); + /* TODO: delet this */ + u_subst_var_value("$var", "value", msh); ret = m_argv(argc, argv, msh); s_destroy(msh); return (ret); diff --git a/src/p_lcom.c b/src/p_lcom.c index f47e2c1..2dbd505 100644 --- a/src/p_lcom.c +++ b/src/p_lcom.c @@ -139,7 +139,6 @@ int8_t while (i <= count && words[i]) { next = FALSE; - /* TODO: split pipes here */ if (ft_strchr(words[i], '|')) { if (!(link = lcom_new(NULL, msh))) diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 8e02c26..5660449 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -14,10 +14,12 @@ #include #include +#include "d_enum.h" +#include "s_lvars.h" #include "s_struct.h" static int64_t - u_get_var_line(const char varname[], + u_get_env_var_line(const char varname[], t_msh *msh) { char **env_ptr; @@ -37,6 +39,28 @@ static int64_t return (-1); } +static t_bool + u_get_custom_var_existance(const char varname[], + t_msh *msh) +{ + t_lvars *ptr; + + ptr = msh->vars; + while (ptr && + ft_strncmp(varname, ptr->name, ft_strlen(varname)) != 0) + { + ptr = ptr->next; + } + if (ptr != NULL) + { + return (TRUE); + } + else + { + return (FALSE); + } +} + /* ** void ** u_subst_var_value(const char varname[], const char newval[], t_msh *msh); @@ -55,7 +79,7 @@ void char new_line_fmt[2048]; int64_t env_line; - if ((env_line = u_get_var_line(varname + 1, msh)) > -1) + if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { ft_memdel((void*)&msh->envp[env_line]); ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); @@ -67,8 +91,12 @@ void (void)ft_strlcpy(msh->envp[env_line], new_line_fmt, ft_strlen(new_line_fmt) + 1); } + else if ((env_line = u_get_custom_var_existance(varname + 1, msh)) == TRUE) + { + lvars_rebind(&msh->vars, varname + 1, newval); + } else { - /* TODO: Search in custom vars */ + lvars_add_front(&msh->vars, lvars_new(varname + 1, newval)); } } -- cgit v1.2.3 From c19bd35afdb45e49cebdfd96e7adb1e6fe477f0c Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 20:30:58 +0200 Subject: Double exit no more --- src/b_cd.c | 8 +++---- src/b_env.c | 2 +- src/b_exit.c | 7 ++++-- src/b_export.c | 2 +- src/b_type.c | 2 +- src/d_define.h | 5 +++-- src/e_builtins.c | 61 --------------------------------------------------- src/e_externs.c | 14 +++++++++--- src/e_externs_next.c | 4 ++-- src/e_externs_pipes.c | 4 ++-- src/f_chdir.c | 2 +- src/f_chdir.h | 2 +- src/f_com.c | 23 +++++++++++++++++++ src/f_com.h | 20 +++++++++++++++++ src/f_fail.c | 8 +++---- src/f_fail.h | 9 ++++---- src/m_funptr.c | 2 +- src/m_loop.c | 1 - src/p_line.c | 2 +- src/u_vars.c | 10 ++++----- 20 files changed, 91 insertions(+), 97 deletions(-) create mode 100644 src/f_com.c create mode 100644 src/f_com.h (limited to 'src') diff --git a/src/b_cd.c b/src/b_cd.c index c892f7e..f30e61f 100644 --- a/src/b_cd.c +++ b/src/b_cd.c @@ -28,7 +28,7 @@ static void if (!(*path = ft_strdup(*args))) { s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } if (!ft_strncmp("~/", *path, 2) || !ft_strncmp("~", *path, 2)) { @@ -36,7 +36,7 @@ static void "~", u_get_var_value("$HOME", msh)))) { s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } } } @@ -50,7 +50,7 @@ uint8_t if (argc >= 2) { - fail_too_many_args("cd", msh); + f_fail_too_many_args("cd", msh); return (1); } else if (argc == 0) @@ -62,7 +62,7 @@ uint8_t set_path(&path, args, msh); if (chdir(path) != 0) { - fail_chd("cd", path, msh); + f_fail_chd("cd", path, msh); ft_memdel((void*)&path); return (1); } diff --git a/src/b_env.c b/src/b_env.c index 08adaf5..8f0a974 100644 --- a/src/b_env.c +++ b/src/b_env.c @@ -24,7 +24,7 @@ uint8_t if (args && args[0]) { - fail_no_options("env", msh); + f_fail_no_options("env", msh); return (127); } ptr = msh->envp; diff --git a/src/b_exit.c b/src/b_exit.c index 777bbb9..5f1c446 100644 --- a/src/b_exit.c +++ b/src/b_exit.c @@ -28,10 +28,9 @@ uint8_t uint8_t ret; const uint64_t argc = get_argc((const char**)args); - ret = 0; if (argc > 1) { - fail_too_many_args("exit", msh); + f_fail_too_many_args("exit", msh); return (1); } if (argc == 1) @@ -39,7 +38,11 @@ uint8_t ret = ft_atoi(args[0]); /* TODO: non numeric args[0] */ } + else + ret = msh->ret; ft_dprintf(STDERR_FILENO, "exit\n"); + lcom_clear(&msh->curr); + s_destroy(msh); exit(ret); return (0); } diff --git a/src/b_export.c b/src/b_export.c index 95ccf40..9cb1c14 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -68,7 +68,7 @@ uint8_t next = 0; if (!check_valid_identifier(*ptr)) { - fail_identifier("export", *ptr, msh); + f_fail_identifier("export", *ptr, msh); next = 1; r = 1; } diff --git a/src/b_type.c b/src/b_type.c index ccaca43..00bad1a 100644 --- a/src/b_type.c +++ b/src/b_type.c @@ -57,7 +57,7 @@ static char if (!(fullpath = ft_strdup(com))) { lcom_clear(&msh->curr); - fail_alloc(msh); + f_fail_alloc(msh); } return (fullpath); } diff --git a/src/d_define.h b/src/d_define.h index 6f93751..b2f2f08 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -42,7 +42,8 @@ ** ====== FAIL MSG ====== */ -#define FT_FAIL_NO_OPTIONS "no options required" -#define FT_FAIL_TOO_MANY_ARGS "too many arguments" +#define FT_FAIL_COMMAND_NOT_FOUND "command not found" +#define FT_FAIL_NO_OPTIONS "no options required" +#define FT_FAIL_TOO_MANY_ARGS "too many arguments" #endif diff --git a/src/e_builtins.c b/src/e_builtins.c index ca512a3..fe4f7e1 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -21,51 +21,6 @@ #include "s_lcom.h" #include "s_struct.h" -/* static void */ -/* e_builtin_child(const t_lcom *ptr, */ -/* uint8_t bu_id, */ -/* t_msh *msh) */ -/* { */ -/* int32_t ret; */ - -/* dup_redirs(ptr, msh); */ -/* ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); */ -/* lcom_clear(&msh->curr); */ -/* s_destroy(msh); */ -/* exit(ret); */ -/* } */ - -/* static void */ -/* e_builtin_parent(pid_t pid, */ -/* const t_lcom *ptr, */ -/* uint8_t bu_id, */ -/* t_msh *msh) */ -/* { */ -/* int32_t status; */ -/* int32_t ret; */ - -/* while (wait(&status) != pid) */ -/* ; */ -/* ret = WEXITSTATUS(status); */ -/* if (bu_id != FT_ID_EXIT) */ -/* msh->ret = ret; */ -/* if (bu_id == FT_ID_CD && msh->ret == 0) */ -/* { */ -/* msh->bu_ptr[bu_id](ptr->argv + 1, msh); */ -/* /\* TODO: export $PWD *\/ */ -/* } */ -/* else if (bu_id == FT_ID_EXIT && ret == 0) */ -/* { */ -/* if (ptr->argv[1]) */ -/* ret = ft_atoi(ptr->argv[1]); */ -/* else */ -/* ret = msh->ret; */ -/* lcom_clear(&msh->curr); */ -/* s_destroy(msh); */ -/* exit(ret); */ -/* } */ -/* } */ - void e_builtin(const t_lcom *ptr, uint8_t bu_id, @@ -77,20 +32,4 @@ void ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); lcom_clear(&msh->curr); msh->ret = ret; - /* pid_t pid; */ - - /* /\* TODO: find a way to handle exit | bu_id = 6 *\/ */ - /* e_builtin_child(ptr, bu_id, msh); */ - /* if ((pid = fork()) == 0) */ - /* { */ - /* e_builtin_child(ptr, bu_id, msh); */ - /* } */ - /* else if (pid < 0) */ - /* { */ - /* /\* TODO: handle fork failed *\/ */ - /* } */ - /* else */ - /* { */ - /* e_builtin_parent(pid, ptr, bu_id, msh); */ - /* } */ } diff --git a/src/e_externs.c b/src/e_externs.c index d7c4854..3d55fa9 100644 --- a/src/e_externs.c +++ b/src/e_externs.c @@ -17,6 +17,7 @@ #include #include "e_externs_next.h" +#include "f_fail.h" #include "m_redirs.h" #include "s_destroy.h" #include "s_lcom.h" @@ -72,8 +73,15 @@ void { fullpath = search_in_path(ptr->com, envpath, msh); ft_delwords(envpath); + if (fullpath == NULL) + { + f_fail_command_not_found(ptr->com); + /* TODO: deal if not found etc */ + } + else + { + exec_path(fullpath, ptr, msh); + ft_memdel((void*)&fullpath); + } } - /* TODO: deal if not found etc */ - exec_path(fullpath, ptr, msh); - ft_memdel((void*)&fullpath); } diff --git a/src/e_externs_next.c b/src/e_externs_next.c index ba3c2d5..e33405c 100644 --- a/src/e_externs_next.c +++ b/src/e_externs_next.c @@ -33,7 +33,7 @@ static char { lcom_clear(&msh->curr); s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } (void)ft_memcpy(fullpath, p_path, path_len); *(fullpath + (path_len)) = '/'; @@ -97,7 +97,7 @@ char { lcom_clear(&msh->curr); s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } return (envpath); } diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index c8f8404..a5ee696 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -122,7 +122,7 @@ void rptr = ptr; pipes = e_get_pipes_count(head); if (!(fullpath = (char **)malloc((pipes + 2) * sizeof(char *)))) - fail_alloc(msh); + f_fail_alloc(msh); fullpath[pipes + 1] = NULL; i = 0; while (rptr != NULL) @@ -130,7 +130,7 @@ void if (ft_ischarset("/.", rptr->one->com[0])) { if (!(fullpath[i] = ft_strdup(rptr->one->com))) - fail_alloc(msh); + f_fail_alloc(msh); } else if ((envpath = get_env_path(msh)) != NULL) { diff --git a/src/f_chdir.c b/src/f_chdir.c index 6bb0497..4623c82 100644 --- a/src/f_chdir.c +++ b/src/f_chdir.c @@ -18,7 +18,7 @@ #include "s_struct.h" void - fail_chd(const char concern[], + f_fail_chd(const char concern[], const char path[], t_msh *msh) { diff --git a/src/f_chdir.h b/src/f_chdir.h index be9773b..76cb737 100644 --- a/src/f_chdir.h +++ b/src/f_chdir.h @@ -15,7 +15,7 @@ #include "s_struct.h" -void fail_chd(const char concern[], +void f_fail_chd(const char concern[], const char pathp[], t_msh *msh); diff --git a/src/f_com.c b/src/f_com.c new file mode 100644 index 0000000..9c4e02a --- /dev/null +++ b/src/f_com.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_com.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" +#include "s_struct.h" + +void + f_fail_command_not_found(const char command[]) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", "minishell", command, FT_FAIL_COMMAND_NOT_FOUND); +} diff --git a/src/f_com.h b/src/f_com.h new file mode 100644 index 0000000..12023f6 --- /dev/null +++ b/src/f_com.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_com.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef F_COM_H +#define F_COM_H + +#include "s_struct.h" + +void f_fail_command_not_found(const char command[]); + +#endif diff --git a/src/f_fail.c b/src/f_fail.c index aae00fb..a3a1df1 100644 --- a/src/f_fail.c +++ b/src/f_fail.c @@ -28,14 +28,14 @@ static void } void - fail_no_options(const char concern[], + f_fail_no_options(const char concern[], t_msh *msh) { write_fail(concern, FT_FAIL_NO_OPTIONS, msh); } void - fail_identifier(const char concern[], + f_fail_identifier(const char concern[], const char identifier[], t_msh *msh) { @@ -44,14 +44,14 @@ void } void - fail_too_many_args(const char concern[], + f_fail_too_many_args(const char concern[], t_msh *msh) { write_fail(concern, FT_FAIL_TOO_MANY_ARGS, msh); } void - fail_alloc(t_msh *msh) + f_fail_alloc(t_msh *msh) { ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno)); exit(FT_RET_ALLOC); diff --git a/src/f_fail.h b/src/f_fail.h index 9699eee..868fe75 100644 --- a/src/f_fail.h +++ b/src/f_fail.h @@ -13,18 +13,19 @@ #ifndef F_FAIL_H #define F_FAIL_H +#include "f_com.h" #include "f_chdir.h" #include "f_errno.h" #include "f_redir.h" #include "s_struct.h" -void fail_no_options(const char concern[], +void f_fail_no_options(const char concern[], t_msh *msh); -void fail_too_many_args(const char concern[], +void f_fail_too_many_args(const char concern[], t_msh *msh); -void fail_identifier(const char concern[], +void f_fail_identifier(const char concern[], const char identifier[], t_msh *msh); -void fail_alloc(t_msh *msh); +void f_fail_alloc(t_msh *msh); #endif diff --git a/src/m_funptr.c b/src/m_funptr.c index 5a02b2e..3bd0969 100644 --- a/src/m_funptr.c +++ b/src/m_funptr.c @@ -32,6 +32,6 @@ void msh->bu_ptr[7] = b_type; if (!(msh->bu_ref = ft_split(FT_BUILTINS, '|'))) { - fail_alloc(msh); + f_fail_alloc(msh); } } diff --git a/src/m_loop.c b/src/m_loop.c index fe3fbcd..143c25d 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -114,7 +114,6 @@ uint8_t /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ /* TODO: GNL 25 leak on "msh ~> exit" | gl hf */ /* TODO: "msh ~> some command \": re GNL into ft_nrealloc */ - /* TODO: the chad pipes | */ /* TODO: a histfile would be nice */ } else diff --git a/src/p_line.c b/src/p_line.c index 4a58537..aafc900 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -45,6 +45,6 @@ void if (p_lcom(line, count, msh) < 0) { s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } } diff --git a/src/u_vars.c b/src/u_vars.c index 377da77..a852d6f 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -30,7 +30,7 @@ static char { lcom_clear(&msh->curr); s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } ft_memcpy((char*)rvarname, (const char*)varname + 1, ft_strlen(varname + 1)); @@ -51,7 +51,7 @@ static char ft_memdel((void*)&rvarname); lcom_clear(&msh->curr); s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } ft_memdel((void*)&rvarname); return (varval); @@ -95,7 +95,7 @@ static char { lcom_clear(&msh->curr); s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } return (varval); } @@ -105,7 +105,7 @@ static char { lcom_clear(&msh->curr); s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } return (varval); } @@ -131,7 +131,7 @@ static char { lcom_clear(&msh->curr); s_destroy(msh); - fail_alloc(msh); + f_fail_alloc(msh); } return (varval); } -- cgit v1.2.3 From db381c5a5d8755937663a5ea61e655186f7606e7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 30 Jul 2020 22:14:06 +0200 Subject: Not too bad --- src/e_externs.c | 1 - src/p_lcom_next.c | 30 ++++++++++++++++++++++++++++++ src/p_lcom_next.h | 1 + src/s_lcom.c | 6 ++---- 4 files changed, 33 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/e_externs.c b/src/e_externs.c index 3d55fa9..93d989f 100644 --- a/src/e_externs.c +++ b/src/e_externs.c @@ -76,7 +76,6 @@ void if (fullpath == NULL) { f_fail_command_not_found(ptr->com); - /* TODO: deal if not found etc */ } else { diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index a0ca8d1..4d0ca72 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -14,6 +14,7 @@ #include #include +#include "d_enum.h" #include "s_struct.h" #include "u_vars.h" @@ -95,3 +96,32 @@ char ft_memdel((void*)&subst); return (words); } + +char + **p_check_first_arg_equals(char *words[]) +{ + t_bool equals; + char *ptr; + + equals = FALSE; + ptr = words[0]; + while (*ptr != '\0') + { + if (*ptr == '=') + { + equals = TRUE; + break ; + } + ptr++; + } + if (equals == TRUE && words[1] != NULL) + { + ft_memdel((void*)&words[0]); + words = words + 1; + return (words); + } + else + { + return (words); + } +} diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h index 3113b6b..ad5c5c5 100644 --- a/src/p_lcom_next.h +++ b/src/p_lcom_next.h @@ -21,5 +21,6 @@ char **subst_vars(char *words[], t_msh *msh); char **subst_args(const char word[], int8_t redir); +char **p_check_first_arg_equals(char *words[]); #endif diff --git a/src/s_lcom.c b/src/s_lcom.c index 31b14d6..773814f 100644 --- a/src/s_lcom.c +++ b/src/s_lcom.c @@ -49,11 +49,8 @@ static int8_t j = 0; while (i > 0 && j < i) { - if (!((*lcom)->argv[j] = - (char*)malloc((ft_strlen(words[j]) + 1) * sizeof(char)))) + if (!((*lcom)->argv[j] = ft_strdup(words[j]))) return (-1); - ft_strlcpy((*lcom)->argv[j], words[j], - ft_strlen(words[j]) + 1); j++; } (*lcom)->argv[j] = 0; @@ -134,6 +131,7 @@ t_lcom return (NULL); if (!(words = subst_vars(words, msh))) return (NULL); + words = p_check_first_arg_equals(words); if (fill_lcom(words, &link) < 0) { ft_delwords(words); -- cgit v1.2.3 From caa35c6770ea21112ccf38d8637d15f2bc717ea6 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 31 Jul 2020 19:25:39 +0200 Subject: Stuff to fix --- src/p_lcom_next.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++--------- src/p_lcom_next.h | 7 ++-- src/s_lcom.c | 9 +++--- 3 files changed, 90 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 4d0ca72..e3f1dfa 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -17,6 +17,7 @@ #include "d_enum.h" #include "s_struct.h" #include "u_vars.h" +#include "u_vars_next.h" static int8_t subst_those_vars(int64_t i, @@ -43,7 +44,7 @@ static int8_t } char - **subst_vars(char *words[], + **p_subst_vars(char *words[], t_msh *msh) { char **p_words; @@ -66,9 +67,8 @@ char return (words); } - char - **subst_args(const char word[], + **p_subst_args(const char word[], int8_t redir) { char **words; @@ -97,31 +97,96 @@ char return (words); } -char - **p_check_first_arg_equals(char *words[]) +static void + p_register_word(char word[], + t_msh *msh) { - t_bool equals; + char name[255]; + char val[255]; char *ptr; + size_t i; - equals = FALSE; - ptr = words[0]; + name[0] = '$'; + ptr = word; + i = 1; + while (*ptr != '=' && *ptr != '\0') + { + name[i] = *ptr; + i++; + ptr++; + } + name[i] = '\0'; + ptr++; + i = 0; while (*ptr != '\0') { - if (*ptr == '=') + val[i] = *ptr; + i++; + ptr++; + } + u_subst_var_value(name, val, msh); +} + +static char + **p_add_to_variables_and_delete( /* t_bool equals[], */ + char *words[], + int64_t i, + t_msh *msh) +{ + char *ptr; + t_bool reg; + int64_t j; + + reg = FALSE; + ptr = words[i - 1]; + while (*ptr != '\0' && *ptr != '=') + { + ptr++; + } + if (*ptr != '\0') + { + reg = TRUE; + } + j = 0; + if (reg == TRUE) + { + while (words[j]) { - equals = TRUE; - break ; + p_register_word(words[j], msh); + j++; } - ptr++; } - if (equals == TRUE && words[1] != NULL) + j = 0; + while (j < i) { ft_memdel((void*)&words[0]); words = words + 1; - return (words); + j++; } - else + return (words); +} + +char + **p_check_args_equals(char *words[], + t_msh *msh) +{ + /* t_bool equals[255]; */ + char *ptr; + int64_t i; + + /* i = -1; */ + /* while (++i < 255) */ + /* equals[i] = FALSE; */ + i = -1; + while (words[++i]) { - return (words); + ptr = words[i]; + while (*ptr != '\0' && *ptr != '=') + ptr++; + /* if (*ptr != '\0') */ + /* equals[i] = TRUE; */ + if (*ptr == '\0') + break ; } + return (p_add_to_variables_and_delete(/* equals, */words, i, msh)); } diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h index ad5c5c5..cd1ef0d 100644 --- a/src/p_lcom_next.h +++ b/src/p_lcom_next.h @@ -17,10 +17,11 @@ #include "s_struct.h" -char **subst_vars(char *words[], +char **p_subst_vars(char *words[], t_msh *msh); -char **subst_args(const char word[], +char **p_subst_args(const char word[], int8_t redir); -char **p_check_first_arg_equals(char *words[]); +char **p_check_args_equals(char *words[], + t_msh *msh); #endif diff --git a/src/s_lcom.c b/src/s_lcom.c index 773814f..088f625 100644 --- a/src/s_lcom.c +++ b/src/s_lcom.c @@ -127,17 +127,18 @@ t_lcom link->pipes = NULL; if (get_redir(word, &link) != 0) return (NULL); - if (!(words = subst_args(word, link->redir))) + if (!(words = p_subst_args(word, link->redir))) return (NULL); - if (!(words = subst_vars(words, msh))) + if (!(words = p_subst_vars(words, msh))) return (NULL); - words = p_check_first_arg_equals(words); + words = p_check_args_equals(words, msh); if (fill_lcom(words, &link) < 0) { ft_delwords(words); return (NULL); } link->next = NULL; - ft_delwords(words); + if (words[0] != NULL) + ft_delwords(words); return (link); } -- cgit v1.2.3 From 11aeaaf8deeb265ac98cbabfa501d2da155475d7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 31 Jul 2020 23:47:45 +0200 Subject: The hell it works, now asan lol --- src/p_lcom_next.c | 68 ++++++++++++++++++++++++++++++++----------------------- src/s_lcom.c | 3 +-- 2 files changed, 41 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index e3f1dfa..f340294 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -15,10 +15,14 @@ #include #include "d_enum.h" +#include "s_destroy.h" +#include "f_fail.h" #include "s_struct.h" #include "u_vars.h" #include "u_vars_next.h" +/* TODO: norme */ + static int8_t subst_those_vars(int64_t i, char **p_words, @@ -124,69 +128,77 @@ static void i++; ptr++; } + val[i] = '\0'; u_subst_var_value(name, val, msh); } static char **p_add_to_variables_and_delete( /* t_bool equals[], */ char *words[], + t_bool reg, int64_t i, t_msh *msh) { - char *ptr; - t_bool reg; int64_t j; + int64_t k; + char **rewords; - reg = FALSE; - ptr = words[i - 1]; - while (*ptr != '\0' && *ptr != '=') - { - ptr++; - } - if (*ptr != '\0') - { - reg = TRUE; - } j = 0; if (reg == TRUE) { - while (words[j]) + while (words[j] && j < i) { p_register_word(words[j], msh); j++; } } j = 0; - while (j < i) - { - ft_memdel((void*)&words[0]); - words = words + 1; + while (words[i + j] != NULL) j++; + if (!(rewords = (char**)malloc((j + 1) * sizeof(char*)))) + { + ft_delwords(words); + s_destroy(msh); + f_fail_alloc(msh); } - return (words); + k = i; + while (i - k < j) + { + if (!(rewords[i - k] = ft_strdup(words[i]))) + { + ft_delwords(words); + s_destroy(msh); + f_fail_alloc(msh); + } + i++; + } + rewords[i - k] = 0; + ft_delwords(words); + i++; + return (rewords); } char **p_check_args_equals(char *words[], t_msh *msh) { - /* t_bool equals[255]; */ char *ptr; + t_bool reg; int64_t i; - /* i = -1; */ - /* while (++i < 255) */ - /* equals[i] = FALSE; */ - i = -1; - while (words[++i]) + i = 0; + reg = TRUE; + while (words[i]) { ptr = words[i]; while (*ptr != '\0' && *ptr != '=') ptr++; - /* if (*ptr != '\0') */ - /* equals[i] = TRUE; */ - if (*ptr == '\0') + if (*ptr == '\0' || words[i][0] == '=') + { + reg = FALSE; break ; + } + i++; } - return (p_add_to_variables_and_delete(/* equals, */words, i, msh)); + return (p_add_to_variables_and_delete(/* equals, */words, reg, i, msh)); } diff --git a/src/s_lcom.c b/src/s_lcom.c index 088f625..f1ef2a4 100644 --- a/src/s_lcom.c +++ b/src/s_lcom.c @@ -138,7 +138,6 @@ t_lcom return (NULL); } link->next = NULL; - if (words[0] != NULL) - ft_delwords(words); + ft_delwords(words); return (link); } -- cgit v1.2.3 From 1c1901379b76e9869e3612d217c60784858c60ad Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 31 Jul 2020 23:59:35 +0200 Subject: Leak free --- src/e_builtins.c | 2 +- src/m_minishell.c | 1 - src/u_vars.c | 7 ++++--- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/e_builtins.c b/src/e_builtins.c index fe4f7e1..5f53f90 100644 --- a/src/e_builtins.c +++ b/src/e_builtins.c @@ -30,6 +30,6 @@ void dup_redirs(ptr, msh); ret = msh->bu_ptr[bu_id](ptr->argv + 1, msh); - lcom_clear(&msh->curr); + /* lcom_clear(&msh->curr); */ msh->ret = ret; } diff --git a/src/m_minishell.c b/src/m_minishell.c index 9306955..b4a8876 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -47,7 +47,6 @@ int ft_memdel((void*)&str_one); ft_memdel((void*)&str_two); /* TODO: delet this */ - u_subst_var_value("$var", "value", msh); ret = m_argv(argc, argv, msh); s_destroy(msh); return (ret); diff --git a/src/u_vars.c b/src/u_vars.c index a852d6f..ebd6a03 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -22,7 +22,7 @@ static char *set_rva(const char varname[], - t_msh *msh) + t_msh *msh) { char *rvarname; @@ -41,8 +41,8 @@ static char static char *dup_env(char *p_env, - char *rvarname, - t_msh *msh) + char *rvarname, + t_msh *msh) { char *varval; @@ -80,6 +80,7 @@ static char } p_env += 1; } + ft_memdel((void*)&rvarname); return (NULL); } -- cgit v1.2.3 From d04b1f10ac2da672383dd05e5d4065d2d7dd8c61 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 19:00:37 +0200 Subject: Opti --- src/p_lcom_next.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index f340294..b106032 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -133,11 +133,10 @@ static void } static char - **p_add_to_variables_and_delete( /* t_bool equals[], */ - char *words[], - t_bool reg, - int64_t i, - t_msh *msh) + **p_add_to_variables_and_delete(char *words[], + t_bool reg, + int64_t i, + t_msh *msh) { int64_t j; int64_t k; @@ -184,10 +183,12 @@ char { char *ptr; t_bool reg; + t_bool isvar; int64_t i; i = 0; reg = TRUE; + isvar = TRUE; while (words[i]) { ptr = words[i]; @@ -196,9 +197,13 @@ char if (*ptr == '\0' || words[i][0] == '=') { reg = FALSE; + if (i == 0) + isvar = FALSE; break ; } i++; } - return (p_add_to_variables_and_delete(/* equals, */words, reg, i, msh)); + if (isvar == TRUE) + return (p_add_to_variables_and_delete(words, reg, i, msh)); + return (words); } -- cgit v1.2.3 From 27046e89845d2c25ed743a074815c6ef88a7e6de Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 20:05:15 +0200 Subject: Bug fix --- src/b_export.c | 30 +++++++++++++++++------------- src/p_lcom_next.c | 2 +- 2 files changed, 18 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 9cb1c14..8225b51 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -14,12 +14,13 @@ #include #include +#include "d_enum.h" #include "b_env.h" #include "f_fail.h" #include "s_struct.h" #include "u_utils.h" -static int8_t +static t_bool check_valid_identifier(const char *arg) { char *ptr; @@ -27,24 +28,24 @@ static int8_t ptr = (char*)arg; if (ft_isalpha(ptr[0])) { - return (1); + return (TRUE); } - return (0); + return (FALSE); } -static int8_t +static t_bool check_equals(const char *arg) { char *ptr; ptr = (char*)arg; - while (*ptr) + while (*ptr != '\0') { if (*ptr == '=') - return (1); + return (TRUE); ptr++; } - return (0); + return (FALSE); } uint8_t @@ -53,7 +54,7 @@ uint8_t { const uint64_t argc = get_argc((const char**)args); char **ptr; - int8_t next; + t_bool next; uint8_t r; if (argc == 0) @@ -65,16 +66,16 @@ uint8_t while (*ptr) { ft_printf("export: [%s]\n", *ptr); - next = 0; - if (!check_valid_identifier(*ptr)) + next = FALSE; + if (check_valid_identifier(*ptr) == FALSE) { f_fail_identifier("export", *ptr, msh); - next = 1; + next = TRUE; r = 1; } - if (next == 0 && !check_equals(*ptr)) + if (next == FALSE && check_equals(*ptr) == FALSE) { - next = 1; + next = TRUE; /* TODO: export already set variable */ /* examples: */ /* ~> QWE=qwe */ @@ -83,6 +84,9 @@ uint8_t /* # non-existant variable QWE */ /* ~> export QWE */ } + else if (next == FALSE && check_equals(*ptr) == TRUE) + { + } ptr++; } /* TODO: finish export */ diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index b106032..1df0a1f 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -194,7 +194,7 @@ char ptr = words[i]; while (*ptr != '\0' && *ptr != '=') ptr++; - if (*ptr == '\0' || words[i][0] == '=') + if (*ptr == '\0' || words[i][0] == '=' || ft_isdigit(words[i][0])) { reg = FALSE; if (i == 0) -- cgit v1.2.3 From 7c0ce2228470d69a9dcc1d734f000c77c026ec24 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 20:48:32 +0200 Subject: export first part is working fine --- src/b_export.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- src/u_vars.c | 2 +- src/u_vars.h | 2 ++ 3 files changed, 59 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 8225b51..9fe5709 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -17,8 +17,12 @@ #include "d_enum.h" #include "b_env.h" #include "f_fail.h" +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_lvars.h" #include "s_struct.h" #include "u_utils.h" +#include "u_vars.h" static t_bool check_valid_identifier(const char *arg) @@ -48,12 +52,55 @@ static t_bool return (FALSE); } +static void + b_add_to_env_from_globals(const char varname[], + const char var[], + t_msh *msh) +{ + size_t i; + char **nenvp; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + i = 0; + while (msh->envp[i] != NULL) + { + if (!(nenvp[i] = ft_strdup(msh->envp[i]))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + i++; + } + if (!(nenvp[i] = ft_strdup(var))) + { + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); + } + nenvp[i + 1] = 0; + ft_delwords(msh->envp); + lvars_delone(&msh->vars, varname); + msh->envp = nenvp; +} + uint8_t b_export(char *args[], - t_msh *msh) + t_msh *msh) { + /* TODO: norme */ const uint64_t argc = get_argc((const char**)args); char **ptr; + char *varval; + char fmt[255]; t_bool next; uint8_t r; @@ -65,7 +112,6 @@ uint8_t r = 0; while (*ptr) { - ft_printf("export: [%s]\n", *ptr); next = FALSE; if (check_valid_identifier(*ptr) == FALSE) { @@ -76,16 +122,17 @@ uint8_t if (next == FALSE && check_equals(*ptr) == FALSE) { next = TRUE; - /* TODO: export already set variable */ - /* examples: */ - /* ~> QWE=qwe */ - /* ~> export QWE */ - /* -------------- */ - /* # non-existant variable QWE */ - /* ~> export QWE */ + ft_sprintf(fmt, "$%s", *ptr); + varval = get_cstm_vr(fmt, msh); + if (varval != NULL) + { + ft_sprintf(fmt, "%s=%s", *ptr, varval); + b_add_to_env_from_globals(*ptr, fmt, msh); + } } else if (next == FALSE && check_equals(*ptr) == TRUE) { + /* TODO: verify global vars, delete if needed, export the thing */ } ptr++; } diff --git a/src/u_vars.c b/src/u_vars.c index ebd6a03..f7123ef 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -113,7 +113,7 @@ static char return (NULL); } -static char +char *get_cstm_vr(const char varname[], t_msh *msh) { diff --git a/src/u_vars.h b/src/u_vars.h index fb8b924..ec6ecf5 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,6 +15,8 @@ #include "s_struct.h" +char *get_cstm_vr(const char varname[], + t_msh *msh); char *u_get_var_value(const char varname[], t_msh *msh); -- cgit v1.2.3 From 8680737a802539f3c21a295ad45eb9be72c73f5f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 21:25:09 +0200 Subject: More UNIX friendly libft --- src/b_export.c | 18 +++--------------- src/f_alloc.c | 36 ++++++++++++++++++++++++++++++++++++ src/f_alloc.h | 21 +++++++++++++++++++++ src/f_fail.c | 7 ------- src/f_fail.h | 2 +- 5 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 src/f_alloc.c create mode 100644 src/f_alloc.h (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 9fe5709..9a7c37d 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -64,28 +64,16 @@ static void while (msh->envp[i] != NULL) i++; if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) - { - lcom_clear(&msh->curr); - s_destroy(msh); - f_fail_alloc(msh); - } + f_fail_alloc_and_destroy(msh); i = 0; while (msh->envp[i] != NULL) { if (!(nenvp[i] = ft_strdup(msh->envp[i]))) - { - lcom_clear(&msh->curr); - s_destroy(msh); - f_fail_alloc(msh); - } + f_fail_alloc_and_destroy(msh); i++; } if (!(nenvp[i] = ft_strdup(var))) - { - lcom_clear(&msh->curr); - s_destroy(msh); - f_fail_alloc(msh); - } + f_fail_alloc_and_destroy(msh); nenvp[i + 1] = 0; ft_delwords(msh->envp); lvars_delone(&msh->vars, varname); diff --git a/src/f_alloc.c b/src/f_alloc.c new file mode 100644 index 0000000..bfa75c3 --- /dev/null +++ b/src/f_alloc.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_alloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include + +#include "s_destroy.h" +#include "s_lcom.h" +#include "s_struct.h" + +void + f_fail_alloc(t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno)); + exit(FT_RET_ALLOC); +} + +void + f_fail_alloc_and_destroy(t_msh *msh) +{ + lcom_clear(&msh->curr); + s_destroy(msh); + f_fail_alloc(msh); +} diff --git a/src/f_alloc.h b/src/f_alloc.h new file mode 100644 index 0000000..34a5e7d --- /dev/null +++ b/src/f_alloc.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* f_alloc.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef F_ALLOC_H +#define F_ALLOC_H + +#include "s_struct.h" + +void f_fail_alloc(t_msh *msh); +void f_fail_alloc_and_destroy(t_msh *msh); + +#endif diff --git a/src/f_fail.c b/src/f_fail.c index a3a1df1..5e6824f 100644 --- a/src/f_fail.c +++ b/src/f_fail.c @@ -49,10 +49,3 @@ void { write_fail(concern, FT_FAIL_TOO_MANY_ARGS, msh); } - -void - f_fail_alloc(t_msh *msh) -{ - ft_dprintf(STDERR_FILENO, "%s: %s\n", msh->shname, strerror(errno)); - exit(FT_RET_ALLOC); -} diff --git a/src/f_fail.h b/src/f_fail.h index 868fe75..4d7c8d4 100644 --- a/src/f_fail.h +++ b/src/f_fail.h @@ -14,6 +14,7 @@ #define F_FAIL_H #include "f_com.h" +#include "f_alloc.h" #include "f_chdir.h" #include "f_errno.h" #include "f_redir.h" @@ -26,6 +27,5 @@ void f_fail_too_many_args(const char concern[], void f_fail_identifier(const char concern[], const char identifier[], t_msh *msh); -void f_fail_alloc(t_msh *msh); #endif -- cgit v1.2.3 From ebee57ed2034a90538a75648453951480f17318d Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 1 Aug 2020 22:19:08 +0200 Subject: Pretty good --- src/b_export.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 9a7c37d..52790e4 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -88,6 +88,7 @@ uint8_t const uint64_t argc = get_argc((const char**)args); char **ptr; char *varval; + /* char *varname; */ char fmt[255]; t_bool next; uint8_t r; -- cgit v1.2.3 From 6ca882c8feb9fb88cf8e1421e882fb7e9efb97f8 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 16:50:32 +0200 Subject: In progress --- src/b_export.c | 5 +++-- src/b_export_next.c | 11 +++++++++++ src/b_export_next.h | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 52790e4..c47a8fb 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -14,8 +14,9 @@ #include #include -#include "d_enum.h" #include "b_env.h" +#include "b_export_next.h" +#include "d_enum.h" #include "f_fail.h" #include "s_destroy.h" #include "s_lcom.h" @@ -121,7 +122,7 @@ uint8_t } else if (next == FALSE && check_equals(*ptr) == TRUE) { - /* TODO: verify global vars, delete if needed, export the thing */ + b_export_with_equals(*ptr, msh); } ptr++; } diff --git a/src/b_export_next.c b/src/b_export_next.c index 44d2b9b..2641f65 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -10,3 +10,14 @@ /* */ /* ************************************************************************** */ +#include + +#include "s_struct.h" + +void + b_export_with_equals(const char arg[], + t_msh *msh) +{ + (void)arg; + (void)msh; +} diff --git a/src/b_export_next.h b/src/b_export_next.h index 4a8781b..8df8746 100644 --- a/src/b_export_next.h +++ b/src/b_export_next.h @@ -15,4 +15,6 @@ #include "s_struct.h" +void b_export_with_equals(const char arg[], t_msh *msh); + #endif -- cgit v1.2.3 From 2ed2a9d739b6dd3be489792ef510b1f2085ab4ce Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 16:56:17 +0200 Subject: Cleaner headers --- src/b_cd.h | 3 +-- src/b_echo.h | 3 +-- src/b_env.h | 3 +-- src/b_exit.h | 4 +--- src/b_export.h | 4 +--- src/b_pwd.h | 3 +-- src/b_type.h | 3 +-- src/b_unset.h | 3 +-- src/d_define.h | 6 +++--- src/e_builtins.h | 4 +--- src/e_externs.h | 3 +-- src/e_externs_next.h | 4 +--- src/f_chdir.h | 4 +--- src/f_errno.h | 3 +-- src/f_fail.h | 6 ++---- src/f_redir.h | 3 +-- src/m_argv.h | 4 +--- src/m_comm.h | 3 +-- src/m_prompt.h | 1 + src/m_redirs.h | 3 +-- src/p_lcom.h | 7 ++----- src/p_lcom_next.h | 9 +++------ src/p_line.h | 3 +-- src/s_init.h | 3 +-- src/s_lvars.h | 13 ++++--------- src/u_vars.h | 6 ++---- 26 files changed, 36 insertions(+), 75 deletions(-) (limited to 'src') diff --git a/src/b_cd.h b/src/b_cd.h index 629cf32..977afd7 100644 --- a/src/b_cd.h +++ b/src/b_cd.h @@ -17,7 +17,6 @@ #include "s_struct.h" -uint8_t b_cd(char *args[], - t_msh *msh); +uint8_t b_cd(char *args[], t_msh *msh); #endif diff --git a/src/b_echo.h b/src/b_echo.h index 3481480..ba4698e 100644 --- a/src/b_echo.h +++ b/src/b_echo.h @@ -17,7 +17,6 @@ #include "s_struct.h" -uint8_t b_echo(char *args[], - t_msh *msh); +uint8_t b_echo(char *args[], t_msh *msh); #endif diff --git a/src/b_env.h b/src/b_env.h index bd338ae..6ad2800 100644 --- a/src/b_env.h +++ b/src/b_env.h @@ -17,7 +17,6 @@ #include "s_struct.h" -uint8_t b_env(char *args[], - t_msh *msh); +uint8_t b_env(char *args[], t_msh *msh); #endif diff --git a/src/b_exit.h b/src/b_exit.h index d437e6a..1c6ec05 100644 --- a/src/b_exit.h +++ b/src/b_exit.h @@ -17,8 +17,6 @@ #include "s_struct.h" -uint8_t - b_exit(char *args[], - t_msh *msh); +uint8_t b_exit(char *args[], t_msh *msh); #endif diff --git a/src/b_export.h b/src/b_export.h index 037f9d8..be703c2 100644 --- a/src/b_export.h +++ b/src/b_export.h @@ -15,8 +15,6 @@ #include "s_struct.h" -uint8_t - b_export(char *args[], - t_msh *msh); +uint8_t b_export(char *args[], t_msh *msh); #endif diff --git a/src/b_pwd.h b/src/b_pwd.h index e165284..649ce8e 100644 --- a/src/b_pwd.h +++ b/src/b_pwd.h @@ -17,7 +17,6 @@ #include "s_struct.h" -uint8_t b_pwd(char *args[], - t_msh *msh); +uint8_t b_pwd(char *args[], t_msh *msh); #endif diff --git a/src/b_type.h b/src/b_type.h index 5828e8d..474ba26 100644 --- a/src/b_type.h +++ b/src/b_type.h @@ -17,7 +17,6 @@ #include "s_struct.h" -uint8_t b_type(char *args[], - t_msh *msh); +uint8_t b_type(char *args[], t_msh *msh); #endif diff --git a/src/b_unset.h b/src/b_unset.h index 24e17f7..fa89161 100644 --- a/src/b_unset.h +++ b/src/b_unset.h @@ -17,7 +17,6 @@ #include "s_struct.h" -uint8_t b_unset(char *args[], - t_msh *msh); +uint8_t b_unset(char *args[], t_msh *msh); #endif diff --git a/src/d_define.h b/src/d_define.h index b2f2f08..f246a5b 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -19,9 +19,9 @@ ** ====== CLASSICS ====== */ -#define FT_PS_ONE "minishell ~> " -#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type" -#define FT_HISTFILE "minishell_history" +#define FT_PS_ONE "minishell ~> " +#define FT_BUILTINS "echo|cd|pwd|export|unset|env|exit|type" +#define FT_HISTFILE "minishell_history" /* ** ====== OPTIONS ====== diff --git a/src/e_builtins.h b/src/e_builtins.h index 1d0804d..25026cc 100644 --- a/src/e_builtins.h +++ b/src/e_builtins.h @@ -17,8 +17,6 @@ #include "s_struct.h" -void e_builtin(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh); +void e_builtin(const t_lcom *ptr, uint8_t bu_id, t_msh *msh); #endif diff --git a/src/e_externs.h b/src/e_externs.h index 9bbb907..878bb83 100644 --- a/src/e_externs.h +++ b/src/e_externs.h @@ -15,7 +15,6 @@ #include "s_struct.h" -void e_extern(t_lcom *ptr, - t_msh *msh); +void e_extern(t_lcom *ptr, t_msh *msh); #endif diff --git a/src/e_externs_next.h b/src/e_externs_next.h index da1cfdf..db03bac 100644 --- a/src/e_externs_next.h +++ b/src/e_externs_next.h @@ -16,8 +16,6 @@ #include "s_struct.h" char **get_env_path(t_msh *msh); -char *search_in_path(const char com[], - char *envpath[], - t_msh *msh); +char *search_in_path(const char com[], char *envpath[], t_msh *msh); #endif diff --git a/src/f_chdir.h b/src/f_chdir.h index 76cb737..c4136af 100644 --- a/src/f_chdir.h +++ b/src/f_chdir.h @@ -15,8 +15,6 @@ #include "s_struct.h" -void f_fail_chd(const char concern[], - const char pathp[], - t_msh *msh); +void f_fail_chd(const char concern[], const char pathp[], t_msh *msh); #endif diff --git a/src/f_errno.h b/src/f_errno.h index 75a6159..bf9e36b 100644 --- a/src/f_errno.h +++ b/src/f_errno.h @@ -15,7 +15,6 @@ #include "s_struct.h" -void f_dump_errno(const char concern[], - t_msh *msh); +void f_dump_errno(const char concern[], t_msh *msh); #endif diff --git a/src/f_fail.h b/src/f_fail.h index 4d7c8d4..9a4b37c 100644 --- a/src/f_fail.h +++ b/src/f_fail.h @@ -20,10 +20,8 @@ #include "f_redir.h" #include "s_struct.h" -void f_fail_no_options(const char concern[], - t_msh *msh); -void f_fail_too_many_args(const char concern[], - t_msh *msh); +void f_fail_no_options(const char concern[], t_msh *msh); +void f_fail_too_many_args(const char concern[], t_msh *msh); void f_fail_identifier(const char concern[], const char identifier[], t_msh *msh); diff --git a/src/f_redir.h b/src/f_redir.h index 25f53cf..450fce5 100644 --- a/src/f_redir.h +++ b/src/f_redir.h @@ -15,7 +15,6 @@ #include "s_struct.h" -void f_redir(const char path[], - t_msh *msh); +void f_redir(const char path[], t_msh *msh); #endif diff --git a/src/m_argv.h b/src/m_argv.h index cdda5d5..8d12d05 100644 --- a/src/m_argv.h +++ b/src/m_argv.h @@ -17,8 +17,6 @@ #include "s_struct.h" -uint8_t m_argv(int argc, - char *const argv[], - t_msh *msh); +uint8_t m_argv(int argc, char *const argv[], t_msh *msh); #endif diff --git a/src/m_comm.h b/src/m_comm.h index a64d01e..3933cfe 100644 --- a/src/m_comm.h +++ b/src/m_comm.h @@ -17,7 +17,6 @@ #include "s_struct.h" -uint8_t m_comm(const char line[], - t_msh *msh); +uint8_t m_comm(const char line[], t_msh *msh); #endif diff --git a/src/m_prompt.h b/src/m_prompt.h index f929298..3697fae 100644 --- a/src/m_prompt.h +++ b/src/m_prompt.h @@ -17,4 +17,5 @@ void m_prompt(t_msh *msh); void m_cont_prompt(void); + #endif diff --git a/src/m_redirs.h b/src/m_redirs.h index 295ea4d..6d1b96a 100644 --- a/src/m_redirs.h +++ b/src/m_redirs.h @@ -15,7 +15,6 @@ #include "s_struct.h" -void dup_redirs(const t_lcom *ptr, - t_msh *msh); +void dup_redirs(const t_lcom *ptr, t_msh *msh); #endif diff --git a/src/p_lcom.h b/src/p_lcom.h index 4e37399..393d7e5 100644 --- a/src/p_lcom.h +++ b/src/p_lcom.h @@ -17,10 +17,7 @@ #include "s_struct.h" -int8_t get_redir(const char word[], - t_lcom **link); -int8_t p_lcom(const char line[], - const uint64_t count, - t_msh *msh); +int8_t get_redir(const char word[], t_lcom **link); +int8_t p_lcom(const char line[], const uint64_t count, t_msh *msh); #endif diff --git a/src/p_lcom_next.h b/src/p_lcom_next.h index cd1ef0d..2d0ae98 100644 --- a/src/p_lcom_next.h +++ b/src/p_lcom_next.h @@ -17,11 +17,8 @@ #include "s_struct.h" -char **p_subst_vars(char *words[], - t_msh *msh); -char **p_subst_args(const char word[], - int8_t redir); -char **p_check_args_equals(char *words[], - t_msh *msh); +char **p_subst_vars(char *words[], t_msh *msh); +char **p_subst_args(const char word[], int8_t redir); +char **p_check_args_equals(char *words[], t_msh *msh); #endif diff --git a/src/p_line.h b/src/p_line.h index 7e8a321..b3a35c5 100644 --- a/src/p_line.h +++ b/src/p_line.h @@ -15,7 +15,6 @@ #include "s_struct.h" -void p_line(char *line, - t_msh *msh); +void p_line(char *line, t_msh *msh); #endif diff --git a/src/s_init.h b/src/s_init.h index 8ffe954..3539086 100644 --- a/src/s_init.h +++ b/src/s_init.h @@ -17,7 +17,6 @@ #include "s_struct.h" -t_msh *init_msh(char *const argv[], - char *const envp[]); +t_msh *init_msh(char *const argv[], char *const envp[]); #endif diff --git a/src/s_lvars.h b/src/s_lvars.h index 6bc0a99..163f6cc 100644 --- a/src/s_lvars.h +++ b/src/s_lvars.h @@ -15,15 +15,10 @@ #include "s_struct.h" -void lvars_rebind(t_lvars **lvars, - const char name[], - const char newval[]); -void lvars_delone(t_lvars **lvars, - const char name[]); -void lvars_add_front(t_lvars **alvars, - t_lvars *new); +void lvars_rebind(t_lvars **lvars, const char name[], const char newval[]); +void lvars_delone(t_lvars **lvars, const char name[]); +void lvars_add_front(t_lvars **alvars, t_lvars *new); void lvars_clear(t_lvars **lvars); -t_lvars *lvars_new(const char name[], - const char val[]); +t_lvars *lvars_new(const char name[], const char val[]); #endif diff --git a/src/u_vars.h b/src/u_vars.h index ec6ecf5..8d96050 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,9 +15,7 @@ #include "s_struct.h" -char *get_cstm_vr(const char varname[], - t_msh *msh); -char *u_get_var_value(const char varname[], - t_msh *msh); +char *get_cstm_vr(const char varname[], t_msh *msh); +char *u_get_var_value(const char varname[], t_msh *msh); #endif -- cgit v1.2.3 From 3b32afb082ba823885ac355413294b5e7ff3c360 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 17:04:30 +0200 Subject: Changed function name --- src/b_cd.c | 2 +- src/b_echo.c | 2 +- src/b_exit.c | 2 +- src/b_export.c | 2 +- src/u_utils.c | 2 +- src/u_utils.h | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/b_cd.c b/src/b_cd.c index f30e61f..441fc3c 100644 --- a/src/b_cd.c +++ b/src/b_cd.c @@ -45,7 +45,7 @@ uint8_t b_cd(char *args[], t_msh *msh) { - const uint64_t argc = get_argc((const char**)args); + const uint64_t argc = u_builtins_get_argc((const char**)args); char *path; if (argc >= 2) diff --git a/src/b_echo.c b/src/b_echo.c index a02dad5..7b998d7 100644 --- a/src/b_echo.c +++ b/src/b_echo.c @@ -81,7 +81,7 @@ uint8_t b_echo(char *args[], t_msh *msh) { - const uint64_t argc = get_argc((const char **)args); + const uint64_t argc = u_builtins_get_argc((const char **)args); char **ptr; char *str; int8_t nopt; diff --git a/src/b_exit.c b/src/b_exit.c index 5f1c446..5384d25 100644 --- a/src/b_exit.c +++ b/src/b_exit.c @@ -26,7 +26,7 @@ uint8_t t_msh *msh) { uint8_t ret; - const uint64_t argc = get_argc((const char**)args); + const uint64_t argc = u_builtins_get_argc((const char**)args); if (argc > 1) { diff --git a/src/b_export.c b/src/b_export.c index c47a8fb..fc1657e 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -86,7 +86,7 @@ uint8_t t_msh *msh) { /* TODO: norme */ - const uint64_t argc = get_argc((const char**)args); + const uint64_t argc = u_builtins_get_argc((const char**)args); char **ptr; char *varval; /* char *varname; */ diff --git a/src/u_utils.c b/src/u_utils.c index c95cbb0..20c5cfc 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -15,7 +15,7 @@ #include uint64_t - get_argc(const char *args[]) + u_builtins_get_argc(const char *args[]) { uint64_t argc; diff --git a/src/u_utils.h b/src/u_utils.h index a02e7e6..d822343 100644 --- a/src/u_utils.h +++ b/src/u_utils.h @@ -15,6 +15,6 @@ #include -uint64_t get_argc(const char *args[]); +uint64_t u_builtins_get_argc(const char *args[]); #endif -- cgit v1.2.3 From 1886374416e5dc436db68e3518603258ae32bac0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 17:26:41 +0200 Subject: Splitted var --- src/b_export.c | 1 - src/b_export_next.c | 31 +++++++++++++++++++++++++++++-- src/b_export_next.h | 7 +++++++ 3 files changed, 36 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index fc1657e..7aeed93 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -89,7 +89,6 @@ uint8_t const uint64_t argc = u_builtins_get_argc((const char**)args); char **ptr; char *varval; - /* char *varname; */ char fmt[255]; t_bool next; uint8_t r; diff --git a/src/b_export_next.c b/src/b_export_next.c index 2641f65..f0bb804 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -11,13 +11,40 @@ /* ************************************************************************** */ #include +#include +#include "b_export_next.h" +#include "f_fail.h" #include "s_struct.h" +static char + **b_get_var(const char arg[], + t_msh *msh) +{ + size_t len; + char **var; + + if ((var = (char**)malloc(3 * sizeof(char*))) == NULL) + f_fail_alloc_and_destroy(msh); + len = 0; + while (arg[len] != '=' && arg[len] != '\0') + len++; + len += 1; + if ((var[FT_VAR_NAME] = (char*)malloc((len) * sizeof(char))) == NULL) + f_fail_alloc_and_destroy(msh); + ft_strlcpy(var[FT_VAR_NAME], arg, len); + if ((var[FT_VAR_VAL] = ft_strdup(arg + len)) == NULL) + f_fail_alloc_and_destroy(msh); + var[FT_VAR_NULL] = NULL; + return (var); +} + void b_export_with_equals(const char arg[], t_msh *msh) { - (void)arg; - (void)msh; + char **var; + + var = b_get_var(arg, msh); + ft_delwords(var); } diff --git a/src/b_export_next.h b/src/b_export_next.h index 8df8746..c9d53ea 100644 --- a/src/b_export_next.h +++ b/src/b_export_next.h @@ -15,6 +15,13 @@ #include "s_struct.h" +enum +{ + FT_VAR_NAME, + FT_VAR_VAL, + FT_VAR_NULL +}; + void b_export_with_equals(const char arg[], t_msh *msh); #endif -- cgit v1.2.3 From c3d540a4d1627ba8e4c140ba9aa76b85333d7a07 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 17:30:33 +0200 Subject: Added $ for varname --- src/b_export_next.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/b_export_next.c b/src/b_export_next.c index f0bb804..683fdd0 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -30,9 +30,10 @@ static char while (arg[len] != '=' && arg[len] != '\0') len++; len += 1; - if ((var[FT_VAR_NAME] = (char*)malloc((len) * sizeof(char))) == NULL) + if ((var[FT_VAR_NAME] = (char*)malloc((len + 1) * sizeof(char))) == NULL) f_fail_alloc_and_destroy(msh); - ft_strlcpy(var[FT_VAR_NAME], arg, len); + var[FT_VAR_NAME][0] = '$'; + ft_strlcpy(var[FT_VAR_NAME] + 1, arg, len); if ((var[FT_VAR_VAL] = ft_strdup(arg + len)) == NULL) f_fail_alloc_and_destroy(msh); var[FT_VAR_NULL] = NULL; @@ -46,5 +47,6 @@ void char **var; var = b_get_var(arg, msh); + ft_printf("[%s] - [%s]\n", var[FT_VAR_NAME], var[FT_VAR_VAL]); ft_delwords(var); } -- cgit v1.2.3 From bfaf4c53a8406ea0bfbd8699807b567a5322a03f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 3 Aug 2020 19:00:47 +0200 Subject: Export is working --- src/b_export.c | 6 ++--- src/b_export_next.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/d_enum.h | 6 ----- src/m_minishell.c | 9 -------- src/s_init.c | 16 ++++++++++++++ src/u_vars.c | 22 +++++++++--------- src/u_vars.h | 2 +- 7 files changed, 94 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 7aeed93..43e1c7d 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -31,7 +31,7 @@ static t_bool char *ptr; ptr = (char*)arg; - if (ft_isalpha(ptr[0])) + if (ft_isalpha(ptr[0]) || ptr[0] == '_') { return (TRUE); } @@ -89,7 +89,7 @@ uint8_t const uint64_t argc = u_builtins_get_argc((const char**)args); char **ptr; char *varval; - char fmt[255]; + char fmt[4096]; t_bool next; uint8_t r; @@ -112,7 +112,7 @@ uint8_t { next = TRUE; ft_sprintf(fmt, "$%s", *ptr); - varval = get_cstm_vr(fmt, msh); + varval = u_get_cstm_vr(fmt, msh); if (varval != NULL) { ft_sprintf(fmt, "%s=%s", *ptr, varval); diff --git a/src/b_export_next.c b/src/b_export_next.c index 683fdd0..0c143b2 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -15,7 +15,9 @@ #include "b_export_next.h" #include "f_fail.h" +#include "s_lvars.h" #include "s_struct.h" +#include "u_vars.h" static char **b_get_var(const char arg[], @@ -40,13 +42,73 @@ static char return (var); } +static int64_t + b_is_it_in_env(const char varname[], + t_msh *msh) +{ + char **p_env; + + p_env = msh->envp; + while (*p_env != NULL) + { + if (ft_strncmp(varname, *p_env, ft_strlen(varname)) == 0) + { + return (p_env - msh->envp); + } + p_env++; + } + return (-1); +} + +static void + b_add_to_env(const char arg[], + t_msh *msh) +{ + size_t i; + char **nenvp; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) + f_fail_alloc_and_destroy(msh); + i = 0; + while (msh->envp[i] != NULL) + { + if (!(nenvp[i] = ft_strdup(msh->envp[i]))) + f_fail_alloc_and_destroy(msh); + i++; + } + if (!(nenvp[i] = ft_strdup(arg))) + f_fail_alloc_and_destroy(msh); + nenvp[i + 1] = 0; + ft_delwords(msh->envp); + msh->envp = nenvp; +} + void b_export_with_equals(const char arg[], t_msh *msh) { + char *varval; char **var; + int64_t env_i; var = b_get_var(arg, msh); - ft_printf("[%s] - [%s]\n", var[FT_VAR_NAME], var[FT_VAR_VAL]); + if ((env_i = b_is_it_in_env(var[FT_VAR_NAME] + 1, msh)) != -1) + { + ft_memdel((void*)&msh->envp[env_i]); + if ((msh->envp[env_i] = ft_strdup(arg)) == NULL) + f_fail_alloc_and_destroy(msh); + } + else if ((varval = u_get_cstm_vr(var[FT_VAR_NAME], msh)) != NULL) + { + b_add_to_env(arg, msh); + lvars_delone(&msh->vars, var[FT_VAR_NAME] + 1); + } + else + { + b_add_to_env(arg, msh); + } ft_delwords(var); } diff --git a/src/d_enum.h b/src/d_enum.h index ec35ca3..921195f 100644 --- a/src/d_enum.h +++ b/src/d_enum.h @@ -20,12 +20,6 @@ ** 1: alloc err */ -typedef enum -{ - FALSE, - TRUE -} t_bool; - enum { FT_RET_FINE, diff --git a/src/m_minishell.c b/src/m_minishell.c index b4a8876..dd47ac3 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -21,8 +21,6 @@ #include "s_struct.h" #include "s_init.h" #include "s_destroy.h" -#include "u_vars.h" -#include "u_vars_next.h" #include "s_lvars.h" int @@ -31,8 +29,6 @@ int char *const envp[]) { int32_t ret; - char *str_one; - char *str_two; t_msh *msh; /* TODO: handle general variables | $var */ @@ -41,11 +37,6 @@ int ft_dprintf(2, "%s\n", strerror(errno)); return (FT_RET_ALLOC); } - u_subst_var_value("$SHLVL", - str_one =ft_itoa( - ft_atoi(str_two = u_get_var_value("$SHLVL", msh)) + 1), msh); - ft_memdel((void*)&str_one); - ft_memdel((void*)&str_two); /* TODO: delet this */ ret = m_argv(argc, argv, msh); s_destroy(msh); diff --git a/src/s_init.c b/src/s_init.c index 454cb67..377cd91 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -18,6 +18,8 @@ #include "d_define.h" #include "m_funptr.h" #include "s_init.h" +#include "u_vars.h" +#include "u_vars_next.h" static char **dupenv_del(char **nenvp, @@ -58,6 +60,19 @@ static char return (nenvp); } +static void + inc_shlvl(t_msh *msh) +{ + char *str_one; + char *str_two; + + u_subst_var_value("$SHLVL", + str_one = ft_itoa( + ft_atoi(str_two = u_get_var_value("$SHLVL", msh)) + 1), msh); + ft_memdel((void*)&str_one); + ft_memdel((void*)&str_two); +} + t_msh *init_msh(char *const argv[], char *const envp[]) @@ -81,5 +96,6 @@ t_msh init_buptr(msh); msh->curr = NULL; msh->vars = NULL; + inc_shlvl(msh); return (msh); } diff --git a/src/u_vars.c b/src/u_vars.c index f7123ef..913b07b 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -21,7 +21,7 @@ #include "s_struct.h" static char - *set_rva(const char varname[], + *u_set_rva(const char varname[], t_msh *msh) { char *rvarname; @@ -32,7 +32,7 @@ static char s_destroy(msh); f_fail_alloc(msh); } - ft_memcpy((char*)rvarname, (const char*)varname + 1, + (void)ft_memcpy((char*)rvarname, (const char*)varname + 1, ft_strlen(varname + 1)); *(rvarname + ft_strlen(varname + 1)) = '='; *(rvarname + ft_strlen(varname + 1) + 1) = '\0'; @@ -40,7 +40,7 @@ static char } static char - *dup_env(char *p_env, + *u_dup_env(char *p_env, char *rvarname, t_msh *msh) { @@ -58,7 +58,7 @@ static char } static char - *get_frm_env(char rvarname[], + *u_get_frm_env(char rvarname[], t_msh *msh) { char **p_env; @@ -75,7 +75,7 @@ static char pp_env += 1; if (*pp_env == '=') pp_env += 1; - varval = dup_env(pp_env, rvarname, msh); + varval = u_dup_env(pp_env, rvarname, msh); return (varval); } p_env += 1; @@ -85,7 +85,7 @@ static char } static char - *get_special_var(const char varname[], + *u_get_special_var(const char varname[], t_msh *msh) { char *varval; @@ -114,7 +114,7 @@ static char } char - *get_cstm_vr(const char varname[], + *u_get_cstm_vr(const char varname[], t_msh *msh) { t_lvars *ptr; @@ -165,18 +165,18 @@ char char *rvarname; varval = NULL; - if ((varval = get_special_var(varname, msh)) != NULL) + if ((varval = u_get_special_var(varname, msh)) != NULL) { return (varval); } - else if ((varval = get_cstm_vr(varname, msh)) != NULL) + else if ((varval = u_get_cstm_vr(varname, msh)) != NULL) { return (varval); } else { - rvarname = set_rva(varname, msh); - varval = get_frm_env(rvarname, msh); + rvarname = u_set_rva(varname, msh); + varval = u_get_frm_env(rvarname, msh); return (varval); } return (NULL); diff --git a/src/u_vars.h b/src/u_vars.h index 8d96050..0d83b4b 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -15,7 +15,7 @@ #include "s_struct.h" -char *get_cstm_vr(const char varname[], t_msh *msh); +char *u_get_cstm_vr(const char varname[], t_msh *msh); char *u_get_var_value(const char varname[], t_msh *msh); #endif -- cgit v1.2.3 From 48acb01ff5a2cf22adebfc25213c2597911b2fd4 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 13:55:12 +0200 Subject: Unset is fine --- src/b_export.c | 23 +++++------ src/b_unset.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/f_fail.c | 7 ++-- src/f_fail.h | 4 +- 4 files changed, 131 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/b_export.c b/src/b_export.c index 43e1c7d..fa9a6d1 100644 --- a/src/b_export.c +++ b/src/b_export.c @@ -26,7 +26,7 @@ #include "u_vars.h" static t_bool - check_valid_identifier(const char *arg) + check_valid_identifier(const char arg[]) { char *ptr; @@ -47,7 +47,9 @@ static t_bool while (*ptr != '\0') { if (*ptr == '=') + { return (TRUE); + } ptr++; } return (FALSE); @@ -86,25 +88,24 @@ uint8_t t_msh *msh) { /* TODO: norme */ - const uint64_t argc = u_builtins_get_argc((const char**)args); - char **ptr; - char *varval; - char fmt[4096]; - t_bool next; - uint8_t r; + char **ptr; + char *varval; + char fmt[4096]; + t_bool next; + uint8_t r; - if (argc == 0) + if (args[0] == NULL) { return (b_env(NULL, msh)); } - ptr = args; r = 0; - while (*ptr) + ptr = args; + while (*ptr != NULL) { next = FALSE; if (check_valid_identifier(*ptr) == FALSE) { - f_fail_identifier("export", *ptr, msh); + f_fail_identifier("export", *ptr); next = TRUE; r = 1; } diff --git a/src/b_unset.c b/src/b_unset.c index 92db749..598db92 100644 --- a/src/b_unset.c +++ b/src/b_unset.c @@ -10,16 +10,127 @@ /* */ /* ************************************************************************** */ +#include #include +#include +#include "f_fail.h" +#include "s_lvars.h" #include "s_struct.h" +static t_bool + check_valid_identifier(const char arg[]) +{ + char *ptr; + t_bool r; + + ptr = (char*)arg; + r = TRUE; + if (ft_isalpha(ptr[0]) == 0) + { + r = FALSE; + } + if (ptr[0] == '_') + { + r = TRUE; + } + while (*ptr != '\0') + { + if (*ptr == '=') + { + r = FALSE; + } + ptr++; + } + return (r); +} + +static void + b_realloc_env(size_t skip, + t_msh *msh) +{ + char **nenvp; + int8_t skipped; + size_t i; + + i = 0; + while (msh->envp[i] != NULL) + i++; + if (!(nenvp = (char**)malloc(i * sizeof(char*)))) + f_fail_alloc_and_destroy(msh); + i = 0; + skipped = 0; + while (msh->envp[i] != NULL) + { + if (i == skip) + { + i += 1; + skipped = 1; + } + if (!(nenvp[i - skipped] = ft_strdup(msh->envp[i]))) + f_fail_alloc_and_destroy(msh); + i++; + } + nenvp[i - 1] = 0; + ft_delwords(msh->envp); + msh->envp = nenvp; +} + +static t_bool + b_removed_from_env(const char arg[], + t_msh *msh) +{ + size_t i; + + i = 0; + while (msh->envp[i] != NULL) + { + if (ft_strncmp(arg, msh->envp[i], ft_strlen(arg)) == 0) + { + b_realloc_env(i, msh); + return (TRUE); + } + i++; + } + return (FALSE); +} + +static void + b_remove_it(const char arg[], + t_msh *msh) +{ + if (b_removed_from_env(arg, msh) == FALSE) + { + lvars_delone(&msh->vars, arg); + } +} + uint8_t b_unset(char *args[], t_msh *msh) { - (void)args; - (void)msh; - /* TODO: do unset */ - return (0); + char **ptr; + t_bool next; + int8_t r; + + if (args[0] == NULL) + return (0); + r = 0; + ptr = args; + while (*ptr != NULL) + { + next = FALSE; + if (check_valid_identifier(*ptr) == FALSE) + { + f_fail_identifier("unset", *ptr); + next = TRUE; + r = 1; + } + if (next == FALSE) + { + b_remove_it(*ptr, msh); + } + ptr++; + } + return (r); } diff --git a/src/f_fail.c b/src/f_fail.c index 5e6824f..3704b1f 100644 --- a/src/f_fail.c +++ b/src/f_fail.c @@ -36,11 +36,10 @@ void void f_fail_identifier(const char concern[], - const char identifier[], - t_msh *msh) + const char identifier[]) { - ft_dprintf(STDERR_FILENO, "%s: %s: `%s': not a valid identifier\n", - msh->shname, concern, identifier); + ft_dprintf(STDERR_FILENO, "minishell: %s: `%s': not a valid identifier\n", + concern, identifier); } void diff --git a/src/f_fail.h b/src/f_fail.h index 9a4b37c..0d3c19b 100644 --- a/src/f_fail.h +++ b/src/f_fail.h @@ -22,8 +22,6 @@ void f_fail_no_options(const char concern[], t_msh *msh); void f_fail_too_many_args(const char concern[], t_msh *msh); -void f_fail_identifier(const char concern[], - const char identifier[], - t_msh *msh); +void f_fail_identifier(const char concern[], const char identifier[]); #endif -- cgit v1.2.3 From d1ee0c0e55f75432da026f5e0e7cf15cee6918da Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 14:28:35 +0200 Subject: New libft function strclen, bug fix --- src/b_unset.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/b_unset.c b/src/b_unset.c index 598db92..03fd2a8 100644 --- a/src/b_unset.c +++ b/src/b_unset.c @@ -65,6 +65,8 @@ static void if (i == skip) { i += 1; + if (msh->envp[i] == NULL) + break ; skipped = 1; } if (!(nenvp[i - skipped] = ft_strdup(msh->envp[i]))) @@ -85,7 +87,7 @@ static t_bool i = 0; while (msh->envp[i] != NULL) { - if (ft_strncmp(arg, msh->envp[i], ft_strlen(arg)) == 0) + if (ft_strncmp(arg, msh->envp[i], ft_strclen(msh->envp[i], '=')) == 0) { b_realloc_env(i, msh); return (TRUE); -- cgit v1.2.3 From 647a984ac6b6e2629ad16d3892ad3c231e8e277a Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 14:44:55 +0200 Subject: Huge bug fix --- src/b_export_next.c | 2 +- src/s_lvars.c | 12 +++++++++--- src/u_vars.c | 6 +++--- src/u_vars_next.c | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/b_export_next.c b/src/b_export_next.c index 0c143b2..0a56968 100644 --- a/src/b_export_next.c +++ b/src/b_export_next.c @@ -51,7 +51,7 @@ static int64_t p_env = msh->envp; while (*p_env != NULL) { - if (ft_strncmp(varname, *p_env, ft_strlen(varname)) == 0) + if (ft_strncmp(varname, *p_env, ft_strclen(*p_env, '=')) == 0) { return (p_env - msh->envp); } diff --git a/src/s_lvars.c b/src/s_lvars.c index f2a1151..d9b7159 100644 --- a/src/s_lvars.c +++ b/src/s_lvars.c @@ -26,7 +26,7 @@ void t_lvars *tmp; tmp = *lvars; - while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) + while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name) + 1)) { tmp = tmp->next; } @@ -40,6 +40,12 @@ void ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); exit(FT_RET_ALLOC); } + /* TODO: delete this */ + tmp = *lvars; + while (tmp) { + ft_printf("[%s]: [%s]\n", tmp->name, tmp->val); + tmp = tmp->next; + } } void @@ -50,7 +56,7 @@ void t_lvars *prev; tmp = *lvars; - if (tmp != NULL && !ft_strncmp(tmp->name, name, ft_strlen(name))) + if (tmp != NULL && !ft_strncmp(tmp->name, name, ft_strlen(name) + 1)) { *lvars = tmp->next; ft_memdel((void*)&tmp->name); @@ -58,7 +64,7 @@ void ft_memdel((void*)&tmp); return ; } - while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name))) + while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name) + 1)) { prev = tmp; tmp = tmp->next; diff --git a/src/u_vars.c b/src/u_vars.c index 913b07b..d6ed5f2 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -68,7 +68,7 @@ static char p_env = msh->envp; while (*p_env) { - if (!ft_strncmp(rvarname, *p_env, ft_strlen(rvarname))) + if (!ft_strncmp(rvarname, *p_env, ft_strclen(*p_env, '='))) { pp_env = *p_env; while (*pp_env != '\0' && *pp_env != '=') @@ -86,7 +86,7 @@ static char static char *u_get_special_var(const char varname[], - t_msh *msh) + t_msh *msh) { char *varval; @@ -122,7 +122,7 @@ char ptr = msh->vars; while (ptr != NULL && - ft_strncmp(varname + 1, ptr->name, ft_strlen(varname + 1)) != 0) + ft_strncmp(varname + 1, ptr->name, ft_strlen(varname + 1) + 1) != 0) { ptr = ptr->next; } diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 5660449..820bbe1 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -28,7 +28,7 @@ static int64_t env_ptr = msh->envp; i = 0; while (*env_ptr && - ft_strncmp(*env_ptr, varname, ft_strlen(varname)) != 0) + ft_strncmp(varname, *env_ptr, ft_strclen(*env_ptr, '=')) != 0) { env_ptr++; i++; @@ -47,7 +47,7 @@ static t_bool ptr = msh->vars; while (ptr && - ft_strncmp(varname, ptr->name, ft_strlen(varname)) != 0) + ft_strncmp(varname, ptr->name, ft_strlen(varname) + 1) != 0) { ptr = ptr->next; } -- cgit v1.2.3 From 78a0df21c2eabb2d652fbbeb198aafdee328248e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 14:51:40 +0200 Subject: Change --- src/u_vars.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/u_vars.c b/src/u_vars.c index d6ed5f2..4001020 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -90,7 +90,7 @@ static char { char *varval; - if (!ft_strncmp(varname, FT_RET_VAR, 3)) + if (ft_strncmp(varname, FT_RET_VAR, 3) == 0) { if (!(varval = ft_uitoa(msh->ret))) { @@ -100,7 +100,7 @@ static char } return (varval); } - else if (!ft_strncmp(varname, FT_ZER_VAR, 3)) + else if (ft_strncmp(varname, FT_ZER_VAR, 3) == 0) { if (!(varval = ft_strdup(msh->shname))) { -- cgit v1.2.3 From 63e8608d87b552c690c76da0be593067840785a0 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Tue, 4 Aug 2020 15:45:22 +0200 Subject: $? fix --- src/d_define.h | 4 ++-- src/p_lcom_next.c | 2 +- src/u_vars.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/d_define.h b/src/d_define.h index f246a5b..899a2e7 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -27,8 +27,8 @@ ** ====== OPTIONS ====== */ -#define FT_OPT_INTERACT "-i" -#define FT_OPT_COMMAND "-c" +#define FT_OPT_INTERACT "-i" +#define FT_OPT_COMMAND "-c" /* ** ====== VARS ====== diff --git a/src/p_lcom_next.c b/src/p_lcom_next.c index 1df0a1f..997878e 100644 --- a/src/p_lcom_next.c +++ b/src/p_lcom_next.c @@ -36,7 +36,7 @@ static int8_t s_varname = NULL; varlen = i + 1; while ((*p_words)[varlen] != '\0' && - !ft_ischarset("$=/#@%^*+{}[],.?-", (*p_words)[varlen])) + !ft_ischarset("$=/#@%^*+{}[],.-", (*p_words)[varlen])) varlen += 1; if (!(s_varname = ft_substr(*p_words, (uint32_t)i, varlen - i))) return (-1); diff --git a/src/u_vars.c b/src/u_vars.c index 4001020..2a78f3d 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -92,7 +92,7 @@ static char if (ft_strncmp(varname, FT_RET_VAR, 3) == 0) { - if (!(varval = ft_uitoa(msh->ret))) + if ((varval = ft_uitoa(msh->ret)) == NULL) { lcom_clear(&msh->curr); s_destroy(msh); @@ -102,7 +102,7 @@ static char } else if (ft_strncmp(varname, FT_ZER_VAR, 3) == 0) { - if (!(varval = ft_strdup(msh->shname))) + if ((varval = ft_strdup(msh->shname)) == NULL) { lcom_clear(&msh->curr); s_destroy(msh); -- cgit v1.2.3