refactoring exec
This commit is contained in:
2
Makefile
2
Makefile
@@ -25,7 +25,7 @@ LIBS = -L $(LIBFT_D) -lft \
|
|||||||
LIBFT_D = ./libft
|
LIBFT_D = ./libft
|
||||||
LIBFT = $(LIBFT_D)/libft.a
|
LIBFT = $(LIBFT_D)/libft.a
|
||||||
|
|
||||||
SRCS = main.c init.c free.c generic.c \
|
SRCS = main.c init.c free.c generic.c error_wrappers.c \
|
||||||
signals.c \
|
signals.c \
|
||||||
shell_loop.c shell_script.c \
|
shell_loop.c shell_script.c \
|
||||||
lexing.c fill_token.c check_operators.c \
|
lexing.c fill_token.c check_operators.c \
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
/* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/01 14:42:38 by lperrey ### ########.fr */
|
/* Updated: 2021/12/01 17:17:49 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -70,6 +70,9 @@ void free_pipeline(t_cmd **pipeline_ptr[]);
|
|||||||
void close_pipeline_fd(t_cmd *pipeline[]);
|
void close_pipeline_fd(t_cmd *pipeline[]);
|
||||||
typedef void (*t_free_f)(void *); // generic
|
typedef void (*t_free_f)(void *); // generic
|
||||||
|
|
||||||
|
// Error wrappers
|
||||||
|
void shell_error(char *s1, char *s2);
|
||||||
|
|
||||||
// Generic
|
// Generic
|
||||||
char *ft_strjoinfree(char *s1, char *s2);
|
char *ft_strjoinfree(char *s1, char *s2);
|
||||||
char *ft_strjoinfree_s1(char *s1, const char *s2);
|
char *ft_strjoinfree_s1(char *s1, const char *s2);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
|
/* Created: 2021/10/08 02:35:52 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/29 12:25:34 by lperrey ### ########.fr */
|
/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ typedef struct s_cmd
|
|||||||
{
|
{
|
||||||
char **argv;
|
char **argv;
|
||||||
char *path;
|
char *path;
|
||||||
t_builtin_f builtin_func;
|
t_builtin_f builtin_f;
|
||||||
int fd_in;
|
int fd_in;
|
||||||
int fd_out;
|
int fd_out;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|||||||
26
srcs/error_wrappers.c
Normal file
26
srcs/error_wrappers.c
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* error_wrappers.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */
|
||||||
|
/* Updated: 2021/12/01 17:19:02 by lperrey ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void shell_error(char *s1, char *s2)
|
||||||
|
{
|
||||||
|
char *prefix;
|
||||||
|
|
||||||
|
prefix = "minishell: ";
|
||||||
|
write(STDERR_FILENO, prefix, ft_strlen(prefix));
|
||||||
|
if (s1)
|
||||||
|
write(STDERR_FILENO, s1, ft_strlen(s1));
|
||||||
|
if (s2)
|
||||||
|
write(STDERR_FILENO, s2, ft_strlen(s2));
|
||||||
|
write(STDERR_FILENO, "\n", 1);
|
||||||
|
}
|
||||||
@@ -6,14 +6,12 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/29 12:26:08 by lperrey ### ########.fr */
|
/* Updated: 2021/12/01 16:07:48 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01
|
|
||||||
|
|
||||||
int exec_cmd_line(t_all *c)
|
int exec_cmd_line(t_all *c)
|
||||||
{
|
{
|
||||||
if (!pipeline(c))
|
if (!pipeline(c))
|
||||||
@@ -23,3 +21,9 @@ int exec_cmd_line(t_all *c)
|
|||||||
}
|
}
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
2.9.1 Simple Commands - Command Search and Execution
|
||||||
|
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
||||||
|
#tag_18_09_01_01
|
||||||
|
*/
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/30 13:37:09 by lperrey ### ########.fr */
|
/* Updated: 2021/12/01 17:16:55 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -17,8 +17,13 @@ static int search_cmd_path(char *cmd_name, char **cmd_path,
|
|||||||
static t_builtin_f search_builtin(char *cmd_name);
|
static t_builtin_f search_builtin(char *cmd_name);
|
||||||
static int handle_access_error(char *file_name);
|
static int handle_access_error(char *file_name);
|
||||||
|
|
||||||
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
|
/*
|
||||||
// faire des test sur la valeur de errno selon les cas (if directory, if pathname invalid, ...)
|
2.8.2 Exit Status for Commands
|
||||||
|
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
||||||
|
#tag_18_08_02
|
||||||
|
*/
|
||||||
|
|
||||||
|
// TODO : faire des test sur la valeur de errno selon les cas (if directory, if pathname invalid, ...)
|
||||||
|
|
||||||
int cmd_find_access(t_cmd *cmd, char *path[])
|
int cmd_find_access(t_cmd *cmd, char *path[])
|
||||||
{
|
{
|
||||||
@@ -35,23 +40,20 @@ int cmd_find_access(t_cmd *cmd, char *path[])
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cmd->builtin_func = search_builtin(cmd->argv[0]);
|
cmd->builtin_f = search_builtin(cmd->argv[0]);
|
||||||
if (cmd->builtin_func)
|
if (cmd->builtin_f)
|
||||||
return (1);
|
return (1);
|
||||||
if (search_cmd_path(cmd->argv[0], &cmd->path, path) == -1)
|
if (search_cmd_path(cmd->argv[0], &cmd->path, path) == -1)
|
||||||
return (0);
|
return (0);
|
||||||
if (!cmd->path)
|
if (!cmd->path)
|
||||||
{
|
{
|
||||||
cmd->error = EXIT_CMD_NOT_FOUND;
|
cmd->error = EXIT_CMD_NOT_FOUND;
|
||||||
ft_putstr_fd("minishell: ", 2);
|
shell_error(cmd->argv[0], ": command not found");
|
||||||
ft_putstr_fd(cmd->argv[0], 2);
|
|
||||||
ft_putstr_fd(": command not found\n", 2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int search_cmd_path(char *cmd_name, char **cmd_path, char *path[])
|
static int search_cmd_path(char *cmd_name, char **cmd_path, char *path[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/29 12:43:52 by lperrey ### ########.fr */
|
/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ int pipeline(t_all *c)
|
|||||||
return (0);
|
return (0);
|
||||||
if (!pipeline_find_access(c->pipeline, c->path))
|
if (!pipeline_find_access(c->pipeline, c->path))
|
||||||
return (0);
|
return (0);
|
||||||
if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_func)
|
if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_f)
|
||||||
simple_command_builtin(c->pipeline[0], c);
|
simple_command_builtin(c->pipeline[0], c);
|
||||||
else
|
else
|
||||||
wait_subshell(pipeline_exec(c->pipeline, c));
|
wait_subshell(pipeline_exec(c->pipeline, c));
|
||||||
@@ -69,8 +69,6 @@ static int pipeline_find_access(t_cmd *pipeline[], char *path[])
|
|||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : Change exit status as in documentation :
|
|
||||||
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
|
|
||||||
static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c)
|
static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/27 11:07:19 by lperrey ### ########.fr */
|
/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ int simple_command_builtin(t_cmd *cmd, t_all *c)
|
|||||||
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
|
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
|
||||||
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
|
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
|
||||||
}
|
}
|
||||||
set_last_exit_status(cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
|
set_last_exit_status(cmd->builtin_f(ft_2d_arrlen(cmd->argv), cmd->argv, c));
|
||||||
if (!restore_stdio(stdin_dup, stdout_dup))
|
if (!restore_stdio(stdin_dup, stdout_dup))
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
return (EXIT_SUCCESS);
|
return (EXIT_SUCCESS);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/11/29 12:43:55 by lperrey ### ########.fr */
|
/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -28,8 +28,8 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
|
|||||||
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
|
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
|
||||||
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
|
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
|
||||||
close_pipeline_fd(c->pipeline);
|
close_pipeline_fd(c->pipeline);
|
||||||
if (cmd->builtin_func)
|
if (cmd->builtin_f)
|
||||||
exit_free(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
|
exit_free(c, cmd->builtin_f(ft_2d_arrlen(cmd->argv), cmd->argv, c));
|
||||||
else if (execve(cmd->path, cmd->argv, environ) == -1)
|
else if (execve(cmd->path, cmd->argv, environ) == -1)
|
||||||
return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0]));
|
return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0]));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user