From bbc9757dccbc50fb051ca3cff585a9100023b1b1 Mon Sep 17 00:00:00 2001 From: salad Date: Fri, 18 Dec 2020 20:18:38 +0100 Subject: qweqweqweqwe --- Makefile | 1 + asd | 4 +++ src/c_init.c | 17 +------------ src/c_utils_next.c | 15 +++++++++++ src/c_utils_next.h | 3 +++ src/m_loop.c | 21 +-------------- src/m_loop_next.c | 32 +++++++++++++++++++++++ src/s_init.c | 58 ++++++++++++++++++++++------------------- src/s_init_next.c | 32 ----------------------- src/s_init_next.h | 3 +-- src/s_set_cwd.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/s_set_cwd.h | 20 +++++++++++++++ src/u_vars_next.c | 19 +++++--------- 13 files changed, 192 insertions(+), 108 deletions(-) create mode 100644 asd create mode 100644 src/s_set_cwd.c create mode 100644 src/s_set_cwd.h diff --git a/Makefile b/Makefile index 8fc99b5..c42ef99 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,7 @@ SRCS_NAME += s_destroy SRCS_NAME += s_com SRCS_NAME += s_init SRCS_NAME += s_init_next +SRCS_NAME += s_set_cwd SRCS_NAME += s_lalias SRCS_NAME += s_line SRCS_NAME += s_lvars diff --git a/asd b/asd new file mode 100644 index 0000000..0ac615b --- /dev/null +++ b/asd @@ -0,0 +1,4 @@ +m_loop GOOD 6func +s_init GOOD -->test +init_next GOOD +u_vars_next diff --git a/src/c_init.c b/src/c_init.c index c5c2dea..4420c13 100644 --- a/src/c_init.c +++ b/src/c_init.c @@ -23,6 +23,7 @@ #include "c_utils_next.h" #include "c_input.h" #include "c_utils.h" +#include "c_utils_next.h" #include "d_define.h" #include "m_prompt.h" #include "s_struct.h" @@ -76,22 +77,6 @@ static short return (0); } -static void - c_set_ptr_norme(char key, t_bool *sw, char **p, char **context) -{ - if ((key == -1 && *p - *context != 0) || key == 1) - { - *p += (key == -1) ? (2 * key) : (0); - while (*sw == FALSE && ((key == -1 && *p - *context != 0) || key == 1) - && **p != C_LF && **p != C_NUL) - *p += key; - if (*sw == TRUE) - *sw = FALSE; - if (*p - *context != 0 && **p != C_NUL) - *p += 1; - } -} - char *c_set_ptr(t_bool reset, char key, t_msh *msh) { diff --git a/src/c_utils_next.c b/src/c_utils_next.c index 27a88db..07d1c88 100644 --- a/src/c_utils_next.c +++ b/src/c_utils_next.c @@ -19,6 +19,21 @@ #include "c_utils.h" #include "d_define.h" +void c_set_ptr_norme(char key, t_bool *sw, char **p, char **context) +{ + if ((key == -1 && *p - *context != 0) || key == 1) + { + *p += (key == -1) ? (2 * key) : (0); + while (*sw == FALSE && ((key == -1 && *p - *context != 0) || key == 1) + && **p != C_LF && **p != C_NUL) + *p += key; + if (*sw == TRUE) + *sw = FALSE; + if (*p - *context != 0 && **p != C_NUL) + *p += 1; + } +} + short c_set_key(char *buf) { if (((*((unsigned int *)buf)) == UP_K) || diff --git a/src/c_utils_next.h b/src/c_utils_next.h index 1cf0132..3a26efb 100644 --- a/src/c_utils_next.h +++ b/src/c_utils_next.h @@ -18,5 +18,8 @@ short c_set_key(char *buf); void c_key_up_down(char *line[], t_caps *tcaps, t_msh *msh); t_msh *c_get_msh(int mode, t_msh *src); +void c_set_ptr_norme(char key, t_bool *sw, char **p, char **context); +short m_set_all(t_line_block *ptr, unsigned char previf, + t_bool pipe, t_msh *msh); #endif diff --git a/src/m_loop.c b/src/m_loop.c index 1641d53..8ae81e7 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -44,7 +44,6 @@ static void m_parse_and_run_line(char line[], t_msh *msh) { t_line_block *ptr; - size_t i; t_bool pipe; p_line(line, msh); @@ -55,26 +54,8 @@ static void m_parse_and_run_line(char line[], || (previf == 2 && msh->ret != 0)) { pipe = p_find_good_pipe(ptr->lblock); - if ((pipe == TRUE && s_split_pipes(ptr->lblock, msh) == NULL) - || (msh->com = s_com_new(ptr->lblock, msh)) == NULL) - { - previf = ptr->nextif; - ptr = ptr->next; + if (!(m_set_all(ptr, previf, pipe, msh))) continue ; - } - if (msh->com != NULL && pipe == FALSE) - { - i = 0; - while (msh->com->argv != NULL && msh->com->argv[i] != NULL) - i++; - if (msh->com->argv != NULL && msh->com->argv[0] != NULL) - u_subst_var_value("$_", msh->com->argv[i - 1], msh); - else - u_subst_var_value("$_", msh->com->bin, msh); - } - e_line_block(msh); - s_com_destroy(&msh->com); - s_lpipes_clear(&msh->pipes); } previf = ptr->nextif; ptr = ptr->next; diff --git a/src/m_loop_next.c b/src/m_loop_next.c index e29b6dd..cde3ccf 100644 --- a/src/m_loop_next.c +++ b/src/m_loop_next.c @@ -14,10 +14,42 @@ #include #include "d_define.h" +#include "e_line.h" #include "m_loop_counter.h" #include "m_loop_multis.h" #include "s_struct.h" #include "u_utils.h" +#include "s_com.h" +#include "s_lpipes.h" +#include "u_vars_next.h" + +short m_set_all(t_line_block *ptr, unsigned char previf, + t_bool pipe, t_msh *msh) +{ + size_t i; + + if ((pipe == TRUE && s_split_pipes(ptr->lblock, msh) == NULL) + || (msh->com = s_com_new(ptr->lblock, msh)) == NULL) + { + previf = ptr->nextif; + ptr = ptr->next; + return (-1); + } + if (msh->com != NULL && pipe == FALSE) + { + i = 0; + while (msh->com->argv != NULL && msh->com->argv[i] != NULL) + i++; + if (msh->com->argv != NULL && msh->com->argv[0] != NULL) + u_subst_var_value("$_", msh->com->argv[i - 1], msh); + else + u_subst_var_value("$_", msh->com->bin, msh); + } + e_line_block(msh); + s_com_destroy(&msh->com); + s_lpipes_clear(&msh->pipes); + return (1); +} char *m_check_multi_backslash(int fd, char line[], t_msh *msh) { diff --git a/src/s_init.c b/src/s_init.c index 1e86727..1536cd1 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -20,6 +20,7 @@ #include #include "b_export_next.h" +#include "s_set_cwd.h" #include "d_define.h" #include "f_fail.h" #include "m_funptr.h" @@ -27,7 +28,8 @@ #include "u_vars.h" #include "u_vars_next.h" -static void s_init_sqb_ref_one(t_msh *msh) +static void + s_init_sqb_ref_one(t_msh *msh) { (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_B], B_SQB_B, (ft_strlen(B_SQB_B) + 1) * sizeof(char)); @@ -55,7 +57,8 @@ static void s_init_sqb_ref_one(t_msh *msh) (ft_strlen(B_SQB_X) + 1) * sizeof(char)); } -static void s_init_sqb_ref_two(t_msh *msh) +static void + s_init_sqb_ref_two(t_msh *msh) { (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_Z], B_SQB_Z, (ft_strlen(B_SQB_Z) + 1) * sizeof(char)); @@ -83,13 +86,8 @@ static void s_init_sqb_ref_two(t_msh *msh) (ft_strlen(B_SQB_LT) + 1) * sizeof(char)); } -static void s_init_sqb_ref_thr(t_msh *msh) -{ - (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_LE], B_SQB_LE, - (ft_strlen(B_SQB_LE) + 1) * sizeof(char)); -} - -static void s_null_some(t_msh *msh) +static void + s_null_some(t_msh *msh) { msh->fd = 0; msh->curr = NULL; @@ -100,23 +98,9 @@ static void s_null_some(t_msh *msh) msh->prev_hist = NULL; } -t_msh *s_init_msh(int argc, char *const argv[], char *const envp[]) +static t_msh + *s_init_msh_next(char *cwd, t_msh *msh) { - t_msh *msh; - char cwd[PATH_MAX]; - - if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL) - return (NULL); - msh->envp = NULL; - (void)envp; - if ((msh->envp = s_dupenv(envp)) == NULL) - { - ft_memdel((void*)&msh); - return (NULL); - } - msh->argc = argc - 1; - msh->argv = (char**)argv; - msh->ret = 0; m_init_buptr(msh); s_null_some(msh); s_set_cwd(cwd, msh); @@ -135,6 +119,28 @@ t_msh *s_init_msh(int argc, char *const argv[], char *const envp[]) } s_init_sqb_ref_one(msh); s_init_sqb_ref_two(msh); - s_init_sqb_ref_thr(msh); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_LE], B_SQB_LE, + (ft_strlen(B_SQB_LE) + 1) * sizeof(char)); return (msh); } + +t_msh + *s_init_msh(int argc, char *const argv[], char *const envp[]) +{ + t_msh *msh; + char cwd[PATH_MAX]; + + if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL) + return (NULL); + msh->envp = NULL; + (void)envp; + if ((msh->envp = s_dupenv(envp)) == NULL) + { + ft_memdel((void*)&msh); + return (NULL); + } + msh->argc = argc - 1; + msh->argv = (char**)argv; + msh->ret = 0; + return (s_init_msh_next(cwd, msh)); +} diff --git a/src/s_init_next.c b/src/s_init_next.c index 6ee17d4..8ee3b7e 100644 --- a/src/s_init_next.c +++ b/src/s_init_next.c @@ -113,35 +113,3 @@ char **s_dupenv(char *const envp[]) nenvp[i] = NULL; return (nenvp); } - -void s_set_cwd(char cwd[], t_msh *msh) -{ - char fmt[PATH_MAX]; - DIR *dir; - - u_get_var_value(cwd, "$PWD", PATH_MAX, msh); - if (cwd[0] == C_NUL) - { - if (getcwd(cwd, PATH_MAX) != NULL) - { - ft_sprintf(fmt, "%s=%s", "PWD", cwd); - b_export_with_equals(fmt, msh); - } - else - ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); - return ; - } - if ((dir = opendir(cwd)) != NULL) - closedir(dir); - else if (errno == ENOENT) - { - if (getcwd(cwd, PATH_MAX) != NULL) - { - ft_sprintf(fmt, "%s=%s", "PWD", cwd); - b_export_with_equals(fmt, msh); - } - else - ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); - return ; - } -} diff --git a/src/s_init_next.h b/src/s_init_next.h index 650e795..f967dea 100644 --- a/src/s_init_next.h +++ b/src/s_init_next.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* s_init_next.h :+: :+: :+: */ +/* c_init.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -18,6 +18,5 @@ char s_inc_shlvl(t_msh *msh); char **s_dupenv_del(char **nenvp, unsigned long i); char **s_dupenv(char *const envp[]); -void s_set_cwd(char cwd[], t_msh *msh); #endif diff --git a/src/s_set_cwd.c b/src/s_set_cwd.c new file mode 100644 index 0000000..38eb750 --- /dev/null +++ b/src/s_set_cwd.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_init_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 +#ifdef __linux__ +# include +#else +# include +#endif +#include +#include +#include +#include + +#include "b_export_next.h" +#include "f_fail.h" +#include "s_destroy.h" +#include "s_struct.h" +#include "u_vars.h" +#include "u_vars_next.h" + +static void + s_set_cwd_next(char *cwd, char *fmt, t_msh *msh) +{ + if (getcwd(cwd, PATH_MAX) != NULL) + { + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); + } +} + +void + s_set_cwd(char cwd[], t_msh *msh) +{ + char fmt[PATH_MAX]; + DIR *dir; + + u_get_var_value(cwd, "$PWD", PATH_MAX, msh); + if (cwd[0] == C_NUL) + { + if (getcwd(cwd, PATH_MAX) != NULL) + { + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); + } + return ; + } + if ((dir = opendir(cwd)) != NULL) + { + closedir(dir); + } + else if (errno == ENOENT) + { + s_set_cwd_next(cwd, fmt, msh); + } +} diff --git a/src/s_set_cwd.h b/src/s_set_cwd.h new file mode 100644 index 0000000..7e17f26 --- /dev/null +++ b/src/s_set_cwd.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* c_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_SET_CWD_H +# define S_SET_CWD_H + +# include "s_struct.h" + +void s_set_cwd(char cwd[], t_msh *msh); + +#endif diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 11027cb..0b48533 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -86,33 +86,28 @@ char u_subst_var_value(const char varname[], t_msh *msh) { size_t i; - long env_line; + long en_l; char new_line_fmt[ARG_MAX]; - if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) + if ((en_l = u_get_env_var_line(varname + 1, msh)) > -1) { - ft_memdel((void*)&msh->envp[env_line]); + ft_memdel((void*)&msh->envp[en_l]); ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); - if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * + if ((msh->envp[en_l] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * sizeof(char))) == NULL) { - i = env_line; + i = en_l; while (msh->envp[++i] != NULL) ft_memdel((void*)&msh->envp[i]); ft_delwords(msh->envp); msh->envp = NULL; return (1); } - (void)ft_strlcpy(msh->envp[env_line], - new_line_fmt, ft_strlen(new_line_fmt) + 1); + ft_strlcpy(msh->envp[en_l], new_line_fmt, ft_strlen(new_line_fmt) + 1); } - else if ((env_line = u_get_custom_var_existance(varname + 1, msh)) == TRUE) - { + else if ((en_l = u_get_custom_var_existance(varname + 1, msh)) == TRUE) lvars_rebind(&msh->vars, varname + 1, newval); - } else - { lvars_add_front(&msh->vars, lvars_new(varname + 1, newval)); - } return (0); } -- cgit v1.2.3