appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::cmd::ReadLine Class Reference

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 ReadLineThisObj = NULL
 static pointer to this single instance More...
 

Detailed Description

ReadLine class provides a C++ wrapper around the readline C library.

Features:
  • line input processing
  • tab completion
  • history
If libreadline is not available, then only simple line input processing is available.

Definition at line 112 of file ReadLine.h.

Member Typedef Documentation

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:

  1. match: Any non-empty, non-whitespace string.
  2. empty: An empty string "" terminates TAB completion generation. RlTabEnd is equivalent to the empty string.

Tab Modifier Flags:

  • FlagTabNoDefault: If this flag is set at index 0 and a match is returned, then a "no default" readline library value will be set. Otherwise the this first match is the default.
  • FlagTabNoFilename: If this flag is set at the end of TAB completion (an empty string is returned), then readline library default filename completion action is disabled. (See rl_attempted_completion_over.) The readline library default action is only invoked if no strings are generated.

Flags only apply to the current TAB completion event and are reset to defaults there after.

ReadLine Support Methods: See AppGenFunc.

Parameters
pAppArgGenerator function argument.
strTextPartial text string to complete.
nIndexMatch candidate index starting from 0.
strContextGenerator context (i.e. input buffer).
nStartStarting index in context of text.
nEndEnding index in context of the position immediately after the end of text. If nStart equals nEnd, then empty text.
[out]uFlagsTAB completion modifier flags.
Returns
Match or empty string.

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:

Parameters
pAppArgGenerator function argument.
strTextPartial text string to complete.
nStateGenerator state. If FIRST(0), initialize any state statics.
strContextGenerator context (i.e. stripped input buffer).
Returns
If a first/next match is made, return allocated completed match.
Otherwise return NULL.

Definition at line 158 of file ReadLine.h.

Member Enumeration Documentation

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.

