56 #ifndef _RNR_COMMAND_LINE_H 57 #define _RNR_COMMAND_LINE_H 68 #include "rnr/rnrconfig.h" 319 typedef void (*DeallocFunc)(
void *);
337 DataSect(
const std::string &ns,
void *pData, DeallocFunc fn = NULL);
344 void set(
const std::string &ns,
void *pData, DeallocFunc fn = NULL);
351 const std::string &
ns()
const 460 const std::string strPrompt =
"> ",
461 bool bUseRlLib =
true,
462 bool bIgnoreCase =
false);
474 bool isDefined()
const;
544 virtual int addCommand(
const CmdDesc &desc);
556 virtual int addCommand(
const std::string strSyntax);
559 virtual int addCommand(
const std::string strSyntax,
CmdExec1Func fnExec);
562 virtual int addCommand(
const std::string strSyntax,
CmdExec2Func fnExec);
565 virtual int addCommand(
const std::string strSyntax,
CmdExec3Func fnExec);
576 virtual int removeCommand(
const int uid);
587 virtual int removeCommand(
const std::string &strName);
594 virtual int removeAllCommands();
607 virtual int compile();
627 int addDataSection(
const std::string &ns,
641 int removeDataSection(
const std::string &ns);
651 void *getDataSection(
const std::string &ns);
660 bool isReservedDataSection(
const std::string &ns)
const;
685 return readCommand(stdin, uid, iform, argv);
715 return readCommand(stdin, argv);
735 virtual int readCommand(FILE *fp,
772 bool kbhit(FILE *fp);
832 void pushPrompt(
const std::string &strPrompt);
846 const std::string &getPrompt()
const;
855 return m_readline.getLineNum();
865 m_readline.setLineNum(uLineNum);
873 m_readline.resetLineNum();
891 int numOfArgs(
int uid,
int iform)
const;
902 int numOfArgs(
const CmdExtArg &arg)
const;
916 int numOfRequiredArgs(
int uid,
int iform)
const;
931 int numOfRequiredArgs(
const CmdExtArg &arg)
const;
943 int numOfOptionalArgs(
int uid,
int iform)
const;
954 int numOfOptionalArgs(
const CmdExtArg &arg)
const;
965 const std::string &getArgName(
const CmdExtArg &arg)
const;
1013 virtual ssize_t tokenizeInput(
const std::string &strInput,
1027 virtual ssize_t tokenizeInput(
const std::string &strInput,
1052 bool hasCmd(
const int uid)
const;
1061 bool hasCmd(
const std::string &strName)
const;
1070 const CmdDef &at(
const int uid)
const;
1079 const CmdDef &at(
const std::string &strName)
const;
1090 const CmdDef &operator[](
const int uid)
const;
1101 const CmdDef &operator[](
const std::string &strName)
const;
1110 return (
int)m_cmdDefs.size();
1121 return m_cmdDefs.begin();
1132 return m_cmdDefs.end();
1140 const std::string &getErrorStr()
const;
1151 std::ostream &backtrace(std::ostream &os,
const bool bAll =
false)
const;
1207 CmdDef &cmdAt(
const int uid);
1218 CmdDef &cmdAt(
const std::string &strName);
1237 virtual ssize_t tokenizeSyntax(
const std::string &strSyntax,
1257 virtual ssize_t lexSyntaxWord(
const std::string &strSyntax,
1285 virtual ssize_t lexSyntaxParenExpr(
const std::string &strSyntax,
1306 virtual ssize_t lexWord(
const std::string &strInput,
1342 virtual ssize_t lexQuotedString(
const std::string &strInput,
1356 virtual void logLexToken(
const std::string &strSource,
1358 const ssize_t cursor,
1360 const bool bLoc =
false);
1370 void pushToken(
const std::string &strSource,
1372 const ssize_t cursor,
1389 virtual int compile(
CmdDef &cmddef);
1436 virtual int parseSyntax(
CmdDef &cmddef,
1459 bool parseArgv0(
CmdDef &cmddef,
1480 bool parseRequiredArgList(
CmdDef &cmddef,
1501 bool parseOptionalArgList(
CmdDef &cmddef,
1523 bool parseArg(
CmdDef &cmddef,
1545 bool parseXorListArg(
CmdDef &cmddef,
1568 bool parseVariableArg(
CmdDef &cmddef,
1590 bool parseLiteralArg(
CmdDef &cmddef,
1612 bool parseXorList(
CmdDef &cmddef,
1635 bool parseIdentifier(
CmdDef &cmddef,
1639 std::string &strIdent);
1663 bool parseVarMod(
CmdDef &cmddef,
1688 bool parseVarType(
CmdDef &cmddef,
1714 bool parseVarRangeExpr(
CmdDef &cmddef,
1736 bool parseVarRegExpr(
CmdDef &cmddef,
1758 bool parseLiteralValue(
CmdDef &cmddef,
1762 std::string &strValue);
1777 bool tokEq(
const std::string strCmp,
const TokenVec &tokens,
size_t &pos);
1791 return token.
value() == strCmp;
1803 bool tokIdentifier(
const TokenVec &tokens,
size_t &pos);
1821 int processInput(
const std::string &strLine,
CmdExtArgVec &argv);
1846 int matchCommand(
const CmdDef &cmddef,
1872 int checkReadResult();
1895 virtual int rlBuildReadLineGenerator();
1915 static const std::string rlGeneratorWrapper(
void *pAppArg,
1916 const std::string &strText,
1918 const std::string &strContext,
1940 virtual const std::string rlGenerator(
const std::string &strText,
1942 const std::string &strContext,
1954 void rlArgDefs(
const std::string &strSubtext,
1955 std::vector<CmdArgDef*> &argdefs);
1966 void rlTabList(
const std::string &strText,
1967 std::vector<CmdArgDef*> &argdefs,
1982 bool rlPartialMatch(
const std::string &strText,
1983 const std::string strLiteral,
1991 #endif // _RNR_COMMAND_LINE_H
Command line extended argument interface.
Command EXTended ARGument class holding parsed command context and the raw and converted argmument va...
CmdExecMap::iterator CmdExecIter
exec iterator type
std::vector< std::string > StringVec
Useful types.
std::string m_strName
name of this command line
CmdExec(int uid, CmdExec2Func fn)
Variant 2 constructor.
CmdDefMap::const_iterator CmdDefCIter
cmd const iter type
LogBook m_log
trace and error log
CmdExec(const CmdExec &src)
Copy constructor.
virtual int readCommand(CmdExtArgVec &argv)
Read an input line from stdin and match to the best compiled command.
const void * data() const
Return section data.
CmdDefCIter begin() const
Return a constant iterator pointing to the first element of the list of command definitions.
ReadLine m_readline
readline interface
Command line interface data section class.
CmdExec2Func fn2
execution function variant 2
size_t getLineNum() const
Get the line number of the last read line.
bool m_bIsCompiled
has [not] been successfully compiled
bool getBtEnable() const
Test if backtracing is enabled.
Variant
Execution function variant enumeration.
void quit()
Mark command-line interface to quit.
Variant m_variant
function variant enum
bool peekEq(const std::string strCmp, const Token &token) const
Peek if token is equal to string.
const char *const DataSectNsCore
Reserved command line data section namespaces.
CmdDefCIter end() const
Return a constant iterator referring to the past-the-end element of the list of command definitions...
int(* CmdExec2Func)(const CmdExtArgVec &argv)
Command execution function type, variant 2.
std::map< unsigned, CmdExec > CmdExecMap
exec map type
const int NoUid
Special values.
int numOfCmds() const
Get the total number of added commands.
CmdExec(int uid, CmdExec3Func fn)
Variant 3 constructor.
Of string spaces and their strangian operators.
User available command description structure.
int m_uid
command unique id associated with this execution
CmdExec()
Default constructor.
void * m_pData
pointer to section data
std::map< std::string, DataSect > DataSectMap
section map type
std::vector< Token > TokenVec
vector of tokens type
Compiled command definition class.
const std::string & value() const
Return token string.
int execute(const str::StringVec &argv)
Execute a comamnd with the given arguments.
std::vector< CmdExtArg > CmdExtArgVec
vector of ext args type
CmdExecMap m_cmdExecs
map of added command executions
CmdDefMap::iterator CmdDefIter
cmd iterator type
DeallocFunc m_fnDealloc
data deallocator function
FnVar m_exec
function execution variant
The thin ReadLine wrapper class interface.
CmdExec(int uid, CmdExec1Func fn)
Variant 1 constructor.
std::string m_strNs
data section namespace
friend std::ostream & operator<<(std::ostream &os, const CmdExec &obj)
Insert object into output stream.
Command line command definition class interface.
Helper class to hold a command line execution function.
const std::string & ns() const
Return namespace.
ArgType
Argument type enumeration.
std::map< unsigned, CmdDef > CmdDefMap
command map type
Command line argument definition class interface.
DataSectMap::iterator DataSectIter
iterator type
void setBtEnable(bool bEnable)
Enable/disable backtracing.
int(* CmdExec3Func)(CommandLine &cli, const CmdExtArgVec &argv)
Command execution function type, variant 3.
int(* CmdExec1Func)(const str::StringVec &argv)
Command execution function type, variant 1.
virtual int readCommand(int &uid, int &iform, str::StringVec &argv)
Read an input line from stdin and match to the best compiled command.
PromptStack m_prompts
stack of prompt strings
const DeallocFunc deallocator() const
Return deallocator.
DeallocFunc deallocator()
Return deallocator.
CmdExecMap::const_iterator CmdExecCIter
exec const iter type
CmdExec3Func fn3
execution function variant 3
ReadLine class provides a C++ wrapper around the readline C library.
void(* DeallocFunc)(void *)
Union of all variant execution functions.
Command line core data types.
bool ok() const
Test if command-line interface is ok to continue.
std::vector< range > RangeVec
vector of subranges
const std::string & getName() const
Get command line interface's name.
Parsed token container class.
The Regular Expression Class interface.
void setLineNum(const size_t uLineNum)
Set the current line number.
void * data()
Return section data.
DataSectMap::const_iterator DataSectCIter
const iter type
DataSectMap m_dataSects
data sections
std::string c14n(const std::string &str)
Simple canonicalization of a string.
bool m_bIgnoreCase
do [not] ignore case on commands
str::StringVec PromptStack
prompt stack type
int m_nUidCnt
unique id counter
int getUid() const
Get associated command's unique id.
void resetLineNum()
Reset the line number to zero.
Simple, token container class interface.
CmdDefMap m_cmdDefs
map of added command definitions
CmdExec1Func fn1
execution function variant 1