This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
/
Shell.cpp
310 lines (269 loc) · 10.2 KB
/
Shell.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
///////////////////////////
// Tanner Kvarfordt //
// A02052217 //
// CS 3100 --Dr. Mathias //
// Assignment 3 //
///////////////////////////
#include "Shell.hpp"
Shell::Shell() :
M_CMD_DELIMITER(" "), M_EXIT_CMD("exit"),
M_HISTORY("history"), M_RUN_HISTORY("^"),
M_PTIME("ptime"), M_CD("cd"),
M_INCLUDE_RUN_HISTORY(false),
M_PIPE_DELIM(" | "),
M_HOME(this->getWD()),
m_history(), m_child_time_total(0) {}
Shell::Shell(bool const & include_run_hist) :
M_CMD_DELIMITER(" "), M_EXIT_CMD("exit"),
M_HISTORY("history"), M_RUN_HISTORY("^"),
M_PTIME("ptime"), M_CD("cd"),
M_HOME(this->getWD()),
M_PIPE_DELIM(" | "),
M_INCLUDE_RUN_HISTORY(include_run_hist),
m_history(), m_child_time_total(0) {}
Shell::Shell(bool const & include_run_hist, std::string const & run_hist_cmd) :
M_CMD_DELIMITER(" "), M_EXIT_CMD("exit"),
M_HISTORY("history"), M_RUN_HISTORY(run_hist_cmd),
M_PTIME("ptime"), M_CD("cd"),
M_HOME(this->getWD()),
M_PIPE_DELIM(" | "),
M_INCLUDE_RUN_HISTORY(include_run_hist),
m_history(), m_child_time_total(0) {}
Shell::Shell(bool const & include_run_hist, std::string const & run_hist_cmd,
std::string const & hist_cmd, std::string const & exit_cmd,
std::string const & ptime, std::string const & pipe_delim, std::string const cmd_delim) :
M_CMD_DELIMITER(cmd_delim), M_EXIT_CMD(exit_cmd),
M_HISTORY(hist_cmd), M_RUN_HISTORY(run_hist_cmd),
M_INCLUDE_RUN_HISTORY(include_run_hist), M_CD("cd"),
M_HOME(this->getWD()),
M_PIPE_DELIM(pipe_delim),
M_PTIME(ptime) {}
// Checks if the user entered the @M_RUN_HISTORY command and if so,
// decides whether or not to include that command in history based on @M_INCLUDE_RUN_HISTORY
void Shell::filterHistory(std::vector<std::string> const & input_args) {
if (!M_INCLUDE_RUN_HISTORY) {
// If command is M_RUN_HISTORY, remove it from m_history to prevent confusion
if (input_args.at(0) == M_RUN_HISTORY) m_history.pop_back();
}
}
// Determines how to run the users unpiped input
void Shell::run_cmd(std::vector<std::string> const & input_args) {
filterHistory(input_args);
// Check if command is built-in
if (!isBuiltIn(input_args)) {
// Otherwise, send it to OS
if (fork()) {
// parent process executes here
// wait for child process to terminate before continuing
m_child_time_total += timeChild();
} else {
// child process executes here
exec_cmd(input_args);
// remove all chance of fork bomb
exit(EXIT_SUCCESS);
}
}
}
// Determines how to run the users piped input
// Pipes currently do not work with built-in commands
// Will not exit the Shell if the first command is exit
// @input is the entire string of commands input by the user
// Adapted from https://gist.github.com/zed/7540510
void Shell::run_piped_cmd(std::string const & input) {
int savedStdout = dup(STDOUT);
int savedStdin = dup(STDIN);
// vector containing each individual command in the pipe sequence and its arguments
std::vector<std::string> pipe_chunks;
parse_string(input, M_PIPE_DELIM, pipe_chunks);
int in = STDIN;
for (int i = 0; i < pipe_chunks.size() - 1; ++i) { // run all but the last command
std::vector<std::string> input_args;
parse_string(pipe_chunks.at(i), M_CMD_DELIMITER, input_args);
int fd[2]; // read/write pipe ends
pid_t pid; // child's pid
int res = pipe(fd); // store value to avoid compiler warning
pid = fork();
if (pid == 0) { // child
close(fd[0]);
redirect(in, STDIN);
redirect(fd[1], STDOUT);
exec_cmd(input_args); // should not return
exit(EXIT_FAILURE);
} else { // parent
close(fd[1]);
close(in);
in = fd[0]; // next command reads from here
m_child_time_total += timeChild();
}
}
if (fork() == 0) { // child
// run last command
std::vector<std::string> input_args;
parse_string(pipe_chunks.back(), M_CMD_DELIMITER, input_args);
redirect(in, STDIN);
exec_cmd(input_args); // should not return
exit(EXIT_FAILURE);
} else { // parent
for (int i = 0; i < pipe_chunks.size(); ++i) m_child_time_total += timeChild();
// Restore standard out and standard in so the shell will work normally
dup2(savedStdout, STDOUT);
dup2(savedStdin, STDIN);
}
}
// Redirects fd's for piping purposes
// Adapted heavily from https://gist.github.com/zed/7540510
void Shell::redirect(int oldfd, int newfd) {
if (oldfd != newfd) {
dup2(oldfd, newfd);
close(oldfd);
}
}
void Shell::run() {
// register signal SIGINT with signalHandler
signal(SIGINT, Shell::signalHandler);
std::vector<std::string> input_args;
std::string input = "";
while (true) {
input_args.clear();
input = prompt();
if (input != "") {
m_history.push_back(input);
// check for pipes
if (input.find(M_PIPE_DELIM) != std::string::npos) {
run_piped_cmd(input);
} else {
parse_string(input, M_CMD_DELIMITER, input_args);
if (input_args.size() > 0) {
if (input_args.at(0) == M_EXIT_CMD) return;
run_cmd(input_args);
}
}
}
}
}
// Prompts a user for input and returns the result
std::string Shell::prompt() const {
std::cout << "[" << getWD() << "]: ";
std::string input = "";
std::getline(std::cin, input);
return input;
}
// @input_args contains the user's command and all of its arguments.
// This function translates the command its arguments into cstrings,
// then passes them off to the OS via execvp to be executed
void Shell::exec_cmd(std::vector<std::string> const &input_args) const {
char** argv = new char*[input_args.size() + 1];
for (unsigned int i = 0; i < input_args.size(); ++i) {
// cstrings require a null terminator, hence the + 1
argv[i] = new char[input_args.at(i).length() + 1];
strncpy(argv[i], input_args[i].c_str(), input_args[i].size() + 1);
}
// NULL to let execvp know there are no more args
argv[input_args.size()] = NULL;
execvp(argv[0], argv);
std::cout << input_args.at(0) << ": command not found" << std::endl;
// protect against fork bomb
exit(EXIT_FAILURE);
}
// Prints the contents of m_history
void Shell::printHistory() const {
std::cout << std::endl << "-- Command History --" << std::endl << std::endl;
for (unsigned int i = 0; i < m_history.size(); ++i) {
std::cout << std::setw(std::to_string(m_history.size()).length()) << i << " : " << m_history.at(i) << std::endl;
}
std::cout << std::endl;
}
// Runs the command in m_history[@entry]
// Returns the amount of time spent running a child process, if any
Duration Shell::runHistoryEntry(std::string const &entry) {
// check for non-int type
if (entry.find('.') != std::string::npos) return Duration(0);
int entry_int;
try {
entry_int = std::stoi(entry);
// check that history index is in range
if (entry_int < 0 || entry_int > m_history.size() - 1) return Duration(0);
} catch (std::exception e) {
// invalid history index
return Duration(0);
}
// parse the input_args from the history index
std::vector<std::string> input_args;
parse_string(m_history.at(entry_int), M_CMD_DELIMITER, input_args);
// Check for built in
if (isBuiltIn(input_args)) {
return Duration(0);
} else {
// Otherwise, send it to the OS
if (fork()) {
// parent process executes here
return timeChild();
} else {
// child process executes here
exec_cmd(input_args);
// protect against fork bomb
exit(EXIT_FAILURE);
}
}
}
// Checks to see if the command in @input_args is built-in
// If so, it calls the appropriate function
// If not, it returns false
bool Shell::isBuiltIn(std::vector<std::string> const &input_args) {
if (input_args.at(0) == M_CD) {
changeDirectory(input_args);
return true;
}
if (input_args.at(0) == M_HISTORY) {
printHistory();
return true;
}
if (input_args.at(0) == M_RUN_HISTORY && input_args.size() > 1) {
m_child_time_total += runHistoryEntry(input_args.at(1));
return true;
}
if (input_args.at(0) == M_PTIME) {
printPtime();
return true;
}
return false;
}
void Shell::printPtime() const {
std::cout << std::endl << "Time spent executing child processes: "
<< std::fixed << std::setprecision(4)
<< m_child_time_total.count() << " seconds" << std::endl << std::endl;
}
// Returns the amount of time spent waiting on a child process to finish executing
Duration Shell::timeChild() const {
Time_Point start = High_Res_Clock::now();
wait(NULL);
Time_Point end = High_Res_Clock::now();
return end - start;
}
// Returns a string containing the current working directory
// or "cmd" in the event of a failure
// See http://pubs.opengroup.org/onlinepubs/009695399/functions/getcwd.html
std::string Shell::getWD() const {
long size = pathconf(".", _PC_PATH_MAX);
char * buf;
std::string wd = "cmd";
if ((buf = (char *)malloc((size_t)size)) != NULL) {
wd = getcwd(buf, (size_t) size);
}
free(buf);
// TODO: Wash hands cuz you touched 'malloc'
// TODO: Pray to the goddess Mnemosyne that this isn't leaking memory
return wd;
}
// Attempts to change the working directory to the directory provided in @input_args[1]
// If no argument is provided, attempts to change to @M_HOME directory
// See http://pubs.opengroup.org/onlinepubs/009695399/functions/chdir.html
void Shell::changeDirectory(std::vector<std::string> const & input_args) const {
if (input_args.size() < 2) {
// store result in an int to avoid compiler warning
int ret = chdir(M_HOME.c_str());
return;
}
// store result in an int to avoid compiler warning
int ret = chdir(input_args.at(1).c_str());
}