-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
134 lines (124 loc) · 3.02 KB
/
Copy pathparser.c
File metadata and controls
134 lines (124 loc) · 3.02 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yucchen <yucchen@student.42singapore.sg +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/11 12:39:25 by yucchen #+# #+# */
/* Updated: 2026/01/09 14:09:12 by yucchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
// parse_one_cmd
static t_cmd *new_cmd(char **argv, t_redir *redir)
{
t_cmd *cmd;
int i;
cmd = malloc(sizeof(t_cmd));
if (!cmd)
{
i = 0;
while (argv[i])
{
free(argv[i]);
i++;
}
free(argv);
return (NULL);
}
cmd->argv = argv;
cmd->redirs = redir;
cmd->next = NULL;
return (cmd);
}
static t_cmd *parse_one_cmd(t_token **token)
{
t_strlist *args;
t_redir *redir_list;
char **argv;
t_cmd *cmd;
args = NULL;
redir_list = NULL;
while (*token && (*token)->token_type != PIPE)
{
if ((*token)->token_type == WORD)
{
if (!add_arg(&args, (*token)->value))
return (free_strlist_all(args), free_redir(redir_list), NULL);
*token = (*token)->next;
}
else if (!parse_redir(token, &redir_list))
return (free_strlist_all(args), free_redir(redir_list), NULL);
}
argv = strlist_to_argv(args);
if (!argv)
return (free_redir(redir_list), NULL);
cmd = new_cmd(argv, redir_list);
if (!cmd)
return (free_redir(redir_list), NULL);
return (cmd);
}
void free_cmd_list(t_cmd *cmd)
{
t_cmd *next;
int i;
while (cmd)
{
next = cmd->next;
if (cmd->argv)
{
i = 0;
while (cmd->argv[i])
{
free(cmd->argv[i]);
i++;
}
free(cmd->argv);
}
free_redir(cmd->redirs);
free(cmd);
cmd = next;
}
}
static void add_cmd(t_cmd **head, t_cmd **tail, t_cmd *new)
{
if (!new)
{
free_cmd_list(*head);
*head = NULL;
return ;
}
if (!(*head))
*head = new;
else
(*tail)->next = new;
*tail = new;
}
t_cmd *parse(t_token *token)
{
t_token *tok_head;
t_cmd *head;
t_cmd *tail;
tok_head = token;
head = NULL;
tail = NULL;
if (token->token_type == PIPE)
return (write(2, "minishell: pipe syntax error\n", 29),
g_last_status = 2, free_tokens(tok_head), NULL);
while (token)
{
add_cmd(&head, &tail, parse_one_cmd(&token));
if (!head)
return (free_tokens(tok_head), NULL);
if (token && token->token_type == PIPE)
{
if (!token->next || token->next->token_type == PIPE)
return (ft_putendl_fd("minishell: pipe syntax error", 2),
g_last_status = 2, free_cmd_list(head),
free_tokens(tok_head), NULL);
token = token->next;
}
}
return (free_tokens(tok_head), head);
}