Skip to content

Commit

Permalink
Merge pull request #94 from LeaYeh/feat-executor-expansion
Browse files Browse the repository at this point in the history
[FEAT] Apply expansion during executing command
  • Loading branch information
itislu authored Jan 20, 2024
2 parents 1a35679 + 23a8705 commit ff352d6
Show file tree
Hide file tree
Showing 38 changed files with 505 additions and 320 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: ldulling <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/23 03:22:46 by ldulling #+# #+# #
# Updated: 2024/01/06 13:17:59 by ldulling ### ########.fr #
# Updated: 2024/01/18 23:33:23 by ldulling ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -46,7 +46,7 @@ MAKEFLAGS := -j -s
# Files

# TODO: need to remove forbidden wildcard
SRC := $(wildcard source/*.c source/*/*.c source/*/*/*.c tests/*.c)
SRC := $(wildcard source/*.c source/*/*.c source/*/*/*.c source/*/*/*/*.c tests/*.c)
OBJ := $(SRC:%.c=$(OBJ_DIR)/%.o)
DEP := $(SRC:%.c=$(DEP_DIR)/%.d)

Expand Down Expand Up @@ -148,7 +148,7 @@ endif

# **************************** CUSTOM MESSAGES ******************************* #

MSG_START := "\e[3mBuilding \e[1;34mCrash \e[0m"
MSG_START := "\e[3mBuilding \e[1;34m🌊rash \e[0;3m... \e[0m"
MSG_PROGRESS := "\e[3m🌊\e[0m"
MSG_SUCCESS := "\e[1;3;36m\nDONE!\n\e[0m"
MSG_NO_CHNG := "\e[3;37mEverything up-to-date!\n\e[0m"
Expand All @@ -167,3 +167,4 @@ print-% :

# test without env value: env -i
# detect memory leak: valgrind -s --leak-check=full --show-leak-kinds=all --suppressions=./minishell.supp ./minishell
# valgrind -s --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes --suppressions=./minishell.supp ./minishell
26 changes: 15 additions & 11 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* defines.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 15:56:26 by lyeh #+# #+# */
/* Updated: 2024/01/11 20:28:15 by lyeh ### ########.fr */
/* Updated: 2024/01/18 23:46:08 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,11 +26,15 @@
# include "libft.h"
# include "ft_printf.h"

# define PROGRAM_NAME "minishell"
// # define PROGRAM_NAME "🌊rash"
# define PROGRAM_NAME "\e[1;34m🌊rash\e[0m"

/* Error codes */
# define SUCCESS 0
# define EXIT_SUCCESS 0
# define GENERAL_ERROR 1
# define BAD_SUBSTITUTION 2
// # define EXPAND_ERROR 2
# define MISUSE_BUILTIN 2
# define CMD_EXEC_FAILED 126
# define CMD_NOT_FOUND 127
Expand Down Expand Up @@ -68,9 +72,9 @@
# define STY_RES "\e[0m"

// TODO: Remove the color codes from the prompt before the evaluations
// # define PROMPT "\033[0;34m🌊>\033[0m"
# define PROMPT "\033[1;32mminishell$ \033[0m"
# define HEREDOC_PROMPT "\033[1;37m> \033[0m"
# define PROMPT "\e[1;34m🌊rash$ \e[0m"
// # define PROMPT "\e[1;32mminishell$ \e[0m"
# define HEREDOC_PROMPT "\e[1;37m> \e[0m"
// # define PROMPT "\001\033[1;32m\002minishell$ \001\033[0m\002"
// # define PROMPT "minishell$ "

Expand All @@ -90,7 +94,7 @@
# define ERROR_HEREDOC_UNEXPECTED_EOF \
"%s: warning: here-document delimited by end-of-file (wanted `%s')\n"
# define ERROR_EXPANDER_BAD_SUBSTITUTION \
"%s: bad substitution\n"
"%s: %s: bad substitution\n"

// TODO: Replace with OS error message
# define ERROR_REMOVE_FILE "%s: \
Expand Down Expand Up @@ -231,16 +235,16 @@ typedef struct s_cmd_table
int pipe_read_fd;
int pipe_write_fd;
int type;
char *cmd_name;
t_list *cmd_args;
t_list *simple_cmd_list;
t_list *assignment_list;
t_list *io_red_list;
} t_cmd_table;

typedef struct s_final_cmd_table
{
char *cmd_name;
char **cmd_args;
char **simple_cmd;
char *exec_path;
char **assignment_array;
char **envp;
} t_final_cmd_table;

