-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell.cc
executable file
·104 lines (77 loc) · 1.7 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
#include <cstdio>
#include "shell.hh"
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
int yyparse(void);
extern void source(FILE *file, bool firstTry);
extern int lastExitCode;
char *shellpath;
void Shell::prompt() {
if (isatty(0)) {
char *value = getenv("PROMPT");
char *errorValue = getenv("ON_ERROR");
if (errorValue != NULL && lastExitCode != 0) {
printf("%s\n", errorValue);
}
if (value != NULL) {
printf("%s", value);
} else {
printf("myshell>");
}
fflush(stdout);
}
}
void controlC(int sig) {
printf("\n");
Shell::_currentCommand.clear();
Shell::prompt();
printf("\r");
}
void zombie(int sig) {
while (waitpid(-1, NULL, WNOHANG) > 0);
}
int main(int argc, char **argv) {
int error;
//signal handler for control c
struct sigaction sa1;
sa1.sa_handler = controlC;
sigemptyset(&sa1.sa_mask);
sa1.sa_flags = SA_RESTART;
error = sigaction(SIGINT, &sa1, NULL);
if (error == -1) {
perror("sigaction");
exit(1);
}
//signal handler for zombie processes
struct sigaction sa2;
sa2.sa_handler = zombie;
sa2.sa_flags = SA_RESTART;
sigemptyset(&sa2.sa_mask);
error = sigaction(SIGCHLD, &sa2, NULL);
if (error == -1) {
perror("sigaction");
exit(1);
}
//reads default source
std::string path = getenv("HOME");
path += "/.shellrc";
FILE *fp = fopen(path.c_str(), "r");
if (fp != NULL) {
source(fp, true);
}
//for shell real path
char actualpath [256];
char *ptr;
ptr = realpath(argv[0], actualpath);
shellpath = actualpath;
//printf("%s\n", shellpath);
//prints prompt and parses
if (!Shell::_currentCommand._source) {
Shell::prompt();
}
yyparse();
}
Command Shell::_currentCommand;