MiniDevil
As beautiful as a shell
Executor prototypes for commands, pipes, redirections & heredocs. More...
Include dependency graph for executor.h:
This graph shows which files directly or indirectly include this file: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... | |
Main AST executor that dispatches by node type.
| node | Root of the AST subtree to execute |
| shell | Shell context |
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:| 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| file | Filename to open |
| type | Redirection node type |
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:| int setup_redirection | ( | int | fd, |
| t_node_type | type | ||
| ) |
Redirect STDIN or STDOUT to fd, saving the original.
| fd | File descriptor to redirect to |
| type | Redirection type (to determine STDIN or STDOUT target) |
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:| void restore_fd | ( | int | saved_fd, |
| t_node_type | type | ||
| ) |
Restore a previously saved file descriptor.
| saved_fd | Saved fd from setup_redirection() |
| type | Redirection 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:Execute a redirection node.
| node | AST redirection node |
| shell | Shell context |
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: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
| right | Right AST subtree to execute |
| pipe_fd | Pipe file descriptors: [read-—write] |
| shell | Shell context |
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: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
| left | Left AST subtree to execute |
| pipe_fd | Pipe file descriptors: [read-—write] |
| shell | Shell context |
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:Execute a pipe node by forking left and right children.
Creates a pipe, forks 2 children:
| node | AST pipe node with binary.left and binary.right subtrees |
| shell | Shell context |
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:| int exec_cmd_not_found | ( | char * | cmd | ) |
Print "command not found" to STDERR.
| cmd | Command name that wasn't found |
Definition at line 25 of file exec_utils.c.
Referenced by prepare_exec().
Here is the caller graph for this function:| int is_builtin | ( | char * | cmd | ) |
Check if a command is a shell builtin.
| cmd | Command name to check |
Definition at line 39 of file exec_utils.c.
Referenced by exec_simple_command().
Here is the caller graph for this function:| int exec_builtin | ( | char ** | args, |
| t_shell * | shell | ||
| ) |
Dispatch a builtin command to its function.
| args | Argument array with arg[0] being the command name |
| shell | Shell context |
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:| int exec_simple_command | ( | char ** | args, |
| t_shell * | shell | ||
| ) |
Execute a simple command (builtin or external)
| args | NULL terminated argument array |
| shell | Shell context |
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:| int pipe_fork_error | ( | int | pipe_fd[2], |
| pid_t | left_pid | ||
| ) |
Clean up pipe fds and wait for left child on fork failure.
| pipe_fd | Pipe file descriptors to close |
| left_pid | PID of left child (0 if wasn't yet forked) |
Definition at line 111 of file exec_utils.c.
Referenced by handle_pipe().
Here is the caller graph for this function:| 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)
| dir | Directory to search in |
| cmd | Command name to look for |
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:| 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
| cmd | Command name or path to resolve |
| env | Environment list |
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:| 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
| args | Argument array with arg[0] being the command name |
| shell | Shell context |
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:| int handle_heredoc | ( | char * | delimiter, |
| int | quoted, | ||
| t_shell * | shell | ||
| ) |
Execute a heredoc (read input and return a readable fd)
| delimiter | Heredoc delimiter |
| quoted | Whether the delimiter was quoted or not |
| shell | Shell context (for signals and variable expansion) |
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:Recursively walk the AST collecting all heredocs.
It traverses the pipe & redirection nodes first to ensure all heredoc prompts appear before any command runs
| node | Root of the AST Subtree |
| shell | Shell context |
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: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
| node | Root of the AST |
| shell | Shell context |
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: