그럴듯한 개발 블로그
반응형

토큰화가 끝나고 커맨드 리스트를 만들던 중에 또 팀원들의 의견이 충돌해 서로 짜서 설득시키기로 했다. 이번엔 진짜 깔끔하게 잘했다는 생각이 들어 자신감 넘치게 코드 리뷰를 했는데 argv 처리 과정에서 다른 팀원들의 방법이 더 효율적이었다 크흑... 팀원 a는 redirection type이면 등록하고 삭제해서  argv만 남긴 후에 등록했고, 팀원 b는 미리 argv의 개수를 구해서 할당한 후에 등록하는 방법으로 두 명 다 내 방법보다 malloc을 적게 하여 내가 자진해서 탈락했다....ㅠ 아쉽지만 전에 채택되지 못한 코드보다 더 완성도 높은 코드라서 만족스럽다.  argv 개수를 미리 세 주는 생각을 왜 못했을까 너무 아쉽다.....  아쉬워서 일단 업로드....

typedef struct s_list
{
	char			*string;
	struct s_token	*next;
}	t_list;

t_list	*make_argv_node(char *str)
{
	t_list	*argv_node;

	argv_node = malloc(sizeof(t_list));
	argv_node->string = ft_strdup(str);
	argv_node->next = NULL;
	return (argv_node);
}

t_cmd	*make_cmd_node(void) // 얘만 빈통으로 만들어줌
{
	t_cmd	*cmd_node;

	cmd_node = malloc(sizeof(t_cmd));
	cmd_node->argv = NULL;
	cmd_node->redirection = NULL;
	// cmd_node->pipe = NULL;
	cmd_node->prev = NULL;
	cmd_node->next = NULL;
	return (cmd_node);
}

t_redirection	*make_redirection_node(char *sign, char *file)
{
	t_redirection	*new_redirection;

	new_redirection = malloc(sizeof(t_redirection));
	new_redirection->type = ft_strdup(sign);
	new_redirection->file = ft_strdup(file);
	new_redirection->next = NULL;
	return (new_redirection);
}

int	find_list_size(t_list *list)
{
	int	i;

	i = 0;
	while (list)
	{
		list = list->next;
		i++;
	}
	return (i);
}

char	**change_list_to_double_ptr(t_list *argv_list)
{
	char	**res;
	int		list_size;
	int		i;

	i = 0;
	list_size = find_list_size(argv_list);
	res = (char **)malloc(sizeof(char *) * (list_size + 1));
	while (argv_list)
		res[i++] = ft_strdup(argv_list->string);
	res[i] = NULL;
	return (res);
}


t_cmd	*create_cmd_list(t_token *token_list)
{
	t_cmd			*cmd_list;
	t_redirection	*redirection_list;
	t_list			*argv_list;
	t_cmd			*head;

	cmd_list = make_cmd_node();
	argv_list = NULL;
	head = cmd_list;
	while (token_list)
	{
		if (token_list->type == TOKEN_TYPE_REDIRECTION) //리다이렉션 만나면 다음 두 노드 밀고 리다이렉션 멤버에 등록해준다
		{
			redirection_list = make_redirection_node(token_list->string, token_list->next->string);// ->[< a] [< b] < c
			if (cmd_list->redirection) // 없으면 만들고
				cmd_list->redirection = redirection_list;
			else // 있으면 뒤에 이어준다
				redirection_list = redirection_list->next; //////// 땡스투자리
			token_list = token_list->next; // 두개 옮겨야 해서 여기서도 한번 옮김
		}
		else if (token_list->type == TOKEN_TYPE_PIPELINE)// 파이프 만나면 여태까지 만든 argv리스트 다 이중포인터로 할당해서 등록해준다
		{ //그냥 끝일때 처리 해줘야함
			cmd_list->argv = change_list_to_double_ptr(argv_list);
			// argv리스트 프리
			cmd_list->next = make_cmd_node();
			cmd_list = cmd_list->next;
		}
		else if (token_list->type == TOKEN_TYPE_ARGV) // 파이프를 만나기 전까지 argv_list에 추가해준다
		{
			if (argv_list) // 아직 argv만나지 못했으면
				argv_list = make_argv_node(token_list->string);
			else
			{
				argv_list->next = make_argv_node(token_list->string);
				argv_list = argv_list->next;
			}
		}
		token_list = token_list->next;
	}
	// 여기서 argv 처리
	return (head);
}
반응형

'<42seoul> > minishell' 카테고리의 다른 글

목걸이를 받지 못한 코드3  (0) 2023.04.13
<< "'hi"|hi'  (2) 2023.03.30
파싱이쉬워보인다고누가그랬어제가그랬어요  (2) 2023.03.26
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!