[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 | */ |
---|
[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; |
---|
| 110 | |
---|
[5184] | 111 | } |
---|
[5191] | 112 | else if (inputSplits.getCount() == 1 && emptyComplete == false) |
---|
[5184] | 113 | { |
---|
[5193] | 114 | completeType |= SHELLC_CLASS; |
---|
[5195] | 115 | completeType |= SHELLC_ALIAS; |
---|
[5184] | 116 | } |
---|
| 117 | |
---|
[5191] | 118 | // OBJECT/FUNCTION COMPLETIONS |
---|
[5192] | 119 | else if ((inputSplits.getCount() == 1 && emptyComplete == true) || |
---|
| 120 | (inputSplits.getCount() == 2 && emptyComplete == false)) |
---|
[5184] | 121 | { |
---|
[5185] | 122 | classID = ClassList::StringToID(inputSplits.getString(0)); |
---|
[5194] | 123 | objectList = ClassList::getList(classID); |
---|
[5185] | 124 | if (classID == CL_NULL) |
---|
| 125 | return false; |
---|
[5192] | 126 | else |
---|
[5186] | 127 | { |
---|
[5194] | 128 | if (objectList != NULL && objectList->getSize() == 1) |
---|
| 129 | completeType |= SHELLC_FUNCTION; |
---|
[5193] | 130 | completeType |= SHELLC_OBJECT; |
---|
[5186] | 131 | } |
---|
[5184] | 132 | } |
---|
[5194] | 133 | else if ((inputSplits.getCount() == 2 && emptyComplete == true) || |
---|
| 134 | (inputSplits.getCount() == 3 && emptyComplete == false)) |
---|
| 135 | { |
---|
| 136 | classID = ClassList::StringToID(inputSplits.getString(0)); |
---|
| 137 | if (classID == CL_NULL) |
---|
| 138 | return false; |
---|
| 139 | else |
---|
| 140 | completeType |= SHELLC_FUNCTION; |
---|
| 141 | } |
---|
[5184] | 142 | |
---|
[5193] | 143 | if (completeType & SHELLC_CLASS) |
---|
| 144 | this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS); |
---|
| 145 | if (completeType & SHELLC_OBJECT) |
---|
| 146 | this->objectComplete(completeString, classID); |
---|
[5194] | 147 | if (completeType & SHELLC_FUNCTION) |
---|
| 148 | this->functionComplete(completeString, classID); |
---|
[5195] | 149 | if (completeType & SHELLC_ALIAS) |
---|
| 150 | this->aliasComplete(completeString); |
---|
[5193] | 151 | |
---|
[5194] | 152 | |
---|
| 153 | this->generalComplete(completeString); |
---|
| 154 | return true; |
---|
[5178] | 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * autocompletes a className |
---|
| 159 | * @param classBegin the Beginning of a String to autoComplete |
---|
| 160 | * @return true on success, false otherwise |
---|
| 161 | */ |
---|
| 162 | bool ShellCompletion::classComplete(const char* classBegin) |
---|
| 163 | { |
---|
| 164 | if (unlikely(classBegin == NULL)) |
---|
| 165 | return false; |
---|
| 166 | const tList<const char>* clList = ClassList::getClassList(); |
---|
| 167 | if (clList != NULL) |
---|
| 168 | { |
---|
[5245] | 169 | if (!this->addToCompleteList(clList, classBegin, SHELLC_CLASS)) |
---|
[5178] | 170 | return false; |
---|
| 171 | } |
---|
| 172 | else |
---|
| 173 | return false; |
---|
| 174 | return true; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * autocompletes an ObjectName |
---|
| 179 | * @param objectBegin the beginning string of a Object |
---|
| 180 | * @param classID the ID of the Class to search for. |
---|
| 181 | * @return true on success, false otherwise |
---|
| 182 | */ |
---|
| 183 | bool ShellCompletion::objectComplete(const char* objectBegin, long classID) |
---|
| 184 | { |
---|
| 185 | if (unlikely(objectBegin == NULL)) |
---|
| 186 | return false; |
---|
[5193] | 187 | const tList<BaseObject>* boList = ClassList::getList(classID); |
---|
[5178] | 188 | if (boList != NULL) |
---|
| 189 | { |
---|
[5245] | 190 | SHELLC_TYPE type = SHELLC_OBJECT; |
---|
| 191 | if (classID == CL_SHELL_COMMAND_CLASS) |
---|
| 192 | type = SHELLC_CLASS; |
---|
| 193 | if (!this->addToCompleteList(boList, objectBegin, type)) |
---|
[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()); |
---|
[5245] | 213 | if (!this->addToCompleteList(&fktList, functionBegin, SHELLC_FUNCTION)) |
---|
[5194] | 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()); |
---|
[5245] | 230 | if (!this->addToCompleteList(&aliasList, aliasBegin, SHELLC_ALIAS)) |
---|
[5195] | 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(); |
---|
[5245] | 261 | SHELLC_TYPE changeType = SHELLC_NONE; |
---|
[5178] | 262 | while (charElem != NULL) |
---|
| 263 | { |
---|
[5245] | 264 | if (charElem->type != changeType) |
---|
| 265 | { |
---|
| 266 | if (changeType != SHELLC_NONE) |
---|
| 267 | PRINT(0)("\n"); |
---|
| 268 | PRINT(0)("%s: ", ShellCompletion::typeToString(charElem->type)); |
---|
| 269 | changeType = charElem->type; |
---|
| 270 | } |
---|
| 271 | PRINTF(0)("%s ", charElem->name); |
---|
[5178] | 272 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
[5187] | 273 | if (addString[i] != charElem->name[i]) |
---|
[5185] | 274 | { |
---|
| 275 | addLength = i; |
---|
[5245] | 276 | // break; |
---|
[5185] | 277 | } |
---|
[5178] | 278 | charElem = charIterator->nextElement(); |
---|
| 279 | } |
---|
| 280 | delete charIterator; |
---|
[5245] | 281 | PRINT(0)("\n"); |
---|
[5178] | 282 | |
---|
| 283 | if (addLength >= inputLenght) |
---|
| 284 | { |
---|
| 285 | char* adder = new char[addLength+1]; |
---|
| 286 | strncpy(adder, addString, addLength); |
---|
| 287 | adder[addLength] = '\0'; |
---|
| 288 | |
---|
[5182] | 289 | if (this->input) |
---|
| 290 | { |
---|
| 291 | this->input->removeCharacters(inputLenght); |
---|
| 292 | this->input->addCharacters(adder); |
---|
[5178] | 293 | |
---|
[5192] | 294 | if (completionList->getSize() == 1) |
---|
[5185] | 295 | { |
---|
| 296 | if ( addBack != NULL ) |
---|
| 297 | this->input->addCharacters(addBack); |
---|
| 298 | this->input->addCharacter(' '); |
---|
| 299 | } |
---|
[5182] | 300 | delete[] adder; |
---|
| 301 | } |
---|
[5178] | 302 | } |
---|
| 303 | return true; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | /** |
---|
[5187] | 307 | * searches for classes, which beginn with completionBegin |
---|
[5178] | 308 | * @param inputList the List to parse through |
---|
[5187] | 309 | * @param completionBegin the beginning string |
---|
[5178] | 310 | * !! The strings MUST NOT be deleted !! |
---|
| 311 | */ |
---|
[5245] | 312 | bool ShellCompletion::addToCompleteList(const tList<const char>* inputList, const char* completionBegin, SHELLC_TYPE type) |
---|
[5178] | 313 | { |
---|
[5187] | 314 | if (inputList == NULL || completionBegin == NULL) |
---|
[5192] | 315 | return false; |
---|
[5187] | 316 | unsigned int searchLength = strlen(completionBegin); |
---|
[5178] | 317 | |
---|
| 318 | tIterator<const char>* iterator = inputList->getIterator(); |
---|
| 319 | const char* enumString = iterator->firstElement(); |
---|
| 320 | while (enumString != NULL) |
---|
| 321 | { |
---|
[5185] | 322 | if (strlen(enumString) >= searchLength && |
---|
[5187] | 323 | !strncasecmp(enumString, completionBegin, searchLength)) |
---|
[5178] | 324 | { |
---|
[5185] | 325 | printf("%s\n", enumString); |
---|
[5187] | 326 | ShellC_Element* newElem = new ShellC_Element; |
---|
| 327 | newElem->name = enumString; |
---|
[5245] | 328 | newElem->type = type; |
---|
[5187] | 329 | this->completionList->add(newElem); |
---|
[5178] | 330 | } |
---|
| 331 | enumString = iterator->nextElement(); |
---|
| 332 | } |
---|
| 333 | delete iterator; |
---|
| 334 | |
---|
[5192] | 335 | return true; |
---|
[5178] | 336 | } |
---|
| 337 | |
---|
| 338 | /** |
---|
[5187] | 339 | * searches for classes, which beginn with completionBegin |
---|
[5178] | 340 | * @param inputList the List to parse through |
---|
[5187] | 341 | * @param completionBegin the beginning string |
---|
[5178] | 342 | * !! The strings MUST NOT be deleted !! |
---|
| 343 | */ |
---|
[5245] | 344 | bool ShellCompletion::addToCompleteList(const tList<BaseObject>* inputList, const char* completionBegin, SHELLC_TYPE type) |
---|
[5178] | 345 | { |
---|
[5187] | 346 | if (inputList == NULL || completionBegin == NULL) |
---|
[5192] | 347 | return false; |
---|
[5187] | 348 | unsigned int searchLength = strlen(completionBegin); |
---|
[5178] | 349 | |
---|
| 350 | tIterator<BaseObject>* iterator = inputList->getIterator(); |
---|
| 351 | BaseObject* enumBO = iterator->firstElement(); |
---|
| 352 | while (enumBO != NULL) |
---|
| 353 | { |
---|
| 354 | if (enumBO->getName() != NULL && |
---|
[5185] | 355 | strlen(enumBO->getName()) >= searchLength && |
---|
[5187] | 356 | !strncasecmp(enumBO->getName(), completionBegin, searchLength)) |
---|
[5178] | 357 | { |
---|
[5187] | 358 | ShellC_Element* newElem = new ShellC_Element; |
---|
| 359 | newElem->name = enumBO->getName(); |
---|
[5245] | 360 | newElem->type = type; |
---|
[5187] | 361 | this->completionList->add(newElem); |
---|
[5178] | 362 | } |
---|
| 363 | enumBO = iterator->nextElement(); |
---|
| 364 | } |
---|
| 365 | delete iterator; |
---|
| 366 | |
---|
[5192] | 367 | return true; |
---|
[5178] | 368 | } |
---|
[5187] | 369 | |
---|
[5197] | 370 | /** |
---|
| 371 | * deletes the Completion List. |
---|
| 372 | * |
---|
| 373 | * This is done at the beginning of each completion-run |
---|
| 374 | */ |
---|
[5187] | 375 | void ShellCompletion::emptyCompletionList() |
---|
| 376 | { |
---|
| 377 | if (this->completionList != NULL) |
---|
| 378 | { |
---|
| 379 | tIterator<ShellC_Element>* elemIT = this->completionList->getIterator(); |
---|
| 380 | ShellC_Element* elem = elemIT->firstElement(); |
---|
| 381 | while (elem != NULL) |
---|
| 382 | { |
---|
| 383 | delete elem; |
---|
| 384 | elem = elemIT->nextElement(); |
---|
| 385 | } |
---|
| 386 | delete this->completionList; |
---|
| 387 | } |
---|
| 388 | this->completionList = new tList<ShellC_Element>; |
---|
| 389 | } |
---|
[5245] | 390 | |
---|
| 391 | const char* ShellCompletion::typeToString(SHELLC_TYPE type) |
---|
| 392 | { |
---|
| 393 | switch (type) |
---|
| 394 | { |
---|
| 395 | default:// SHELLC_NONE |
---|
| 396 | return "error"; |
---|
| 397 | case SHELLC_CLASS: |
---|
| 398 | return "class"; |
---|
| 399 | case SHELLC_OBJECT: |
---|
| 400 | return "object"; |
---|
| 401 | case SHELLC_FUNCTION: |
---|
| 402 | return "function"; |
---|
| 403 | case SHELLC_ALIAS: |
---|
| 404 | return "alias"; |
---|
| 405 | } |
---|
| 406 | } |
---|