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 | |
---|
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 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | /** |
---|
34 | * standard constructor |
---|
35 | */ |
---|
36 | ShellCompletion::ShellCompletion(ShellInput* input) |
---|
37 | { |
---|
38 | this->completionList = NULL; |
---|
39 | this->input = input; |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | /** |
---|
44 | * standard deconstructor |
---|
45 | */ |
---|
46 | ShellCompletion::~ShellCompletion () |
---|
47 | { |
---|
48 | // delete what has to be deleted here |
---|
49 | if (this->completionList) |
---|
50 | { |
---|
51 | this->emptyCompletionList(); |
---|
52 | delete this->completionList; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | /** |
---|
59 | * autocompletes the Shell's inputLine |
---|
60 | * @returns true, if a result was found, false otherwise |
---|
61 | */ |
---|
62 | bool ShellCompletion::autoComplete(ShellInput* input) |
---|
63 | { |
---|
64 | const char* completionLine; //< the inputLine we complete. |
---|
65 | |
---|
66 | long classID; //< the classID retrieved from the Class. |
---|
67 | tList<BaseObject>* objectList; //< the list of Objects stored in classID |
---|
68 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
69 | long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
70 | const char* completeString; //< the string to complete. |
---|
71 | |
---|
72 | |
---|
73 | PRINTF(5)("AutoComplete on input\n"); |
---|
74 | this->emptyCompletionList(); |
---|
75 | |
---|
76 | if (input != NULL) |
---|
77 | this->input = input; |
---|
78 | if (this->input == NULL) |
---|
79 | { |
---|
80 | PRINTF(2)("No ShellInput supplied\n"); |
---|
81 | return false; |
---|
82 | } |
---|
83 | |
---|
84 | // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object |
---|
85 | if (this->input->getInput() != NULL && |
---|
86 | strrchr(this->input->getInput(), ' ') >= this->input->getInput() + strlen(this->input->getInput())-1) |
---|
87 | { |
---|
88 | emptyComplete = true; |
---|
89 | } |
---|
90 | |
---|
91 | // CREATE INPUTS |
---|
92 | if (this->input->getInput() == NULL) |
---|
93 | completionLine = ""; |
---|
94 | else |
---|
95 | completionLine = this->input->getInput() + strspn(this->input->getInput(), " \t\n"); |
---|
96 | SubString inputSplits(completionLine, true); |
---|
97 | |
---|
98 | // What String will be completed |
---|
99 | if (emptyComplete == true) |
---|
100 | completeString = ""; |
---|
101 | else |
---|
102 | completeString = inputSplits.getString(inputSplits.getCount()-1); |
---|
103 | |
---|
104 | // CLASS COMPLETION |
---|
105 | if (inputSplits.getCount() == 0) |
---|
106 | { |
---|
107 | completeType |= SHELLC_CLASS; |
---|
108 | completeType |= SHELLC_ALIAS; |
---|
109 | } |
---|
110 | else if (inputSplits.getCount() == 1 && emptyComplete == false) |
---|
111 | { |
---|
112 | completeType |= SHELLC_CLASS; |
---|
113 | completeType |= SHELLC_ALIAS; |
---|
114 | } |
---|
115 | |
---|
116 | // OBJECT/FUNCTION COMPLETIONS |
---|
117 | else if ((inputSplits.getCount() == 1 && emptyComplete == true) || |
---|
118 | (inputSplits.getCount() == 2 && emptyComplete == false)) |
---|
119 | { |
---|
120 | classID = ClassList::StringToID(inputSplits.getString(0)); |
---|
121 | objectList = ClassList::getList(classID); |
---|
122 | if (classID != CL_NULL) |
---|
123 | completeType |= SHELLC_OBJECT; |
---|
124 | //if (objectList != NULL && objectList->getSize() == 1) |
---|
125 | completeType |= SHELLC_FUNCTION; |
---|
126 | } |
---|
127 | else if ((inputSplits.getCount() == 2 && emptyComplete == true) || |
---|
128 | (inputSplits.getCount() == 3 && emptyComplete == false)) |
---|
129 | { |
---|
130 | classID = ClassList::StringToID(inputSplits.getString(0)); |
---|
131 | if (classID == CL_NULL) |
---|
132 | return false; |
---|
133 | else |
---|
134 | completeType |= SHELLC_FUNCTION; |
---|
135 | } |
---|
136 | |
---|
137 | if (completeType & SHELLC_CLASS) |
---|
138 | this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS); |
---|
139 | if (completeType & SHELLC_OBJECT) |
---|
140 | this->objectComplete(completeString, classID); |
---|
141 | if (completeType & SHELLC_FUNCTION) |
---|
142 | this->functionComplete(completeString, inputSplits.getString(0)); |
---|
143 | if (completeType & SHELLC_ALIAS) |
---|
144 | this->aliasComplete(completeString); |
---|
145 | |
---|
146 | |
---|
147 | this->generalComplete(completeString); |
---|
148 | return true; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * autocompletes a className |
---|
153 | * @param classBegin the Beginning of a String to autoComplete |
---|
154 | * @return true on success, false otherwise |
---|
155 | */ |
---|
156 | bool ShellCompletion::classComplete(const char* classBegin) |
---|
157 | { |
---|
158 | if (unlikely(classBegin == NULL)) |
---|
159 | return false; |
---|
160 | const tList<const char>* clList = ClassList::getClassList(); |
---|
161 | if (clList != NULL) |
---|
162 | { |
---|
163 | if (!this->addToCompleteList(clList, classBegin, SHELLC_CLASS)) |
---|
164 | return false; |
---|
165 | } |
---|
166 | else |
---|
167 | return false; |
---|
168 | return true; |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * autocompletes an ObjectName |
---|
173 | * @param objectBegin the beginning string of a Object |
---|
174 | * @param classID the ID of the Class to search for. |
---|
175 | * @return true on success, false otherwise |
---|
176 | */ |
---|
177 | bool ShellCompletion::objectComplete(const char* objectBegin, long classID) |
---|
178 | { |
---|
179 | if (unlikely(objectBegin == NULL)) |
---|
180 | return false; |
---|
181 | const tList<BaseObject>* boList = ClassList::getList(classID); |
---|
182 | if (boList != NULL) |
---|
183 | { |
---|
184 | SHELLC_TYPE type = SHELLC_OBJECT; |
---|
185 | if (classID == CL_SHELL_COMMAND_CLASS) |
---|
186 | type = SHELLC_CLASS; |
---|
187 | if (!this->addToCompleteList(boList, objectBegin, type)) |
---|
188 | return false; |
---|
189 | } |
---|
190 | else |
---|
191 | return false; |
---|
192 | return true; |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * completes a Function |
---|
197 | * @param functionBegin the beginning of the function String |
---|
198 | * @param classID the class' ID to complete the function of |
---|
199 | */ |
---|
200 | bool ShellCompletion::functionComplete(const char* functionBegin, const char* className) |
---|
201 | { |
---|
202 | if (unlikely(functionBegin == NULL)) |
---|
203 | return false; |
---|
204 | tList<const char> fktList; |
---|
205 | ShellCommandClass::getCommandListOfClass(className, &fktList); |
---|
206 | //printf("%s\n", boList->firstElement()->getName()); |
---|
207 | if (!this->addToCompleteList(&fktList, functionBegin, SHELLC_FUNCTION)) |
---|
208 | return false; |
---|
209 | return true; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * completes an Alias |
---|
214 | * @param aliasBegin the beginning of the Alias-String to complete |
---|
215 | * @returns true on succes, false if something went wrong |
---|
216 | */ |
---|
217 | bool 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, SHELLC_ALIAS)) |
---|
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 | */ |
---|
238 | bool 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 | SHELLC_TYPE changeType = SHELLC_NONE; |
---|
256 | while (charElem != NULL) |
---|
257 | { |
---|
258 | if (charElem->type != changeType) |
---|
259 | { |
---|
260 | if (changeType != SHELLC_NONE) |
---|
261 | PRINT(0)("\n"); |
---|
262 | PRINT(0)("%s: ", ShellCompletion::typeToString(charElem->type)); |
---|
263 | changeType = charElem->type; |
---|
264 | } |
---|
265 | PRINTF(0)("%s ", charElem->name); |
---|
266 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
267 | if (addString[i] != charElem->name[i]) |
---|
268 | { |
---|
269 | addLength = i; |
---|
270 | // break; |
---|
271 | } |
---|
272 | charElem = charIterator->nextElement(); |
---|
273 | } |
---|
274 | delete charIterator; |
---|
275 | PRINT(0)("\n"); |
---|
276 | |
---|
277 | if (addLength >= inputLenght) |
---|
278 | { |
---|
279 | char* adder = new char[addLength+1]; |
---|
280 | strncpy(adder, addString, addLength); |
---|
281 | adder[addLength] = '\0'; |
---|
282 | |
---|
283 | if (this->input) |
---|
284 | { |
---|
285 | this->input->removeCharacters(inputLenght); |
---|
286 | this->input->addCharacters(adder); |
---|
287 | |
---|
288 | if (completionList->getSize() == 1) |
---|
289 | { |
---|
290 | if ( addBack != NULL ) |
---|
291 | this->input->addCharacters(addBack); |
---|
292 | this->input->addCharacter(' '); |
---|
293 | } |
---|
294 | delete[] adder; |
---|
295 | } |
---|
296 | } |
---|
297 | return true; |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * searches for classes, which beginn with completionBegin |
---|
302 | * @param inputList the List to parse through |
---|
303 | * @param completionBegin the beginning string |
---|
304 | * !! The strings MUST NOT be deleted !! |
---|
305 | */ |
---|
306 | bool ShellCompletion::addToCompleteList(const tList<const char>* inputList, const char* completionBegin, SHELLC_TYPE type) |
---|
307 | { |
---|
308 | if (inputList == NULL || completionBegin == NULL) |
---|
309 | return false; |
---|
310 | unsigned int searchLength = strlen(completionBegin); |
---|
311 | |
---|
312 | tIterator<const char>* iterator = inputList->getIterator(); |
---|
313 | const char* enumString = iterator->firstElement(); |
---|
314 | while (enumString != NULL) |
---|
315 | { |
---|
316 | if (strlen(enumString) >= searchLength && |
---|
317 | !strncasecmp(enumString, completionBegin, searchLength)) |
---|
318 | { |
---|
319 | printf("%s\n", enumString); |
---|
320 | ShellC_Element* newElem = new ShellC_Element; |
---|
321 | newElem->name = enumString; |
---|
322 | newElem->type = type; |
---|
323 | this->completionList->add(newElem); |
---|
324 | } |
---|
325 | enumString = iterator->nextElement(); |
---|
326 | } |
---|
327 | delete iterator; |
---|
328 | |
---|
329 | return true; |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * searches for classes, which beginn with completionBegin |
---|
334 | * @param inputList the List to parse through |
---|
335 | * @param completionBegin the beginning string |
---|
336 | * !! The strings MUST NOT be deleted !! |
---|
337 | */ |
---|
338 | bool ShellCompletion::addToCompleteList(const tList<BaseObject>* inputList, const char* completionBegin, SHELLC_TYPE type) |
---|
339 | { |
---|
340 | if (inputList == NULL || completionBegin == NULL) |
---|
341 | return false; |
---|
342 | unsigned int searchLength = strlen(completionBegin); |
---|
343 | |
---|
344 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
---|
345 | BaseObject* enumBO = iterator->firstElement(); |
---|
346 | while (enumBO != NULL) |
---|
347 | { |
---|
348 | if (enumBO->getName() != NULL && |
---|
349 | strlen(enumBO->getName()) >= searchLength && |
---|
350 | !strncasecmp(enumBO->getName(), completionBegin, searchLength)) |
---|
351 | { |
---|
352 | ShellC_Element* newElem = new ShellC_Element; |
---|
353 | newElem->name = enumBO->getName(); |
---|
354 | newElem->type = type; |
---|
355 | this->completionList->add(newElem); |
---|
356 | } |
---|
357 | enumBO = iterator->nextElement(); |
---|
358 | } |
---|
359 | delete iterator; |
---|
360 | |
---|
361 | return true; |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | * deletes the Completion List. |
---|
366 | * |
---|
367 | * This is done at the beginning of each completion-run |
---|
368 | */ |
---|
369 | void ShellCompletion::emptyCompletionList() |
---|
370 | { |
---|
371 | if (this->completionList != NULL) |
---|
372 | { |
---|
373 | tIterator<ShellC_Element>* elemIT = this->completionList->getIterator(); |
---|
374 | ShellC_Element* elem = elemIT->firstElement(); |
---|
375 | while (elem != NULL) |
---|
376 | { |
---|
377 | delete elem; |
---|
378 | elem = elemIT->nextElement(); |
---|
379 | } |
---|
380 | delete this->completionList; |
---|
381 | } |
---|
382 | this->completionList = new tList<ShellC_Element>; |
---|
383 | } |
---|
384 | |
---|
385 | const char* ShellCompletion::typeToString(SHELLC_TYPE type) |
---|
386 | { |
---|
387 | switch (type) |
---|
388 | { |
---|
389 | default:// SHELLC_NONE |
---|
390 | return "error"; |
---|
391 | case SHELLC_CLASS: |
---|
392 | return "class"; |
---|
393 | case SHELLC_OBJECT: |
---|
394 | return "object"; |
---|
395 | case SHELLC_FUNCTION: |
---|
396 | return "function"; |
---|
397 | case SHELLC_ALIAS: |
---|
398 | return "alias"; |
---|
399 | } |
---|
400 | } |
---|