Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_command_class.cc @ 6484

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

orxonox/trunk: no more segfault in the Shell

File size: 6.7 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
[5639]18#include "shell_command_class.h"
19
[5129]20#include "shell_command.h"
[1853]21
[5129]22#include "debug.h"
[5113]23#include "class_list.h"
[5780]24#include "compiler.h"
[5113]25
[5075]26#include <stdio.h>
[5174]27#include <string.h>
[5075]28
[1856]29using namespace std;
[1853]30
[5779]31std::list<ShellCommandClass*>* ShellCommandClass::commandClassList = NULL;
32std::list<ShellCommandAlias*>* ShellCommandClass::aliasList = NULL;
[5639]33
[5166]34/**
[5170]35 * creates a new ShellCommandClass
36 * @param className the Name of the command-class to create
37 */
38ShellCommandClass::ShellCommandClass(const char* className)
39{
[5188]40  this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
41  this->setName(className);
42
[5170]43  this->className = className;
44  this->classID = CL_NULL;
45
[5779]46  ShellCommandClass::commandClassList->push_back(this);
[5170]47}
48
49/**
50 * destructs the shellCommandClass again
51 */
52ShellCommandClass::~ShellCommandClass()
53{
[5779]54  while(this->commandList.size() > 0)
[5170]55  {
[5779]56    delete this->commandList.front();
57    this->commandList.pop_front();
[5170]58  }
59}
60
[5197]61/**
62 * collects the Commands registered to some class.
63 * @param className the name of the Class to collect the Commands from.
64 * @param stringList a List to paste the Commands into.
65 * @returns true on success, false otherwise
66 */
[5779]67bool ShellCommandClass::getCommandListOfClass(const char* className, std::list<const char*>* stringList)
[5189]68{
[5192]69  if (stringList == NULL || className == NULL)
[5190]70    return false;
71
[5779]72  list<ShellCommandClass*>::iterator elem;
73  for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++)
[5189]74  {
[5779]75    if (!strcmp ((*elem)->getName(), className))
[5189]76    {
[5779]77      list<ShellCommand*>::iterator command;
78      for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++)
79        stringList->push_back((*command)->getName());
[5189]80    }
81  }
[5190]82  return true;
[5189]83}
84
[5197]85/**
86 * collects the Aliases registered to the ShellCommands
87 * @param stringList a List to paste the Aliases into.
88 * @returns true on success, false otherwise
89 */
[5779]90bool ShellCommandClass::getCommandListOfAlias(std::list<const char*>* stringList)
[5195]91{
[5196]92  if (stringList == NULL || ShellCommandClass::aliasList == NULL)
[5195]93    return false;
94
[5779]95  list<ShellCommandAlias*>::iterator alias;
96  for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
97    stringList->push_back((*alias)->getName());
98  return true;
[5195]99}
100
[5171]101/**
102 * unregisters all Commands that exist
103 */
104void ShellCommandClass::unregisterAllCommands()
105{
[5780]106   if (ShellCommandClass::commandClassList != NULL)
[5171]107  {
[5780]108    // unregister all commands and Classes
109    std::list<ShellCommandClass*>::iterator classIT;
110    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
111      delete (*classIT);
[5636]112    delete ShellCommandClass::commandClassList;
113    ShellCommandClass::commandClassList = NULL;
[5171]114  }
115
[5195]116  // unregister all aliases (there should be nothing to do here :))
[5196]117  if (ShellCommandClass::aliasList != NULL)
[5195]118  {
[5780]119    std::list<ShellCommandAlias*>::iterator alias;
120    for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
121      delete (*alias);
[5197]122    delete ShellCommandClass::aliasList;
123    ShellCommandClass::aliasList = NULL;
[5780]124  }
[5171]125}
126
[5197]127/**
128 * checks if a Class is already registered to the Commands' class-stack
129 * @param className the Name of the Class to check for
130 * @returns the CommandClass if found, NULL otherwise
131 */
[5170]132const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
133{
134  if (ShellCommandClass::commandClassList == NULL)
135    initCommandClassList();
136
[5779]137  list<ShellCommandClass*>::const_iterator classIT;
138  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5170]139  {
[5779]140    if (!strcmp(className, (*classIT)->className))
[5170]141    {
[5779]142      if ((*classIT)->classID == CL_NULL)
143        (*classIT)->classID = ClassList::StringToID(className);
[5171]144
[5779]145      return (*classIT);
[5170]146    }
147  }
148  return NULL;
149}
150
[5172]151/**
152 * searches for a CommandClass
153 * @param className the name of the CommandClass
154 * @returns the CommandClass if found, or a new CommandClass if not
155 */
[5170]156ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
157{
158  if (ShellCommandClass::commandClassList == NULL)
159    initCommandClassList();
160
[5779]161  list<ShellCommandClass*>::iterator classIT;
162  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5170]163  {
[5779]164    if (!strcmp(className, (*classIT)->className))
[5170]165    {
[5779]166      return (*classIT);
[5170]167    }
168  }
169  return new ShellCommandClass(className);
170}
171
[5172]172/**
173 * initializes the CommandList (if it is NULL)
174 */
[5170]175void ShellCommandClass::initCommandClassList()
176{
177  if (ShellCommandClass::commandClassList == NULL)
178  {
[5779]179    ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>;
[5641]180    ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug));
[5170]181  }
182}
183
[5644]184/**
185 * displays help about ShellCommandClass
186 * @param className: the Class of Commands to show help about
187 */
[5204]188void ShellCommandClass::help(const char* className)
189{
190  if (className == NULL)
191    return;
192  if (likely(ShellCommandClass::commandClassList != NULL))
193  {
[5779]194    list<ShellCommandClass*>::iterator classIT;
195    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5204]196    {
[5779]197      if ((*classIT)->className && !strcasecmp(className, (*classIT)->className))
[5204]198      {
[5779]199        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className, (*classIT)->commandList.size());
200        list<ShellCommand*>::const_iterator cmdIT;
201        for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
[5204]202        {
[5779]203          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
[5642]204          /// FIXME
205          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
206            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
[5779]207          if ((*cmdIT)->description != NULL)
208            PRINT(0)("- %s", (*cmdIT)->description);
[5204]209          PRINT(0)("\n");
210        }
211        return;
212      }
213    }
214    PRINTF(3)("Class %s not found in Command's classes\n", className);
215  }
216  else
217  {
218    PRINTF(1)("List of commandClasses does not exist");
219  }
220}
221
Note: See TracBrowser for help on using the repository browser.