Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: default value can now be supplied as aditional arg of the SHELL_COMMAND-macro

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