Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5178 was 5174, checked in by bensch, 19 years ago

orxonox/trunk: ShellBuffer is extern to Shell now… (But NOT used in Shell yet)

File size: 13.2 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5129]18#include "shell_command.h"
[1853]19
[5072]20#include "list.h"
[5129]21#include "debug.h"
[5113]22#include "class_list.h"
23
24#include "key_names.h"
[5075]25#include <stdarg.h>
26#include <stdio.h>
[5174]27#include <string.h>
[5075]28
[1856]29using namespace std;
[1853]30
[5166]31/**
[5170]32 * creates a new ShellCommandClass
33 * @param className the Name of the command-class to create
34 */
35ShellCommandClass::ShellCommandClass(const char* className)
36{
37  this->className = className;
38  this->classID = CL_NULL;
39  this->commandList = new tList<ShellCommandBase>;
40
41  ShellCommandClass::commandClassList->add(this);
42}
43
44/**
45 * destructs the shellCommandClass again
46 */
47ShellCommandClass::~ShellCommandClass()
48{
49  tIterator<ShellCommandBase>* iterator = this->commandList->getIterator();
50  ShellCommandBase* elem = iterator->firstElement();
51  while(elem != NULL)
52  {
53    delete elem;
54    elem = iterator->nextElement();
55  }
56  delete iterator;
57  delete this->commandList;
58}
59
[5171]60/**
61 * unregisters all Commands that exist
62 */
63void ShellCommandClass::unregisterAllCommands()
64{
65  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
66  ShellCommandClass* elem = iterator->firstElement();
67  while(elem != NULL)
68  {
69    delete elem;
70
71    elem = iterator->nextElement();
72  }
73  delete iterator;
74
75  delete ShellCommandClass::commandClassList;
76  ShellCommandClass::commandClassList = NULL;
77}
78
[5170]79const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
80{
81  if (ShellCommandClass::commandClassList == NULL)
82    initCommandClassList();
83
84  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
85  ShellCommandClass* elem = iterator->firstElement();
86  while(elem != NULL)
87  {
88    if (!strcmp(className, elem->className))
89    {
[5171]90      if (elem->classID == CL_NULL)
91        elem->classID = ClassList::StringToID(className);
92
[5170]93      delete iterator;
94      return elem;
95    }
96    elem = iterator->nextElement();
97  }
98  delete iterator;
99  return NULL;
100}
101
[5172]102/**
103 * searches for a CommandClass
104 * @param className the name of the CommandClass
105 * @returns the CommandClass if found, or a new CommandClass if not
106 */
[5170]107ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
108{
109  if (ShellCommandClass::commandClassList == NULL)
110    initCommandClassList();
111
112  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
113  ShellCommandClass* elem = iterator->firstElement();
114  while(elem != NULL)
115  {
116    if (!strcmp(className, elem->className))
117    {
118      delete iterator;
119      return elem;
120    }
121    elem = iterator->nextElement();
122  }
123  delete iterator;
124  return new ShellCommandClass(className);
125}
126
[5172]127/**
128 * initializes the CommandList (if it is NULL)
129 */
[5170]130void ShellCommandClass::initCommandClassList()
131{
132  if (ShellCommandClass::commandClassList == NULL)
133  {
134    ShellCommandClass::commandClassList = new tList<ShellCommandClass>;
135    ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn);
136  }
137}
138
139tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;
140
141/**
[5166]142 * constructs and registers a new Command
143 * @param commandName the name of the Command
144 * @param className the name of the class to apply this command to
145 * @param paramCount the count of parameters this command takes
146 * @return self
147 */
[5161]148ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)
[3365]149{
[5141]150  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
151  this->setName(commandName);
[5164]152  this->description = NULL;
[5141]153
[5161]154//  this->classID = classID;
[5170]155  ShellCommandClass::getCommandClass(className)->commandList->add(this); //ClassList::IDToString(classID);
[5141]156
[5130]157  // handling parameters, and storing them:
[5142]158  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
159    paramCount = FUNCTOR_MAX_ARGUMENTS;
[5130]160  this->paramCount = paramCount;
[5148]161  this->parameters = new unsigned int[paramCount];
[5130]162
[5148]163  va_list parameterList;
164  va_start(parameterList, paramCount);
165
[5130]166  for (unsigned int i = 0; i < paramCount; i++)
[5142]167  {
[5148]168    this->parameters[i] = va_arg(parameterList, int);
[5130]169
[5146]170    switch (this->parameters[i])
[5142]171    {
172      case ParameterBool:
[5148]173        this->defaultBools[i] = va_arg(parameterList, int);
[5142]174        break;
175      case ParameterChar:
176        this->defaultStrings[i] = new char[2];
[5148]177        sprintf(this->defaultStrings[0], "%c",  va_arg(parameterList, int));
[5142]178        break;
179      case ParameterString:
[5148]180        this->defaultStrings[i] = va_arg(parameterList, char*);
[5142]181        break;
182      case ParameterInt:
[5148]183        this->defaultInts[i] = va_arg(parameterList, int);
[5142]184        break;
185      case ParameterUInt:
[5148]186        this->defaultInts[i] = va_arg(parameterList, unsigned int);
[5142]187        break;
188      case ParameterFloat:
[5148]189        this->defaultFloats[i] = va_arg(parameterList, double);
[5142]190        break;
191      case ParameterLong:
[5148]192        this->defaultInts[i] = va_arg(parameterList, long);
[5142]193        break;
194      default:
195        break;
196    }
197  }
[5068]198}
[4320]199
[5166]200/**
201 * deconstructs a ShellCommand
202 * @return
203 */
[5130]204ShellCommandBase::~ShellCommandBase()
205{
206  delete[] this->parameters;
207}
[1853]208
[5166]209/**
210 * unregister an existing commandName
211 * @param className the name of the Class the command belongs to.
212 * @param commandName the name of the command itself
213 *
214 * @todo implement
215 */
216void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)
[5165]217{
[5171]218  if (ShellCommandClass::commandClassList == NULL)
219    ShellCommandClass::initCommandClassList();
220
[5172]221 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
[5171]222
[5172]223 if (checkClass != NULL)
[5171]224  {
225    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
226    ShellCommandBase* elem = iterator->firstElement();
227    while(elem != NULL)
228    {
229      if (!strcmp(commandName, elem->getName()))
230      {
231        checkClass->commandList->remove(elem);
232        delete elem;
233        break;
234      }
235      elem = iterator->nextElement();
236    }
237    delete iterator;
238
239    if (checkClass->commandList->getSize() == 0)
240    {
241      ShellCommandClass::commandClassList->remove(checkClass);
242      delete checkClass;
243    }
244  }
[5165]245}
246
[5166]247/**
248 * checks if a command has already been registered.
249 * @param commandName the name of the Command
250 * @param className the name of the Class the command should apply to.
251 * @param paramCount how many arguments the Command takes
252 * @returns true, if the command is registered/false otherwise
253 *
254 * This is used internally, to see, if we have multiple command subscriptions.
255 * This is checked in the registerCommand-function.
256 */
[5161]257bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
[5113]258{
[5170]259  if (ShellCommandClass::commandClassList == NULL)
[5072]260  {
[5170]261    ShellCommandClass::initCommandClassList();
[5113]262    return false;
263  }
[5105]264
[5170]265  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
266  if (checkClass != NULL)
[5113]267  {
[5170]268    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
269    ShellCommandBase* elem = iterator->firstElement();
270    while(elem != NULL)
271   {
272     if (!strcmp(commandName, elem->getName()))
273     {
274       PRINTF(2)("Command already registered\n");
275       delete iterator;
276       return true;
277      }
278     elem = iterator->nextElement();
279   }
280   delete iterator;
281   return false;
[5113]282  }
[5170]283  else
284    return false;
[5113]285}
286
[5140]287
[5145]288/**
289 * executes commands
290 * @param executionString the string containing the following input
[5148]291 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
[5145]292 * @return true on success, false otherwise.
293 */
[5135]294bool ShellCommandBase::execute(const char* executionString)
295{
[5170]296//   if (ShellCommandBase::commandList == NULL)
297//     return false;
298//
299//   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
300//   ShellCommandBase* elem = iterator->firstElement();
301//   while(elem != NULL)
302//   {
303//     printf("%s::%s\n", elem->className, elem->getName());
304//     if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
305//          (*(executionString+strlen(elem->className)) == ' ' ||
306//           *(executionString+strlen(elem->className)) == ':' ))
307//     {
308//       const char* commandBegin = executionString + strlen(elem->className);
309//
310//       PRINTF(4)("Class %s matches\n", elem->className);
311//       BaseObject* objectPointer = NULL;
312//       if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
313//       {
314//         while(*commandBegin == ' ')
315//           commandBegin++;
316//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
317//             *(commandBegin + strlen(elem->getName())) != ' ' &&
318//             *(commandBegin + strlen(elem->getName())) != '\0')
319//         {
320//           elem = iterator->nextElement();
321//           continue;
322//         }
323//         PRINTF(4)("Command %s matches\n", elem->getName());
324//         // getting singleton-reference
325//         tList<BaseObject>* list =  ClassList::getList(elem->className);
326//         if (list != NULL)
327//           objectPointer = list->firstElement();
328//       }
329//       else
330//       {
331//         // checking for the Object
332//         while(*commandBegin == ' ')
333//           commandBegin++;
334//         tList<BaseObject>* list = ClassList::getList(elem->className);
335//         if (list == NULL)
336//           break;
337//         tIterator<BaseObject>* iterBO = list->getIterator();
338//         BaseObject* enumBO = iterBO->firstElement();
339//         while(enumBO != NULL)
340//         {
341//           if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
342//           {
343//             PRINTF(4)("Object %s matches\n", enumBO->getName());
344//             objectPointer = enumBO;
345//             break;
346//           }
347//           enumBO = iterBO->nextElement();
348//         }
349//         delete iterBO;
350//
351//         // break on no object Found. We cannot operate on Classes, but on Objects
352//         if (objectPointer == NULL)
353//           break;
354//         commandBegin = commandBegin + strlen(objectPointer->getName());
355//         while(*commandBegin == ' ')
356//           commandBegin++;
357//         // checking for the requested function.
358//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
359//         {
360//           elem = iterator->nextElement();
361//           continue;
362//         }
363//         PRINTF(4)("Function '%s' found\n", commandBegin);
364//       }
365//       const char* paramBegin = strchr(commandBegin, ' ');
366//       if (paramBegin == NULL)
367//         paramBegin = commandBegin + strlen(elem->getName());
368//       while (*paramBegin == ' ')
369//         paramBegin++;
370//
371//       PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
372//       if (objectPointer != NULL && paramBegin != NULL)
373//       {
374//         elem->executeCommand(objectPointer, paramBegin);
375//         delete iterator;
376//         return true;
377//       }
378//     }
379//     elem = iterator->nextElement();
380//   }
381//   delete iterator;
382//   return true;
[5135]383}
[5148]384
[5166]385/**
386 * lets a command be described
387 * @param description the description of the Given command
388 */
[5164]389ShellCommandBase* ShellCommandBase::describe(const char* description)
390{
391  if (this == NULL)
392    return NULL;
[5165]393 else
394 {
395   this->description = description;
396   return this;
397 }
[5164]398}
399
[5166]400/**
401 * see ShellCommandBase::debug()
402 */
[5161]403void ShellCommandBase::debugDyn()
404{
405  this->debug();
406}
[5148]407
[5166]408/**
409 * prints out nice information about the Shells Commands
410 */
[5148]411void ShellCommandBase::debug()
412{
[5170]413  if (ShellCommandClass::commandClassList == NULL)
[5148]414  {
[5171]415    PRINT(0)("No Command registered.\n");
[5148]416    return;
417  }
418
[5170]419  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
420  ShellCommandClass* elemCL = iteratorCL->firstElement();
421  while(elemCL != NULL)
[5148]422  {
[5171]423    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
[5170]424    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
425    const ShellCommandBase* elem = iterator->firstElement();
[5172]426    while(elem != NULL)
[5170]427    {
[5171]428      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
[5170]429      for (unsigned int i = 0; i< elem->paramCount; i++)
430       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
431      if (elem->description != NULL)
432       printf("- %s", elem->description);
433      printf("\n");
[5148]434
[5170]435      elem = iterator->nextElement();
436    }
437    delete iterator;
438    elemCL = iteratorCL->nextElement();
[5148]439  }
[5170]440  delete iteratorCL;
[5148]441}
442
[5166]443/**
444 * converts a Parameter to a String
445 * @param parameter the Parameter we have.
446 * @returns the Name of the Parameter at Hand
447 */
[5148]448const char* ShellCommandBase::paramToString(long parameter)
449{
450  switch (parameter)
451  {
452    case ParameterBool:
453      return "BOOL";
454      break;
455    case ParameterChar:
456      return "CHAR";
457      break;
458    case ParameterString:
459      return "STRING";
460      break;
461    case ParameterInt:
462      return "INT";
463      break;
464    case ParameterUInt:
465      return "UINT";
466      break;
467    case ParameterFloat:
468      return "FLOAT";
469      break;
470    case ParameterLong:
471      return "LONG";
472      break;
473    default:
474      return "NULL";
475      break;
476  }
477}
Note: See TracBrowser for help on using the repository browser.