PTLib
Version 2.10.10
|
Command Line Interpreter class. More...
#include <cli.h>
Classes | |
class | Arguments |
This class is an enhancement to PArgList to add context. More... | |
class | Context |
Context for command line interpreter. More... | |
struct | InternalCommand |
Public Member Functions | |
virtual Context * | StartForeground () |
Initialise a foreground context and return it. More... | |
virtual bool | RunContext (Context *context) |
Run a context. More... | |
Construction | |
PCLI (const char *prompt=NULL) | |
Contracut a new command line interpreter. More... | |
virtual | ~PCLI () |
Destroy the command line interpreter. More... | |
Operations | |
virtual bool | Start (bool runInBackground=true) |
Start a command line interpreter. More... | |
virtual void | Stop () |
Stop and clean up command line interpreters. More... | |
bool | StartContext (PChannel *channel, bool autoDelete=true, bool runInBackground=true) |
Open a command line interpreter context. More... | |
bool | StartContext (PChannel *readChannel, PChannel *writeChannel, bool autoDeleteRead=true, bool autoDeleteWrite=true, bool runInBackground=true) |
virtual Context * | CreateContext () |
Create a new context. More... | |
virtual Context * | AddContext (Context *context=NULL) |
Add a command line interpreter context to the system. More... | |
virtual void | RemoveContext (Context *context) |
Remove the command line interpreter context. More... | |
virtual void | GarbageCollection () |
Remove any closed command line interpreter contexts. More... | |
virtual void | OnReceivedLine (Arguments &line) |
Received a completed command line. More... | |
virtual bool | OnLogIn (const PString &username, const PString &password) |
Received a login name/pasword to be verified. More... | |
void | Broadcast (const PString &message) const |
Set a string to all command line interpreter contexts. More... | |
bool | SetCommand (const char *command, const PNotifier ¬ifier, const char *help=NULL, const char *usage=NULL) |
Register a new command to be interpreted. More... | |
void | ShowHelp (Context &context) |
Show help for registered commands to the context. More... | |
Member access | |
const PString & | GetNewLine () const |
Get new line string output at the end of every line. More... | |
void | SetNewLine (const PString &newLine) |
Set new line string output at the end of every line. More... | |
bool | GetRequireEcho () const |
Get flag for echo is required for entered characters. More... | |
void | SetRequireEcho (bool requireEcho) |
Set flag for echo is required for entered characters. More... | |
const PString & | GetEditCharacters () const |
Get characters used for editing (backspace/delete) command lines. More... | |
void | SetEditCharacters (const PString &editCharacters) |
Set characters used for editing (backspace/delete) command lines. More... | |
const PString & | GetPrompt () const |
Get prompt used for command line interpreter. More... | |
void | SetPrompt (const PString &prompt) |
Set prompt used for command line interpreter. More... | |
const PString & | GetUsernamePrompt () const |
Get prompt used for login (if enabled). More... | |
void | SetUsernamePrompt (const PString &prompt) |
Set prompt used for login (if enabled). More... | |
const PString & | GetPasswordPrompt () const |
Get prompt used for password (if enabled). More... | |
void | SetPasswordPrompt (const PString &prompt) |
Set prompt used for password (if enabled). More... | |
const PString & | GetUsername () const |
Get username for log in validation. More... | |
void | SetUsername (const PString &username) |
Set username for log in validation. More... | |
const PString & | GetPassword () const |
Get password for log in validation. More... | |
void | SetPassword (const PString &password) |
Set password for log in validation. More... | |
const PCaselessString & | GetExitCommand () const |
Get command to be used to exit session. More... | |
void | SetExitCommand (const PCaselessString &exitCommand) |
Set command to be used to exit session. More... | |
const PCaselessString & | GetHelpCommand () const |
Get command to be used to display help. More... | |
void | SetHelpCommand (const PCaselessString &helpCommand) |
Set command to be used to display help. More... | |
const PString & | GetHelpOnHelp () const |
Get help on help. More... | |
void | SetHelpOnHelp (const PCaselessString &helpOnHelp) |
Set help on help. More... | |
const PCaselessString & | GetRepeatCommand () const |
Get the command to be used to repeat the last executed command. More... | |
void | SetRepeatCommand (const PCaselessString &repeatCommand) |
Set the command to be used to repeat the last executed command. More... | |
const PCaselessString & | GetHistoryCommand () const |
Get command that will list/execute command history. More... | |
void | SetHistoryCommand (const PCaselessString &historyCommand) |
Set command that will list/execute command history. More... | |
const PString & | GetNoHistoryError () const |
Get error message for if there is no history. More... | |
void | SetNoHistoryError (const PString &noHistoryError) |
Set error message for if there is no history. More... | |
const PString & | GetCommandUsagePrefix () const |
Get usage prefix for if Arguments::WriteUsage() called. More... | |
void | SetCommandUsagePrefix (const PString &commandUsagePrefix) |
Set usage prefix for if Arguments::WriteUsage() called. More... | |
const PString & | GetCommandErrorPrefix () const |
Get error prefix for if Arguments::WriteError() called. More... | |
void | SetCommandErrorPrefix (const PString &commandErrorPrefix) |
Set error prefix for if Arguments::WriteError() called. More... | |
const PString & | GetUnknownCommandError () const |
Get error message for if unknown command is entered. More... | |
void | SetUnknownCommandError (const PString &unknownCommandError) |
Set error message for if unknown command is entered. More... | |
![]() | |
virtual | ~PObject () |
virtual PObject * | Clone () const |
Create a copy of the class on the heap. More... | |
virtual PINDEX | HashFunction () const |
This function yields a hash value required by the PDictionary class. More... | |
virtual Comparison | Compare (const PObject &obj) const |
Compare the two objects and return their relative rank. More... | |
virtual Comparison | CompareObjectMemoryDirect (const PObject &obj) const |
Determine the byte wise comparison of two objects. More... | |
bool | operator== (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator!= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator< (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator> (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator<= (const PObject &obj) const |
Compare the two objects. More... | |
bool | operator>= (const PObject &obj) const |
Compare the two objects. More... | |
virtual const char * | GetClass (unsigned ancestor=0) const |
Get the current dynamic type of the object instance. More... | |
PBoolean | IsClass (const char *cls) const |
virtual PBoolean | InternalIsDescendant (const char *clsName) const |
Determine if the dynamic type of the current instance is a descendent of the specified class. More... | |
virtual void | PrintOn (ostream &strm) const |
Output the contents of the object to the stream. More... | |
virtual void | ReadFrom (istream &strm) |
Input the contents of the object from the stream. More... | |
Protected Types | |
typedef std::map< PString, InternalCommand > | CommandMap_t |
typedef std::list< Context * > | ContextList_t |
Additional Inherited Members | |
![]() | |
enum | Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 } |
Result of the comparison operation performed by the Compare() function. More... | |
![]() | |
static Comparison | InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size) |
Internal function caled from CompareObjectMemoryDirect() More... | |
static const char * | Class () |
Get the name of the class as a C string. More... | |
![]() | |
PObject () | |
Constructor for PObject, made protected so cannot ever create one on its own. More... | |
Command Line Interpreter class.
This class contains a set of commands, which are executed via a PNotifier, when entered via a PChannel.
The system supports multiple simultaneous interpreter which may access the same command set. For example several telnet sessions.
Note that the various command interpreters could be operating in different threads, so care should be taken for sybchronisation issues on the object being acted upon via the PNotifiers.
|
protected |
|
protected |
PCLI::PCLI | ( | const char * | prompt = NULL | ) |
Contracut a new command line interpreter.
|
virtual |
Destroy the command line interpreter.
This will call Stop() to assure everything is cleaned up before exiting.
Add a command line interpreter context to the system.
If context is NULL then CreateContext() is called to create one.
context | New context to add to the system. |
void PCLI::Broadcast | ( | const PString & | message | ) | const |
Set a string to all command line interpreter contexts.
message | Message to broadcast. |
|
virtual |
Create a new context.
Users may use this to create derived classes for their own use.
|
virtual |
Remove any closed command line interpreter contexts.
|
inline |
Get error prefix for if Arguments::WriteError() called.
Default is ": error: ", always prefixed by command name.
References m_commandErrorPrefix.
|
inline |
Get usage prefix for if Arguments::WriteUsage() called.
Default is "Usage: ".
References m_commandUsagePrefix.
|
inline |
Get characters used for editing (backspace/delete) command lines.
Default is "\b\x7f".
References m_editCharacters.
|
inline |
|
inline |
|
inline |
Get help on help.
This string is output before the individual help is output for each command. Default describes ?, !, !n, !! followed by "Command available are:".
References m_helpOnHelp.
|
inline |
|
inline |
|
inline |
Get error message for if there is no history.
Default is "No command history".
References m_noHistoryError.
|
inline |
Get password for log in validation.
Default is empty string, disabling entry of password.
References m_password.
|
inline |
|
inline |
|
inline |
Get the command to be used to repeat the last executed command.
Default is "!!".
References m_repeatCommand.
|
inline |
|
inline |
Get error message for if unknown command is entered.
Default is "Unknown command".
References m_unknownCommandError.
|
inline |
Get username for log in validation.
Default is empty string, disabling entry of username/password.
References m_username.
|
inline |
Received a login name/pasword to be verified.
Note that the m_username or m_password field must be non-empty for a log in sequence to occur, even if this function is overridden and the memnbers not actually used for validation.
If the m_username is an empty string, but m_password is not, then it the username not prompted for, just the password is required. The reverse is also poassible and only a username entry required.
Default returns true if parameters are equal to m_username, m_password respectively.
|
virtual |
|
virtual |
Remove the command line interpreter context.
The context thread is stopped, the channel closed and memory deleted.
context | Context to remove |
|
virtual |
Run a context.
bool PCLI::SetCommand | ( | const char * | command, |
const PNotifier & | notifier, | ||
const char * | help = NULL , |
||
const char * | usage = NULL |
||
) |
Register a new command to be interpreted.
Note the command may be a series of synonyms of the same command separated by the '
' character.
The command may also contain spaces which separates sub-commands, e.g. "list users".
Returns false if one of the command synonyms was a dupicate of an existing command.
command | Command(s) to register |
notifier | Callback to execute when command interpreted |
help | Help text on command (what it does) |
usage | Usage text on command (syntax/options) |
|
inline |
Set error prefix for if Arguments::WriteError() called.
Default is ": error: ", always prefixed by command name.
References m_commandErrorPrefix.
|
inline |
Set usage prefix for if Arguments::WriteUsage() called.
Default is "Usage: ".
References m_commandUsagePrefix.
|
inline |
Set characters used for editing (backspace/delete) command lines.
Default is "\b\x7f".
References m_editCharacters.
|
inline |
|
inline |
|
inline |
Set help on help.
This string is output before the individual help is output for each command. Default describes ?, !, !n, !! followed by "Command available are:".
References m_helpOnHelp.
|
inline |
|
inline |
|
inline |
Set error message for if there is no history.
Default is "No command history".
References m_noHistoryError.
|
inline |
Set password for log in validation.
Default is empty string, disabling entry of password.
References m_password.
|
inline |
|
inline |
|
inline |
Set the command to be used to repeat the last executed command.
Default is "!!".
References m_repeatCommand.
|
inline |
|
inline |
Set error message for if unknown command is entered.
Default is "Unknown command".
References m_unknownCommandError.
|
inline |
Set username for log in validation.
Default is empty string, disabling entry of username/password.
References m_username.
|
inline |
void PCLI::ShowHelp | ( | Context & | context | ) |
Show help for registered commands to the context.
context | Context to output help to. |
|
virtual |
Start a command line interpreter.
If runInBackground is true the all the command line interpreter contexts that have been added will have their background threads started.
If runInBackground is false, then there must only be one context added and that context is continuously read until it's channel is closed or returns end of file.
runInBackground | Spawn a thread to read and interpret commands |
Reimplemented in PCLISocket, and PCLIStandard.
bool PCLI::StartContext | ( | PChannel * | channel, |
bool | autoDelete = true , |
||
bool | runInBackground = true |
||
) |
Open a command line interpreter context.
channel | Channel to read/write |
autoDelete | Automatically delete channel on exit |
runInBackground | Spawn a thread to read and interpret commands |
bool PCLI::StartContext | ( | PChannel * | readChannel, |
PChannel * | writeChannel, | ||
bool | autoDeleteRead = true , |
||
bool | autoDeleteWrite = true , |
||
bool | runInBackground = true |
||
) |
readChannel | Channel to be used for both read operations. |
writeChannel | Channel to be used for both write operations. |
autoDeleteRead | Automatically delete the read channel |
autoDeleteWrite | Automatically delete the write channel |
runInBackground | Spawn a thread to read and interpret commands |
|
virtual |
Initialise a foreground context and return it.
Reimplemented in PCLIStandard.
|
virtual |
Stop and clean up command line interpreters.
All the running contexts threads will be stopped, closing the channels and memory cleaned up.
Reimplemented in PCLISocket.
|
protected |
Referenced by GetCommandErrorPrefix(), and SetCommandErrorPrefix().
|
protected |
|
protected |
Referenced by GetCommandUsagePrefix(), and SetCommandUsagePrefix().
|
protected |
|
protected |
|
protected |
Referenced by GetEditCharacters(), and SetEditCharacters().
|
protected |
Referenced by GetExitCommand(), and SetExitCommand().
|
protected |
Referenced by GetHelpCommand(), and SetHelpCommand().
|
protected |
Referenced by GetHelpOnHelp(), and SetHelpOnHelp().
|
protected |
Referenced by GetHistoryCommand(), and SetHistoryCommand().
|
protected |
Referenced by GetNewLine(), and SetNewLine().
|
protected |
Referenced by GetNoHistoryError(), and SetNoHistoryError().
|
protected |
Referenced by GetPassword(), and SetPassword().
|
protected |
Referenced by GetPasswordPrompt(), and SetPasswordPrompt().
|
protected |
Referenced by GetPrompt(), and SetPrompt().
|
protected |
Referenced by GetRepeatCommand(), and SetRepeatCommand().
|
protected |
Referenced by GetRequireEcho(), and SetRequireEcho().
|
protected |
Referenced by GetUnknownCommandError(), and SetUnknownCommandError().
|
protected |
Referenced by GetUsername(), and SetUsername().
|
protected |
Referenced by GetUsernamePrompt(), and SetUsernamePrompt().