-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_shell.cpp
More file actions
408 lines (333 loc) · 11 KB
/
custom_shell.cpp
File metadata and controls
408 lines (333 loc) · 11 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
#include<iostream> //l226726 maaz khan
#include<string>
#include<vector>
#include<unistd.h>
#include<sys/wait.h>
#include<fcntl.h>
#include<sys/stat.h>
using namespace std;
/// file_descriptor:
// 0-> read
// 1-> write
// 2-> error
vector<string> tokenizeCommand(string str, char delim)
{
vector<string> strTokens;
string tmp = "";
bool bgFlag = false;
for (int i = 0; i < str.length(); i++) {
if (str[i] == delim)
{
strTokens.push_back(tmp);
tmp = "";
}
else if (str[i] == '&')
{
strTokens.push_back(tmp);
strTokens.push_back("bgRunTriggered"); //as a reference for toggling bgRunFlag in main()
return strTokens; //return from func because last token already pushed so no need to execute on more push_back at last
}
else if (str[i] == '\'')
{
//do nothing!
}
else {
tmp += str[i];
}
}
strTokens.push_back(tmp); //for pushing last token because it doesn't has space after it
return strTokens;
}
vector<string> separateCommands(string str, char delim)
{
vector<string> strTokens;
string tmp = "";
bool foundPipe=false;
for (int i = 0; i < str.length(); i++) {
if(foundPipe == true) //basically avoiding the space just after |
{
//no nothing
foundPipe=false;
}
else if (str[i] == delim)
{
strTokens.push_back(tmp);
tmp = "";
foundPipe=true;
}
else if (str[i] == ' ' && str[i+1]==delim)
{
//do nothing!
}
else {
tmp += str[i];
}
}
strTokens.push_back(tmp); //for pushing last token because it doesn't has space after it
return strTokens;
}
void shiftAndPop(vector<string>& vec) {
if (vec.empty()) {
return;
}
// Shift elements to lower indices
for (size_t i = 0; i < vec.size() - 1; ++i) {
vec[i] = vec[i + 1];
}
vec.pop_back();
}
int main()
{
string cmd;
pid_t ret_Val;
int childCount=0;
bool pipesFlg=0;
vector<string> history;
int historyCommandsCounter=0;
while (true)
{
cout << "\n( Enter Command ) : ";
getline(cin, cmd);
if (cmd == "exit")
break;
if(cmd=="history")
{
int srNo=history.size();
cout<<"\n( Total Commands entered: "<< historyCommandsCounter << " )\n";
cout<<"( Total history size: 10" << " )\n";
cout<<"( Current history size: "<< history.size() << " )\n";
cout<<"\nShowing HISTORY...\n";
for(int i=history.size()-1;i>=0;i--)
{
cout << srNo-- << "-> " << history[i]<<endl;
}
continue; //leave the rest of the loop
}
if(cmd=="!!")
{
if(history.size()==0){
cout<<"history is empty\n";
continue;
}
cmd=history.back();
cout<<"most recent command was " <<" "<<cmd<<endl<<endl;
}
if(cmd[0]=='!' && cmd[1]!='!' )
{
string numStr="";
int index=0;
for(int i=1;i<cmd.length();i++)
{
numStr[index++]=cmd[i];
}
int no=stoi(numStr);
//no = static_cast<int>(cmd[1]) - '0'; // it can work on only one digit so used different approach above..
if(no > history.size() || no == 0)
{
cout<<" command - "<<no<<" not found is history! (//ERROR_404)\n";
continue;
}
cout<<"no "<<no<<" command will be going to execute...\n\n";
cmd=history[no-1];
}
historyCommandsCounter++;
if(historyCommandsCounter>10)
{
shiftAndPop(history);
history.push_back(cmd);
}
else
{
history.push_back(cmd);
}
vector<string> commands = separateCommands(cmd, '|');
//for testing the separation of chunks of related commands
// cout<<"\n\nseparating each chunk of related commands:\n\n";
// for (size_t i = 0; i < commands.size(); i++)
// {
// cout<<commands[i]<<"(finished)"<<endl;
// }
// //for tokenizing the individual commands
// cout<<"\n\nnow tokenizing each chunk of related commands:\n\n";
// for (size_t i = 0; i < commands.size(); i++)
// {
// vector<string> tokens = tokenizeCommand(commands[i], ' ');
// for (size_t i = 0; i < tokens.size(); i++)
// {
// cout<<tokens[i]<<"(finished)"<<endl;
// }
// cout<<endl;
// }
if(commands.size()>1)
{
pipesFlg=true;
}
int prev_pipe[2];
int next_pipe[2]; //can also say as curr_pipe
for (size_t i = 0; i < commands.size(); i++)
{
vector<string> tokens = tokenizeCommand(commands[i], ' ');
bool bgRunFlag = false;
if (tokens.back() == "bgRunTriggered")
{
bgRunFlag = true;
tokens.pop_back();
}
if (tokens[0] == "cd")
{
if (tokens.size() < 2)
{
cout << "Usage: cd <directory>" << endl;
}
else
{
if (chdir(tokens[1].c_str()) != 0)
{
perror("chdir failed");
}
}
continue; // Skip the rest of the loop and start over
}
if(tokens[0]=="mkfifo")
{
if(tokens.size()<2)
{
cout << "Usage: mkfifo <filename> " << endl;
}
else
{
if(mkfifo(tokens[1].c_str(), 0666) != 0) //if it does not return successful creation status (0)
{
perror("mkfifo failed!\n");
}
else{
cout << "named pipe : " << tokens[1] << " created successfully!\n";
}
}
continue;
}
if(i<commands.size()-1 && pipesFlg && pipe(next_pipe)==-1) //pipe creation failed in any command other than last command //in last command we donot make pipe
{
perror("pipe creation failed!\n");
exit(EXIT_FAILURE);
}
childCount++;
ret_Val = fork();
if (ret_Val < 0) {
cerr << "\nFork() failed!\n";
}
else if (ret_Val == 0)
{//child
cout << "\nchild: "<<getpid()<<"\n";
//the erase method in c++ having range flavor of paramters deletes the starting range element
//but doesn't deletes the ending range element and obviously it deletes the mid elements also
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]=="<") //we can also implement 0< here because they are exactly the same thing
{
int fd_read=open(tokens[i+1].c_str(), O_RDONLY);
dup2(fd_read, 0);
close(fd_read);
tokens.erase(tokens.begin()+i, tokens.begin()+i+2); //removing < and filename
// it will delete token[i] and token[i+1] but not token[i+2]
break;
}
}
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]==">"){ //we can also implement 1> here because they are exactly the same thing
int fd_write=open(tokens[i+1].c_str(), O_WRONLY | O_CREAT | O_TRUNC); //we can also do O_APPEND
dup2(fd_write, 1);
close(fd_write);
tokens.erase(tokens.begin()+i, tokens.begin()+i+2); //removing > and filename
// it will delete token[i] and token[i+1] but not token[i+2]
break;
}
}
for(int i=0;i<tokens.size();i++)
{
if(tokens[i]=="2>"){ //this has no alternative..
int fd_error=open(tokens[i+1].c_str(), O_WRONLY | O_CREAT | O_APPEND); //we can also do O_APPEND
dup2(fd_error, 2);
close(fd_error);
tokens.erase(tokens.begin()+i, tokens.begin()+i+2); //removing > and filename
// it will delete token[i] and token[i+1] but not token[i+2]
break;
}
}
////for testing..
//cout<<"\n\nprinting command tokens for testing after redirection(removal of < > etc):\n";
//for (string s : tokens)
// cout << s << endl;
if(pipesFlg)
{
if(i<commands.size()-1) //if current command is not the last command
{
dup2(next_pipe[1], STDOUT_FILENO); // Redirect stdout to write end of pipe
}
if(i>0) //if current command is not the first command
{
dup2(prev_pipe[0], STDIN_FILENO); // Redirect stdin to read end of previous pipe
}
close(prev_pipe[0]); // Close read end of previous pipe
close(prev_pipe[1]); // Close write end of previous pipe
close(next_pipe[0]); // Close read end of next pipe
close(next_pipe[1]); // Close write end of next pipe
}
char* args[tokens.size() + 1];
for (size_t i = 0; i < tokens.size(); i++)
args[i] = const_cast<char*>(tokens[i].c_str());
args[tokens.size()] = NULL;
if(execvp(args[0], args)==-1)
{
cout<<"\nCOMMAND NOT IDENTIFIED BY SHELL!\n";
exit(EXIT_FAILURE);
//OR exit(1);
}
cout<<endl;
exit(0);
}
else
{//parent
cout<<"\n_____________________________________\n";
cout << "\nparent: "<<getpid()<<"\n";
if(pipesFlg)
{
if(i<commands.size()-1)
{
close(next_pipe[1]); //close read end of prev pipe
}
if(i>0)
{
close(prev_pipe[0]); //close read end of prev pipe
}
// updating the prev_pipe array with the file descriptors of the next pipe.
// this is done to prepare for the next iteration of the loop, where
// the current pipe will become the previous pipe for the next command.
// basically we are shifting the prev_pipe ptr's for visual understanding..
prev_pipe[0]=next_pipe[0];
prev_pipe[1]=next_pipe[1];
}
if(!bgRunFlag)
{
while(childCount>0)
{
int status;
pid_t c_pid = wait(&status); //wait for child to terminate...
if(status==0)
cout<<"child with pid "<<c_pid<<" terminated!\n";
childCount--;
}
}
else
{
cout<<"\nchild running in background!\n";
}
}
}
}
cout << "\n***** shell terminated! *****\n";
cout << "\n";
//system("pause");
return 0;
}