-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_args.c
88 lines (80 loc) · 2.48 KB
/
parse_args.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aoizel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 11:12:06 by aoizel #+# #+# */
/* Updated: 2024/01/23 11:12:33 by aoizel ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *find_cmd(t_wordlst *wordlst, t_shell *shell)
{
if (wordlst->type >= BUILTIN || ft_strchr(wordlst->word, '/'))
return (ft_strdup(wordlst->word));
else
return (retrieve_absolute_path(shell->envlst, wordlst->word));
return (NULL);
}
int args_size(t_wordlst *wordlst)
{
int args_size;
args_size = 0;
while (wordlst && wordlst->type != PIPE)
{
if (wordlst->type == ARG || wordlst->type == CMD
|| wordlst->type >= BUILTIN)
args_size++;
wordlst = wordlst->next;
}
return (args_size);
}
int input_args(t_wordlst *wordlst, t_processlst *process,
t_shell *shell, int *i)
{
if (wordlst->type == CMD || wordlst->type >= BUILTIN)
{
process->builtin_index = wordlst->type - BUILTIN;
process->argv[0] = find_cmd(wordlst, shell);
if (!process->argv[0])
return (perror("retrieve path"), -1);
}
if (wordlst->type == ARG)
{
process->argv[*i] = ft_strdup(wordlst->word);
if (!process->argv[*i])
return (perror("retrieve arg"), -1);
(*i)++;
}
return (0);
}
int empty_process(t_processlst *process)
{
process->argv = NULL;
process->builtin_index = 7;
process->status = 0;
return (0);
}
int get_args(t_wordlst *wordlst, t_processlst *process, t_shell *shell)
{
int i;
i = 1;
if (args_size(wordlst) == 0)
return (empty_process(process));
process->argv = ft_calloc(args_size(wordlst) + 1, sizeof(char *));
while (wordlst && wordlst->type != PIPE)
{
if (input_args(wordlst, process, shell, &i) == -1)
{
while (--i >= 0)
free(process->argv[i]);
free(process->argv);
return (-1);
}
wordlst = wordlst->next;
}
process->argv[i] = NULL;
return (0);
}