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