-
Notifications
You must be signed in to change notification settings - Fork 29
/
check_syntax_error.c
177 lines (150 loc) · 3.21 KB
/
check_syntax_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
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
175
176
177
#include "holberton.h"
/**
* repeated_char - counts the repetitions of a char
*
* @input: input string
* @i: index
* Return: repetitions
*/
int repeated_char(char *input, int i)
{
if (*(input - 1) == *input)
return (repeated_char(input - 1, i + 1));
return (i);
}
/**
* error_sep_op - finds syntax errors
*
* @input: input string
* @i: index
* @last: last char read
* Return: index of error. 0 when there are no
* errors
*/
int error_sep_op(char *input, int i, char last)
{
int count;
count = 0;
if (*input == '\0')
return (0);
if (*input == ' ' || *input == '\t')
return (error_sep_op(input + 1, i + 1, last));
if (*input == ';')
if (last == '|' || last == '&' || last == ';')
return (i);
if (*input == '|')
{
if (last == ';' || last == '&')
return (i);
if (last == '|')
{
count = repeated_char(input, 0);
if (count == 0 || count > 1)
return (i);
}
}
if (*input == '&')
{
if (last == ';' || last == '|')
return (i);
if (last == '&')
{
count = repeated_char(input, 0);
if (count == 0 || count > 1)
return (i);
}
}
return (error_sep_op(input + 1, i + 1, *input));
}
/**
* first_char - 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(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);
}
/**
* print_syntax_error - prints when a syntax error is found
*
* @datash: data structure
* @input: input string
* @i: index of the error
* @bool: to control msg error
* Return: no return
*/
void print_syntax_error(data_shell *datash, 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 = aux_itoa(datash->counter);
length = _strlen(datash->av[0]) + _strlen(counter);
length += _strlen(msg) + _strlen(msg2) + _strlen(msg3) + 2;
error = malloc(sizeof(char) * (length + 1));
if (error == 0)
{
free(counter);
return;
}
_strcpy(error, datash->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
*
* @datash: data structure
* @input: input string
* Return: 1 if there is an error. 0 in other case
*/
int check_syntax_error(data_shell *datash, char *input)
{
int begin = 0;
int f_char = 0;
int i = 0;
f_char = first_char(input, &begin);
if (f_char == -1)
{
print_syntax_error(datash, input, begin, 0);
return (1);
}
i = error_sep_op(input + begin, 0, *(input + begin));
if (i != 0)
{
print_syntax_error(datash, input, begin + i, 1);
return (1);
}
return (0);
}