Skip to content

Commit

Permalink
Changing how error codes are assigned and remembered by the program (#27
Browse files Browse the repository at this point in the history
)

- [x] Remove signal struct and replace it with an entry in the main data
struct
- [x] Change all builtin prototypes and error handling
- [x] Change executor prototypes to pass the correct data struct to the
builtins
  • Loading branch information
LithiumOx authored Nov 22, 2023
2 parents 9c91fe1 + 534a15d commit 6dd19fc
Show file tree
Hide file tree
Showing 38 changed files with 688 additions and 392 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "libft"]
path = libft
url = git@github.com:lithiumox-codam/libft.git
url = https://github.com/lithiumox-codam/libft.git
branch = minishell
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ SRC = main \
built_in/utils \
built_in/unset \
built_in/env \
built_in/sort \
lexer/index \
expander/index \
expander/expand_env \
expander/expand_quotes
expander/expand_quotes \
signals/index

SRCS = $(addsuffix .c, $(addprefix src/, $(SRC)))
OBJS = $(patsubst src/%.c, build/%.o, $(SRCS))
Expand Down
14 changes: 13 additions & 1 deletion includes/enum.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/13 17:41:00 by mdekker/jde #+# #+# */
/* Updated: 2023/10/30 18:26:44 by julius ######## odam.nl */
/* Updated: 2023/11/20 20:59:54 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -63,6 +63,18 @@ typedef enum e_exit
OUT_OF_SCOPE
} t_exit;

typedef enum e_error
{
NO_ERROR = 0,
CATCH_ALL = 1,
MISUSE_OF_SHELL = 2,
PERMISSION_DENIED = 126,
COMMAND_NOT_FOUND = 127,
NOT_VALID = 128,
UNEXPECTED_EOF = 130,
SYNTAX_ERROR = 258,
} t_error;

/**
* @brief defines which process is being executed
*/
Expand Down
26 changes: 20 additions & 6 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/16 23:17:36 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/22 14:20:29 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

Expand All @@ -18,6 +18,7 @@
# include <libft.h>
# include <readline/history.h>
# include <readline/readline.h>
# include <signal.h>
# include <stdarg.h>
# include <stdbool.h>
# include <stdio.h>
Expand Down Expand Up @@ -71,10 +72,10 @@ bool hdoc_expand(char **str, t_shell *data);
/* executor */
bool executor(t_shell *data);
bool create_processes(t_shell *data);
void exec_process(t_group *group, t_process type, t_vector *env_vec);
void exec_process(t_group *group, t_process type, t_shell *data);
void dup_fd(t_group *group, t_process type);
void check_cmd(t_group *group, t_process type, t_vector *env_vec);
void exec_built_in(t_group *group, t_process type, t_vector *env_vec);
void exec_built_in(t_group *group, t_process type, t_shell *data);
void exec_absolute_path(t_group *group, t_process type, t_vector *env_vec);
void handle_redirects(t_group *group);
void validate_redirects(t_group *group);
Expand All @@ -89,13 +90,25 @@ char **combine_env(t_vector *env_vec);

/* built_in */
void ft_exit(t_group *group, t_shell *data);
void ft_cd(t_group *group, t_vector *env_vec);
void ft_export(t_group *group, t_vector *env_vec);
void ft_unset(t_group *group, t_vector *env_vec);
void ft_cd(t_group *group, t_shell *data);
void ft_export(t_group *group, t_shell *data);
void ft_unset(t_group *group, t_shell *data);
void ft_echo(t_group *group);
void ft_pwd(void);
void ft_env(t_vector *env_vec);
void print_env_dec(t_vector *env, char *arg_2);
void update_or_create_env(t_vector *env, char *key, char *value);
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);

/* signals */
void signal_main(int signal_num);
void setup_hdoc_signals(void);
void signal_hdoc(int signal_num);
void setup_child_signals(void);
void signal_child(int signal_num);

/* structs */
t_token *create_token(char *value, t_types type);
Expand All @@ -118,6 +131,7 @@ void exec_err(char *str, t_exit type);
void write_err(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);
// bool out_of_scope(t_vector *found, t_shell *data);
bool is_redirect(t_token *token);

Expand Down
3 changes: 2 additions & 1 deletion includes/structs.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/10 11:15:16 by mdekker #+# #+# */
/* Updated: 2023/10/12 18:19:49 by julius ######## odam.nl */
/* Updated: 2023/11/18 23:06:40 by mdekker/jde ######## odam.nl */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -83,6 +83,7 @@ typedef struct s_shell
t_vector env;
t_exec *exec;
t_exit exit_type;
t_error error_type;
char *exit_msg;
} t_shell;

Expand Down
2 changes: 1 addition & 1 deletion libft
114 changes: 80 additions & 34 deletions src/built_in/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,101 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/11/14 17:29:00 by mdekker #+# #+# */
/* Updated: 2023/11/17 01:07:00 by mdekker/jde ######## odam.nl */
/* Updated: 2023/11/21 15:32:46 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

extern t_signal g_signal;

/**
* @brief Updates the value of an environment variable when it already exists
* @param env_var The env struct
* @param key The key to compare to
* @return void
*/
static void update_env(t_vector *env, char *key, char *value)
static void update_env(t_shell *data, char *oldpwd)
{
char *buff;

buff = getcwd(NULL, 0);
update_or_create_env(&data->env, "OLDPWD", oldpwd);
update_or_create_env(&data->env, "PWD", buff);
free(buff);
}

