[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 | |
---|
[7374] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
---|
[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 | |
---|
| 24 | #include "key_names.h" |
---|
[5075] | 25 | |
---|
[7374] | 26 | namespace OrxShell |
---|
[3365] | 27 | { |
---|
[9715] | 28 | ObjectListDefinition(ShellCommand); |
---|
[7742] | 29 | SHELL_COMMAND(debug, ShellCommandClass, help); |
---|
[5141] | 30 | |
---|
[7394] | 31 | |
---|
[7374] | 32 | /** |
---|
[7394] | 33 | * @brief constructs and registers a new Command |
---|
[7374] | 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 | */ |
---|
[7722] | 38 | ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, Executor* executor) |
---|
[7374] | 39 | { |
---|
[9692] | 40 | this->registerObject(this, ShellCommand::_objectList); |
---|
[7456] | 41 | PRINTF(4)("create shellcommand '%s' for class '%s'\n", commandName.c_str(), className.c_str()); |
---|
[7374] | 42 | this->setName(commandName); |
---|
[7398] | 43 | |
---|
| 44 | // copy the executor: |
---|
[7722] | 45 | this->executor = executor; |
---|
[7374] | 46 | this->executor->setName(commandName); |
---|
[7398] | 47 | |
---|
[7407] | 48 | for (unsigned int i = 0; i < this->executor->getParamCount(); i++) |
---|
| 49 | this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i))); |
---|
[7388] | 50 | this->alias = NULL; |
---|
[4320] | 51 | |
---|
[7374] | 52 | // this->classID = classID; |
---|
[7408] | 53 | this->shellClass = ShellCommandClass::acquireCommandClass(className); |
---|
[7398] | 54 | assert (this->shellClass != NULL); |
---|
[7399] | 55 | this->shellClass->registerCommand(this); |
---|
[7374] | 56 | } |
---|
| 57 | |
---|
| 58 | /** |
---|
[7394] | 59 | * @brief deconstructs a ShellCommand |
---|
[7374] | 60 | */ |
---|
| 61 | ShellCommand::~ShellCommand() |
---|
[5196] | 62 | { |
---|
[7399] | 63 | this->shellClass->unregisterCommand(this); |
---|
[7389] | 64 | if (this->alias != NULL) |
---|
[7374] | 65 | delete this->alias; |
---|
[7407] | 66 | while (!this->completors.empty()) |
---|
| 67 | { |
---|
| 68 | delete this->completors.back(); |
---|
| 69 | this->completors.pop_back(); |
---|
| 70 | } |
---|
[7374] | 71 | delete this->executor; |
---|
[5196] | 72 | } |
---|
[1853] | 73 | |
---|
[7374] | 74 | /** |
---|
[7398] | 75 | * @brief registers a new ShellCommand |
---|
[7374] | 76 | */ |
---|
[7722] | 77 | ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, Executor* executor) |
---|
[7374] | 78 | { |
---|
[7403] | 79 | if (ShellCommand::exists(commandName, className)) |
---|
[7722] | 80 | { |
---|
| 81 | delete executor; |
---|
[7374] | 82 | return NULL; |
---|
[7722] | 83 | } |
---|
[7374] | 84 | else |
---|
| 85 | return new ShellCommand(commandName, className, executor); |
---|
[5636] | 86 | |
---|
[7374] | 87 | } |
---|
[5636] | 88 | |
---|
[7374] | 89 | /** |
---|
[7398] | 90 | * @brief unregister an existing commandName |
---|
[7374] | 91 | * @param className the name of the Class the command belongs to. |
---|
| 92 | * @param commandName the name of the command itself |
---|
| 93 | */ |
---|
| 94 | void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className) |
---|
| 95 | { |
---|
[7408] | 96 | ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className); |
---|
[7399] | 97 | if (cmdClass != NULL) |
---|
| 98 | { |
---|
[7742] | 99 | CmdList::iterator cmd; |
---|
[9692] | 100 | for (cmd = cmdClass->_commandList.begin(); cmd != cmdClass->_commandList.end(); cmd++) |
---|
[7399] | 101 | if (commandName == (*cmd)->getName()) |
---|
[7420] | 102 | { |
---|
[7399] | 103 | delete (*cmd); |
---|
[7420] | 104 | break; |
---|
| 105 | } |
---|
[7399] | 106 | } |
---|
[5113] | 107 | } |
---|
[5105] | 108 | |
---|
[7413] | 109 | /** |
---|
| 110 | * @brief gets a command if it has already been registered. |
---|
| 111 | * @param commandName the name of the Command |
---|
| 112 | * @param cmdClass the CommandClass of the Class the command is in. |
---|
| 113 | * @returns The Registered Command, or NULL if it does not exist. |
---|
| 114 | */ |
---|
| 115 | const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const ShellCommandClass* cmdClass) |
---|
| 116 | { |
---|
| 117 | assert(cmdClass != NULL); |
---|
[7408] | 118 | |
---|
[7742] | 119 | CmdList::const_iterator elem; |
---|
[9692] | 120 | for (unsigned int i = 0; i < cmdClass->_commandList.size(); i++) |
---|
[9406] | 121 | { |
---|
[9692] | 122 | if (commandName == cmdClass->_commandList[i]->getName()) |
---|
| 123 | return (cmdClass->_commandList[i]); |
---|
[9406] | 124 | } |
---|
[7413] | 125 | return NULL; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
[7374] | 129 | /** |
---|
[7408] | 130 | * @brief gets a command if it has already been registered. |
---|
[7374] | 131 | * @param commandName the name of the Command |
---|
[7413] | 132 | * @param className the name of the Class the command is in. |
---|
[7408] | 133 | * @returns The Registered Command, or NULL if it does not exist. |
---|
[7374] | 134 | */ |
---|
[7408] | 135 | const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className) |
---|
[5113] | 136 | { |
---|
[7408] | 137 | const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className); |
---|
[7403] | 138 | if (likely(checkClass != NULL)) |
---|
[7413] | 139 | return ShellCommand::getCommand(commandName, checkClass); |
---|
[9406] | 140 | else |
---|
| 141 | return NULL; |
---|
[7413] | 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * @brief takes out an InputLine, searching for a Command. |
---|
[7414] | 146 | * @see const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings) |
---|
[7413] | 147 | * @param inputLine: the Input to analyse. |
---|
[7414] | 148 | * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section. |
---|
[7413] | 149 | * @returns: The ShellCommand if found. |
---|
| 150 | */ |
---|
[7417] | 151 | const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList) |
---|
[7413] | 152 | { |
---|
[9406] | 153 | return ShellCommand::getCommandFromInput(SubString(inputLine, SubString::WhiteSpaces), paramBegin, boList); |
---|
[7414] | 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * @brief takes out an InputLine, searching for a Command. |
---|
| 158 | * @param strings: the Input to analyse. |
---|
| 159 | * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section. |
---|
| 160 | * @returns: The ShellCommand if found. |
---|
| 161 | */ |
---|
[7417] | 162 | const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList) |
---|
[7414] | 163 | { |
---|
[7413] | 164 | // no input, no Command. |
---|
| 165 | if (strings.size() == 0) |
---|
[7414] | 166 | { |
---|
| 167 | paramBegin = 0; |
---|
[7413] | 168 | return NULL; |
---|
[7414] | 169 | } |
---|
[7413] | 170 | |
---|
| 171 | // CHECK FOR ALIAS |
---|
| 172 | std::vector<ShellCommandAlias*>::const_iterator alias; |
---|
| 173 | for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ ) |
---|
[7374] | 174 | { |
---|
[7413] | 175 | if (strings[0] == (*alias)->getName()) |
---|
| 176 | { |
---|
| 177 | assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL); |
---|
[7418] | 178 | // Search for Objects. |
---|
| 179 | if (strings.size() == 1) |
---|
[7422] | 180 | { |
---|
| 181 | if (fillObjectList("", (*alias)->getCommand(), boList)) |
---|
| 182 | ; |
---|
| 183 | } |
---|
[7418] | 184 | else |
---|
| 185 | { |
---|
| 186 | if (!fillObjectList(strings[1], (*alias)->getCommand(), boList)) |
---|
| 187 | fillObjectList("", (*alias)->getCommand(), boList); |
---|
| 188 | } |
---|
[7771] | 189 | paramBegin = 1; |
---|
[7413] | 190 | return (*alias)->getCommand(); |
---|
| 191 | } |
---|
[5779] | 192 | } |
---|
[7413] | 193 | |
---|
[7417] | 194 | // CHECK FOR COMMAND_CLASS |
---|
[7413] | 195 | const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]); |
---|
| 196 | if (cmdClass != NULL) |
---|
| 197 | { |
---|
[8350] | 198 | const ShellCommand* retCmd = NULL; |
---|
[7417] | 199 | // Function/Command right after Class |
---|
[7413] | 200 | if (strings.size() >= 1) |
---|
[7414] | 201 | { |
---|
[7418] | 202 | // Search for Objects. |
---|
[9406] | 203 | retCmd = ShellCommand::getCommand((strings.size() > 1) ? strings[1] : "", cmdClass); |
---|
[7417] | 204 | if (retCmd != NULL) |
---|
| 205 | { |
---|
| 206 | paramBegin = 2; |
---|
[7418] | 207 | fillObjectList("", retCmd, boList); |
---|
[7417] | 208 | return retCmd; |
---|
| 209 | } |
---|
[7414] | 210 | } |
---|
[7417] | 211 | // Function/Command after Class and 'Object' |
---|
[7413] | 212 | if (retCmd == NULL && strings.size() >= 2) |
---|
[7414] | 213 | { |
---|
[9406] | 214 | retCmd = ShellCommand::getCommand((strings.size() > 2) ? strings[2] : "", cmdClass); |
---|
[7417] | 215 | if (retCmd != NULL) |
---|
| 216 | { |
---|
[7415] | 217 | paramBegin = 3; |
---|
[7418] | 218 | fillObjectList(strings[1], retCmd, boList); |
---|
[7417] | 219 | return retCmd; |
---|
| 220 | } |
---|
[7414] | 221 | } |
---|
| 222 | if (retCmd != NULL) // check for the paramBegin. |
---|
| 223 | return retCmd; |
---|
[7413] | 224 | } |
---|
[7417] | 225 | // Nothing usefull found at all. |
---|
[7414] | 226 | paramBegin = 0; |
---|
| 227 | return NULL; |
---|
[5113] | 228 | } |
---|
| 229 | |
---|
[7418] | 230 | /** |
---|
| 231 | * @brief fills the ObjectList boList with Objects that can be reffered to by cmd. |
---|
| 232 | * @param objectDescriptor: the ObjectName (beginning, full name or empty) to fill the List with |
---|
| 233 | * @param cmd: The Command to complete Objects for. |
---|
| 234 | * @param boList: The List of BaseObject's that will be filled with found entries. |
---|
| 235 | * @returns: true if more than one Entry was fond, else (false , or if boList is NULL). |
---|
| 236 | */ |
---|
| 237 | bool ShellCommand::fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList) |
---|
[7417] | 238 | { |
---|
[7418] | 239 | assert (cmd != NULL && cmd->shellClass != NULL); |
---|
| 240 | if(boList == NULL) |
---|
| 241 | return false; |
---|
[7413] | 242 | |
---|
[9715] | 243 | const ObjectListBase* const objectList = ObjectListBase::getObjectList(cmd->shellClass->getName()); |
---|
[7417] | 244 | if (objectList != NULL) |
---|
| 245 | { |
---|
[9715] | 246 | ObjectListBase::base_list list; |
---|
[9702] | 247 | objectList->getBaseObjectList(&list); |
---|
[9715] | 248 | ObjectListBase::base_iterator it; |
---|
[7417] | 249 | |
---|
| 250 | // No Description given (only for speedup) |
---|
| 251 | if (objectDescriptor.empty()) |
---|
| 252 | { |
---|
[9702] | 253 | for (it = list.begin(); it != list.end(); it++) |
---|
| 254 | boList->push_back(*it); |
---|
[7417] | 255 | } |
---|
| 256 | // some description |
---|
| 257 | else |
---|
| 258 | { |
---|
[9702] | 259 | for (it = list.begin(); it != list.end(); it++) |
---|
| 260 | if (!nocaseCmp(objectDescriptor, (*it)->getName(), objectDescriptor.size())) |
---|
| 261 | boList->push_back(*it); |
---|
[7417] | 262 | } |
---|
| 263 | } |
---|
[7418] | 264 | return !boList->empty(); |
---|
[7417] | 265 | } |
---|
| 266 | |
---|
[7408] | 267 | /** |
---|
| 268 | * @brief checks if a command has already been registered. |
---|
| 269 | * @param commandName the name of the Command |
---|
| 270 | * @param className the name of the Class the command should apply to. |
---|
| 271 | * @returns true, if the command is registered/false otherwise |
---|
| 272 | * |
---|
| 273 | * This is used internally, to see, if we have multiple command subscriptions. |
---|
| 274 | * This is checked in the registerCommand-function. |
---|
| 275 | */ |
---|
| 276 | bool ShellCommand::exists(const std::string& commandName, const std::string& className) |
---|
| 277 | { |
---|
| 278 | return (ShellCommand::getCommand(commandName, className) != NULL); |
---|
| 279 | } |
---|
[5140] | 280 | |
---|
[7408] | 281 | |
---|
[7374] | 282 | /** |
---|
[7394] | 283 | * @brief executes commands |
---|
[7374] | 284 | * @param executionString the string containing the following input |
---|
| 285 | * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]] |
---|
| 286 | * @return true on success, false otherwise. |
---|
| 287 | */ |
---|
| 288 | bool ShellCommand::execute(const std::string& executionString) |
---|
| 289 | { |
---|
| 290 | SubString inputSplits(executionString, SubString::WhiteSpacesWithComma); |
---|
[5198] | 291 | |
---|
[7374] | 292 | // if we do not have any input return |
---|
| 293 | if (inputSplits.empty()) |
---|
| 294 | return false; |
---|
[7340] | 295 | |
---|
[7419] | 296 | unsigned int paramBegin; |
---|
[7771] | 297 | const ShellCommand* sc = NULL; |
---|
[7419] | 298 | std::vector<BaseObject*> boList; |
---|
| 299 | sc = getCommandFromInput(inputSplits, paramBegin, &boList); |
---|
| 300 | if (sc != NULL) |
---|
| 301 | { |
---|
| 302 | for(std::vector<BaseObject*>::const_iterator bo = boList.begin(); bo != boList.end(); ++bo) |
---|
[7420] | 303 | { |
---|
[9406] | 304 | PRINT(0)("Command '%s' on '%s::%s'\n", sc->getCName(), (*bo)->getClassCName(), (*bo)->getCName()); |
---|
| 305 | (*sc->executor)((*bo), inputSplits.subSet(paramBegin)); |
---|
[7420] | 306 | } |
---|
| 307 | return true; |
---|
[7419] | 308 | } |
---|
[7420] | 309 | return false; |
---|
[5198] | 310 | } |
---|
[5148] | 311 | |
---|
[7411] | 312 | |
---|
[7374] | 313 | /** |
---|
[7401] | 314 | * @brief lets a command be described |
---|
[7374] | 315 | * @param description the description of the Given command |
---|
| 316 | */ |
---|
| 317 | ShellCommand* ShellCommand::describe(const std::string& description) |
---|
[7340] | 318 | { |
---|
[7374] | 319 | if (this == NULL) |
---|
| 320 | return NULL; |
---|
[7403] | 321 | this->description = description; |
---|
| 322 | return this; |
---|
[7340] | 323 | } |
---|
[5164] | 324 | |
---|
[7374] | 325 | /** |
---|
[7389] | 326 | * @brief adds an Alias to this Command |
---|
[7374] | 327 | * @param alias the name of the Alias to set |
---|
| 328 | * @returns itself |
---|
| 329 | */ |
---|
| 330 | ShellCommand* ShellCommand::setAlias(const std::string& alias) |
---|
[5196] | 331 | { |
---|
[7374] | 332 | if (this == NULL) |
---|
| 333 | return NULL; |
---|
[5196] | 334 | |
---|
[7374] | 335 | if (this->alias != NULL) |
---|
| 336 | { |
---|
[9406] | 337 | PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getCName(), this->shellClass->getCName()); |
---|
[7374] | 338 | } |
---|
| 339 | else |
---|
| 340 | { |
---|
| 341 | ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this); |
---|
| 342 | this->alias = aliasCMD; |
---|
| 343 | } |
---|
| 344 | return this; |
---|
[5196] | 345 | } |
---|
[5195] | 346 | |
---|
[7374] | 347 | /** |
---|
| 348 | * @brief set the default values of the executor |
---|
| 349 | * @param value0 the first default value |
---|
| 350 | * @param value1 the second default value |
---|
| 351 | * @param value2 the third default value |
---|
| 352 | * @param value3 the fourth default value |
---|
| 353 | * @param value4 the fifth default value |
---|
| 354 | */ |
---|
| 355 | ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1, |
---|
| 356 | const MultiType& value2, const MultiType& value3, |
---|
| 357 | const MultiType& value4) |
---|
| 358 | { |
---|
| 359 | if (this == NULL || this->executor == NULL) |
---|
| 360 | return NULL; |
---|
[5207] | 361 | |
---|
[7374] | 362 | this->executor->defaultValues(value0, value1, value2, value3, value4); |
---|
[5207] | 363 | |
---|
[7374] | 364 | return this; |
---|
[5148] | 365 | } |
---|
| 366 | |
---|
[7412] | 367 | ShellCommand* ShellCommand::completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin) |
---|
| 368 | { |
---|
| 369 | if (this == NULL || this->executor == NULL) |
---|
| 370 | return NULL; |
---|
| 371 | |
---|
| 372 | if (parameter >= this->executor->getParamCount()) |
---|
| 373 | { |
---|
| 374 | PRINTF(1)("Parameter %d not inside of valid ParameterCount %d of Command %s::%s\n", |
---|
[9406] | 375 | parameter, this->executor->getParamCount(), this->getCName(), this->shellClass->getCName()); |
---|
[7412] | 376 | } |
---|
| 377 | else |
---|
| 378 | { |
---|
[9112] | 379 | // if(this->completors[parameter] == NULL) |
---|
| 380 | // delete this->completors[parameter]; |
---|
[9406] | 381 | // this->completors[parameter] = completorPlugin.clone(); |
---|
[7412] | 382 | } |
---|
| 383 | return this; |
---|
| 384 | } |
---|
| 385 | |
---|
[7374] | 386 | /** |
---|
[7742] | 387 | * @brief prints a Help string from this Command |
---|
[7374] | 388 | */ |
---|
[7742] | 389 | void ShellCommand::help() const |
---|
[5148] | 390 | { |
---|
[9406] | 391 | PRINT(0)("%s ", this->getCName()); |
---|
[5148] | 392 | } |
---|
| 393 | |
---|
[7374] | 394 | /** |
---|
[7401] | 395 | * @brief converts a Parameter to a String |
---|
[7374] | 396 | * @param parameter the Parameter we have. |
---|
| 397 | * @returns the Name of the Parameter at Hand |
---|
| 398 | */ |
---|
[7401] | 399 | const std::string& ShellCommand::paramToString(long parameter) |
---|
[7374] | 400 | { |
---|
| 401 | return MultiType::MultiTypeToString((MT_Type)parameter); |
---|
| 402 | } |
---|
| 403 | |
---|
[7389] | 404 | |
---|
[7412] | 405 | |
---|
| 406 | /////////// |
---|
| 407 | // ALIAS // |
---|
| 408 | /////////// |
---|
| 409 | |
---|
[7397] | 410 | /** |
---|
| 411 | * @param aliasName the name of the Alias |
---|
| 412 | * @param command the Command, to associate this alias with |
---|
| 413 | */ |
---|
[7389] | 414 | ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command) |
---|
| 415 | { |
---|
| 416 | this->aliasName = aliasName; |
---|
| 417 | this->command = command; |
---|
| 418 | ShellCommandAlias::aliasList.push_back(this); |
---|
| 419 | }; |
---|
| 420 | |
---|
| 421 | ShellCommandAlias::~ShellCommandAlias() |
---|
| 422 | { |
---|
| 423 | std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this); |
---|
| 424 | if (delA != aliasList.end()) |
---|
| 425 | ShellCommandAlias::aliasList.push_back(this); |
---|
| 426 | |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList; |
---|
| 430 | /** |
---|
| 431 | * @brief collects the Aliases registered to the ShellCommands |
---|
| 432 | * @param stringList a List to paste the Aliases into. |
---|
| 433 | * @returns true on success, false otherwise |
---|
| 434 | */ |
---|
[7403] | 435 | |
---|
[7389] | 436 | bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList) |
---|
| 437 | { |
---|
| 438 | std::vector<ShellCommandAlias*>::iterator alias; |
---|
| 439 | for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++) |
---|
| 440 | stringList.push_back((*alias)->getName()); |
---|
| 441 | return true; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | |
---|
[5148] | 445 | } |
---|