Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'dev_loader_multi_heredoc' into dev_loader
Browse files Browse the repository at this point in the history
  • Loading branch information
Dohoon Gwak committed Apr 9, 2024
2 parents 8c5a18b + 06b741b commit 292f2fe
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 23 deletions.
9 changes: 7 additions & 2 deletions codes/includes/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* loader.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: dogwak <dogwak@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:47:14 by nicknamemoh #+# #+# */
/* Updated: 2024/04/09 13:13:49 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:05:21 by dogwak ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -113,6 +113,11 @@ char *ldexec_heredoc_assign_f(void);
t_bool ldexec_heredoc(int fd, char *delim,
t_bool expansion, t_ld_map_env *env);

// ldpre_heredoc_vector.c

int construct_heredoc_name(void *pos, void *param);
void destruct_heredoc_name(void *pos);

// ldpre_ast_exec.c

int ldpre_ast_exec(t_ast_node *ast, t_ld_map_env *env,
Expand Down
11 changes: 6 additions & 5 deletions codes/includes/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: dogwak <dogwak@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:18:03 by kyungjle #+# #+# */
/* Updated: 2024/04/09 13:54:30 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:31:58 by dogwak ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,7 @@

// import sig_atomic_t type
# include <signal.h>
# include "ft_vector.h"

// ANSI terminal contrl characters
# define TERM_COLOR_END "\001\033[m\002"
Expand Down Expand Up @@ -46,9 +47,9 @@ extern volatile sig_atomic_t g_sigint;
// used by loader.preprocessor.ast
typedef struct s_ld_heredoc
{
char *heredoc_name;
int stdin_fd;
int stdout_fd;
t_ft_vector *phd_name_vec;
int stdin_fd;
int stdout_fd;
} t_ld_heredoc;

// used by loader.preprocessor.param.expansion
Expand Down
27 changes: 19 additions & 8 deletions codes/loader/ldpre_ast_redir2.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ldpre_ast_redir2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: dogwak <dogwak@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:41:08 by kyungjle #+# #+# */
/* Updated: 2024/04/09 14:02:04 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:30:39 by dogwak ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,7 +19,7 @@ t_bool ldpre_ast_redir_infile(char *filename, t_ld_heredoc heredoc,
enum e_node_type mode, t_ld_map_env *env);
static t_bool infile_read(char *filename);
static t_bool infile_heredoc(char *filename,
t_ld_map_env *env, t_ld_heredoc heredoc);
t_ld_map_env *env, char *heredoc_name);

t_bool ldpre_ast_redir_outfile(char *filename, enum e_node_type mode)
{
Expand Down Expand Up @@ -54,6 +54,17 @@ t_bool ldpre_ast_redir_infile(char *filename, t_ld_heredoc heredoc,
close(STDOUT_FD);
if (dup(heredoc.stdout_fd) < 0)
do_exit("ldpre_ast_redir.dup");
///////////////////////////////////////////////////
// heredoc vector에 새로운 heredoc 파일을 생성 및 push back
// 생성된 heredoc 파일의 이름을 전달할것
// push_back 시 인자로 NULL을 전달, 필요 정보는 모두 ldexec_heredoc_assign_f()가 제공함.
if (!heredoc.phd_name_vec->push_back(heredoc.phd_name_vec, NULL))
{
// malloc error시 실행.
}
// 벡터의 가장 최근에 push한 원소 포인터 획득
(char *)heredoc.phd_name_vec->back(heredoc.phd_name_vec);
///////////////////////////////////////////////////
ret = infile_heredoc(filename, env, heredoc);
close(STDOUT_FD);
if (dup(stdout_fd) < 0)
Expand All @@ -79,26 +90,26 @@ static t_bool infile_read(char *filename)
}

static t_bool infile_heredoc(char *filename,
t_ld_map_env *env, t_ld_heredoc heredoc)
t_ld_map_env *env, char *heredoc_name)
{
int fd;
size_t delim_len;
t_bool res;
t_bool expansion;

res = TRUE;
if (access(heredoc.heredoc_name, F_OK | W_OK | R_OK) != 0)
return (ld_errno_file("ldpre_ast_redir.access", heredoc.heredoc_name));
if (access(heredoc_name, F_OK | W_OK | R_OK) != 0)
return (ld_errno_file("ldpre_ast_redir.access", heredoc_name));
delim_len = ft_strlen(filename);
filename = ldpre_param_quote_f(ft_strdup(filename), env, NULL);
expansion = (ft_strlen(filename) == delim_len);
fd = open(heredoc.heredoc_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
fd = open(heredoc_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
do_exit("ldpre_ast_redir.open");
res = ldexec_heredoc(fd, filename, expansion, env);
close(fd);
close(STDIN_FD);
fd = open(heredoc.heredoc_name, O_RDONLY);
fd = open(heredoc_name, O_RDONLY);
if (fd < 0)
return (ld_errno_file("ldpre_ast_redir.open", filename));
return (res);
Expand Down
27 changes: 27 additions & 0 deletions codes/loader/ldpre_heredoc_vector.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ldpre_heredoc_vector.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dogwak <dogwak@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/09 14:31:52 by dogwak #+# #+# */
/* Updated: 2024/04/09 15:27:30 by dogwak ### ########.fr */
/* */
/* ************************************************************************** */

