diff options
Diffstat (limited to '')
-rw-r--r-- | src/b_alias.c | 81 |
1 files changed, 75 insertions, 6 deletions
diff --git a/src/b_alias.c b/src/b_alias.c index 08f40ed..8aff531 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -10,9 +10,14 @@ /* */ /* ************************************************************************** */ +#include <libft.h> #include <stdint.h> +#include <unistd.h> +#include <limits.h> +#include "d_define.h" #include "s_struct.h" +#include "u_alias.h" #include "u_utils.h" static void @@ -28,10 +33,64 @@ static void } } -static void +static uint8_t b_print_arg(const char arg[], t_msh *msh) { char *ptr; + char val[ARG_MAX]; + t_bool invalid; + + ptr = (char*)arg; + invalid = FALSE; + while (*ptr != C_NUL && *ptr != C_EQUALS) + { + if (ft_iswhitespace(*ptr) == TRUE) + invalid = TRUE; + ptr++; + } + if (*ptr == C_NUL) + { + if (u_get_alias_value(val, arg, ARG_MAX, msh) == 0) + { + ft_printf("alias %s='%s'", arg, val); + return (0); + } + else + { + ft_dprintf(STDERR_FILENO, "minishell: alias: %s: not found\n", arg); + return (1); + } + } + else if (*ptr == C_EQUALS && invalid == TRUE) + { + ft_strlcpy(val, arg, ptr - arg); + ft_dprintf(STDERR_FILENO, + "minishell: alias: `%s': invalid alias name\n", + val); + return (1); + } + return (0); +} + +static void + b_register_arg(const char arg[], t_msh *msh) +{ + char *ptr; + char name[255]; + char value[ARG_MAX]; + + ptr = (char*)arg; + while (*ptr != C_NUL && *ptr != C_EQUALS) + { + ptr++; + } + if (*ptr == C_EQUALS) + { + ft_strlcpy(name, arg, (ptr - arg < 255) ? ((ptr - arg) + 1) : (255)); + ptr += 1; + ft_strlcpy(value, ptr, ARG_MAX); + u_set_alias_value(name, value, msh); + } } uint8_t @@ -39,7 +98,9 @@ uint8_t { const uint64_t argc = u_builtins_get_argc((const char**)args); int32_t i; + uint8_t ret; + ret = 0; if (argc == 0) { b_print_alias_list(msh); @@ -49,21 +110,29 @@ uint8_t i = 0; while (args[i] != NULL) { - b_print_arg(args[i], msh); + if (b_print_arg(args[i], msh) != 0) + { + ret = 1; + } i++; } } - return (0); + return (ret); } -uint8_t +void b_alias_mute(char *args[], t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); + int32_t i; - (void)msh; if (argc > 0) { + i = 0; + while (args[i] != NULL) + { + b_register_arg(args[i], msh); + i++; + } } - return (0); } |