121  {
122  FlagTabNoMods = 0x0000, ///< no TAB modifiers
123  FlagTabNoDefault = 0x0001, ///< no default TAB completion match
124  FlagTabNoFilename = 0x0002, ///< no filename TAB completion attempt
125  FlagTabNoSpace = 0x0004 ///< no space(' ') after TAB completion
126  };
no filename TAB completion attempt
Definition: ReadLine.h:124
no space(&#39; &#39;) after TAB completion
Definition: ReadLine.h:125
no default TAB completion match
Definition: ReadLine.h:123

Constructor & Destructor Documentation

ReadLine::ReadLine ( const std::string  strName = "",
const std::string  strPrompt = "> ",
bool  bUseRlLib = true 
)

Default initialization constructor.

Parameters
strNameName of readline. Used to allow for conditional parsing of the ~/.inputrc file.
strPromptCommand line primary prompt string. Prompting occurs only in interactive mode.
bUseRlLibUse 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.

113  :
114  m_strName(strName), m_strPrompt(strPrompt), m_bUseRlLib(bUseRlLib)
115 {
116  m_uLineNum = 0;
117  m_bEoF = false;
118  m_bFError = false;
119  m_fnAppGen = NULL;
120  m_fnAltAppGen = NULL;
121  m_pAppArg = NULL;
122 
123 #ifdef HAVE_READLINE
124  //
125  // Only one instance of this class can be instantiated since:
126  // 1. The readline library provides no user argument(s) for callbacks to use.
127  // 2. And more importantly, readline library uses global variables.
128  //
129  assert(ThisObj == NULL);
130 
131  // limitation of readline library which does not provide context feedback
132  ThisObj = this;
133 
134  // tell readline completer that we want a crack at it first before default
135  rl_attempted_completion_function = ReadLine::completionWrapper;
136 
137  // allow conditional parsing of the ~/.inputrc file
138  rl_readline_name = m_strName.c_str();
139 
140 #endif // HAVE_READLINE
141 }
bool m_bFError
last file op file error condition
Definition: ReadLine.h:632
std::string m_strName
readline name
Definition: ReadLine.h:626
static char ** completionWrapper(const char *sText, int nStart, int nEnd)
Command completion callback function wrapper.
Definition: ReadLine.cxx:471
AppGenFunc m_fnAppGen
application-specific generator
Definition: ReadLine.h:634
std::string m_strPrompt
prompt string
Definition: ReadLine.h:627
bool m_bUseRlLib
[do not] use readline library
Definition: ReadLine.h:628
bool m_bEoF
last file op end of file condition
Definition: ReadLine.h:631
void * m_pAppArg
application-specific argument
Definition: ReadLine.h:636
size_t m_uLineNum
line number
Definition: ReadLine.h:630
AltAppGenFunc m_fnAltAppGen
alternate app-specific generator
Definition: ReadLine.h:635
static ReadLine * ThisObj
static pointer to this single instance
Definition: ReadLine.h:624

Member Function Documentation

char ** ReadLine::altCompletion ( const std::string  strText,
int  nStart,
int  nEnd 
)
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.

Parameters
sTextText string to complete as a command.
nStartStart index of text string in line.
nEndEnd index of text string in line.
Returns
Array of matches, NULL if none.

Definition at line 493 of file ReadLine.cxx.

References dupstr(), FlagTabNoDefault, FlagTabNoFilename, FlagTabNoSpace, m_fnAltAppGen, and m_pAppArg.

Referenced by completion().

494 {
495 #ifdef HAVE_READLINE
496  string strContext(rl_line_buffer);
497  int nIndex;
498  unsigned uFlags;
499  string strMatch;
500  vector<string> matchList;
501  size_t i;
502  char **tabList;
503 
504  rl_attempted_completion_over = 0; // disable default filename TAB completion
505  rl_completion_suppress_append = 0; // enable default space (' ') after match
506 
507  // no flags
508  uFlags = 0;
509 
510  for(nIndex = 0; ; ++nIndex)
511  {
512  strMatch = m_fnAltAppGen(m_pAppArg, strText, nIndex,
513  strContext, nStart, nEnd, uFlags);
514 
515  // end of completion generation
516  if( strMatch.empty() )
517  {
518  if( uFlags & FlagTabNoFilename )
519  {
520  rl_attempted_completion_over = 1;
521  }
522 
523  if( uFlags & FlagTabNoSpace )
524  {
525  rl_completion_suppress_append = 1;
526  }
527  break;
528  }
529 
530  // regular completion
531  else
532  {
533  // Empty string disables default. Undocumented.
534  if( (nIndex == 0) && (uFlags & FlagTabNoDefault) )
535  {
536  matchList.push_back("");
537  }
538  matchList.push_back(strMatch);
539  }
540  }
541 
542  // no matches
543  if( matchList.size() == 0 )
544  {
545  return NULL;
546  }
547 
548  //
549  // Malloc a tap-completion array.
550  //
551  // Note: The readlline library frees all entries in this array along with
552  // the array itself. Undocumented.
553  //
554  i = sizeof(char*) * (matchList.size() + 1);
555 
556  tabList = (char**)malloc(i);
557 
558  // copy allocated char *'s
559  for(i = 0; i < matchList.size(); ++i)
560  {
561  tabList[i] = dupstr(matchList[i]);
562  }
563  tabList[i] = NULL;
564 
565  return tabList;
566 
567 #else // !HAVE_READLINE
568 
569  return NULL;
570 
571 #endif // HAVE_READLINE
572 }
no filename TAB completion attempt
Definition: ReadLine.h:124
no space(&#39; &#39;) after TAB completion
Definition: ReadLine.h:125
void * m_pAppArg
application-specific argument
Definition: ReadLine.h:636
no default TAB completion match
Definition: ReadLine.h:123
AltAppGenFunc m_fnAltAppGen
alternate app-specific generator
Definition: ReadLine.h:635
static char * dupstr(const std::string &str)
Duplicate string.
Definition: ReadLine.h:556
char ** ReadLine::completion ( const char *  sText,
int  nStart,
int  nEnd 
)
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.

Parameters
sTextText string to complete as a command.
nStartStart index of text string in line.
nEndEnd index of text string in line.
Returns
Array of matches, NULL if none.

Definition at line 476 of file ReadLine.cxx.

References altCompletion(), generatorWrapper(), and m_fnAltAppGen.

Referenced by completionWrapper().

477 {
478 #ifdef HAVE_READLINE
479  if( m_fnAltAppGen != NULL )
480  {
481  return altCompletion(sText, nStart, nEnd);
482  }
483  else
484  {
485  return rl_completion_matches(sText, generatorWrapper);
486  }
487 
488 #else
489  return NULL;
490 #endif // HAVE_READLINE
491 }
char ** altCompletion(const std::string strText, int nStart, int nEnd)
Command completion callback function.
Definition: ReadLine.cxx:493
static char * generatorWrapper(const char *sText, int nState)
Generator wrapper.
Definition: ReadLine.cxx:574
AltAppGenFunc m_fnAltAppGen
alternate app-specific generator
Definition: ReadLine.h:635
char ** ReadLine::completionWrapper ( const char *  sText,
int  nStart,
int  nEnd 
)
staticprotected

Command completion callback function wrapper.

Wraps method ReadLine::completion().

Parameters
sTextText string to complete as a command.
nStartStart index of text string in line.
nEndEnd index of text string in line.
Returns
Array of matches, NULL if none.

Definition at line 471 of file ReadLine.cxx.

References completion(), and ThisObj.

Referenced by ReadLine().

472 {
473  return ThisObj->completion(sText, nStart, nEnd);
474 }
char ** completion(const char *sText, int nStart, int nEnd)
Command completion callback function.
Definition: ReadLine.cxx:476
static ReadLine * ThisObj
static pointer to this single instance
Definition: ReadLine.h:624
static char* rnr::cmd::ReadLine::dupstr ( const std::string &  str)
inlinestatic

Duplicate string.

The readline library requires allocated char*'s.

Parameters
strString to dup.
Returns
Duplicated, allocated char*.

Definition at line 556 of file ReadLine.h.

References tokenize().

Referenced by altCompletion().

557  {
558  return ReadLine::dupstr(str.c_str());
559  }
static char * dupstr(const std::string &str)
Duplicate string.
Definition: ReadLine.h:556
char * ReadLine::dupstr ( const char *  s)
static

Duplicate string.

The readline library requires allocated char*'s.

Parameters
sNull-terminated string to dup.
Returns
Duplicated, allocated char *.

Definition at line 356 of file ReadLine.cxx.

References tokenize().

357 {
358  size_t size = sizeof(char) * (strlen(s) + 1);
359  char *t = (char *)malloc(size);
360  strcpy(t, s);
361  return t;
362 }
static char* rnr::cmd::ReadLine::filenameCompletionGenerator ( const char *  sText,
int  nState 
)
inlinestatic

Filename TAB completion generator.

A completion generator for filenames.

Parameters
sTextPartial text string to complete.
nStateGenerator state. If FIRST,then initialize any statics.
Returns
If a first/next match is made, return allocated completed match.
Otherwise return NULL.

Definition at line 512 of file ReadLine.h.

513  {
514  #ifdef HAVE_READLINE
515  return rl_filename_completion_function(sText, nState);
516  #else // !HAVE_READLINE
517  return NULL;
518  #endif // HAVE_READLINE
519  }
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.

Parameters
fpInput file stream pointer.
Returns
Returns the string of the line read, stripped of leading and trailing whitespace. If the string is empty, then one of:
  • The line was all whitespace.
  • EOF was reached. Use isEoF() to test this condition.
  • A file read error occurred. Use isFError() to test this conition.

Definition at line 239 of file ReadLine.cxx.

References clearStreamStatus(), m_strLine, m_uLineNum, setStreamStatus(), and rnr::str::trim().

Referenced by ireadLine(), and rlreadLine().

240 {
241  static size_t BufSize = 4096;
242 
243  char bufLine[BufSize];
244  size_t n;
245 
247  m_strLine.clear();
248 
249  if( fgets(bufLine, BufSize, fp) != NULL )
250  {
251  bufLine[BufSize-1] = 0;
252  n = strlen(bufLine);
253 
254  // some characters were read
255  if( n > 0 )
256  {
257  // got end of line
258  if( bufLine[n-1] == '\n' )
259  {
260  bufLine[n-1] = 0;
261  ++m_uLineNum;
262  }
263 
264  // oops, input line trunctated - too long
265  else
266  {
267  bufLine[n-1] = 0;
268  ++m_uLineNum;
269  LOGWARN("Input line too long. Exceeds %zu characters.", BufSize);
270  }
271  }
272 
273  // no i/o errors but also no characters read - odd
274  else
275  {
276  setStreamStatus(fp);
277 
278  bufLine[0] = 0;
279  }
280  }
281 
282  // read failure
283  else
284  {
285  setStreamStatus(fp);
286 
287  bufLine[0] = 0;
288  }
289 
290  m_strLine = bufLine;
291 
292  trim(m_strLine);
293 
294  return m_strLine;
295 }
void clearStreamStatus()
Clear stream status prior to next read operation.
Definition: ReadLine.cxx:349
std::string & trim(std::string &str)
Trim string in-place of leading and trailing whitespace.
Definition: StringTheory.h:197
void setStreamStatus(FILE *fp)
Set stream status for the last read operation.
Definition: ReadLine.cxx:327
size_t m_uLineNum
line number
Definition: ReadLine.h:630
std::string m_strLine
last read line
Definition: ReadLine.h:629
char * ReadLine::generatorWrapper ( const char *  sText,
int  nState 
)
staticprotected

Generator wrapper.

Calls the registered application-specific generator.

Parameters
sTextPartial text string to complete.
nStateGenerator state. If FIRST,then initialize any statics.
Returns
If a first/next match is made, return allocated completed match.
Otherwise return NULL.

Definition at line 574 of file ReadLine.cxx.

References FIRST, m_fnAppGen, m_pAppArg, ThisObj, and rnr::str::trim().

Referenced by completion().

575 {
576 #ifdef HAVE_READLINE
577  // no registered generator match current path
578  if( ThisObj->m_fnAppGen == NULL )
579  {
580  return NULL;
581  }
582 
583  string strText(sText);
584  string strContext(rl_line_buffer);
585 
586  // trim left and right whitespace
587  trim(strContext);
588 
589  // first time through
590  if( nState == ReadLine::FIRST )
591  {
592  }
593 
594  // call application-specific generator
595  return ThisObj->m_fnAppGen(ThisObj->m_pAppArg, strText, nState, strContext);
596 
597 #else // !HAVE_READLINE
598 
599  return NULL;
600 
601 #endif // HAVE_READLINE
602 }
AppGenFunc m_fnAppGen
application-specific generator
Definition: ReadLine.h:634
std::string & trim(std::string &str)
Trim string in-place of leading and trailing whitespace.
Definition: StringTheory.h:197
static const int FIRST
first state
Definition: ReadLine.h:115
void * m_pAppArg
application-specific argument
Definition: ReadLine.h:636
static ReadLine * ThisObj
static pointer to this single instance
Definition: ReadLine.h:624
const string & ReadLine::getErrorStr ( ) const

Get the most recently set error string.

Returns
Error string.

Definition at line 322 of file ReadLine.cxx.

References m_strError.

Referenced by rnr::cmd::CommandLine::checkReadResult(), and isFError().

323 {
324  return m_strError;
325 }
std::string m_strError
error string
Definition: ReadLine.h:633
const std::string& rnr::cmd::ReadLine::getLastRead ( ) const
inline

Get last read line.

Returns
String.

Definition at line 433 of file ReadLine.h.

References m_strLine.

434  {
435  return m_strLine;
436  }
std::string m_strLine
last read line
Definition: ReadLine.h:629
size_t rnr::cmd::ReadLine::getLineNum ( ) const
inline

Get the line number of the last read line.

Returns
Line number.

Definition at line 443 of file ReadLine.h.

References m_uLineNum.

444  {
445  return m_uLineNum;
446  }
size_t m_uLineNum
line number
Definition: ReadLine.h:630
const std::string& rnr::cmd::ReadLine::getPrompt ( ) const
inline

Get the current prompt string.

Returns
String.

Definition at line 423 of file ReadLine.h.

References m_strPrompt.

Referenced by ireadLine(), and rlreadLine().

424  {
425  return m_strPrompt;
426  }
std::string m_strPrompt
prompt string
Definition: ReadLine.h:627
bool rnr::cmd::ReadLine::haveRlLib ( ) const
inline

Test if have readline library.

Returns
Returns true or false.

Definition at line 379 of file ReadLine.h.

Referenced by rnr::cmd::CommandLine::c14n().

380  {
381 #ifdef HAVE_READLINE
382  return true;
383 #else // !HAVE_READLINE
384  return false;
385 #endif // HAVE_READLINE
386  }
std::string& rnr::cmd::ReadLine::ireadLine ( )
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).

Note
The rlreadLine() call is the preferred method since it automatically determines the best underlining function/method to call.
Returns
Returns the string of the line read, stripped of leading and trailing whitespace. If the string is empty, then one of:
  • The line was all whitespace.
  • EOF was reached. Use isEoF() to test this condition.
  • A file read error occurred. Use isFError() to test this conition.

Definition at line 313 of file ReadLine.h.

References freadLine().

Referenced by rnr::cmd::CommandLine::readCommand(), and rlreadLine().

314  {
315  return ireadLine(stdin);
316  }
std::string & ireadLine()
Interactively read a line of input from standard input (stdin).
Definition: ReadLine.h:313
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).

