![]() |
appkit
1.5.1
RoadNarrows Robotics Application Kit
|
ReadLine class provides a C++ wrapper around the readline C library. More...
#include <ReadLine.h>
Public Types | |
| enum | RlFlags { FlagTabNoMods = 0x0000, FlagTabNoDefault = 0x0001, FlagTabNoFilename = 0x0002, FlagTabNoSpace = 0x0004 } |
| ReadLine flag modifiers. More... | |
| typedef char *(* | AppGenFunc) (void *pAppArg, const std::string &strText, int nState, const std::string &strContext) |
| Application-specific TAB completion generator function type. More... | |
| typedef const std::string(* | AltAppGenFunc) (void *pAppArg, const std::string &strText, int nIndex, const std::string &strContext, int nStart, int nEnd, unsigned &uFlags) |
| Alternative application-specific TAB completion generator function type. More... | |
Public Member Functions | |
| ReadLine (const std::string strName="", const std::string strPrompt="> ", bool bUseRlLib=true) | |
| Default initialization constructor. More... | |
| virtual | ~ReadLine () |
| Destructor. | |
| void | registerGenerator (AppGenFunc fnAppGen, void *pAppArg) |
| Register application-specific tab-completion generator. More... | |
| void | registerAltGenerator (AltAppGenFunc fnAltAppGen, void *pAppArg) |
| Register alternate application-specific tab-completion generator. More... | |
| void | unregisterGenerator () |
| Unregister application-specific generator associated with path. | |
| std::string & | rlreadLine () |
| Interactively read a line of input from standard input (stdin). More... | |
| std::string & | ireadLine () |
| Interactively read a line of input from standard input (stdin). More... | |
| std::string & | ireadLine (FILE *fp) |
| Interactively read a line of input from an input stream. More... | |
| std::string & | freadLine (FILE *fp) |
| Read one input line from the given input stream. More... | |
| void | addToHistory (const std::string &strLine) |
| const std::string & | getName () const |
| Get this instance of readline name. | |
| bool | haveRlLib () const |
| Test if have readline library. More... | |
| bool | useRlLib () const |
| Test if the readline library is enabled. More... | |
| bool | isInteractive (FILE *fp) |
| Test if input file pointer is interactive. More... | |
| void | setPrompt (const std::string &strPrompt) |
| Set the prompt string. More... | |
| const std::string & | getPrompt () const |
| Get the current prompt string. More... | |
| const std::string & | getLastRead () const |
| Get last read line. More... | |
| size_t | getLineNum () const |
| Get the line number of the last read line. More... | |
| void | setLineNum (const size_t uLineNum) |
| Set the current line number. More... | |
| void | resetLineNum () |
| Reset the line number to zero. | |
| bool | isEoF () const |
| Test if the last read operation resulted in an end-of-file condition. More... | |
| bool | isFError () const |
| Test if the last read operation resulted in an I/O error condition. More... | |
| const std::string & | getErrorStr () const |
| Get the most recently set error string. More... | |
| size_t | tokenize (char *s, char *tokv[], size_t tokmax) |
| In-place string tokenizer. More... | |
Static Public Member Functions | |
| static char * | filenameCompletionGenerator (const char *sText, int nState) |
| Filename TAB completion generator. More... | |
| static char * | usernameCompletionGenerator (const char *sText, int nState) |
| Username TAB completion generator. More... | |
| static char * | dupstr (const std::string &str) |
| Duplicate string. More... | |
| static char * | dupstr (const char *s) |
| Duplicate string. More... | |
| static size_t | tokenize (const std::string &str, str::StringVec &tokens) |
| Tokenize the given string. More... | |
| static int | wc (const std::string &str) |
| Count the words in the string. More... | |
| static int | wc (const char *s) |
| Count the words in the string. More... | |
Static Public Attributes | |
| static const int | FIRST = 0 |
| first state | |
Protected Member Functions | |
| char ** | completion (const char *sText, int nStart, int nEnd) |
| Command completion callback function. More... | |
| char ** | altCompletion (const std::string strText, int nStart, int nEnd) |
| Command completion callback function. More... | |
| void | setStreamStatus (FILE *fp) |
| Set stream status for the last read operation. More... | |
| void | clearStreamStatus () |
| Clear stream status prior to next read operation. | |
Static Protected Member Functions | |
| static char ** | completionWrapper (const char *sText, int nStart, int nEnd) |
| Command completion callback function wrapper. More... | |
| static char * | generatorWrapper (const char *sText, int nState) |
| Generator wrapper. More... | |
Protected Attributes | |
| std::string | m_strName |
| readline name | |
| std::string | m_strPrompt |
| prompt string | |
| bool | m_bUseRlLib |
| [do not] use readline library | |
| std::string | m_strLine |
| last read line | |
| size_t | m_uLineNum |
| line number | |
| bool | m_bEoF |
| last file op end of file condition | |
| bool | m_bFError |
| last file op file error condition | |
| std::string | m_strError |
| error string | |
| AppGenFunc | m_fnAppGen |
| application-specific generator | |
| AltAppGenFunc | m_fnAltAppGen |
| alternate app-specific generator | |
| void * | m_pAppArg |
| application-specific argument | |
Static Protected Attributes | |
| static ReadLine * | ThisObj = NULL |
| static pointer to this single instance More... | |
ReadLine class provides a C++ wrapper around the readline C library.
Definition at line 112 of file ReadLine.h.
| typedef const std::string(* rnr::cmd::ReadLine::AltAppGenFunc) (void *pAppArg, const std::string &strText, int nIndex, const std::string &strContext, int nStart, int nEnd, unsigned &uFlags) |
Alternative application-specific TAB completion generator function type.
This is a somewhat more powerful TAB completion function that remains entirely in C++. The calling function is ReadLine::completion() which, in turn, is called by the readline library through the set rl_attempted_completion_function variable.
The generator is called multiple times on a TAB completion event. On the first call, nIndex is equal to 0. Subsequent calls, nIndex is incremented by one. The function may safely use this index to sequence through any application-specific data.
Return Values:
Tab Modifier Flags:
Flags only apply to the current TAB completion event and are reset to defaults there after.
ReadLine Support Methods: See AppGenFunc.
| pAppArg | Generator function argument. | |
| strText | Partial text string to complete. | |
| nIndex | Match candidate index starting from 0. | |
| strContext | Generator context (i.e. input buffer). | |
| nStart | Starting index in context of text. | |
| nEnd | Ending index in context of the position immediately after the end of text. If nStart equals nEnd, then empty text. | |
| [out] | uFlags | TAB completion modifier flags. |
Definition at line 211 of file ReadLine.h.
| typedef char*(* rnr::cmd::ReadLine::AppGenFunc) (void *pAppArg, const std::string &strText, int nState, const std::string &strContext) |
Application-specific TAB completion generator function type.
The generator is called multiple times on a TAB completion event. The call is made through the readline library rl_completion_matches() C function. On the first call, nState equals ReadLine::FIRST. This allows for any necessary initaliazation of (static) application specific state variables. On subsequent calls, nState > 0.
For each matched candidate, a malloc'd string must be returned. The readline library automatically frees the memory allocation after use.
When all candidates are exhausted, return NULL to signal no more matches to the readline library calling function.
ReadLine Support Methods:
| pAppArg | Generator function argument. |
| strText | Partial text string to complete. |
| nState | Generator state. If FIRST(0), initialize any state statics. |
| strContext | Generator context (i.e. stripped input buffer). |
Definition at line 158 of file ReadLine.h.
ReadLine flag modifiers.
| Enumerator | |
|---|---|
| FlagTabNoMods |
no TAB modifiers |
| FlagTabNoDefault |
no default TAB completion match |
| FlagTabNoFilename |
no filename TAB completion attempt |
| FlagTabNoSpace |
no space(' ') after TAB completion |
Definition at line 120 of file ReadLine.h.
| ReadLine::ReadLine | ( | const std::string | strName = "", |
| const std::string | strPrompt = "> ", |
||
| bool | bUseRlLib = true |
||
| ) |
Default initialization constructor.
| strName | Name of readline. Used to allow for conditional parsing of the ~/.inputrc file. |
| strPrompt | Command line primary prompt string. Prompting occurs only in interactive mode. |
| bUseRlLib | Use the readline library. Note that readline must be available and the input is interactive. Otherwise a simple command-line interface will be used. |
Definition at line 111 of file ReadLine.cxx.
References completionWrapper(), m_bEoF, m_bFError, m_fnAltAppGen, m_fnAppGen, m_pAppArg, m_strName, m_uLineNum, and ThisObj.
|
protected |
Command completion callback function.
If an alternative application-specific generator is registered, this method is used to provide the requisite C++ interface.
Attempt to complete on the contents of text. The start and end bound the region of rl_line_buffer that contains the word to complete. Text is the word to complete. We can use the entire contents of rl_line_buffer to provide context.
This method also provides the indexing for the registered generator.
| sText | Text string to complete as a command. |
| nStart | Start index of text string in line. |
| nEnd | End index of text string in line. |
Definition at line 493 of file ReadLine.cxx.
References dupstr(), FlagTabNoDefault, FlagTabNoFilename, FlagTabNoSpace, m_fnAltAppGen, and m_pAppArg.
Referenced by completion().
|
protected |
Command completion callback function.
Attempt to complete on the contents of text. The start and end bound the region of rl_line_buffer that contains the word to complete. Text is the word to complete. We can use the entire contents of rl_line_buffer to provide context.
| sText | Text string to complete as a command. |
| nStart | Start index of text string in line. |
| nEnd | End index of text string in line. |
Definition at line 476 of file ReadLine.cxx.
References altCompletion(), generatorWrapper(), and m_fnAltAppGen.
Referenced by completionWrapper().
|
staticprotected |
Command completion callback function wrapper.
Wraps method ReadLine::completion().
| sText | Text string to complete as a command. |
| nStart | Start index of text string in line. |
| nEnd | End index of text string in line. |
Definition at line 471 of file ReadLine.cxx.
References completion(), and ThisObj.
Referenced by ReadLine().
|
inlinestatic |
Duplicate string.
The readline library requires allocated char*'s.
| str | String to dup. |
Definition at line 556 of file ReadLine.h.
References tokenize().
Referenced by altCompletion().
|
static |
Duplicate string.
The readline library requires allocated char*'s.
| s | Null-terminated string to dup. |
Definition at line 356 of file ReadLine.cxx.
References tokenize().
|
inlinestatic |
Filename TAB completion generator.
A completion generator for filenames.
| sText | Partial text string to complete. |
| nState | Generator state. If FIRST,then initialize any statics. |
Definition at line 512 of file ReadLine.h.
| string & ReadLine::freadLine | ( | FILE * | fp | ) |
Read one input line from the given input stream.
The input stream may be attached to a terminal, pipe, or file. No prompting will occur.
| fp | Input file stream pointer. |
Definition at line 239 of file ReadLine.cxx.
References clearStreamStatus(), m_strLine, m_uLineNum, setStreamStatus(), and rnr::str::trim().
Referenced by ireadLine(), and rlreadLine().
|
staticprotected |
Generator wrapper.
Calls the registered application-specific generator.
| sText | Partial text string to complete. |
| nState | Generator state. If FIRST,then initialize any statics. |
Definition at line 574 of file ReadLine.cxx.
References FIRST, m_fnAppGen, m_pAppArg, ThisObj, and rnr::str::trim().
Referenced by completion().
| const string & ReadLine::getErrorStr | ( | ) | const |
Get the most recently set error string.
Definition at line 322 of file ReadLine.cxx.
References m_strError.
Referenced by rnr::cmd::CommandLine::checkReadResult(), and isFError().
|
inline |
|
inline |
Get the line number of the last read line.
Definition at line 443 of file ReadLine.h.
References m_uLineNum.
|
inline |
Get the current prompt string.
Definition at line 423 of file ReadLine.h.
References m_strPrompt.
Referenced by ireadLine(), and rlreadLine().
|
inline |
Test if have readline library.
Definition at line 379 of file ReadLine.h.
Referenced by rnr::cmd::CommandLine::c14n().
|
inline |
Interactively read a line of input from standard input (stdin).
The readline library is not used.
If in interactive mode, the user will be prompted with the currently set prompt string. Any prompt is written to standard output (stdout).
Definition at line 313 of file ReadLine.h.
References freadLine().
Referenced by rnr::cmd::CommandLine::readCommand(), and rlreadLine().
| string & ReadLine::ireadLine | ( | FILE * | fp | ) |
Interactively read a line of input from an input stream.
The readline library is not used.
If in interactive mode, the user will be prompted with the currently set prompt string. Any prompt is written to standard output (stdout).
| fp | Input file stream pointer. |
Definition at line 227 of file ReadLine.cxx.
References freadLine(), getPrompt(), and isInteractive().
|
inline |
Test if the last read operation resulted in an end-of-file condition.
Definition at line 472 of file ReadLine.h.
References m_bEoF.
Referenced by rnr::cmd::CommandLine::checkReadResult().
|
inline |
Test if the last read operation resulted in an I/O error condition.
Definition at line 483 of file ReadLine.h.
References getErrorStr(), and m_bFError.
Referenced by rnr::cmd::CommandLine::checkReadResult().
| bool ReadLine::isInteractive | ( | FILE * | fp | ) |
Test if input file pointer is interactive.
A file input stream is interactive if it is bound to a terminal or tty.
Definition at line 317 of file ReadLine.cxx.
Referenced by ireadLine(), rlreadLine(), and useRlLib().
| void ReadLine::registerAltGenerator | ( | AltAppGenFunc | fnAltAppGen, |
| void * | pAppArg | ||
| ) |
Register alternate application-specific tab-completion generator.
The registered function will be called through the ReadLine::completion C++ method.
| fnAltAppGen | Alt application-specific generator function. |
| pAppArg | Optional application argument generator function. |
Definition at line 155 of file ReadLine.cxx.
References m_fnAltAppGen, m_fnAppGen, and m_pAppArg.
Referenced by rnr::cmd::CommandLine::compile().
| void ReadLine::registerGenerator | ( | AppGenFunc | fnAppGen, |
| void * | pAppArg | ||
| ) |
Register application-specific tab-completion generator.
The registered function will be called through the readline library rl_completion_matches() C function.
| fnAppGen | Application-specific generator function. |
| pAppArg | Optional application argument generator function. |
Definition at line 148 of file ReadLine.cxx.
References m_fnAltAppGen, m_fnAppGen, and m_pAppArg.
| string & ReadLine::rlreadLine | ( | ) |
Interactively read a line of input from standard input (stdin).
The readline library function readline() will be called iff:
Otherwise, ireadLine() is used.
The readline() library call enables TAB completion, line editing, and history recording/recall.
If in interactive mode, the user will be prompted with the current prompt string. Any prompt is written to standard output (stdout).
Definition at line 174 of file ReadLine.cxx.
References clearStreamStatus(), freadLine(), getPrompt(), ireadLine(), isInteractive(), m_bUseRlLib, m_strLine, m_uLineNum, and setStreamStatus().
Referenced by rnr::cmd::CommandLine::readCommand().
|
inline |
Set the current line number.
| Line | number. |
Definition at line 453 of file ReadLine.h.
References m_uLineNum.
|
inline |
Set the prompt string.
| strPrompt | New prompt string. |
Definition at line 413 of file ReadLine.h.
References m_strPrompt.
Referenced by rnr::cmd::CommandLine::popPrompt(), and rnr::cmd::CommandLine::pushPrompt().
|
protected |
Set stream status for the last read operation.
| fp | Input file pointer. |
Definition at line 327 of file ReadLine.cxx.
References m_bEoF, m_bFError, and m_strError.
Referenced by freadLine(), and rlreadLine().
|
static |
Tokenize the given string.
Tokens are contiguous non-whitespace character sequences, separated by either the start-of-string, whitespace, or the end-of-string.
| [in] | str | Input string to generate tokens. |
| [out] | tokens | Vector of tokens. |
Referenced by dupstr().
| size_t ReadLine::tokenize | ( | char * | s, |
| char * | tokv[], | ||
| size_t | tokmax | ||
| ) |
In-place string tokenizer.
Tokens are contiguous non-whitespace character sequences, separated by either the start-of-string, whitespace, or the end-of-string.
The '\0' character is inserted at the end of each detected word in s. No memory allocation is used.
| [in,out] | s | Input/output null-terminated string to tokenize. |
| [out] | tokv | Array of tokens (pointer to locations in s). |
| [in] | tokmax | Maximum number of tokens. |
Definition at line 401 of file ReadLine.cxx.
|
inline |
Test if the readline library is enabled.
Definition at line 393 of file ReadLine.h.
References isInteractive(), and m_bUseRlLib.
Referenced by rnr::cmd::CommandLine::c14n().
|
inlinestatic |
Username TAB completion generator.
A completion generator for usernames.
| sText | Partial text string to complete. |
| nState | Generator state. If FIRST,then initialize any statics. |
Definition at line 533 of file ReadLine.h.
|
inlinestatic |
Count the words in the string.
| str | String to count. |
Definition at line 609 of file ReadLine.h.
Referenced by wc().
|
static |
Count the words in the string.
| s | Null-terminated string to count. |
Definition at line 441 of file ReadLine.cxx.
References wc().
|
staticprotected |
static pointer to this single instance
Pointer to the only instance of ReadLine supported per application.
Definition at line 624 of file ReadLine.h.
Referenced by completionWrapper(), generatorWrapper(), ReadLine(), and ~ReadLine().