-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminishell.h
More file actions
241 lines (211 loc) · 6.56 KB
/
minishell.h
File metadata and controls
241 lines (211 loc) · 6.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgrisost <fgrisost@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/30 14:10:43 by fgrisost #+# #+# */
/* Updated: 2024/08/30 14:10:43 by fgrisost ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# include <stdbool.h>
# include <stdio.h>
# include <unistd.h>
# include <stdlib.h>
# include <signal.h>
# include <limits.h>
# include <fcntl.h>
# include <errno.h>
# include <sys/wait.h>
# include <sys/ioctl.h>
# include <readline/readline.h>
# include <readline/history.h>
# include "my_lib/Libft/libft.h"
# include "my_lib/ft_printf/ft_printf.h"
# include "my_lib/get_next_line/get_next_line_bonus.h"
# define PROMPT "\001\033[0;95m\002minicecco\001\033[0m\002:"
# define PROMPT_PIPE "\001\033[0;31m\002|\001\033[0m\002> "
# define PROMPT_SINGLE_QUOTE "\001\033[0;33m\002'\001\033[0m\002> "
# define PROMPT_DOUBLE_QUOTE "\001\033[0;38;5;214m\002\"\001\033[0m\002> "
extern int g_signal;
/* Executer */
typedef enum e_builtin
{
INTERNAL,
NONE,
ECHO,
CD,
PWD,
EXPORT,
UNSET,
ENV,
EXIT
} t_builtin;
typedef enum e_type
{
CMD,
TRUNC,
APPEND,
INPUT,
HEREDOC,
PIPE,
} t_type;
typedef struct s_input
{
t_type type;
char *str;
int fd;
struct s_input *next;
} t_input;
typedef struct s_cmd
{
char *path;
char **argv;
char **env;
int fd_in;
int fd_out;
} t_cmd;
/* Parsing */
typedef struct s_token_ctx
{
char *token;
t_type current_type;
char *cmd_str;
char *temp_cmd_str;
char temp[3];
int i;
char c_for_quotes;
char *start;
char *copy_str;
int flag;
} t_token_ctx;
typedef struct s_main_vars
{
char *input;
char *temp_input;
t_input *tokens;
char *expanded_str;
char *new_input;
t_input *current;
int history_fd;
char *line;
t_input *free_tmp;
char *tmp_str;
char *tmp2_str;
int tmp;
int ret;
} t_main_vars;
typedef struct s_parse_utils
{
size_t len;
size_t cleaned_len;
size_t i;
size_t j;
size_t ri;
char *result;
} t_parse_utils;
typedef struct s_expand_ctx
{
t_input *current;
t_list *env;
char *str;
char *result;
char *var_name;
char *var_value;
char *expanded_value;
size_t i;
size_t j;
size_t len;
size_t result_len;
size_t result_index;
size_t var_start;
size_t var_len;
size_t value_len;
int in_single_quotes;
int in_double_quotes;
} t_expand_ctx;
/* Executer */
void *executer(t_input **input, t_list **env, int *exit_status);
void create_heredocs(t_input *input, t_list *env);
void exec_cmd(t_input **input, t_cmd *cmd, t_list **env);
int exec_builtin(t_input **input, t_cmd *cmd,
t_int_list **stdio_pipes_fds, t_list **env);
void exit_error(t_input **input, t_list **env,
t_int_list **stdio_pipes_fds);
void clean_int_list(t_int_list **lst);
void clean_block(t_input **input, int unlink_heredoc);
void clean_and_exit(t_input **input, t_list **env, int exit_status,
int forked);
t_builtin which_builtin(t_input *input);
char *get_block_cmd(t_input *input);
int get_flags(t_type type);
int only_one_cmd(t_input *input);
/* Builtins */
int exit_builtin(char **argv, t_input **input, t_list **env,
t_int_list **stdio_pipes_fds);
int echo_builtin(char **argv);
int pwd_builtin(void);
int env_builtin(t_list *env);
int unset_builtin(char **argv, t_list **env);
int export_builtin(char **argv, t_list **env);
void export_append(char *str, char *key, t_list **env);
int cd_builtin(char **argv, t_list **env);
/* General */
char *ft_getenv(char *key, t_list *env);
char *get_key(char *str);
int ft_export(char *str, t_list **env, int check);
int is_valid_identifier(char *str, int check);
void lst_remove_key(t_list **env, char *key);
void handle_sig_def(int sig);
void handle_sig_execve(int sig);
/* Parsing */
int check_syntax_errors(char *token);
t_type identify_type(char *token);
t_input *new_node(t_type type, char *str);
void add_node(t_input **head, t_input *new_node);
t_type identify_type(char *token);
char *clean_variable_value(char *var_value, t_input *current);
int is_pipe_balanced(char *str);
int is_quote_balanced(const char *str, char *quotes);
void normal_char(t_token_ctx *ctx);
void quotes(t_token_ctx *ctx);
void special_char(t_input **head, t_token_ctx *ctx);
int spaces(char c);
char *expand_variables(t_input *current, t_list *env);
void process_variable_expansion(t_expand_ctx *ctx);
void perform_expansion_str(t_expand_ctx *ctx);
size_t calculate_expand_len(char *str, t_list *env);
size_t calculate_expanded_length(t_input *current, t_list *env);
char *ft_strndup(char *s, size_t n);
void check_dollar_wrapper(t_expand_ctx *ctx);
char *expand_str(char *str, t_list *env);
void heredoc_quotes(t_input *current);
char *quotes_to_special(char *input);
int update_history_and_syntax(t_main_vars *vars, int *exit_status,
t_list **lst_env);
void take_history(t_main_vars *vars);
void update_history(t_main_vars *vars);
void shell_loop(t_main_vars *vars, int *exit_status,
t_list **lst_env);
void tokenize_and_execute(t_main_vars *vars, int *exit_status,
t_list **lst_env);
bool ask_more_for_quotes(t_main_vars *vars, char quotes);
int more_pipes_help(t_main_vars *vars);
int ask_more_for_pipes(t_main_vars *vars);
void remove_empty_nodes(t_input **head);
void process_variable_str(t_expand_ctx *ctx);
void expand_tokens(t_main_vars *vars, t_list *lst_env);
int just_spaces(const char *str);
char *remove_spaces(char *str);
t_input *tokenize(char *str);
void handle_single_quotes(t_expand_ctx *ctx);
void handle_double_quotes(t_expand_ctx *ctx);
void handle_variable_expansion(t_expand_ctx *ctx);
void expand_variable_value(t_expand_ctx *ctx);
int is_terminator(char c);
void init_expand_ctx(t_expand_ctx *ctx, t_input *current, t_list *env);
void check_var_len(t_expand_ctx *ctx);
#endif