/* Copyright (C) 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 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef READLINE_H #define READLINE_H #include #include #include #include #include class ReadLine { public: /** * @brief Constructor. * @param prompt The prompt for this readline instance. * @param name The name of this readline instance. */ ReadLine(const std::string & prompt = "", const std::string & name = ""); /** * @brief Register a function with this readline interface. * @param cmd The command string. * @param function The function to execute upon a match. * @param help The help string for this function. */ void RegisterFunction(const std::string & cmd, rl_icpfunc_t * completer, const std::string & help); /** * @brief Get the prompt in use. * @return A string holding the current prompt. */ std::string GetPrompt() const; /** * @brief Set the prompt for future invocations. * @param prompt The new prompt to use. */ void SetPrompt(const std::string & prompt); /** * @brief Let the caller know if the stream has failed for any reason. * @return True if the stream is bad. */ bool Bad() const; /** * @brief Get a line from readline. * @return A string containing the line read. * @post If any problems occured the bad flag will be set and should be checked. */ std::string GetLine(); /** * @brief Print out the help strings for all of the commands. */ void PrintHelp() const; /** * @brief Get function for line. * @param line The line to check for a function call. * @return The function pointer or null if not found. */ rl_icpfunc_t * GetFunction(const std::string & line); private: std::string prompt; bool bad; /** * @brief The default function to complete commands. * @param text The text from readline. * @param start The start position. * @param end The end position. * @return An array of strings. */ static char ** completer(const char * text, int start, int end); /** * @brief Generates a list of commands. * @param text The text to check. * @param state The stateth item to return. * @return A word for completion. */ static char * command_generator(const char * text, int state); /** * @brief Set the completion function for readline. * @param completer The function to use for finding completion items. */ void setCompleter(char ** (completer)(const char *, int, int)); }; #endif // READLINE_H