-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.h
143 lines (124 loc) · 4.02 KB
/
shell.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* shell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pchung <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/09 03:36:43 by pchung #+# #+# */
/* Updated: 2025/03/09 03:58:39 by pchung ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SHELL_H
# define SHELL_H
# include "libft/libft.h"
# include <dirent.h>
# include <errno.h>
# include <fcntl.h>
# include <glob.h>
# include <readline/history.h>
# include <readline/readline.h>
# include <signal.h>
# include <stdbool.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <sys/wait.h>
# include <term.h>
# include <termios.h>
# include <unistd.h>
# define HISTORY_SIZE 10
# define NOTSIG 1
# define RSTSIG 0
# define SHELL_BUF_SIZE 8192
# define SIGINT_ 42
extern volatile sig_atomic_t g_signal_received;
typedef struct s_ast
{
t_list *command_groups;
} t_ast;
typedef struct s_command_group
{
t_list *cmds;
int cmd_amount;
int **pipes;
t_list *pids;
int return_value;
} t_command_group;
typedef struct s_cmd
{
t_list *tokens;
t_list *redirs;
} t_cmd;
typedef struct s_redir
{
char *direction;
char type[2];
} t_redir;
typedef struct s_env
{
char *key;
char *value;
struct s_env *next;
} t_env;
typedef struct s_token
{
char *value;
int is_single_quoted;
} t_token;
t_ast *get_ast(const char *input, t_env *env_list,
int *exit_status);
t_env *init_env_list(char **environ);
// Utility Functions
void run_shell(t_env *env_list, char **envp);
char *get_env_value(t_env *env_list,
const char *key);
void set_env_value(t_env **env_list, const char *key,
const char *value);
void unset_env_value(t_env **env_list,
const char *key);
void free_env_list(t_env **env_list);
int exec_ast(t_ast *ast, t_env *env_list,
char **envp);
char **convert_env_list_to_array(t_env *env_list);
void free_env_array(char **envp);
;
void shell_unknown_command(char *cmd);
int is_parent_builtin(t_cmd *cmd);
// FREE FUNCTIONS
void free_ast(t_ast *ast);
void free_cmd(t_cmd *cmd);
void free_command_group(
t_command_group *command_group);
void free_redir(void *ptr);
void free_argv(char **argv);
void free_paths(char **paths);
void free_tokens(char **tokens);
// BUILD-IN FUNCTIONS
int shell_env(t_env *env_list);
int shell_export(char **args, t_env *env_list);
int shell_unset(char **args, t_env *env_list);
int shell_clear(void);
int shell_cd(char **argv, t_env *env_list);
int shell_pwd(void);
int shell_echo(char **args);
int shell_exit(char **argv);
// TERMINAL SETTINGS
void reset_terminal_settings(
const struct termios *old_termios);
void setup_terminal(struct termios *old_termios);
void term_clear_screen(void);
// TERMCAP FUNCTIONS
void handle_input(char *buf, size_t *len,
size_t max_len);
// SINGAL FUNCTIONS
void init_readline_for_signal(void);
void setup_signals(void);
void handle_sigint(int sig);
void set_signal(int signum, void (*handler)(int),
int flags);
void init_signal(void (*handler_for_sigint)(int),
void (*handler_for_sigquit)(int));
// PARSER FUNCTIONS
char **token_list_to_argv(t_cmd *cmd);
#endif