Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/avi_play/src/lib/shell/shell_completion.cc @ 6323

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

orxonox/branches/avi_play: merged the Trunk back into the branche avi_play again

merged with command svn merge ../trunk/ avi_play/ -r5845:HEAD
no conflicts

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