-
Notifications
You must be signed in to change notification settings - Fork 0
/
minishell.h
199 lines (173 loc) · 5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/14 04:08:20 by san #+# #+# */
/* Updated: 2022/06/16 13:05:53 by juhan ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MINISHELL_H
# define MINISHELL_H
# include "libft/libft.h"
# include <stdio.h>
# include <readline/readline.h>
# include <readline/history.h>
# include <string.h>
# include <termios.h>
# include <signal.h>
# include <errno.h>
# include <sys/wait.h>
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>
# define STDIN 0
# define STDOUT 1
# define STDERR 2
# define BUILTIN_COUNT 7
# define TYPE_CMD_CD 1
# define TYPE_CMD_ECHO 2
# define TYPE_CMD_ENV 3
# define TYPE_CMD_EXIT 4
# define TYPE_CMD_EXPORT 5
# define TYPE_CMD_PWD 6
# define TYPE_CMD_UNSET 7
# define TYPE_CMD 0
# define TYPE_ARG 1
# define TYPE_PIPE 2
# define TYPE_REDIR_INPUT 3
# define TYPE_REDIR_OUTPUT 4
# define TYPE_REDIR_APPEND 5
# define TYPE_REDIR_HEREDOC 6
# define ERR_CMD_NOT_FOUND 127
# define ERR_MALLOC 1
# define NO_PIPE 0
# define PIPE_LEFT 1
# define PIPE_RIGHT 2
# define PIPE_BOTH 3
# define MAX 1024
# define CHAR_NO_QUOTE 0
# define CHAR_SINGLE_QUOTE 1
# define CHAR_DOUBLE_QUOTE 2
typedef struct s_stack
{
int top;
int items[MAX];
} t_stack;
typedef struct s_cmd
{
int is_head;
int type;
int fds[2];
int is_first;
int redir_in;
int redir_out;
int is_left_pipe;
int is_right_pipe;
int arg_count;
char *content;
char *path;
char **argv;
struct s_cmd *next;
struct s_cmd *prev;
} t_cmd;
typedef struct s_minishell
{
pid_t pid;
int in;
int out;
char **envp;
t_cmd *cmd_list;
} t_minishell;
int g_e_status;
/*
* parse functions
*/
void init_argv(t_minishell *sh, t_cmd *cur);
char **create_argv(t_cmd *cmd, int len);
void get_arg_count(t_cmd *cmd);
char *handle_expansion(t_minishell *sh, char *str);
void append_and_increment(char *ret, char *str, int *i, int inc);
void set_flag_and_increment(int *flag, int *i);
int parse(t_minishell *sh, char *line);
/*
* readline & execute
*/
void read_line(char **line_read);
int handle_cmd(t_minishell *sh);
char *find_path(char **envp, char *command);
int is_builtin(char *content);
void excecute_cmd(t_minishell *sh, t_cmd *cmd, int *prefds, char *path);
/*
* builtin functions
*/
void cmd_cd(t_minishell *sh, char **argv);
void cmd_echo(t_minishell *sh, char **argv);
void cmd_env(t_minishell *sh, char **argv);
void cmd_exit(char **argv);
void cmd_export(t_minishell *sh, char **argv);
void cmd_pwd(void);
void cmd_unset(t_minishell *sh, char **argv);
/*
* list util functions
*/
void ft_lstadd_back(t_cmd **lst, t_cmd *new);
void ft_lstadd_front(t_cmd **lst, t_cmd *new);
void ft_lstclear(t_cmd **lst, void (*del)(void *));
void ft_lstdelone(t_cmd *lst, void (*del)(void *));
void ft_lstiter(t_cmd *lst, void (*f)(void *));
t_cmd *ft_lstlast(t_cmd *lst);
t_cmd *ft_lstmap(t_cmd *lst, void *(*f)(void *), void (*del)(void *));
t_cmd *ft_lstnew(void *content, int type, int is_head, int is_first);
int ft_lstsize(t_cmd *lst);
/*
* utils
*/
int ch_strncmp(const char *s1, const char *s2, size_t n);
void ft_free_double(char **ptr);
char *av_have_eq(char *argv);
long long ft_atoll(const char *str);
/*
* envp_util functions
*/
int ft_envplen(char **envp);
int check_argv_name(char *str, char *cmd);
char **copy_envp(char **envp);
char *get_envp_name(char *envp);
char *ft_getenv(char **envp, char *envp_name);
void change_envp(char ***envp, char *str, char *env_name);
int ch_envp_with_name(char ***envp, char *s1, char *s2);
/*
* signal.c
*/
void ft_signal_handler(int signo);
/*
* redirection
*/
t_cmd *redirection(t_minishell *sh, t_cmd *cmd);
void read_line_heredoc_continue(char *end, int temp_fd);
void open_for_heredoc(t_minishell *sh, t_cmd *cmd, char *end);
/*
* free.c
*/
void ft_free(char *str);
void ft_free_double(char **ptr);
void ft_free_cmd_list(t_minishell *sh);
void ft_exit(int exit_code);
void ft_free_cmd_lst(t_minishell *sh);
/*
* ft_error.c
*/
void ft_error(char *command, char *argv, char *err_msg, int err_code);
void ft_error_2(char *command, char *argv, int err_code);
void ft_error_redir(char *err_msg, int err_code, int fd);
void ft_err_hdoc(char *errmsg, int errcode, int fd, char *name);
void ft_error_open(char *err_msg, int err_code);
/*
* fd.c
*/
void ft_reset_fd(int *fd);
void ft_close(int fd);
#endif