-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMiniShell.h
More file actions
283 lines (261 loc) · 9.26 KB
/
MiniShell.h
File metadata and controls
283 lines (261 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* MiniShell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ytouate <ytouate@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 13:03:19 by ytouate #+# #+# */
/* Updated: 2022/06/18 15:46:20 by ytouate ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# include "./libft/libft.h"
# include <stdio.h>
# include <sys/types.h>
# include <sys/uio.h>
# include <unistd.h>
# include <signal.h>
# include <stdlib.h>
# include <errno.h>
# include <curses.h>
# include <term.h>
# include <sys/wait.h>
# include <termios.h>
# include <sys/ioctl.h>
# include <dirent.h>
# include <fcntl.h>
# include <stdio.h>
# include <readline/readline.h>
# include <readline/history.h>
# include <string.h>
# include <sys/stat.h>
# include <limits.h>
# define PERMISSION_DENIED 126
# define COMMAND_NOT_FOUND 127
# define SUCCESS 0
# define CNTRL_C 130
# define CNTRL_BACKSLASH 131
# define SYNTAX_ERROR_EXIT 258
typedef enum type
{
T_WORD,
T_IN,
T_OUT,
T_HERDOC,
T_APPEND,
T_PIPE
} t_type;
typedef struct vars
{
pid_t pid;
int exit_code;
int signal_flag;
int sig_type;
}t_vars_g;
typedef struct TOKEN{
t_type token;
char *value;
struct TOKEN *next;
}t_token;
typedef struct t_token_head{
t_token *first_token;
}t_token_head;
typedef struct minishellpars{
char **flags;
struct minishellpars *next_command;
t_token_head *redi;
t_token_head *herdoc;
}t_command;
typedef struct head
{
int taille;
t_command *first_c;
}t_head_c;
typedef struct lexer
{
char *content;
char c;
size_t i;
}t_lexer;
typedef struct s_files
{
int in;
int trunc;
int append;
}t_files;
typedef struct s_path_vars{
char *path;
char *temp;
char **command_path;
int i;
}t_path_vars;
typedef struct s_cd_vars
{
char current_wd[PATH_MAX];
char buffer[PATH_MAX];
char *temp_path;
char **temp;
t_list *old_wd;
int i;
}t_cd_vars;
typedef struct s_vars
{
t_list *env_list;
t_list *export_list;
char **env;
t_head_c *head;
int num_of_commands;
t_command *command;
}t_vars;
typedef struct s_contex
{
int fd_in;
int fd_out;
int herdoc_fildes;
}t_contex;
typedef struct s_norm
{
t_list *first;
t_list *second;
t_list *temp;
char **cmd;
int *ids;
int i;
int id;
int size;
int temp_fd;
t_contex contex;
int fd[2];
}t_norm;
extern t_vars_g g_global_vars;
t_token *ft_init_token(int type, char *value);
t_lexer *ft_init_lexer(char *content);
t_token *ft_get_next_token(t_lexer *lexer, t_list *env_list);
t_head_c *ft_get_for_exec(char *content, t_list *env_list);
t_token *ft_red(t_lexer *lexer, t_list *env_list);
t_token *ft_her_app(t_lexer *lexer, t_list *env_list);
t_list *ft_getenv(t_list *env_list, char *var_name);
t_list *get_env_list(char **env);
t_contex open_files(t_token_head redi);
int fill_temp_stdin(t_command *command);
void exec_node(t_vars *vars, t_command *command, t_contex contex);
int check_built_in_commands(t_vars *vars,
t_command *command, t_contex contex);
void exec_node(t_vars *vars, t_command *command, t_contex contex);
char *join_for_echo(char **s, char flag);
void check_commands_order(t_vars *vars, t_norm *data);
void ft_free_all(t_head_c *head);
int heredoc_return(int outfile, t_contex contex);
void check_out_files(int *out_file, int *fd_out);
void walk_to_heredoc(t_command **command);
void close_pipe(int *fd);
void check_in_files(int *fd_in, int *temp_stdin);
void ft_add_red(t_token_head *head, t_token *t);
void ft_init_head(t_head_c *head);
void ft_add_node(t_head_c *head, t_command *commande);
void ft_advance(t_lexer *lexer);
void ft_skip_spaces(t_lexer *lexer);
void check_export_error(t_vars *vars, t_command *command);
void check_cd_errors(t_vars *vars, t_command *command);
void set_exit_code_inside_pipe(t_vars *vars, t_command *command);
void read_for_heredoc(t_command *command, int fd_in);
void open_heredoc(t_command **command);
void set_signal_flag(int num);
void free_list(t_list *list);
void ft_error(char *arg, char *msg, int exit_code);
void wait_for_child(int *ids, int i, int temp_fd);
void exec_commands_before_heredoc(t_vars *vars);
void check_cmd(t_command *command, t_vars *vars, t_contex contex);
void exec_node(t_vars *vars, t_command *command, t_contex contex);
void ft_execute(t_command *command, t_vars *vars, t_contex contex);
void ft_pipe(t_vars *vars);
void ft_env(t_vars vars, t_command *command, t_contex contex);
void free_2d_array(char **a);
void sig_handler(int sig);
void ft_pwd(t_vars vars, t_command *command, t_contex contex);
void sort_list(t_list **env_list);
void ft_unset(t_list **env_list, char *to_delete);
void ft_setenv(t_list **env_list, char *var_name, char *var_val);
void set_exit_code(int num);
void ft_redirect_output_append_mode(t_command *command, t_vars *vars);
void ft_redirect_output_trunc_mode(t_vars *vars, t_command *command);
void redirect_input(t_vars *vars, t_command *command);
void exec_last_node(t_vars *vars, t_norm data);
void exec_first_node(t_vars *vars, t_norm data);
void exec_other_node(t_vars *vars, t_norm data);
void ft_export(t_command *command, t_list *env, char *arg);
void exec_first_command_before_heredoc(t_vars *vars, t_norm data);
void exec_last_command_before_heredoc(t_vars *vars, t_norm data);
void exec_other_command_before_heredoc(t_vars *vars, t_norm data);
void add_properly_named_word(t_command *command, t_vars *vars, int i);
void show_export_error(int *flag, int i, t_command *command);
void ft_exit(char *arg, char flag);
void add_existed_variable(t_command *command, t_vars *vars,
int i, char **temp);
void add_non_variable(t_command *command,
t_vars *vars, char **temp, int i);
void add_unexisted_variable(t_command *command, t_vars *vars,
char **temp, int i);
void exec_command(t_command *command, t_vars *vars,
t_contex contex, char *command_path);
void set_exit_code_inside_pipe(t_vars *vars, t_command *command);
void init_contex(t_contex *contex);
void show_export_list(t_command *command, t_vars vars, t_contex contex);
void cd_oldwd(t_list *env_list, t_list *export_list);
void cd_home(t_list *env_list, t_list *export_list);
void ft_cd(char *path, t_list *env_list, t_list *export_list);
int check_built_in_commands(t_vars *vars,
t_command *command, t_contex contex);
int ft_add_commande(t_head_c *head, t_lexer *lexer, t_list *env_list);
int ft_strcmp(char *s, char *str);
int get_len(t_command *command);
int cut_exit_code(char *arg);
int get_exit_code(void);
int get_parts(char *s, char c);
int is_variable(char *s);
int ft_check_after_dollar(t_lexer *lexer);
int check_echo_flag(char *s);
int is_properly_named(char *s);
int ft_heredoc(t_vars *vars, t_command *command, t_contex contex);
int count_commands_before_heredoc(t_command *command);
int get_signal_flag(void);
int ft_syntax(char *value, t_token *t, t_head_c *head);
int ft_rederictions(t_command *re, t_token *token, t_head_c *head);
int ft_check_pipe(t_lexer *lexer, t_token *token,
int k, t_head_c *head);
int ft_check_token(t_token *token, t_command *re,
int *i, t_head_c *head);
int ft_fill_node(t_command *re, t_lexer *lexer,
t_list *env_list, t_head_c *head);
bool run_pwd(t_vars vars, t_command *command, t_contex contex);
char *get_promt(void);
char *get_path(t_list *env_list, char *cmd);
char *ft_get_env_val(t_list *env_list, char *var_name);
char *ft_help_collect_str(t_lexer *lexer, \
t_list *env_list, char c, int h);
char *ft_join_and_clean(char *str, char *s);
char *ft_str_for_join(t_lexer *lexer, t_list *env_list, int h);
char *ft_after_dollar(t_lexer *lexer, t_list *env_list);
char *ft_collect_string(t_lexer *lexer, char c, t_list *env_list, int h);
char *ft_get_value(t_lexer *lexer, t_list *env_list, int h);
char *ft_get_str(t_lexer *lexer, t_list *env_list, int h);
char *ft_get_str_without_quote(t_lexer *lexer, t_list *env_list, int h);
char **ft_replace(char **av, int i, char *value);
char *join_for_echo(char **s, char flag);
char *check_for_space(char **s, char *result, int i);
char *join_for_echo(char **s, char flag);
bool is_number(char *s);
bool exec_echo(t_vars vars, t_command *command, t_contex contex);
bool add_variable(t_command *command, t_vars *vars, char **temp, int i);
bool check_redirection(t_vars *vars, t_command *command);
bool heredoc_outside_pipe(t_vars *vars, t_command *command);
bool run_exit(t_vars vars, t_command *command);
bool run_cd(t_vars vars, t_command *command);
bool run_env(t_vars vars, t_command *command, t_contex contex);
bool run_unset(t_vars *vars, t_command *command);
void ft_echo(t_command *command, char *s, char flag, t_contex contex);
bool run_export(t_command *command, t_vars *vars, t_contex contex);
void show_export_list(t_command *command, t_vars vars, t_contex contex);
#endif