-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokenization_utils.c
82 lines (76 loc) · 2.22 KB
/
tokenization_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tokenization_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: csweetin <csweetin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 15:42:21 by csweetin #+# #+# */
/* Updated: 2024/05/17 14:55:53 by csweetin ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
int check_quote(char *line)
{
size_t i;
size_t quote;
char quotetype;
i = 0;
quote = 0;
quotetype = 0;
while (line[i])
{
if (line[i] == '"' || line[i] == '\'')
{
quotetype = line[i++];
quote++;
while (line[i] && line[i] != quotetype)
i++;
if (line[i] && line[i] == quotetype)
quote++;
}
if (line[i])
i++;
}
if ((quote % 2) != 0)
return (1);
return (0);
}
static int syntax_error(char *line, size_t *i)
{
if (line[*i] == '\0')
print_error(0, "syntax error near unexpected token 'newline'");
else if (line[*i] == '>' || line[*i] == '<' || line[*i] == '|')
{
if (ft_putstr_fd("\033[1;31m", STDERR_FILENO) == -1)
return (1);
if (ft_putstr_fd("minishell : syntax error near unexpected token ", 2) \
== -1)
return (1);
if (ft_putchar_fd(line[*i], STDERR_FILENO) == -1)
return (1);
if (ft_putendl_fd("\033[0m", STDERR_FILENO) == -1)
return (1);
}
return (2);
}
char **get_data(t_input **input, char *line, size_t *i, int *rv)
{
char **data;
data = NULL;
while (line[*i] && ((line[*i] >= '\t' && line[*i] <= '\r')
|| line[*i] == ' '))
*i += 1;
if (line[*i] && line[*i] != '<' && line[*i] != '>' && line[*i] != '|')
{
data = build_tab(line, i, 1);
if (!data)
input_freelst(input);
}
else
{
input_freelst(input);
*rv = syntax_error(line, i);
}
return (data);
}