Skip to content

Commit

Permalink
Fix handling heredoc (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
servettonga authored Jul 17, 2024
1 parent 6b551ad commit d8d1bbf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
6 changes: 3 additions & 3 deletions include/execute.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ bool is_async(t_pipeline *pipeline);
bool should_execute(t_connection conn_type, int exit_status);

// ** execute command **
int execute_command(t_shell *shell, t_pipeline *p, t_pipeline *cur);
int exec_command(t_shell *shell, t_pipeline *p, t_pipeline *cur, pid_t pid);
pid_t create_child(t_shell *shell, t_pipeline *p, t_pipeline *cur);
void execute_pipeline(t_shell *shell, int *cmds, int num_cmds);
void execute_pipe(t_shell *shell, pid_t pid);
void exec_pipeline(t_shell *shell, int *cmds, int num_cmds);
void exec_pipe(t_shell *shell, pid_t pid);

// ** builtin functions **
int cmd_cd(t_shell *shell, char **args);
Expand Down
4 changes: 2 additions & 2 deletions src/exec/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ void execute(t_pipeline *pipeline, t_shell *shell)
break ;
cmds[i] = find_type(shell, pipeline, cur);
if (!async)
execute_pipe(shell, cmds[i]);
exec_pipe(shell, cmds[i]);
i++;
cur = cur->next;
}
close_pipes(pipeline);
if (async)
execute_pipeline(shell, cmds, i);
exec_pipeline(shell, cmds, i);
}

static pid_t find_type(t_shell *shell, t_pipeline *p, t_pipeline *cur)
Expand Down
33 changes: 32 additions & 1 deletion src/exec/execute_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
static void handle_input(t_command *cmd);
static void handle_output(t_command *cmd);
static void handle_pipe(t_pipeline *current);
static void handle_heredoc(t_command *cmd, pid_t pid);

/**
* @brief Execute a command
Expand All @@ -23,10 +24,11 @@ static void handle_pipe(t_pipeline *current);
* @param cur The current command to execute
* @note This function executes the command in a child process
*/
int execute_command(t_shell *shell, t_pipeline *p, t_pipeline *cur)
int exec_command(t_shell *shell, t_pipeline *p, t_pipeline *cur, pid_t pid)
{
char **env_array;

handle_heredoc(&cur->cmd, pid);
handle_input(&cur->cmd);
handle_output(&cur->cmd);
handle_pipe(cur);
Expand Down Expand Up @@ -116,3 +118,32 @@ static void handle_pipe(t_pipeline *current)
if (current->next == NULL && current->prev != NULL)
dup2(current->prev->fd_in, STDIN_FILENO);
}

static void handle_heredoc(t_command *cmd, pid_t pid)
{
int fd;
char *line;

if (cmd->is_heredoc)
{
if (cmd->infile != NULL)
free(cmd->infile);
cmd->infile = ft_strjoin("/tmp/heredoc_pid.", ft_itoa(pid));
fd = open(cmd->infile, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd == -1)
{
perror("minishell: open ");
exit(EXIT_FAILURE);
}
line = readline("heredoc> ");
while (line != NULL)
{
if (ft_strcmp(line, cmd->limiter) == 0)
break ;
ft_putendl_fd(line, fd);
free(line);
line = readline("heredoc> ");
}
close(fd);
}
}
6 changes: 3 additions & 3 deletions src/exec/execute_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pid_t create_child(t_shell *shell, t_pipeline *p, t_pipeline *cur)
return (EXIT_FAILURE);
}
if (pid == 0)
exit(execute_command(shell, p, cur));
exit(exec_command(shell, p, cur, pid));
return (pid);
}

Expand All @@ -40,7 +40,7 @@ pid_t create_child(t_shell *shell, t_pipeline *p, t_pipeline *cur)
* @param cmds The array of process IDs
* @param num_cmds The number of commands in the pipeline
*/
void execute_pipeline(t_shell *shell, int *cmds, int num_cmds)
void exec_pipeline(t_shell *shell, int *cmds, int num_cmds)
{
int i;

Expand All @@ -58,7 +58,7 @@ void execute_pipeline(t_shell *shell, int *cmds, int num_cmds)
* @param shell The shell structure
* @param pid The process ID of the child process
*/
void execute_pipe(t_shell *shell, pid_t pid)
void exec_pipe(t_shell *shell, pid_t pid)
{
int status;

Expand Down

0 comments on commit d8d1bbf

Please sign in to comment.