Changeset 1502 for code/trunk/src/core/TclBind.cc
- Timestamp:
- Jun 1, 2008, 3:54:20 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/core/TclBind.cc
r1214 r1502 33 33 #include "CommandExecutor.h" 34 34 #include "Debug.h" 35 #include "TclThreadManager.h" 35 36 #include "TclBind.h" 37 #include "util/String.h" 36 38 37 39 namespace orxonox 38 40 { 39 ConsoleCommandShortcutGeneric(tcl, createExecutor(createFunctor(&TclBind::tcl), "tcl", AccessLevel::None)); 41 SetConsoleCommandShortcutGeneric(tcl, createConsoleCommand(createFunctor(&TclBind::tcl), "tcl")); 42 SetConsoleCommandShortcutGeneric(bgerror, createConsoleCommand(createFunctor(&TclBind::bgerror), "bgerror")); 40 43 41 44 TclBind::TclBind() … … 59 62 void TclBind::setDataPath(const std::string& datapath) 60 63 { 61 this->tclLibPath_ = datapath + "/tcl" ;64 this->tclLibPath_ = datapath + "/tcl" + TCL_VERSION + "/"; 62 65 this->bSetTclLibPath_ = true; 63 66 … … 70 73 { 71 74 this->interpreter_ = new Tcl::interpreter(this->tclLibPath_); 72 this->interpreter_->def("puts", TclBind::puts, Tcl::variadic()); 73 this->interpreter_->def("orxonox", TclBind::orxonox, Tcl::variadic()); 74 this->interpreter_->def("execute", TclBind::execute, Tcl::variadic()); 75 this->interpreter_->eval("proc unknown {args} { return [orxonox $args] }"); 76 this->interpreter_->eval("rename exit tclexit; proc exit {} { orxonox exit }"); 75 this->interpreter_->def("orxonox::query", TclBind::tcl_query, Tcl::variadic()); 76 this->interpreter_->def("orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic()); 77 this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic()); 78 79 try 80 { 81 this->interpreter_->eval("proc query args { orxonox::query $args }"); 82 this->interpreter_->eval("proc crossquery {id args} { orxonox::crossquery 0 $id $args }"); 83 this->interpreter_->eval("set id 0"); 84 this->interpreter_->eval("rename exit tcl::exit; proc exit {} { execute exit }"); 85 this->interpreter_->eval("redef_puts"); 86 } 87 catch (Tcl::tcl_error const &e) 88 { COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl; } 89 catch (std::exception const &e) 90 { COUT(1) << "Error while creating Tcl-interpreter: " << e.what() << std::endl; } 77 91 } 78 92 } … … 89 103 } 90 104 91 void TclBind::puts(Tcl::object const &args)105 std::string TclBind::tcl_query(Tcl::object const &args) 92 106 { 93 COUT(0) << args.get() << std::endl; 94 } 107 COUT(4) << "Tcl_query: " << args.get() << std::endl; 95 108 96 std::string TclBind::orxonox(Tcl::object const &args) 97 { 98 std::cout << "Tcl_execute: args: " << args.get() << std::endl; 99 std::string command = args.get(); 100 101 if (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}') 102 command = command.substr(1, command.size() - 2); 109 std::string command = stripEnclosingBraces(args.get()); 103 110 104 111 if (!CommandExecutor::execute(command, false)) 112 { 105 113 COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; 114 } 106 115 107 116 if (CommandExecutor::getLastEvaluation().hasReturnvalue()) … … 111 120 } 112 121 113 void TclBind:: execute(Tcl::object const &args)122 void TclBind::tcl_execute(Tcl::object const &args) 114 123 { 124 COUT(4) << "Tcl_execute: " << args.get() << std::endl; 125 std::string command = stripEnclosingBraces(args.get()); 126 127 if (!CommandExecutor::execute(command, false)) 128 { 129 COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; 130 } 115 131 } 116 132 117 133 std::string TclBind::tcl(const std::string& tclcode) 118 134 { 119 try135 if (TclBind::getInstance().interpreter_) 120 136 { 121 std::string output = TclBind::getInstance().interpreter_->eval(tclcode); 122 if (output != "") 123 COUT(0) << "tcl> " << output << std::endl; 124 return output; 125 } 126 catch (Tcl::tcl_error const &e) 127 { 128 COUT(1) << "tcl> Error: " << e.what() << std::endl; 129 } 130 catch (std::exception const &e) 131 { 132 COUT(1) << "Error while executing tcl: " << e.what() << std::endl; 137 try 138 { 139 std::string output = TclBind::getInstance().interpreter_->eval(tclcode); 140 if (output != "") 141 { 142 COUT(0) << "tcl> " << output << std::endl; 143 } 144 return output; 145 } 146 catch (Tcl::tcl_error const &e) 147 { COUT(1) << "tcl> Error: " << e.what() << std::endl; } 148 catch (std::exception const &e) 149 { COUT(1) << "Error while executing Tcl: " << e.what() << std::endl; } 133 150 } 134 151 135 152 return ""; 153 } 154 155 void TclBind::bgerror(std::string error) 156 { 157 COUT(1) << "Tcl background error: " << stripEnclosingBraces(error) << std::endl; 136 158 } 137 159 … … 144 166 } 145 167 catch (Tcl::tcl_error const &e) 146 { 147 COUT(1) << "Error: " << e.what() << std::endl; 148 } 168 { COUT(1) << "Tcl error: " << e.what() << std::endl; } 149 169 catch (std::exception const &e) 150 { 151 COUT(1) << "Error while executing tcl: " << e.what() << std::endl; 152 } 170 { COUT(1) << "Error while executing Tcl: " << e.what() << std::endl; } 153 171 154 172 return false;
Note: See TracChangeset
for help on using the changeset viewer.