Parameters
fpInput file stream pointer.
Returns
Returns the string of the line read, stripped of leading and trailing whitespace. If the string is empty, then one of:
  • The line was all whitespace.
  • EOF was reached. Use isEoF() to test this condition.
  • A file read error occurred. Use isFError() to test this conition.

Definition at line 227 of file ReadLine.cxx.

References freadLine(), getPrompt(), and isInteractive().

228 {
229  // prompt
230  if( isInteractive(fp) && !getPrompt().empty() )
231  {
232  fprintf(stdout, "%s", getPrompt().c_str());
233  fflush(stdout);
234  }
235 
236  return freadLine(fp);
237 }
const std::string & getPrompt() const
Get the current prompt string.
Definition: ReadLine.h:423
std::string & freadLine(FILE *fp)
Read one input line from the given input stream.
Definition: ReadLine.cxx:239
bool isInteractive(FILE *fp)
Test if input file pointer is interactive.
Definition: ReadLine.cxx:317
bool rnr::cmd::ReadLine::isEoF ( ) const
inline

Test if the last read operation resulted in an end-of-file condition.

Returns
Returns true or false.

Definition at line 472 of file ReadLine.h.

References m_bEoF.

Referenced by rnr::cmd::CommandLine::checkReadResult().

473  {
474  return m_bEoF;
475  }
bool m_bEoF
last file op end of file condition
Definition: ReadLine.h:631
bool rnr::cmd::ReadLine::isFError ( ) const
inline

