Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Another CompletionMode is working

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