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