[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 | |
---|
[5639] | 18 | #include "shell_command_class.h" |
---|
| 19 | |
---|
[5129] | 20 | #include "shell_command.h" |
---|
[1853] | 21 | |
---|
[5072] | 22 | #include "list.h" |
---|
[5129] | 23 | #include "debug.h" |
---|
[5113] | 24 | #include "class_list.h" |
---|
| 25 | |
---|
[5075] | 26 | #include <stdio.h> |
---|
[5174] | 27 | #include <string.h> |
---|
[5075] | 28 | |
---|
[1856] | 29 | using namespace std; |
---|
[1853] | 30 | |
---|
[5639] | 31 | tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL; |
---|
| 32 | tList<ShellCommandAlias>* ShellCommandClass::aliasList = NULL; |
---|
| 33 | |
---|
[5166] | 34 | /** |
---|
[5170] | 35 | * creates a new ShellCommandClass |
---|
| 36 | * @param className the Name of the command-class to create |
---|
| 37 | */ |
---|
| 38 | ShellCommandClass::ShellCommandClass(const char* className) |
---|
| 39 | { |
---|
[5188] | 40 | this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass"); |
---|
| 41 | this->setName(className); |
---|
| 42 | |
---|
[5170] | 43 | this->className = className; |
---|
| 44 | this->classID = CL_NULL; |
---|
[5636] | 45 | this->commandList = new tList<ShellCommand>; |
---|
[5170] | 46 | |
---|
| 47 | ShellCommandClass::commandClassList->add(this); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * destructs the shellCommandClass again |
---|
| 52 | */ |
---|
| 53 | ShellCommandClass::~ShellCommandClass() |
---|
| 54 | { |
---|
[5636] | 55 | tIterator<ShellCommand>* iterator = this->commandList->getIterator(); |
---|
| 56 | ShellCommand* elem = iterator->firstElement(); |
---|
[5170] | 57 | while(elem != NULL) |
---|
| 58 | { |
---|
| 59 | delete elem; |
---|
| 60 | elem = iterator->nextElement(); |
---|
| 61 | } |
---|
| 62 | delete iterator; |
---|
| 63 | delete this->commandList; |
---|
| 64 | } |
---|
| 65 | |
---|
[5197] | 66 | /** |
---|
| 67 | * collects the Commands registered to some class. |
---|
| 68 | * @param className the name of the Class to collect the Commands from. |
---|
| 69 | * @param stringList a List to paste the Commands into. |
---|
| 70 | * @returns true on success, false otherwise |
---|
| 71 | */ |
---|
[5190] | 72 | bool ShellCommandClass::getCommandListOfClass(const char* className, tList<const char>* stringList) |
---|
[5189] | 73 | { |
---|
[5192] | 74 | if (stringList == NULL || className == NULL) |
---|
[5190] | 75 | return false; |
---|
| 76 | |
---|
[5189] | 77 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 78 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 79 | while(elem != NULL) |
---|
| 80 | { |
---|
| 81 | if (!strcmp (elem->getName(), className)) |
---|
| 82 | { |
---|
[5636] | 83 | tIterator<ShellCommand>* itFkt = elem->commandList->getIterator(); |
---|
| 84 | ShellCommand* command = itFkt->firstElement(); |
---|
[5189] | 85 | while (command != NULL) |
---|
| 86 | { |
---|
[5190] | 87 | stringList->add(command->getName()); |
---|
[5189] | 88 | command = itFkt->nextElement(); |
---|
| 89 | } |
---|
| 90 | delete itFkt; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | elem = iterator->nextElement(); |
---|
| 94 | } |
---|
| 95 | delete iterator; |
---|
[5190] | 96 | return true; |
---|
[5189] | 97 | } |
---|
| 98 | |
---|
[5197] | 99 | /** |
---|
| 100 | * collects the Aliases registered to the ShellCommands |
---|
| 101 | * @param stringList a List to paste the Aliases into. |
---|
| 102 | * @returns true on success, false otherwise |
---|
| 103 | */ |
---|
[5195] | 104 | bool ShellCommandClass::getCommandListOfAlias(tList<const char>* stringList) |
---|
| 105 | { |
---|
[5196] | 106 | if (stringList == NULL || ShellCommandClass::aliasList == NULL) |
---|
[5195] | 107 | return false; |
---|
| 108 | |
---|
| 109 | tIterator<ShellCommandAlias>* iterator = ShellCommandClass::aliasList->getIterator(); |
---|
[5196] | 110 | ShellCommandAlias* elem = iterator->firstElement(); |
---|
| 111 | while(elem != NULL) |
---|
| 112 | { |
---|
| 113 | stringList->add(elem->getName()); |
---|
| 114 | elem = iterator->nextElement(); |
---|
| 115 | } |
---|
| 116 | delete iterator; |
---|
| 117 | return true; |
---|
[5195] | 118 | } |
---|
| 119 | |
---|
[5171] | 120 | /** |
---|
| 121 | * unregisters all Commands that exist |
---|
| 122 | */ |
---|
| 123 | void ShellCommandClass::unregisterAllCommands() |
---|
| 124 | { |
---|
[5636] | 125 | if (ShellCommandClass::commandClassList != NULL) |
---|
[5171] | 126 | { |
---|
[5636] | 127 | // unregister all commands |
---|
| 128 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 129 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 130 | while(elem != NULL) |
---|
| 131 | { |
---|
| 132 | delete elem; |
---|
[5171] | 133 | |
---|
[5636] | 134 | elem = iterator->nextElement(); |
---|
| 135 | } |
---|
| 136 | delete iterator; |
---|
| 137 | |
---|
| 138 | delete ShellCommandClass::commandClassList; |
---|
| 139 | ShellCommandClass::commandClassList = NULL; |
---|
[5171] | 140 | } |
---|
| 141 | |
---|
[5195] | 142 | // unregister all aliases (there should be nothing to do here :)) |
---|
[5196] | 143 | if (ShellCommandClass::aliasList != NULL) |
---|
[5195] | 144 | { |
---|
[5197] | 145 | tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator(); |
---|
| 146 | ShellCommandAlias* elemAL = itAL->firstElement(); |
---|
| 147 | while(elemAL != NULL) |
---|
| 148 | { |
---|
| 149 | delete elemAL; |
---|
| 150 | elemAL = itAL->nextElement(); |
---|
| 151 | } |
---|
| 152 | delete itAL; |
---|
| 153 | delete ShellCommandClass::aliasList; |
---|
| 154 | ShellCommandClass::aliasList = NULL; |
---|
[5195] | 155 | } |
---|
[5171] | 156 | } |
---|
| 157 | |
---|
[5197] | 158 | /** |
---|
| 159 | * checks if a Class is already registered to the Commands' class-stack |
---|
| 160 | * @param className the Name of the Class to check for |
---|
| 161 | * @returns the CommandClass if found, NULL otherwise |
---|
| 162 | */ |
---|
[5170] | 163 | const ShellCommandClass* ShellCommandClass::isRegistered(const char* className) |
---|
| 164 | { |
---|
| 165 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 166 | initCommandClassList(); |
---|
| 167 | |
---|
| 168 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 169 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 170 | while(elem != NULL) |
---|
| 171 | { |
---|
| 172 | if (!strcmp(className, elem->className)) |
---|
| 173 | { |
---|
[5171] | 174 | if (elem->classID == CL_NULL) |
---|
| 175 | elem->classID = ClassList::StringToID(className); |
---|
| 176 | |
---|
[5170] | 177 | delete iterator; |
---|
| 178 | return elem; |
---|
| 179 | } |
---|
| 180 | elem = iterator->nextElement(); |
---|
| 181 | } |
---|
| 182 | delete iterator; |
---|
| 183 | return NULL; |
---|
| 184 | } |
---|
| 185 | |
---|
[5172] | 186 | /** |
---|
| 187 | * searches for a CommandClass |
---|
| 188 | * @param className the name of the CommandClass |
---|
| 189 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 190 | */ |
---|
[5170] | 191 | ShellCommandClass* ShellCommandClass::getCommandClass(const char* className) |
---|
| 192 | { |
---|
| 193 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 194 | initCommandClassList(); |
---|
| 195 | |
---|
| 196 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 197 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 198 | while(elem != NULL) |
---|
| 199 | { |
---|
| 200 | if (!strcmp(className, elem->className)) |
---|
| 201 | { |
---|
| 202 | delete iterator; |
---|
| 203 | return elem; |
---|
| 204 | } |
---|
| 205 | elem = iterator->nextElement(); |
---|
| 206 | } |
---|
| 207 | delete iterator; |
---|
| 208 | return new ShellCommandClass(className); |
---|
| 209 | } |
---|
| 210 | |
---|
[5172] | 211 | /** |
---|
| 212 | * initializes the CommandList (if it is NULL) |
---|
| 213 | */ |
---|
[5170] | 214 | void ShellCommandClass::initCommandClassList() |
---|
| 215 | { |
---|
| 216 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 217 | { |
---|
| 218 | ShellCommandClass::commandClassList = new tList<ShellCommandClass>; |
---|
[5641] | 219 | ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug)); |
---|
[5170] | 220 | } |
---|
| 221 | } |
---|
| 222 | |
---|
[5644] | 223 | /** |
---|
| 224 | * displays help about ShellCommandClass |
---|
| 225 | * @param className: the Class of Commands to show help about |
---|
| 226 | */ |
---|
[5204] | 227 | void ShellCommandClass::help(const char* className) |
---|
| 228 | { |
---|
| 229 | if (className == NULL) |
---|
| 230 | return; |
---|
| 231 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
| 232 | { |
---|
| 233 | tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator(); |
---|
| 234 | ShellCommandClass* elemCL = itCL->firstElement(); |
---|
| 235 | while(elemCL != NULL) |
---|
| 236 | { |
---|
| 237 | if (elemCL->className && !strcasecmp(className, elemCL->className)) |
---|
| 238 | { |
---|
| 239 | PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); |
---|
[5636] | 240 | tIterator<ShellCommand>* iterator = elemCL->commandList->getIterator(); |
---|
| 241 | const ShellCommand* elem = iterator->firstElement(); |
---|
[5204] | 242 | while(elem != NULL) |
---|
| 243 | { |
---|
[5642] | 244 | PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->executor->getParamCount()); |
---|
| 245 | /// FIXME |
---|
| 246 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 247 | PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
[5204] | 248 | if (elem->description != NULL) |
---|
| 249 | PRINT(0)("- %s", elem->description); |
---|
| 250 | PRINT(0)("\n"); |
---|
| 251 | elem = iterator->nextElement(); |
---|
| 252 | } |
---|
| 253 | delete iterator; |
---|
| 254 | |
---|
| 255 | delete itCL; |
---|
| 256 | return; |
---|
| 257 | } |
---|
| 258 | elemCL = itCL->nextElement(); |
---|
| 259 | } |
---|
| 260 | delete itCL; |
---|
| 261 | PRINTF(3)("Class %s not found in Command's classes\n", className); |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | PRINTF(1)("List of commandClasses does not exist"); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|