2025-03-27 16:10:41 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* simple_cmd_execute.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
2025-05-06 13:36:55 +02:00
|
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
2025-03-27 16:10:41 +01:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
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 */
|
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
|
|
|
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-30 14:06:38 +02:00
|
|
|
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)
|
2025-03-27 16:10:41 +01:00
|
|
|
{
|
|
|
|
|
char *exe;
|
|
|
|
|
int pid;
|
|
|
|
|
|
2025-05-06 13:36:55 +02:00
|
|
|
pid = fork();
|
|
|
|
|
if (pid == 0)
|
2025-04-28 15:04:47 +02:00
|
|
|
{
|
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-04-28 15:04:47 +02:00
|
|
|
}
|
2025-05-06 13:36:55 +02:00
|
|
|
if (pid < 0)
|
|
|
|
|
return (fork_fail(app));
|
|
|
|
|
do_waitpid(app, pid);
|
2025-04-29 12:58:26 +02:00
|
|
|
return (PARENTPROCESS);
|
2025-04-18 14:07:48 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-29 12:58:26 +02:00
|
|
|
t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
2025-04-18 14:07:48 +02:00
|
|
|
{
|
2025-04-29 15:50:38 +02:00
|
|
|
t_builtin_type type;
|
2025-04-28 12:11:27 +02:00
|
|
|
|
2025-04-28 14:20:13 +02:00
|
|
|
if (cmd == NULL)
|
2025-04-29 12:58:26 +02:00
|
|
|
return (PARENTPROCESS);
|
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
|
|
|
{
|
2025-04-30 15:03:30 +02:00
|
|
|
if (ft_errno_get() != FT_ESUCCESS && errno != 0)
|
2025-04-21 15:12:25 +02:00
|
|
|
ft_dprintf(STDERR_FILENO, "minishell: post-processing error\n");
|
2025-04-29 12:58:26 +02:00
|
|
|
return (PARENTPROCESS);
|
2025-04-21 15:12:25 +02:00
|
|
|
}
|
2025-04-18 14:07:48 +02:00
|
|
|
simple_cmd_execute_debug(cmd, app);
|
2025-04-29 15:50:38 +02:00
|
|
|
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));
|
2025-03-27 16:10:41 +01:00
|
|
|
}
|