MiniDevil As beautiful as a shell
executor.h File Reference

Executor prototypes for commands, pipes, redirections & heredocs. More...

#include <sys/types.h>
#include "structs.h"
+ Include dependency graph for executor.h:
+ This graph shows which files directly or indirectly include this file:

Detailed Description

Executor prototypes for commands, pipes, redirections & heredocs.

Definition in file executor.h.

Functions

int executor (t_ast *node, t_shell *shell)
 Main AST executor that dispatches by node type. More...
 
int open_redir_file (char *file, t_node_type type)
 Open a file for redirection (based on the redirection type) More...
 
int setup_redirection (int fd, t_node_type type)
 Redirect STDIN or STDOUT to fd, saving the original. More...
 
void restore_fd (int saved_fd, t_node_type type)
 Restore a previously saved file descriptor. More...
 
int handle_redir (t_ast *node, t_shell *shell)
 Execute a redirection node. More...
 
void exec_right_pipe_child (t_ast *right, int pipe_fd[2], t_shell *shell)
 Execute the right side of a pipe in a child process. More...
 
void exec_left_pipe_child (t_ast *left, int pipe_fd[2], t_shell *shell)
 Execute the left side of a pipe in a child process. More...
 
int handle_pipe (t_ast *node, t_shell *shell)
 Execute a pipe node by forking left and right children. More...
 
int exec_cmd_not_found (char *cmd)
 Print "command not found" to STDERR. More...
 
int is_builtin (char *cmd)
 Check if a command is a shell builtin. More...
 
int exec_builtin (char **args, t_shell *shell)
 Dispatch a builtin command to its function. More...
 
int exec_simple_command (char **args, t_shell *shell)
 Execute a simple command (builtin or external) More...
 
int pipe_fork_error (int pipe_fd[2], pid_t left_pid)
 Clean up pipe fds and wait for left child on fork failure. More...
 
char * search_in_dir (char *dir, char *cmd)
 Search an executable command in a directory. More...
 
char * find_cmd_path (char *cmd, t_env *env)
 Resolve a command name into its full executable path. More...
 
int exec_external (char **args, t_shell *shell)
 Fork and execute an external command. More...
 
int handle_heredoc (char *delimiter, int quoted, t_shell *shell)
 Execute a heredoc (read input and return a readable fd) More...
 
int walk_heredocs (t_ast *node, t_shell *shell)
 Recursively walk the AST collecting all heredocs. More...
 
int collect_heredocs (t_ast *node, t_shell *shell)
 Pre collect all heredocs in the AST before the execution phase. More...
 

Function Documentation

◆ executor()

int executor ( t_ast node,
t_shell shell 
)

Main AST executor that dispatches by node type.

Parameters
nodeRoot of the AST subtree to execute
shellShell context
Returns
Exit status of the executed pipe/command and 0 for NULL nodes

Definition at line 42 of file executor.c.

References exec_command_node(), handle_pipe(), handle_redir(), NODE_COMMAND, NODE_PIPE, NODE_REDIR_HEREDOC, NODE_REDIR_IN, and t_ast::type.

Referenced by exec_left_pipe_child(), exec_right_pipe_child(), handle_redir(), process_input(), and process_ui_input().

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

◆ open_redir_file()

int open_redir_file ( char *  file,
t_node_type  type 
)

Open a file for redirection (based on the redirection type)

Opens the file with appropriate flags:

  • < O_RDONLY for input
  • > O_WRONLY | O_CREAT | O_TRUNC for output
  • >> O_WRONLY | O_CREAT | O_APPEND for append
Parameters
fileFilename to open
typeRedirection node type
Returns
File descriptor on success or -1 on failure

Definition at line 30 of file executor_redir.c.

References NODE_REDIR_APPEND, NODE_REDIR_IN, and NODE_REDIR_OUT.

Referenced by handle_redir().

+ Here is the caller graph for this function:

◆ setup_redirection()

int setup_redirection ( int  fd,
t_node_type  type 
)

Redirect STDIN or STDOUT to fd, saving the original.

  • Saves the original fd via dup and replaces it with dup2
  • Closes the source fd after duplication
Parameters
fdFile descriptor to redirect to
typeRedirection type (to determine STDIN or STDOUT target)
Returns
Saved original fd or -1 on error

Definition at line 59 of file executor_redir.c.

References NODE_REDIR_HEREDOC, and NODE_REDIR_IN.

