-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandFactory.cpp
More file actions
412 lines (364 loc) · 13.3 KB
/
CommandFactory.cpp
File metadata and controls
412 lines (364 loc) · 13.3 KB
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
#include "CommandFactory.h"
#include <iostream>
#include <string>
#include <fstream>
#include "Stream.h"
void CommandFactory::printErrorContext(const std::string& command, const std::string& option, const std::string& argument, const std::string& errorMessage, ErrorCode code) {
char uChar = '~';
int size = 0;
if (command != "wc")
std::cerr << command << " " << option << " " << argument << std::endl;
else std::cerr << command << " " << option << " " << "Stream->File()" << std::endl;
std::string underline;
if (code == INVALID_ARGUMENT) {
size = static_cast<int>(command.size() + option.size()) + 1;
underline.append(size, ' ');
underline.append(argument.size(), uChar);
}
else if (code == INVALID_OPTION) {
size = static_cast<int>(command.size()) + 1;
underline.append(size, ' ');
underline.append(option.size(), uChar);
}
else if (code == UNKNOWN_COMMAND) {
size = static_cast<int>(command.size());
underline.append(size, uChar);
}
else if (code == SYNTAX_ERROR) {
size = static_cast<int>(command.size() + option.size()) + 1;
underline.append(size, ' ');
underline.append(argument.size(), uChar);
}
std::cerr << underline << std::endl;
underline = "";
underline.append(size, ' ');
std::cerr << underline << "|" << std::endl;
std::cerr << underline << "|" << std::endl;
std::cerr << underline << "|" << std::endl;
std::cerr << underline + errorMessage << std::endl;
}
void CommandFactory::handleCommand(ErrorCode code, const std::string& command, const std::string& option, const std::string& argument) {
std::string errorMessage;
std::cout << std::endl;
switch (code) {
case INVALID_ARGUMENT:
errorMessage = "Error code 1 - Invalid argument";
break;
case INVALID_OPTION:
errorMessage = "Error code 2 - Invalid option";
break;
case SYNTAX_ERROR:
errorMessage = "Error code 3 - Syntax error";
break;
case UNKNOWN_COMMAND:
errorMessage = "Error code 4 - Unknown command";
break;
default:
errorMessage = "Error code 0 -Segmentation fault";
break;
}
printErrorContext(command, option, argument, errorMessage, code);
}
ErrorCode CommandFactory::validatePromptCommand(const std::string& command, const std::string& option, const std::string& argument, PSIGN& prompt) {
//std::cout << argument << " " << argument.size() << std::endl;
if (!option.empty() || argument.empty()){
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
// Support both: prompt $ and prompt \"$\"'
if (argument.size() >= 2 && argument.front() == '"' && argument.back() == '"') {
std::string inner = argument.substr(1, argument.size() - 2);
if (inner.size() == 1) {
prompt = inner[0];
return SUCCESS;
}
// Quoted but not a single character -> invalid
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
// Unquoted single character
if (argument.size() == 1) {
prompt = argument[0];
return SUCCESS;
}
// Anything else is invalid
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
ErrorCode CommandFactory::validateEchoCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (!option.empty()) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
if (argument.empty()) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
// Unterminated quote is a syntax error (e.g., echo "hello\n)
if (hasUnterminatedQuote(argument)) {
handleCommand(SYNTAX_ERROR, command, option, argument);
return SYNTAX_ERROR;
}
// Only accept a properly quoted string as the final validated argument.
// If a .txt filename was provided, the parser replaces it with quoted file contents before validation.
bool quoted = isProperQuoted(argument);
if (quoted) return SUCCESS;
// Any other unquoted token (words like asdsaas, symbols like >, >>, <, <<) is invalid for echo
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
ErrorCode CommandFactory::validateSimpleCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (!option.empty() || !argument.empty()) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
return SUCCESS;
}
ErrorCode CommandFactory::validateUnknownCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (!option.empty() || !argument.empty()) {
handleCommand(UNKNOWN_COMMAND, command, option, argument);
return UNKNOWN_COMMAND;
}
return SUCCESS;
}
ErrorCode CommandFactory::validateTouchCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (!option.empty()) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
else if (argument.empty()) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
else if (argument.front() == '"' || argument.back() == '"') {
handleCommand(SYNTAX_ERROR, command, option, argument);
return SYNTAX_ERROR;
}
if(!validateFileForOpen(argument,false,true,false)){
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
// Do not pre-fail when the file already exists; let TouchCommand produce the proper error code (7)
return SUCCESS;
}
ErrorCode CommandFactory::validateWcCommand(const std::string& command, const std::string& option, const std::string& argument) {
// option validation
if ((option.length() > 1 && option[0] != '-') ||
(option.length() > 1 && option[1] != 'c' && option[1] != 'w')) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
// Unterminated quote is a syntax error
if (hasUnterminatedQuote(argument)) {
handleCommand(SYNTAX_ERROR, command, option, argument);
return SYNTAX_ERROR;
}
// argument validation: wc expects its final evaluated argument to be quoted text
// If a .txt filename was provided or input was redirected, the parser replaces it with quoted text.
if (argument.empty() || !isProperQuoted(argument)) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
return SUCCESS;
}
ErrorCode CommandFactory::validateTruncateCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (!option.empty()) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
else if (argument.empty()) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
else if (argument.front() == '"' || argument.back() == '"') {
handleCommand(SYNTAX_ERROR, command, option, argument);
return SYNTAX_ERROR;
}
if (!validateFileForOpen(argument, true, true, true)) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
return SUCCESS;
}
ErrorCode CommandFactory::validateRmCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (!option.empty()) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
else if (argument.empty()) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
else if (argument.front() == '"' || argument.back() == '"') {
handleCommand(SYNTAX_ERROR, command, option, argument);
return SYNTAX_ERROR;
}
if (!validateFileForOpen(argument, true, true, false)) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
return SUCCESS;
}
// tr: argument must be quoted input text (parser ensures file -> quoted),
// what must be quoted, with can be empty or quoted. No options allowed.
ErrorCode CommandFactory::validateTrCommand(const std::string& command, const std::string& option, const std::string& inputArg, const std::string& whatArg, const std::string& withArg) {
if (!option.empty()) {
handleCommand(INVALID_OPTION, command, option, inputArg);
return INVALID_OPTION;
}
// syntax errors on any arg
if (hasUnterminatedQuote(inputArg) || hasUnterminatedQuote(whatArg) || hasUnterminatedQuote(withArg)) {
handleCommand(SYNTAX_ERROR, command, option, inputArg);
return SYNTAX_ERROR;
}
if (inputArg.empty()) {
handleCommand(INVALID_ARGUMENT, command, option, inputArg);
return INVALID_ARGUMENT;
}
if (!isProperQuoted(inputArg)) {
// input must be quoted text by the time we validate
handleCommand(INVALID_ARGUMENT, command, option, inputArg);
return INVALID_ARGUMENT;
}
if (whatArg.empty() || !isProperQuoted(whatArg)) {
handleCommand(INVALID_ARGUMENT, command, option, whatArg);
return INVALID_ARGUMENT;
}
if (!withArg.empty() && !isProperQuoted(withArg)) {
handleCommand(INVALID_ARGUMENT, command, option, withArg);
return INVALID_ARGUMENT;
}
return SUCCESS;
}
// head: option must be -n<digits up to 5>, argument must be quoted text (or loaded from file)
ErrorCode CommandFactory::validateHeadCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (option.size() < 2 || option[0] != '-') {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
if (option.size() < 3 || option[1] != 'n') {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
std::string digits = option.substr(2);
if (digits.empty() || digits.size() > 5) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
for (char c : digits) {
if (!std::isdigit(static_cast<unsigned char>(c))) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
}
// Unterminated quote is a syntax error
if (hasUnterminatedQuote(argument)) {
handleCommand(SYNTAX_ERROR, command, option, argument);
return SYNTAX_ERROR;
}
// argument must be quoted text
if (argument.empty() || !isProperQuoted(argument)) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
return SUCCESS;
}
// batch: no options, argument must be quoted text (possibly loaded from file)
ErrorCode CommandFactory::validateBatchCommand(const std::string& command, const std::string& option, const std::string& argument) {
if (!option.empty()) {
handleCommand(INVALID_OPTION, command, option, argument);
return INVALID_OPTION;
}
// Unterminated quote is a syntax error
if (hasUnterminatedQuote(argument)) {
handleCommand(SYNTAX_ERROR, command, option, argument);
return SYNTAX_ERROR;
}
if (argument.empty() || !isProperQuoted(argument)) {
handleCommand(INVALID_ARGUMENT, command, option, argument);
return INVALID_ARGUMENT;
}
return SUCCESS;
}
bool CommandFactory::validateFileForOpen(const std::string& path, bool requireExist, bool requireTxt, bool forWrite){
if(path.empty()) return false;
if(requireTxt){
if(path.size() < 4) return false;
if(path.substr(path.size()-4) != ".txt") return false;
}
if(requireExist){
std::ifstream in(path);
if(!in.good()) return false;
if(!in.is_open()) return false;
}
if(forWrite){
std::ofstream out(path, std::ios::app);
if(!out.good()) return false;
if(!out.is_open()) return false;
}
return true;
}
Command* CommandFactory::createCommand(const std::string& command, const std::string& option, const std::string& arg1, const std::string& arg2, const std::string& arg3, PSIGN& prompt) {
if (command == "prompt") {
if (validatePromptCommand(command, option, arg1, prompt) == SUCCESS) return nullptr;
return nullptr;
}
if (command == "echo") {
if (validateEchoCommand(command, option, arg1) == SUCCESS) return new EchoCommand(arg1);
return nullptr;
}
if (command == "time") {
if (validateSimpleCommand(command, option, arg1) == SUCCESS) return new TimeCommand();
return nullptr;
}
if (command == "date") {
if (validateSimpleCommand(command, option, arg1) == SUCCESS) return new DateCommand();
return nullptr;
}
if (command == "clear") {
if (validateSimpleCommand(command, option, arg1) == SUCCESS) return new ClearCommand();
return nullptr;
}
if (command == "touch") {
if (validateTouchCommand(command, option, arg1) == SUCCESS) return new TouchCommand(arg1);
return nullptr;
}
if (command == "wc") {
if (validateWcCommand(command, option, arg1) == SUCCESS) return new WcCommand(arg1, option);
return nullptr;
}
if (command == "exit") {
if (validateSimpleCommand(command, option, arg1) == SUCCESS) return new ExitCommand();
return nullptr;
}
if (command == "help") {
if (validateSimpleCommand(command, option, arg1) == SUCCESS) return new HelpCommand();
return nullptr;
}
if (command == "truncate") {
if (validateTruncateCommand(command, option, arg1) == SUCCESS) return new TruncateCommand(arg1);
return nullptr;
}
if (command == "rm") {
if (validateRmCommand(command, option, arg1) == SUCCESS) return new RmCommand(arg1);
return nullptr;
}
if (command == "tr") {
if (validateTrCommand(command, option, arg1, arg2, arg3) == SUCCESS) return new TrCommand(arg1, arg2, arg3);
return nullptr;
}
if (command == "head") {
if (validateHeadCommand(command, option, arg1) == SUCCESS) {
int n = std::stoi(option.substr(2));
return new HeadCommand(arg1, n);
}
return nullptr;
}
if (command == "batch") {
if (validateBatchCommand(command, option, arg1) == SUCCESS) return new BatchCommand(arg1);
return nullptr;
}
// Unknown
handleCommand(UNKNOWN_COMMAND, command, option, arg1);
return nullptr;
}