58 #include <sys/types.h> 74 #include <readline/readline.h> 75 #include <readline/history.h> 76 #endif // HAVE_READLINE 78 #include "rnr/rnrconfig.h" 111 ReadLine::ReadLine(
const string strName,
112 const string strPrompt,
114 m_strName(strName), m_strPrompt(strPrompt), m_bUseRlLib(bUseRlLib)
140 #endif // HAVE_READLINE 169 rl_attempted_completion_over = 0;
170 rl_completion_suppress_append = 0;
171 #endif // HAVE_READLINE 187 char *sLine = readline(
getPrompt().c_str());
207 else if( bInteractive )
220 #else // !HAVE_READLINE 224 #endif // HAVE_READLINE 232 fprintf(stdout,
"%s",
getPrompt().c_str());
241 static size_t BufSize = 4096;
243 char bufLine[BufSize];
249 if( fgets(bufLine, BufSize, fp) != NULL )
251 bufLine[BufSize-1] = 0;
258 if( bufLine[n-1] ==
'\n' )
269 LOGWARN(
"Input line too long. Exceeds %zu characters.", BufSize);
297 void ReadLine::addToHistory(
const string &strLine)
301 HIST_ENTRY *pCurHist;
307 pCurHist = previous_history();
309 if( (pCurHist == NULL) || strcmp(pCurHist->line, str.c_str()) )
311 add_history(str.c_str());
314 #endif // HAVE_READLINE 319 return isatty(fileno(fp))?
true:
false;
329 m_bEoF = feof(fp)?
true:
false;
338 ss << strerror(errno) <<
"(errno=" << errno <<
"}";
342 ss <<
"File read error.";
358 size_t size =
sizeof(char) * (strlen(s) + 1);
359 char *t = (
char *)malloc(size);
373 for(i = 0; i < len; )
376 while( (i < len) && isspace((
int)str[i]) )
390 while( (i < len) && !isspace((
int)str[i]) )
392 tok.push_back(str[i++]);
395 tokens.push_back(tok);
398 return tokens.size();
405 while( tokc < (
int)tokmax )
408 while( *s && isspace((
int)*s) )
423 while( *s && !isspace((
int)*s) )
448 while( *s && isspace((
int)*s) )
462 while( *s && !isspace((
int)*s) )
490 #endif // HAVE_READLINE 496 string strContext(rl_line_buffer);
500 vector<string> matchList;
504 rl_attempted_completion_over = 0;
505 rl_completion_suppress_append = 0;
510 for(nIndex = 0; ; ++nIndex)
513 strContext, nStart, nEnd, uFlags);
516 if( strMatch.empty() )
520 rl_attempted_completion_over = 1;
525 rl_completion_suppress_append = 1;
536 matchList.push_back(
"");
538 matchList.push_back(strMatch);
543 if( matchList.size() == 0 )
554 i =
sizeof(
char*) * (matchList.size() + 1);
556 tabList = (
char**)malloc(i);
559 for(i = 0; i < matchList.size(); ++i)
561 tabList[i] =
dupstr(matchList[i]);
567 #else // !HAVE_READLINE 571 #endif // HAVE_READLINE 583 string strText(sText);
584 string strContext(rl_line_buffer);
597 #else // !HAVE_READLINE 601 #endif // HAVE_READLINE bool m_bFError
last file op file error condition
std::string m_strError
error string
std::string m_strName
readline name
no filename TAB completion attempt
std::vector< std::string > StringVec
Useful types.
std::string & rlreadLine()
Interactively read a line of input from standard input (stdin).
static char ** completionWrapper(const char *sText, int nStart, int nEnd)
Command completion callback function wrapper.
void clearStreamStatus()
Clear stream status prior to next read operation.
AppGenFunc m_fnAppGen
application-specific generator
char *(* AppGenFunc)(void *pAppArg, const std::string &strText, int nState, const std::string &strContext)
Application-specific TAB completion generator function type.
void registerAltGenerator(AltAppGenFunc fnAltAppGen, void *pAppArg)
Register alternate application-specific tab-completion generator.
bool m_bUseRlLib
[do not] use readline library
no space(' ') after TAB completion
Of string spaces and their strangian operators.
const std::string & getPrompt() const
Get the current prompt string.
static int wc(const std::string &str)
Count the words in the string.
bool m_bEoF
last file op end of file condition
char ** altCompletion(const std::string strText, int nStart, int nEnd)
Command completion callback function.
static size_t tokenize(const std::string &str, str::StringVec &tokens)
Tokenize the given string.
std::string & trim(std::string &str)
Trim string in-place of leading and trailing whitespace.
static const int FIRST
first state
void * m_pAppArg
application-specific argument
void setStreamStatus(FILE *fp)
Set stream status for the last read operation.
std::string & freadLine(FILE *fp)
Read one input line from the given input stream.
static char * generatorWrapper(const char *sText, int nState)
Generator wrapper.
The thin ReadLine wrapper class interface.
bool isInteractive(FILE *fp)
Test if input file pointer is interactive.
virtual ~ReadLine()
Destructor.
char ** completion(const char *sText, int nStart, int nEnd)
Command completion callback function.
size_t m_uLineNum
line number
ReadLine class provides a C++ wrapper around the readline C library.
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.
no default TAB completion match
void registerGenerator(AppGenFunc fnAppGen, void *pAppArg)
Register application-specific tab-completion generator.
AltAppGenFunc m_fnAltAppGen
alternate app-specific generator
std::string & ireadLine()
Interactively read a line of input from standard input (stdin).
static ReadLine * ThisObj
static pointer to this single instance
static char * dupstr(const std::string &str)
Duplicate string.
void unregisterGenerator()
Unregister application-specific generator associated with path.
std::string m_strLine
last read line
const std::string & getErrorStr() const
Get the most recently set error string.