/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL #include "shell_command.h" #include "shell_command_class.h" #include "compiler.h" #include "debug.h" #include "class_list.h" #include "key_names.h" namespace OrxShell { SHELL_COMMAND_STATIC(debug, ShellCommand, ShellCommand::debug); /** * @brief constructs and registers a new Command * @param commandName the name of the Command * @param className the name of the class to apply this command to * @param paramCount the count of parameters this command takes */ ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor) { this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); PRINTF(5)("create shellcommand %s %s\n", commandName, className); this->setName(commandName); // copy the executor: this->executor = executor.clone(); this->executor->setName(commandName); for (unsigned int i = 0; i < this->executor->getParamCount(); i++) this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i))); this->alias = NULL; // this->classID = classID; this->shellClass = ShellCommandClass::acquireCommandClass(className); assert (this->shellClass != NULL); this->shellClass->registerCommand(this); } /** * @brief deconstructs a ShellCommand */ ShellCommand::~ShellCommand() { this->shellClass->unregisterCommand(this); if (this->alias != NULL) delete this->alias; while (!this->completors.empty()) { delete this->completors.back(); this->completors.pop_back(); } delete this->executor; } /** * @brief registers a new ShellCommand */ ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, const Executor& executor) { if (ShellCommand::exists(commandName, className)) return NULL; else return new ShellCommand(commandName, className, executor); } /** * @brief unregister an existing commandName * @param className the name of the Class the command belongs to. * @param commandName the name of the command itself */ void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className) { ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className); if (cmdClass != NULL) { std::vector::iterator cmd; for (cmd = cmdClass->commandList.begin(); cmd < cmdClass->commandList.end(); cmd++) if (commandName == (*cmd)->getName()) delete (*cmd); } } /** * @brief gets a command if it has already been registered. * @param commandName the name of the Command * @param className the name of the Class the command should apply to. * @returns The Registered Command, or NULL if it does not exist. */ const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className) { const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className); if (likely(checkClass != NULL)) { std::vector::const_iterator elem; for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++) if (commandName == (*elem)->getName()) { PRINTF(2)("Command '%s::%s' already registered\n", className.c_str(), commandName.c_str()); return (*elem); } } return NULL; } /** * @brief checks if a command has already been registered. * @param commandName the name of the Command * @param className the name of the Class the command should apply to. * @returns true, if the command is registered/false otherwise * * This is used internally, to see, if we have multiple command subscriptions. * This is checked in the registerCommand-function. */ bool ShellCommand::exists(const std::string& commandName, const std::string& className) { return (ShellCommand::getCommand(commandName, className) != NULL); } /** * @brief executes commands * @param executionString the string containing the following input * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] * @return true on success, false otherwise. */ bool ShellCommand::execute(const std::string& executionString) { long classID = CL_NULL; //< the classID retrieved from the Class. ShellCommandClass* commandClass = NULL; //< the command class this command applies to. const std::list* objectList = NULL; //< the list of Objects stored in classID BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on // bool emptyComplete = false; //< if the completion input is empty string. e.g "" // long completeType = SHELLC_NONE; //< the Type we'd like to complete. SubString inputSplits(executionString, SubString::WhiteSpacesWithComma); // if we do not have any input return if (inputSplits.empty()) return false; // if we only have one input (!MUST BE AN ALIAS) // CHECK FOR ALIAS std::vector::const_iterator alias; for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ ) { if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL ) { objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName()); if (objectList != NULL) { (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join()); return true; } } } // looking for a Matching Class (in the First Argument) std::vector::iterator commandClassIT; for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++) { if (inputSplits[0] == (*commandClassIT)->getName()) { //elemCL->getName(); classID = ClassList::StringToID((*commandClassIT)->getName()); commandClass = (*commandClassIT); objectList = ClassList::getList((ClassID)classID); break; } } // Second Agument. (either Object, or Function) if (commandClass != NULL && inputSplits.size() >= 2) { int fktPos = 1; // The position of the Function (either at pos 1(if object given), or 2) // If we have an ObjectList. if (objectList != NULL) { // Checking for a Match in the Objects of classID (else take the first) std::list::const_iterator object; for (object = objectList->begin(); object != objectList->end(); object++) { if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName()) { /// TODO make this work for multiple Objects at once. objectPointer = (*object); fktPos = 2; break; } } // if we did not find an Object with matching name, take the first. if (objectPointer == NULL) objectPointer = objectList->front(); } // match a function. if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3))) { std::vector::iterator cmdIT; for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++) { if (inputSplits[fktPos] == (*cmdIT)->getName()) { if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective) return false; else { (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK return true; } } } } } return false; } /** * @brief lets a command be described * @param description the description of the Given command */ ShellCommand* ShellCommand::describe(const std::string& description) { if (this == NULL) return NULL; this->description = description; return this; } /** * @brief adds an Alias to this Command * @param alias the name of the Alias to set * @returns itself */ ShellCommand* ShellCommand::setAlias(const std::string& alias) { if (this == NULL) return NULL; if (this->alias != NULL) { PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); } else { ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); this->alias = aliasCMD; } return this; } /** * @brief set the default values of the executor * @param value0 the first default value * @param value1 the second default value * @param value2 the third default value * @param value3 the fourth default value * @param value4 the fifth default value */ ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1, const MultiType& value2, const MultiType& value3, const MultiType& value4) { if (this == NULL || this->executor == NULL) return NULL; this->executor->defaultValues(value0, value1, value2, value3, value4); return this; } /** * @brief prints out nice information about the Shells Commands */ void ShellCommand::debug() { std::vector::iterator classIT; for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) { PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); std::vector::iterator cmdIT; for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) { PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); /// FIXME /* for (unsigned int i = 0; i< elem->paramCount; i++) printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ if (!(*cmdIT)->description.empty()) printf("- %s", (*cmdIT)->description.c_str()); printf("\n"); } } } /** * @brief converts a Parameter to a String * @param parameter the Parameter we have. * @returns the Name of the Parameter at Hand */ const std::string& ShellCommand::paramToString(long parameter) { return MultiType::MultiTypeToString((MT_Type)parameter); } /** * @param aliasName the name of the Alias * @param command the Command, to associate this alias with */ ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command) { this->aliasName = aliasName; this->command = command; ShellCommandAlias::aliasList.push_back(this); }; ShellCommandAlias::~ShellCommandAlias() { std::vector::iterator delA = std::find(aliasList.begin(), aliasList.end(), this); if (delA != aliasList.end()) ShellCommandAlias::aliasList.push_back(this); } std::vector ShellCommandAlias::aliasList; /** * @brief collects the Aliases registered to the ShellCommands * @param stringList a List to paste the Aliases into. * @returns true on success, false otherwise */ bool ShellCommandAlias::getCommandListOfAlias(std::list& stringList) { std::vector::iterator alias; for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++) stringList.push_back((*alias)->getName()); return true; } }