Test if the last read operation resulted in an I/O error condition.

Returns
Returns true or false.

Definition at line 483 of file ReadLine.h.

References getErrorStr(), and m_bFError.

Referenced by rnr::cmd::CommandLine::checkReadResult().

484  {
485  return m_bFError;
486  }
bool m_bFError
last file op file error condition
Definition: ReadLine.h:632
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.

Returns
Returns true or false.

Definition at line 317 of file ReadLine.cxx.

Referenced by ireadLine(), rlreadLine(), and useRlLib().

318 {
319  return isatty(fileno(fp))? true: false;
320 }
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.

Parameters
fnAltAppGenAlt application-specific generator function.
pAppArgOptional 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().

156 {
157  m_fnAppGen = NULL;
158  m_fnAltAppGen = fnAltAppGen;
159  m_pAppArg = pAppArg;
160 }
AppGenFunc m_fnAppGen
application-specific generator
Definition: ReadLine.h:634
void * m_pAppArg
application-specific argument
Definition: ReadLine.h:636
AltAppGenFunc m_fnAltAppGen
alternate app-specific generator
Definition: ReadLine.h:635
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.

Parameters
fnAppGenApplication-specific generator function.
pAppArgOptional application argument generator function.

Definition at line 148 of file ReadLine.cxx.

