Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/shell/shell_command.cc @ 7913

Last change on this file since 7913 was 7340, checked in by bensch, 19 years ago

orxonox/trunk: fixed a ShellCommand-bug

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