-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorHandling.cpp
496 lines (428 loc) · 18.8 KB
/
ErrorHandling.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include "ErrorHandling.h"
#include "Command.h"
#include <sstream>
#include <iostream>
#include <algorithm>
#include <regex>
bool ErrorHandling::validateCommand(Parser::ParsedCommand parsedCommand) {
try {
auto it = validators.find(parsedCommand.commandName);
if (it == validators.end()) {
throw std::runtime_error("Unknown command: " + parsedCommand.commandName);
}
it->second(parsedCommand);
}
catch (std::exception e) {
std::cerr << "Error - " << e.what() << std::endl;
return false;
}
return true;
}
void ErrorHandling::validateEcho(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: echo [argument]");
}
}
void ErrorHandling::validatePrompt(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: prompt argument");
}
if (parsedCommand.streams.size() != 0) {
throw std::runtime_error("The command does not take a redirection");
}
}
void ErrorHandling::validateTime(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: time");
}
if (std::any_of(parsedCommand.streams.begin(), parsedCommand.streams.end(), [](Redirection stream) { return stream.type == Redirection::Input; })) {
throw std::runtime_error("The command does not take an input redirection");
}
if (!parsedCommand.commandArg.empty()) {
throw std::runtime_error("The command does not take an argument. Its format is: time");
}
}
void ErrorHandling::validateDate(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: date");
}
if (std::any_of(parsedCommand.streams.begin(), parsedCommand.streams.end(), [](Redirection stream) { return stream.type == Redirection::Input; })) {
throw std::runtime_error("The command does not take an input redirection");
}
if (!parsedCommand.commandArg.empty()) {
throw std::runtime_error("The command does not take an argument. Its format is: date");
}
}
void ErrorHandling::validateTouch(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: touch filename");
}
if (std::any_of(parsedCommand.streams.begin(), parsedCommand.streams.end(), [](Redirection stream) { return (stream.type == Redirection::Output || stream.type == Redirection::Append); })) {
throw std::runtime_error("The command does not take an output redirection");
}
}
void ErrorHandling::validateTruncate(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: truncate filename");
}
if (std::any_of(parsedCommand.streams.begin(), parsedCommand.streams.end(), [](Redirection stream) { return (stream.type == Redirection::Output || stream.type == Redirection::Append); })) {
throw std::runtime_error("The command does not take an output redirection");
}
}
void ErrorHandling::validateRm(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: rm filename");
}
if (std::any_of(parsedCommand.streams.begin(), parsedCommand.streams.end(), [](Redirection stream) { return (stream.type == Redirection::Output || stream.type == Redirection::Append); })) {
throw std::runtime_error("The command does not take an output redirection");
}
}
void ErrorHandling::validateWc(Parser::ParsedCommand parsedCommand) {
if (parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command takes an option. Its format is: wc -opt [argument]");
}
if (parsedCommand.commandOpt != "-c" && parsedCommand.commandOpt != "-w") {
throw std::runtime_error("Incorrect option. Available options are: -c and -w");
}
}
void ErrorHandling::validateTr(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: tr [argument] what [with]");
}
}
void ErrorHandling::validateHead(Parser::ParsedCommand parsedCommand) {
if (parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command takes an option. Its format is: head -ncount [argument]");
}
if (parsedCommand.commandOpt.find("-n", 0) != 0) {
throw std::runtime_error("Incorrect option. Available option is: -ncount");
}
else {
int n = stoi(parsedCommand.commandOpt.substr(2, parsedCommand.commandOpt.length() - 2));
if (n <= 0 || n >= 100000) {
throw std::runtime_error("n count is either smaller than 1 or bigger than 99999");
}
}
}
void ErrorHandling::validateBatch(Parser::ParsedCommand parsedCommand) {
if (!parsedCommand.commandOpt.empty()) {
throw std::runtime_error("The command does not take an option. Its format is: batch [argument]");
}
}
bool ErrorHandling::catchErrors(std::string commandLine, Parser::ParsedCommand command) {
int lineLength = commandLine.length();
char* mistakes = new char[lineLength + 1];
for (int i = 0; i < lineLength; i++) {
mistakes[i] = ' ';
}
int index = 0;
try {
skipWhiteSpace(commandLine, index);
validateCommandName(commandLine, command.commandName, mistakes, index);
skipWhiteSpace(commandLine, index);
validateCommandOption(commandLine, command.commandOpt, mistakes, index);
skipWhiteSpace(commandLine, index);
if (command.commandName == "tr") {
validateCommandArgumentTr(commandLine, command.commandArg, mistakes, index);
}
else if (command.commandName == "batch") {
validateCommandArgumentBatch(commandLine, command.commandArg, mistakes, index);
}
else {
validateCommandArgument(commandLine, command.commandArg, mistakes, index);
}
skipWhiteSpace(commandLine, index);
validateStreams(commandLine, command.streams, mistakes, index);
for (int i = index; i < lineLength; i++) {
mistakes[i] = '^';
throw std::runtime_error("Unexpected characters at the end of the command");
}
if (!command.commandArg.empty() &&
(std::any_of(command.streams.begin(), command.streams.end(), [&command](Redirection stream) { return stream.file != command.commandArg; })) &&
(std::any_of(command.streams.begin(), command.streams.end(), [](Redirection stream) { return stream.type == Redirection::Input; }))) {
throw std::runtime_error("The command can only take one input redirection");
}
}
catch (std::exception e) {
std::cerr << "Error - " << e.what();
if (!std::all_of(mistakes, mistakes + lineLength, [](char c) { return c == ' '; })) {
std::cerr << ":" << std::endl << commandLine << std::endl;
for (int i = 0; i < lineLength; i++) {
std::cerr << mistakes[i];
}
}
std::cerr << std::endl;
delete[] mistakes;
return true;
}
delete[] mistakes;
return false;
}
bool ErrorHandling::catchPipeErrors(std::string commandLine, std::vector<Parser::ParsedCommand> commands) {
int lineLength = commandLine.length();
char* mistakes = new char[lineLength + 1];
for (int i = 0; i < lineLength; i++) {
mistakes[i] = ' ';
}
int index = 0;
try {
for (int i = 0; i < commands.size(); i++) {
skipWhiteSpace(commandLine, index);
validateCommandName(commandLine, commands[i].commandName, mistakes, index);
skipWhiteSpace(commandLine, index);
validateCommandOption(commandLine, commands[i].commandOpt, mistakes, index);
skipWhiteSpace(commandLine, index);
if (commands[i].commandName.find("tr") != std::string::npos) {
validateCommandArgumentTr(commandLine, commands[i].commandArg, mistakes, index);
}
else if (commands[i].commandName.find("batch") != std::string::npos) {
validateCommandArgumentBatch(commandLine, commands[i].commandArg, mistakes, index);
}
else {
validateCommandArgument(commandLine, commands[i].commandArg, mistakes, index);
}
skipWhiteSpace(commandLine, index);
validateStreams(commandLine, commands[i].streams, mistakes, index);
skipWhiteSpace(commandLine, index);
index++;
if (i == 0) {
if (std::any_of(commands[i].streams.begin(), commands[i].streams.end(), [](Redirection stream) { return (stream.type == Redirection::Output || stream.type == Redirection::Append); })) {
throw std::runtime_error("First command in the pipeline does not accept output redirection");
}
}
else if (i == commands.size() - 1) {
if (!commands[i].commandArg.empty() || std::any_of(commands[i].streams.begin(), commands[i].streams.end(), [](Redirection stream) { return stream.type == Redirection::Input; })) {
std::regex pattern(R"(^"\S+" "\S+"$)");
if (commands[i].commandName == "tr" && !std::regex_match(commands[i].commandArg, pattern)) {
throw std::runtime_error("Last command in the pipeline does not accept argument or input redirection");
}
}
}
else {
if (!commands[i].commandArg.empty() || !commands[i].streams.empty()) {
std::regex pattern(R"(^"\S+" "\S+"$)");
if (commands[i].commandName == "tr" && !std::regex_match(commands[i].commandArg, pattern)) {
throw std::runtime_error("Commands between first and last in the pipeline do not accept argument or redirections");
}
}
}
}
}
catch (std::exception e) {
std::cerr << "Error - " << e.what();
if (!std::all_of(mistakes, mistakes + lineLength, [](char c) { return c == ' '; })) {
std::cerr << ":" << std::endl << commandLine << std::endl;
for (int i = 0; i < lineLength; i++) {
std::cerr << mistakes[i];
}
}
std::cerr << std::endl;
delete[] mistakes;
return true;
}
delete[] mistakes;
return false;
}
void ErrorHandling::skipWhiteSpace(std::string commandLine, int& index) {
while (index < commandLine.length() && (std::isspace(commandLine[index]) || commandLine[index] == '\t')) {
index++;
}
}
void ErrorHandling::validateCommandName(std::string commandLine, std::string name, char* mistakes, int& index) {
for (int i = 0; i < name.length(); i++) {
if (index < commandLine.length() && !std::isalpha(name[i])) {
mistakes[index] = '^';
throw std::runtime_error("Invalid command name");
}
index++;
}
}
void ErrorHandling::validateCommandOption(std::string commandLine, std::string option, char* mistakes, int& index) {
if (!option.empty()) {
if (index < commandLine.length() && commandLine[index] == '-') {
index++;
for (int i = 1; i < option.length(); i++) {
if (index < commandLine.length() && !std::isalpha(option[i]) && !std::isdigit(option[i])) {
mistakes[index] = '^';
throw std::runtime_error("Invalid character in command option");
}
index++;
}
}
else if (index < commandLine.length()) {
mistakes[index] = '^';
throw std::runtime_error("Missing dash for command option");
index++;
}
}
}
void ErrorHandling::validateCommandArgument(std::string commandLine, std::string argument, char* mistakes, int& index) {
if (!argument.empty() && commandLine[index] != '<' && commandLine[index] != '>' && index < commandLine.length()) {
int firstQuote = commandLine.find('"', index);
int secondQuote = commandLine.find('"', firstQuote + 1);
if (firstQuote != std::string::npos && secondQuote != std::string::npos) {
if (firstQuote > index || secondQuote + 1 < commandLine.length()) {
for (int i = index; i < firstQuote; i++) {
if (!std::isspace(commandLine[i]) && commandLine[i] != '\t') {
mistakes[i] = '^';
throw std::runtime_error("Unexpected character before quoted argument");
}
}
for (int i = secondQuote + 1; i < commandLine.length(); i++) {
if (commandLine[i] == '>' || commandLine[i] == '<' || commandLine[i] == '|') {
break;
}
if (!std::isspace(commandLine[i]) && commandLine[i] != '\t') {
mistakes[i] = '^';
throw std::runtime_error("Unexpected character after quoted argument");
}
}
}
}
else {
if (argument.find(' ') != std::string::npos) {
for (int i = 0; i < argument.length(); i++) {
if (std::isspace(argument[i])) {
mistakes[index + i] = '^';
throw std::runtime_error("Spaces are not allowed in the filename");
}
}
}
}
index += argument.length();
}
}
void ErrorHandling::validateCommandArgumentTr(std::string commandLine, std::string argument, char* mistakes, int& index) {
if (commandLine[index] == '"') {
index++;
for (int i = 1; i < argument.length(); i++) {
if (argument[i] != '"') {
index++;
}
else {
index++;
break;
}
}
}
else if (std::isalnum(commandLine[index])) {
std::string filename;
int tempIndex = -1, quote = commandLine.find('"', index), firstCharOfArg = index, lastCharOfArg;
for (int i = 0; i < argument.length(); i++) {
if (std::isspace(argument[i])) {
tempIndex = index;
index++;
break;
}
filename += argument[i];
index++;
}
lastCharOfArg = index;
if (commandLine[index] != '"') {
mistakes[tempIndex] = '^';
throw std::runtime_error("Unexpected character in argument");
}
if (quote > firstCharOfArg && quote < lastCharOfArg) {
mistakes[firstCharOfArg] = '^';
throw std::runtime_error("Unexpected character in argument");
}
}
else {
mistakes[index] = '^';
throw std::runtime_error("Unexpected character in argument");
}
skipWhiteSpace(commandLine, index);
for (int i = 0; i < 2; i++) {
if (commandLine[index] == '"' && index < commandLine.length()) {
index++;
for (int j = 1; j < argument.length(); j++) {
if (commandLine[index] != '"') {
index++;
}
else {
index++;
break;
}
}
}
else if (commandLine[index] == '|' && index < commandLine.length()) {
break;
}
else if (index < commandLine.length()) {
mistakes[index] = '^';
throw std::runtime_error("Unexpected character");
}
skipWhiteSpace(commandLine, index);
}
}
void ErrorHandling::validateCommandArgumentBatch(std::string commandLine, std::string argument, char* mistakes, int& index) {
int firstQuote = argument.find('"');
int lastQuote = argument.rfind('"');
if (firstQuote != std::string::npos) {
for (int i = 0; i < firstQuote; i++) {
if (!std::isspace(argument[i]) && argument[i] != '\t') {
mistakes[index + i] = '^';
throw std::runtime_error("Unexpected character before quoted argument");
}
}
for (int i = lastQuote + 1; i < argument.size(); i++) {
if (!std::isspace(argument[i]) && argument[i] != '\t') {
mistakes[index + i] = '^';
throw std::runtime_error("Unexpected character after quoted argument");
}
}
}
else {
if (argument.find(' ') != std::string::npos) {
for (int i = 0; i < argument.size(); i++) {
if (argument[i] == ' ') {
mistakes[index + i] = '^';
throw std::runtime_error("Spaces are not allowed in the filename");
}
}
}
}
index += argument.size();
}
void ErrorHandling::validateStreams(std::string commandLine, std::vector<Redirection> streams, char* mistakes, int& index) {
if (!streams.empty()) {
std::string streamFile1, streamFile2;
if (streams.size() == 1) {
streamFile1 = streams[0].file;
}
else if (streams.size() == 2) {
streamFile2 = streams[1].file;
}
while (index < commandLine.length()) {
if (commandLine[index] == '>' || commandLine[index] == '<') {
char redirectionType = commandLine[index];
index++;
if (redirectionType == '>' && index < commandLine.length() && commandLine[index] == '>') {
index++;
}
skipWhiteSpace(commandLine, index);
std::string fileName;
if (redirectionType == '>') {
fileName = (!streamFile1.empty() ? streamFile1 : streamFile2);
}
else if (redirectionType == '<') {
fileName = (!streamFile2.empty() ? streamFile2 : streamFile1);
}
if (fileName.empty()) {
continue;
}
for (int i = 0; i < fileName.length(); i++) {
if (index + i >= commandLine.length()) break;
if (std::isspace(fileName[i])) {
mistakes[index + i] = '^';
throw std::runtime_error("Spaces are not allowed in the filename");
}
}
index += fileName.length();
}
else {
break;
}
}
}
}