-
Notifications
You must be signed in to change notification settings - Fork 0
/
msh.c
305 lines (239 loc) · 7.49 KB
/
msh.c
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
/*
Maverick Shell
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define BFR 512
#define MAX_ARGS 10
#define EXIT_SUCCESS 0
#define DELIMITER " \n\t"
/* Code to hande keyboard interrupt
Ctrl-C and Ctrl-Z
*/
static void handle_signal(int sig)
{
if(sig == 2)
printf("\nExiting process..\n");
else {
printf("\n Process suspended..\n");
}
}
// The Main Function
int main(void)
{
/*cmd character pointer to accept input
from user
*/
char* cmd = (char*)malloc(BFR);
/*code to declare ProcessID, status
array of processIds,history buffer
to store history of commands and
counter
*/
pid_t pid;
int status;
pid_t pidarr[10];
int i = 0;
int c = 0;
char history[15][BFR];
memset(&history, NULL, sizeof(history));
/* Code that declares a sigaction struct
whose structure object is used to call
the signal handler code above
returns error if less than 0
*/
struct sigaction act;
memset(&act, '\0', sizeof(act));
act.sa_handler = &handle_signal;
if(sigaction(SIGINT, &act, NULL) < 0) {
perror("sigaction: ");
return 1;
} else if(sigaction(SIGTSTP, &act, NULL) < 0) {
perror("sigaction: ");
return 1;
}
// Code to declare a character array to parse/tokenize the command
char* parse[MAX_ARGS];
/*Function to tokenize the given command by the specified
delimiter whitespace and tab
Using strsep function to sperate strings by whitespace characters
*/
void parseCommand(char* cmdStr)
{
char* sep;
int parseCount = 0;
char* working_str = strdup(cmdStr);
while((sep = strsep(&working_str, DELIMITER)) != NULL && (parseCount < MAX_ARGS)) {
parse[parseCount] = strndup(sep, BFR);
if(strlen(parse[parseCount]) == 0) {
parse[parseCount] = NULL;
}
parseCount++;
}
}
/* Function to display the last 10 processIDs
when showpids command is entered by the user
*/
void showpids()
{
int k;
if((i < 10) && (pidarr[9] == NULL)) {
for(k = 0; k < i; k++) {
printf("\n%d. %d", k + 1, pidarr[k]);
}
} else {
for(k = 0; k < 10; k++) {
printf("\n%d. %d", k + 1, pidarr[k]);
}
}
printf("\n");
}
/* Code to handle change directory
function, like cd, cd.. using
chdir() function.
*/
void handlecd()
{
if(parse[1] == NULL) {
fprintf(stderr, "msh> Folder Name \"cd\"\n");
} else if(chdir(parse[1]) != 0) {
perror("msh>");
}
}
/*Code to hande history command:
checks if the history buffer is not
full.If it is not full it executes the first for
loop, else executes the second for loop.
The for loop iterates through the history buffer
and prints the last 15 commands (max) if present
*/
void handleHistory()
{
int x;
if((c < 15) && (*history[14] == NULL || *history[14] == '\0')) {
for(x = 0; x < c; x++) {
printf("\n%d. %s", x + 1, history[x]);
}
} else {
for(x = 0; x < 15; x++) {
printf("\n%d. %s", x + 1, history[x]);
}
}
printf("\n");
}
/*Infinitely loops through the program,
accepts input from the user
*/
while(1) {
int f = 0;
// prints out the shell promt "msh>"
printf("msh>");
fgets(cmd, BFR, stdin);
// if exit or quit exit the shell
if(!(strcmp(cmd, "exit\n") && strcmp(cmd, "quit\n")))
exit(1);
/* Copy all commands into history buffer except
newline character
*/
if(c > 14)
c = 0;
if(strcmp(cmd, "\n") /*&& strcmp(cmd, "history\n") && strchr(cmd, '!') == NULL*/) {
strncpy(history[c], cmd, BFR);
c++;
}
/*if newline character, go to next
line and prompt
*/
if(!strcmp(cmd, "\n"))
continue;
/*if the command is showpids
call showpids function
*/
if(!strcmp(cmd, "showpids\n")) {
showpids();
continue;
}
/*if the comman entered is history
call handleHistory function
*/
if(!strcmp(cmd, "history\n")) {
handleHistory();
continue;
}
/*if the command is !n where n
is between 1 and fifteen from
the history, execute that cmd,
else display command not found
in history.
*/
if(strchr(cmd, '!') != NULL) {
int n = 0;
if(strlen(cmd) - 1 == 3)
n = (cmd[1] - '0') * 10 + (cmd[2] - '0');
else
n = cmd[1] - '0';
if((*history[n] == NULL) || (*history[n] == '\0')) {
printf("\n Command not found in history\n");
continue;
}
printf("you choose option %d so executing %s", n, history[n - 1]);
if(!strcmp(history[n - 1], "showpids\n")) {
showpids();
continue;
} else if(!strncmp(history[n - 1], "cd", 2)) {
parseCommand(history[n - 1]);
handlecd();
continue;
} else if(!strcmp(history[n - 1], "history\n")) {
handleHistory();
continue;
}
parseCommand(history[n - 1]);
} else {
parseCommand(cmd);
}
/*if the command is cd call
the handlecd function.
*/
if(!strncmp(cmd, "cd", 2)) {
parseCommand(cmd);
handlecd();
continue;
}
/*We are forking a process,
if it is a child process,
call the execvp function
to execute the commands.
if execvp fails, print
command not found.The else
part is the parent block,it
waits for the child process
to finish and returns.Meanwhile
the processIDs are copied into the
processID array.
*/
if((pid = fork()) < 0) {
exit(1);
}
else if(pid == 0) {
if(execvp(parse[0], parse) < 0) {
printf("Command not found: %s", cmd);
exit(1);
}
}
else {
while(wait(&status) != pid)
;
if(i > 9)
i = 0;
pidarr[i] = pid;
i++;
}
}
return 0;
}