MiniDevil As beautiful as a shell
structs.h
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* structs.h :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/08 02:24:43 by baelgadi #+# #+# */
9 /* Updated: 2026/03/11 03:00:18 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #ifndef STRUCTS_H
14 # define STRUCTS_H
15 
16 /* ────────────────────────────────────────────────── */
17 /* ────────────────────── ENUMS ───────────────────── */
18 /* ────────────────────────────────────────────────── */
19 
23 typedef enum e_token_type
24 {
35 } t_token_type;
36 
40 typedef enum e_quote_type
41 {
46 
50 typedef enum e_node_type
51 {
62 
63 /* ────────────────────────────────────────────────── */
64 /* ──────────────── TOKEN STRUCTURE ───────────────── */
65 /* ────────────────────────────────────────────────── */
66 
70 typedef struct s_token
71 {
73  char *value;
75  int connected;
76  struct s_token *next;
77 } t_token;
78 
79 /* ────────────────────────────────────────────────── */
80 /* ───────────────── ENV STRUCTURE ────────────────── */
81 /* ────────────────────────────────────────────────── */
82 
86 typedef struct s_env
87 {
88  char *key;
89  char *value;
90  struct s_env *next;
91  struct s_env *prev;
92 } t_env;
93 
94 /* ────────────────────────────────────────────────── */
95 /* ──────────────── AST STRUCTURES ────────────────── */
96 /* ────────────────────────────────────────────────── */
97 
101 typedef struct s_cmd_node
102 {
103  char **args;
104  int argc;
105 } t_cmd_node;
106 
110 typedef struct s_redir_node
111 {
112  char *file;
113  struct s_ast *cmd;
115  int quote;
117 } t_redir_node;
118 
122 typedef struct s_binary_node
123 {
124  struct s_ast *left;
125  struct s_ast *right;
126 } t_binary_node;
127 
131 typedef struct s_subshell_node
132 {
133  struct s_ast *child;
135 
139 typedef union u_ast_data
140 {
145 } t_ast_data;
146 
150 typedef struct s_ast
151 {
154 } t_ast;
155 
156 /* ────────────────────────────────────────────────── */
157 /* ──────────────── SHELL STRUCTURE ───────────────── */
158 /* ────────────────────────────────────────────────── */
159 
163 typedef struct s_shell
164 {
167  int running;
169  int ui_mode;
170  struct s_ui *ui;
171  struct s_ast *current_ast;
173  int is_child;
174 } t_shell;
175 
176 /* ────────────────────────────────────────────────── */
177 /* ────────────── SYNTAX ERROR CODES ──────────────── */
178 /* ────────────────────────────────────────────────── */
179 
183 typedef enum e_syntax_error
184 {
185  ERR_NONE = 0,
200 
201 /* ────────────────────────────────────────────────── */
202 /* ──────────── WILDCARD CONTEXT (BONUS) ──────────── */
203 /* ────────────────────────────────────────────────── */
204 
208 typedef struct s_wild_ctx
209 {
214 } t_wild_ctx;
215 
216 #endif
t_syntax_error
Syntax error codes (for parser)
Definition: structs.h:184
@ ERR_REDIR_NO_FILE
Definition: structs.h:191
@ ERR_PIPE_DOUBLE
Definition: structs.h:189
@ ERR_EMPTY_INPUT
Definition: structs.h:186
@ ERR_AND_UNEXPECTED
Definition: structs.h:193
@ ERR_NONE
Definition: structs.h:185
@ ERR_PIPE_START
Definition: structs.h:187
@ ERR_UNEXPECTED_LPAREN
Definition: structs.h:198
@ ERR_PIPE_NO_CMD
Definition: structs.h:190
@ ERR_OR_UNEXPECTED
Definition: structs.h:194
@ ERR_REDIR_AFTER_PIPE
Definition: structs.h:192
@ ERR_PAREN_CLOSE
Definition: structs.h:196
@ ERR_PAREN_EMPTY
Definition: structs.h:197
@ ERR_PIPE_END
Definition: structs.h:188
@ ERR_PAREN_OPEN
Definition: structs.h:195
t_token_type
Token types produced by Lexer.
Definition: structs.h:24
@ TOKEN_REDIR_OUT
Definition: structs.h:28
@ TOKEN_WORD
Definition: structs.h:25
@ TOKEN_HEREDOC
Definition: structs.h:30
@ TOKEN_AND
Definition: structs.h:31
@ TOKEN_PIPE
Definition: structs.h:26
@ TOKEN_REDIR_IN
Definition: structs.h:27
@ TOKEN_RPAREN
Definition: structs.h:34
@ TOKEN_APPEND
Definition: structs.h:29
@ TOKEN_LPAREN
Definition: structs.h:33
@ TOKEN_OR
Definition: structs.h:32
t_node_type
AST node types.
Definition: structs.h:51
@ NODE_OR
Definition: structs.h:59
@ NODE_REDIR_HEREDOC
Definition: structs.h:57
@ NODE_REDIR_APPEND
Definition: structs.h:56
@ NODE_AND
Definition: structs.h:58
@ NODE_COMMAND
Definition: structs.h:52
@ NODE_PIPE
Definition: structs.h:53
@ NODE_REDIR_OUT
Definition: structs.h:55
@ NODE_REDIR_IN
Definition: structs.h:54
@ NODE_SUBSHELL
Definition: structs.h:60
t_quote_type
Quote context of a token.
Definition: structs.h:41
@ QUOTE_SINGLE
Definition: structs.h:43
@ QUOTE_NONE
Definition: structs.h:42
@ QUOTE_DOUBLE
Definition: structs.h:44
AST node (union based ↑)
Definition: structs.h:151
t_ast_data data
cmd, redir or binary data
Definition: structs.h:153
t_node_type type
To determine which union member to pick.
Definition: structs.h:152
Binary operator data inside AST Node (pipe)
Definition: structs.h:123
struct s_ast * left
left child (before pipe)
Definition: structs.h:124
struct s_ast * right
right child (after pipe)
Definition: structs.h:125
Command data inside AST node.
Definition: structs.h:102
char ** args
NULL terminated args array.
Definition: structs.h:103
int argc
Number of arguments.
Definition: structs.h:104
Environment variable (doubly linked list)
Definition: structs.h:87
char * key
Variable name.
Definition: structs.h:88
char * value
Value (NULL if export only)
Definition: structs.h:89
struct s_env * prev
Previous node.
Definition: structs.h:91
struct s_env * next
Next node.
Definition: structs.h:90
Redirection data inside AST node.
Definition: structs.h:111
int heredoc_fd
Heredoc pipe fd (-1 if not used)
Definition: structs.h:116
t_node_type redir_type
Redirection type.
Definition: structs.h:114
char * file
Target filename or delimiter.
Definition: structs.h:112
struct s_ast * cmd
Command subtree.
Definition: structs.h:113
int quote
1 if delimiter was quoted
Definition: structs.h:115
Shell state.
Definition: structs.h:164
int interactive
1 if STDIN is a tty
Definition: structs.h:168
t_env * env
Environment linked list.
Definition: structs.h:165
int is_child
Child flag (to avoid leaks)
Definition: structs.h:173
int running
1 while main loop is active
Definition: structs.h:167
char * current_input
Current input line.
Definition: structs.h:172
int exit_status
Last command's exit status.
Definition: structs.h:166
struct s_ast * current_ast
Currently executing AST.
Definition: structs.h:171
struct s_ui * ui
UI state (NULL if off)
Definition: structs.h:170
int ui_mode
1 if UI mode (–ui)
Definition: structs.h:169
Subshell data inside of the AST node.
Definition: structs.h:132
struct s_ast * child
Definition: structs.h:133
Lexer token (singly linked list)
Definition: structs.h:71
char * value
Text content (allocated)
Definition: structs.h:73
t_token_type type
Token type (word, pipe, redir)
Definition: structs.h:72
struct s_token * next
Next token.
Definition: structs.h:76
t_quote_type quote_type
Quote context.
Definition: structs.h:74
int connected
1 if connected
Definition: structs.h:75
(BONUS) Wildcard context
Definition: structs.h:209
t_token ** head
pointer to the list head pointer
Definition: structs.h:211
t_token * end
last token of the current chain
Definition: structs.h:213
t_token * cur
first token of the current chain
Definition: structs.h:212
t_token ** prev
pointer to the previous token
Definition: structs.h:210
Only one will be valid depending on node's type.
Definition: structs.h:140
t_binary_node binary
Definition: structs.h:143
t_subshell_node subshell
Definition: structs.h:144
t_cmd_node cmd
Definition: structs.h:141
t_redir_node redir
Definition: structs.h:142