mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
82 lines
2.8 KiB
C
82 lines
2.8 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* simple_cmd_execute.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
|
/* Updated: 2025/04/17 17:51:25 by khais ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "simple_cmd_execute.h"
|
|
#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 "../../parser/simple_cmd/simple_cmd.h"
|
|
#include "handle_redirections.h"
|
|
|
|
static void command_not_found(t_simple_cmd *cmd, t_minishell *app)
|
|
{
|
|
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
|
|
cmd->words->word->word);
|
|
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);
|
|
if (simple_cmd_fieldsplit(cmd) == NULL)
|
|
return (NULL);
|
|
if (simple_cmd_expand_wildcards(cmd) == NULL)
|
|
return (NULL);
|
|
if (simple_cmd_remove_quotes(cmd) == NULL)
|
|
return (NULL);
|
|
return (cmd);
|
|
}
|
|
|
|
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
|
|
bool should_exit)
|
|
{
|
|
char *exe;
|
|
int pid;
|
|
|
|
if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL)
|
|
return ;
|
|
if (post_process_command(cmd, app) == NULL)
|
|
return ;
|
|
simple_cmd_execute_debug(cmd, app);
|
|
if (handle_redirections(cmd, app) == NULL)
|
|
return ;
|
|
if (execute_builtin(cmd, app, should_exit) != BUILTIN_INVALID)
|
|
return ;
|
|
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);
|
|
restore_std_fds(app);
|
|
free(exe);
|
|
do_waitpid(app, pid);
|
|
if (should_exit)
|
|
exit(app->last_return_value);
|
|
}
|