42sh 1.0.0
Create a shell in C
Loading...
Searching...
No Matches
myshell.h File Reference

The file containing the prototypes of the myshell functions. More...

#include "mymemory.h"
#include <termios.h>

Go to the source code of this file.

Data Structures

struct  alias_s
 
struct  variable_s
 
struct  input_command_s
 
struct  mysh_s
 
struct  builtin_s
 

Macros

#define MYSHELL_H_
 
#define CTRL_KEYPRESS(k)   ((k) & 0x1f)
 
#define CTRL_D   4
 
#define CTRL_C   3
 
#define IS_ATTY_PRINT(str)   if (isatty(0)) my_putstr(str)
 

Typedefs

typedef struct alias_s alias_t
 
typedef struct variable_s variable_t
 
typedef struct input_command_s input_command_t
 
typedef struct mysh_s mysh_t
 
typedef struct builtin_s builtin_t
 

Functions

mysh_tget_mysh (void)
 Get the shell structure.
 
mysh_tinit_shell (char **env)
 Initialize the shell structure.
 
void shell (char **env)
 The shell loop.
 
int set_new_env_var (mysh_t *mysh, char *var, char *value)
 Set a new environment variable.
 
char * get_env_var (char **env, char *var)
 Get an environment variable.
 
void replace_env_var (char **env, char *name, char *value)
 Replace an environment variable.
 
void check_path (mysh_t *mysh)
 Check if the PATH is set in the environment and set it if not.
 
void update_path_list (mysh_t *mysh)
 Update the path list.
 
void check_tty (void)
 Check if the shell is a tty and display the prompt if it is.
 
void set_title (void)
 Set the title of the shell.
 
void display_prompt (void)
 Display the prompt.
 
void is_git_repository (void)
 Check if we are in a git repository and display the branch.
 
char * check_command_exist (mysh_t *mysh, char *command)
 Check if the command exists.
 
void command (mysh_t *mysh, input_command_t *input)
 Execute the command.
 
builtin_tget_builtin_command (int index)
 Get the builtin command.
 
int exec_about (mysh_t *mysh)
 The about builtin.
 
int exec_alias (mysh_t *mysh)
 The alias builtin.
 
int exec_cd (mysh_t *mysh)
 The cd builtin.
 
int exec_command (mysh_t *mysh)
 The exec command builtin.
 
int exec_echo (mysh_t *mysh)
 The echo builtin.
 
int exec_else (mysh_t *mysh)
 The else builtin.
 
int exec_else_if (mysh_t *mysh, char **line_content)
 The else if builtin.
 
int exec_end (mysh_t *mysh)
 The end builtin.
 
int exec_endif (mysh_t *mysh)
 The endif builtin.
 
int exec_env (mysh_t *mysh)
 The env builtin.
 
int exec_exit (mysh_t *mysh)
 The exit builtin.
 
int exec_foreach (mysh_t *mysh)
 The foreach builtin.
 
int exec_help (mysh_t *mysh)
 The help builtin.
 
int exec_history (mysh_t *mysh)
 The history builtin.
 
int exec_if (mysh_t *mysh)
 The if builtin.
 
int exec_repeat (mysh_t *mysh)
 The source builtin.
 
int exec_set (mysh_t *mysh)
 The set builtin.
 
int exec_setenv (mysh_t *mysh)
 The setenv builtin.
 
int exec_source (mysh_t *mysh)
 The source builtin.
 
int exec_unalias (mysh_t *mysh)
 The unalias builtin.
 
int exec_unset (mysh_t *mysh)
 The unset builtin.
 
int exec_unsetenv (mysh_t *mysh)
 The unsetenv builtin.
 
int exec_where (mysh_t *mysh)
 The where builtin.
 
int exec_which (mysh_t *mysh)
 The which builtin.
 
void * free_str_and_tab (char *str, char **tab)
 Free an string and his array.
 
void free_input_list (mysh_t *mysh)
 Free the input list.
 
