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
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#include "shell_command_class.h"
20
21#include "compiler.h"
22#include "debug.h"
23#include "class_list.h"
24
25#include "key_names.h"
26#include <stdarg.h>
27#include <stdio.h>
28#include <string.h>
29
30using namespace std;
31
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 */
38ShellCommand::ShellCommand(const char* commandName, const char* className, const Executor& executor)
39{
40  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
41  PRINTF(5)("create shellcommand %s %s\n", commandName, className);
42  this->setName(commandName);
43  this->description = NULL;
44  this->alias = NULL;
45  this->executor = executor.clone();
46  this->executor->setName(commandName);
47
48//  this->classID = classID;
49  this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);
50  if (this->shellClass != NULL)
51    this->shellClass->commandList.push_back(this);
52}
53
54/**
55 * deconstructs a ShellCommand
56 */
57ShellCommand::~ShellCommand()
58{
59  if (this->alias != NULL && ShellCommandClass::aliasList != NULL)
60  {
61    ShellCommandClass::aliasList->remove(this->alias);
62    delete this->alias;
63  }
64  delete this->executor;
65}
66
67/**
68 * registers a new ShellCommand
69 */
70ShellCommand* ShellCommand::registerCommand(const char* commandName, const char* className, const Executor& executor)
71{
72  if (ShellCommand::isRegistered(commandName, className))
73    return NULL;
74  else
75    return new ShellCommand(commandName, className, executor);
76
77}
78
79/**
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 */
84void ShellCommand::unregisterCommand(const char* commandName, const char* className)
85{
86  /// FIXME
87/*  if (ShellCommandClass::commandClassList == NULL)
88    ShellCommandClass::initCommandClassList();
89
90 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
91
92 if (checkClass != NULL)
93  {
94    std::list<ShellCommand*>::iterator elem;
95    for (elem = checkClass->commandList.begin(); elem != checkClass->commandList.end(); elem++)
96    {
97      if (!strcmp(commandName, (*elem)->getName()))
98      {
99        delete (*elem);
100        checkClass->commandList.remove(*elem);
101        break;
102      }
103    }
104
105    if (checkClass->commandList->size() == 0)
106    {
107      ShellCommandClass::commandClassList->remove(checkClass);
108      delete checkClass;
109    }
110  }*/
111}
112
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 */
122bool ShellCommand::isRegistered(const char* commandName, const char* className)
123{
124  if (ShellCommandClass::commandClassList == NULL)
125  {
126    ShellCommandClass::initCommandClassList();
127    return false;
128  }
129
130  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
131  if (checkClass != NULL)
132  {
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;
140      }
141    }
142   return false;
143  }
144  else
145    return false;
146}
147
148
149/**
150 * executes commands
151 * @param executionString the string containing the following input
152 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
153 * @return true on success, false otherwise.
154 */
155bool ShellCommand::execute(const std::string& executionString)
156{
157  if (ShellCommandClass::commandClassList == NULL)
158    return false;
159
160  long classID = CL_NULL;                      //< the classID retrieved from the Class.
161  ShellCommandClass* commandClass = NULL;      //< the command class this command applies to.
162  const std::list<BaseObject*>* objectList = NULL;   //< the list of Objects stored in classID
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.
167  SubString inputSplits(executionString, " \t\n,");
168
169  if (inputSplits.getCount() == 0)
170    return false;
171  if (inputSplits.getCount() >= 1)
172  {
173    // CHECK FOR ALIAS
174    if (ShellCommandClass::aliasList != NULL)
175    {
176      list<ShellCommandAlias*>::iterator alias;
177      for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++ )
178      {
179        if ((*alias)->getName() != NULL && inputSplits.getString(0) == (*alias)->getName() && (*alias)->getCommand() != NULL &&
180            (*alias)->getCommand()->shellClass != NULL )
181        {
182          objectList = ClassList::getList((*alias)->getCommand()->shellClass->getName());
183          if (objectList != NULL)
184          {
185            if (inputSplits.getCount() > 1)
186            {
187
188              (*alias)->getCommand()->executor->execute(objectList->front(), executionString.substr(inputSplits.getOffset(1))); /// TODO CHECK IF OK
189            }
190            else
191              (*alias)->getCommand()->executor->execute(objectList->front(), "");
192           return true;
193          }
194        }
195      }
196    }
197    // looking for a Matching Class
198    if (likely(ShellCommandClass::commandClassList != NULL))
199    {
200      list<ShellCommandClass*>::iterator commandClassIT;
201      for (commandClassIT = ShellCommandClass::commandClassList->begin(); commandClassIT != ShellCommandClass::commandClassList->end(); commandClassIT++)
202      {
203        if ((*commandClassIT)->getName() && inputSplits.getString(0) == (*commandClassIT)->getName())
204        {
205          //elemCL->getName();
206          classID = ClassList::StringToID((*commandClassIT)->getName());
207          commandClass = (*commandClassIT);
208          objectList = ClassList::getList((ClassID)classID);
209          break;
210        }
211      }
212    }
213
214    if (commandClass != NULL && inputSplits.getCount() >= 2)
215    {
216      if (objectList != NULL)
217      {
218        // Checking for a Match in the Objects of classID (else take the first)
219        list<BaseObject*>::const_iterator object;
220        for (object = objectList->begin(); object != objectList->end(); object++)
221        {
222          if ((*object)->getName() != NULL && inputSplits.getString(1) == (*object)->getName() )
223          {
224            objectPointer = (*object);
225            fktPos = 2;
226            break;
227          }
228         }
229
230      //
231        if (objectPointer == NULL)
232          objectPointer = objectList->front();
233      }
234      // match a function.
235      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))
236      {
237        list<ShellCommand*>::iterator cmdIT;
238        for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
239        {
240          if (inputSplits.getString(fktPos) == (*cmdIT)->getName())
241          {
242            if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
243              return false;
244            if (inputSplits.getCount() > fktPos+1)
245              (*cmdIT)->executor->execute(objectPointer, executionString.substr(inputSplits.getOffset(fktPos +1))); /// TODO CHECK IF OK
246            else
247              (*cmdIT)->executor->execute(objectPointer, "");
248            return true;
249          }
250        }
251      }
252    }
253  }
254}
255
256/**
257 * lets a command be described
258 * @param description the description of the Given command
259 */
260ShellCommand* ShellCommand::describe(const char* description)
261{
262  if (this == NULL)
263    return NULL;
264 else
265 {
266   this->description = description;
267   return this;
268 }
269}
270
271/**
272 * adds an Alias to this Command
273 * @param alias the name of the Alias to set
274 * @returns itself
275 */
276ShellCommand* ShellCommand::setAlias(const char* alias)
277{
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)
288      ShellCommandClass::aliasList = new std::list<ShellCommandAlias*>;
289
290    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
291    ShellCommandClass::aliasList->push_back(aliasCMD);
292    this->alias = aliasCMD;
293  }
294  return this;
295}
296
297/**
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
304 */
305ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
306                                          const MultiType& value2, const MultiType& value3,
307                                          const MultiType& value4)
308{
309  if (this == NULL || this->executor == NULL)
310    return NULL;
311
312  this->executor->defaultValues(value0, value1, value2, value3, value4);
313
314  return this;
315}
316
317/**
318 * prints out nice information about the Shells Commands
319 */
320void ShellCommand::debug()
321{
322  if (ShellCommandClass::commandClassList == NULL)
323  {
324    PRINT(0)("No Command registered.\n");
325    return;
326  }
327
328  list<ShellCommandClass*>::iterator classIT;
329  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
330  {
331    PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
332
333    list<ShellCommand*>::iterator cmdIT;
334    for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
335    {
336      PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
337      /// FIXME
338      /*      for (unsigned int i = 0; i< elem->paramCount; i++)
339       printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
340      if ((*cmdIT)->description != NULL)
341       printf("- %s", (*cmdIT)->description);
342      printf("\n");
343
344    }
345  }
346}
347
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 */
353const char* ShellCommand::paramToString(long parameter)
354{
355  return MultiType::MultiTypeToString((MT_Type)parameter);
356// FIXME
357  /*  switch (parameter)
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;
383  }*/
384}
Note: See TracBrowser for help on using the repository browser.