/* 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 ShellCommandClass * @param className the Name of the command-class to create */ ShellCommandClass::ShellCommandClass(const char* className) { this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass"); this->setName(className); this->className = className; this->classID = CL_NULL; this->commandList = new tList; ShellCommandClass::commandClassList->add(this); } /** * destructs the shellCommandClass again */ ShellCommandClass::~ShellCommandClass() { tIterator* iterator = this->commandList->getIterator(); ShellCommandBase* 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 ShellCommandClass::getCommandListOfClass(const char* className, tList* stringList) { if (stringList == NULL || className == NULL) return false; tIterator* iterator = ShellCommandClass::commandClassList->getIterator(); ShellCommandClass* elem = iterator->firstElement(); while(elem != NULL) { if (!strcmp (elem->getName(), className)) { tIterator* itFkt = elem->commandList->getIterator(); ShellCommandBase* 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 ShellCommands * @param stringList a List to paste the Aliases into. * @returns true on success, false otherwise */ bool ShellCommandClass::getCommandListOfAlias(tList* stringList) { if (stringList == NULL || ShellCommandClass::aliasList == NULL) return false; tIterator* iterator = ShellCommandClass::aliasList->getIterator(); ShellCommandAlias* elem = iterator->firstElement(); while(elem != NULL) { stringList->add(elem->getName()); elem = iterator->nextElement(); } delete iterator; return true; } /** * unregisters all Commands that exist */ void ShellCommandClass::unregisterAllCommands() { // unregister all commands tIterator* iterator = ShellCommandClass::commandClassList->getIterator(); ShellCommandClass* elem = iterator->firstElement(); while(elem != NULL) { delete elem; elem = iterator->nextElement(); } delete iterator; delete ShellCommandClass::commandClassList; ShellCommandClass::commandClassList = NULL; // unregister all aliases (there should be nothing to do here :)) if (ShellCommandClass::aliasList != NULL) { tIterator* itAL = ShellCommandClass::aliasList->getIterator(); ShellCommandAlias* elemAL = itAL->firstElement(); while(elemAL != NULL) { delete elemAL; elemAL = itAL->nextElement(); } delete itAL; delete ShellCommandClass::aliasList; ShellCommandClass::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 ShellCommandClass* ShellCommandClass::isRegistered(const char* className) { if (ShellCommandClass::commandClassList == NULL) initCommandClassList(); tIterator* iterator = ShellCommandClass::commandClassList->getIterator(); ShellCommandClass* 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 */ ShellCommandClass* ShellCommandClass::getCommandClass(const char* className) { if (ShellCommandClass::commandClassList == NULL) initCommandClassList(); tIterator* iterator = ShellCommandClass::commandClassList->getIterator(); ShellCommandClass* elem = iterator->firstElement(); while(elem != NULL) { if (!strcmp(className, elem->className)) { delete iterator; return elem; } elem = iterator->nextElement(); } delete iterator; return new ShellCommandClass(className); } /** * initializes the CommandList (if it is NULL) */ void ShellCommandClass::initCommandClassList() { if (ShellCommandClass::commandClassList == NULL) { ShellCommandClass::commandClassList = new tList; ShellCommandStatic::registerCommand("debug", "ShellCommand", ShellCommandBase::debug); } } void ShellCommandClass::help(const char* className) { if (className == NULL) return; if (likely(ShellCommandClass::commandClassList != NULL)) { tIterator* itCL = ShellCommandClass::commandClassList->getIterator(); ShellCommandClass* 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 ShellCommandBase* 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 ", ShellCommandBase::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* ShellCommandClass::commandClassList = NULL; tList* ShellCommandClass::aliasList = NULL; //////////////////////// // SHELL COMMAND BASE // //////////////////////// /** * 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 */ ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...) { this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); this->setName(commandName); this->description = NULL; this->alias = NULL; // this->classID = classID; this->shellClass = ShellCommandClass::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]; this->defaultValue = new MultiType[paramCount]; va_list parameterList; va_start(parameterList, paramCount); // What Parameters we have got for (unsigned int i = 0; i < paramCount; i++) this->parameters[i] = va_arg(parameterList, int); } /** * deconstructs a ShellCommand * @return */ ShellCommandBase::~ShellCommandBase() { delete[] this->parameters; delete[] this->defaultValue; if (this->alias != NULL && ShellCommandClass::aliasList != NULL) { ShellCommandClass::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 ShellCommandBase::unregisterCommand(const char* commandName, const char* className) { if (ShellCommandClass::commandClassList == NULL) ShellCommandClass::initCommandClassList(); const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); if (checkClass != NULL) { tIterator* iterator = checkClass->commandList->getIterator(); ShellCommandBase* 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) { ShellCommandClass::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 ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...) { if (ShellCommandClass::commandClassList == NULL) { ShellCommandClass::initCommandClassList(); return false; } const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); if (checkClass != NULL) { tIterator* iterator = checkClass->commandList->getIterator(); ShellCommandBase* 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 ShellCommandBase::execute(const char* executionString) { if (ShellCommandClass::commandClassList == NULL) return false; long classID = CL_NULL; //< the classID retrieved from the Class. ShellCommandClass* 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 (ShellCommandClass::aliasList != NULL) { tIterator* itAL = ShellCommandClass::aliasList->getIterator(); ShellCommandAlias* 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(ShellCommandClass::commandClassList != NULL)) { tIterator* itCL = ShellCommandClass::commandClassList->getIterator(); ShellCommandClass* 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(); ShellCommandBase* enumCMD = itCMD->firstElement(); while (enumCMD != NULL) { if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos))) { if (objectPointer == NULL && enumCMD->functorType == ShellCommand_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 */ ShellCommandBase* ShellCommandBase::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 */ ShellCommandBase* ShellCommandBase::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 (ShellCommandClass::aliasList == NULL) ShellCommandClass::aliasList = new tList; ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); ShellCommandClass::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] */ ShellCommandBase* ShellCommandBase::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->defaultValue[i].setInt(va_arg(defaultList, int)); break; case ParameterChar: this->defaultValue[i].setChar((char)va_arg(defaultList, int)); break; case ParameterString: this->defaultValue[i].setString(va_arg(defaultList, char*)); break; case ParameterInt: this->defaultValue[i].setInt(va_arg(defaultList, int)); break; case ParameterUInt: this->defaultValue[i].setInt((int)va_arg(defaultList, unsigned int)); break; case ParameterFloat: this->defaultValue[i].setFloat(va_arg(defaultList, double)); break; case ParameterLong: this->defaultValue[i].setInt((int) va_arg(defaultList, long)); break; default: break; } } return this; } /** * prints out nice information about the Shells Commands */ void ShellCommandBase::debug() { if (ShellCommandClass::commandClassList == NULL) { PRINT(0)("No Command registered.\n"); return; } tIterator* iteratorCL = ShellCommandClass::commandClassList->getIterator(); ShellCommandClass* 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 ShellCommandBase* 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 ", ShellCommandBase::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* ShellCommandBase::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; } }