merge hugogogo with master
This commit is contained in:
1
Makefile
1
Makefile
@@ -27,7 +27,6 @@ SRCS = main.c init.c free.c generic.c \
|
|||||||
lexing.c fill_token.c check_operators.c \
|
lexing.c fill_token.c check_operators.c \
|
||||||
parsing.c \
|
parsing.c \
|
||||||
valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \
|
valid_syntax.c valid_pipeline.c valid_command.c valid_io_redirect.c \
|
||||||
fill_cmd.c \
|
|
||||||
env.c exit.c echo.c
|
env.c exit.c echo.c
|
||||||
|
|
||||||
DIR_OBJS = builds
|
DIR_OBJS = builds
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/02 13:53:08 by hulamy ### ########.fr */
|
/* Updated: 2021/11/02 14:03:51 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -34,6 +34,7 @@ int valid_syntax(t_token *token_list);
|
|||||||
int valid_token(t_token **token_list, enum e_token_id token_id);
|
int valid_token(t_token **token_list, enum e_token_id token_id);
|
||||||
int valid_command_separator(const t_token *token_list);
|
int valid_command_separator(const t_token *token_list);
|
||||||
t_cmd **fill_cmd(t_token *token, char **envp);
|
t_cmd **fill_cmd(t_token *token, char **envp);
|
||||||
|
void cmd_expansion(t_cmd **cmd_arr, char **envp);
|
||||||
|
|
||||||
// Builtins
|
// Builtins
|
||||||
int builtin_env(int argc, char *argv[], t_all *c);
|
int builtin_env(int argc, char *argv[], t_all *c);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/02 13:49:26 by hulamy ### ########.fr */
|
/* Updated: 2021/11/02 14:03:31 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -26,4 +26,3 @@ int main(int argc, char *argv[], char *envp[])
|
|||||||
shell_script(&c);
|
shell_script(&c);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,165 +0,0 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
int nbr_pipes(t_token *token)
|
|
||||||
{
|
|
||||||
int nb;
|
|
||||||
|
|
||||||
nb = 0;
|
|
||||||
while (token)
|
|
||||||
{
|
|
||||||
if (token->id == T_PIPE)
|
|
||||||
nb++;
|
|
||||||
token = token->next;
|
|
||||||
}
|
|
||||||
return (nb);
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbr_argv(t_token *token)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (token && token->id != T_PIPE)
|
|
||||||
{
|
|
||||||
token = token->next;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (i);
|
|
||||||
}
|
|
||||||
|
|
||||||
// T_T
|
|
||||||
// T_L
|
|
||||||
// T_G
|
|
||||||
// T_P
|
|
||||||
// T_D
|
|
||||||
// T_D
|
|
||||||
// T_W
|
|
||||||
void handle_argv(t_token **token, t_cmd *cmd)
|
|
||||||
{
|
|
||||||
int argc;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
argc = nbr_argv(*token);
|
|
||||||
cmd->argv = calloc(argc + 1, sizeof(char *));
|
|
||||||
cmd->argv[argc] = NULL;
|
|
||||||
i = 0;
|
|
||||||
while (*token && (*token)->id != T_PIPE)
|
|
||||||
{
|
|
||||||
if ((*token)->id == T_WORD)
|
|
||||||
{
|
|
||||||
cmd->argv[i] = ft_strdup((*token)->content);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
*token = (*token)->next;
|
|
||||||
}
|
|
||||||
if (*token && (*token)->id == T_PIPE)
|
|
||||||
*token = (*token)->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
void handle_fd(t_token *token, t_cmd **cmd)
|
|
||||||
{
|
|
||||||
int *pipes_fd;
|
|
||||||
|
|
||||||
(*cmd)->fd_out = 1;
|
|
||||||
while (token && token->id != T_PIPE)
|
|
||||||
{
|
|
||||||
// T_LESS : '<',
|
|
||||||
if (token->id == T_LESS)
|
|
||||||
{
|
|
||||||
if ((*cmd)->fd_in != 0)
|
|
||||||
close((*cmd)->fd_in);
|
|
||||||
(*cmd)->fd_in = open(token->next->content, O_RDONLY | O_CREAT);
|
|
||||||
token->id = T_TOKEN;
|
|
||||||
token->next->id = T_TOKEN;
|
|
||||||
}
|
|
||||||
// T_GREAT : '>',
|
|
||||||
if (token->id == T_GREAT)
|
|
||||||
{
|
|
||||||
(*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
|
|
||||||
token->id = T_TOKEN;
|
|
||||||
token->next->id = T_TOKEN;
|
|
||||||
}
|
|
||||||
// T_DGREAT : '>>'
|
|
||||||
if (token->id == T_DGREAT)
|
|
||||||
{
|
|
||||||
(*cmd)->fd_out = open(token->next->content, O_WRONLY | O_CREAT | O_APPEND, S_IRWXU);
|
|
||||||
token->id = T_TOKEN;
|
|
||||||
token->next->id = T_TOKEN;
|
|
||||||
}
|
|
||||||
// T_DLESS : '<<' (heredoc)
|
|
||||||
// if (token->id == T_DGREAT)
|
|
||||||
// {
|
|
||||||
// cmd->fd_out = open(token->next->content, O_WRONLY | O_TMPFILE | O_APPEND, S_IRWXU);
|
|
||||||
// token->out = T_TOKEN;
|
|
||||||
// token->next->id = T_TOKEN;
|
|
||||||
// }
|
|
||||||
token = token->next;
|
|
||||||
}
|
|
||||||
if (token && token->id == T_PIPE)
|
|
||||||
{
|
|
||||||
pipes_fd = calloc(2, sizeof(int));
|
|
||||||
pipe(pipes_fd);
|
|
||||||
if ((*cmd)->fd_out == 1)
|
|
||||||
(*cmd)->fd_out = pipes_fd[1];
|
|
||||||
else
|
|
||||||
close(pipes_fd[1]);
|
|
||||||
cmd[1]->fd_in = pipes_fd[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int handle_builtin(t_token *token, t_cmd *cmd)
|
|
||||||
{
|
|
||||||
(void)token;
|
|
||||||
(void)cmd;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void handle_cmd(char **argv, char **envp)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char **path;
|
|
||||||
char *cmd;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (envp[i] && ft_strncmp(envp[i], "PATH=", 5))
|
|
||||||
i++;
|
|
||||||
path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH="
|
|
||||||
i = -1;
|
|
||||||
while (*path && i != 0)
|
|
||||||
{
|
|
||||||
cmd = ft_strjoin(path[0], "/");
|
|
||||||
cmd = ft_strjoin(cmd, argv[0]);
|
|
||||||
i = access(cmd, X_OK);
|
|
||||||
path++;
|
|
||||||
}
|
|
||||||
argv[0] = cmd;
|
|
||||||
}
|
|
||||||
|
|
||||||
t_cmd **fill_cmd(t_token *token, char **envp)
|
|
||||||
{
|
|
||||||
t_cmd **cmd;
|
|
||||||
int pipes;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
pipes = nbr_pipes(token);
|
|
||||||
cmd = ft_calloc(pipes + 2, sizeof(t_cmd*));
|
|
||||||
cmd[pipes + 1] = NULL;
|
|
||||||
i = -1;
|
|
||||||
while (++i <= pipes)
|
|
||||||
{
|
|
||||||
cmd[i] = ft_calloc(1, sizeof(t_cmd));
|
|
||||||
ft_bzero(cmd[i], sizeof(t_cmd));
|
|
||||||
}
|
|
||||||
i = 0;
|
|
||||||
while (i <= pipes)
|
|
||||||
{
|
|
||||||
handle_fd(token, cmd + i);
|
|
||||||
handle_argv(&token, cmd[i]);
|
|
||||||
if (!handle_builtin(token, cmd[i]))
|
|
||||||
handle_cmd(cmd[i]->argv, envp);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -12,40 +12,255 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
// A quoi bon un arbre binaire ? Je ne vois plus l'utilité.
|
size_t count_pipes(t_token *token);
|
||||||
|
t_cmd **create_cmd(t_token *token_list, size_t cmd_nbr);
|
||||||
/* typedef struct s_binary_tree
|
void handle_argv(t_token *token, t_cmd **cmd, size_t cmd_nbr);
|
||||||
{
|
void handle_path(t_cmd **cmd_arr, char **envp);
|
||||||
char *content;
|
void handle_fd(t_token *token, t_cmd **cmd_arr);
|
||||||
struct s_binary_tree *sub;
|
void fd_heredoc(t_token *token, t_cmd *cmd);
|
||||||
struct s_binary_tree *sibling;
|
void fd_redirection(t_token *token, t_cmd *cmd);
|
||||||
enum e_token_id id;
|
void find_path(char **argv, char **envp);
|
||||||
} t_binary_tree; */
|
int handle_builtin(t_cmd *cmd);
|
||||||
|
int fill_builtin(t_cmd *cmd, int (*builtin)(int, char **, t_all *));
|
||||||
|
int next_cmd(t_token *token);
|
||||||
|
int is_redirection(enum e_token_id id);
|
||||||
|
|
||||||
t_cmd **parsing(t_token *token_list, char **envp)
|
t_cmd **parsing(t_token *token_list, char **envp)
|
||||||
{
|
{
|
||||||
t_cmd **cmd_arr;
|
t_cmd **cmd_arr;
|
||||||
|
size_t cmd_nbr;
|
||||||
/* t_binary_tree *syntax_tree;
|
|
||||||
|
|
||||||
syntax_tree = ft_calloc(1, sizeof *syntax_tree);
|
|
||||||
if (!syntax_tree)
|
|
||||||
return (0); //WIP ERROR */
|
|
||||||
|
|
||||||
if (!valid_syntax(token_list))
|
if (!valid_syntax(token_list))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
// Pipes count (determine cmd_nbr)
|
cmd_nbr = count_pipes(token_list);
|
||||||
|
cmd_arr = create_cmd(token_list, cmd_nbr);
|
||||||
// Struct CMD alloc/fill
|
// cmd_expansion(cmd_arr, envp);
|
||||||
cmd_arr = fill_cmd(token_list, envp);
|
handle_argv(token_list, cmd_arr, cmd_nbr);
|
||||||
|
handle_path(cmd_arr, envp);
|
||||||
// 2.9.1 - 2) Expansion
|
handle_fd(token_list, cmd_arr);
|
||||||
|
|
||||||
// 2.9.1 - 3) Redirection
|
|
||||||
|
|
||||||
return (cmd_arr);
|
return (cmd_arr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t count_pipes(t_token *token)
|
||||||
|
{
|
||||||
|
size_t nb;
|
||||||
|
|
||||||
|
nb = 0;
|
||||||
|
while (token)
|
||||||
|
{
|
||||||
|
if (token->id == T_PIPE)
|
||||||
|
nb++;
|
||||||
|
token = token->next;
|
||||||
|
}
|
||||||
|
return (nb + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd **create_cmd(t_token *token_list, size_t cmd_nbr)
|
||||||
|
{
|
||||||
|
t_cmd **cmd_arr;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
|
(void)token_list;
|
||||||
|
cmd_arr = ft_calloc(cmd_nbr + 1, sizeof(t_cmd *));
|
||||||
|
cmd_arr[cmd_nbr] = NULL;
|
||||||
|
i = 0;
|
||||||
|
while (i < cmd_nbr)
|
||||||
|
{
|
||||||
|
cmd_arr[i] = ft_calloc(1, sizeof(t_cmd));
|
||||||
|
ft_bzero(cmd_arr[i], sizeof(t_cmd));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (cmd_arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_redirection(enum e_token_id id)
|
||||||
|
{
|
||||||
|
if (id == T_LESS) // <
|
||||||
|
return (1);
|
||||||
|
else if (id == T_GREAT) // >
|
||||||
|
return (1);
|
||||||
|
else if (id == T_DLESS) // <<
|
||||||
|
return (1);
|
||||||
|
else if (id == T_DGREAT) // >>
|
||||||
|
return (1);
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// count nbr word in cmd, minus redirection and heredoc
|
||||||
|
int next_cmd(t_token *token)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (token && token->id != T_PIPE)
|
||||||
|
{
|
||||||
|
if (is_redirection(token->id))
|
||||||
|
i--;
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
token = token->next;
|
||||||
|
}
|
||||||
|
if (token && token->id == T_PIPE)
|
||||||
|
token = token->next;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_argv(t_token *token, t_cmd **cmd, size_t cmd_nbr)
|
||||||
|
{
|
||||||
|
int argc;
|
||||||
|
int j;
|
||||||
|
int i;
|
||||||
|
int redirection;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (cmd_nbr)
|
||||||
|
{
|
||||||
|
argc = next_cmd(token);
|
||||||
|
cmd[i]->argv = calloc(argc + 1, sizeof(char *));
|
||||||
|
cmd[i]->argv[argc] = NULL;
|
||||||
|
j = 0;
|
||||||
|
redirection = is_redirection(token->id);
|
||||||
|
while (token && token->id != T_PIPE)
|
||||||
|
{
|
||||||
|
if (!redirection && token->id == T_WORD)
|
||||||
|
{
|
||||||
|
cmd[i]->argv[j] = ft_strdup(token->content);
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
redirection = is_redirection(token->id);
|
||||||
|
token = token->next;
|
||||||
|
}
|
||||||
|
if (token && token->id == T_PIPE)
|
||||||
|
token = token->next;
|
||||||
|
cmd_nbr--;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int fill_builtin(t_cmd *cmd, int (*builtin)(int, char **, t_all *))
|
||||||
|
{
|
||||||
|
cmd->builtin_command = &builtin;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int handle_builtin(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
if (!ft_strncmp(cmd->argv[0], "echo", 4))
|
||||||
|
return (fill_builtin(cmd, &builtin_echo));
|
||||||
|
// else if (!ft_strncmp(cmd->argv[0], "cd", 2))
|
||||||
|
// return (fill_builtin(cmd, &builtin_cd));
|
||||||
|
// else if (!ft_strncmp(cmd->argv[0], "pwd", 3))
|
||||||
|
// return (fill_builtin(cmd, &builtin_pwd));
|
||||||
|
// else if (!ft_strncmp(cmd->argv[0], "export", 6))
|
||||||
|
// return (fill_builtin(cmd, &builtin_export));
|
||||||
|
// else if (!ft_strncmp(cmd->argv[0], "unset", 5))
|
||||||
|
// return (fill_builtin(cmd, &builtin_unset));
|
||||||
|
else if (!ft_strncmp(cmd->argv[0], "env", 3))
|
||||||
|
return (fill_builtin(cmd, &builtin_env));
|
||||||
|
else if (!ft_strncmp(cmd->argv[0], "exit", 4))
|
||||||
|
return (fill_builtin(cmd, &builtin_exit));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void find_path(char **argv, char **envp)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char **path;
|
||||||
|
char *cmd;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (envp[i] && ft_strncmp(envp[i], "PATH=", 5))
|
||||||
|
i++;
|
||||||
|
path = ft_split(envp[i] + 5, ':'); // 5 = lentgh of "PATH="
|
||||||
|
i = -1;
|
||||||
|
while (*path && i != 0)
|
||||||
|
{
|
||||||
|
cmd = ft_strjoin(path[0], "/");
|
||||||
|
cmd = ft_strjoin(cmd, argv[0]);
|
||||||
|
i = access(cmd, X_OK);
|
||||||
|
path++;
|
||||||
|
}
|
||||||
|
if (!(*path))
|
||||||
|
exit(0); // gerer erreur
|
||||||
|
argv[0] = cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_path(t_cmd **cmd_arr, char **envp)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (cmd_arr[i])
|
||||||
|
{
|
||||||
|
if (!handle_builtin(cmd_arr[i]))
|
||||||
|
find_path(cmd_arr[i]->argv, envp);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fd_redirection(t_token *token, t_cmd *cmd)
|
||||||
|
{
|
||||||
|
int flag;
|
||||||
|
|
||||||
|
if (token->id == T_LESS) // '<'
|
||||||
|
{
|
||||||
|
flag = O_RDONLY | O_CREAT;
|
||||||
|
if (cmd->fd_in != 0)
|
||||||
|
close(cmd->fd_in);
|
||||||
|
cmd->fd_in = open(token->next->content, flag);
|
||||||
|
}
|
||||||
|
else if (token->id == T_GREAT) // '>'
|
||||||
|
{
|
||||||
|
flag = O_WRONLY | O_CREAT | O_TRUNC;
|
||||||
|
cmd->fd_out = open(token->next->content, flag, S_IRWXU);
|
||||||
|
}
|
||||||
|
else if (token->id == T_DGREAT) // '>>'
|
||||||
|
{
|
||||||
|
flag = O_WRONLY | O_CREAT | O_APPEND;
|
||||||
|
cmd->fd_out = open(token->next->content, flag, S_IRWXU);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fd_heredoc(t_token *token, t_cmd *cmd)
|
||||||
|
{
|
||||||
|
(void)token;
|
||||||
|
(void)cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_fd(t_token *token, t_cmd **cmd_arr)
|
||||||
|
{
|
||||||
|
int *pipes_fd;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (cmd_arr[i])
|
||||||
|
{
|
||||||
|
cmd_arr[i]->fd_out = 1;
|
||||||
|
while (token && token->id != T_PIPE)
|
||||||
|
{
|
||||||
|
if (token->id == T_DGREAT) // '<<'
|
||||||
|
fd_heredoc(token, cmd_arr[i]);
|
||||||
|
else
|
||||||
|
fd_redirection(token, cmd_arr[i]);
|
||||||
|
token = token->next;
|
||||||
|
}
|
||||||
|
if (token && token->id == T_PIPE)
|
||||||
|
{
|
||||||
|
token = token->next;
|
||||||
|
pipes_fd = calloc(2, sizeof(int));
|
||||||
|
pipe(pipes_fd);
|
||||||
|
if (cmd_arr[i]->fd_out == 1)
|
||||||
|
cmd_arr[i]->fd_out = pipes_fd[1];
|
||||||
|
else
|
||||||
|
close(pipes_fd[1]);
|
||||||
|
cmd_arr[i + 1]->fd_in = pipes_fd[0];
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* -------------------------------------------------------
|
/* -------------------------------------------------------
|
||||||
The grammar symbols
|
The grammar symbols
|
||||||
------------------------------------------------------- */
|
------------------------------------------------------- */
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/02 13:48:36 by hulamy ### ########.fr */
|
/* Updated: 2021/11/02 14:01:34 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -16,41 +16,8 @@
|
|||||||
void sigint_handler(int signum); //tmp
|
void sigint_handler(int signum); //tmp
|
||||||
void sigquit_aka_eof_handler(int signum); //tmp
|
void sigquit_aka_eof_handler(int signum); //tmp
|
||||||
|
|
||||||
void close_fd(t_cmd *cmd)
|
void close_fd(t_cmd *cmd);
|
||||||
{
|
void execute_cmd(char **envp, t_cmd **cmd_arr);
|
||||||
if (cmd->fd_in != 0)
|
|
||||||
close(cmd->fd_in);
|
|
||||||
if (cmd->fd_out != 1)
|
|
||||||
close(cmd->fd_out);
|
|
||||||
}
|
|
||||||
|
|
||||||
void execute_cmd(char **envp, t_cmd **cmd_arr)
|
|
||||||
{
|
|
||||||
pid_t pid;
|
|
||||||
pid_t wpid;
|
|
||||||
int status;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while(cmd_arr[i])
|
|
||||||
{
|
|
||||||
pid = fork();
|
|
||||||
if (pid == 0)
|
|
||||||
{
|
|
||||||
if (cmd_arr[i]->fd_in != 0)
|
|
||||||
dup2(cmd_arr[i]->fd_in, STDIN_FILENO);
|
|
||||||
if (cmd_arr[i]->fd_out != 1)
|
|
||||||
dup2(cmd_arr[i]->fd_out, STDOUT_FILENO);
|
|
||||||
close_fd(cmd_arr[i]);
|
|
||||||
execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
close_fd(cmd_arr[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
// waitpid pour la derniere commande (pour '$?')
|
|
||||||
while ((wpid = wait(&status)) > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void shell_loop(t_all *c)
|
void shell_loop(t_all *c)
|
||||||
{
|
{
|
||||||
@@ -99,46 +66,73 @@ void shell_loop(t_all *c)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// EXEC_PIPES_AND_CO()
|
// EXEC_PIPES_AND_CO
|
||||||
|
|
||||||
// temp placeholder
|
|
||||||
/* if (ft_strncmp(c->token_list->content, "env", 4) == 0)
|
|
||||||
builtin_env(0, NULL, c);
|
|
||||||
else if (ft_strncmp(c->token_list->content, "exit", 5) == 0)
|
|
||||||
builtin_exit(0, NULL, c);
|
|
||||||
else if (ft_strncmp(c->token_list->content, "echo", 5) == 0)
|
|
||||||
builtin_echo(ft_lstsize((t_list *)c->token_list) + 1, tokens_list_to_argv(c->token_list), c);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
c->cmd_arr = parsing(c->token_list, c->envp);
|
c->cmd_arr = parsing(c->token_list, c->envp);
|
||||||
if (c->cmd_arr == NULL)
|
|
||||||
ft_putstr_fd("Syntax KO:\n-----------\n", 1);
|
|
||||||
// else
|
|
||||||
// ft_putstr_fd("Syntax OK:\n-----------\n", 1);
|
|
||||||
// ft_putstr_fd("TOKENS LIST :\n-----------\n", 1);
|
|
||||||
// ft_lstprint((t_list *)c->token_list, 1);
|
|
||||||
execute_cmd(c->envp, c->cmd_arr);
|
execute_cmd(c->envp, c->cmd_arr);
|
||||||
ft_lstclear((t_list **)&c->token_list, free);
|
ft_lstclear((t_list **)&c->token_list, free);
|
||||||
} */
|
|
||||||
}
|
}
|
||||||
else if (!line_input)
|
else if (!line_input)
|
||||||
write(1, "\n", 1);
|
write(1, "\n", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static char **tokens_list_to_argv(t_token *t) // temp test
|
void wip_test()
|
||||||
{
|
{
|
||||||
size_t i;
|
char term_desc[2048];
|
||||||
char **argv;
|
char *term_type;
|
||||||
|
int term_width;
|
||||||
|
int term_height;
|
||||||
|
int ret;
|
||||||
|
|
||||||
i = ft_lstsize((t_list *)t);
|
term_type = getenv("TERM");
|
||||||
argv = ft_calloc(i + 1, sizeof(char*));
|
if (term_type == 0)
|
||||||
i = 0;
|
ft_putstr_fd("Specify a terminal type with `setenv TERM <yourtype>'.\n", 2);
|
||||||
while (t)
|
ret = tgetent(term_desc, term_type);
|
||||||
{
|
if (ret < 0)
|
||||||
argv[i] = t->content;
|
ft_putstr_fd("Could not access the termcap data base.\n", 2);
|
||||||
i++;
|
if (ret == 0)
|
||||||
t = t->next;
|
ft_putstr_fd("Terminal type `%s' is not defined.\n", 2);
|
||||||
|
term_height = tgetnum ("li");
|
||||||
|
term_width = tgetnum ("co");
|
||||||
|
/* Extract information that termcap functions use. */
|
||||||
|
/* temp = tgetstr ("pc", BUFFADDR);
|
||||||
|
PC = temp ? *temp : 0;
|
||||||
|
BC = tgetstr ("le", BUFFADDR);
|
||||||
|
UP = tgetstr ("up", BUFFADDR); */
|
||||||
|
}
|
||||||
|
|
||||||
|
void close_fd(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
if (cmd->fd_in != 0)
|
||||||
|
close(cmd->fd_in);
|
||||||
|
if (cmd->fd_out != 1)
|
||||||
|
close(cmd->fd_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
void execute_cmd(char **envp, t_cmd **cmd_arr)
|
||||||
|
{
|
||||||
|
pid_t pid;
|
||||||
|
pid_t wpid;
|
||||||
|
int status;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(cmd_arr[i])
|
||||||
|
{
|
||||||
|
pid = fork();
|
||||||
|
if (pid == 0)
|
||||||
|
{
|
||||||
|
if (cmd_arr[i]->fd_in != 0)
|
||||||
|
dup2(cmd_arr[i]->fd_in, STDIN_FILENO);
|
||||||
|
if (cmd_arr[i]->fd_out != 1)
|
||||||
|
dup2(cmd_arr[i]->fd_out, STDOUT_FILENO);
|
||||||
|
close_fd(cmd_arr[i]);
|
||||||
|
execve(cmd_arr[i]->argv[0], cmd_arr[i]->argv, envp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
close_fd(cmd_arr[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
// waitpid pour la derniere commande (pour '$?')
|
||||||
|
while ((wpid = wait(&status)) > 0);
|
||||||
}
|
}
|
||||||
return (argv);
|
|
||||||
} */
|
|
||||||
|
|||||||
Reference in New Issue
Block a user