Dynamixel  2.9.5
RoadNarrows Robotics Dynamixel Package
ReadLine Class Reference

ReadLine class provides a c++ wrapper around the readline c library. More...

#include <dynashell_readline.h>

Public Member Functions

 ReadLine (const string &strName)
 Initialization constructor.
 
 ReadLine (const char *sName)
 Initialization constructor.
 
virtual ~ReadLine ()
 Destructor.
 
int RegisterGenerator (const string strRegEx, ReadLineAppGenFunc_T fnAppGen, void *pAppArg)
 Register application-specific tab-completion generator associated. More...
 
int RegisterGenerator (const char *sRegEx, ReadLineAppGenFunc_T fnAppGen, void *pAppArg)
 Register application-specific tab-completion generator associated. More...
 
void UnregisterGenerator (int nUid)
 Unregister application-specific generator associated with path. More...
 
char * iReadLine (const char *sPrompt)
 Interactively read a line of input from standard input. More...
 
void AddToHistory (const char *sInput)
 Add line to history. More...
 

Static Public Member Functions

static char * fReadLine (FILE *fp, const char *sPrompt)
 Read one input line from the given input stream. More...
 
static char * FileCompletionGenerator (const char *sText, int nState)
 File name tab completion generator. More...
 
static char * UserCompletionGenerator (const char *sText, int nState)
 User name tab completion generator. More...
 
static char * dupstr (const string &str)
 Duplicate string. More...
 
static char * dupstr (const char *s)
 Duplicate string. More...
 
static string strip (string &str)
 Strip string of leading and trailing white space. More...
 
static char * strip (char *s)
 Strip string of leading and trailing white space. More...
 
static string c14n (const string &str, size_t nLen)
 Canonicalization of a string. More...
 
static string c14n (const char *s, size_t nLen)
 Canonicalization of a string. More...
 
static int tokenize (char *s, char *tokv[], size_t tokmax)
 Tokenize input. More...
 
