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
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ LIBFT := $(PATH_LIBFT)libft.a

PATH_SRCS += srcs/
PATH_SRCS += srcs/minishell_main_routine
PATH_SRCS += srcs/signals_handling
PATH_SRCS += srcs/exit_routines
PATH_SRCS += srcs/user_interface
PATH_SRCS += srcs/history
Expand Down Expand Up @@ -42,7 +43,6 @@ SRCS += exit_shell_routine.c

# scrs/user_interface

SRCS += signals.c
SRCS += prompt.c

# srcs/history
Expand Down Expand Up @@ -88,6 +88,7 @@ SRCS += set_variable_to_environment.c
# srcs/environment_building

SRCS += build_environment.c
SRCS += build_minimal_environment.c

# srcs/semantic_analysis

Expand All @@ -106,6 +107,9 @@ SRCS += get_command_name.c
SRCS += is_builtin.c
SRCS += delete_command_pipeline.c
SRCS += setup_pipe.c
SRCS += is_delimiter.c
SRCS += ensure_stdin_is_open.c
SRCS += heredoc_interruption_routine.c

# srcs/command_interpretation

Expand Down Expand Up @@ -156,6 +160,14 @@ SRCS += expander_exit.c
SRCS += erase_quotes.c
SRCS += expand_variable_utils.c

# srcs/signals_handling

SRCS += setup_main_process_signals_handling.c
SRCS += setup_command_mode_signals.c
SRCS += setup_heredoc_signals_handling.c
#SRCS += setup_execution_mode_signal_handling.c
SRCS += signals.c

# srcs/exit_status

SRCS += set_exit_status.c
Expand Down
1 change: 1 addition & 0 deletions includes/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct s_variable

t_variable_list *get_environment(void);
t_status build_environment(char **variables);
t_status build_minimal_environment(void);
t_status set_variable_from_keyvalue_to_environment(
const char *keyvalue, bool make_it_exportable,
t_variable_list *environment);
Expand Down
7 changes: 6 additions & 1 deletion includes/expander.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
/* */
/* ************************************************************************** */

#include "minishell.h"
#ifndef EXPANDER_H
# define EXPANDER_H

# include "minishell.h"

typedef enum e_quote_state
{
Expand All @@ -33,3 +36,5 @@ void change_quote_state(char quote, t_quote_state *quote_state);
void expander_exit(void);
void erase_quotes(t_token_list token_list);
char *manage_return_cmd(t_lexem temp_expanded, t_lexem expanded_word);

#endif
1 change: 1 addition & 0 deletions includes/lexing.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ typedef struct s_token
{
t_lexem token_lexem;
t_token_type token_type;
bool is_surrounded_by_quotes;
} t_token;

typedef t_token t_grammar_element;
Expand Down
14 changes: 12 additions & 2 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
# include <sys/types.h>
# include <sys/wait.h>
# include <limits.h>
# include <signal.h>

// GLOBAL VARIABLE

extern volatile sig_atomic_t g_received_signal;

// DEFINES

Expand Down Expand Up @@ -82,14 +87,13 @@ typedef enum e_new_line_status
NO_NEW_LINE
} t_new_line_status;

typedef int (*t_builtin)(t_command *current_command);
typedef int (*t_builtin)(t_command *current_command);

// PROTOTYPES

void display_minishell_header(void);
int core_routine(t_minishell_context *minishell_context);
int exit_shell_routine(void);
char *prompt_gets_user_input(void);
t_lexing_status lexe_input(t_command_session *current_command);
t_syntax_status parse_input(t_token_list token_list);
t_command_pipeline semantic_analyzer(t_token_list token_list);
Expand Down Expand Up @@ -148,4 +152,10 @@ int update_env_variables(char *old_pwd);
void set_exit_status(int exit_status);
int get_exit_status_value(void);

// SIGNALS HANDLING

void setup_default_signals_handling(void);
void setup_command_mode_signals_handling(void);
void setup_heredoc_signals_handling(void);

#endif
6 changes: 6 additions & 0 deletions includes/semantic.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ void delete_command_pipeline(
t_command_pipeline *cmd_pipeline);
void get_command_name(t_command *command);
bool is_builtin(const char *command_name);
bool is_delimiter(const char *delimiter,
const char *line);
void ensure_stdin_is_open(void);
void heredoc_interruption_routine(
const char *delimiter,
int heredoc_lines);

