Merge branch 'main' into quinten

This commit is contained in:
Quinten Mennen 2025-02-05 18:59:35 +01:00
commit 8f9c2e33e8
22 changed files with 322 additions and 97 deletions

2
.gitignore vendored
View File

@ -3,4 +3,4 @@ a.out
*.d *.d
*.o *.o
obj/ obj/
.vscode .minishell_history

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"minishell.h": "c"
}
}

View File

@ -1,12 +1,12 @@
# **************************************************************************** # # **************************************************************************** #
# # # #
# ::: :::::::: # # :::::::: #
# Makefile :+: :+: :+: # # Makefile :+: :+: #
# +:+ +:+ +:+ # # +:+ #
# 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/04 16:57:05 by qmennen ### ########.fr # # Updated: 2025/02/05 16:02:39 by whaffman ######## odam.nl #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src:src/enviroment:src/prompt:src/tokenizer VPATH = src:src/enviroment:src/prompt:src/tokenizer:src/utils
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))

View File

@ -6,20 +6,20 @@
/* 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/05 12:37:14 by whaffman ######## odam.nl */ /* Updated: 2025/02/05 16:28:59 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MINISHELL_H #ifndef MINISHELL_H
# define MINISHELL_H # define MINISHELL_H
# include "typedef.h"
# include "allowed.h" # include "allowed.h"
# include "libft.h" # include "libft.h"
# include "typedef.h"
# include "enviroment.h" # include "enviroment.h"
# include "prompt.h" # include "prompt.h"
# include "tokenizer.h" # include "tokenizer.h"
# include "utils.h"
# define TRUE 1 # define TRUE 1
# define FALSE 0 # define FALSE 0
@ -36,7 +36,4 @@
# define CYAN "\001\033[0;36m\002" # define CYAN "\001\033[0;36m\002"
# define RESET "\001\033[0m\002" # define RESET "\001\033[0m\002"
int ft_isspace(const char c);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:35:35 by whaffman #+# #+# */ /* Created: 2025/02/04 16:35:35 by whaffman #+# #+# */
/* Updated: 2025/02/05 11:54:38 by whaffman ######## odam.nl */ /* Updated: 2025/02/05 17:05:32 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,8 +15,9 @@
# include "minishell.h" # include "minishell.h"
typedef struct s_minishell t_minishell; // Forward declaration WHY? void print_banner(void);
char *ft_prompt(t_minishell *minishell); char *ft_prompt(t_minishell *minishell);
void ft_write_history(char *line);
void ft_load_history(void);
#endif // PROMPT_H #endif // PROMPT_H

View File

@ -6,7 +6,7 @@
/* 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/05 12:36:44 by whaffman ######## odam.nl */ /* Updated: 2025/02/05 15:25:40 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,13 @@ typedef enum e_token_type
T_ERROR T_ERROR
} t_token_type; } t_token_type;
typedef struct s_enviroment
{
char *name;
char *value;
struct s_enviroment *next;
} t_enviroment;
typedef struct s_token typedef struct s_token
{ {
t_token_type type; t_token_type type;
@ -39,18 +46,11 @@ typedef struct s_lexer
char current_char; char current_char;
} t_lexer; } t_lexer;
typedef struct s_enviroment
{
char *name;
char *value;
struct s_enviroment *next;
} t_enviroment;
typedef struct s_minishell typedef struct s_minishell
{ {
t_enviroment *enviroment; t_enviroment *enviroment;
char *line;
t_lexer *lexer; t_lexer *lexer;
t_list *tokens;
} t_minishell; } t_minishell;
#endif // TYPEDEF_H #endif // TYPEDEF_H

24
inc/utils.h Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* utils.h :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:06:35 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:23:21 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#ifndef UTILS_H
# define UTILS_H
void free_minishell_line(t_minishell *minishell);
void free_minishell(t_minishell *minishell);
t_minishell *init_minishell(void);
void print_banner(void);
void print_list(void *content);
int cmp_value(t_list *list, char *str);
void simple_builtins(t_minishell *minishell);
#endif // UTILS_H

@ -1 +1 @@
Subproject commit d76fa9350d07e61e0b2524a411e8aca5151381e4 Subproject commit 8072c13f1e064dabf7249e2f148aa6111660dab0

View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse_enviroment.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 15:52:33 by whaffman #+# #+# */
/* Updated: 2025/02/05 15:52:34 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int parse_enviroment(char **envp, t_enviroment **enviroment) int parse_enviroment(char **envp, t_enviroment **enviroment)
@ -5,7 +17,6 @@ int parse_enviroment(char **envp, t_enviroment **enviroment)
char **env; char **env;
*enviroment = NULL; *enviroment = NULL;
if (envp == NULL) if (envp == NULL)
return (FAILURE); return (FAILURE);
while (*envp != NULL) while (*envp != NULL)

View File

@ -6,13 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:04 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:04 by whaffman #+# #+# */
/* Updated: 2025/02/04 16:15:52 by whaffman ######## odam.nl */ /* Updated: 2025/02/05 15:52:44 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void print_enviroment(t_enviroment *enviroment) void print_enviroment(t_enviroment *enviroment)
{ {
while (enviroment != NULL) while (enviroment != NULL)
{ {

View File

@ -6,75 +6,31 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ /* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */
/* Updated: 2025/02/05 11:59:23 by whaffman ######## odam.nl */ /* Updated: 2025/02/05 17:06:47 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "prompt.h"
#include <stdio.h>
void print_banner(void) int main(int argc, char **argv, char **envp)
{ {
ft_printf("%s\n", BOLD RED "__ ________________________________________________________________ __" GREEN); t_minishell *minishell;
ft_printf("%s\n", RED "__" GREEN " _______ _____ __ _ _____ _______ _ _ _______ " RED "__");
ft_printf("%s\n", RED "__" GREEN " | | | | | \\ | | |______ |_____| |______ | | "RED "__");
ft_printf("%s\n", RED "__" GREEN " | | | __|__ | \\_| __|__ ______| | | |______ |_____ |_____ "RED "__");
ft_printf("%s\n", RED "__ ________________________________________________________________ __\n" RESET);
}
void print_list(void *content)
{
t_token *token;
token = (t_token *)content;
if (token->type == T_ERROR)
{
printf("An error occurred near position %i\n", token->position);
}
else
ft_printf("%s\n", token->value);
}
int main(int argc, char **argv, char **envp)
{
t_enviroment *enviroment;
char *line;
t_list *list;
t_minishell *minishell;
(void)argc; (void)argc;
(void)argv; (void)argv;
print_banner(); print_banner();
minishell = malloc(sizeof(t_minishell)); ft_load_history();
if (!minishell) minishell = init_minishell();
{ parse_enviroment(envp, &(minishell->enviroment));
perror("failed assigning minishell memory");
exit(EXIT_FAILURE);
}
parse_enviroment(envp, &enviroment);
minishell->enviroment = enviroment;
while (TRUE) while (TRUE)
{ {
line = ft_prompt(minishell); minishell->line = ft_prompt(minishell);
if (line != NULL) minishell->lexer = ft_lexer_new(minishell->line);
add_history(line); minishell->tokens = ft_parse_input(minishell->lexer);
minishell->lexer = ft_lexer_new(line); simple_builtins(minishell);
list = ft_parse_input(minishell->lexer); ft_lstiter(minishell->tokens, print_list);
ft_lstiter(list, print_list); free_minishell_line(minishell);
free(line);
if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "clear\0", 6) == 0)
printf("\033[2J\033[1;1H");
else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "env\0", 4) == 0)
print_enviroment(enviroment);
else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0)
break;
ft_lstclear(&list, ft_clear_tokenlist);
ft_lexer_free(minishell->lexer);
} }
ft_lstclear(&list, ft_clear_tokenlist); free_minishell(minishell);
ft_lexer_free(minishell->lexer);
// print_enviroment(enviroment);
free_enviroment(enviroment);
free(minishell);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_load_history.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:44:51 by whaffman #+# #+# */
/* Updated: 2025/02/05 17:11:59 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_load_history(void)
{
int fd;
char *line;
fd = open(".minishell_history", O_RDONLY);
if (fd < 0)
return ;
while (TRUE)
{
line = get_next_line(fd);
if (!line)
return ;
line[ft_strlen(line) - 1] = '\0';
if (*line)
add_history(line);
free(line);
}
close(fd);
}

View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_write_history.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 17:12:17 by whaffman #+# #+# */
/* Updated: 2025/02/05 17:12:23 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_write_history(char *line)
{
int fd;
fd = open(".minishell_history", O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd < 0)
return ;
if (*line)
ft_putendl_fd(line, fd);
close(fd);
}

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */ /* Created: 2025/02/04 16:13:08 by whaffman #+# #+# */
/* Updated: 2025/02/05 11:54:12 by whaffman ######## odam.nl */ /* Updated: 2025/02/05 17:05:12 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,14 +26,11 @@ char *get_user(t_enviroment *enviroment)
free(str1); free(str1);
str1 = ft_strjoin(str2, RESET "@" GREEN "minishell" RESET ": "); str1 = ft_strjoin(str2, RESET "@" GREEN "minishell" RESET ": ");
free(str2); free(str2);
return (str1); return (str1);
} }
char *get_path(t_enviroment *enviroment) char *get_path(t_enviroment *enviroment)
{ {
char *home; char *home;
char *temp; char *temp;
char *cwd; char *cwd;
@ -68,9 +65,7 @@ char *ft_prompt(t_minishell *minishell)
cwd = get_path(minishell->enviroment); cwd = get_path(minishell->enviroment);
if (cwd == NULL) if (cwd == NULL)
{
return (NULL); return (NULL);
}
user = get_user(minishell->enviroment); user = get_user(minishell->enviroment);
temp = ft_strjoin(user, cwd); temp = ft_strjoin(user, cwd);
free(user); free(user);
@ -79,6 +74,10 @@ char *ft_prompt(t_minishell *minishell)
free(temp); free(temp);
line = readline(prompt); line = readline(prompt);
free(prompt); free(prompt);
if (line != NULL)
{
add_history(line);
ft_write_history(line);
}
return (line); return (line);
} }

24
src/utils/cmp_value.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* cmp_value.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:13:19 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:14:51 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmp_value(t_list *list, char *str)
{
if (list != NULL
&& ft_strncmp(
((t_token *)list->content)->value,
str,
ft_strlen(str) + 1) == 0)
return (TRUE);
return (FALSE);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* free_minishell.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:03:57 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:04:18 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_minishell(t_minishell *minishell)
{
if (minishell->line)
free_minishell_line(minishell);
if (minishell->enviroment)
free_enviroment(minishell->enviroment);
free(minishell);
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* free_minishell_line.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:01:44 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:02:07 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void free_minishell_line(t_minishell *minishell)
{
if (minishell->line)
free(minishell->line);
if (minishell->lexer)
ft_lexer_free(minishell->lexer);
if (minishell->tokens)
ft_lstclear(&minishell->tokens, ft_clear_tokenlist);
}

View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* init_minishell.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:03:03 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:03:15 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_minishell *init_minishell(void)
{
t_minishell *minishell;
minishell = malloc(sizeof(t_minishell));
if (!minishell)
{
perror("failed assigning minishell memory");
exit(EXIT_FAILURE);
}
minishell->enviroment = NULL;
minishell->line = NULL;
minishell->lexer = NULL;
minishell->tokens = NULL;
return (minishell);
}

27
src/utils/print_banner.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_banner.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:04:44 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:05:07 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_banner(void)
{
ft_printf("%s\n", BOLD RED "__ ___________________________"
"_____________________________________ __" GREEN);
ft_printf("%s\n", RED "__" GREEN " _______ _____ __ _ __"
"___ _______ _ _ _______ " RED "__");
ft_printf("%s\n", RED "__" GREEN " | | | | | \\ | "
" | |______ |_____| |______ | | "RED "__");
ft_printf("%s\n", RED "__" GREEN " | | | __|__ | \\_| _"
"_|__ ______| | | |______ |_____ |_____ "RED "__");
ft_printf("%s\n", RED "__ ________________________________"
"________________________________ __\n" RESET);
}

21
src/utils/print_list.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* print_list.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:12:14 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:12:45 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_list(void *content)
{
t_token *token;
token = (t_token *)content;
ft_printf("%s\n", token->value);
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* simple_builtins.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 16:21:39 by whaffman #+# #+# */
/* Updated: 2025/02/05 16:22:47 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
void simple_builtins(t_minishell *minishell)
{
if (cmp_value(minishell->tokens, "clear"))
printf("\033[2J\033[1;1H");
else if (cmp_value(minishell->tokens, "env"))
print_enviroment(minishell->enviroment);
else if (cmp_value(minishell->tokens, "exit"))
{
free_minishell(minishell);
exit(EXIT_SUCCESS);
}
}