[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" |
---|
[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 |
---|
[3245] | 35 | */ |
---|
[5182] | 36 | ShellCompletion::ShellCompletion(ShellInput* input) |
---|
[3365] | 37 | { |
---|
[5181] | 38 | this->completionList = NULL; |
---|
[5182] | 39 | this->input = input; |
---|
[3365] | 40 | } |
---|
[1853] | 41 | |
---|
| 42 | |
---|
[3245] | 43 | /** |
---|
[4838] | 44 | * standard deconstructor |
---|
[3245] | 45 | */ |
---|
[5170] | 46 | ShellCompletion::~ShellCompletion () |
---|
[3543] | 47 | { |
---|
| 48 | // delete what has to be deleted here |
---|
[5181] | 49 | if (this->completionList) |
---|
| 50 | { |
---|
[5188] | 51 | this->emptyCompletionList(); |
---|
[5181] | 52 | delete this->completionList; |
---|
| 53 | } |
---|
[3543] | 54 | } |
---|
[5178] | 55 | |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * autocompletes the Shell's inputLine |
---|
| 60 | * @returns true, if a result was found, false otherwise |
---|
| 61 | */ |
---|
[5181] | 62 | bool ShellCompletion::autoComplete(ShellInput* input) |
---|
[5178] | 63 | { |
---|
[5194] | 64 | const char* completionLine; //< the inputLine we complete. |
---|
[5183] | 65 | |
---|
[5187] | 66 | long classID; //< the classID retrieved from the Class. |
---|
| 67 | tList<BaseObject>* objectList; //< the list of Objects stored in classID |
---|
[5190] | 68 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
[5194] | 69 | long completeType = SHELLC_NONE; //< the Type we'd like to complete. |
---|
| 70 | const char* completeString; //< the string to complete. |
---|
[5183] | 71 | |
---|
[5190] | 72 | |
---|
[5191] | 73 | PRINTF(5)("AutoComplete on input\n"); |
---|
[5187] | 74 | this->emptyCompletionList(); |
---|
[5185] | 75 | |
---|
[5182] | 76 | if (input != NULL) |
---|
| 77 | this->input = input; |
---|
[5184] | 78 | if (this->input == NULL) |
---|
[5185] | 79 | { |
---|
| 80 | PRINTF(2)("No ShellInput supplied\n"); |
---|
[5184] | 81 | return false; |
---|
[5185] | 82 | } |
---|
[5190] | 83 | |
---|
| 84 | // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object |
---|
[5191] | 85 | if (this->input->getInput() != NULL && |
---|
| 86 | strrchr(this->input->getInput(), ' ') >= this->input->getInput() + strlen(this->input->getInput())-1) |
---|
[5190] | 87 | { |
---|
| 88 | emptyComplete = true; |
---|
| 89 | } |
---|
| 90 | |
---|
[5193] | 91 | // CREATE INPUTS |
---|
[5185] | 92 | if (this->input->getInput() == NULL) |
---|
[5191] | 93 | completionLine = ""; |
---|
| 94 | else |
---|
| 95 | completionLine = this->input->getInput() + strspn(this->input->getInput(), " \t\n"); |
---|
[5184] | 96 | SubString inputSplits(completionLine, true); |
---|
| 97 | |
---|
[5193] | 98 | // What String will be completed |
---|
| 99 | if (emptyComplete == true) |
---|
| 100 | completeString = ""; |
---|
| 101 | else |
---|
| 102 | completeString = inputSplits.getString(inputSplits.getCount()-1); |
---|
| 103 | |
---|
[5185] | 104 | // CLASS COMPLETION |
---|
[5184] | 105 | if (inputSplits.getCount() == 0) |
---|
| 106 | { |
---|
[5193] | 107 | completeType |= SHELLC_CLASS; |
---|
[5195] | 108 | completeType |= SHELLC_ALIAS; |
---|
[5184] | 109 | } |
---|
[5191] | 110 | else if (inputSplits.getCount() == 1 && emptyComplete == false) |
---|
[5184] | 111 | { |
---|
[5193] | 112 | completeType |= SHELLC_CLASS; |
---|
[5195] | 113 | completeType |= SHELLC_ALIAS; |
---|
[5184] | 114 | } |
---|
| 115 | |
---|
[5191] | 116 | // OBJECT/FUNCTION COMPLETIONS |
---|
[5192] | 117 | else if ((inputSplits.getCount() == 1 && emptyComplete == true) || |
---|
| 118 | (inputSplits.getCount() == 2 && emptyComplete == false)) |
---|
[5184] | 119 | { |
---|
[5185] | 120 | classID = ClassList::StringToID(inputSplits.getString(0)); |
---|
[5194] | 121 | objectList = ClassList::getList(classID); |
---|
[5330] | 122 | if (classID != CL_NULL) |
---|
[5193] | 123 | completeType |= SHELLC_OBJECT; |
---|
[5330] | 124 | //if (objectList != NULL && objectList->getSize() == 1) |
---|
| 125 | completeType |= SHELLC_FUNCTION; |
---|
[5184] | 126 | } |
---|
[5194] | 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 | } |
---|
[5184] | 136 | |
---|
[5193] | 137 | if (completeType & SHELLC_CLASS) |
---|
| 138 | this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS); |
---|
| 139 | if (completeType & SHELLC_OBJECT) |
---|
| 140 | this->objectComplete(completeString, classID); |
---|
[5194] | 141 | if (completeType & SHELLC_FUNCTION) |
---|
[5330] | 142 | this->functionComplete(completeString, inputSplits.getString(0)); |
---|
[5195] | 143 | if (completeType & SHELLC_ALIAS) |
---|
| 144 | this->aliasComplete(completeString); |
---|
[5193] | 145 | |
---|
[5194] | 146 | |
---|
| 147 | this->generalComplete(completeString); |
---|
| 148 | return true; |
---|
[5178] | 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 | { |
---|
[5245] | 163 | if (!this->addToCompleteList(clList, classBegin, SHELLC_CLASS)) |
---|
[5178] | 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; |
---|
[5193] | 181 | const tList<BaseObject>* boList = ClassList::getList(classID); |
---|
[5178] | 182 | if (boList != NULL) |
---|
| 183 | { |
---|
[5245] | 184 | SHELLC_TYPE type = SHELLC_OBJECT; |
---|
| 185 | if (classID == CL_SHELL_COMMAND_CLASS) |
---|
| 186 | type = SHELLC_CLASS; |
---|
| 187 | if (!this->addToCompleteList(boList, objectBegin, type)) |
---|
[5178] | 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 |
---|
[5197] | 198 | * @param classID the class' ID to complete the function of |
---|
[5178] | 199 | */ |
---|
[5330] | 200 | bool ShellCompletion::functionComplete(const char* functionBegin, const char* className) |
---|
[5178] | 201 | { |
---|
[5194] | 202 | if (unlikely(functionBegin == NULL)) |
---|
| 203 | return false; |
---|
| 204 | tList<const char> fktList; |
---|
[5330] | 205 | ShellCommandClass::getCommandListOfClass(className, &fktList); |
---|
[5194] | 206 | //printf("%s\n", boList->firstElement()->getName()); |
---|
[5245] | 207 | if (!this->addToCompleteList(&fktList, functionBegin, SHELLC_FUNCTION)) |
---|
[5194] | 208 | return false; |
---|
| 209 | return true; |
---|
[5178] | 210 | } |
---|
| 211 | |
---|
[5197] | 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 | */ |
---|
[5195] | 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()); |
---|
[5245] | 224 | if (!this->addToCompleteList(&aliasList, aliasBegin, SHELLC_ALIAS)) |
---|
[5195] | 225 | return false; |
---|
| 226 | return true; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | |
---|
[5178] | 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 | */ |
---|
[5192] | 238 | bool ShellCompletion::generalComplete(const char* begin, const char* displayAs, const char* addBack, const char* addFront) |
---|
[5178] | 239 | { |
---|
[5192] | 240 | if (completionList == NULL || this->input == NULL ) |
---|
[5185] | 241 | return false; |
---|
[5192] | 242 | if (completionList->getSize() == 0) |
---|
[5178] | 243 | return false; |
---|
| 244 | |
---|
[5192] | 245 | ShellC_Element* addElem = completionList->firstElement(); |
---|
[5187] | 246 | const char* addString = addElem->name; |
---|
[5178] | 247 | unsigned int addLength = 0; |
---|
| 248 | unsigned int inputLenght = strlen(begin); |
---|
| 249 | |
---|
[5187] | 250 | // Determin the longest Match |
---|
[5178] | 251 | if (addString != NULL) |
---|
| 252 | addLength = strlen(addString); |
---|
[5192] | 253 | tIterator<ShellC_Element>* charIterator = completionList->getIterator(); |
---|
[5187] | 254 | ShellC_Element* charElem = charIterator->firstElement(); |
---|
[5245] | 255 | SHELLC_TYPE changeType = SHELLC_NONE; |
---|
[5178] | 256 | while (charElem != NULL) |
---|
| 257 | { |
---|
[5245] | 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); |
---|
[5178] | 266 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
[5187] | 267 | if (addString[i] != charElem->name[i]) |
---|
[5185] | 268 | { |
---|
| 269 | addLength = i; |
---|
[5245] | 270 | // break; |
---|
[5185] | 271 | } |
---|
[5178] | 272 | charElem = charIterator->nextElement(); |
---|
| 273 | } |
---|
| 274 | delete charIterator; |
---|
[5245] | 275 | PRINT(0)("\n"); |
---|
[5178] | 276 | |
---|
| 277 | if (addLength >= inputLenght) |
---|
| 278 | { |
---|
| 279 | char* adder = new char[addLength+1]; |
---|
| 280 | strncpy(adder, addString, addLength); |
---|
| 281 | adder[addLength] = '\0'; |
---|
| 282 | |
---|
[5182] | 283 | if (this->input) |
---|
| 284 | { |
---|
| 285 | this->input->removeCharacters(inputLenght); |
---|
| 286 | this->input->addCharacters(adder); |
---|
[5178] | 287 | |
---|
[5192] | 288 | if (completionList->getSize() == 1) |
---|
[5185] | 289 | { |
---|
| 290 | if ( addBack != NULL ) |
---|
| 291 | this->input->addCharacters(addBack); |
---|
| 292 | this->input->addCharacter(' '); |
---|
| 293 | } |
---|
[5182] | 294 | delete[] adder; |
---|
| 295 | } |
---|
[5178] | 296 | } |
---|
| 297 | return true; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | /** |
---|
[5187] | 301 | * searches for classes, which beginn with completionBegin |
---|
[5178] | 302 | * @param inputList the List to parse through |
---|
[5187] | 303 | * @param completionBegin the beginning string |
---|
[5178] | 304 | * !! The strings MUST NOT be deleted !! |
---|
| 305 | */ |
---|
[5245] | 306 | bool ShellCompletion::addToCompleteList(const tList<const char>* inputList, const char* completionBegin, SHELLC_TYPE type) |
---|
[5178] | 307 | { |
---|
[5187] | 308 | if (inputList == NULL || completionBegin == NULL) |
---|
[5192] | 309 | return false; |
---|
[5187] | 310 | unsigned int searchLength = strlen(completionBegin); |
---|
[5178] | 311 | |
---|
| 312 | tIterator<const char>* iterator = inputList->getIterator(); |
---|
| 313 | const char* enumString = iterator->firstElement(); |
---|
| 314 | while (enumString != NULL) |
---|
| 315 | { |
---|
[5185] | 316 | if (strlen(enumString) >= searchLength && |
---|
[5187] | 317 | !strncasecmp(enumString, completionBegin, searchLength)) |
---|
[5178] | 318 | { |
---|
[5185] | 319 | printf("%s\n", enumString); |
---|
[5187] | 320 | ShellC_Element* newElem = new ShellC_Element; |
---|
| 321 | newElem->name = enumString; |
---|
[5245] | 322 | newElem->type = type; |
---|
[5187] | 323 | this->completionList->add(newElem); |
---|
[5178] | 324 | } |
---|
| 325 | enumString = iterator->nextElement(); |
---|
| 326 | } |
---|
| 327 | delete iterator; |
---|
| 328 | |
---|
[5192] | 329 | return true; |
---|
[5178] | 330 | } |
---|
| 331 | |
---|
| 332 | /** |
---|
[5187] | 333 | * searches for classes, which beginn with completionBegin |
---|
[5178] | 334 | * @param inputList the List to parse through |
---|
[5187] | 335 | * @param completionBegin the beginning string |
---|
[5178] | 336 | * !! The strings MUST NOT be deleted !! |
---|
| 337 | */ |
---|
[5245] | 338 | bool ShellCompletion::addToCompleteList(const tList<BaseObject>* inputList, const char* completionBegin, SHELLC_TYPE type) |
---|
[5178] | 339 | { |
---|
[5187] | 340 | if (inputList == NULL || completionBegin == NULL) |
---|
[5192] | 341 | return false; |
---|
[5187] | 342 | unsigned int searchLength = strlen(completionBegin); |
---|
[5178] | 343 | |
---|
| 344 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
---|
| 345 | BaseObject* enumBO = iterator->firstElement(); |
---|
| 346 | while (enumBO != NULL) |
---|
| 347 | { |
---|
| 348 | if (enumBO->getName() != NULL && |
---|
[5185] | 349 | strlen(enumBO->getName()) >= searchLength && |
---|
[5187] | 350 | !strncasecmp(enumBO->getName(), completionBegin, searchLength)) |
---|
[5178] | 351 | { |
---|
[5187] | 352 | ShellC_Element* newElem = new ShellC_Element; |
---|
| 353 | newElem->name = enumBO->getName(); |
---|
[5245] | 354 | newElem->type = type; |
---|
[5187] | 355 | this->completionList->add(newElem); |
---|
[5178] | 356 | } |
---|
| 357 | enumBO = iterator->nextElement(); |
---|
| 358 | } |
---|
| 359 | delete iterator; |
---|
| 360 | |
---|
[5192] | 361 | return true; |
---|
[5178] | 362 | } |
---|
[5187] | 363 | |
---|
[5197] | 364 | /** |
---|
| 365 | * deletes the Completion List. |
---|
| 366 | * |
---|
| 367 | * This is done at the beginning of each completion-run |
---|
| 368 | */ |
---|
[5187] | 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 | } |
---|
[5245] | 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 | } |
---|