1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * Reto Grieder |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Shell.h" |
---|
30 | |
---|
31 | #include "util/OutputHandler.h" |
---|
32 | #include "util/StringUtils.h" |
---|
33 | #include "util/SubString.h" |
---|
34 | #include "CommandExecutor.h" |
---|
35 | #include "CoreIncludes.h" |
---|
36 | #include "ConfigValueIncludes.h" |
---|
37 | #include "ConsoleCommand.h" |
---|
38 | |
---|
39 | namespace orxonox |
---|
40 | { |
---|
41 | SetConsoleCommandShortcut(OutputHandler, log); |
---|
42 | SetConsoleCommandShortcut(OutputHandler, error); |
---|
43 | SetConsoleCommandShortcut(OutputHandler, warning); |
---|
44 | SetConsoleCommandShortcut(OutputHandler, info); |
---|
45 | SetConsoleCommandShortcut(OutputHandler, debug); |
---|
46 | |
---|
47 | Shell::Shell(const std::string& consoleName, bool bScrollable, bool bPrependOutputLevel) |
---|
48 | : OutputListener(consoleName) |
---|
49 | , inputBuffer_(new InputBuffer()) |
---|
50 | , consoleName_(consoleName) |
---|
51 | , bPrependOutputLevel_(bPrependOutputLevel) |
---|
52 | , bScrollable_(bScrollable) |
---|
53 | { |
---|
54 | RegisterRootObject(Shell); |
---|
55 | |
---|
56 | this->scrollPosition_ = 0; |
---|
57 | this->maxHistoryLength_ = 100; |
---|
58 | this->historyPosition_ = 0; |
---|
59 | this->historyOffset_ = 0; |
---|
60 | this->bFinishedLastLine_ = true; |
---|
61 | |
---|
62 | this->clearOutput(); |
---|
63 | this->configureInputBuffer(); |
---|
64 | |
---|
65 | // Get a config file for the command history |
---|
66 | this->commandHistoryConfigFileType_ = ConfigFileManager::getInstance().getNewConfigFileType(); |
---|
67 | ConfigFileManager::getInstance().setFilename(this->commandHistoryConfigFileType_, "commandHistory.ini"); |
---|
68 | |
---|
69 | // Use a stringstream object to buffer the output and get it line by line in update() |
---|
70 | this->outputStream_ = &this->outputBuffer_; |
---|
71 | |
---|
72 | this->setConfigValues(); |
---|
73 | |
---|
74 | // Get the previous output and add it to the Shell |
---|
75 | for (OutputHandler::OutputVectorIterator it = OutputHandler::getInstance().getOutputVectorBegin(); |
---|
76 | it != OutputHandler::getInstance().getOutputVectorEnd(); ++it) |
---|
77 | { |
---|
78 | if (it->first <= this->getSoftDebugLevel()) |
---|
79 | { |
---|
80 | this->outputBuffer_ << it->second; |
---|
81 | this->outputChanged(it->first); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | // Register the shell as output listener |
---|
86 | OutputHandler::getInstance().registerOutputListener(this); |
---|
87 | } |
---|
88 | |
---|
89 | Shell::~Shell() |
---|
90 | { |
---|
91 | OutputHandler::getInstance().unregisterOutputListener(this); |
---|
92 | this->inputBuffer_->destroy(); |
---|
93 | } |
---|
94 | |
---|
95 | void Shell::setConfigValues() |
---|
96 | { |
---|
97 | SetConfigValue(maxHistoryLength_, 100) |
---|
98 | .callback(this, &Shell::commandHistoryLengthChanged); |
---|
99 | SetConfigValue(historyOffset_, 0) |
---|
100 | .callback(this, &Shell::commandHistoryOffsetChanged); |
---|
101 | SetConfigValueVectorGeneric(commandHistoryConfigFileType_, commandHistory_, std::vector<std::string>()); |
---|
102 | |
---|
103 | #ifdef ORXONOX_RELEASE |
---|
104 | const unsigned int defaultLevel = 1; |
---|
105 | #else |
---|
106 | const unsigned int defaultLevel = 3; |
---|
107 | #endif |
---|
108 | SetConfigValueGeneric(ConfigFileType::Settings, softDebugLevel_, "softDebugLevel" + this->consoleName_, "OutputHandler", defaultLevel) |
---|
109 | .description("The maximal level of debug output shown in the Shell"); |
---|
110 | this->setSoftDebugLevel(this->softDebugLevel_); |
---|
111 | } |
---|
112 | |
---|
113 | void Shell::commandHistoryOffsetChanged() |
---|
114 | { |
---|
115 | if (this->historyOffset_ >= this->maxHistoryLength_) |
---|
116 | this->historyOffset_ = 0; |
---|
117 | } |
---|
118 | |
---|
119 | void Shell::commandHistoryLengthChanged() |
---|
120 | { |
---|
121 | this->commandHistoryOffsetChanged(); |
---|
122 | |
---|
123 | while (this->commandHistory_.size() > this->maxHistoryLength_) |
---|
124 | { |
---|
125 | unsigned int index = this->commandHistory_.size() - 1; |
---|
126 | this->commandHistory_.erase(this->commandHistory_.begin() + index); |
---|
127 | ModifyConfigValue(commandHistory_, remove, index); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | void Shell::configureInputBuffer() |
---|
132 | { |
---|
133 | this->inputBuffer_->registerListener(this, &Shell::inputChanged, true); |
---|
134 | this->inputBuffer_->registerListener(this, &Shell::execute, '\r', false); |
---|
135 | this->inputBuffer_->registerListener(this, &Shell::execute, '\n', false); |
---|
136 | this->inputBuffer_->registerListener(this, &Shell::hintAndComplete, '\t', true); |
---|
137 | this->inputBuffer_->registerListener(this, &Shell::backspace, '\b', true); |
---|
138 | this->inputBuffer_->registerListener(this, &Shell::backspace, '\177', true); |
---|
139 | this->inputBuffer_->registerListener(this, &Shell::exit, '\033', true); // escape |
---|
140 | this->inputBuffer_->registerListener(this, &Shell::deleteChar, KeyCode::Delete); |
---|
141 | this->inputBuffer_->registerListener(this, &Shell::cursorRight, KeyCode::Right); |
---|
142 | this->inputBuffer_->registerListener(this, &Shell::cursorLeft, KeyCode::Left); |
---|
143 | this->inputBuffer_->registerListener(this, &Shell::cursorEnd, KeyCode::End); |
---|
144 | this->inputBuffer_->registerListener(this, &Shell::cursorHome, KeyCode::Home); |
---|
145 | this->inputBuffer_->registerListener(this, &Shell::historyUp, KeyCode::Up); |
---|
146 | this->inputBuffer_->registerListener(this, &Shell::historyDown, KeyCode::Down); |
---|
147 | if (this->bScrollable_) |
---|
148 | { |
---|
149 | this->inputBuffer_->registerListener(this, &Shell::scrollUp, KeyCode::PageUp); |
---|
150 | this->inputBuffer_->registerListener(this, &Shell::scrollDown, KeyCode::PageDown); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | this->inputBuffer_->registerListener(this, &Shell::historySearchUp, KeyCode::PageUp); |
---|
155 | this->inputBuffer_->registerListener(this, &Shell::historySearchDown, KeyCode::PageDown); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | void Shell::history() |
---|
161 | { |
---|
162 | Shell& instance = Shell::getInstance(); |
---|
163 | |
---|
164 | for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i) |
---|
165 | instance.addOutputLine(instance.commandHistory_[i], -1); |
---|
166 | for (unsigned int i = 0; i < instance.historyOffset_; ++i) |
---|
167 | instance.addOutputLine(instance.commandHistory_[i], -1); |
---|
168 | } |
---|
169 | */ |
---|
170 | |
---|
171 | void Shell::registerListener(ShellListener* listener) |
---|
172 | { |
---|
173 | this->listeners_.push_back(listener); |
---|
174 | } |
---|
175 | |
---|
176 | void Shell::unregisterListener(ShellListener* listener) |
---|
177 | { |
---|
178 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
179 | { |
---|
180 | if ((*it) == listener) |
---|
181 | it = this->listeners_.erase(it); |
---|
182 | else |
---|
183 | ++it; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | void Shell::setCursorPosition(unsigned int cursor) |
---|
188 | { |
---|
189 | this->inputBuffer_->setCursorPosition(cursor); |
---|
190 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
191 | } |
---|
192 | |
---|
193 | void Shell::addOutputLine(const std::string& line, int level) |
---|
194 | { |
---|
195 | // Make sure we really only have one line per line (no new lines!) |
---|
196 | SubString lines(line, '\n'); |
---|
197 | for (unsigned i = 0; i < lines.size(); ++i) |
---|
198 | { |
---|
199 | if (level <= this->softDebugLevel_) |
---|
200 | this->outputLines_.push_front(lines[i]); |
---|
201 | this->updateListeners<&ShellListener::lineAdded>(); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | void Shell::clearOutput() |
---|
206 | { |
---|
207 | this->outputLines_.clear(); |
---|
208 | this->scrollIterator_ = this->outputLines_.begin(); |
---|
209 | |
---|
210 | this->scrollPosition_ = 0; |
---|
211 | this->bFinishedLastLine_ = true; |
---|
212 | |
---|
213 | this->updateListeners<&ShellListener::linesChanged>(); |
---|
214 | } |
---|
215 | |
---|
216 | std::list<std::string>::const_iterator Shell::getNewestLineIterator() const |
---|
217 | { |
---|
218 | if (this->scrollPosition_) |
---|
219 | return this->scrollIterator_; |
---|
220 | else |
---|
221 | return this->outputLines_.begin(); |
---|
222 | } |
---|
223 | |
---|
224 | std::list<std::string>::const_iterator Shell::getEndIterator() const |
---|
225 | { |
---|
226 | return this->outputLines_.end(); |
---|
227 | } |
---|
228 | |
---|
229 | void Shell::addToHistory(const std::string& command) |
---|
230 | { |
---|
231 | ModifyConfigValue(commandHistory_, set, this->historyOffset_, command); |
---|
232 | this->historyPosition_ = 0; |
---|
233 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
---|
234 | } |
---|
235 | |
---|
236 | std::string Shell::getFromHistory() const |
---|
237 | { |
---|
238 | unsigned int index = mod(static_cast<int>(this->historyOffset_) - static_cast<int>(this->historyPosition_), this->maxHistoryLength_); |
---|
239 | if (index < this->commandHistory_.size() && this->historyPosition_ != 0) |
---|
240 | return this->commandHistory_[index]; |
---|
241 | else |
---|
242 | return ""; |
---|
243 | } |
---|
244 | |
---|
245 | void Shell::outputChanged(int level) |
---|
246 | { |
---|
247 | bool newline = false; |
---|
248 | do |
---|
249 | { |
---|
250 | std::string output; |
---|
251 | std::getline(this->outputBuffer_, output); |
---|
252 | |
---|
253 | bool eof = this->outputBuffer_.eof(); |
---|
254 | bool fail = this->outputBuffer_.fail(); |
---|
255 | if (eof) |
---|
256 | this->outputBuffer_.flush(); |
---|
257 | if (eof || fail) |
---|
258 | this->outputBuffer_.clear(); |
---|
259 | newline = (!eof && !fail); |
---|
260 | |
---|
261 | if (!newline && output == "") |
---|
262 | break; |
---|
263 | |
---|
264 | if (this->bFinishedLastLine_) |
---|
265 | { |
---|
266 | if (this->bPrependOutputLevel_) |
---|
267 | output.insert(0, 1, static_cast<char>(level)); |
---|
268 | |
---|
269 | this->outputLines_.push_front(output); |
---|
270 | |
---|
271 | if (this->scrollPosition_) |
---|
272 | this->scrollPosition_++; |
---|
273 | else |
---|
274 | this->scrollIterator_ = this->outputLines_.begin(); |
---|
275 | |
---|
276 | this->bFinishedLastLine_ = newline; |
---|
277 | |
---|
278 | if (!this->scrollPosition_) |
---|
279 | { |
---|
280 | this->updateListeners<&ShellListener::lineAdded>(); |
---|
281 | } |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | (*this->outputLines_.begin()) += output; |
---|
286 | this->bFinishedLastLine_ = newline; |
---|
287 | this->updateListeners<&ShellListener::onlyLastLineChanged>(); |
---|
288 | } |
---|
289 | |
---|
290 | } while (newline); |
---|
291 | } |
---|
292 | |
---|
293 | void Shell::clearInput() |
---|
294 | { |
---|
295 | this->inputBuffer_->clear(); |
---|
296 | this->historyPosition_ = 0; |
---|
297 | this->updateListeners<&ShellListener::inputChanged>(); |
---|
298 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
299 | } |
---|
300 | |
---|
301 | void Shell::setPromptPrefix(const std::string& str) |
---|
302 | { |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | // ########################################## |
---|
307 | // ### InputBuffer callback functions ### |
---|
308 | // ########################################## |
---|
309 | |
---|
310 | void Shell::inputChanged() |
---|
311 | { |
---|
312 | this->updateListeners<&ShellListener::inputChanged>(); |
---|
313 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
314 | } |
---|
315 | |
---|
316 | void Shell::execute() |
---|
317 | { |
---|
318 | this->addToHistory(this->inputBuffer_->get()); |
---|
319 | this->updateListeners<&ShellListener::executed>(); |
---|
320 | |
---|
321 | if (!CommandExecutor::execute(this->inputBuffer_->get())) |
---|
322 | this->addOutputLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1); |
---|
323 | |
---|
324 | this->clearInput(); |
---|
325 | } |
---|
326 | |
---|
327 | void Shell::hintAndComplete() |
---|
328 | { |
---|
329 | this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get())); |
---|
330 | this->addOutputLine(CommandExecutor::hint(this->inputBuffer_->get()), -1); |
---|
331 | |
---|
332 | this->inputChanged(); |
---|
333 | } |
---|
334 | |
---|
335 | void Shell::backspace() |
---|
336 | { |
---|
337 | this->inputBuffer_->removeBehindCursor(); |
---|
338 | this->updateListeners<&ShellListener::inputChanged>(); |
---|
339 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
340 | } |
---|
341 | |
---|
342 | void Shell::exit() |
---|
343 | { |
---|
344 | if (this->inputBuffer_->getSize() > 0) |
---|
345 | { |
---|
346 | this->clearInput(); |
---|
347 | return; |
---|
348 | } |
---|
349 | |
---|
350 | this->clearInput(); |
---|
351 | this->scrollPosition_ = 0; |
---|
352 | this->scrollIterator_ = this->outputLines_.begin(); |
---|
353 | |
---|
354 | this->updateListeners<&ShellListener::exit>(); |
---|
355 | } |
---|
356 | |
---|
357 | void Shell::deleteChar() |
---|
358 | { |
---|
359 | this->inputBuffer_->removeAtCursor(); |
---|
360 | this->updateListeners<&ShellListener::inputChanged>(); |
---|
361 | } |
---|
362 | |
---|
363 | void Shell::cursorRight() |
---|
364 | { |
---|
365 | this->inputBuffer_->increaseCursor(); |
---|
366 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
367 | } |
---|
368 | |
---|
369 | void Shell::cursorLeft() |
---|
370 | { |
---|
371 | this->inputBuffer_->decreaseCursor(); |
---|
372 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
373 | } |
---|
374 | |
---|
375 | void Shell::cursorEnd() |
---|
376 | { |
---|
377 | this->inputBuffer_->setCursorToEnd(); |
---|
378 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
379 | } |
---|
380 | |
---|
381 | void Shell::cursorHome() |
---|
382 | { |
---|
383 | this->inputBuffer_->setCursorToBegin(); |
---|
384 | this->updateListeners<&ShellListener::cursorChanged>(); |
---|
385 | } |
---|
386 | |
---|
387 | void Shell::historyUp() |
---|
388 | { |
---|
389 | if (this->historyPosition_ < this->commandHistory_.size()) |
---|
390 | { |
---|
391 | this->historyPosition_++; |
---|
392 | this->inputBuffer_->set(this->getFromHistory()); |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | void Shell::historyDown() |
---|
397 | { |
---|
398 | if (this->historyPosition_ > 0) |
---|
399 | { |
---|
400 | this->historyPosition_--; |
---|
401 | this->inputBuffer_->set(this->getFromHistory()); |
---|
402 | } |
---|
403 | } |
---|
404 | |
---|
405 | void Shell::historySearchUp() |
---|
406 | { |
---|
407 | if (this->historyPosition_ == this->historyOffset_) |
---|
408 | return; |
---|
409 | unsigned int cursorPosition = this->getCursorPosition(); |
---|
410 | std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline until the cursor position |
---|
411 | for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++) |
---|
412 | { |
---|
413 | if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // search case insensitive |
---|
414 | { |
---|
415 | this->historyPosition_ = newPos; |
---|
416 | this->inputBuffer_->set(this->getFromHistory()); |
---|
417 | this->setCursorPosition(cursorPosition); |
---|
418 | return; |
---|
419 | } |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | void Shell::historySearchDown() |
---|
424 | { |
---|
425 | if (this->historyPosition_ == 0) |
---|
426 | return; |
---|
427 | unsigned int cursorPosition = this->getCursorPosition(); |
---|
428 | std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning |
---|
429 | for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--) |
---|
430 | { |
---|
431 | if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // sear$ |
---|
432 | { |
---|
433 | this->historyPosition_ = newPos; |
---|
434 | this->inputBuffer_->set(this->getFromHistory()); |
---|
435 | this->setCursorPosition(cursorPosition); |
---|
436 | return; |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | void Shell::scrollUp() |
---|
442 | { |
---|
443 | if (this->scrollIterator_ != this->outputLines_.end()) |
---|
444 | { |
---|
445 | ++this->scrollIterator_; |
---|
446 | ++this->scrollPosition_; |
---|
447 | |
---|
448 | this->updateListeners<&ShellListener::linesChanged>(); |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | void Shell::scrollDown() |
---|
453 | { |
---|
454 | if (this->scrollIterator_ != this->outputLines_.begin()) |
---|
455 | { |
---|
456 | --this->scrollIterator_; |
---|
457 | --this->scrollPosition_; |
---|
458 | |
---|
459 | this->updateListeners<&ShellListener::linesChanged>(); |
---|
460 | } |
---|
461 | } |
---|
462 | } |
---|