-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
258 lines (224 loc) · 8.48 KB
/
Copy pathmyshell.c
File metadata and controls
258 lines (224 loc) · 8.48 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
/********************************************************************************************
This is a template for assignment on writing a custom Shell.
Students may change the return types and argumentuments of the functions given in this template,
but do not change the names of these functions.
Though use of any extra functions is not recommended, students may use new functions if they need to,
but that should not make code unnecessorily complex to read.
Students should keep names of declared variable (and any new functions) self explanatory,
and add proper comments for every logical step.
Students need to be careful while forking a new process (no unnecessory process creations)
or while inserting the single handler code (should be added at the correct places).
Finally, keep your filename as myshell.c, do not change this name (not even myshell.cpp,
as you not need to use any features for this assignment that are supported by C++ but not by C).
*********************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // exit()
#include <unistd.h> // fork(), getpid(), exec()
#include <sys/wait.h> // wait()
#include <signal.h> // signal()
#include <fcntl.h> // close(), open()
typedef enum {EXE_SINGLE, EXE_PARALLEL, EXE_SEQUENTIAL, EXE_REDIRECT}STATUS_CODE;
#define MAXARGLEN 128
#define MAXPATHLEN 2048
char* trim(char* str) {
int len=strlen(str);
char *trimmed=(char*)malloc(sizeof(char)*(len+1));
int start=0, end=len-1, p=0;
while(start<len && str[start]==' ') start++;
while(end>=0 && str[end]==' ') end--;
while(start<=end)
trimmed[p++]=str[start++];
trimmed[p]='\0';
free(str);
return trimmed;
}
char* preprocess(char* original_input) {
char* preprocessed_input=(char*)malloc(sizeof(char)*(4*strlen(original_input)+1));
int p=0;
for(int i=0;i<strlen(original_input);i++) {
char c=original_input[i];
if(i<strlen(original_input)-1 && c=='&' && original_input[i+1]=='&') {
preprocessed_input[p++]=' '; preprocessed_input[p++]='&'; preprocessed_input[p++]='&';
preprocessed_input[p++]=' ';
i++;
}
else if(i<strlen(original_input)-1 && c=='#' && original_input[i+1]=='#') {
preprocessed_input[p++]=' '; preprocessed_input[p++]='#'; preprocessed_input[p++]='#';
preprocessed_input[p++]=' ';
i++;
}
else if(c=='>') {
preprocessed_input[p++]=' '; preprocessed_input[p++]='>'; preprocessed_input[p++]=' ';
}
else if(c==';') {
preprocessed_input[p++]=' '; preprocessed_input[p++]=';'; preprocessed_input[p++]=' ';
}
else preprocessed_input[p++]=c;
}
preprocessed_input[p]='\0';
free(original_input);
return preprocessed_input;
}
STATUS_CODE parseInput(char *arguments[MAXARGLEN], char** parsedInput[MAXARGLEN], int* parsedInputLen) {
// This function will parse the input string into multiple commands or a single command with argumentuments depending on the delimiter (&&, ##, >, or spaces).
STATUS_CODE returnStatus=EXE_SINGLE;
int i=0;
int j=0; parsedInput[j]=(char**)malloc(sizeof(char*)*MAXARGLEN);
int p=0;
while(arguments[i]!=NULL) {
if(strcmp(arguments[i],"&&")!=0 && strcmp(arguments[i],"##")!=0 && strcmp(arguments[i],">")!=0 && strcmp(arguments[i],";")!=0) {
parsedInput[j][p]=arguments[i];
p++;
} else {
if(strcmp(arguments[i],"##")==0 || strcmp(arguments[i],";")==0) returnStatus=EXE_SEQUENTIAL;
else if(strcmp(arguments[i],"&&")==0) returnStatus=EXE_PARALLEL;
else if(strcmp(arguments[i],">")==0) returnStatus=EXE_REDIRECT;
parsedInput[j][p]=NULL;
p=0;
j++;
parsedInput[j]=(char**)malloc(sizeof(char*)*MAXARGLEN);
}
i++;
}
parsedInput[j][p]=NULL;
*parsedInputLen=j+1;
return returnStatus;
}
void executeCommand(char** parsedInput[MAXARGLEN], int currentInput) {
// This function will fork a new process to execute a command
int st_code;
char* command=parsedInput[currentInput][0];
if(strcmp(command,"cd")==0) {
if(parsedInput[currentInput][2]!=NULL) {
printf("Shell: Incorrect command\n");
return;
}
st_code=chdir(parsedInput[currentInput][1]);
if (st_code!=0) {
printf("Shell: Incorrect command\n");
}
} else if(strcmp(command,"exit")==0) {
printf("Exiting shell...\n");
exit(0);
} else {
int pid=fork();
if(pid==0) {
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
execvp(parsedInput[currentInput][0], parsedInput[currentInput]);
printf("Shell: Incorrect command\n");
exit(1);
} else if(pid>0) {
wait(NULL);
}
}
}
void executeParallelCommands(char **parsedInput[MAXARGLEN], int parsedInputLen) {
// This function will run multiple commands in parallel
int pids[parsedInputLen];
for(int i=0;i<parsedInputLen;i++) {
int pid=fork();
if(pid==0) {
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
char *command=parsedInput[i][0];
if(strcmp(command,"cd")==0) {
int st_code;
if(parsedInput[i][2]!=NULL) {
printf("Shell: Incorrect command\n");
} else
st_code=chdir(parsedInput[i][1]);
if(st_code!=0) printf("Shell: Incorrect command\n");
exit(0);
} else {
execvp(command, parsedInput[i]);
printf("Shell: Incorrect command\n");
exit(1);
}
} else if(pid>0) pids[i]=pid;
}
for(int i=0;i<parsedInputLen;i++) {
waitpid(pids[i],NULL,0);
}
}
void executeSequentialCommands(char **parsedInput[MAXARGLEN], int parsedInputLen) {
// This function will run multiple commands sequentially
for(int i=0;i<parsedInputLen;i++) {
executeCommand(parsedInput,i);
}
}
void executeCommandRedirection(char **parsedInput[MAXARGLEN]) {
char *command=parsedInput[0][0];
char *file=parsedInput[1][0];
int pid=fork();
if(pid==0) {
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
int fd=open(file,O_CREAT|O_WRONLY|O_TRUNC,0644);
dup2(fd,STDOUT_FILENO);
close(fd);
execvp(command,parsedInput[0]);
exit(1);
}
else if (pid > 0) {
waitpid(pid, NULL, 0);
}
}
int main() {
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
// Initial declarations
STATUS_CODE executionType;
int i=0;
char *input=NULL, *arguments[MAXARGLEN];
size_t buff_size=0;
ssize_t len=0;
char currentWorkingDirectory[MAXPATHLEN];
char** parsedInput[MAXARGLEN]; int parsedInputLen=0;
while(1) {// This loop will keep your shell running until user exits.
getcwd(currentWorkingDirectory,MAXPATHLEN);
printf("%s$",currentWorkingDirectory);// Print the prompt in format - currentWorkingDirectory$
// accept input with 'getline()'
len=getline(&input,&buff_size,stdin);
if(len==-1) {
printf("Exiting shell...\n");
break;
}
input[strcspn(input,"\n")]='\0';
input=trim(input);
char* argument; i=0;
if(strlen(input)==0) continue;
input=preprocess(input);
char *copy=strdup(input);
//Parse input with 'strsep()' for different symbols (&&, ##, >) and for spaces.
argument=strsep(©," ");
while(argument!=NULL) {
if(*argument!='\0') {
arguments[i]=argument;
i++;
}
argument=strsep(©," ");
}
arguments[i]=NULL;
if(strcmp(input,"exit")==0) {// When user uses exit command.
printf("Exiting shell...\n");
break;
}
executionType=parseInput(arguments,parsedInput,&parsedInputLen);
if(executionType==EXE_PARALLEL)
executeParallelCommands(parsedInput,parsedInputLen); // This function is invoked when user wants to run multiple commands in parallel (commands separated by &&)
else if(executionType==EXE_SEQUENTIAL)
executeSequentialCommands(parsedInput,parsedInputLen); // This function is invoked when user wants to run multiple commands sequentially (commands separated by ##)
else if(executionType==EXE_REDIRECT)
executeCommandRedirection(parsedInput); // This function is invoked when user wants redirect output of a single command to and output file specificed by user
else
executeCommand(parsedInput,0); // This function is invoked when user wants to run a single commands
while(parsedInputLen) {
free(parsedInput[parsedInputLen-1]);
parsedInputLen--;
}
}
free(input);
return 0;
}