References m_fnAltAppGen, m_fnAppGen, and m_pAppArg.

149 {
150  m_fnAppGen = fnAppGen;
151  m_fnAltAppGen = NULL;
152  m_pAppArg = pAppArg;
153 }
AppGenFunc m_fnAppGen
application-specific generator
Definition: ReadLine.h:634
void * m_pAppArg
application-specific argument
Definition: ReadLine.h:636
AltAppGenFunc m_fnAltAppGen
alternate app-specific generator
Definition: ReadLine.h:635
string & ReadLine::rlreadLine ( )

Interactively read a line of input from standard input (stdin).

The readline library function readline() will be called iff:

  • The readline library is available (HAVE_READLINE).
  • The application enabled the ReadLine instance to use the readline.
  • library (bUseRlLib).
  • The input stdin is interactive (i.e. bound to a terminal or tty).

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).

Returns
Returns the string of the line read, stripped of leading and trailing whitespace. If the string is empty, then one of:
  • The line was all whitespace.
  • EOF was reached. Use isEoF() to test this condition.
  • A file read error occurred. Use isFError() to test this conition.

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().

175 {
176 #ifdef HAVE_READLINE
177  bool bInteractive = isInteractive(stdin);
178 
179  //
180  // Use readline library.
181  //
182  if( m_bUseRlLib && bInteractive )
183  {
185  m_strLine.clear();
186 
187  char *sLine = readline(getPrompt().c_str()); // fixed at stdin
188 
189  if( sLine != NULL )
190  {
191  m_strLine = sLine;
192  free(sLine);
193  ++m_uLineNum;
194  }
195 
196  else
197  {
198  setStreamStatus(stdin);
199  }
200 
201  return m_strLine;
202  }
203 
204  //
205  // Interactive, but don't use the readline library.
206  //
207  else if( bInteractive )
208  {
209  return ireadLine(stdin);
210  }
211 
212  //
213  // Non-interactive.
214  //
215  else
216  {
217  return freadLine(stdin);
218  }
219 
220 #else // !HAVE_READLINE
221 
222  return ireadLine(stdin);
223 
224 #endif // HAVE_READLINE
225 }
void clearStreamStatus()
Clear stream status prior to next read operation.
Definition: ReadLine.cxx:349
bool m_bUseRlLib
[do not] use readline library
Definition: ReadLine.h:628
const std::string & getPrompt() const
Get the current prompt string.
Definition: ReadLine.h:423
void setStreamStatus(FILE *fp)
Set stream status for the last read operation.
Definition: ReadLine.cxx:327
std::string & freadLine(FILE *fp)
Read one input line from the given input stream.
Definition: ReadLine.cxx:239
bool isInteractive(FILE *fp)
Test if input file pointer is interactive.
Definition: ReadLine.cxx:317
size_t m_uLineNum
line number
Definition: ReadLine.h:630
std::string & ireadLine()
Interactively read a line of input from standard input (stdin).
Definition: ReadLine.h:313
std::string m_strLine
last read line
Definition: ReadLine.h:629
void rnr::cmd::ReadLine::setLineNum ( const size_t  uLineNum)
inline

