Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc @ 7228

Last change on this file since 7228 was 7228, checked in by landauf, 14 years ago

re-implemented CommandExecutor and CommandEvaluation. parameter evaluation is currently not implemented, will come soon.

  • Property svn:eol-style set to native
File size: 3.4 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 "CommandExecutor.h"
30
31#include "ConsoleCommand.h"
32#include "TclBind.h"
33#include "Shell.h"
34
35namespace orxonox
36{
37    static const std::string __CC_CommandExecutor_name = "CommandExecutor";
38    static const std::string __CC_autocomplete_name = "autocomplete";
39
40    _SetConsoleCommand(__CC_CommandExecutor_name, __CC_autocomplete_name, &CommandExecutor::_autocomplete)
41        .hide()
42        .argumentCompleter(0, autocompletion::groupsandcommands())
43        .argumentCompleter(1, autocompletion::subcommands());
44
45    /* static */ CommandExecutor& CommandExecutor::getInstance()
46    {
47        static CommandExecutor instance;
48        return instance;
49    }
50
51    /* static */ int CommandExecutor::execute(const std::string& command, bool useTcl)
52    {
53        int error;
54        CommandExecutor::queryMT(command, &error, useTcl);
55        return error;
56    }
57
58    /* static */ MultiType CommandExecutor::queryMT(const std::string& command, int* error, bool useTcl)
59    {
60        if (useTcl)
61            return TclBind::eval(command, error);
62        else
63            return CommandExecutor::evaluate(command).query(error);
64    }
65
66    /* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl)
67    {
68        return CommandExecutor::queryMT(command, error, useTcl).getString();
69    }
70
71    /* static */ CommandEvaluation CommandExecutor::evaluate(const std::string& command)
72    {
73        CommandEvaluation evaluation;
74        evaluation.initialize(command);
75
76        evaluation.hintCommand_ = _ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
77
78        if (evaluation.getNumberOfArguments() >= 1)
79        {
80            evaluation.execCommand_ = _ConsoleCommand::getCommandLC(evaluation.getToken(0));
81            if (evaluation.execCommand_)
82                evaluation.execArgumentsOffset_ = 1;
83            else if (evaluation.getNumberOfArguments() >= 2)
84            {
85                evaluation.execCommand_ = _ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
86                if (evaluation.execCommand_)
87                    evaluation.execArgumentsOffset_ = 2;
88            }
89        }
90
91        if (evaluation.execCommand_ && evaluation.getNumberOfArguments() > evaluation.execArgumentsOffset_)
92        {
93            evaluation.hintCommand_ = evaluation.execCommand_;
94            evaluation.hintArgumentsOffset_ = evaluation.execArgumentsOffset_;
95        }
96
97        return evaluation;
98    }
99}
Note: See TracBrowser for help on using the repository browser.