-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_args.c
More file actions
81 lines (75 loc) · 2.01 KB
/
make_args.c
File metadata and controls
81 lines (75 loc) · 2.01 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
78
79
80
81
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* make_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elavrich <elavrich@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/17 08:21:24 by elavrich #+# #+# */
/* Updated: 2025/07/25 23:17:52 by elavrich ### ########.fr */
/* */
/* ************************************************************************** */
#include "Minishell.h"
//cat test.txt | grep "42" | sort | wc -l (testing)
char **make_args(t_token **tokens, t_shell *shell)
{
char **cmd;
int i;
t_token *tmp;
i = 0;
if (!tokens || !*tokens)
return (NULL);
cmd = malloc(sizeof(char *) * (size_args(*tokens) + 1));
if (!cmd)
return (NULL);
tmp = *tokens;
while (tmp)
{
if (tmp->com && tmp->com[0] != '\0')
{
cmd[i] = toks_to_args(tmp, NULL, shell);
if (!cmd[i])
return (free_partial_args(cmd, i), NULL);
i++;
}
tmp = tmp->next;
}
cmd[i] = NULL;
return (cmd);
}
char *toks_to_args(t_token *tokens, char *cmd, t_shell *shell)
{
(void)shell;
free(cmd);
cmd = ft_strdup(tokens->com);
if (!cmd)
return (free(cmd), NULL);
return (cmd);
}
char *handle_dollar(char *cmd, t_shell *shell)
{
int idx;
int i;
char *prefix;
i = 0;
if (!cmd)
return (NULL);
while (cmd[i])
{
if (cmd[i] == '$')
break ;
i++;
}
prefix = ft_substr(cmd, 0, i);
idx = search_env(shell, cmd + (i + 1));
if (idx >= 0)
{
shell->env_idx = idx;
return (process_env_var(cmd, shell, prefix, i));
}
else
{
shell->env_idx = -1;
return (process_env_var(cmd, shell, prefix, i));
}
}