/**
* @brief Helper function to safely get an environment variable
* @param *env The environment vector
* @param *key The key to compare to
* @return char* The value of the environment variable or NULL
*/
static char *safe_env_get(t_vector *env, char *key)
{
t_env *env_var;

env_var = vec_find_f(env, compare_env_key, key);
if (env_var)
{
free(env_var->value);
env_var->value = ft_strdup(value);
}
return (env_var->value);
return (NULL);
}

/**
* @brief Prints the error message for cd
* @param path The path that caused the error
* @return void
*/
static void print_err_cd(char *path)
static bool error_check(t_shell *data, t_group *group)
{
printf("cd: %s: %s\n", strerror(errno), path);
g_signal.exit_status = 1;
char *path;

if (group->args[2])
{
printf("cd: too many arguments\n");
data->error_type = CATCH_ALL;
return (false);
}
if (!ft_strcmp(group->args[1], "-"))
{
path = safe_env_get(&data->env, "OLDPWD");
if (!path)
{
printf("cd: OLDPWD not set\n");
data->error_type = CATCH_ALL;
return (false);
}
printf("%s\n", path);
data->error_type = CATCH_ALL;
return (false);
}
return (true);
}

/**
* @brief Helper function for vec_find_f to safely get an environment variable
* @param *env The environment vector
* @param *key The key to compare to
* @return char* The value of the environment variable or NULL
* @brief Gets the path to change to
*
* @param group The group struct with the args
* @param data The shell struct
* @return char* The path to change to
*/
static char *safe_env_get(t_vector *env, char *key)
static char *get_path(t_group *group, t_shell *data)
{
t_env *env_var;
char *path;

env_var = vec_find_f(env, compare_env_key, key);
if (env_var)
return (env_var->value);
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");
data->error_type = CATCH_ALL;
return (NULL);
}
}
else if (ft_strncmp(group->args[1], "~/", 2) == 0)
path = ft_strjoin(safe_env_get(&data->env, "HOME"), group->args[1] + 1);
else
path = ft_strdup(group->args[1]);
return (path);
}

/**
Expand All @@ -67,29 +111,31 @@ static char *safe_env_get(t_vector *env, char *key)
* @param env The environment
* @return void
*/
void ft_cd(t_group *group, t_vector *env)
void ft_cd(t_group *group, t_shell *data)
{
char *path;
char *oldpwd;

if (!error_check(data, group))
return ;
oldpwd = getcwd(NULL, 0);
if (!oldpwd)
exit_mini("ft_cd", 1);
if (!group->args[1])
path = safe_env_get(env, "HOME");
else if (ft_strcmp(group->args[1], "-") == 0)
path = safe_env_get(env, "OLDPWD");
else
path = group->args[1];
if (!path)
{
printf("cd: HOME not set\n");
g_signal.exit_status = 1;
printf("cd: error retrieving current directory: %s\n", strerror(errno));
data->error_type = CATCH_ALL;
return ;
}
path = get_path(group, data);
if (!path)
return ;
if (chdir(path) == -1)
print_err_cd(path);
{
printf("cd: %s: %s\n", path, strerror(errno));
data->error_type = CATCH_ALL;
}
else
update_env(env, "OLDPWD", oldpwd);
update_env(data, oldpwd);
data->error_type = NO_ERROR;
free(oldpwd);
free(path);
}
50 changes: 26 additions & 24 deletions src/built_in/echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,37 @@
/* By: mdekker/jde-baai <team@codam.nl> +#+ */
/* +#+ */
/* Created: 2023/10/23 10:10:23 by mdekker/jde #+# #+# */
/* Updated: 2023/11/17 15:39:14 by mdekker ######## odam.nl */
/* Updated: 2023/11/21 16:08:07 by mdekker ######## odam.nl */
/* */
/* ************************************************************************** */

#include <minishell.h>

extern t_signal g_signal;

/**
* @brief Checks if a flag only contains n's and if it does, it will return the
* index of the first non-flag argument or 1 if no flags.
*
* @param group The group struct with the args
* @return size_t The index of the first non-flag argument or 1 if no flags
*/
static size_t check_flags(t_group *group)
* @brief Checks if a flag only contains n's and if it does,
it will return the
* index of the first non-flag argument or 1 if no flags.
*
* @param group The group struct with the args
* @return size_t The index of the first non-flag argument or 1 if no flags
*/
static bool check_flag(char *arg, bool *flag)
{
size_t i;
size_t j;

i = 1;
while (group->args[i])
{
j = 1;
if (group->args[i][0] != '-')
return (i);
while (group->args[i][j] == 'n')
j++;
if (group->args[i][j] != '\0' && group->args[i + 1] == NULL)
return (i);
i = 0;
if (arg[i] != '-')
return (false);
else
i++;
while (arg[i] == 'n')
i++;
if (arg[i] == '\0')
{
*flag = true;
return (true);
}
return (1);
return (false);
}

/**
Expand All @@ -49,16 +47,20 @@ static size_t check_flags(t_group *group)
void ft_echo(t_group *group)
{
size_t i;
bool flag;

i = check_flags(group);
i = 1;
flag = false;
while (group->args[i] && check_flag(group->args[i], &flag))
i++;
while (group->args[i])
{
write(1, group->args[i], ft_strlen(group->args[i]));
if (group->args[i + 1])
write(1, " ", 1);
i++;
}
if (group->args[1] && ft_strcmp(group->args[1], "-n") != 0)
if (!flag)
write(1, "\n", 1);
exit(0);
}
Loading

0 comments on commit 6dd19fc

Please sign in to comment.