minishell/src/executing/simple_cmd/simple_cmd_execute.c

121 lines
3.7 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* simple_cmd_execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
2025-05-06 13:36:55 +02:00
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2025-05-06 13:36:55 +02:00
/* Created: 2025/05/06 14:25:45 by kcolin #+# #+# */
/* Updated: 2025/05/06 14:59:02 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../ft_errno.h"
#include "simple_cmd_execute.h"
#include "std_fds.h"
2025-03-31 14:53:05 +02:00
#include "builtins.h"
#include "subprocess.h"
#include "libft.h"
#include "../../subst/subst.h"
#include "../common/do_waitpid.h"
#include "../../minishell.h"
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../parser/remove_quotes/remove_quotes.h"
#include "../../postprocess/expansion/expand_vars.h"
#include "../../postprocess/fieldsplit/fieldsplit.h"
#include "../../postprocess/expansion/expand_wildcard.h"
#include "simple_cmd_execute_debug.h"
#include "handle_redirections.h"
#include "../../subst/simple_filename_exp_utils_utils.h"
#include "../../subst/path_split.h"
static void command_not_found(t_simple_cmd *cmd, t_minishell *app)
{
char **path;
path = get_paths_array(app->env);
if (path == NULL || path[0] == NULL)
{
ft_dprintf(STDERR_FILENO, "minishell: %s: No such file or directory\n",
cmd->words->word->word);
}
else
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
cmd->words->word->word);
path_split_destroy(path);
app->last_return_value = 127;
}
static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
{
simple_cmd_post_process_debug(cmd, app);
if (simple_cmd_expand_vars(cmd, app) == NULL)
return (NULL);
simple_cmd_post_process_debug(cmd, app);
if (simple_cmd_fieldsplit(cmd) == NULL)
{
app->last_return_value = 1;
return (NULL);
}
simple_cmd_post_process_debug(cmd, app);
if (simple_cmd_expand_wildcards(cmd) == NULL)
return (NULL);
simple_cmd_post_process_debug(cmd, app);
if (simple_cmd_remove_quotes(cmd) == NULL)
return (NULL);
return (cmd);
}
static t_subprocess fork_fail(t_minishell *app)
{
perror("minishell: fork");
app->last_return_value = 255;
return (PARENTPROCESS);
}
2025-05-06 13:36:55 +02:00
static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app)
{
char *exe;
int pid;
2025-05-06 13:36:55 +02:00
pid = fork();
if (pid == 0)
{
2025-05-06 13:36:55 +02:00
if (handle_redirections(cmd->redirections, app) == NULL)
return (SUBPROCESS);
if (cmd->words == NULL)
return (SUBPROCESS);
exe = get_cmdpath(cmd->words->word->word, app);
if (exe == NULL)
return (command_not_found(cmd, app), SUBPROCESS);
return (execute_subprocess(exe, cmd, app));
}
2025-05-06 13:36:55 +02:00
if (pid < 0)
return (fork_fail(app));
do_waitpid(app, pid);
return (PARENTPROCESS);
}
t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
{
t_builtin_type type;
if (cmd == NULL)
return (PARENTPROCESS);
ft_errno(FT_ESUCCESS);
if (post_process_command(cmd, app) == NULL)
{
if (ft_errno_get() != FT_ESUCCESS && errno != 0)
ft_dprintf(STDERR_FILENO, "minishell: post-processing error\n");
return (PARENTPROCESS);
}
simple_cmd_execute_debug(cmd, app);
type = get_builtin(cmd);
if (type != BUILTIN_INVALID)
2025-05-06 13:36:55 +02:00
return (execute_builtin(cmd, app, type));
return (exec_external_cmd(cmd, app));
}