Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/shell/shell_command.cc @ 9728

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

cleanup

File size: 13.7 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
24#include "key_names.h"
25
26namespace OrxShell
27{
28  ObjectListDefinition(ShellCommand);
29  SHELL_COMMAND(debug, ShellCommandClass, help);
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, Executor<const SubString>* executor)
39  {
40    this->registerObject(this, ShellCommand::_objectList);
41    PRINTF(4)("create shellcommand '%s' for class '%s'\n", commandName.c_str(), className.c_str());
42    this->setName(commandName);
43
44    // copy the executor:
45    this->executor = executor;
46
47    for (unsigned int i = 0; i < this->executor->getParamCount(); i++)
48      this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i)));
49    this->alias = NULL;
50
51    //  this->classID = classID;
52    this->shellClass = ShellCommandClass::acquireCommandClass(className);
53    assert (this->shellClass != NULL);
54    this->shellClass->registerCommand(this);
55  }
56
57  /**
58   * @brief deconstructs a ShellCommand
59   */
60  ShellCommand::~ShellCommand()
61  {
62    this->shellClass->unregisterCommand(this);
63    if (this->alias != NULL)
64      delete this->alias;
65    while (!this->completors.empty())
66    {
67      delete this->completors.back();
68      this->completors.pop_back();
69    }
70    delete this->executor;
71  }
72
73  /**
74   * @brief registers a new ShellCommand
75   */
76  ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, Executor<const SubString>* executor)
77  {
78    if (ShellCommand::exists(commandName, className))
79    {
80      delete executor;
81      return NULL;
82    }
83    else
84      return new ShellCommand(commandName, className, executor);
85
86  }
87
88  /**
89   * @brief unregister an existing commandName
90   * @param className the name of the Class the command belongs to.
91   * @param commandName the name of the command itself
92   */
93  void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className)
94  {
95    ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className);
96    if (cmdClass != NULL)
97    {
98      CmdList::iterator cmd;
99      for (cmd = cmdClass->_commandList.begin(); cmd != cmdClass->_commandList.end(); cmd++)
100        if (commandName == (*cmd)->getName())
101        {
102          delete (*cmd);
103          break;
104        }
105    }
106  }
107
108  /**
109   * @brief gets a command if it has already been registered.
110   * @param commandName the name of the Command
111   * @param cmdClass the CommandClass of the Class the command is in.
112   * @returns The Registered Command, or NULL if it does not exist.
113   */
114  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const ShellCommandClass* cmdClass)
115  {
116    assert(cmdClass != NULL);
117
118    CmdList::const_iterator elem;
119    for (unsigned int i = 0; i < cmdClass->_commandList.size(); i++)
120    {
121      if (commandName == cmdClass->_commandList[i]->getName())
122        return (cmdClass->_commandList[i]);
123    }
124    return NULL;
125  }
126
127
128  /**
129   * @brief gets a command if it has already been registered.
130   * @param commandName the name of the Command
131   * @param className the name of the Class the command is in.
132   * @returns The Registered Command, or NULL if it does not exist.
133   */
134  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className)
135  {
136    const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className);
137    if (likely(checkClass != NULL))
138      return ShellCommand::getCommand(commandName, checkClass);
139    else
140      return NULL;
141  }
142
143  /**
144   * @brief takes out an InputLine, searching for a Command.
145   * @see const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings)
146   * @param inputLine: the Input to analyse.
147   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
148   * @returns: The ShellCommand if found.
149   */
150  const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
151  {
152    return ShellCommand::getCommandFromInput(SubString(inputLine, SubString::WhiteSpaces), paramBegin, boList);
153  }
154
155  /**
156   * @brief takes out an InputLine, searching for a Command.
157   * @param strings: the Input to analyse.
158   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
159   * @returns: The ShellCommand if found.
160   */
161  const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
162  {
163    // no input, no Command.
164    if (strings.size() == 0)
165    {
166      paramBegin = 0;
167      return NULL;
168    }
169
170    // CHECK FOR ALIAS
171    std::vector<ShellCommandAlias*>::const_iterator alias;
172    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
173    {
174      if (strings[0] == (*alias)->getName())
175      {
176        assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL);
177        // Search for Objects.
178        if (strings.size() == 1)
179        {
180          if (fillObjectList("", (*alias)->getCommand(), boList))
181            ;
182        }
183        else
184        {
185          if (!fillObjectList(strings[1], (*alias)->getCommand(), boList))
186            fillObjectList("", (*alias)->getCommand(), boList);
187        }
188        paramBegin = 1;
189        return (*alias)->getCommand();
190      }
191    }
192
193    // CHECK FOR COMMAND_CLASS
194    const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]);
195    if (cmdClass != NULL)
196    {
197      const ShellCommand* retCmd = NULL;
198      // Function/Command right after Class
199      if (strings.size() >= 1)
200      {
201        // Search for Objects.
202        retCmd = ShellCommand::getCommand((strings.size() > 1) ? strings[1] : "", cmdClass);
203        if (retCmd != NULL)
204        {
205          paramBegin = 2;
206          fillObjectList("", retCmd, boList);
207          return retCmd;
208        }
209      }
210      // Function/Command after Class and 'Object'
211      if (retCmd == NULL && strings.size() >= 2)
212      {
213        retCmd = ShellCommand::getCommand((strings.size() > 2) ? strings[2] : "", cmdClass);
214        if (retCmd != NULL)
215        {
216          paramBegin = 3;
217          fillObjectList(strings[1], retCmd, boList);
218          return retCmd;
219        }
220      }
221      if (retCmd != NULL) // check for the paramBegin.
222        return retCmd;
223    }
224    // Nothing usefull found at all.
225    paramBegin = 0;
226    return NULL;
227  }
228
229  /**
230   * @brief fills the ObjectList boList with Objects that can be reffered to by cmd.
231   * @param objectDescriptor: the ObjectName (beginning, full name or empty) to fill the List with
232   * @param cmd: The Command to complete Objects for.
233   * @param boList: The List of BaseObject's that will be filled with found entries.
234   * @returns: true if more than one Entry was fond, else (false , or if boList is NULL).
235   */
236  bool ShellCommand::fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList)
237  {
238    assert (cmd != NULL && cmd->shellClass != NULL);
239    if(boList == NULL)
240      return false;
241
242    const ObjectListBase* const objectList = ObjectListBase::getObjectList(cmd->shellClass->getName());
243    if (objectList != NULL)
244    {
245      ObjectListBase::base_list list;
246      objectList->getBaseObjectList(&list);
247      ObjectListBase::base_iterator it;
248
249      // No Description given (only for speedup)
250      if (objectDescriptor.empty())
251      {
252        for (it = list.begin(); it != list.end(); it++)
253          boList->push_back(*it);
254      }
255      // some description
256      else
257      {
258        for (it = list.begin(); it != list.end(); it++)
259          if (!nocaseCmp(objectDescriptor, (*it)->getName(), objectDescriptor.size()))
260            boList->push_back(*it);
261      }
262    }
263    return !boList->empty();
264  }
265
266  /**
267   * @brief checks if a command has already been registered.
268   * @param commandName the name of the Command
269   * @param className the name of the Class the command should apply to.
270   * @returns true, if the command is registered/false otherwise
271   *
272   * This is used internally, to see, if we have multiple command subscriptions.
273   * This is checked in the registerCommand-function.
274   */
275  bool ShellCommand::exists(const std::string& commandName, const std::string& className)
276  {
277    return (ShellCommand::getCommand(commandName, className) != NULL);
278  }
279
280
281  /**
282   * @brief executes commands
283   * @param executionString the string containing the following input
284   * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
285   * @return true on success, false otherwise.
286   */
287  bool ShellCommand::execute(const std::string& executionString)
288  {
289    SubString inputSplits(executionString, SubString::WhiteSpacesWithComma);
290
291    // if we do not have any input return
292    if (inputSplits.empty())
293      return false;
294
295    unsigned int paramBegin;
296    const ShellCommand* sc = NULL;
297    std::vector<BaseObject*> boList;
298    sc = getCommandFromInput(inputSplits, paramBegin, &boList);
299    if (sc != NULL)
300    {
301      for(std::vector<BaseObject*>::const_iterator bo = boList.begin(); bo != boList.end(); ++bo)
302      {
303        PRINT(0)("Command '%s' on '%s::%s'\n", sc->getCName(), (*bo)->getClassCName(), (*bo)->getCName());
304        (*sc->executor)((*bo), inputSplits.subSet(paramBegin));
305      }
306      return true;
307    }
308    return false;
309  }
310
311
312  /**
313   * @brief lets a command be described
314   * @param description the description of the Given command
315   */
316  ShellCommand* ShellCommand::describe(const std::string& description)
317  {
318    if (this == NULL)
319      return NULL;
320    this->description = description;
321    return this;
322  }
323
324  /**
325   * @brief adds an Alias to this Command
326   * @param alias the name of the Alias to set
327   * @returns itself
328   */
329  ShellCommand* ShellCommand::setAlias(const std::string& alias)
330  {
331    if (this == NULL)
332      return NULL;
333
334    if (this->alias != NULL)
335    {
336      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getCName(), this->shellClass->getCName());
337    }
338    else
339    {
340      ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
341      this->alias = aliasCMD;
342    }
343    return this;
344  }
345
346  /**
347   * @brief set the default values of the executor
348   * @param value0 the first default value
349   * @param value1 the second default value
350   * @param value2 the third default value
351   * @param value3 the fourth default value
352   * @param value4 the fifth default value
353   */
354  ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
355      const MultiType& value2, const MultiType& value3,
356      const MultiType& value4)
357  {
358    if (this == NULL || this->executor == NULL)
359      return NULL;
360
361    this->executor->defaultValues(value0, value1, value2, value3, value4);
362
363    return this;
364  }
365
366  ShellCommand* ShellCommand::completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin)
367  {
368    if (this == NULL || this->executor == NULL)
369      return NULL;
370
371    if (parameter >= this->executor->getParamCount())
372    {
373      PRINTF(1)("Parameter %d not inside of valid ParameterCount %d of Command %s::%s\n",
374                parameter, this->executor->getParamCount(), this->getCName(), this->shellClass->getCName());
375    }
376    else
377    {
378//       if(this->completors[parameter] == NULL)
379//       delete this->completors[parameter];
380//       this->completors[parameter] = completorPlugin.clone();
381    }
382    return this;
383  }
384
385  /**
386   * @brief prints a Help string from this Command
387   */
388  void ShellCommand::help() const
389  {
390    PRINT(0)("%s ", this->getCName());
391  }
392
393  /**
394   * @brief converts a Parameter to a String
395   * @param parameter the Parameter we have.
396   * @returns the Name of the Parameter at Hand
397   */
398  const std::string& ShellCommand::paramToString(long parameter)
399  {
400    return MultiType::MultiTypeToString((MT_Type)parameter);
401  }
402
403
404
405  ///////////
406  // ALIAS //
407  ///////////
408
409  /**
410   * @param aliasName the name of the Alias
411   * @param command the Command, to associate this alias with
412   */
413  ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command)
414  {
415    this->aliasName = aliasName;
416    this->command = command;
417    ShellCommandAlias::aliasList.push_back(this);
418  };
419
420  ShellCommandAlias::~ShellCommandAlias()
421  {
422    std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this);
423    if (delA != aliasList.end())
424      ShellCommandAlias::aliasList.push_back(this);
425
426  }
427
428  std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList;
429  /**
430  * @brief collects the Aliases registered to the ShellCommands
431  * @param stringList a List to paste the Aliases into.
432  * @returns true on success, false otherwise
433   */
434
435  bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList)
436  {
437    std::vector<ShellCommandAlias*>::iterator alias;
438    for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++)
439      stringList.push_back((*alias)->getName());
440    return true;
441  }
442
443
444}
Note: See TracBrowser for help on using the repository browser.