void my_exit (mysh_t *mysh, unsigned char status, char const *message)
 Free the shell structure and exit the shell.
 
void status_handler (int status)
 Display the signal returned by executing a command.
 
void print_line (int signal)
 Display the prompt when the SIGINT signal is received.
 
void print_exit (int signal)
 Display "exit" when the a CRTL+D is received.
 
int check_tilde (mysh_t *mysh)
 Transform the tilde in the command line by the home path.
 
char * get_redirection_path (char *line)
 Browse the command line to get the redirection path.
 
int missing_name (mysh_t *mysh, char **commands)
 Display the error message when the name is missing.
 
void analyse_backticks (mysh_t *mysh, char *line)
 Analyse and execute backticks commands.
 
void analyse_multi_commands (mysh_t *mysh, char *line)
 Analyse and execute multiple commands.
 
void analyse_operators (mysh_t *mysh, char *line)
 Analyse the operators and execute the commands.
 
void analyse_pipes (mysh_t *mysh, char *line)
 Analyse and execute the pipes.
 
void exec_right_redirection (mysh_t *mysh, input_command_t *input)
 Execute the right redirection.
 
void exec_left_double_redirection (mysh_t *mysh, input_command_t *input)
 Execute the left double redirection.
 
void exec_left_simple_redirection (mysh_t *mysh, input_command_t *input)
 Execute the left simple redirection.
 
void select_redirections (mysh_t *mysh, input_command_t *input)
 Analyse and execute the good redirection.
 
void analyse_parentheses (mysh_t *mysh, input_command_t *input)
 Analyse the parentheses.
 
input_command_tget_input_command (char *command)
 Get the input command.
 
void display_input_command (input_command_t *input_command)
 Display the input command.
 
char char_is_inhibited (char *str, int index)
 Check if a character is inhibited.
 
int char_is_paranthesed (char *str, int index)
 Check if a char is paranthesed.
 
int char_is_protected (char *str, int index)
 Check if a char is protected.
 
char * find_valid_str (char *str, char const *to_find)
 Find a valid string in a string depending on inhibitors and parentheses.
 
char ** str_to_array_inhibitors (char *str)
 Transform a string into an array of words with inhibitors.
 
char ** array_string (char const *str, char const *separator)
 Transform a string into an array of words delimited by a string with inhibitors.
 
char ** array_separators (char const *str, char const *separator)
 Transform a string into an array of words delimited by separators with inhibitors.
 
char ** globbing (char **args)
 Analyse if arguments are globbing.
 
int my_getline (char **line, FILE *stream)
 Get the line from the stream.
 
void save_termios (struct termios *saved_termios)
 Save terminal attributes.
 
void restore_termios (struct termios *saved_termios)
 Restore terminal attributes.
 
int disable_buffer (void)
 Disable the buffer.
 
void move_cursor (int *pos, char *str, char **str2)
 Move the cursor.
 
int ctrl (char c, int *pos, char *str, char **str2)
 Handle the control key.
 
int backspace (char c, int *pos, char *str, char **str2)
 Handle the backspace key.
 
int execute_bash_file (mysh_t *mysh, int file, size_t size)
 Execute a bash file.
 
void check_42shrc (mysh_t *mysh)
 Check if the 42shrc file exists in the home directory and create it if it doesn't.
 
void create_history (mysh_t *mysh)
 Create the history file.
 
void set_command_in_history (mysh_t *mysh, char *line)
 Set the command in the history.
 
char ** replace_history (char **args)
 Replace '!' by history event.
 
char ** replace_alias_in_line (char **args, node_t **alias_list)
 Replace the alias if it already exists.
 
void my_sort_alias (node_t **begin, int(*cmp)())
 sort the alias list
 
int is_valid_variable (char *variable, char *builtin)
 Check if the variable is valid.
 
void add_variable (mysh_t *mysh, char *name, char *value)
 Add a variable to the list.
 
