Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7212 was 7211, checked in by bensch, 19 years ago

orxonox/trunk: new SubString class

File size: 11.4 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 char* 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              (*alias)->getCommand()->executor->execute(objectList->front(), executionString+inputSplits.getOffset(1));
187            else
188              (*alias)->getCommand()->executor->execute(objectList->front(), "");
189           return true;
190          }
191        }
192      }
193    }
194    // looking for a Matching Class
195    if (likely(ShellCommandClass::commandClassList != NULL))
196    {
197      list<ShellCommandClass*>::iterator commandClassIT;
198      for (commandClassIT = ShellCommandClass::commandClassList->begin(); commandClassIT != ShellCommandClass::commandClassList->end(); commandClassIT++)
199      {
200        if ((*commandClassIT)->getName() && inputSplits.getString(0) == (*commandClassIT)->getName())
201        {
202          //elemCL->getName();
203          classID = ClassList::StringToID((*commandClassIT)->getName());
204          commandClass = (*commandClassIT);
205          objectList = ClassList::getList((ClassID)classID);
206          break;
207        }
208      }
209    }
210
211    if (commandClass != NULL && inputSplits.getCount() >= 2)
212    {
213      if (objectList != NULL)
214      {
215        // Checking for a Match in the Objects of classID (else take the first)
216        list<BaseObject*>::const_iterator object;
217        for (object = objectList->begin(); object != objectList->end(); object++)
218        {
219          if ((*object)->getName() != NULL && inputSplits.getString(1) == (*object)->getName() )
220          {
221            objectPointer = (*object);
222            fktPos = 2;
223            break;
224          }
225         }
226
227      //
228        if (objectPointer == NULL)
229          objectPointer = objectList->front();
230      }
231      // match a function.
232      if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))
233      {
234        list<ShellCommand*>::iterator cmdIT;
235        for (cmdIT = commandClass->commandList.begin(); cmdIT != commandClass->commandList.end(); cmdIT++)
236        {
237          if (inputSplits.getString(fktPos) == (*cmdIT)->getName())
238          {
239            if (objectPointer == NULL && (*cmdIT)->executor->getType() & Executor_Objective)
240              return false;
241            if (inputSplits.getCount() > fktPos+1)
242              (*cmdIT)->executor->execute(objectPointer, executionString+inputSplits.getOffset(fktPos +1));
243            else
244              (*cmdIT)->executor->execute(objectPointer, "");
245            return true;
246          }
247        }
248      }
249    }
250  }
251}
252
253/**
254 * lets a command be described
255 * @param description the description of the Given command
256 */
257ShellCommand* ShellCommand::describe(const char* description)
258{
259  if (this == NULL)
260    return NULL;
261 else
262 {
263   this->description = description;
264   return this;
265 }
266}
267
268/**
269 * adds an Alias to this Command
270 * @param alias the name of the Alias to set
271 * @returns itself
272 */
273ShellCommand* ShellCommand::setAlias(const char* alias)
274{
275  if (this == NULL)
276    return NULL;
277
278  if (this->alias != NULL)
279  {
280    PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
281  }
282  else
283  {
284    if (ShellCommandClass::aliasList == NULL)
285      ShellCommandClass::aliasList = new std::list<ShellCommandAlias*>;
286
287    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
288    ShellCommandClass::aliasList->push_back(aliasCMD);
289    this->alias = aliasCMD;
290  }
291  return this;
292}
293
294/**
295 * @brief set the default values of the executor
296 * @param value0 the first default value
297 * @param value1 the second default value
298 * @param value2 the third default value
299 * @param value3 the fourth default value
300 * @param value4 the fifth default value
301 */
302ShellCommand* ShellCommand::defaultValues(const MultiType& value0, const MultiType& value1,
303                                          const MultiType& value2, const MultiType& value3,
304                                          const MultiType& value4)
305{
306  if (this == NULL || this->executor == NULL)
307    return NULL;
308
309  this->executor->defaultValues(value0, value1, value2, value3, value4);
310
311  return this;
312}
313
314/**
315 * prints out nice information about the Shells Commands
316 */
317void ShellCommand::debug()
318{
319  if (ShellCommandClass::commandClassList == NULL)
320  {
321    PRINT(0)("No Command registered.\n");
322    return;
323  }
324
325  list<ShellCommandClass*>::iterator classIT;
326  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
327  {
328    PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className, (*classIT)->commandList.size());
329
330    list<ShellCommand*>::iterator cmdIT;
331    for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
332    {
333      PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
334      /// FIXME
335      /*      for (unsigned int i = 0; i< elem->paramCount; i++)
336       printf("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
337      if ((*cmdIT)->description != NULL)
338       printf("- %s", (*cmdIT)->description);
339      printf("\n");
340
341    }
342  }
343}
344
345/**
346 * converts a Parameter to a String
347 * @param parameter the Parameter we have.
348 * @returns the Name of the Parameter at Hand
349 */
350const char* ShellCommand::paramToString(long parameter)
351{
352  return MultiType::MultiTypeToString((MT_Type)parameter);
353// FIXME
354  /*  switch (parameter)
355  {
356    case ParameterBool:
357      return "BOOL";
358      break;
359    case ParameterChar:
360      return "CHAR";
361      break;
362    case ParameterString:
363      return "STRING";
364      break;
365    case ParameterInt:
366      return "INT";
367      break;
368    case ParameterUInt:
369      return "UINT";
370      break;
371    case ParameterFloat:
372      return "FLOAT";
373      break;
374    case ParameterLong:
375      return "LONG";
376      break;
377    default:
378      return "NULL";
379      break;
380  }*/
381}
Note: See TracBrowser for help on using the repository browser.