Skip to content

Commit

Permalink
implemented several heredocs
Browse files Browse the repository at this point in the history
  • Loading branch information
emSali committed Aug 12, 2023
1 parent 9bc6ae4 commit ba8c915
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
5 changes: 3 additions & 2 deletions executor/command_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* command_chain.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simao <simao@student.42.fr> +#+ +:+ +#+ */
/* By: esali <esali@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/03 23:14:14 by simao #+# #+# */
/* Updated: 2023/08/09 12:33:54 by simao ### ########.fr */
/* Updated: 2023/08/12 19:37:24 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -60,6 +60,7 @@ t_list *handle_append(t_list *curr)
*/
t_list *handle_heredoc(t_list *curr)
{
get_hdoc()->command = curr;
heredoc(curr->next);
if (curr->next->next->next != NULL)
curr = curr->next->next->next;
Expand Down
11 changes: 9 additions & 2 deletions executor/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/21 18:41:23 by esali #+# #+# */
/* Updated: 2023/08/12 16:38:11 by esali ### ########.fr */
/* Updated: 2023/08/12 19:46:25 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -85,6 +85,13 @@ void heredoc(t_list *cur)
return ;
}
write_heredoc(cur, new_line, hdoc->fd);
exec_heredoc(cur);
while (its_heredoc(cur->next->next))
{
heredoc(cur->next->next);
close(hdoc->fd);
unlink(cur->next->token[0]);
return ;
}
close(hdoc->fd);
exec_heredoc(cur);
}
8 changes: 4 additions & 4 deletions executor/heredoc_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 13:44:58 by esali #+# #+# */
/* Updated: 2023/08/01 21:28:23 by esali ### ########.fr */
/* Updated: 2023/08/12 19:35:30 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,7 +23,7 @@ void write_to_command(t_list *cur)
{
in = open(cur->next->token[0], O_RDONLY, 0644);
dup2(in, STDIN_FILENO);
execve(cur->prev->path, cur->prev->token, NULL);
execve(get_hdoc()->command->path, get_hdoc()->command->token, NULL);
close(in);
exit(errno);
}
Expand All @@ -47,7 +47,7 @@ void heredoc_to_pipe(t_list *cur)
in = open(cur->next->token[0], O_RDONLY, 0644);
dup2(in, STDIN_FILENO);
dup2(get_pipe()->fd[1], STDOUT_FILENO);
execve(cur->prev->path, cur->prev->token, NULL);
execve(get_hdoc()->command->path, get_hdoc()->command->token, NULL);
close(in);
exit(errno);
}
Expand Down Expand Up @@ -99,7 +99,7 @@ void heredoc_to_append(t_list *cur)
in = open(cur->next->token[0], O_RDONLY, 0644);
dup2(in, STDIN_FILENO);
dup2(outfile, STDOUT_FILENO);
execve(cur->prev->path, cur->prev->token, NULL);
execve(get_hdoc()->command->path, get_hdoc()->command->token, NULL);
close(in);
exit(errno);
}
Expand Down
13 changes: 7 additions & 6 deletions minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: esali <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/20 17:16:06 by esali #+# #+# */
/* Updated: 2023/08/12 19:02:23 by esali ### ########.fr */
/* Updated: 2023/08/12 19:37:03 by esali ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -39,11 +39,6 @@ typedef struct s_pipe
int stdout;
} t_pipe;

typedef struct s_heredoc
{
int fd;
} t_heredoc;

typedef struct s_list
{
char **token;
Expand All @@ -53,6 +48,12 @@ typedef struct s_list
int is_head;
} t_list;

typedef struct s_heredoc
{
int fd;
t_list *command;
} t_heredoc;

typedef struct s_env
{
char *key;
Expand Down

0 comments on commit ba8c915

Please sign in to comment.