Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: aliases now can also take arguments

File size: 17.9 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  this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);
236  if (this->shellClass != NULL)
237    this->shellClass->commandList->add(this);
238  // handling parameters, and storing them:
239  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
240    paramCount = FUNCTOR_MAX_ARGUMENTS;
241  this->paramCount = paramCount;
242  this->parameters = new unsigned int[paramCount];
243
244  va_list parameterList;
245  va_start(parameterList, paramCount);
246
247  for (unsigned int i = 0; i < paramCount; i++)
248  {
249    this->parameters[i] = va_arg(parameterList, int);
250
251    switch (this->parameters[i])
252    {
253      case ParameterBool:
254        this->defaultBools[i] = va_arg(parameterList, int);
255        break;
256      case ParameterChar:
257        this->defaultStrings[i] = new char[2];
258        sprintf(this->defaultStrings[0], "%c",  va_arg(parameterList, int));
259        break;
260      case ParameterString:
261        this->defaultStrings[i] = va_arg(parameterList, char*);
262        break;
263      case ParameterInt:
264        this->defaultInts[i] = va_arg(parameterList, int);
265        break;
266      case ParameterUInt:
267        this->defaultInts[i] = va_arg(parameterList, unsigned int);
268        break;
269      case ParameterFloat:
270        this->defaultFloats[i] = va_arg(parameterList, double);
271        break;
272      case ParameterLong:
273        this->defaultInts[i] = va_arg(parameterList, long);
274        break;
275      default:
276        break;
277    }
278  }
279}
280
281/**
282 * deconstructs a ShellCommand
283 * @return
284 */
285ShellCommandBase::~ShellCommandBase()
286{
287  delete[] this->parameters;
288  if (this->alias != NULL && ShellCommandClass::aliasList != NULL)
289  {
290    ShellCommandClass::aliasList->remove(this->alias);
291    delete this->alias;
292  }
293}
294
295/**
296 * unregister an existing commandName
297 * @param className the name of the Class the command belongs to.
298 * @param commandName the name of the command itself
299 *
300 * @todo implement
301 */
302void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)
303{
304  if (ShellCommandClass::commandClassList == NULL)
305    ShellCommandClass::initCommandClassList();
306
307 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
308
309 if (checkClass != NULL)
310  {
311    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
312    ShellCommandBase* elem = iterator->firstElement();
313    while(elem != NULL)
314    {
315      if (!strcmp(commandName, elem->getName()))
316      {
317        checkClass->commandList->remove(elem);
318        delete elem;
319        break;
320      }
321      elem = iterator->nextElement();
322    }
323    delete iterator;
324
325    if (checkClass->commandList->getSize() == 0)
326    {
327      ShellCommandClass::commandClassList->remove(checkClass);
328      delete checkClass;
329    }
330  }
331}
332
333/**
334 * checks if a command has already been registered.
335 * @param commandName the name of the Command
336 * @param className the name of the Class the command should apply to.
337 * @param paramCount how many arguments the Command takes
338 * @returns true, if the command is registered/false otherwise
339 *
340 * This is used internally, to see, if we have multiple command subscriptions.
341 * This is checked in the registerCommand-function.
342 */
343bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
344{
345  if (ShellCommandClass::commandClassList == NULL)
346  {
347    ShellCommandClass::initCommandClassList();
348    return false;
349  }
350
351  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
352  if (checkClass != NULL)
353  {
354    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
355    ShellCommandBase* elem = iterator->firstElement();
356    while(elem != NULL)
357   {
358     if (!strcmp(commandName, elem->getName()))
359     {
360       PRINTF(2)("Command already registered\n");
361       delete iterator;
362       return true;
363      }
364     elem = iterator->nextElement();
365   }
366   delete iterator;
367   return false;
368  }
369  else
370    return false;
371}
372
373
374/**
375 * executes commands
376 * @param executionString the string containing the following input
377 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
378 * @return true on success, false otherwise.
379 */
380#include  "stdlibincl.h"
381bool ShellCommandBase::execute(const char* executionString)
382{
383  if (ShellCommandClass::commandClassList == NULL)
384    return false;
385
386
387  long classID;                    //< the classID retrieved from the Class.
388  tList<BaseObject>* objectList;   //< the list of Objects stored in classID
389  BaseObject* objectPointer;       //< a pointer to th Object to Execute the command on
390  bool emptyComplete = false;      //< if the completion input is empty string. e.g ""
391//  long completeType = SHELLC_NONE; //< the Type we'd like to complete.
392  SubString inputSplits(executionString, true);
393
394  printf("!!!!%s!!!\n", executionString);
395
396  if (inputSplits.getCount() == 0)
397    return false;
398  if (inputSplits.getCount() >= 1)
399  {
400    // CHECK FOR ALIAS
401    if (ShellCommandClass::aliasList != NULL)
402    {
403      tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
404      ShellCommandAlias* elemAL = itAL->firstElement();
405      while(elemAL != NULL)
406      {
407        if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL &&
408            elemAL->getCommand()->shellClass != NULL )
409        {
410          objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName());
411          if (objectList != NULL)
412          {
413            elemAL->getCommand()->executeCommand(objectList->firstElement(), executionString+inputSplits.getOffset(1));
414            delete itAL;
415            return true;
416          }
417        }
418        elemAL = itAL->nextElement();
419      }
420      delete itAL;
421    }
422
423  }
424
425
426   //
427//   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
428//   ShellCommandBase* elem = iterator->firstElement();
429//   while(elem != NULL)
430//   {
431//     printf("%s::%s\n", elem->className, elem->getName());
432//     if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
433//          (*(executionString+strlen(elem->className)) == ' ' ||
434//           *(executionString+strlen(elem->className)) == ':' ))
435//     {
436//       const char* commandBegin = executionString + strlen(elem->className);
437//
438//       PRINTF(4)("Class %s matches\n", elem->className);
439//       BaseObject* objectPointer = NULL;
440//       if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
441//       {
442//         while(*commandBegin == ' ')
443//           commandBegin++;
444//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
445//             *(commandBegin + strlen(elem->getName())) != ' ' &&
446//             *(commandBegin + strlen(elem->getName())) != '\0')
447//         {
448//           elem = iterator->nextElement();
449//           continue;
450//         }
451//         PRINTF(4)("Command %s matches\n", elem->getName());
452//         // getting singleton-reference
453//         tList<BaseObject>* list =  ClassList::getList(elem->className);
454//         if (list != NULL)
455//           objectPointer = list->firstElement();
456//       }
457//       else
458//       {
459//         // checking for the Object
460//         while(*commandBegin == ' ')
461//           commandBegin++;
462//         tList<BaseObject>* list = ClassList::getList(elem->className);
463//         if (list == NULL)
464//           break;
465//         tIterator<BaseObject>* iterBO = list->getIterator();
466//         BaseObject* enumBO = iterBO->firstElement();
467//         while(enumBO != NULL)
468//         {
469//           if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
470//           {
471//             PRINTF(4)("Object %s matches\n", enumBO->getName());
472//             objectPointer = enumBO;
473//             break;
474//           }
475//           enumBO = iterBO->nextElement();
476//         }
477//         delete iterBO;
478//
479//         // break on no object Found. We cannot operate on Classes, but on Objects
480//         if (objectPointer == NULL)
481//           break;
482//         commandBegin = commandBegin + strlen(objectPointer->getName());
483//         while(*commandBegin == ' ')
484//           commandBegin++;
485//         // checking for the requested function.
486//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
487//         {
488//           elem = iterator->nextElement();
489//           continue;
490//         }
491//         PRINTF(4)("Function '%s' found\n", commandBegin);
492//       }
493//       const char* paramBegin = strchr(commandBegin, ' ');
494//       if (paramBegin == NULL)
495//         paramBegin = commandBegin + strlen(elem->getName());
496//       while (*paramBegin == ' ')
497//         paramBegin++;
498//
499//       PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
500//       if (objectPointer != NULL && paramBegin != NULL)
501//       {
502//         elem->executeCommand(objectPointer, paramBegin);
503//         delete iterator;
504//         return true;
505//       }
506//     }
507//     elem = iterator->nextElement();
508//   }
509//   delete iterator;
510//   return true;
511}
512
513/**
514 * lets a command be described
515 * @param description the description of the Given command
516 */
517ShellCommandBase* ShellCommandBase::describe(const char* description)
518{
519  if (this == NULL)
520    return NULL;
521 else
522 {
523   this->description = description;
524   return this;
525 }
526}
527
528/**
529 * adds an Alias to this Command
530 * @param alias the name of the Alias to set
531 * @returns itself
532 */
533ShellCommandBase* ShellCommandBase::setAlias(const char* alias)
534{
535  if (this == NULL)
536    return NULL;
537
538  if (this->alias != NULL)
539  {
540    PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
541  }
542  else
543  {
544    if (ShellCommandClass::aliasList == NULL)
545      ShellCommandClass::aliasList = new tList<ShellCommandAlias>;
546
547    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
548    ShellCommandClass::aliasList->add(aliasCMD);
549    this->alias = aliasCMD;
550  }
551  return this;
552}
553
554/**
555 * see ShellCommandBase::debug()
556 */
557void ShellCommandBase::debugDyn()
558{
559  this->debug();
560}
561
562/**
563 * prints out nice information about the Shells Commands
564 */
565void ShellCommandBase::debug()
566{
567  if (ShellCommandClass::commandClassList == NULL)
568  {
569    PRINT(0)("No Command registered.\n");
570    return;
571  }
572
573  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
574  ShellCommandClass* elemCL = iteratorCL->firstElement();
575  while(elemCL != NULL)
576  {
577    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
578    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
579    const ShellCommandBase* elem = iterator->firstElement();
580    while(elem != NULL)
581    {
582      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
583      for (unsigned int i = 0; i< elem->paramCount; i++)
584       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
585      if (elem->description != NULL)
586       printf("- %s", elem->description);
587      printf("\n");
588
589      elem = iterator->nextElement();
590    }
591    delete iterator;
592    elemCL = iteratorCL->nextElement();
593  }
594  delete iteratorCL;
595}
596
597/**
598 * converts a Parameter to a String
599 * @param parameter the Parameter we have.
600 * @returns the Name of the Parameter at Hand
601 */
602const char* ShellCommandBase::paramToString(long parameter)
603{
604  switch (parameter)
605  {
606    case ParameterBool:
607      return "BOOL";
608      break;
609    case ParameterChar:
610      return "CHAR";
611      break;
612    case ParameterString:
613      return "STRING";
614      break;
615    case ParameterInt:
616      return "INT";
617      break;
618    case ParameterUInt:
619      return "UINT";
620      break;
621    case ParameterFloat:
622      return "FLOAT";
623      break;
624    case ParameterLong:
625      return "LONG";
626      break;
627    default:
628      return "NULL";
629      break;
630  }
631}
Note: See TracBrowser for help on using the repository browser.