diff options
author | salad <fmoenne-@student.le-101.fr> | 2020-10-26 13:42:56 +0100 |
---|---|---|
committer | salad <fmoenne-@student.le-101.fr> | 2020-10-26 13:42:56 +0100 |
commit | 0277ddfac754ab4ad5bdd2b692e31a717efbe569 (patch) | |
tree | 49d7c5fd3a12248af85e2c3a3254bc1538ae5775 /src/m_prompt.c | |
parent | reqdy for MERGE (diff) | |
parent | TODO update (diff) | |
download | 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.gz 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.bz2 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.xz 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.tar.zst 42-minishell-0277ddfac754ab4ad5bdd2b692e31a717efbe569.zip |
merge wif master
Diffstat (limited to 'src/m_prompt.c')
-rw-r--r-- | src/m_prompt.c | 69 |
1 files changed, 40 insertions, 29 deletions
diff --git a/src/m_prompt.c b/src/m_prompt.c index ad3c957..584a98d 100644 --- a/src/m_prompt.c +++ b/src/m_prompt.c @@ -13,6 +13,7 @@ #include <libft.h> #include <stdint.h> #include <unistd.h> +#include <limits.h> #include "d_define.h" #include "m_prompt.h" @@ -20,33 +21,45 @@ #include "u_vars.h" static char - *m_subst_prompt_rice(char var[], - t_msh *msh) + *m_subst_prompt_rice(char var[], t_msh *msh) { + char tmp[PATH_MAX]; size_t i; size_t j; - char *tmp; i = 0; - while (var[i] != '\0') + while (var[i] != C_NUL) { - if (var[i] == '\\') + if (var[i] == C_BACKS) { - if (var[i + 1] == 'w') + if (var[i + 1] == '0' && var[i + 2] == '3' && var[i + 3] == '3') { - tmp = u_get_var_value("$PWD", msh); - if (tmp != NULL) - { - var = ft_strsubst(var, "\\w", tmp); - ft_memdel((void*)&tmp); - } + (void)ft_memmove(var + i, var + (i + 3), (ft_strlen(var + (i + 3)) + 1) * sizeof(char)); + var[i] = C_ESC; + } + else if (var[i + 1] == 'n') + { + (void)ft_memmove(var + i, var + (i + 1), (ft_strlen(var + (i + 1)) + 1) * sizeof(char)); + var[i] = C_LF; + } + else if (var[i + 1] == 'h' || var[i + 1] == 'H') + { + gethostname(tmp, 255); + (void)ft_memmove(var + i + ft_strlen(tmp), var + (i + 2), (ft_strlen(var + (i + 2)) + 1) * sizeof(char)); + (void)ft_memcpy(var + i, tmp, ft_strlen(tmp) * sizeof(char)); + } + else if (var[i + 1] == 'w') + { + u_get_var_value(tmp, "$PWD", PATH_MAX, msh); + if (tmp[0] != C_NUL) + ft_strsubst_s(var, "\\w", tmp); else - var = ft_strsubst(var, "\\w", "?"); + ft_strsubst_s(var, "\\w", "?"); } else if (var[i + 1] == 'W') { - tmp = u_get_var_value("$PWD", msh); - if (tmp != NULL) + u_get_var_value(tmp, "$PWD", PATH_MAX, msh); + if (tmp[0] != C_NUL) { j = ft_strlen(tmp); if (j > 1) @@ -54,18 +67,18 @@ static char while (tmp[j] != '/' && j != 0) j--; j++; - ft_memmove(tmp, tmp + j, (ft_strlen(tmp) - j) + 1); + (void)ft_memmove(tmp, tmp + j, + ((ft_strlen(tmp) - j) + 1) * sizeof(char)); } - var = ft_strsubst(var, "\\W", tmp); - ft_memdel((void*)&tmp); + ft_strsubst_s(var, "\\W", tmp); } else - var = ft_strsubst(var, "\\w", "?"); + ft_strsubst_s(var, "\\w", "?"); } else if (var[i + 1] == 'v') - var = ft_strsubst(var, "\\v", FT_MSH_VERSION); + ft_strsubst_s(var, "\\v", FT_MSH_VERSION); else if (var[i + 1] == 's') - var = ft_strsubst(var, "\\s", FT_PROGNAME); + ft_strsubst_s(var, "\\s", FT_PROGNAME); } i++; } @@ -73,26 +86,24 @@ static char } static void - m_update_psx(uint8_t x, - t_msh *msh) + m_update_psx(uint8_t x, t_msh *msh) { - char *var; + char var[ARG_MAX]; char psx[5]; ft_sprintf(psx, "$PS%hhu", x); - if ((var = u_get_var_value(psx, msh)) == NULL) + u_get_var_value(var, psx, ARG_MAX, msh); + if (var[0] == C_NUL) { msh->ps[x - 1][0] = '\0'; return ; } - var = m_subst_prompt_rice(var, msh); + m_subst_prompt_rice(var, msh); ft_strlcpy(msh->ps[x - 1], var, ft_strlen(var) + 1); - ft_memdel((void*)&var); } void - m_prompt_psx(uint8_t x, - t_msh *msh) + m_prompt_psx(uint8_t x, t_msh *msh) { m_update_psx(x, msh); ft_dprintf(STDERR_FILENO, "%s", msh->ps[x - 1]); |