/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* cmd_array.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/11/02 22:46:23 by lperrey #+# #+# */ /* Updated: 2021/11/14 12:24:39 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" static size_t cmd_words_count(t_token *t); size_t count_pipes(t_token *t) { size_t count; count = 0; while (t) { if (t->id == T_PIPE) count++; t = t->next; } return (count); } t_cmd **cmd_array_alloc(size_t cmd_nbr) { t_cmd **cmd_arr; size_t i; cmd_arr = ft_calloc(cmd_nbr + 1, sizeof (void *)); if (!cmd_arr) return (ft_retp_perror(NULL, "cmd_array_alloc()")); i = 0; while (i < cmd_nbr) { cmd_arr[i] = ft_calloc(1, sizeof (*cmd_arr[i])); if (!cmd_arr[i]) { ft_free_2d_arr(cmd_arr); return (ft_retp_perror(NULL, "cmd_array_alloc()")); } cmd_arr[i]->fd_in = STDIN_FILENO; cmd_arr[i]->fd_out = STDOUT_FILENO; i++; } return (cmd_arr); } int cmd_array_fill_argv(t_token *t, t_cmd **cmd_arr) { size_t i; size_t arg_i; i = 0; while (cmd_arr[i]) { cmd_arr[i]->argv = ft_calloc(cmd_words_count(t) + 1, sizeof (char *)); if (!cmd_arr[i]->argv) return (ft_reti_perror(0, "cmd_array_fill_argv()")); arg_i = 0; while (t && t->id != '|') { if (t->id == T_WORD) { cmd_arr[i]->argv[arg_i++] = t->content; t->content = NULL; } t = t->next; } if (t && t->id == '|') t = t->next; i++; } return (1); } static size_t cmd_words_count(t_token *t) { size_t count; count = 0; while (t && t->id != '|') { if (t->id == T_WORD) count++; t = t->next; } return (count); }