blob: da43503b5ebecae647aa0869a0f64e11d284b9c3 (
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collect_assigns.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:54:36 by dkaiser #+# #+# */
/* Updated: 2024/08/02 14:37:41 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "token.h"
static t_assign *to_assign(char *str);
static int count_tokens(t_token *tokens);
static int is_quote(char c);
t_assign **collect_assigns(t_token **tokens)
{
t_token *cur;
t_assign **result;
int i;
result = malloc(sizeof(t_assign *) * (count_tokens(*tokens) + 1));
if (result == NULL)
return (free_tokens(*tokens), NULL);
cur = *tokens;
i = 0;
while (cur != NULL && cur->type == STRING_TOKEN
&& !is_quote(cur->content.string[0]) && ft_strchr(cur->content.string,
'=') != NULL)
{
result[i++] = to_assign(cur->content.string);
if (cur->next != NULL)
{
cur = cur->next;
free_token(cur->previous);
}
else
free_token(cur);
}
*tokens = cur;
result[i] = NULL;
return (result);
}
static t_assign *to_assign(char *str)
{
t_assign *result;
char *split_pos;
split_pos = ft_strchr(str, '=');
*split_pos = '\0';
result = malloc(sizeof(t_assign));
if (result == NULL)
{
return (NULL);
}
result->var = str;
result->value = split_pos + 1;
return (result);
}
static int count_tokens(t_token *tokens)
{
int len;
len = 0;
while (tokens != NULL && tokens->type == STRING_TOKEN
&& !is_quote(tokens->content.string[0])
&& ft_strchr(tokens->content.string, '=') != NULL)
{
len++;
tokens = tokens->next;
}
return (len);
}
static int is_quote(char c)
{
return (c == '"' || c == '\'');
}
|