start execution implementation
This commit is contained in:
		
										
											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
 | 
					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}
 | 
					OBJS = ${SRCS:.c=.o}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										78
									
								
								execution.c
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								execution.c
									
									
									
									
									
								
							@ -1,23 +1,55 @@
 | 
				
			|||||||
#include "libftx/libftx.h"
 | 
					#include "libftx/libftx.h"
 | 
				
			||||||
#include "minishell.h"
 | 
					#include "minishell.h"
 | 
				
			||||||
 | 
					#include "utils/utils.h"
 | 
				
			||||||
#include <unistd.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) */
 | 
					static char	*ft_get_executable_path(char *executable_name, char **env)
 | 
				
			||||||
/* { */
 | 
					{
 | 
				
			||||||
/* 	if (ft_strcmp(executable, "env") == 0) */
 | 
						char	*path;
 | 
				
			||||||
/* 		return (ft_strjoin("", executable)); */
 | 
						char	**tab;
 | 
				
			||||||
/* 	else */
 | 
						size_t	i;
 | 
				
			||||||
/* 		return  */
 | 
					 | 
				
			||||||
/* } */
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
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;
 | 
						int		pid;
 | 
				
			||||||
 | 
						char	*executable;
 | 
				
			||||||
 | 
						int		return_value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pid = fork();
 | 
						pid = fork();
 | 
				
			||||||
	if (pid == -1)
 | 
						if (pid == -1)
 | 
				
			||||||
@ -26,9 +58,31 @@ int	ft_excutor(t_cmd *cmd)
 | 
				
			|||||||
	{
 | 
						{
 | 
				
			||||||
		dup2(cmd->fd_out, 1);
 | 
							dup2(cmd->fd_out, 1);
 | 
				
			||||||
		dup2(cmd->fd_in, 0);
 | 
							dup2(cmd->fd_in, 0);
 | 
				
			||||||
		//TODO ADD ENV VARIABLES
 | 
							execve(executable, cmd->args, env);
 | 
				
			||||||
		execve(cmd->executable, cmd->args, NULL);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else
 | 
						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++;
 | 
							i++;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	content->args = args;
 | 
						content->args = args;
 | 
				
			||||||
	//TODO check if executable exist
 | 
						content->executable = args[0];
 | 
				
			||||||
	//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);
 | 
					 | 
				
			||||||
	return (0);
 | 
						return (0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -188,41 +183,26 @@ t_list	**ft_parse_cmds(char *line)
 | 
				
			|||||||
	return (cmds);
 | 
						return (cmds);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int	ft_cmds_excutor(t_list **cmds)
 | 
					int	main(int ac, char **av, char **env)
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	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)
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	t_list	**cmds;
 | 
						t_list	**cmds;
 | 
				
			||||||
 | 
						t_data	data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (ac == 1)
 | 
						if (ac == 1)
 | 
				
			||||||
		return (1);
 | 
							return (1);
 | 
				
			||||||
 | 
						data.env = init_env(env);
 | 
				
			||||||
 | 
						if (data.env == NULL)
 | 
				
			||||||
 | 
							return (1);
 | 
				
			||||||
	cmds = ft_parse_cmds(av[1]);
 | 
						cmds = ft_parse_cmds(av[1]);
 | 
				
			||||||
	if (cmds == NULL)
 | 
						if (cmds == NULL)
 | 
				
			||||||
		return (1);
 | 
							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);
 | 
							ft_lstclear(cmds, ft_lstdel);
 | 
				
			||||||
		return (1);
 | 
							return (1);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						ft_lstclear(data.env, env_del);
 | 
				
			||||||
	ft_lstclear(cmds, ft_lstdel);
 | 
						ft_lstclear(cmds, ft_lstdel);
 | 
				
			||||||
	free(cmds);
 | 
						return (0);
 | 
				
			||||||
	return (1);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -11,9 +11,9 @@
 | 
				
			|||||||
# include <readline/history.h>
 | 
					# include <readline/history.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
t_list		**init_env(char **env);
 | 
					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_index(int index, t_list **head);
 | 
				
			||||||
char		*get_value_key(char *key, t_list **head);
 | 
					char		*get_value_key(char *key, t_list **head);
 | 
				
			||||||
 | 
					 | 
				
			||||||
int			ft_syntatic_verif(const char *str);
 | 
					int			ft_syntatic_verif(const char *str);
 | 
				
			||||||
int			ft_file_is_readable(const char *path);
 | 
					int			ft_file_is_readable(const char *path);
 | 
				
			||||||
int			ft_file_is_writeable(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_outfile(char *line);
 | 
				
			||||||
int			ft_heredoc(char *stop);
 | 
					int			ft_heredoc(char *stop);
 | 
				
			||||||
size_t		ft_seglen_quoted(const char *str, char c);
 | 
					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);
 | 
					char		**ft_split_quoted(const char *s, char c);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct s_cmd
 | 
					typedef struct s_cmd
 | 
				
			||||||
@ -35,6 +36,7 @@ typedef struct s_cmd
 | 
				
			|||||||
typedef struct s_data
 | 
					typedef struct s_data
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	t_list	**env;
 | 
						t_list	**env;
 | 
				
			||||||
 | 
						int		heredoc;
 | 
				
			||||||
}	t_data;
 | 
					}	t_data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user