void print_arguments_list(
t_command_args arguments_list);
Expand Down
11 changes: 8 additions & 3 deletions includes/user_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
// DEFINES

# define MSH_PROMPT "SDF$> "
# define HEREDOC_PROMPT "captain'hirdock>"
# define CTRL_D 0
# define FAILURE -1
# define MAIN_PROMPT 0
# define SUBPROMPT 1

// PROTOTYPES

void signal_handler(int signum);
void received_signal(void);
void setup_signals(struct sigaction *sa);
//void signal_handler(int signum);
//void received_signal(void);
char *prompt_gets_user_input(bool is_subprompt);
void setup_main_prompt_signals_handling(void);
//void setup_signals(struct sigaction *sa);

#endif
2 changes: 1 addition & 1 deletion srcs/builtins/cd/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static int change_directory(char *target)
if (chdir(target) == -1)
{
free(target);
perror("chdir");
perror("cd");
return (EXIT_FAILURE);
}
free(target);
Expand Down
2 changes: 2 additions & 0 deletions srcs/command_interpretation/close_command_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ int close_command_pipeline(t_command_pipeline cmd_pipeline,
{
if (WIFEXITED(last_command_status))
exit_status = WEXITSTATUS(last_command_status);
else if (WIFSTOPPED(last_command_status))
exit_status = 128 + WSTOPSIG(last_command_status);
else if (WIFSIGNALED(last_command_status))
exit_status = (128 + WTERMSIG(last_command_status));
else
Expand Down
3 changes: 3 additions & 0 deletions srcs/command_interpretation/command_interpreter.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ static void launch_command(t_minishell_context *minishell_context,
command->command_pid = command_process_pid;
if (command_process_pid > 0)
{
setup_default_signals_handling();
main_process_io_management(command);
}
if (command_process_pid == 0)
{
setup_command_mode_signals_handling();
command_process(minishell_context, command);
}
}
Expand Down Expand Up @@ -78,6 +80,7 @@ int command_pipeline_interpreter(t_minishell_context *minishell_context)
t_command_pipeline current_command;
pid_t last_command_pid;

setup_default_signals_handling();
cmd_pipeline = minishell_context->command_session.command_pipeline;
current_command = cmd_pipeline;
while (current_command != NULL)
Expand Down
2 changes: 2 additions & 0 deletions srcs/command_interpretation/command_path_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ static t_command_status check_complete_absolute_path(t_command *command,
static t_command_status check_uncomplete_absolute_path(t_command *command,
char **command_env)
{
if (ft_getenv("PATH", command_env) == NULL)
return (INVALID_COMMAND);
return (build_complete_path(command, command_env));
}

Expand Down
13 changes: 10 additions & 3 deletions srcs/command_interpretation/command_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,26 @@ void close_command_process_unused_fds(t_minishell_context *minishell_context,
}
}

static void undefined_command_process(t_minishell_context *minishell_context)
{
const int exit_value = get_exit_status_value();

clean_command_process(minishell_context);
exit(exit_value);
}

void command_process(t_minishell_context *minishell_context,
t_command *command)
{
close_command_process_unused_fds(minishell_context, command);
if (setup_command_redirections(command) == EXIT_FAILURE)
{
clean_command_process(minishell_context);
exit(FAILURE);
exit(EXIT_FAILURE);
}
if (command->command_nature == UNDEFINED)
{
clean_command_process(minishell_context);
exit(SUCCESS);
undefined_command_process(minishell_context);
}
if (command->command_nature == BUILTIN)
execute_builtin(minishell_context, command, BUILTIN_IN_PIPELINE);
Expand Down
4 changes: 3 additions & 1 deletion srcs/command_interpretation/execute_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static t_command_status get_command_validity(t_command *command,
const t_path_type command_path_type = get_path_type(
command->command_name);

if (*(command->command_name) == '\0')
return (INVALID_COMMAND);
return (command_path_manager(command, command_env, command_path_type));
}

Expand All @@ -61,7 +63,7 @@ t_command_status execute_command(t_command *command)
execve(command->command_binary_path, command_arguments,
command_environment);
}
ft_dprintf(STDERR_FILENO, "minishell: command not found: %s\n",
ft_dprintf(STDERR_FILENO, "minishell: %s: command not found\n",
command->command_args->content);
clean_command_attributes(command_arguments, command_environment);
return (INVALID_COMMAND);
Expand Down
37 changes: 37 additions & 0 deletions srcs/environment_building/build_minimal_environment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* build_minimal_environment.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchobert <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 18:58:08 by tchobert #+# #+# */
/* Updated: 2025/02/15 18:58:17 by tchobert ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

t_status set_pwd(void)
{
char *pwd_variable;
char cwd[PATH_MAX];

if (getcwd(cwd, PATH_MAX) == NULL)
{
perror("getcwd");
return (PROCESS_FAILURE);
}
if (ft_asprintf(&pwd_variable, "PWD=%s", cwd) == -1)
return (PROCESS_FAILURE);
if (set_variable_from_keyvalue(pwd_variable, EXPORTABLE) == PROCESS_FAILURE)
return (PROCESS_FAILURE);
return (PROCESS_SUCCESS);
}

t_status build_minimal_environment(void)
{
if (set_variable_from_keyvalue("SHLVL=1", EXPORTABLE) == PROCESS_FAILURE)
return (PROCESS_FAILURE);
return (set_pwd());
}
6 changes: 4 additions & 2 deletions srcs/exit_routines/exit_shell_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

int exit_shell_routine(void)
{
const int exit_value = get_exit_status_value();

delete_variables_list();
printf("exit\n");
exit (EXIT_SUCCESS);
ft_putstr_fd("exit\n", STDERR_FILENO);
exit(exit_value);
}
19 changes: 19 additions & 0 deletions srcs/expander/erase_quotes.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@

#include "minishell.h"

static bool is_surrounded_by_quotes(char *word)
{
size_t word_len;

if (word == NULL || *word == '\0')
return (false);
word_len = ft_strlen(word);
if (word_len < 2)
return (false);
if (*word == '\"' && word[word_len -1] == '\"')
return (true);
else if (*word == '\'' && word[word_len -1] == '\'')
return (true);
else
return (false);
}

static char *erase_quotes_in_word(t_lexem word, t_quote_state *quote_state)
{
size_t i;
Expand Down Expand Up @@ -51,6 +68,8 @@ void erase_quotes(t_token_list token_list)
token = current_token->content;
if (token->token_type == WORD)
{
token->is_surrounded_by_quotes
= is_surrounded_by_quotes(token->token_lexem);
expanded_token = erase_quotes_in_word(token->token_lexem,
&quote_state);
free(token->token_lexem);
Expand Down
3 changes: 2 additions & 1 deletion srcs/minishell_main_routine/core_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ int core_routine(t_minishell_context *minishell_context)
{
while (MSH_LOOP)
{
g_received_signal = 0;
minishell_context->command_session.user_input_line
= prompt_gets_user_input();
= prompt_gets_user_input(MAIN_PROMPT);
current_loop_process(minishell_context);
clean_current_loop_context(minishell_context);
}
Expand Down
30 changes: 23 additions & 7 deletions srcs/minishell_main_routine/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,37 @@

#ifndef TEST_MODE

volatile sig_atomic_t g_received_signal = 0;

static void environment_initialisation_failure_exit_process(void)
{
ft_dprintf(STDERR_FILENO, "Fatal error while initializing minishell's"
" environment. Aborting\n");
exit(EXIT_FAILURE);
}

static int launch_shell(char **env)
{
t_minishell_context minishell_context;
struct sigaction sa;

display_minishell_header();
setup_default_signals_handling();
ft_bzero(&minishell_context, sizeof(minishell_context));
if (build_environment(env) == PROCESS_FAILURE)
if (*env != NULL)
{
if (build_environment(env) == PROCESS_FAILURE)
{
environment_initialisation_failure_exit_process();
}
}
else
{
ft_dprintf(STDERR_FILENO, "Fatal error while initializing minishell's"
" environment\n");
exit(EXIT_FAILURE);
if (build_minimal_environment() == PROCESS_FAILURE)
{
environment_initialisation_failure_exit_process();
}
}
set_exit_status(0);
setup_signals(&sa);
set_exit_status(EXIT_SUCCESS);
return (core_routine(&minishell_context));
}

Expand Down
Loading