Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now all commands are executeable again

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