/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* simple_cmd_execute.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/06 14:25:45 by kcolin #+# #+# */ /* Updated: 2025/05/06 14:59:02 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 #include #include #include #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; } 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) { char *exe; int pid; pid = fork(); if (pid == 0) { 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)); } if (pid < 0) return (fork_fail(app)); do_waitpid(app, pid); return (PARENTPROCESS); } t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) { 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); type = get_builtin(cmd); if (type != BUILTIN_INVALID) return (execute_builtin(cmd, app, type)); return (exec_external_cmd(cmd, app)); }