-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.cpp
More file actions
328 lines (305 loc) · 12.9 KB
/
Copy pathshell.cpp
File metadata and controls
328 lines (305 loc) · 12.9 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
#include<unistd.h> // for geteuid(), gethostname()
#include<pwd.h> // for getpwuid()
#include<stdio.h> // for scanf printf
#include<iostream>
#include<sys/wait.h> //for waitpid
#include<signal.h>
#include "currwd.h"
#include "tabSugg.h"
#include "tokenization.h"
#include "echoFn.h"
#include "pwdFn.h"
#include "pInfoFn.h"
#include "searchFn.h"
#include "handleLSFn.h"
#include "redirection.h"
#include "pipeCmd.h"
using namespace std;
int foregroundPID=0;
int backrounfPGrp;
void handleSIGINT(int signal)
{
kill(foregroundPID,SIGINT);
}
void handleSIGTSTP(int signal)
{
tcsetpgrp(foregroundPID,backrounfPGrp);
kill(foregroundPID,SIGTSTP);
}
int main()
{
/*0 and 1 denote STDIN_FILENO and STDOUT_FILENO referring to stdin and stdout so when we do
dup2(0,STDIN_FILENO) we are not changing anything. 0 means STDIN_FILENO only. It is not the file desriptor of stdin,
It is stdin.
*/
int stdinFD = dup(STDIN_FILENO);
int stdoutFD = dup(STDOUT_FILENO);
// Signal handling
backrounfPGrp=tcgetpgrp(STDIN_FILENO);
signal(SIGINT,handleSIGINT);
signal(SIGTSTP,handleSIGTSTP);
//getting hostname and username
struct passwd *pw;
long uid = geteuid();
pw = getpwuid(uid);
char hostname[1024];
tcgetattr(STDIN_FILENO,&old_attributes);
new_attributes=old_attributes;
new_attributes.c_lflag=new_attributes.c_lflag & (~ICANON) & (~ECHO);
tcsetattr(STDIN_FILENO,TCSANOW,&new_attributes);
if(!pw)
{
printf("Cannot find username for uid: %ld\n",uid);
_exit(1);
}
else
{
readFromFile();
gethostname(hostname,1024);
//getting current working directory from currwd.h header file
int get_curr_wd=get_currwd();
if(get_curr_wd!=0)
{
_exit(1);
}
//
string homePath=wd;
bool noError=true;
string previousDir=wd;
string username=pw->pw_name;
string shellPrompt=username+"@"+hostname+":~>";
int flagToExit=0;
while(noError)
{
input.clear();
int exitCondition=getTabSuggestion(shellPrompt);
if(exitCondition==1)
{
break;
}
if(!input.empty())
{
vector<string> fullCommands=getTokens(input,";");
for(string command:fullCommands)
{
stdinFD = dup(STDIN_FILENO);
stdoutFD = dup(STDOUT_FILENO);
if((command.find('|')!=-1))
{
pipeRedir(command,stdinFD,stdoutFD,foregroundPID);
}
else if((command.find('<')!=-1)&&(command.find('>')==-1)) //only input redirection
{
inpRedir(command,stdinFD,foregroundPID);
}
else if( (command.find('<')==-1) && (command.find('>')!=-1)) //only output redirection
{
outRedir(command,stdoutFD,foregroundPID);
}
else if((command.find('<')!=-1)&&(command.find('>')!=-1)) //both input/output redirection
{
inoutRedir(command,stdinFD,stdoutFD,foregroundPID);
}
else
{
if(command.compare("")==0)
{
string errMsg="An error occurred while parsing the string. Please recheck your commands.\n";
write(STDOUT_FILENO,errMsg.c_str(),errMsg.size());
continue;
}
string dupCommand=command;
string shellCommand= strtok(dupCommand.data(), " "); //taking command name
vector<string> arguments=getTokens(command," "); //taking all arguments
if(shellCommand.compare("echo")==0)
{
if(command.size()<5)
{
string errMsg="Please provide an input for echo command\n";
write(STDOUT_FILENO,errMsg.c_str(),errMsg.size());
}
else
{
printEchoString(command.substr(5,command.size()-5));
}
}
else if(shellCommand.compare("pwd")==0)
{
printPwdString();
}
else if(shellCommand.compare("pinfo")==0)
{
printPinfo(command);
}
else if(shellCommand.compare("search")==0)
{
if(command.size()<7)
{
string errMsg="Please provide an input for the search command\n";
write(STDOUT_FILENO,errMsg.c_str(),errMsg.size());
}
else
{
printSearchResult(command.substr(7,command.size()-7));
}
}
else if(shellCommand.compare("history")==0)
{
int nOfArgs=arguments.size();
if(nOfArgs==1)
{
printHistory(10);
}
else if(nOfArgs==2)
{
string histArg=arguments[1];
const char* numbers="0123456789";
int index=histArg.find_first_not_of(numbers);
if(index!=-1)
{
string errMsg="Invalid argument: It should be a positive integer\n";
write(STDOUT_FILENO,errMsg.c_str(),errMsg.size());
}
else
{
int intArg=stoi(histArg);
printHistory(intArg);
}
}
else
{
string errMsg="Invalid number of arguments given for history command\n";
write(STDOUT_FILENO,errMsg.c_str(),errMsg.size());
}
}
else if(shellCommand.compare("cd")==0)
{
int nOfArgs=arguments.size();
if(nOfArgs==1)
{
shellPrompt=username+"@"+hostname+":~>";
previousDir=wd;
chdir(homePath.c_str());
get_curr_wd=get_currwd();
if(get_curr_wd!=0)
{
_exit(1);
}
}
else if(nOfArgs>2)
{
string errMsg="cd: Too many arguments\n";
write(STDOUT_FILENO,errMsg.c_str(),errMsg.size());
}
else
{
if(arguments[1].compare("~")==0)
{
arguments[1]=homePath;
}
if(arguments[1].compare("-")==0)
{
arguments[1]=previousDir;
}
previousDir=wd;
int cdir=chdir(arguments[1].c_str());
if(cdir==-1)
{
perror("An error occurred while trying to change the directory");
}
else
{
get_curr_wd=get_currwd();
if(get_curr_wd!=0)
{
_exit(1);
}
string newPath=wd;
if(homePath.compare(newPath)==0)
{
shellPrompt=username+"@"+hostname+":~>";
}
else
{
int posOfHomePath=newPath.find(homePath);
if(posOfHomePath==-1)
{
shellPrompt=username+"@"+hostname+":~"+newPath+">";
}
else
{
int startFrom=posOfHomePath+homePath.size();
int sizeOfPath=newPath.size()-startFrom;
string afterHomePath=newPath.substr(startFrom,sizeOfPath);
shellPrompt=username+"@"+hostname+":~"+afterHomePath+">";
}
}
}
}
}
else if(shellCommand.compare("exit")==0)
{
flagToExit=1;
}
else if(shellCommand.compare("ls")==0)
{
handleLS(arguments,homePath);
}
else
{
int pid=fork();
if(pid==-1)
{
perror("An error occurred while trying to execute the process");
}
else if(pid==0) //child code
{
int length=arguments.size();
char* execvpArgs[length+1];
int i;
for(i=0;i<length;i++)
{
if(arguments[i].compare("&")==0)
{
break;
}
execvpArgs[i]=arguments[i].data();
}
execvpArgs[i]=nullptr;
int child=execvp(shellCommand.c_str(),execvpArgs);
if(child==-1)
{
perror("Invalid Command");
_exit(1);
}
}
else //parent
{
if(command.find('&')!=-1)
{
printf("%d\n",pid);
}
else
{
foregroundPID=pid;
int exitStatus;
int pidOfChild=waitpid(pid,&exitStatus,WUNTRACED);
if(pidOfChild==-1)
{
perror("An error occurred");
}
}
}
}
}
}
}
writeToFile(homePath);
if(flagToExit==1)
{
break;
}
}
}
tcsetattr(STDIN_FILENO,TCSANOW,&old_attributes);
}