-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfill_list.c
69 lines (63 loc) · 2.28 KB
/
fill_list.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fill_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 10:03:28 by phelebra #+# #+# */
/* Updated: 2023/08/02 11:09:09 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define ARG_SIZE 256
void init_fill_list_vars(t_fill_list_vars *fl_vars)
{
fl_vars->cmds = malloc(sizeof(char *) * ARG_SIZE);
fl_vars->head = NULL;
fl_vars->tail = NULL;
fl_vars->curr = NONE;
fl_vars->i = 0;
fl_vars->j = 0;
}
t_parsed *add_last_command_if_not_empty(t_fill_list_vars *fl_vars)
{
if (fl_vars->j != 0)
{
fl_vars->cmds[fl_vars->j] = NULL;
fl_vars->tail = add_new_node(fl_vars->cmds, NONE, &fl_vars->head,
&fl_vars->tail);
(fl_vars->tail)->next = NULL;
}
else
free(fl_vars->cmds);
return (fl_vars->head);
}
void add_node_and_update_current_operation(t_fill_list_vars *fl_vars,
char **args)
{
fl_vars->cmds[(fl_vars->j)] = NULL;
(fl_vars->tail) = add_new_node(fl_vars->cmds, fl_vars->curr,
&fl_vars->head, &fl_vars->tail);
fl_vars->cmds = malloc(sizeof(char *) * ARG_SIZE);
fl_vars->j = 0;
if (fl_vars->curr == RED_OUT || fl_vars->curr == RED_IN
|| fl_vars->curr == RED_APP || fl_vars->curr == HEREDOC)
update_current_operation(&fl_vars->curr, args,
&fl_vars->i, fl_vars->tail);
}
t_parsed *fill_list(char **args)
{
t_fill_list_vars fl_vars;
init_fill_list_vars(&fl_vars);
while (args[fl_vars.i] != NULL)
{
fl_vars.curr = check_op(args[fl_vars.i]);
if (fl_vars.curr != NONE)
add_node_and_update_current_operation(&fl_vars, args);
else
fl_vars.cmds[fl_vars.j++] = args[fl_vars.i];
fl_vars.i++;
}
return (add_last_command_if_not_empty(&fl_vars));
}