[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5068] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[5129] | 18 | #include "shell_command.h" |
---|
[5639] | 19 | #include "shell_command_class.h" |
---|
[1853] | 20 | |
---|
[6222] | 21 | #include "compiler.h" |
---|
[5129] | 22 | #include "debug.h" |
---|
[5113] | 23 | #include "class_list.h" |
---|
| 24 | |
---|
| 25 | #include "key_names.h" |
---|
[5075] | 26 | #include <stdarg.h> |
---|
| 27 | #include <stdio.h> |
---|
[5174] | 28 | #include <string.h> |
---|
[5075] | 29 | |
---|
[1856] | 30 | using namespace std; |
---|
[1853] | 31 | |
---|
[5166] | 32 | /** |
---|
| 33 | * constructs and registers a new Command |
---|
| 34 | * @param commandName the name of the Command |
---|
| 35 | * @param className the name of the class to apply this command to |
---|
| 36 | * @param paramCount the count of parameters this command takes |
---|
| 37 | */ |
---|
[7225] | 38 | ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor) |
---|
[3365] | 39 | { |
---|
[5141] | 40 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
[7199] | 41 | PRINTF(5)("create shellcommand %s %s\n", commandName, className); |
---|
[5141] | 42 | this->setName(commandName); |
---|
[5642] | 43 | this->executor = executor.clone(); |
---|
[7201] | 44 | this->executor->setName(commandName); |
---|
[5141] | 45 | |
---|
[5161] | 46 | // this->classID = classID; |
---|
[5198] | 47 | this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID); |
---|
| 48 | if (this->shellClass != NULL) |
---|
[5779] | 49 | this->shellClass->commandList.push_back(this); |
---|
[5068] | 50 | } |
---|
[4320] | 51 | |
---|
[5166] | 52 | /** |
---|
| 53 | * deconstructs a ShellCommand |
---|
| 54 | */ |
---|
[5636] | 55 | ShellCommand::~ShellCommand() |
---|
[5130] | 56 | { |
---|
[5196] | 57 | if (this->alias != NULL && ShellCommandClass::aliasList != NULL) |
---|
| 58 | { |
---|
| 59 | ShellCommandClass::aliasList->remove(this->alias); |
---|
| 60 | delete this->alias; |
---|
| 61 | } |
---|
[5641] | 62 | delete this->executor; |
---|
[5130] | 63 | } |
---|
[1853] | 64 | |
---|
[5166] | 65 | /** |
---|
[5636] | 66 | * registers a new ShellCommand |
---|
| 67 | */ |
---|
[7225] | 68 | ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, const Executor& executor) |
---|
[5636] | 69 | { |
---|
[7201] | 70 | if (ShellCommand::isRegistered(commandName, className)) |
---|
[5637] | 71 | return NULL; |
---|
| 72 | else |
---|
| 73 | return new ShellCommand(commandName, className, executor); |
---|
[5636] | 74 | |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
[5166] | 78 | * unregister an existing commandName |
---|
| 79 | * @param className the name of the Class the command belongs to. |
---|
| 80 | * @param commandName the name of the command itself |
---|
| 81 | */ |
---|
[7225] | 82 | void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className) |
---|
[5165] | 83 | { |
---|
[5779] | 84 | /// FIXME |
---|
| 85 | /* if (ShellCommandClass::commandClassList == NULL) |
---|
[5171] | 86 | ShellCommandClass::initCommandClassList(); |
---|
| 87 | |
---|
[5172] | 88 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
[5171] | 89 | |
---|
[5172] | 90 | if (checkClass != NULL) |
---|
[5171] | 91 | { |
---|
[5779] | 92 | std::list<ShellCommand*>::iterator elem; |
---|
| 93 | for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++) |
---|
[5171] | 94 | { |
---|
[5779] | 95 | if (!strcmp(commandName, (*elem)->getName())) |
---|
[5171] | 96 | { |
---|
[5779] | 97 | delete (*elem); |
---|
| 98 | checkClass->commandList.remove(*elem); |
---|
[5171] | 99 | break; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
[5779] | 103 | if (checkClass->commandList->size() == 0) |
---|
[5171] | 104 | { |
---|
| 105 | ShellCommandClass::commandClassList->remove(checkClass); |
---|
| 106 | delete checkClass; |
---|
| 107 | } |
---|
[5779] | 108 | }*/ |
---|
[5165] | 109 | } |
---|
| 110 | |
---|
[5166] | 111 | /** |
---|
| 112 | * checks if a command has already been registered. |
---|
| 113 | * @param commandName the name of the Command |
---|
| 114 | * @param className the name of the Class the command should apply to. |
---|
| 115 | * @returns true, if the command is registered/false otherwise |
---|
| 116 | * |
---|
| 117 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
| 118 | * This is checked in the registerCommand-function. |
---|
| 119 | */ |
---|
[7225] | 120 | bool ShellCommand::isRegistered(const std::string& commandName, const std::string& className) |
---|
[5113] | 121 | { |
---|
[5170] | 122 | if (ShellCommandClass::commandClassList == NULL) |
---|
[5072] | 123 | { |
---|
[5170] | 124 | ShellCommandClass::initCommandClassList(); |
---|
[5113] | 125 | return false; |
---|
| 126 | } |
---|
[5105] | 127 | |
---|
[5170] | 128 | const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className); |
---|
| 129 | if (checkClass != NULL) |
---|
[5113] | 130 | { |
---|
[5779] | 131 | std::list<ShellCommand*>::const_iterator elem; |
---|
| 132 | for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++) |
---|
| 133 | { |
---|
[7225] | 134 | if (commandName == (*elem)->getName()) |
---|
[5779] | 135 | { |
---|
[7225] | 136 | PRINTF(2)("Command '%s::%s' already registered\n", className.c_str(), commandName.c_str()); |
---|
[5779] | 137 | return true; |
---|
[5170] | 138 | } |
---|
[5779] | 139 | } |
---|
[5170] | 140 | return false; |
---|
[5113] | 141 | } |
---|
[5170] | 142 | else |
---|
| 143 | return false; |
---|
[5113] | 144 | } |
---|
| 145 | |
---|
[5140] | 146 | |
---|
[5145] | 147 | /** |
---|
| 148 | * executes commands |
---|
| 149 | * @param executionString the string containing the following input |
---|
[5148] | 150 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
[5145] | 151 | * @return true on success, false otherwise. |
---|
| 152 | */ |
---|
[7221] | 153 | bool ShellCommand::execute(const std::string& executionString) |
---|
[5135] | 154 | { |
---|
[5198] | 155 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 156 | return false; |
---|
| 157 | |
---|
[5779] | 158 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
| 159 | ShellCommandClass* commandClass = NULL; //< the command class this command applies to. |
---|
[5885] | 160 | const std::list<BaseObject*>* objectList = NULL; //< the list of Objects stored in classID |
---|
[5779] | 161 | BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on |
---|
| 162 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
| 163 | unsigned int fktPos = 1; //< the position of the function (needed for finding it) |
---|
| 164 | // long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
[5656] | 165 | SubString inputSplits(executionString, " \t\n,"); |
---|
[5198] | 166 | |
---|
| 167 | if (inputSplits.getCount() == 0) |
---|
| 168 | return false; |
---|
| 169 | if (inputSplits.getCount() >= 1) |
---|
| 170 | { |
---|
[5200] | 171 | // CHECK FOR ALIAS |
---|
[5198] | 172 | if (ShellCommandClass::aliasList != NULL) |
---|
| 173 | { |
---|
[5779] | 174 | list<ShellCommandAlias*>::iterator alias; |
---|
| 175 | for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++ ) |
---|
[5198] | 176 | { |
---|
[7221] | 177 | if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL && |
---|
[5779] | 178 | (*alias)->getCommand()->shellClass != NULL ) |
---|
[5198] | 179 | { |
---|
[5779] | 180 | objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName()); |
---|
[5199] | 181 | if (objectList != NULL) |
---|
| 182 | { |
---|
[5204] | 183 | if (inputSplits.getCount() > 1) |
---|
[7221] | 184 | { |
---|
| 185 | |
---|
| 186 | (*alias)->getCommand()->executor->execute(objectList->front(), executionString.substr(inputSplits.getOffset(1))); /// TODO CHECK IF OK |
---|
| 187 | } |
---|
[5204] | 188 | else |
---|
[5779] | 189 | (*alias)->getCommand()->executor->execute(objectList->front(), ""); |
---|
| 190 | return true; |
---|
[5199] | 191 | } |
---|
[5198] | 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
[5203] | 195 | // looking for a Matching Class |
---|
| 196 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
| 197 | { |
---|
[5779] | 198 | list<ShellCommandClass*>::iterator commandClassIT; |
---|
| 199 | for (commandClassIT = ShellCommandClass::commandClassList->begin(); commandClassIT != ShellCommandClass::commandClassList->end(); commandClassIT++) |
---|
[5203] | 200 | { |
---|
[7221] | 201 | if ((*commandClassIT)->getName() && inputSplits.getString(0) == (*commandClassIT)->getName()) |
---|
[5203] | 202 | { |
---|
| 203 | //elemCL->getName(); |
---|
[5779] | 204 | classID = ClassList::StringToID((*commandClassIT)->getName()); |
---|
| 205 | commandClass = (*commandClassIT); |
---|
[5791] | 206 | objectList = ClassList::getList((ClassID)classID); |
---|
[5203] | 207 | break; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | } |
---|
[5200] | 211 | |
---|
[5329] | 212 | if (commandClass != NULL && inputSplits.getCount() >= 2) |
---|
[5203] | 213 | { |
---|
[5329] | 214 | if (objectList != NULL) |
---|
[5203] | 215 | { |
---|
[5329] | 216 | // Checking for a Match in the Objects of classID (else take the first) |
---|
[5779] | 217 | list<BaseObject*>::const_iterator object; |
---|
| 218 | for (object = objectList->begin(); object != objectList->end(); object++) |
---|
[5203] | 219 | { |
---|
[7221] | 220 | if ((*object)->getName() != NULL && inputSplits.getString(1) == (*object)->getName() ) |
---|
[5329] | 221 | { |
---|
[5779] | 222 | objectPointer = (*object); |
---|
[5329] | 223 | fktPos = 2; |
---|
| 224 | break; |
---|
| 225 | } |
---|
| 226 | } |
---|
[5203] | 227 | |
---|
| 228 | // |
---|
[5329] | 229 | if (objectPointer == NULL) |
---|
[5779] | 230 | objectPointer = objectList->front(); |
---|
[5329] | 231 | } |
---|
[5203] | 232 | // match a function. |
---|
[5329] | 233 | if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3))) |
---|
[5203] | 234 | { |
---|
[5779] | 235 | list<ShellCommand*>::iterator cmdIT; |
---|
| 236 | for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++) |
---|
[5203] | 237 | { |
---|
[7221] | 238 | if (inputSplits.getString(fktPos) == (*cmdIT)->getName()) |
---|
[5203] | 239 | { |
---|
[5779] | 240 | if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective) |
---|
[5329] | 241 | return false; |
---|
[5203] | 242 | if (inputSplits.getCount() > fktPos+1) |
---|
[7221] | 243 | (*cmdIT)->executor->execute(objectPointer, executionString.substr(inputSplits.getOffset(fktPos +1))); /// TODO CHECK IF OK |
---|
[5203] | 244 | else |
---|
[5779] | 245 | (*cmdIT)->executor->execute(objectPointer, ""); |
---|
[5203] | 246 | return true; |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | } |
---|
[5198] | 251 | } |
---|
[5135] | 252 | } |
---|
[5148] | 253 | |
---|
[5166] | 254 | /** |
---|
| 255 | * lets a command be described |
---|
| 256 | * @param description the description of the Given command |
---|
| 257 | */ |
---|
[7221] | 258 | ShellCommand* ShellCommand::describe(const std::string& description) |
---|
[5164] | 259 | { |
---|
| 260 | if (this == NULL) |
---|
| 261 | return NULL; |
---|
[5165] | 262 | else |
---|
| 263 | { |
---|
| 264 | this->description = description; |
---|
| 265 | return this; |
---|
| 266 | } |
---|
[5164] | 267 | } |
---|
| 268 | |
---|
[5197] | 269 | /** |
---|
| 270 | * adds an Alias to this Command |
---|
| 271 | * @param alias the name of the Alias to set |
---|
| 272 | * @returns itself |
---|
| 273 | */ |
---|
[7225] | 274 | ShellCommand* ShellCommand::setAlias(const std::string& alias) |
---|
[5195] | 275 | { |
---|
[5196] | 276 | if (this == NULL) |
---|
| 277 | return NULL; |
---|
| 278 | |
---|
| 279 | if (this->alias != NULL) |
---|
| 280 | { |
---|
| 281 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName()); |
---|
| 282 | } |
---|
| 283 | else |
---|
| 284 | { |
---|
| 285 | if (ShellCommandClass::aliasList == NULL) |
---|
[5779] | 286 | ShellCommandClass::aliasList = new std::list<ShellCommandAlias*>; |
---|
[5196] | 287 | |
---|
| 288 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
[5779] | 289 | ShellCommandClass::aliasList->push_back(aliasCMD); |
---|
[5196] | 290 | this->alias = aliasCMD; |
---|
| 291 | } |
---|
| 292 | return this; |
---|
[5195] | 293 | } |
---|
| 294 | |
---|
[5166] | 295 | /** |
---|
[7198] | 296 | * @brief set the default values of the executor |
---|
| 297 | * @param value0 the first default value |
---|
| 298 | * @param value1 the second default value |
---|
| 299 | * @param value2 the third default value |
---|
| 300 | * @param value3 the fourth default value |
---|
| 301 | * @param value4 the fifth default value |
---|
[5207] | 302 | */ |
---|
[7198] | 303 | ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1, |
---|
| 304 | const MultiType& value2, const MultiType& value3, |
---|
| 305 | const MultiType& value4) |
---|
[5207] | 306 | { |
---|
[7201] | 307 | if (this == NULL || this->executor == NULL) |
---|
[5207] | 308 | return NULL; |
---|
| 309 | |
---|
[7198] | 310 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
---|
[5207] | 311 | |
---|
| 312 | return this; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | /** |
---|
[5166] | 316 | * prints out nice information about the Shells Commands |
---|
| 317 | */ |
---|
[5636] | 318 | void ShellCommand::debug() |
---|
[5148] | 319 | { |
---|
[5170] | 320 | if (ShellCommandClass::commandClassList == NULL) |
---|
[5148] | 321 | { |
---|
[5171] | 322 | PRINT(0)("No Command registered.\n"); |
---|
[5148] | 323 | return; |
---|
| 324 | } |
---|
| 325 | |
---|
[5779] | 326 | list<ShellCommandClass*>::iterator classIT; |
---|
| 327 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5148] | 328 | { |
---|
[7221] | 329 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
[5779] | 330 | |
---|
| 331 | list<ShellCommand*>::iterator cmdIT; |
---|
| 332 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
[5170] | 333 | { |
---|
[5779] | 334 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
[5642] | 335 | /// FIXME |
---|
| 336 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 337 | printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
[7221] | 338 | if (!(*cmdIT)->description.empty()) |
---|
| 339 | printf("- %s", (*cmdIT)->description.c_str()); |
---|
[5170] | 340 | printf("\n"); |
---|
[5148] | 341 | |
---|
[5170] | 342 | } |
---|
[5148] | 343 | } |
---|
| 344 | } |
---|
| 345 | |
---|
[5166] | 346 | /** |
---|
| 347 | * converts a Parameter to a String |
---|
| 348 | * @param parameter the Parameter we have. |
---|
| 349 | * @returns the Name of the Parameter at Hand |
---|
| 350 | */ |
---|
[5636] | 351 | const char* ShellCommand::paramToString(long parameter) |
---|
[5148] | 352 | { |
---|
[5634] | 353 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
[5148] | 354 | } |
---|