2025-03-27 16:10:41 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* simple_cmd_execute.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
2025-04-28 14:20:13 +02:00
|
|
|
/* Updated: 2025/04/28 14:42:13 by khais ### ########.fr */
|
2025-03-27 16:10:41 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-04-21 15:12:25 +02:00
|
|
|
#include "../../ft_errno.h"
|
2025-03-27 16:10:41 +01:00
|
|
|
#include "simple_cmd_execute.h"
|
2025-04-28 12:11:27 +02:00
|
|
|
#include "std_fds.h"
|
2025-03-31 14:53:05 +02:00
|
|
|
#include "builtins.h"
|
2025-04-02 18:17:37 +02:00
|
|
|
#include "subprocess.h"
|
2025-03-27 16:10:41 +01:00
|
|
|
#include "libft.h"
|
|
|
|
|
#include "../../subst/subst.h"
|
2025-04-04 19:56:54 +02:00
|
|
|
#include "../common/do_waitpid.h"
|
2025-04-07 11:59:44 +02:00
|
|
|
#include "../../minishell.h"
|
2025-03-27 16:10:41 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/wait.h>
|
2025-04-01 16:28:06 +02:00
|
|
|
#include <stdio.h>
|
2025-04-02 15:26:11 +02:00
|
|
|
#include <stdlib.h>
|
2025-04-08 16:10:34 +02:00
|
|
|
#include "../../parser/remove_quotes/remove_quotes.h"
|
|
|
|
|
#include "../../postprocess/expansion/expand_vars.h"
|
|
|
|
|
#include "../../postprocess/fieldsplit/fieldsplit.h"
|
|
|
|
|
#include "../../postprocess/expansion/expand_wildcard.h"
|
2025-04-08 16:18:57 +02:00
|
|
|
#include "simple_cmd_execute_debug.h"
|
2025-04-17 10:09:20 +02:00
|
|
|
#include "handle_redirections.h"
|
2025-04-25 15:36:59 +02:00
|
|
|
#include "../../subst/simple_filename_exp_utils_utils.h"
|
|
|
|
|
#include "../../subst/path_split.h"
|
2025-03-27 16:10:41 +01:00
|
|
|
|
2025-04-16 14:59:22 +02:00
|
|
|
static void command_not_found(t_simple_cmd *cmd, t_minishell *app)
|
2025-03-28 16:57:00 +01:00
|
|
|
{
|
2025-04-25 15:36:59 +02:00
|
|
|
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);
|
2025-04-16 14:59:22 +02:00
|
|
|
app->last_return_value = 127;
|
2025-03-28 16:57:00 +01:00
|
|
|
}
|
|
|
|
|
|
2025-04-08 16:10:34 +02:00
|
|
|
/*
|
|
|
|
|
** Do all the post-processing steps relating to a command.
|
|
|
|
|
*/
|
|
|
|
|
static t_simple_cmd *post_process_command(t_simple_cmd *cmd, t_minishell *app)
|
|
|
|
|
{
|
2025-04-16 16:39:47 +02:00
|
|
|
simple_cmd_post_process_debug(cmd, app);
|
2025-04-08 16:10:34 +02:00
|
|
|
if (simple_cmd_expand_vars(cmd, app) == NULL)
|
2025-04-17 14:03:35 +02:00
|
|
|
return (NULL);
|
2025-04-22 12:45:28 +02:00
|
|
|
simple_cmd_post_process_debug(cmd, app);
|
2025-04-08 16:10:34 +02:00
|
|
|
if (simple_cmd_fieldsplit(cmd) == NULL)
|
2025-04-21 08:25:49 +02:00
|
|
|
{
|
|
|
|
|
app->last_return_value = 1;
|
2025-04-17 14:03:35 +02:00
|
|
|
return (NULL);
|
2025-04-21 08:25:49 +02:00
|
|
|
}
|
2025-04-22 12:45:28 +02:00
|
|
|
simple_cmd_post_process_debug(cmd, app);
|
2025-04-08 16:10:34 +02:00
|
|
|
if (simple_cmd_expand_wildcards(cmd) == NULL)
|
2025-04-17 14:03:35 +02:00
|
|
|
return (NULL);
|
2025-04-22 12:45:28 +02:00
|
|
|
simple_cmd_post_process_debug(cmd, app);
|
2025-04-08 16:10:34 +02:00
|
|
|
if (simple_cmd_remove_quotes(cmd) == NULL)
|
2025-04-17 14:03:35 +02:00
|
|
|
return (NULL);
|
2025-04-08 16:10:34 +02:00
|
|
|
return (cmd);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-28 12:11:27 +02:00
|
|
|
static void exec_external_cmd(t_simple_cmd *cmd, t_minishell *app,
|
|
|
|
|
t_std_fds *fds)
|
2025-03-27 16:10:41 +01:00
|
|
|
{
|
|
|
|
|
char *exe;
|
|
|
|
|
int pid;
|
|
|
|
|
|
2025-04-28 14:20:13 +02:00
|
|
|
if (cmd->words == NULL)
|
|
|
|
|
return (restore_std_fds(fds));
|
2025-03-27 16:10:41 +01:00
|
|
|
exe = get_cmdpath(cmd->words->word->word, app);
|
|
|
|
|
if (exe == NULL)
|
2025-04-17 10:09:20 +02:00
|
|
|
return (command_not_found(cmd, app));
|
2025-03-27 16:10:41 +01:00
|
|
|
pid = fork();
|
|
|
|
|
if (pid == 0)
|
2025-04-28 12:11:27 +02:00
|
|
|
execute_subprocess(exe, cmd, app, fds);
|
|
|
|
|
restore_std_fds(fds);
|
2025-03-27 16:10:41 +01:00
|
|
|
free(exe);
|
2025-04-02 15:26:11 +02:00
|
|
|
do_waitpid(app, pid);
|
2025-04-18 14:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app,
|
|
|
|
|
bool should_exit)
|
|
|
|
|
{
|
2025-04-28 12:11:27 +02:00
|
|
|
t_std_fds fds;
|
|
|
|
|
|
2025-04-28 14:20:13 +02:00
|
|
|
if (cmd == NULL)
|
2025-04-18 14:07:48 +02:00
|
|
|
return ;
|
2025-04-21 15:12:25 +02:00
|
|
|
ft_errno(FT_ESUCCESS);
|
2025-04-18 14:07:48 +02:00
|
|
|
if (post_process_command(cmd, app) == NULL)
|
2025-04-21 15:12:25 +02:00
|
|
|
{
|
|
|
|
|
if (ft_errno_get() != FT_ESUCCESS)
|
|
|
|
|
ft_dprintf(STDERR_FILENO, "minishell: post-processing error\n");
|
2025-04-18 14:07:48 +02:00
|
|
|
return ;
|
2025-04-21 15:12:25 +02:00
|
|
|
}
|
2025-04-18 14:07:48 +02:00
|
|
|
simple_cmd_execute_debug(cmd, app);
|
2025-04-28 12:11:27 +02:00
|
|
|
if (handle_redirections(cmd->redirections, app, &fds) == NULL)
|
2025-04-18 14:07:48 +02:00
|
|
|
return ;
|
2025-04-28 12:11:27 +02:00
|
|
|
if (execute_builtin(cmd, app, &fds) == BUILTIN_INVALID)
|
|
|
|
|
exec_external_cmd(cmd, app, &fds);
|
2025-04-16 16:39:47 +02:00
|
|
|
if (should_exit)
|
|
|
|
|
exit(app->last_return_value);
|
2025-03-27 16:10:41 +01:00
|
|
|
}
|