start execution implementation
This commit is contained in:
parent
4533b7a75d
commit
402b6e875e
Binary file not shown.
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c
|
||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c
|
||||
SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c ft_split_quoted.c env.c execution.c
|
||||
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
|
||||
|
78
execution.c
78
execution.c
@ -1,23 +1,55 @@
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "utils/utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int ac, char **av, char **env)
|
||||
static char *ft_get_variable(char **env, char *variable)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (env[i] == NULL)
|
||||
{
|
||||
if (ft_strncmp(variable, env[1], ft_strlen(variable)))
|
||||
return (ft_strchr(env[1], '=') + 1);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
/* char *ft_get_executable_path(t_data *data, char *executable) */
|
||||
/* { */
|
||||
/* if (ft_strcmp(executable, "env") == 0) */
|
||||
/* return (ft_strjoin("", executable)); */
|
||||
/* else */
|
||||
/* return */
|
||||
/* } */
|
||||
static char *ft_get_executable_path(char *executable_name, char **env)
|
||||
{
|
||||
char *path;
|
||||
char **tab;
|
||||
size_t i;
|
||||
|
||||
int ft_excutor(t_cmd *cmd)
|
||||
if (executable_name[0] == '.' || executable_name[0] == '/')
|
||||
path = executable_name;
|
||||
else
|
||||
{
|
||||
tab = ft_split(ft_get_variable(env, "PATH"), ':');
|
||||
if (tab == NULL)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
if (access(tab[i], X_OK) == 0)
|
||||
{
|
||||
path = ft_strmerger(3, tab[i], "/", executable_name);
|
||||
free(executable_name);
|
||||
break ;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_freer_tab_ultimate(1, tab);
|
||||
}
|
||||
return (path);
|
||||
}
|
||||
|
||||
static int ft_excutor(t_cmd *cmd, char **env)
|
||||
{
|
||||
int pid;
|
||||
char *executable;
|
||||
int return_value;
|
||||
|
||||
pid = fork();
|
||||
if (pid == -1)
|
||||
@ -26,9 +58,31 @@ int ft_excutor(t_cmd *cmd)
|
||||
{
|
||||
dup2(cmd->fd_out, 1);
|
||||
dup2(cmd->fd_in, 0);
|
||||
//TODO ADD ENV VARIABLES
|
||||
execve(cmd->executable, cmd->args, NULL);
|
||||
execve(executable, cmd->args, env);
|
||||
}
|
||||
else
|
||||
waitpid(pid);
|
||||
waitpid(pid, &return_value, 0);
|
||||
return (return_value);
|
||||
}
|
||||
|
||||
int ft_cmds_executor(t_list **cmds, char **env)
|
||||
{
|
||||
t_cmd *content;
|
||||
t_list *current;
|
||||
int fds[2];
|
||||
|
||||
current = *cmds;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (pipe(fds) == -1)
|
||||
{
|
||||
ft_eprintf("minishell: pipe failed");
|
||||
return (1);
|
||||
}
|
||||
content->fd_out = fds[1];
|
||||
((t_cmd *) current->next)->fd_in = fds[0];
|
||||
ft_excutor(content, env);
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
40
main.c
40
main.c
@ -132,12 +132,7 @@ int ft_cmd_filler(t_list *element, char **args)
|
||||
i++;
|
||||
}
|
||||
content->args = args;
|
||||
//TODO check if executable exist
|
||||
//TODO change it by env value
|
||||
//TODO add switch to bultin
|
||||
content->executable = ft_strjoin("/usr/bin/", args[0]);
|
||||
if (content->executable == NULL)
|
||||
return (1);
|
||||
content->executable = args[0];
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -188,41 +183,26 @@ t_list **ft_parse_cmds(char *line)
|
||||
return (cmds);
|
||||
}
|
||||
|
||||
int ft_cmds_excutor(t_list **cmds)
|
||||
{
|
||||
t_cmd *content;
|
||||
t_list *current;
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
current = *cmds;
|
||||
while (current != NULL)
|
||||
{
|
||||
content = current->content;
|
||||
ft_printf("--- COMMAND %d\n", i);
|
||||
ft_printf("excutable: %s\n", content->executable);
|
||||
ft_printf("args:\n%S", content->args);
|
||||
current = current->next;
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
int main(int ac, char **av, char **env)
|
||||
{
|
||||
t_list **cmds;
|
||||
t_data data;
|
||||
|
||||
if (ac == 1)
|
||||
return (1);
|
||||
data.env = init_env(env);
|
||||
if (data.env == NULL)
|
||||
return (1);
|
||||
cmds = ft_parse_cmds(av[1]);
|
||||
if (cmds == NULL)
|
||||
return (1);
|
||||
if (ft_cmds_excutor(cmds) == 1)
|
||||
if (ft_cmds_executor(cmds, env) == 1)
|
||||
{
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
return (1);
|
||||
}
|
||||
ft_lstclear(data.env, env_del);
|
||||
ft_lstclear(cmds, ft_lstdel);
|
||||
free(cmds);
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
@ -11,9 +11,9 @@
|
||||
# include <readline/history.h>
|
||||
|
||||
t_list **init_env(char **env);
|
||||
int set_value_key(t_list **env, char *key, char *value);
|
||||
char *get_value_index(int index, t_list **head);
|
||||
char *get_value_key(char *key, t_list **head);
|
||||
|
||||
int ft_syntatic_verif(const char *str);
|
||||
int ft_file_is_readable(const char *path);
|
||||
int ft_file_is_writeable(const char *path);
|
||||
@ -22,6 +22,7 @@ int ft_infile(char *line);
|
||||
int ft_outfile(char *line);
|
||||
int ft_heredoc(char *stop);
|
||||
size_t ft_seglen_quoted(const char *str, char c);
|
||||
int ft_cmds_executor(t_list **cmds, char **env);
|
||||
char **ft_split_quoted(const char *s, char c);
|
||||
|
||||
typedef struct s_cmd
|
||||
@ -35,6 +36,7 @@ typedef struct s_cmd
|
||||
typedef struct s_data
|
||||
{
|
||||
t_list **env;
|
||||
int heredoc;
|
||||
} t_data;
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user