Skip to content

Commit

Permalink
refactoring folder structure! next will be functions and some basic f…
Browse files Browse the repository at this point in the history
…ormatting
  • Loading branch information
jackoske committed Jul 25, 2024
1 parent 0e4c53f commit 59fabb5
Show file tree
Hide file tree
Showing 80 changed files with 194 additions and 165 deletions.
42 changes: 27 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ #
# By: Jskehan <jskehan@student.42berlin.de> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/05/14 15:15:48 by iverniho #+# #+# #
# Updated: 2024/07/18 18:08:18 by Jskehan ### ########.fr #
# Updated: 2024/07/25 16:02:24 by Jskehan ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -26,25 +26,37 @@ OBJ_DIR = ./obj/
READLINE = -lreadline

SRC_DIR = ./src/main.c
SRC_TESTER_DIR = ./src/tester/
SRC_BUILTIN_DIR = ./src/builtin/
SRC_ENV_DIR = ./src/env/
SRC_EXEC_DIR = ./src/exec/
SRC_INIT_DIR = ./src/init/
SRC_PARS_DIR = ./src/parser/
SRC_EXEC_DIR = ./src/execution/
SRC_BUILTINS_DIR = ./src/builtins/


SRC_PARS = parser.c prompt.c parser_utils.c expand_vars.c tokenization.c \
set_redir_utils.c
SRC_EXEC = execution.c redirection.c here_doc.c error.c signal.c node_utils.c \
builtin_cd.c builtin_functions.c builtin_utils.c path_resolution.c envp_functions.c

SRC_REDIRECT_DIR = ./src/redirect/
SRC_SIGNAL_DIR = ./src/signal/
SRC_TESTER_DIR = ./src/tester/
SRC_UTILS_DIR = ./src/utils/

SRC_BUILTIN = builtin_cd.c builtin_functions.c builtin_utils.c
SRC_ENV = envp_functions.c path_resolution.c
SRC_EXEC = execution.c
SRC_INIT = initilisation.c
SRC_PARS = parser.c prompt.c parser_utils.c expand_vars.c tokenization.c set_redir_utils.c
SRC_REDIRECT = here_doc.c redirection.c
SRC_SIGNAL = signal.c
SRC_TESTER = libft_extra_tests.c execution_tests.c node_tester.c
SRC_BUILTINS = # cd.c echo.c env.c exit.c export.c pwd.c unset.c
SRC_UTILS = error.c node_utils.c utils.c

SRC = $(SRC_DIR) \
$(SRC_GNL) \
$(addprefix $(SRC_PARS_DIR), $(SRC_PARS)) \
$(addprefix $(SRC_BUILTIN_DIR), $(SRC_BUILTIN)) \
$(addprefix $(SRC_ENV_DIR), $(SRC_ENV)) \
$(addprefix $(SRC_EXEC_DIR), $(SRC_EXEC)) \
$(addprefix $(SRC_INIT_DIR), $(SRC_INIT)) \
$(addprefix $(SRC_PARS_DIR), $(SRC_PARS)) \
$(addprefix $(SRC_REDIRECT_DIR), $(SRC_REDIRECT)) \
$(addprefix $(SRC_TESTER_DIR), $(SRC_TESTER)) \
$(addprefix $(SRC_BUILTINS_DIR), $(SRC_BUILTINS))
$(addprefix $(SRC_SIGNAL_DIR), $(SRC_SIGNAL)) \
$(addprefix $(SRC_UTILS_DIR), $(SRC_UTILS))

OBJ = $(patsubst %.c,$(OBJ_DIR)%.o,$(SRC))

