/* 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_ #include "shell_command.h" #include "list.h" #include "debug.h" #include "class_list.h" #include "key_names.h" #include #include #include using namespace std; /** * creates a new CommandChainClass * @param className the Name of the command-class to create */ CommandChainClass::CommandChainClass(const char* className) { this->setClassID(CL_COMMAND_CHAIN_CLASS, "CommandChainClass"); this->setName(className); this->className = className; this->classID = CL_NULL; this->commandList = new tList; CommandChainClass::commandClassList->add(this); } /** * destructs the shellCommandClass again */ CommandChainClass::~CommandChainClass() { tIterator* iterator = this->commandList->getIterator(); CommandChainBase* elem = iterator->firstElement(); while(elem != NULL) { delete elem; elem = iterator->nextElement(); } delete iterator; delete this->commandList; } /** * collects the Commands registered to some class. * @param className the name of the Class to collect the Commands from. * @param stringList a List to paste the Commands into. * @returns true on success, false otherwise */ bool CommandChainClass::getCommandListOfClass(const char* className, tList* stringList) { if (stringList == NULL || className == NULL) return false; tIterator* iterator = CommandChainClass::commandClassList->getIterator(); CommandChainClass* elem = iterator->firstElement(); while(elem != NULL) { if (!strcmp (elem->getName(), className)) { tIterator* itFkt = elem->commandList->getIterator(); CommandChainBase* command = itFkt->firstElement(); while (command != NULL) { stringList->add(command->getName()); command = itFkt->nextElement(); } delete itFkt; } elem = iterator->nextElement(); } delete iterator; return true; } /** * collects the Aliases registered to the CommandChains * @param stringList a List to paste the Aliases into. * @returns true on success, false otherwise */ bool CommandChainClass::getCommandListOfAlias(tList* stringList) { if (stringList == NULL || CommandChainClass::aliasList == NULL) return false; tIterator* iterator = CommandChainClass::aliasList->getIterator(); CommandChainAlias* elem = iterator->firstElement(); while(elem != NULL) { stringList->add(elem->getName()); elem = iterator->nextElement(); } delete iterator; return true; } /** * unregisters all Commands that exist */ void CommandChainClass::unregisterAllCommands() { // unregister all commands tIterator* iterator = CommandChainClass::commandClassList->getIterator(); CommandChainClass* elem = iterator->firstElement(); while(elem != NULL) { delete elem; elem = iterator->nextElement(); } delete iterator; delete CommandChainClass::commandClassList; CommandChainClass::commandClassList = NULL; // unregister all aliases (there should be nothing to do here :)) if (CommandChainClass::aliasList != NULL) { tIterator* itAL = CommandChainClass::aliasList->getIterator(); CommandChainAlias* elemAL = itAL->firstElement(); while(elemAL != NULL) { delete elemAL; elemAL = itAL->nextElement(); } delete itAL; delete CommandChainClass::aliasList; CommandChainClass::aliasList = NULL; } } /** * checks if a Class is already registered to the Commands' class-stack * @param className the Name of the Class to check for * @returns the CommandClass if found, NULL otherwise */ const CommandChainClass* CommandChainClass::isRegistered(const char* className) { if (CommandChainClass::commandClassList == NULL) initCommandClassList(); tIterator* iterator = CommandChainClass::commandClassList->getIterator(); CommandChainClass* elem = iterator->firstElement(); while(elem != NULL) { if (!strcmp(className, elem->className)) { if (elem->classID == CL_NULL) elem->classID = ClassList::StringToID(className); delete iterator; return elem; } elem = iterator->nextElement(); } delete iterator; return NULL; } /** * searches for a CommandClass * @param className the name of the CommandClass * @returns the CommandClass if found, or a new CommandClass if not */ CommandChainClass* CommandChainClass::getCommandClass(const char* className) { if (CommandChainClass::commandClassList == NULL) initCommandClassList(); tIterator* iterator = CommandChainClass::commandClassList->getIterator(); CommandChainClass* elem = iterator->firstElement(); while(elem != NULL) { if (!strcmp(className, elem->className)) { delete iterator; return elem; } elem = iterator->nextElement(); } delete iterator; return new CommandChainClass(className); } /** * initializes the CommandList (if it is NULL) */ void CommandChainClass::initCommandClassList() { if (CommandChainClass::commandClassList == NULL) { CommandChainClass::commandClassList = new tList; CommandChainStatic::registerCommand("debug", "CommandChain", CommandChainBase::debug); } } void CommandChainClass::help(const char* className) { if (className == NULL) return; if (likely(CommandChainClass::commandClassList != NULL)) { tIterator* itCL = CommandChainClass::commandClassList->getIterator(); CommandChainClass* elemCL = itCL->firstElement(); while(elemCL != NULL) { if (elemCL->className && !strcasecmp(className, elemCL->className)) { PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); tIterator* iterator = elemCL->commandList->getIterator(); const CommandChainBase* elem = iterator->firstElement(); while(elem != NULL) { PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->paramCount); for (unsigned int i = 0; i< elem->paramCount; i++) PRINT(0)("%s ", CommandChainBase::paramToString(elem->parameters[i])); if (elem->description != NULL) PRINT(0)("- %s", elem->description); PRINT(0)("\n"); elem = iterator->nextElement(); } delete iterator; delete itCL; return; } elemCL = itCL->nextElement(); } delete itCL; PRINTF(3)("Class %s not found in Command's classes\n", className); } else { PRINTF(1)("List of commandClasses does not exist"); } } tList* CommandChainClass::commandClassList = NULL; tList* CommandChainClass::aliasList = NULL; /** * 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 * @return self */ CommandChainBase::CommandChainBase(const char* commandName, const char* className, unsigned int paramCount, ...) { this->setClassID(CL_COMMAND_CHAIN, "CommandChain"); this->setName(commandName); this->description = NULL; this->alias = NULL; // this->classID = classID; this->shellClass = CommandChainClass::getCommandClass(className); //ClassList::IDToString(classID); if (this->shellClass != NULL) this->shellClass->commandList->add(this); // handling parameters, and storing them: if (paramCount > FUNCTOR_MAX_ARGUMENTS) paramCount = FUNCTOR_MAX_ARGUMENTS; this->paramCount = paramCount; this->parameters = new unsigned int[paramCount]; va_list parameterList; va_start(parameterList, paramCount); for (unsigned int i = 0; i < paramCount; i++) { this->parameters[i] = va_arg(parameterList, int); switch (this->parameters[i]) { case ParameterBool: this->defaultBools[i] = va_arg(parameterList, int); break; case ParameterChar: this->defaultStrings[i] = new char[2]; sprintf(this->defaultStrings[0], "%c", va_arg(parameterList, int)); break; case ParameterString: this->defaultStrings[i] = va_arg(parameterList, char*); break; case ParameterInt: this->defaultInts[i] = va_arg(parameterList, int); break; case ParameterUInt: this->defaultInts[i] = va_arg(parameterList, unsigned int); break; case ParameterFloat: this->defaultFloats[i] = va_arg(parameterList, double); break; case ParameterLong: this->defaultInts[i] = va_arg(parameterList, long); break; default: break; } } } /** * deconstructs a CommandChain * @return */ CommandChainBase::~CommandChainBase() { delete[] this->parameters; if (this->alias != NULL && CommandChainClass::aliasList != NULL) { CommandChainClass::aliasList->remove(this->alias); delete this->alias; } } /** * unregister an existing commandName * @param className the name of the Class the command belongs to. * @param commandName the name of the command itself */ void CommandChainBase::unregisterCommand(const char* commandName, const char* className) { if (CommandChainClass::commandClassList == NULL) CommandChainClass::initCommandClassList(); const CommandChainClass* checkClass = CommandChainClass::isRegistered(className); if (checkClass != NULL) { tIterator* iterator = checkClass->commandList->getIterator(); CommandChainBase* elem = iterator->firstElement(); while(elem != NULL) { if (!strcmp(commandName, elem->getName())) { checkClass->commandList->remove(elem); delete elem; break; } elem = iterator->nextElement(); } delete iterator; if (checkClass->commandList->getSize() == 0) { CommandChainClass::commandClassList->remove(checkClass); delete checkClass; } } } /** * 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. * @param paramCount how many arguments the Command takes * @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 CommandChainBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...) { if (CommandChainClass::commandClassList == NULL) { CommandChainClass::initCommandClassList(); return false; } const CommandChainClass* checkClass = CommandChainClass::isRegistered(className); if (checkClass != NULL) { tIterator* iterator = checkClass->commandList->getIterator(); CommandChainBase* elem = iterator->firstElement(); while(elem != NULL) { if (!strcmp(commandName, elem->getName())) { PRINTF(2)("Command already registered\n"); delete iterator; return true; } elem = iterator->nextElement(); } delete iterator; return false; } else return false; } /** * executes commands * @param executionString the string containing the following input * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] * @return true on success, false otherwise. */ #include "stdlibincl.h" bool CommandChainBase::execute(const char* executionString) { if (CommandChainClass::commandClassList == NULL) return false; long classID = CL_NULL; //< the classID retrieved from the Class. CommandChainClass* commandClass = NULL; //< the command class this command applies to. tList* 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 "" unsigned int fktPos = 1; //< the position of the function (needed for finding it) // long completeType = SHELLC_NONE; //< the Type we'd like to complete. SubString inputSplits(executionString, true); if (inputSplits.getCount() == 0) return false; if (inputSplits.getCount() >= 1) { // CHECK FOR ALIAS if (CommandChainClass::aliasList != NULL) { tIterator* itAL = CommandChainClass::aliasList->getIterator(); CommandChainAlias* elemAL = itAL->firstElement(); while(elemAL != NULL) { if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL && elemAL->getCommand()->shellClass != NULL ) { objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName()); if (objectList != NULL) { if (inputSplits.getCount() > 1) elemAL->getCommand()->executeCommand(objectList->firstElement(), executionString+inputSplits.getOffset(1)); else elemAL->getCommand()->executeCommand(objectList->firstElement(), ""); delete itAL; return true; } } elemAL = itAL->nextElement(); } delete itAL; } // looking for a Matching Class if (likely(CommandChainClass::commandClassList != NULL)) { tIterator* itCL = CommandChainClass::commandClassList->getIterator(); CommandChainClass* elemCL = itCL->firstElement(); while(elemCL != NULL) { if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName())) { //elemCL->getName(); classID = ClassList::StringToID(elemCL->getName()); commandClass = elemCL; objectList = ClassList::getList(classID); break; } elemCL = itCL->nextElement(); } delete itCL; } if (commandClass != NULL && inputSplits.getCount() >= 2) { if (objectList != NULL) { // Checking for a Match in the Objects of classID (else take the first) tIterator* itBO = objectList->getIterator(); BaseObject* enumBO = itBO->firstElement(); while(enumBO) { if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1))) { objectPointer = enumBO; fktPos = 2; break; } enumBO = itBO->nextElement(); } delete itBO; // if (objectPointer == NULL) objectPointer = objectList->firstElement(); } // match a function. if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3))) { tIterator* itCMD = commandClass->commandList->getIterator(); CommandChainBase* enumCMD = itCMD->firstElement(); while (enumCMD != NULL) { if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos))) { if (objectPointer == NULL && enumCMD->functorType == CommandChain_Objective) { delete itCMD; return false; } if (inputSplits.getCount() > fktPos+1) enumCMD->executeCommand(objectPointer, executionString+inputSplits.getOffset(fktPos +1)); else enumCMD->executeCommand(objectPointer, ""); delete itCMD; return true; } enumCMD = itCMD->nextElement(); } delete itCMD; } } } } /** * lets a command be described * @param description the description of the Given command */ CommandChainBase* CommandChainBase::describe(const char* description) { if (this == NULL) return NULL; else { this->description = description; return this; } } /** * adds an Alias to this Command * @param alias the name of the Alias to set * @returns itself */ CommandChainBase* CommandChainBase::setAlias(const char* 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 { if (CommandChainClass::aliasList == NULL) CommandChainClass::aliasList = new tList; CommandChainAlias* aliasCMD = new CommandChainAlias(alias, this); CommandChainClass::aliasList->add(aliasCMD); this->alias = aliasCMD; } return this; } /** * sets default Values of the Commands * @param count how many default Values to set. * @param ... the default Values in order. They will be cast to the right type * @returns itself * * Be aware, that when you use this Function, you !!MUST!! match the input as * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS] */ CommandChainBase* CommandChainBase::defaultValues(unsigned int count, ...) { if (this == NULL) return NULL; if (count == 0) return this; if (count > this->paramCount) count = this->paramCount; va_list defaultList; va_start(defaultList, count); for (unsigned int i = 0; i < count; i++) { switch (this->parameters[i]) { case ParameterBool: this->defaultBools[i] = va_arg(defaultList, int); break; case ParameterChar: this->defaultStrings[i] = new char[2]; sprintf(this->defaultStrings[0], "%c", va_arg(defaultList, int)); break; case ParameterString: this->defaultStrings[i] = va_arg(defaultList, char*); break; case ParameterInt: this->defaultInts[i] = va_arg(defaultList, int); break; case ParameterUInt: this->defaultInts[i] = va_arg(defaultList, unsigned int); break; case ParameterFloat: this->defaultFloats[i] = va_arg(defaultList, double); break; case ParameterLong: this->defaultInts[i] = va_arg(defaultList, long); break; default: break; } } return this; } /** * prints out nice information about the Shells Commands */ void CommandChainBase::debug() { if (CommandChainClass::commandClassList == NULL) { PRINT(0)("No Command registered.\n"); return; } tIterator* iteratorCL = CommandChainClass::commandClassList->getIterator(); CommandChainClass* elemCL = iteratorCL->firstElement(); while(elemCL != NULL) { PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); tIterator* iterator = elemCL->commandList->getIterator(); const CommandChainBase* elem = iterator->firstElement(); while(elem != NULL) { PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->paramCount); for (unsigned int i = 0; i< elem->paramCount; i++) printf("%s ", CommandChainBase::paramToString(elem->parameters[i])); if (elem->description != NULL) printf("- %s", elem->description); printf("\n"); elem = iterator->nextElement(); } delete iterator; elemCL = iteratorCL->nextElement(); } delete iteratorCL; } /** * converts a Parameter to a String * @param parameter the Parameter we have. * @returns the Name of the Parameter at Hand */ const char* CommandChainBase::paramToString(long parameter) { switch (parameter) { case ParameterBool: return "BOOL"; break; case ParameterChar: return "CHAR"; break; case ParameterString: return "STRING"; break; case ParameterInt: return "INT"; break; case ParameterUInt: return "UINT"; break; case ParameterFloat: return "FLOAT"; break; case ParameterLong: return "LONG"; break; default: return "NULL"; break; } }