-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.c
94 lines (85 loc) · 2.4 KB
/
errors.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 11:11:50 by zhabri #+# #+# */
/* Updated: 2022/12/20 10:43:32 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int find_next_quote(const char *s, char c)
{
int i;
i = 0;
while (s[i] && s[i] != (char)c)
i++;
return (i + 1);
}
int quote_error(const char *input)
{
size_t i;
i = 0;
while (input[i])
{
if (input[i] == '"' || input[i] == '\'')
i += find_next_quote(input + i + 1, input[i]);
if (i >= ft_strlen(input))
{
ft_putstr_fd("Parse error (quote)\n", 2);
return (-1);
}
i++;
}
return (0);
}
char *op_error_trimmed(t_list *curr)
{
t_token *token;
char *trimmed;
t_token *token_next;
while (curr->next && ((t_token *)curr->next->content)->label == VARIABLE)
curr = curr->next;
token_next = NULL;
token = (t_token *)curr->content;
if (curr->next)
{
token_next = (t_token *)curr->next->content;
trimmed = ft_strtrimf(ft_substr(g_glob->input, token->str_idx \
+ ft_strlen(token->str), \
token_next->str_idx - \
(ft_strlen(token->str) + token->str_idx)), " \t");
}
else
trimmed = ft_strtrimf(ft_substr(g_glob->input, token->str_idx \
+ ft_strlen(token->str), \
ft_strlen(g_glob->input) - token->str_idx), " \t");
return (trimmed);
}
int op_error(void)
{
t_list *curr;
char *trimmed;
curr = NULL;
if (g_glob->head)
curr = *g_glob->head;
while (curr)
{
trimmed = op_error_trimmed(curr);
if (((t_token *)curr->content)->label != VARIABLE \
&& ((t_token *)curr->content)->label != PIPE)
{
if (!ft_strlen(trimmed))
{
ft_putstr_fd("Parse error\n", 2);
free(trimmed);
return (-1);
}
}
free(trimmed);
curr = curr->next;
}
return (0);
}