/
shell.c
420 lines (364 loc) · 10.7 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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <math.h>
#include <signal.h>
#define FALSE 0
#define TRUE 1
#define INPUT_STRING_SIZE 80
#define PATH_MAX 80
#define MAX_TABLE_WIDTH 60
#define COLUMN 20
#include "io.h"
#include "parse.h"
#include "process.h"
#include "shell.h"
process *first_process = NULL;
int cmd_quit(tok_t arg[]) {
printf("Bye\n");
exit(0);
return 1;
}
int cmd_help(tok_t arg[]);
int cmd_chDir(tok_t arg[]);
int cmd_listProcs(tok_t arg[]);
int cmd_wait();
char * toArray(int number) {
int n = log10(number) + 1;
int i;
char *numberArray = calloc(n, sizeof(char));
for ( i = 0; i < n; ++i, number /= 10 ) {
numberArray[i] = number % 10;
}
return numberArray;
}
void fillLine(int remainder) {
int i;
for (i=0; i<remainder; i++) {
fprintf(stdout," ");
}
fprintf(stdout, "|\n");
}
/* Command Lookup table */
typedef int cmd_fun_t (tok_t args[]); /* cmd functions take token array and return int */
typedef struct fun_desc {
cmd_fun_t *fun;
char *cmd;
char *doc;
} fun_desc_t;
fun_desc_t cmd_table[] = {
{cmd_help, "?", "show this help menu"},
{cmd_quit, "quit", "quit the command shell"},
{cmd_chDir, "cd", "change directory to argv"},
{cmd_listProcs, "listProcs", "list all processes started by this shell"},
{cmd_wait, "wait", "wait until all background processes have completed"}
//{cmd_fg, "fg", "move process with pid given by argv to foreground"},
//{cmd_bg, "bg", "move process with pid given by argv to background"}
};
//Traverse linked list recursively, waiting for each process in bg to finish
int waitForProcesses(process *curr) {
int count = 0;
if(curr->next != NULL) {
if ( (curr->next->completed == 'N') && (curr->next->stopped =='N') && (curr->next->background == 'Y')) {
waitpid(curr->next->pid, 0, 0);
curr->next->completed = 'Y';
count++;
}
return count + waitForProcesses(curr->next);
}
return 0;
}
int cmd_wait() {
fprintf(stdout, "%d background processes completed.\n", waitForProcesses(first_process));
return 1;
}
int cmd_listProcs(tok_t arg[]) {
process *curr = first_process;
int i = 0;
int printed = 0;
int k;
//Printing process structure
while (curr->next != NULL) {
fprintf(stdout, "|----------------------------------------------------------|\n");
if ( (curr->next->completed == 'Y') || (curr->next->stopped == 'Y') )
{fprintf(stdout, "|Process number %2d (Inactive) |\n", i);}
else
{fprintf(stdout, "|Process number %2d (Active) |\n", i);}
fprintf(stdout, "|------------------+---------------------------------------|\n");
fprintf(stdout, "|PID: | %d%n", curr->next->pid, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
/* --FOR PRINTING FILE DESCRIPTORS:--
fprintf(stdout, "|Stdin: | %d%n", curr->next->stdin, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
fprintf(stdout, "|Stdout: | %d%n", curr->next->stdout, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
fprintf(stdout, "|Stderr: | %d%n", curr->next->stderr, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
*/
fprintf(stdout, "|Completed: | %c%n", curr->next->completed, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
fprintf(stdout, "|Stopped: | %c%n", curr->next->stopped, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
fprintf(stdout, "|Background: | %c%n", curr->next->background, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
fprintf(stdout, "|Arguments: | %d%n", curr->next->argc, &printed);
fillLine( (MAX_TABLE_WIDTH-printed)-1 );
for (k = 0; k < curr->next->argc; k++) {
fprintf(stdout, "|Arg number [%d]: | %s%n", k, curr->next->argv[k], &printed);
fillLine( (MAX_TABLE_WIDTH-printed) - 1);
}
fprintf(stdout, "|------------------+---------------------------------------|\n\n");
curr = curr->next;
i++;
}
return 1;
}
int cmd_chDir(tok_t arg[]) {
chdir(arg[0]);
return 1;
}
int cmd_help(tok_t arg[]) {
int i;
for (i=0; i < (sizeof(cmd_table)/sizeof(fun_desc_t)); i++) {
printf("%s - %s\n",cmd_table[i].cmd, cmd_table[i].doc);
}
return 1;
}
int lookup(char cmd[]) {
int i;
for (i=0; i < (sizeof(cmd_table)/sizeof(fun_desc_t)); i++) {
if (cmd && (strcmp(cmd_table[i].cmd, cmd) == 0)) return i;
}
return -1;
}
void init_shell()
{
/* Check if we are running interactively */
shell_terminal = STDIN_FILENO;
/** Note that we cannot take control of the terminal if the shell
is not interactive */
shell_is_interactive = isatty(shell_terminal);
if(shell_is_interactive){
/* force into foreground */
while(tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp()))
kill( - shell_pgid, SIGTTIN);
shell_pgid = getpid();
/* Put shell in its own process group */
if(setpgid(shell_pgid, shell_pgid) < 0){
perror("Couldn't put the shell in its own process group");
exit(1);
}
/* Take control of the terminal */
tcsetpgrp(shell_terminal, shell_pgid);
tcgetattr(shell_terminal, &shell_tmodes);
}
/** YOUR CODE HERE */
}
void completionListener(process* foo) {
pid_t thispid = fork();
if (thispid == 0) {
waitpid(foo->pid, 0, 0);
foo->completed = 'Y';
}
}
//Recursive function to add process to linked list.
void add_process(process *curr, process *p)
{
if (curr->next == NULL) {
curr->next = p;
p->prev = curr;
}
else
{add_process(curr->next, p);}
}
process* create_process(char* path, char** argvIN)
{
process *foo;
int i;
FILE *newInput, *newOutput;
char** newargvIN = malloc(MAXTOKS*sizeof(char*));
for (i=0; i<MAXTOKS; i++) newargvIN[i] = NULL;
int newStart = 0;
foo = malloc(sizeof(process));
//Setting standard file desc in process structure
foo->stdin = 0;
foo->stdout = 1;
foo->stderr = 2;
pid_t pid = fork();
//IO redirection (and updating file desc)
if (argvIN[1] != NULL) {
if ( strcmp(argvIN[1],"&") == 0) {
foo->background = 'Y';
argvIN[newStart+1] = argvIN[0];
newStart++;
}
else foo->background = 'N';
if ( (argvIN[newStart+1] != NULL) && (strcmp(argvIN[newStart+1],">") == 0) ) {
if (pid == 0) {
newOutput = fopen(argvIN[newStart+2], "a");
dup2(fileno(newOutput), 1);
}
foo->stdout = fileno(newOutput);
argvIN[newStart+2] = argvIN[newStart];
newStart += 2;
}
if ( (argvIN[newStart+1] != NULL) && (strcmp(argvIN[newStart+1],"<") == 0) ) {
if (pid == 0) {
newInput = fopen(argvIN[newStart+2], "r");
dup2(fileno(newInput), 0);
}
foo->stdin = fileno(newInput);
argvIN[newStart+2] = argvIN[newStart];
newStart += 2;
}
}
if (pid == 0) {
execv(path, &argvIN[newStart]);
exit(-1);
}
else {
if (pid == -1) {
return NULL;
}
else {
//Building process structure
foo->argv = &argvIN[newStart];
foo->completed = 'N';
foo->stopped = 'N';
if ((foo->background != 'N') && (foo->background != 'Y')) foo->background = 'Y';
foo->status = 1;
foo->pid = pid;
foo->next = NULL;
foo->prev = NULL;
i = newStart;
while (argvIN[i] != NULL)
{i++;}
foo->argc = i - newStart;
add_process(first_process, foo);
if (foo->background == 'N') {
waitpid(pid,0,0);
foo->completed = 'Y';
}
else completionListener(foo);
}
}
return foo;
}
//Recursive function to kill process
int killProcesses(process *curr, int signal) {
int count = 0;
if(curr->next != NULL) {
if ( (curr->next->completed == 'N') && (curr->next->stopped =='N') && (curr->next->background == 'N')) {
kill(curr->next->pid, signal);
curr->next->stopped = 'Y';
count++;
}
return count + killProcesses(curr->next, signal);
}
return 0;
}
static void handler(int signum) {
if (signum == SIGINT)
{fprintf(stdout, "\n%d processes ended.\n", killProcesses(first_process, signum));}
else if (signum == SIGTSTP)
{fprintf(stdout, "\n%d processes stopped.\n", killProcesses(first_process, signum));}
}
//Move all elements (starting at and including "index") right one index
//Pretty elegant solution if I do say so myself....
void shiftTokensRight(int index, tok_t *tokens) {
tok_t temp[2];
int i = index;
temp[1] = tokens[i];
temp[0] = tokens[i+1];
i++;
while (tokens[i] != NULL) {
tokens[i] = temp[(i-1)%2];
temp[(i-1)%2] = tokens[i+1];
i++;
}
tokens[i] = temp[(i-1)%2];
tokens[index] = NULL;
}
int shell (int argc, char *argv[]) {
char *s = malloc(INPUT_STRING_SIZE+1); /* user input string */
tok_t *t; /* tokens parsed from input */
int fundex = -1;
pid_t pid = getpid(); /* get current processes PID */
pid_t ppid = getppid(); /* get parents PID */
char *buf = NULL;
char *cwd = getcwd(buf, PATH_MAX);
char *path = getenv("PATH");
tok_t *paths = getToks(path);
int k = 0;
int isExecutable;
int background;
struct sigaction sa;
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
first_process = malloc(sizeof(process));
first_process->next = NULL;
first_process->prev = NULL;
if (sigaction(SIGINT, &sa, NULL) == -1)
{fprintf(stdout, "Failed to send request.");}
else if (sigaction(SIGTSTP, &sa, NULL) == -1)
{fprintf(stdout, "Failed to send request.");}
init_shell();
printf("%s running as PID %d under %d\n",argv[0],pid,ppid);
fprintf(stdout, "%s: ", cwd);
while ((s = freadln(stdin))){
char *newCommand = malloc(INPUT_STRING_SIZE*sizeof(char));
t = getToks(s); /* break the line into tokens */
k = 0;
background = 0;
while ((t[0][k] != '&') && (t[0][k] != '\0')) {
if (t[0][k+1] == '&') background = 1;
newCommand[k] = t[0][k];
k++;
}
t[0] = newCommand;
if (background == 1) {
shiftTokensRight(1,t);
t[1] = "&";
}
fundex = lookup(t[0]); /* Is first token a shell literal */
if(fundex >= 0) {
if ( (sizeof(t)/sizeof(char**) > 1) && (t[1][0] == '&') ) cmd_table[fundex].fun(&t[2]);
else cmd_table[fundex].fun(&t[1]);
}
else {
if (strstr(t[0], "/") != NULL) { //executable with path defined
create_process(t[0], t);
}
else { //executable with no path defined
char *pathResolution = malloc(PATH_MAX*sizeof(char));
k = 0;
isExecutable = 0;
while ( (paths[k] != NULL) && (isExecutable == 0)) {
strcpy(pathResolution, paths[k]);
strcat(pathResolution, "/");
strcat(pathResolution, t[0]);
if (access(pathResolution, F_OK|X_OK) == 0)
{isExecutable = 1;}
k++;
}
if (isExecutable == 1) {
t[0] = pathResolution;
create_process(pathResolution, t);
}
else {
fprintf(stdout, "Couldn't resolve path for executable '%s'.\n", t[0]);
}
}
}
cwd = getcwd(buf, PATH_MAX);
fprintf(stdout, "%s: ", cwd);
}
return 0;
}