Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/world_entities/src/lib/shell/shell_command.cc @ 5558

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

orxonox/branches/world_entities: merged the Trunk to the world_entities branche
merged with command
svn merge -r5516:HEAD ../trunk/ world_entities/
no conflicts

File size: 19.4 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
[5197]63/**
64 * collects the Commands registered to some class.
65 * @param className the name of the Class to collect the Commands from.
66 * @param stringList a List to paste the Commands into.
67 * @returns true on success, false otherwise
68 */
[5190]69bool ShellCommandClass::getCommandListOfClass(const char* className, tList<const char>* stringList)
[5189]70{
[5192]71  if (stringList == NULL || className == NULL)
[5190]72    return false;
73
[5189]74  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
75  ShellCommandClass* elem = iterator->firstElement();
76  while(elem != NULL)
77  {
78    if (!strcmp (elem->getName(), className))
79    {
80      tIterator<ShellCommandBase>* itFkt = elem->commandList->getIterator();
81      ShellCommandBase* command = itFkt->firstElement();
82      while (command != NULL)
83      {
[5190]84        stringList->add(command->getName());
[5189]85        command = itFkt->nextElement();
86      }
87      delete itFkt;
88    }
89
90    elem = iterator->nextElement();
91  }
92  delete iterator;
[5190]93  return true;
[5189]94}
95
[5197]96/**
97 * collects the Aliases registered to the ShellCommands
98 * @param stringList a List to paste the Aliases into.
99 * @returns true on success, false otherwise
100 */
[5195]101bool ShellCommandClass::getCommandListOfAlias(tList<const char>* stringList)
102{
[5196]103  if (stringList == NULL || ShellCommandClass::aliasList == NULL)
[5195]104    return false;
105
106  tIterator<ShellCommandAlias>* iterator = ShellCommandClass::aliasList->getIterator();
[5196]107   ShellCommandAlias* elem = iterator->firstElement();
108   while(elem != NULL)
109   {
110     stringList->add(elem->getName());
111     elem = iterator->nextElement();
112   }
113   delete iterator;
114   return true;
[5195]115}
116
[5171]117/**
118 * unregisters all Commands that exist
119 */
120void ShellCommandClass::unregisterAllCommands()
121{
[5195]122  // unregister all commands
[5171]123  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
124  ShellCommandClass* elem = iterator->firstElement();
125  while(elem != NULL)
126  {
127    delete elem;
128
129    elem = iterator->nextElement();
130  }
131  delete iterator;
132
133  delete ShellCommandClass::commandClassList;
134  ShellCommandClass::commandClassList = NULL;
[5195]135
136  // unregister all aliases (there should be nothing to do here :))
[5196]137  if (ShellCommandClass::aliasList != NULL)
[5195]138  {
[5197]139    tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
140    ShellCommandAlias* elemAL = itAL->firstElement();
141    while(elemAL != NULL)
142    {
143      delete elemAL;
144      elemAL = itAL->nextElement();
145    }
146    delete itAL;
147    delete ShellCommandClass::aliasList;
148    ShellCommandClass::aliasList = NULL;
[5195]149  }
[5171]150}
151
[5197]152/**
153 * checks if a Class is already registered to the Commands' class-stack
154 * @param className the Name of the Class to check for
155 * @returns the CommandClass if found, NULL otherwise
156 */
[5170]157const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
158{
159  if (ShellCommandClass::commandClassList == NULL)
160    initCommandClassList();
161
162  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
163  ShellCommandClass* elem = iterator->firstElement();
164  while(elem != NULL)
165  {
166    if (!strcmp(className, elem->className))
167    {
[5171]168      if (elem->classID == CL_NULL)
169        elem->classID = ClassList::StringToID(className);
170
[5170]171      delete iterator;
172      return elem;
173    }
174    elem = iterator->nextElement();
175  }
176  delete iterator;
177  return NULL;
178}
179
[5172]180/**
181 * searches for a CommandClass
182 * @param className the name of the CommandClass
183 * @returns the CommandClass if found, or a new CommandClass if not
184 */
[5170]185ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
186{
187  if (ShellCommandClass::commandClassList == NULL)
188    initCommandClassList();
189
190  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
191  ShellCommandClass* elem = iterator->firstElement();
192  while(elem != NULL)
193  {
194    if (!strcmp(className, elem->className))
195    {
196      delete iterator;
197      return elem;
198    }
199    elem = iterator->nextElement();
200  }
201  delete iterator;
202  return new ShellCommandClass(className);
203}
204
[5172]205/**
206 * initializes the CommandList (if it is NULL)
207 */
[5170]208void ShellCommandClass::initCommandClassList()
209{
210  if (ShellCommandClass::commandClassList == NULL)
211  {
212    ShellCommandClass::commandClassList = new tList<ShellCommandClass>;
[5328]213    ShellCommandStatic<ShellCommandBase>::registerCommand("debug", "ShellCommand", ShellCommandBase::debug);
[5170]214  }
215}
216
[5204]217void ShellCommandClass::help(const char* className)
218{
219  if (className == NULL)
220    return;
221  if (likely(ShellCommandClass::commandClassList != NULL))
222  {
223    tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();
224    ShellCommandClass* elemCL = itCL->firstElement();
225    while(elemCL != NULL)
226    {
227      if (elemCL->className && !strcasecmp(className, elemCL->className))
228      {
229        PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
230        tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
231        const ShellCommandBase* elem = iterator->firstElement();
232        while(elem != NULL)
233        {
234          PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
235          for (unsigned int i = 0; i< elem->paramCount; i++)
236            PRINT(0)("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
237          if (elem->description != NULL)
238            PRINT(0)("- %s", elem->description);
239          PRINT(0)("\n");
240          elem = iterator->nextElement();
241        }
242        delete iterator;
243
244        delete itCL;
245        return;
246      }
247      elemCL = itCL->nextElement();
248    }
249    delete itCL;
250    PRINTF(3)("Class %s not found in Command's classes\n", className);
251  }
252  else
253  {
254    PRINTF(1)("List of commandClasses does not exist");
255  }
256}
257
[5170]258tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;
[5195]259tList<ShellCommandAlias>* ShellCommandClass::aliasList = NULL;
[5170]260
[5558]261
262
263
264
265
266
267
268
269
270////////////////////////
271// SHELL COMMAND BASE //
272////////////////////////
[5170]273/**
[5166]274 * constructs and registers a new Command
275 * @param commandName the name of the Command
276 * @param className the name of the class to apply this command to
277 * @param paramCount the count of parameters this command takes
278 */
[5161]279ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)
[3365]280{
[5141]281  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
282  this->setName(commandName);
[5164]283  this->description = NULL;
[5196]284  this->alias = NULL;
[5141]285
[5161]286//  this->classID = classID;
[5198]287  this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);
288  if (this->shellClass != NULL)
289    this->shellClass->commandList->add(this);
[5130]290  // handling parameters, and storing them:
[5142]291  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
292    paramCount = FUNCTOR_MAX_ARGUMENTS;
[5130]293  this->paramCount = paramCount;
[5148]294  this->parameters = new unsigned int[paramCount];
[5558]295  this->defaultValue = new MultiType[paramCount];
[5130]296
[5148]297  va_list parameterList;
298  va_start(parameterList, paramCount);
299
[5558]300  // What Parameters we have got
[5130]301  for (unsigned int i = 0; i < paramCount; i++)
[5148]302    this->parameters[i] = va_arg(parameterList, int);
[5068]303}
[4320]304
[5166]305/**
306 * deconstructs a ShellCommand
307 */
[5130]308ShellCommandBase::~ShellCommandBase()
309{
310  delete[] this->parameters;
[5558]311  delete[] this->defaultValue;
[5196]312  if (this->alias != NULL && ShellCommandClass::aliasList != NULL)
313  {
314    ShellCommandClass::aliasList->remove(this->alias);
315    delete this->alias;
316  }
[5130]317}
[1853]318
[5166]319/**
320 * unregister an existing commandName
321 * @param className the name of the Class the command belongs to.
322 * @param commandName the name of the command itself
323 */
324void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)
[5165]325{
[5171]326  if (ShellCommandClass::commandClassList == NULL)
327    ShellCommandClass::initCommandClassList();
328
[5172]329 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
[5171]330
[5172]331 if (checkClass != NULL)
[5171]332  {
333    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
334    ShellCommandBase* elem = iterator->firstElement();
335    while(elem != NULL)
336    {
337      if (!strcmp(commandName, elem->getName()))
338      {
339        checkClass->commandList->remove(elem);
340        delete elem;
341        break;
342      }
343      elem = iterator->nextElement();
344    }
345    delete iterator;
346
347    if (checkClass->commandList->getSize() == 0)
348    {
349      ShellCommandClass::commandClassList->remove(checkClass);
350      delete checkClass;
351    }
352  }
[5165]353}
354
[5166]355/**
356 * checks if a command has already been registered.
357 * @param commandName the name of the Command
358 * @param className the name of the Class the command should apply to.
359 * @param paramCount how many arguments the Command takes
360 * @returns true, if the command is registered/false otherwise
361 *
362 * This is used internally, to see, if we have multiple command subscriptions.
363 * This is checked in the registerCommand-function.
364 */
[5161]365bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
[5113]366{
[5170]367  if (ShellCommandClass::commandClassList == NULL)
[5072]368  {
[5170]369    ShellCommandClass::initCommandClassList();
[5113]370    return false;
371  }
[5105]372
[5170]373  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
374  if (checkClass != NULL)
[5113]375  {
[5170]376    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
377    ShellCommandBase* elem = iterator->firstElement();
378    while(elem != NULL)
379   {
380     if (!strcmp(commandName, elem->getName()))
381     {
382       PRINTF(2)("Command already registered\n");
383       delete iterator;
384       return true;
385      }
386     elem = iterator->nextElement();
387   }
388   delete iterator;
389   return false;
[5113]390  }
[5170]391  else
392    return false;
[5113]393}
394
[5140]395
[5145]396/**
397 * executes commands
398 * @param executionString the string containing the following input
[5148]399 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
[5145]400 * @return true on success, false otherwise.
401 */
[5135]402bool ShellCommandBase::execute(const char* executionString)
403{
[5198]404  if (ShellCommandClass::commandClassList == NULL)
405    return false;
406
[5203]407  long classID = CL_NULL;                 //< the classID retrieved from the Class.
408  ShellCommandClass* commandClass = NULL; //< the command class this command applies to.
409  tList<BaseObject>* objectList = NULL;   //< the list of Objects stored in classID
410  BaseObject* objectPointer = NULL;       //< a pointer to th Object to Execute the command on
411  bool emptyComplete = false;             //< if the completion input is empty string. e.g ""
412  unsigned int fktPos = 1;                //< the position of the function (needed for finding it)
413//  long completeType = SHELLC_NONE;      //< the Type we'd like to complete.
[5198]414  SubString inputSplits(executionString, true);
415
416  if (inputSplits.getCount() == 0)
417    return false;
418  if (inputSplits.getCount() >= 1)
419  {
[5200]420    // CHECK FOR ALIAS
[5198]421    if (ShellCommandClass::aliasList != NULL)
422    {
423      tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
424      ShellCommandAlias* elemAL = itAL->firstElement();
425      while(elemAL != NULL)
426      {
[5199]427        if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL &&
428            elemAL->getCommand()->shellClass != NULL )
[5198]429        {
[5199]430          objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName());
431          if (objectList != NULL)
432          {
[5204]433            if (inputSplits.getCount() > 1)
434              elemAL->getCommand()->executeCommand(objectList->firstElement(), executionString+inputSplits.getOffset(1));
435            else
436              elemAL->getCommand()->executeCommand(objectList->firstElement(), "");
[5200]437            delete itAL;
[5199]438            return true;
439          }
[5198]440        }
441        elemAL = itAL->nextElement();
442      }
443      delete itAL;
444    }
[5203]445    // looking for a Matching Class
446    if (likely(ShellCommandClass::commandClassList != NULL))
447    {
448      tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();
449      ShellCommandClass* elemCL = itCL->firstElement();
450      while(elemCL != NULL)
451      {
452        if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName()))
453        {
454          //elemCL->getName();
455          classID = ClassList::StringToID(elemCL->getName());
456          commandClass = elemCL;
457          objectList = ClassList::getList(classID);
458          break;
459        }
460        elemCL = itCL->nextElement();
461      }
462      delete itCL;
463    }
[5200]464
[5329]465    if (commandClass != NULL && inputSplits.getCount() >= 2)
[5203]466    {
[5329]467      if (objectList != NULL)
[5203]468      {
[5329]469        // Checking for a Match in the Objects of classID (else take the first)
470        tIterator<BaseObject>* itBO = objectList->getIterator();
471        BaseObject* enumBO = itBO->firstElement();
472        while(enumBO)
[5203]473        {
[5329]474          if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1)))
475          {
476            objectPointer = enumBO;
477            fktPos = 2;
478            break;
479          }
480          enumBO = itBO->nextElement();
481         }
482         delete itBO;
[5203]483
484      //
[5329]485        if (objectPointer == NULL)
486          objectPointer = objectList->firstElement();
487      }
[5203]488      // match a function.
[5329]489      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))
[5203]490      {
491        tIterator<ShellCommandBase>* itCMD = commandClass->commandList->getIterator();
492        ShellCommandBase* enumCMD = itCMD->firstElement();
493        while (enumCMD != NULL)
494        {
495          if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos)))
496          {
[5329]497            if (objectPointer == NULL && enumCMD->functorType == ShellCommand_Objective)
498            {
499              delete itCMD;
500              return false;
501            }
[5203]502            if (inputSplits.getCount() > fktPos+1)
503              enumCMD->executeCommand(objectPointer, executionString+inputSplits.getOffset(fktPos +1));
504            else
505              enumCMD->executeCommand(objectPointer, "");
506            delete itCMD;
507            return true;
508          }
509
510          enumCMD = itCMD->nextElement();
511        }
512        delete itCMD;
513      }
514    }
[5198]515  }
[5135]516}
[5148]517
[5166]518/**
519 * lets a command be described
520 * @param description the description of the Given command
521 */
[5164]522ShellCommandBase* ShellCommandBase::describe(const char* description)
523{
524  if (this == NULL)
525    return NULL;
[5165]526 else
527 {
528   this->description = description;
529   return this;
530 }
[5164]531}
532
[5197]533/**
534 * adds an Alias to this Command
535 * @param alias the name of the Alias to set
536 * @returns itself
537 */
[5195]538ShellCommandBase* ShellCommandBase::setAlias(const char* alias)
539{
[5196]540  if (this == NULL)
541    return NULL;
542
543  if (this->alias != NULL)
544  {
545    PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
546  }
547  else
548  {
549    if (ShellCommandClass::aliasList == NULL)
550      ShellCommandClass::aliasList = new tList<ShellCommandAlias>;
551
552    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
553    ShellCommandClass::aliasList->add(aliasCMD);
554    this->alias = aliasCMD;
555  }
556  return this;
[5195]557}
558
[5166]559/**
[5207]560 * sets default Values of the Commands
561 * @param count how many default Values to set.
562 * @param ... the default Values in order. They will be cast to the right type
563 * @returns itself
564 *
565 * Be aware, that when you use this Function, you !!MUST!! match the input as
566 * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS]
567 */
568ShellCommandBase* ShellCommandBase::defaultValues(unsigned int count, ...)
569{
570  if (this == NULL)
571    return NULL;
572  if (count == 0)
573    return this;
574  if (count > this->paramCount)
575    count = this->paramCount;
576
577  va_list defaultList;
578  va_start(defaultList, count);
579
580  for (unsigned int i = 0; i < count; i++)
581  {
[5558]582
583
[5207]584    switch (this->parameters[i])
585    {
586      case ParameterBool:
[5558]587        this->defaultValue[i].setInt(va_arg(defaultList, int));
[5207]588        break;
589      case ParameterChar:
[5558]590        this->defaultValue[i].setChar((char)va_arg(defaultList, int));
[5207]591        break;
592      case ParameterString:
[5558]593        this->defaultValue[i].setString(va_arg(defaultList, char*));
[5207]594        break;
595      case ParameterInt:
[5558]596        this->defaultValue[i].setInt(va_arg(defaultList, int));
[5207]597        break;
598      case ParameterUInt:
[5558]599        this->defaultValue[i].setInt((int)va_arg(defaultList, unsigned int));
[5207]600        break;
601      case ParameterFloat:
[5558]602        this->defaultValue[i].setFloat(va_arg(defaultList, double));
[5207]603        break;
604      case ParameterLong:
[5558]605        this->defaultValue[i].setInt((int) va_arg(defaultList, long));
[5207]606        break;
607      default:
608        break;
609    }
610  }
611  return this;
612}
613
614/**
[5166]615 * prints out nice information about the Shells Commands
616 */
[5148]617void ShellCommandBase::debug()
618{
[5170]619  if (ShellCommandClass::commandClassList == NULL)
[5148]620  {
[5171]621    PRINT(0)("No Command registered.\n");
[5148]622    return;
623  }
624
[5170]625  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
626  ShellCommandClass* elemCL = iteratorCL->firstElement();
627  while(elemCL != NULL)
[5148]628  {
[5171]629    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
[5170]630    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
631    const ShellCommandBase* elem = iterator->firstElement();
[5172]632    while(elem != NULL)
[5170]633    {
[5171]634      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
[5170]635      for (unsigned int i = 0; i< elem->paramCount; i++)
636       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
637      if (elem->description != NULL)
638       printf("- %s", elem->description);
639      printf("\n");
[5148]640
[5170]641      elem = iterator->nextElement();
642    }
643    delete iterator;
644    elemCL = iteratorCL->nextElement();
[5148]645  }
[5170]646  delete iteratorCL;
[5148]647}
648
[5166]649/**
650 * converts a Parameter to a String
651 * @param parameter the Parameter we have.
652 * @returns the Name of the Parameter at Hand
653 */
[5148]654const char* ShellCommandBase::paramToString(long parameter)
655{
656  switch (parameter)
657  {
658    case ParameterBool:
659      return "BOOL";
660      break;
661    case ParameterChar:
662      return "CHAR";
663      break;
664    case ParameterString:
665      return "STRING";
666      break;
667    case ParameterInt:
668      return "INT";
669      break;
670    case ParameterUInt:
671      return "UINT";
672      break;
673    case ParameterFloat:
674      return "FLOAT";
675      break;
676    case ParameterLong:
677      return "LONG";
678      break;
679    default:
680      return "NULL";
681      break;
682  }
683}
Note: See TracBrowser for help on using the repository browser.