Referenced by handle_redir().

+ Here is the caller graph for this function:

◆ restore_fd()

void restore_fd ( int  saved_fd,
t_node_type  type 
)

Restore a previously saved file descriptor.

Parameters
saved_fdSaved fd from setup_redirection()
typeRedirection type

Definition at line 90 of file executor_redir.c.

References NODE_REDIR_HEREDOC, and NODE_REDIR_IN.

Referenced by handle_redir().

+ Here is the caller graph for this function:

◆ handle_redir()

int handle_redir ( t_ast node,
t_shell shell 
)

Execute a redirection node.

  • Opens the redirection target (file or heredoc)
  • Saves the original fd
  • Executes the child command with the redirected fd
  • Restores the original fd
Parameters
nodeAST redirection node
shellShell context
Returns
Exit status of the child command or 1 on error
Note
For precollected heredocs, uses the cached heredoc_fd

Definition at line 117 of file executor_redir.c.

References t_redir_node::cmd, t_ast::data, executor(), t_redir_node::file, t_redir_node::heredoc_fd, NODE_REDIR_HEREDOC, open_redir_file(), t_ast_data::redir, restore_fd(), setup_redirection(), and t_ast::type.

Referenced by executor().

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

◆ exec_right_pipe_child()

void exec_right_pipe_child ( t_ast right,
int  pipe_fd[2],
t_shell shell 
)

Execute the right side of a pipe in a child process.

Closes the write end of the pipe and redirects STDIN to the read end

Parameters
rightRight AST subtree to execute
pipe_fdPipe file descriptors: [read-—write]
shellShell context
Note
Never returns (calls exit)

Definition at line 60 of file executor_pipe.c.

References t_shell::current_ast, t_shell::current_input, t_shell::env, executor(), free_ast(), free_env_list(), t_shell::is_child, and reset_child_signals().

Referenced by handle_pipe().

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

◆ exec_left_pipe_child()

void exec_left_pipe_child ( t_ast left,
int  pipe_fd[2],
t_shell shell 
)

Execute the left side of a pipe in a child process.

Closes the read end of the pipe and redirects STDOUT to the write end

Parameters
leftLeft AST subtree to execute
pipe_fdPipe file descriptors: [read-—write]
shellShell context
Note
Never returns (calls exit)

Definition at line 33 of file executor_pipe.c.

References t_shell::current_ast, t_shell::current_input, t_shell::env, executor(), free_ast(), free_env_list(), t_shell::is_child, and reset_child_signals().

Referenced by handle_pipe().

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

◆ handle_pipe()

int handle_pipe ( t_ast node,
t_shell shell 
)

Execute a pipe node by forking left and right children.

Creates a pipe, forks 2 children:

  • Left child writes to pipe and right reads from it
  • Parent closes both pipe ends & waits for both children
Parameters
nodeAST pipe node with binary.left and binary.right subtrees
shellShell context
Returns
Exit status of the rightmost command in the pipe

Definition at line 119 of file executor_pipe.c.

References t_ast_data::binary, t_ast::data, exec_left_pipe_child(), exec_right_pipe_child(), t_binary_node::left, pipe_fork_error(), t_binary_node::right, and wait_for_pipe_children().

Referenced by executor().

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

◆ exec_cmd_not_found()

int exec_cmd_not_found ( char *  cmd)

Print "command not found" to STDERR.

Parameters
cmdCommand name that wasn't found
Returns
always 127 (standard bash)

Definition at line 25 of file exec_utils.c.

Referenced by prepare_exec().

+ Here is the caller graph for this function:

◆ is_builtin()

int is_builtin ( char *  cmd)

Check if a command is a shell builtin.

Parameters
cmdCommand name to check
Returns
1 if builtin or 0 if external

Definition at line 39 of file exec_utils.c.

Referenced by exec_simple_command().

+ Here is the caller graph for this function:

◆ exec_builtin()

int exec_builtin ( char **  args,
t_shell shell 
)

Dispatch a builtin command to its function.

Parameters
argsArgument array with arg[0] being the command name
shellShell context
Returns
Exit status from the builtin function

Definition at line 67 of file exec_utils.c.

References builtin_cd(), builtin_echo(), builtin_env(), builtin_exit(), builtin_export(), builtin_pwd(), builtin_unset(), and t_shell::env.

Referenced by exec_simple_command().

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

◆ exec_simple_command()

