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