Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5170 in orxonox.OLD for trunk/src/lib/shell


Ignore:
Timestamp:
Sep 7, 2005, 10:25:21 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: registration of ShellCommands now via the ShellCommandClass… still a lot of work has to be done.

Location:
trunk/src/lib/shell
Files:
4 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/shell/Makefile.am

    r5160 r5170  
    55
    66libORXshell_a_SOURCES = shell.cc \
    7                         shell_command.cc
     7                        shell_command.cc \
     8                        shell_completion.cc
    89
    910
    1011noinst_HEADERS= shell.h \
    11                 shell_command.h
     12                shell_command.h \
     13                shell_completion.h
  • trunk/src/lib/shell/Makefile.in

    r5160 r5170  
    5454libORXshell_a_AR = $(AR) $(ARFLAGS)
    5555libORXshell_a_LIBADD =
    56 am_libORXshell_a_OBJECTS = shell.$(OBJEXT) shell_command.$(OBJEXT)
     56am_libORXshell_a_OBJECTS = shell.$(OBJEXT) shell_command.$(OBJEXT) \
     57        shell_completion.$(OBJEXT)
    5758libORXshell_a_OBJECTS = $(am_libORXshell_a_OBJECTS)
    5859DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)
     
    6061am__depfiles_maybe = depfiles
    6162@AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/shell.Po \
    62 @AMDEP_TRUE@    ./$(DEPDIR)/shell_command.Po
     63@AMDEP_TRUE@    ./$(DEPDIR)/shell_command.Po \
     64@AMDEP_TRUE@    ./$(DEPDIR)/shell_completion.Po
    6365CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
    6466        $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
     
    183185noinst_LIBRARIES = libORXshell.a
    184186libORXshell_a_SOURCES = shell.cc \
    185                         shell_command.cc
     187                        shell_command.cc \
     188                        shell_completion.cc
    186189
    187190noinst_HEADERS = shell.h \
    188                 shell_command.h
     191                shell_command.h \
     192                shell_completion.h
    189193
    190194all: all-am
     
    237241@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/shell.Po@am__quote@
    238242@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/shell_command.Po@am__quote@
     243@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/shell_completion.Po@am__quote@
    239244
    240245.cc.o:
  • trunk/src/lib/shell/shell_command.cc

    r5166 r5170  
    2727
    2828using namespace std;
     29
     30/**
     31 * creates a new ShellCommandClass
     32 * @param className the Name of the command-class to create
     33 */
     34ShellCommandClass::ShellCommandClass(const char* className)
     35{
     36  this->className = className;
     37  this->classID = CL_NULL;
     38  this->commandList = new tList<ShellCommandBase>;
     39
     40  ShellCommandClass::commandClassList->add(this);
     41}
     42
     43/**
     44 * destructs the shellCommandClass again
     45 */
     46ShellCommandClass::~ShellCommandClass()
     47{
     48  tIterator<ShellCommandBase>* iterator = this->commandList->getIterator();
     49  ShellCommandBase* elem = iterator->firstElement();
     50  while(elem != NULL)
     51  {
     52    delete elem;
     53    elem = iterator->nextElement();
     54  }
     55  delete iterator;
     56  delete this->commandList;
     57}
     58
     59const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
     60{
     61  if (ShellCommandClass::commandClassList == NULL)
     62    initCommandClassList();
     63
     64  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
     65  ShellCommandClass* elem = iterator->firstElement();
     66  while(elem != NULL)
     67  {
     68    if (!strcmp(className, elem->className))
     69    {
     70      delete iterator;
     71      return elem;
     72    }
     73    elem = iterator->nextElement();
     74  }
     75  delete iterator;
     76  return NULL;
     77}
     78
     79ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
     80{
     81  if (ShellCommandClass::commandClassList == NULL)
     82    initCommandClassList();
     83
     84  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
     85  ShellCommandClass* elem = iterator->firstElement();
     86  while(elem != NULL)
     87  {
     88    if (!strcmp(className, elem->className))
     89    {
     90      delete iterator;
     91      return elem;
     92    }
     93    elem = iterator->nextElement();
     94  }
     95  delete iterator;
     96  return new ShellCommandClass(className);
     97}
     98
     99
     100void ShellCommandClass::initCommandClassList()
     101{
     102  if (ShellCommandClass::commandClassList == NULL)
     103  {
     104    ShellCommandClass::commandClassList = new tList<ShellCommandClass>;
     105    ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn);
     106  }
     107}
     108
     109tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;
    29110
    30111/**
     
    42123
    43124//  this->classID = classID;
    44   this->className = className; //ClassList::IDToString(classID);
     125  ShellCommandClass::getCommandClass(className)->commandList->add(this); //ClassList::IDToString(classID);
    45126
    46127  // handling parameters, and storing them:
     
    85166    }
    86167  }
    87 
    88   // adding this ShellCommand to the list of known Commands
    89   ShellCommandBase::commandList->add(this);
    90168}
    91169
     
    104182void ShellCommandBase::unregisterAllCommands()
    105183{
    106   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
    107   ShellCommandBase* elem = iterator->firstElement();
    108   while(elem != NULL)
    109   {
    110     delete elem;
    111 
    112     elem = iterator->nextElement();
    113   }
    114   delete iterator;
    115 
    116   delete ShellCommandBase::commandList;
    117   ShellCommandBase::commandList = NULL;
     184//   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
     185//   ShellCommandBase* elem = iterator->firstElement();
     186//   while(elem != NULL)
     187//   {
     188//     delete elem;
     189//
     190//     elem = iterator->nextElement();
     191//   }
     192//   delete iterator;
     193//
     194//   delete ShellCommandBase::commandList;
     195//   ShellCommandBase::commandList = NULL;
    118196}
    119197
     
    130208}
    131209
    132 tList<ShellCommandBase>* ShellCommandBase::commandList = NULL;
    133210
    134211
     
    145222bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
    146223{
    147   va_list parameterList;
    148   va_start(parameterList, paramCount);
    149 
    150   if (ShellCommandBase::commandList == NULL)
    151   {
    152     ShellCommandBase::commandList = new tList<ShellCommandBase>;
    153     ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn);
     224  if (ShellCommandClass::commandClassList == NULL)
     225  {
     226    ShellCommandClass::initCommandClassList();
    154227    return false;
    155228  }
    156229
    157   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
    158   ShellCommandBase* elem = iterator->firstElement();
    159   while(elem != NULL)
    160   {
    161     if (!strcmp(className, elem->className) && !strcmp(commandName, elem->getName()))
    162     {
    163       PRINTF(2)("Command already registered\n");
    164       delete iterator;
    165       return true;
    166     }
    167     elem = iterator->nextElement();
    168   }
    169   delete iterator;
    170   return false;
     230  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
     231  if (checkClass != NULL)
     232  {
     233    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
     234    ShellCommandBase* elem = iterator->firstElement();
     235    while(elem != NULL)
     236   {
     237     if (!strcmp(commandName, elem->getName()))
     238     {
     239       PRINTF(2)("Command already registered\n");
     240       delete iterator;
     241       return true;
     242      }
     243     elem = iterator->nextElement();
     244   }
     245   delete iterator;
     246   return false;
     247  }
     248  else
     249    return false;
    171250}
    172251
     
    180259bool ShellCommandBase::execute(const char* executionString)
    181260{
    182   if (ShellCommandBase::commandList == NULL)
    183     return false;
    184 
    185   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
    186   ShellCommandBase* elem = iterator->firstElement();
    187   while(elem != NULL)
    188   {
    189     printf("%s::%s\n", elem->className, elem->getName());
    190     if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
    191          (*(executionString+strlen(elem->className)) == ' ' ||
    192           *(executionString+strlen(elem->className)) == ':' ))
    193     {
    194       const char* commandBegin = executionString + strlen(elem->className);
    195 
    196       PRINTF(4)("Class %s matches\n", elem->className);
    197       BaseObject* objectPointer = NULL;
    198       if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
    199       {
    200         while(*commandBegin == ' ')
    201           commandBegin++;
    202         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
    203             *(commandBegin + strlen(elem->getName())) != ' ' &&
    204             *(commandBegin + strlen(elem->getName())) != '\0')
    205         {
    206           elem = iterator->nextElement();
    207           continue;
    208         }
    209         PRINTF(4)("Command %s matches\n", elem->getName());
    210         // getting singleton-reference
    211         tList<BaseObject>* list =  ClassList::getList(elem->className);
    212         if (list != NULL)
    213           objectPointer = list->firstElement();
    214       }
    215       else
    216       {
    217         // checking for the Object
    218         while(*commandBegin == ' ')
    219           commandBegin++;
    220         tList<BaseObject>* list = ClassList::getList(elem->className);
    221         if (list == NULL)
    222           break;
    223         tIterator<BaseObject>* iterBO = list->getIterator();
    224         BaseObject* enumBO = iterBO->firstElement();
    225         while(enumBO != NULL)
    226         {
    227           if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
    228           {
    229             PRINTF(4)("Object %s matches\n", enumBO->getName());
    230             objectPointer = enumBO;
    231             break;
    232           }
    233           enumBO = iterBO->nextElement();
    234         }
    235         delete iterBO;
    236 
    237         // break on no object Found. We cannot operate on Classes, but on Objects
    238         if (objectPointer == NULL)
    239           break;
    240         commandBegin = commandBegin + strlen(objectPointer->getName());
    241         while(*commandBegin == ' ')
    242           commandBegin++;
    243         // checking for the requested function.
    244         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
    245         {
    246           elem = iterator->nextElement();
    247           continue;
    248         }
    249         PRINTF(4)("Function '%s' found\n", commandBegin);
    250       }
    251       const char* paramBegin = strchr(commandBegin, ' ');
    252       if (paramBegin == NULL)
    253         paramBegin = commandBegin + strlen(elem->getName());
    254       while (*paramBegin == ' ')
    255         paramBegin++;
    256 
    257       PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
    258       if (objectPointer != NULL && paramBegin != NULL)
    259       {
    260         elem->executeCommand(objectPointer, paramBegin);
    261         delete iterator;
    262         return true;
    263       }
    264     }
    265     elem = iterator->nextElement();
    266   }
    267   delete iterator;
    268   return true;
     261//   if (ShellCommandBase::commandList == NULL)
     262//     return false;
     263//
     264//   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
     265//   ShellCommandBase* elem = iterator->firstElement();
     266//   while(elem != NULL)
     267//   {
     268//     printf("%s::%s\n", elem->className, elem->getName());
     269//     if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
     270//          (*(executionString+strlen(elem->className)) == ' ' ||
     271//           *(executionString+strlen(elem->className)) == ':' ))
     272//     {
     273//       const char* commandBegin = executionString + strlen(elem->className);
     274//
     275//       PRINTF(4)("Class %s matches\n", elem->className);
     276//       BaseObject* objectPointer = NULL;
     277//       if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
     278//       {
     279//         while(*commandBegin == ' ')
     280//           commandBegin++;
     281//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
     282//             *(commandBegin + strlen(elem->getName())) != ' ' &&
     283//             *(commandBegin + strlen(elem->getName())) != '\0')
     284//         {
     285//           elem = iterator->nextElement();
     286//           continue;
     287//         }
     288//         PRINTF(4)("Command %s matches\n", elem->getName());
     289//         // getting singleton-reference
     290//         tList<BaseObject>* list =  ClassList::getList(elem->className);
     291//         if (list != NULL)
     292//           objectPointer = list->firstElement();
     293//       }
     294//       else
     295//       {
     296//         // checking for the Object
     297//         while(*commandBegin == ' ')
     298//           commandBegin++;
     299//         tList<BaseObject>* list = ClassList::getList(elem->className);
     300//         if (list == NULL)
     301//           break;
     302//         tIterator<BaseObject>* iterBO = list->getIterator();
     303//         BaseObject* enumBO = iterBO->firstElement();
     304//         while(enumBO != NULL)
     305//         {
     306//           if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
     307//           {
     308//             PRINTF(4)("Object %s matches\n", enumBO->getName());
     309//             objectPointer = enumBO;
     310//             break;
     311//           }
     312//           enumBO = iterBO->nextElement();
     313//         }
     314//         delete iterBO;
     315//
     316//         // break on no object Found. We cannot operate on Classes, but on Objects
     317//         if (objectPointer == NULL)
     318//           break;
     319//         commandBegin = commandBegin + strlen(objectPointer->getName());
     320//         while(*commandBegin == ' ')
     321//           commandBegin++;
     322//         // checking for the requested function.
     323//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
     324//         {
     325//           elem = iterator->nextElement();
     326//           continue;
     327//         }
     328//         PRINTF(4)("Function '%s' found\n", commandBegin);
     329//       }
     330//       const char* paramBegin = strchr(commandBegin, ' ');
     331//       if (paramBegin == NULL)
     332//         paramBegin = commandBegin + strlen(elem->getName());
     333//       while (*paramBegin == ' ')
     334//         paramBegin++;
     335//
     336//       PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
     337//       if (objectPointer != NULL && paramBegin != NULL)
     338//       {
     339//         elem->executeCommand(objectPointer, paramBegin);
     340//         delete iterator;
     341//         return true;
     342//       }
     343//     }
     344//     elem = iterator->nextElement();
     345//   }
     346//   delete iterator;
     347//   return true;
    269348}
    270349
     
    297376void ShellCommandBase::debug()
    298377{
    299   if (ShellCommandBase::commandList == NULL)
     378  if (ShellCommandClass::commandClassList == NULL)
    300379  {
    301380    PRINT(0)("No Command registered so far\n");
     
    303382  }
    304383
    305   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
    306   ShellCommandBase* elem = iterator->firstElement();
    307   while(elem != NULL)
    308   {
    309     PRINT(0)("Class:'%s':command:'%s':params:%d: ", elem->className, elem->getName(), elem->paramCount);
    310     for (unsigned int i = 0; i< elem->paramCount; i++)
    311       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
    312     if (elem->description != NULL)
    313       printf("- %s", elem->description);
    314     printf("\n");
    315 
    316     elem = iterator->nextElement();
    317   }
    318   delete iterator;
     384  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
     385  ShellCommandClass* elemCL = iteratorCL->firstElement();
     386  while(elemCL != NULL)
     387  {
     388    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
     389    const ShellCommandBase* elem = iterator->firstElement();
     390    {
     391      PRINT(0)("Class:'%s' registered: \n", elemCL->className);
     392      PRINT(0)("  command:'%s':params:%d: ", elem->getName(), elem->paramCount);
     393      for (unsigned int i = 0; i< elem->paramCount; i++)
     394       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
     395      if (elem->description != NULL)
     396       printf("- %s", elem->description);
     397      printf("\n");
     398
     399      elem = iterator->nextElement();
     400    }
     401    delete iterator;
     402    elemCL = iteratorCL->nextElement();
     403  }
     404  delete iteratorCL;
    319405}
    320406
  • trunk/src/lib/shell/shell_command.h

    r5166 r5170  
    3535// BASE CLASS //
    3636////////////////
     37class ShellCommandBase;
     38
     39//! A class to hold all Classes that have (once) registered Commands.
     40class ShellCommandClass : public BaseObject
     41{
     42  friend class ShellCommandBase;
     43
     44  public:
     45    static const tList<ShellCommandClass>* getCommandClassList() { return ShellCommandClass::commandClassList; };
     46    static ShellCommandClass* getCommandClass(const char* className);
     47
     48  private:
     49    ShellCommandClass(const char* className);
     50    ~ShellCommandClass();
     51
     52    static const ShellCommandClass* isRegistered(const char* className);
     53    static void initCommandClassList();
     54
     55  private:
     56  const char*                      className;
     57  ClassID                          classID;
     58  tList<ShellCommandBase>*         commandList;
     59  static tList<ShellCommandClass>* commandClassList;                     //!< A list of Classes
     60};
     61
     62
    3763//! a baseClass for all possible ShellCommands
    3864class ShellCommandBase : public BaseObject
    3965{
     66  friend class ShellCommandClass;
    4067  public:
    4168    static bool execute (const char* executionString);
     
    4471
    4572    /** @returns the CommandList of the Shell */
    46     static const tList<ShellCommandBase>* getCommandList() { return ShellCommandBase::commandList; };
    4773
    4874    static void unregisterAllCommands();
     
    74100
    75101  private:
    76 //    long                             classID;             //!< The ID of the Class associated to this Command
    77     const char*                      className;                            //!< The Name of the Class associated to this Command
     102    ShellCommandClass*               shellClass;                           //!< A Pointer to the Shell-Class this Command belongs to.
    78103
    79104    const char*                      description;                          //!< A description for this commnand. (initially NULL). Assigned with (create)->describe("blablabla");
    80 
    81     // STATIC MEMBERS
    82     static tList<ShellCommandBase>*  commandList;                          //!< A list of availiable commands.
    83105};
    84106
  • trunk/src/lib/shell/shell_completion.cc

    r5166 r5170  
    1616//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
    1717
    18 #include "proto_class.h"
     18#include "shell_completion.h"
    1919
    2020using namespace std;
     
    2525 * @todo this constructor is not jet implemented - do it
    2626*/
    27 ProtoClass::ProtoClass ()
     27ShellCompletion::ShellCompletion ()
    2828{
    29   this->setClassID(CL_PROTO_ID, "ProtoClass");
     29// this->setClassID(CL_PROTO_ID, "ProtoClass");
    3030
    3131   /* If you make a new class, what is most probably the case when you write this file
     
    4545 * standard deconstructor
    4646*/
    47 ProtoClass::~ProtoClass ()
     47ShellCompletion::~ShellCompletion ()
    4848{
    4949  // delete what has to be deleted here
  • trunk/src/lib/shell/shell_completion.h

    r5166 r5170  
    11/*!
    2  * @file proto_class.h
    3  * @brief Definition of ...
     2 * @file shell_completion.h
     3 * @brief The Shell Completion Tasks
    44*/
    55
    6 #ifndef _PROTO_CLASS_H
    7 #define _PROTO_CLASS_H
    8 
    9 #include "base_object.h"
     6#ifndef _SHELL_COMPLETION_H
     7#define _SHELL_COMPLETION_H
    108
    119// FORWARD DECLARATION
    1210
    1311
    14 
    1512//! A class for ...
    16 class ProtoClass : public BaseObject {
     13class ShellCompletion {
    1714
    1815 public:
    19   ProtoClass();
    20   virtual ~ProtoClass();
     16  ShellCompletion();
     17  virtual ~ShellCompletion();
    2118
    2219
     
    2522};
    2623
    27 #endif /* _PROTO_CLASS_H */
     24#endif /* _SHELL_COMPLETION_H */
Note: See TracChangeset for help on using the changeset viewer.