Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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