-
Notifications
You must be signed in to change notification settings - Fork 0
/
pelicaseaspas.c
109 lines (100 loc) · 2.31 KB
/
pelicaseaspas.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pelicaseaspas.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 19:29:10 by fvieira #+# #+# */
/* Updated: 2023/02/27 21:56:32 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char **sep_init(char *s)
{
char **sep;
int i;
int c;
int j;
c = -1;
j = 0;
sep = malloc((count_words(s, "><|") + 1) * sizeof(char *));
while (s[++c])
{
i = 2;
if (c > 2 && (s[c - 1] != '>' && s[c - 1] != '<' && s[c - 1] != '|')
&& (s[c] == '>' || s[c] == '<' || s[c] == '|') && between(s, c))
{
while (s[c + i - 1] == s[c])
i++;
sep[j] = ft_calloc(i, sizeof(char));
sep[j][0] = s[c];
sep[j++][i - 1] = '\0';
if (s[c + 1] == s[c])
sep[j - 1][1] = s[++c];
}
}
sep[j] = 0;
return (sep);
}
int between(const char *str, int pos)
{
int temp;
int flag;
int c;
temp = -1;
flag = 1;
c = -1;
while (str[++c] && c <= pos)
{
if (temp >= 0 && str[c] == str[temp])
{
temp = -1;
flag = 1;
continue ;
}
if ((str[c] == 39 || str[c] == '"') && temp == -1)
{
temp = c;
flag = 0;
}
}
if (flag == 1)
return (1);
return (0);
}
int correctorder(char **sep, int i)
{
int c;
int counter;
c = 0;
counter = 0;
if (!sep[c] || !sep[i])
return (999);
while (sep[c])
{
if (sep[i][0] > sep[c][0])
counter++;
if (sep[i][0] == sep[c][0] && i > c)
counter++;
c++;
}
return (counter);
}
int *order(t_prompt *every)
{
int *order;
int i;
int count;
count = (count_words(every->prompt, "><|"));
if (count == 0)
count++;
order = malloc(count * sizeof(int));
i = 0;
while (i < count)
{
order[i] = correctorder(every->sep, i);
i++;
}
return (order);
}