Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7388 was 7388, checked in by bensch, 19 years ago

orxonox/trunk: fixed a notNULL definition

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