-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
465 lines (370 loc) · 13.6 KB
/
shell.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
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
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include "shell.h"
/* Global variable to keep track of forked children */
pid_t child_pid = 0;
int main() {
char input[MAX_INPUT], *s = NULL;
/* Keep track of exit_status of previously executed command for continuing/discontinuing execution of current commands with operators eg && */
int exit_status = 1;
struct Token token = {NULL, NULL, NULL, NULL};
/* Installing signal handlers */
signal(SIGINT, SigintHandler);
while(1) {
/* Determine if previous statements continuation exists eg. "ls && ps" or to wait for new command */
if (token.s_next != NULL && ContinueExec(token.operator, exit_status)) {
s = token.s_next;
} else {
printf(SHELL_SYMBOL);
s = fgets(input, MAX_INPUT, stdin);
}
/* Parse token from string*/
token = ParseArgs(s);
/* Check and execute depending on Builtin or command */
if (Builtin(token.command,token.args) == BUILTIN_EXEC) {
continue;
} else {
/* sleep 30 & sleep 40 -> sleep 30 & is background */
int is_background = (strcmp(token.operator, "&") == 0);
exit_status = ExecProgram(token.command, token.args, is_background);
}
}
}
/**
* Forks a subprocess, executes program and returns exit_status
**/
int ExecProgram(char *command, char *args[], int is_background) {
pid_t pid;
int status, exit_status;
int is_foreground = !is_background;
/* Fork child, create new process group for it and then execute */
if ((pid = Fork(FORK_ERR)) == 0) {
setpgid(0,0);
Execvp(command, args, COMMAND_ERR);
}
/* Set global variable child_pid */
child_pid = pid;
/* Reap child process - wait on execd children only if background process - is while loop required here (?)*/
while (is_foreground && (pid = wait(&status)) > 0) {
if WIFEXITED(status) {
printf(PROCESS_REAP_SUCCESS, pid);
/* Exit status of normally terminated process */
exit_status = WEXITSTATUS(status);
} else {
printf(PROCESS_REAP_FAILURE, pid);
/* Not grabbing exact exit status of abnormally terminated process currently - set it to arbitrary value */
exit_status = 2;
}
}
return exit_status;
}
/* Wrapper for fork() with error handling */
pid_t Fork(char *errmsg) {
pid_t pid = fork();
if (pid < 0) {
printf("%s:%s", errmsg,strerror(errno));
}
return pid;
}
/* Wrapper for execvp(..) with error handling */
int Execvp(char *command, char *args[], char *errmsg) {
/* Allocate new array to store command + args */
char **command_and_args = (char**)malloc(sizeof(char**) * MAX_ARGS);
command_and_args[0] = command;
for (int i = 1; args[i-1] != NULL; i++) {
command_and_args[i] = args[i-1];
}
/* Execute command */
int result_status;
if ((result_status = execvp(command, command_and_args)) == -1) {
printf("%s: %s", errmsg, strerror(errno));
}
return result_status;
}
/**
* Parse args from input string:
*
* - Parses string till an operator is encountered ie given a string "ls -a && ps", parses "ls", parses "-a" grabs "&&", and notes the position of "ps"
* - Returns this info as a token, token contains:
* a) command ie "ls"
* b) args ie ["-a"] in an array
* c) s_next ie pointer to next command "ps"
* d) *operator ie "&&"
**/
struct Token ParseArgs(char *s) {
struct Token token = {NULL, NULL, NULL, NULL};
int arg_idx = 0;
if (s == NULL) {
return token;
}
/* Skip initial spaces */
for(;*s==' ';s++);
/* Use a separate variable to traverse so we can calculate length of the command/arg we are traversing*/
char *cur_s = s;
/* Iterate until string ends or an operator is found */
while (*s != '\0' && *(token.operator = GetOperator(s)) == '\0') {
/* Find delimiter */
for(;*cur_s != ' ' && *cur_s != '\n' && *cur_s != '\0'; cur_s++);
/* Allocate space for command/arg */
char *new_arg = (char *)malloc(sizeof(char) * (cur_s - s + 1));
strncpy(new_arg, s, cur_s-s);
new_arg[cur_s-s] = '\0';
/* Determine whether we have parsed a command or an arg */
if (token.command == NULL) {
token.command = new_arg;
} else {
token.args[arg_idx++] = new_arg;
}
/* Skip additional spaces */
for (;*cur_s==' ' || *cur_s=='\n'; cur_s++);
/* Bring s to par with cur_s's position */
s = cur_s;
}
/* If stopped parsing due to operator, additional tokens may exist, provide index to start of next command */
if (token.operator != NULL) {
token.s_next = s + strlen(token.operator);
}
token.args[arg_idx] = NULL;
return token;
}
/**
* Builtin alias command implementation
*
* Notes:
* - only alias -p and alias without arguments handled for now
* - multiple -p flags will lead to all alias-es being printed once
* - no arguments provided to alias = print all aliases
*
*
**/
void Alias(char *args[]) {
FILE *f;
fpos_t *value_offset;
int cur_char, are_all_printed = 0, is_match;
char *value_offset_arg, *match_str;
if (*(args+1) == NULL) {
AliasPrintAll(args);
return;
}
// if ((f = fopen(SHELL_PROFILE, "a+")) == NULL) {
// printf("alias error: %s", strerror(errno));
// return;
// }
while (*++args != NULL) { /* Handle each arg independently */
char *cur_str = *args;
if(strcmp(cur_str, "-p") == 0 && !are_all_printed) { /* If -p encountered and all alias-es not already printed once */
are_all_printed = 1;
AliasPrintAll(args);
continue;
}
else { // only for now
continue;
}
/* For handling arguments other than -p */
/* ------------------------------------------------------INCOMPLETE --------------------------------------------------------------------------- */
if((value_offset_arg = strrchr(cur_str, '=')) != NULL) { /* If assignment statement found, search in file and replace */
f = fopen(SHELL_PROFILE, "r+");
cur_char = '\0';
while (cur_char != EOF) {
is_match = 1;
match_str = cur_str; /* Compare char by char */
while((cur_char = fgetc(f)) != '\n' && cur_char != EOF) {
if (cur_char == '=') {
fgetpos(f, value_offset); /* Record start of string position; TODO: handle error */
}
if (*match_str++ != cur_char){ /* If mis_match at any point, co */
is_match = 0;
break;
}
}
if (is_match) { /* Start writing from value_offsetIdx+1 in cur_str to value_offset in file */
fsetpos(f, value_offset);
for (char *s = value_offset_arg + 1; *s != '\0'; s++) {
fputc(*s, f); /* TODO: handle err */
}
fseek(f, 0, SEEK_END); /* TODO handle err */
} else { /* Iterate till EOF/EOL */
while((cur_char = fgetc(f)) != '\n' && cur_char != EOF);
}
}
} else { /* Search line wise */
}
/* ------------------------------------------------------INCOMPLETE --------------------------------------------------------------------------- */
}
}
void AliasPrintAll(char *args[]) {
FILE *f;
int cur_char;
if ((f = fopen(SHELL_PROFILE, "r")) == NULL) {
printf("alias error: %s", strerror(errno));
return;
}
while ((cur_char = fgetc(f)) != EOF){ /* Make sure \n exists in file itself */
fputc(cur_char, stdout);
}
return;
}
/****** HELPERS ******/
/**
* Helper to execute Builtins
*
* - Implemented only the alias builtin for now
* - TODO: implement more builtins
**/
int Builtin(char *command, char *args[]) {
int return_status = BUILTIN_NO_EXEC;
if (command == NULL) {
return return_status;
}
if (strcmp("alias", command) == 0) {
Alias(args);
return_status = BUILTIN_EXEC;
} else if (strcmp("exit", command) == 0) {
printf(EXIT_MSG, SHELL_SYMBOL, SHELL_SYMBOL);
exit(0);
}
return return_status;
}
/**
* Recognizes only valid operators, parses the valid operator and returns it
* Currently valid operators are:
* - &&
* - ||
* - &
* - |
**/
char *GetOperator(char *s) {
char *operator = "\0";
if ((*s == '&' && *(s+1) == '&' && *(s+2) == ' ') || (*s == '|' && *(s+1) == '|' && *(s+2) == ' ')) {
operator = (char *)malloc(3 * sizeof(char));
strncpy(operator, s, 2);
operator[2] = '\0';
} else if ((*s == '|' && *(s+1) == ' ') || (*s == '&' && *(s+1) == ' ')) {
operator = (char *)malloc(2 * sizeof(char));
strncpy(operator, s, 1);
operator[1] = '\0';
}
return operator;
}
/**
* Determine if execution is to be continued,
* depending on if a known operator is found
**/
int ContinueExec(char *operator, int exit_status) {
if (strcmp(operator, "&&") == 0) {
return exit_status == 0;
} else if (strcmp(operator, "||") == 0) {
return exit_status != 0;
} else if (strcmp(operator, "&") == 0) {
return 1;
}
return 0; /* Unknown operator */
}
/****** SIGNAL HANDLERS ******/
void SigintHandler(int sig) {
const char msg[] = "SIGINT caught - terminating child\n";
/* If child_pid is set, terminate it */
if (child_pid > 0) {
write(STDOUT_FILENO, msg, sizeof(msg)-1);
kill(child_pid, SIGTERM);
child_pid = 0;
}
return;
}
void SigintChildHandler(int sig) {
printf("Sitgint child\n");
exit(0);
}
/****** NOTES ******/
/**
* Naming convention
*
* THIS_IS_MY_CONVENTION for macros, enum members
* ThisIsMyConvention for file name, object name (class, struct, enum, union...), function name, method name, typedef
* this_is_my_convention global and local variables,
* parameters, struct and union elements
* thisismyconvention [optional] very local and temporary variables (such like a for() loop index)
*
*
**/
/**
* Rules:
*
* - Commands, flags, operators must be separated by a space delimiter ie "ls&&ps" is invalid
* - Valid operators: &&, ||, &, |
**/
// int main() {
// char *s = NULL, *check = NULL;
// char input[MAX_INPUT];
// char *args[MAX_ARGS];
// int status, exit_status;
// struct Token token = {NULL, NULL, NULL, NULL};
// sigset_t mask, prev_mask;
// shell_pid = getpid();
// // signal(SIGINT, SigintHandler); /* Installing SIGINT handler */
// // signal(SIGTSTP, SigtstpHandler); /* Installing SIGTSTP handler */
// // signal(SIGCHLD, SigchldHandler); /* Installing SIGTSTP handler */
// while(1) {
// /* Determine if previous command's execution to be continued or to wait for new command */
// if (token.s_next != NULL && ContinueExec(token.operator, exit_status)) {
// s = token.s_next;
// } else {
// printf(SHELL_SYMBOL);
// s = fgets(input, MAX_INPUT, stdin);
// }
// // if (s) {
// // token = ParseArgs(s, args); /* parseArg will always return a token depending on operators used in expression */
// // signal(SIGTTOU, SIG_IGN);
// // pid_t pid = Fork(FORK_ERR);
// // if (pid == 0) {
// // pid_t child_pid = getpid();
// // setpgid(child_pid, child_pid); /* We want to implement job control, so we will create separate pgid for each process */
// // if (*token.operator != '&' ) {
// // tcsetpgrp(STDIN_FILENO, child_pid);
// // }
// // signal(SIGTTOU, SIG_DFL);
// // Execvp(args, COMMAND_ERR);
// // }
// // if (*token.operator != '&' ) { /* Don't wait on background processes */
// // while (waitpid(pid, &status, WUNTRACED) > 0); /* Reaping child process and tracking last exit_status*/
// // if (WIFEXITED(status)) {
// // exit_status = WEXITSTATUS(status);
// // }
// // tcsetpgrp(STDIN_FILENO, getpgid(shell_pid));
// // signal(SIGTTOU, SIG_DFL);
// // }
// // }
// token = ParseArgs(s);
// /* Check and execute if command is a builtin */
// if (Builtin(token.command,token.args) == BUILTIN_EXEC) {
// continue;
// } else {
// ExecToken(token.command, token.args);
// }
// // if (s && !strchr(s, '\n')) { /* If newline not found, print first MAX chars and flush remaining - why was this condition written (?)*/
// // while ((s = fgets(input, MAX_INPUT, stdin)) && !strchr(s, '\n'));
// // printf("\n");
// // }
// }
// }
/*
void SigintHandler(int sig) {
const char msg[] = "SIGINT caught\n";
write(STDOUT_FILENO, msg, sizeof(msg)-1);
return;
}
void SigtstpHandler(int sig) {
const char msg[] = "SIGTSTP caught\n";
write(STDOUT_FILENO, msg, sizeof(msg)-1);
tcsetpgrp(STDIN_FILENO, getpgid(shell_pid));
exit(0);
}
void SigchldHandler(int sig) {
const char msg[] = "SIGCHLDcaught\n";
write(STDOUT_FILENO, msg, sizeof(msg)-1);
printf("%d shellPgid", getpgid(shell_pid));
tcsetpgrp(STDIN_FILENO, getpgid(shell_pid));
// exit(0);
return;
} */