Skip to content

Commit

Permalink
feat: heredoc $ 해석, heredoc sigint 종료, 릭 수정 #32 #45
Browse files Browse the repository at this point in the history
  • Loading branch information
Oris482 committed Sep 19, 2022
1 parent 8af7180 commit 59ddda9
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 57 deletions.
5 changes: 2 additions & 3 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/19 23:09:27 by minsuki2 ### ########.fr #
# Updated: 2022/09/19 23:22:57 by jaesjeon ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -55,8 +55,7 @@ FT_COMMAND_SRCS = builtin_cd.c \
FT_ENVIRON_SRCS = about_env.c \
dict_utils.c \
dict_list_utils.c \
dict_node_utils.c \
envp_utils.c
dict_node_utils.c
FT_FILE_SRCS = about_dir.c \
dirent_utils.c \
find_files.c \
Expand Down
8 changes: 4 additions & 4 deletions 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/19 01:05:26 by jaesjeon ### ########.fr */
/* Updated: 2022/09/20 00:26:13 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,10 +28,10 @@ unsigned int check_syntax_error(t_lx_token *cur_node)
if (check_syntax_middleware(cur_node, &parentheses_counter) == FALSE)
return (SYNTAX_ERROR_EXIT_CODE);
if (cur_node->token_type == HERE_DOC)
if (make_tmp_heredoc(cur_node, cur_node->next->token_str) == \
GENERAL_EXIT_CODE)
if (make_tmp_heredoc(cur_node, cur_node->next->token_str) != \
SUCCESS_EXIT_CODE)
return (GENERAL_EXIT_CODE);
cur_node = cur_node->next;
}
return (SUCCESS);
return (SUCCESS_EXIT_CODE);
}
13 changes: 8 additions & 5 deletions ft_command/ft_command.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/09/17 20:46:45 by jaesjeon #+# #+# */
/* Updated: 2022/09/19 23:07:45 by minsuki2 ### ########.fr */
/* Updated: 2022/09/20 02:55:01 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,8 +19,11 @@
# include <errno.h>
# include <stdlib.h>

# define OPTION_ERROR 3
# define ARG_ERROR 4
# define DO_NOT_TRANSLATE 0
# define TRANSLATE_ENV 1

# define OPTION_ERROR 3
# define ARG_ERROR 4

// built-in functions
int redi_heredoc(char *limiter);
Expand All @@ -42,12 +45,12 @@ void count_pipe(t_tree *tree_node, t_pipe *info);
void add_pid_to_list(t_pid_list *pid_list, pid_t pid);

// heredoc.c
int make_tmp_heredoc(t_lx_token *token, char *limiter);
int make_tmp_heredoc(t_lx_token *token, char *pure_limiter);
int handle_redirections_error(const char *cmd, const char *arg);

// heredoc_utils.c
int make_tmpfile(char **tmpname, int *fd);
void write_tmp_heredoc(char *limiter, int write_fd);
void write_tmp_heredoc(t_heredoc_info *heredoc_info, int write_fd);

// builtin_utils.c
int is_builtin(const char *str);
Expand Down
43 changes: 35 additions & 8 deletions ft_command/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,60 @@
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/18 23:22:59 by jaesjeon #+# #+# */
/* Updated: 2022/09/19 20:47:05 by jaesjeon ### ########.fr */
/* Updated: 2022/09/20 01:38:49 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell_info.h"
#include "ft_command.h"
#include "minishell.h"
#include "ft_print.h"
#include "ft_string.h"
#include "ft_check.h"
#include "ft_alloc.h"

int make_tmp_heredoc(t_lx_token *token, char *limiter)
void _set_heredoc_info(t_heredoc_info *heredoc_info, char *pure_limiter)
{
int write_fd;
pid_t pid;
int status;
char *tmpname;
heredoc_info->limiter = ft_strdup("");
if (ft_strchr(pure_limiter, '\"') || ft_strchr(pure_limiter, '\''))
heredoc_info->option = DO_NOT_TRANSLATE;
else
heredoc_info->option = TRANSLATE_ENV;
while (*pure_limiter)
{
if (!is_quote(*pure_limiter))
{
ft_strjoin_self_add_free(&heredoc_info->limiter, \
ft_chr_to_str(*pure_limiter));
}
pure_limiter++;
}
}

int make_tmp_heredoc(t_lx_token *token, char *pure_limiter)
{
int write_fd;
pid_t pid;
int status;
char *tmpname;
t_heredoc_info heredoc_info;

_set_heredoc_info(&heredoc_info, pure_limiter);
if (make_tmpfile(&tmpname, &write_fd) == FALSE)
{
my_free(heredoc_info.limiter);
return (handle_redirections_error("Here-doc", NULL));
}
token->interpreted_str = tmpname;
signal(SIGINT, SIG_IGN);
pid = my_fork();
if (pid == CHILD_PID)
write_tmp_heredoc(limiter, write_fd);
write_tmp_heredoc(&heredoc_info, write_fd);
waitpid(pid, &status, WUNTRACED);
set_init_signal();
close(write_fd);
return (SUCCESS_EXIT_CODE);
my_free(heredoc_info.limiter);
return (WEXITSTATUS(status));
}

int redi_heredoc(char *filename)
Expand Down
22 changes: 17 additions & 5 deletions ft_command/heredoc_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/18 23:26:11 by jaesjeon #+# #+# */
/* Updated: 2022/09/18 23:32:37 by jaesjeon ### ########.fr */
/* Updated: 2022/09/20 01:33:06 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,6 +16,8 @@
#include "ft_string.h"
#include "ft_alloc.h"
#include "ft_environ.h"
#include "ft_token.h"
#include "ft_print.h"

static char *_make_tmpname(char *ref, char *prefix)
{
Expand Down Expand Up @@ -74,15 +76,25 @@ static void _heredoc_signal_handler(void)
signal(SIGQUIT, SIG_IGN);
}

void write_tmp_heredoc(char *limiter, int write_fd)
void write_tmp_heredoc(t_heredoc_info *heredoc_info, int write_fd)
{
char *line;
char *line;
t_lx_token tmp_token;

_heredoc_signal_handler();
line = my_readline("> ");
while (line && !ft_strcmp(line, limiter))
while (line && !ft_strcmp(line, heredoc_info->limiter))
{
write(write_fd, line, ft_strlen(line));
if (heredoc_info->option == DO_NOT_TRANSLATE \
|| !ft_strchr(line, '$'))
write(write_fd, line, ft_strlen(line));
else
{
tmp_token.interpreted_str = NULL;
dquote_translator(&tmp_token, line);
ft_putstr_fd(tmp_token.interpreted_str, write_fd);
my_free(tmp_token.interpreted_str);
}
write(write_fd, "\n", 1);
my_free(line);
line = my_readline("> ");
Expand Down
10 changes: 6 additions & 4 deletions ft_environ/dict_node_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
/* ::: :::::::: */
/* dict_node_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: minsuki2 <minsuki2@student.42seoul.kr +#+ +:+ +#+ */
/* By: jaesjeon <jaesjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/19 22:44:25 by minsuki2 #+# #+# */
/* Updated: 2022/09/19 22:57:48 by minsuki2 ### ########.fr */
/* Updated: 2022/09/19 23:22:25 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell_info.h"
#include "ft_environ.h"
#include "ft_alloc.h"

int comapre_order_dict(const t_dict *next, const t_dict *new)
{
Expand Down Expand Up @@ -52,7 +54,7 @@ static void _dict_lstadd_last(t_dict *head, t_dict *new)
static void _dict_lstadd_next(t_dict *cur, t_dict *new)
{
if (cur == get_last_dict(cur))
return (dict_lstadd_last(get_first_dict(cur), new));
return (_dict_lstadd_last(get_first_dict(cur), new));
new->prev = cur;
new->next = cur->next;
cur->next->prev = new;
Expand All @@ -72,7 +74,7 @@ void dict_lstadd_order(t_dict *head, t_dict *new)
{
val = comapre_order_dict(cur->next, new);
if (val > 0)
dict_lstadd_next(cur, new);
_dict_lstadd_next(cur, new);
else if (val == 0)
return ;
cur = cur->next;
Expand Down
5 changes: 3 additions & 2 deletions ft_token/translator.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/19 22:39:04 by jaesjeon ### ########.fr */
/* Updated: 2022/09/20 02:53:01 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,7 +31,7 @@ static char *_make_dollar_find_str(char *chunk)
else if (*chunk == '$' || *chunk == '\0')
ret_str = ft_chr_to_str('$');
else
ret_str = my_getenv(chunk);
ret_str = ft_strdup(my_getenv(chunk));
if (!ret_str || !*ret_str)
return (NULL);
return (ret_str);
Expand All @@ -48,6 +48,7 @@ void dollar_translator(t_lx_token *token_cur, char *chunk, int split_flag)
str_cur = cursor_to_space(split_flag, find_str);
ft_strjoin_self_add_free(&token_cur->interpreted_str, \
ft_strcpy(find_str, str_cur));
my_free(find_str);
while (*str_cur)
{
if (ft_isspace(*str_cur) && str_cur++)
Expand Down
22 changes: 0 additions & 22 deletions leaks.c

This file was deleted.

8 changes: 7 additions & 1 deletion minishell_info.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/09/17 20:40:53 by jaesjeon #+# #+# */
/* Updated: 2022/09/19 23:08:26 by minsuki2 ### ########.fr */
/* Updated: 2022/09/20 00:47:11 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,6 +43,12 @@ typedef struct s_pid_list
struct s_pid_list *next;
} t_pid_list;

typedef struct s_heredoc_info
{
int option;
char *limiter;
} t_heredoc_info;

# define DICT_MAX 53

typedef struct s_dict
Expand Down
8 changes: 5 additions & 3 deletions parser.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/24 10:31:09 by minsuki2 #+# #+# */
/* Updated: 2022/09/19 01:06:38 by jaesjeon ### ########.fr */
/* Updated: 2022/09/20 00:27:17 by jaesjeon ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -62,10 +62,12 @@ void expand_token_to_tree(t_tree *root)
t_tree *parser(t_lx_token *head)
{
t_tree *root;
int exit_code;

if (check_syntax_error(head) == SYNTAX_ERROR_EXIT_CODE)
exit_code = check_syntax_error(head);
if (exit_code != SUCCESS_EXIT_CODE)
{
set_exit_status(SYNTAX_ERROR_EXIT_CODE);
set_exit_status(exit_code);
return (list_tree_free(head, NULL));
}
root = make_tree_node(TREE_UNDEFINED, NULL, head);
Expand Down

0 comments on commit 59ddda9

Please sign in to comment.