Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ SRCS += core_routine.c
SRCS += clean_current_loop_context.c
SRCS += is_empty_input.c
SRCS += display_minishell_header.c
SRCS += getter.c

# srcs/exit_routines

SRCS += exit_shell_routine.c
SRCS += clean_process.c

# scrs/user_interface

Expand Down Expand Up @@ -133,6 +135,7 @@ SRCS += close_command_pipeline.c

SRCS += env.c
SRCS += export.c
SRCS += unset.c
SRCS += display_sorted_exportables_variables.c
SRCS += pwd.c
SRCS += exit.c
Expand Down
Empty file removed echo
Empty file.
3 changes: 3 additions & 0 deletions includes/command_interpretation.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ typedef enum e_path_type

typedef enum e_command_status
{
ERROR_SYSTEM,
ERROR_DENIED,
ERROR_NOT_FOUND,
ERROR_COMMAND = -1,
INVALID_COMMAND = -1,
VALID_COMMAND
Expand Down
9 changes: 9 additions & 0 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef enum e_builtin_type
{
ENV,
EXPORT,
UNSET,
PWD,
CD,
ECHO,
Expand Down Expand Up @@ -139,6 +140,7 @@ int execute_builtin(t_minishell_context *minishell_context,
t_command *command, bool is_in_pipeline);
int env(t_command *current_command);
int export(t_command *command);
int unset(t_command *command);
void display_sorted_exportables_variables(char **env);
int pwd(t_command *command);
int echo(t_command *command);
Expand All @@ -160,4 +162,11 @@ void setup_default_signals_handling(void);
void setup_command_mode_signals_handling(void);
void setup_heredoc_signals_handling(void);

// CLEAN AND EXIT

t_minishell_context *get_minishell_context(t_minishell_context *ptr,
int reset);
void clean_process(t_minishell_context *minishell_context,
t_command *command);

#endif
2 changes: 1 addition & 1 deletion libft
Submodule libft updated from dd43fd to 18c098
Empty file removed output2.txt
Empty file.
Empty file removed output3.txt
Empty file.
Empty file removed output4.txt
Empty file.
46 changes: 34 additions & 12 deletions srcs/builtins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "minishell.h"

bool ft_isnumber(char *args)
static bool ft_isnumber(char *args)
{
int i;

Expand All @@ -28,40 +28,62 @@ bool ft_isnumber(char *args)
return (true);
}

int error_exit(char *arg)
static int error_exit(char *arg, char **args)
{
ft_putstr_fd("exit\n", 2);
ft_putstr_fd("minishell: exit: ", 2);
ft_putstr_fd(arg, 2);
ft_putstr_fd(": numeric argument required\n", 2);
ft_free_and_null(args);
return (EXIT_FAILURE);
}

static void clean_exit(t_command *command)
{
t_minishell_context *minishell_context;

minishell_context = get_minishell_context(NULL, 0);
clean_process(minishell_context, command);
}

static void exit_process(t_command *command, char **args,
int *exit_code)
{
if (ft_isnumber(args[1]))
*exit_code = ft_atoi(args[1]);
else
{
error_exit(args[1], args);
clean_exit(command);
exit(EXIT_FAILURE);
}
}

int exit_builtin(t_command *command)
{
int exit_code;
char **args;

args = list_to_strs_array(command->command_args, args_list_to_args_array);
if (args == NULL)
{
ft_dprintf(STDERR_FILENO, "malloc error during exit builtin process. "
"Aborting\n");
exit(FAILURE);
}
exit_code = 0;
if (ft_lstsize(command->command_args) > 2)
{
ft_free_and_null(args);
ft_putstr_fd("exit\n", 2);
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
return (EXIT_FAILURE);
}
if (ft_lstsize(command->command_args) > 1)
{
if (ft_isnumber(args[1]))
{
exit_code = ft_atoi(args[1]);
}
else
{
return ((unsigned char)error_exit(args[1]));
}
}
exit_process(command, args, &exit_code);
ft_putstr_fd("exit\n", 2);
clean_exit(command);
ft_free_and_null(args);
exit((unsigned char)exit_code);
return ((unsigned char)exit_code);
}
75 changes: 74 additions & 1 deletion srcs/builtins/unset.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,80 @@

#include "minishell.h"

static size_t ft_get_max_size(const char *a, const char *b)
{
if (ft_strlen(a) > ft_strlen(b))
return (ft_strlen(a));
return (ft_strlen(b));
}

static t_variable_list get_variable_in_env(t_variable_list env,
char *variable_key)
{
t_variable_list to_find;

to_find = env;
while (to_find != NULL)
{
if (ft_strncmp(((t_variable *)to_find->content)->key, variable_key,
ft_get_max_size(((t_variable *)to_find->content)->key,
variable_key)) == 0)
return (to_find);
to_find = to_find->next;
}
return (NULL);
}

static void unset_variable(char *variable)
{
t_variable_list env;
t_variable_list variable_to_unset;
t_variable_list previous_variable_in_env;

env = *(get_environment());
variable_to_unset = get_variable_in_env(env, variable);
if (variable_to_unset == NULL)
return ;
previous_variable_in_env = (t_variable_list)ft_list_find_previous_element(
env, variable_to_unset);
if (previous_variable_in_env == NULL)
return ;
if (previous_variable_in_env != NULL)
previous_variable_in_env->next = variable_to_unset->next;
else
env = variable_to_unset->next;
delete_variable(variable_to_unset->content);
free(variable_to_unset);
}

static void unset_variables(char **variables)
{
size_t i;

i = 0;
while (variables[i] != NULL)
{
unset_variable(variables[i]);
++i;
}
}

int unset(t_command *command)
{
t_variable_list variable_to_delete;
char **variables_to_unset;

variables_to_unset = NULL;
if (ft_lstsize(command->command_args) == 1)
return (EXIT_SUCCESS);
variables_to_unset = list_to_strs_array(command->command_args,
args_list_to_args_array);
if (variables_to_unset == NULL)
{
ft_dprintf(STDERR_FILENO, "minishell: malloc "
"failure during export builtin process. Aborting\n");
exit(EXIT_FAILURE);
}
unset_variables(variables_to_unset + 1);
ft_free_and_null(variables_to_unset);
return (EXIT_SUCCESS);
}
4 changes: 4 additions & 0 deletions srcs/command_interpretation/command_path_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ static t_command_status check_uncomplete_absolute_path(t_command *command,
char **command_env)
{
if (ft_getenv("PATH", command_env) == NULL)
{
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
command->command_args->content);
return (INVALID_COMMAND);
}
return (build_complete_path(command, command_env));
}

Expand Down
22 changes: 20 additions & 2 deletions srcs/command_interpretation/command_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ static void undefined_command_process(t_minishell_context *minishell_context)
exit(exit_value);
}