char * get_variable_value (mysh_t *mysh, char *name)
 Get the value of a variable.
 
int replace_variables (mysh_t *mysh)
 Replace the variables in the command arguments.
 
void remove_comments (char *line)
 Remove the comments from a line.
 
int read_file_in_stdin (char *file)
 Read a file and execute the commands in it.
 
int get_condition (char *str)
 Get the condition result from a string.
 

Detailed Description

The file containing the prototypes of the myshell functions.

Function Documentation

◆ add_variable()

void add_variable ( mysh_t * mysh,
char * name,
char * value )

Add a variable to the list.

Parameters
myshThe shell structure
nameThe name of the variable
valueThe value of the variable
Returns
void

◆ analyse_backticks()

void analyse_backticks ( mysh_t * mysh,
char * line )

Analyse and execute backticks commands.

Parameters
myshThe shell structure
lineThe command line
Returns
void

◆ analyse_multi_commands()

void analyse_multi_commands ( mysh_t * mysh,
char * line )

Analyse and execute multiple commands.

Parameters
myshThe shell structure
lineThe command line
Returns
void

◆ analyse_operators()

void analyse_operators ( mysh_t * mysh,
char * line )

Analyse the operators and execute the commands.

Parameters
myshThe shell structure
lineThe command line
Returns
void

◆ analyse_parentheses()

void analyse_parentheses ( mysh_t * mysh,
input_command_t * input )

Analyse the parentheses.

Parameters
myshThe shell structure
inputThe input command structure
Returns
void

◆ analyse_pipes()

void analyse_pipes ( mysh_t * mysh,
char * line )

Analyse and execute the pipes.

Parameters
myshThe shell structure
lineThe command line
Returns
void

◆ array_separators()

char ** array_separators ( char const * str,
char const * separator )

Transform a string into an array of words delimited by separators with inhibitors.

Parameters
strThe string to transform
separatorThe separator
Returns
char ** The array of words

◆ array_string()

char ** array_string ( char const * str,
char const * separator )

Transform a string into an array of words delimited by a string with inhibitors.

Parameters
strThe string to transform
separatorThe separator
Returns
char ** The array of words

◆ backspace()

int backspace ( char c,
int * pos,
char * str,
char ** str2 )

Handle the backspace key.

Parameters
cThe character
posThe position of the cursor
strThe string
str2The second string
Returns
int 0 if success, 1 otherwise

◆ char_is_inhibited()

char char_is_inhibited ( char * str,
int index )

Check if a character is inhibited.

Parameters
strThe string to check
indexThe index of the character
Returns
char The inhibitor if the character is inhibited, 0 otherwise

◆ char_is_paranthesed()

int char_is_paranthesed ( char * str,
int index )

Check if a char is paranthesed.

Parameters
strThe string
indexThe index
Returns
int 1 if the char is paranthesed, 0 otherwise

◆ char_is_protected()

int char_is_protected ( char * str,
int index )

Check if a char is protected.

Parameters
strThe string
indexThe index
Returns
int 1 if the char is protected, 0 otherwise

◆ check_42shrc()

void check_42shrc ( mysh_t * mysh)

Check if the 42shrc file exists in the home directory and create it if it doesn't.

Parameters
myshThe shell structure
Returns
void

◆ check_command_exist()

char * check_command_exist ( mysh_t * mysh,
char * command )

Check if the command exists.

Parameters
myshThe shell structure
commandThe command
Returns
char * The path of the command if it exists, NULL otherwise

◆ check_path()

void check_path ( mysh_t * mysh)

Check if the PATH is set in the environment and set it if not.

Parameters
myshThe shell structure
Returns
void

◆ check_tilde()

int check_tilde ( mysh_t * mysh)

Transform the tilde in the command line by the home path.

Parameters
myshThe shell structure
Returns
int 0 if the replacement is successful, 1 otherwise

◆ check_tty()

void check_tty ( void )

Check if the shell is a tty and display the prompt if it is.

