-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
113 lines (105 loc) · 2.55 KB
/
error.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ofadhel <ofadhel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 23:53:18 by ofadhel #+# #+# */
/* Updated: 2024/01/14 16:53:36 by ofadhel ### ########.fr */
/* */
/* ************************************************************************** */
#include "include/minishell.h"
int check_inside_words(char *cmd, int i, t_lexer *lexer)
{
int j;
j = i + 1;
i++;
while (cmd[i] && cmd[i] != '|')
i++;
while (cmd[j] && cmd[j] != '|')
{
if (j < i)
{
if (ft_isalnum(cmd[j]) != 0)
return (0);
}
j++;
}
lexer->flag = 3;
return (-1);
}
int check_error2(char *cmd, t_lexer *lexer, int i)
{
if ((cmd[i] == '>' && cmd[i + 1] == '>')
|| (cmd[i] == '<' && cmd[i + 1] == '<'))
{
if (check_inside_words(cmd, i + 1, lexer) == -1)
return (-1);
}
else if (cmd[i] == '>' || cmd[i] == '<')
{
if (check_inside_words(cmd, i, lexer) == -1)
return (-1);
}
else
return (0);
return (1);
}
int check_only_spaces(char *cmd, int i, t_lexer *lexer)
{
if (cmd[i] == ' ')
{
while (cmd[i] == ' ' && cmd[i] != '\0' && cmd[i] != '|')
{
i++;
if (cmd[i] == '\0' || cmd[i] == '|')
{
lexer->flag = 5;
return (-1);
}
}
}
return (0);
}
int check_error(char *cmd, t_lexer *lexer)
{
int i;
int flag;
i = 0;
flag = 0;
flag = check_only_spaces(cmd, i, lexer);
while (cmd[i])
{
flag = check_error2(cmd, lexer, i);
if (flag == -1)
break ;
else if (flag == 1)
;
else if (cmd[i] == '|')
{
if (check_inside_words(cmd, i, lexer) == -1)
{
lexer->flag = 4;
return (-1);
}
}
i++;
}
return (lexer->flag);
}
int ft_error(int flag)
{
if (flag == 1)
printf("Error: unclosed quotes\n");
if (flag == 2)
printf("Error: No such file or directory\n");
if (flag == 3)
printf("Error: expecting file name after redirection\n");
if (flag == 4)
printf("Error: syntax error near unexpected token\n");
if (flag == 5)
printf("Error: empty command line\n");
g_exit_status = 2;
return (0);
}