#include "loader.h"

int construct_heredoc_name(void *pos, void *param)
{
param++;
*((char **)pos) = ldexec_heredoc_assign_f();
if (*((char **)pos) == NULL)
return (0);
return (1);
}

void destruct_heredoc_name(void *pos)
{
free(*(char **)pos);
}
50 changes: 42 additions & 8 deletions codes/loader/loader_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* loader_wrapper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* By: dogwak <dogwak@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/01 13:16:24 by nicknamemoh #+# #+# */
/* Updated: 2024/04/09 13:59:56 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 15:25:04 by dogwak ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,6 +20,44 @@ void loader_wrapper(char *input, t_ld_map_env *env);
static void fd_preserve(int *stdin_fd, int *stdout_fd);
static void fd_restore(int stdin_fd, int stdout_fd);

/*
initialize object 't_ld_heredoc'.
construct vector of char *, vector of heredoc file's name.
set in and out fd of tty
*/
static t_bool init_heredoc_f(t_ld_heredoc *self, int infd, int outfd)
{
self->phd_name_vec = new_ftvec(construct_heredoc_name,
destruct_heredoc_name, sizeof(char *));
if (self->phd_name_vec == NULL)
return (FALSE);
self->stdin_fd = infd;
self->stdout_fd = outfd;
}

/*
clean content of object 't_ld_heredoc'
unlink all the heredoc file using name of the heredoc.
all the name of the heredoc file is in phd_name_vec.
delete phd_name_vec.
*/
static void clean_heredoc(t_ld_heredoc *self)
{
const char *hhdoc_name_vec = self->phd_name_vec->pbuffer;
size_t idx;

idx = -1;
while (++idx < self->phd_name_vec->size)
{
unlink((const char *)hhdoc_name_vec[idx]);
}
delete_ftvec(self->phd_name_vec);
self->phd_name_vec = NULL;
}

void loader_wrapper(char *input, t_ld_map_env *env)
{
int stdin_fd;
Expand All @@ -30,10 +68,7 @@ void loader_wrapper(char *input, t_ld_map_env *env)

fd_preserve(&stdin_fd, &stdout_fd);
ldexec_sigign_setup(oldacts);
heredoc.heredoc_name = ldexec_heredoc_assign_f();
heredoc.stdin_fd = stdin_fd;
heredoc.stdout_fd = stdout_fd;
if (heredoc.heredoc_name == NULL)
if (!init_heredoc_f(&heredoc, stdin_fd, stdout_fd))
{
printf("cannot open heredoc :tried %d times :(\n", HEREDOC_MAX);
return ;
Expand All @@ -44,8 +79,7 @@ void loader_wrapper(char *input, t_ld_map_env *env)
fd_restore(stdin_fd, stdout_fd);
input_sighandler_restore(oldacts);
delete_ast_node(ast);
unlink(heredoc.heredoc_name);
free(heredoc.heredoc_name);
clean_heredoc(&heredoc);
}

static void fd_preserve(int *stdin_fd, int *stdout_fd)
Expand Down

0 comments on commit 292f2fe

Please sign in to comment.