-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.c
80 lines (73 loc) · 2.37 KB
/
parser.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/19 12:38:20 by fvonsovs #+# #+# */
/* Updated: 2023/08/02 10:04:07 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// checks for heredoc and append, needed for fill_list
void update_current_operation(t_ops *curr, char **args,
int *i, t_parsed *node)
{
if ((*curr == RED_OUT && args[*i + 1] != NULL)
&& check_op(args[*i + 1]) == RED_OUT)
{
*curr = RED_APP;
node = get_outfile2(node, args, i);
}
else if ((*curr == RED_OUT && args[*i + 1] != NULL)
&& check_op(args[*i + 1]) == NONE)
node = get_outfile1(node, args, i);
else if ((*curr == RED_IN && args[*i + 1] != NULL)
&& check_op(args[*i + 1]) == RED_IN)
{
*curr = HEREDOC;
node = get_infile2(node, args, i);
}
else if ((*curr == RED_IN && args[*i + 1] != NULL)
&& check_op(args[*i + 1]) == NONE)
node = get_infile1(node, args, i);
}
// adds new node to linked list with current commands and operations
t_parsed *add_new_node(char **cmds, t_ops curr,
t_parsed **head, t_parsed **tail)
{
t_parsed *new;
new = new_parser_node(cmds, curr);
if (*head == NULL)
*head = new;
else
(*tail)->next = new;
return (new);
}
t_parsed *new_parser_node(char **args, t_ops op)
{
t_parsed *new;
new = malloc(sizeof(t_parsed));
new->args = args;
new->op = op;
new->infile = STDIN_FILENO;
new->outfile = STDOUT_FILENO;
new->next = NULL;
return (new);
}
// checks for op type
t_ops check_op(char *str)
{
if (!strcmp(str, "|"))
return (PIPE);
if (!strcmp(str, "<"))
return (RED_IN);
if (!strcmp(str, ">"))
return (RED_OUT);
if (!strcmp(str, ">>"))
return (RED_APP);
if (!strcmp(str, "<<"))
return (HEREDOC);
return (NONE);
}