MiniDevil As beautiful as a shell
parser_cmd.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* parser_cmd.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/07 22:34:13 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 06:50:27 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "parser.h"
14 #include "ast.h"
15 #include "libft.h"
16 
26 {
27  int count;
28 
29  count = 0;
30  while (tokens && tokens->type == TOKEN_WORD)
31  {
32  count++;
33  while (tokens && tokens->connected)
34  tokens = tokens->next;
35  if (tokens)
36  tokens = tokens->next;
37  }
38  return (count);
39 }
40 
53 static int fill_args_array(char **args, t_token **tokens, int count)
54 {
55  int i;
56  char *word;
57  char *temp;
58 
59  i = 0;
60  while (i < count)
61  {
62  word = ft_strdup((*tokens)->value);
63  if (!word)
64  return (0);
65  while ((*tokens)->connected && (*tokens)->next)
66  {
67  *tokens = (*tokens)->next;
68  temp = ft_strjoin(word, (*tokens)->value);
69  free(word);
70  if (!temp)
71  return (0);
72  word = temp;
73  }
74  args[i] = word;
75  *tokens = (*tokens)->next;
76  i++;
77  }
78  return (1);
79 }
80 
88 char **collect_args(t_token **tokens, int *argc)
89 {
90  char **args;
91  int count;
92 
93  count = count_word_tokens(*tokens);
94  if (count == 0)
95  return (NULL);
96  args = ft_calloc(count + 1, sizeof(char *));
97  if (!args)
98  return (NULL);
99  if (!fill_args_array(args, tokens, count))
100  {
101  ft_free_strarray(args);
102  return (NULL);
103  }
104  *argc = count;
105  return (args);
106 }
107 
118 {
119  char **args;
120  int argc;
121 
122  if (!tokens || !*tokens)
123  return (NULL);
124  args = collect_args(tokens, &argc);
125  if (!args)
126  return (NULL);
127  return (create_cmd_node(args, argc));
128 }
t_ast * create_cmd_node(char **args, int argc)
Create a NODE_COMMAND AST node.
Definition: ast.c:23
AST node creation and destruction prototypes.
Parser entry point & sub parser prototypes.
int count_word_tokens(t_token *tokens)
Count consecutive word token groups.
Definition: parser_cmd.c:25
t_ast * parse_simple_command(t_token **tokens)
Parse a simple command from consecutive word tokens.
Definition: parser_cmd.c:117
char ** collect_args(t_token **tokens, int *argc)
Collect consecutive word tokens into an arguments array.
Definition: parser_cmd.c:88
static int fill_args_array(char **args, t_token **tokens, int count)
Fill argument array from consecutive word tokens.
Definition: parser_cmd.c:53
@ TOKEN_WORD
Definition: structs.h:25
AST node (union based ↑)
Definition: structs.h:151
Lexer token (singly linked list)
Definition: structs.h:71
t_token_type type
Token type (word, pipe, redir)
Definition: structs.h:72
struct s_token * next
Next token.
Definition: structs.h:76
int connected
1 if connected
Definition: structs.h:75