Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: shell works again.

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