Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Test in Execute (first implementation)

File size: 17.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{
[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   */
[7417]143  const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
[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   */
[7417]154  const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
[7414]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);
[7418]170        // Search for Objects.
171        if (strings.size() == 1)
172          fillObjectList("", (*alias)->getCommand(), boList);
173        else
174        {
175          if (!fillObjectList(strings[1], (*alias)->getCommand(), boList))
176            fillObjectList("", (*alias)->getCommand(), boList);
177        }
[7413]178        return (*alias)->getCommand();
179      }
[5779]180    }
[7413]181
[7417]182    // CHECK FOR COMMAND_CLASS
[7413]183    const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]);
184    if (cmdClass != NULL)
185    {
186      const ShellCommand* retCmd;
[7417]187      // Function/Command right after Class
[7413]188      if (strings.size() >= 1)
[7414]189      {
[7418]190        // Search for Objects.
[7413]191        retCmd = ShellCommand::getCommand(strings[1], cmdClass);
[7417]192        if (retCmd != NULL)
193        {
194          paramBegin = 2;
[7418]195          fillObjectList("", retCmd, boList);
[7417]196          return retCmd;
197        }
[7414]198      }
[7417]199      // Function/Command after Class and 'Object'
[7413]200      if (retCmd == NULL && strings.size() >= 2)
[7414]201      {
[7413]202        retCmd = ShellCommand::getCommand(strings[2], cmdClass);
[7417]203        if (retCmd != NULL)
204        {
[7415]205          paramBegin = 3;
[7418]206          fillObjectList(strings[1], retCmd, boList);
[7417]207          return retCmd;
208        }
[7414]209      }
210      if (retCmd != NULL) // check for the paramBegin.
211        return retCmd;
[7413]212    }
[7417]213    // Nothing usefull found at all.
[7414]214    paramBegin = 0;
215    return NULL;
[5113]216  }
217
[7418]218  /**
219   * @brief fills the ObjectList boList with Objects that can be reffered to by cmd.
220   * @param objectDescriptor: the ObjectName (beginning, full name or empty) to fill the List with
221   * @param cmd: The Command to complete Objects for.
222   * @param boList: The List of BaseObject's that will be filled with found entries.
223   * @returns: true if more than one Entry was fond, else (false , or if boList is NULL).
224   */
225  bool ShellCommand::fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList)
[7417]226  {
[7418]227    assert (cmd != NULL && cmd->shellClass != NULL);
228    if(boList == NULL)
229      return false;
[7413]230
[7417]231    const std::list<BaseObject*>* objectList = ClassList::getList(cmd->shellClass->getName());
232    if (objectList != NULL)
233    {
234      std::list<BaseObject*>::const_iterator bo;
235
236      // No Description given (only for speedup)
237      if (objectDescriptor.empty())
238      {
239        for (bo = objectList->begin(); bo != objectList->end(); bo++)
240          boList->push_back(*bo);
241      }
242      // some description
243      else
244      {
245        for (bo = objectList->begin(); bo != objectList->end(); bo++)
246          if ((*bo)->getName() != NULL && nocaseCmp(objectDescriptor, (*bo)->getName(), objectDescriptor.size()))
247            boList->push_back(*bo);
248      }
249    }
[7418]250    return !boList->empty();
[7417]251  }
252
[7408]253  /**
254   * @brief checks if a command has already been registered.
255   * @param commandName the name of the Command
256   * @param className the name of the Class the command should apply to.
257   * @returns true, if the command is registered/false otherwise
258   *
259   * This is used internally, to see, if we have multiple command subscriptions.
260   * This is checked in the registerCommand-function.
261   */
262  bool ShellCommand::exists(const std::string& commandName, const std::string& className)
263  {
264    return (ShellCommand::getCommand(commandName, className) != NULL);
265  }
[5140]266
[7408]267
[7374]268  /**
[7394]269   * @brief executes commands
[7374]270   * @param executionString the string containing the following input
271   * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
272   * @return true on success, false otherwise.
273   */
274  bool ShellCommand::execute(const std::string& executionString)
275  {
276    long classID = CL_NULL;                      //< the classID retrieved from the Class.
277    ShellCommandClass* commandClass = NULL;      //< the command class this command applies to.
278    const std::list<BaseObject*>* objectList = NULL;   //< the list of Objects stored in classID
279    BaseObject* objectPointer = NULL;            //< a pointer to th Object to Execute the command on
[7411]280    //    bool emptyComplete = false;                  //< if the completion input is empty string. e.g ""
[7374]281    //  long completeType = SHELLC_NONE;           //< the Type we'd like to complete.
[7403]282
[7374]283    SubString inputSplits(executionString, SubString::WhiteSpacesWithComma);
[5198]284
[7374]285    // if we do not have any input return
286    if (inputSplits.empty())
287      return false;
[7340]288
[7419]289    unsigned int paramBegin;
290    const ShellCommand* sc;
291    std::vector<BaseObject*> boList;
292    sc = getCommandFromInput(inputSplits, paramBegin, &boList);
293    if (sc != NULL)
294    {
295      PRINT(0)("Command %s on ", sc->getName());
296      for(std::vector<BaseObject*>::const_iterator bo = boList.begin(); bo != boList.end(); ++bo)
297        PRINT(0)("%s::%s\n", (*bo)->getClassName(), (*bo)->getName());
298    }
299
[7374]300    // if we only have one input (!MUST BE AN ALIAS)
[7403]301    // CHECK FOR ALIAS
302    std::vector<ShellCommandAlias*>::const_iterator alias;
303    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
[5198]304    {
[7403]305      if (inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
306          (*alias)->getCommand()->shellClass != NULL )
[5198]307      {
[7403]308        objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
309        if (objectList != NULL)
[5198]310        {
[7403]311          (*(*alias)->getCommand()->executor)(objectList->front(), inputSplits.getSubSet(1).join());
312          return true;
[5198]313        }
314      }
[7403]315    }
[7340]316
[7403]317    // looking for a Matching Class (in the First Argument)
318    std::vector<ShellCommandClass*>::iterator commandClassIT;
319    for (commandClassIT = ShellCommandClass::commandClassList.begin(); commandClassIT != ShellCommandClass::commandClassList.end(); commandClassIT++)
320    {
321      if (inputSplits[0] == (*commandClassIT)->getName())
[5203]322      {
[7403]323        //elemCL->getName();
324        classID = ClassList::StringToID((*commandClassIT)->getName());
325        commandClass = (*commandClassIT);
326        objectList = ClassList::getList((ClassID)classID);
327        break;
[5203]328      }
[7403]329    }
[5200]330
[7403]331    // Second Agument. (either Object, or Function)
332    if (commandClass != NULL && inputSplits.size() >= 2)
333    {
334      int fktPos = 1; // The position of the Function (either at pos 1(if object given), or 2)
335      // If we have an ObjectList.
336      if (objectList != NULL)
[5203]337      {
[7403]338        // Checking for a Match in the Objects of classID (else take the first)
339        std::list<BaseObject*>::const_iterator object;
340        for (object = objectList->begin(); object != objectList->end(); object++)
[5203]341        {
[7403]342          if ((*object)->getName() != NULL && inputSplits[1] == (*object)->getName())
[5329]343          {
[7403]344            /// TODO make this work for multiple Objects at once.
345            objectPointer = (*object);
346            fktPos = 2;
347            break;
[5329]348          }
[7340]349        }
[5203]350
[7403]351        // if we did not find an Object with matching name, take the first.
352        if (objectPointer == NULL)
353          objectPointer = objectList->front();
354      }
355
356      // match a function.
357      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.size() >= 3)))
358      {
359        std::vector<ShellCommand*>::iterator cmdIT;
360        for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
[5203]361        {
[7403]362          if (inputSplits[fktPos] == (*cmdIT)->getName())
[5203]363          {
[7403]364            if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
365              return false;
366            else
[7340]367            {
[7403]368              (*(*cmdIT)->executor)(objectPointer, inputSplits.getSubSet(fktPos+1).join()); /// TODO CHECK IF OK
369              return true;
[7340]370            }
[5203]371          }
372        }
373      }
374    }
[7374]375    return false;
[5198]376  }
[5148]377
[7411]378
[7374]379  /**
[7401]380   * @brief lets a command be described
[7374]381   * @param description the description of the Given command
382   */
383  ShellCommand* ShellCommand::describe(const std::string& description)
[7340]384  {
[7374]385    if (this == NULL)
386      return NULL;
[7403]387    this->description = description;
388    return this;
[7340]389  }
[5164]390
[7374]391  /**
[7389]392   * @brief adds an Alias to this Command
[7374]393   * @param alias the name of the Alias to set
394   * @returns itself
395   */
396  ShellCommand* ShellCommand::setAlias(const std::string& alias)
[5196]397  {
[7374]398    if (this == NULL)
399      return NULL;
[5196]400
[7374]401    if (this->alias != NULL)
402    {
403      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
404    }
405    else
406    {
407      ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
408      this->alias = aliasCMD;
409    }
410    return this;
[5196]411  }
[5195]412
[7374]413  /**
414   * @brief set the default values of the executor
415   * @param value0 the first default value
416   * @param value1 the second default value
417   * @param value2 the third default value
418   * @param value3 the fourth default value
419   * @param value4 the fifth default value
420   */
421  ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
422      const MultiType& value2, const MultiType& value3,
423      const MultiType& value4)
424  {
425    if (this == NULL || this->executor == NULL)
426      return NULL;
[5207]427
[7374]428    this->executor->defaultValues(value0, value1, value2, value3, value4);
[5207]429
[7374]430    return this;
[5148]431  }
432
[7412]433  ShellCommand* ShellCommand::completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin)
434  {
435    if (this == NULL || this->executor == NULL)
436      return NULL;
437
438    if (parameter >= this->executor->getParamCount())
439    {
440      PRINTF(1)("Parameter %d not inside of valid ParameterCount %d of Command %s::%s\n",
[7413]441                parameter, this->executor->getParamCount(), this->getName(), this->shellClass->getName());
[7412]442    }
443    else
444    {
445      delete this->completors[parameter];
446      this->completors[parameter] = completorPlugin.clone();
447    }
448    return this;
449  }
450
451
[7374]452  /**
[7401]453   * @brief prints out nice information about the Shells Commands
[7374]454   */
455  void ShellCommand::debug()
[5148]456  {
[7394]457    std::vector<ShellCommandClass*>::iterator classIT;
458    for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++)
[7374]459    {
460      PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
[5148]461
[7388]462      std::vector<ShellCommand*>::iterator cmdIT;
[7374]463      for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
464      {
465        PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
466        /// FIXME
467        /*      for (unsigned int i = 0; i< elem->paramCount; i++)
468         printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
469        if (!(*cmdIT)->description.empty())
470          printf("- %s", (*cmdIT)->description.c_str());
471        printf("\n");
472
473      }
[5170]474    }
[5148]475  }
476
[7374]477  /**
[7401]478   * @brief converts a Parameter to a String
[7374]479   * @param parameter the Parameter we have.
480   * @returns the Name of the Parameter at Hand
481   */
[7401]482  const std::string& ShellCommand::paramToString(long parameter)
[7374]483  {
484    return MultiType::MultiTypeToString((MT_Type)parameter);
485  }
486
[7389]487
[7412]488
489  ///////////
490  // ALIAS //
491  ///////////
492
[7397]493  /**
494   * @param aliasName the name of the Alias
495   * @param command the Command, to associate this alias with
496   */
[7389]497  ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command)
498  {
499    this->aliasName = aliasName;
500    this->command = command;
501    ShellCommandAlias::aliasList.push_back(this);
502  };
503
504  ShellCommandAlias::~ShellCommandAlias()
505  {
506    std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this);
507    if (delA != aliasList.end())
508      ShellCommandAlias::aliasList.push_back(this);
509
510  }
511
512  std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList;
513  /**
514  * @brief collects the Aliases registered to the ShellCommands
515  * @param stringList a List to paste the Aliases into.
516  * @returns true on success, false otherwise
517   */
[7403]518
[7389]519  bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList)
520  {
521    std::vector<ShellCommandAlias*>::iterator alias;
522    for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++)
523      stringList.push_back((*alias)->getName());
524    return true;
525  }
526
527
[5148]528}
Note: See TracBrowser for help on using the repository browser.