Skip to content

Commit

Permalink
feat : evnp 관련 함수 제작 #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Giromi committed Sep 13, 2022
1 parent 0fce330 commit 491bfe7
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 46 deletions.
7 changes: 5 additions & 2 deletions 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/08 18:16:20 by jaesjeon ### ########.fr #
# Updated: 2022/09/12 23:32:01 by minsuki2 ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -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



Expand Down
40 changes: 40 additions & 0 deletions custom_str_utils2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* custom_str_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minsuki2 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
27 changes: 27 additions & 0 deletions debug_print_envp.c
Original file line number Diff line number Diff line change
@@ -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]);
}
145 changes: 145 additions & 0 deletions envp_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* envp_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minsuki2 <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();
}
24 changes: 24 additions & 0 deletions getenv_example.c
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 2 additions & 2 deletions lexer/check_syntax_error.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/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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lexer/debug_function.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:23:45 by jaesjeon #+# #+# */
/* Updated: 2022/09/08 18:10:43 by jaesjeon ### ########.fr */
/* Updated: 2022/09/13 12:45:14 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions linked_list_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

#include "minishell.h"

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)
Expand All @@ -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;

Expand All @@ -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)
Expand All @@ -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;
Expand Down
11 changes: 4 additions & 7 deletions minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 491bfe7

Please sign in to comment.