Moved find command in path to executor, fixed env save code
This commit is contained in:
@@ -28,16 +28,12 @@ static char **lst_to_argv(t_list *argv_list);
|
||||
static void set_argc(t_command *command);
|
||||
static void set_infile(t_command *command);
|
||||
static void set_outfile(t_command *command);
|
||||
static void set_path(t_command *command, t_minishell *minishell);
|
||||
static u_int8_t path_is_solved(char *cmd_name, t_minishell *msh);
|
||||
static char *solve_path(char *command_name, t_minishell *minishell);
|
||||
|
||||
t_list *parse(
|
||||
char *line,
|
||||
t_minishell *minishell
|
||||
) {
|
||||
t_list *commands;
|
||||
t_list *tokens;
|
||||
t_command *command;
|
||||
char *command_str;
|
||||
size_t i;
|
||||
@@ -48,7 +44,8 @@ t_list *parse(
|
||||
i = 0;
|
||||
while (line[i] != '\0')
|
||||
{
|
||||
tokens = tokenize();
|
||||
/* TODO: re-enable when parser consumes lexer tokens */
|
||||
/* tokens = tokenize(); */
|
||||
command_str = extract_next_command(line, &i);
|
||||
if (command_str != NULL)
|
||||
{
|
||||
@@ -157,7 +154,6 @@ static t_command *cmdnew(
|
||||
set_argc(command);
|
||||
set_infile(command);
|
||||
set_outfile(command);
|
||||
set_path(command, minishell);
|
||||
return (command);
|
||||
}
|
||||
|
||||
@@ -237,64 +233,14 @@ static void set_infile(
|
||||
t_command *command
|
||||
) {
|
||||
// test_infile
|
||||
command->infile = -1;
|
||||
/* command->infile = -1; */
|
||||
(void)command;
|
||||
}
|
||||
|
||||
static void set_outfile(
|
||||
t_command *command
|
||||
) {
|
||||
// test_outfile
|
||||
command->outfile = STDOUT_FILENO;
|
||||
}
|
||||
|
||||
static void set_path(
|
||||
t_command *command,
|
||||
t_minishell *minishell
|
||||
) {
|
||||
char *command_path;
|
||||
char *command_name;
|
||||
|
||||
command_name = command->argv[0];
|
||||
if (!path_is_solved(command_name, minishell))
|
||||
command_path = solve_path(command_name, minishell);
|
||||
else
|
||||
command_path = ft_strdup(command_name);
|
||||
command->path = command_path;
|
||||
}
|
||||
|
||||
static char *solve_path(
|
||||
char *command_name,
|
||||
t_minishell *minishell
|
||||
){
|
||||
char *command_path;
|
||||
char **path_env;
|
||||
size_t i;
|
||||
|
||||
path_env = ft_split(get_env("PATH", minishell), ':');
|
||||
if (!path_env)
|
||||
return (NULL);
|
||||
command_path = NULL;
|
||||
i = -1;
|
||||
while (!command_path && path_env[++i])
|
||||
{
|
||||
command_path = ft_strnjoin(3, path_env[i], "/", command_name);
|
||||
if (command_path != NULL && access(command_path, F_OK) != EXIT_SUCCESS)
|
||||
{
|
||||
free(command_path);
|
||||
command_path = NULL;
|
||||
}
|
||||
}
|
||||
ft_free_split(path_env);
|
||||
return (command_path);
|
||||
}
|
||||
|
||||
static u_int8_t path_is_solved(
|
||||
char *command_name,
|
||||
t_minishell *minishell
|
||||
){
|
||||
return (ft_strncmp(command_name, "/", 1) == 0
|
||||
|| (command_name[1] && ft_strncmp(command_name, "./", 2) == 0)
|
||||
|| (command_name[2] && ft_strncmp(command_name, "../", 3) == 0)
|
||||
|| is_builtin(command_name, minishell)
|
||||
);
|
||||
/* command->outfile = STDOUT_FILENO; */
|
||||
(void)command;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user