24# include <readline/readline.h>
25# include <readline/history.h>
29# include <sys/types.h>
33# include "../libft/libft.h"
41# define ERR_SYN_QUOTES "Syntax error: quotes unclosed\n"
42# define ERR_SYN_RD "Syntax error: redirections\n"
43# define ERR_SYN_PIPE "Syntax error: pipes\n"
44# define EXIT_SYN_QUOTES 2
46# define EXIT_SYN_PIPE 2
54# define FAIL_SYSCALL -1
56# define FAIL_SYSCALL_PARENT 1
57# define FAIL_SYSCALL_CHILD 2
150 int recently_invalid;
168extern int g_exit_code;
243int find_token(
char **input_array,
char **input,
int index);
264char **
combine_execs(
char **resplit,
char **new_execs,
char **execs);
340int echo(
char **args);
void executor(t_shell *shell)
Primary entrypoint of command execution.
Definition executor.c:76
bool handle_redirections_single_builtin(t_shell *shell)
Handle redirections for a single non-builtin command.
Definition single_builtin_utils.c:36
char ** create_input_array(t_shell *shell)
Create an array of input strings for tokenization.
Definition create_input_array.c:172
bool parser(t_shell *shell)
Parses the input using the lexer.
Definition parser.c:96
int create_heredoc(char *delimiter)
Creates a here-document file and gathers input.
Definition process_in_heredoc.c:69
void wait_pipeline(t_info *info, int number)
Wait for child processes to finish execution + update the exit code.
Definition handle_multi.c:59
void free_exp(t_expander *exp)
Frees resources associated with the expander struct.
Definition expander_utils.c:155
t_shell * init_shell(char **envp)
Initializes the main data structure (shell environment).
Definition init_shell.c:114
void delete_variable(t_shell *shell, char *arg)
Deletes an environment variable.
Definition unset_builtin.c:69
void handle_single_child(t_shell *shell)
Process creation for execution of non-builtin command.
Definition handle_single_child.c:165
void execute_builtin(t_shell *shell)
Execute a builtin-command.
Definition handle_single_builtin.c:56
int exit_builtin(t_shell *shell, char **args, bool in_child)
Handles the exit command when executed in a child process.
Definition exit_builtin.c:65
void no_delim_found(char *str, int *len)
Checks if there is a quote('\'' or '"') at the current position the given string.
Definition input_array_utils.c:62
void handle_multi(t_shell *shell, t_executor *current)
Handles the execution of multiple commands.
Definition handle_multi.c:116
int get_end_quote(const char *input, char c)
Find the index of the end of a quoted part in a string.
Definition check_quote_syntax.c:33
char * ft_strjoin_expander(char const *s1, char const *s2)
Concatenates two strings and expands the memory as needed.
Definition expander_utils.c:65
char * get_absolute_path(t_shell *shell)
Transform the non-absolut path into one.
Definition single_child_utils.c:64
int cd_no_args(t_shell *shell)
Handles the case when changing to a directory with no arguments.
Definition cd_builtin.c:124
bool restore_stds(int stdin_cpy, int stdout_cpy)
Restore the stdin and stdout file descriptors.
Definition single_builtin_utils.c:83
char ** free_str_arr(char **arr)
Frees the allocated memory of a string array.
Definition free_heap.c:28
t_executor * prepare_executor(t_shell *shell)
Prepare a linked list of t_executor elements for the executor.
Definition prep_executor.c:138
void transform_error(char **environ_array)
Error handling while copying the environment list into the array.
Definition single_child_utils_2.c:83
char ** transform_environ_array(t_shell *shell)
Copy the environment variables into an array for execve.
Definition single_child_utils_2.c:137
void get_path_error(char *str)
Handle errors when obtaining an empty path to an executable.
Definition single_child_utils.c:132
t_lexer_list * fill_lexer_list(char **input_array)
Fills the lexer list using the input array.
Definition tokenize.c:110
char ** create_environ_array(t_environ_list *environ)
Create the environment array based on the list size.
Definition single_child_utils_2.c:57
bool is_builtin(char *exec)
Check if a given command is a builtin-command.
Definition handle_single_builtin.c:34
void free_heap(t_shell *shell)
Frees the memory used by the main data structure (shell environment).
Definition free_heap.c:143
bool close_outputfile(int fd)
Closes and file descriptor.
Definition process_out_append.c:29
int get_size_executor(t_lexer_list *head)
Calculates the number of t_executor elements/nodes needed.
Definition prep_executor.c:32
int find_token(char **input_array, char **input, int index)
Finds predefined tokens in the input string and adds it to the input_array.
Definition create_input_array.c:101
void set_executor_defaults(t_executor *node)
Sets default values for a t_executor node.
Definition prep_executor.c:77
void set_path_executor(t_executor *list, t_environ_list *environ)
Sets the path variable for t_executor nodes.
Definition set_path_executor.c:29
int echo(char **args)
Implements the "echo" command to display text to the standard output.
Definition echo_builtin.c:85
int len_substr(char *str)
Calculates the length of a substring('token') until a delimiter or a token is found.
Definition create_input_array.c:33
struct s_environ_node t_environ_node
Structure representing an environment variable node.
char * execute_other_helper(t_executor *current)
Helper function to find the executable path.
Definition child_handler_multi.c:98
int handle_pipeline(int *fildes, t_executor *current, t_info *info, int i)
Handles the execution of a pipeline of commands.
Definition handle_pipeline.c:96
void child(t_shell *shell, char **args, int number)
Handles the exit command when executed within a child process.
Definition exit_builtin_utils.c:50
bool check_pipe_syntax(t_lexer_list *lexer)
Performs a pipe syntax check on the given list of lexems t_lexer_list *lexer.
Definition parser.c:30
void check_errno(t_shell *shell, char *path)
Check and handle errors related to command execution.
Definition single_child_utils.c:98
char * replace_with_clean(char *rl_copy, size_t start, size_t end)
Removes characters from a string that are before and after a given index.
Definition clean_rl_copy.c:50
void close_fds(t_executor *current)
Close file descriptors based on command redirection settings.
Definition executor.c:29
t_expander * init_expander(char *str)
Initializes the expander struct for variable expansion.
Definition expander_utils.c:125
void check_exitcode(int wstatus, bool *is_print)
Checks the exit code of a child process and update g_exit_code.
Definition check_exitcode.c:36
void executor(t_shell *shell)
Primary entrypoint of command execution.
Definition executor.c:76
int count_elements(char *rl_copy)
Count the number of elements in the input string.
Definition create_input_array.c:58
int handle_redirections_pipeline(int *fildes, t_executor *current, t_info *info)
Handles file redirections and file descriptor setup.
Definition child_handler_multi.c:32
void signal_setup(int process)
Sets up signal handlers.
Definition signal_setup.c:56
int env_var_update(t_shell *shell, char *old, char *new)
Update PWD after directory change.
Definition cd_builtin.c:83
int process_command(t_executor *current, t_lexer_list *lexer)
Processes the command part for the given node of the executor list.
Definition fill_executor_list.c:101
t_executor * fill_executor_list(t_shell *shell, t_executor *list)
Fills the t_executor linked list with info about the parsed commands.
Definition fill_executor_list.c:169
t_token which_type(char *s)
Set the type of token.
Definition tokenize.c:48
int get_size_execs(t_lexer_list *lexer)
Calculates the number of consecutive NONE type lexer list nodes.
Definition process_command.c:32
char ** get_execs_array(t_lexer_list *lexer, int size)
Creates an array of the found NONE type tokens from the lexer list.
Definition process_command.c:56
struct s_expander t_expander
Structure used in variable expansion.
int get_size_environ(t_environ_node *current)
Get the size of the environment list.
Definition single_child_utils_2.c:33
t_lexer_list * tokenize(t_shell *shell)
Tokenizes the input string and creates the lexer list.
Definition tokenize.c:149
int parent_handler_multi(int *fildes, t_executor *current)
Handles the parent process during multi-command execution.
Definition handle_pipeline.c:67
int export_no_args(t_shell *shell)
Handles the "export" command without arguments.
Definition export_builtin.c:32
t_executor * init_executor_list(int size)
Initializes the t_executor linked list.
Definition prep_executor.c:101
t_token assign_type(char tok)
Assign a type based on the input char.
Definition tokenize.c:26
int handle_parent(t_shell *shell, char **args, int number)
Handles the exit command when executed within the parent process.
Definition exit_builtin_utils.c:149
int ft_isspace(int c)
Checks if the given character is a whitespace.
Definition clean_rl_copy.c:30
int execute_other(t_executor *current, t_info *info)
Executes a non-built-in command in a child process using execve.
Definition child_handler_multi.c:137
int cd(t_shell *shell, char **args)
Handles changing the current working directory with specified arguments.
Definition cd_builtin.c:163
void exporting(t_shell *shell, char *str)
Updates or creates environment variables based on user-provided arguments.
Definition export_builtin.c:76
void update_old(t_shell *shell, char *old)
Update OLDPWD.
Definition cd_builtin.c:50
bool check_quote_syntax(const char *input)
Check the syntax of quotes in the input string.
Definition check_quote_syntax.c:59
t_executor * free_executor_list(t_executor *list)
Frees the allocated memory of a linked list of t_executor elements.
Definition free_heap.c:112
bool check_execs_after_expand(t_shell *shell)
Checks and processes execution commands after expansion.
Definition check_execs_after_expand.c:139
void ft_error(t_executor *current, t_info *info)
Handle errors during command execution and set the exit code accordingly.
Definition handle_multi.c:88
void handle_child(t_shell *shell, char **args, int number)
Handles the exit command when executed within a child process.
Definition exit_builtin_utils.c:86
t_lexer_list * lexer(t_shell *shell)
Lexical analysis and tokenization of the input string.
Definition parser.c:64
bool expander(t_shell *shell)
Starting point of variable expansion.
Definition expander.c:167
void free_environ(t_environ_list *environ)
Frees the memory used by the environment list and its nodes.
Definition free_heap.c:52
bool is_token2(char to_check)
Check if a character is a token.
Definition input_array_utils.c:97
t_executor * process_lexemes(t_executor *list, t_executor *current, t_lexer_list **lexer)
Processes lexemes and sets values in the given t_executor node.
Definition fill_executor_list.c:132
void transform_helper(t_environ_node *current, char **environ_array)
Copy environment variables into the env array.
Definition single_child_utils_2.c:100
char * get_path(t_shell *shell, bool printerror)
Get the path to an executable or convert it to an absolute path.
Definition single_child_utils.c:151
int process_out_append(t_executor *current, t_lexer_list *lexer)
Processes output redirections and append for the given node of the executor list.
Definition fill_executor_list.c:33
char * interpolate(t_shell *shell, char *str)
Interpolates variables and handles quotes within a shell command.
Definition interpolate.c:142
struct s_environ_list t_environ_list
Structure representing a list of environment variables.
t_environ_list * init_environ(char **envp)
Initializes the environment variables linked list.
Definition init_shell.c:81
int env(t_shell *shell, char **args)
Displays the environment variables and their values.
Definition env_builtin.c:61
int handle_pipes(int fildes[2])
Creates a pipe and sets the file descriptors in the given array.
Definition handle_pipeline.c:29
int handle_single_builtin(t_shell *shell)
Handle the execution of a single builtin-command.
Definition handle_single_builtin.c:88
int expand_var(t_shell *shell, char **str, t_expander *exp, int pos)
Expand variables.
Definition expander.c:129
int export_args(t_shell *shell, char **args)
Handles the export command with specified arguments.
Definition export_builtin.c:114
char ** fill_input_array(char **input_array, char *input)
Fills the array of strings with substrings('tokens') from the input string.
Definition create_input_array.c:133
void execute_builtin_child(t_executor *current, t_info *info)
Executes a built-in command in a child process and exits.
Definition child_handler_multi.c:62
void lvl_up(t_shell *shell)
Increases the shell level (SHLVL) environment variable.
Definition init_shell.c:146
struct s_executor t_executor
Structure representing an executor node.
t_info * init_info(t_shell *shell)
Initializes the t_info structure.
Definition handle_multi.c:30
int process_in_heredoc(t_executor *current, t_lexer_list *lexer)
Processes input redirections and heredoc for the given node of the executor list.
Definition fill_executor_list.c:70
void unset_arg(t_shell *shell, char *arg, int *fails)
Unsets an environment variable or prints an error message.
Definition unset_builtin.c:108
int after_expand(t_executor *current)
Process the execs array of the current executor node after expansion.
Definition check_execs_after_expand.c:105
bool is_token(char to_check)
Check if a character is a token.
Definition input_array_utils.c:80
int get_sizeof_args(char **args)
Retrieves the number of arguments in an array of strings.
Definition pwd_builtin.c:33
void handle_single(t_shell *shell)
Handle the execution of a single command.
Definition executor.c:50
s_token
Enumeration representing different token types.
Definition minishell.h:96
int pwd(void)
Executes the "pwd" command to print the current working directory.
Definition pwd_builtin.c:54
bool handle_redirections_single_child(t_shell *shell)
Handle redirections for single non-builtin command in child process.
Definition single_child_utils.c:36
int child_handler_multi(int *fildes, t_executor *current, t_info *info)
Handles the execution of a child process in a multi-command.
Definition child_handler_multi.c:175
pid_t handle_forks(void)
Creates a child process using fork() and returns the process ID.
Definition handle_pipeline.c:47
char * realloc_str(char *res, int pos)
Reallocates memory for a string.
Definition expander_utils.c:104
char * ft_charjoin_expander(char const *s, char const c)
Concatenates character to the end of a string.
Definition expander_utils.c:33
int skip_quotes(char *str)
Skips over a quoted section ('\'' or '"') of a string.
Definition input_array_utils.c:32
struct s_lexer_list t_lexer_list
Structure representing a list of lexemes.
t_environ_node * get_node(t_environ_list *environ, char *key)
Find an node by the key from an environment variables linked list.
Definition environ_list_utils.c:96
t_lexer_list * free_lex(t_lexer_list *lex_head)
Frees the allocated memory of a linked list of lexer elements.
Definition free_heap.c:82
t_environ_list * empty_environ(t_environ_list *environ)
Initializes environment variables for an empty environment.
Definition init_shell.c:50
bool is_arg_number(char *arg)
Checks if a string represents a valid numeric argument.
Definition exit_builtin.c:34
t_environ_node * create_environ_node(char *var)
Creates an node for an environment variable linked list.
Definition environ_list_utils.c:60
struct s_shell t_shell
Structure representing the main data structure.
void saving_stds(int *stdin_cpy, int *stdout_cpy)
Save the stdin and stdout file descriptors.
Definition single_builtin_utils.c:64
struct s_info t_info
Structure representing information for pipeline execution.
t_environ_node * add_back_environ_node(t_environ_list *environ, t_environ_node *node)
Adds an node to the end of an environment variables linked list.
Definition environ_list_utils.c:30
char * handle_quotes(t_shell *shell, char **str, char end_quote, t_expander *inter)
Handles quoted strings within a shell command.
Definition handle_quotes.c:78
pid_t ft_getpid(void)
Retrieves the process id (PID) of the current process.
Definition ft_getpid.c:97
void parent(t_shell *shell, char **args, int number)
Handles the exit command when executed within the parent process.
Definition exit_builtin_utils.c:111
char ** combine_execs(char **resplit, char **new_execs, char **execs)
Combine resplit array with execs array into new_execs array.
Definition check_execs_after_expand.c:72
enum s_token t_token
Enumeration representing different token types.
int export(t_shell *shell, char **args)
Implements the export builtin function.
Definition export_builtin.c:158
bool open_inputfile(t_executor *current, t_lexer_list *lexer)
Handles opening an input file.
Definition process_in_heredoc.c:96
int unset(t_shell *shell, char **args)
Executes the "unset" command to remove environment variables.
Definition unset_builtin.c:130
t_lexer_list * create_lexer_list(char **input_array)
Allocates memory for the lexer list.
Definition tokenize.c:83
bool open_outputfile(t_executor *current, t_lexer_list *lexer)
Handles opening an output file.
Definition process_out_append.c:48
bool get_input(int fd, char *delimiter)
Reads input for a here-document until a specified delimiter.
Definition process_in_heredoc.c:32
char * clean_rl_copy(char *rl_copy)
Create a copy of the input string with its leading and trailing whitespaces removed.
Definition clean_rl_copy.c:89
t_executor * create_executor_list(int size)
Create a linked list of t_executor nodes/elements.
Definition prep_executor.c:56
Structure representing a list of environment variables.
Definition minishell.h:77
Structure representing an environment variable node.
Definition minishell.h:86
Structure representing an executor node.
Definition minishell.h:121
Structure used in variable expansion.
Definition minishell.h:140
Structure representing information for pipeline execution.
Definition minishell.h:159
Structure representing a list of lexemes.
Definition minishell.h:109
Structure representing the main data structure.
Definition minishell.h:65