"cmd_arr/cmd_array" renamed to "pipeline"
This commit is contained in:
2
Makefile
2
Makefile
@@ -29,7 +29,7 @@ SRCS = main.c init.c free.c generic.c \
|
||||
signals.c \
|
||||
shell_loop.c shell_script.c \
|
||||
lexing.c fill_token.c check_operators.c \
|
||||
parsing.c cmd_array.c \
|
||||
parsing.c create_pipeline.c \
|
||||
valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \
|
||||
words_expansions.c expand_token.c rejoin_after_expand.c new_token_for_each_field.c \
|
||||
ft_split_quotes.c ft_strdup_quotes.c \
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/27 12:28:20 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:25:28 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -40,11 +40,11 @@ int valid_syntax(t_token *token_list);
|
||||
int valid_token(t_token **token_list, enum e_token_id token_id);
|
||||
int valid_command_separator(const t_token *token_list);
|
||||
size_t count_pipes(t_token *token_list);
|
||||
t_cmd **cmd_array_alloc(size_t cmd_nbr);
|
||||
int cmd_array_fill_argv(t_token *token_list, t_cmd **cmd_arr);
|
||||
t_cmd **pipeline_alloc(size_t cmd_nbr);
|
||||
int pipeline_fill_argv(t_token *token_list, t_cmd **pipeline);
|
||||
int words_expansions(t_token *token_list);
|
||||
int token_expansions(t_token **t);
|
||||
int redirections(t_token *token_list, t_cmd **cmd_arr);
|
||||
int redirections(t_token *token_list, t_cmd **pipeline);
|
||||
|
||||
// Exec
|
||||
int exec_cmd_line(t_all *c);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/27 11:28:13 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:25:34 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,7 +37,7 @@ typedef struct s_cmd
|
||||
|
||||
typedef struct s_all
|
||||
{
|
||||
t_cmd **cmd_arr;
|
||||
t_cmd **pipeline;
|
||||
char **path;
|
||||
char *prompt_base;
|
||||
char *prompt;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/18 13:29:25 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:26:08 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -18,7 +18,7 @@ int exec_cmd_line(t_all *c)
|
||||
{
|
||||
if (!pipeline(c))
|
||||
{
|
||||
free_pipeline(&c->cmd_arr);
|
||||
free_pipeline(&c->pipeline);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/27 10:48:13 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:26:27 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -18,15 +18,15 @@ static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c);
|
||||
|
||||
int pipeline(t_all *c)
|
||||
{
|
||||
if (!open_pipes(c->cmd_arr))
|
||||
if (!open_pipes(c->pipeline))
|
||||
return (0);
|
||||
if (!pipeline_find_access(c->cmd_arr, c->path))
|
||||
if (!pipeline_find_access(c->pipeline, c->path))
|
||||
return (0);
|
||||
if (ft_2d_arrlen(c->cmd_arr) == 1 && c->cmd_arr[0]->builtin_func)
|
||||
simple_command_builtin(c->cmd_arr[0], c);
|
||||
if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_func)
|
||||
simple_command_builtin(c->pipeline[0], c);
|
||||
else
|
||||
wait_subshell(pipeline_exec(c->cmd_arr, c));
|
||||
free_pipeline(&c->cmd_arr);
|
||||
wait_subshell(pipeline_exec(c->pipeline, c));
|
||||
free_pipeline(&c->pipeline);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c)
|
||||
}
|
||||
i++;
|
||||
}
|
||||
close_pipeline_fd(c->cmd_arr);
|
||||
close_pipeline_fd(c->pipeline);
|
||||
i -= 1;
|
||||
if (pipeline[i]->error)
|
||||
set_last_exit_status(pipeline[i]->error);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/27 11:11:12 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:26:33 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -27,7 +27,7 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
|
||||
if (cmd->fd_out != STDOUT_FILENO)
|
||||
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
|
||||
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
|
||||
close_pipeline_fd(c->cmd_arr);
|
||||
close_pipeline_fd(c->pipeline);
|
||||
if (cmd->builtin_func)
|
||||
free_exit(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
|
||||
else if (execve(cmd->path, cmd->argv, environ) == -1)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/27 23:50:15 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:25:46 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -42,7 +42,7 @@ int free_exit(t_all *c, int exit_status)
|
||||
//environ = g_ori_environ; // WIP free test
|
||||
environ = NULL; // WIP free test
|
||||
ft_free_2d_arr(c->path);
|
||||
free_pipeline(&c->cmd_arr);
|
||||
free_pipeline(&c->pipeline);
|
||||
//if (c->termios_changed)
|
||||
// tcsetattr(STDIN_FILENO, TCSANOW, &c->ori_termios);
|
||||
rl_clear_history();
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cmd_array.c :+: :+: :+: */
|
||||
/* create_pipeline.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/02 22:46:23 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/14 12:24:39 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:29:27 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -28,47 +28,47 @@ size_t count_pipes(t_token *t)
|
||||
return (count);
|
||||
}
|
||||
|
||||
t_cmd **cmd_array_alloc(size_t cmd_nbr)
|
||||
t_cmd **pipeline_alloc(size_t cmd_nbr)
|
||||
{
|
||||
t_cmd **cmd_arr;
|
||||
t_cmd **pipeline;
|
||||
size_t i;
|
||||
|
||||
cmd_arr = ft_calloc(cmd_nbr + 1, sizeof (void *));
|
||||
if (!cmd_arr)
|
||||
return (ft_retp_perror(NULL, "cmd_array_alloc()"));
|
||||
pipeline = ft_calloc(cmd_nbr + 1, sizeof (void *));
|
||||
if (!pipeline)
|
||||
return (ft_retp_perror(NULL, "pipeline_alloc()"));
|
||||
i = 0;
|
||||
while (i < cmd_nbr)
|
||||
{
|
||||
cmd_arr[i] = ft_calloc(1, sizeof (*cmd_arr[i]));
|
||||
if (!cmd_arr[i])
|
||||
pipeline[i] = ft_calloc(1, sizeof (*pipeline[i]));
|
||||
if (!pipeline[i])
|
||||
{
|
||||
ft_free_2d_arr(cmd_arr);
|
||||
return (ft_retp_perror(NULL, "cmd_array_alloc()"));
|
||||
ft_free_2d_arr(pipeline);
|
||||
return (ft_retp_perror(NULL, "pipeline_alloc()"));
|
||||
}
|
||||
cmd_arr[i]->fd_in = STDIN_FILENO;
|
||||
cmd_arr[i]->fd_out = STDOUT_FILENO;
|
||||
pipeline[i]->fd_in = STDIN_FILENO;
|
||||
pipeline[i]->fd_out = STDOUT_FILENO;
|
||||
i++;
|
||||
}
|
||||
return (cmd_arr);
|
||||
return (pipeline);
|
||||
}
|
||||
|
||||
int cmd_array_fill_argv(t_token *t, t_cmd **cmd_arr)
|
||||
int pipeline_fill_argv(t_token *t, t_cmd **pipeline)
|
||||
{
|
||||
size_t i;
|
||||
size_t arg_i;
|
||||
|
||||
i = 0;
|
||||
while (cmd_arr[i])
|
||||
while (pipeline[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()"));
|
||||
pipeline[i]->argv = ft_calloc(cmd_words_count(t) + 1, sizeof (char *));
|
||||
if (!pipeline[i]->argv)
|
||||
return (ft_reti_perror(0, "pipeline_fill_argv()"));
|
||||
arg_i = 0;
|
||||
while (t && t->id != '|')
|
||||
{
|
||||
if (t->id == T_WORD)
|
||||
{
|
||||
cmd_arr[i]->argv[arg_i++] = t->content;
|
||||
pipeline[i]->argv[arg_i++] = t->content;
|
||||
t->content = NULL;
|
||||
}
|
||||
t = t->next;
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/27 11:09:48 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:28:07 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -26,25 +26,25 @@ void save_redirections_words(t_token *t)
|
||||
}
|
||||
}
|
||||
|
||||
void print_cmd_array(t_cmd **cmd_arr)
|
||||
void print_pipeline(t_cmd **pipeline)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (cmd_arr[i])
|
||||
while (pipeline[i])
|
||||
{
|
||||
printf("CMD %i, fd_in=%i, fd_out=%i\n", i, cmd_arr[i]->fd_in, cmd_arr[i]->fd_out);
|
||||
printf("CMD %i, fd_in=%i, fd_out=%i\n", i, pipeline[i]->fd_in, pipeline[i]->fd_out);
|
||||
ft_putstr_fd(" |", 1);
|
||||
print_matrix(cmd_arr[i]->argv, "|\n |");
|
||||
print_matrix(pipeline[i]->argv, "|\n |");
|
||||
i++;
|
||||
if (cmd_arr[i])
|
||||
if (pipeline[i])
|
||||
ft_putstr_fd("----------------\n", 1);
|
||||
}
|
||||
}
|
||||
|
||||
t_cmd **parsing(t_token *token_list)
|
||||
{
|
||||
t_cmd **cmd_arr;
|
||||
t_cmd **pipeline;
|
||||
|
||||
if (!valid_syntax(token_list))
|
||||
return (NULL);
|
||||
@@ -67,20 +67,20 @@ t_cmd **parsing(t_token *token_list)
|
||||
//ft_lstprint((t_list *)token_list, 1);
|
||||
|
||||
// Struct CMD alloc
|
||||
cmd_arr = cmd_array_alloc(1 + count_pipes(token_list));
|
||||
if (!cmd_arr)
|
||||
pipeline = pipeline_alloc(1 + count_pipes(token_list));
|
||||
if (!pipeline)
|
||||
return (NULL);
|
||||
|
||||
// 2.9.1 - 3) Redirection
|
||||
if (!redirections(token_list, cmd_arr))
|
||||
return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline));
|
||||
if (!redirections(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
|
||||
// Struct CMD fill
|
||||
if (!cmd_array_fill_argv(token_list, cmd_arr))
|
||||
return (ft_retp_free(NULL, &cmd_arr, (t_free_f)free_pipeline));
|
||||
print_cmd_array(cmd_arr);
|
||||
if (!pipeline_fill_argv(token_list, pipeline))
|
||||
return (ft_retp_free(NULL, &pipeline, (t_free_f)free_pipeline));
|
||||
print_pipeline(pipeline);
|
||||
|
||||
return (cmd_arr);
|
||||
return (pipeline);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/11/11 18:46:43 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/26 20:00:42 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:28:25 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -17,23 +17,23 @@ int here_doc(char *delimiter);
|
||||
static int redirect_cmd_input(t_token *t, t_cmd *cmd);
|
||||
static int redirect_cmd_output(t_token *t, t_cmd *cmd);
|
||||
|
||||
int redirections(t_token *t, t_cmd **cmd_arr)
|
||||
int redirections(t_token *t, t_cmd **pipeline)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (t)
|
||||
{
|
||||
if (!cmd_arr[i]->error)
|
||||
if (!pipeline[i]->error)
|
||||
{
|
||||
if (t->id == '<' || t->id == T_DLESS)
|
||||
{
|
||||
if (!redirect_cmd_input(t, cmd_arr[i]))
|
||||
if (!redirect_cmd_input(t, pipeline[i]))
|
||||
return (0);
|
||||
}
|
||||
else if (t->id == '>' || t->id == T_DGREAT)
|
||||
{
|
||||
if (!redirect_cmd_output(t, cmd_arr[i]))
|
||||
if (!redirect_cmd_output(t, pipeline[i]))
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||
/* Updated: 2021/11/27 10:47:05 by lperrey ### ########.fr */
|
||||
/* Updated: 2021/11/29 12:25:59 by lperrey ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -36,9 +36,9 @@ void shell_loop(t_all *c)
|
||||
if (!c->token_list)
|
||||
continue ;
|
||||
// Parsing
|
||||
c->cmd_arr = parsing(c->token_list);
|
||||
c->pipeline = parsing(c->token_list);
|
||||
ft_lstclear((t_list **)&c->token_list, free);
|
||||
if (!c->cmd_arr)
|
||||
if (!c->pipeline)
|
||||
continue ;
|
||||
// Exec Pipeline
|
||||
exec_cmd_line(c);
|
||||
|
||||
Reference in New Issue
Block a user