Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_command.cc @ 7394

Last change on this file since 7394 was 7394, checked in by bensch, 18 years ago

orxonox/trunk: list to vector

File size: 11.8 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
17
18#include "shell_command.h"
19#include "shell_command_class.h"
20
21#include "compiler.h"
22#include "debug.h"
23#include "class_list.h"
24
25#include "key_names.h"
26
27namespace OrxShell
28{
29  SHELL_COMMAND_STATIC(debug, ShellCommand, ShellCommand::debug);
30
31
32  /**
33   * @brief constructs and registers a new Command
34   * @param commandName the name of the Command
35   * @param className the name of the class to apply this command to
36   * @param paramCount the count of parameters this command takes
37   */
38  ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, const Executor& executor)
39  {
40    this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
41    PRINTF(5)("create shellcommand %s %s\n", commandName, className);
42    this->setName(commandName);
43    this->executor = executor.clone();
44    this->executor->setName(commandName);
45    this->alias = NULL;
46
47    //  this->classID = classID;
48    this->shellClass = ShellCommandClass::getCommandClass(className);
49    if (this->shellClass != NULL)
50      this->shellClass->commandList.push_back(this);
51  }
52
53  /**
54   * @brief deconstructs a ShellCommand
55   */
56  ShellCommand::~ShellCommand()
57  {
58    if (this->alias != NULL)
59      delete this->alias;
60    delete this->executor;
61  }
62
63  /**
64   * registers a new ShellCommand
65   */
66  ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, const Executor& executor)
67  {
68    if (ShellCommand::isRegistered(commandName, className))
69      return NULL;
70    else
71      return new ShellCommand(commandName, className, executor);
72
73  }
74
75  /**
76   * unregister an existing commandName
77   * @param className the name of the Class the command belongs to.
78   * @param commandName the name of the command itself
79   */
80  void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className)
81  {
82    /// FIXME
83    /*  if (ShellCommandClass::commandClassList == NULL)
84        ShellCommandClass::initCommandClassList();
85
86     const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
87
88     if (checkClass != NULL)
89      {
90        std::list<ShellCommand*>::iterator elem;
91        for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++)
92        {
93          if (!strcmp(commandName, (*elem)->getName()))
94          {
95            delete (*elem);
96            checkClass->commandList.remove(*elem);
97            break;
98          }
99        }
100
101        if (checkClass->commandList->size() == 0)
102        {
103          ShellCommandClass::commandClassList->remove(checkClass);
104          delete checkClass;
105        }
106      }*/
107  }
108
109  /**
110   * @brief checks if a command has already been registered.
111   * @param commandName the name of the Command
112   * @param className the name of the Class the command should apply to.
113   * @returns true, if the command is registered/false otherwise
114   *
115   * This is used internally, to see, if we have multiple command subscriptions.
116   * This is checked in the registerCommand-function.
117   */
118  bool ShellCommand::isRegistered(const std::string& commandName, const std::string& className)
119  {
120    const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
121    if (checkClass != NULL)
122    {
123      std::vector<ShellCommand*>::const_iterator elem;
124      for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++)
125      {
126        if (commandName == (*elem)->getName())
127        {
128          PRINTF(2)("Command '%s::%s' already registered\n", className.c_str(), commandName.c_str());
129          return true;
130        }
131      }
132      return false;
133    }
134    else
135      return false;
136  }
137
138
139  /**
140   * @brief executes commands
141   * @param executionString the string containing the following input
142   * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
143   * @return true on success, false otherwise.
144   */
145  bool ShellCommand::execute(const std::string& executionString)
146  {
147    long classID = CL_NULL;                      //< the classID retrieved from the Class.
148    ShellCommandClass* commandClass = NULL;      //< the command class this command applies to.
149    const std::list<BaseObject*>* objectList = NULL;   //< the list of Objects stored in classID
150    BaseObject* objectPointer = NULL;            //< a pointer to th Object to Execute the command on
151    bool emptyComplete = false;                  //< if the completion input is empty string. e.g ""
152    //  long completeType = SHELLC_NONE;           //< the Type we'd like to complete.
153    SubString inputSplits(executionString, SubString::WhiteSpacesWithComma);
154
155
156    // if we do not have any input return
157    if (inputSplits.empty())
158      return false;
159
160    // if we only have one input (!MUST BE AN ALIAS)
161    if (inputSplits.size() >= 1)
162    {
163      // CHECK FOR ALIAS
164      std::vector<ShellCommandAlias*>::iterator alias;
165      for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++ )
166      {
167        if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
168            (*alias)->getCommand()->shellClass != NULL )
169        {
170          objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
171          if (objectList != NULL)
172          {
173            (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join());
174            return true;
175          }
176          /// TODO CHECK FOR STATIC functions.
177        }
178      }
179
180      // looking for a Matching Class
181      std::vector<ShellCommandClass*>::iterator commandClassIT;
182      for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++)
183      {
184        if ((*commandClassIT)->getName() && inputSplits[0] == (*commandClassIT)->getName())
185        {
186          //elemCL->getName();
187          classID = ClassList::StringToID((*commandClassIT)->getName());
188          commandClass = (*commandClassIT);
189          objectList = ClassList::getList((ClassID)classID);
190          break;
191        }
192      }
193
194      // Second Agument. (either Object, or Function)
195      if (commandClass != NULL && inputSplits.size() >= 2)
196      {
197        int fktPos = 1; // The position of the Function (either at pos 1, or 2)
198        // If we have an ObjectList.
199        if (objectList != NULL)
200        {
201          // Checking for a Match in the Objects of classID (else take the first)
202          std::list<BaseObject*>::const_iterator object;
203          for (object = objectList->begin(); object != objectList->end(); object++)
204          {
205            if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName())
206            {
207              objectPointer = (*object);
208              fktPos = 2;
209              break;
210            }
211          }
212
213          // if we did not find an Object with matching name, take the first.
214          if (objectPointer == NULL)
215            objectPointer = objectList->front();
216        }
217
218        // match a function.
219        if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3)))
220        {
221          std::vector<ShellCommand*>::iterator cmdIT;
222          for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
223          {
224            if (inputSplits[fktPos] == (*cmdIT)->getName())
225            {
226              if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
227                return false;
228              else
229              {
230                (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK
231                return true;
232              }
233            }
234          }
235        }
236      }
237    }
238    return false;
239  }
240
241  /**
242   * lets a command be described
243   * @param description the description of the Given command
244   */
245  ShellCommand* ShellCommand::describe(const std::string& description)
246  {
247    if (this == NULL)
248      return NULL;
249    else
250    {
251      this->description = description;
252      return this;
253    }
254  }
255
256  /**
257   * @brief adds an Alias to this Command
258   * @param alias the name of the Alias to set
259   * @returns itself
260   */
261  ShellCommand* ShellCommand::setAlias(const std::string& alias)
262  {
263    if (this == NULL)
264      return NULL;
265
266    if (this->alias != NULL)
267    {
268      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
269    }
270    else
271    {
272      ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
273      ShellCommandAlias::aliasList.push_back(aliasCMD);
274      this->alias = aliasCMD;
275    }
276    return this;
277  }
278
279  /**
280   * @brief set the default values of the executor
281   * @param value0 the first default value
282   * @param value1 the second default value
283   * @param value2 the third default value
284   * @param value3 the fourth default value
285   * @param value4 the fifth default value
286   */
287  ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
288      const MultiType& value2, const MultiType& value3,
289      const MultiType& value4)
290  {
291    if (this == NULL || this->executor == NULL)
292      return NULL;
293
294    this->executor->defaultValues(value0, value1, value2, value3, value4);
295
296    return this;
297  }
298
299  /**
300   * prints out nice information about the Shells Commands
301   */
302  void ShellCommand::debug()
303  {
304    std::vector<ShellCommandClass*>::iterator classIT;
305    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
306    {
307      PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
308
309      std::vector<ShellCommand*>::iterator cmdIT;
310      for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
311      {
312        PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
313        /// FIXME
314        /*      for (unsigned int i = 0; i< elem->paramCount; i++)
315         printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
316        if (!(*cmdIT)->description.empty())
317          printf("- %s", (*cmdIT)->description.c_str());
318        printf("\n");
319
320      }
321    }
322  }
323
324  /**
325   * converts a Parameter to a String
326   * @param parameter the Parameter we have.
327   * @returns the Name of the Parameter at Hand
328   */
329  const char* ShellCommand::paramToString(long parameter)
330  {
331    return MultiType::MultiTypeToString((MT_Type)parameter);
332  }
333
334
335  ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command)
336  {
337    this->aliasName = aliasName;
338    this->command = command;
339    ShellCommandAlias::aliasList.push_back(this);
340  };
341
342  ShellCommandAlias::~ShellCommandAlias()
343  {
344    std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this);
345    if (delA != aliasList.end())
346      ShellCommandAlias::aliasList.push_back(this);
347
348  }
349
350  std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList;
351  /**
352  * @brief collects the Aliases registered to the ShellCommands
353  * @param stringList a List to paste the Aliases into.
354  * @returns true on success, false otherwise
355   */
356  bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList)
357  {
358    std::vector<ShellCommandAlias*>::iterator alias;
359    for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++)
360      stringList.push_back((*alias)->getName());
361    return true;
362  }
363
364
365}
Note: See TracBrowser for help on using the repository browser.