-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell.cc
More file actions
230 lines (199 loc) · 5.47 KB
/
Shell.cc
File metadata and controls
230 lines (199 loc) · 5.47 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
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
/** @file Shell.cc
* Implementation of class Shell.
*/
#include <signal.h> // for: signal, SIG*
#include "asserts.h"
#include "syntax_error.h"
#include "Shell.h"
#include "Token.h"
using namespace std;
// =============================================================================
// INITIALIZE
// Initialize a Shell instance
Shell::Shell(istream& input)
: input(input), tp(0)
{
// Select the set of tokens to be recognized
Token::setLevel( Token::BASIC );
}
// =============================================================================
// MAIN CYCLE
// The main level function
void Shell::main()
{
// Ignore some signals
// TODO:
// but never these ones
// TODO:
// The command loop
do {
cout << "SHELL: " << flush; // prompt the user for command
Sequence *sequence = parse(); // parse one line of input
// syntax sanity check
if ( (tp->getType() != Token::EOL) // expect either an EOL
&& (tp->getType() != Token::END) ) // or end-of-input
throw syntax_error("Expected EOL got " + tp->getText() );
if (!sequence->isEmpty()) // anything to do?
execute( sequence ); // oke, execute it
delete sequence; // discard the sequence
} while (tp->getType() == Token::EOL); // while more input expected
cout << "GOODBYE\n";
}
// =============================================================================
// PARSER SUPPORT
// Delete the current token
// and read the next one
void Shell::proceed()
{
if (tp) // some old token?
delete tp; // discard it.
tp = Token::nextToken(input); // get a new one
//cerr << *tp << endl; //DEBUG
}
// =============================================================================
// PARSER
// Parse a single line of input
// returning the sequence of pipelines created.
Sequence *Shell::parse()
{
//cerr << "Shell::parse\n"; //DEBUG
proceed(); // get the first input token
Sequence *sp = new Sequence;
parseSequence(sp); // read a commandline
return sp;
}
// Try to parse a sequence ... e.g. abc ; def ; ghi
void Shell::parseSequence(Sequence *sp)
{
//cerr << "Shell::parseSequence\n"; //DEBUG
Pipeline *pp = new Pipeline;
parsePipeline(pp); // read a pipeline
if (pp->isEmpty()) {
delete pp;
pp = 0;
}
else
sp->addPipeline(pp);
// More to do ?
//cerr << "Shell::parseSequence more?\n"; //DEBUG
while ( (tp->getType() == Token::SEQUENCE) // a ';' ?
|| (tp->getType() == Token::BACKGROUND) ) // or a '&' ?
{
// Do we have to set background mode for the previous pipeline?
if ((pp) && (tp->getType() == Token::BACKGROUND))
pp->setBackground();
proceed(); // oke, discard the ';' or '&'
pp = new Pipeline();
parsePipeline(pp); // read a pipeline
if (pp->isEmpty()) {
delete pp;
pp = 0;
} else
sp->addPipeline(pp);
}
}
// Try to parse a pipeline ... e.g. abc | def | ghi
void Shell::parsePipeline(Pipeline *pp)
{
//cerr << "Shell::parsePipeline\n"; //DEBUG
Command *cp = new Command;
parseCommand(cp); // read a command
if (cp->isEmpty()) {
delete cp;
cp = 0;
} else
pp->addCommand(cp);
// More to do ?
//cerr << "Shell::parsePipeline more?\n"; //DEBUG
while (tp->getType() == Token::PIPE) { // a '|' ?
proceed(); // oke, discard the '|'
cp = new Command;
parseCommand(cp); // read a command
if (cp->isEmpty()) {
delete cp;
cp = 0;
} else
pp->addCommand(cp);
}
}
// Try to parse a command ... e.g. word word word
void Shell::parseCommand(Command *cp)
{
//cerr << "Shell::parseCommand\n"; //DEBUG
// A command is a sequence of words ...
do {
//cerr << "Shell::Command getWord\n"; //DEBUG
while (tp->getType() == Token::WORD) {
cp->addWord( tp->getText() );
proceed(); // oke
}
// ... possibly interspersed with io-redirections
} while (parseIORedirection(cp));
}
// Iff an IO-redirection is found,
// add it to the given Command
// @return true iff an io-redirection was found
bool Shell::parseIORedirection(Command *cp)
{
//cerr << "Shell::parseIORedirection\n"; //DEBUG
// Examine the current token
switch (tp->getType())
{
case Token::INPUT: // the < symbol
proceed(); // discard <
parseInput(cp);
return true;
case Token::OUTPUT: // the > symbol
proceed(); // discard >
parseOutput(cp);
return true;
case Token::APPEND: // the >> symbol
proceed(); // discard >>
parseAppend(cp);
return true;
default:
break;
}
return false;
}
// Parse an < IO-redirection
void Shell::parseInput(Command *cp)
{
//cerr << "Shell::parseInput\n"; //DEBUG
if (tp->getType() == Token::WORD) {
cp->setInput( tp->getText() );
proceed();
} else
throw syntax_error("Expect a word after <, got " + tp->getText() );
}
// Parse an > IO-redirection
void Shell::parseOutput(Command *cp)
{
//cerr << "Shell::parseOutput\n"; //DEBUG
if (tp->getType() == Token::WORD) {
cp->setOutput(tp->getText());
proceed();
} else
throw syntax_error("Expect a word after >, got " + tp->getText() );
}
// Parse an >> IO-redirection
void Shell::parseAppend(Command *cp)
{
//cerr << "Shell::parseAppend\n"; //DEBUG
if (tp->getType() == Token::WORD) {
cp->setAppend(tp->getText());
proceed();
} else
throw syntax_error("Expect a word after >>, got " + tp->getText() );
}
// =============================================================================
// EXECUTE
void Shell::execute( Sequence *sequence )
{
//cerr << "Shell::execute\n"; //DEBUG
require(sequence != 0);
if (!sequence->isEmpty()) { // anything to do?
sequence->execute(); // delegate the work
}
}
// vim:ai:aw:ts=4:sw=4: