mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-05 23:18:08 +01:00
128 lines
3.9 KiB
C
128 lines
3.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* simple_cmd_execute.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/03/27 16:21:56 by kcolin #+# #+# */
|
|
/* Updated: 2025/04/30 15:08:01 by kcolin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../../ft_errno.h"
|
|
#include "simple_cmd_execute.h"
|
|
#include "std_fds.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 "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 t_subprocess fork_fail(t_minishell *app)
|
|
{
|
|
perror("minishell: fork");
|
|
app->last_return_value = 255;
|
|
return (PARENTPROCESS);
|
|
}
|
|
|
|
static t_subprocess 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), PARENTPROCESS);
|
|
exe = get_cmdpath(cmd->words->word->word, app);
|
|
if (exe != NULL)
|
|
{
|
|
pid = fork();
|
|
if (pid == 0)
|
|
return (execute_subprocess(exe, cmd, app, fds));
|
|
free(exe);
|
|
if (pid < 0)
|
|
return (fork_fail(app));
|
|
do_waitpid(app, pid);
|
|
}
|
|
restore_std_fds(fds);
|
|
if (exe == NULL)
|
|
command_not_found(cmd, app);
|
|
return (PARENTPROCESS);
|
|
}
|
|
|
|
t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
|
{
|
|
t_std_fds fds;
|
|
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);
|
|
if (handle_redirections(cmd->redirections, app, &fds) == NULL)
|
|
return (PARENTPROCESS);
|
|
type = get_builtin(cmd);
|
|
if (type != BUILTIN_INVALID)
|
|
return (execute_builtin(cmd, app, &fds, type));
|
|
return (exec_external_cmd(cmd, app, &fds));
|
|
}
|