-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcmd_lst.c
126 lines (116 loc) · 3.28 KB
/
cmd_lst.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_lst.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/08 11:17:19 by bmachado #+# #+# */
/* Updated: 2022/02/23 15:43:36 by bmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int fix_join(int type, int next_type)
{
if (type == PIPE || type == SPACE || redir_type(type))
return (0);
else if (next_type == PIPE || next_type == SPACE || redir_type(next_type))
return (0);
return (1);
}
char **create_cmd_lst(t_token **token_lst, char **cmd, int i)
{
char *tmp;
t_token *token;
token = *token_lst;
tmp = token->value;
while (token)
{
if (token->next && fix_join(token->type, token->next->type))
join_args(&tmp, token->next->value);
else if ((token->next && is_operator(token->type)) || !token->next)
{
send_to_cmd(cmd, i++, tmp, token);
i += get_operator(token, cmd, i);
while (token->next && is_operator(token->next->type))
{
if (check_operator(token))
cmd[i++] = token->next->value;
token = token->next;
}
if (token->next)
tmp = token->next->value;
}
token = token->next;
}
return (cmd);
}
static int pipe_issue(char *input)
{
int i;
i = 0;
if (input[i] == '|')
{
printf("minishell: parse error near \'|\'\n");
g_shell.error = 130;
return (1);
}
while (input[i])
{
if (input[i] == '|' && !input[i + 1])
{
printf("minishell: parse error near \'|\'\n");
g_shell.error = 130;
return (1);
}
i++;
}
return (0);
}
static int invalid_redir(t_token **token_lst)
{
t_token *tk;
tk = *token_lst;
while (tk)
{
if (redir_type(tk->type) && (!tk->next || redir_type(tk->next->type)))
{
printf("minishell: parse error near \'\\n\'\n");
g_shell.error = 130;
return (1);
}
else if (is_operator(tk->type) && tk->next && tk->next->type == EQUAL)
{
if (tk->next->next)
printf("minishell: not found: %s\n", tk->next->next->value);
g_shell.error = 1;
return (1);
}
tk = tk->next;
}
return (0);
}
int set_cmd_lst(t_shell *table, t_token *token_lst, char *input)
{
char **cmd_lst;
int i;
i = 0;
if (pipe_issue(input))
return (0);
tokenizer(&token_lst, input);
if (token_size(token_lst) == 0 || invalid_redir(&token_lst)
|| invalid_assig(&token_lst, table))
return (free_tokenlst(&token_lst));
expand_vars(&token_lst, table);
cmd_lst = (char **)calloc(sizeof(char *), token_size(token_lst) + 1);
if (!cmd_lst)
return (0);
cmd_lst = create_cmd_lst(&token_lst, cmd_lst, i);
cmd_lst[token_size(token_lst)] = NULL;
if (!cmd_lst)
return (free_tokenlst(&token_lst));
exec_parsed_cmdlst(table, cmd_lst, &token_lst);
free(cmd_lst);
clear_token_lst(&token_lst);
return (1);
}