Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/TclBind.cc @ 1188

Last change on this file since 1188 was 1188, checked in by landauf, 16 years ago

removed some bugs
tcl knows now orxonox

File size: 2.8 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 <iostream>
30#include <string>
31
32#include "ConsoleCommand.h"
33#include "CommandExecutor.h"
34#include "Debug.h"
35#include "TclBind.h"
36
37namespace orxonox
38{
39    ConsoleCommandShortcutExtern(tcl, AccessLevel::None);
40
41    void Tcl_puts(Tcl::object const &args)
42    {
43        COUT(0) << args.get() << std::endl;
44    }
45
46    std::string Tcl_execute(Tcl::object const &args)
47    {
48std::cout << "1\n";
49std::cout << "args: " << args.get() << std::endl;
50        std::string command = args.get();
51        if (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}')
52            command = command.substr(1, command.size() - 2);
53        CommandEvaluation evaluation = CommandExecutor::evaluate(command);
54std::cout << "2\n";
55        if (!CommandExecutor::execute(evaluation))
56            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
57std::cout << "3\n";
58        if (evaluation.hasReturnvalue())
59        {
60std::cout << "4\n";
61            return evaluation.getReturnvalue().toString();
62        }
63std::cout << "5\n";
64        return "";
65    }
66
67    std::string tcl(const std::string& tclcode)
68    {
69        try
70        {
71            static Tcl::interpreter i;
72            i.def("puts", Tcl_puts, Tcl::variadic());
73            i.def("execute", Tcl_execute, Tcl::variadic());
74            i.eval("proc unknown {args} { return [execute $args] }");
75            std::string output = i.eval(tclcode);
76            if (output != "")
77                COUT(0) << "tcl> " << output << std::endl;
78            return output;
79        }
80        catch (Tcl::tcl_error const &e)
81        {
82            COUT(1) << "tcl> Error: " << e.what() << std::endl;
83        }
84        catch (std::exception const &e)
85        {
86            COUT(1) << "Error while executing tcl: " << e.what() << std::endl;
87        }
88
89        return "";
90    }
91}
Note: See TracBrowser for help on using the repository browser.