Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: alias-completion works too now :)

File size: 10.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: ...
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 */
205bool ShellCompletion::functionComplete(const char* functionBegin, long classID)
206{
207  if (unlikely(functionBegin == NULL))
208    return false;
209  tList<const char> fktList;
210  ShellCommandClass::getCommandListOfClass(ClassList::IDToString(classID), &fktList);
211  //printf("%s\n", boList->firstElement()->getName());
212  if (!this->addToCompleteList(&fktList, functionBegin))
213    return false;
214  return true;
215}
216
217bool ShellCompletion::aliasComplete(const char* aliasBegin)
218{
219  if (unlikely(aliasBegin == NULL))
220    return false;
221  tList<const char> aliasList;
222  ShellCommandClass::getCommandListOfAlias(&aliasList);
223  //printf("%s\n", boList->firstElement()->getName());
224  if (!this->addToCompleteList(&aliasList, aliasBegin))
225    return false;
226  return true;
227}
228
229
230/**
231 * completes the inputline on grounds of an inputList
232 * @param begin the String to search in the inputList, and to extend with it.
233 * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::"
234 * @param addBack what should be added at the end of the completion
235 * @param addFront what should be added to the front of one finished completion
236 * @return true if ok, false otherwise
237 */
238bool ShellCompletion::generalComplete(const char* begin, const char* displayAs, const char* addBack, const char* addFront)
239{
240  if (completionList == NULL || this->input == NULL )
241    return false;
242  if (completionList->getSize() == 0)
243    return false;
244
245  ShellC_Element* addElem = completionList->firstElement();
246  const char* addString = addElem->name;
247  unsigned int addLength = 0;
248  unsigned int inputLenght = strlen(begin);
249
250  // Determin the longest Match
251  if (addString != NULL)
252    addLength = strlen(addString);
253  tIterator<ShellC_Element>* charIterator = completionList->getIterator();
254  ShellC_Element* charElem = charIterator->firstElement();
255  while (charElem != NULL)
256  {
257    PRINTF(0)(displayAs, charElem->name);
258    for (unsigned int i = inputLenght; i < addLength; i++)
259      if (addString[i] != charElem->name[i])
260      {
261       addLength = i;
262       break;
263      }
264    charElem = charIterator->nextElement();
265  }
266  delete charIterator;
267
268  if (addLength >= inputLenght)
269  {
270    char* adder = new char[addLength+1];
271    strncpy(adder, addString, addLength);
272    adder[addLength] = '\0';
273
274    if (this->input)
275    {
276     this->input->removeCharacters(inputLenght);
277     this->input->addCharacters(adder);
278
279      if (completionList->getSize() == 1)
280      {
281        if ( addBack != NULL )
282         this->input->addCharacters(addBack);
283        this->input->addCharacter(' ');
284      }
285     delete[] adder;
286    }
287  }
288  return true;
289}
290
291/**
292 * searches for classes, which beginn with completionBegin
293 * @param inputList the List to parse through
294 * @param completionBegin the beginning string
295 * !! The strings MUST NOT be deleted !!
296 */
297bool ShellCompletion::addToCompleteList(const tList<const char>* inputList, const char* completionBegin)
298{
299  if (inputList == NULL || completionBegin == NULL)
300    return false;
301  unsigned int searchLength = strlen(completionBegin);
302
303  tIterator<const char>* iterator = inputList->getIterator();
304  const char* enumString = iterator->firstElement();
305  while (enumString != NULL)
306  {
307    if (strlen(enumString) >= searchLength &&
308        !strncasecmp(enumString, completionBegin, searchLength))
309    {
310      printf("%s\n", enumString);
311      ShellC_Element* newElem = new ShellC_Element;
312      newElem->name = enumString;
313      this->completionList->add(newElem);
314    }
315    enumString = iterator->nextElement();
316  }
317  delete iterator;
318
319  return true;
320}
321
322/**
323 * searches for classes, which beginn with completionBegin
324 * @param inputList the List to parse through
325 * @param completionBegin the beginning string
326 * !! The strings MUST NOT be deleted !!
327 */
328bool ShellCompletion::addToCompleteList(const tList<BaseObject>* inputList, const char* completionBegin)
329{
330  if (inputList == NULL || completionBegin == NULL)
331    return false;
332  unsigned int searchLength = strlen(completionBegin);
333
334  tIterator<BaseObject>* iterator = inputList->getIterator();
335  BaseObject* enumBO = iterator->firstElement();
336  while (enumBO != NULL)
337  {
338    if (enumBO->getName() != NULL &&
339        strlen(enumBO->getName()) >= searchLength &&
340        !strncasecmp(enumBO->getName(), completionBegin, searchLength))
341    {
342      ShellC_Element* newElem = new ShellC_Element;
343      newElem->name = enumBO->getName();
344      this->completionList->add(newElem);
345      printf("%s\n",enumBO->getName());
346    }
347    enumBO = iterator->nextElement();
348  }
349  delete iterator;
350
351  return true;
352}
353
354
355void ShellCompletion::emptyCompletionList()
356{
357  if (this->completionList != NULL)
358  {
359    tIterator<ShellC_Element>* elemIT = this->completionList->getIterator();
360    ShellC_Element* elem = elemIT->firstElement();
361    while (elem != NULL)
362    {
363      delete elem;
364      elem = elemIT->nextElement();
365    }
366    delete this->completionList;
367  }
368  this->completionList = new tList<ShellC_Element>;
369}
Note: See TracBrowser for help on using the repository browser.