minishell/src/executing/simple_cmd/builtins.c

62 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
/* Updated: 2025/04/28 14:40:19 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include "libft.h"
#include "simple_cmd_execute.h"
#include "std_fds.h"
t_builtin_type get_builtin(t_simple_cmd *cmd)
{
char *word;
if (cmd->words == NULL)
return (BUILTIN_INVALID);
word = cmd->words->word->word;
if (ft_strcmp("pwd", word) == 0)
return (BUILTIN_PWD);
if (ft_strcmp("cd", word) == 0)
return (BUILTIN_CD);
if (ft_strcmp("export", word) == 0)
return (BUILTIN_EXPORT);
if (ft_strcmp("exit", word) == 0)
return (BUILTIN_EXIT);
if (ft_strcmp("echo", word) == 0)
return (BUILTIN_ECHO);
if (ft_strcmp("env", word) == 0)
return (BUILTIN_ENV);
if (ft_strcmp("unset", word) == 0)
return (BUILTIN_UNSET);
return (BUILTIN_INVALID);
}
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app,
t_std_fds *fds)
{
t_builtin_type type;
static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = {
[BUILTIN_INVALID] = builtin_invalid,
[BUILTIN_PWD] = builtin_pwd,
[BUILTIN_CD] = builtin_cd,
[BUILTIN_EXPORT] = builtin_export,
[BUILTIN_EXIT] = builtin_exit,
[BUILTIN_ECHO] = builtin_echo,
[BUILTIN_ENV] = builtin_env,
[BUILTIN_UNSET] = builtin_unset,
};
type = get_builtin(cmd);
builtins[type](cmd, app);
if (type != BUILTIN_INVALID)
restore_std_fds(fds);
return (type);
}