42sh 1.0.0
Create a shell in C
Loading...
Searching...
No Matches
myshell.h
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** 42sh
4** File description:
5** The file containing the prototypes of the myshell functions
6*/
12#include "mymemory.h"
13#include <termios.h>
14
15#ifndef MYSHELL_H_
16 #define MYSHELL_H_
17
18 #define CTRL_KEYPRESS(k) ((k) & 0x1f)
19 #define CTRL_D 4
20 #define CTRL_C 3
21
22 #define IS_ATTY_PRINT(str) if (isatty(0)) my_putstr(str)
23
24// TYPEDEFS :
25
26/* The structure of an alias */
27typedef struct alias_s {
28 char *name; /* The name of the alias */
29 char *value; /* The value of the alias */
30 int is_concated;
31} alias_t;
32
33/* The structure of a variable */
34typedef struct variable_s {
35 char *name; /* The name of the variable */
36 char *value; /* The value of the variable */
38
39/* The structure of the input command */
40typedef struct input_command_s {
41 char **args; /* The details of the command */
42 char *command; /* The command */
43 char *left; /* The left redirection */
44 char *right; /* The right redirection */
45 int left_type; /* The left redirection type */
46 int right_type; /* The right redirection type */
47 char *current; /* The current command */
49
50/* The structure containing the shell variables */
51typedef struct mysh_s {
52 int exit_status; /* The exit status of the shell */
53 int config_file; /* The fd of the configuration file (.42shrc) */
54 char **env; /* The environment variables */
55 char **path_list; /* The list of path for exec */
56 char **args; /* The arguments of the command */
57 char *old_pwd; /* The old pwd */
58 char *line; /* The command line */
59 int saved_stdin; /* The saved stdin */
60 int saved_stdout; /* The saved stdout */
61 char **multi_cmds; /* The multiple commands */
62 node_t *operators_cmds; /* The operators commands */
63 node_t *operators_list; /* The list of operators */
64 char **pipe_cmds; /* The pipe commands */
65 input_command_t **input_list; /* The input list */
66 FILE *fd_history; /* The file descriptor for the history */
67 char *history_path; /* The path of the history file */
68 node_t *history; /* The history list */
69 node_t *alias_list; /* The alias list */
70 node_t *variable_list; /* The variable list */
71} mysh_t;
72
73/* The structure of a builtin command */
74typedef struct builtin_s {
75 char *name; /* The name of the builtin command */
76 int (*builtin_function)(mysh_t *mysh); /* The functions of the command */
77} builtin_t;
78
79
80
81// FUNCTIONS PROTOTYPES :
82
83 // Shell functions :
84
89mysh_t *get_mysh(void);
90
96mysh_t *init_shell(char **env);
97
103void shell(char **env);
104
105
106
107 // Environment functions
108
116int set_new_env_var(mysh_t *mysh, char *var, char *value);
117
124char *get_env_var(char **env, char *var);
125
133void replace_env_var(char **env, char *name, char *value);
134
140void check_path(mysh_t *mysh);
141
147void update_path_list(mysh_t *mysh);
148
149
150
151 // Prompt functions
152
157void check_tty(void);
158
163void set_title(void);
164
170void display_prompt(void);
171
176void is_git_repository(void);
177
178
179
180 // Commands functions
181
189char *check_command_exist(mysh_t *mysh, char *command);
190
197void command(mysh_t *mysh, input_command_t *input);
198
199
200
201
202 // Builtins functions
203
210
216int exec_about(mysh_t *mysh);
217
223int exec_alias(mysh_t *mysh);
224
230int exec_cd(mysh_t *mysh);
231
238int exec_command(mysh_t *mysh);
239
245int exec_echo(mysh_t *mysh);
246
252int exec_else(mysh_t *mysh);
253
260int exec_else_if(mysh_t *mysh, char **line_content);
261
267int exec_end(mysh_t *mysh);
268
274int exec_endif(mysh_t *mysh);
275
284int exec_env(mysh_t *mysh);
285
292int exec_exit(mysh_t *mysh);
293
299int exec_foreach(mysh_t *mysh);
300
306int exec_help(mysh_t *mysh);
307
313int exec_history(mysh_t *mysh);
314
320int exec_if(mysh_t *mysh);
321
327int exec_repeat(mysh_t *mysh);
328
334int exec_set(mysh_t *mysh);
335
341int exec_setenv(mysh_t *mysh);
342
348int exec_source(mysh_t *mysh);
349
355int exec_unalias(mysh_t *mysh);
356
362int exec_unset(mysh_t *mysh);
363
369int exec_unsetenv(mysh_t *mysh);
370
376int exec_where(mysh_t *mysh);
377
383int exec_which(mysh_t *mysh);
384
385
386
387 // Exit functions
388
395void *free_str_and_tab(char *str, char **tab);
396
402void free_input_list(mysh_t *mysh);
403
411void my_exit(mysh_t *mysh, unsigned char status, char const *message);
412
413
414
415 // Signals functions
416
422void status_handler(int status);
423
429void print_line(int signal);
430
437void print_exit(int signal);
438
439
440
441 // Tilde functions
442
449int check_tilde(mysh_t *mysh);
450
451
452
453 // Redirections functions
454
460char *get_redirection_path(char *line);
461
468int missing_name(mysh_t *mysh, char **commands);
469
470
471
472 // Analyse and select functions
473
480void analyse_backticks(mysh_t *mysh, char *line);
481
488void analyse_multi_commands(mysh_t *mysh, char *line);
489
496void analyse_operators(mysh_t *mysh, char *line);
497
504void analyse_pipes(mysh_t *mysh, char *line);
505
513
521
529
536void select_redirections(mysh_t *mysh, input_command_t *input);
537
544void analyse_parentheses(mysh_t *mysh, input_command_t *input);
545
546
547
548 // Input command functions
549
556
563void display_input_command(input_command_t *input_command);
564
565
566
567 // Parsing functions
568
576char char_is_inhibited(char *str, int index);
577
584int char_is_paranthesed(char *str, int index);
585
592int char_is_protected(char *str, int index);
593
603char *find_valid_str(char *str, char const *to_find);
604
605
606
607 // Inhibitors functions
608
614char **str_to_array_inhibitors(char *str);
615
623char **array_string(char const *str, char const *separator);
624
632char **array_separators(char const *str, char const *separator);
633
634
635
636 // Globbing functions
637
643char **globbing(char **args);
644
645
646
647 // Line editing functions
648
655int my_getline(char **line, FILE *stream);
656
662void save_termios(struct termios *saved_termios);
663
670void restore_termios(struct termios *saved_termios);
671
677int disable_buffer(void);
678
687void move_cursor(int *pos, char *str, char **str2);
688
697int ctrl(char c, int *pos, char *str, char **str2);
698
707int backspace(char c, int *pos, char *str, char **str2);
708
709
710
711 // .42shrc functions
712
721int execute_bash_file(mysh_t *mysh, int file, size_t size);
722
729void check_42shrc(mysh_t *mysh);
730
731
732
733 // History functions
734
740void create_history(mysh_t *mysh);
741
748void set_command_in_history(mysh_t *mysh, char *line);
749
755char **replace_history(char **args);
756
757
758
759 // Alias functions
760
766char **replace_alias_in_line(char **args, node_t **alias_list);
767
774void my_sort_alias(node_t **begin, int (*cmp)());
775
776
777
778 // Variables functions
779
786int is_valid_variable(char *variable, char *builtin);
787
795void add_variable(mysh_t *mysh, char *name, char *value);
796
803char *get_variable_value(mysh_t *mysh, char *name);
804
810int replace_variables(mysh_t *mysh);
811
812
813
814 // Scripting functions
815
821void remove_comments(char *line);
822
828int read_file_in_stdin(char *file);
829
830
831
832 // Condition functions
833
840int get_condition(char *str);
841
842#endif /* MYSHELL_H_ */
The header file of the libmymemory.
int my_getline(char **line, FILE *stream)
Get the line from the stream.
Definition my_getline.c:115
int exec_alias(mysh_t *mysh)
The alias builtin.
Definition alias.c:142
void * free_str_and_tab(char *str, char **tab)
Free an string and his array.
Definition exit.c:20
char ** replace_alias_in_line(char **args, node_t **alias_list)
Replace the alias if it already exists.
Definition replace_alias.c:100
void analyse_pipes(mysh_t *mysh, char *line)
Analyse and execute the pipes.
Definition pipes.c:153
void check_42shrc(mysh_t *mysh)
Check if the 42shrc file exists in the home directory and create it if it doesn't.
Definition config_file.c:38
void is_git_repository(void)
Check if we are in a git repository and display the branch.
Definition git_repository.c:57
void add_variable(mysh_t *mysh, char *name, char *value)
Add a variable to the list.
Definition set.c:100
void shell(char **env)
The shell loop.
Definition shell.c:58
int execute_bash_file(mysh_t *mysh, int file, size_t size)
Execute a bash file.
Definition source.c:22
int exec_env(mysh_t *mysh)
The env builtin.
Definition env.c:22
char * check_command_exist(mysh_t *mysh, char *command)
Check if the command exists.
Definition command.c:97
void print_line(int signal)
Display the prompt when the SIGINT signal is received.
Definition signals.c:38
builtin_t * get_builtin_command(int index)
Get the builtin command.
Definition command.c:19
void replace_env_var(char **env, char *name, char *value)
Replace an environment variable.
Definition environnement.c:52
int exec_if(mysh_t *mysh)
The if builtin.
Definition if.c:134
void analyse_multi_commands(mysh_t *mysh, char *line)
Analyse and execute multiple commands.
Definition multiple_commands.c:20
int exec_set(mysh_t *mysh)
The set builtin.
Definition set.c:143
int check_tilde(mysh_t *mysh)
Transform the tilde in the command line by the home path.
Definition tilde.c:47
int exec_foreach(mysh_t *mysh)
The foreach builtin.
Definition foreach.c:115
void check_path(mysh_t *mysh)
Check if the PATH is set in the environment and set it if not.
Definition environnement.c:68
void set_command_in_history(mysh_t *mysh, char *line)
Set the command in the history.
Definition history_file.c:20
int ctrl(char c, int *pos, char *str, char **str2)
Handle the control key.
Definition ansi.c:147
int replace_variables(mysh_t *mysh)
Replace the variables in the command arguments.
Definition replace_variable.c:181
void my_exit(mysh_t *mysh, unsigned char status, char const *message)
Free the shell structure and exit the shell.
Definition exit.c:78
int exec_unalias(mysh_t *mysh)
The unalias builtin.
Definition unalias.c:35
void display_input_command(input_command_t *input_command)
Display the input command.
Definition input_command.c:226
int exec_else_if(mysh_t *mysh, char **line_content)
The else if builtin.
Definition if.c:107
int exec_source(mysh_t *mysh)
The source builtin.
Definition source.c:49
void select_redirections(mysh_t *mysh, input_command_t *input)
Analyse and execute the good redirection.
Definition redirections.c:58
void remove_comments(char *line)
Remove the comments from a line.
Definition scripting.c:19
int exec_help(mysh_t *mysh)
The help builtin.
Definition help.c:127
void print_exit(int signal)
Display "exit" when the a CRTL+D is received.
Definition signals.c:50
void status_handler(int status)
Display the signal returned by executing a command.
Definition signals.c:19
int exec_where(mysh_t *mysh)
The where builtin.
Definition where.c:80
char ** array_string(char const *str, char const *separator)
Transform a string into an array of words delimited by a string with inhibitors.
Definition array_string.c:133
int char_is_paranthesed(char *str, int index)
Check if a char is paranthesed.
Definition parsing.c:50
char ** array_separators(char const *str, char const *separator)
Transform a string into an array of words delimited by separators with inhibitors.
Definition array_separators.c:130
mysh_t * init_shell(char **env)
Initialize the shell structure.
Definition shell.c:30
input_command_t * get_input_command(char *command)
Get the input command.
Definition input_command.c:199
int exec_exit(mysh_t *mysh)
The exit builtin.
Definition exit.c:108
void check_tty(void)
Check if the shell is a tty and display the prompt if it is.
Definition shell.c:45
int exec_repeat(mysh_t *mysh)
The source builtin.
Definition repeat.c:19
int exec_end(mysh_t *mysh)
The end builtin.
Definition end.c:19
char * get_env_var(char **env, char *var)
Get an environment variable.
Definition environnement.c:36
int exec_echo(mysh_t *mysh)
The echo builtin.
Definition echo.c:19
char * get_variable_value(mysh_t *mysh, char *name)
Get the value of a variable.
Definition set.c:172
int missing_name(mysh_t *mysh, char **commands)
Display the error message when the name is missing.
Definition redirections.c:44
void set_title(void)
Set the title of the shell.
Definition title.c:18
int exec_history(mysh_t *mysh)
The history builtin.
Definition history.c:60
char * get_redirection_path(char *line)
Browse the command line to get the redirection path.
Definition redirections.c:19
char ** str_to_array_inhibitors(char *str)
Transform a string into an array of words with inhibitors.
Definition inhibitors.c:198
void exec_left_double_redirection(mysh_t *mysh, input_command_t *input)
Execute the left double redirection.
Definition left_redirections.c:79
int exec_about(mysh_t *mysh)
The about builtin.
Definition about.c:94
void analyse_backticks(mysh_t *mysh, char *line)
Analyse and execute backticks commands.
Definition backticks.c:134
int is_valid_variable(char *variable, char *builtin)
Check if the variable is valid.
Definition parsing.c:107
mysh_t * get_mysh(void)
Get the shell structure.
Definition shell.c:18
int exec_cd(mysh_t *mysh)
The cd builtin.
Definition cd.c:75
int exec_endif(mysh_t *mysh)
The endif builtin.
Definition endif.c:19
void exec_right_redirection(mysh_t *mysh, input_command_t *input)
Execute the right redirection.
Definition right_redirections.c:44
int exec_which(mysh_t *mysh)
The which builtin.
Definition which.c:84
int disable_buffer(void)
Disable the buffer.
Definition termios.c:40
int exec_else(mysh_t *mysh)
The else builtin.
Definition else.c:19
int exec_setenv(mysh_t *mysh)
The setenv builtin.
Definition setenv.c:86
void command(mysh_t *mysh, input_command_t *input)
Execute the command.
Definition command.c:149
int exec_unset(mysh_t *mysh)
The unset builtin.
Definition unset.c:36
void display_prompt(void)
Display the prompt.
Definition prompt.c:105
void exec_left_simple_redirection(mysh_t *mysh, input_command_t *input)
Execute the left simple redirection.
Definition left_redirections.c:109
char ** replace_history(char **args)
Replace '!' by history event.
Definition event.c:76
int read_file_in_stdin(char *file)
Read a file and execute the commands in it.
Definition scripting.c:81
int backspace(char c, int *pos, char *str, char **str2)
Handle the backspace key.
Definition ansi.c:173
char char_is_inhibited(char *str, int index)
Check if a character is inhibited.
Definition parsing.c:21
void free_input_list(mysh_t *mysh)
Free the input list.
Definition exit.c:34
void restore_termios(struct termios *saved_termios)
Restore terminal attributes.
Definition termios.c:30
int get_condition(char *str)
Get the condition result from a string.
Definition condition.c:91
int char_is_protected(char *str, int index)
Check if a char is protected.
Definition parsing.c:73
void analyse_parentheses(mysh_t *mysh, input_command_t *input)
Analyse the parentheses.
Definition parentheses.c:172
int exec_unsetenv(mysh_t *mysh)
The unsetenv builtin.
Definition unsetenv.c:56
int set_new_env_var(mysh_t *mysh, char *var, char *value)
Set a new environment variable.
Definition setenv.c:59
void save_termios(struct termios *saved_termios)
Save terminal attributes.
Definition termios.c:19
void create_history(mysh_t *mysh)
Create the history file.
Definition history_file.c:44
void analyse_operators(mysh_t *mysh, char *line)
Analyse the operators and execute the commands.
Definition operators.c:106
char * find_valid_str(char *str, char const *to_find)
Find a valid string in a string depending on inhibitors and parentheses.
Definition parsing.c:89
void move_cursor(int *pos, char *str, char **str2)
Move the cursor.
Definition ansi.c:120
void my_sort_alias(node_t **begin, int(*cmp)())
sort the alias list
Definition replace_alias.c:27
char ** globbing(char **args)
Analyse if arguments are globbing.
Definition globbing.c:42
int exec_command(mysh_t *mysh)
The exec command builtin.
Definition exec.c:73
void update_path_list(mysh_t *mysh)
Update the path list.
Definition environnement.c:79
Definition myshell.h:27
Definition myshell.h:74
Definition myshell.h:40
Definition myshell.h:51
Definition mylist.h:43
Definition myshell.h:34