Expand Down
13 changes: 8 additions & 5 deletions include/executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* executor.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/26 15:05:16 by lyeh #+# #+# */
/* Updated: 2024/01/11 21:14:13 by lyeh ### ########.fr */
/* Updated: 2024/01/20 01:27:52 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,13 +21,16 @@ bool ft_heredoc(t_list_d *cmd_table_list);

void handle_process(t_shell *shell, t_list_d *cmd_table_node);
void handle_subshell(t_shell *shell, t_list_d **cmd_table_node);
void handle_control_op(t_shell *shell, t_list_d **cmd_table_node);
void handle_control_op(t_shell *shell, t_list_d **cmd_table_node);
void handle_pipeline(t_shell *shell, t_list_d **cmd_table_list);
void handle_simple_cmd(t_shell *shell, t_list_d **cmd_table_list);
void handle_builtin(t_shell *shell, t_list_d **cmd_table_node);
void handle_assignment(t_shell *shell, t_final_cmd_table *final_cmd_table);
void handle_external_cmd(t_shell *shell, t_final_cmd_table *final_cmd_table);
void handle_builtin(t_shell *shell,
t_list_d **cmd_table_node, t_final_cmd_table *final_cmd_table);

/* Error checker */
bool check_executable(char *filename);
bool check_executable(t_shell *shell, char *filename);
bool check_file(char *filename, int o_flag, int permission);

/* Utils */
Expand Down
11 changes: 4 additions & 7 deletions include/expander.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/28 23:25:46 by ldulling #+# #+# */
/* Updated: 2024/01/06 01:11:29 by ldulling ### ########.fr */
/* Updated: 2024/01/18 14:08:52 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,15 +19,13 @@ enum e_is_open_pair_operations
{
GET = 0,
TOGGLE,
RESET
RESET,
CLEAN
};

/* bad_substitution.c */
bool bad_substitution(char *str);

/* create_expanded_list.c */
bool create_expanded_list(t_list **lst, char *dup);

/* expand_special_param.c */
bool expand_exit_code(char **str, size_t *i, int exit_code);

Expand All @@ -37,12 +35,11 @@ size_t count_var_len(char *str);
char *get_replacement(char *var, t_list *env_list);

/* expander.c */
bool ft_expander(char **str, t_list **lst, t_shell *shell);
int ft_expander(char *str, t_list **lst, t_shell *shell);

/* expander_utils.c */
size_t count_offset(char *str);
size_t count_replace_len(char *str);
void free_and_reset(char *dup, char **str);
bool is_open_pair(unsigned char c, int operation);
void skip_to_dollar_not_in_single_quotes(char *str, size_t *i);

Expand Down
13 changes: 9 additions & 4 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/17 13:38:17 by lyeh #+# #+# */
/* Updated: 2024/01/15 16:15:32 by lyeh ### ########.fr */
/* Updated: 2024/01/20 01:19:35 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,7 +43,7 @@ bool append_cmd_table_by_scenario(
t_cmd_table *get_cmd_table_from_list(t_list_d *cmd_table_node);
int get_cmd_table_type_from_list(t_list_d *cmd_table_list);
bool is_control_op_cmd_table(int cmd_table_type);
bool is_builtin(t_cmd_table *cmd_table);
bool is_builtin(char *cmd_name);
bool is_scmd_in_pipeline(t_list_d *cmd_table_node);
void move_past_subshell(t_list_d **cmd_table_node);
void move_past_pipeline(t_list_d **cmd_table_node);
Expand All @@ -55,10 +55,15 @@ void free_final_cmd_table(t_final_cmd_table **final_cmd_table);
t_final_cmd_table *get_final_cmd_table(t_shell *shell,
t_cmd_table *cmd_table);

/* Expansion utils */
int expand_list(t_shell *shell, t_list *list, t_list **expanded_list);
int expand_array(t_shell *shell, char ***array);

/* Array utils */
void free_array(char **array);
void free_array(char ***array);
int get_array_len(char **arr);
char **convert_list_to_string_array(t_list *list);
char **append_string_array(char **array, char *str);

/* Process utils */
void wait_process(t_shell *shell, int pid);
Expand Down
6 changes: 4 additions & 2 deletions libraries/libft/build/libft.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# libft.mk :+: :+: :+: #
# +:+ +:+ +:+ #
# By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ #
# By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/16 13:33:38 by ldulling #+# #+# #
# Updated: 2024/01/06 20:33:25 by lyeh ### ########.fr #
# Updated: 2024/01/18 01:54:20 by ldulling ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -51,6 +51,7 @@ TMP += $(addprefix $(DIR)$(SUBDIR), \
ft_lstclear_d.c \
ft_lstdelone_d.c \
ft_lstlast_d.c \
ft_lstnew_back_d.c \
ft_lstnew_d.c \
)

