Executor
Walks the AST and runs each node: simple commands, pipelines, logical operators, subshells, blocks, and background jobs.
defines the executor functions for handling AST nodes.
- Author
zweng, pulgamecanica
Functions
-
int executor_execute(struct s_shell *shell, t_ast *ast)
Main dispatch function for executing AST nodes.
- Parameters:
shell – Pointer to the central shell state
ast – Pointer to the AST node to execute
- Returns:
Exit status code
-
int execute_simple_command(struct s_shell *shell, t_cmd *cmd)
Node-type executors.
- Parameters:
shell – Pointer to the central shell state
ast – Pointer to the AST node to execute
- Returns:
Exit status code
-
int setup_redirections(t_list *redirs, int saved_fds[3])
redirection setup
redirection setup
If saved_fds is not NULL, save stdin/stdout/stderr first (for restore).
- Parameters:
redirs – List of redirection nodes
saved_fds – Array to save original file descriptors
redirs – The list of redirections.
saved_fds – The array to store the saved file descriptors.
- Returns:
Exit status code
- Returns:
0 on success, -1 on error.
-
void restore_redirections(int saved_fds[3])
Restore redirections.
Restore redirections.
- Parameters:
saved_fds – Array of saved file descriptors
saved_fds – The array of saved file descriptors.
-
int setup_heredoc(t_redir *redir)
Heredoc setup.
Heredoc setup.
the write end, close the write end, and return the read end fd.
- Parameters:
redir – Pointer to the redirection node
redir – The redirection structure.
- Returns:
Exit status code
- Returns:
The read-end fd on success, -1 on error.
-
char *find_command(struct s_shell *shell, const char *name)
Command search (PATH)
- Parameters:
shell – Pointer to the central shell state
name – Name of the command to search for
- Returns:
Pointer to the found command, or NULL if not found
-
void exec_pipeline_external(struct s_shell *shell, t_cmd *cmd)
Pipeline helper (called from pipe_child, does not return)
- Parameters:
shell – Pointer to the central shell state
cmd – Pointer to the command node
-
int get_exit_status(int wstatus)
get exit status from wait status
get exit status from wait status
Normal exit: WEXITSTATUS (0-255)
Killed by signal: 128 + signal number
- Parameters:
wstatus – Wait status code
wstatus – The waitpid status.
- Returns:
Exit status code
- Returns:
The shell exit code.
-
void split_assignment(const char *assign, char **name, char **value)
Split “NAME=value” at first ‘=’ into separate name and value strings.
Caller must free both *name and *value.
If no ‘=’ found, *name = dup of assign, *value = dup of “”.
- Parameters:
assign – The assignment string.
name – The pointer to store the name.
value – The pointer to store the value.
executor.c — Dispatch
Command execution functionality for 42sh.
- Author
wengzhang, pulgamecanica
exec_command.c — Simple Commands
Command execution functionality for 42sh.
- Author
wengzhang, pulgamecanica
Functions
-
static void apply_assignments(t_shell *shell, t_list *assigns, int do_export)
Apply assignments permanently to shell variables.
Used when command is empty (bare assignment) or in child for execve.
- Parameters:
shell – The shell instance.
assigns – The list of assignments.
do_export – Whether to export the variables.
-
static int exec_assignment_only(t_shell *shell, t_cmd *cmd)
Handle empty command (just assignments and/or redirections).
Example: FOO=bar or FOO=bar > file
- Parameters:
shell – The shell instance.
cmd – The command structure.
- Returns:
0 on success, 1 on failure.
-
static int save_and_apply_assigns(t_shell *shell, t_list *assigns, char **old_names, char **old_vals)
Save old values, apply temporary assignments for builtin scope.
old_names/old_vals arrays store what to restore afterward.
- Parameters:
shell – The shell instance.
assigns – The list of assignments.
old_names – Array to store old variable names.
old_vals – Array to store old variable values.
- Returns:
Count of saved assignments.
-
static void restore_assigns(t_shell *shell, char **old_names, char **old_vals, int count)
Restore old variable values after builtin execution.
- Parameters:
shell – The shell instance.
old_names – Array of old variable names.
old_vals – Array of old variable values.
count – Number of assignments to restore.
-
static int exec_builtin(t_shell *shell, t_cmd *cmd, t_builtin_fn fn)
Execute a builtin with temporary assignments and redirections.
Assignments are scoped to this command only, then restored.
- Parameters:
shell – The shell instance.
cmd – The command structure.
fn – The builtin function to execute.
- Returns:
The exit status of the builtin.
exec_logical.c — &&, ||, ;
Logical command execution functionality for 42sh.
- Author
wengzhang, pulgamecanica
Functions
-
int execute_and(t_shell *shell, t_ast *ast)
Execute logical AND operation.
cmd1 && cmd2 : run right only if left succeeds (exit 0)
- Parameters:
shell – The shell instance.
ast – The abstract syntax tree node.
- Returns:
The exit status of the operation.
exec_pipeline.c — Pipes
Pipeline command execution functionality for 42sh.
- Author
wengzhang, pulgamecanica
Functions
-
static int collect_pipeline(t_ast *ast, t_ast **cmds, int max)
Flatten nested PIPE nodes left-to-right into a flat array.
Example: (A | B) | C => [A, B, C]
- Parameters:
ast – The abstract syntax tree node.
cmds – The array to store the pipeline commands.
max – The maximum number of commands to store.
- Returns:
The number of commands collected.
-
static void close_pipes(int pipes[][2], int count)
Close all pipe fds in the array.
- Parameters:
pipes – The array of pipe file descriptors.
count – The number of pipes to close.
-
static void pipe_child(t_shell *shell, t_ast *cmd_ast, int pipes[][2], int info[3])
Execute a single pipeline stage in the child process.
Wire stdin/stdout from pipes, then exec or run builtin.
- Parameters:
shell – The shell instance.
cmd_ast – The abstract syntax tree node for the command.
pipes – The array of pipe file descriptors.
info – The information array for the pipeline stage.
exec_subshell.c — Subshell, Block, Background
Functions
-
int execute_subshell(t_shell *shell, t_ast *ast)
Execute a command in a subshell.
( cmd ) - runs in a forked child (subshell).
- Parameters:
shell – The shell instance.
ast – The abstract syntax tree node.
- Returns:
The exit status of the command.
redirections.c — I/O Redirections
Functions
-
int setup_redirections(t_list *redirs, int saved_fds[3])
Set up all redirections in the list.
redirection setup
If saved_fds is not NULL, save stdin/stdout/stderr first (for restore).
- Parameters:
redirs – The list of redirections.
saved_fds – The array to store the saved file descriptors.
- Returns:
0 on success, -1 on error.
-
void restore_redirections(int saved_fds[3])
Restore stdin/stdout/stderr from saved fds, then close the saved copies.
Restore redirections.
- Parameters:
saved_fds – The array of saved file descriptors.
heredoc.c — Here-documents
Heredoc functionality for 42sh.
- Author
wengzhang, pulgamecanica
Functions
-
int setup_heredoc(t_redir *redir)
Set up a heredoc: create a pipe, write the collected content to.
Heredoc setup.
the write end, close the write end, and return the read end fd.
- Parameters:
redir – The redirection structure.
- Returns:
The read-end fd on success, -1 on error.
command_search.c — PATH Lookup
Command search functionality for 42sh.
- Author
wengzhang, pulgamecanica
Functions
-
static char *build_path(const char *dir, const char *name)
Build “dir/name” path. Caller must free the result.
- Parameters:
dir – The directory path.
name – The file name.
- Returns:
The full path, or NULL if allocation fails.
-
static void free_split(char **arr)
Search each directory in PATH for an executable named
name.- Parameters:
path_var – The PATH environment variable.
name – The command name.
- Returns:
Heap-allocated full path, or NULL if not found.
-
static char *search_path(const char *path_var, const char *name)
Search each directory in PATH for an executable named
name.- Parameters:
path_var – The PATH environment variable.
name – The command name.
- Returns:
Heap-allocated full path, or NULL if not found.
exec_utils.c — Utilities
Utility functions for command execution in 42sh.
- Author
wengzhang, pulgamecanica
Functions
-
int get_exit_status(int wstatus)
Convert raw waitpid status to shell exit code.
get exit status from wait status
Normal exit: WEXITSTATUS (0-255)
Killed by signal: 128 + signal number
- Parameters:
wstatus – The waitpid status.
- Returns:
The shell exit code.
-
void split_assignment(const char *assign, char **name, char **value)
Split “NAME=value” at first ‘=’ into separate name and value strings.
Caller must free both *name and *value.
If no ‘=’ found, *name = dup of assign, *value = dup of “”.
- Parameters:
assign – The assignment string.
name – The pointer to store the name.
value – The pointer to store the value.