blob: 739c02219e7946efc7b3d66535a553ac935bd0d2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
/* Updated: 2024/07/08 17:29:22 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "minishell.h"
#include "token.h"
#include <stdlib.h>
#include <string.h>
static t_redirection **collect_redirs(t_token **tokens);
static t_assign **collect_assigns(t_token **tokens);
static char **collect_args(t_token **tokens);
static t_assign *to_assign(char *str);
t_node *parse_cmd(t_token *tokens)
{
char **args;
t_assign **assigns;
t_redirection redirs[2];
redirs = collect_redirs(&tokens);
assigns = collect_assigns(&tokens);
args = collect_args(&tokens);
return (new_cmd_node(args, assigns, redirs));
}
static t_redirection **collect_redirs(t_token **tokens)
{
return (NULL);
}
static t_assign **collect_assigns(t_token **tokens)
{
int i;
t_assign **result;
i = 0;
while (ft_strchr(tokens[i]->content.string, '=') != NULL)
{
i++;
}
result = malloc(sizeof(t_assign *) * (i + 1));
if (result == NULL)
{
// free everything
return (NULL);
}
result[i] = NULL;
i--;
while (i >= 0)
{
result[i] = to_assign(tokens[i]->content.string);
i--;
}
return (result);
}
static t_assign *to_assign(char *str)
{
t_assign *result;
char *split;
result = malloc(sizeof(t_assign));
if (result == NULL)
return (NULL);
split = ft_strchr(str, '=');
if (split == NULL)
return (NULL);
*split = '\0';
result->var = str;
result->value = split + 1;
return (result);
}
static char **collect_args(t_token **tokens)
{
t_token *cur;
char **result;
int i;
cur = *tokens;
i = 0;
while (cur != NULL) {
i++;
cur = cur->next;
}
result = malloc(sizeof(char*) * (i + 1));
if (!result)
{
//free all tokens;
return (NULL);
}
cur = *tokens;
i = 0;
while(cur != NULL)
{
result[i] = cur->content.string;
// free token
i++;
cur = cur->next;
}
return (result);
}
|