Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6429 was 6222, checked in by bensch, 19 years ago

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

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