core: rebuild of parsing and the execution
This commit is contained in:
128
parse/cmds_parse.c
Normal file
128
parse/cmds_parse.c
Normal file
@ -0,0 +1,128 @@
|
||||
#include "./parse_private.h"
|
||||
|
||||
static int ft_redirection_parse(t_data *data, char *cmd_str, t_cmd *cmd)
|
||||
{
|
||||
int fd_in;
|
||||
int fd_out;
|
||||
|
||||
fd_in = ft_infile(data, cmd_str);
|
||||
if (fd_in == -2)
|
||||
return (1);
|
||||
fd_out = ft_outfile(data, cmd_str);
|
||||
if (fd_out == -2)
|
||||
{
|
||||
close(fd_in);
|
||||
return (1);
|
||||
}
|
||||
cmd->fd_in[0] = fd_in;
|
||||
cmd->fd_in[1] = -1;
|
||||
cmd->fd_out[0] = fd_out;
|
||||
cmd->fd_out[1] = -1;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_args_parse(t_data *data, char *cmd_str, t_cmd *cmd)
|
||||
{
|
||||
char **tab;
|
||||
char *str;
|
||||
size_t i;
|
||||
|
||||
tab = ft_split_quoted(cmd_str, ' ');
|
||||
if (tab == NULL)
|
||||
return (1);
|
||||
i = 0;
|
||||
while (tab[i] == NULL)
|
||||
{
|
||||
str = ft_env_filler(data, tab[i]);
|
||||
if (str == NULL)
|
||||
{
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (1);
|
||||
}
|
||||
free(tab[i]);
|
||||
tab[i] = str;
|
||||
i++;
|
||||
}
|
||||
cmd->args = tab;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_executable_parse(t_data *data, t_cmd *cmd)
|
||||
{
|
||||
bool own;
|
||||
char *path;
|
||||
|
||||
path = cmd->args[0];
|
||||
own = 0;
|
||||
if (ft_strcmp(cmd->args[0], "env") == 0)
|
||||
own = 1;
|
||||
else if (ft_strcmp(cmd->args[0], "export") == 0)
|
||||
own = 1;
|
||||
else if (ft_strcmp(cmd->args[0], "echo") == 0)
|
||||
own = 1;
|
||||
else if (ft_strcmp(cmd->args[0], "unset") == 0)
|
||||
own = 1;
|
||||
else if (ft_strcmp(cmd->args[0], "exit") == 0)
|
||||
own = 1;
|
||||
else if (ft_strcmp(cmd->args[0], "pwd") == 0)
|
||||
own = 1;
|
||||
else if (ft_strcmp(cmd->args[0], "cmd") == 0)
|
||||
own = 1;
|
||||
if (own == 0)
|
||||
path = ft_get_executable(data->env, cmd->args[0]);
|
||||
cmd->own_cmd = own;
|
||||
cmd->executable = path;
|
||||
return (own);
|
||||
}
|
||||
|
||||
int ft_cmd_parser(t_data *data, char *cmd_str)
|
||||
{
|
||||
t_cmd *cmd;
|
||||
t_list *element;
|
||||
|
||||
cmd = ft_calloc(sizeof(t_cmd), 1);
|
||||
if (cmd == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
if (ft_redirection_parse(data, cmd_str, cmd))
|
||||
return (1);
|
||||
if (ft_args_parse(data, cmd_str, cmd))
|
||||
{
|
||||
ft_cmddel(cmd);
|
||||
return (1);
|
||||
}
|
||||
ft_executable_parse(data, cmd);
|
||||
element = ft_lstnew(cmd);
|
||||
if (element == NULL)
|
||||
{
|
||||
ft_cmddel(cmd);
|
||||
return (1);
|
||||
}
|
||||
ft_lstadd_back(data->cmds, element);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_cmds_parser(t_data *data, const char *line)
|
||||
{
|
||||
char **tab;
|
||||
size_t i;
|
||||
|
||||
tab = ft_split_quoted(line, '|');
|
||||
if (tab == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
i = 0;
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
ft_cmd_parser(data, tab[i]);
|
||||
i++;
|
||||
}
|
||||
ft_add_fd(((t_cmd *) (*data->cmds)->content)->fd_in, 0);
|
||||
ft_add_fd(((t_cmd *) ((ft_lstlast(*data->cmds))->content))->fd_out, 1);
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
return (0);
|
||||
}
|
7
parse/parse.h
Normal file
7
parse/parse.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef PARSE_H
|
||||
# define PARSE_H
|
||||
# include "../data/data.h"
|
||||
|
||||
int ft_cmds_parser(t_data *data, const char *line);
|
||||
|
||||
#endif
|
9
parse/parse_private.h
Normal file
9
parse/parse_private.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef PARSE_PRIVATE_H
|
||||
# define PARSE_PRIVATE_H
|
||||
# include "../redirection/redirection.h"
|
||||
# include "../libftx/libftx.h"
|
||||
# include "../utils/utils.h"
|
||||
# include "../env/env.h"
|
||||
# include "../data/data.h"
|
||||
# include "../cmd/cmd.h"
|
||||
#endif
|
Reference in New Issue
Block a user