feat: builtin router, checker and buitlin stubs

This commit is contained in:
whaffman 2025-02-20 11:43:15 +01:00
parent 12c439afb0
commit d289065ade
15 changed files with 256 additions and 14 deletions

View File

@ -6,7 +6,7 @@
# By: qmennen <qmennen@student.codam.nl> +#+ # # By: qmennen <qmennen@student.codam.nl> +#+ #
# +#+ # # +#+ #
# Created: 2024/10/15 11:48:46 by whaffman #+# #+# # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# #
# Updated: 2025/02/19 17:43:13 by whaffman ######## odam.nl # # Updated: 2025/02/20 11:10:42 by whaffman ######## odam.nl #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -22,7 +22,8 @@ LIBFT = $(LIBFT_PATH)/libft.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:src/executor:src/parser:src/expander:src/debug:src/signal VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:
VPATH += src/executor:src/parser:src/expander:src/debug:src/signal:src/builtin
SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

27
inc/builtin.h Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin.h :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:09:02 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:58 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#ifndef BUILTIN_H
# define BUILTIN_H
# include "minishell.h"
int is_builtin(char *cmd);
int builtin_router(t_minishell *minishell, t_command *cmd);
int builtin_echo(t_minishell *minishell, t_command *cmd);
int builtin_cd(t_minishell *minishell, t_command *cmd);
int builtin_pwd(t_minishell *minishell, t_command *cmd);
int builtin_export(t_minishell *minishell, t_command *cmd);
int builtin_unset(t_minishell *minishell, t_command *cmd);
int builtin_env(t_minishell *minishell, t_command *cmd);
int builtin_exit(t_minishell *minishell, t_command *cmd);
#endif // BUILTIN_H

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:13 by whaffman #+# #+# */
/* Updated: 2025/02/19 14:46:53 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 11:35:24 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,6 +20,7 @@
# include "environment.h" # include "environment.h"
# include "prompt.h" # include "prompt.h"
# include "tokenizer.h" # include "tokenizer.h"
# include "builtin.h"
# include "executor.h" # include "executor.h"
# include "parser.h" # include "parser.h"
# include "expander.h" # include "expander.h"

View File

@ -6,13 +6,15 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */ /* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
/* Updated: 2025/02/18 14:34:39 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 11:37:00 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef TYPEDEF_H #ifndef TYPEDEF_H
# define TYPEDEF_H # define TYPEDEF_H
typedef enum e_token_type typedef enum e_token_type
{ {
T_WORD, T_WORD,
@ -69,4 +71,6 @@ typedef struct s_minishell
t_list *tokens; t_list *tokens;
t_list *commands; t_list *commands;
} t_minishell; } t_minishell;
typedef int (*t_builtin_fn)(t_minishell *, t_command *);
#endif // TYPEDEF_H #endif // TYPEDEF_H

20
src/builtin/builtin_cd.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_cd.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:33:07 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:03 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_cd(t_minishell *minishell, t_command *cmd)
{
(void)minishell;
(void)cmd;
return (SUCCESS);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_echo.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:33:05 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:10 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_echo(t_minishell *minishell, t_command *cmd)
{
(void)minishell;
(void)cmd;
return (SUCCESS);
}

20
src/builtin/builtin_env.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_env.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:33:02 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:17 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_env(t_minishell *minishell, t_command *cmd)
{
(void)minishell;
(void)cmd;
return (SUCCESS);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_exit.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:25 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_exit(t_minishell *minishell, t_command *cmd)
{
(void)minishell;
(void)cmd;
return (SUCCESS);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_export.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:32 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_export(t_minishell *minishell, t_command *cmd)
{
(void)minishell;
(void)cmd;
return (SUCCESS);
}

20
src/builtin/builtin_pwd.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_pwd.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:38:37 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_pwd(t_minishell *minishell, t_command *cmd)
{
(void)minishell;
(void)cmd;
return (SUCCESS);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_router.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:12:38 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:41:28 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_router(t_minishell *minishell, t_command *cmd)
{
const t_builtin_fn builtin_fn[] = {
builtin_echo,
builtin_cd,
builtin_pwd,
builtin_export,
builtin_unset,
builtin_env,
builtin_exit};
if (!is_builtin(cmd->args[0]))
return (FALSE);
builtin_fn[is_builtin(cmd->args[0])](minishell, cmd);
return (TRUE);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_unset.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:25:43 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:31:25 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_unset(t_minishell *minishell, t_command *cmd)
{
int i;
i = 1;
while (cmd->args[i] != NULL)
{
if (environment_get(minishell->environment, cmd->args[i]) != NULL)
environment_del(&(minishell->environment), cmd->args[i]);
i++;
}
return (SUCCESS);
}

29
src/builtin/is_builtin.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* is_builtin.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */
/* Updated: 2025/02/20 11:12:25 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int is_builtin(char *cmd)
{
const char *builtins[] = {"echo", "cd", "pwd", "export",
"unset", "env", "exit", NULL};
int i;
i = 0;
while (builtins[i])
{
if (ft_strcmp(cmd, builtins[i]) == 0)
return (1);
i++;
}
return (FALSE);
}

View File

@ -6,13 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */ /* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
/* Updated: 2025/02/18 15:49:36 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 11:42:29 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void builtin_export(t_minishell *minishell) void builtin_export_simple(t_minishell *minishell)
{ {
t_list *tmp; t_list *tmp;
t_environment *env; t_environment *env;
@ -93,7 +93,7 @@ void simple_builtins(t_minishell *minishell)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else if (cmp_value(minishell->tokens, "export")) else if (cmp_value(minishell->tokens, "export"))
builtin_export(minishell); builtin_export_simple(minishell);
else if (cmp_value(minishell->tokens, "unset")) else if (cmp_value(minishell->tokens, "unset"))
{ {
environment_del(&(minishell->environment), environment_del(&(minishell->environment),

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* :::::::: */
/* lexer_token_next.c :+: :+: :+: */ /* lexer_token_next.c :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+ */
/* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ /* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */
/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ /* Updated: 2025/02/20 10:39:50 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -68,6 +68,9 @@ t_token *ft_token_next(t_lexer *lexer)
free(word); free(word);
} }
else else
{
token = token_new(T_ERROR, NULL, current_pos); token = token_new(T_ERROR, NULL, current_pos);
printf("token->type: %d\n", token->type);
}
return (token); return (token);
} }