refactoring exec
This commit is contained in:
2
Makefile
2
Makefile
@@ -25,7 +25,7 @@ LIBS = -L $(LIBFT_D) -lft \
|
||||
LIBFT_D = ./libft
|
||||
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 \
|
||||
shell_loop.c shell_script.c \
|
||||
lexing.c fill_token.c check_operators.c \
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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[]);
|
||||
typedef void (*t_free_f)(void *); // generic
|
||||
|
||||
// Error wrappers
|
||||
void shell_error(char *s1, char *s2);
|
||||
|
||||
// Generic
|
||||
char *ft_strjoinfree(char *s1, char *s2);
|
||||
char *ft_strjoinfree_s1(char *s1, const char *s2);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 *path;
|
||||
t_builtin_f builtin_func;
|
||||
t_builtin_f builtin_f;
|
||||
int fd_in;
|
||||
int fd_out;
|
||||
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> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01
|
||||
|
||||
int exec_cmd_line(t_all *c)
|
||||
{
|
||||
if (!pipeline(c))
|
||||
@@ -23,3 +21,9 @@ int exec_cmd_line(t_all *c)
|
||||
}
|
||||
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,19 +6,24 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int search_cmd_path(char *cmd_name, char **cmd_path,
|
||||
char *path[]);
|
||||
char *path[]);
|
||||
static t_builtin_f search_builtin(char *cmd_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[])
|
||||
{
|
||||
@@ -29,29 +34,26 @@ int cmd_find_access(t_cmd *cmd, char *path[])
|
||||
else
|
||||
{
|
||||
cmd->path = ft_strdup(cmd->argv[0]);
|
||||
if (!cmd->path)
|
||||
if (!cmd->path)
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd->builtin_func = search_builtin(cmd->argv[0]);
|
||||
if (cmd->builtin_func)
|
||||
cmd->builtin_f = search_builtin(cmd->argv[0]);
|
||||
if (cmd->builtin_f)
|
||||
return (1);
|
||||
if (search_cmd_path(cmd->argv[0], &cmd->path, path) == -1)
|
||||
return (0);
|
||||
if (!cmd->path)
|
||||
{
|
||||
cmd->error = EXIT_CMD_NOT_FOUND;
|
||||
ft_putstr_fd("minishell: ", 2);
|
||||
ft_putstr_fd(cmd->argv[0], 2);
|
||||
ft_putstr_fd(": command not found\n", 2);
|
||||
shell_error(cmd->argv[0], ": command not found");
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
|
||||
static int search_cmd_path(char *cmd_name, char **cmd_path, char *path[])
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
if (!pipeline_find_access(c->pipeline, c->path))
|
||||
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);
|
||||
else
|
||||
wait_subshell(pipeline_exec(c->pipeline, c));
|
||||
@@ -69,8 +69,6 @@ static int pipeline_find_access(t_cmd *pipeline[], char *path[])
|
||||
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)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
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))
|
||||
return (EXIT_FAILURE);
|
||||
return (EXIT_SUCCESS);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
|
||||
close_pipeline_fd(c->pipeline);
|
||||
if (cmd->builtin_func)
|
||||
exit_free(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
|
||||
if (cmd->builtin_f)
|
||||
exit_free(c, cmd->builtin_f(ft_2d_arrlen(cmd->argv), cmd->argv, c));
|
||||
else if (execve(cmd->path, cmd->argv, environ) == -1)
|
||||
return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user