split functions cleanup init and free process

This commit is contained in:
whaffman 2025-02-05 16:24:17 +01:00
parent 55ba622714
commit f64e1f055d
16 changed files with 255 additions and 93 deletions

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))
@ -31,7 +31,7 @@ DEPENDS = ${OBJECTS:.o=.d}
CC = cc CC = cc
RM = rm -rf RM = rm -rf
INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH)
CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD CFLAGS = -Wall -Wextra -Werror -fsanitize=address,undefined -MMD
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)

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:06:21 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
@ -37,6 +37,4 @@
# 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 16:05:44 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,8 +15,7 @@
# 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);
#endif // PROMPT_H #endif // PROMPT_H

View File

@ -6,27 +6,13 @@
/* 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef TYPEDEF_H #ifndef TYPEDEF_H
# define TYPEDEF_H # define TYPEDEF_H
typedef struct s_enviroment
{
char *name;
char *value;
struct s_enviroment *next;
} t_enviroment;
typedef struct s_minishell
{
t_enviroment *enviroment;
} t_minishell;
typedef enum e_token_type typedef enum e_token_type
{ {
T_WORD, T_WORD,
@ -38,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;
@ -53,4 +46,11 @@ typedef struct s_lexer
char current_char; char current_char;
} t_lexer; } t_lexer;
typedef struct s_minishell
{
t_enviroment *enviroment;
char *line;
t_lexer *lexer;
t_list *tokens;
} 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

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,70 +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 16:23:08 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "prompt.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);
}
void print_list(void *content) int main(int argc, char **argv, char **envp)
{ {
t_token *token; t_minishell *minishell;
token = (t_token *)content;
ft_printf("%s\n", token->value);
}
int main(int argc, char **argv, char **envp)
{
t_enviroment *enviroment;
char *line;
t_lexer *lexer;
t_list *list;
t_minishell *minishell;
(void)argc; (void)argc;
(void)argv; (void)argv;
print_banner(); print_banner();
minishell = malloc(sizeof(t_minishell)); minishell = init_minishell();
if (!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);
lexer = ft_lexer_new(line); simple_builtins(minishell);
list = ft_parse_input(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(lexer);
} }
ft_lstclear(&list, ft_clear_tokenlist); free_minishell(minishell);
ft_lexer_free(lexer);
// print_enviroment(enviroment);
free_enviroment(enviroment);
free(minishell);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

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 16:18:18 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,7 @@ 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);
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);
}
}