Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_completion.cc @ 5239

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

orxonox/trunk: doxygen-tags

File size: 10.3 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: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell_completion.h"
19
20#include "shell_input.h"
21#include "shell_command.h"
22
23#include "substring.h"
24#include "base_object.h"
25#include "class_list.h"
26#include "list.h"
27#include "debug.h"
28
29#include "stdlibincl.h"
30
31using namespace std;
32
33/**
34 * standard constructor
35 * @todo this constructor is not jet implemented - do it
36*/
37ShellCompletion::ShellCompletion(ShellInput* input)
38{
39  this->completionList = NULL;
40  this->input = input;
41}
42
43
44/**
45 * standard deconstructor
46*/
47ShellCompletion::~ShellCompletion ()
48{
49  // delete what has to be deleted here
50  if (this->completionList)
51  {
52    this->emptyCompletionList();
53    delete this->completionList;
54  }
55}
56
57
58
59/**
60 * autocompletes the Shell's inputLine
61 * @returns true, if a result was found, false otherwise
62 *
63 * @todo implement it!!
64 */
65bool ShellCompletion::autoComplete(ShellInput* input)
66{
67  const char* completionLine;      //< the inputLine we complete.
68
69  long classID;                    //< the classID retrieved from the Class.
70  tList<BaseObject>* objectList;   //< the list of Objects stored in classID
71  bool emptyComplete = false;      //< if the completion input is empty string. e.g ""
72  long completeType = SHELLC_NONE; //< the Type we'd like to complete.
73  const char* completeString;      //< the string to complete.
74
75
76  PRINTF(5)("AutoComplete on input\n");
77  this->emptyCompletionList();
78
79  if (input != NULL)
80    this->input = input;
81  if (this->input == NULL)
82  {
83    PRINTF(2)("No ShellInput supplied\n");
84    return false;
85  }
86
87  // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object
88  if (this->input->getInput() != NULL &&
89      strrchr(this->input->getInput(), ' ') >= this->input->getInput() + strlen(this->input->getInput())-1)
90  {
91    emptyComplete = true;
92  }
93
94  // CREATE INPUTS
95  if (this->input->getInput() == NULL)
96    completionLine = "";
97  else
98    completionLine = this->input->getInput() + strspn(this->input->getInput(), " \t\n");
99  SubString inputSplits(completionLine, true);
100
101  // What String will be completed
102  if (emptyComplete == true)
103    completeString = "";
104  else
105    completeString = inputSplits.getString(inputSplits.getCount()-1);
106
107  // CLASS COMPLETION
108  if (inputSplits.getCount() == 0)
109  {
110    completeType |= SHELLC_CLASS;
111    completeType |= SHELLC_ALIAS;
112
113  }
114  else if (inputSplits.getCount() == 1 && emptyComplete == false)
115  {
116    completeType |= SHELLC_CLASS;
117    completeType |= SHELLC_ALIAS;
118  }
119
120  // OBJECT/FUNCTION COMPLETIONS
121  else if ((inputSplits.getCount() == 1 && emptyComplete == true) ||
122            (inputSplits.getCount() == 2 && emptyComplete == false))
123  {
124    classID = ClassList::StringToID(inputSplits.getString(0));
125    objectList = ClassList::getList(classID);
126    if (classID == CL_NULL)
127      return false;
128    else
129    {
130      if (objectList != NULL && objectList->getSize() == 1)
131        completeType |= SHELLC_FUNCTION;
132      completeType |= SHELLC_OBJECT;
133    }
134  }
135  else if ((inputSplits.getCount() == 2 && emptyComplete == true) ||
136            (inputSplits.getCount() == 3 && emptyComplete == false))
137  {
138    classID = ClassList::StringToID(inputSplits.getString(0));
139    if (classID == CL_NULL)
140      return false;
141    else
142     completeType |= SHELLC_FUNCTION;
143  }
144
145  if (completeType & SHELLC_CLASS)
146    this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS);
147  if (completeType & SHELLC_OBJECT)
148    this->objectComplete(completeString, classID);
149  if (completeType & SHELLC_FUNCTION)
150    this->functionComplete(completeString, classID);
151  if (completeType & SHELLC_ALIAS)
152    this->aliasComplete(completeString);
153
154
155  this->generalComplete(completeString);
156  return true;
157}
158
159/**
160 * autocompletes a className
161 * @param classBegin the Beginning of a String to autoComplete
162 * @return true on success, false otherwise
163 */
164bool ShellCompletion::classComplete(const char* classBegin)
165{
166  if (unlikely(classBegin == NULL))
167    return false;
168  const tList<const char>* clList = ClassList::getClassList();
169  if (clList != NULL)
170  {
171    if (!this->addToCompleteList(clList, classBegin))
172      return false;
173  }
174  else
175    return false;
176  return true;
177}
178
179/**
180 * autocompletes an ObjectName
181 * @param objectBegin the beginning string of a Object
182 * @param classID the ID of the Class to search for.
183 * @return true on success, false otherwise
184 */
185bool ShellCompletion::objectComplete(const char* objectBegin, long classID)
186{
187  if (unlikely(objectBegin == NULL))
188    return false;
189  const tList<BaseObject>* boList = ClassList::getList(classID);
190  if (boList != NULL)
191  {
192    //printf("%s\n", boList->firstElement()->getName());
193    if (!this->addToCompleteList(boList, objectBegin))
194      return false;
195  }
196  else
197    return false;
198  return true;
199}
200
201/**
202 * completes a Function
203 * @param functionBegin the beginning of the function String
204 * @param classID the class' ID to complete the function of
205 */
206bool ShellCompletion::functionComplete(const char* functionBegin, long classID)
207{
208  if (unlikely(functionBegin == NULL))
209    return false;
210  tList<const char> fktList;
211  ShellCommandClass::getCommandListOfClass(ClassList::IDToString(classID), &fktList);
212  //printf("%s\n", boList->firstElement()->getName());
213  if (!this->addToCompleteList(&fktList, functionBegin))
214    return false;
215  return true;
216}
217
218/**
219 * completes an Alias
220 * @param aliasBegin the beginning of the Alias-String to complete
221 * @returns true on succes, false if something went wrong
222 */
223bool ShellCompletion::aliasComplete(const char* aliasBegin)
224{
225  if (unlikely(aliasBegin == NULL))
226    return false;
227  tList<const char> aliasList;
228  ShellCommandClass::getCommandListOfAlias(&aliasList);
229  //printf("%s\n", boList->firstElement()->getName());
230  if (!this->addToCompleteList(&aliasList, aliasBegin))
231    return false;
232  return true;
233}
234
235
236/**
237 * completes the inputline on grounds of an inputList
238 * @param begin the String to search in the inputList, and to extend with it.
239 * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::"
240 * @param addBack what should be added at the end of the completion
241 * @param addFront what should be added to the front of one finished completion
242 * @return true if ok, false otherwise
243 */
244bool ShellCompletion::generalComplete(const char* begin, const char* displayAs, const char* addBack, const char* addFront)
245{
246  if (completionList == NULL || this->input == NULL )
247    return false;
248  if (completionList->getSize() == 0)
249    return false;
250
251  ShellC_Element* addElem = completionList->firstElement();
252  const char* addString = addElem->name;
253  unsigned int addLength = 0;
254  unsigned int inputLenght = strlen(begin);
255
256  // Determin the longest Match
257  if (addString != NULL)
258    addLength = strlen(addString);
259  tIterator<ShellC_Element>* charIterator = completionList->getIterator();
260  ShellC_Element* charElem = charIterator->firstElement();
261  while (charElem != NULL)
262  {
263    PRINTF(0)(displayAs, charElem->name);
264    for (unsigned int i = inputLenght; i < addLength; i++)
265      if (addString[i] != charElem->name[i])
266      {
267       addLength = i;
268       break;
269      }
270    charElem = charIterator->nextElement();
271  }
272  delete charIterator;
273
274  if (addLength >= inputLenght)
275  {
276    char* adder = new char[addLength+1];
277    strncpy(adder, addString, addLength);
278    adder[addLength] = '\0';
279
280    if (this->input)
281    {
282     this->input->removeCharacters(inputLenght);
283     this->input->addCharacters(adder);
284
285      if (completionList->getSize() == 1)
286      {
287        if ( addBack != NULL )
288         this->input->addCharacters(addBack);
289        this->input->addCharacter(' ');
290      }
291     delete[] adder;
292    }
293  }
294  return true;
295}
296
297/**
298 * searches for classes, which beginn with completionBegin
299 * @param inputList the List to parse through
300 * @param completionBegin the beginning string
301 * !! The strings MUST NOT be deleted !!
302 */
303bool ShellCompletion::addToCompleteList(const tList<const char>* inputList, const char* completionBegin)
304{
305  if (inputList == NULL || completionBegin == NULL)
306    return false;
307  unsigned int searchLength = strlen(completionBegin);
308
309  tIterator<const char>* iterator = inputList->getIterator();
310  const char* enumString = iterator->firstElement();
311  while (enumString != NULL)
312  {
313    if (strlen(enumString) >= searchLength &&
314        !strncasecmp(enumString, completionBegin, searchLength))
315    {
316      printf("%s\n", enumString);
317      ShellC_Element* newElem = new ShellC_Element;
318      newElem->name = enumString;
319      this->completionList->add(newElem);
320    }
321    enumString = iterator->nextElement();
322  }
323  delete iterator;
324
325  return true;
326}
327
328/**
329 * searches for classes, which beginn with completionBegin
330 * @param inputList the List to parse through
331 * @param completionBegin the beginning string
332 * !! The strings MUST NOT be deleted !!
333 */
334bool ShellCompletion::addToCompleteList(const tList<BaseObject>* inputList, const char* completionBegin)
335{
336  if (inputList == NULL || completionBegin == NULL)
337    return false;
338  unsigned int searchLength = strlen(completionBegin);
339
340  tIterator<BaseObject>* iterator = inputList->getIterator();
341  BaseObject* enumBO = iterator->firstElement();
342  while (enumBO != NULL)
343  {
344    if (enumBO->getName() != NULL &&
345        strlen(enumBO->getName()) >= searchLength &&
346        !strncasecmp(enumBO->getName(), completionBegin, searchLength))
347    {
348      ShellC_Element* newElem = new ShellC_Element;
349      newElem->name = enumBO->getName();
350      this->completionList->add(newElem);
351      printf("%s\n",enumBO->getName());
352    }
353    enumBO = iterator->nextElement();
354  }
355  delete iterator;
356
357  return true;
358}
359
360/**
361 * deletes the Completion List.
362 *
363 * This is done at the beginning of each completion-run
364 */
365void ShellCompletion::emptyCompletionList()
366{
367  if (this->completionList != NULL)
368  {
369    tIterator<ShellC_Element>* elemIT = this->completionList->getIterator();
370    ShellC_Element* elem = elemIT->firstElement();
371    while (elem != NULL)
372    {
373      delete elem;
374      elem = elemIT->nextElement();
375    }
376    delete this->completionList;
377  }
378  this->completionList = new tList<ShellC_Element>;
379}
Note: See TracBrowser for help on using the repository browser.