Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/lib/shell/shell_command.cc @ 6782

Last change on this file since 6782 was 5656, checked in by bensch, 19 years ago

orxonox/trunk: new Version of the String-splitter (without Escape-sequence for the time being)

File size: 11.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_
17
18#include "shell_command.h"
19#include "shell_command_class.h"
20
21#include "list.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 char* commandName, const char* className, const Executor& executor)
39{
40  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
41  this->setName(commandName);
42  this->description = NULL;
43  this->alias = NULL;
44  this->executor = executor.clone();
45
46//  this->classID = classID;
47  this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);
48  if (this->shellClass != NULL)
49    this->shellClass->commandList->add(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 char* commandName, const char* className, const Executor& executor)
69{
70  if (ShellCommand::isRegistered(commandName, className, executor))
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 char* commandName, const char* className)
83{
84  if (ShellCommandClass::commandClassList == NULL)
85    ShellCommandClass::initCommandClassList();
86
87 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
88
89 if (checkClass != NULL)
90  {
91    tIterator<ShellCommand>* iterator = checkClass->commandList->getIterator();
92    ShellCommand* elem = iterator->firstElement();
93    while(elem != NULL)
94    {
95      if (!strcmp(commandName, elem->getName()))
96      {
97        checkClass->commandList->remove(elem);
98        delete elem;
99        break;
100      }
101      elem = iterator->nextElement();
102    }
103    delete iterator;
104
105    if (checkClass->commandList->getSize() == 0)
106    {
107      ShellCommandClass::commandClassList->remove(checkClass);
108      delete checkClass;
109    }
110  }
111}
112
113/**
114 * checks if a command has already been registered.
115 * @param commandName the name of the Command
116 * @param className the name of the Class the command should apply to.
117 * @param paramCount how many arguments the Command takes
118 * @returns true, if the command is registered/false otherwise
119 *
120 * This is used internally, to see, if we have multiple command subscriptions.
121 * This is checked in the registerCommand-function.
122 */
123bool ShellCommand::isRegistered(const char* commandName, const char* className, const Executor& executor)
124{
125  if (ShellCommandClass::commandClassList == NULL)
126  {
127    ShellCommandClass::initCommandClassList();
128    return false;
129  }
130
131  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
132  if (checkClass != NULL)
133  {
134    tIterator<ShellCommand>* iterator = checkClass->commandList->getIterator();
135    ShellCommand* elem = iterator->firstElement();
136    while(elem != NULL)
137   {
138     if (!strcmp(commandName, elem->getName()))
139     {
140       PRINTF(2)("Command already registered\n");
141       delete iterator;
142       return true;
143      }
144     elem = iterator->nextElement();
145   }
146   delete iterator;
147   return false;
148  }
149  else
150    return false;
151}
152
153
154/**
155 * executes commands
156 * @param executionString the string containing the following input
157 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
158 * @return true on success, false otherwise.
159 */
160bool ShellCommand::execute(const char* executionString)
161{
162  if (ShellCommandClass::commandClassList == NULL)
163    return false;
164
165  long classID = CL_NULL;                 //< the classID retrieved from the Class.
166  ShellCommandClass* commandClass = NULL; //< the command class this command applies to.
167  tList<BaseObject>* objectList = NULL;   //< the list of Objects stored in classID
168  BaseObject* objectPointer = NULL;       //< a pointer to th Object to Execute the command on
169  bool emptyComplete = false;             //< if the completion input is empty string. e.g ""
170  unsigned int fktPos = 1;                //< the position of the function (needed for finding it)
171//  long completeType = SHELLC_NONE;      //< the Type we'd like to complete.
172  SubString inputSplits(executionString, " \t\n,");
173
174  if (inputSplits.getCount() == 0)
175    return false;
176  if (inputSplits.getCount() >= 1)
177  {
178    // CHECK FOR ALIAS
179    if (ShellCommandClass::aliasList != NULL)
180    {
181      tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
182      ShellCommandAlias* elemAL = itAL->firstElement();
183      while(elemAL != NULL)
184      {
185        if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL &&
186            elemAL->getCommand()->shellClass != NULL )
187        {
188          objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName());
189          if (objectList != NULL)
190          {
191            if (inputSplits.getCount() > 1)
192              elemAL->getCommand()->executor->execute(objectList->firstElement(), executionString+inputSplits.getOffset(1));
193            else
194              elemAL->getCommand()->executor->execute(objectList->firstElement(), "");
195            delete itAL;
196            return true;
197          }
198        }
199        elemAL = itAL->nextElement();
200      }
201      delete itAL;
202    }
203    // looking for a Matching Class
204    if (likely(ShellCommandClass::commandClassList != NULL))
205    {
206      tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();
207      ShellCommandClass* elemCL = itCL->firstElement();
208      while(elemCL != NULL)
209      {
210        if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName()))
211        {
212          //elemCL->getName();
213          classID = ClassList::StringToID(elemCL->getName());
214          commandClass = elemCL;
215          objectList = ClassList::getList(classID);
216          break;
217        }
218        elemCL = itCL->nextElement();
219      }
220      delete itCL;
221    }
222
223    if (commandClass != NULL && inputSplits.getCount() >= 2)
224    {
225      if (objectList != NULL)
226      {
227        // Checking for a Match in the Objects of classID (else take the first)
228        tIterator<BaseObject>* itBO = objectList->getIterator();
229        BaseObject* enumBO = itBO->firstElement();
230        while(enumBO)
231        {
232          if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1)))
233          {
234            objectPointer = enumBO;
235            fktPos = 2;
236            break;
237          }
238          enumBO = itBO->nextElement();
239         }
240         delete itBO;
241
242      //
243        if (objectPointer == NULL)
244          objectPointer = objectList->firstElement();
245      }
246      // match a function.
247      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))
248      {
249        tIterator<ShellCommand>* itCMD = commandClass->commandList->getIterator();
250        ShellCommand* enumCMD = itCMD->firstElement();
251        while (enumCMD != NULL)
252        {
253          if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos)))
254          {
255            if (objectPointer == NULL && enumCMD->executor->getType() & Executor_Objective)
256            {
257              delete itCMD;
258              return false;
259            }
260            if (inputSplits.getCount() > fktPos+1)
261              enumCMD->executor->execute(objectPointer, executionString+inputSplits.getOffset(fktPos +1));
262            else
263              enumCMD->executor->execute(objectPointer, "");
264            delete itCMD;
265            return true;
266          }
267
268          enumCMD = itCMD->nextElement();
269        }
270        delete itCMD;
271      }
272    }
273  }
274}
275
276/**
277 * lets a command be described
278 * @param description the description of the Given command
279 */
280ShellCommand* ShellCommand::describe(const char* description)
281{
282  if (this == NULL)
283    return NULL;
284 else
285 {
286   this->description = description;
287   return this;
288 }
289}
290
291/**
292 * adds an Alias to this Command
293 * @param alias the name of the Alias to set
294 * @returns itself
295 */
296ShellCommand* ShellCommand::setAlias(const char* alias)
297{
298  if (this == NULL)
299    return NULL;
300
301  if (this->alias != NULL)
302  {
303    PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
304  }
305  else
306  {
307    if (ShellCommandClass::aliasList == NULL)
308      ShellCommandClass::aliasList = new tList<ShellCommandAlias>;
309
310    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
311    ShellCommandClass::aliasList->add(aliasCMD);
312    this->alias = aliasCMD;
313  }
314  return this;
315}
316
317/**
318 * sets default Values of the Commands
319 * @param count how many default Values to set.
320 * @param ... the default Values in order. They will be cast to the right type
321 * @returns itself
322 *
323 * Be aware, that when you use this Function, you !!MUST!! match the input as
324 * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS]
325 */
326ShellCommand* ShellCommand::defaultValues(unsigned int count, ...)
327{
328  if (this == NULL)
329    return NULL;
330
331  va_list values;
332  va_start(values, count);
333
334  this->executor->defaultValues(count, values);
335
336  return this;
337}
338
339/**
340 * prints out nice information about the Shells Commands
341 */
342void ShellCommand::debug()
343{
344  if (ShellCommandClass::commandClassList == NULL)
345  {
346    PRINT(0)("No Command registered.\n");
347    return;
348  }
349
350  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
351  ShellCommandClass* elemCL = iteratorCL->firstElement();
352  while(elemCL != NULL)
353  {
354    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
355    tIterator<ShellCommand>* iterator = elemCL->commandList->getIterator();
356    const ShellCommand* elem = iterator->firstElement();
357    while(elem != NULL)
358    {
359      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->executor->getParamCount());
360      /// FIXME
361      /*      for (unsigned int i = 0; i< elem->paramCount; i++)
362       printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
363      if (elem->description != NULL)
364       printf("- %s", elem->description);
365      printf("\n");
366
367      elem = iterator->nextElement();
368    }
369    delete iterator;
370    elemCL = iteratorCL->nextElement();
371  }
372  delete iteratorCL;
373}
374
375/**
376 * converts a Parameter to a String
377 * @param parameter the Parameter we have.
378 * @returns the Name of the Parameter at Hand
379 */
380const char* ShellCommand::paramToString(long parameter)
381{
382  return MultiType::MultiTypeToString((MT_Type)parameter);
383// FIXME
384  /*  switch (parameter)
385  {
386    case ParameterBool:
387      return "BOOL";
388      break;
389    case ParameterChar:
390      return "CHAR";
391      break;
392    case ParameterString:
393      return "STRING";
394      break;
395    case ParameterInt:
396      return "INT";
397      break;
398    case ParameterUInt:
399      return "UINT";
400      break;
401    case ParameterFloat:
402      return "FLOAT";
403      break;
404    case ParameterLong:
405      return "LONG";
406      break;
407    default:
408      return "NULL";
409      break;
410  }*/
411}
Note: See TracBrowser for help on using the repository browser.