Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5199 was 5199, checked in by bensch, 20 years ago

orxonox/trunk: saver execution

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 */
380bool ShellCommandBase::execute(const char* executionString)
381{
382  if (ShellCommandClass::commandClassList == NULL)
383    return false;
384
385
386  long classID;                    //< the classID retrieved from the Class.
387  tList<BaseObject>* objectList;   //< the list of Objects stored in classID
388  BaseObject* objectPointer;       //< a pointer to th Object to Execute the command on
389  bool emptyComplete = false;      //< if the completion input is empty string. e.g ""
390//  long completeType = SHELLC_NONE; //< the Type we'd like to complete.
391  SubString inputSplits(executionString, true);
392
393  printf("!!!!%s!!!\n", executionString);
394
395  if (inputSplits.getCount() == 0)
396    return false;
397  // CHECK FOR ALIAS
398  if (inputSplits.getCount() >= 1)
399  {
400    if (ShellCommandClass::aliasList != NULL)
401    {
402      tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
403      ShellCommandAlias* elemAL = itAL->firstElement();
404      while(elemAL != NULL)
405      {
406        if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL &&
407            elemAL->getCommand()->shellClass != NULL )
408        {
409          objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName());
410          if (objectList != NULL)
411          {
412            elemAL->getCommand()->executeCommand(objectList->firstElement(), "");
413            return true;
414          }
415        }
416        elemAL = itAL->nextElement();
417      }
418      delete itAL;
419    }
420  }
421
422
423   //
424//   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
425//   ShellCommandBase* elem = iterator->firstElement();
426//   while(elem != NULL)
427//   {
428//     printf("%s::%s\n", elem->className, elem->getName());
429//     if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
430//          (*(executionString+strlen(elem->className)) == ' ' ||
431//           *(executionString+strlen(elem->className)) == ':' ))
432//     {
433//       const char* commandBegin = executionString + strlen(elem->className);
434//
435//       PRINTF(4)("Class %s matches\n", elem->className);
436//       BaseObject* objectPointer = NULL;
437//       if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
438//       {
439//         while(*commandBegin == ' ')
440//           commandBegin++;
441//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
442//             *(commandBegin + strlen(elem->getName())) != ' ' &&
443//             *(commandBegin + strlen(elem->getName())) != '\0')
444//         {
445//           elem = iterator->nextElement();
446//           continue;
447//         }
448//         PRINTF(4)("Command %s matches\n", elem->getName());
449//         // getting singleton-reference
450//         tList<BaseObject>* list =  ClassList::getList(elem->className);
451//         if (list != NULL)
452//           objectPointer = list->firstElement();
453//       }
454//       else
455//       {
456//         // checking for the Object
457//         while(*commandBegin == ' ')
458//           commandBegin++;
459//         tList<BaseObject>* list = ClassList::getList(elem->className);
460//         if (list == NULL)
461//           break;
462//         tIterator<BaseObject>* iterBO = list->getIterator();
463//         BaseObject* enumBO = iterBO->firstElement();
464//         while(enumBO != NULL)
465//         {
466//           if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
467//           {
468//             PRINTF(4)("Object %s matches\n", enumBO->getName());
469//             objectPointer = enumBO;
470//             break;
471//           }
472//           enumBO = iterBO->nextElement();
473//         }
474//         delete iterBO;
475//
476//         // break on no object Found. We cannot operate on Classes, but on Objects
477//         if (objectPointer == NULL)
478//           break;
479//         commandBegin = commandBegin + strlen(objectPointer->getName());
480//         while(*commandBegin == ' ')
481//           commandBegin++;
482//         // checking for the requested function.
483//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
484//         {
485//           elem = iterator->nextElement();
486//           continue;
487//         }
488//         PRINTF(4)("Function '%s' found\n", commandBegin);
489//       }
490//       const char* paramBegin = strchr(commandBegin, ' ');
491//       if (paramBegin == NULL)
492//         paramBegin = commandBegin + strlen(elem->getName());
493//       while (*paramBegin == ' ')
494//         paramBegin++;
495//
496//       PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
497//       if (objectPointer != NULL && paramBegin != NULL)
498//       {
499//         elem->executeCommand(objectPointer, paramBegin);
500//         delete iterator;
501//         return true;
502//       }
503//     }
504//     elem = iterator->nextElement();
505//   }
506//   delete iterator;
507//   return true;
508}
509
510/**
511 * lets a command be described
512 * @param description the description of the Given command
513 */
514ShellCommandBase* ShellCommandBase::describe(const char* description)
515{
516  if (this == NULL)
517    return NULL;
518 else
519 {
520   this->description = description;
521   return this;
522 }
523}
524
525/**
526 * adds an Alias to this Command
527 * @param alias the name of the Alias to set
528 * @returns itself
529 */
530ShellCommandBase* ShellCommandBase::setAlias(const char* alias)
531{
532  if (this == NULL)
533    return NULL;
534
535  if (this->alias != NULL)
536  {
537    PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());
538  }
539  else
540  {
541    if (ShellCommandClass::aliasList == NULL)
542      ShellCommandClass::aliasList = new tList<ShellCommandAlias>;
543
544    ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);
545    ShellCommandClass::aliasList->add(aliasCMD);
546    this->alias = aliasCMD;
547  }
548  return this;
549}
550
551/**
552 * see ShellCommandBase::debug()
553 */
554void ShellCommandBase::debugDyn()
555{
556  this->debug();
557}
558
559/**
560 * prints out nice information about the Shells Commands
561 */
562void ShellCommandBase::debug()
563{
564  if (ShellCommandClass::commandClassList == NULL)
565  {
566    PRINT(0)("No Command registered.\n");
567    return;
568  }
569
570  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
571  ShellCommandClass* elemCL = iteratorCL->firstElement();
572  while(elemCL != NULL)
573  {
574    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
575    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
576    const ShellCommandBase* elem = iterator->firstElement();
577    while(elem != NULL)
578    {
579      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
580      for (unsigned int i = 0; i< elem->paramCount; i++)
581       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
582      if (elem->description != NULL)
583       printf("- %s", elem->description);
584      printf("\n");
585
586      elem = iterator->nextElement();
587    }
588    delete iterator;
589    elemCL = iteratorCL->nextElement();
590  }
591  delete iteratorCL;
592}
593
594/**
595 * converts a Parameter to a String
596 * @param parameter the Parameter we have.
597 * @returns the Name of the Parameter at Hand
598 */
599const char* ShellCommandBase::paramToString(long parameter)
600{
601  switch (parameter)
602  {
603    case ParameterBool:
604      return "BOOL";
605      break;
606    case ParameterChar:
607      return "CHAR";
608      break;
609    case ParameterString:
610      return "STRING";
611      break;
612    case ParameterInt:
613      return "INT";
614      break;
615    case ParameterUInt:
616      return "UINT";
617      break;
618    case ParameterFloat:
619      return "FLOAT";
620      break;
621    case ParameterLong:
622      return "LONG";
623      break;
624    default:
625      return "NULL";
626      break;
627  }
628}
Note: See TracBrowser for help on using the repository browser.