-
Notifications
You must be signed in to change notification settings - Fork 1
/
minishell.h
218 lines (198 loc) · 5.88 KB
/
minishell.h
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
#ifndef MINISHELL_H
#define MINISHELL_H
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <dirent.h>
#include <sys/wait.h>
#define DOUBLE_QUOTE "\""
#define SINGLE_QUOTE "'"
#define DOLLAR_SIGN "$"
#define WILD_CARD "*"
#define QUESTION_MARK "?"
#define BRACETS "{}"
#define SLASH "/"
#define READ_END 0
#define WRITE_END 1
#define ESLASH 1 // Komutun slash karakteri içerdiği hata kodu
#define EADDPATH 2 // Path eklerken oluşan hata kodu
#define EPWD 3 // Getcwd fonksiyonu çalışmazsa oluşacak hata kodu
#define ECD 4 // Chdir fonksiyonu çalışmazsa oluşacak hata kodu
enum e_builtin_types
{
CD = 1,
ENV,
PWD,
ECHO,
EXIT,
UNSET,
EXPORT
};
enum tokens{
TOKEN_PIPE,
TOKEN_OR,
TOKEN_AND,
TOKEN_GREATER,
TOKEN_SMALLER,
TOKEN_APPEND,
TOKEN_HERE_DOC,
TOKEN_OPEN_PAR,
TOKEN_CLOSE_PAR,
TOKEN_STR
};
enum errors{
FILE_NOT_FOUND = 1,
SYNTAX_ERROR = 2,
PERM_DENIED = 4,
CMD_NOT_FOUND = 127,
SYSTEM_ERR = -1,
MEMORY_ERR = -2,
DUP_ERR = -3,
FORK_ERR = -4,
PIPE_ERR = -5
};
typedef struct s_token
{
enum tokens type;
char *value;
struct s_token *prev;
struct s_token *next;
} t_token;
typedef struct s_file
{
enum tokens type;
char *file_name;
struct s_file *next;
} t_file;
typedef struct s_ms
{
int ignore;
int opening_prompt;
int parent_pid;
int *child_pids;
int child_pids_count;
int in_file;
int out_file;
char **ev;
char **paths;
struct s_parsed **parsed_commands;
struct s_token *tokens;
} t_ms;
typedef struct s_parsed
{
int exec;
int in_file;
int out_file;
char *cmd;
char **arguments;
t_token *paranthesis;
struct s_parsed **parantheses_andor;
t_file *file_list;
struct s_parsed *prev;
struct s_parsed *next;
} t_parsed;
extern t_ms g_ms;
// utils
char *ft_strdup(const char *str);
void init_ms(char **ev);
char *get_env(char *str);
void check_dir(char *cmd);
char *get_path(char *cmd);
int addenv(char *key, char *val);
int is_whitespace(char c);
char **set_ev(char **ev);
int env_len(void);
void set_paths(void);
size_t ft_strlen(const char *s);
char *ft_strchr(const char *s, int c);
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize);
size_t ft_strcpy(char *dst, const char *src);
char *strnstr_wildcard(const char *haystack, const char *needle, size_t len);
int ft_atoi(const char *str);
char *ft_itoa(int n);
void *ft_calloc(size_t count, size_t size);
void ft_bzero(void *s, size_t n);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strjoin_freed(char *s1, char *s2, int free_place);
char **ft_split(char const *s, char c);
int ft_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int find_pair(char *input, char c);
int list_len(char **char_list);
int ft_strnsearch(char *string, char *chars_to_search, size_t len);
//error
// void cmd_err(char *str);
// void no_file_err(char *str);
int print_error(int error_code, char *param);
void open_file_error(void);
//free
void free_all(t_token *tokens, t_parsed **parsed_commands);
void free_tokens(t_token *tokens);
void free_parsed(t_parsed **parsed_commands);
void free_redirections(t_file *file_list);
void free_array(char **arr);
//builtin
int is_builtin(char *command);
void run_builtin(char **exe);
void builtin_cd(char **execute);
void builtin_echo(char **input);
void builtin_env(void);
void builtin_exit(char **input);
void builtin_export(char **input);
void builtin_pwd(void);
void builtin_unset(char **input);
//--------------lexer--------------
int is_metacharacter(char c);
int token_str_lexer(char *input);
int add_token(char *input, t_token *command_table, enum tokens type, int len);
void find_token(char *input, t_token *command_table);
t_token *tokenizer(char *input);
void get_next_token(t_token *command_table);
int syntax_check(t_token *command_table);
//--------------expander--------------
int add_double_quote(char **str, char *val);
int add_single_quote(char **str, char *val);
int add_dollar(char **str, char *val);
void add_dollar_other(char **str, char *val);
int add_char(char **str, char *val);
char *check_str(char *value);
void updating_argument_list(int argument_no, t_parsed **command, char **wildcard_list);
void expander(t_parsed **command);
//--------------wildcard--------------
int wildcard_count(char *wild_string);
int checking_between_wildcards(int wildcard_count, char *wild_str, char *expected_str);
int is_wild_eq_string(char *wild_str, char *expected_str);
void add_wildcard_to_list(char *path, char ***arguments);
void wildcard(char *path, char **destined_path, int way, char ***arguments);
//--------------parser--------------
int andor_count(t_token *command_table);
t_parsed *new_parse_command(int in_file, int out_file);
void add_andor_list(t_parsed *command, t_parsed **andor_table);
void add_redirection(t_token **command_table, t_parsed **command);
void skip_paranthesis(t_token **command_table);
t_parsed *add_parse(t_token **command_table, t_parsed **old_command);
void add_argument(t_token **command_table, t_parsed **command);
void add_paranthesis(t_token **command_table, t_parsed **command);
t_parsed **parse_commands(int in_file, int out_file, t_token *command_table);
//--------------executor--------------
void close_fd(t_parsed *command);
int here_doc_fd(char *limiter);
int read_file_fd(char *file_name, int type);
int write_file_fd(char *file_name, int type);
void apply_redirection(t_parsed **command);
void child_organizer(t_parsed *command);
void command_executor(t_parsed *command);
void create_pipe(t_parsed **command);
void create_redirections(t_parsed **andor_table);
void organizer(t_parsed **andor_table);
void executor(t_parsed **andor_table);
#endif