-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
436 lines (381 loc) · 10.9 KB
/
shell.c
File metadata and controls
436 lines (381 loc) · 10.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
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
#include "shell.h"
/**
* Simple Shell, developed by
* ------------------
* Jamie McKitting
* Mary Amanda Iferi-Norman
* James Dick
* Cameron MacReady
* Yohanes Zhelezov
* ------------------
* Group 28
*/
int main() {
char* user_input = malloc(sizeof(char) * BUFFER_SIZE);
char* alias_cmd = malloc(sizeof(char) * BUFFER_SIZE);
char* args[ARG_LIMIT];
char* orig_path = getenv("PATH");
History* history = load_history();
Alias_List alias_list = load_aliases();
chdir(getenv("HOME"));
while(get_input(user_input)) {
strcpy(alias_cmd, "");
split_str(args, user_input, " \n\t|><&;");
insert_aliases(alias_list, args, user_input, alias_cmd);
free(args[0]);
invoke_history(history, user_input);
split_str(args, user_input, " \n\t|><&;");
insert_aliases(alias_list, args, user_input, alias_cmd);
add_entry(history, user_input, alias_cmd);
free(args[0]);
split_str(args, user_input, " \n\t|><&;");
exec_cmd(args, handle_cmd(args, history, alias_list));
free(args[0]);
}
if(feof(stdin)) printf("\n");
set_new_path(orig_path);
save_history(history);
save_aliases(alias_list);
free(user_input);
free(alias_cmd);
}
/**
* Displays a prompt and gets the user input.
* Returns NULL if the user has indicated they wish to exit.
* @param user_input Buffer in which to place the user input
* @return The result of the input prompt.
*/
char* get_input(char* user_input) {
display_prompt();
char* result = fgets(user_input, BUFFER_SIZE, stdin);
if(strncmp(user_input, "exit", 4) == 0)
result = NULL;
return result;
}
/**
* Displays the prompt for user input,
* in the format: username@hostname:/path/to/directory$
*/
void display_prompt() {
char username[32];
strcpy(username, getenv("USER"));
char hostname[255];
gethostname(hostname, 255);
char directory[4096];
getcwd(directory, 4096);
/**
* \033 is an escape sequence,
* [1;92m and [1;94m are colour codes,
* [0m is to reset
*/
printf("\033[1;92m%s@%s\033[0m:\033[1;94m%s\033[0m$ ",
username, hostname, directory);
}
/**
* Populates an array of strings by tokenizing the user input
*
* @param array Array in which to place the tokenized strings
* @param user_input Buffer holding the user input to be tokenized
* @param separators Buffer holding the separators
* @return The number of strings
*/
int split_str(char** array, char* user_input, char* separators) {
char* token = malloc(sizeof(char) * BUFFER_SIZE);
strcpy(token, user_input);
strtok(token, separators);
int size = strlen(token);
if(token[size - 1] == '\n') token[size - 1] = 0;
if(!strcmp(token, "")) token = NULL;
int count = 0;
while(token) {
array[count++] = token;
token = strtok(NULL, separators);
}
array[count] = NULL;
return count;
}
/**
* Invokes the appropriate command from history
* based off of the command entered by the user.
*
* @param history The data structure in which the history is stored
* @param user_input The most recent command from the user
*/
void invoke_history(History* history, char* user_input) {
if(user_input[0] == '!') {
char* error_msg = get_entry(history, user_input);
if(!strcmp(user_input, "")) {
printf("%s", error_msg);
}
free(error_msg);
}
}
/**
* Create a child process and execute the user's arguments
*
* @param args Array containing the arguments to be executed
* @param stop Bool set to true if no command is to be ran or an internal command was ran
*/
void exec_cmd(char** args, bool stop) {
if(stop) return;
pid_t c_pid, pid;
int status;
c_pid = fork();
if(c_pid == -1) { // fork failed
perror("Error");
_exit(1);
}
else if(c_pid == 0) { // child
execvp(args[0], args);
fprintf(stderr, "Command [%s] failed: %s\n", args[0], strerror(errno));
_exit(1);
}
else if(c_pid > 0){ // parent
if((pid = wait(&status)) < 0) {
perror("Error");
_exit(1);
}
}
}
/**
* Function dedicated to the 'cd' internal command
*
* @param args Array containing the arguments
*/
void changedir_cmd(char** args) {
if(args[1] == NULL) {
chdir(getenv("HOME"));
}
else if(args[2] == NULL) {
set_dir(args[1]);
}
else {
printf(TOO_MANY_ARGS "please only enter one path\n");
}
}
/**
* Function dedicated to the 'getpath' internal command
*
* @param args Array containing the arguments
*/
void getpath_cmd(char** args) {
if(args[1] == NULL) {
printf("PATH: %s\n", getenv("PATH"));
}
else {
printf(TOO_MANY_ARGS "please enter the command without any arguments\n");
}
}
/**
* Function dedicated to the 'setpath' internal command
*
* @param args Array containing the arguments
*/
void setpath_cmd(char** args) {
if(args[1] == NULL) {
printf(TOO_FEW_ARGS "please enter a path to set\n");
}
else if(args[2] == NULL) {
get_new_path(args[1]);
}
else {
printf(TOO_MANY_ARGS "please only enter one path\n");
}
}
/**
* Function dedicated to the 'history' internal command
*
* @param args Array containing the arguments
* @param history Pointer to the history structure
*/
void history_cmd(char** args, History* history) {
if(args[1] == NULL) {
print_history(history);
}
else {
printf(TOO_MANY_ARGS "please enter the command without any arguments\n");
}
}
/**
* Function dedicated to the 'alias' internal command
*
* @param args Array containing the arguments to be executed
* @param alias_list Pointer to the alias structure
*/
void alias_cmd(char** args, Alias_List alias_list) {
if(args[1] == NULL) {
print_aliases(alias_list);
}
else if(args[2] == NULL) {
printf(TOO_FEW_ARGS "please enter the command and its arguments aswell\n");
}
else {
char* alias_ptr = reconstruct_args(args, 1, 1);
char* full_command_ptr = reconstruct_args(args, 2, -1);
add_alias(alias_list, alias_ptr, full_command_ptr);
free(alias_ptr);
free(full_command_ptr);
}
}
/**
* Function dedicated to the 'unalias' internal command
*
* @param args Array containing the arguments to be executed
* @param alias_list Pointer to the alias structure
*/
void unalias_cmd(char** args, Alias_List alias_list) {
if(args[1] == NULL) {
printf(TOO_FEW_ARGS "please enter an alias to remove\n");
}
else if(args[2] == NULL) {
remove_alias(alias_list, args[1]);
}
else {
printf(TOO_MANY_ARGS "please enter only one alias to remove\n");
}
}
/**
* Handle internal commands
*
* @param args Array containing the arguments to be executed
* @param history Pointer to the history structure
* @param alias_list Pointer to the alias structure
* @return Bool value to indicate whether the command should execute externally
*/
bool handle_cmd(char** args, History* history, Alias_List alias_list) {
if(args[0] == NULL) return true;
if(strcmp(args[0], "cd") == 0) {
changedir_cmd(args);
return true;
}
else if(strcmp(args[0], "getpath") == 0) {
getpath_cmd(args);
return true;
}
else if(strcmp(args[0], "setpath") == 0) {
setpath_cmd(args);
return true;
}
else if(strcmp(args[0], "history") == 0) {
history_cmd(args, history);
return true;
}
else if(strcmp(args[0], "alias") == 0) {
alias_cmd(args, alias_list);
return true;
}
else if(strcmp(args[0], "unalias") == 0) {
unalias_cmd(args, alias_list);
return true;
}
return false;
}
/**
* Make a copy of the specified arguments
*
* @param args Array containing the arguments to be executed
* @param start Integer which indicates the start point of the array
* @param end Integer which indicates the end point of the array
* @return Buffer containing the arguments split by a space character
*/
char* reconstruct_args(char** args, int start, int end) {
char* buffer = malloc(sizeof(char) * BUFFER_SIZE);
strcpy(buffer, "");
while((start <= end && args[start] != NULL) || (end == -1 && args[start] != NULL)) {
strcat(buffer, args[start++]);
if((end != -1 && start > end) || (end == -1 && args[start] == NULL)) break;
strcat(buffer, " ");
}
return buffer;
}
/**
* Check if the path is a valid directory
*
* @param user_input String containing the user entered path
*/
void get_new_path(char* user_input) {
char new_path[BUFFER_SIZE];
strcpy(new_path, user_input);
DIR* dir = opendir(user_input);
if(dir) {
set_new_path(new_path);
closedir(dir);
}
else {
perror("Error");
}
}
/**
* Sets the specified path to the PATH environment variable
*
* @param new_path String containing the new path
*/
void set_new_path(char* new_path) {
if(new_path != NULL) {
if(setenv("PATH", new_path, 1) != 0) {
perror("Error");
}
}
else {
printf("New PATH cannot be equal to null\n");
}
}
/**
* Sets the user entered directory
*
* @param user_input String containing the user entered directory
*/
void set_dir(char* user_input) {
char* final_path_ptr = malloc(sizeof(char) * BUFFER_SIZE);
strcpy(final_path_ptr, user_input);
if(final_path_ptr[0] == '~') {
replacetilde(final_path_ptr);
}
if(chdir(final_path_ptr) == -1) {
char* prefix = "cd:";
char* error_msg = strerror(errno);
if(errno == ENOTDIR) {
char* file_name = get_last_word(user_input);
printf("%s %s: %s\n", prefix, file_name, error_msg);
free(file_name);
return;
}
printf("%s %s\n", prefix, error_msg);
}
free(final_path_ptr);
}
/**
* Gets the last word of the user entered directory
*
* @param user_input String containing the user entered directory
* @return The last word
*/
char* get_last_word(char* user_input) {
char* word_ptr = malloc(sizeof(char) * BUFFER_SIZE);
char* path_split[ARG_LIMIT];
int words_length = split_str(path_split, user_input, "/");
strcpy(word_ptr, path_split[words_length - 1]);
free(path_split[0]);
return word_ptr;
}
/**
* Replaces the '~' character with the HOME directory of the user
*
* @param path String containing the user entered directory
*/
void replacetilde(char* path) {
char* ready_path_ptr = malloc(sizeof(char) * BUFFER_SIZE);
char* path_split[ARG_LIMIT];
split_str(path_split, path, "/");
strcpy(ready_path_ptr, getenv("HOME"));
strcat(ready_path_ptr, "/");
int i = 1;
while(path_split[i] != NULL) {
strcat(ready_path_ptr, path_split[i++]);
if(path_split[i] == NULL) break;
strcat(ready_path_ptr, "/");
}
strcpy(path, ready_path_ptr);
free(path_split[0]);
free(ready_path_ptr);
}