Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cloning the Completors

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