Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: doxygen-tags

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