Set the current line number.

Parameters
Linenumber.

Definition at line 453 of file ReadLine.h.

References m_uLineNum.

454  {
455  m_uLineNum = uLineNum;
456  }
size_t m_uLineNum
line number
Definition: ReadLine.h:630
void rnr::cmd::ReadLine::setPrompt ( const std::string &  strPrompt)
inline

Set the prompt string.

Parameters
strPromptNew prompt string.

Definition at line 413 of file ReadLine.h.

References m_strPrompt.

Referenced by rnr::cmd::CommandLine::popPrompt(), and rnr::cmd::CommandLine::pushPrompt().

414  {
415  m_strPrompt = strPrompt;
416  }
std::string m_strPrompt
prompt string
Definition: ReadLine.h:627
void ReadLine::setStreamStatus ( FILE *  fp)
protected

Set stream status for the last read operation.

Parameters
fpInput file pointer.

Definition at line 327 of file ReadLine.cxx.

References m_bEoF, m_bFError, and m_strError.

Referenced by freadLine(), and rlreadLine().

328 {
329  m_bEoF = feof(fp)? true: false;
330  m_bFError = ferror(fp)? true: false;
331 
332  if( m_bFError )
333  {
334  stringstream ss;
335 
336  if( errno > 0 )
337  {
338  ss << strerror(errno) << "(errno=" << errno << "}";
339  }
340  else
341  {
342  ss << "File read error.";
343  }
344 
345  m_strError = ss.str();
346  }
347 }
bool m_bFError
last file op file error condition
Definition: ReadLine.h:632
std::string m_strError
error string
Definition: ReadLine.h:633
bool m_bEoF
last file op end of file condition
Definition: ReadLine.h:631
static size_t rnr::cmd::ReadLine::tokenize ( const std::string &  str,
str::StringVec tokens 
)
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.

