/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ #include "shell_completion.h" #include "shell_command_class.h" #include "shell_input.h" #include "shell_command.h" #include "substring.h" #include "class_list.h" #include "debug.h" using namespace std; /** * @brief standard constructor */ ShellCompletion::ShellCompletion() { } /** * @brief standard deconstructor */ ShellCompletion::~ShellCompletion () { } /** * @brief autocompletes the Shell's inputLine * @param input the input to complete. * @returns true, if a result was found, false otherwise */ bool ShellCompletion::autoComplete(std::string& input) { const char* completionLine; //< the inputLine we complete. long classID; //< the classID retrieved from the Class. const std::list* objectList; //< the list of Objects stored in classID bool emptyComplete = false; //< if the completion input is empty string. e.g "" long completeType = NullCompletion; //< the Type we'd like to complete. std::string completeString; //< the string to complete. PRINTF(5)("AutoComplete on input\n"); this->clearCompletionList(); // Check if we are in a input. eg. the supplied string "class " and now we complete either function or object if (input[input.size()-1] == ' ') emptyComplete = true; // CREATE INPUTS SubString inputSplits(input, SubString::WhiteSpacesWithComma); // What String will be completed if (emptyComplete == true) completeString = ""; else completeString = inputSplits.getString(inputSplits.size()-1); // CLASS COMPLETION if (inputSplits.size() == 0) { completeType |= ClassCompletion; completeType |= AliasCompletion; } else if (inputSplits.size() == 1 && emptyComplete == false) { completeType |= ClassCompletion; completeType |= AliasCompletion; } // OBJECT/FUNCTION COMPLETIONS else if ((inputSplits.size() == 1 && emptyComplete == true) || (inputSplits.size() == 2 && emptyComplete == false)) { classID = ClassList::StringToID(inputSplits.getString(0)); objectList = ClassList::getList((ClassID)classID); if (classID != CL_NULL) completeType |= ObjectCompletion; //if (objectList != NULL && objectList->getSize() == 1) completeType |= FunctionCompletion; } else if ((inputSplits.size() == 2 && emptyComplete == true) || (inputSplits.size() == 3 && emptyComplete == false)) { classID = ClassList::StringToID(inputSplits.getString(0)); if (classID == CL_NULL) return false; else completeType |= FunctionCompletion; } if (completeType & ClassCompletion) this->objectComplete(completeString, CL_SHELL_COMMAND_CLASS); if (completeType & ObjectCompletion) this->objectComplete(completeString, classID); if (completeType & FunctionCompletion) this->functionComplete(completeString, inputSplits.getString(0)); if (completeType & AliasCompletion) this->aliasComplete(completeString); this->generalComplete(input, completeString); return true; } /** * @brief autocompletes a className * @param classBegin the Beginning of a String to autoComplete * @return true on success, false otherwise */ bool ShellCompletion::classComplete(const std::string& classBegin) { const std::list* clList = ClassList::getClassNames(); if (clList != NULL) { if (!this->addToCompleteList(*clList, classBegin, ClassCompletion)) return false; } else return false; return true; } /** * @brief autocompletes an ObjectName * @param objectBegin the beginning string of a Object * @param classID the ID of the Class to search for. * @return true on success, false otherwise */ bool ShellCompletion::objectComplete(const std::string& objectBegin, long classID) { const std::list* boList = ClassList::getList((ClassID)classID); if (boList != NULL) { CompletionType type = ObjectCompletion; if (classID == CL_SHELL_COMMAND_CLASS) type = ClassCompletion; if (!this->addToCompleteList(*boList, objectBegin, type)) return false; } else return false; return true; } /** * @brief completes a Function * @param functionBegin the beginning of the function String * @param classID the class' ID to complete the function of */ bool ShellCompletion::functionComplete(const std::string& functionBegin, const std::string& className) { std::list fktList; ShellCommandClass::getCommandListOfClass(className, &fktList); //printf("%s\n", boList->firstElement()->getName()); if (!this->addToCompleteList(fktList, functionBegin, FunctionCompletion)) return false; return true; } /** * @brief completes an Alias * @param aliasBegin the beginning of the Alias-String to complete * @returns true on succes, false if something went wrong */ bool ShellCompletion::aliasComplete(const std::string& aliasBegin) { std::list aliasList; ShellCommandClass::getCommandListOfAlias(&aliasList); //printf("%s\n", boList->firstElement()->getName()); if (!this->addToCompleteList(aliasList, aliasBegin, AliasCompletion)) return false; return true; } /** * @brief completes the inputline on grounds of an inputList * @param input the Input to complete. * @param begin the String to search in the inputList, and to extend with it. * @param displayAs how to display the found value to the user, printf-style, !!with only one %s!! ex.: "::%s::" * @param addBack what should be added at the end of the completion * @param addFront what should be added to the front of one finished completion * @return true if ok, false otherwise */ bool ShellCompletion::generalComplete(std::string& input, const std::string& begin, const std::string& displayAs, const std::string& addBack, const std::string& addFront) { if (completionList.size() == 0) return false; CompletionElement addElem = completionList.front(); const std::string& addString = addElem.name; unsigned int addLength = 0; unsigned int inputLenght = begin.size(); // Determin the longest Match addLength = addString.size(); CompletionType changeType = NullCompletion; std::vector::iterator charIT; for (charIT = completionList.begin(); charIT != completionList.end(); charIT++) { if ((*charIT).type != changeType) { if (changeType != NullCompletion) PRINT(0)("\n"); PRINT(0)("%s: ", ShellCompletion::typeToString((*charIT).type).c_str()); changeType = (*charIT).type; } PRINTF(0)("%s ", (*charIT).name.c_str()); for (unsigned int i = inputLenght; i < addLength; i++) if (addString[i] != (*charIT).name[i]) { addLength = i; // break; } } PRINT(0)("\n"); if (addLength >= inputLenght) { std::string adder = addString; adder.resize(addLength); input.resize(input.size()-inputLenght); input += adder; if (completionList.size() == 1) { if ( addBack != "") input += addBack; input += ' '; } } return true; } /** * @brief searches for classes, which beginn with completionBegin * @param inputList the List to parse through * @param completionBegin the beginning string * !! The strings MUST NOT be deleted !! */ bool ShellCompletion::addToCompleteList(const std::list& inputList, const std::string& completionBegin, CompletionType type) { unsigned int searchLength = completionBegin.size(); std::list::const_iterator string; for (string = inputList.begin(); string != inputList.end(); string++) { if ((*string).size() >= searchLength && !nocaseCmp(*string, completionBegin, searchLength)) { printf ("%s\n", (*string).c_str()); CompletionElement newElem; newElem.name = (*string); newElem.type = type; this->completionList.push_back(newElem); } } return true; } /** * @brief searches for classes, which beginn with completionBegin * @param inputList the List to parse through * @param completionBegin the beginning string * !! The strings MUST NOT be deleted !! */ bool ShellCompletion::addToCompleteList(const std::list& inputList, const std::string& completionBegin, CompletionType type) { unsigned int searchLength = completionBegin.size(); std::list::const_iterator bo; for(bo = inputList.begin(); bo != inputList.end(); bo++) { if ((*bo)->getName() != NULL && strlen((*bo)->getName()) >= searchLength && !nocaseCmp((*bo)->getName(), completionBegin, searchLength)) { CompletionElement newElem; newElem.name = (*bo)->getName(); newElem.type = type; this->completionList.push_back(newElem); } } return true; } /** * @brief deletes the Completion List. * * This is done at the beginning of each completion-run */ void ShellCompletion::clearCompletionList() { this->completionList.clear(); } const std::string& ShellCompletion::typeToString(CompletionType type) { switch (type) { default:// SHELLC_NONE return typeNames[0]; case ClassCompletion: return typeNames[1]; case ObjectCompletion: return typeNames[2]; case FunctionCompletion: return typeNames[3]; case AliasCompletion: return typeNames[4]; } } const std::string ShellCompletion::typeNames[] = { "error", "class", "object", "function", "alias" };