-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_cmd.c
130 lines (119 loc) · 3.37 KB
/
find_cmd.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
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* find_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/09 14:14:40 by zhabri #+# #+# */
/* Updated: 2023/01/09 14:23:41 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static bool str_is_op(char *needle)
{
int i;
static const char *op_tab[6] = {"<<", ">>", "|", \
">", "<", NULL};
i = 0;
while (op_tab[i])
{
if (!strncmp(op_tab[i], needle, ft_strlen(op_tab[i])))
return (true);
i++;
}
return (false);
}
void find_cmd(t_list **cmds)
{
char *trimmed;
t_cmd *node;
int i;
i = 0;
trimmed = ft_strtrim(g_glob->input, " \t");
node = init_cmd_token(0, 1, NULL, true);
if (trimmed && trimmed[0])
{
while (trimmed[i] && !str_is_op(trimmed + i))
i += skip_if_quotes(trimmed, i);
node->str = ft_substr(trimmed, 0, i);
}
ft_lstadd_back(cmds, ft_lstnew(node));
free(trimmed);
}
void find_cmd_infile(t_token *token, t_cmd *node, bool *pb)
{
int i;
char *cmd;
char *file;
i = 0;
while (ft_isprint_nospace(token->arg[i]))
{
if (token->arg[i] == '"')
i += find_quote(token->arg + i + 1, '"');
else if (token->arg[i] == '\'')
i += find_quote(token->arg + i + 1, '\'');
i++;
}
file = ft_substr(token->arg, 0, i);
cmd = ft_strtrimf(ft_strdup(token->arg + i), " \t");
node->str = ft_strtrimf(ft_strjoinf(ft_strjoinf(node->str, " "), cmd), \
" \t");
free(cmd);
if (token->label != HEREDOC)
token->file = remove_quotes(file);
else
free(file);
*pb = false;
if (!g_glob->sig_int)
*pb = ft_open_in(node, token);
}
void find_cmd_outfile(t_token *token, t_cmd *node, bool *pb)
{
int i;
char *cmd;
char *file;
i = 0;
while (ft_isprint_nospace(token->arg[i]))
{
if (token->arg[i] == '"')
i += find_quote(token->arg + i + 1, '"');
else if (token->arg[i] == '\'')
i += find_quote(token->arg + i + 1, '\'');
i++;
}
file = ft_substr(token->arg, 0, i);
cmd = ft_strtrimf(ft_strdup(token->arg + i), " \t");
node->str = ft_strtrimf(ft_strjoinf(ft_strjoinf(node->str, " "), cmd), \
" \t");
free(cmd);
token->file = remove_quotes(file);
if (!g_glob->sig_int)
*pb = ft_open_out(node, token);
else
*pb = false;
}
void get_cmd(void)
{
t_list *curr;
t_token *token;
t_list **cmds;
bool pb;
pb = false;
curr = *g_glob->head;
cmds = malloc(sizeof(t_list *));
*cmds = NULL;
find_cmd(cmds);
while (curr)
{
token = (t_token *)curr->content;
if (!pb && (token->label == INFILE || token->label == HEREDOC))
find_cmd_infile(token, ft_lstlast(*cmds)->content, &pb);
else if (!pb && (token->label == OUTFILE || token->label == OUTFILE_A))
find_cmd_outfile(token, ft_lstlast(*cmds)->content, &pb);
else if (token->label == PIPE)
add_cmd(token, cmds, &pb);
curr = curr->next;
}
g_glob->cmds = cmds;
}