Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 13.6 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[7374]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
[1853]17
[5129]18#include "shell_command.h"
[5639]19#include "shell_command_class.h"
[1853]20
[6222]21#include "compiler.h"
[5129]22#include "debug.h"
[5113]23#include "class_list.h"
24
25#include "key_names.h"
[5075]26
[7374]27namespace OrxShell
[3365]28{
[7742]29  SHELL_COMMAND(debug, ShellCommandClass, help);
[5141]30
[7394]31
[7374]32  /**
[7394]33   * @brief constructs and registers a new Command
[7374]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   */
[7722]38  ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, Executor* executor)
[7374]39  {
40    this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
[7456]41    PRINTF(4)("create shellcommand '%s' for class '%s'\n", commandName.c_str(), className.c_str());
[7374]42    this->setName(commandName);
[7398]43
44    // copy the executor:
[7722]45    this->executor = executor;
[7374]46    this->executor->setName(commandName);
[7398]47
[7407]48    for (unsigned int i = 0; i < this->executor->getParamCount(); i++)
49      this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i)));
[7388]50    this->alias = NULL;
[4320]51
[7374]52    //  this->classID = classID;
[7408]53    this->shellClass = ShellCommandClass::acquireCommandClass(className);
[7398]54    assert (this->shellClass != NULL);
[7399]55    this->shellClass->registerCommand(this);
[7374]56  }
57
58  /**
[7394]59   * @brief deconstructs a ShellCommand
[7374]60   */
61  ShellCommand::~ShellCommand()
[5196]62  {
[7399]63    this->shellClass->unregisterCommand(this);
[7389]64    if (this->alias != NULL)
[7374]65      delete this->alias;
[7407]66    while (!this->completors.empty())
67    {
68      delete this->completors.back();
69      this->completors.pop_back();
70    }
[7374]71    delete this->executor;
[5196]72  }
[1853]73
[7374]74  /**
[7398]75   * @brief registers a new ShellCommand
[7374]76   */
[7722]77  ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, Executor* executor)
[7374]78  {
[7403]79    if (ShellCommand::exists(commandName, className))
[7722]80    {
81      delete executor;
[7374]82      return NULL;
[7722]83    }
[7374]84    else
85      return new ShellCommand(commandName, className, executor);
[5636]86
[7374]87  }
[5636]88
[7374]89  /**
[7398]90   * @brief unregister an existing commandName
[7374]91   * @param className the name of the Class the command belongs to.
92   * @param commandName the name of the command itself
93   */
94  void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className)
95  {
[5171]96
[7408]97    ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className);
[7399]98    if (cmdClass != NULL)
99    {
[7742]100      CmdList::iterator cmd;
101      for (cmd = cmdClass->commandList.begin(); cmd != cmdClass->commandList.end(); cmd++)
[7399]102        if (commandName == (*cmd)->getName())
[7420]103        {
[7399]104          delete (*cmd);
[7420]105          break;
106        }
[7399]107    }
[5113]108  }
[5105]109
[7413]110  /**
111   * @brief gets a command if it has already been registered.
112   * @param commandName the name of the Command
113   * @param cmdClass the CommandClass of the Class the command is in.
114   * @returns The Registered Command, or NULL if it does not exist.
115   */
116  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const ShellCommandClass* cmdClass)
117  {
118    assert(cmdClass != NULL);
[7408]119
[7742]120    CmdList::const_iterator elem;
[9406]121    for (unsigned int i = 0; i < cmdClass->commandList.size(); i++)
122    {
123      if (commandName == cmdClass->commandList[i]->getName())
124        return (cmdClass->commandList[i]);
125    }
[7413]126    return NULL;
127  }
128
129
[7374]130  /**
[7408]131   * @brief gets a command if it has already been registered.
[7374]132   * @param commandName the name of the Command
[7413]133   * @param className the name of the Class the command is in.
[7408]134   * @returns The Registered Command, or NULL if it does not exist.
[7374]135   */
[7408]136  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className)
[5113]137  {
[7408]138    const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className);
[7403]139    if (likely(checkClass != NULL))
[7413]140      return ShellCommand::getCommand(commandName, checkClass);
[9406]141    else
142      return NULL;
[7413]143  }
144
145  /**
146   * @brief takes out an InputLine, searching for a Command.
[7414]147   * @see const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings)
[7413]148   * @param inputLine: the Input to analyse.
[7414]149   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
[7413]150   * @returns: The ShellCommand if found.
151   */
[7417]152  const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
[7413]153  {
[9406]154    return ShellCommand::getCommandFromInput(SubString(inputLine, SubString::WhiteSpaces), paramBegin, boList);
[7414]155  }
156
157  /**
158   * @brief takes out an InputLine, searching for a Command.
159   * @param strings: the Input to analyse.
160   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
161   * @returns: The ShellCommand if found.
162   */
[7417]163  const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
[7414]164  {
[7413]165    // no input, no Command.
166    if (strings.size() == 0)
[7414]167    {
168      paramBegin = 0;
[7413]169      return NULL;
[7414]170    }
[7413]171
172    // CHECK FOR ALIAS
173    std::vector<ShellCommandAlias*>::const_iterator alias;
174    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
[7374]175    {
[7413]176      if (strings[0] == (*alias)->getName())
177      {
178        assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL);
[7418]179        // Search for Objects.
180        if (strings.size() == 1)
[7422]181        {
182          if (fillObjectList("", (*alias)->getCommand(), boList))
183            ;
184        }
[7418]185        else
186        {
187          if (!fillObjectList(strings[1], (*alias)->getCommand(), boList))
188            fillObjectList("", (*alias)->getCommand(), boList);
189        }
[7771]190        paramBegin = 1;
[7413]191        return (*alias)->getCommand();
192      }
[5779]193    }
[7413]194
[7417]195    // CHECK FOR COMMAND_CLASS
[7413]196    const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]);
197    if (cmdClass != NULL)
198    {
[8350]199      const ShellCommand* retCmd = NULL;
[7417]200      // Function/Command right after Class
[7413]201      if (strings.size() >= 1)
[7414]202      {
[7418]203        // Search for Objects.
[9406]204        retCmd = ShellCommand::getCommand((strings.size() > 1) ? strings[1] : "", cmdClass);
[7417]205        if (retCmd != NULL)
206        {
207          paramBegin = 2;
[7418]208          fillObjectList("", retCmd, boList);
[7417]209          return retCmd;
210        }
[7414]211      }
[7417]212      // Function/Command after Class and 'Object'
[7413]213      if (retCmd == NULL && strings.size() >= 2)
[7414]214      {
[9406]215        retCmd = ShellCommand::getCommand((strings.size() > 2) ? strings[2] : "", cmdClass);
[7417]216        if (retCmd != NULL)
217        {
[7415]218          paramBegin = 3;
[7418]219          fillObjectList(strings[1], retCmd, boList);
[7417]220          return retCmd;
221        }
[7414]222      }
223      if (retCmd != NULL) // check for the paramBegin.
224        return retCmd;
[7413]225    }
[7417]226    // Nothing usefull found at all.
[7414]227    paramBegin = 0;
228    return NULL;
[5113]229  }
230
[7418]231  /**
232   * @brief fills the ObjectList boList with Objects that can be reffered to by cmd.
233   * @param objectDescriptor: the ObjectName (beginning, full name or empty) to fill the List with
234   * @param cmd: The Command to complete Objects for.
235   * @param boList: The List of BaseObject's that will be filled with found entries.
236   * @returns: true if more than one Entry was fond, else (false , or if boList is NULL).
237   */
238  bool ShellCommand::fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList)
[7417]239  {
[7418]240    assert (cmd != NULL && cmd->shellClass != NULL);
241    if(boList == NULL)
242      return false;
[7413]243
[7417]244    const std::list<BaseObject*>* objectList = ClassList::getList(cmd->shellClass->getName());
245    if (objectList != NULL)
246    {
247      std::list<BaseObject*>::const_iterator bo;
248
249      // No Description given (only for speedup)
250      if (objectDescriptor.empty())
251      {
252        for (bo = objectList->begin(); bo != objectList->end(); bo++)
253          boList->push_back(*bo);
254      }
255      // some description
256      else
257      {
258        for (bo = objectList->begin(); bo != objectList->end(); bo++)
[9406]259          if (!nocaseCmp(objectDescriptor, (*bo)->getName(), objectDescriptor.size()))
[7417]260            boList->push_back(*bo);
261      }
262    }
[7418]263    return !boList->empty();
[7417]264  }
265
[7408]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  }
[5140]279
[7408]280
[7374]281  /**
[7394]282   * @brief executes commands
[7374]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);
[5198]290
[7374]291    // if we do not have any input return
292    if (inputSplits.empty())
293      return false;
[7340]294
[7419]295    unsigned int paramBegin;
[7771]296    const ShellCommand* sc = NULL;
[7419]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)
[7420]302      {
[9406]303        PRINT(0)("Command '%s' on '%s::%s'\n", sc->getCName(), (*bo)->getClassCName(), (*bo)->getCName());
304        (*sc->executor)((*bo), inputSplits.subSet(paramBegin));
[7420]305      }
306      return true;
[7419]307    }
[7420]308    return false;
[5198]309  }
[5148]310
[7411]311
[7374]312  /**
[7401]313   * @brief lets a command be described
[7374]314   * @param description the description of the Given command
315   */
316  ShellCommand* ShellCommand::describe(const std::string& description)
[7340]317  {
[7374]318    if (this == NULL)
319      return NULL;
[7403]320    this->description = description;
321    return this;
[7340]322  }
[5164]323
[7374]324  /**
[7389]325   * @brief adds an Alias to this Command
[7374]326   * @param alias the name of the Alias to set
327   * @returns itself
328   */
329  ShellCommand* ShellCommand::setAlias(const std::string& alias)
[5196]330  {
[7374]331    if (this == NULL)
332      return NULL;
[5196]333
[7374]334    if (this->alias != NULL)
335    {
[9406]336      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getCName(), this->shellClass->getCName());
[7374]337    }
338    else
339    {
340      ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
341      this->alias = aliasCMD;
342    }
343    return this;
[5196]344  }
[5195]345
[7374]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;
[5207]360
[7374]361    this->executor->defaultValues(value0, value1, value2, value3, value4);
[5207]362
[7374]363    return this;
[5148]364  }
365
[7412]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",
[9406]374                parameter, this->executor->getParamCount(), this->getCName(), this->shellClass->getCName());
[7412]375    }
376    else
377    {
[9112]378//       if(this->completors[parameter] == NULL)
379//       delete this->completors[parameter];
[9406]380//       this->completors[parameter] = completorPlugin.clone();
[7412]381    }
382    return this;
383  }
384
[7374]385  /**
[7742]386   * @brief prints a Help string from this Command
[7374]387   */
[7742]388  void ShellCommand::help() const
[5148]389  {
[9406]390    PRINT(0)("%s ", this->getCName());
[5148]391  }
392
[7374]393  /**
[7401]394   * @brief converts a Parameter to a String
[7374]395   * @param parameter the Parameter we have.
396   * @returns the Name of the Parameter at Hand
397   */
[7401]398  const std::string& ShellCommand::paramToString(long parameter)
[7374]399  {
400    return MultiType::MultiTypeToString((MT_Type)parameter);
401  }
402
[7389]403
[7412]404
405  ///////////
406  // ALIAS //
407  ///////////
408
[7397]409  /**
410   * @param aliasName the name of the Alias
411   * @param command the Command, to associate this alias with
412   */
[7389]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   */
[7403]434
[7389]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
[5148]444}
Note: See TracBrowser for help on using the repository browser.