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