Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final #29

Merged
merged 9 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/built_in/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/16 23:12:07 by mdekker/jde #+# #+# */
/* Updated: 2023/11/21 18:15:44 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 22:55:19 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief The env command built-in
* @details Prints the enviroment variables
*
* @warning This function also sorts the enviroment variables alphabetically
* which is technically not part of the env command but if you want me to
* remove it, I can. I just thought it would be nice to have it sorted.
*
* @param env_vec The enviroment vector
*/
void ft_env(t_vector *env_vec)
{
size_t i;
Expand Down
31 changes: 30 additions & 1 deletion src/built_in/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/10/04 15:01:59 by mdekker/jde #+# #+# */
/* Updated: 2023/11/22 22:03:01 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/22 22:48:24 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief Simple atoi function that returns a int long long
*
* @param str The string to convert
* @return int long long The converted string in a int long long
*/
int long long ft_exit_atoi(char *str)
{
size_t i;
Expand All @@ -27,6 +33,15 @@ int long long ft_exit_atoi(char *str)
return (output);
}

/**
* @brief Checks if the arguments are valid for the exit command
* think about exit 1 2 3 (too many arguments) or exit hello (not a number)
*
* @param group
* @param data
* @return true
* @return false
*/
static bool check_args(t_group *group, t_shell *data)
{
if (group->args[1] == NULL)
Expand All @@ -43,6 +58,12 @@ static bool check_args(t_group *group, t_shell *data)
return (true);
}

/**
* @brief Simple error message for when the argument is not a number
*
* @param data The shell struct to set the error type
* @param str The string that is not a number
*/
static void numeric_error(t_shell *data, char *str)
{
write(2, "minishell: exit: ", 17);
Expand All @@ -51,6 +72,14 @@ static void numeric_error(t_shell *data, char *str)
data->error_type = MISUSE_OF_SHELL;
}

/**
* @brief Returns the start of the string if it is a valid number
* if not, it will print an error message and return NULL
*
* @param data The shell struct
* @param str The string to check
* @return char* The start of the string if it is a valid number
*/
static char *return_start(t_shell *data, char *str)
{
size_t i;
Expand Down
20 changes: 19 additions & 1 deletion src/built_in/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/10/31 22:20:10 by mdekker/jde #+# #+# */
/* Updated: 2023/11/22 17:16:22 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/22 22:50:56 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief Validates the input of the export builtin
*
* @param arg The argument to validate
* @param i The index of the argument
* @param data The data struct
* @return true When the argument is valid
* @return false When the argument is invalid
*/
static bool validate_input(char *arg, size_t *i, t_shell *data)
{
size_t j;
Expand Down Expand Up @@ -95,6 +104,15 @@ static void add_env(t_vector *env_vec, char *key, char *value)
clear_token(token);
}

/**
* @brief A helper function for the export builtin that handles the
* export of a variable that already exists or adds a new variable
* using add_env
*
* @param token The token to update
* @param data The data struct
* @param env The env array that contains the key and value (can be null)
*/
static void export_helper(t_env *token, t_shell *data, char **env)
{
if (token)
Expand Down
18 changes: 12 additions & 6 deletions src/built_in/utils.c
LithiumOx marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/16 18:52:54 by mdekker/jde #+# #+# */
/* Updated: 2023/11/21 18:26:53 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 23:00:58 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -24,21 +24,20 @@ void print_env_dec(t_vector *env, char *arg_2)
size_t i;
size_t **arr;
char *value;
char *key;

i = 0;
if (arg_2)
return ;
arr = return_sorted_arr(env);
while (i < env->length)
{
key = ((t_env *)vec_get(env, *arr[i]))->key;
value = ((t_env *)vec_get(env, *arr[i]))->value;
if (value)
{
printf("declare -x %s=\"%s\"\n", ((t_env *)vec_get(env,
*arr[i]))->key, value);
}
printf("declare -x %s=\"%s\"\n", key, value);
else
printf("declare -x %s\n", ((t_env *)vec_get(env, *arr[i]))->key);
printf("declare -x %s\n", key);
i++;
}
ft_free_size_t(arr, env->length);
Expand Down Expand Up @@ -69,6 +68,13 @@ void update_or_create_env(t_vector *env, char *key, char *value)
}
}

/**
* @brief A function for when the export command is given a string without a
* value (e.g. export FOO) then it will return a char ** with the key and NULL
*
* @param src The string to split
* @return char** The splitted string
*/
static char **only_key_helper(char *src)
{
char **ret;
Expand Down
Loading