Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Shell.cc @ 3301

Last change on this file since 3301 was 3301, checked in by rgrieder, 15 years ago

Found even more casts. They sure aren't all of them, but I hope to have caught every pointer C-style cast because they can be very dangerous.
Note: I didn't do the pointer casts in the network library because that would have taken way too long.

  • Property svn:eol-style set to native
File size: 12.1 KB
Line 
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 "CommandExecutor.h"
33#include "CoreIncludes.h"
34#include "ConfigValueIncludes.h"
35#include "Core.h"
36#include "ConsoleCommand.h"
37
38#define SHELL_UPDATE_LISTENERS(function) \
39    for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) \
40        (*(it++))->function()
41
42namespace orxonox
43{
44    SetConsoleCommand(Shell, clearShell, true);
45    SetConsoleCommand(Shell, history, true);
46
47    SetConsoleCommandShortcut(OutputHandler, log);
48    SetConsoleCommandShortcut(OutputHandler, error);
49    SetConsoleCommandShortcut(OutputHandler, warning);
50    SetConsoleCommandShortcut(OutputHandler, info);
51    SetConsoleCommandShortcut(OutputHandler, debug);
52
53    Shell* Shell::singletonRef_s = 0;
54
55    Shell::Shell()
56    {
57        assert(singletonRef_s == 0);
58        singletonRef_s = this;
59
60        int level = Core::getSoftDebugLevel(OutputHandler::LD_Shell);
61        Core::setSoftDebugLevel(OutputHandler::LD_Shell, -1);
62
63        RegisterRootObject(Shell);
64
65        this->scrollPosition_ = 0;
66        this->maxHistoryLength_ = 100;
67        this->historyPosition_ = 0;
68        this->historyOffset_ = 0;
69        this->finishedLastLine_ = true;
70        this->bAddOutputLevel_ = false;
71
72        this->clearLines();
73
74        this->inputBuffer_ = new InputBuffer();
75        this->configureInputBuffer();
76
77        this->outputBuffer_.registerListener(this);
78        OutputHandler::getOutStream().setOutputBuffer(&this->outputBuffer_);
79
80        // Get a config file for the command history
81        this->commandHistoryConfigFileType_ = ConfigFileManager::getInstance().getNewConfigFileType();
82        ConfigFileManager::getInstance().setFilename(this->commandHistoryConfigFileType_, "commandHistory.ini");
83
84        this->setConfigValues();
85
86        Core::setSoftDebugLevel(OutputHandler::LD_Shell, level);
87    }
88
89    Shell::~Shell()
90    {
91        OutputHandler::getOutStream().setOutputBuffer(0);
92        if (this->inputBuffer_)
93            delete this->inputBuffer_;
94        singletonRef_s = 0;
95    }
96
97    void Shell::setConfigValues()
98    {
99        SetConfigValueGeneric(commandHistoryConfigFileType_, maxHistoryLength_, 100)
100            .callback(this, &Shell::commandHistoryLengthChanged);
101        SetConfigValueGeneric(commandHistoryConfigFileType_, historyOffset_, 0)
102            .callback(this, &Shell::commandHistoryOffsetChanged);
103        SetConfigValueVectorGeneric(commandHistoryConfigFileType_, commandHistory_, std::vector<std::string>());
104    }
105
106    void Shell::commandHistoryOffsetChanged()
107    {
108        if (this->historyOffset_ >= this->maxHistoryLength_)
109            this->historyOffset_ = 0;
110    }
111
112    void Shell::commandHistoryLengthChanged()
113    {
114        this->commandHistoryOffsetChanged();
115
116        while (this->commandHistory_.size() > this->maxHistoryLength_)
117        {
118            unsigned int index = this->commandHistory_.size() - 1;
119            this->commandHistory_.erase(this->commandHistory_.begin() + index);
120            ModifyConfigValue(commandHistory_, remove, index);
121        }
122    }
123
124    void Shell::configureInputBuffer()
125    {
126        this->inputBuffer_->registerListener(this, &Shell::inputChanged, true);
127        this->inputBuffer_->registerListener(this, &Shell::execute, '\r', false);
128        this->inputBuffer_->registerListener(this, &Shell::hintandcomplete, '\t', true);
129        this->inputBuffer_->registerListener(this, &Shell::backspace, '\b', true);
130        this->inputBuffer_->registerListener(this, &Shell::deletechar, KeyCode::Delete);
131        this->inputBuffer_->registerListener(this, &Shell::exit, static_cast<char>(27), true);
132        this->inputBuffer_->registerListener(this, &Shell::cursor_right, KeyCode::Right);
133        this->inputBuffer_->registerListener(this, &Shell::cursor_left, KeyCode::Left);
134        this->inputBuffer_->registerListener(this, &Shell::cursor_end, KeyCode::End);
135        this->inputBuffer_->registerListener(this, &Shell::cursor_home, KeyCode::Home);
136        this->inputBuffer_->registerListener(this, &Shell::history_up, KeyCode::Up);
137        this->inputBuffer_->registerListener(this, &Shell::history_down, KeyCode::Down);
138        this->inputBuffer_->registerListener(this, &Shell::scroll_up, KeyCode::PageUp);
139        this->inputBuffer_->registerListener(this, &Shell::scroll_down, KeyCode::PageDown);
140    }
141
142    void Shell::clearShell()
143    {
144        Shell::getInstance().clearLines();
145    }
146
147    void Shell::history()
148    {
149        Shell& instance = Shell::getInstance();
150
151        for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i)
152            instance.addLine(instance.commandHistory_[i], -1);
153        for (unsigned int i =  0; i < instance.historyOffset_; ++i)
154            instance.addLine(instance.commandHistory_[i], -1);
155    }
156
157    void Shell::registerListener(ShellListener* listener)
158    {
159        this->listeners_.insert(this->listeners_.end(), listener);
160    }
161
162    void Shell::unregisterListener(ShellListener* listener)
163    {
164        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
165        {
166            if ((*it) == listener)
167                this->listeners_.erase(it++);
168            else
169                ++it;
170        }
171    }
172
173    void Shell::setCursorPosition(unsigned int cursor)
174    {
175        this->inputBuffer_->setCursorPosition(cursor);
176        SHELL_UPDATE_LISTENERS(cursorChanged);
177    }
178
179    void Shell::setInput(const std::string& input)
180    {
181        this->inputBuffer_->set(input);
182        this->inputChanged();
183    }
184
185    void Shell::addLine(const std::string& line, int level)
186    {
187        int original_level = OutputHandler::getOutStream().getOutputLevel();
188        OutputHandler::getOutStream().setOutputLevel(level);
189
190        if (!this->finishedLastLine_)
191            this->outputBuffer_ << std::endl;
192
193        this->outputBuffer_ << line << std::endl;
194        OutputHandler::getOutStream().setOutputLevel(original_level);
195    }
196
197    void Shell::clearLines()
198    {
199        this->lines_.clear();
200        this->scrollIterator_ = this->lines_.begin();
201
202        this->scrollPosition_ = 0;
203        this->finishedLastLine_ = true;
204
205        SHELL_UPDATE_LISTENERS(linesChanged);
206    }
207
208    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
209    {
210        if (this->scrollPosition_)
211            return this->scrollIterator_;
212        else
213            return this->lines_.begin();
214    }
215
216    std::list<std::string>::const_iterator Shell::getEndIterator() const
217    {
218        return this->lines_.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    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 "";
235    }
236
237    void Shell::outputChanged()
238    {
239        std::string output;
240        bool newline;
241        do
242        {
243            newline = this->outputBuffer_.getLine(&output);
244
245            if (!newline && output == "")
246                break;
247
248            if (this->finishedLastLine_)
249            {
250                if (this->bAddOutputLevel_)
251                    output.insert(0, 1, static_cast<char>(OutputHandler::getOutStream().getOutputLevel()));
252
253                this->lines_.insert(this->lines_.begin(), output);
254
255                if (this->scrollPosition_)
256                    this->scrollPosition_++;
257                else
258                    this->scrollIterator_ = this->lines_.begin();
259
260                this->finishedLastLine_ = newline;
261
262                if (!this->scrollPosition_)
263                {
264                    SHELL_UPDATE_LISTENERS(lineAdded);
265                }
266            }
267            else
268            {
269                (*this->lines_.begin()) += output;
270                this->finishedLastLine_ = newline;
271                SHELL_UPDATE_LISTENERS(onlyLastLineChanged);
272            }
273
274        } while (newline);
275    }
276
277    void Shell::inputChanged()
278    {
279        SHELL_UPDATE_LISTENERS(inputChanged);
280        SHELL_UPDATE_LISTENERS(cursorChanged);
281    }
282
283    void Shell::execute()
284    {
285        this->addToHistory(this->inputBuffer_->get());
286        this->addLine(this->inputBuffer_->get(), 0);
287
288        if (!CommandExecutor::execute(this->inputBuffer_->get()))
289            this->addLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
290
291        this->clear();
292    }
293
294    void Shell::hintandcomplete()
295    {
296        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
297        this->addLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
298
299        this->inputChanged();
300    }
301
302    void Shell::backspace()
303    {
304        this->inputBuffer_->removeBehindCursor();
305        SHELL_UPDATE_LISTENERS(inputChanged);
306        SHELL_UPDATE_LISTENERS(cursorChanged);
307    }
308
309    void Shell::deletechar()
310    {
311        this->inputBuffer_->removeAtCursor();
312        SHELL_UPDATE_LISTENERS(inputChanged);
313    }
314
315    void Shell::clear()
316    {
317        this->inputBuffer_->clear();
318        this->historyPosition_ = 0;
319        SHELL_UPDATE_LISTENERS(inputChanged);
320        SHELL_UPDATE_LISTENERS(cursorChanged);
321    }
322
323    void Shell::cursor_right()
324    {
325        this->inputBuffer_->increaseCursor();
326        SHELL_UPDATE_LISTENERS(cursorChanged);
327    }
328
329    void Shell::cursor_left()
330    {
331        this->inputBuffer_->decreaseCursor();
332        SHELL_UPDATE_LISTENERS(cursorChanged);
333    }
334
335    void Shell::cursor_end()
336    {
337        this->inputBuffer_->setCursorToEnd();
338        SHELL_UPDATE_LISTENERS(cursorChanged);
339    }
340
341    void Shell::cursor_home()
342    {
343        this->inputBuffer_->setCursorToBegin();
344        SHELL_UPDATE_LISTENERS(cursorChanged);
345    }
346
347    void Shell::history_up()
348    {
349        if (this->historyPosition_ < this->commandHistory_.size())
350        {
351            this->historyPosition_++;
352            this->inputBuffer_->set(this->getFromHistory());
353        }
354    }
355
356    void Shell::history_down()
357    {
358        if (this->historyPosition_ > 0)
359        {
360            this->historyPosition_--;
361            this->inputBuffer_->set(this->getFromHistory());
362        }
363    }
364
365    void Shell::scroll_up()
366    {
367        if (this->scrollIterator_ != this->lines_.end())
368        {
369            ++this->scrollIterator_;
370            ++this->scrollPosition_;
371
372            SHELL_UPDATE_LISTENERS(linesChanged);
373        }
374    }
375
376    void Shell::scroll_down()
377    {
378        if (this->scrollIterator_ != this->lines_.begin())
379        {
380            --this->scrollIterator_;
381            --this->scrollPosition_;
382
383            SHELL_UPDATE_LISTENERS(linesChanged);
384        }
385    }
386
387    void Shell::exit()
388    {
389        if (this->inputBuffer_->getSize() > 0)
390        {
391            this->clear();
392            return;
393        }
394
395        this->clear();
396        this->scrollPosition_ = 0;
397        this->scrollIterator_ = this->lines_.begin();
398
399        SHELL_UPDATE_LISTENERS(exit);
400    }
401}
Note: See TracBrowser for help on using the repository browser.