-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.cpp
More file actions
318 lines (268 loc) · 10.7 KB
/
Shell.cpp
File metadata and controls
318 lines (268 loc) · 10.7 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
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <vector>
#include <deque>
#include <fstream>
#include <string>
#include <cstring>
#include <signal.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <iomanip>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include"commandUtils.h"
#include "Commands.h"
#include <sys/wait.h>
#include <unistd.h>
#include "Shell.h"
Shell::Shell(){localHome=getLocalHome();loadHistory();}; // Constructor declaration
Shell::~Shell(){saveHistory();}; // Destructor declaration
// Other member functions
void Shell:: updatePrompt() {
currentDirectory = getCurrentDirectory();
char hostname[1024];
gethostname(hostname, sizeof(hostname));
std::string relativePath = (currentDirectory.find(localHome) == 0)
? "~" + currentDirectory.substr(localHome.length())
: currentDirectory;
std::cout << getenv("USER") << "@" << hostname << ":" << relativePath << "$ "<<std::flush;
}
void Shell::loadHistory() {
std::ifstream infile(historyFile);
std::string line;
while (std::getline(infile, line)) {
if (!line.empty()) {
commandHistory.push_back(line);
}
}
infile.close();
historyIndex = commandHistory.size();
}
void Shell::historyCommand(std::vector<std::string> &args){
loadHistory();
for(auto cmd:commandHistory){
std::cout<<cmd<<std::endl;
}
}
void Shell::saveHistory() {
std::ofstream outfile(historyFile);
for (const auto &cmd : commandHistory) {
outfile << cmd << std::endl;
}
outfile.close();
}
bool Shell::handleBuiltInCommands(std::vector<std::string>& args,Shell &shell) {
if (args.empty()) return false;
const std::string& command = args[0];
if (command == "cd") {
std::cerr<<"reached handleBuiltins with args "<<args[1];
cdCommand cd;
cd.execute(args);
} else if (command == "ls") {
LsCommand ls;
ls.execute(args);
} else if (command == "pinfo") {
pinfo(args);
} else if (command == "history") {
shell.historyCommand(args);
} else if (command == "search") {
SearchCommand srch;
srch.execute(args);
// } else if (command == "echo") {
// executeEcho(args);
}
else {
return false; // Not a built-in command
}
return true; // Command was handled as a built-in
}
int Shell::runCommand(std::vector<std::string> commands) {
int pipe_fd[2];
int in_fd = 0; // This will track the input for the current command (initially stdin)
int out_fd = 1; // Standard output default
int append_fd = 1;
bool isBackgroundP = false;
std::vector<std::string> command;
if (!commands.empty()) {
for (auto it : commands) {
if (it != "|") {
if (it == "&") {
isBackgroundP = true;
} else {
command.push_back(it);
}
} else {
// Create a pipe
if (pipe(pipe_fd) == -1) {
std::cerr << "Error creating pipe" << std::endl;
return 1;
}
if (isBackgroundP == true) { // | cannot occur after &
std::cerr << "myShell: syntax error near unexpected token `|'" <<std::endl;
return 1;
}
if (!command.empty()) {
int pid = fork();
if (pid == -1) {
std::cerr << "Error forking" <<std::endl;
return 1;
} else if (pid == 0) { // Child process
handleIndirections(command, in_fd, out_fd, append_fd); // Handle indirections
close(pipe_fd[0]); // Close unused read end
dup2(in_fd, STDIN_FILENO); // Duplicate the input from the last command
dup2(pipe_fd[1], STDOUT_FILENO); // Redirect output to pipe's write end
close(pipe_fd[1]); // Close original write end
// Check if it's a built-in command
if (!handleBuiltInCommands(command, *this)) {
std::vector<char*> args;
for (auto& cmd : command) {
args.push_back(&cmd[0]);
}
args.push_back(nullptr);
execvp(args[0], args.data()); // Execute command
perror("execvp failed");
}
exit(1); // Ensure child exits after executing the command
} else { // Parent process
waitpid(pid, NULL, 0); // Wait for the child process to finish
close(pipe_fd[1]); // Close write end of the pipe
in_fd = pipe_fd[0]; // Save the read end for the next command
command.clear(); // Clear the command vector for the next command
}
}
}
}
// For background process or any remaining command
if (!command.empty()) {
handleIndirections(command, in_fd, out_fd, append_fd);
int back_pid = 0;
if (isBackgroundP) {
back_pid = fork();
}
if (back_pid == 0 || !isBackgroundP) { // Child process or foreground process
dup2(in_fd, STDIN_FILENO); // Input from the last command
close(in_fd);
if (!handleBuiltInCommands(command, *this)) {
std::vector<char*> args;
for (auto& cmd : command) {
args.push_back(&cmd[0]);
}
args.push_back(nullptr);
execvp(args[0], args.data()); // Execute command
perror("execvp failed");
exit(1); // Ensure child exits after executing the command
}
exit(0); // Ensure the child process exits after execution
} else if (back_pid > 0) { // Parent process in background mode
if (isBackgroundP) std::cout << back_pid <<std::endl;
}
}
}
return 0;
}
void Shell::run() {
termios orig_termios;
// Get original terminal attributes
tcgetattr(STDIN_FILENO, &orig_termios);
// Enable raw mode
enableRawMode(orig_termios);
while (true) {
std::string input;
updatePrompt();
std::string currentInput;
historyIndex = commandHistory.size(); // Reset history index
std::string buffer; // Buffer for unfinished commands
std::cout << "\033[?12h\033[?25h" << std::flush;
char c;
while (read(0,&c,1)==1) {
if (c == '\n') {
std::cout << std::endl;
if (!currentInput.empty()) {
// Add to history and reset index
if (commandHistory.size() == historyLimit) {
commandHistory.pop_front();
}
commandHistory.push_back(currentInput);
historyIndex = commandHistory.size();
}
buffer.clear(); // Clear the buffer as the command is now completed
break;
} else if (c == 127) { // Handle backspace
if (!currentInput.empty()) {
currentInput.pop_back();
std::cout << "\b \b" << std::flush; // Flush after backspace
}
} else if (c == 27) { // Handle escape sequences (arrows)
char seq[3];
if (read(STDIN_FILENO, &seq[0], 1) == 1 && seq[0] == '[') {
if (read(STDIN_FILENO, &seq[1], 1) == 1) {
if (seq[1] == 'A') { // Up arrow
if (historyIndex > 0) {
if (historyIndex == commandHistory.size()) {
buffer = currentInput; // Save unfinished command in buffer
}
historyIndex--;
// Clear current line
std::cout << "\r\033[K" << std::flush;
updatePrompt();
currentInput = commandHistory[historyIndex];
std::cout << currentInput << std::flush;
}
} else if (seq[1] == 'B') { // Down arrow
if (historyIndex < commandHistory.size()) {
historyIndex++;
// Clear current line
std::cout << "\r\033[K" << std::flush;
updatePrompt();
if (historyIndex < commandHistory.size()) {
currentInput = commandHistory[historyIndex];
} else {
currentInput = buffer; // Restore unfinished command from buffer
}
std::cout << currentInput << std::flush;
}
}
}
}
} else {
currentInput += c;
std::cout << c << std::flush; // Flush after every character
}
}
input = currentInput;
if (input.empty()){
std::cin.ignore();
continue;
} // Skip processing if input is empty
if(input=="exit"){
break;
}
// Tokenization and further processing of the input
std::vector<std::string> tokenizedCommands = tokenize(input);
std::vector<std::string> indiCommand;
for (auto it : tokenizedCommands) {
if (it != ";") {
indiCommand.push_back(it);
} else {
if (!indiCommand.empty()) {
runCommand(indiCommand);
indiCommand.clear();
}
}
}
if (!indiCommand.empty()) {
runCommand(indiCommand);
}
// Restore original terminal attributes before exit
}
disableRawMode(orig_termios);
}
int main(){
Shell* sh=new Shell();
sh->run();
return 0;
}