-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_syntax.c
174 lines (148 loc) · 3.2 KB
/
error_syntax.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "shs.h"
/**
* count_char - counts the repetitions of a char
*
* @input: input string
* @i: index
* Return: repetitions
*/
int count_char(char *input, int i)
{
if (*(input - 1) == *input)
return (count_char(input - 1, i + 1));
return (i);
}
/**
* find_sy_error - finds syntax errors
*
* @input: input string
* @i: index
* @last: last char read
* Return: index of error. 0 when there are no errors
*/
int find_sy_error(char *input, int i, char last)
{
int count;
count = 0;
if (*input == '\0')
return (0);
if (*input == ' ' || *input == '\t')
return (find_sy_error(input + 1, i + 1, last));
if (*input == ';')
if (last == '|' || last == '&' || last == ';')
return (i);
if (*input == '|')
{
if (last == ';' || last == '&')
return (i);
if (last == '|')
{
count = count_char(input, 0);
if (count == 0 || count > 1)
return (i);
}
}
if (*input == '&')
{
if (last == ';' || last == '|')
return (i);
if (last == '&')
{
count = count_char(input, 0);
if (count == 0 || count > 1)
return (i);
}
}
return (find_sy_error(input + 1, i + 1, *input));
}
/**
* first_char_input - finds index of the first char
*
* @input: input string
* @i: index
* Return: 1 if there is an error. 0 in other case.
*/
int first_char_input(char *input, int *i)
{
for (*i = 0; input[*i]; *i += 1)
{
if (input[*i] == ' ' || input[*i] == '\t')
continue;
if (input[*i] == ';' || input[*i] == '|' || input[*i] == '&')
return (-1);
break;
}
return (0);
}
/**
* syntax_error_info - prints when a syntax error is found
*
* @shdata: data structure
* @input: input string
* @i: index of the error
* @bool: to control msg error
* Return: no return
*/
void syntax_error_info(shell_data *shdata, char *input, int i, int bool)
{
char *msg, *msg2, *msg3, *error, *counter;
int length;
if (input[i] == ';')
{
if (bool == 0)
msg = (input[i + 1] == ';' ? ";;" : ";");
else
msg = (input[i - 1] == ';' ? ";;" : ";");
}
if (input[i] == '|')
msg = (input[i + 1] == '|' ? "||" : "|");
if (input[i] == '&')
msg = (input[i + 1] == '&' ? "&&" : "&");
msg2 = ": Syntax error: \"";
msg3 = "\" unexpected\n";
counter = int_to_string(shdata->counter);
length = _strlen(shdata->av[0]) + _strlen(counter);
length += _strlen(msg) + _strlen(msg2) + _strlen(msg3) + 2;
error = malloc(sizeof(char) * (length + 1));
if (!error)
{
free(counter);
return;
}
_strcpy(error, shdata->av[0]);
_strcat(error, ": ");
_strcat(error, counter);
_strcat(error, msg2);
_strcat(error, msg);
_strcat(error, msg3);
_strcat(error, "\0");
write(STDERR_FILENO, error, length);
free(error);
free(counter);
}
/**
* check_syntax_error - intermediate function to find and print a syntax error
*
* @shdata: data structure
* @input: input string
* Return: 1 if there is an error. 0 in other case
*/
int check_syntax_error(shell_data *shdata, char *input)
{
int begin = 0;
int f_char = 0;
int i = 0;
f_char = first_char_input(input, &begin);
if (f_char == -1)
{
syntax_error_info(shdata, input, begin, 0);
return (1);
}
i = find_sy_error(input + begin, 0, *(input + begin));
if (i != 0)
{
syntax_error_info(shdata, input, begin + i, 1);
return (1);
}
return (0);
}