Expand Down
14 changes: 8 additions & 6 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ */
/* By: Jskehan <jskehan@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 15:11:41 by iverniho #+# #+# */
/* Updated: 2024/07/19 16:14:47 by Jskehan ### ########.fr */
/* Updated: 2024/07/25 16:03:18 by Jskehan ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,13 +16,13 @@
# include "../lib/gnl/get_next_line.h"
# include "../lib/libft/includes/libft.h"
# include <dirent.h>
# include <errno.h>
# include <limits.h>
# include <readline/history.h>
# include <readline/readline.h>
# include <signal.h>
# include <stdio.h>
# include <stdlib.h>
# include <errno.h>
# include <limits.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <unistd.h>
Expand All @@ -38,7 +38,7 @@
# define WRITE_END 1

extern volatile sig_atomic_t g_sigint_received;
extern volatile sig_atomic_t g_is_executing_command;
extern volatile sig_atomic_t g_is_executing_command;

# define MINISHELL_ASCII \
"\
Expand Down Expand Up @@ -94,6 +94,7 @@ t_cmd *init_cmd(t_mini *mini);
void setup_signal_handlers(void);
void setup_child_signals(void);
void handle_sigint(int sig);
void initialize_envp(t_mini **mini, char **envp);

/* Error Handling */
void *mini_perror(char *str, char *str2, int fd);
Expand Down Expand Up @@ -140,12 +141,13 @@ int get_here_doc(t_mini *mini, const char *limit,
void libft_extra_tester(void);
void print_nodes(t_list *node);
void test_exec(void);
void test_heredoc(void);
void test_heredoc(void);
void debug_print_command_parts(t_cmd *cmd);
void debug_tokenized_input(char **tokenized_input);

char *ft_getenv(const char *name, char **envp,
int len);
char **ft_setenv(const char *name, const char *value,
char **envp, int overwrite);
char **copy_env(char **envp);
#endif // MINISHELL_H
Binary file added lib/libft/libft.a
Binary file not shown.
Binary file added lib/libft/obj/src/ft_atoi.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_bzero.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_calloc.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_isalnum.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_isalpha.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_isascii.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_isdigit.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_isprint.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_itoa.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstadd_back.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstadd_front.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstclear.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstdelone.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstiter.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstlast.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstmap.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstnew.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_lstsize.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_memchr.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_memcmp.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_memcpy.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_memmove.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_memset.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_putchar_fd.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_putendl_fd.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_putnbr_fd.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_putstr_fd.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_realloc.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_split.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strchr.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strdup.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_striteri.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strjoin.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strlcat.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strlcpy.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strlen.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strmapi.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strncmp.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strnstr.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strrchr.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_strtrim.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_substr.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_tolower.o
Binary file not shown.
Binary file added lib/libft/obj/src/ft_toupper.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_1st_char_in_set_i.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_2d_array_len.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_add_row_2d_array.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_add_row_2d_array_i.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_count_char.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_duplicate_2d_array.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_free_2d_array.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_is_only_special.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_is_space.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_is_special_symbol.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_print_2d_array_fd.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_realloc_2d_array.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_splice_2d_array.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_strchr_i.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_strcmp.o
Binary file not shown.
Binary file added lib/libft/obj/src_extra/ft_strjoin_free.o
Binary file not shown.
Binary file added minishell
Binary file not shown.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/execution/builtin_utils.c → src/builtin/builtin_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* builtin_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ */
/* By: Jskehan <jskehan@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 10:43:33 by Jskehan #+# #+# */
/* Updated: 2024/07/24 16:29:11 by iverniho ### ########.fr */
/* Updated: 2024/07/25 15:07:38 by Jskehan ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -59,4 +59,4 @@ void execute_builtin(t_mini *mini, t_cmd *cmd)
// else if (ft_strcmp(cmd->full_command[0], "env") == 0)
// mini_env(mini);
mini->exit_status = 0;
}
}
30 changes: 28 additions & 2 deletions src/execution/envp_functions.c → src/env/envp_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* envp_functions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ */
/* By: Jskehan <jskehan@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/18 15:49:43 by Jskehan #+# #+# */
/* Updated: 2024/07/19 16:40:25 by Jskehan ### ########.fr */
/* Updated: 2024/07/25 15:26:40 by Jskehan ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -78,3 +78,29 @@ char **ft_setenv(const char *name, const char *value, char **envp,
printf("New environment variable added: %s=%s\n", name, value);
return (new_envp_array);
}

char **copy_env(char **envp)
{
int i;
char **new_envp;

for (i = 0; envp[i]; i++)
;
new_envp = malloc((i + 1) * sizeof(char *));
if (!new_envp)
return (NULL);
for (i = 0; envp[i]; i++)
{
new_envp[i] = strdup(envp[i]);
if (!new_envp[i])
{
// Free previously allocated memory in case of failure
while (i--)
free(new_envp[i]);
free(new_envp);
return (NULL);
}
}
new_envp[i] = NULL;
return (new_envp);
}
File renamed without changes.
6 changes: 3 additions & 3 deletions src/execution/execution.c → src/exec/execution.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ */
/* By: Jskehan <jskehan@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 10:52:13 by Jskehan #+# #+# */
/* Updated: 2024/07/24 13:35:17 by iverniho ### ########.fr */
/* Updated: 2024/07/25 15:08:07 by Jskehan ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -137,4 +137,4 @@ void *check_to_fork(t_mini *mini, t_list *command)
else
mini->exit_status = 127; // Command not found or not executable
return ("");
}
}
77 changes: 77 additions & 0 deletions src/init/initilisation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* initilisation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/25 15:18:28 by Jskehan #+# #+# */
/* Updated: 2024/07/25 16:03:26 by Jskehan ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"


void initialize_envp(t_mini **mini, char **envp)
{
char cwd[PATH_MAX];
char *shlvl_str;
int shlvl;

*mini = malloc(sizeof(t_mini));
if (!*mini)
{
perror("Failed to allocate t_mini structure");
exit(EXIT_FAILURE);
}
(*mini)->envp = copy_env(envp);
if (!(*mini)->envp)
{
// Handle memory allocation failure
perror("Failed to initialize environment variables");
exit(EXIT_FAILURE);
}
// Set any additional required environment variables
if (getcwd(cwd, sizeof(cwd)))
{
(*mini)->envp = ft_setenv("PWD", cwd, (*mini)->envp, 1);
if (!(*mini)->envp)
{
perror("Failed to set PWD environment variable");
exit(EXIT_FAILURE);
}
}
(*mini)->envp = ft_setenv("SHELL", "jackoshell", (*mini)->envp, 1);

// Handle SHLVL
shlvl_str = ft_getenv("SHLVL", (*mini)->envp, strlen("SHLVL"));
if (shlvl_str)
{
shlvl = atoi(shlvl_str) + 1;
}
else
{
shlvl = 1;
}
char shlvl_value[12];
snprintf(shlvl_value, sizeof(shlvl_value), "%d", shlvl);
(*mini)->envp = ft_setenv("SHLVL", shlvl_value, (*mini)->envp, 1);
}

t_cmd *init_cmd(t_mini *mini)
{
t_cmd *node;

node = malloc(sizeof(t_cmd));
if (!node)
return (NULL);
node->full_command = NULL;
node->command_path = NULL;
node->fd_in = 0;
node->fd_out = 1;
node->is_heredoc = 0;
node->is_append = 0;
node->mini = mini;
return (node);
}
93 changes: 2 additions & 91 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,104 +3,15 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ */
/* By: Jskehan <jskehan@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 20:09:06 by Jskehan #+# #+# */
/* Updated: 2024/07/19 13:46:45 by Jskehan ### ########.fr */
/* Updated: 2024/07/25 16:03:23 by Jskehan ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

char **copy_env(char **envp)
{
int i;
char **new_envp;

for (i = 0; envp[i]; i++)
;
new_envp = malloc((i + 1) * sizeof(char *));
if (!new_envp)
return (NULL);
for (i = 0; envp[i]; i++)
{
new_envp[i] = strdup(envp[i]);
if (!new_envp[i])
{
// Free previously allocated memory in case of failure
while (i--)
free(new_envp[i]);
free(new_envp);
return (NULL);
}
}
new_envp[i] = NULL;
return (new_envp);
}

void initialize_envp(t_mini **mini, char **envp)
{
char cwd[PATH_MAX];
char *shlvl_str;
int shlvl;

*mini = malloc(sizeof(t_mini));
if (!*mini)
{
perror("Failed to allocate t_mini structure");
exit(EXIT_FAILURE);
}
(*mini)->envp = copy_env(envp);
if (!(*mini)->envp)
{
// Handle memory allocation failure
perror("Failed to initialize environment variables");
exit(EXIT_FAILURE);
}
// Set any additional required environment variables
if (getcwd(cwd, sizeof(cwd)))
{
(*mini)->envp = ft_setenv("PWD", cwd, (*mini)->envp, 1);
if (!(*mini)->envp)
{
perror("Failed to set PWD environment variable");
exit(EXIT_FAILURE);
}
}
(*mini)->envp = ft_setenv("SHELL", "jackoshell", (*mini)->envp, 1);

// Handle SHLVL
shlvl_str = ft_getenv("SHLVL", (*mini)->envp, strlen("SHLVL"));
if (shlvl_str)
{
shlvl = atoi(shlvl_str) + 1;
}
else
{
shlvl = 1;
}
char shlvl_value[12];
snprintf(shlvl_value, sizeof(shlvl_value), "%d", shlvl);
(*mini)->envp = ft_setenv("SHLVL", shlvl_value, (*mini)->envp, 1);
}

t_cmd *init_cmd(t_mini *mini)
{
t_cmd *node;

node = malloc(sizeof(t_cmd));
if (!node)
return (NULL);
node->full_command = NULL;
node->command_path = NULL;
node->fd_in = 0;
node->fd_out = 1;
node->is_heredoc = 0;
node->is_append = 0;
node->mini = mini;
return (node);
}

int main(int argc, char **argv, char **envp)
{
t_mini *mini;
Expand Down
Loading

0 comments on commit 59fabb5

Please sign in to comment.