-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
110 lines (90 loc) · 2.33 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
#include <stdio.h>
#include <unistd.h>
#include "launcher.h"
#include <sys/wait.h>
#include <stdlib.h>
#include <signal.h>
#include "list.h"
#include <sys/types.h>
/**
* Main program execution
*/
int main( int argc, char *argv[] )
{
node * first = NULL;
struct sigaction new_action;
new_action.sa_handler = SIG_IGN;
sigemptyset (&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction (SIGTTOU, &new_action, NULL);
sigaction (SIGTTIN, &new_action, NULL);
sigaction (SIGTERM, &new_action, NULL);
sigaction (SIGTSTP, &new_action, NULL);
sigaction (SIGINT, &new_action, NULL);
char string[1027] = "";
int br;
pid_t last_stopped = -1;
pid_t last_executed = -1;
string[1026] = '\0'; /* ensure that string is always null-terminated */
fprintf(stderr, "$ ");
while ((br = read( STDIN_FILENO, string, 1026)) > 0) {
if (br > 1025) {
printf( "The line you've entered exceeds the 1024 char limit\n" );
printf("$ ");
fflush(stdout);
continue;
}
string[br-1] = '\0'; /* remove trailing \n */
/* tokenize string */
launch(string, &last_stopped, &last_executed, &first);
//printf("shell is reasserting terminal control\n");
tcsetpgrp(STDIN_FILENO, getpgid(0));
//get rid of all zombies
node * nd = first;
while (nd != NULL) {
pid_t pid = nd->pid;
int status;
while ((waitpid(-pid, &status, WUNTRACED|WNOHANG)) > 0) {
if (WIFSTOPPED(status)) {
last_stopped = pid;
node * x = lookup(first, pid);
if (x != NULL) {
x->print = 1;
}
}
if (WIFEXITED(status) || WIFSIGNALED(status)) {
node * x = lookup(first, pid);
if (x != NULL) {
x->pipe = x->pipe - 1;
}
}
}
nd = nd->next;
}
node * x = first;
while (x != NULL) {
node * next = x->next;
//printf("%s \t %d \t %d \n", x->string, x->pipe, x->print);
if (x->pipe == -1) {
if (!x->fore) {
printf("Finished: %s\n", x->string);
}
first = delete(first, x);
} else {
if (x->print) {
printf("Stopped: %s\n", x->string);
x->print = 0;
}
}
x = next;
}
printf("$ ");
fflush(stdout);
}
node * x = first;
while (x != NULL) {
x = delete(x, x);
}
printf( "\nBye!\n" );
return 0; /* all's well that end's well */
}