[ADD] signals handler *

also reorganize the code
This commit is contained in:
mcolonna 2024-06-18 15:43:22 +02:00
parent 435d06f3a9
commit 0898d3b79a
21 changed files with 454 additions and 151 deletions

22
include/ask_command.h Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ask_command.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 13:27:53 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ASK_COMMAND_H
# define ASK_COMMAND_H
# include "include.h"
// Show the prompt and ask a command to the user.
// Return the command, or NULL if EOF if ^D.
const char *ask_command(t_env *env);
#endif

24
include/builtins.h Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 13:00:54 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef BUILTINS_H
# define BUILTINS_H
# include "include.h"
// Check if the command is a builtin.
bool is_builtin(const char *str);
// Execute the builtin command
int exec_builtin(t_call call);
#endif

19
include/cool_readline.h Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cool_readline.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/18 13:41:32 by mcolonna #+# #+# */
/* Updated: 2024/06/18 13:43:42 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef COOL_READLINE_H
# define COOL_READLINE_H
// Same than readline, but (TODO)
char *cool_readline(const char *prompt);
#endif

31
include/error.h Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 13:50:43 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ERROR_H
# define ERROR_H
# include "include.h"
// Set the global mc freed when exit.
void set_global_mc(t_memclass mc);
// Call to show an error.
// If msg == "errno", use perror()
void minishell_error(const char *msg);
// Show the errno error and exit the program.
void fatal_error(const char *msg);
// Show a specific message and exit the program.
void fatal_error_msg(const char *msg);
#endif

21
include/exec_command.h Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_command.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 13:54:58 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef EXEC_COMMAND_H
# define EXEC_COMMAND_H
# include "include.h"
// Execute the command given. Return the exit status.
int execute_command(t_env *env, t_command command);
#endif

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/06 16:36:19 by mcolonna ### ########.fr */
/* Updated: 2024/06/18 13:53:15 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,95 +24,21 @@
# include <stdlib.h>
# include <strings.h>
# include <sys/wait.h>
# include "libft.h"
# include "libtf.h"
# include <readline/history.h>
# include <readline/readline.h>
# include <signal.h>
# include <termios.h>
# include "libft.h"
# include "libtf.h"
///// ASK COMMAND /////
// Show the prompt and ask a command to the user.
// Return the command.
const char *ask_command(t_memclass mc);
///// PARSE_COMMAND /////
typedef struct s_variable
{
const char *name;
const char *value;
} t_variable;
// Represents a call to a program (the program name and its arguments)
typedef struct s_call
{
const char *program; // path of the program to call.
int argc; // number of arguments given
char *const *argv; // arguments given (ended by NULL)
// (argc and argv must include the program name)
} t_call;
// Represents a command given by the user.
typedef struct s_command
{
int error; // 0 if parse_command() succeded, error status if not
bool empty; // true if there isn't anything to do
const t_call *calls; // all calls to programs (ended by .program == NULL)
int input_fd; // fd to use with '<' redirection (0 by default)
int output_fd; // fd to use with '>' redirection (1 by default)
} t_command;
// An element of a list of pipes
typedef struct s_pipes
{
int pipe[2]; // Simple pipe, we write on pipe[1] and we read on pipe[0]
} t_pipes;
// Return the t_command representing the command given by the user.
// If error, return a t_command wth the value .error = true.
t_command parse_command(const t_memclass mc, const char *command,
t_list *variables);
///// EXECUTE COMMAND /////
// Execute the command given. Return the exit status.
int execute_command(t_memclass mc, t_command command,
char *const envp[]);
///// ERROR /////
// Call to show an error.
// If msg == "errno", use perror()
void minishell_error(const char *msg);
// Call perror() and exit the program.
void fatal_error(const char *msg);
// Call strerror() and exit the program.
void fatal_error_msg(const char *msg);
///// PATH /////
// Get the PATH values.
// Return a list of strings ended by NULL.
const char **get_path(const t_memclass mc, char *const envp[]);
// Search the program in $PATH.
// Returns the path of the program to use, or NULL if there is none.
// If there is several possibilities, it returns the one from the first path
// given in $PATH.
const char *search_path(
const t_memclass mc, const char **path, const char *prog);
///// BUILTIN /////
// Check if the command is a builtin.
bool is_builtin(const char *str);
// Execute the builtin command
int exec_builtin(t_call call);
///// MAIN /////
extern t_memclass g_mc;
# include "main.h"
# include "cool_readline.h"
# include "ask_command.h"
# include "parse_command.h"
# include "exec_command.h"
# include "error.h"
# include "path.h"
# include "builtins.h"
# include "signalhandler.h"
#endif

28
include/main.h Normal file
View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 16:39:07 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAIN_H
# define MAIN_H
# include "main.h"
typedef struct s_env
{
t_memclass mc_global; // open during all the runtime
t_memclass mc_command; // open during the execution of a command
int errorstatus; // Error status of the last executed command
t_list *variables; // Shell variables
char *const *envp; // Environment variables
bool exit; // Set to true to exit minishell
} t_env;
#endif

53
include/parse_command.h Normal file
View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_command.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 14:01:41 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PARSE_COMMAND_H
# define PARSE_COMMAND_H
# include "include.h"
typedef struct s_variable
{
const char *name;
const char *value;
} t_variable;
// Represents a call to a program (the program name and its arguments)
typedef struct s_call
{
const char *program; // path of the program to call.
int argc; // number of arguments given
char *const *argv; // arguments given (ended by NULL)
// (argc and argv must include the program name)
} t_call;
// Represents a command given by the user.
typedef struct s_command
{
int error; // 0 if parse_command() succeded, error status if not
bool empty; // true if there isn't anything to do
const t_call *calls; // all calls to programs (ended by .program == NULL)
int input_fd; // fd to use with '<' redirection (0 by default)
int output_fd; // fd to use with '>' redirection (1 by default)
} t_command;
// An element of a list of pipes
typedef struct s_pipes
{
int pipe[2]; // Simple pipe, we write on pipe[1] and we read on pipe[0]
} t_pipes;
// Return the t_command representing the command given by the user.
// If error, return a t_command wth the value .error = true.
t_command parse_command(t_env *env, const char *command);
#endif

31
include/path.h Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* path.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 13:00:10 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PATH_H
# define PATH_H
# include "include.h"
///// PATH /////
// Get the PATH values.
// Return a list of strings ended by NULL.
const char **get_path(const t_memclass mc, char *const envp[]);
// Search the program in $PATH.
// Returns the path of the program to use, or NULL if there is none.
// If there is several possibilities, it returns the one from the first path
// given in $PATH.
const char *search_path(
const t_memclass mc, const char **path, const char *prog);
#endif

20
include/signalhandler.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signalhandler.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/06/13 13:08:35 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SIGNALHANDLER_H
# define SIGNALHANDLER_H
# include "include.h"
void handle_signals(void);
#endif