| 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 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "Shell.h" |
|---|
| 30 | |
|---|
| 31 | #include "util/OutputHandler.h" |
|---|
| 32 | #include "util/StringUtils.h" |
|---|
| 33 | #include "CommandExecutor.h" |
|---|
| 34 | #include "CoreIncludes.h" |
|---|
| 35 | #include "ConfigValueIncludes.h" |
|---|
| 36 | #include "Core.h" |
|---|
| 37 | #include "ConsoleCommand.h" |
|---|
| 38 | |
|---|
| 39 | namespace orxonox |
|---|
| 40 | { |
|---|
| 41 | SetConsoleCommand(Shell, clearShell, true); |
|---|
| 42 | SetConsoleCommand(Shell, history, true); |
|---|
| 43 | |
|---|
| 44 | SetConsoleCommandShortcut(OutputHandler, log); |
|---|
| 45 | SetConsoleCommandShortcut(OutputHandler, error); |
|---|
| 46 | SetConsoleCommandShortcut(OutputHandler, warning); |
|---|
| 47 | SetConsoleCommandShortcut(OutputHandler, info); |
|---|
| 48 | SetConsoleCommandShortcut(OutputHandler, debug); |
|---|
| 49 | |
|---|
| 50 | Shell* Shell::singletonPtr_s = 0; |
|---|
| 51 | |
|---|
| 52 | Shell::Shell() |
|---|
| 53 | : OutputListener("shell") |
|---|
| 54 | { |
|---|
| 55 | RegisterRootObject(Shell); |
|---|
| 56 | |
|---|
| 57 | this->scrollPosition_ = 0; |
|---|
| 58 | this->maxHistoryLength_ = 100; |
|---|
| 59 | this->historyPosition_ = 0; |
|---|
| 60 | this->historyOffset_ = 0; |
|---|
| 61 | this->finishedLastLine_ = true; |
|---|
| 62 | this->bAddOutputLevel_ = false; |
|---|
| 63 | |
|---|
| 64 | this->clearLines(); |
|---|
| 65 | |
|---|
| 66 | this->inputBuffer_ = new InputBuffer(); |
|---|
| 67 | this->configureInputBuffer(); |
|---|
| 68 | |
|---|
| 69 | // Get a config file for the command history |
|---|
| 70 | this->commandHistoryConfigFileType_ = ConfigFileManager::getInstance().getNewConfigFileType(); |
|---|
| 71 | ConfigFileManager::getInstance().setFilename(this->commandHistoryConfigFileType_, "commandHistory.ini"); |
|---|
| 72 | |
|---|
| 73 | // Use a stringstream object to buffer the output and get it line by line in update() |
|---|
| 74 | this->outputStream_ = &this->outputBuffer_; |
|---|
| 75 | |
|---|
| 76 | this->setConfigValues(); |
|---|
| 77 | |
|---|
| 78 | // Get the previous output and add it to the Shell |
|---|
| 79 | for (OutputHandler::OutputVectorIterator it = OutputHandler::getInstance().getOutputVectorBegin(); |
|---|
| 80 | it != OutputHandler::getInstance().getOutputVectorEnd(); ++it) |
|---|
| 81 | this->addLine(it->second, it->first); |
|---|
| 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 | SetConfigValueGeneric(commandHistoryConfigFileType_, maxHistoryLength_, "maxHistoryLength_", "Shell", 100) |
|---|
| 96 | .callback(this, &Shell::commandHistoryLengthChanged); |
|---|
| 97 | SetConfigValueGeneric(commandHistoryConfigFileType_, historyOffset_, "historyOffset_", "Shell", 0) |
|---|
| 98 | .callback(this, &Shell::commandHistoryOffsetChanged); |
|---|
| 99 | SetConfigValueVectorGeneric(commandHistoryConfigFileType_, 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(ConfigFileType::Settings, softDebugLevel_, "softDebugLevelShell", "OutputHandler", defaultLevel) |
|---|
| 107 | .description("The maximal level of debug output shown in the Shell"); |
|---|
| 108 | OutputHandler::getInstance().setSoftDebugLevel("shell", 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::deletechar, KeyCode::Delete); |
|---|
| 138 | this->inputBuffer_->registerListener(this, &Shell::exit, '\033', true); // escape |
|---|
| 139 | this->inputBuffer_->registerListener(this, &Shell::cursor_right, KeyCode::Right); |
|---|
| 140 | this->inputBuffer_->registerListener(this, &Shell::cursor_left, KeyCode::Left); |
|---|
| 141 | this->inputBuffer_->registerListener(this, &Shell::cursor_end, KeyCode::End); |
|---|
| 142 | this->inputBuffer_->registerListener(this, &Shell::cursor_home, KeyCode::Home); |
|---|
| 143 | this->inputBuffer_->registerListener(this, &Shell::history_up, KeyCode::Up); |
|---|
| 144 | this->inputBuffer_->registerListener(this, &Shell::history_down, KeyCode::Down); |
|---|
| 145 | this->inputBuffer_->registerListener(this, &Shell::scroll_up, KeyCode::PageUp); |
|---|
| 146 | this->inputBuffer_->registerListener(this, &Shell::scroll_down, KeyCode::PageDown); |
|---|
| 147 | this->inputBuffer_->registerListener(this, &Shell::history_search_up, KeyCode::AltPageUp); |
|---|
| 148 | this->inputBuffer_->registerListener(this, &Shell::history_search_down, KeyCode::AltPageDown); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | void Shell::clearShell() |
|---|
| 152 | { |
|---|
| 153 | Shell::getInstance().clearLines(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | void Shell::history() |
|---|
| 157 | { |
|---|
| 158 | Shell& instance = Shell::getInstance(); |
|---|
| 159 | |
|---|
| 160 | for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i) |
|---|
| 161 | instance.addLine(instance.commandHistory_[i], -1); |
|---|
| 162 | for (unsigned int i = 0; i < instance.historyOffset_; ++i) |
|---|
| 163 | instance.addLine(instance.commandHistory_[i], -1); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | void Shell::registerListener(ShellListener* listener) |
|---|
| 167 | { |
|---|
| 168 | this->listeners_.push_back(listener); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | void Shell::unregisterListener(ShellListener* listener) |
|---|
| 172 | { |
|---|
| 173 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
|---|
| 174 | { |
|---|
| 175 | if ((*it) == listener) |
|---|
| 176 | it = this->listeners_.erase(it); |
|---|
| 177 | else |
|---|
| 178 | ++it; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | void Shell::setCursorPosition(unsigned int cursor) |
|---|
| 183 | { |
|---|
| 184 | this->inputBuffer_->setCursorPosition(cursor); |
|---|
| 185 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | void Shell::setInput(const std::string& input) |
|---|
| 189 | { |
|---|
| 190 | this->inputBuffer_->set(input); |
|---|
| 191 | this->inputChanged(); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | void Shell::addLine(const std::string& line, int level) |
|---|
| 195 | { |
|---|
| 196 | if (level <= this->softDebugLevel_) |
|---|
| 197 | this->lines_.push_front(line); |
|---|
| 198 | this->updateListeners<&ShellListener::lineAdded>(); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | void Shell::clearLines() |
|---|
| 202 | { |
|---|
| 203 | this->lines_.clear(); |
|---|
| 204 | this->scrollIterator_ = this->lines_.begin(); |
|---|
| 205 | |
|---|
| 206 | this->scrollPosition_ = 0; |
|---|
| 207 | this->finishedLastLine_ = true; |
|---|
| 208 | |
|---|
| 209 | this->updateListeners<&ShellListener::linesChanged>(); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | std::list<std::string>::const_iterator Shell::getNewestLineIterator() const |
|---|
| 213 | { |
|---|
| 214 | if (this->scrollPosition_) |
|---|
| 215 | return this->scrollIterator_; |
|---|
| 216 | else |
|---|
| 217 | return this->lines_.begin(); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | std::list<std::string>::const_iterator Shell::getEndIterator() const |
|---|
| 221 | { |
|---|
| 222 | return this->lines_.end(); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | void Shell::addToHistory(const std::string& command) |
|---|
| 226 | { |
|---|
| 227 | ModifyConfigValue(commandHistory_, set, this->historyOffset_, command); |
|---|
| 228 | this->historyPosition_ = 0; |
|---|
| 229 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | std::string Shell::getFromHistory() const |
|---|
| 233 | { |
|---|
| 234 | unsigned int index = mod(static_cast<int>(this->historyOffset_) - static_cast<int>(this->historyPosition_), this->maxHistoryLength_); |
|---|
| 235 | if (index < this->commandHistory_.size() && this->historyPosition_ != 0) |
|---|
| 236 | return this->commandHistory_[index]; |
|---|
| 237 | else |
|---|
| 238 | return ""; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | void Shell::outputChanged() |
|---|
| 242 | { |
|---|
| 243 | std::string output; |
|---|
| 244 | bool newline; |
|---|
| 245 | do |
|---|
| 246 | { |
|---|
| 247 | std::getline(this->outputBuffer_, output); |
|---|
| 248 | |
|---|
| 249 | bool eof = this->outputBuffer_.eof(); |
|---|
| 250 | bool fail = this->outputBuffer_.fail(); |
|---|
| 251 | if (eof) |
|---|
| 252 | this->outputBuffer_.flush(); |
|---|
| 253 | if (eof || fail) |
|---|
| 254 | this->outputBuffer_.clear(); |
|---|
| 255 | newline = (!eof && !fail); |
|---|
| 256 | |
|---|
| 257 | if (!newline && output == "") |
|---|
| 258 | break; |
|---|
| 259 | |
|---|
| 260 | if (this->finishedLastLine_) |
|---|
| 261 | { |
|---|
| 262 | if (this->bAddOutputLevel_) |
|---|
| 263 | output.insert(0, 1, static_cast<char>(OutputHandler::getInstance().getOutputLevel())); |
|---|
| 264 | |
|---|
| 265 | this->lines_.push_front(output); |
|---|
| 266 | |
|---|
| 267 | if (this->scrollPosition_) |
|---|
| 268 | this->scrollPosition_++; |
|---|
| 269 | else |
|---|
| 270 | this->scrollIterator_ = this->lines_.begin(); |
|---|
| 271 | |
|---|
| 272 | this->finishedLastLine_ = newline; |
|---|
| 273 | |
|---|
| 274 | if (!this->scrollPosition_) |
|---|
| 275 | { |
|---|
| 276 | this->updateListeners<&ShellListener::lineAdded>(); |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | else |
|---|
| 280 | { |
|---|
| 281 | (*this->lines_.begin()) += output; |
|---|
| 282 | this->finishedLastLine_ = newline; |
|---|
| 283 | this->updateListeners<&ShellListener::onlyLastLineChanged>(); |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | } while (newline); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | void Shell::inputChanged() |
|---|
| 290 | { |
|---|
| 291 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 292 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | void Shell::execute() |
|---|
| 296 | { |
|---|
| 297 | this->addToHistory(this->inputBuffer_->get()); |
|---|
| 298 | this->updateListeners<&ShellListener::executed>(); |
|---|
| 299 | |
|---|
| 300 | if (!CommandExecutor::execute(this->inputBuffer_->get())) |
|---|
| 301 | this->addLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1); |
|---|
| 302 | |
|---|
| 303 | this->clear(); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | void Shell::hintandcomplete() |
|---|
| 307 | { |
|---|
| 308 | this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get())); |
|---|
| 309 | this->addLine(CommandExecutor::hint(this->inputBuffer_->get()), -1); |
|---|
| 310 | |
|---|
| 311 | this->inputChanged(); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | void Shell::backspace() |
|---|
| 315 | { |
|---|
| 316 | this->inputBuffer_->removeBehindCursor(); |
|---|
| 317 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 318 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | void Shell::deletechar() |
|---|
| 322 | { |
|---|
| 323 | this->inputBuffer_->removeAtCursor(); |
|---|
| 324 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | void Shell::clear() |
|---|
| 328 | { |
|---|
| 329 | this->inputBuffer_->clear(); |
|---|
| 330 | this->historyPosition_ = 0; |
|---|
| 331 | this->updateListeners<&ShellListener::inputChanged>(); |
|---|
| 332 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | void Shell::cursor_right() |
|---|
| 336 | { |
|---|
| 337 | this->inputBuffer_->increaseCursor(); |
|---|
| 338 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | void Shell::cursor_left() |
|---|
| 342 | { |
|---|
| 343 | this->inputBuffer_->decreaseCursor(); |
|---|
| 344 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | void Shell::cursor_end() |
|---|
| 348 | { |
|---|
| 349 | this->inputBuffer_->setCursorToEnd(); |
|---|
| 350 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | void Shell::cursor_home() |
|---|
| 354 | { |
|---|
| 355 | this->inputBuffer_->setCursorToBegin(); |
|---|
| 356 | this->updateListeners<&ShellListener::cursorChanged>(); |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | void Shell::history_up() |
|---|
| 360 | { |
|---|
| 361 | if (this->historyPosition_ < this->commandHistory_.size()) |
|---|
| 362 | { |
|---|
| 363 | this->historyPosition_++; |
|---|
| 364 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 365 | } |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | void Shell::history_down() |
|---|
| 369 | { |
|---|
| 370 | if (this->historyPosition_ > 0) |
|---|
| 371 | { |
|---|
| 372 | this->historyPosition_--; |
|---|
| 373 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 374 | } |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | void Shell::history_search_up() |
|---|
| 378 | { |
|---|
| 379 | if (this->historyPosition_ == this->historyOffset_) |
|---|
| 380 | return; |
|---|
| 381 | unsigned int cursorPosition = this->getCursorPosition(); |
|---|
| 382 | std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginning of the inputline untill the cursor position |
|---|
| 383 | for (unsigned int newPos = this->historyPosition_ + 1; newPos <= this->historyOffset_; newPos++) |
|---|
| 384 | { |
|---|
| 385 | if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // search case insensitive |
|---|
| 386 | { |
|---|
| 387 | this->historyPosition_ = newPos; |
|---|
| 388 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 389 | this->setCursorPosition(cursorPosition); |
|---|
| 390 | return; |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | void Shell::history_search_down() |
|---|
| 396 | { |
|---|
| 397 | if (this->historyPosition_ == 0) |
|---|
| 398 | return; |
|---|
| 399 | unsigned int cursorPosition = this->getCursorPosition(); |
|---|
| 400 | std::string input_str(this->getInput().substr(0, cursorPosition)); // only search for the expression from the beginn$ |
|---|
| 401 | for (unsigned int newPos = this->historyPosition_ - 1; newPos > 0; newPos--) |
|---|
| 402 | { |
|---|
| 403 | if (getLowercase(this->commandHistory_[this->historyOffset_ - newPos]).find(getLowercase(input_str)) == 0) // sear$ |
|---|
| 404 | { |
|---|
| 405 | this->historyPosition_ = newPos; |
|---|
| 406 | this->inputBuffer_->set(this->getFromHistory()); |
|---|
| 407 | this->setCursorPosition(cursorPosition); |
|---|
| 408 | return; |
|---|
| 409 | } |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | void Shell::scroll_up() |
|---|
| 414 | { |
|---|
| 415 | if (this->scrollIterator_ != this->lines_.end()) |
|---|
| 416 | { |
|---|
| 417 | ++this->scrollIterator_; |
|---|
| 418 | ++this->scrollPosition_; |
|---|
| 419 | |
|---|
| 420 | this->updateListeners<&ShellListener::linesChanged>(); |
|---|
| 421 | } |
|---|
| 422 | } |
|---|
| 423 | |
|---|
| 424 | void Shell::scroll_down() |
|---|
| 425 | { |
|---|
| 426 | if (this->scrollIterator_ != this->lines_.begin()) |
|---|
| 427 | { |
|---|
| 428 | --this->scrollIterator_; |
|---|
| 429 | --this->scrollPosition_; |
|---|
| 430 | |
|---|
| 431 | this->updateListeners<&ShellListener::linesChanged>(); |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | void Shell::exit() |
|---|
| 436 | { |
|---|
| 437 | if (this->inputBuffer_->getSize() > 0) |
|---|
| 438 | { |
|---|
| 439 | this->clear(); |
|---|
| 440 | return; |
|---|
| 441 | } |
|---|
| 442 | |
|---|
| 443 | this->clear(); |
|---|
| 444 | this->scrollPosition_ = 0; |
|---|
| 445 | this->scrollIterator_ = this->lines_.begin(); |
|---|
| 446 | |
|---|
| 447 | this->updateListeners<&ShellListener::exit>(); |
|---|
| 448 | } |
|---|
| 449 | } |
|---|