diff --git a/Makefile b/Makefile index f0746f8..a33b989 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: jaesjeon +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/08/20 15:48:55 by jaesjeon #+# #+# # -# Updated: 2022/09/08 18:16:20 by jaesjeon ### ########.fr # +# Updated: 2022/09/12 23:32:01 by minsuki2 ### ########.fr # # # # **************************************************************************** # @@ -64,14 +64,17 @@ MANDA_SRCS = minishell.c \ origin_str_utils2.c \ origin_putfd_utils.c \ custom_str_utils.c \ + custom_str_utils2.c \ terminal_setting.c \ error_utils.c \ free_utils.c \ linked_list_utils.c \ + envp_utils.c \ $(addprefix $(MY_FUNC_DIR), $(MY_FUNC_SRCS)) \ $(addprefix $(LINER_DIR), $(LINER_SRCS)) \ $(addprefix $(LEXER_DIR), $(LEXER_SRCS)) \ - $(addprefix $(PARSER_DIR), $(PARSER_SRCS)) + $(addprefix $(PARSER_DIR), $(PARSER_SRCS)) \ + debug_print_envp.c diff --git a/custom_str_utils2.c b/custom_str_utils2.c new file mode 100644 index 0000000..b66b140 --- /dev/null +++ b/custom_str_utils2.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* custom_str_utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: minsuki2 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/11 18:04:59 by minsuki2 #+# #+# */ +/* Updated: 2022/09/11 19:02:59 by minsuki2 ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *ft_strcpy(const char *start, const char *end) +{ + char *ret; + size_t len; + int idx; + + len = end - start; + ret = (char *)malloc(len + 1); + if (ret == NULL) + exit(GENERAL_EXIT_CODE); + idx = 0; + while (idx < (int)len) + ret[idx++] = *start++; + ret[idx] = '\0'; + return (ret); +} + +char *ft_chr_to_str(char c) +{ + char *str; + + str = (char *)my_malloc(2 * sizeof(char)); + str[1] = '\0'; + str[0] = c; + return (str); +} diff --git a/debug_print_envp.c b/debug_print_envp.c new file mode 100644 index 0000000..593a232 --- /dev/null +++ b/debug_print_envp.c @@ -0,0 +1,27 @@ + +#include "minishell.h" + +void print_dictionary_lst() +{ + int i; + t_dic *cur; + + i = 0; + while (i < DIC_MAX) + { + printf("category : %s\n", g_dic[i].value); + cur = g_dic[i].next; + while (cur) + { + printf("L %s=%s\n", cur->name, cur->value); + cur = cur->next; + } + i++; + } +} + +void print_envp(char *envp[]) +{ + for (int i = 0; envp[i]; i++) + printf("%s\n", envp[i]); +} diff --git a/envp_utils.c b/envp_utils.c new file mode 100644 index 0000000..29814ee --- /dev/null +++ b/envp_utils.c @@ -0,0 +1,145 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* envp_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: minsuki2 +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/09/09 15:25:42 by minsuki2 #+# #+# */ +/* Updated: 2022/09/13 14:54:39 by minsuki2 ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_dic *make_envp_node(char *name, char *value, t_dic *next, t_dic *prev) +{ + t_dic *new; + + new = (t_dic *)my_calloc(1, sizeof(t_dic)); + new->name = name; + new->value = value; + new->next = next; + new->prev = prev; + return (new); +} + +void setting_dictionary(void) +{ + int i; + char category; + + i = 0; + category = 'A'; + while (i < DIC_MAX) + { + category += 4 * (category == 'Z' + 1) + (category == '_' + 1); + g_dic[i].value = ft_chr_to_str(category++); + g_dic[i].prev = &g_dic[i]; + i++; + } +} + +void ft_lstadd_last(t_dic *head, t_dic *new) +{ + t_dic *const last = get_last_dic(head); + + head->prev = new; + last->next = new; + new->prev = last; +} + +void ft_lstadd_next(t_dic *cur, t_dic *new) +{ + if (cur == get_last_dic(cur)) + return ft_lstadd_last(get_first_dic(cur), new); + new->next = cur->next; + cur->next = new; + new->prev = cur; +} + +int comapre_order_dic(const t_dic *next, const t_dic *new) +{ + int i; + const char *s1; + const char *s2; + + i = 0; + if (!next) + return (GREATER_THAN); + s1 = next->name; + s2 = new->name; + while (s1[i] && s2[i] && s1[i] == s2[i]) + i++; + return (s1[i] - s2[i]); +} + +// int comapre_order_word(const char *s1, const char *s2) +// { +// int i; +// +// i = 0; +// if (!s1) +// return (GREATER_THAN); +// while (s1[i] && s2[i] && s1[i] == s2[i]) +// i++; +// return (s1[i] - s2[i]); +// } +/* AA AB AC AA AAB => 음수 + * AAA AAB => 음수 + * AAB AB AAB => 양수 + */ + +void ft_lstadd_order(t_dic *head, t_dic *new) +{ + int old_val; + int val; + t_dic *cur; + + if (!head) + return ; + cur = head; + old_val = LESS_THAN; + while (cur) + { + val = comapre_order_word(cur->next, new); + if (old_val < 0 && val > 0) + return ft_lstadd_next(cur, new); + old_val = val; + cur = cur->next; + } +} + + +int chr_to_idx(char c) +{ + if ('A' <= c && c <= 'Z') + return (c - 'A' + 0); + else if ('a' <= c && c <= 'z') + return (c - 'a' + 27); + return (c - '_' + 26); +} +// 'A' 0 +// 'Z' 25 +// '_' 26 +// 'a' 27 + +void char_dimen2_to_lst(char *envp[]) +{ + int idx; + int j; + char *pos; + + print_envp(envp); + setting_dictionary(); + j = 0; + while (envp[j]) + { + pos = ft_strchr_null(envp[j], '='); + idx = chr_to_idx(*envp[j]); + ft_lstadd_order(&g_dic[idx], make_envp_node(ft_strcpy(envp[j], pos), \ + ft_strdup(pos + 1), NULL, NULL)); + j++; + } + print_dictionary_lst(); +} diff --git a/getenv_example.c b/getenv_example.c new file mode 100644 index 0000000..c1ef72c --- /dev/null +++ b/getenv_example.c @@ -0,0 +1,24 @@ + +#include "minishell.h" + +int main(int argc, char *argv[], char *envp[]) +{ + int i; + char *cur; + int j; + + i = 0; + j = 0; + while (envp[i]) + { + cur = envp[i]; + while (*cur) + { + ft_putchar_fd(*cur, STDOUT_FILENO); + cur++; + } + ft_putchar_fd('\n', STDOUT_FILENO); + i++; + } + return (0); +} diff --git a/lexer/check_syntax_error.c b/lexer/check_syntax_error.c index 705f160..b8ff433 100644 --- a/lexer/check_syntax_error.c +++ b/lexer/check_syntax_error.c @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/09/03 15:24:20 by jaesjeon #+# #+# */ -/* Updated: 2022/09/08 19:46:43 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/13 12:45:33 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,7 @@ static int _check_parentheses_syntax(t_lx_token **token, \ if ((*token)->token_type == PARENTHESES_OPEN) { - if (prev_token == get_last_node(prev_token) \ + if (prev_token == get_last_token(prev_token) \ || prev_token->token_type != WORD) return (SUCCESS); else if (prev_token && prev_token->prev->next != NULL) diff --git a/lexer/debug_function.c b/lexer/debug_function.c index 26acaf4..33cf80d 100644 --- a/lexer/debug_function.c +++ b/lexer/debug_function.c @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/31 15:23:45 by jaesjeon #+# #+# */ -/* Updated: 2022/09/08 18:10:43 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/13 12:45:14 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,7 +80,7 @@ void print_token_prev(t_lx_token *token_list) if (!head) return ; printf(" [(null)] "); - token_list = get_last_node(token_list); + token_list = get_last_token(token_list); while (token_list && token_list != head) { if (token_list->interpreted_str) diff --git a/linked_list_utils.c b/linked_list_utils.c index feb8988..4217426 100644 --- a/linked_list_utils.c +++ b/linked_list_utils.c @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/09/04 19:38:01 by jaesjeon #+# #+# */ -/* Updated: 2022/09/08 19:03:02 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/13 12:44:57 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_lx_token *cut_front_node(t_lx_token *cur_node) { - t_lx_token *const last_node = get_last_node(cur_node); + t_lx_token *const last_node = get_last_token(cur_node); t_lx_token *const new_last_node = cur_node->prev; if (cur_node->prev == last_node) @@ -30,7 +30,7 @@ t_lx_token *cut_front_node(t_lx_token *cur_node) t_lx_token *cut_back_node(t_lx_token *cur_node) { - t_lx_token *const last_node = get_last_node(cur_node); + t_lx_token *const last_node = get_last_token(cur_node); t_lx_token *const new_last_node = cur_node; t_lx_token *const rtn_node = cur_node->next; @@ -47,7 +47,7 @@ t_lx_token *cut_back_node(t_lx_token *cur_node) t_lx_token *pop_node(t_lx_token **cur_node, t_lx_token *end_node) { t_lx_token *const start_node = *cur_node; - t_lx_token *const last_node = get_last_node(*cur_node); + t_lx_token *const last_node = get_last_token(*cur_node); *cur_node = end_node->next; if (start_node->prev == last_node && end_node == last_node) @@ -68,8 +68,8 @@ t_lx_token *pop_node(t_lx_token **cur_node, t_lx_token *end_node) void merge_linked_list(t_lx_token *dst, t_lx_token *src) { - const t_lx_token *dst_last_node = get_last_node(dst); - const t_lx_token *src_last_node = get_last_node(src); + const t_lx_token *dst_last_node = get_last_token(dst); + const t_lx_token *src_last_node = get_last_token(src); dst->prev->next = src; dst->prev = (t_lx_token *)src_last_node; diff --git a/minishell.c b/minishell.c index 2cdf563..deb5cdb 100644 --- a/minishell.c +++ b/minishell.c @@ -6,15 +6,12 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/25 11:00:34 by jaesjeon #+# #+# */ -/* Updated: 2022/09/08 19:50:07 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/13 13:17:05 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -#include "myfunc.h" -#include "liner.h" -#include "lexer.h" -#include "parser.h" + static void _minishell_routine(char *full_line, t_oflag *oflag) { @@ -43,8 +40,7 @@ int main(int argc, char *argv[], char *envp[]) signal_handler(); terminal_off_control_chars(); - (void)envp; - // ft_str_array_dup envp 할당 + char_dimen2_to_lst(envp); while (true) { full_line = liner(&oflag); @@ -57,6 +53,7 @@ int main(int argc, char *argv[], char *envp[]) add_history(full_line); _minishell_routine(full_line, &oflag); my_multi_free(full_line, NULL, NULL, NULL); + // list_tree_free(t_dic); system("leaks -q minishell"); } (void)argc; diff --git a/minishell.h b/minishell.h index adaa82c..a77ebad 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/23 09:08:52 by minsuki2 #+# #+# */ -/* Updated: 2022/09/08 18:10:25 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/13 14:28:06 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ # include # include + # define UNDEFINED 0b00000000 # define QUOTE 0b00000001 # define DQUOTE 0b00000010 @@ -31,6 +32,18 @@ # define WILDCARD 0b00001000 # define TILDE 0b00010000 +# define DIC_MAX 53 + +typedef struct s_dic +{ + char *name; + char *value; + struct s_dic *next; + struct s_dic *prev; +} t_dic; + +t_dic g_dic[DIC_MAX]; + typedef struct s_lx_token { char *token_str; @@ -111,15 +124,23 @@ typedef struct s_oflag int and_if; } t_oflag; +enum e_order +{ + LESS_THAN = -1, + EQUAL, + GREATER_THAN, +}; + + // origin_str_utils.c size_t ft_strlen(const char *s); -char *ft_strcpy(const char *start, const char *end); char *ft_strchr(const char *s, int c); size_t ft_strlcat(char *dst, char const *src, size_t dstsize); size_t ft_strlcpy(char *dst, char const *src, size_t dstsize); // origin_str_utils2.c void *ft_memset(void *b, int c, size_t len); +char *ft_strdup(const char *s1); // origin_put_fd_utils.c void ft_putchar_fd(char c, int fd); @@ -132,6 +153,11 @@ char *ft_strsjoin(char const *s1, char const *s2, char const *s3); int ft_strjoin_self(char **str, char *add); char *ft_strchr_null(const char *s, int c); char *ft_strrchr_right_away(const char *s, int c, char *const end); + +// custom_str_utils2.c +char *ft_strcpy(const char *start, const char *end); +char *ft_chr_to_str(char c); + // check_char_utils.c int ft_isspace(const char c); unsigned char is_target_char(const char c, const char target); @@ -154,7 +180,9 @@ void merge_linked_list(t_lx_token *dst, t_lx_token *src); /* minishell_utils.c */ char *get_token_str(const t_lx_token *token); -t_lx_token *get_last_node(t_lx_token *token); +t_lx_token *get_last_token(t_lx_token *token); +t_dic *get_last_dic(t_dic *dic); +t_dic *get_first_dic(t_dic *dic); void *make_new_node(size_t size); t_lx_token *make_new_token(char *token_str, int token_type, t_lx_token *prev); @@ -242,4 +270,12 @@ t_tree *parser(t_lx_token *head); // print_tree.c void print_ascii_tree(t_tree * t); + + +// envp_utils.c +void char_dimen2_to_lst(char *envp[]); + +// debug_print_evnp.c +void print_dictionary_lst(); +void print_envp(char *envp[]); #endif diff --git a/minishell_utils.c b/minishell_utils.c index 5513348..a198de5 100644 --- a/minishell_utils.c +++ b/minishell_utils.c @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/23 18:11:18 by minsuki2 #+# #+# */ -/* Updated: 2022/09/08 17:06:14 by minsuki2 ### ########.fr */ +/* Updated: 2022/09/13 14:28:29 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ char *get_token_str(const t_lx_token *token) return (token->token_str); } -t_lx_token *get_last_node(t_lx_token *token) +t_lx_token *get_last_token(t_lx_token *token) { if (!token) return (NULL); @@ -29,6 +29,24 @@ t_lx_token *get_last_node(t_lx_token *token) return (token); } +t_dic *get_last_dic(t_dic *dic) +{ + if (!dic) + return (NULL); + while (dic->next) + dic = dic->prev; + return (dic); +} + +t_dic *get_first_dic(t_dic *dic) +{ + if (!dic) + return (NULL); + while (dic->prev->next) + dic = dic->prev; + return (dic); +} + void *make_new_node(size_t size) { void *new; diff --git a/origin_str_utils.c b/origin_str_utils.c index f2e399c..59ade80 100644 --- a/origin_str_utils.c +++ b/origin_str_utils.c @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/31 15:05:05 by jaesjeon #+# #+# */ -/* Updated: 2022/09/01 10:57:39 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/11 18:05:17 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,23 +24,6 @@ size_t ft_strlen(const char *s) return (i); } -char *ft_strcpy(const char *start, const char *end) -{ - char *ret; - size_t len; - int idx; - - len = end - start; - ret = (char *)malloc(len + 1); - if (ret == NULL) - exit(GENERAL_EXIT_CODE); - idx = 0; - while (idx < (int)len) - ret[idx++] = *start++; - ret[idx] = '\0'; - return (ret); -} - char *ft_strchr(const char *s, int c) { if (!s) diff --git a/origin_str_utils2.c b/origin_str_utils2.c index 3fc58e5..d185e5c 100644 --- a/origin_str_utils2.c +++ b/origin_str_utils2.c @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/09/03 23:05:40 by minsuki2 #+# #+# */ -/* Updated: 2022/09/04 16:19:23 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/11 18:01:09 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,3 +18,14 @@ void *ft_memset(void *b, int c, size_t len) *((unsigned char *)b + len) = (unsigned char)c; return (b); } + +char *ft_strdup(const char *s1) +{ + char *dup; + size_t size; + + size = ft_strlen(s1) + 1; + dup = (char *)my_malloc(size * sizeof(char)); + ft_strlcpy(dup, s1, size); + return (dup); +} diff --git a/parser/parser.c b/parser/parser.c index c552414..5e0d2b8 100644 --- a/parser/parser.c +++ b/parser/parser.c @@ -6,7 +6,7 @@ /* By: jaesjeon +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/24 10:31:09 by minsuki2 #+# #+# */ -/* Updated: 2022/09/08 19:42:07 by jaesjeon ### ########.fr */ +/* Updated: 2022/09/13 12:45:43 by minsuki2 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,7 +57,7 @@ void making_tree_node(t_tree *const cur, unsigned char(* is_tree_type)(int)) if (!cur) return ; - find_node = find_tree_node(get_last_node(cur->token_data), \ + find_node = find_tree_node(get_last_token(cur->token_data), \ &cur->type, is_tree_type); if (!find_node) return ; @@ -73,7 +73,7 @@ void handle_subshell(t_tree *head) t_lx_token *close_token; open_token = head->token_data; - close_token = get_last_node(open_token); + close_token = get_last_token(open_token); head->left = make_tree_node(TREE_UNDEFINED, head, head->token_data->next); head->token_data = pop_node(&open_token, open_token); merge_linked_list(head->token_data, pop_node(&close_token, close_token));