Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Windows runs again without any segfaults
it seems, that windows links object files differently… and extremely strange

File size: 15.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
[9916]21#include "shell_input.h"
22#include "shell.h"
23
[6222]24#include "compiler.h"
[9869]25#include "helper_functions.h"
[5129]26#include "debug.h"
[5113]27
28#include "key_names.h"
[5075]29
[7374]30namespace OrxShell
[3365]31{
[9869]32  ObjectListDefinition(ShellCommand);
[9916]33
[7742]34  SHELL_COMMAND(debug, ShellCommandClass, help);
[5141]35
[9916]36  SHELL_COMMAND(help, ShellInput, help)
37  ->describe("retrieve some help about the input mode")
38  ->setAlias("help");
[7394]39
[9916]40  SHELL_COMMAND(clear, Shell, clear)
41  ->describe("Clears the shell from unwanted lines (empties all buffers)")
42  ->setAlias("clear");
43  SHELL_COMMAND(deactivate, Shell, deactivate)
44  ->describe("Deactivates the Shell. (moves it into background)")
45  ->setAlias("hide");
46  SHELL_COMMAND(textsize, Shell, setTextSize)
47  ->describe("Sets the size of the Text size, linespacing")
48  ->defaultValues(15, 0);
49  SHELL_COMMAND(textcolor, Shell, setTextColor)
50  ->describe("Sets the Color of the Shells Text (red, green, blue, alpha)")
51  ->defaultValues(SHELL_DEFAULT_TEXT_COLOR);
52  SHELL_COMMAND(backgroundcolor, Shell, setBackgroundColor)
53  ->describe("Sets the Color of the Shells Background (red, green, blue, alpha)")
54  ->defaultValues(SHELL_DEFAULT_BACKGROUND_COLOR);
55  SHELL_COMMAND(backgroundimage, Shell, setBackgroundImage)
56  ->describe("sets the background image to load for the Shell")
57  ->completionPlugin(0, OrxShell::CompletorFileSystem());
58  SHELL_COMMAND(font, Shell, setFont)
59  ->describe("Sets the font of the Shell")
60  ->defaultValues(SHELL_DEFAULT_FONT)
61  ->completionPlugin(0, OrxShell::CompletorFileSystem(".ttf", "fonts/"));
62
[7374]63  /**
[7394]64   * @brief constructs and registers a new Command
[7374]65   * @param commandName the name of the Command
66   * @param className the name of the class to apply this command to
67   * @param paramCount the count of parameters this command takes
68   */
[9869]69  ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, Executor<const SubString>* executor)
[7374]70  {
[9916]71    PRINTF(4)("create shellcommand '%s' for class '%s'\n", commandName.c_str(), className.c_str());
[9899]72    this->registerObject(this, ShellCommand::_objectList);
[7374]73    this->setName(commandName);
[7398]74
75    // copy the executor:
[7722]76    this->executor = executor;
[7398]77
[7407]78    for (unsigned int i = 0; i < this->executor->getParamCount(); i++)
79      this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i)));
[7388]80    this->alias = NULL;
[4320]81
[7374]82    //  this->classID = classID;
[7408]83    this->shellClass = ShellCommandClass::acquireCommandClass(className);
[7398]84    assert (this->shellClass != NULL);
[7399]85    this->shellClass->registerCommand(this);
[7374]86  }
87
88  /**
[7394]89   * @brief deconstructs a ShellCommand
[7374]90   */
91  ShellCommand::~ShellCommand()
[5196]92  {
[7399]93    this->shellClass->unregisterCommand(this);
[7389]94    if (this->alias != NULL)
[7374]95      delete this->alias;
[7407]96    while (!this->completors.empty())
97    {
98      delete this->completors.back();
99      this->completors.pop_back();
100    }
[7374]101    delete this->executor;
[5196]102  }
[1853]103
[7374]104  /**
[7398]105   * @brief registers a new ShellCommand
[7374]106   */
[9869]107  ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, Executor<const SubString>* executor)
[7374]108  {
[7403]109    if (ShellCommand::exists(commandName, className))
[7722]110    {
111      delete executor;
[7374]112      return NULL;
[7722]113    }
[7374]114    else
115      return new ShellCommand(commandName, className, executor);
[5636]116
[7374]117  }
[5636]118
[7374]119  /**
[7398]120   * @brief unregister an existing commandName
[7374]121   * @param className the name of the Class the command belongs to.
122   * @param commandName the name of the command itself
123   */
124  void ShellCommand::unregisterCommand(const std::string& commandName, const std::string& className)
125  {
[7408]126    ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className);
[7399]127    if (cmdClass != NULL)
128    {
[7742]129      CmdList::iterator cmd;
[9869]130      for (cmd = cmdClass->_commandList.begin(); cmd != cmdClass->_commandList.end(); cmd++)
[7399]131        if (commandName == (*cmd)->getName())
[7420]132        {
[7399]133          delete (*cmd);
[7420]134          break;
135        }
[7399]136    }
[5113]137  }
[5105]138
[7413]139  /**
140   * @brief gets a command if it has already been registered.
141   * @param commandName the name of the Command
142   * @param cmdClass the CommandClass of the Class the command is in.
143   * @returns The Registered Command, or NULL if it does not exist.
144   */
145  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const ShellCommandClass* cmdClass)
146  {
147    assert(cmdClass != NULL);
[7408]148
[7742]149    CmdList::const_iterator elem;
[9869]150    for (unsigned int i = 0; i < cmdClass->_commandList.size(); i++)
[9406]151    {
[9869]152      if (commandName == cmdClass->_commandList[i]->getName())
153        return (cmdClass->_commandList[i]);
[9406]154    }
[7413]155    return NULL;
156  }
157
158
[7374]159  /**
[7408]160   * @brief gets a command if it has already been registered.
[7374]161   * @param commandName the name of the Command
[7413]162   * @param className the name of the Class the command is in.
[7408]163   * @returns The Registered Command, or NULL if it does not exist.
[7374]164   */
[7408]165  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className)
[5113]166  {
[7408]167    const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className);
[7403]168    if (likely(checkClass != NULL))
[7413]169      return ShellCommand::getCommand(commandName, checkClass);
[9406]170    else
171      return NULL;
[7413]172  }
173
174  /**
175   * @brief takes out an InputLine, searching for a Command.
[7414]176   * @see const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings)
[7413]177   * @param inputLine: the Input to analyse.
[7414]178   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
[7413]179   * @returns: The ShellCommand if found.
180   */
[7417]181  const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
[7413]182  {
[9406]183    return ShellCommand::getCommandFromInput(SubString(inputLine, SubString::WhiteSpaces), paramBegin, boList);
[7414]184  }
185
186  /**
187   * @brief takes out an InputLine, searching for a Command.
188   * @param strings: the Input to analyse.
189   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
190   * @returns: The ShellCommand if found.
191   */
[7417]192  const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
[7414]193  {
[7413]194    // no input, no Command.
195    if (strings.size() == 0)
[7414]196    {
197      paramBegin = 0;
[7413]198      return NULL;
[7414]199    }
[7413]200
201    // CHECK FOR ALIAS
202    std::vector<ShellCommandAlias*>::const_iterator alias;
203    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
[7374]204    {
[7413]205      if (strings[0] == (*alias)->getName())
206      {
207        assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL);
[7418]208        // Search for Objects.
209        if (strings.size() == 1)
[7422]210        {
211          if (fillObjectList("", (*alias)->getCommand(), boList))
212            ;
213        }
[7418]214        else
215        {
216          if (!fillObjectList(strings[1], (*alias)->getCommand(), boList))
217            fillObjectList("", (*alias)->getCommand(), boList);
218        }
[7771]219        paramBegin = 1;
[7413]220        return (*alias)->getCommand();
221      }
[5779]222    }
[7413]223
[7417]224    // CHECK FOR COMMAND_CLASS
[7413]225    const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]);
226    if (cmdClass != NULL)
227    {
[8350]228      const ShellCommand* retCmd = NULL;
[7417]229      // Function/Command right after Class
[7413]230      if (strings.size() >= 1)
[7414]231      {
[7418]232        // Search for Objects.
[9406]233        retCmd = ShellCommand::getCommand((strings.size() > 1) ? strings[1] : "", cmdClass);
[7417]234        if (retCmd != NULL)
235        {
236          paramBegin = 2;
[7418]237          fillObjectList("", retCmd, boList);
[7417]238          return retCmd;
239        }
[7414]240      }
[7417]241      // Function/Command after Class and 'Object'
[7413]242      if (retCmd == NULL && strings.size() >= 2)
[7414]243      {
[9406]244        retCmd = ShellCommand::getCommand((strings.size() > 2) ? strings[2] : "", cmdClass);
[7417]245        if (retCmd != NULL)
246        {
[7415]247          paramBegin = 3;
[7418]248          fillObjectList(strings[1], retCmd, boList);
[7417]249          return retCmd;
250        }
[7414]251      }
252      if (retCmd != NULL) // check for the paramBegin.
253        return retCmd;
[7413]254    }
[7417]255    // Nothing usefull found at all.
[7414]256    paramBegin = 0;
257    return NULL;
[5113]258  }
259
[7418]260  /**
261   * @brief fills the ObjectList boList with Objects that can be reffered to by cmd.
262   * @param objectDescriptor: the ObjectName (beginning, full name or empty) to fill the List with
263   * @param cmd: The Command to complete Objects for.
264   * @param boList: The List of BaseObject's that will be filled with found entries.
265   * @returns: true if more than one Entry was fond, else (false , or if boList is NULL).
266   */
267  bool ShellCommand::fillObjectList(const std::string& objectDescriptor, const ShellCommand* cmd, std::vector<BaseObject*>* boList)
[7417]268  {
[7418]269    assert (cmd != NULL && cmd->shellClass != NULL);
270    if(boList == NULL)
271      return false;
[7413]272
[9869]273    const ObjectListBase* const objectList = ObjectListBase::getObjectList(cmd->shellClass->getName());
[7417]274    if (objectList != NULL)
275    {
[9869]276      ObjectListBase::base_list list;
277      objectList->getBaseObjectList(&list);
278      ObjectListBase::base_iterator it;
[7417]279
280      // No Description given (only for speedup)
281      if (objectDescriptor.empty())
282      {
[9869]283        for (it = list.begin(); it != list.end(); it++)
284          boList->push_back(*it);
[7417]285      }
286      // some description
287      else
288      {
[9869]289        for (it = list.begin(); it != list.end(); it++)
290          if (!nocaseCmp(objectDescriptor, (*it)->getName(), objectDescriptor.size()))
291            boList->push_back(*it);
[7417]292      }
293    }
[7418]294    return !boList->empty();
[7417]295  }
296
[7408]297  /**
298   * @brief checks if a command has already been registered.
299   * @param commandName the name of the Command
300   * @param className the name of the Class the command should apply to.
301   * @returns true, if the command is registered/false otherwise
302   *
303   * This is used internally, to see, if we have multiple command subscriptions.
304   * This is checked in the registerCommand-function.
305   */
306  bool ShellCommand::exists(const std::string& commandName, const std::string& className)
307  {
308    return (ShellCommand::getCommand(commandName, className) != NULL);
309  }
[5140]310
[7408]311
[7374]312  /**
[7394]313   * @brief executes commands
[7374]314   * @param executionString the string containing the following input
315   * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
316   * @return true on success, false otherwise.
317   */
318  bool ShellCommand::execute(const std::string& executionString)
319  {
320    SubString inputSplits(executionString, SubString::WhiteSpacesWithComma);
[5198]321
[7374]322    // if we do not have any input return
323    if (inputSplits.empty())
324      return false;
[7340]325
[7419]326    unsigned int paramBegin;
[7771]327    const ShellCommand* sc = NULL;
[7419]328    std::vector<BaseObject*> boList;
329    sc = getCommandFromInput(inputSplits, paramBegin, &boList);
330    if (sc != NULL)
331    {
[9869]332
333      if(sc->executor->getType() == Executor<const SubString>::FunctionStatic )
[7420]334      {
[9869]335        PRINT(0)("Static Command '%s' of Class '%s' with parameters\n", sc->getCName(), sc->shellClass->getCName());
336        (*sc->executor)(NULL, inputSplits.subSet(paramBegin));
[7420]337      }
[9869]338      else
339        for(std::vector<BaseObject*>::const_iterator bo = boList.begin(); bo != boList.end(); ++bo)
340        {
341          PRINT(0)("Command '%s' on '%s::%s'\n", sc->getCName(), (*bo)->getClassCName(), (*bo)->getCName());
342          (*sc->executor)((*bo), inputSplits.subSet(paramBegin));
343        }
[7420]344      return true;
[7419]345    }
[7420]346    return false;
[5198]347  }
[5148]348
[7411]349
[7374]350  /**
[7401]351   * @brief lets a command be described
[7374]352   * @param description the description of the Given command
353   */
354  ShellCommand* ShellCommand::describe(const std::string& description)
[7340]355  {
[7374]356    if (this == NULL)
357      return NULL;
[7403]358    this->description = description;
359    return this;
[7340]360  }
[5164]361
[7374]362  /**
[7389]363   * @brief adds an Alias to this Command
[7374]364   * @param alias the name of the Alias to set
365   * @returns itself
366   */
367  ShellCommand* ShellCommand::setAlias(const std::string& alias)
[5196]368  {
[7374]369    if (this == NULL)
370      return NULL;
[5196]371
[7374]372    if (this->alias != NULL)
373    {
[9406]374      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getCName(), this->shellClass->getCName());
[7374]375    }
376    else
377    {
378      ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
379      this->alias = aliasCMD;
380    }
381    return this;
[5196]382  }
[5195]383
[7374]384  /**
385   * @brief set the default values of the executor
386   * @param value0 the first default value
387   * @param value1 the second default value
388   * @param value2 the third default value
389   * @param value3 the fourth default value
390   * @param value4 the fifth default value
391   */
392  ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
393      const MultiType& value2, const MultiType& value3,
394      const MultiType& value4)
395  {
396    if (this == NULL || this->executor == NULL)
397      return NULL;
[5207]398
[7374]399    this->executor->defaultValues(value0, value1, value2, value3, value4);
[5207]400
[7374]401    return this;
[5148]402  }
403
[7412]404  ShellCommand* ShellCommand::completionPlugin(unsigned int parameter, const CompletorPlugin& completorPlugin)
405  {
406    if (this == NULL || this->executor == NULL)
407      return NULL;
408
409    if (parameter >= this->executor->getParamCount())
410    {
411      PRINTF(1)("Parameter %d not inside of valid ParameterCount %d of Command %s::%s\n",
[9406]412                parameter, this->executor->getParamCount(), this->getCName(), this->shellClass->getCName());
[7412]413    }
414    else
415    {
[9869]416      //       if(this->completors[parameter] == NULL)
417      //       delete this->completors[parameter];
418      //       this->completors[parameter] = completorPlugin.clone();
[7412]419    }
420    return this;
421  }
422
[7374]423  /**
[7742]424   * @brief prints a Help string from this Command
[7374]425   */
[7742]426  void ShellCommand::help() const
[5148]427  {
[9406]428    PRINT(0)("%s ", this->getCName());
[5148]429  }
430
[7374]431  /**
[7401]432   * @brief converts a Parameter to a String
[7374]433   * @param parameter the Parameter we have.
434   * @returns the Name of the Parameter at Hand
435   */
[7401]436  const std::string& ShellCommand::paramToString(long parameter)
[7374]437  {
438    return MultiType::MultiTypeToString((MT_Type)parameter);
439  }
440
[7389]441
[7412]442
443  ///////////
444  // ALIAS //
445  ///////////
446
[7397]447  /**
448   * @param aliasName the name of the Alias
449   * @param command the Command, to associate this alias with
450   */
[7389]451  ShellCommandAlias::ShellCommandAlias(const std::string& aliasName, ShellCommand* command)
452  {
453    this->aliasName = aliasName;
454    this->command = command;
455    ShellCommandAlias::aliasList.push_back(this);
456  };
457
458  ShellCommandAlias::~ShellCommandAlias()
459  {
460    std::vector<ShellCommandAlias*>::iterator delA = std::find(aliasList.begin(), aliasList.end(), this);
461    if (delA != aliasList.end())
462      ShellCommandAlias::aliasList.push_back(this);
463
464  }
465
466  std::vector<ShellCommandAlias*> ShellCommandAlias::aliasList;
467  /**
468  * @brief collects the Aliases registered to the ShellCommands
469  * @param stringList a List to paste the Aliases into.
470  * @returns true on success, false otherwise
471   */
[7403]472
[7389]473  bool ShellCommandAlias::getCommandListOfAlias(std::list<std::string>& stringList)
474  {
475    std::vector<ShellCommandAlias*>::iterator alias;
476    for (alias = ShellCommandAlias::aliasList.begin(); alias != ShellCommandAlias::aliasList.end(); alias++)
477      stringList.push_back((*alias)->getName());
478    return true;
479  }
480
481
[5148]482}
Note: See TracBrowser for help on using the repository browser.