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 4 commits
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NAME = minishell
SRC = main \
utils/error \
utils/error_extra \
utils/miscellaneous \
structs/token \
structs/env \
Expand Down Expand Up @@ -28,6 +29,7 @@ SRC = main \
built_in/export \
built_in/echo \
built_in/cd \
built_in/cd_err \
built_in/utils \
built_in/unset \
built_in/env \
Expand Down
6 changes: 4 additions & 2 deletions includes/minishell.h
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/07/09 21:25:59 by mdekker #+# #+# */
/* Updated: 2023/11/22 14:20:29 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 22:34:36 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -102,7 +102,7 @@ char **ft_export_split(char *src, char delimter);
size_t **return_sorted_arr(t_vector *env);
void ft_free_size_t(size_t **arr, size_t len);
char *string_handler(char *input);

void write_err_cd(t_shell *data, size_t option, char *path, char *extra);
/* signals */
void signal_main(int signal_num);
void setup_hdoc_signals(void);
Expand All @@ -128,7 +128,9 @@ bool compare_env_key(void *item, void *key);
void exit_mini(char *str, int exit_code);
bool set_err(t_exit type, char *msg, t_shell *data);
void exec_err(char *str, t_exit type);
void exec_err_extra(char *str, t_exit type);
void write_err(t_shell *data);
void write_err_extra(t_shell *data);
bool rm_quotes(t_token *token, bool set_string);
bool type_compare(size_t num_args, t_types type, ...);
bool filter_operators(void *item);
Expand Down
25 changes: 10 additions & 15 deletions src/built_in/cd.c
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/14 17:29:00 by mdekker #+# #+# */
/* Updated: 2023/11/21 15:32:46 by mdekker ######## odam.nl */
/* Updated: 2023/11/22 22:15:34 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -53,9 +53,11 @@ static bool error_check(t_shell *data, t_group *group)
{
char *path;

if (!group->args[1])
return (true);
if (group->args[2])
{
printf("cd: too many arguments\n");
write(2, "cd: too many arguments\n", 23);
data->error_type = CATCH_ALL;
return (false);
}
Expand All @@ -64,12 +66,11 @@ static bool error_check(t_shell *data, t_group *group)
path = safe_env_get(&data->env, "OLDPWD");
if (!path)
{
printf("cd: OLDPWD not set\n");
write(2, "cd: OLDPWD not set\n", 19);
data->error_type = CATCH_ALL;
return (false);
}
printf("%s\n", path);
data->error_type = CATCH_ALL;
return (false);
}
return (true);
Expand All @@ -86,12 +87,14 @@ static char *get_path(t_group *group, t_shell *data)
{
char *path;

if (!group->args)
return (NULL);
if (!group->args[1] || !ft_strcmp(group->args[1], "~"))
{
path = ft_strdup(safe_env_get(&data->env, "HOME"));
if (!path)
{
printf("cd: HOME not set\n");
printf("minishell: cd: HOME not set\n");
data->error_type = CATCH_ALL;
return (NULL);
}
Expand Down Expand Up @@ -120,22 +123,14 @@ void ft_cd(t_group *group, t_shell *data)
return ;
oldpwd = getcwd(NULL, 0);
if (!oldpwd)
{
printf("cd: error retrieving current directory: %s\n", strerror(errno));
data->error_type = CATCH_ALL;
return ;
}
return (write_err_cd(data, 1, group->args[1], NULL));
path = get_path(group, data);
if (!path)
return ;
if (chdir(path) == -1)
{
printf("cd: %s: %s\n", path, strerror(errno));
data->error_type = CATCH_ALL;
}
return (write_err_cd(data, 2, path, strerror(errno)));
else
update_env(data, oldpwd);
data->error_type = NO_ERROR;
free(oldpwd);
free(path);
}
40 changes: 40 additions & 0 deletions src/built_in/cd_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* cd_err.c :+: :+: */
/* +:+ */
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/22 17:42:21 by mdekker/jde #+# #+# */
/* Updated: 2023/11/22 21:37:32 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

/**
* @brief Prints an error message based on the type of error
* @details If the type is GOOD, nothing is printed
* @details the t_Signal exit_status is set to the correct value
*
* @param data general data struct
*/
void write_err_cd(t_shell *data, size_t option, char *path, char *extra)
{
if (option)
{
write(2, "minishell: cd: ", 16);
write(2, path, ft_strlen(path));
write(2, ": No such file or directory\n", 29);
data->error_type = CATCH_ALL;
}
else if (option == 2)
{
write(2, "minishell: cd: ", 15);
write(2, path, ft_strlen(path));
write(2, ": ", 2);
write(2, extra, ft_strlen(extra));
write(2, "\n", 1);
data->error_type = CATCH_ALL;
}
}
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
81 changes: 65 additions & 16 deletions 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/20 20:56:32 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 @@ -36,13 +51,56 @@ static bool check_args(t_group *group, t_shell *data)
}
if (group->args[2] != NULL)
{
write(1, "minishell: exit: too many arguments\n", 37);
write(2, "minishell: exit: too many arguments\n", 37);
data->error_type = CATCH_ALL;
return (false);
}
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);
write(2, str, ft_strlen(str));
write(2, ": numeric argument required\n", 28);
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;
size_t offset;

i = 0;
offset = 0;
if (str[i] == '-')
return (free_shell(data, true), exit(156), NULL);
if (str[i] == '+')
{
offset++;
i++;
}
while (ft_isdigit(str[i]))
i++;
if (str[i] != '\0')
return (numeric_error(data, str), NULL);
return (&str[offset]);
}

/**
* @brief The exit command will exit the shell with the given exit code.
*
Expand All @@ -52,24 +110,15 @@ static bool check_args(t_group *group, t_shell *data)
*/
void ft_exit(t_group *group, t_shell *data)
{
size_t i;
int long long output;
char *str;

i = 0;
if (!check_args(group, data))
return ;
while (group->args[1][i])
{
if (!ft_isdigit(group->args[1][i]))
{
printf("minishell: exit: %s: numeric argument required\n",
group->args[1]);
data->error_type = CATCH_ALL;
return ;
}
i++;
}
output = ft_exit_atoi(group->args[1]);
str = return_start(data, group->args[1]);
if (!str)
return ;
output = ft_exit_atoi(str);
free_shell(data, true);
exit(output);
}
37 changes: 33 additions & 4 deletions src/built_in/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,46 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/10/31 22:20:10 by mdekker/jde #+# #+# */
/* Updated: 2023/11/20 21:49:48 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;

j = 0;
if (!ft_isalpha(arg[0]))
{
printf("export: `%s': not a valid identifier\n", arg);
write(2, "export: `", 9);
write(2, arg, ft_strlen(arg));
write(2, "': not a valid identifier\n", 26);
data->error_type = CATCH_ALL;
return ((*i)++, false);
}
while (arg[j] && arg[j] != '=')
while (arg[j] && ft_isalnum(arg[j]))
j++;
if (arg[j] == '\0')
return (true);
if (arg[j] != '=')
{
write(2, "export: `", 9);
write(2, arg, ft_strlen(arg));
write(2, "': not a valid identifier\n", 26);
data->error_type = CATCH_ALL;
return ((*i)++, false);
}
return (true);
}

Expand Down Expand Up @@ -83,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 Expand Up @@ -124,5 +154,4 @@ void ft_export(t_group *group, t_shell *data)
ft_free(env);
i++;
}
data->error_type = NO_ERROR;
}
Loading
Loading