Returns
void

◆ command()

void command ( mysh_t * mysh,
input_command_t * input )

Execute the command.

Parameters
myshThe shell structure
inputThe input command
Returns
void

◆ create_history()

void create_history ( mysh_t * mysh)

Create the history file.

Parameters
myshThe shell structure
Returns
void

◆ ctrl()

int ctrl ( char c,
int * pos,
char * str,
char ** str2 )

Handle the control key.

Parameters
cThe character
posThe position of the cursor
strThe string
str2The second string
Returns
int 0 if success, 1 otherwise

◆ disable_buffer()

int disable_buffer ( void )

Disable the buffer.

Note
We'll need to disable the buffer when we want to my_getline
Returns
int 0 if the buffer is disabled, -1 otherwise

◆ display_input_command()

void display_input_command ( input_command_t * input_command)

Display the input command.

Note
The function is used for debugging
Parameters
input_commandThe input command
Returns
void

◆ display_prompt()

void display_prompt ( void )

Display the prompt.

Returns
void

◆ exec_about()

int exec_about ( mysh_t * mysh)

The about builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_alias()

int exec_alias ( mysh_t * mysh)

The alias builtin.

Parameters
myshThe shell structure
Returns
int Always 0

◆ exec_cd()

int exec_cd ( mysh_t * mysh)

The cd builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_command()

int exec_command ( mysh_t * mysh)

The exec command builtin.

Parameters
myshThe shell structure
Returns
int 0 or the exit value of the command if the command succeed, 1 otherwise

The exec command builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_echo()

int exec_echo ( mysh_t * mysh)

The echo builtin.

Parameters
myshThe shell structure
Returns
int The exit status of the echo command

◆ exec_else()

int exec_else ( mysh_t * mysh)

The else builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_else_if()

int exec_else_if ( mysh_t * mysh,
char ** line_content )

The else if builtin.

Parameters
myshThe shell structure
line_contentThe command arguments
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_end()

int exec_end ( mysh_t * mysh)

The end builtin.

Parameters
myshThe shell structure
Returns
int Always 1

◆ exec_endif()

int exec_endif ( mysh_t * mysh)

The endif builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_env()

int exec_env ( mysh_t * mysh)

The env builtin.

Note
If there is no argument, it will display the environment, else it will execute the command with the environment
Parameters
myshThe shell structure
Returns
int 0 if the command succeed, return the exit value of the env command if the command failed

◆ exec_exit()

int exec_exit ( mysh_t * mysh)

The exit builtin.

Parameters
myshThe shell structure
Returns
int 0 or the exit value if the command succeed, 1 otherwise

◆ exec_foreach()

int exec_foreach ( mysh_t * mysh)

The foreach builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_help()

int exec_help ( mysh_t * mysh)

The help builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_history()

int exec_history ( mysh_t * mysh)

The history builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_if()

int exec_if ( mysh_t * mysh)

The if builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_left_double_redirection()

void exec_left_double_redirection ( mysh_t * mysh,
input_command_t * input )

Execute the left double redirection.

Parameters
myshThe shell structure
inputThe input command
Returns
void

◆ exec_left_simple_redirection()

void exec_left_simple_redirection ( mysh_t * mysh,
input_command_t * input )

Execute the left simple redirection.

Parameters
myshThe shell structure
inputThe input command
Returns
void

◆ exec_repeat()

int exec_repeat ( mysh_t * mysh)

The source builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

The source builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_right_redirection()

void exec_right_redirection ( mysh_t * mysh,
input_command_t * input )

Execute the right redirection.

Parameters
myshThe shell structure
inputThe input command
Returns
void

◆ exec_set()

int exec_set ( mysh_t * mysh)

The set builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_setenv()

int exec_setenv ( mysh_t * mysh)

The setenv builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_source()

int exec_source ( mysh_t * mysh)

The source builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_unalias()

