maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
AST.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* AST.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/12 19:35:45 by rocky #+# #+# */
9/* Updated: 2024/09/09 16:18:58 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "tokens.h"
14
15t_ast *manage_redirs(t_token **tokens, t_ms_data *data);
17int is_redir_node(t_token *tokens);
18t_ast *new_ast_node(void);
19
21{
22 t_ast *command_node;
23 t_ast *redirect_node;
24 t_token *current_token;
25
26 if (!tokens || !*tokens)
27 return (NULL);
28 command_node = manage_commands(tokens, data);
29 current_token = *tokens;
30 while (current_token && is_redir_node(current_token))
31 {
32 redirect_node = create_redir_node(current_token);
33 redirect_node->left = command_node;
34 *tokens = current_token->next;
35 if (*tokens)
36 {
37 redirect_node->right = create_redir_node(*tokens);
38 *tokens = (*tokens)->next;
39 }
40 else
41 redirect_node->right = NULL;
42 command_node = redirect_node;
43 current_token = *tokens;
44 }
45 return (command_node);
46}
47
48void set_command_args(t_ast *command_node, t_token **tokens, int arg_count)
49{
50 int i;
51
52 i = 0;
53 while (i < arg_count)
54 {
55 command_node->args[i] = ft_strdup((*tokens)->data);
56 *tokens = (*tokens)->next;
57 i++;
58 }
59 command_node->args[arg_count] = NULL;
60}
61
63{
64 t_ast *node;
65
66 node = new_ast_node();
67 node->type = token->type;
68 node->args = malloc(sizeof(char *) * 2);
69 if (!node->args)
70 {
71 free(node);
72 return (NULL);
73 }
74 node->args[0] = ft_strdup(token->data);
75 node->args[1] = NULL;
76 return (node);
77}
78
80{
81 if (tokens->type == REDIR_IN
82 || tokens->type == REDIR_OUT
83 || tokens->type == REDIR_APPEND
84 || tokens->type == REDIR_HEREDOC)
85 return (1);
86 return (0);
87}
88
90{
91 t_ast *node;
92
93 node = malloc(sizeof(t_ast));
94 if (!node)
95 return (NULL);
96 node->type = NONE;
97 node->args = NULL;
98 node->left = NULL;
99 node->right = NULL;
100 return (node);
101}
t_ast * new_ast_node(void)
Definition AST.c:89
t_ast * create_redir_node(t_token *token)
Definition AST.c:62
int is_redir_node(t_token *tokens)
Definition AST.c:79
t_ast * manage_redirs(t_token **tokens, t_ms_data *data)
Definition AST.c:20
void set_command_args(t_ast *command_node, t_token **tokens, int arg_count)
Definition AST.c:48
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
Definition tokens.h:50
char ** args
Definition tokens.h:52
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53
t_token_type type
Definition tokens.h:51
t_token_type type
Definition tokens.h:43
struct s_token * next
Definition tokens.h:45
char * data
Definition tokens.h:44
@ REDIR_IN
Definition tokens.h:34
@ REDIR_HEREDOC
Definition tokens.h:37
@ NONE
Definition tokens.h:38
@ REDIR_OUT
Definition tokens.h:35
@ REDIR_APPEND
Definition tokens.h:36
t_ast * manage_commands(t_token **tokens, t_ms_data *data)
Definition AST_utils.c:72