forked from goduphi/cse3320-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsh.c
More file actions
513 lines (425 loc) · 11.7 KB
/
msh.c
File metadata and controls
513 lines (425 loc) · 11.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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
Author: Sarker Nadir Afridi Azmi
ID: 1001644326
Resources used: https://github.com/CSE3320/Shell-Assignment
*/
// The only thing I know is that defining this allows me to used
// the get_current_dir_name function
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdbool.h>
#include <ctype.h>
// The maximum number of characters that a the command line supports
#define MAX_COMMAND_LENGTH 1024
// The maximum number of arguments that the command line supports
#define MAX_NUM_OF_ARGUMENTS 12
#define MAX_NUM_COMMANDS 14
// We want to split our command line up into tokens
// so we need to define what delimits our tokens.
// In this case white space
// will separate the tokens on our command line
#define WHITESPACE_DELIM " \t\n"
#define MAX_NUM_OF_PATHS 4
#define MAX_STRING_LENGTH 15
#define CHANGE_DIRECTORY "cd"
#define LAST_CMD "!"
#define CURRENT_DIR "./"
#define PARENT_DIR get_current_dir_name()
/*
This struct defines the queue
*/
typedef struct Queue
{
char command[MAX_COMMAND_LENGTH];
struct Queue *next_ptr;
} Queue;
/*
This function frees the entire queue.
Param 1: Pass in queue head.
Param 2: Pass in queue tail.
Param 3: Pass in the command to be stored.
*/
bool enQueue(Queue **head, Queue **tail, char cmd[])
{
Queue * new_node = (Queue *)malloc(sizeof(Queue));
strcpy(new_node->command, cmd);
new_node->next_ptr = NULL;
if(*head == NULL)
{
*head = *tail = new_node;
}
else
{
(*tail)->next_ptr = new_node;
(*tail) = new_node;
}
return true;
}
/*
This function checks to see whether the queue is empty or not!
Param: Pass in queue head.
*/
bool empty(Queue *head)
{
if(head == NULL)
return true;
else
return false;
}
/*
This function frees the first node
Param: Pass in queue head.
*/
void deQueue(Queue **head)
{
Queue *temp = (*head)->next_ptr;
if(*head == NULL)
{
printf("No commands in history");
}
else
{
free(*head);
*head = temp;
}
}
/*
This function frees the entire queue.
Param: Pass in queue head.
*/
void free_queue(Queue **head)
{
while(*head != NULL)
deQueue(head);
}
/*
This function prints all of the contents inside of the queue.
Param: Pass in queue head.
*/
void list_commands(Queue *head)
{
int counter = 0;
Queue * temp = head;
while(temp != NULL)
{
printf("%d. %s\n", counter, temp->command);
temp = temp->next_ptr;
counter++;
}
}
/*
This function allows the directory to change.
Param: Pass in a string that contains the path to the desired directory.
*/
void change_directory(char **cmd)
{
char temp_path[MAX_COMMAND_LENGTH];
strcpy(temp_path, CURRENT_DIR);
int token_idx = 1;
while(cmd[token_idx] != NULL)
{
// This check is to help change to a directory that has a space in its namespace
// Let's say Homework 1
if((cmd[2] != NULL) && ((token_idx % 2) == 0))
{
strcat(temp_path, " ");
}
strcat(temp_path, cmd[token_idx]);
token_idx++;
}
chdir(temp_path);
}
/*
This function executes the commands based on its index inside of the queue.
Param 1: Command index.
Param 2: History queue.
*/
char * execute_specified_command(int command_number, Queue *head)
{
Queue *temp = head;
int counter = 0;
if(head == NULL)
{
printf("No commands have been executed yet!\n");
return "\n";
}
while(temp != NULL)
{
if(counter == command_number)
{
counter = 0;
return temp->command;
}
temp = temp->next_ptr;
counter++;
}
return NULL;
}
/*
This function takes a string and converts it into lower case.
Param 1: Char array to be converted.
Praam 2: Char array where the converted string is to be stored.
*/
void str_to_lower(char input_string[], char output_string[])
{
int char_idx = 0;
for(char_idx = 0; char_idx < strlen(input_string); char_idx++)
{
if(input_string[char_idx] == '\n')
{
output_string[char_idx] = '\0';
}
else
{
output_string[char_idx] = tolower(input_string[char_idx]);
}
}
return;
}
// This function is only meant to be use if the '!' command is used
// Takes whatever the integer value is right after !, let's say for !4, the 4
// Converts it into an integer and returns it
int parse_command_number(char *cmd_str)
{
int command_number;
sscanf(cmd_str, "%*c%d", &command_number);
return command_number;
}
/*
This function removes the \n from the passed in string.
*/
void remove_slashn(char cmd[])
{
if((cmd[strlen(cmd) - 1]) == '\n')
cmd[strlen(cmd) - 1] = '\0';
}
/*
This function frees all of the allocated memory for the command line arguments.
Param: Pass in array of pointers to char of command line arguments.
*/
void free_token_arr(char *token[])
{
int token_idx = 0;
for(token_idx = 0; token[token_idx] != NULL && token_idx < MAX_NUM_OF_ARGUMENTS; token_idx++)
{
free(token[token_idx]);
}
}
/*
This function checks for the pipe symbol, '|', inside of the command string
Param 1: Pointer to char array of arguments
Param 2: Address of an int which stores the index in which the pipe symbol is at
*/
char * check_for_pipe(char *token[], int *pipe_idx)
{
int token_idx = 0;
for(token_idx = 0; token[token_idx] != NULL && token_idx < MAX_NUM_OF_ARGUMENTS; token_idx++)
{
if(strcmp(token[token_idx], "|") == 0)
{
*pipe_idx = token_idx;
return token[token_idx];
}
}
return NULL;
}
int main(void)
{
// This string will hold the entire string written down into the command line
char * cmd_str = (char *)malloc(MAX_COMMAND_LENGTH);
char paths_array[MAX_NUM_OF_PATHS][MAX_STRING_LENGTH] = {"/usr/local/bin/",
"/usr/bin/",
"/bin/", "./"};
// holds commands history information
Queue *history_qhead = NULL, *history_qtail = NULL;
Queue *pid_qhead = NULL, *pid_qtail = NULL;
printf("\n\033[01;33m**********************************************\n"
"Welcome to MAV SHELL, the Shell of your dreams\n"
"**********************************************\033[0m\n\n");
int cmd_counter = 0;
int pid_counter = 0;
bool enQueued = false;
while(1)
{
// Print out the msh prompt
printf("\033[01;33mmsh>\033[0m ");
// Read the command from the commandline.
// The maximum number that can be read is specified by MAX_COMMAND_LENGTH
// The purpose of using the while loop to get user input, rather than a simple
// fgets() is so that if the user presses enter, it keeps asking for input
// since fgets returns NULL when there is no input
while(!fgets(cmd_str, MAX_COMMAND_LENGTH, stdin));
char exit_quit[strlen(cmd_str)];
str_to_lower(cmd_str, exit_quit);
if(strcmp(exit_quit, "quit") == 0 || strcmp(exit_quit, "exit") == 0)
{
exit(0);
}
if(cmd_str[0] == '!')
{
// call function to find node with command and set cmd_str to that
// command for processing
char * cmd_to_execute = execute_specified_command(parse_command_number(cmd_str),
history_qhead);
if(cmd_to_execute == NULL)
{
printf("Command not in history.\n");
continue;
}
else
{
strcpy(cmd_str, cmd_to_execute);
}
}
if(strcmp(cmd_str, "\n") == 0)
{
continue;
}
if(cmd_counter > MAX_NUM_COMMANDS)
{
deQueue(&history_qhead);
}
if((pid_counter > 1 + MAX_NUM_COMMANDS) && enQueued)
{
enQueued = false;
deQueue(&pid_qhead);
}
//This 'array of string' will hold all of the commands from the command line
char *token[MAX_NUM_OF_ARGUMENTS];
int token_count = 0;
// Pointer to point to the token parsed by strsep
char *arg_ptr;
char *working_str = strdup(cmd_str);
remove_slashn(cmd_str);
if(enQueue(&history_qhead, &history_qtail, cmd_str))
{
cmd_counter++;
}
// we are going to move the working_str pointer so
// keep track of its original value so we can deallocate
// the correct amount at the end
char *working_root = working_str;
while((arg_ptr = strsep(&working_root, WHITESPACE_DELIM)) != NULL
&& (token_count < MAX_NUM_OF_ARGUMENTS))
{
token[token_count] = strndup(arg_ptr, strlen(arg_ptr));
// if we hit a "\n", after which is an empty string, we replace it with NULL
// this comes in handy when we put all of the arguments inside of execl
if(strlen(token[token_count]) == 0)
{
free(token[token_count]);
token[token_count] = NULL;
}
token_count++;
}
free(working_str);
free(working_root);
if(strcmp(token[0], CHANGE_DIRECTORY) == 0)
{
change_directory(token);
}
else if(strcmp(token[0], "history") == 0)
{
if(!empty(history_qhead))
list_commands(history_qhead);
else
printf("No commands have been executed yet.\n");
}
else if(strcmp(token[0], "showpids") == 0)
{
if(!empty(pid_qhead))
list_commands(pid_qhead);
else
printf("No processes have been spawned yet.\n");
}
else if(token_count > 0)
{
/*
fork() allows for the creation of a child process from the parent process
where fork() was called.
*/
int fd[2];
char *pipe_addr_ptr = NULL;
int pipe_idx = 0;
pid_t child_pid = fork();
if(child_pid == -1)
{
perror("fork failed: ");
}
else if(child_pid == 0)
{
if((pipe_addr_ptr = check_for_pipe(token, &pipe_idx)) != NULL)
{
pipe(fd);
if(!fork())
{
token[pipe_idx] = NULL;
close(STDOUT_FILENO); /* close normal stdout */
dup(fd[1]); /* make stdout same as pfds[1] */
close(fd[0]); /* we don't need this */
}
else
{
close(STDIN_FILENO); /* close normal stdin */
dup(fd[0]); /* make stdin same as pfds[0] */
close(fd[1]); /* we don't need this */
//------------------------------------------
int execvp_status = 0;
execvp_status = execvp(token[pipe_idx + 1], token + pipe_idx + 1);
if(execvp_status == -1)
{
printf("%s: Command not found\n", token[pipe_idx + 1]);
}
}
}
int execl_status = 0;
int path_idx = 0;
for(path_idx = 0; path_idx < MAX_NUM_OF_ARGUMENTS; path_idx++)
{
char path_to_search_cmd[20];
// create the path that will be used as first arg of execl
// for example, ls is concatenated to /bin/
strcpy(path_to_search_cmd, paths_array[path_idx]);
strcat(path_to_search_cmd, token[0]);
execl_status = execl(path_to_search_cmd, token[0], token[1],
token[2], token[3],
token[4], token[5],
token[6], token[7],
token[8], token[9],
token[10], NULL);
}
if(execl_status == -1)
{
printf("%s: Command not found\n", cmd_str);
}
token[pipe_idx] = pipe_addr_ptr;
}
// enQueue pid info inside of the parent because child has no idea about
// pid returned to parent
else
{
char pid_buffer[20];
sprintf(pid_buffer, "%d", (int)child_pid);
if(enQueue(&pid_qhead, &pid_qtail, pid_buffer))
{
enQueued = true;
pid_counter++;
}
}
int child_status = 0;
// Wait for the child process to exit
waitpid(child_pid, &child_status, 0);
}
free_token_arr(token);
}
// Do a bit of house keeping by freeing all malloced data
free(cmd_str);
free_queue(&history_qhead);
free_queue(&pid_qhead);
return EXIT_SUCCESS;
}