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, 18 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
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    ShellCommandStatic<ShellCommandBase>::registerCommand("debug", "ShellCommand", ShellCommandBase::debug);
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
263
264
265
266
267
268
269
270////////////////////////
271// SHELL COMMAND BASE //
272////////////////////////
273/**
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 */
280ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)
281{
282  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
283  this->setName(commandName);
284  this->description = NULL;
285  this->alias = NULL;
286
287//  this->classID = classID;
288  this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);
289  if (this->shellClass != NULL)
290    this->shellClass->commandList->add(this);
291  // handling parameters, and storing them:
292  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
293    paramCount = FUNCTOR_MAX_ARGUMENTS;
294  this->paramCount = paramCount;
295  this->parameters = new unsigned int[paramCount];
296  this->defaultValue = new MultiType[paramCount];
297
298  va_list parameterList;
299  va_start(parameterList, paramCount);
300
301  // What Parameters we have got
302  for (unsigned int i = 0; i < paramCount; i++)
303    this->parameters[i] = va_arg(parameterList, int);
304}
305
306/**
307 * deconstructs a ShellCommand
308 * @return
309 */
310ShellCommandBase::~ShellCommandBase()
311{
312  delete[] this->parameters;
313  delete[] this->defaultValue;
314  if (this->alias != NULL && ShellCommandClass::aliasList != NULL)
315  {
316    ShellCommandClass::aliasList->remove(this->alias);
317    delete this->alias;
318  }
319}
320
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)
327{
328  if (ShellCommandClass::commandClassList == NULL)
329    ShellCommandClass::initCommandClassList();
330
331 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
332
333 if (checkClass != NULL)
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  }
355}
356
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 */
367bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
368{
369  if (ShellCommandClass::commandClassList == NULL)
370  {
371    ShellCommandClass::initCommandClassList();
372    return false;
373  }
374
375  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
376  if (checkClass != NULL)
377  {
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;
392  }
393  else
394    return false;
395}
396
397
398/**
399 * executes commands
400 * @param executionString the string containing the following input
401 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
402 * @return true on success, false otherwise.
403 */
404#include  "stdlibincl.h"
405bool ShellCommandBase::execute(const char* executionString)
406{
407  if (ShellCommandClass::commandClassList == NULL)
408    return false;
409
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.
417  SubString inputSplits(executionString, true);
418
419  if (inputSplits.getCount() == 0)
420    return false;
421  if (inputSplits.getCount() >= 1)
422  {
423    // CHECK FOR ALIAS
424    if (ShellCommandClass::aliasList != NULL)
425    {
426      tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
427      ShellCommandAlias* elemAL = itAL->firstElement();
428      while(elemAL != NULL)
429      {
430        if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL &&
431            elemAL->getCommand()->shellClass != NULL )
432        {
433          objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName());
434          if (objectList != NULL)
435          {
436            if (inputSplits.getCount() > 1)
437              elemAL->getCommand()->executeCommand(objectList->firstElement(), executionString+inputSplits.getOffset(1));
438            else
439              elemAL->getCommand()->executeCommand(objectList->firstElement(), "");
440            delete itAL;
441            return true;
442          }
443        }
444        elemAL = itAL->nextElement();
445      }
446      delete itAL;
447    }
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    }
467
468    if (commandClass != NULL && inputSplits.getCount() >= 2)
469    {
470      if (objectList != NULL)
471      {
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)
476        {
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;
486
487      //
488        if (objectPointer == NULL)
489          objectPointer = objectList->firstElement();
490      }
491      // match a function.
492      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))
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          {
500            if (objectPointer == NULL && enumCMD->functorType == ShellCommand_Objective)
501            {
502              delete itCMD;
503              return false;
504            }
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    }
518  }
519}
520
521/**
522 * lets a command be described
523 * @param description the description of the Given command
524 */
525ShellCommandBase* ShellCommandBase::describe(const char* description)
526{
527  if (this == NULL)
528    return NULL;
529 else
530 {
531   this->description = description;
532   return this;
533 }
534}
535
536/**
537 * adds an Alias to this Command
538 * @param alias the name of the Alias to set
539 * @returns itself
540 */
541ShellCommandBase* ShellCommandBase::setAlias(const char* alias)
542{
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;
560}
561
562/**
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  {
585
586
587    switch (this->parameters[i])
588    {
589      case ParameterBool:
590        this->defaultValue[i].setInt(va_arg(defaultList, int));
591        break;
592      case ParameterChar:
593        this->defaultValue[i].setChar((char)va_arg(defaultList, int));
594        break;
595      case ParameterString:
596        this->defaultValue[i].setString(va_arg(defaultList, char*));
597        break;
598      case ParameterInt:
599        this->defaultValue[i].setInt(va_arg(defaultList, int));
600        break;
601      case ParameterUInt:
602        this->defaultValue[i].setInt((int)va_arg(defaultList, unsigned int));
603        break;
604      case ParameterFloat:
605        this->defaultValue[i].setFloat(va_arg(defaultList, double));
606        break;
607      case ParameterLong:
608        this->defaultValue[i].setInt((int) va_arg(defaultList, long));
609        break;
610      default:
611        break;
612    }
613  }
614  return this;
615}
616
617/**
618 * prints out nice information about the Shells Commands
619 */
620void ShellCommandBase::debug()
621{
622  if (ShellCommandClass::commandClassList == NULL)
623  {
624    PRINT(0)("No Command registered.\n");
625    return;
626  }
627
628  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
629  ShellCommandClass* elemCL = iteratorCL->firstElement();
630  while(elemCL != NULL)
631  {
632    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
633    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
634    const ShellCommandBase* elem = iterator->firstElement();
635    while(elem != NULL)
636    {
637      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
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");
643
644      elem = iterator->nextElement();
645    }
646    delete iterator;
647    elemCL = iteratorCL->nextElement();
648  }
649  delete iteratorCL;
650}
651
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 */
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.