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