[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 | |
---|
[5779] | 31 | std::list<ShellCommandClass*>* ShellCommandClass::commandClassList = NULL; |
---|
| 32 | std::list<ShellCommandAlias*>* ShellCommandClass::aliasList = NULL; |
---|
[5639] | 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; |
---|
| 45 | |
---|
[5779] | 46 | ShellCommandClass::commandClassList->push_back(this); |
---|
[5170] | 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * destructs the shellCommandClass again |
---|
| 51 | */ |
---|
| 52 | ShellCommandClass::~ShellCommandClass() |
---|
| 53 | { |
---|
[5779] | 54 | while(this->commandList.size() > 0) |
---|
[5170] | 55 | { |
---|
[5779] | 56 | delete this->commandList.front(); |
---|
| 57 | this->commandList.pop_front(); |
---|
[5170] | 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
[5197] | 61 | /** |
---|
| 62 | * collects the Commands registered to some class. |
---|
| 63 | * @param className the name of the Class to collect the Commands from. |
---|
| 64 | * @param stringList a List to paste the Commands into. |
---|
| 65 | * @returns true on success, false otherwise |
---|
| 66 | */ |
---|
[5779] | 67 | bool ShellCommandClass::getCommandListOfClass(const char* className, std::list<const char*>* stringList) |
---|
[5189] | 68 | { |
---|
[5192] | 69 | if (stringList == NULL || className == NULL) |
---|
[5190] | 70 | return false; |
---|
| 71 | |
---|
[5779] | 72 | list<ShellCommandClass*>::iterator elem; |
---|
| 73 | for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++) |
---|
[5189] | 74 | { |
---|
[5779] | 75 | if (!strcmp ((*elem)->getName(), className)) |
---|
[5189] | 76 | { |
---|
[5779] | 77 | list<ShellCommand*>::iterator command; |
---|
| 78 | for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++) |
---|
| 79 | stringList->push_back((*command)->getName()); |
---|
[5189] | 80 | } |
---|
| 81 | } |
---|
[5190] | 82 | return true; |
---|
[5189] | 83 | } |
---|
| 84 | |
---|
[5197] | 85 | /** |
---|
| 86 | * collects the Aliases registered to the ShellCommands |
---|
| 87 | * @param stringList a List to paste the Aliases into. |
---|
| 88 | * @returns true on success, false otherwise |
---|
| 89 | */ |
---|
[5779] | 90 | bool ShellCommandClass::getCommandListOfAlias(std::list<const char*>* stringList) |
---|
[5195] | 91 | { |
---|
[5196] | 92 | if (stringList == NULL || ShellCommandClass::aliasList == NULL) |
---|
[5195] | 93 | return false; |
---|
| 94 | |
---|
[5779] | 95 | list<ShellCommandAlias*>::iterator alias; |
---|
| 96 | for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++) |
---|
| 97 | stringList->push_back((*alias)->getName()); |
---|
| 98 | return true; |
---|
[5195] | 99 | } |
---|
| 100 | |
---|
[5171] | 101 | /** |
---|
| 102 | * unregisters all Commands that exist |
---|
| 103 | */ |
---|
| 104 | void ShellCommandClass::unregisterAllCommands() |
---|
| 105 | { |
---|
[5779] | 106 | /// FIXME |
---|
| 107 | |
---|
| 108 | /* if (ShellCommandClass::commandClassList != NULL) |
---|
[5171] | 109 | { |
---|
[5636] | 110 | // unregister all commands |
---|
| 111 | tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator(); |
---|
| 112 | ShellCommandClass* elem = iterator->firstElement(); |
---|
| 113 | while(elem != NULL) |
---|
| 114 | { |
---|
| 115 | delete elem; |
---|
[5171] | 116 | |
---|
[5636] | 117 | elem = iterator->nextElement(); |
---|
| 118 | } |
---|
| 119 | delete iterator; |
---|
| 120 | |
---|
| 121 | delete ShellCommandClass::commandClassList; |
---|
| 122 | ShellCommandClass::commandClassList = NULL; |
---|
[5171] | 123 | } |
---|
| 124 | |
---|
[5195] | 125 | // unregister all aliases (there should be nothing to do here :)) |
---|
[5196] | 126 | if (ShellCommandClass::aliasList != NULL) |
---|
[5195] | 127 | { |
---|
[5197] | 128 | tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator(); |
---|
| 129 | ShellCommandAlias* elemAL = itAL->firstElement(); |
---|
| 130 | while(elemAL != NULL) |
---|
| 131 | { |
---|
| 132 | delete elemAL; |
---|
| 133 | elemAL = itAL->nextElement(); |
---|
| 134 | } |
---|
| 135 | delete itAL; |
---|
| 136 | delete ShellCommandClass::aliasList; |
---|
| 137 | ShellCommandClass::aliasList = NULL; |
---|
[5779] | 138 | }*/ |
---|
[5171] | 139 | } |
---|
| 140 | |
---|
[5197] | 141 | /** |
---|
| 142 | * checks if a Class is already registered to the Commands' class-stack |
---|
| 143 | * @param className the Name of the Class to check for |
---|
| 144 | * @returns the CommandClass if found, NULL otherwise |
---|
| 145 | */ |
---|
[5170] | 146 | const ShellCommandClass* ShellCommandClass::isRegistered(const char* className) |
---|
| 147 | { |
---|
| 148 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 149 | initCommandClassList(); |
---|
| 150 | |
---|
[5779] | 151 | list<ShellCommandClass*>::const_iterator classIT; |
---|
| 152 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5170] | 153 | { |
---|
[5779] | 154 | if (!strcmp(className, (*classIT)->className)) |
---|
[5170] | 155 | { |
---|
[5779] | 156 | if ((*classIT)->classID == CL_NULL) |
---|
| 157 | (*classIT)->classID = ClassList::StringToID(className); |
---|
[5171] | 158 | |
---|
[5779] | 159 | return (*classIT); |
---|
[5170] | 160 | } |
---|
| 161 | } |
---|
| 162 | return NULL; |
---|
| 163 | } |
---|
| 164 | |
---|
[5172] | 165 | /** |
---|
| 166 | * searches for a CommandClass |
---|
| 167 | * @param className the name of the CommandClass |
---|
| 168 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 169 | */ |
---|
[5170] | 170 | ShellCommandClass* ShellCommandClass::getCommandClass(const char* className) |
---|
| 171 | { |
---|
| 172 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 173 | initCommandClassList(); |
---|
| 174 | |
---|
[5779] | 175 | list<ShellCommandClass*>::iterator classIT; |
---|
| 176 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5170] | 177 | { |
---|
[5779] | 178 | if (!strcmp(className, (*classIT)->className)) |
---|
[5170] | 179 | { |
---|
[5779] | 180 | return (*classIT); |
---|
[5170] | 181 | } |
---|
| 182 | } |
---|
| 183 | return new ShellCommandClass(className); |
---|
| 184 | } |
---|
| 185 | |
---|
[5172] | 186 | /** |
---|
| 187 | * initializes the CommandList (if it is NULL) |
---|
| 188 | */ |
---|
[5170] | 189 | void ShellCommandClass::initCommandClassList() |
---|
| 190 | { |
---|
| 191 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 192 | { |
---|
[5779] | 193 | ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>; |
---|
[5641] | 194 | ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug)); |
---|
[5170] | 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
[5644] | 198 | /** |
---|
| 199 | * displays help about ShellCommandClass |
---|
| 200 | * @param className: the Class of Commands to show help about |
---|
| 201 | */ |
---|
[5204] | 202 | void ShellCommandClass::help(const char* className) |
---|
| 203 | { |
---|
| 204 | if (className == NULL) |
---|
| 205 | return; |
---|
| 206 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
| 207 | { |
---|
[5779] | 208 | list<ShellCommandClass*>::iterator classIT; |
---|
| 209 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5204] | 210 | { |
---|
[5779] | 211 | if ((*classIT)->className && !strcasecmp(className, (*classIT)->className)) |
---|
[5204] | 212 | { |
---|
[5779] | 213 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className, (*classIT)->commandList.size()); |
---|
| 214 | list<ShellCommand*>::const_iterator cmdIT; |
---|
| 215 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
[5204] | 216 | { |
---|
[5779] | 217 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
[5642] | 218 | /// FIXME |
---|
| 219 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 220 | PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
[5779] | 221 | if ((*cmdIT)->description != NULL) |
---|
| 222 | PRINT(0)("- %s", (*cmdIT)->description); |
---|
[5204] | 223 | PRINT(0)("\n"); |
---|
| 224 | } |
---|
| 225 | return; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | PRINTF(3)("Class %s not found in Command's classes\n", className); |
---|
| 229 | } |
---|
| 230 | else |
---|
| 231 | { |
---|
| 232 | PRINTF(1)("List of commandClasses does not exist"); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|