MiniDevil As beautiful as a shell
execute.c File Reference

Command execution from UI (forks, captures output, updates mood) More...

#include <sys/wait.h>
#include <fcntl.h>
#include "minishell_ui.h"
#include "env.h"
#include "get_next_line.h"
+ Include dependency graph for execute.c:

Detailed Description

Command execution from UI (forks, captures output, updates mood)

Definition in file execute.c.

Functions

static int init_pipe_and_fds (int *pipe_fd, int *saved_fds, t_ui *ui)
 Create a pipe and save current STDOUT / STDERR file descriptors. More...
 
static void handle_fork_error (t_ui *ui, int *pipe_fd, int *saved_fds)
 Clean up after a failed fork & report the error to the UI. More...
 
static void exec_child (int *pipe_fd, int *saved_fds, t_shell *shell)
 Redirect I/O to the pipe and run the command. More...
 
static void exec_parent (int *pipe_fd, int *saved_fds, t_shell *sh, pid_t pid)
 Collect output, restore I/O and wait for child. More...
 
void execute_minishell_cmd (t_shell *shell)
 Fork a child to execute a minishell command & capture its output. More...
 

Function Documentation

◆ init_pipe_and_fds()

static int init_pipe_and_fds ( int *  pipe_fd,
int *  saved_fds,
t_ui ui 
)
static

Create a pipe and save current STDOUT / STDERR file descriptors.

On pipe failure it sets exit_code = 1 and waifu's mood to upset

Parameters
pipe_fd2 element array to receive the pipe read and write ends
saved_fds2 elements array to receive the duped STDOUT and STDERR
uiUI state
Returns
0 on success and -1 on pipe failure

Definition at line 29 of file execute.c.

References t_out::exit_code, MOOD_UPSET, t_ui::out, out_add_line(), and update_waifu_mood().

Referenced by execute_minishell_cmd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_fork_error()

static void handle_fork_error ( t_ui ui,
int *  pipe_fd,
int *  saved_fds 
)
static

Clean up after a failed fork & report the error to the UI.

Parameters
uiUI state
pipe_fdThe pipe ends to close
saved_fdsThe saved STDOUT / STDERR to close

Definition at line 50 of file execute.c.

References t_out::exit_code, MOOD_UPSET, t_ui::out, out_add_line(), and update_waifu_mood().

Referenced by execute_minishell_cmd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exec_child()

static void exec_child ( int *  pipe_fd,
int *  saved_fds,
t_shell shell 
)
static

Redirect I/O to the pipe and run the command.

  • Redirects STDIN from /dev/null and STDOUT & STDERR to the pipe write end
  • Calls process_ui_input(), frees memory and exits with the command status
Parameters
pipe_fdPipe read and write ends
saved_fdsSaved STDOUT / STDERR originals to close
shellShell state

Definition at line 71 of file execute.c.

References t_shell::env, free_env_list(), out_free(), process_ui_input(), and t_shell::ui.

Referenced by execute_minishell_cmd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exec_parent()

static void exec_parent ( int *  pipe_fd,
int *  saved_fds,
t_shell sh,
pid_t  pid 
)
static

Collect output, restore I/O and wait for child.

  • Reads all child output before restoring STDOUT and STDERR
  • Updates exit_code and waifu mood accordingly
Parameters
pipe_fdThe pipe read and write ends
saved_fdsSaved STDOUT / STDERR to restore
shShell state (owns the UI)
pidPID of the child to wait for

Definition at line 107 of file execute.c.

References MOOD_PROUD, MOOD_UPSET, read_output_from_fd(), t_shell::ui, and update_waifu_mood().

Referenced by execute_minishell_cmd().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ execute_minishell_cmd()

void execute_minishell_cmd ( t_shell shell)

Fork a child to execute a minishell command & capture its output.

Creates a pipe, forks, runs exec_child() in the child and exec_parent() in the parent

Parameters
shellShell state (contains command buffer and UI output)

Definition at line 137 of file execute.c.

References exec_child(), exec_parent(), handle_fork_error(), init_pipe_and_fds(), MOOD_HAPPY, t_shell::ui, and update_waifu_mood().

Referenced by cmd_execute().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.