-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummerize.c
executable file
·113 lines (104 loc) · 2.31 KB
/
summerize.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* summerize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vpetrosy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/03 21:13:41 by zkarapet #+# #+# */
/* Updated: 2023/03/02 21:16:35 by aivanyan ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_substr_m(char *s, int start, int end)
{
char *dst;
int i;
if (!s)
return (0);
i = 0;
dst = malloc(sizeof(char) * (end - start + 1));
while (start < end)
{
dst[i] = s[start];
start++;
i++;
}
dst[i] = '\0';
return (dst);
}
int find_last_quote(char *s, char quote)
{
int i;
i = 0;
while (s[++i])
if (s[i] == quote)
return (i);
return (0);
}
int more_pipes(char *s)
{
if (*s)
{
if (*(s) == '|' || *s == '\0')
{
ft_putstr("parse error near '|'\n");
return (1);
}
while (*s && *s == ' ')
s++;
if (*s == '|' || *s == '\0')
{
ft_putstr("parse error near '|'\n");
return (1);
}
}
return (0);
}
int pipe_check(char *s, t_list *group, int i, int f)
{
if (f == 0)
{
if (more_pipes(&s[i + 1]))
{
lst_destruct(&group);
return (1);
}
}
else if (f)
{
if (s && s[0] == '|')
{
free(group);
ft_putstr("parse error near '|'\n");
return (1);
}
}
return (0);
}
t_list *group_until_pipe(char *s)
{
int i;
int start;
t_list *group;
i = 0;
start = 0;
group = lst_construct();
if (pipe_check(s, group, i, 1))
return (NULL);
while (s && s[i])
{
if (s[i] == '"' || s[i] == '\'')
i += find_last_quote(&s[i], s[i]);
else if (s[i] == '|')
{
if (pipe_check(s, group, i, 0))
return (NULL);
lst_add_last(group, ft_substr_m(s, start, i));
start = i + 1;
}
i++;
}
lst_add_last(group, ft_substr_m(s, start, i));
return (group);
}