Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some command-stuff

File size: 12.1 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
[7408]104
[7374]105  /**
[7408]106   * @brief gets a command if it has already been registered.
[7374]107   * @param commandName the name of the Command
108   * @param className the name of the Class the command should apply to.
[7408]109   * @returns The Registered Command, or NULL if it does not exist.
[7374]110   */
[7408]111  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className)
[5113]112  {
[7408]113    const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className);
[7403]114    if (likely(checkClass != NULL))
[7374]115    {
[7388]116      std::vector<ShellCommand*>::const_iterator elem;
[7374]117      for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++)
118        if (commandName == (*elem)->getName())
119        {
120          PRINTF(2)("Command '%s::%s' already registered\n", className.c_str(), commandName.c_str());
[7408]121          return (*elem);
[7374]122        }
[5779]123    }
[7411]124    return NULL;
[5113]125  }
126
[7408]127  /**
128   * @brief checks if a command has already been registered.
129   * @param commandName the name of the Command
130   * @param className the name of the Class the command should apply to.
131   * @returns true, if the command is registered/false otherwise
132   *
133   * This is used internally, to see, if we have multiple command subscriptions.
134   * This is checked in the registerCommand-function.
135   */
136  bool ShellCommand::exists(const std::string& commandName, const std::string& className)
137  {
138    return (ShellCommand::getCommand(commandName, className) != NULL);
139  }
[5140]140
[7408]141
[7374]142  /**
[7394]143   * @brief executes commands
[7374]144   * @param executionString the string containing the following input
145   * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
146   * @return true on success, false otherwise.
147   */
148  bool ShellCommand::execute(const std::string& executionString)
149  {
150    long classID = CL_NULL;                      //< the classID retrieved from the Class.
151    ShellCommandClass* commandClass = NULL;      //< the command class this command applies to.
152    const std::list<BaseObject*>* objectList = NULL;   //< the list of Objects stored in classID
153    BaseObject* objectPointer = NULL;            //< a pointer to th Object to Execute the command on
[7411]154    //    bool emptyComplete = false;                  //< if the completion input is empty string. e.g ""
[7374]155    //  long completeType = SHELLC_NONE;           //< the Type we'd like to complete.
[7403]156
[7374]157    SubString inputSplits(executionString, SubString::WhiteSpacesWithComma);
[5198]158
[7340]159
[7374]160    // if we do not have any input return
161    if (inputSplits.empty())
162      return false;
[7340]163
[7374]164    // if we only have one input (!MUST BE AN ALIAS)
[7403]165    // CHECK FOR ALIAS
166    std::vector<ShellCommandAlias*>::const_iterator alias;
167    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
[5198]168    {
[7403]169      if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
170          (*alias)->getCommand()->shellClass != NULL )
[5198]171      {
[7403]172        objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
173        if (objectList != NULL)
[5198]174        {
[7403]175          (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join());
176          return true;
[5198]177        }
178      }
[7403]179    }
[7340]180
[7403]181    // looking for a Matching Class (in the First Argument)
182    std::vector<ShellCommandClass*>::iterator commandClassIT;
183    for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++)
184    {
185      if (inputSplits[0] == (*commandClassIT)->getName())
[5203]186      {
[7403]187        //elemCL->getName();
188        classID = ClassList::StringToID((*commandClassIT)->getName());
189        commandClass = (*commandClassIT);
190        objectList = ClassList::getList((ClassID)classID);
191        break;
[5203]192      }
[7403]193    }
[5200]194
[7403]195    // Second Agument. (either Object, or Function)
196    if (commandClass != NULL && inputSplits.size() >= 2)
197    {
198      int fktPos = 1; // The position of the Function (either at pos 1(if object given), or 2)
199      // If we have an ObjectList.
200      if (objectList != NULL)
[5203]201      {
[7403]202        // Checking for a Match in the Objects of classID (else take the first)
203        std::list<BaseObject*>::const_iterator object;
204        for (object = objectList->begin(); object != objectList->end(); object++)
[5203]205        {
[7403]206          if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName())
[5329]207          {
[7403]208            /// TODO make this work for multiple Objects at once.
209            objectPointer = (*object);
210            fktPos = 2;
211            break;
[5329]212          }
[7340]213        }
[5203]214
[7403]215        // if we did not find an Object with matching name, take the first.
216        if (objectPointer == NULL)
217          objectPointer = objectList->front();
218      }
219
220      // match a function.
221      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3)))
222      {
223        std::vector<ShellCommand*>::iterator cmdIT;
224        for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
[5203]225        {
[7403]226          if (inputSplits[fktPos] == (*cmdIT)->getName())
[5203]227          {
[7403]228            if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
229              return false;
230            else
[7340]231            {
[7403]232              (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK
233              return true;
[7340]234            }
[5203]235          }
236        }
237      }
238    }
[7374]239    return false;
[5198]240  }
[5148]241
[7411]242
[7374]243  /**
[7401]244   * @brief lets a command be described
[7374]245   * @param description the description of the Given command
246   */
247  ShellCommand* ShellCommand::describe(const std::string& description)
[7340]248  {
[7374]249    if (this == NULL)
250      return NULL;
[7403]251    this->description = description;
252    return this;
[7340]253  }
[5164]254
[7374]255  /**
[7389]256   * @brief adds an Alias to this Command
[7374]257   * @param alias the name of the Alias to set
258   * @returns itself
259   */
260  ShellCommand* ShellCommand::setAlias(const std::string& alias)
[5196]261  {
[7374]262    if (this == NULL)
263      return NULL;
[5196]264
[7374]265    if (this->alias != NULL)
266    {
267      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
268    }
269    else
270    {
271      ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
272      this->alias = aliasCMD;
273    }
274    return this;
[5196]275  }
[5195]276
[7374]277  /**
278   * @brief set the default values of the executor
279   * @param value0 the first default value
280   * @param value1 the second default value
281   * @param value2 the third default value
282   * @param value3 the fourth default value
283   * @param value4 the fifth default value
284   */
285  ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
286      const MultiType& value2, const MultiType& value3,
287      const MultiType& value4)
288  {
289    if (this == NULL || this->executor == NULL)
290      return NULL;
[5207]291
[7374]292    this->executor->defaultValues(value0, value1, value2, value3, value4);
[5207]293
[7374]294    return this;
[5148]295  }
296
[7374]297  /**
[7401]298   * @brief prints out nice information about the Shells Commands
[7374]299   */
300  void ShellCommand::debug()
[5148]301  {
[7394]302    std::vector<ShellCommandClass*>::iterator classIT;
303    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
[7374]304    {
305      PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
[5148]306
[7388]307      std::vector<ShellCommand*>::iterator cmdIT;
[7374]308      for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
309      {
310        PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
311        /// FIXME
312        /*      for (unsigned int i = 0; i< elem->paramCount; i++)
313         printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
314        if (!(*cmdIT)->description.empty())
315          printf("- %s", (*cmdIT)->description.c_str());
316        printf("\n");
317
318      }
[5170]319    }
[5148]320  }
321
[7374]322  /**
[7401]323   * @brief converts a Parameter to a String
[7374]324   * @param parameter the Parameter we have.
325   * @returns the Name of the Parameter at Hand
326   */
[7401]327  const std::string& ShellCommand::paramToString(long parameter)
[7374]328  {
329    return MultiType::MultiTypeToString((MT_Type)parameter);
330  }
331
[7389]332
[7397]333  /**
334   * @param aliasName the name of the Alias
335   * @param command the Command, to associate this alias with
336   */
[7389]337  ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command)
338  {
339    this->aliasName = aliasName;
340    this->command = command;
341    ShellCommandAlias::aliasList.push_back(this);
342  };
343
344  ShellCommandAlias::~ShellCommandAlias()
345  {
346    std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this);
347    if (delA != aliasList.end())
348      ShellCommandAlias::aliasList.push_back(this);
349
350  }
351
352  std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList;
353  /**
354  * @brief collects the Aliases registered to the ShellCommands
355  * @param stringList a List to paste the Aliases into.
356  * @returns true on success, false otherwise
357   */
[7403]358
[7389]359  bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList)
360  {
361    std::vector<ShellCommandAlias*>::iterator alias;
362    for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++)
363      stringList.push_back((*alias)->getName());
364    return true;
365  }
366
367
[5148]368}
Note: See TracBrowser for help on using the repository browser.