Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fixed out some warnings

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