-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_expand.c
More file actions
77 lines (70 loc) · 2.17 KB
/
parser_expand.c
File metadata and controls
77 lines (70 loc) · 2.17 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser_expand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <almelo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 16:40:36 by almelo #+# #+# */
/* Updated: 2023/03/31 15:44:32 by almelo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
size_t count_keys(char *content)
{
size_t len;
size_t i;
t_parser_state state;
state.prevent_default = FALSE;
state.prevent_expand = FALSE;
len = 0;
i = 0;
while (content[i])
{
if (is_quote(content[i]))
update_parser_state(content, i, &state);
else if ((content[i] == '$')
&& ((state.prevent_expand == FALSE)
|| ((state.prevent_default && state.prevent_expand == TRUE)
&& content[0] == '\"')))
len++;
i++;
}
return (len);
}
void handle_key(char *content, char **keys, t_index *i, enum e_bool *is_key)
{
if (*is_key == FALSE)
*is_key = TRUE;
else if (*is_key == TRUE)
{
keys[i->key] = ft_substr(content, i->start, i->old - i->start);
i->key++;
}
i->start = i->old + 1;
}
void save_switch(char *content, char **key, t_index *i, enum e_bool *is_key)
{
key[i->key] = ft_substr(content, i->start, i->old - i->start);
i->key++;
*is_key = FALSE;
}
void expand_variable(t_envl *env_lst, char *key, char *new, t_index *i)
{
t_env *tmp;
tmp = get_env(env_lst, key);
if (tmp)
{
ft_memcpy(&new[i->new], tmp->value, ft_strlen(tmp->value) + 1);
i->old += ft_strlen(tmp->key) + 1;
i->new += ft_strlen(tmp->value);
}
else
{
ft_memcpy(&new[i->new], "$", 1 + 1);
i->old++;
i->new++;
ft_memcpy(&new[i->new], key, ft_strlen(key) + 1);
}
i->key++;
}