/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* b_alias.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" #include "u_alias.h" #include "u_utils.h" static char **b_alloc_buff(t_lalias *ptr, size_t i) { char **buff; if ((buff = (char**)malloc((i + 1) * sizeof(char*))) == NULL) return (NULL); i = 0; while (ptr != NULL) { buff[i + 1] = NULL; if ((buff[i] = (char*)malloc((6 + ft_strlen(ptr->name) + 2 + ft_strlen(ptr->val) + 3) * sizeof(char))) == NULL) { ft_delwords(buff); return (NULL); } ptr = ptr->next; i++; } return (buff); } static void b_fill_buff(t_lalias *ptr, char *buff[]) { size_t i; i = 0; while (ptr != NULL) { ft_sprintf(buff[i], "alias %s='%s'\n", ptr->name, ptr->val); ptr = ptr->next; i++; } } static void b_sort_buff(char *buff[]) { size_t i; int cmp; char tmp[ARG_MAX]; i = 1; while (buff[i] != NULL) { if ((cmp = ft_strncmp(buff[i - 1], buff[i], ARG_MAX)) > 0) { ft_strlcpy(tmp, buff[i - 1], ARG_MAX); ft_memdel((void*)&buff[i - 1]); if ((buff[i - 1] = ft_strdup(buff[i])) == NULL) return ; ft_memdel((void*)&buff[i]); if ((buff[i] = ft_strdup(tmp)) == NULL) return ; i = 0; } i++; } } static void b_print_buff(char *buff[]) { char **ptr; ptr = buff; while (*ptr != NULL) { ft_printf("%s", *ptr); ptr++; } } static void b_print_alias_list(t_msh *msh) { t_lalias *ptr; char **buff; size_t i; ptr = msh->alias; i = 0; while (ptr != NULL) { ptr = ptr->next; i++; } ptr = msh->alias; if ((buff = b_alloc_buff(ptr, i)) == NULL) return ; ptr = msh->alias; b_fill_buff(ptr, buff); b_sort_buff(buff); b_print_buff(buff); ft_delwords(buff); } 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 b_alias(char *args[], t_msh *msh) { 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); } if (argc > 0) { i = 0; while (args[i] != NULL) { if (b_print_arg(args[i], msh) != 0) { ret = 1; } i++; } } return (ret); } void b_alias_mute(char *args[], t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); int32_t i; if (argc > 0) { i = 0; while (args[i] != NULL) { b_register_arg(args[i], msh); i++; } } }