-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.cc
115 lines (99 loc) · 2.69 KB
/
shell.cc
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
#include <cstdio>
#include <unistd.h>
#include "shell.hh"
#include <signal.h>
#include <string.h>
#include <wait.h>
int yyparse(void);
void swtchBfr(char*);
void initBfr();
void flushBfr();
extern char ** history;
extern "C" void resetLine();
void Shell::prompt() {
// prompt only when IO are console and excludes special occasions
if ( isatty(0) && isatty(1) && Shell::isPrompt) {
char * p = secure_getenv("PROMPT");
if (p) printf("%s ", p);
else printf("λ> ");
fflush(stdout);
}
}
// prompt function for read-line.c
extern "C" void prompt() {
Shell::prompt();
}
// signal handler for ctrl-C
void Shell::termination(int signum) {
// if a command is running, kill it
if (_currentCommand._pid != 0) kill(_currentCommand._pid, SIGINT);
// if not, clear the current command, reset input buffer
// and start a new input line
else {
flushBfr();
_currentCommand.clear();
if ( isatty(0) && isatty(1) && Shell::isPrompt) {
resetLine();
printf("\n");
}
Shell::prompt();
}
}
// signal handler for zombie elimination
void Shell::elimination(int signum) {
int r;
int e;
while((e = wait(&r)) > 0) {
if (isatty(0)) printf("%d exited\n", e);
if (WEXITSTATUS(r) && secure_getenv("ON_ERROR")) puts(secure_getenv("ON_ERROR"));
}
}
int main(int argc, char* argv[], char* envp[]) {
// Extra functionality: allows commands to be passed as arguments
// and execute without entering the shell
if (argc > 1) {
Shell::isPrompt = false;
char * input = (char *) calloc(strlen(argv[1]) + 2, sizeof(char));
strcpy(input, argv[1]);
// add newline to trigger command execution
input[strlen(argv[1])] = '\n';
Shell::_currentCommand._init = false;
// using input stirng as buffer for flex scanner
swtchBfr(input);
yyparse();
free(input);
exit(0);
}
// initialize history
history = (char**) calloc(8, sizeof(char*));
// store argv[0] for env exp
Shell::argv = argv[0];
// Signal handling for ctrl-c
struct sigaction c, d;
c.sa_handler = Shell::termination;
sigemptyset(&c.sa_mask);
c.sa_flags = SA_RESTART;
if(sigaction(SIGINT, &c, NULL)){
perror("sigaction");
exit(2);
}
// Signal handling for child exiting
d.sa_handler = Shell::elimination;
sigemptyset(&d.sa_mask);
c.sa_flags = SA_RESTART;
if(sigaction(SIGCHLD, &d, NULL)){
perror("sigaction");
exit(2);
}
// init stdin buffer
initBfr();
// Extra functionality: first time execution runs .shellrc
Shell::_currentCommand.execute();
yyparse();
}
Command Shell::_currentCommand;
bool Shell::isPrompt = true;
int Shell::lstRtn = 0;
int Shell::lstPid = 0;
char * Shell::lstArg = NULL;
char * Shell::argv = NULL;