Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/qt_gui/src/lib/shell/shell_command.cc @ 7609

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

trunk: work work…

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