Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability.old/src/lib/shell/shell_command.cc @ 10621

Last change on this file since 10621 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
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
17
18#include "shell_command.h"
19#include "shell_command_class.h"
20
21#include "shell_input.h"
22#include "shell.h"
23
24#include "compiler.h"
25#include "helper_functions.h"
26#include "debug.h"
27
28#include "key_names.h"
29
30namespace OrxShell
31{
32  ObjectListDefinition(ShellCommand);
33
34  SHELL_COMMAND(debug, ShellCommandClass, help);
35
36  SHELL_COMMAND(help, ShellInput, help)
37  ->describe("retrieve some help about the input mode")
38  ->setAlias("help");
39
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
63  /**
64   * @brief constructs and registers a new Command
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   */
69  ShellCommand::ShellCommand(const std::string& commandName, const std::string& className, Executor<const SubString>* executor)
70  {
71    PRINTF(4)("create shellcommand '%s' for class '%s'\n", commandName.c_str(), className.c_str());
72    this->registerObject(this, ShellCommand::_objectList);
73    this->setName(commandName);
74
75    // copy the executor:
76    this->executor = executor;
77
78    for (unsigned int i = 0; i < this->executor->getParamCount(); i++)
79      this->completors.push_back(new CompletorDefault(&this->executor->getDefaultValue(i)));
80    this->alias = NULL;
81
82    //  this->classID = classID;
83    this->shellClass = ShellCommandClass::acquireCommandClass(className);
84    assert (this->shellClass != NULL);
85    this->shellClass->registerCommand(this);
86  }
87
88  /**
89   * @brief deconstructs a ShellCommand
90   */
91  ShellCommand::~ShellCommand()
92  {
93    this->shellClass->unregisterCommand(this);
94    if (this->alias != NULL)
95      delete this->alias;
96    while (!this->completors.empty())
97    {
98      delete this->completors.back();
99      this->completors.pop_back();
100    }
101    delete this->executor;
102  }
103
104  /**
105   * @brief registers a new ShellCommand
106   */
107  ShellCommand* ShellCommand::registerCommand(const std::string& commandName, const std::string& className, Executor<const SubString>* executor)
108  {
109    if (ShellCommand::exists(commandName, className))
110    {
111      delete executor;
112      return NULL;
113    }
114    else
115      return new ShellCommand(commandName, className, executor);
116
117  }
118
119  /**
120   * @brief unregister an existing commandName
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  {
126    ShellCommandClass* cmdClass = ShellCommandClass::acquireCommandClass(className);
127    if (cmdClass != NULL)
128    {
129      CmdList::iterator cmd;
130      for (cmd = cmdClass->_commandList.begin(); cmd != cmdClass->_commandList.end(); cmd++)
131        if (commandName == (*cmd)->getName())
132        {
133          delete (*cmd);
134          break;
135        }
136    }
137  }
138
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);
148
149    CmdList::const_iterator elem;
150    for (unsigned int i = 0; i < cmdClass->_commandList.size(); i++)
151    {
152      if (commandName == cmdClass->_commandList[i]->getName())
153        return (cmdClass->_commandList[i]);
154    }
155    return NULL;
156  }
157
158
159  /**
160   * @brief gets a command if it has already been registered.
161   * @param commandName the name of the Command
162   * @param className the name of the Class the command is in.
163   * @returns The Registered Command, or NULL if it does not exist.
164   */
165  const ShellCommand* const ShellCommand::getCommand(const std::string& commandName, const std::string& className)
166  {
167    const ShellCommandClass* checkClass = ShellCommandClass::getCommandClass(className);
168    if (likely(checkClass != NULL))
169      return ShellCommand::getCommand(commandName, checkClass);
170    else
171      return NULL;
172  }
173
174  /**
175   * @brief takes out an InputLine, searching for a Command.
176   * @see const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings)
177   * @param inputLine: the Input to analyse.
178   * @param paramBegin: The begin of the Splitted SubStrings entry of the Parameters section.
179   * @returns: The ShellCommand if found.
180   */
181  const ShellCommand* const ShellCommand::getCommandFromInput(const std::string& inputLine, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
182  {
183    return ShellCommand::getCommandFromInput(SubString(inputLine, SubString::WhiteSpaces), paramBegin, boList);
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   */
192  const ShellCommand* const ShellCommand::getCommandFromInput(const SubString& strings, unsigned int& paramBegin, std::vector<BaseObject*>* boList)
193  {
194    // no input, no Command.
195    if (strings.size() == 0)
196    {
197      paramBegin = 0;
198      return NULL;
199    }
200
201    // CHECK FOR ALIAS
202    std::vector<ShellCommandAlias*>::const_iterator alias;
203    for (alias = ShellCommandAlias::getAliases().begin(); alias != ShellCommandAlias::getAliases().end(); alias++ )
204    {
205      if (strings[0] == (*alias)->getName())
206      {
207        assert ((*alias)->getCommand() != NULL && (*alias)->getCommand()->shellClass != NULL);
208        // Search for Objects.
209        if (strings.size() == 1)
210        {
211          if (fillObjectList("", (*alias)->getCommand(), boList))
212            ;
213        }
214        else
215        {
216          if (!fillObjectList(strings[1], (*alias)->getCommand(), boList))
217            fillObjectList("", (*alias)->getCommand(), boList);
218        }
219        paramBegin = 1;
220        return (*alias)->getCommand();
221      }
222    }
223
224    // CHECK FOR COMMAND_CLASS
225    const ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(strings[0]);
226    if (cmdClass != NULL)
227    {
228      const ShellCommand* retCmd = NULL;
229      // Function/Command right after Class
230      if (strings.size() >= 1)
231      {
232        // Search for Objects.
233        retCmd = ShellCommand::getCommand((strings.size() > 1) ? strings[1] : "", cmdClass);
234        if (retCmd != NULL)
235        {
236          paramBegin = 2;
237          fillObjectList("", retCmd, boList);
238          return retCmd;
239        }
240      }
241      // Function/Command after Class and 'Object'
242      if (retCmd == NULL && strings.size() >= 2)
243      {
244        retCmd = ShellCommand::getCommand((strings.size() > 2) ? strings[2] : "", cmdClass);
245        if (retCmd != NULL)
246        {
247          paramBegin = 3;
248          fillObjectList(strings[1], retCmd, boList);
249          return retCmd;
250        }
251      }
252      if (retCmd != NULL) // check for the paramBegin.
253        return retCmd;
254    }
255    // Nothing usefull found at all.
256    paramBegin = 0;
257    return NULL;
258  }
259
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)
268  {
269    assert (cmd != NULL && cmd->shellClass != NULL);
270    if(boList == NULL)
271      return false;
272
273    const ObjectListBase* const objectList = ObjectListBase::getObjectList(cmd->shellClass->getName());
274    if (objectList != NULL)
275    {
276      ObjectListBase::base_list list;
277      objectList->getBaseObjectList(&list);
278      ObjectListBase::base_iterator it;
279
280      // No Description given (only for speedup)
281      if (objectDescriptor.empty())
282      {
283        for (it = list.begin(); it != list.end(); it++)
284          boList->push_back(*it);
285      }
286      // some description
287      else
288      {
289        for (it = list.begin(); it != list.end(); it++)
290          if (!nocaseCmp(objectDescriptor, (*it)->getName(), objectDescriptor.size()))
291            boList->push_back(*it);
292      }
293    }
294    return !boList->empty();
295  }
296
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  }
310
311
312  /**
313   * @brief executes commands
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);
321
322    // if we do not have any input return
323    if (inputSplits.empty())
324      return false;
325
326    unsigned int paramBegin;
327    const ShellCommand* sc = NULL;
328    std::vector<BaseObject*> boList;
329    sc = getCommandFromInput(inputSplits, paramBegin, &boList);
330    if (sc != NULL)
331    {
332
333      if(sc->executor->getType() == Executor<const SubString>::FunctionStatic )
334      {
335        PRINT(0)("Static Command '%s' of Class '%s' with parameters\n", sc->getCName(), sc->shellClass->getCName());
336        (*sc->executor)(NULL, inputSplits.subSet(paramBegin));
337      }
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        }
344      return true;
345    }
346    return false;
347  }
348
349
350  /**
351   * @brief lets a command be described
352   * @param description the description of the Given command
353   */
354  ShellCommand* ShellCommand::describe(const std::string& description)
355  {
356    if (this == NULL)
357      return NULL;
358    this->description = description;
359    return this;
360  }
361
362  /**
363   * @brief adds an Alias to this Command
364   * @param alias the name of the Alias to set
365   * @returns itself
366   */
367  ShellCommand* ShellCommand::setAlias(const std::string& alias)
368  {
369    if (this == NULL)
370      return NULL;
371
372    if (this->alias != NULL)
373    {
374      PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getCName(), this->shellClass->getCName());
375    }
376    else
377    {
378      ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
379      this->alias = aliasCMD;
380    }
381    return this;
382  }
383
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;
398
399    this->executor->defaultValues(value0, value1, value2, value3, value4);
400
401    return this;
402  }
403
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",
412                parameter, this->executor->getParamCount(), this->getCName(), this->shellClass->getCName());
413    }
414    else
415    {
416      //       if(this->completors[parameter] == NULL)
417      //       delete this->completors[parameter];
418      //       this->completors[parameter] = completorPlugin.clone();
419    }
420    return this;
421  }
422
423  /**
424   * @brief prints a Help string from this Command
425   */
426  void ShellCommand::help() const
427  {
428    PRINT(0)("%s ", this->getCName());
429  }
430
431  /**
432   * @brief converts a Parameter to a String
433   * @param parameter the Parameter we have.
434   * @returns the Name of the Parameter at Hand
435   */
436  const std::string& ShellCommand::paramToString(long parameter)
437  {
438    return MultiType::MultiTypeToString((MT_Type)parameter);
439  }
440
441
442
443  ///////////
444  // ALIAS //
445  ///////////
446
447  /**
448   * @param aliasName the name of the Alias
449   * @param command the Command, to associate this alias with
450   */
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   */
472
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
482}
Note: See TracBrowser for help on using the repository browser.