-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.cc
More file actions
85 lines (69 loc) · 1.58 KB
/
Shell.cc
File metadata and controls
85 lines (69 loc) · 1.58 KB
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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
#include <unistd.h>
#include <cstdio>
#include "Command.hh"
#include "Shell.hh"
int yyparse(void);
Shell * Shell::TheShell;
Shell::Shell() {
this->_level = 0;
this->_enablePrompt = true;
this->_listCommands = new ListCommands();
this->_simpleCommand = new SimpleCommand();
this->_pipeCommand = new PipeCommand();
this->_currentCommand = this->_pipeCommand;
if ( !isatty(0)) {
this->_enablePrompt = false;
}
}
void Shell::prompt() {
if (_enablePrompt) {
printf("myshell>");
fflush(stdout);
}
}
void Shell::print() {
printf("\n--------------- Command Table ---------------\n");
this->_listCommands->print();
}
void Shell::clear() {
this->_listCommands->clear();
this->_simpleCommand->clear();
this->_pipeCommand->clear();
this->_currentCommand->clear();
this->_level = 0;
}
void Shell::execute() {
if (this->_level == 0 ) {
//this->print();
this->_listCommands->execute();
this->_listCommands->clear();
this->prompt();
}
}
void yyset_in (FILE * in_str );
int
main(int argc, char **argv) {
char * input_file = NULL;
if ( argc > 1 ) {
input_file = argv[1];
FILE * f = fopen(input_file, "r");
if (f==NULL) {
fprintf(stderr, "Cannot open file %s\n", input_file);
perror("fopen");
exit(1);
}
yyset_in(f);
}
Shell::TheShell = new Shell();
if (input_file != NULL) {
// No prompt if running a script
Shell::TheShell->_enablePrompt = false;
}
else {
Shell::TheShell->prompt();
}
yyparse();
}