Skip to content

Commit

Permalink
feat : envp 기능 추가 #35 #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Minsu Kim committed Sep 13, 2022
1 parent bc5f78b commit 946c2e3
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 60 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/08/20 15:48:55 by jaesjeon #+# #+# #
# Updated: 2022/09/13 18:30:14 by jaesjeon ### ########.fr #
# Updated: 2022/09/13 23:29:28 by minsuki2 ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -34,6 +34,7 @@ NAME = minishell
MY_FUNC_SRCS = about_alloc.c \
about_dir.c \
about_readline.c \
about_env.c \
about_pipe.c

LINER_SRCS = liner.c
Expand Down Expand Up @@ -78,6 +79,7 @@ MANDA_SRCS = minishell.c \
linked_list_utils.c \
exit_status.c \
envp_utils.c \
dict_utils.c \
debug_print_envp.c \
$(addprefix $(MY_FUNC_DIR), $(MY_FUNC_SRCS)) \
$(addprefix $(LINER_DIR), $(LINER_SRCS)) \
Expand Down
4 changes: 2 additions & 2 deletions debug_print_envp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ void print_dictionary_lst()
i = 0;
while (i < DICT_MAX)
{
printf("category : %s\n", g_dic[i].value);
cur = g_dic[i].next;
printf("category : %s\n", g_dict[i].value);
cur = g_dict[i].next;
while (cur)
{
printf("L %s=%s\n", cur->name, cur->value);
Expand Down
35 changes: 35 additions & 0 deletions dict_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dict_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minsuki2 <minsuki2@student.42seoul.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 22:51:24 by minsuki2 #+# #+# */
/* Updated: 2022/09/13 23:29:36 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

t_dict *find_env_dict(t_dict *cur, const char *name)
{
while (cur)
{
if (check_match_word(cur->name, name) == TRUE)
return (cur);
cur = cur->next;
}
return (NULL);
}

// void erase_dict(char *name)
// {
// t_dict *find_node;
//
// find_node = find_env_dict(g_dict[chr_to_idx(*name)], );
// }

// void add_dict(char *name, char *value)
// {
// }
43 changes: 5 additions & 38 deletions envp_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/09 15:25:42 by minsuki2 #+# #+# */
/* Updated: 2022/09/13 21:09:33 by jaesjeon ### ########.fr */
/* Updated: 2022/09/13 23:02:21 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,10 +34,10 @@ void setting_dictionary(void)
while (i < DICT_MAX)
{
// debug
category += 4 * (category == ''Z' + 1') + (category == '_' + 1);
g_dic[i].value = ft_chr_to_str(category++);
category += 4 * (category == 'Z' + 1) + (category == '_' + 1);
g_dict[i].value = ft_chr_to_str(category++);
// debug
g_dic[i].prev = &g_dic[i];
g_dict[i].prev = &g_dict[i];
i++;
}
}
Expand Down Expand Up @@ -76,22 +76,6 @@ int comapre_order_dict(const t_dict *next, const t_dict *new)
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_dict *head, t_dict *new)
{
int val;
Expand All @@ -109,7 +93,6 @@ void ft_lstadd_order(t_dict *head, t_dict *new)
}
}


int chr_to_idx(char c)
{
if ('A' <= c && c <= 'Z')
Expand All @@ -118,10 +101,6 @@ int chr_to_idx(char c)
return (c - 'a' + 27);
return (c - '_' + 26);
}
// 'A' 0
// 'Z' 25
// '_' 26
// 'a' 27

void char_dimen2_to_lst(char *envp[])
{
Expand All @@ -136,23 +115,11 @@ void char_dimen2_to_lst(char *envp[])
{
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_lstadd_order(&g_dict[idx], make_envp_node(ft_strcpy(envp[j], pos), \
ft_strdup(pos + 1), NULL, NULL));
j++;
}
print_dictionary_lst();
}
v
A=hello

aa0 aa_95 -95
a_ aa_ 95 - 97 = -2
aa_
ab aa_ 97 - 96 = 1


aa_


a

15 changes: 10 additions & 5 deletions lexer/interpreter_middleware.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/31 15:29:22 by jaesjeon #+# #+# */
/* Updated: 2022/09/12 03:20:12 by jaesjeon ### ########.fr */
/* Updated: 2022/09/13 23:30:41 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,20 +27,25 @@ static void _dollar_translator(t_lx_token *cur, char *chunk, int split_flag)
{
char *find_str;
char *str_cur;
char *temp;

if (*chunk == '?')
{
cur->interpreted_str = ft_strsjoin(ft_itoa(get_exit_status()), \
chunk + 1, NULL);
temp = ft_itoa(get_exit_status());
ft_strjoin_self(&cur->interpreted_str, temp);
ft_strjoin_self(&cur->interpreted_str, chunk + 1);
free(temp);
return ;
}
find_str = getenv(chunk);
find_str = my_getenv(chunk);
if (!find_str || !*find_str)
return ;
str_cur = find_str;
if (split_flag && _cursor_to_space(&str_cur))
;
ft_strjoin_self(&cur->interpreted_str, ft_strcpy(find_str, str_cur));
temp = ft_strcpy(find_str, str_cur);
ft_strjoin_self(&cur->interpreted_str, temp);
free(temp);
while (*str_cur)
{
if (ft_isspace(*str_cur) && str_cur++)
Expand Down
37 changes: 24 additions & 13 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/23 09:08:52 by minsuki2 #+# #+# */
/* Updated: 2022/09/13 21:08:03 by jaesjeon ### ########.fr */
/* Updated: 2022/09/13 23:03:34 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -53,13 +53,13 @@ typedef struct s_pipe

typedef struct s_dict
{
char *name;
char *value; "A"
char *name;
char *value;
struct s_dict *next;
struct s_dict *prev;
} t_dict;

t_dict g_dic[DICT_MAX];
t_dict g_dict[DICT_MAX];

typedef struct s_lx_token
{
Expand Down Expand Up @@ -215,27 +215,30 @@ 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);
void merge_linked_list(t_lx_token *dst, t_lx_token *src);

/* minishell_utils.c */
// minishell_utils.c
char *get_token_str(const t_lx_token *token);
t_lx_token *get_last_token(t_lx_token *token);
t_dict *get_last_dict(t_dict *dic);
t_dict *get_first_dict(t_dict *dic);
void *make_new_node(size_t size);
t_lx_token *make_new_token(char *token_str, int token_type, t_lx_token *prev);
int check_match_word(const char *word1, const char *word2);

/* error_utils.c */
// error_utils.c
void print_error_syntax(char *token);
void print_error_not_close(char *str);
int print_error_str(const char *err_cmd, const char *err_arg, \
const char *custom_msg, int err_code);

/* free_utils.c */
// free_utils.c
void *list_tree_free(t_lx_token *list, t_tree *tree);

// debug_function.c
void print_token_list(t_lx_token *token_list);
void classify(struct dirent *ent);
void print_token_next(t_lx_token *token_list);
void print_token_prev(t_lx_token *token_list);

// lexer.c
t_lx_token *lexer(char *full_line, t_oflag *oflag);

Expand Down Expand Up @@ -281,21 +284,24 @@ unsigned int check_syntax_error(t_lx_token *head);
// liner.c
char *liner(t_oflag *oflag);

/* about_dir */
// about_dir
DIR *my_opendir(const char *name);
struct dirent *my_readdir(DIR *dirp);
int my_closedir(DIR *dirp);

/* about_alloc */
// about_alloc
void *my_malloc(size_t size);
void *my_calloc(size_t count, size_t size);
void my_free(void *ptr);
void my_multi_free(void *ptr1, void *ptr2, void *ptr3, void *ptr4);

/* about_readline */
// about_readline
char *my_readline(const char *prompt);
void tree_traversal(t_tree *cur_tree, int tree_type, \
void (*handler)(t_tree *));
// about_env.c
char *my_getenv(const char *name);

// tree_utils.c
unsigned char is_tree_and_or(int token_type);
unsigned char is_tree_pipe(int token_type);
Expand Down Expand Up @@ -324,9 +330,14 @@ int builtin_exit(t_lx_token *token);
int executor(t_tree *root, char set_exit_status_flag);

// envp_utils.c
void char_dimen2_to_lst(char *envp[]);
void char_dimen2_to_lst(char *envp[]);
int chr_to_idx(char c);

// debug_print_evnp.c
void print_dictionary_lst();
void print_envp(char *envp[]);
void print_dictionary_lst();
void print_envp(char *envp[]);

// dict_utils.c
t_dict *find_env_dict(t_dict *cur, const char *name);

#endif
10 changes: 9 additions & 1 deletion minishell_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/23 18:11:18 by minsuki2 #+# #+# */
/* Updated: 2022/09/13 18:35:11 by jaesjeon ### ########.fr */
/* Updated: 2022/09/13 23:39:10 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -65,3 +65,11 @@ t_lx_token *make_new_token(char *token_str, int token_type, t_lx_token *prev)
token->prev = prev;
return (token);
}

int check_match_word(const char *word1, const char *word2)
{
while (*word1 && *word2 && )
if (*word1 != *word2)
return (FALSE);
return (TRUE);
}
27 changes: 27 additions & 0 deletions myfunc/about_env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* about_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minsuki2 <minsuki2@student.42seoul.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/13 16:59:38 by minsuki2 #+# #+# */
/* Updated: 2022/09/13 23:02:24 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

#include "../minishell.h"

char *my_getenv(const char *name)
{
int idx;
t_dict *find_node;

idx = chr_to_idx(*name);
find_node = find_env_dict(g_dict[idx].next, name);
if (!find_node)
return (NULL);
return (find_node->value);
}

0 comments on commit 946c2e3

Please sign in to comment.