int exec_unalias ( mysh_t * mysh)

The unalias builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_unset()

int exec_unset ( mysh_t * mysh)

The unset builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_unsetenv()

int exec_unsetenv ( mysh_t * mysh)

The unsetenv builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_where()

int exec_where ( mysh_t * mysh)

The where builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ exec_which()

int exec_which ( mysh_t * mysh)

The which builtin.

Parameters
myshThe shell structure
Returns
int 0 if the command succeed, 1 otherwise

◆ execute_bash_file()

int execute_bash_file ( mysh_t * mysh,
int file,
size_t size )

Execute a bash file.

Parameters
myshThe shell structure
fileThe file descriptor
sizeThe size of the file
Returns
int The exit status of last command if the command succeed, 1 otherwise

◆ find_valid_str()

char * find_valid_str ( char * str,
char const * to_find )

Find a valid string in a string depending on inhibitors and parentheses.

Note
It's an improved version of my_strstr
Parameters
strThe string to check
to_findThe string to find
Returns
char * The address of the string if it's valid, NULL otherwise

◆ free_input_list()

void free_input_list ( mysh_t * mysh)

Free the input list.

Parameters
myshThe shell structure
Returns
void

◆ free_str_and_tab()

void * free_str_and_tab ( char * str,
char ** tab )

Free an string and his array.

Parameters
strThe string to free
tabThe array of the string to free
Returns
void * Always NULL

◆ get_builtin_command()

builtin_t * get_builtin_command ( int index)

Get the builtin command.

Parameters
indexThe index of the command
Returns
builtin_t * The builtin command selected

◆ get_condition()

int get_condition ( char * str)

Get the condition result from a string.

Parameters
strThe string to calculate
Returns
int The result of the condition : 0 if false, 1 if true and -1 if an error occurred

◆ get_env_var()

char * get_env_var ( char ** env,
char * var )

Get an environment variable.

Parameters
envThe environment
varThe variable to get
Returns
char * The value of the variable

◆ get_input_command()

input_command_t * get_input_command ( char * command)

Get the input command.

Parameters
commandThe command
Returns
input_command_t * The input command

◆ get_mysh()

mysh_t * get_mysh ( void )

Get the shell structure.

Returns
mysh_t * The shell structure

◆ get_redirection_path()

char * get_redirection_path ( char * line)

Browse the command line to get the redirection path.

Parameters
lineThe command line
Returns
char * The redirection path

◆ get_variable_value()

char * get_variable_value ( mysh_t * mysh,
char * name )

Get the value of a variable.

Parameters
myshThe shell structure
nameThe name of the variable
Returns
char * The value of the variable

◆ globbing()

char ** globbing ( char ** args)

Analyse if arguments are globbing.

Parameters
argsThe arguments
Returns
New Args

Analyse if arguments are globbing.

Parameters
argsThe command arguments
Returns
char ** The new command arguments
Note
Pushing each argument in a list and then converting it to an array

◆ init_shell()

mysh_t * init_shell ( char ** env)

Initialize the shell structure.

Parameters
envThe environment variables
Returns
mysh_t * The shell structure
Parameters
environThe environment variables
Returns
mysh_t * The shell structure

◆ is_git_repository()

void is_git_repository ( void )

Check if we are in a git repository and display the branch.

Returns
void

◆ is_valid_variable()

int is_valid_variable ( char * variable,
char * builtin )

Check if the variable is valid.

Parameters
variableThe variable
builtinThe builtin who called the function
Returns
int 1 if the variable is valid, 0 otherwise

◆ missing_name()

int missing_name ( mysh_t * mysh,
char ** commands )

Display the error message when the name is missing.

Parameters
myshThe shell structure
commandsThe command line
Returns
int Always 1

◆ move_cursor()

void move_cursor ( int * pos,
char * str,
char ** str2 )

Move the cursor.

Parameters
streamThe stream
posThe position of the cursor
strThe string
str2The second string
Returns
void

