[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: ... |
---|
| 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[5170] | 18 | #include "shell_completion.h" |
---|
[1853] | 19 | |
---|
[5181] | 20 | #include "shell_input.h" |
---|
[5194] | 21 | #include "shell_command.h" |
---|
[5181] | 22 | |
---|
[5183] | 23 | #include "substring.h" |
---|
[5178] | 24 | #include "base_object.h" |
---|
| 25 | #include "class_list.h" |
---|
| 26 | #include "list.h" |
---|
| 27 | #include "debug.h" |
---|
| 28 | |
---|
| 29 | #include "stdlibincl.h" |
---|
| 30 | |
---|
[1856] | 31 | using namespace std; |
---|
[1853] | 32 | |
---|
[3245] | 33 | /** |
---|
[4838] | 34 | * standard constructor |
---|
| 35 | * @todo this constructor is not jet implemented - do it |
---|
[3245] | 36 | */ |
---|
[5182] | 37 | ShellCompletion::ShellCompletion(ShellInput* input) |
---|
[3365] | 38 | { |
---|
[5181] | 39 | this->completionList = NULL; |
---|
[5182] | 40 | this->input = input; |
---|
[3365] | 41 | } |
---|
[1853] | 42 | |
---|
| 43 | |
---|
[3245] | 44 | /** |
---|
[4838] | 45 | * standard deconstructor |
---|
[3245] | 46 | */ |
---|
[5170] | 47 | ShellCompletion::~ShellCompletion () |
---|
[3543] | 48 | { |
---|
| 49 | // delete what has to be deleted here |
---|
[5181] | 50 | if (this->completionList) |
---|
| 51 | { |
---|
[5188] | 52 | this->emptyCompletionList(); |
---|
[5181] | 53 | delete this->completionList; |
---|
| 54 | } |
---|
[3543] | 55 | } |
---|
[5178] | 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 | */ |
---|
[5181] | 65 | bool ShellCompletion::autoComplete(ShellInput* input) |
---|
[5178] | 66 | { |
---|
[5194] | 67 | const char* completionLine; //< the inputLine we complete. |
---|
[5183] | 68 | |
---|
[5187] | 69 | long classID; //< the classID retrieved from the Class. |
---|
| 70 | tList<BaseObject>* objectList; //< the list of Objects stored in classID |
---|
[5190] | 71 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
[5194] | 72 | long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
| 73 | const char* completeString; //< the string to complete. |
---|
[5183] | 74 | |
---|
[5190] | 75 | |
---|
[5191] | 76 | PRINTF(5)("AutoComplete on input\n"); |
---|
[5187] | 77 | this->emptyCompletionList(); |
---|
[5185] | 78 | |
---|
[5182] | 79 | if (input != NULL) |
---|
| 80 | this->input = input; |
---|
[5184] | 81 | if (this->input == NULL) |
---|
[5185] | 82 | { |
---|
| 83 | PRINTF(2)("No ShellInput supplied\n"); |
---|
[5184] | 84 | return false; |
---|
[5185] | 85 | } |
---|
[5190] | 86 | |
---|
| 87 | // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object |
---|
[5191] | 88 | if (this->input->getInput() != NULL && |
---|
| 89 | strrchr(this->input->getInput(), ' ') >= this->input->getInput() + strlen(this->input->getInput())-1) |
---|
[5190] | 90 | { |
---|
| 91 | emptyComplete = true; |
---|
| 92 | } |
---|
| 93 | |
---|
[5193] | 94 | // CREATE INPUTS |
---|
[5185] | 95 | if (this->input->getInput() == NULL) |
---|
[5191] | 96 | completionLine = ""; |
---|
| 97 | else |
---|
| 98 | completionLine = this->input->getInput() + strspn(this->input->getInput(), " \t\n"); |
---|
[5184] | 99 | SubString inputSplits(completionLine, true); |
---|
| 100 | |
---|
[5193] | 101 | // What String will be completed |
---|
| 102 | if (emptyComplete == true) |
---|
| 103 | completeString = ""; |
---|
| 104 | else |
---|
| 105 | completeString = inputSplits.getString(inputSplits.getCount()-1); |
---|
| 106 | |
---|
[5185] | 107 | // CLASS COMPLETION |
---|
[5184] | 108 | if (inputSplits.getCount() == 0) |
---|
| 109 | { |
---|
[5193] | 110 | completeType |= SHELLC_CLASS; |
---|
[5195] | 111 | completeType |= SHELLC_ALIAS; |
---|
| 112 | |
---|
[5184] | 113 | } |
---|
[5191] | 114 | else if (inputSplits.getCount() == 1 && emptyComplete == false) |
---|
[5184] | 115 | { |
---|
[5193] | 116 | completeType |= SHELLC_CLASS; |
---|
[5195] | 117 | completeType |= SHELLC_ALIAS; |
---|
[5184] | 118 | } |
---|
| 119 | |
---|
[5191] | 120 | // OBJECT/FUNCTION COMPLETIONS |
---|
[5192] | 121 | else if ((inputSplits.getCount() == 1 && emptyComplete == true) || |
---|
| 122 | (inputSplits.getCount() == 2 && emptyComplete == false)) |
---|
[5184] | 123 | { |
---|
[5185] | 124 | classID = ClassList::StringToID(inputSplits.getString(0)); |
---|
[5194] | 125 | objectList = ClassList::getList(classID); |
---|
[5185] | 126 | if (classID == CL_NULL) |
---|
| 127 | return false; |
---|
[5192] | 128 | else |
---|
[5186] | 129 | { |
---|
[5194] | 130 | if (objectList != NULL && objectList->getSize() == 1) |
---|
| 131 | completeType |= SHELLC_FUNCTION; |
---|
[5193] | 132 | completeType |= SHELLC_OBJECT; |
---|
[5186] | 133 | } |
---|
[5184] | 134 | } |
---|
[5194] | 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 | } |
---|
[5184] | 144 | |
---|
[5193] | 145 | if (completeType & SHELLC_CLASS) |
---|
| 146 | this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS); |
---|
| 147 | if (completeType & SHELLC_OBJECT) |
---|
| 148 | this->objectComplete(completeString, classID); |
---|
[5194] | 149 | if (completeType & SHELLC_FUNCTION) |
---|
| 150 | this->functionComplete(completeString, classID); |
---|
[5195] | 151 | if (completeType & SHELLC_ALIAS) |
---|
| 152 | this->aliasComplete(completeString); |
---|
[5193] | 153 | |
---|
[5194] | 154 | |
---|
| 155 | this->generalComplete(completeString); |
---|
| 156 | return true; |
---|
[5178] | 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 | */ |
---|
| 164 | bool 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 | { |
---|
[5192] | 171 | if (!this->addToCompleteList(clList, classBegin)) |
---|
[5178] | 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 | */ |
---|
| 185 | bool ShellCompletion::objectComplete(const char* objectBegin, long classID) |
---|
| 186 | { |
---|
| 187 | if (unlikely(objectBegin == NULL)) |
---|
| 188 | return false; |
---|
[5193] | 189 | const tList<BaseObject>* boList = ClassList::getList(classID); |
---|
[5178] | 190 | if (boList != NULL) |
---|
| 191 | { |
---|
[5188] | 192 | //printf("%s\n", boList->firstElement()->getName()); |
---|
[5192] | 193 | if (!this->addToCompleteList(boList, objectBegin)) |
---|
[5178] | 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 |
---|
[5197] | 204 | * @param classID the class' ID to complete the function of |
---|
[5178] | 205 | */ |
---|
[5194] | 206 | bool ShellCompletion::functionComplete(const char* functionBegin, long classID) |
---|
[5178] | 207 | { |
---|
[5194] | 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; |
---|
[5178] | 216 | } |
---|
| 217 | |
---|
[5197] | 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 | */ |
---|
[5195] | 223 | bool 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 | |
---|
[5178] | 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 | */ |
---|
[5192] | 244 | bool ShellCompletion::generalComplete(const char* begin, const char* displayAs, const char* addBack, const char* addFront) |
---|
[5178] | 245 | { |
---|
[5192] | 246 | if (completionList == NULL || this->input == NULL ) |
---|
[5185] | 247 | return false; |
---|
[5192] | 248 | if (completionList->getSize() == 0) |
---|
[5178] | 249 | return false; |
---|
| 250 | |
---|
[5192] | 251 | ShellC_Element* addElem = completionList->firstElement(); |
---|
[5187] | 252 | const char* addString = addElem->name; |
---|
[5178] | 253 | unsigned int addLength = 0; |
---|
| 254 | unsigned int inputLenght = strlen(begin); |
---|
| 255 | |
---|
[5187] | 256 | // Determin the longest Match |
---|
[5178] | 257 | if (addString != NULL) |
---|
| 258 | addLength = strlen(addString); |
---|
[5192] | 259 | tIterator<ShellC_Element>* charIterator = completionList->getIterator(); |
---|
[5187] | 260 | ShellC_Element* charElem = charIterator->firstElement(); |
---|
[5178] | 261 | while (charElem != NULL) |
---|
| 262 | { |
---|
[5187] | 263 | PRINTF(0)(displayAs, charElem->name); |
---|
[5178] | 264 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
[5187] | 265 | if (addString[i] != charElem->name[i]) |
---|
[5185] | 266 | { |
---|
| 267 | addLength = i; |
---|
| 268 | break; |
---|
| 269 | } |
---|
[5178] | 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 | |
---|
[5182] | 280 | if (this->input) |
---|
| 281 | { |
---|
| 282 | this->input->removeCharacters(inputLenght); |
---|
| 283 | this->input->addCharacters(adder); |
---|
[5178] | 284 | |
---|
[5192] | 285 | if (completionList->getSize() == 1) |
---|
[5185] | 286 | { |
---|
| 287 | if ( addBack != NULL ) |
---|
| 288 | this->input->addCharacters(addBack); |
---|
| 289 | this->input->addCharacter(' '); |
---|
| 290 | } |
---|
[5182] | 291 | delete[] adder; |
---|
| 292 | } |
---|
[5178] | 293 | } |
---|
| 294 | return true; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
[5187] | 298 | * searches for classes, which beginn with completionBegin |
---|
[5178] | 299 | * @param inputList the List to parse through |
---|
[5187] | 300 | * @param completionBegin the beginning string |
---|
[5178] | 301 | * !! The strings MUST NOT be deleted !! |
---|
| 302 | */ |
---|
[5192] | 303 | bool ShellCompletion::addToCompleteList(const tList<const char>* inputList, const char* completionBegin) |
---|
[5178] | 304 | { |
---|
[5187] | 305 | if (inputList == NULL || completionBegin == NULL) |
---|
[5192] | 306 | return false; |
---|
[5187] | 307 | unsigned int searchLength = strlen(completionBegin); |
---|
[5178] | 308 | |
---|
| 309 | tIterator<const char>* iterator = inputList->getIterator(); |
---|
| 310 | const char* enumString = iterator->firstElement(); |
---|
| 311 | while (enumString != NULL) |
---|
| 312 | { |
---|
[5185] | 313 | if (strlen(enumString) >= searchLength && |
---|
[5187] | 314 | !strncasecmp(enumString, completionBegin, searchLength)) |
---|
[5178] | 315 | { |
---|
[5185] | 316 | printf("%s\n", enumString); |
---|
[5187] | 317 | ShellC_Element* newElem = new ShellC_Element; |
---|
| 318 | newElem->name = enumString; |
---|
| 319 | this->completionList->add(newElem); |
---|
[5178] | 320 | } |
---|
| 321 | enumString = iterator->nextElement(); |
---|
| 322 | } |
---|
| 323 | delete iterator; |
---|
| 324 | |
---|
[5192] | 325 | return true; |
---|
[5178] | 326 | } |
---|
| 327 | |
---|
| 328 | /** |
---|
[5187] | 329 | * searches for classes, which beginn with completionBegin |
---|
[5178] | 330 | * @param inputList the List to parse through |
---|
[5187] | 331 | * @param completionBegin the beginning string |
---|
[5178] | 332 | * !! The strings MUST NOT be deleted !! |
---|
| 333 | */ |
---|
[5192] | 334 | bool ShellCompletion::addToCompleteList(const tList<BaseObject>* inputList, const char* completionBegin) |
---|
[5178] | 335 | { |
---|
[5187] | 336 | if (inputList == NULL || completionBegin == NULL) |
---|
[5192] | 337 | return false; |
---|
[5187] | 338 | unsigned int searchLength = strlen(completionBegin); |
---|
[5178] | 339 | |
---|
| 340 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
---|
| 341 | BaseObject* enumBO = iterator->firstElement(); |
---|
| 342 | while (enumBO != NULL) |
---|
| 343 | { |
---|
| 344 | if (enumBO->getName() != NULL && |
---|
[5185] | 345 | strlen(enumBO->getName()) >= searchLength && |
---|
[5187] | 346 | !strncasecmp(enumBO->getName(), completionBegin, searchLength)) |
---|
[5178] | 347 | { |
---|
[5187] | 348 | ShellC_Element* newElem = new ShellC_Element; |
---|
| 349 | newElem->name = enumBO->getName(); |
---|
| 350 | this->completionList->add(newElem); |
---|
[5188] | 351 | printf("%s\n",enumBO->getName()); |
---|
[5178] | 352 | } |
---|
| 353 | enumBO = iterator->nextElement(); |
---|
| 354 | } |
---|
| 355 | delete iterator; |
---|
| 356 | |
---|
[5192] | 357 | return true; |
---|
[5178] | 358 | } |
---|
[5187] | 359 | |
---|
[5197] | 360 | /** |
---|
| 361 | * deletes the Completion List. |
---|
| 362 | * |
---|
| 363 | * This is done at the beginning of each completion-run |
---|
| 364 | */ |
---|
[5187] | 365 | void 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 | } |
---|