MiniDevil As beautiful as a shell
ast.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* ast.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/07 20:42:39 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 07:22:37 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "ast.h"
14 #include "libft.h"
15 
23 t_ast *create_cmd_node(char **args, int argc)
24 {
25  t_ast *node;
26 
27  node = ft_calloc(sizeof(t_ast), 1);
28  if (!node)
29  return (NULL);
30  node->type = NODE_COMMAND;
31  node->data.cmd.args = args;
32  node->data.cmd.argc = argc;
33  return (node);
34 }
35 
44 {
45  t_ast *node;
46 
47  node = ft_calloc(sizeof(t_ast), 1);
48  if (!node)
49  {
50  free_ast(left);
51  free_ast(right);
52  return (NULL);
53  }
54  node->type = NODE_PIPE;
55  node->data.binary.left = left;
56  node->data.binary.right = right;
57  return (node);
58 }
59 
71 t_ast *create_redir_node(t_node_type type, char *file, t_ast *cmd, int quoted)
72 {
73  t_ast *node;
74 
75  node = ft_calloc(sizeof(t_ast), 1);
76  if (!node)
77  return (NULL);
78  node->type = type;
79  node->data.redir.file = file;
80  node->data.redir.cmd = cmd;
81  node->data.redir.redir_type = type;
82  node->data.redir.quote = quoted;
83  node->data.redir.heredoc_fd = -1;
84  return (node);
85 }
t_ast * create_redir_node(t_node_type type, char *file, t_ast *cmd, int quoted)
Create a redirection AST node.
Definition: ast.c:71
t_ast * create_cmd_node(char **args, int argc)
Create a NODE_COMMAND AST node.
Definition: ast.c:23
t_ast * create_pipe_node(t_ast *left, t_ast *right)
Create a pipe AST node connecting 2 subtrees.
Definition: ast.c:43
AST node creation and destruction prototypes.
void free_ast(t_ast *node)
Recursively free an entire AST tree.
Definition: ast_utils.c:46
t_node_type
AST node types.
Definition: structs.h:51
@ NODE_COMMAND
Definition: structs.h:52
@ NODE_PIPE
Definition: structs.h:53
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
struct s_ast * left
left child (before pipe)
Definition: structs.h:124
struct s_ast * right
right child (after pipe)
Definition: structs.h:125
char ** args
NULL terminated args array.
Definition: structs.h:103
int argc
Number of arguments.
Definition: structs.h:104
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
t_binary_node binary
Definition: structs.h:143
t_cmd_node cmd
Definition: structs.h:141
t_redir_node redir
Definition: structs.h:142