Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellCommandAlias should really be completed now

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