-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_cmd.c
More file actions
78 lines (70 loc) · 2.32 KB
/
Copy pathparse_cmd.c
File metadata and controls
78 lines (70 loc) · 2.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpalomin <lpalomin@student.42malaga.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/28 17:47:24 by lpalomin #+# #+# */
/* Updated: 2025/08/28 17:47:48 by lpalomin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void handle_double_redir(t_cmd *cmd, char *line, char *new_line, int *i)
{
new_line[0] = line[*i];
new_line[1] = line[*i + 1];
new_line[2] = '\0';
cmd->argv[cmd->amount_cmd++] = ft_strdup(new_line);
(*i)++;
}
static void handle_single_redir(t_cmd *cmd, char *line, char *new_line, int i)
{
new_line[0] = line[i];
new_line[1] = '\0';
cmd->argv[cmd->amount_cmd++] = ft_strdup(new_line);
}
static void handle_operator(t_cmd *cmd, char *line, char *new_line, int *i)
{
if ((line[*i] == '<' && line[*i + 1] == '<')
|| (line[*i] == '>' && line[*i + 1] == '>'))
handle_double_redir(cmd, line, new_line, i);
else
handle_single_redir(cmd, line, new_line, *i);
}
static void parse_loop(t_cmd *cmd, char *line,
char *new_line, int *status)
{
int count1;
int count2;
count1 = 0;
count2 = 0;
while (line[count1])
{
modify_status(line[count1], status);
if ((line[count1] == ' ' || line[count1] == '|'
|| line[count1] == '<' || line[count1] == '>')
&& *status == 0)
{
add_to_cmd(cmd, new_line, &count2);
if (line[count1] != ' ')
handle_operator(cmd, line, new_line, &count1);
}
else
new_line[count2++] = line[count1];
count1++;
}
if (count2 > 0)
add_to_cmd(cmd, new_line, &count2);
}
void parse_line(t_cmd *cmd, char *line)
{
int status;
char *new_line;
status = 0;
new_line = malloc(ft_strlen(line) + 1);
if (!new_line)
return ;
parse_loop(cmd, line, new_line, &status);
free(new_line);
}