static int wc (const 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
 
static const int NOT_REG = -1
 not registered return value
 
static ReadLineReadLineThis
 static pointer this single instance More...
 

Protected Types

typedef vector< ReadLineEntryVecAppEntry
 Internal registered generator map type.
 

Protected Member Functions

char ** Completion (const char *sText, int nStart, int nEnd)
 Command completion callback function. More...
 
void MatchGenerator (int nEnd)
 Find application-specific generator associated with the first characters in the readline buffer. More...
 

Static Protected Member Functions

static char ** CompletionWrap (const char *sText, int nStart, int nEnd)
 Command completion callback function wrapper. More...
 
static char * GeneratorWrap (const char *sText, int nState)
 Generator wrapper. More...
 

Protected Attributes

char * m_sName
 readline name
 
int m_nUidCounter
 unique id counter
 
VecAppEntry m_vecGenerators
 map of generators
 
VecAppEntry::iterator m_posMatched
 matched entry position
 
string m_strContext
 current readline buffer context
 
size_t m_uTextLen
 text length of text to generator
 

Detailed Description

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

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

Definition at line 166 of file dynashell_readline.h.

Member Function Documentation

void ReadLine::AddToHistory ( const char *  sLine)

Add line to history.

The line is added if the line is not empty and does not match the last command.

Parameters
Lineto add.

Definition at line 320 of file dynashell_readline.cxx.

Referenced by DynaShell::AddToHistory().

321 {
322 #ifdef HAVE_READLINE
323  string str = sLine;
324  HIST_ENTRY *pCurHist;
325 
326  str = strip(str);
327 
328  if( !str.empty() )
329  {
330  pCurHist = previous_history();
331 
332  if( (pCurHist == NULL) || strcmp(pCurHist->line, str.c_str()) )
333  {
334  add_history(sLine);
335  }
336  }
337 #endif // HAVE_READLINE
338 }
static string strip(string &str)
Strip string of leading and trailing white space.
static string ReadLine::c14n ( const string &  str,
size_t  nLen 
)
inlinestatic

Canonicalization of a string.

Parameters
sNull-terminated string to canonicalize.
uLen(Sub)length of string to canonicalize.
Returns
Return string holding canonical form.

Definition at line 304 of file dynashell_readline.h.

Referenced by MatchGenerator().

305  {
306  return ReadLine::c14n(str.c_str(), nLen);
307  }
static string c14n(const string &str, size_t nLen)
Canonicalization of a string.
string ReadLine::c14n ( const char *  s,
size_t  uLen 
)
static

Canonicalization of a string.

Note
c14n is an cute abbreviation where 14 represents the number of letters between the c and n.
Parameters
sNull-terminated string to canonicalize.
uLen(Sub)length of string to canonicalize.
Returns
Return string holding canonical form.

Definition at line 441 of file dynashell_readline.cxx.

References strip().

442 {
443  string str(s, uLen);
444  size_t pos;
445 
446  // strip leading and trailing whitespace
447  str = ReadLine::strip(str);
448 
449  // convert multiple whitespace sequences to a single blank
450  if( str.length() != 0 )
451  {
452  while( (pos = str.find(" ")) != str.npos )
453  {
454  str = str.replace(pos, 2, " ");
455  }
456  }
457 
458  //cout << " (" << str << ")" << endl;
459 
460  return str;
461 }
static string strip(string &str)
Strip string of leading and trailing white space.
char ** ReadLine::Completion ( const char *  sText,
int  nStart,
int  nEnd 
)
protected

Command completion callback function.

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 577 of file dynashell_readline.cxx.

578 {
579 #ifdef HAVE_READLINE
580  MatchGenerator(nStart);
581 
582  return rl_completion_matches(sText, GeneratorWrap);
583 
584 #else
585  return NULL;
586 #endif // HAVE_READLINE
587 }
void MatchGenerator(int nEnd)
Find application-specific generator associated with the first characters in the readline buffer...
static char * GeneratorWrap(const char *sText, int nState)
Generator wrapper.
char ** ReadLine::CompletionWrap ( const char *  sText,
int  nStart,
int  nEnd 
)
staticprotected

Command completion callback function wrapper.

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 in case we want to do some simple parsing.

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 563 of file dynashell_readline.cxx.

564 {
565  return ReadLineThis->Completion(sText, nStart, nEnd);
566 }
char ** Completion(const char *sText, int nStart, int nEnd)
Command completion callback function.
static ReadLine * ReadLineThis
static pointer this single instance
char * ReadLine::dupstr ( const char *  s)
static

Duplicate string.

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

Definition at line 347 of file dynashell_readline.cxx.

348 {
349  char *t = new char[strlen(s)+1];
350  strcpy(t, s);
351  return t;
352 }
Definition: t.py:1
static char* ReadLine::FileCompletionGenerator ( const char *  sText,
int  nState 
)
inlinestatic

File name tab completion 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 245 of file dynashell_readline.h.

Referenced by DynaShellCmdCreate::TabCompletion(), DynaShellCmdLoadRecording::TabCompletion(), DynaShellCmdScript::TabCompletion(), DynaShellCmdMegaScan::TabCompletion(), DynaShellCmdSaveRecording::TabCompletion(), and DynaShellCmdPlay::TabCompletion().

246  {
247 #ifdef HAVE_READLINE
248  return rl_filename_completion_function(sText, nState);
249 #else
250  return NULL;
251 #endif // HAVE_READLINE
252  }
char * ReadLine::fReadLine ( FILE *  fp,
const char *  sPrompt 
)
static

Read one input line from the given input stream.

The freadline functions will read one line of input from the given input file pointer using the prompt string to prompt the user. If the prompt is NULL or an empty string then no prompt is issued. The line returned is allocated, so the caller must free it when finished.

The line returned has the final newline removed, so only the text of the line remains.

Parameters
fpFile pointer to input steam.
sPromptOptional user prompt string.
Returns
If no errors occurred and EOF is not encountered, an allocated, null-terminated line buffer is return. Else NULL is return.

Definition at line 281 of file dynashell_readline.cxx.

Referenced by DynaShell::GetInputLine().

282 {
283  static size_t BufSize = 4096;
284 
285  char *bufLine;
286  size_t n;
287 
288  bufLine = new char[BufSize];
289 
290  if( sPrompt && *sPrompt )
291  {
292  fprintf(stdout, "%s", sPrompt);
293  fflush(stdout);
294  }
295 
296  if( fgets(bufLine, BufSize, fp) == NULL )
297  {
298  delete[] bufLine;
299  return NULL;
300  }
301 
302  bufLine[BufSize-1] = 0;
303  n = strlen(bufLine);
304  if( (n > 0) && (bufLine[n-1] == '\n') )
305  {
306  bufLine[n-1] = 0;
307  }
308 
309  return bufLine;
310 }
char * ReadLine::GeneratorWrap ( const char *  sText,
int  nState 
)
staticprotected

Generator wrapper.

Calls the matched, 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 601 of file dynashell_readline.cxx.

References FIRST.

602 {
603  // no registered generator match current path
605  {
606  return NULL;
607  }
608 
609  // first time through
610  if( nState == ReadLine::FIRST )
611  {
612  ReadLineThis->m_uTextLen = strlen(sText);
613  }
614 
615  // call application-specific generator
616  return ReadLineThis->m_posMatched->m_fnAppGen(
617  ReadLineThis->m_posMatched->GetUid(),
618  sText,
620  nState,
621  ReadLineThis->m_strContext.c_str(),
622  ReadLineThis->m_posMatched->m_pAppArg);
623 }
VecAppEntry::iterator m_posMatched
matched entry position
VecAppEntry m_vecGenerators
map of generators
static const int FIRST
first state
static ReadLine * ReadLineThis
static pointer this single instance
size_t m_uTextLen
text length of text to generator
string m_strContext
current readline buffer context
char* ReadLine::iReadLine ( const char *  sPrompt)
inline

Interactively read a line of input from standard input.

If the readline library feature is enabled, then the input is controlled by the readline() call along with history and tab completion.

Parameters
sPromptOptional user prompt string.
Returns
If no errors occurred and EOF is not encountered, an allocated, null-terminated line buffer is return. Else NULL is return.

Definition at line 217 of file dynashell_readline.h.

Referenced by DynaShell::GetInputLine().

218  {
219 #ifdef HAVE_READLINE
220  return readline(sPrompt); // fixed at stdin
221 #else
222  retrun fReadLine(stdin, sPrompt); // so then so are we
223 #endif // HAVE_READLINE
224  }
static char * fReadLine(FILE *fp, const char *sPrompt)
Read one input line from the given input stream.
void ReadLine::MatchGenerator ( int  nLen)
protected

Find application-specific generator associated with the first characters in the readline buffer.

Parameters
nLenLength in readline buffer.

Definition at line 631 of file dynashell_readline.cxx.

References c14n(), and ReadLineThis.

632 {
634 
635  m_strContext.clear();
636 
637 #ifdef HAVE_READLINE
638 
639  if( nLen > 0 )
640  {
641  m_strContext = ReadLine::c14n(rl_line_buffer, (size_t)nLen);
642  }
643  else
644  {
645  m_strContext = "";
646  }
647 
648  //fprintf(stderr, "\nDBG: matching '%s' ", m_strContext.c_str());
649 
650  for(m_posMatched = m_vecGenerators.begin();
651  m_posMatched != m_vecGenerators.end();
652  ++m_posMatched)
653  {
654  //fprintf(stderr, "DBG: %d: regex(%s) ",
655  // m_posMatched->GetUid(), m_posMatched->m_regex.GetRegEx());
656 
657  if( m_posMatched->m_regex.Match(m_strContext) )
658  {
659  //fprintf(stderr, "matched regex(%s)", m_posMatched->m_regex.GetRegEx());
660  break;
661  }
662  }
663  //fprintf(stderr, "\n");
664 #endif // HAVE_READLINE
665 }
VecAppEntry::iterator m_posMatched
matched entry position
static string c14n(const string &str, size_t nLen)
Canonicalization of a string.
VecAppEntry m_vecGenerators
map of generators
string m_strContext
current readline buffer context
int ReadLine::RegisterGenerator ( const string  strRegEx,
ReadLineAppGenFunc_T  fnAppGen,
void *  pAppArg 
)
inline

Register application-specific tab-completion generator associated.

Parameters
strRegExRegular expression applied to current readline buffer state.
fnAppGenApplication-specific generator function.
pAppArgOptional application argument generator function.
Returns
On successful registration, a unique id ≥ 0 is returned.
On regular expression evalution failure, the generator is not registered and ReadLine::NOT_REG is returned.

Definition at line 193 of file dynashell_readline.h.

Referenced by DynaShell::InputInit(), DynaShell::PublishCommand(), and DynaShell::PublishMap().

196  {
197  return RegisterGenerator(strRegEx.c_str(), fnAppGen, pAppArg);
198  }
int RegisterGenerator(const string strRegEx, ReadLineAppGenFunc_T fnAppGen, void *pAppArg)
Register application-specific tab-completion generator associated.
int ReadLine::RegisterGenerator ( const char *  sRegEx,
ReadLineAppGenFunc_T  fnAppGen,
void *  pAppArg 
)

Register application-specific tab-completion generator associated.

Parameters
sRegExRegular expression applied to current readline buffer state.
fnAppGenApplication-specific generator function.
pAppArgOptional application argument generator function.
Returns
On successful registration, a unique id ≥ 0 is returned.
On regular expression evalution failure, the generator is not registered and ReadLine::NOT_REG is returned.

Definition at line 223 of file dynashell_readline.cxx.

References ReadLineEntry::IsValid(), and ReadLineEntry::m_nUid.

226 {
227  string str;
228  ReadLineEntry entry(sRegEx, fnAppGen, pAppArg);
229 
230  if( entry.IsValid() )
231  {
232  entry.m_nUid = m_nUidCounter;
233  m_vecGenerators.push_back(entry);
234  //fprintf(stderr, "DBG: %d: regex(%s)\n", m_nUidCounter, sRegEx);
235  return m_nUidCounter++;
236  }
237  else
238  {
239  return NOT_REG;
240  }
241 }
VecAppEntry m_vecGenerators
map of generators
int m_nUidCounter
unique id counter
static const int NOT_REG
not registered return value
string ReadLine::strip ( string &  str)
static

Strip string of leading and trailing white space.

Parameters
[in]strInput string to strip.
Returns
Stripped string.

Definition at line 361 of file dynashell_readline.cxx.

Referenced by c14n().

362 {
363  string t = str;
364  int i;
365 
366  // strip leading blanks
367  for(i=0; i<t.length(); ++i)
368  {
369  if( !isspace((int)t[i]) )
370  {
371  break;
372  }
373  }
374 
375  if( i != 0 )
376  {
377  t = t.substr(i);
378  }
379 
380  // strip trailing blanks
381  for(i=t.length()-1; i>0; --i)
382  {
383  if( !isspace((int)t[i]) )
384  {
385  break;
386  }
387  }
388 
389  if( i != t.length()-1 )
390  {
391  t = t.substr(0, i+1);
392  }
393 
394  return t;
395 }
Definition: t.py:1
char * ReadLine::strip ( char *  s)
static

Strip string of leading and trailing white space.

A null character '\0' is inserted after the last non-white space character.

Parameters
[in,out]sNull-terminated char*.
Returns
Pointer to first non-white space character in s or to null-terminator if no non-white space characters are found.

Definition at line 407 of file dynashell_readline.cxx.

408 {
409  char *left, *right;
410 
411  for(left = s; isspace((int)*left); left++) ;
412 
413  if( *left == 0 )
414  {
415  return left;
416  }
417 
418  right = left + strlen(left) - 1;
419 
420  while( (right > left) && isspace((int)*right) )
421  {
422  right--;
423  }
424 
425  *++right = '\0';
426 
427  return left;
428 }
int ReadLine::tokenize ( char *  s,
char *  tokv[],
size_t  tokmax 
)
static

Tokenize input.

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

Definition at line 472 of file dynashell_readline.cxx.

Referenced by DynaShell::Run().

473 {
474  int tokc = 0;
475 
476  while( tokc < (int)tokmax )
477  {
478  // find start of the next token
479  while( *s && isspace((int)*s) )
480  {
481  s++;
482  }
483 
484  // no more tokens
485  if( *s == 0 )
486  {
487  return tokc;
488  }
489 
490  // new token
491  tokv[tokc++] = s;
492 
493  // find end of the token
494  while( *s && !isspace((int)*s) )
495  {
496  s++;
497  }
498 
499  // end of the line
500  if( *s == 0 )
501  {
502  return tokc;
503  }
504 
505  // null terminate
506  *s++ = 0;
507  }
508 
509  return tokc;
510 }
void ReadLine::UnregisterGenerator ( int  nUid)

Unregister application-specific generator associated with path.

Parameters
strPathUnique path string.

Definition at line 248 of file dynashell_readline.cxx.

249 {
250  VecAppEntry::iterator iter;
251 
252  for(iter = m_vecGenerators.begin();
253  iter != m_vecGenerators.begin();
254  ++iter)
255  {
256  if( iter->GetUid() == nUid )
257  {
258  m_vecGenerators.erase(iter);
259  return;
260  }
261  }
262 }
VecAppEntry m_vecGenerators
map of generators
static char* ReadLine::UserCompletionGenerator ( const char *  sText,
int  nState 
)
inlinestatic

User name tab completion 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 264 of file dynashell_readline.h.

265  {
266 #ifdef HAVE_READLINE
267  return rl_username_completion_function(sText, nState);
268 #else
269  return NULL;
270 #endif // HAVE_READLINE
271  }
int ReadLine::wc ( const char *  s)
static

Count the words in the string.

Parameters
strString to count.
Returns
Number of words.

Definition at line 519 of file dynashell_readline.cxx.

520 {
521  int wc = 0;
522 
523  while( *s )
524  {
525  // find start of the next word
526  while( *s && isspace((int)*s) )
527  {
528  s++;
529  }
530 
531  // no more words
532  if( *s == 0 )
533  {
534  break;
535  }
536 
537  ++wc;
538 
539  // find end of the word
540  while( *s && !isspace((int)*s) )
541  {
542  s++;
543  }
544  }
545 
546  return wc;
547 }
static int wc(const string &str)
Count the words in the string.

Member Data Documentation

ReadLine * ReadLine::ReadLineThis
static

static pointer this single instance

Pointer to the only instance of readline supported per application.

Definition at line 172 of file dynashell_readline.h.

Referenced by MatchGenerator().


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