Expand All @@ -70,6 +71,7 @@ TMP += $(addprefix $(DIR)$(SUBDIR), \
ft_lstpop_front.c \
ft_lstpop_front_content.c \
ft_lstsize.c \
ft_lstsize_non_null.c \
ft_lstsort_bubble.c \
ft_lstswap_head.c \
)
Expand Down
6 changes: 4 additions & 2 deletions libraries/libft/inc/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyeh <lyeh@student.42vienna.com> +#+ +:+ +#+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:17:46 by ldulling #+# #+# */
/* Updated: 2024/01/06 20:33:10 by lyeh ### ########.fr */
/* Updated: 2024/01/18 01:54:09 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -54,6 +54,7 @@ void ft_lstadd_front_d(t_list_d **lst, t_list_d *new);
void ft_lstclear_d(t_list_d **lst, void (*del)(void *));
void ft_lstdelone_d(t_list_d *lst, void (*del)(void *));
t_list_d *ft_lstlast_d(t_list_d *lst);
bool ft_lstnew_back_d(t_list_d **lst, void *content);
t_list_d *ft_lstnew_d(void *content);

\
Expand All @@ -72,6 +73,7 @@ bool ft_lstnew_back(t_list **lst, void *content);
t_list *ft_lstpop_front(t_list **lst);
void *ft_lstpop_front_content(t_list **lst);
int ft_lstsize(t_list *lst);
int ft_lstsize_non_null(t_list *lst);
void ft_lstsort_bubble(t_list **lst, void *(*cmp)(void *, void *));
void ft_lstswap_head(t_list **lst);

Expand Down
35 changes: 35 additions & 0 deletions libraries/libft/src/libft/lists/doubly_linked/ft_lstnew_back_d.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew_back_d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/06 20:30:19 by ldulling #+# #+# */
/* Updated: 2024/01/18 01:13:59 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/**
* The ft_lstnew_back_d function creates a new node with the provided content
* and adds it to the end of the doubly linked list.
*
* @param lst The address of the list to add the new node to.
* @param content The content to be added to the new node.
*
* @return Returns true if the new node was successfully added, false if
* malloc failed.
*
*/
bool ft_lstnew_back_d(t_list_d **lst, void *content)
{
t_list_d *new_node;

new_node = ft_lstnew_d(content);
if (new_node == NULL)
return (false);
ft_lstadd_back_d(lst, new_node);
return (true);
}
4 changes: 2 additions & 2 deletions libraries/libft/src/libft/lists/doubly_linked/ft_lstnew_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:05:19 by ldulling #+# #+# */
/* Updated: 2023/11/27 00:25:51 by ldulling ### ########.fr */
/* Updated: 2024/01/18 04:48:36 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,7 +16,7 @@ t_list_d *ft_lstnew_d(void *content)
{
t_list_d *new_node;

new_node = malloc(sizeof(t_list_d));
new_node = (t_list_d *) malloc(sizeof(t_list_d));
if (new_node == NULL)
return (NULL);
new_node->content = content;
Expand Down
4 changes: 2 additions & 2 deletions libraries/libft/src/libft/lists/singly_linked/ft_lstnew.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:05:19 by ldulling #+# #+# */
/* Updated: 2023/09/24 16:22:04 by ldulling ### ########.fr */
/* Updated: 2024/01/18 04:48:50 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,7 +16,7 @@ t_list *ft_lstnew(void *content)
{
t_list *new_node;

new_node = malloc(sizeof(t_list));
new_node = (t_list *) malloc(sizeof(t_list));
if (new_node == NULL)
return (NULL);
new_node->content = content;
Expand Down
6 changes: 2 additions & 4 deletions libraries/libft/src/libft/lists/singly_linked/ft_lstsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:08:06 by ldulling #+# #+# */
/* Updated: 2023/09/24 16:22:06 by ldulling ### ########.fr */
/* Updated: 2024/01/18 03:43:31 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,14 +17,12 @@ int ft_lstsize(t_list *lst)
int n;
t_list *cur;

if (lst == NULL)
return (0);
n = 0;
cur = lst;
while (cur != NULL)
{
cur = cur->next;
n++;
cur = cur->next;
}
return (n);
}
Loading

0 comments on commit ff352d6

Please sign in to comment.