Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellBuffer is extern to Shell now… (But NOT used in Shell yet)

File size: 13.2 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
20#include "list.h"
21#include "debug.h"
22#include "class_list.h"
23
24#include "key_names.h"
25#include <stdarg.h>
26#include <stdio.h>
27#include <string.h>
28
29using namespace std;
30
31/**
32 * creates a new ShellCommandClass
33 * @param className the Name of the command-class to create
34 */
35ShellCommandClass::ShellCommandClass(const char* className)
36{
37  this->className = className;
38  this->classID = CL_NULL;
39  this->commandList = new tList<ShellCommandBase>;
40
41  ShellCommandClass::commandClassList->add(this);
42}
43
44/**
45 * destructs the shellCommandClass again
46 */
47ShellCommandClass::~ShellCommandClass()
48{
49  tIterator<ShellCommandBase>* iterator = this->commandList->getIterator();
50  ShellCommandBase* elem = iterator->firstElement();
51  while(elem != NULL)
52  {
53    delete elem;
54    elem = iterator->nextElement();
55  }
56  delete iterator;
57  delete this->commandList;
58}
59
60/**
61 * unregisters all Commands that exist
62 */
63void ShellCommandClass::unregisterAllCommands()
64{
65  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
66  ShellCommandClass* elem = iterator->firstElement();
67  while(elem != NULL)
68  {
69    delete elem;
70
71    elem = iterator->nextElement();
72  }
73  delete iterator;
74
75  delete ShellCommandClass::commandClassList;
76  ShellCommandClass::commandClassList = NULL;
77}
78
79const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
80{
81  if (ShellCommandClass::commandClassList == NULL)
82    initCommandClassList();
83
84  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
85  ShellCommandClass* elem = iterator->firstElement();
86  while(elem != NULL)
87  {
88    if (!strcmp(className, elem->className))
89    {
90      if (elem->classID == CL_NULL)
91        elem->classID = ClassList::StringToID(className);
92
93      delete iterator;
94      return elem;
95    }
96    elem = iterator->nextElement();
97  }
98  delete iterator;
99  return NULL;
100}
101
102/**
103 * searches for a CommandClass
104 * @param className the name of the CommandClass
105 * @returns the CommandClass if found, or a new CommandClass if not
106 */
107ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
108{
109  if (ShellCommandClass::commandClassList == NULL)
110    initCommandClassList();
111
112  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
113  ShellCommandClass* elem = iterator->firstElement();
114  while(elem != NULL)
115  {
116    if (!strcmp(className, elem->className))
117    {
118      delete iterator;
119      return elem;
120    }
121    elem = iterator->nextElement();
122  }
123  delete iterator;
124  return new ShellCommandClass(className);
125}
126
127/**
128 * initializes the CommandList (if it is NULL)
129 */
130void ShellCommandClass::initCommandClassList()
131{
132  if (ShellCommandClass::commandClassList == NULL)
133  {
134    ShellCommandClass::commandClassList = new tList<ShellCommandClass>;
135    ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn);
136  }
137}
138
139tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;
140
141/**
142 * constructs and registers a new Command
143 * @param commandName the name of the Command
144 * @param className the name of the class to apply this command to
145 * @param paramCount the count of parameters this command takes
146 * @return self
147 */
148ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)
149{
150  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
151  this->setName(commandName);
152  this->description = NULL;
153
154//  this->classID = classID;
155  ShellCommandClass::getCommandClass(className)->commandList->add(this); //ClassList::IDToString(classID);
156
157  // handling parameters, and storing them:
158  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
159    paramCount = FUNCTOR_MAX_ARGUMENTS;
160  this->paramCount = paramCount;
161  this->parameters = new unsigned int[paramCount];
162
163  va_list parameterList;
164  va_start(parameterList, paramCount);
165
166  for (unsigned int i = 0; i < paramCount; i++)
167  {
168    this->parameters[i] = va_arg(parameterList, int);
169
170    switch (this->parameters[i])
171    {
172      case ParameterBool:
173        this->defaultBools[i] = va_arg(parameterList, int);
174        break;
175      case ParameterChar:
176        this->defaultStrings[i] = new char[2];
177        sprintf(this->defaultStrings[0], "%c",  va_arg(parameterList, int));
178        break;
179      case ParameterString:
180        this->defaultStrings[i] = va_arg(parameterList, char*);
181        break;
182      case ParameterInt:
183        this->defaultInts[i] = va_arg(parameterList, int);
184        break;
185      case ParameterUInt:
186        this->defaultInts[i] = va_arg(parameterList, unsigned int);
187        break;
188      case ParameterFloat:
189        this->defaultFloats[i] = va_arg(parameterList, double);
190        break;
191      case ParameterLong:
192        this->defaultInts[i] = va_arg(parameterList, long);
193        break;
194      default:
195        break;
196    }
197  }
198}
199
200/**
201 * deconstructs a ShellCommand
202 * @return
203 */
204ShellCommandBase::~ShellCommandBase()
205{
206  delete[] this->parameters;
207}
208
209/**
210 * unregister an existing commandName
211 * @param className the name of the Class the command belongs to.
212 * @param commandName the name of the command itself
213 *
214 * @todo implement
215 */
216void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)
217{
218  if (ShellCommandClass::commandClassList == NULL)
219    ShellCommandClass::initCommandClassList();
220
221 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
222
223 if (checkClass != NULL)
224  {
225    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
226    ShellCommandBase* elem = iterator->firstElement();
227    while(elem != NULL)
228    {
229      if (!strcmp(commandName, elem->getName()))
230      {
231        checkClass->commandList->remove(elem);
232        delete elem;
233        break;
234      }
235      elem = iterator->nextElement();
236    }
237    delete iterator;
238
239    if (checkClass->commandList->getSize() == 0)
240    {
241      ShellCommandClass::commandClassList->remove(checkClass);
242      delete checkClass;
243    }
244  }
245}
246
247/**
248 * checks if a command has already been registered.
249 * @param commandName the name of the Command
250 * @param className the name of the Class the command should apply to.
251 * @param paramCount how many arguments the Command takes
252 * @returns true, if the command is registered/false otherwise
253 *
254 * This is used internally, to see, if we have multiple command subscriptions.
255 * This is checked in the registerCommand-function.
256 */
257bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
258{
259  if (ShellCommandClass::commandClassList == NULL)
260  {
261    ShellCommandClass::initCommandClassList();
262    return false;
263  }
264
265  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
266  if (checkClass != NULL)
267  {
268    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
269    ShellCommandBase* elem = iterator->firstElement();
270    while(elem != NULL)
271   {
272     if (!strcmp(commandName, elem->getName()))
273     {
274       PRINTF(2)("Command already registered\n");
275       delete iterator;
276       return true;
277      }
278     elem = iterator->nextElement();
279   }
280   delete iterator;
281   return false;
282  }
283  else
284    return false;
285}
286
287
288/**
289 * executes commands
290 * @param executionString the string containing the following input
291 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
292 * @return true on success, false otherwise.
293 */
294bool ShellCommandBase::execute(const char* executionString)
295{
296//   if (ShellCommandBase::commandList == NULL)
297//     return false;
298//
299//   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
300//   ShellCommandBase* elem = iterator->firstElement();
301//   while(elem != NULL)
302//   {
303//     printf("%s::%s\n", elem->className, elem->getName());
304//     if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
305//          (*(executionString+strlen(elem->className)) == ' ' ||
306//           *(executionString+strlen(elem->className)) == ':' ))
307//     {
308//       const char* commandBegin = executionString + strlen(elem->className);
309//
310//       PRINTF(4)("Class %s matches\n", elem->className);
311//       BaseObject* objectPointer = NULL;
312//       if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
313//       {
314//         while(*commandBegin == ' ')
315//           commandBegin++;
316//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
317//             *(commandBegin + strlen(elem->getName())) != ' ' &&
318//             *(commandBegin + strlen(elem->getName())) != '\0')
319//         {
320//           elem = iterator->nextElement();
321//           continue;
322//         }
323//         PRINTF(4)("Command %s matches\n", elem->getName());
324//         // getting singleton-reference
325//         tList<BaseObject>* list =  ClassList::getList(elem->className);
326//         if (list != NULL)
327//           objectPointer = list->firstElement();
328//       }
329//       else
330//       {
331//         // checking for the Object
332//         while(*commandBegin == ' ')
333//           commandBegin++;
334//         tList<BaseObject>* list = ClassList::getList(elem->className);
335//         if (list == NULL)
336//           break;
337//         tIterator<BaseObject>* iterBO = list->getIterator();
338//         BaseObject* enumBO = iterBO->firstElement();
339//         while(enumBO != NULL)
340//         {
341//           if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
342//           {
343//             PRINTF(4)("Object %s matches\n", enumBO->getName());
344//             objectPointer = enumBO;
345//             break;
346//           }
347//           enumBO = iterBO->nextElement();
348//         }
349//         delete iterBO;
350//
351//         // break on no object Found. We cannot operate on Classes, but on Objects
352//         if (objectPointer == NULL)
353//           break;
354//         commandBegin = commandBegin + strlen(objectPointer->getName());
355//         while(*commandBegin == ' ')
356//           commandBegin++;
357//         // checking for the requested function.
358//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
359//         {
360//           elem = iterator->nextElement();
361//           continue;
362//         }
363//         PRINTF(4)("Function '%s' found\n", commandBegin);
364//       }
365//       const char* paramBegin = strchr(commandBegin, ' ');
366//       if (paramBegin == NULL)
367//         paramBegin = commandBegin + strlen(elem->getName());
368//       while (*paramBegin == ' ')
369//         paramBegin++;
370//
371//       PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
372//       if (objectPointer != NULL && paramBegin != NULL)
373//       {
374//         elem->executeCommand(objectPointer, paramBegin);
375//         delete iterator;
376//         return true;
377//       }
378//     }
379//     elem = iterator->nextElement();
380//   }
381//   delete iterator;
382//   return true;
383}
384
385/**
386 * lets a command be described
387 * @param description the description of the Given command
388 */
389ShellCommandBase* ShellCommandBase::describe(const char* description)
390{
391  if (this == NULL)
392    return NULL;
393 else
394 {
395   this->description = description;
396   return this;
397 }
398}
399
400/**
401 * see ShellCommandBase::debug()
402 */
403void ShellCommandBase::debugDyn()
404{
405  this->debug();
406}
407
408/**
409 * prints out nice information about the Shells Commands
410 */
411void ShellCommandBase::debug()
412{
413  if (ShellCommandClass::commandClassList == NULL)
414  {
415    PRINT(0)("No Command registered.\n");
416    return;
417  }
418
419  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
420  ShellCommandClass* elemCL = iteratorCL->firstElement();
421  while(elemCL != NULL)
422  {
423    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
424    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
425    const ShellCommandBase* elem = iterator->firstElement();
426    while(elem != NULL)
427    {
428      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
429      for (unsigned int i = 0; i< elem->paramCount; i++)
430       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
431      if (elem->description != NULL)
432       printf("- %s", elem->description);
433      printf("\n");
434
435      elem = iterator->nextElement();
436    }
437    delete iterator;
438    elemCL = iteratorCL->nextElement();
439  }
440  delete iteratorCL;
441}
442
443/**
444 * converts a Parameter to a String
445 * @param parameter the Parameter we have.
446 * @returns the Name of the Parameter at Hand
447 */
448const char* ShellCommandBase::paramToString(long parameter)
449{
450  switch (parameter)
451  {
452    case ParameterBool:
453      return "BOOL";
454      break;
455    case ParameterChar:
456      return "CHAR";
457      break;
458    case ParameterString:
459      return "STRING";
460      break;
461    case ParameterInt:
462      return "INT";
463      break;
464    case ParameterUInt:
465      return "UINT";
466      break;
467    case ParameterFloat:
468      return "FLOAT";
469      break;
470    case ParameterLong:
471      return "LONG";
472      break;
473    default:
474      return "NULL";
475      break;
476  }
477}
Note: See TracBrowser for help on using the repository browser.