Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ShellCommandAlias should really be completed now

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