-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuiltInCommands.cpp
executable file
·321 lines (274 loc) · 7.62 KB
/
BuiltInCommands.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
#include "BuiltInCommands.h"
#include "CommandUtils.h"
#pragma region ChangePromptCommand
/// @brief Change the prompt of the shell
void ChangePromptCommand::execute()
{
if (cmd_v.size() > 1)
{
prompt = cmd_v[1];
}
else
{
prompt = "smash";
}
SmallShell::getInstance().setPrompt(prompt);
}
#pragma endregion
#pragma region ShowPidCommand
ShowPidCommand::ShowPidCommand(const char *cmd_line) : BuiltInCommand(cmd_line)
{
}
/// @brief Show the PID of the shell
void ShowPidCommand::execute()
{
std::cout << "smash pid is " << SmallShell::getInstance().getPid() << std::endl;
}
#pragma endregion
#pragma region ChangeDirCommand
ChangeDirCommand::ChangeDirCommand(const char *cmd_line) : BuiltInCommand(cmd_line)
{
}
/// @brief Change the current directory of the shell
void ChangeDirCommand::execute()
{
if (cmd_v.size() == 2)
{
path = cmd_v[1];
}
else if (cmd_v.size() > 2)
{
this->printError("too many arguments");
return;
}
if (path == "-")
{
if (SmallShell::getInstance().getPrevPath() == "")
{
this->printError("OLDPWD not set");
return;
}
path = SmallShell::getInstance().getPrevPath();
}
std::string currPath = SmallShell::getInstance().getWorkingDir();
if (chdir(path.c_str()) == -1)
{
perror("smash error: chdir failed");
}
else
{
SmallShell::getInstance().setPrevPath(currPath);
}
}
#pragma endregion
#pragma region GetCurrDirCommand
GetCurrDirCommand::GetCurrDirCommand(const char *cmd_line) : BuiltInCommand(cmd_line)
{
}
void GetCurrDirCommand::execute()
{
std::cout << SmallShell::getInstance().getWorkingDir() << std::endl;
}
#pragma endregion
#pragma region JobsCommand
JobsCommand::JobsCommand(const char *cmd_line) : BuiltInCommand(cmd_line)
{
}
void JobsCommand::execute()
{
SmallShell::getInstance().getJobsList().printJobsList();
}
#pragma endregion
#pragma region ForegroundCommand
ForegroundCommand::ForegroundCommand(const char *cmd_line) : BuiltInCommand(cmd_line)
{
}
void ForegroundCommand::execute()
{
// check if there is any id with the command
if (cmd_v.size() > 2)
{
this->printError("invalid arguments");
return;
}
if (cmd_v.size() == 1)
{
if (SmallShell::getInstance().getJobsList().size() == 0)
{
this->printError("jobs list is empty");
return;
}
JobsList::JobEntry job = SmallShell::getInstance().getJobsList().getJobWithMaxID();
MoveJobToForeground(job);
}
else
{
if (!CommandUtils::is_digits(cmd_v[1]))
{
this->printError("invalid arguments");
return;
}
int id = std::atoi(cmd_v[1].c_str());
try
{
JobsList::JobEntry job = SmallShell::getInstance().getJobsList().getJobById(id);
MoveJobToForeground(job);
}
catch (const std::exception &e)
{
this->printError("job-id " + cmd_v[1] + " does not exist");
}
}
}
void ForegroundCommand::MoveJobToForeground(JobsList::JobEntry &job)
{
// print the command with PID
std::cout << job.cmd << " : " << job.getPid() << std::endl;
// sent job to foreground
SmallShell::getInstance().setForeground(job.getJobId());
job.continueProcess();
waitpid(job.getPid(), nullptr, 0);
}
#pragma endregion
#pragma region BackgroundCommand
void BackgroundCommand::execute()
{
JobsList &jlInstance = SmallShell::getInstance().getJobsList();
// if number of arguments is not 1 or 2
if (cmd_v.size() > 2 || !CommandUtils::is_digits(cmd_v[1]))
{
this->printError("invalid arguments");
return;
}
// if bg alone is called and there are no jobs in the list (STOPPED)
if (cmd_v.size() == 1 && jlInstance.countStoppedJobs() == 0)
{
this->printError("there is no stopped jobs to resume");
return;
}
int jobId = std::stoi(cmd_v[1]);
try
{
JobsList::JobEntry &job = jlInstance.getJobById(jobId);
if (cmd_v.size() == 2 && job.getStatus() == 0)
{
this->printError("job-id " + cmd_v[1] + " is already running in the background");
return;
}
// if the job does not exist
if (cmd_v.size() == 2 && job.getJobId() == -1)
{
this->printError("job-id " + cmd_v[1] + " does not exist");
return;
}
}
catch (const std::exception &e)
{
this->printError("job-id " + cmd_v[1] + " does not exist");
return;
}
if (cmd_v.size() > 1)
jlInstance.continueJob(jobId);
else
jlInstance.getLastStoppedJob().continueProcess();
}
#pragma endregion BackgroundCommand
#pragma region QuitCommand
void QuitCommand::execute()
{
if (cmd_v.size() > 1 && cmd_v[1] == "kill")
{
SmallShell::getInstance().getJobsList().killAllJobs();
}
SmallShell::getInstance().setRunning(false);
}
#pragma endregion QuitCommand
#pragma region KillCommand
void KillCommand::execute()
{
if (cmd_v.size() != 3 || !CommandUtils::is_digits(cmd_v[1]) || !CommandUtils::is_digits(cmd_v[2]))
{
this->printError("invalid arguments");
return;
}
int signal = -atoi(cmd_v[1].c_str());
int jobId = atoi(cmd_v[2].c_str());
JobsList::JobEntry job("", jobId, 0, 0);
try
{
job = SmallShell::getInstance().getJobsList().getJobById(jobId);
}
catch (const std::exception &e)
{
this->printError("job-id " + cmd_v[2] + " does not exist");
return;
}
if (job.getJobId() == -1)
{
this->printError("job-id " + cmd_v[2] + " does not exist");
return;
}
if (kill(job.getPid(), signal) < 0)
{
perror("smash error: kill failed");
}
else
{
std::cout << "signal number " << signal << " was sent to pid " << job.getPid() << std::endl;
}
}
#pragma endregion KillCommand
#pragma region SetCoreCommand
void SetCoreCommand::execute()
{
if (cmd_v.size() != 3 || !CommandUtils::is_digits(cmd_v[1]) || !CommandUtils::is_digits(cmd_v[2]))
{
this->printError("invalid arguments");
return;
}
int jobId = std::stoi(cmd_v[1]);
pid_t pid = -1;
// get job pid
try
{
pid = SmallShell::getInstance().getJobsList().getJobById(jobId).getPid();
}
catch (const std::exception &e)
{
this->printError("job-id " + cmd_v[1] + " does not exist");
return;
}
int core = std::stoi(cmd_v[2]);
if (core < 0)
{
this->printError("invalid core number");
return;
}
// set the core using sched_setaffinity
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(core, &set);
if (sched_setaffinity(pid, sizeof(set), &set) < 0)
{
perror("smash error: sched_setaffinity failed");
return;
}
}
#pragma endregion SetCoreCommand
#pragma region ChangeFileModeCommand
void ChangeFileModeCommand::execute()
{
if (cmd_v.size() != 3)
{
this->printError("invalid arguments");
return;
}
int mode = strtoul(cmd_v[1].c_str(), nullptr, 8);
std::string path = cmd_v[2];
if (chmod(path.c_str(), mode) < 0)
{
perror("smash error: chmod failed");
return;
}
}
#pragma endregion ChangeFileModeCommand