Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: stuff, that has been done (removed todo's)

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