Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7583 was 7456, checked in by bensch, 19 years ago

trunk: work work…

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