Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellCommand now uses MultiType instead of many different strange constellations →
saves space and complexity (a bit complexity at least)

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
[5552]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 * @return self
279 */
[5161]280ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)
[3365]281{
[5141]282  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
283  this->setName(commandName);
[5164]284  this->description = NULL;
[5196]285  this->alias = NULL;
[5141]286
[5161]287//  this->classID = classID;
[5198]288  this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);
289  if (this->shellClass != NULL)
290    this->shellClass->commandList->add(this);
[5130]291  // handling parameters, and storing them:
[5142]292  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
293    paramCount = FUNCTOR_MAX_ARGUMENTS;
[5130]294  this->paramCount = paramCount;
[5148]295  this->parameters = new unsigned int[paramCount];
[5552]296  this->defaultValue = new MultiType[paramCount];
[5130]297
[5148]298  va_list parameterList;
299  va_start(parameterList, paramCount);
300
[5552]301  // What Parameters we have got
[5130]302  for (unsigned int i = 0; i < paramCount; i++)
[5148]303    this->parameters[i] = va_arg(parameterList, int);
[5068]304}
[4320]305
[5166]306/**
307 * deconstructs a ShellCommand
308 * @return
309 */
[5130]310ShellCommandBase::~ShellCommandBase()
311{
312  delete[] this->parameters;
[5552]313  delete[] this->defaultValue;
[5196]314  if (this->alias != NULL && ShellCommandClass::aliasList != NULL)
315  {
316    ShellCommandClass::aliasList->remove(this->alias);
317    delete this->alias;
318  }
[5130]319}
[1853]320
[5166]321/**
322 * unregister an existing commandName
323 * @param className the name of the Class the command belongs to.
324 * @param commandName the name of the command itself
325 */
326void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)
[5165]327{
[5171]328  if (ShellCommandClass::commandClassList == NULL)
329    ShellCommandClass::initCommandClassList();
330
[5172]331 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
[5171]332
[5172]333 if (checkClass != NULL)
[5171]334  {
335    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
336    ShellCommandBase* elem = iterator->firstElement();
337    while(elem != NULL)
338    {
339      if (!strcmp(commandName, elem->getName()))
340      {
341        checkClass->commandList->remove(elem);
342        delete elem;
343        break;
344      }
345      elem = iterator->nextElement();
346    }
347    delete iterator;
348
349    if (checkClass->commandList->getSize() == 0)
350    {
351      ShellCommandClass::commandClassList->remove(checkClass);
352      delete checkClass;
353    }
354  }
[5165]355}
356
[5166]357/**
358 * checks if a command has already been registered.
359 * @param commandName the name of the Command
360 * @param className the name of the Class the command should apply to.
361 * @param paramCount how many arguments the Command takes
362 * @returns true, if the command is registered/false otherwise
363 *
364 * This is used internally, to see, if we have multiple command subscriptions.
365 * This is checked in the registerCommand-function.
366 */
[5161]367bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
[5113]368{
[5170]369  if (ShellCommandClass::commandClassList == NULL)
[5072]370  {
[5170]371    ShellCommandClass::initCommandClassList();
[5113]372    return false;
373  }
[5105]374
[5170]375  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
376  if (checkClass != NULL)
[5113]377  {
[5170]378    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
379    ShellCommandBase* elem = iterator->firstElement();
380    while(elem != NULL)
381   {
382     if (!strcmp(commandName, elem->getName()))
383     {
384       PRINTF(2)("Command already registered\n");
385       delete iterator;
386       return true;
387      }
388     elem = iterator->nextElement();
389   }
390   delete iterator;
391   return false;
[5113]392  }
[5170]393  else
394    return false;
[5113]395}
396
[5140]397
[5145]398/**
399 * executes commands
400 * @param executionString the string containing the following input
[5148]401 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
[5145]402 * @return true on success, false otherwise.
403 */
[5200]404#include  "stdlibincl.h"
[5135]405bool ShellCommandBase::execute(const char* executionString)
406{
[5198]407  if (ShellCommandClass::commandClassList == NULL)
408    return false;
409
[5203]410  long classID = CL_NULL;                 //< the classID retrieved from the Class.
411  ShellCommandClass* commandClass = NULL; //< the command class this command applies to.
412  tList<BaseObject>* objectList = NULL;   //< the list of Objects stored in classID
413  BaseObject* objectPointer = NULL;       //< a pointer to th Object to Execute the command on
414  bool emptyComplete = false;             //< if the completion input is empty string. e.g ""
415  unsigned int fktPos = 1;                //< the position of the function (needed for finding it)
416//  long completeType = SHELLC_NONE;      //< the Type we'd like to complete.
[5198]417  SubString inputSplits(executionString, true);
418
419  if (inputSplits.getCount() == 0)
420    return false;
421  if (inputSplits.getCount() >= 1)
422  {
[5200]423    // CHECK FOR ALIAS
[5198]424    if (ShellCommandClass::aliasList != NULL)
425    {
426      tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
427      ShellCommandAlias* elemAL = itAL->firstElement();
428      while(elemAL != NULL)
429      {
[5199]430        if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL &&
431            elemAL->getCommand()->shellClass != NULL )
[5198]432        {
[5199]433          objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName());
434          if (objectList != NULL)
435          {
[5204]436            if (inputSplits.getCount() > 1)
437              elemAL->getCommand()->executeCommand(objectList->firstElement(), executionString+inputSplits.getOffset(1));
438            else
439              elemAL->getCommand()->executeCommand(objectList->firstElement(), "");
[5200]440            delete itAL;
[5199]441            return true;
442          }
[5198]443        }
444        elemAL = itAL->nextElement();
445      }
446      delete itAL;
447    }
[5203]448    // looking for a Matching Class
449    if (likely(ShellCommandClass::commandClassList != NULL))
450    {
451      tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();
452      ShellCommandClass* elemCL = itCL->firstElement();
453      while(elemCL != NULL)
454      {
455        if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName()))
456        {
457          //elemCL->getName();
458          classID = ClassList::StringToID(elemCL->getName());
459          commandClass = elemCL;
460          objectList = ClassList::getList(classID);
461          break;
462        }
463        elemCL = itCL->nextElement();
464      }
465      delete itCL;
466    }
[5200]467
[5329]468    if (commandClass != NULL && inputSplits.getCount() >= 2)
[5203]469    {
[5329]470      if (objectList != NULL)
[5203]471      {
[5329]472        // Checking for a Match in the Objects of classID (else take the first)
473        tIterator<BaseObject>* itBO = objectList->getIterator();
474        BaseObject* enumBO = itBO->firstElement();
475        while(enumBO)
[5203]476        {
[5329]477          if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1)))
478          {
479            objectPointer = enumBO;
480            fktPos = 2;
481            break;
482          }
483          enumBO = itBO->nextElement();
484         }
485         delete itBO;
[5203]486
487      //
[5329]488        if (objectPointer == NULL)
489          objectPointer = objectList->firstElement();
490      }
[5203]491      // match a function.
[5329]492      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))
[5203]493      {
494        tIterator<ShellCommandBase>* itCMD = commandClass->commandList->getIterator();
495        ShellCommandBase* enumCMD = itCMD->firstElement();
496        while (enumCMD != NULL)
497        {
498          if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos)))
499          {
[5329]500            if (objectPointer == NULL && enumCMD->functorType == ShellCommand_Objective)
501            {
502              delete itCMD;
503              return false;
504            }
[5203]505            if (inputSplits.getCount() > fktPos+1)
506              enumCMD->executeCommand(objectPointer, executionString+inputSplits.getOffset(fktPos +1));
507            else
508              enumCMD->executeCommand(objectPointer, "");
509            delete itCMD;
510            return true;
511          }
512
513          enumCMD = itCMD->nextElement();
514        }
515        delete itCMD;
516      }
517    }
[5198]518  }
[5135]519}
[5148]520
[5166]521/**
522 * lets a command be described
523 * @param description the description of the Given command
524 */
[5164]525ShellCommandBase* ShellCommandBase::describe(const char* description)
526{
527  if (this == NULL)
528    return NULL;
[5165]529 else
530 {
531   this->description = description;
532   return this;
533 }
[5164]534}
535
[5197]536/**
537 * adds an Alias to this Command
538 * @param alias the name of the Alias to set
539 * @returns itself
540 */
[5195]541ShellCommandBase* ShellCommandBase::setAlias(const char* alias)
542{
[5196]543  if (this == NULL)
544    return NULL;
545
546  if (this->alias != NULL)
547  {
548    PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
549  }
550  else
551  {
552    if (ShellCommandClass::aliasList == NULL)
553      ShellCommandClass::aliasList = new tList<ShellCommandAlias>;
554
555    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
556    ShellCommandClass::aliasList->add(aliasCMD);
557    this->alias = aliasCMD;
558  }
559  return this;
[5195]560}
561
[5166]562/**
[5207]563 * sets default Values of the Commands
564 * @param count how many default Values to set.
565 * @param ... the default Values in order. They will be cast to the right type
566 * @returns itself
567 *
568 * Be aware, that when you use this Function, you !!MUST!! match the input as
569 * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS]
570 */
571ShellCommandBase* ShellCommandBase::defaultValues(unsigned int count, ...)
572{
573  if (this == NULL)
574    return NULL;
575  if (count == 0)
576    return this;
577  if (count > this->paramCount)
578    count = this->paramCount;
579
580  va_list defaultList;
581  va_start(defaultList, count);
582
583  for (unsigned int i = 0; i < count; i++)
584  {
[5552]585
586
[5207]587    switch (this->parameters[i])
588    {
589      case ParameterBool:
[5552]590        this->defaultValue[i].setInt(va_arg(defaultList, int));
[5207]591        break;
592      case ParameterChar:
[5552]593        this->defaultValue[i].setChar((char)va_arg(defaultList, int));
[5207]594        break;
595      case ParameterString:
[5552]596        this->defaultValue[i].setString(va_arg(defaultList, char*));
[5207]597        break;
598      case ParameterInt:
[5552]599        this->defaultValue[i].setInt(va_arg(defaultList, int));
[5207]600        break;
601      case ParameterUInt:
[5552]602        this->defaultValue[i].setInt((int)va_arg(defaultList, unsigned int));
[5207]603        break;
604      case ParameterFloat:
[5552]605        this->defaultValue[i].setFloat(va_arg(defaultList, double));
[5207]606        break;
607      case ParameterLong:
[5552]608        this->defaultValue[i].setInt((int) va_arg(defaultList, long));
[5207]609        break;
610      default:
611        break;
612    }
613  }
614  return this;
615}
616
617/**
[5166]618 * prints out nice information about the Shells Commands
619 */
[5148]620void ShellCommandBase::debug()
621{
[5170]622  if (ShellCommandClass::commandClassList == NULL)
[5148]623  {
[5171]624    PRINT(0)("No Command registered.\n");
[5148]625    return;
626  }
627
[5170]628  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
629  ShellCommandClass* elemCL = iteratorCL->firstElement();
630  while(elemCL != NULL)
[5148]631  {
[5171]632    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
[5170]633    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
634    const ShellCommandBase* elem = iterator->firstElement();
[5172]635    while(elem != NULL)
[5170]636    {
[5171]637      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
[5170]638      for (unsigned int i = 0; i< elem->paramCount; i++)
639       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
640      if (elem->description != NULL)
641       printf("- %s", elem->description);
642      printf("\n");
[5148]643
[5170]644      elem = iterator->nextElement();
645    }
646    delete iterator;
647    elemCL = iteratorCL->nextElement();
[5148]648  }
[5170]649  delete iteratorCL;
[5148]650}
651
[5166]652/**
653 * converts a Parameter to a String
654 * @param parameter the Parameter we have.
655 * @returns the Name of the Parameter at Hand
656 */
[5148]657const char* ShellCommandBase::paramToString(long parameter)
658{
659  switch (parameter)
660  {
661    case ParameterBool:
662      return "BOOL";
663      break;
664    case ParameterChar:
665      return "CHAR";
666      break;
667    case ParameterString:
668      return "STRING";
669      break;
670    case ParameterInt:
671      return "INT";
672      break;
673    case ParameterUInt:
674      return "UINT";
675      break;
676    case ParameterFloat:
677      return "FLOAT";
678      break;
679    case ParameterLong:
680      return "LONG";
681      break;
682    default:
683      return "NULL";
684      break;
685  }
686}
Note: See TracBrowser for help on using the repository browser.