-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.h
More file actions
510 lines (386 loc) · 12.1 KB
/
Commands.h
File metadata and controls
510 lines (386 loc) · 12.1 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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// Ver: 10-4-2025
#ifndef SMASH_COMMAND_H_
#define SMASH_COMMAND_H_
#include <vector>
#include <string>
#include <map>
#include <unistd.h>
#include <cstring>
#include <regex.h>
#include <memory>
#define COMMAND_MAX_LENGTH (200)
#define COMMAND_MAX_ARGS (20)
class SmallShell;
class Command {
private:
std::string m_cmd_args[COMMAND_MAX_ARGS];
int m_num_of_args;
public:
Command(std::string cmd_args[COMMAND_MAX_ARGS], int num_of_args){
for(int i = 0; i < COMMAND_MAX_ARGS; ++i){
this->m_cmd_args[i] = cmd_args[i];
}
this->m_num_of_args = num_of_args;
};
std::string* getCmdArgs(){
return this->m_cmd_args;
}
int getNumOfArgs() const{
return this->m_num_of_args;
}
virtual ~Command() = default;
virtual void execute() = 0;
};
class BuiltInCommand : public Command {
protected:
SmallShell& m_small_shell;
public:
BuiltInCommand(std::string cmd_args[], int num_of_args, SmallShell& other) : Command(cmd_args, num_of_args), m_small_shell(other){};
virtual void execute() = 0;
virtual ~BuiltInCommand() {}
};
class Chprompt : public BuiltInCommand{
std::string m_name = "chprompt";
public:
Chprompt(std::string cmd_args[], int num_of_args, SmallShell& other) : BuiltInCommand(cmd_args, num_of_args, other){};
virtual void execute();
};
class Showpid : public BuiltInCommand{
public:
Showpid(std::string cmd_args[], int num_of_args, SmallShell& other) : BuiltInCommand(cmd_args, num_of_args, other){};
virtual void execute();
};
class Pwd : public BuiltInCommand{
public:
Pwd(std::string cmd_args[], int num_of_args, SmallShell& other) : BuiltInCommand(cmd_args, num_of_args, other){};
virtual void execute();
};
class Cd : public BuiltInCommand{
public:
Cd(std::string cmd_args[], int num_of_args, SmallShell& other) : BuiltInCommand(cmd_args, num_of_args, other){};
virtual void execute();
};
class ExternalCommand : public Command {
bool m_is_bg_cmd;
SmallShell& m_shell;
const char* m_og_cmd;
public:
ExternalCommand(std::string cmd_args[], const char* og_cmd, int num_of_args, SmallShell& shell, bool is_bg_cmd) :
Command(cmd_args, num_of_args), m_shell(shell) {
m_is_bg_cmd = is_bg_cmd;
m_og_cmd = og_cmd;
};
virtual ~ExternalCommand() {
}
bool getIsBgCmd() const {
return m_is_bg_cmd;
}
void execute() override;
};
class RedirectionCommand : public Command {
// TODO: Add your data members
SmallShell& m_small_shell;
std::string m_path;
std::string m_mode;
public:
explicit RedirectionCommand(std::string cmd_args[], size_t numOfArgs, std::string& mode, std::string& path, SmallShell& other)
: Command(cmd_args, numOfArgs), m_small_shell(other) {
m_path = path;
m_mode = mode;
};
virtual ~RedirectionCommand() {
}
std::string getPath() const{
return this->m_path;
}
std::string getMode() const {
return this->m_mode;
}
void execute() override;
};
class PipeCommand : public Command {
// TODO: Add your data members
SmallShell& m_small_shell;
int m_pipe_place;
bool m_is_err;
public:
PipeCommand(std::string cmd_args[], int numOfArgs, SmallShell& other, int pipe_place, bool is_err) :
Command(cmd_args, numOfArgs), m_small_shell(other), m_pipe_place(pipe_place), m_is_err(is_err) {};
virtual ~PipeCommand() {
}
bool getIsErr() const{
return m_is_err;
}
int getPipePlace() {
return this->m_pipe_place;
}
void execute() override;
void executePipeHandler(int fd);
};
class DiskUsageCommand : public Command {
SmallShell& m_small_shell;
public:
DiskUsageCommand(std::string cmd_args[], int numOfArgs, SmallShell& other): Command(cmd_args, numOfArgs), m_small_shell(other) {};
virtual ~DiskUsageCommand() {
}
void execute() override;
};
class WhoAmICommand : public Command {
SmallShell& m_small_shell;
public:
WhoAmICommand(std::string cmd_args[], int numOfArgs, SmallShell& other): Command(cmd_args, numOfArgs), m_small_shell(other){};
virtual ~WhoAmICommand() {
}
void execute() override;
};
class NetInfo : public Command {
// TODO: Add your data members **BONUS: 10 Points**
SmallShell& m_small_shell;
public:
NetInfo(std::string cmd_args[], int numOfArgs, SmallShell& other): Command(cmd_args, numOfArgs), m_small_shell(other){};
virtual ~NetInfo() {
}
void execute() override;
};
class JobsList;
class QuitCommand : public BuiltInCommand {
public:
// TODO: Add your data members public:
QuitCommand(std::string cmd_args[], int num_of_args, SmallShell& other)
: BuiltInCommand(cmd_args, num_of_args, other){};
virtual ~QuitCommand() {
}
void execute() override;
};
class JobsList {
private:
class JobEntry {
private:
int m_jobId;
std::string m_command;
pid_t m_pid;
public:
JobEntry(const int& jobId, pid_t pid, const char* command){
m_jobId = jobId;
m_command = command;
m_pid = pid;
};
~JobEntry() = default;
int getJobId() const {
return m_jobId;
}
int getPid() const {
return m_pid;
}
std::string getCommand() const {
return m_command;
}
};
// TODO: Add your data members
std::vector<JobEntry> m_jobs;
int max_id = 0;
public:
JobsList() = default;
~JobsList() = default;
int getMaxId() const {
return this->max_id;
}
void setMaxId(int max_id) {
this->max_id = max_id;
}
std::vector<JobEntry>& getJobsList() {
return this->m_jobs;
}
void addJob(const char* cmd, pid_t pid);
void printJobsList();
void killAllJobs();
void removeFinishedJobs();
JobEntry *getJobById(int jobId);
void removeJobById(int jobId);
JobEntry *getLastJob();
JobEntry *getLastStoppedJob(int *jobId);
void updateMaxJobId();
// TODO: Add extra methods or modify exisitng ones as needed
};
class JobsCommand : public BuiltInCommand {
// TODO: Add your data members
public:
JobsCommand(std::string cmd_line[], int num_of_args, SmallShell& other) : BuiltInCommand(cmd_line, num_of_args, other) {};
virtual ~JobsCommand() {
}
void execute() override;
};
class KillCommand : public BuiltInCommand {
// TODO: Add your data members
// JobsList& m_job_list;
public:
KillCommand(std::string cmd_line[], int num_of_args, SmallShell& other)
: BuiltInCommand(cmd_line, num_of_args, other) {};
virtual ~KillCommand() {
}
void execute() override;
};
class ForegroundCommand : public BuiltInCommand {
// TODO: Add your data members
public:
ForegroundCommand(std::string cmd_line[], int num_of_args, SmallShell& other) : BuiltInCommand(cmd_line, num_of_args, other) {};
virtual ~ForegroundCommand() {
}
void execute() override;
void executeFgByID();
void executeLastId();
};
class AliasCommand : public BuiltInCommand {
private:
std::string m_alias;
std::string m_cmd;
public:
AliasCommand(std::string cmd, std::string alias, std::string cmd_args[], int num_of_args, SmallShell& other)
: BuiltInCommand(cmd_args, num_of_args, other){
m_alias = alias;
m_cmd= cmd;
};
virtual ~AliasCommand() {
}
std::string& getAlias(){
return this->m_alias;
}
std::string& getCmd(){
return m_cmd;
}
void execute() override;
};
class UnAliasCommand : public BuiltInCommand {
public:
UnAliasCommand(std::string cmd_args[], int num_of_args, SmallShell& other)
: BuiltInCommand(cmd_args, num_of_args, other){};
virtual ~UnAliasCommand() {
}
void execute() override;
void removeAllAlias(int numOfArgs);
};
class UnSetEnvCommand : public BuiltInCommand {
public:
UnSetEnvCommand(std::string cmd_args[], int num_of_args, SmallShell& other)
: BuiltInCommand(cmd_args, num_of_args, other){};
virtual ~UnSetEnvCommand() {
}
void execute() override;
};
class WatchProcCommand : public BuiltInCommand {
public:
WatchProcCommand(std::string cmd_args[], int num_of_args, SmallShell& other)
: BuiltInCommand(cmd_args, num_of_args, other){};
virtual ~WatchProcCommand() {
}
void execute() override;
double calculateCpuUsege(int pid);
};
class SmallShell {
private:
std::string m_promptName = "smash";
std::string m_lastWorkingDir = "\n"; // Initialized
std::map<std::string, std::string> m_name_to_cmd;
std::vector<std::string> m_order;
JobsList m_jobs_list;
int saved_std_out;
int saved_std_in;
int saved_std_err;
int redirected_out;
int redirected_in;
int redirected_err;
pid_t m_fg_proc_pid = 0;
SmallShell() {
saved_std_out = dup(STDOUT_FILENO);
if (saved_std_out < 0) {
std::perror("smash error: dup failed");
}
saved_std_in = dup(STDIN_FILENO);
if (saved_std_in < 0) {
std::perror("smash error: dup failed");
}
saved_std_err = dup(STDERR_FILENO);
if (saved_std_err < 0) {
std::perror("smash error: dup failed");
}
};
public:
std::string getPromptName() const{
return m_promptName;
}
void setPromptName(const std::string& newPromptName){
this->m_promptName = newPromptName;
}
std::unique_ptr<Command>CreateCommand(const char *cmd_line);
std::unique_ptr<Command> createCommandHandler(std::string args_cmd[], const char* og_cmd, int numOfArgs, bool is_bg_cmd, int is_pipe_cmd);
std::unique_ptr<Command> build_alias(std::string args_cmd[], int numOfArgs);
std::vector<std::string> CreateCommandVectorAlias(std::string args_cmd[], size_t numOfArgs);
std::unique_ptr<Command> CreateCommandAndRedirect(std::string args_cmd[], size_t numOfArgs, const char* cmd_line, int start);
SmallShell(SmallShell const &) = delete; // disable copy ctor
void operator=(SmallShell const &) = delete; // disable = operator
static SmallShell &getInstance() // make SmallShell singleton
{
static SmallShell instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
~SmallShell();
void executeCommand(const char *cmd_line);
// TODO: add extra methods as needed
std::string getlastWorkingDir(){
return this->m_lastWorkingDir;
}
void setlastWorkingDir(const std::string& dir) {
this->m_lastWorkingDir = dir;
}
void addAlias(std::string& cmd, std::string& alias){
m_name_to_cmd.insert({alias, cmd});
m_order.push_back(alias);
}
std::map<std::string, std::string>& getMap(){
return this->m_name_to_cmd;
}
std::vector<std::string>& getOrder(){
return this->m_order;
}
JobsList& getJobsList() {
return this->m_jobs_list;
}
int getSavedStdOut() const {
return saved_std_out;
}
int getSavedStdIn() const {
return saved_std_in;
}
int getSavedStdErr() const {
return saved_std_err;
}
void setRedirectionOut(int new_fd) {
this->redirected_out = new_fd;
}
void setRedirectionIn(int new_fd) {
this->redirected_in = new_fd;
}
void setRedirectionErr(int new_fd) {
this->redirected_err = new_fd;
}
int getRedirectedOut() {
return this->redirected_out;
}
int getRedirectedIn() {
return this->redirected_in;
}
int getRedirectedErr() {
return this->redirected_err;
}
void restoreStdOut();
void restoreStdIn();
void restoreStdErr();
pid_t getFgProcPID() const {
return m_fg_proc_pid;
}
void setFgProcPID(pid_t pid) {
this->m_fg_proc_pid = pid;
}
};
#endif //SMASH_COMMAND_H_