Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: exist get functions

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