static void exit_errors(t_minishell_context *minishell_context)
{
if (errno == EACCES)
{
clean_command_process(minishell_context);
exit(126);
}
if (errno == ENOENT)
{
clean_command_process(minishell_context);
exit(127);
}
else
{
clean_command_process(minishell_context);
exit(1);
}
}

void command_process(t_minishell_context *minishell_context,
t_command *command)
{
Expand All @@ -92,7 +111,6 @@ void command_process(t_minishell_context *minishell_context,
}
if (execute_command(command) == INVALID_COMMAND)
{
clean_command_process(minishell_context);
exit(COMMAND_NOT_FOUND_EXIT_STATUS);
exit_errors(minishell_context);
}
}
12 changes: 10 additions & 2 deletions srcs/command_interpretation/execute_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ t_command_status execute_command(t_command *command)
execve(command->command_binary_path, command_arguments,
command_environment);
}
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
command->command_args->content);
if (errno == EACCES)
{
ft_dprintf(STDERR_FILENO, "minishell: %s: permission denied\n",
command->command_args->content);
}
if (errno == ENOENT)
{
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
command->command_args->content);
}
clean_command_attributes(command_arguments, command_environment);
return (INVALID_COMMAND);
}
12 changes: 5 additions & 7 deletions srcs/command_interpretation/launch_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

#include "minishell.h"

static t_builtin_type get_builtin_type(const char *command_name)
static t_builtin_type get_b_type(const char *command_name)
{
static const char *builtins_dictionary[] = {
"env",
"export",
"unset",
"pwd",
"cd",
"echo",
Expand Down Expand Up @@ -59,11 +60,11 @@ static int execute_alone_builtin(t_command *command, t_builtin builtin)
int execute_builtin(t_minishell_context *minishell_context,
t_command *command, bool is_in_pipeline)
{
const t_builtin_type builtin_type
= get_builtin_type(command->command_name);
const t_builtin_type builtin_type = get_b_type(command->command_name);
static t_builtin builtins[] = {
env,
export,
unset,
pwd,
cd,
echo,
Expand All @@ -77,10 +78,7 @@ int execute_builtin(t_minishell_context *minishell_context,
if (is_in_pipeline == true)
{
builtin_return = builtins[builtin_type](command);
clean_command_process(minishell_context);
delete_command_pipeline(
&minishell_context->command_session.command_pipeline);
close_command_process_unused_fds(minishell_context, command);
clean_process(minishell_context, command);
exit(builtin_return);
}
return (builtin_return);
Expand Down
23 changes: 23 additions & 0 deletions srcs/exit_routines/clean_process.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* clean_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchobert <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/16 22:50:16 by tchobert #+# #+# */
/* Updated: 2025/02/16 22:50:24 by tchobert ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

void clean_process(t_minishell_context *minishell_context,
t_command *command)
{
clean_command_process(minishell_context);
delete_command_pipeline(
&minishell_context->command_session.command_pipeline);
close_command_process_unused_fds(minishell_context, command);
get_minishell_context(NULL, 1);
}
29 changes: 29 additions & 0 deletions srcs/minishell_main_routine/getter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* getter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchobert <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/16 21:28:51 by tchobert #+# #+# */
/* Updated: 2025/02/16 21:29:01 by tchobert ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

t_minishell_context *get_minishell_context(t_minishell_context *ptr, int reset)
{
static t_minishell_context *minishell_context;

if (reset == 1)
{
minishell_context = NULL;
return (NULL);
}
if (ptr == NULL)
return (minishell_context);
else
minishell_context = ptr;
return (minishell_context);
}
2 changes: 1 addition & 1 deletion srcs/minishell_main_routine/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ static int launch_shell(char **env)
{
t_minishell_context minishell_context;

display_minishell_header();
setup_default_signals_handling();
ft_bzero(&minishell_context, sizeof(minishell_context));
get_minishell_context(&minishell_context, 0);
if (*env != NULL)
{
if (build_environment(env) == PROCESS_FAILURE)
Expand Down