mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
simple_cmd executing refactor: put builtin related utilities into own file
This commit is contained in:
parent
ba4192bf8d
commit
0a80b9fbe3
4 changed files with 48 additions and 30 deletions
42
src/executing/simple_cmd/builtins.c
Normal file
42
src/executing/simple_cmd/builtins.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* builtins.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
|
||||
/* Updated: 2025/04/01 16:37:38 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "builtins.h"
|
||||
#include "libft.h"
|
||||
|
||||
t_builtin_type get_builtin(t_simple_cmd *cmd)
|
||||
{
|
||||
char *word;
|
||||
|
||||
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);
|
||||
return (BUILTIN_INVALID);
|
||||
}
|
||||
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
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,
|
||||
};
|
||||
|
||||
type = get_builtin(cmd);
|
||||
return (builtins[type](cmd, app));
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
|
||||
/* Updated: 2025/04/01 14:00:26 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/01 16:37:15 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -20,4 +20,7 @@ t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app);
|
|||
t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app);
|
||||
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app);
|
||||
|
||||
t_builtin_type get_builtin(t_simple_cmd *cmd);
|
||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);
|
||||
|
||||
#endif // BUILTINS_H
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */
|
||||
/* Updated: 2025/04/01 14:00:15 by khais ### ########.fr */
|
||||
/* Updated: 2025/04/01 16:37:31 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -39,34 +39,6 @@ static void command_not_found(t_simple_cmd *cmd)
|
|||
cmd->words->word->word);
|
||||
}
|
||||
|
||||
static t_builtin_type get_builtin(t_simple_cmd *cmd)
|
||||
{
|
||||
char *word;
|
||||
|
||||
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);
|
||||
return (BUILTIN_INVALID);
|
||||
}
|
||||
|
||||
static t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
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,
|
||||
};
|
||||
|
||||
type = get_builtin(cmd);
|
||||
return (builtins[type](cmd, app));
|
||||
}
|
||||
|
||||
void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app)
|
||||
{
|
||||
char *exe;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue