Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

also get the offset

File size: 15.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"
[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");
41    PRINTF(5)("create shellcommand %s %s\n", commandName, className);
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())
100          delete (*cmd);
101    }
[5113]102  }
[5105]103
[7413]104  /**
105   * @brief gets a command if it has already been registered.
106   * @param commandName the name of the Command
107   * @param cmdClass the CommandClass of the Class the command is in.
108   * @returns The Registered Command, or NULL if it does not exist.
109   */
110  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const ShellCommandClass* cmdClass)
111  {
112    assert(cmdClass != NULL);
[7408]113
[7413]114    std::vector<ShellCommand*>::const_iterator elem;
115    for (elem = cmdClass->commandList.begin(); elem != cmdClass->commandList.end(); elem++)
116      if (commandName == (*elem)->getName())
117        return (*elem);
118    return NULL;
119  }
120
121
[7374]122  /**
[7408]123   * @brief gets a command if it has already been registered.
[7374]124   * @param commandName the name of the Command
[7413]125   * @param className the name of the Class the command is in.
[7408]126   * @returns The Registered Command, or NULL if it does not exist.
[7374]127   */
[7408]128  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className)
[5113]129  {
[7408]130    const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className);
[7403]131    if (likely(checkClass != NULL))
[7413]132      return ShellCommand::getCommand(commandName, checkClass);
133    return NULL;
134  }
135
136  /**
137   * @brief takes out an InputLine, searching for a Command.
[7414]138   * @see const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings)
[7413]139   * @param inputLine: the Input to analyse.
[7414]140   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
[7413]141   * @returns: The ShellCommand if found.
142   */
[7414]143  const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin)
[7413]144  {
[7414]145    ShellCommand::getCommandFromInput(SubString(inputLine, SubString::WhiteSpaces), paramBegin);
146  }
147
148  /**
149   * @brief takes out an InputLine, searching for a Command.
150   * @param strings: the Input to analyse.
151   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
152   * @returns: The ShellCommand if found.
153   */
154  const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin)
155  {
[7413]156    // no input, no Command.
157    if (strings.size() == 0)
[7414]158    {
159      paramBegin = 0;
[7413]160      return NULL;
[7414]161    }
[7413]162
163    // CHECK FOR ALIAS
164    std::vector<ShellCommandAlias*>::const_iterator alias;
165    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
[7374]166    {
[7413]167      if (strings[0] == (*alias)->getName())
168      {
169        assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL);
170        return (*alias)->getCommand();
171      }
[5779]172    }
[7413]173
174    const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]);
175    if (cmdClass != NULL)
176    {
177      const ShellCommand* retCmd;
178      if (strings.size() >= 1)
[7414]179      {
[7413]180        retCmd = ShellCommand::getCommand(strings[1], cmdClass);
[7414]181        paramBegin = 2;
182      }
[7413]183      if (retCmd == NULL && strings.size() >= 2)
[7414]184      {
[7413]185        retCmd = ShellCommand::getCommand(strings[2], cmdClass);
[7414]186        paramBegin = 3;
187      }
188      if (retCmd != NULL) // check for the paramBegin.
189        return retCmd;
[7413]190    }
[7414]191    paramBegin = 0;
192    return NULL;
[5113]193  }
194
[7413]195
[7408]196  /**
197   * @brief checks if a command has already been registered.
198   * @param commandName the name of the Command
199   * @param className the name of the Class the command should apply to.
200   * @returns true, if the command is registered/false otherwise
201   *
202   * This is used internally, to see, if we have multiple command subscriptions.
203   * This is checked in the registerCommand-function.
204   */
205  bool ShellCommand::exists(const std::string& commandName, const std::string& className)
206  {
207    return (ShellCommand::getCommand(commandName, className) != NULL);
208  }
[5140]209
[7408]210
[7374]211  /**
[7394]212   * @brief executes commands
[7374]213   * @param executionString the string containing the following input
214   * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
215   * @return true on success, false otherwise.
216   */
217  bool ShellCommand::execute(const std::string& executionString)
218  {
219    long classID = CL_NULL;                      //< the classID retrieved from the Class.
220    ShellCommandClass* commandClass = NULL;      //< the command class this command applies to.
221    const std::list<BaseObject*>* objectList = NULL;   //< the list of Objects stored in classID
222    BaseObject* objectPointer = NULL;            //< a pointer to th Object to Execute the command on
[7411]223    //    bool emptyComplete = false;                  //< if the completion input is empty string. e.g ""
[7374]224    //  long completeType = SHELLC_NONE;           //< the Type we'd like to complete.
[7403]225
[7374]226    SubString inputSplits(executionString, SubString::WhiteSpacesWithComma);
[5198]227
[7340]228
[7374]229    // if we do not have any input return
230    if (inputSplits.empty())
231      return false;
[7340]232
[7374]233    // if we only have one input (!MUST BE AN ALIAS)
[7403]234    // CHECK FOR ALIAS
235    std::vector<ShellCommandAlias*>::const_iterator alias;
236    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
[5198]237    {
[7403]238      if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
239          (*alias)->getCommand()->shellClass != NULL )
[5198]240      {
[7403]241        objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
242        if (objectList != NULL)
[5198]243        {
[7403]244          (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join());
245          return true;
[5198]246        }
247      }
[7403]248    }
[7340]249
[7403]250    // looking for a Matching Class (in the First Argument)
251    std::vector<ShellCommandClass*>::iterator commandClassIT;
252    for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++)
253    {
254      if (inputSplits[0] == (*commandClassIT)->getName())
[5203]255      {
[7403]256        //elemCL->getName();
257        classID = ClassList::StringToID((*commandClassIT)->getName());
258        commandClass = (*commandClassIT);
259        objectList = ClassList::getList((ClassID)classID);
260        break;
[5203]261      }
[7403]262    }
[5200]263
[7403]264    // Second Agument. (either Object, or Function)
265    if (commandClass != NULL && inputSplits.size() >= 2)
266    {
267      int fktPos = 1; // The position of the Function (either at pos 1(if object given), or 2)
268      // If we have an ObjectList.
269      if (objectList != NULL)
[5203]270      {
[7403]271        // Checking for a Match in the Objects of classID (else take the first)
272        std::list<BaseObject*>::const_iterator object;
273        for (object = objectList->begin(); object != objectList->end(); object++)
[5203]274        {
[7403]275          if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName())
[5329]276          {
[7403]277            /// TODO make this work for multiple Objects at once.
278            objectPointer = (*object);
279            fktPos = 2;
280            break;
[5329]281          }
[7340]282        }
[5203]283
[7403]284        // if we did not find an Object with matching name, take the first.
285        if (objectPointer == NULL)
286          objectPointer = objectList->front();
287      }
288
289      // match a function.
290      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3)))
291      {
292        std::vector<ShellCommand*>::iterator cmdIT;
293        for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
[5203]294        {
[7403]295          if (inputSplits[fktPos] == (*cmdIT)->getName())
[5203]296          {
[7403]297            if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
298              return false;
299            else
[7340]300            {
[7403]301              (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK
302              return true;
[7340]303            }
[5203]304          }
305        }
306      }
307    }
[7374]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    {
336      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
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",
[7413]374                parameter, this->executor->getParamCount(), this->getName(), this->shellClass->getName());
[7412]375    }
376    else
377    {
378      delete this->completors[parameter];
379      this->completors[parameter] = completorPlugin.clone();
380    }
381    return this;
382  }
383
384
[7374]385  /**
[7401]386   * @brief prints out nice information about the Shells Commands
[7374]387   */
388  void ShellCommand::debug()
[5148]389  {
[7394]390    std::vector<ShellCommandClass*>::iterator classIT;
391    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
[7374]392    {
393      PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
[5148]394
[7388]395      std::vector<ShellCommand*>::iterator cmdIT;
[7374]396      for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
397      {
398        PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
399        /// FIXME
400        /*      for (unsigned int i = 0; i< elem->paramCount; i++)
401         printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
402        if (!(*cmdIT)->description.empty())
403          printf("- %s", (*cmdIT)->description.c_str());
404        printf("\n");
405
406      }
[5170]407    }
[5148]408  }
409
[7374]410  /**
[7401]411   * @brief converts a Parameter to a String
[7374]412   * @param parameter the Parameter we have.
413   * @returns the Name of the Parameter at Hand
414   */
[7401]415  const std::string& ShellCommand::paramToString(long parameter)
[7374]416  {
417    return MultiType::MultiTypeToString((MT_Type)parameter);
418  }
419
[7389]420
[7412]421
422  ///////////
423  // ALIAS //
424  ///////////
425
[7397]426  /**
427   * @param aliasName the name of the Alias
428   * @param command the Command, to associate this alias with
429   */
[7389]430  ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command)
431  {
432    this->aliasName = aliasName;
433    this->command = command;
434    ShellCommandAlias::aliasList.push_back(this);
435  };
436
437  ShellCommandAlias::~ShellCommandAlias()
438  {
439    std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this);
440    if (delA != aliasList.end())
441      ShellCommandAlias::aliasList.push_back(this);
442
443  }
444
445  std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList;
446  /**
447  * @brief collects the Aliases registered to the ShellCommands
448  * @param stringList a List to paste the Aliases into.
449  * @returns true on success, false otherwise
450   */
[7403]451
[7389]452  bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList)
453  {
454    std::vector<ShellCommandAlias*>::iterator alias;
455    for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++)
456      stringList.push_back((*alias)->getName());
457    return true;
458  }
459
460
[5148]461}
Note: See TracBrowser for help on using the repository browser.