| 1 | /* |
|---|
| 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. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
|---|
| 17 | |
|---|
| 18 | #include "shell_completion.h" |
|---|
| 19 | #include "shell_command_class.h" |
|---|
| 20 | #include "shell_completion_plugin.h" |
|---|
| 21 | |
|---|
| 22 | #include "shell_command.h" |
|---|
| 23 | |
|---|
| 24 | #include "substring.h" |
|---|
| 25 | #include "class_list.h" |
|---|
| 26 | #include "debug.h" |
|---|
| 27 | |
|---|
| 28 | namespace OrxShell |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * @brief standard constructor |
|---|
| 33 | */ |
|---|
| 34 | ShellCompletion::ShellCompletion() |
|---|
| 35 | { } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * @brief standard deconstructor |
|---|
| 40 | */ |
|---|
| 41 | ShellCompletion::~ShellCompletion () |
|---|
| 42 | { } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * @brief autocompletes the Shell's inputLine |
|---|
| 48 | * @param input the input to complete, will most possibly be changed. |
|---|
| 49 | * @returns true, if a result was found, false otherwise |
|---|
| 50 | */ |
|---|
| 51 | bool ShellCompletion::autoComplete(std::string& input) |
|---|
| 52 | { |
|---|
| 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. |
|---|
| 58 | unsigned int completeParam = 0; //< The Parameter to finish. |
|---|
| 59 | const ShellCommand* command = NULL; //< The Command. |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | PRINTF(5)("AutoComplete on input\n"); |
|---|
| 63 | this->clearCompletionList(); |
|---|
| 64 | |
|---|
| 65 | // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object |
|---|
| 66 | if (input.empty() || input[input.size()-1] == ' ') |
|---|
| 67 | emptyComplete = true; |
|---|
| 68 | |
|---|
| 69 | // CREATE INPUTS |
|---|
| 70 | SubString inputSplits(input, SubString::WhiteSpacesWithComma); |
|---|
| 71 | |
|---|
| 72 | // What String will be completed |
|---|
| 73 | if (!emptyComplete && inputSplits.size() >= 1) |
|---|
| 74 | completeString = inputSplits.getString(inputSplits.size()-1); |
|---|
| 75 | |
|---|
| 76 | // CLASS/ALIAS COMPLETION (on first argument) |
|---|
| 77 | if (inputSplits.size() == 0 || (!emptyComplete && inputSplits.size() == 1)) |
|---|
| 78 | { |
|---|
| 79 | completeType |= ClassCompletion; |
|---|
| 80 | completeType |= AliasCompletion; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | // OBJECT/FUNCTION COMPLETIONS |
|---|
| 84 | else if ((emptyComplete && inputSplits.size() == 1) || |
|---|
| 85 | (!emptyComplete && inputSplits.size() == 2)) |
|---|
| 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 | } |
|---|
| 93 | // Complete the last Function |
|---|
| 94 | else if ((emptyComplete && inputSplits.size() == 2 ) || |
|---|
| 95 | (!emptyComplete && inputSplits.size() == 3)) |
|---|
| 96 | { |
|---|
| 97 | if (ClassList::exists(inputSplits[0], inputSplits[1])) |
|---|
| 98 | completeType |= FunctionCompletion; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | // Looking for ParameterCompletions. |
|---|
| 102 | { |
|---|
| 103 | unsigned int parameterBegin; |
|---|
| 104 | if ((command = ShellCommand::getCommandFromInput(inputSplits, parameterBegin)) != NULL) |
|---|
| 105 | { |
|---|
| 106 | completeType |= ParamCompletion; |
|---|
| 107 | if (emptyComplete) |
|---|
| 108 | completeParam = inputSplits.size() - parameterBegin; |
|---|
| 109 | else |
|---|
| 110 | completeParam = inputSplits.size() - parameterBegin - 1; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 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) |
|---|
| 119 | this->commandComplete(completeString, inputSplits[0]); |
|---|
| 120 | if (completeType & AliasCompletion) |
|---|
| 121 | this->aliasComplete(completeString); |
|---|
| 122 | if (completeType & ParamCompletion) |
|---|
| 123 | this->paramComplete(completeString, command, completeParam); |
|---|
| 124 | |
|---|
| 125 | this->generalComplete(input, completeString); |
|---|
| 126 | return true; |
|---|
| 127 | } |
|---|
| 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) |
|---|
| 136 | { |
|---|
| 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; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * @brief completes a Command |
|---|
| 153 | * @param commandBegin the beginning of the function String |
|---|
| 154 | * @param classID the class' ID to complete the function of |
|---|
| 155 | */ |
|---|
| 156 | bool ShellCompletion::commandComplete(const std::string& commandBegin, const std::string& className) |
|---|
| 157 | { |
|---|
| 158 | std::list<std::string> fktList; |
|---|
| 159 | ShellCommandClass::getCommandListOfClass(className, fktList); |
|---|
| 160 | if (!this->addToCompleteList(fktList, commandBegin, FunctionCompletion)) |
|---|
| 161 | return false; |
|---|
| 162 | return true; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 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) |
|---|
| 171 | { |
|---|
| 172 | std::list<std::string> aliasList; |
|---|
| 173 | ShellCommandAlias::getCommandListOfAlias(aliasList); |
|---|
| 174 | if (!this->addToCompleteList(aliasList, aliasBegin, AliasCompletion)) |
|---|
| 175 | return false; |
|---|
| 176 | return true; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /** |
|---|
| 180 | * @brief completes Parameters. |
|---|
| 181 | * @param paramBegin: Begin of the Parameters. |
|---|
| 182 | * @returns true on succes, false if something went wrong |
|---|
| 183 | */ |
|---|
| 184 | bool ShellCompletion::paramComplete(const std::string& paramBegin, const ShellCommand* command, unsigned int paramNumber) |
|---|
| 185 | { |
|---|
| 186 | if (paramNumber >= command->getParamCount()) |
|---|
| 187 | { |
|---|
| 188 | PRINT(0)("Last Parameter reached\n"); |
|---|
| 189 | return false; |
|---|
| 190 | } |
|---|
| 191 | std::vector<std::string> completed; |
|---|
| 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; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 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) |
|---|
| 211 | { |
|---|
| 212 | if (completionList.size() == 0) |
|---|
| 213 | return false; |
|---|
| 214 | |
|---|
| 215 | CompletionElement addElem = completionList.front(); |
|---|
| 216 | const std::string& addString = addElem.name; |
|---|
| 217 | unsigned int addLength = addString.size(); |
|---|
| 218 | unsigned int inputLenght = begin.size(); |
|---|
| 219 | |
|---|
| 220 | // Determin the longest Match (starting with the first candidate in full length). |
|---|
| 221 | CompletionType changeType = NullCompletion; |
|---|
| 222 | std::vector<CompletionElement>::iterator charIT; |
|---|
| 223 | for (charIT = completionList.begin(); charIT != completionList.end(); charIT++) |
|---|
| 224 | { |
|---|
| 225 | if ((*charIT).type != changeType) |
|---|
| 226 | { |
|---|
| 227 | if (changeType != NullCompletion) |
|---|
| 228 | PRINT(0)("\n"); |
|---|
| 229 | PRINT(0)("%s: ", ShellCompletion::typeToString((*charIT).type).c_str()); |
|---|
| 230 | changeType = (*charIT).type; |
|---|
| 231 | } |
|---|
| 232 | PRINTF(0)("%s ", (*charIT).name.c_str()); |
|---|
| 233 | for (unsigned int i = inputLenght; i < addLength; i++) |
|---|
| 234 | if (addString[i] != (*charIT).name[i]) |
|---|
| 235 | addLength = i; |
|---|
| 236 | } |
|---|
| 237 | PRINT(0)("\n"); |
|---|
| 238 | |
|---|
| 239 | if (addLength >= inputLenght) |
|---|
| 240 | { |
|---|
| 241 | std::string adder = addString; |
|---|
| 242 | adder.resize(addLength); |
|---|
| 243 | |
|---|
| 244 | input.resize(input.size()-inputLenght); |
|---|
| 245 | input += adder; |
|---|
| 246 | |
|---|
| 247 | if (completionList.size() == 1) |
|---|
| 248 | { |
|---|
| 249 | if ( addBack != "") |
|---|
| 250 | input += addBack; |
|---|
| 251 | input += ' '; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | return true; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | /** |
|---|
| 258 | * @brief searches for classes, which beginn with completionBegin |
|---|
| 259 | * @param inputList the List to parse through |
|---|
| 260 | * @param completionBegin the beginning string |
|---|
| 261 | * !! The strings MUST NOT be deleted !! |
|---|
| 262 | */ |
|---|
| 263 | bool ShellCompletion::addToCompleteList(const std::list<std::string>& inputList, const std::string& completionBegin, CompletionType type) |
|---|
| 264 | { |
|---|
| 265 | unsigned int searchLength = completionBegin.size(); |
|---|
| 266 | |
|---|
| 267 | std::list<std::string>::const_iterator string; |
|---|
| 268 | for (string = inputList.begin(); string != inputList.end(); string++) |
|---|
| 269 | { |
|---|
| 270 | if ((*string).size() >= searchLength && |
|---|
| 271 | !nocaseCmp(*string, completionBegin, searchLength)) |
|---|
| 272 | { |
|---|
| 273 | this->completionList.push_back(CompletionElement (*string, type)); |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | return true; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /** |
|---|
| 280 | * @brief searches for classes, which beginn with completionBegin |
|---|
| 281 | * @param inputList the List to parse through |
|---|
| 282 | * @param completionBegin the beginning string |
|---|
| 283 | * !! The strings MUST NOT be deleted !! |
|---|
| 284 | */ |
|---|
| 285 | bool ShellCompletion::addToCompleteList(const std::list<BaseObject*>& inputList, const std::string& completionBegin, CompletionType type) |
|---|
| 286 | { |
|---|
| 287 | unsigned int searchLength = completionBegin.size(); |
|---|
| 288 | |
|---|
| 289 | std::list<BaseObject*>::const_iterator bo; |
|---|
| 290 | for(bo = inputList.begin(); bo != inputList.end(); bo++) |
|---|
| 291 | { |
|---|
| 292 | if ((*bo)->getName().size() >= searchLength && |
|---|
| 293 | !nocaseCmp((*bo)->getName(), completionBegin, searchLength)) |
|---|
| 294 | { |
|---|
| 295 | // set all the spaces to "\\ " |
|---|
| 296 | std::string name = (*bo)->getName(); |
|---|
| 297 | for(unsigned int i = 0; i < name.size(); i++) |
|---|
| 298 | { |
|---|
| 299 | if (name[i] == ' ') |
|---|
| 300 | name.replace(i++, 1, "\\ "); |
|---|
| 301 | } |
|---|
| 302 | this->completionList.push_back(CompletionElement(name, type)); |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | return true; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | /** |
|---|
| 310 | * @brief deletes the Completion List. |
|---|
| 311 | * |
|---|
| 312 | * This is done at the beginning of each completion-run |
|---|
| 313 | */ |
|---|
| 314 | void ShellCompletion::clearCompletionList() |
|---|
| 315 | { |
|---|
| 316 | this->completionList.clear(); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | const std::string& ShellCompletion::typeToString(CompletionType type) |
|---|
| 320 | { |
|---|
| 321 | switch (type) |
|---|
| 322 | { |
|---|
| 323 | default:// SHELLC_NONE |
|---|
| 324 | return typeNames[0]; |
|---|
| 325 | case ClassCompletion: |
|---|
| 326 | return typeNames[1]; |
|---|
| 327 | case ObjectCompletion: |
|---|
| 328 | return typeNames[2]; |
|---|
| 329 | case FunctionCompletion: |
|---|
| 330 | return typeNames[3]; |
|---|
| 331 | case AliasCompletion: |
|---|
| 332 | return typeNames[4]; |
|---|
| 333 | case ParamCompletion: |
|---|
| 334 | return typeNames[5]; |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | const std::string ShellCompletion::typeNames[] = |
|---|
| 340 | { |
|---|
| 341 | "error", |
|---|
| 342 | "class", |
|---|
| 343 | "object", |
|---|
| 344 | "function", |
|---|
| 345 | "alias", |
|---|
| 346 | "parameter", |
|---|
| 347 | }; |
|---|
| 348 | |
|---|
| 349 | } |
|---|