Skip to content

Commit

Permalink
Here_doc parser now closes the file descriptor it opens so that the c…
Browse files Browse the repository at this point in the history
…hild can now reopen it, modified parser logic and execution logic to reflect that change. added protection to parser_clean exit logic to avoid calling free on the heredoc path.
  • Loading branch information
jteissie committed Jul 15, 2024
1 parent 1a7c726 commit 618eed2
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 35 deletions.
6 changes: 3 additions & 3 deletions include/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execution.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 16:53:36 by jteissie #+# #+# */
/* Updated: 2024/07/15 19:31:09 by jteissie ### ########.fr */
/* Updated: 2024/07/15 20:52:44 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -28,7 +28,7 @@ void handle_error(char *message, int code);
void execute_cmd(char *cmd, char **env, t_parser *data);

int process_files(t_lex_parser *table);
int get_redirections(t_lex_parser *table, char *redirection[], int *heredoc_fd);
void get_redirections(t_lex_parser *roaming, char *redirection[]);

int execute_data(t_parser *parsed_data, char **env);
int execute_commands(t_parser *parsed, char **envp, int std_fds[]);
Expand Down
6 changes: 3 additions & 3 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/01 13:17:51 by jteissie #+# #+# */
/* Updated: 2024/07/15 15:55:57 by jteissie ### ########.fr */
/* Updated: 2024/07/15 21:25:11 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -42,8 +42,8 @@ typedef struct s_cmd_table
typedef struct s_redirect_table
{
char *redir_str;
int heredoc_fd;
t_redir_token type;
int heredoc;
} t_redirect_table;

typedef struct s_lex_parser
Expand Down
9 changes: 4 additions & 5 deletions src/execution/execution_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execution_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 18:14:25 by jteissie #+# #+# */
/* Updated: 2024/07/15 19:30:35 by jteissie ### ########.fr */
/* Updated: 2024/07/15 20:52:22 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,7 +34,7 @@ void trash(char **array)
free(array);
}

int get_redirections(t_lex_parser *roaming, char *redirection[], int *heredoc_fd)
void get_redirections(t_lex_parser *roaming, char *redirection[])
{
char *outfile;
char *infile;
Expand All @@ -46,13 +46,12 @@ int get_redirections(t_lex_parser *roaming, char *redirection[], int *heredoc_fd
{
redir = roaming->table;
if (redir->type == TK_PARS_HEREDOC)
*heredoc_fd = dup(redir->heredoc_fd);
infile = redir->redir_str;
else if (redir->type == TK_PARS_IN)
infile = redir->redir_str;
else if (redir->type == TK_PARS_OUT)
outfile = redir->redir_str;
}
redirection[0] = infile;
redirection[1] = outfile;
return (*heredoc_fd);
}
21 changes: 6 additions & 15 deletions src/execution/redir_child.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@
/* ::: :::::::: */
/* redir_child.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/12 14:00:07 by jteissie #+# #+# */
/* Updated: 2024/07/15 19:32:56 by jteissie ### ########.fr */
/* Updated: 2024/07/15 20:55:05 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int redirect_files(int file_fd[], int *heredoc_fd)
int redirect_files(int file_fd[])
{
int dup_status;

dup_status = 0;
if (*heredoc_fd > 0)
{
dup_status += dup2(*heredoc_fd, STDIN_FILENO);
close(*heredoc_fd);
*heredoc_fd = 0;
}
else if (file_fd[0])
if (file_fd[0])
{
dup_status += dup2(file_fd[0], STDIN_FILENO);
close(file_fd[0]);
Expand Down Expand Up @@ -52,7 +46,6 @@ int open_files(char *redir[], int file_fd[])
int process_files(t_lex_parser *table)
{
char *redir[2];
int heredoc_fd;
int file_fd[2];
t_lex_parser *roaming;

Expand All @@ -65,14 +58,12 @@ int process_files(t_lex_parser *table)
roaming = roaming->prev;
while (roaming && roaming->type != TK_PARS_PIPE)
{
heredoc_fd = 0;
if (roaming->type == TK_PARS_REDIR)
{
if (get_redirections(roaming, redir, &heredoc_fd) < 0)
return (-1);
get_redirections(roaming, redir);
if (open_files(redir, file_fd) == PANIC)
return (-1);
if (redirect_files(file_fd, &heredoc_fd) < 0)
if (redirect_files(file_fd) < 0)
return (-1);
}
roaming = roaming->next;
Expand Down
5 changes: 3 additions & 2 deletions src/here_doc_parsing/parse_here_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* parse_here_doc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/15 16:09:14 by jteissie #+# #+# */
/* Updated: 2024/07/15 19:05:50 by jteissie ### ########.fr */
/* Updated: 2024/07/15 20:50:32 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -118,6 +118,7 @@ t_heredoc *process_here_doc(char *limiter)
free(heredoc);
return (NULL);
}
close(here_fd);
return (heredoc);
}

Expand Down
9 changes: 5 additions & 4 deletions src/parser/build_redirect_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* build_redirect_table.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 18:21:54 by jteissie #+# #+# */
/* Updated: 2024/07/15 16:10:48 by jteissie ### ########.fr */
/* Updated: 2024/07/15 21:24:37 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,15 +32,16 @@ int build_redirect_table(t_lex_parser *parsed, t_token *lexer)
redir_table = ft_calloc(1, sizeof(t_redirect_table));
if (!redir_table)
return (PANIC);
redir_table->heredoc_fd = 0;
redir_table->redir_str = NULL;
redir_table->heredoc = TRUE;
if (lexer->type == TK_HEREDOC)
redir_table->heredoc_fd = lexer->heredoc->fd;
redir_table->redir_str = lexer->heredoc->path;
else
{
redir_table->redir_str = ft_strdup(lexer->next->lexstr);
if (!redir_table->redir_str)
return (PANIC);
redir_table->heredoc = FALSE;
lexer->next->type = TK_RESERVED;
}
redir_table->type = get_redir_type(lexer);
Expand Down
6 changes: 3 additions & 3 deletions src/parser/parser_clean_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* parser_clean_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/13 19:21:20 by jteissie #+# #+# */
/* Updated: 2024/07/13 19:22:30 by jteissie ### ########.fr */
/* Updated: 2024/07/15 21:24:51 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,7 +30,7 @@ void free_tables(t_lex_parser *r)
if (r->type == TK_PARS_REDIR)
{
redir_table = r->table;
if (redir_table->redir_str)
if (redir_table->redir_str && redir_table->heredoc == FALSE)
free(redir_table->redir_str);
free(redir_table);
redir_table = NULL;
Expand Down

0 comments on commit 618eed2

Please sign in to comment.