| 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) |
|---|
| 48 | : OutputListener(consoleName) |
|---|
| 49 | , inputBuffer_(new InputBuffer()) |
|---|
| 50 | , consoleName_(consoleName) |
|---|
| 51 | , bScrollable_(bScrollable) |
|---|
| 52 | { |
|---|
| 53 | RegisterRootObject(Shell); |
|---|
| 54 | |
|---|
| 55 | this->scrollPosition_ = 0; |
|---|
| 56 | this->maxHistoryLength_ = 100; |
|---|
| 57 | this->historyPosition_ = 0; |
|---|
| 58 | this->historyOffset_ = 0; |
|---|
| 59 | this->bFinishedLastLine_ = true; |
|---|
| 60 | |
|---|
| 61 | this->clearOutput(); |
|---|
| 62 | this->configureInputBuffer(); |
|---|
| 63 | |
|---|
| 64 | // Specify file for the command history |
|---|
| 65 | ConfigFileManager::getInstance().setFilename(ConfigFileType::CommandHistory, "commandHistory.ini"); |
|---|
| 66 | |
|---|
| 67 | // Use a stringstream object to buffer the output |
|---|
| 68 | this->outputStream_ = &this->outputBuffer_; |
|---|
| 69 | |
|---|
| 70 | this->setConfigValues(); |
|---|
| 71 | |
|---|
| 72 | // Get the previous output and add it to the Shell |
|---|
| 73 | for (OutputHandler::OutputVectorIterator it = OutputHandler::getInstance().getOutputVectorBegin(); |
|---|
| 74 | it != OutputHandler::getInstance().getOutputVectorEnd(); ++it) |
|---|
| 75 | { |
|---|
| 76 | if (it->first <= this->getSoftDebugLevel()) |
|---|
| 77 | { |
|---|
| 78 | this->outputBuffer_ << it->second; |
|---|
| 79 | this->outputChanged(it->first); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | // Register the shell as output listener |
|---|
| 84 | OutputHandler::getInstance().registerOutputListener(this); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | Shell::~Shell() |
|---|
| 88 | { |
|---|
| 89 | OutputHandler::getInstance().unregisterOutputListener(this); |
|---|
| 90 | this->inputBuffer_->destroy(); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | void Shell::setConfigValues() |
|---|
| 94 | { |
|---|
| 95 | SetConfigValue(maxHistoryLength_, 100) |
|---|
| 96 | .callback(this, &Shell::commandHistoryLengthChanged); |
|---|
| 97 | SetConfigValue(historyOffset_, 0) |
|---|
| 98 | .callback(this, &Shell::commandHistoryOffsetChanged); |
|---|
| 99 | setConfigValueGeneric(this, &commandHistory_, ConfigFileType::CommandHistory, "Shell", "commandHistory_", std::vector<std::string>()); |
|---|
| 100 | |
|---|
| 101 | #ifdef ORXONOX_RELEASE |
|---|
| 102 | const unsigned int defaultLevel = 1; |
|---|
| 103 | #else |
|---|
| 104 | const unsigned int defaultLevel = 3; |
|---|
| 105 | #endif |
|---|
| 106 | setConfigValueGeneric(this, &softDebugLevel_, ConfigFileType::Settings, "OutputHandler", "softDebugLevel" + this->consoleName_, defaultLevel) |
|---|
| 107 | .description("The maximal level of debug output shown in the Shell"); |
|---|
| 108 | this->setSoftDebugLevel(this->softDebugLevel_); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void Shell::commandHistoryOffsetChanged() |
|---|
| 112 | { |
|---|
| 113 | if (this->historyOffset_ >= this->maxHistoryLength_) |
|---|
| 114 | this->historyOffset_ = 0; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void Shell::commandHistoryLengthChanged() |
|---|
| 118 | { |
|---|
| 119 | this->commandHistoryOffsetChanged(); |
|---|
| 120 | |
|---|
| 121 | while (this->commandHistory_.size() > this->maxHistoryLength_) |
|---|
| 122 | { |
|---|
| 123 | unsigned int index = this->commandHistory_.size() - 1; |
|---|
| 124 | this->commandHistory_.erase(this->commandHistory_.begin() + index); |
|---|
| 125 | ModifyConfigValue(commandHistory_, remove, index); |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void Shell::configureInputBuffer() |
|---|
| 130 | { |
|---|
| 131 | this->inputBuffer_->registerListener(this, &Shell::inputChanged, true); |
|---|
| 132 | this->inputBuffer_->registerListener(this, &Shell::execute, '\r', false); |
|---|
| 133 | this->inputBuffer_->registerListener(this, &Shell::execute, '\n', false); |
|---|
| 134 | this->inputBuffer_->registerListener(this, &Shell::hintAndComplete, '\t', true); |
|---|
| 135 | this->inputBuffer_->registerListener(this, &Shell::backspace, '\b', true); |
|---|
| 136 | this->inputBuffer_->registerListener(this, &Shell::backspace, '\177', true); |
|---|
| 137 | this->inputBuffer_->registerListener(this, &Shell::exit, '\033', true); // escape |
|---|
| 138 | this->inputBuffer_->registerListener(this, &Shell::deleteChar, KeyCode::Delete); |
|---|
| 139 | this->inputBuffer_->registerListener(this, &Shell::cursorRight, KeyCode::Right); |
|---|
| 140 | this->inputBuffer_->registerListener(this, &Shell::cursorLeft, KeyCode::Left); |
|---|
| 141 | this->inputBuffer_->registerListener(this, &Shell::cursorEnd, KeyCode::End); |
|---|
| 142 | this->inputBuffer_->registerListener(this, &Shell::cursorHome, KeyCode::Home); |
|---|
| 143 | this->inputBuffer_->registerListener(this, &Shell::historyUp, KeyCode::Up); |
|---|
| 144 | this->inputBuffer_->registerListener(this, &Shell::historyDown, KeyCode::Down); |
|---|
| 145 | if (this->bScrollable_) |
|---|
| 146 | { |
|---|
| 147 | this->inputBuffer_->registerListener(this, &Shell::scrollUp, KeyCode::PageUp); |
|---|
| 148 | this->inputBuffer_->registerListener(this, &Shell::scrollDown, KeyCode::PageDown); |
|---|
| 149 | } |
|---|
| 150 | else |
|---|
| 151 | { |
|---|
| 152 | this->inputBuffer_->registerListener(this, &Shell::historySearchUp, KeyCode::PageUp); |
|---|
| 153 | this->inputBuffer_->registerListener(this, &Shell::historySearchDown, KeyCode::PageDown); |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /* |
|---|
| 158 | void Shell::history() |
|---|
| 159 | { |
|---|
| 160 | Shell& instance = Shell::getInstance(); |
|---|
| 161 | |
|---|
| 162 | for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i) |
|---|
| 163 | instance.addOutput(instance.commandHistory_[i] + '\n', -1); |
|---|
| 164 | for (unsigned int i = 0; i < instance.historyOffset_; ++i) |
|---|
| 165 | instance.addOutput(instance.commandHistory_[i] + '\n', -1); |
|---|
| 166 | } |
|---|
| 167 | */ |
|---|
| 168 | |
|---|
| 169 | void Shell::registerListener(ShellListener* listener) |
|---|
| 170 | { |
|---|
| 171 | this->listeners_.push_back(listener); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void Shell::unregisterListener(ShellListener* listener) |
|---|
| 175 | { |
|---|
| 176 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
|---|
| 177 | { |
|---|
| 178 | if ((*it) == listener) |
|---|
| 179 | it = this->listeners_.erase(it); |
|---|
| 180 | else |
|---|
| 181 | ++it; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | void Shell::setCursorPosition(unsigned int cursor) |
|---|
| 186 | { |
|---|
| 187 | this->inputBuffer_->setCursorPosition(cursor); |
|---|
| 188 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | void Shell::addOutput(const std::string& text, LineType type) |
|---|
| 192 | { |
|---|
| 193 | this->outputBuffer_ << text; |
|---|
| 194 | this->outputChanged(type); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | void Shell::clearOutput() |
|---|
| 198 | { |
|---|
| 199 | this->outputLines_.clear(); |
|---|
| 200 | this->scrollIterator_ = this->outputLines_.begin(); |
|---|
| 201 | |
|---|
| 202 | this->scrollPosition_ = 0; |
|---|
| 203 | this->bFinishedLastLine_ = true; |
|---|
| 204 | |
|---|
| 205 | this->updateListeners<&ShellListener::linesChanged>(); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | Shell::LineList::const_iterator Shell::getNewestLineIterator() const |
|---|
| 209 | { |
|---|
| 210 | if (this->scrollPosition_) |
|---|
| 211 | return this->scrollIterator_; |
|---|
| 212 | else |
|---|
| 213 | return this->outputLines_.begin(); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | Shell::LineList::const_iterator Shell::getEndIterator() const |
|---|
| 217 | { |
|---|
| 218 | return this->outputLines_.end(); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | void Shell::addToHistory(const std::string& command) |
|---|
| 222 | { |
|---|
| 223 | ModifyConfigValue(commandHistory_, set, this->historyOffset_, command); |
|---|
| 224 | this->historyPosition_ = 0; |
|---|
| 225 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | const std::string& Shell::getFromHistory() const |
|---|
| 229 | { |
|---|
| 230 | unsigned int index = mod(static_cast<int>(this->historyOffset_) - static_cast<int>(this->historyPosition_), this->maxHistoryLength_); |
|---|
| 231 | if (index < this->commandHistory_.size() && this->historyPosition_ != 0) |
|---|
| 232 | return this->commandHistory_[index]; |
|---|
| 233 | else |
|---|
| 234 | return BLANKSTRING; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | void Shell::outputChanged(int lineType) |
|---|
| 238 | { |
|---|
| 239 | bool newline = false; |
|---|
| 240 | do |
|---|
| 241 | { |
|---|
| 242 | std::string output; |
|---|
| 243 | std::getline(this->outputBuffer_, output); |
|---|
| 244 | |
|---|
| 245 | bool eof = this->outputBuffer_.eof(); |
|---|
| 246 | bool fail = this->outputBuffer_.fail(); |
|---|
| 247 | if (eof) |
|---|
| 248 | this->outputBuffer_.flush(); |
|---|
| 249 | if (eof || fail) |
|---|
| 250 | this->outputBuffer_.clear(); |
|---|
| 251 | newline = (!eof && !fail); |
|---|
| 252 | |
|---|
| 253 | if (!newline && output.empty()) |
|---|
| 254 | break; |
|---|
| 255 | |
|---|
| 256 | if (this->bFinishedLastLine_) |
|---|
| 257 | { |
|---|
| 258 | this->outputLines_.push_front(std::make_pair(output, static_cast<LineType>(lineType))); |
|---|
| 259 | |
|---|
| 260 | if (this->scrollPosition_) |
|---|
| 261 | this->scrollPosition_++; |
|---|
| 262 | else |
|---|
| 263 | this->scrollIterator_ = this->outputLines_.begin(); |
|---|
| 264 | |
|---|
| 265 | this->bFinishedLastLine_ = newline; |
|---|
| 266 | |
|---|
| 267 | if (!this->scrollPosition_) |
|---|
| 268 | this->updateListeners<&ShellListener::lineAdded>(); |
|---|
| 269 | } |
|---|
| 270 | else |
|---|
| 271 | { |
|---|
| 272 | this->outputLines_.front().first += output; |
|---|
| 273 | this->bFinishedLastLine_ = newline; |
|---|
| 274 | this->updateListeners<&ShellListener::onlyLastLineChanged>(); |
|---|
| 275 | } |
|---|
| 276 | this->bFinishedLastLine_ = newline; |
|---|
| 277 | |
|---|
| 278 | } while (newline); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | void Shell::clearInput() |
|---|
| 282 | { |
|---|
| 283 | this->inputBuffer_->clear(); |
|---|
| 284 | this->historyPosition_ = 0; |
|---|
| 285 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 286 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | void Shell::setPromptPrefix(const std::string& str) |
|---|
| 290 | { |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | // ########################################## |
|---|
| 295 | // ### InputBuffer callback functions ### |
|---|
| 296 | // ########################################## |
|---|
| 297 | |
|---|
| 298 | void Shell::inputChanged() |
|---|
| 299 | { |
|---|
| 300 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 301 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | void Shell::execute() |
|---|
| 305 | { |
|---|
| 306 | this->addToHistory(this->inputBuffer_->get()); |
|---|
| 307 | this->updateListeners<&ShellListener::executed>(); |
|---|
| 308 | |
|---|
| 309 | if (!CommandExecutor::execute(this->inputBuffer_->get())) |
|---|
| 310 | { |
|---|
| 311 | this->outputBuffer_ << "Error: Can't execute \"" << this->inputBuffer_->get() << "\"." << std::endl; |
|---|
| 312 | this->outputChanged(Error); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | this->clearInput(); |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | void Shell::hintAndComplete() |
|---|
| 319 | { |
|---|
| 320 | this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get())); |
|---|
| 321 | this->outputBuffer_ << CommandExecutor::hint(this->inputBuffer_->get()) << std::endl; |
|---|
| 322 | this->outputChanged(Hint); |
|---|
| 323 | |
|---|
| 324 | this->inputChanged(); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | void Shell::backspace() |
|---|
| 328 | { |
|---|
| 329 | this->inputBuffer_->removeBehindCursor(); |
|---|
| 330 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 331 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | void Shell::exit() |
|---|
| 335 | { |
|---|
| 336 | if (this->inputBuffer_->getSize() > 0) |
|---|
| 337 | { |
|---|
| 338 | this->clearInput(); |
|---|
| 339 | return; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | this->clearInput(); |
|---|
| 343 | this->scrollPosition_ = 0; |
|---|
| 344 | this->scrollIterator_ = this->outputLines_.begin(); |
|---|
| 345 | |
|---|
| 346 | this->updateListeners<&ShellListener::exit>(); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | void Shell::deleteChar() |
|---|
| 350 | { |
|---|
| 351 | this->inputBuffer_->removeAtCursor(); |
|---|
| 352 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | void Shell::cursorRight() |
|---|
| 356 | { |
|---|
| 357 | this->inputBuffer_->increaseCursor(); |
|---|
| 358 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | void Shell::cursorLeft() |
|---|
| 362 | { |
|---|
| 363 | this->inputBuffer_->decreaseCursor(); |
|---|
| 364 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | void Shell::cursorEnd() |
|---|
| 368 | { |
|---|
| 369 | this->inputBuffer_->setCursorToEnd(); |
|---|
| 370 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | void Shell::cursorHome() |
|---|
| 374 | { |
|---|
| 375 | this->inputBuffer_->setCursorToBegin(); |
|---|
| 376 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 377 | } |
|---|
| 378 | |
|---|
| 379 | void Shell::historyUp() |
|---|
| 380 | { |
|---|
| 381 | if (this->historyPosition_ < this->commandHistory_.size()) |
|---|
| 382 | { |
|---|
| 383 | this->historyPosition_++; |
|---|
| 384 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | void Shell::historyDown() |
|---|
| 389 | { |
|---|
| 390 | if (this->historyPosition_ > 0) |
|---|
| 391 | { |
|---|
| 392 | this->historyPosition_--; |
|---|
| 393 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | void Shell::historySearchUp() |
|---|
| 398 | { |
|---|
| 399 | if (this->historyPosition_ == this->historyOffset_) |
|---|
| 400 | return; |
|---|
| 401 | unsigned int cursorPosition = this->getCursorPosition(); |
|---|
| 402 | const std::string& input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline until the cursor position |
|---|
| 403 | for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++) |
|---|
| 404 | { |
|---|
| 405 | if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // search case insensitive |
|---|
| 406 | { |
|---|
| 407 | this->historyPosition_ = newPos; |
|---|
| 408 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 409 | this->setCursorPosition(cursorPosition); |
|---|
| 410 | return; |
|---|
| 411 | } |
|---|
| 412 | } |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | void Shell::historySearchDown() |
|---|
| 416 | { |
|---|
| 417 | if (this->historyPosition_ == 0) |
|---|
| 418 | return; |
|---|
| 419 | unsigned int cursorPosition = this->getCursorPosition(); |
|---|
| 420 | const std::string& input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning |
|---|
| 421 | for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--) |
|---|
| 422 | { |
|---|
| 423 | if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // sear$ |
|---|
| 424 | { |
|---|
| 425 | this->historyPosition_ = newPos; |
|---|
| 426 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 427 | this->setCursorPosition(cursorPosition); |
|---|
| 428 | return; |
|---|
| 429 | } |
|---|
| 430 | } |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | void Shell::scrollUp() |
|---|
| 434 | { |
|---|
| 435 | if (this->scrollIterator_ != this->outputLines_.end()) |
|---|
| 436 | { |
|---|
| 437 | ++this->scrollIterator_; |
|---|
| 438 | ++this->scrollPosition_; |
|---|
| 439 | |
|---|
| 440 | this->updateListeners<&ShellListener::linesChanged>(); |
|---|
| 441 | } |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | void Shell::scrollDown() |
|---|
| 445 | { |
|---|
| 446 | if (this->scrollIterator_ != this->outputLines_.begin()) |
|---|
| 447 | { |
|---|
| 448 | --this->scrollIterator_; |
|---|
| 449 | --this->scrollPosition_; |
|---|
| 450 | |
|---|
| 451 | this->updateListeners<&ShellListener::linesChanged>(); |
|---|
| 452 | } |
|---|
| 453 | } |
|---|
| 454 | } |
|---|