|
| 1 | +/* ************************************************************************** */ |
| 2 | +/* */ |
| 3 | +/* ::: :::::::: */ |
| 4 | +/* shell.h :+: :+: :+: */ |
| 5 | +/* +:+ +:+ +:+ */ |
| 6 | +/* By: pchung <[email protected]> +#+ +:+ +#+ */ |
| 7 | +/* +#+#+#+#+#+ +#+ */ |
| 8 | +/* Created: 2025/03/09 03:36:43 by pchung #+# #+# */ |
| 9 | +/* Updated: 2025/03/09 03:58:39 by pchung ### ########.fr */ |
| 10 | +/* */ |
| 11 | +/* ************************************************************************** */ |
| 12 | + |
1 | 13 | #ifndef SHELL_H
|
2 |
| -#define SHELL_H |
3 |
| - |
4 |
| -#include "libft/libft.h" |
5 |
| -#include <signal.h> |
6 |
| -#include <stdio.h> |
7 |
| -#include <stdlib.h> |
8 |
| -#include <unistd.h> |
9 |
| -#include <glob.h> |
10 |
| -#include <string.h> |
11 |
| -#include <errno.h> |
12 |
| -#include <dirent.h> |
13 |
| -#include <readline/readline.h> |
14 |
| -#include <readline/history.h> |
15 |
| -#include <fcntl.h> |
16 |
| -#include <stdbool.h> |
17 |
| -#include <sys/wait.h> |
18 |
| -#include <term.h> |
19 |
| -#include <termios.h> |
20 |
| - |
21 |
| -#define HISTORY_SIZE 10 |
22 |
| -#define NOTSIG 1 |
23 |
| -#define RSTSIG 0 |
24 |
| - |
25 |
| -extern volatile sig_atomic_t g_signal_received; |
| 14 | +# define SHELL_H |
| 15 | + |
| 16 | +# include "libft/libft.h" |
| 17 | +# include <dirent.h> |
| 18 | +# include <errno.h> |
| 19 | +# include <fcntl.h> |
| 20 | +# include <glob.h> |
| 21 | +# include <readline/history.h> |
| 22 | +# include <readline/readline.h> |
| 23 | +# include <signal.h> |
| 24 | +# include <stdbool.h> |
| 25 | +# include <stdio.h> |
| 26 | +# include <stdlib.h> |
| 27 | +# include <string.h> |
| 28 | +# include <sys/wait.h> |
| 29 | +# include <term.h> |
| 30 | +# include <termios.h> |
| 31 | +# include <unistd.h> |
| 32 | + |
| 33 | +# define HISTORY_SIZE 10 |
| 34 | +# define NOTSIG 1 |
| 35 | +# define RSTSIG 0 |
| 36 | +# define SHELL_BUF_SIZE 8192 |
| 37 | +# define SIGINT_ 42 |
| 38 | + |
| 39 | +extern volatile sig_atomic_t g_signal_received; |
26 | 40 |
|
27 | 41 | typedef struct s_ast
|
28 | 42 | {
|
29 |
| - t_list *command_groups; |
30 |
| -} t_ast; |
| 43 | + t_list *command_groups; |
| 44 | +} t_ast; |
31 | 45 |
|
32 | 46 | typedef struct s_command_group
|
33 | 47 | {
|
34 |
| - t_list *cmds; |
35 |
| - int cmd_amount; |
36 |
| - int **pipes; |
37 |
| - t_list *pids; |
38 |
| - int return_value; |
39 |
| -} t_command_group; |
| 48 | + t_list *cmds; |
| 49 | + int cmd_amount; |
| 50 | + int **pipes; |
| 51 | + t_list *pids; |
| 52 | + int return_value; |
| 53 | +} t_command_group; |
40 | 54 |
|
41 | 55 | typedef struct s_cmd
|
42 | 56 | {
|
43 |
| - t_list *tokens; |
44 |
| - t_list *redirs; |
45 |
| -} t_cmd; |
| 57 | + t_list *tokens; |
| 58 | + t_list *redirs; |
| 59 | +} t_cmd; |
46 | 60 |
|
47 | 61 | typedef struct s_redir
|
48 | 62 | {
|
49 |
| - char *direction; |
50 |
| - char type[2]; |
51 |
| -} t_redir; |
| 63 | + char *direction; |
| 64 | + char type[2]; |
| 65 | +} t_redir; |
52 | 66 |
|
53 | 67 | typedef struct s_env
|
54 | 68 | {
|
55 |
| - char *key; |
56 |
| - char *value; |
57 |
| - struct s_env *next; |
58 |
| -} t_env; |
| 69 | + char *key; |
| 70 | + char *value; |
| 71 | + struct s_env *next; |
| 72 | +} t_env; |
59 | 73 |
|
60 | 74 | typedef struct s_token
|
61 | 75 | {
|
62 |
| - char *value; |
63 |
| - int is_single_quoted; |
64 |
| -} t_token; |
65 |
| - |
| 76 | + char *value; |
| 77 | + int is_single_quoted; |
| 78 | +} t_token; |
66 | 79 |
|
67 |
| -t_ast *get_ast(const char *input, t_env *env_list, int *exit_status); |
68 |
| -t_env *init_env_list(char ** environ); |
| 80 | +t_ast *get_ast(const char *input, t_env *env_list, |
| 81 | + int *exit_status); |
| 82 | +t_env *init_env_list(char **environ); |
69 | 83 |
|
70 | 84 | // Utility Functions
|
71 |
| -void run_shell(t_env *env_list, char **envp); |
72 |
| -char *get_env_value(t_env *env_list, const char *key); |
73 |
| -void set_env_value(t_env **env_list, const char *key, const char *value); |
74 |
| -void unset_env_value(t_env **env_list, const char *key); |
75 |
| -void free_env_list(t_env **env_list); |
76 |
| -int exec_ast(t_ast *ast, t_env *env_list, char **envp); |
77 |
| -char **convert_env_list_to_array(t_env *env_list); |
78 |
| -void free_env_array(char **envp);; |
79 |
| -void shell_unknown_command(char *cmd); |
80 |
| -int is_parent_builtin(t_cmd *cmd); |
| 85 | +void run_shell(t_env *env_list, char **envp); |
| 86 | +char *get_env_value(t_env *env_list, |
| 87 | + const char *key); |
| 88 | +void set_env_value(t_env **env_list, const char *key, |
| 89 | + const char *value); |
| 90 | +void unset_env_value(t_env **env_list, |
| 91 | + const char *key); |
| 92 | +void free_env_list(t_env **env_list); |
| 93 | +int exec_ast(t_ast *ast, t_env *env_list, |
| 94 | + char **envp); |
| 95 | +char **convert_env_list_to_array(t_env *env_list); |
| 96 | +void free_env_array(char **envp); |
| 97 | +; |
| 98 | +void shell_unknown_command(char *cmd); |
| 99 | +int is_parent_builtin(t_cmd *cmd); |
81 | 100 |
|
82 | 101 | // FREE FUNCTIONS
|
83 |
| -void free_ast(t_ast *ast); |
84 |
| -void free_cmd(t_cmd *cmd); |
85 |
| -void free_command_group(t_command_group *command_group); |
86 |
| -void free_redir(void *ptr); |
87 |
| -void free_argv(char **argv); |
| 102 | +void free_ast(t_ast *ast); |
| 103 | +void free_cmd(t_cmd *cmd); |
| 104 | +void free_command_group( |
| 105 | + t_command_group *command_group); |
| 106 | +void free_redir(void *ptr); |
| 107 | +void free_argv(char **argv); |
| 108 | +void free_paths(char **paths); |
| 109 | +void free_tokens(char **tokens); |
88 | 110 |
|
89 | 111 | // BUILD-IN FUNCTIONS
|
90 |
| -int shell_env(t_env *env_list); |
91 |
| -int shell_export(char **args, t_env *env_list); |
92 |
| -int shell_unset(char **args, t_env *env_list); |
93 |
| -int shell_clear(void); |
94 |
| -int shell_cd(char **argv, t_env *env_list); |
95 |
| -int shell_pwd(void); |
96 |
| -int shell_echo(char **args); |
97 |
| -int shell_exit(char **argv); |
| 112 | +int shell_env(t_env *env_list); |
| 113 | +int shell_export(char **args, t_env *env_list); |
| 114 | +int shell_unset(char **args, t_env *env_list); |
| 115 | +int shell_clear(void); |
| 116 | +int shell_cd(char **argv, t_env *env_list); |
| 117 | +int shell_pwd(void); |
| 118 | +int shell_echo(char **args); |
| 119 | +int shell_exit(char **argv); |
98 | 120 |
|
99 | 121 | // TERMINAL SETTINGS
|
100 |
| -void reset_terminal_settings(const struct termios *old_termios); |
101 |
| -void setup_terminal(struct termios *old_termios); |
102 |
| -void term_clear_screen(); |
| 122 | +void reset_terminal_settings( |
| 123 | + const struct termios *old_termios); |
| 124 | +void setup_terminal(struct termios *old_termios); |
| 125 | +void term_clear_screen(void); |
103 | 126 |
|
104 | 127 | // TERMCAP FUNCTIONS
|
105 |
| -void handle_input(char *buf, size_t *len, size_t max_len); |
| 128 | +void handle_input(char *buf, size_t *len, |
| 129 | + size_t max_len); |
106 | 130 |
|
107 | 131 | // SINGAL FUNCTIONS
|
108 |
| -void init_readline_for_signal(void); |
109 |
| -void setup_signals(void); |
110 |
| -void handle_sigint(int sig); |
111 |
| -void set_signal(int signum, void (*handler)(int), int flags); |
112 |
| -void init_signal(void (*handler_for_sigint)(int), |
113 |
| - void (*handler_for_sigquit)(int)); |
| 132 | +void init_readline_for_signal(void); |
| 133 | +void setup_signals(void); |
| 134 | +void handle_sigint(int sig); |
| 135 | +void set_signal(int signum, void (*handler)(int), |
| 136 | + int flags); |
| 137 | +void init_signal(void (*handler_for_sigint)(int), |
| 138 | + void (*handler_for_sigquit)(int)); |
114 | 139 |
|
115 | 140 | // PARSER FUNCTIONS
|
116 |
| -char **token_list_to_argv(t_cmd *cmd); |
| 141 | +char **token_list_to_argv(t_cmd *cmd); |
117 | 142 |
|
118 | 143 | #endif
|
0 commit comments