Commit ab0251a7 authored by abenoit's avatar abenoit
Browse files

norm fix on src/execution and syntax analyser

parent 80f92279
......@@ -16,6 +16,7 @@ SRC += parsing_vars_redirs.c
SRC += parsing_utils.c
SRC += exec_handler.c
SRC += parent_process.c
SRC += launch_command.c
SRC += redirs.c
SRC += exec_utils.c
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 18:37:02 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:37:27 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUILTINS_H
# define BUILTINS_H
......@@ -5,7 +17,7 @@
# include "error.h"
# define ECHO_OPTIONS "n"
# define ECHO_N_OPTION 0b00000001
# define ECHO_N_OPTION 0x01
# define ENTIRE_STR -1
......@@ -13,13 +25,13 @@
** builtins
*/
int ft_echo(char **args, t_xe *xe);
int ft_pwd(char **args, t_xe *xe);
int ft_cd(char **args, t_xe *xe);
int ft_export(char **args, t_xe *xe);
int ft_unset(char **args, t_xe *xe);
int ft_env(char **args, t_xe *xe);
int ft_exit(char **args, t_xe *xe);
int ft_echo(char **args, t_xe *xe);
int ft_pwd(char **args, t_xe *xe);
int ft_cd(char **args, t_xe *xe);
int ft_export(char **args, t_xe *xe);
int ft_unset(char **args, t_xe *xe);
int ft_env(char **args, t_xe *xe);
int ft_exit(char **args, t_xe *xe);
/*
** utils
......
......@@ -10,6 +10,7 @@
# include "error.h"
# define NOT_FOUND -1
# define KEEP_ON 303
enum e_cmd_code
{
......@@ -29,8 +30,13 @@ typedef int (*t_exec)(char **args, t_xe *xe);
** main function
*/
int parent_pipe_end(t_command *cur_command, t_xe *xe,
int fd_in, int proc);
int execute_cmd(char **args, char **redir_paths, enum e_redir_op *redir_types, t_xe *xe);
int apply_redirs(char **redir_paths, enum e_redir_op *redir_types, t_xe *xe);
int find_ambig_redir(char **redir_paths, enum e_redir_op *redir_types);
/*
** main function
......
......@@ -29,8 +29,6 @@
# define TRUE 1
# define FALSE 0
# define _XOPEN_SOURCE // qu'est-ce?
# define FT_PS1 "$ "
# define SHLVL_STR "SHLVL"
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 18:36:35 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:36:37 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSING_H
# define PARSING_H
......@@ -59,9 +71,29 @@ typedef int (*t_syntax)(unsigned int *flags);
/*
** check_syntax.c
*/
int parsing_error(int err_code, t_xe *xe);
int check_syntax(char *line);
/*
** syntax_utils_1.c
*/
int check_backsl(unsigned int *flags);
int check_quote(unsigned int *flags);
int check_dquote(unsigned int *flags);
int check_r_redir(unsigned int *flags);
int check_l_redir(unsigned int *flags);
/*
** syntax_utils_2.c
*/
int check_semicol(unsigned int *flags);
int check_pipe(unsigned int *flags);
int check_space(unsigned int *flags);
int check_others(unsigned int *flags);
/*
** main function
*/
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 17:52:41 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:13:54 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
#include "execution.h"
static int child_setup(const int *fd, int fd_in, t_xe *xe)
static int child_pipe(t_command *cur_command, const int *fd,
int fd_in, t_xe *xe)
{
int ret;
xe->flags += CHILD;
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
......@@ -11,10 +26,14 @@ static int child_setup(const int *fd, int fd_in, t_xe *xe)
if (fd_in != STDIN_FILENO)
{
if (dup2(fd_in, STDIN_FILENO) == ERROR || close(fd_in) == ERROR)
return (FAILURE);
return (FD_ERROR);
}
if (dup2(fd[1], STDOUT_FILENO) == ERROR || close(fd[1]) == ERROR)
return (FAILURE);
return (FD_ERROR);
ret = execute_cmd(cur_command->args, cur_command->redir_paths,
cur_command->redir_types, xe);
free_command(cur_command);
xe->flags -= RUN;
return (SUCCESS);
}
......@@ -30,14 +49,7 @@ int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
if (xe->gpid == ERROR)
return (FORK_ERROR);
else if (xe->gpid == 0)
{
if (child_setup(fd, fd_in, xe) != SUCCESS)
return (FD_ERROR);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);
free_command(cur_command);
xe->flags -= RUN;
return (ret);
}
return (child_pipe(cur_command, fd, fd_in, xe));
else
{
if (!(xe->flags & CMD_PIPE))
......@@ -49,89 +61,60 @@ int handle_pipe(t_command *cur_command, t_xe *xe, int fd_in, int proc)
}
}
int parent_pipe_end(t_command *cur_command, t_xe *xe, int fd_in, int proc)
int handle_command(t_command *cur_command, t_xe *xe,
int fd_in, int proc)
{
int i;
int ret;
int tmp;
int wait_ret;
ret = SUCCESS;
if (fd_in != STDIN_FILENO)
if (cur_command->args == NULL)
{
if (dup2(fd_in, STDIN_FILENO) == ERROR || close(fd_in) == ERROR)
return (FD_ERROR);
free_command(cur_command);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
return (handle_execution(xe, STDIN_FILENO, 0));
}
if (dup2(xe->backup_stdout, STDOUT_FILENO) == ERROR)
return (FD_ERROR);
ret = execute_cmd(cur_command->args, cur_command->redir_paths, cur_command->redir_types, xe);
tmp = xe->stat_loc;
free_command(cur_command);
if (dup2(xe->backup_stdout, STDOUT_FILENO) == ERROR)
return (FD_ERROR);
if (dup2(xe->backup_stdin, STDIN_FILENO) == ERROR)
return (FD_ERROR);
i = 0;
while (i < proc)
else if (cur_command->pipe_flag == TRUE)
{
wait_ret = wait(&xe->stat_loc);
if (wait_ret == ERROR)
return (WAIT_ERROR);
if (WIFSIGNALED(xe->stat_loc))
{
xe->stat_loc = WTERMSIG(xe->stat_loc);
if (xe->stat_loc == SIGQUIT)
{
if (ft_putstr_fd("\b\b^\\Quit (core dumped)\n", STDERR_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
else if (xe->stat_loc == SIGINT)
{
if (ft_putstr_fd("\n", STDERR_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
xe->stat_loc = (xe->stat_loc % 256) + 128;
}
else if(WIFEXITED(xe->stat_loc))
xe->stat_loc = WEXITSTATUS(xe->stat_loc);
i++;
return (handle_pipe(cur_command, xe, fd_in, proc));
}
if (xe->flags & EXIT_FLAG || (ret != SUCCESS && ret < _ERRNO_MSG_))
return (ret);
else
{
xe->stat_loc = tmp;
ft_error(ret, xe);
if (ret == EXIT_ARG_ERR)
return (SUCCESS);
return (handle_execution(xe, STDIN_FILENO, 0));
return (parent_pipe_end(cur_command, xe, fd_in, proc));
}
}
int handle_command(t_command *cur_command, t_xe *xe, int fd_in, int proc)
static int check_redir_type(t_command *cur_command, t_xe *xe,
int fd_in, int proc)
{
int ret;
ret = SUCCESS;
if (cur_command->args == NULL)
if (find_ambig_redir(cur_command->redir_paths,
cur_command->redir_types) != SUCCESS)
{
free_command(cur_command);
if (ft_error(AMBIG_REDIR, xe) != SUCCESS)
return (FAILURE);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
return (handle_execution(xe, STDIN_FILENO, 0));
xe->stat_loc = 1;
return (handle_execution(xe, fd_in, proc));
}
else if (cur_command->pipe_flag == TRUE)
{
return (handle_pipe(cur_command, xe, fd_in, proc));
}
else
if (cur_command->args == NULL)
{
return (parent_pipe_end(cur_command, xe, fd_in, proc));
if (apply_redirs(cur_command->redir_paths,
cur_command->redir_types, xe) != SUCCESS)
{
free_command(cur_command);
return (FD_ERROR);
}
free_command(cur_command);
return (handle_execution(xe, fd_in, proc));
}
return (SUCCESS);
}
int handle_execution(t_xe *xe, int fd_in, int proc)
{
int ret;
t_command *cur_command;
xe->gpid = -1;
......@@ -140,26 +123,9 @@ int handle_execution(t_xe *xe, int fd_in, int proc)
return (MALLOC_ERR);
if (cur_command->redir_types != NULL)
{
if (cur_command->redir_types[0] == AMBIG)
{
free_command(cur_command);
if (ft_error(AMBIG_REDIR, xe) != SUCCESS)
return (FAILURE);
dup2(xe->backup_stdout, STDOUT_FILENO);
dup2(xe->backup_stdin, STDIN_FILENO);
xe->stat_loc = 1;
return (handle_execution(xe, fd_in, proc));
}
if (cur_command->args == NULL)
{
if (apply_redirs(cur_command->redir_paths, cur_command->redir_types, xe) != SUCCESS)
{
free_command(cur_command);
return (FD_ERROR);
}
free_command(cur_command);
return (handle_execution(xe, fd_in, proc));
}
ret = check_redir_type(cur_command, xe, fd_in, proc);
if (ret != KEEP_ON)
return (ret);
}
if (cur_command->args != NULL || cur_command->var_flag == TRUE)
return (handle_command(cur_command, xe, fd_in, proc));
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 18:16:01 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:16:23 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#include "execution.h"
static int search_path(DIR *dirp, char *name)
......@@ -38,7 +50,7 @@ static int search_exec(char **path, char *name)
return (NOT_FOUND);
}
int create_cmd(char **tmp, char **path, char **args)
int create_cmd(char **tmp, char **path, char **args)
{
char *cmd;
char *dir;
......@@ -69,7 +81,7 @@ static char *add_path_to_localdir(void)
return (tmp);
}
char **create_path_array(t_xe *xe)
char **create_path_array(t_xe *xe)
{
int ret;
char *tmp;
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* launch_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 18:15:26 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:15:37 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#include "execution.h"
#include "builtins.h"
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parent_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 18:10:57 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:14:59 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#include "execution.h"
static int signal_interupt(t_xe *xe)
{
xe->stat_loc = WTERMSIG(xe->stat_loc);
if (xe->stat_loc == SIGQUIT)
{
if (ft_putstr_fd("\b\b^\\Quit (core dumped)\n", STDERR_FILENO)
!= WRITE_SUCCESS)
return (WRITE_ERR);
}
else if (xe->stat_loc == SIGINT)
{
if (ft_putstr_fd("\n", STDERR_FILENO) != WRITE_SUCCESS)
return (WRITE_ERR);
}
xe->stat_loc = (xe->stat_loc % 256) + 128;
return (SUCCESS);
}
static int parent_wait(t_xe *xe, int proc)
{
int i;
int ret;
i = 0;
while (i < proc)
{
ret = wait(&xe->stat_loc);
if (ret == ERROR)
return (WAIT_ERROR);
if (WIFSIGNALED(xe->stat_loc))
{
ret = signal_interupt(xe);
if (ret != SUCCESS)
return (ret);
}
else if (WIFEXITED(xe->stat_loc))
xe->stat_loc = WEXITSTATUS(xe->stat_loc);
i++;
}
return (SUCCESS);
}
static int parent_pipe_exec(t_command *cur_command, t_xe *xe,
int fd_in, int *tmp)
{
int ret;
ret = SUCCESS;
if (fd_in != STDIN_FILENO)
{
if (dup2(fd_in, STDIN_FILENO) == ERROR || close(fd_in) == ERROR)
return (FD_ERROR);
}
if (dup2(xe->backup_stdout, STDOUT_FILENO) == ERROR)
return (FD_ERROR);
ret = execute_cmd(cur_command->args, cur_command->redir_paths,
cur_command->redir_types, xe);
*tmp = xe->stat_loc;
free_command(cur_command);
if (dup2(xe->backup_stdout, STDOUT_FILENO) == ERROR)
return (FD_ERROR);
if (dup2(xe->backup_stdin, STDIN_FILENO) == ERROR)
return (FD_ERROR);
return (ret);
}
int parent_pipe_end(t_command *cur_command, t_xe *xe,
int fd_in, int proc)
{
int ret;
int tmp;
int wait_ret;
ret = SUCCESS;
ret = parent_pipe_exec(cur_command, xe, fd_in, &tmp);
wait_ret = parent_wait(xe, proc);
if (wait_ret != SUCCESS)
return (wait_ret);
if (xe->flags & EXIT_FLAG || (ret != SUCCESS && ret < _ERRNO_MSG_))
return (ret);
else
{
xe->stat_loc = tmp;
ft_error(ret, xe);
if (ret == EXIT_ARG_ERR)
return (SUCCESS);
return (handle_execution(xe, STDIN_FILENO, 0));
}
}
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 18:15:30 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:15:33 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#include "execution.h"
int find_ambig_redir(char **redir_paths, enum e_redir_op *redir_types)
{
int i;
i = 0;
while (redir_paths[i] != NULL)
{
if (redir_types[i] == AMBIG)
return (FAILURE);
i++;
}
return (SUCCESS);
}
static void create_fd(char *cur_arg, enum e_redir_op redir,
int *src_fd, int *redir_fd)
{
......@@ -42,7 +68,8 @@ int apply_redir(char *cur_arg, enum e_redir_op redir, t_xe *xe)
return (SUCCESS);
}
int apply_redirs(char **redir_paths, enum e_redir_op *redir_types, t_xe *xe)
int apply_redirs(char **redir_paths, enum e_redir_op *redir_types,
t_xe *xe)
{
int i;
int ret;
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenoit <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/11 18:31:21 by abenoit #+# #+# */
/* Updated: 2021/02/11 18:35:05 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int check_backsl(unsigned int *flags)
{
if (*flags & S_QUOTE)
return (SUCCESS);
if (!(*flags & S_BACKSL))
*flags += S_BACKSL;
return (SUCCESS);
}
int check_quote(unsigned int *flags)
{
if (*flags & S_DQUOTE)
return (SUCCESS);
else if (!(*flags & S_QUOTE))
*flags += S_QUOTE;
else
*flags -= S_QUOTE;
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
return (SUCCESS);
}
int check_dquote(unsigned int *flags)
{
if (*flags & S_QUOTE)
return (SUCCESS);
else if (!(*flags & S_DQUOTE))
*flags += S_DQUOTE;
else
*flags -= S_DQUOTE;
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
return (SUCCESS);
}
int check_r_redir(unsigned int *flags)
{
if ((*flags & S_R_REDIR))
{
*flags += S_APPEND;
*flags -= S_R_REDIR;
}
else if (!(*flags & S_R_REDIR) && !(*flags & S_L_REDIR))
*flags += S_R_REDIR;
else
return (REDIR_PATH_MISSING);
return (SUCCESS);
}
int check_l_redir(unsigned int *flags)
{
if (!(*flags & S_L_REDIR) && !(*flags & S_R_REDIR))
*flags += S_L_REDIR;
else
return (REDIR_PATH_MISSING);
return (SUCCESS);
}
int check_semicol(unsigned int *flags)
{
if (!(*flags & S_SEMICOL) && !(*flags & S_PIPE))
*flags += S_SEMICOL;