Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: small fixes

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