[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 | |
---|
[7374] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
---|
[1853] | 17 | |
---|
[5170] | 18 | #include "shell_completion.h" |
---|
[5639] | 19 | #include "shell_command_class.h" |
---|
[7388] | 20 | #include "shell_completion_plugin.h" |
---|
[1853] | 21 | |
---|
[5194] | 22 | #include "shell_command.h" |
---|
[5181] | 23 | |
---|
[5183] | 24 | #include "substring.h" |
---|
[5178] | 25 | #include "class_list.h" |
---|
| 26 | #include "debug.h" |
---|
| 27 | |
---|
[7374] | 28 | namespace OrxShell |
---|
| 29 | { |
---|
[1853] | 30 | |
---|
[7374] | 31 | /** |
---|
| 32 | * @brief standard constructor |
---|
| 33 | */ |
---|
| 34 | ShellCompletion::ShellCompletion() |
---|
| 35 | { } |
---|
[1853] | 36 | |
---|
| 37 | |
---|
[7374] | 38 | /** |
---|
| 39 | * @brief standard deconstructor |
---|
| 40 | */ |
---|
| 41 | ShellCompletion::~ShellCompletion () |
---|
| 42 | { } |
---|
[5178] | 43 | |
---|
| 44 | |
---|
| 45 | |
---|
[7374] | 46 | /** |
---|
| 47 | * @brief autocompletes the Shell's inputLine |
---|
[7406] | 48 | * @param input the input to complete, will most possibly be changed. |
---|
[7374] | 49 | * @returns true, if a result was found, false otherwise |
---|
| 50 | */ |
---|
| 51 | bool ShellCompletion::autoComplete(std::string& input) |
---|
| 52 | { |
---|
[7406] | 53 | long classID = CL_NULL; //< the classID retrieved from the Class. |
---|
| 54 | const std::list<BaseObject*>* objectList = NULL; //< the list of Objects stored in classID's ClassList |
---|
| 55 | bool emptyComplete = false; //< if the completion input is empty string. e.g "" |
---|
| 56 | long completeType = NullCompletion; //< the Type we'd like to complete. |
---|
| 57 | std::string completeString = ""; //< the string to complete. |
---|
[7415] | 58 | unsigned int completeParam = 0; //< The Parameter to finish. |
---|
[7413] | 59 | const ShellCommand* command = NULL; //< The Command. |
---|
[5183] | 60 | |
---|
[5190] | 61 | |
---|
[7374] | 62 | PRINTF(5)("AutoComplete on input\n"); |
---|
| 63 | this->clearCompletionList(); |
---|
[5185] | 64 | |
---|
[7374] | 65 | // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object |
---|
[7403] | 66 | if (input.empty() || input[input.size()-1] == ' ') |
---|
[7374] | 67 | emptyComplete = true; |
---|
[5190] | 68 | |
---|
[7374] | 69 | // CREATE INPUTS |
---|
| 70 | SubString inputSplits(input, SubString::WhiteSpacesWithComma); |
---|
[5184] | 71 | |
---|
[7374] | 72 | // What String will be completed |
---|
[7403] | 73 | if (!emptyComplete && inputSplits.size() >= 1) |
---|
[7374] | 74 | completeString = inputSplits.getString(inputSplits.size()-1); |
---|
[5193] | 75 | |
---|
[7403] | 76 | // CLASS/ALIAS COMPLETION (on first argument) |
---|
| 77 | if (inputSplits.size() == 0 || (!emptyComplete && inputSplits.size() == 1)) |
---|
[7374] | 78 | { |
---|
| 79 | completeType |= ClassCompletion; |
---|
| 80 | completeType |= AliasCompletion; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // OBJECT/FUNCTION COMPLETIONS |
---|
[7403] | 84 | else if ((emptyComplete && inputSplits.size() == 1) || |
---|
[7413] | 85 | (!emptyComplete && inputSplits.size() == 2)) |
---|
[7374] | 86 | { |
---|
| 87 | classID = ClassList::StringToID(inputSplits.getString(0)); |
---|
| 88 | objectList = ClassList::getList((ClassID)classID); |
---|
| 89 | if (classID != CL_NULL) |
---|
| 90 | completeType |= ObjectCompletion; |
---|
| 91 | completeType |= FunctionCompletion; |
---|
| 92 | } |
---|
[7404] | 93 | // Complete the last Function |
---|
[7403] | 94 | else if ((emptyComplete && inputSplits.size() == 2 ) || |
---|
| 95 | (!emptyComplete && inputSplits.size() == 3)) |
---|
[7374] | 96 | { |
---|
[7403] | 97 | if (ClassList::exists(inputSplits[0], inputSplits[1])) |
---|
[7374] | 98 | completeType |= FunctionCompletion; |
---|
| 99 | } |
---|
| 100 | |
---|
[7408] | 101 | // Looking for ParameterCompletions. |
---|
[7413] | 102 | { |
---|
[7421] | 103 | unsigned int parameterBegin; |
---|
[7414] | 104 | if ((command = ShellCommand::getCommandFromInput(inputSplits, parameterBegin)) != NULL) |
---|
[7421] | 105 | { |
---|
[7413] | 106 | completeType |= ParamCompletion; |
---|
[7421] | 107 | if (emptyComplete) |
---|
| 108 | completeParam = inputSplits.size() - parameterBegin; |
---|
| 109 | else |
---|
| 110 | completeParam = inputSplits.size() - parameterBegin - 1; |
---|
| 111 | } |
---|
[7413] | 112 | } |
---|
[7408] | 113 | |
---|
[7374] | 114 | if (completeType & ClassCompletion) |
---|
| 115 | this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS); |
---|
| 116 | if (completeType & ObjectCompletion) |
---|
| 117 | this->objectComplete(completeString, classID); |
---|
| 118 | if (completeType & FunctionCompletion) |
---|
[7409] | 119 | this->commandComplete(completeString, inputSplits[0]); |
---|
[7374] | 120 | if (completeType & AliasCompletion) |
---|
| 121 | this->aliasComplete(completeString); |
---|
[7413] | 122 | if (completeType & ParamCompletion) |
---|
[7415] | 123 | this->paramComplete(completeString, command, completeParam); |
---|
[7374] | 124 | |
---|
| 125 | this->generalComplete(input, completeString); |
---|
| 126 | return true; |
---|
[5184] | 127 | } |
---|
[7374] | 128 | |
---|
| 129 | /** |
---|
| 130 | * @brief autocompletes an ObjectName |
---|
| 131 | * @param objectBegin the beginning string of a Object |
---|
| 132 | * @param classID the ID of the Class to search for. |
---|
| 133 | * @return true on success, false otherwise |
---|
| 134 | */ |
---|
| 135 | bool ShellCompletion::objectComplete(const std::string& objectBegin, long classID) |
---|
[5184] | 136 | { |
---|
[7374] | 137 | const std::list<BaseObject*>* boList = ClassList::getList((ClassID)classID); |
---|
| 138 | if (boList != NULL) |
---|
| 139 | { |
---|
| 140 | CompletionType type = ObjectCompletion; |
---|
| 141 | if (classID == CL_SHELL_COMMAND_CLASS) |
---|
| 142 | type = ClassCompletion; |
---|
| 143 | if (!this->addToCompleteList(*boList, objectBegin, type)) |
---|
| 144 | return false; |
---|
| 145 | } |
---|
| 146 | else |
---|
| 147 | return false; |
---|
| 148 | return true; |
---|
[5184] | 149 | } |
---|
[7374] | 150 | |
---|
| 151 | /** |
---|
[7393] | 152 | * @brief completes a Command |
---|
| 153 | * @param commandBegin the beginning of the function String |
---|
[7374] | 154 | * @param classID the class' ID to complete the function of |
---|
| 155 | */ |
---|
[7393] | 156 | bool ShellCompletion::commandComplete(const std::string& commandBegin, const std::string& className) |
---|
[5194] | 157 | { |
---|
[7374] | 158 | std::list<std::string> fktList; |
---|
[7386] | 159 | ShellCommandClass::getCommandListOfClass(className, fktList); |
---|
[7393] | 160 | if (!this->addToCompleteList(fktList, commandBegin, FunctionCompletion)) |
---|
[5194] | 161 | return false; |
---|
[7374] | 162 | return true; |
---|
[5194] | 163 | } |
---|
[5184] | 164 | |
---|
[7374] | 165 | /** |
---|
| 166 | * @brief completes an Alias |
---|
| 167 | * @param aliasBegin the beginning of the Alias-String to complete |
---|
| 168 | * @returns true on succes, false if something went wrong |
---|
| 169 | */ |
---|
| 170 | bool ShellCompletion::aliasComplete(const std::string& aliasBegin) |
---|
[5178] | 171 | { |
---|
[7374] | 172 | std::list<std::string> aliasList; |
---|
[7389] | 173 | ShellCommandAlias::getCommandListOfAlias(aliasList); |
---|
[7374] | 174 | if (!this->addToCompleteList(aliasList, aliasBegin, AliasCompletion)) |
---|
[5178] | 175 | return false; |
---|
[7374] | 176 | return true; |
---|
[5178] | 177 | } |
---|
| 178 | |
---|
[7413] | 179 | /** |
---|
| 180 | * @brief completes Parameters. |
---|
| 181 | * @param paramBegin: Begin of the Parameters. |
---|
| 182 | * @returns true on succes, false if something went wrong |
---|
| 183 | */ |
---|
[7415] | 184 | bool ShellCompletion::paramComplete(const std::string& paramBegin, const ShellCommand* command, unsigned int paramNumber) |
---|
[7413] | 185 | { |
---|
[7415] | 186 | if (paramNumber >= command->getParamCount()) |
---|
| 187 | { |
---|
| 188 | PRINT(0)("Last Parameter reached\n"); |
---|
| 189 | return false; |
---|
| 190 | } |
---|
[7413] | 191 | std::vector<std::string> completed; |
---|
[7415] | 192 | command->getCompletorPlugin(paramNumber)->addToCompleteList(completed, paramBegin); |
---|
| 193 | for (unsigned int i = 0; i < completed.size(); i++) |
---|
| 194 | this->completionList.push_back(CompletionElement(completed[i], ParamCompletion)); |
---|
| 195 | return true; |
---|
[7413] | 196 | } |
---|
[7374] | 197 | |
---|
[7413] | 198 | |
---|
[7374] | 199 | /** |
---|
| 200 | * @brief completes the inputline on grounds of an inputList |
---|
| 201 | * @param input the Input to complete. |
---|
| 202 | * @param begin the String to search in the inputList, and to extend with it. |
---|
| 203 | * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::" |
---|
| 204 | * @param addBack what should be added at the end of the completion |
---|
| 205 | * @param addFront what should be added to the front of one finished completion |
---|
| 206 | * @return true if ok, false otherwise |
---|
| 207 | */ |
---|
| 208 | bool ShellCompletion::generalComplete(std::string& input, |
---|
| 209 | const std::string& begin, const std::string& displayAs, |
---|
| 210 | const std::string& addBack, const std::string& addFront) |
---|
[5178] | 211 | { |
---|
[7374] | 212 | if (completionList.size() == 0) |
---|
[5178] | 213 | return false; |
---|
| 214 | |
---|
[7374] | 215 | CompletionElement addElem = completionList.front(); |
---|
| 216 | const std::string& addString = addElem.name; |
---|
[7413] | 217 | unsigned int addLength = addString.size(); |
---|
[7374] | 218 | unsigned int inputLenght = begin.size(); |
---|
[5178] | 219 | |
---|
[7413] | 220 | // Determin the longest Match (starting with the first candidate in full length). |
---|
[7374] | 221 | CompletionType changeType = NullCompletion; |
---|
| 222 | std::vector<CompletionElement>::iterator charIT; |
---|
| 223 | for (charIT = completionList.begin(); charIT != completionList.end(); charIT++) |
---|
| 224 | { |
---|
[7413] | 225 | printf("== %s\n", (*charIT).name.c_str()); |
---|
[7374] | 226 | if ((*charIT).type != changeType) |
---|
| 227 | { |
---|
| 228 | if (changeType != NullCompletion) |
---|
| 229 | PRINT(0)("\n"); |
---|
| 230 | PRINT(0)("%s: ", ShellCompletion::typeToString((*charIT).type).c_str()); |
---|
| 231 | changeType = (*charIT).type; |
---|
| 232 | } |
---|
| 233 | PRINTF(0)("%s ", (*charIT).name.c_str()); |
---|
| 234 | for (unsigned int i = inputLenght; i < addLength; i++) |
---|
| 235 | if (addString[i] != (*charIT).name[i]) |
---|
| 236 | addLength = i; |
---|
| 237 | } |
---|
| 238 | PRINT(0)("\n"); |
---|
[5195] | 239 | |
---|
[7374] | 240 | if (addLength >= inputLenght) |
---|
| 241 | { |
---|
| 242 | std::string adder = addString; |
---|
| 243 | adder.resize(addLength); |
---|
[5178] | 244 | |
---|
[7374] | 245 | input.resize(input.size()-inputLenght); |
---|
| 246 | input += adder; |
---|
[5178] | 247 | |
---|
[7374] | 248 | if (completionList.size() == 1) |
---|
| 249 | { |
---|
| 250 | if ( addBack != "") |
---|
| 251 | input += addBack; |
---|
| 252 | input += ' '; |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | return true; |
---|
| 256 | } |
---|
[5779] | 257 | |
---|
[7374] | 258 | /** |
---|
| 259 | * @brief searches for classes, which beginn with completionBegin |
---|
| 260 | * @param inputList the List to parse through |
---|
| 261 | * @param completionBegin the beginning string |
---|
| 262 | * !! The strings MUST NOT be deleted !! |
---|
| 263 | */ |
---|
| 264 | bool ShellCompletion::addToCompleteList(const std::list<std::string>& inputList, const std::string& completionBegin, CompletionType type) |
---|
[5178] | 265 | { |
---|
[7374] | 266 | unsigned int searchLength = completionBegin.size(); |
---|
| 267 | |
---|
| 268 | std::list<std::string>::const_iterator string; |
---|
| 269 | for (string = inputList.begin(); string != inputList.end(); string++) |
---|
[5245] | 270 | { |
---|
[7374] | 271 | if ((*string).size() >= searchLength && |
---|
| 272 | !nocaseCmp(*string, completionBegin, searchLength)) |
---|
[5185] | 273 | { |
---|
[7412] | 274 | this->completionList.push_back(CompletionElement (*string, type)); |
---|
[5185] | 275 | } |
---|
[7374] | 276 | } |
---|
| 277 | return true; |
---|
[5178] | 278 | } |
---|
| 279 | |
---|
[7374] | 280 | /** |
---|
| 281 | * @brief searches for classes, which beginn with completionBegin |
---|
| 282 | * @param inputList the List to parse through |
---|
| 283 | * @param completionBegin the beginning string |
---|
| 284 | * !! The strings MUST NOT be deleted !! |
---|
| 285 | */ |
---|
| 286 | bool ShellCompletion::addToCompleteList(const std::list<BaseObject*>& inputList, const std::string& completionBegin, CompletionType type) |
---|
[5178] | 287 | { |
---|
[7374] | 288 | unsigned int searchLength = completionBegin.size(); |
---|
[5178] | 289 | |
---|
[7374] | 290 | std::list<BaseObject*>::const_iterator bo; |
---|
| 291 | for(bo = inputList.begin(); bo != inputList.end(); bo++) |
---|
[5182] | 292 | { |
---|
[7374] | 293 | if ((*bo)->getName() != NULL && |
---|
| 294 | strlen((*bo)->getName()) >= searchLength && |
---|
| 295 | !nocaseCmp((*bo)->getName(), completionBegin, searchLength)) |
---|
| 296 | { |
---|
[7475] | 297 | // set all the spaces to "\\ " |
---|
| 298 | std::string name = (*bo)->getName(); |
---|
| 299 | for(unsigned int i = 0; i < name.size(); i++) |
---|
| 300 | { |
---|
| 301 | if (name[i] == ' ') |
---|
| 302 | name.replace(i++, 1, "\\ "); |
---|
| 303 | } |
---|
| 304 | this->completionList.push_back(CompletionElement(name, type)); |
---|
[7374] | 305 | } |
---|
[5182] | 306 | } |
---|
[7374] | 307 | |
---|
| 308 | return true; |
---|
[5178] | 309 | } |
---|
| 310 | |
---|
[7374] | 311 | /** |
---|
| 312 | * @brief deletes the Completion List. |
---|
| 313 | * |
---|
| 314 | * This is done at the beginning of each completion-run |
---|
| 315 | */ |
---|
| 316 | void ShellCompletion::clearCompletionList() |
---|
[5178] | 317 | { |
---|
[7374] | 318 | this->completionList.clear(); |
---|
[5178] | 319 | } |
---|
| 320 | |
---|
[7374] | 321 | const std::string& ShellCompletion::typeToString(CompletionType type) |
---|
[5178] | 322 | { |
---|
[7374] | 323 | switch (type) |
---|
[5178] | 324 | { |
---|
[7421] | 325 | default:// SHELLC_NONE |
---|
[7374] | 326 | return typeNames[0]; |
---|
[7421] | 327 | case ClassCompletion: |
---|
[7374] | 328 | return typeNames[1]; |
---|
[7421] | 329 | case ObjectCompletion: |
---|
[7374] | 330 | return typeNames[2]; |
---|
[7421] | 331 | case FunctionCompletion: |
---|
[7374] | 332 | return typeNames[3]; |
---|
[7421] | 333 | case AliasCompletion: |
---|
[7374] | 334 | return typeNames[4]; |
---|
[7421] | 335 | case ParamCompletion: |
---|
[7412] | 336 | return typeNames[5]; |
---|
[5178] | 337 | } |
---|
| 338 | } |
---|
| 339 | |
---|
[5187] | 340 | |
---|
[7374] | 341 | const std::string ShellCompletion::typeNames[] = |
---|
| 342 | { |
---|
| 343 | "error", |
---|
| 344 | "class", |
---|
| 345 | "object", |
---|
| 346 | "function", |
---|
[7412] | 347 | "alias", |
---|
| 348 | "parameter", |
---|
[7374] | 349 | }; |
---|
[5245] | 350 | |
---|
| 351 | } |
---|