◆ my_exit()

void my_exit ( mysh_t * mysh,
unsigned char status,
char const * message )

Free the shell structure and exit the shell.

Parameters
myshThe shell structure
statusThe exit status
messageThe message to display
Returns
void

◆ my_getline()

int my_getline ( char ** line,
FILE * stream )

Get the line from the stream.

Parameters
lineThe line to store the command
streamThe stream
Returns
int 0 if success, -1 if error

◆ my_sort_alias()

void my_sort_alias ( node_t ** begin,
int(* cmp )() )

sort the alias list

Parameters
beginThe alias list
cmpThe compare function
Returns
void

◆ print_exit()

void print_exit ( int signal)

Display "exit" when the a CRTL+D is received.

Note
I disabled it because it didn't work
Parameters
signalThe signal
Returns
void

◆ print_line()

void print_line ( int signal)

Display the prompt when the SIGINT signal is received.

Parameters
signalThe signal
Returns
void

◆ read_file_in_stdin()

int read_file_in_stdin ( char * file)

Read a file and execute the commands in it.

Parameters
fileThe file path
Returns
int 0 if success, 1 if an error occurred

◆ remove_comments()

void remove_comments ( char * line)

Remove the comments from a line.

Parameters
lineThe line
Returns
void

◆ replace_alias_in_line()

char ** replace_alias_in_line ( char ** args,
node_t ** alias_list )

Replace the alias if it already exists.

Parameters
myshThe shell structure
Returns
char ** args to be interpreted and executed

◆ replace_env_var()

void replace_env_var ( char ** env,
char * name,
char * value )

Replace an environment variable.

Parameters
envThe environment
nameThe name of the variable
valueThe new value
Returns
void

◆ replace_history()

char ** replace_history ( char ** args)

Replace '!' by history event.

Parameters
argsThe command arguments
Returns
char ** The new command arguments

◆ replace_variables()

int replace_variables ( mysh_t * mysh)

Replace the variables in the command arguments.

Parameters
myshThe shell structure
Returns
int/b> The new command arguments

◆ restore_termios()

void restore_termios ( struct termios * saved_termios)

Restore terminal attributes.

Parameters
saved_termiosThe termios structure to restore the terminal attributes
Returns
void

◆ save_termios()

void save_termios ( struct termios * saved_termios)

Save terminal attributes.

Parameters
saved_termiosThe termios structure to save the terminal attributes
Returns
void

◆ select_redirections()

void select_redirections ( mysh_t * mysh,
input_command_t * input )

Analyse and execute the good redirection.

Parameters
myshThe shell structure
inputThe input command
Returns
void

◆ set_command_in_history()

void set_command_in_history ( mysh_t * mysh,
char * line )

Set the command in the history.

Parameters
myshThe shell structure
lineThe line to set in the history
Returns
void

◆ set_new_env_var()

int set_new_env_var ( mysh_t * mysh,
char * var,
char * value )

Set a new environment variable.

Parameters
myshThe shell structure
varThe variable name
valueThe variable value
Returns
int 0 if the command succeed, 1 otherwise
Parameters
myshThe shell structure
varThe variable name
valueThe variable value
Returns
int 0 if the command succeed, 1 otherwise

◆ set_title()

void set_title ( void )

Set the title of the shell.

Returns
void

◆ shell()

void shell ( char ** env)

The shell loop.

Parameters
envThe environment variables
Returns
void
Parameters
environThe environment variables
Returns
void

◆ status_handler()

void status_handler ( int status)

Display the signal returned by executing a command.

Parameters
signalThe signal
Returns
void

◆ str_to_array_inhibitors()

char ** str_to_array_inhibitors ( char * str)

Transform a string into an array of words with inhibitors.

Parameters
strThe string to transform
Returns
char ** The array of words

◆ update_path_list()

void update_path_list ( mysh_t * mysh)

Update the path list.

Parameters
myshThe shell structure
Returns
void