Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/std/src/lib/shell/shell_command.cc @ 7218

Last change on this file since 7218 was 7218, checked in by bensch, 18 years ago

orxonox/std:: more strings

File size: 11.5 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"
[5639]19#include "shell_command_class.h"
[1853]20
[6222]21#include "compiler.h"
[5129]22#include "debug.h"
[5113]23#include "class_list.h"
24
25#include "key_names.h"
[5075]26#include <stdarg.h>
27#include <stdio.h>
[5174]28#include <string.h>
[5075]29
[1856]30using namespace std;
[1853]31
[5166]32/**
33 * constructs and registers a new Command
34 * @param commandName the name of the Command
35 * @param className the name of the class to apply this command to
36 * @param paramCount the count of parameters this command takes
37 */
[5641]38ShellCommand::ShellCommand(const char* commandName, const char* className, const Executor& executor)
[3365]39{
[5141]40  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
[7199]41  PRINTF(5)("create shellcommand %s %s\n", commandName, className);
[5141]42  this->setName(commandName);
[5164]43  this->description = NULL;
[5196]44  this->alias = NULL;
[5642]45  this->executor = executor.clone();
[7201]46  this->executor->setName(commandName);
[5141]47
[5161]48//  this->classID = classID;
[5198]49  this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);
50  if (this->shellClass != NULL)
[5779]51    this->shellClass->commandList.push_back(this);
[5068]52}
[4320]53
[5166]54/**
55 * deconstructs a ShellCommand
56 */
[5636]57ShellCommand::~ShellCommand()
[5130]58{
[5196]59  if (this->alias != NULL && ShellCommandClass::aliasList != NULL)
60  {
61    ShellCommandClass::aliasList->remove(this->alias);
62    delete this->alias;
63  }
[5641]64  delete this->executor;
[5130]65}
[1853]66
[5166]67/**
[5636]68 * registers a new ShellCommand
69 */
[5641]70ShellCommand* ShellCommand::registerCommand(const char* commandName, const char* className, const Executor& executor)
[5636]71{
[7201]72  if (ShellCommand::isRegistered(commandName, className))
[5637]73    return NULL;
74  else
75    return new ShellCommand(commandName, className, executor);
[5636]76
77}
78
79/**
[5166]80 * unregister an existing commandName
81 * @param className the name of the Class the command belongs to.
82 * @param commandName the name of the command itself
83 */
[5636]84void ShellCommand::unregisterCommand(const char* commandName, const char* className)
[5165]85{
[5779]86  /// FIXME
87/*  if (ShellCommandClass::commandClassList == NULL)
[5171]88    ShellCommandClass::initCommandClassList();
89
[5172]90 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
[5171]91
[5172]92 if (checkClass != NULL)
[5171]93  {
[5779]94    std::list<ShellCommand*>::iterator elem;
95    for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++)
[5171]96    {
[5779]97      if (!strcmp(commandName, (*elem)->getName()))
[5171]98      {
[5779]99        delete (*elem);
100        checkClass->commandList.remove(*elem);
[5171]101        break;
102      }
103    }
104
[5779]105    if (checkClass->commandList->size() == 0)
[5171]106    {
107      ShellCommandClass::commandClassList->remove(checkClass);
108      delete checkClass;
109    }
[5779]110  }*/
[5165]111}
112
[5166]113/**
114 * checks if a command has already been registered.
115 * @param commandName the name of the Command
116 * @param className the name of the Class the command should apply to.
117 * @returns true, if the command is registered/false otherwise
118 *
119 * This is used internally, to see, if we have multiple command subscriptions.
120 * This is checked in the registerCommand-function.
121 */
[7201]122bool ShellCommand::isRegistered(const char* commandName, const char* className)
[5113]123{
[5170]124  if (ShellCommandClass::commandClassList == NULL)
[5072]125  {
[5170]126    ShellCommandClass::initCommandClassList();
[5113]127    return false;
128  }
[5105]129
[5170]130  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
131  if (checkClass != NULL)
[5113]132  {
[5779]133    std::list<ShellCommand*>::const_iterator elem;
134    for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++)
135    {
136      if (!strcmp(commandName, (*elem)->getName()))
137      {
138        PRINTF(2)("Command '%s::%s' already registered\n", className, commandName);
139        return true;
[5170]140      }
[5779]141    }
[5170]142   return false;
[5113]143  }
[5170]144  else
145    return false;
[5113]146}
147
[5140]148
[5145]149/**
150 * executes commands
151 * @param executionString the string containing the following input
[5148]152 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
[5145]153 * @return true on success, false otherwise.
154 */
[7216]155bool ShellCommand::execute(const std::string& executionString)
[5135]156{
[5198]157  if (ShellCommandClass::commandClassList == NULL)
158    return false;
159
[5779]160  long classID = CL_NULL;                      //< the classID retrieved from the Class.
161  ShellCommandClass* commandClass = NULL;      //< the command class this command applies to.
[5885]162  const std::list<BaseObject*>* objectList = NULL;   //< the list of Objects stored in classID
[5779]163  BaseObject* objectPointer = NULL;            //< a pointer to th Object to Execute the command on
164  bool emptyComplete = false;                  //< if the completion input is empty string. e.g ""
165  unsigned int fktPos = 1;                     //< the position of the function (needed for finding it)
166//  long completeType = SHELLC_NONE;           //< the Type we'd like to complete.
[5656]167  SubString inputSplits(executionString, " \t\n,");
[5198]168
169  if (inputSplits.getCount() == 0)
170    return false;
171  if (inputSplits.getCount() >= 1)
172  {
[5200]173    // CHECK FOR ALIAS
[5198]174    if (ShellCommandClass::aliasList != NULL)
175    {
[5779]176      list<ShellCommandAlias*>::iterator alias;
177      for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++ )
[5198]178      {
[7211]179        if ((*alias)->getName() != NULL && inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
[5779]180            (*alias)->getCommand()->shellClass != NULL )
[5198]181        {
[5779]182          objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
[5199]183          if (objectList != NULL)
184          {
[5204]185            if (inputSplits.getCount() > 1)
[7216]186            {
187
[7218]188              (*alias)->getCommand()->executor->execute(objectList->front(), executionString.substr(inputSplits.getOffset(1))); /// TODO CHECK IF OK
[7216]189            }
[5204]190            else
[5779]191              (*alias)->getCommand()->executor->execute(objectList->front(), "");
192           return true;
[5199]193          }
[5198]194        }
195      }
196    }
[5203]197    // looking for a Matching Class
198    if (likely(ShellCommandClass::commandClassList != NULL))
199    {
[5779]200      list<ShellCommandClass*>::iterator commandClassIT;
201      for (commandClassIT = ShellCommandClass::commandClassList->begin(); commandClassIT != ShellCommandClass::commandClassList->end(); commandClassIT++)
[5203]202      {
[7211]203        if ((*commandClassIT)->getName() && inputSplits.getString(0) == (*commandClassIT)->getName())
[5203]204        {
205          //elemCL->getName();
[5779]206          classID = ClassList::StringToID((*commandClassIT)->getName());
207          commandClass = (*commandClassIT);
[5791]208          objectList = ClassList::getList((ClassID)classID);
[5203]209          break;
210        }
211      }
212    }
[5200]213
[5329]214    if (commandClass != NULL && inputSplits.getCount() >= 2)
[5203]215    {
[5329]216      if (objectList != NULL)
[5203]217      {
[5329]218        // Checking for a Match in the Objects of classID (else take the first)
[5779]219        list<BaseObject*>::const_iterator object;
220        for (object = objectList->begin(); object != objectList->end(); object++)
[5203]221        {
[7211]222          if ((*object)->getName() != NULL && inputSplits.getString(1) == (*object)->getName() )
[5329]223          {
[5779]224            objectPointer = (*object);
[5329]225            fktPos = 2;
226            break;
227          }
228         }
[5203]229
230      //
[5329]231        if (objectPointer == NULL)
[5779]232          objectPointer = objectList->front();
[5329]233      }
[5203]234      // match a function.
[5329]235      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))
[5203]236      {
[5779]237        list<ShellCommand*>::iterator cmdIT;
238        for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
[5203]239        {
[7211]240          if (inputSplits.getString(fktPos) == (*cmdIT)->getName())
[5203]241          {
[5779]242            if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
[5329]243              return false;
[5203]244            if (inputSplits.getCount() > fktPos+1)
[7218]245              (*cmdIT)->executor->execute(objectPointer, executionString.substr(inputSplits.getOffset(fktPos +1))); /// TODO CHECK IF OK
[5203]246            else
[5779]247              (*cmdIT)->executor->execute(objectPointer, "");
[5203]248            return true;
249          }
250        }
251      }
252    }
[5198]253  }
[5135]254}
[5148]255
[5166]256/**
257 * lets a command be described
258 * @param description the description of the Given command
259 */
[5636]260ShellCommand* ShellCommand::describe(const char* description)
[5164]261{
262  if (this == NULL)
263    return NULL;
[5165]264 else
265 {
266   this->description = description;
267   return this;
268 }
[5164]269}
270
[5197]271/**
272 * adds an Alias to this Command
273 * @param alias the name of the Alias to set
274 * @returns itself
275 */
[5636]276ShellCommand* ShellCommand::setAlias(const char* alias)
[5195]277{
[5196]278  if (this == NULL)
279    return NULL;
280
281  if (this->alias != NULL)
282  {
283    PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
284  }
285  else
286  {
287    if (ShellCommandClass::aliasList == NULL)
[5779]288      ShellCommandClass::aliasList = new std::list<ShellCommandAlias*>;
[5196]289
290    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
[5779]291    ShellCommandClass::aliasList->push_back(aliasCMD);
[5196]292    this->alias = aliasCMD;
293  }
294  return this;
[5195]295}
296
[5166]297/**
[7198]298 * @brief set the default values of the executor
299 * @param value0 the first default value
300 * @param value1 the second default value
301 * @param value2 the third default value
302 * @param value3 the fourth default value
303 * @param value4 the fifth default value
[5207]304 */
[7198]305ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
306                                          const MultiType& value2, const MultiType& value3,
307                                          const MultiType& value4)
[5207]308{
[7201]309  if (this == NULL || this->executor == NULL)
[5207]310    return NULL;
311
[7198]312  this->executor->defaultValues(value0, value1, value2, value3, value4);
[5207]313
314  return this;
315}
316
317/**
[5166]318 * prints out nice information about the Shells Commands
319 */
[5636]320void ShellCommand::debug()
[5148]321{
[5170]322  if (ShellCommandClass::commandClassList == NULL)
[5148]323  {
[5171]324    PRINT(0)("No Command registered.\n");
[5148]325    return;
326  }
327
[5779]328  list<ShellCommandClass*>::iterator classIT;
329  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5148]330  {
[7216]331    PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
[5779]332
333    list<ShellCommand*>::iterator cmdIT;
334    for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
[5170]335    {
[5779]336      PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
[5642]337      /// FIXME
338      /*      for (unsigned int i = 0; i< elem->paramCount; i++)
339       printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
[5779]340      if ((*cmdIT)->description != NULL)
341       printf("- %s", (*cmdIT)->description);
[5170]342      printf("\n");
[5148]343
[5170]344    }
[5148]345  }
346}
347
[5166]348/**
349 * converts a Parameter to a String
350 * @param parameter the Parameter we have.
351 * @returns the Name of the Parameter at Hand
352 */
[5636]353const char* ShellCommand::paramToString(long parameter)
[5148]354{
[5634]355  return MultiType::MultiTypeToString((MT_Type)parameter);
356// FIXME
357  /*  switch (parameter)
[5148]358  {
359    case ParameterBool:
360      return "BOOL";
361      break;
362    case ParameterChar:
363      return "CHAR";
364      break;
365    case ParameterString:
366      return "STRING";
367      break;
368    case ParameterInt:
369      return "INT";
370      break;
371    case ParameterUInt:
372      return "UINT";
373      break;
374    case ParameterFloat:
375      return "FLOAT";
376      break;
377    case ParameterLong:
378      return "LONG";
379      break;
380    default:
381      return "NULL";
382      break;
[5634]383  }*/
[5148]384}
Note: See TracBrowser for help on using the repository browser.