int exec_simple_command ( char **  args,
t_shell shell 
)

Execute a simple command (builtin or external)

Parameters
argsNULL terminated argument array
shellShell context
Returns
Exit status of the command

Definition at line 95 of file exec_utils.c.

References exec_builtin(), exec_external(), and is_builtin().

Referenced by exec_command_node().

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

◆ pipe_fork_error()

int pipe_fork_error ( int  pipe_fd[2],
pid_t  left_pid 
)

Clean up pipe fds and wait for left child on fork failure.

Parameters
pipe_fdPipe file descriptors to close
left_pidPID of left child (0 if wasn't yet forked)
Returns
always 1 (exit code = error)

Definition at line 111 of file exec_utils.c.

Referenced by handle_pipe().

+ Here is the caller graph for this function:

◆ search_in_dir()

char* search_in_dir ( char *  dir,
char *  cmd 
)

Search an executable command in a directory.

Joins dir and cmd then checks if their result is executable via access(X_OK)

Parameters
dirDirectory to search in
cmdCommand name to look for
Returns
Full path if found AND executable or NULL

Definition at line 61 of file path.c.

References join_path().

Referenced by find_cmd_path(), and search_in_path().

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

◆ find_cmd_path()

char* find_cmd_path ( char *  cmd,
t_env env 
)

Resolve a command name into its full executable path.

If the command contains / it's treated as a direct path. Otherwise it looks up PATH in env and searches each directory until an executable match is found

Parameters
cmdCommand name or path to resolve
envEnvironment list
Returns
Full path, CWD match if PATH is unset or NULL if not found

Definition at line 115 of file path.c.

References get_env_value(), is_direct_path(), search_in_dir(), and search_in_path().

Referenced by prepare_exec().

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

◆ exec_external()

int exec_external ( char **  args,
t_shell shell 
)

Fork and execute an external command.

Resolves the path, builds envp and forks a child that calls execve

  • If already in a child process, executes directly without forking
  • The parent waits for the child and returns its exit status code
Parameters
argsArgument array with arg[0] being the command name
shellShell context
Returns
exit status (0 success, 126 exec error or 127 not found)

Definition at line 150 of file exec_cmd.c.

References child_execute(), t_shell::env, t_shell::is_child, prepare_exec(), and wait_for_child().

Referenced by exec_simple_command().

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

◆ handle_heredoc()

int handle_heredoc ( char *  delimiter,
int  quoted,
t_shell shell 
)

Execute a heredoc (read input and return a readable fd)

  • Creates a pipe
  • Reads lines into the write end until the delimiter is reached
  • Closes the write end and returns the read end
  • If the delimiter was quoted, no expansion occurs
  • On SIGINT closes both pipe ends and restores STDIN
Parameters
delimiterHeredoc delimiter
quotedWhether the delimiter was quoted or not
shellShell context (for signals and variable expansion)
Returns
Read end fd of the heredoc pipe or -1 on error/interrupted

Definition at line 133 of file heredoc.c.

References g_signal, read_heredoc_lines(), restore_stdin(), setup_heredoc_signals(), and setup_interactive_signals().

Referenced by collect_one().

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

◆ walk_heredocs()

int walk_heredocs ( t_ast node,
t_shell shell 
)

Recursively walk the AST collecting all heredocs.

It traverses the pipe & redirection nodes first to ensure all heredoc prompts appear before any command runs

Parameters
nodeRoot of the AST Subtree
shellShell context
Returns
0 on success and -1 if a heredoc fails (SIGINT or error)

Definition at line 95 of file heredoc_collect.c.

References t_ast_data::binary, t_ast::data, t_binary_node::left, NODE_PIPE, NODE_REDIR_HEREDOC, NODE_REDIR_IN, t_binary_node::right, t_ast::type, and walk_redir().

Referenced by collect_heredocs(), and walk_redir().

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

◆ collect_heredocs()

int collect_heredocs ( t_ast node,
t_shell shell 
)

Pre collect all heredocs in the AST before the execution phase.

Walks the entire AST to read all heredoc inputs first. On failure it closes any previously collected heredocs fds to prevent leaks

Parameters
nodeRoot of the AST
shellShell context
Returns
0 on success and -1 on failure

Definition at line 120 of file heredoc_collect.c.

References close_heredoc_fds(), and walk_heredocs().

Referenced by process_input(), and process_ui_input().

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

Go to the source code of this file.