Parameters
[in]strInput string to generate tokens.
[out]tokensVector of tokens.
Returns
Number 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.

Parameters
[in,out]sInput/output null-terminated string to tokenize.
[out]tokvArray of tokens (pointer to locations in s).
[in]tokmaxMaximum number of tokens.
Returns
Number of tokens.

Definition at line 401 of file ReadLine.cxx.

402 {
403  int tokc = 0;
404 
405  while( tokc < (int)tokmax )
406  {
407  // find start of the next token
408  while( *s && isspace((int)*s) )
409  {
410  s++;
411  }
412 
413  // no more tokens
414  if( *s == 0 )
415  {
416  return tokc;
417  }
418 
419  // new token
420  tokv[tokc++] = s;
421 
422  // find end of the token
423  while( *s && !isspace((int)*s) )
424  {
425  s++;
426  }
427 
428  // end of the line
429  if( *s == 0 )
430  {
431  return tokc;
432  }
433 
434  // null terminate
435  *s++ = 0;
436  }
437 
438  return (size_t)tokc;
439 }
bool rnr::cmd::ReadLine::useRlLib ( ) const
inline

Test if the readline library is enabled.

Returns
Returns true or false.

Definition at line 393 of file ReadLine.h.

References isInteractive(), and m_bUseRlLib.

Referenced by rnr::cmd::CommandLine::c14n().

394  {
395  return m_bUseRlLib;
396  }
bool m_bUseRlLib
[do not] use readline library
Definition: ReadLine.h:628
static char* rnr::cmd::ReadLine::usernameCompletionGenerator ( const char *  sText,
int  nState 
)
inlinestatic

Username TAB completion generator.

A completion generator for usernames.

Parameters
sTextPartial text string to complete.
nStateGenerator state. If FIRST,then initialize any statics.
Returns
If a first/next match is made, return allocated completed match.
Otherwise return NULL.

Definition at line 533 of file ReadLine.h.

534  {
535  #ifdef HAVE_READLINE
536  return rl_username_completion_function(sText, nState);
537  #else // !HAVE_READLINE
538  return NULL;
539  #endif // HAVE_READLINE
540  }
static int rnr::cmd::ReadLine::wc ( const std::string &  str)
inlinestatic

Count the words in the string.

Parameters
strString to count.
Returns
Number of words.

Definition at line 609 of file ReadLine.h.

Referenced by wc().

610  {
611  return ReadLine::wc(str.c_str());
612  }
static int wc(const std::string &str)
Count the words in the string.
Definition: ReadLine.h:609
int ReadLine::wc ( const char *  s)
static

Count the words in the string.

Parameters
sNull-terminated string to count.
Returns
Number of words.

Definition at line 441 of file ReadLine.cxx.

References wc().

442 {
443  int wc = 0;
444 
445  while( *s )
446  {
447  // find start of the next word
448  while( *s && isspace((int)*s) )
449  {
450  s++;
451  }
452 
453  // no more words
454  if( *s == 0 )
455  {
456  break;
457  }
458 
459  ++wc;
460 
461  // find end of the word
462  while( *s && !isspace((int)*s) )
463  {
464  s++;
465  }
466  }
467 
468  return wc;
469 }
static int wc(const std::string &str)
Count the words in the string.
Definition: ReadLine.h:609

Member Data Documentation

ReadLine * ReadLine::ThisObj = NULL
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().


The documentation for this class was generated from the following files: