minishell/src/executing/simple_cmd/simple_cmd_execute.c

116 lines
3.5 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* simple_cmd_execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/28 14:42:13 by khais ### ########.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;
}
/*
** Do all the post-processing steps relating to a command.
*/
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 void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds)
{
char *exe;
int pid;
if (cmd->words == NULL)
return (restore_std_fds(fds));
exe = get_cmdpath(cmd->words->word->word, app);
if (exe == NULL)
return (command_not_found(cmd, app));
pid = fork();
if (pid == 0)
execute_subprocess(exe, cmd, app, fds);
restore_std_fds(fds);
free(exe);
do_waitpid(app, pid);
}
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
bool should_exit)
{
t_std_fds fds;
if (cmd == NULL)
return ;
ft_errno(FT_ESUCCESS);
if (post_process_command(cmd, app) == NULL)
{
if (ft_errno_get() != FT_ESUCCESS)
ft_dprintf(STDERR_FILENO, "minishell: post-processing error\n");
return ;
}
simple_cmd_execute_debug(cmd, app);
if (handle_redirections(cmd->redirections, app, &fds) == NULL)
return ;
if (execute_builtin(cmd, app, &fds) == BUILTIN_INVALID)
exec_external_cmd(cmd, app, &fds);
if (should_exit)
exit(app->last_return_value);
}