minishell
Minishell Project for École 42
|
This is the headerfile for minishell. More...
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <unistd.h>
#include "../libft/libft.h"
Go to the source code of this file.
Classes | |
struct | s_shell |
Structure representing the main data structure. More... | |
struct | s_environ_list |
Structure representing a list of environment variables. More... | |
struct | s_environ_node |
Structure representing an environment variable node. More... | |
struct | s_lexer_list |
Structure representing a list of lexemes. More... | |
struct | s_executor |
Structure representing an executor node. More... | |
struct | s_expander |
Structure used in variable expansion. More... | |
struct | s_info |
Structure representing information for pipeline execution. More... | |
Typedefs | |
typedef struct s_shell | t_shell |
Structure representing the main data structure. | |
typedef struct s_environ_list | t_environ_list |
Structure representing a list of environment variables. | |
typedef struct s_environ_node | t_environ_node |
Structure representing an environment variable node. | |
typedef enum s_token | t_token |
Enumeration representing different token types. | |
typedef struct s_lexer_list | t_lexer_list |
Structure representing a list of lexemes. | |
typedef struct s_executor | t_executor |
Structure representing an executor node. | |
typedef struct s_expander | t_expander |
Structure used in variable expansion. | |
typedef struct s_info | t_info |
Structure representing information for pipeline execution. | |
Enumerations | |
enum | s_token { NONE = 0 , PIPE = 1 , TRUNCATE = 2 , APPEND = 3 , REDIRECT_INPUT = 4 , HEREDOC = 5 } |
Enumeration representing different token types. | |
Functions | |
pid_t | ft_getpid (void) |
Retrieves the process id (PID) of the current process. | |
void | signal_setup (int process) |
Sets up signal handlers. | |
void | check_exitcode (int wstatus, bool *is_print) |
Checks the exit code of a child process and update g_exit_code. | |
t_shell * | init_shell (char **envp) |
Initializes the main data structure (shell environment). | |
t_environ_list * | init_environ (char **envp) |
Initializes the environment variables linked list. | |
t_environ_list * | empty_environ (t_environ_list *environ) |
Initializes environment variables for an empty environment. | |
void | lvl_up (t_shell *shell) |
Increases the shell level (SHLVL) environment variable. | |
t_environ_node * | create_environ_node (char *var) |
Creates an node for an environment variable linked list. | |
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. | |
t_environ_node * | get_node (t_environ_list *environ, char *key) |
Find an node by the key from an environment variables linked list. | |
char ** | free_str_arr (char **arr) |
Frees the allocated memory of a string array. | |
void | free_environ (t_environ_list *environ) |
Frees the memory used by the environment list and its nodes. | |
t_lexer_list * | free_lex (t_lexer_list *lex_head) |
Frees the allocated memory of a linked list of lexer elements. | |
t_executor * | free_executor_list (t_executor *list) |
Frees the allocated memory of a linked list of t_executor elements. | |
void | free_heap (t_shell *shell) |
Frees the memory used by the main data structure (shell environment). | |
bool | check_pipe_syntax (t_lexer_list *lexer) |
Performs a pipe syntax check on the given list of lexems t_lexer_list *lexer. | |
t_lexer_list * | lexer (t_shell *shell) |
Lexical analysis and tokenization of the input string. | |
bool | parser (t_shell *shell) |
Parses the input using the lexer. | |
int | get_end_quote (const char *input, char c) |
Find the index of the end of a quoted part in a string. | |
bool | check_quote_syntax (const char *input) |
Check the syntax of quotes in the input string. | |
int | ft_isspace (int c) |
Checks if the given character is a whitespace. | |
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. | |
char * | clean_rl_copy (char *rl_copy) |
Create a copy of the input string with its leading and trailing whitespaces removed. | |
t_token | assign_type (char tok) |
Assign a type based on the input char. | |
t_token | which_type (char *s) |
Set the type of token. | |
t_lexer_list * | create_lexer_list (char **input_array) |
Allocates memory for the lexer list. | |
t_lexer_list * | fill_lexer_list (char **input_array) |
Fills the lexer list using the input array. | |
t_lexer_list * | tokenize (t_shell *shell) |
Tokenizes the input string and creates the lexer list. | |
int | len_substr (char *str) |
Calculates the length of a substring('token') until a delimiter or a token is found. | |
int | count_elements (char *rl_copy) |
Count the number of elements in the input string. | |
int | find_token (char **input_array, char **input, int index) |
Finds predefined tokens in the input string and adds it to the input_array. | |
char ** | fill_input_array (char **input_array, char *input) |
Fills the array of strings with substrings('tokens') from the input string. | |
char ** | create_input_array (t_shell *shell) |
Create an array of input strings for tokenization. | |
int | skip_quotes (char *str) |
Skips over a quoted section ('\'' or '"') of a string. | |
void | no_delim_found (char *str, int *len) |
Checks if there is a quote('\'' or '"') at the current position the given string. | |
bool | is_token (char to_check) |
Check if a character is a token. | |
bool | is_token2 (char to_check) |
Check if a character is a token. | |
int | get_size_executor (t_lexer_list *head) |
Calculates the number of t_executor elements/nodes needed. | |
t_executor * | create_executor_list (int size) |
Create a linked list of t_executor nodes/elements. | |
void | set_executor_defaults (t_executor *node) |
Sets default values for a t_executor node. | |
t_executor * | init_executor_list (int size) |
Initializes the t_executor linked list. | |
t_executor * | prepare_executor (t_shell *shell) |
Prepare a linked list of t_executor elements for the executor. | |
char ** | combine_execs (char **resplit, char **new_execs, char **execs) |
Combine resplit array with execs array into new_execs array. | |
int | after_expand (t_executor *current) |
Process the execs array of the current executor node after expansion. | |
bool | check_execs_after_expand (t_shell *shell) |
Checks and processes execution commands after expansion. | |
void | set_path_executor (t_executor *list, t_environ_list *environ) |
Sets the path variable for t_executor nodes. | |
int | process_out_append (t_executor *current, t_lexer_list *lexer) |
Processes output redirections and append for the given node of the executor list. | |
int | process_in_heredoc (t_executor *current, t_lexer_list *lexer) |
Processes input redirections and heredoc for the given node of the executor list. | |
int | process_command (t_executor *current, t_lexer_list *lexer) |
Processes the command part for the given node of the executor list. | |
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. | |
t_executor * | fill_executor_list (t_shell *shell, t_executor *list) |
Fills the t_executor linked list with info about the parsed commands. | |
bool | close_outputfile (int fd) |
Closes and file descriptor. | |
bool | open_outputfile (t_executor *current, t_lexer_list *lexer) |
Handles opening an output file. | |
bool | get_input (int fd, char *delimiter) |
Reads input for a here-document until a specified delimiter. | |
int | create_heredoc (char *delimiter) |
Creates a here-document file and gathers input. | |
bool | open_inputfile (t_executor *current, t_lexer_list *lexer) |
Handles opening an input file. | |
int | get_size_execs (t_lexer_list *lexer) |
Calculates the number of consecutive NONE type lexer list nodes. | |
char ** | get_execs_array (t_lexer_list *lexer, int size) |
Creates an array of the found NONE type tokens from the lexer list. | |
void | update_old (t_shell *shell, char *old) |
Update OLDPWD. | |
int | env_var_update (t_shell *shell, char *old, char *new) |
Update PWD after directory change. | |
int | cd_no_args (t_shell *shell) |
Handles the case when changing to a directory with no arguments. | |
int | cd (t_shell *shell, char **args) |
Handles changing the current working directory with specified arguments. | |
int | get_sizeof_args (char **args) |
Retrieves the number of arguments in an array of strings. | |
int | pwd (void) |
Executes the "pwd" command to print the current working directory. | |
int | export_no_args (t_shell *shell) |
Handles the "export" command without arguments. | |
void | exporting (t_shell *shell, char *str) |
Updates or creates environment variables based on user-provided arguments. | |
int | export_args (t_shell *shell, char **args) |
Handles the export command with specified arguments. | |
int | export (t_shell *shell, char **args) |
Implements the export builtin function. | |
int | env (t_shell *shell, char **args) |
Displays the environment variables and their values. | |
void | child (t_shell *shell, char **args, int number) |
Handles the exit command when executed within a child process. | |
void | handle_child (t_shell *shell, char **args, int number) |
Handles the exit command when executed within a child process. | |
void | parent (t_shell *shell, char **args, int number) |
Handles the exit command when executed within the parent process. | |
int | handle_parent (t_shell *shell, char **args, int number) |
Handles the exit command when executed within the parent process. | |
bool | is_arg_number (char *arg) |
Checks if a string represents a valid numeric argument. | |
int | exit_builtin (t_shell *shell, char **args, bool in_child) |
Handles the exit command when executed in a child process. | |
void | delete_variable (t_shell *shell, char *arg) |
Deletes an environment variable. | |
void | unset_arg (t_shell *shell, char *arg, int *fails) |
Unsets an environment variable or prints an error message. | |
int | unset (t_shell *shell, char **args) |
Executes the "unset" command to remove environment variables. | |
int | echo (char **args) |
Implements the "echo" command to display text to the standard output. | |
bool | expander (t_shell *shell) |
Starting point of variable expansion. | |
int | expand_var (t_shell *shell, char **str, t_expander *exp, int pos) |
Expand variables. | |
char * | handle_quotes (t_shell *shell, char **str, char end_quote, t_expander *inter) |
Handles quoted strings within a shell command. | |
char * | interpolate (t_shell *shell, char *str) |
Interpolates variables and handles quotes within a shell command. | |
t_expander * | init_expander (char *str) |
Initializes the expander struct for variable expansion. | |
char * | realloc_str (char *res, int pos) |
Reallocates memory for a string. | |
void | free_exp (t_expander *exp) |
Frees resources associated with the expander struct. | |
char * | ft_strjoin_expander (char const *s1, char const *s2) |
Concatenates two strings and expands the memory as needed. | |
char * | ft_charjoin_expander (char const *s, char const c) |
Concatenates character to the end of a string. | |
void | close_fds (t_executor *current) |
Close file descriptors based on command redirection settings. | |
void | handle_single (t_shell *shell) |
Handle the execution of a single command. | |
void | executor (t_shell *shell) |
Primary entrypoint of command execution. | |
bool | is_builtin (char *exec) |
Check if a given command is a builtin-command. | |
void | execute_builtin (t_shell *shell) |
Execute a builtin-command. | |
int | handle_single_builtin (t_shell *shell) |
Handle the execution of a single builtin-command. | |
bool | handle_redirections_single_builtin (t_shell *shell) |
Handle redirections for a single non-builtin command. | |
void | saving_stds (int *stdin_cpy, int *stdout_cpy) |
Save the stdin and stdout file descriptors. | |
bool | restore_stds (int stdin_cpy, int stdout_cpy) |
Restore the stdin and stdout file descriptors. | |
void | handle_single_child (t_shell *shell) |
Process creation for execution of non-builtin command. | |
bool | handle_redirections_single_child (t_shell *shell) |
Handle redirections for single non-builtin command in child process. | |
char * | get_absolute_path (t_shell *shell) |
Transform the non-absolut path into one. | |
void | check_errno (t_shell *shell, char *path) |
Check and handle errors related to command execution. | |
void | get_path_error (char *str) |
Handle errors when obtaining an empty path to an executable. | |
char * | get_path (t_shell *shell, bool printerror) |
Get the path to an executable or convert it to an absolute path. | |
int | get_size_environ (t_environ_node *current) |
Get the size of the environment list. | |
char ** | create_environ_array (t_environ_list *environ) |
Create the environment array based on the list size. | |
void | transform_error (char **environ_array) |
Error handling while copying the environment list into the array. | |
void | transform_helper (t_environ_node *current, char **environ_array) |
Copy environment variables into the env array. | |
char ** | transform_environ_array (t_shell *shell) |
Copy the environment variables into an array for execve. | |
t_info * | init_info (t_shell *shell) |
Initializes the t_info structure. | |
void | wait_pipeline (t_info *info, int number) |
Wait for child processes to finish execution + update the exit code. | |
void | ft_error (t_executor *current, t_info *info) |
Handle errors during command execution and set the exit code accordingly. | |
void | handle_multi (t_shell *shell, t_executor *current) |
Handles the execution of multiple commands. | |
int | handle_redirections_pipeline (int *fildes, t_executor *current, t_info *info) |
Handles file redirections and file descriptor setup. | |
void | execute_builtin_child (t_executor *current, t_info *info) |
Executes a built-in command in a child process and exits. | |
char * | execute_other_helper (t_executor *current) |
Helper function to find the executable path. | |
int | execute_other (t_executor *current, t_info *info) |
Executes a non-built-in command in a child process using execve. | |
int | child_handler_multi (int *fildes, t_executor *current, t_info *info) |
Handles the execution of a child process in a multi-command. | |
int | handle_pipes (int fildes[2]) |
Creates a pipe and sets the file descriptors in the given array. | |
pid_t | handle_forks (void) |
Creates a child process using fork() and returns the process ID. | |
int | parent_handler_multi (int *fildes, t_executor *current) |
Handles the parent process during multi-command execution. | |
int | handle_pipeline (int *fildes, t_executor *current, t_info *info, int i) |
Handles the execution of a pipeline of commands. | |
Variables | |
int | g_exit_code |
This is the headerfile for minishell.
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.
Adds a given node to the end of an environment variable linked list. The linked list's size is incremented accordingly.
[in,out] | environ | A pointer to the environment variables linked list. |
[in] | node | A pointer to the node to be added. |
int after_expand | ( | t_executor * | current | ) |
Process the execs array of the current executor node after expansion.
Takes a node of the executor list, splits its execs array (execution command) and combines the resulting strings with the existing execs array.
[in,out] | current | The current executor node. |
t_token assign_type | ( | char | tok | ) |
Assign a type based on the input char.
tok | The input char to assign a tyoe for. |
int cd | ( | t_shell * | shell, |
char ** | args | ||
) |
Handles changing the current working directory with specified arguments.
This function is called when the cd command is executed with one argument, which is the target directory to change to. It checks the number of arguments, validates the input, and attempts to change the current working directory.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing command arguments. |
int cd_no_args | ( | t_shell * | shell | ) |
Handles the case when changing to a directory with no arguments.
This function is called when the cd command is executed without any arguments. It attempts to change the current working directory to the user's home directory (retrieved from the environment variable HOME).
[in] | shell | A pointer to the shell struct. |
void check_errno | ( | t_shell * | shell, |
char * | path | ||
) |
Check and handle errors related to command execution.
The check_errno function checks the value of the errno variable and sets the appropriate exit code and error message based on whether the command was not found or permission was denied.
[in] | shell | A pointer to the shell struct containing the executor. |
[in] | path | The path to the executable. |
bool check_execs_after_expand | ( | t_shell * | shell | ) |
Checks and processes execution commands after expansion.
Iterates through the executor list ands calls after_expand() on each of the nodes of that list. Retuns true if all processing is successfull, or false if any processing fails.
[in,out] | shell | The main data structure. |
void check_exitcode | ( | int | wstatus, |
bool * | is_print | ||
) |
Checks the exit code of a child process and update g_exit_code.
Checks the exit code of a child process and updates g_exit_code based on wether the process exited normally or was terminated by a signal. If the process exited normally, the exit status is used. If the process was terminated bt a signal, the signalnumber is added to the base (128) to set the exit code.
[in] | wstatus | The status of the child process. |
[in,out] | is_print | A pointer to a boolean indicating wether to print the SIGQUIT message. |
bool check_pipe_syntax | ( | t_lexer_list * | lexer | ) |
Performs a pipe syntax check on the given list of lexems t_lexer_list *lexer.
Checks the lexer list for a syntax error with pipes, by checking if a valid token is encounterd after a PIPE token.
lexer | A pointer to the lexer list. |
bool check_quote_syntax | ( | const char * | input | ) |
Check the syntax of quotes in the input string.
Checks if the syntax of quoted parts (single or double quotes) in the input string is valid. Opening and closing quotes need to be correctly matched. Handles escape characters. If all quoted parts are correct, return true, else return false.
input | The string to be checked. |
void child | ( | t_shell * | shell, |
char ** | args, | ||
int | number | ||
) |
Handles the exit command when executed within a child process.
This function is called when the exit command is executed within a child process. It handles the termination of the child process based on the provided arguments, including the exit status.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
[in] | number | The number of arguments in the array. |
int child_handler_multi | ( | int * | fildes, |
t_executor * | current, | ||
t_info * | info | ||
) |
Handles the execution of a child process in a multi-command.
This function handles the execution of a child process, including setting up file descriptors, executing built-in or non-built-in commands, and managing the child process's exit status.
fildes | Array of pipe file descriptors. |
current | The current node of the executor list representing a command. |
info | Pointer to the t_info structure. |
char * clean_rl_copy | ( | char * | rl_copy | ) |
Create a copy of the input string with its leading and trailing whitespaces removed.
Removes leading and trailing whitespaces from rl_copy and creates a new string with these whitespaces removed. The original input string is freed.
rl_copy | The input string to be cleaned. |
void close_fds | ( | t_executor * | current | ) |
Close file descriptors based on command redirection settings.
This function is responsible for closing file descriptors associated with input and output redirection, as determined by the settings in the current executor node.
[in] | current | A pointer to the executor struct. |
bool close_outputfile | ( | int | fd | ) |
Closes and file descriptor.
Closes an output file descriptor, checking for errors.
fd | The file descriptor to close. |
char ** combine_execs | ( | char ** | resplit, |
char ** | new_execs, | ||
char ** | execs | ||
) |
Combine resplit array with execs array into new_execs array.
Takes 2 arrays of strings, 'resplit' and 'execs', combines them into a new array 'new_execs'. Also frees the memory of the input arrays.
[in] | resplit | The first array of strings. |
[in,out] | new_execs | The resulting array of strings after combining. |
[in] | execs | The second array of strings. |
int count_elements | ( | char * | rl_copy | ) |
Count the number of elements in the input string.
Takes an input and counts the substrings('tokens') in it. Makes use of len_substr() to get the length of each substring. Returns the number of substrings found.
rl_copy | The string for which to count the number of substrings. |
char ** create_environ_array | ( | t_environ_list * | environ | ) |
Create the environment array based on the list size.
The create_environ_array function allocates memory for the environment array. It gets the size of the list from get_size_environ and returns the allocated array.
[in] | environ | A pointer to the environment variable linked list. |
t_environ_node * create_environ_node | ( | char * | var | ) |
Creates an node for an environment variable linked list.
Creates and initializes an node using the provided parameter string. The string is typically in the format "KEY=VALUE", where KEY is the variable name and VALUE is its value ("duhh").
[in] | var | The string representing the environment variable in the "KEY=VALUE" format. |
t_executor * create_executor_list | ( | int | size | ) |
Create a linked list of t_executor nodes/elements.
Allocates memory for a linked list of t_executor nodes, based on the size.
size | The size of the t_executor linked list. |
int create_heredoc | ( | char * | delimiter | ) |
Creates a here-document file and gathers input.
Creates a temporary file for a here-document, gathers input for it by calling get_input(). Returns a file descriptor to the created file.
delimiter | The delimiter that signals the end of the here-document. |
char ** create_input_array | ( | t_shell * | shell | ) |
Create an array of input strings for tokenization.
Creates an array of tokens from shell->rl_copy(removed leading and trailing whitespaces). Calculates the number of elements needed for the array and allocates for it. Calls fill_input_array() to fill the array with substrings ('tokens').
shell | The main data structure. |
t_lexer_list * create_lexer_list | ( | char ** | input_array | ) |
Allocates memory for the lexer list.
Creates a double-linked list using the size of the input array. Calculates size of input array. and allocates memory for the lexer list. If malloc fails, displays error message, returns NULL.
input_array | The array with the substrings('tokens'). |
void delete_variable | ( | t_shell * | shell, |
char * | arg | ||
) |
Deletes an environment variable.
This function deletes an environment variable specified by its name (arg) from the shell's environment linked list. It finds the variable to delete and removes it from the linked list, freeing the associated memory.
[in,out] | shell | A pointer to the shell struct. |
[in] | arg | The name of the environment variable to delete. |
int echo | ( | char ** | args | ) |
Implements the "echo" command to display text to the standard output.
This function handles the "echo" command, which displays text to the standard output. It can be called with multiple arguments, and it processes the -n
option to omit the trailing newline character.
[in] | args | An array of strings containing the arguments. |
t_environ_list * empty_environ | ( | t_environ_list * | environ | ) |
Initializes environment variables for an empty environment.
Initializes environment variables for an empty environment, such as adding the "PWD" and "SHLVL" variables to the provided environment variables linked list.
[in] | environ | A pointer to the environment variables linked list. |
int env | ( | t_shell * | shell, |
char ** | args | ||
) |
Displays the environment variables and their values.
This function implements the "env" command, which is used to display the environment variables and their corresponding values to the standard output.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
int env_var_update | ( | t_shell * | shell, |
char * | old, | ||
char * | new | ||
) |
Update PWD after directory change.
Updates the environment variable PWD and passes the OLDPWD to the other update function.
[in] | shell | A pointer to the shell struct. |
[in] | old | The previous working directory. |
[in] | new | The new working directory. |
void execute_builtin | ( | t_shell * | shell | ) |
Execute a builtin-command.
The execute_builtin function executes the appropriate builtin-command based on the provided command name in the execs array. It sets the global g_exit_code variable to the exit status of the command.
[in,out] | shell | A pointer to the shell struct. |
void execute_builtin_child | ( | t_executor * | current, |
t_info * | info | ||
) |
Executes a built-in command in a child process and exits.
This function executes a built-in command in a child process, sets the exit code accordingly, frees the heap and then exits the child process.
current | The current node of the executor list representing a command. |
info | Pointer to the t_info structure. |
int execute_other | ( | t_executor * | current, |
t_info * | info | ||
) |
Executes a non-built-in command in a child process using execve.
This function executes a non-built-in command in a child process using the execve() system call and sets the exit code accordingly. If the path is not absolute then it uses execute_other_helper() to get one. Before the execve() call the function transforms the env list into a 2D array in order to pass it to execve().
current | The current node of the executor list representing a command. |
info | Pointer to the t_info structure. |
char * execute_other_helper | ( | t_executor * | current | ) |
Helper function to find the executable path.
This function searches for the executable in the directories listed in the PATH environment variable and returns the full path to the executable.
current | The current node of the executor list representing a command. |
void executor | ( | t_shell * | shell | ) |
Primary entrypoint of command execution.
The executor function determines whether to handle a single command or multiple command based on the size of commands passed to it.
[in,out] | shell | A pointer to the shell struct. |
int exit_builtin | ( | t_shell * | shell, |
char ** | args, | ||
bool | in_child | ||
) |
Handles the exit command when executed in a child process.
This function is called when the exit command is executed within a child process. It handles the termination of the child process based on the provided arguments.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
[in] | in_child | A boolean indicating whether the function is called in a child process. |
int expand_var | ( | t_shell * | shell, |
char ** | str, | ||
t_expander * | exp, | ||
int | pos | ||
) |
Expand variables.
This function is expanding variables. It iterates through the command string, identifies variables and replaces them with the corresponding values.
[in] | shell | A pointer to the shell struct. |
[in,out] | str | A pointer to the command string. |
[in,out] | exp | A pointer to the expander struct. |
[in] | pos | The current position in the result string. |
bool expander | ( | t_shell * | shell | ) |
Starting point of variable expansion.
This function is the starting point for expanding variables.It iterates through each command and applies variable expansion if needed or just copies from the input.
[in,out] | shell | A pointer to the shell struct. |
int export | ( | t_shell * | shell, |
char ** | args | ||
) |
Implements the export builtin function.
This function is responsible for executing the export command. It handles cases where the command is executed with or without arguments and calls the appropriate functions to set or display environment variables. When executed with arguments, it processes each argument and sets or modifies the corresponding environment variable. If executed without arguments, it displays the names and values of all currently set environment variables in the "declare -x KEY=VALUE" format.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
int export_args | ( | t_shell * | shell, |
char ** | args | ||
) |
Handles the export command with specified arguments.
This function is called when the export
command is executed with one or more arguments. It processes the arguments and sets or modifies environment variables accordingly.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
int export_no_args | ( | t_shell * | shell | ) |
Handles the "export" command without arguments.
This function is called when the export command is executed without any arguments. It displays the names and values of all currently set environment variables in the "declare -x KEY=VALUE" format.
[in] | shell | A pointer to the shell struct. |
void exporting | ( | t_shell * | shell, |
char * | str | ||
) |
Updates or creates environment variables based on user-provided arguments.
This function processes individual argument strings and sets or modifies environment variables accordingly. It checks for the validity of identifiers and updates their values.
[in] | shell | A pointer to the shell struct. |
[in] | str | The argument string to be processed and used as an environment variable. |
t_executor * fill_executor_list | ( | t_shell * | shell, |
t_executor * | list | ||
) |
Fills the t_executor linked list with info about the parsed commands.
Fills the t_executor linked list by processing the lexer linked list and setting values in a t_executor node for each command and its operation.
shell | A pointer to the main data structure. |
list | A pointer the t_executor linked list to be filled. |
char ** fill_input_array | ( | char ** | input_array, |
char * | input | ||
) |
Fills the array of strings with substrings('tokens') from the input string.
Takes an input string, splits it into substrings('tokens') and fills the given array of strings with these substrings('tokens'). The function iterates over the input, calls find_token() to find predefined 'tokens' and adds them to the array. Also handles whitespace separated substrings and adds them to the array.
input_array | Pointer to the array of strings to fill. |
input | The input string. |
t_lexer_list * fill_lexer_list | ( | char ** | input_array | ) |
Fills the lexer list using the input array.
Calls create_lexer_list() to allocates a double-linked list with as many nodes as substrings in input_array. Fills each node of the list with information about each 'token'.
input_array | The array with the substrings('tokens'). |
int find_token | ( | char ** | input_array, |
char ** | input, | ||
int | index | ||
) |
Finds predefined tokens in the input string and adds it to the input_array.
Finds tokens in the input string and adds them to the input_array. It calls is_token() and is_token2() to check for existing valid tokens. If a token is found, a substring is created and added to the input_array.
input_array | Pointer to the array of strings. |
input | Pointer to the input string. |
index | Current index in the input_array. |
void free_environ | ( | t_environ_list * | environ | ) |
Frees the memory used by the environment list and its nodes.
Releases the memory allocated for the environment list and its associated nodes, freeing the memory used by keys and values as well.
[in,out] | environ | A pointer the the environment list to be freed. |
t_executor * free_executor_list | ( | t_executor * | list | ) |
Frees the allocated memory of a linked list of t_executor elements.
Frees the memory of each node in the linked list, also frees the allocated members path and execs of each node. The entire linked list is freed. Its pointer is set to NULL.
list | A pointer to the head of the t_executor linked list. |
void free_exp | ( | t_expander * | exp | ) |
Frees resources associated with the expander struct.
This function deallocates memory used by the expander struct and its associated fields. It ensures that no memory leaks occur during variable expansion.
[in] | exp | A pointer to the expander struct to be freed. |
void free_heap | ( | t_shell * | shell | ) |
Frees the memory used by the main data structure (shell environment).
Releases the memory allocated for the main data structure and it's components, including the environment list, readline input, etc.
[in,out] | shell | A pointer to the shell structure to be freed. |
t_lexer_list * free_lex | ( | t_lexer_list * | lex_head | ) |
Frees the allocated memory of a linked list of lexer elements.
Frees the memory of each node in the linked list, starting from the "lex_head" node. Also frees the allocated 'str' member of each node. The entire linked list is then freed and 'lex_head' is set to NULL.
lex_head | A pointer to the head of the lexer list. |
char ** free_str_arr | ( | char ** | arr | ) |
Frees the allocated memory of a string array.
Frees the memory of a string array by each string in the array.
arr | The pointer the the array of strings. |
char * ft_charjoin_expander | ( | char const * | s, |
char const | c | ||
) |
Concatenates character to the end of a string.
This function takes an input string s and a character c, and concatenates the character to the end of the string. The memory for the resulting string is dynamically allocated to accommodate the additional character. The memory for the original string is freed before returning the result.
s | The input string. |
c | The character to concatenate. |
void ft_error | ( | t_executor * | current, |
t_info * | info | ||
) |
Handle errors during command execution and set the exit code accordingly.
This function checks for specific error conditions during command execution (e.g., command not found or permission denied) and sets the exit code accordingly before exiting. Afterwards it frees everything in heap.
current | The current executor representing a command. |
info | Pointer to the t_info structure. |
pid_t ft_getpid | ( | void | ) |
Retrieves the process id (PID) of the current process.
Retrieves the process id (PID) of the current process by reading the status file of the process.
int ft_isspace | ( | int | c | ) |
Checks if the given character is a whitespace.
Checks if a given character is a whitespace and returns 1 if it is. Otherwise returns 0.
c | The character to check. |
char * ft_strjoin_expander | ( | char const * | s1, |
char const * | s2 | ||
) |
Concatenates two strings and expands the memory as needed.
This function takes two input strings, s1 and s2, and concatenates them into a new string. The memory for the resulting string is dynamically allocated to accommodate both input strings. The memory for s1 is freed before returning the result.
s1 | The first input string. |
s2 | The second input string. |
char * get_absolute_path | ( | t_shell * | shell | ) |
Transform the non-absolut path into one.
The get_absolute_path function constructs the absolute path to an executable by combining it with each directory in the PATH environment variable. If the provided executable path is not absolute and is found in PATH, it is converted to an absolute path.
[in] | shell | A pointer to the shell struct. |
int get_end_quote | ( | const char * | input, |
char | c | ||
) |
Find the index of the end of a quoted part in a string.
Looks for the end(matching quote) of a quoted part in the input string. The matching quote is given as an argument to the function. If the matching quote is found and is not escaped, its index is returned. Else the entire length of the input string is returned.
input | The input string. |
c | The character representing the matching closing quote. |
char ** get_execs_array | ( | t_lexer_list * | lexer, |
int | size | ||
) |
Creates an array of the found NONE type tokens from the lexer list.
Allocates memory for an array of NONE tokens. Fills the array with the strings from the consecutive NONE type tokens in the lexer list.
lexer | A pointer to the current lexer list node. |
int | The size of the array. |
bool get_input | ( | int | fd, |
char * | delimiter | ||
) |
Reads input for a here-document until a specified delimiter.
Reads input from the user, displaying a prompt, until the specified delimiter is entered. The input is written to the given file descriptor. Also handles ctrl-d (end-of-file from readline).
fd | The file descriptor to write the input to. |
delimiter | The delimiter that signals the end of the here-document. |
t_environ_node * get_node | ( | t_environ_list * | environ, |
char * | key | ||
) |
Find an node by the key from an environment variables linked list.
Searches for an node with a specific key, given as parameter, in the environment variables linked list, given as parameter. Returns the matching node.
[in] | environ | A pointer to the environment linked list. |
[in] | key | The KEY ov the environment variable to search for. |
char * get_path | ( | t_shell * | shell, |
bool | printerror | ||
) |
Get the path to an executable or convert it to an absolute path.
The function checks if the input for the command is already an absolute path. If not, it transforms the input to have the correct format else if just copies the absolute path into the path variable.
[in] | shell | A pointer to the shell struct. |
void get_path_error | ( | char * | str | ) |
Handle errors when obtaining an empty path to an executable.
The get_path_error function sets the errno and exit code for when the path is empty or the executable was not found and prints an error message.
[in] | str | The name of the executable. |
int get_size_environ | ( | t_environ_node * | current | ) |
Get the size of the environment list.
The get_size_environ function counts and returns the size of the list of environment variables.
[in] | current | A pointer to the head of the environment variable list. |
int get_size_execs | ( | t_lexer_list * | lexer | ) |
Calculates the number of consecutive NONE type lexer list nodes.
Calculates the number of command arguments based on the number of consecutive NONE type tokens in the lexer list. Each NONE token represents one of the following: a command(executable/buildin), a command option(-i –version), or a command argument(filenames, etc).
lexer | A pointer to the current lexer list node. |
int get_size_executor | ( | t_lexer_list * | head | ) |
Calculates the number of t_executor elements/nodes needed.
Calculates the number of t_executor elements/nodes needed based on the number of pipe token in the lexer linked list. Each pipe token represents a new t_executor node. Example: 2 pipe token -> 2 + 1 t_executor nodes.
head | A pointer to the head of the lexer linked list. |
int get_sizeof_args | ( | char ** | args | ) |
Retrieves the number of arguments in an array of strings.
This function counts the number of elements (arguments) in an array of strings and returns the count.
[in] | args | An array of strings containing command arguments. |
void handle_child | ( | t_shell * | shell, |
char ** | args, | ||
int | number | ||
) |
Handles the exit command when executed within a child process.
This function is called when the exit command is executed within a child process. It handles the termination of the child process based on the provided arguments, including the exit status.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
[in] | number | The number of arguments in the array. |
pid_t handle_forks | ( | void | ) |
Creates a child process using fork() and returns the process ID.
This function creates a child process using the fork() system call and returns the process ID for the child process.
void handle_multi | ( | t_shell * | shell, |
t_executor * | current | ||
) |
Handles the execution of multiple commands.
This function initializes the t_info structure, executes a sequence of commands represented by the provided nodes of the executor list, and waits for their completion. It also makes a copie of the file descriptor for input redirection and restores it after completion. It handles errors returned from handle_pipeline().
shell | The minishell main data structure. |
current | The head of the executor list representing a command. |
int handle_parent | ( | t_shell * | shell, |
char ** | args, | ||
int | number | ||
) |
Handles the exit command when executed within the parent process.
This function is called when the exit command is executed within the parent process. It handles the termination of the shell or reports an error when provided with invalid arguments.
[in] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
[in] | number | The number of arguments in the array. |
int handle_pipeline | ( | int * | fildes, |
t_executor * | current, | ||
t_info * | info, | ||
int | i | ||
) |
Handles the execution of a pipeline of commands.
This function manages the execution of multiple commands, including creating pipes, forking child processes, calling child and parent handlers and handling error conditions.
fildes | Array of pipe file descriptors. |
current | The current node of the executor list representing a command. |
info | Pointer to the t_info structure. |
i | Index of the current command/child in the pipeline. |
int handle_pipes | ( | int | fildes[2] | ) |
Creates a pipe and sets the file descriptors in the given array.
This function creates a pipe using the provided file descriptor array and sets the appropriate file descriptors for communication between commands.
fildes | Array to store pipe file descriptors. |
char * handle_quotes | ( | t_shell * | shell, |
char ** | str, | ||
char | end_quote, | ||
t_expander * | inter | ||
) |
Handles quoted strings within a shell command.
This function processes quoted strings within a shell command, including variable expansions if present. It handles both single (' ') and double (" ") quotes.
[in] | shell | A pointer to the shell struct. |
[in,out] | str | A pointer to the command string to be processed. |
[in] | end_quote | The character that marks the end of the quoted string (' ' or " "). |
[in,out] | inter | A pointer to the expander struct. |
int handle_redirections_pipeline | ( | int * | fildes, |
t_executor * | current, | ||
t_info * | info | ||
) |
Handles file redirections and file descriptor setup.
Sets up file descriptors for a child process in a pipeline, including standard input and output redirection. Manages file descriptors for communication if more commands are in the pipeline.
fildes | Array of pipe file descriptors. |
current | The current node of the executor list representing a command. |
info | Pointer to the t_info structure. |
bool handle_redirections_single_builtin | ( | t_shell * | shell | ) |
Handle redirections for a single non-builtin command.
The handle_redirections_single_builtin function handles redirections for a single non-builtin command. It duplicates the file descriptors needed by the child process and closes back the copy.
[in,out] | shell | A pointer to the shell struct. |
true
if redirections are successfully handled, or false
if an error occurs. bool handle_redirections_single_child | ( | t_shell * | shell | ) |
Handle redirections for single non-builtin command in child process.
The handle_redirections_single_child function handles redirections for a single non-builtin command. It duplicates the file descriptors needed by the child process and closes back the copy.
[in,out] | shell | A pointer to the shell struct. |
true
if redirections are successfully handled, or false
if an error occurs. void handle_single | ( | t_shell * | shell | ) |
Handle the execution of a single command.
The handle_single function first checks if the command is an empty string and fails accordingly. If the command is a builtin it would execute it in the parent process, otherwise it would do a fork and execute the external command in the child process.
[in,out] | shell | A pointer to the shell struct. |
int handle_single_builtin | ( | t_shell * | shell | ) |
Handle the execution of a single builtin-command.
The handle_single_builtin function checks if the provided command is a builtin, saves the file descriptors, handles standard input and output redirections, executes the builtin-command and restores standard file descriptors.
[in,out] | shell | A pointer to the shell struct. |
void handle_single_child | ( | t_shell * | shell | ) |
Process creation for execution of non-builtin command.
The handle_single_child function creates a child process and executes a non-built-in command in the child process. It waits for the child process to complete and updates the environment variable "_=".
[in,out] | shell | A pointer to the shell struct. |
t_environ_list * init_environ | ( | char ** | envp | ) |
Initializes the environment variables linked list.
Initializes a linked list to store environment variables based on the provided 2D array of environment variables.
[in] | envp | A 2D array of environment variables. |
t_executor * init_executor_list | ( | int | size | ) |
Initializes the t_executor linked list.
Initializes the t_executor linked list, by first calling create_executor_list() to allocate memory for it. It sets every nodes id and overall size, then calls set_executor_defaults() for setting default values.
size | The amount of nodes in the t_executor linked list. |
t_expander * init_expander | ( | char * | str | ) |
Initializes the expander struct for variable expansion.
This function initializes the expander struct used during variable expansion. It allocates memory for various fields and sets their initial values.
[in] | str | The input string to be expanded. |
Initializes the t_info structure.
This function allocates memory for the t_info structure, stores process IDs for child processes, a copy of the standard input file descriptor, and a reference to the minishell main data structure.
shell | The minishell main data structure. |
t_shell * init_shell | ( | char ** | envp | ) |
Initializes the main data structure (shell environment).
Starts the process of initializing the shell's main data structure, which includes components such as the environment variables in a linked list and other relevant information.
[in] | envp | A 2D char array of environment variables. |
char * interpolate | ( | t_shell * | shell, |
char * | str | ||
) |
Interpolates variables and handles quotes within a shell command.
This function processes variable interpolation and handles quotes (single or double) within the shell prompt. It iterates through the prompt string, expanding variables and preserving quoted strings.
[in] | shell | A pointer to the shell struct. |
[in,out] | str | A pointer to the command string to be processed. |
[in] | inter | A pointer to the expander struct. |
bool is_arg_number | ( | char * | arg | ) |
Checks if a string represents a valid numeric argument.
This function is used to determine if a string can be interpreted as a valid numeric argument for the exit command. It checks whether all characters in the string are digits. Skips the first '-' or '+'.
[in] | arg | The string to be checked. |
bool is_builtin | ( | char * | exec | ) |
Check if a given command is a builtin-command.
This function determines whether the provided command is one of the shell builtin-commands such as "echo", "cd", "pwd", "export", "unset", "env" or "exit."
[in] | exec | The command name to check. |
bool is_token | ( | char | to_check | ) |
Check if a character is a token.
Takes an input character and checks if it is a token character, which includes '<', '>', and '|'. If the input character matches any of these token characters, the function returns true. Otherwise, it returns false.
to_check | The character to check if it's a token. |
bool is_token2 | ( | char | to_check | ) |
Check if a character is a token.
Takes an input character and checks if it is a token character, which includes '<', '>'. If the input character matches any of these token characters, the function returns true. Otherwise, it returns false.
to_check | The character to check if it's a token. |
int len_substr | ( | char * | str | ) |
Calculates the length of a substring('token') until a delimiter or a token is found.
Takes an input string and finds the length of the substring('token') until a delimiter(whitespaces) or token('<', '>', '|') is encountered. Iterates over the string while checking for delimiter and token. If delimiter is not found, call no_delim_found() to modify the len. Returns the len of the substring until the delimiter or token.
str | The input string to find the lenght of substring for. |
t_lexer_list * lexer | ( | t_shell * | shell | ) |
Lexical analysis and tokenization of the input string.
Performs the lexical analysis and tokenization of the input string. First frees the previous 'rl_copy' if it exists, then creates a clean copy of the input string and removes leading/trailing whitespaces. Checks the syntax of the quotes. If syntax is incorrect, print message, set g_exit_code and return NULL. Calls tokenize() to tokenize the clean copy and returns the head of the returned lexer list.
shell | The shell data structure (main data structure). @ return A pointer to the head of the lexer list if successful, otherwise NULL. |
void lvl_up | ( | t_shell * | shell | ) |
Increases the shell level (SHLVL) environment variable.
Increments the value of the shell level (SHLVL) environment variable. If the variable doesn't exist, it is added with a value of 1.
[in,out] | shell | A pointer to the shell data structure. |
void no_delim_found | ( | char * | str, |
int * | len | ||
) |
Checks if there is a quote('\'' or '"') at the current position the given string.
The function is called when no delimiter(whitespace) is found. Checks if the current position in the string is a quote ('\'' or '"') and calls skip_quotes() to skip over the quoted section. Otherwise it incremets the lenght(and current position).
str | The input string. |
*len | A pointer to the lenght(and current index) of the string. |
bool open_inputfile | ( | t_executor * | current, |
t_lexer_list * | lexer | ||
) |
Handles opening an input file.
Handles opening an input file for the given t_executor node, based on the given lexer list token type. It supports input redirection and here-document.
current | A pointer to the current t_executor node. |
lexer | A pointer to the current t_lexer_list node. |
bool open_outputfile | ( | t_executor * | current, |
t_lexer_list * | lexer | ||
) |
Handles opening an output file.
Handles opening an output file for the given t_executor node, based on the given lexer list token type. It supports truncating and appending to the file.
current | A pointer to the current t_executor node. |
lexer | A pointer to the current t_lexer_list node. |
void parent | ( | t_shell * | shell, |
char ** | args, | ||
int | number | ||
) |
Handles the exit command when executed within the parent process.
This function is called when the exit command is executed within the parent process. It handles the termination of the shell or reports an error when provided with invalid arguments.
[in] | shell | A pointer to the shell. |
[in] | args | An array of strings containing the arguments. |
[in] | number | The number of arguments in the array. |
int parent_handler_multi | ( | int * | fildes, |
t_executor * | current | ||
) |
Handles the parent process during multi-command execution.
This function manages file descriptors for input and output redirection. It manages the pipe for the parent process.
fildes | Array of pipe file descriptors. |
current | The current node of the executor list representing a command. |
bool parser | ( | t_shell * | shell | ) |
Parses the input using the lexer.
Performs the parsing operation using the lexer. It first frees the existing lexer list if it exists. Then uses lexer() to generate a new list. If it failes to produce a new list, returns false. Else returns true.
shell | The shell data structure (main data structure). |
t_executor * prepare_executor | ( | t_shell * | shell | ) |
Prepare a linked list of t_executor elements for the executor.
Prepares a linked list of t_executor elements by calculating the number of elements needed, initializing them and filling them with the relevant data from the lexer linked list and the environ linked list.
shell | A pointer to the main data structure. |
int process_command | ( | t_executor * | current, |
t_lexer_list * | lexer | ||
) |
Processes the command part for the given node of the executor list.
Handles the command part of the given executor list node by creating an array of command arguments.
current | A pointer to the current t_executor node. |
lexer | A pointer to the current lexer list node. |
int process_in_heredoc | ( | t_executor * | current, |
t_lexer_list * | lexer | ||
) |
Processes input redirections and heredoc for the given node of the executor list.
Handles input redirections and heredoc for the given executor list node by calling its subfunctions. Has some edgecase handling.
current | A pointer to the current t_executor node. |
lexer | A pointer to the current lexer list node. |
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.
Processes lexemes in the lexer linked list and populates the executor linked list accordingly. It handles output redirections, input redirections inclusive heredoc and command executions. On error, frees the entire t_executor linked list and returns NULL.
list | A pointer to the t_executor linked list. |
current | A pointer to the current t_executor node. |
lexer | A pointer to the lexer linked list. |
int process_out_append | ( | t_executor * | current, |
t_lexer_list * | lexer | ||
) |
Processes output redirections and append for the given node of the executor list.
Handles output redirections and append for the given executor list node by calling its subfunctions. Has some edgecase handling.
current | A pointer to the current t_executor node. |
lexer | A pointer to the current lexer list node. |
int pwd | ( | void | ) |
Executes the "pwd" command to print the current working directory.
This function gets the current working directory using getcwd() and prints it to the standard output. If successful, it returns EXIT_SUCCESS
; otherwise, it prints an error message and returns EXIT_FAILURE
.
char * realloc_str | ( | char * | res, |
int | pos | ||
) |
Reallocates memory for a string.
This function reallocates memory for a string, extending its size while preserving its existing content. It is used to dynamically expand the result string during variable expansion.
[in] | res | The original string to be reallocated. |
[in] | pos | The current position in the result string. |
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.
Creates a new string containing characters from the input string (rl_copy) starting from the 'start' index up to the 'end' index. If allocation fails, a error message is displayed and NULL is returned.
rl_copy | The input string. |
start | The starting index. |
end | The ending index. |
bool restore_stds | ( | int | stdin_cpy, |
int | stdout_cpy | ||
) |
Restore the stdin and stdout file descriptors.
The restore_stds function restores the standard input and output file descriptors by duplicating the saved file descriptors back to stdin and stdout. It also handles errors if somehow the restoration fails.
[in] | stdin_cpy | The duplicated stdin file descriptor. |
[in] | stdout_cpy | The duplicated stdout file descriptor. |
true
if the restoration is successful, or false
if an error occurs. void saving_stds | ( | int * | stdin_cpy, |
int * | stdout_cpy | ||
) |
Save the stdin and stdout file descriptors.
The saving_stds function saves the stdin and stdout file descriptors by duplicating and storing them.
[out] | stdin_cpy | A pointer to store the duplicated stdin file descriptor. |
[out] | stdout_cpy | A pointer to store the duplicated stdout file descriptor. |
void set_executor_defaults | ( | t_executor * | node | ) |
Sets default values for a t_executor node.
Initializes the variables of a t_executor node with default values. They include filediscriptors and bools.
node | A pointer to the t_executor node to initialize. |
void set_path_executor | ( | t_executor * | list, |
t_environ_list * | environ | ||
) |
Sets the path variable for t_executor nodes.
Sets the path variable for t_executor nodes in the provided linked list. If PATH doesn't exist then t_executor->path remains at default(NULL);
list | A pointer to the head of the t_executor linked list. |
environ | A pointer to the environ linked list. |
void signal_setup | ( | int | process | ) |
Sets up signal handlers.
configures signal handlers based on the provided process type. For the parent process, SIGINT is set to parent_handler_sig, and the SIGQUIT signal is ignored. For the child process, both SIGINT and SIGQUIT are set to their default behavior. For IGNORE, both SIGINT and SIGQUIT are ignored.
[in] | process | An int representing the process type (PARENT or CHILD, IGNORE). |
int skip_quotes | ( | char * | str | ) |
Skips over a quoted section ('\'' or '"') of a string.
Used to skip over a quoted section ('\'' or '"') of a string. It starts from the quote pointed to by the str pointer, and iterates over the string until it finds the same quote. Handles escaped quotes (preceded by '\'). Returns the index of the character after the 2nd quote (closing quote).
str | The input string. |
t_lexer_list * tokenize | ( | t_shell * | shell | ) |
Tokenizes the input string and creates the lexer list.
Tokenizes the input by creating an array of substrings. Creates a linked list of lexer tokens using the substrings. Returns a pointer to the head of the lexer linked list. if malloc fails for input_array or lexer list, display message and return NULL.
shell | The shell data structure (main data structure). @ return The head of the lexer list if successful, otherwise NULL. |
char ** transform_environ_array | ( | t_shell * | shell | ) |
Copy the environment variables into an array for execve.
The transform_environ_array function creates an environment array for execve. It first allocates memory for the environment array and then calls transform_helper to populate it with environment variable strings.
[in] | shell | A pointer to the shell struct. |
void transform_error | ( | char ** | environ_array | ) |
Error handling while copying the environment list into the array.
The transform_error function is called when an error occurs during the copying process. It frees the allocated memory for the array and reports an error.
[in] | environ_array | A pointer to the environment array to be freed. |
void transform_helper | ( | t_environ_node * | current, |
char ** | environ_array | ||
) |
Copy environment variables into the env array.
The transform_helper function is responsible for copying the linked list into the environment array used for execve calls. It allocates memory for each environment variable string and combines the key + value with an equal sign.
[in] | current | A pointer to the head of the environment variable list. |
[out] | environ_array | A pointer to the environment array. |
int unset | ( | t_shell * | shell, |
char ** | args | ||
) |
Executes the "unset" command to remove environment variables.
This function processes the "unset" command and removes the specified environment variables based on the provided arguments. It returns EXIT_SUCCESS
on success or EXIT_FAILURE
if any errors occurred.
[in,out] | shell | A pointer to the shell struct. |
[in] | args | An array of strings containing the arguments. |
void unset_arg | ( | t_shell * | shell, |
char * | arg, | ||
int * | fails | ||
) |
Unsets an environment variable or prints an error message.
This function unsets an environment variable specified by its name (arg) if it's a valid identifier (starts with an alphabetic character or '_'). If the identifier is not valid, it prints an error message and increments the failure count.
[in,out] | shell | A pointer to the shell struct. |
[in] | arg | The name of the environment variable to unset. |
[in,out] | fails | A pointer to the failure count. |
void update_old | ( | t_shell * | shell, |
char * | old | ||
) |
Update OLDPWD.
Updates the OLDPWD environment variable with the value of the previous working directory before the change.
[in] | shell | A pointer to the shell struct. |
[in] | old | The previous working directory. |
void wait_pipeline | ( | t_info * | info, |
int | number | ||
) |
Wait for child processes to finish execution + update the exit code.
This function waits for the child processes created during command execution to finish and updates the exit code based on their exit status only if the exit status is not 79 (exit without argument). Then it frees the info struct and all it's allocated members.
info | Pointer to the t_info structure. |
number | Number of child processes to wait for. |
t_token which_type | ( | char * | s | ) |
Set the type of token.
Takes an input string('token') and chooses the type of token it represents. Only sets if the token is a pipe or some redirection, otherwise sets type to NONE.
s | The input string ('token') to identify the type for. |