/* core/commandbuffer.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "core/commandbuffer.h" #include "sys/sys.h" // C++ headers #include #include #include namespace core { std::stringstream cmd(std::stringstream::in | std::stringstream::out); namespace commandbuffer { void exec(const char *text) { std::stringstream cmdstream(text); std::string command; if (!(cmdstream >> command)) return; // is it a function Func f = func::find(command); if (f) { // function exists, execute it if (f->flags() && func::Game) { // it's a game function if (game() && game()->connected) { GameFuncPtr function = (GameFuncPtr) f->ptr; function(localplayer, cmdstream); } } else { // it's a normal function FuncPtr function = (FuncPtr) f->ptr; function(cmdstream); } return; } // is it a cvar Cvar cv = cvar::find(command); if (cv) { // cvar exists std::string args; if (((cv->flags() & cvar::ReadOnly) == 0) && (cmdstream >> args)) { // we're setting a new value char c; while (cmdstream >> c) args += c; (*cv) = args; } con_print << command << " " << cv->text() << std::endl; return; } con_print << "Unknown command '" << command << "'" << std::endl; } void execute() { if (core::cmd.eof()) return; char line[MAXCMDSIZE]; while (core::cmd.getline(line, MAXCMDSIZE-1)) { exec(line); } cmd.clear(); } void clear() { char line[MAXCMDSIZE]; while (core::cmd.getline(line, MAXCMDSIZE-1)); } void complete(std::string &input, size_t &pos) { std::list match; std::string partial = input.substr(0, pos); if (!partial.size()) return; // search function registry for matches std::map::iterator f; for (f = func::registry.begin(); f != func::registry.end(); f++) { if (partial == (*f).first.substr(0, partial.size())) { match.push_back((*f).first); //con_print << " " << (*f).first << std::endl; } } // search cvar registry for matches std::map::iterator c; for (c = cvar::registry.begin(); c != cvar::registry.end(); c++) { if (partial == (*c).first.substr(0, partial.size())) { match.push_back((*c).first); //con_print << " " << (*c).first << std::endl; } } if (!match.size()) return; std::string maxmatch(*match.begin()); if (match.size() > 1) { std::list::iterator l; for (l = match.begin(); l !=match.end(); l++) { if (maxmatch.size()) { size_t i =0; while ((i < maxmatch.size() && i < (*l).size()) && (maxmatch[i] == (*l)[i])) { i++; } if (i < maxmatch.size()) maxmatch.erase(i); } con_print << " " << (*l) << std::endl; } con_print << match.size() << " matches" << std::endl; } if (maxmatch.size() > partial.size()) { if (match.size()==1) maxmatch += ' '; input.replace(0, pos, maxmatch); pos = maxmatch.size(); } } } // namespace commandbuffer } // namespace core