# Minishell - Function Documentation ## main ```c int init_data(t_data *data, char **envp) ``` initializes the data structure that stores all the data required by the program converts the environment variables into a linked list and stores it stores the current working directory intializes 2 termios structures for later use ```c int handle_line(t_data *data) ``` function that handles reading user input and history also handles Crtl + D ## lexer ```c int is_token(char c) ``` this function checks if the character is part of the token group token group is ' " | < > if it does, return a non-zero value ```c int bunny_ears(char *line, int stop, int to_match) ``` this function handles the special index searching for bunny ears specifically, the stop index the start index would be at the first bunny ear found, the last index would be at the last bunny ear found these two indexes is then extracted and ft_substr is used to create the token ```c int find_token_pos(char *line, int *index_pair) ``` this function finds the index start and end for all the token it will first skip all leading whitespaces from start then it checks if the character is a token if it is, a specific way of extracting the indexes will be used if it is not, the end would be the location of the first character that is part of a token, a space or the adjacent character is the NULL terminating character ```c int lexer(t_data *data) ``` this function seperate all the tokens in the line of code each token will follow the following format (the token)(a single space to represent if it is connected to the previous token) so a command like : ls -la a b c will be expressed as (i hope you can see whitespaces) [ls ] -> [-la ] -> [a ] -> [b ] -> [c ] a command like : echo stuff"abcdef" \$SHLVL'pizza' will be expressed as [echo ] -> [stuff] -> ["abcdef"] -> [""] (special tokens will have "" nodes instead of spaces) -> [$SHLVL] -> ['pizza'] the expander will convert the spaces into "", and remove the spaces at the words the parser will remove the "" nodes, and combine the tokens that should be together ## parser ```c int find_next_cmd(char **tokens, int *index_pair) ``` this function locates the next command in the sequence of tokens commands are seperated by pipes with the '|' character ```c int remove_ears(char **string) ``` this will trim off the bunny ears in all of the tokens ```c char **extract_cmd(char **tokens, int *index_pair) ``` this function extracts the command based on the index pair it will first append all the tokens found into a buffer then when it meets a space token --> "" ```c int parser(t_data *data) ``` this function will do two things 1. seperate the token list according to the pipe token | 2. remove the space token --> "" it will then reconstruct a "command list" and return the list ## signals ```c void reset_attr(struct termios *saved) ``` resets the terminal attributes using the default termios structure saved ```c void new_line_handler(int sig_code) ``` function that handles the printing of a new line after a signal is sent used as the signal handler for SIGINT (Ctrl + C) ## vars ```c int get_keyword(char *line, int stop) ``` this function handles the special index searching for variables ($) the start index would be the dollar sign, the last index would be at the first character that isn't part of token or a space ```c void reset(char **temp_strings, int *indexes) ``` function resets the temporary storage used in recombine_parts, freeing all the strings inside it ```c void break_down(char *line, int *indexes, char **temp_strings) ``` this function breaks down the line based on the indexes given the value is stored inside the temporary storage in temp_strings example = ls $SHLVL la temp_string[0] = 'ls ' temp_string[1] = 'SHLVL' temp_string[2] = ' la' ```c void recombine_parts(char **store, char **temp_strings) ``` this function recombines all the strings found in temp_strings into a single string using ft_append() the recombined strings is stored in store ```c int one_side(char *line, int i, int in_ears) ``` function handles the $ disappearing when $"stuff" ```c void replace_dollar(t_data *data) ``` main function that handles variable replacement in commands ```c int get_equal_pos(t_list *node) ``` function that gets the position of the equal in an env variable ```c char *get_val(t_list *node) ``` function that gets the value of the node passed as parameter ```c t_list *find_var(t_list *vars, char *to_find) ``` function that finds and returns the node in which the variable to_find is located at ```c char *access_var(t_data *data, char *name) ``` function used to access variables if the keyword given in name begins with a ?, it will immediately return the value of the exit value of the last command else, it will search for the variable in the env variable list, and then return the value of the variable using get_val() if the keyword cannot be found in the list, it returns "" ```c void rebuild_envp(t_data *data) ``` function that converts the linked list of env variables into a 2d array inside data ```c t_list *set_env(char **envp) ``` function that converts the envp array of strings into a linked list ## redirection ```c int handle_redir_input(char *filename, int *in_fd) ``` function to handle normal input redirection in_fd is duplicated to the file opened as input ```c int handle_redir_input_heredoc(char *delimiter, int *in_fd, int std_in) ``` function to handle here document input redirection uses a pipe which acts as temporary storage for input when the delimiter character is found, the whole input except the delimiter is redirected when in_fd is duplicated to open the write end of the pipe ```c int handle_redir_output(char *filename, int *out_fd) ``` function to handle normal output redirection out_fd is duplicated to the file opened as output ```c int handle_redir_output_append(char *filename, int *out_fd) ``` function to handle append output redirection out_fd is duplicated to the file opened in append mode as output ```c int is_redirect(char *arg) ``` function to check to if a string is a redirection symbol ```c int contains_redirect(char **args) ``` function to check if a command (array of stringsg) contains a redirection symbol ```c int get_redirect_type(char *arg) ``` function to get the type of redirection that is passed into it ```c int handle_redirect(char **args, int *in_fd, int *out_fd, int std_in) ``` main function that handles redirections redirect info is a size 2 int array where 0 - type 1 - index ## cmds ```c int single_command(t_data *data, t_list *cmds) ``` function that runs a single command in the node cmds goes through builtin commands first and then tries to exec it ```c int get_command_count(t_data *data) ``` gets the number of commands in the cmd linked list ```c int get_exit_code(t_data *data, int exit_status) ``` gets the exit code of the last exited command ```c void multiple_commands(t_data *data) ``` function that handles the execution of multiple commands, simulating the behaviour of piping 1. the in_fd and out_fd of each command is set up - if the command is the first one being executed, the command output is the pipe input - if the command is the last one being executed, the command input is the previous pipe output - if the command is in between, command input is previous pipe output and command output is current pipe input 2. a child process is forked and redirections are handled, and the input and output are redirected 3. the parent process cleans up unused file descriptors and moves on to the next iteration of the loop 4. waiting for death (apparently) of all child processes ```c int exec_cmd(t_data *data, char **cmd_paths, char **args, char *cmd) ``` function that executes commands first gets all the potential paths, then checks if the process can access said path after that, the command is executed ```c void run_cmd(t_data *data) ``` main function that handles the execution of one or many commands ```c char *get_path_envp(t_data *data) ``` function that locates the PATH env variable from the linked list of env variables and then returns it ```c int is_executable(char *str) ``` function that checks if a string is in the format of an executable ```c char **get_cmd_path(t_data *data, char *cmd) ``` function that gets all the possible paths of a command if it is not an executable ```c char *trim_path(char *path) ``` function that trims the string "PATH=" from the front of a path ```c void append_stuff(char **paths, char *cmd) ``` function that appends the command name at the end of the path ## builtins ```c int builtin_cd(char **args, t_data *data) ``` function for the cd command in bash handles the changing in directory and \- symbol => previous directory ~ symbol => goes back to the directory stated in $HOME ```c int builtin_echo(char **args) ``` function for the echo command in bash handles option -n to print the arguements without a "\n" at the end ```c int builtin_exit(char **args) ``` function for the exit command in bash handles more than 2 arguments handles no arguments exiting with code 0 handles non numeric arguments handles arguments with values larger than 255 (max exit code) ```c int builtin_export(char **args, t_data *data) ``` function for the export command in bash locates the variable and then either adds it to the end of the linked list of env variables or updates the value of an existing variable handles no arguments ```c int builtin_pwd(t_data *data) ``` function for the pwd command in bash prints the current working directory ```c int builtin_env(t_data *data) ``` function for the env command in bash prints the linked list of env variables ```c int builtin_unset(char **args, t_data *data) ``` function for the unset command in bash locates the variable specified and then deletes it from the linked list of env variables handles "_" not being able to be unset ```c int handle_builtins(char *cmd, char **args, t_data *data) ``` main handler function for all builtin commands