Commander-API V3.0.0
Lightweight Command Parser
|
#include <Commander-API.hpp>
Classes | |
struct | API_t |
Structure for command data. More... | |
struct | systemVariableData_t |
union | systemVariablePointer_t |
Public Types | |
enum | debugLevel_t { DEBUG_OFF = 0 , DEBUG_ERROR = 1 , DEBUG_DEBUG = 2 , DEBUG_VERBOSE = 3 } |
enum | systemVariableType_t { VARIABLE_FLOAT , VARIABLE_INT , VARIABLE_STRING } |
Enum for system variable type. More... | |
typedef struct Commander::API_t | API_t |
Structure for command data. More... | |
typedef CommanderDatabase< API_t >::dataRecord_t | systemCommand_t |
typedef CommanderDatabase< systemVariableData_t >::dataRecord_t | systemVariable_t |
Public Member Functions | |
void | attachTreeFunction (systemCommand_t *API_tree_p, uint32_t API_tree_size_p) |
Attach API-tree to the object. More... | |
bool | init () |
Initializer. More... | |
bool | execute (const char *cmd, Stream *channel_p=NULL, CommandCaller *caller_p=NULL) |
Execution function for Stream response. More... | |
void | attachDebugChannel (Stream *resp) |
Attach a debug channel to the object. More... | |
void | printHelp (Stream *out, bool description, bool style=false) |
Print the help text to a specified Stream. More... | |
bool | enablePipeModuleFunc (char *buffer, int bufferSize, commanderPipeChannel *pipeChannel_p) |
Enable pipe functionality. More... | |
void | enableFormatting () |
Enable formatting. More... | |
void | disableFormatting () |
Disable formatting. More... | |
void | update (char *buffer, int bufferSize, Stream *channel_p) |
Create a very very simple command lime. More... | |
bool | commandExists (const char *cmd, systemCommand_t **cmd_ptr=NULL) |
int | generateHint (const char *fraction, char *buffer_p, int buffer_size_p) |
const char * | getHint (int index, bool only_remaining_chars=false) |
Static Public Member Functions | |
static void | attachVariablesFunction (systemVariable_t *variables_p, uint32_t variables_size_p) |
Attach system variables list to the object. More... | |
static systemVariable_t * | getSystemVariable (const char *name) |
Get the instance if a system variable by its name. More... | |
static void | printSystemVariable (Stream *channel_p, const char *name, int decimalPlaces=2) |
Print the value of a system variable. More... | |
static void | printSystemVariables (Stream *channel_p) |
Print all the available system variables. More... | |
static void | setDebugLevel (debugLevel_t debugLevel_p) |
static void | printArgumentError (Stream *channel_p) |
Print 'Argument error!' to a specified Serial channel. More... | |
static bool | inString (const char *source, int index) |
Detect if character is in the string. More... | |
static int | floatToString (float number, char *buffer, int bufferSize) |
Public Attributes | |
AutoComplete * | lastHint = NULL |
const char * | lastCommandHint = NULL |
int | lastCommandHintOffset = 0 |
Static Public Attributes | |
static const char * | version = COMMANDER_API_VERSION |
Library version string. More... | |
Private Member Functions | |
bool | executeCommand (const char *cmd) |
Command execution. More... | |
int | hasChar (const char *str, char c, bool ignoreString=true) |
Search for a character in a string. More... | |
int | hasChar (const char *str, char c, int number, bool ignoreString=true) |
Search for a character in a string. More... | |
void | printBrokenPipe () |
Private Attributes | |
CommanderDatabase< API_t > | regularCommands |
Starting address of the API-tree. More... | |
char | tempBuff [COMMANDER_MAX_COMMAND_SIZE+1] |
Internal command buffer. More... | |
const char * | originalCommandData |
This variable will store the address of the input data. More... | |
bool | formatting = false |
Stream * | dbgResponse = NULL |
Pointer to response class. More... | |
commanderPipeChannel * | pipeChannel = NULL |
Channel for the internal piping. More... | |
char * | pipeArgBuffer = NULL |
If piping happens the output of the first command will be copied to this buffer. More... | |
int | pipeCounter |
This function tracks the current pipe in case of a piped command. More... | |
int | updateBufferCounter = 0 |
This variable tracks the next free elements index in the update functions buffer. More... | |
CommandCaller | defaultCommandCaller |
CommandCaller * | caller = NULL |
Static Private Attributes | |
static CommanderDatabase< systemVariableData_t > | systemVariables = CommanderDatabase<systemVariableData_t>() |
static debugLevel_t | debugLevel = Commander::DEBUG_OFF |
Flag to enable or disable debug messages. More... | |
static const char | empty_string = '\0' |
Friends | |
class | CommanderUT |
For unit testing. More... | |
Commander class.
This class can be used to create a command parser. Its job is to parse a command from a character array( string ) and find the matching function for that command from a database. This database consists an array of API_t-type elements, and these elements are store the relevant information for that specific command.
Definition at line 177 of file Commander-API.hpp.
typedef struct Commander::API_t Commander::API_t |
Structure for command data.
Every command will get a structure like this. This structure is used to store your commands in a balanced binary tree.
typedef CommanderDatabase<API_t>::dataRecord_t Commander::systemCommand_t |
Definition at line 210 of file Commander-API.hpp.
typedef CommanderDatabase<systemVariableData_t>::dataRecord_t Commander::systemVariable_t |
Definition at line 232 of file Commander-API.hpp.
Enumerator | |
---|---|
DEBUG_OFF | |
DEBUG_ERROR | |
DEBUG_DEBUG | |
DEBUG_VERBOSE |
Definition at line 184 of file Commander-API.hpp.
Enum for system variable type.
Enumerator | |
---|---|
VARIABLE_FLOAT | Used for float data. |
VARIABLE_INT | Used for int data. |
VARIABLE_STRING | Used for string data. |
Definition at line 213 of file Commander-API.hpp.
void Commander::attachDebugChannel | ( | Stream * | resp | ) |
Attach a debug channel to the object.
This function attaches a Serial channel for debug messages. It also enables the debug functionality. It can be useful during the init phase.
resp | Pointer to a Stream object. The output data will be printed to this Stream. |
Definition at line 434 of file Commander-API.cpp.
void Commander::attachTreeFunction | ( | Commander::systemCommand_t * | API_tree_p, |
uint32_t | API_tree_size_p | ||
) |
Attach API-tree to the object.
With this function you can attach the API-tree structure array to the object. This array contains the data for each command.
Definition at line 43 of file Commander-API.cpp.
|
static |
Attach system variables list to the object.
With this function you can attach the system variables list to the object. This array contains the data for each variable.
Definition at line 575 of file Commander-API.cpp.
bool Commander::commandExists | ( | const char * | cmd, |
Commander::systemCommand_t ** | cmd_ptr = NULL |
||
) |
void Commander::disableFormatting | ( | ) |
Disable formatting.
You can disable formatting with this function.
Definition at line 558 of file Commander-API.cpp.
void Commander::enableFormatting | ( | ) |
Enable formatting.
If you want to get fancy, you can enable coloring on the output text.
Definition at line 555 of file Commander-API.cpp.
bool Commander::enablePipeModuleFunc | ( | char * | buffer, |
int | bufferSize, | ||
commanderPipeChannel * | pipeChannel_p | ||
) |
Enable pipe functionality.
With this function you can enable piping. The syntax for piping is more less the same as with a Linux system. Sadly it is a bit resource hungry, because it requires a pipe buffer and a pipe Stream to make it work. For this reason the required amount buffer memory is at least three times as much as without the pipe module enabled.
buffer | Pointer to a char buffer. |
bufferSize | The size of the buffer. |
pipeChannel_p | This is a pointer to a commanderPipeChannel object. |
Definition at line 66 of file Commander-API.cpp.
bool Commander::execute | ( | const char * | cmd, |
Stream * | channel_p = NULL , |
||
CommandCaller * | caller_p = NULL |
||
) |
Execution function for Stream response.
This function tries to execute a command. It uses the Stream response channel, so the messages from the command handler will be passed to the selected Stream object.
cmd | This is a command string and it will be executed. |
caller_p | This can be a pointer to anything. The reason why it is implemented, is because the Shellminator project needed a simple way to communicate with the caller terminal from the called command function. It is optional, the default value for it is NULL. |
Definition at line 399 of file Commander-API.cpp.
|
private |
Command execution.
This function executes a command. Before calling this function, the response pointer and it's channel has to be configured correctly.
Definition at line 87 of file Commander-API.cpp.
|
static |
int Commander::generateHint | ( | const char * | fraction, |
char * | buffer_p, | ||
int | buffer_size_p | ||
) |
Definition at line 928 of file Commander-API.cpp.
const char * Commander::getHint | ( | int | index, |
bool | only_remaining_chars = false |
||
) |
Definition at line 914 of file Commander-API.cpp.
|
static |
Get the instance if a system variable by its name.
With this function you can get the address of a system variable structure element by the name if the system variable.
name | The name of the system variable. |
Definition at line 585 of file Commander-API.cpp.
|
private |
Search for a character in a string.
str | Pointer to a character array where the search will be. |
c | This character will be searched in the string. |
ignoreString | If this flag is set to true, the searched character within a string will be ignored. For example if the searched character is 'A' and the input str is 'dog "Apple" Arduino' 12 will be returned, which is the start of the Arduino word. |
Definition at line 495 of file Commander-API.cpp.
|
private |
Search for a character in a string.
str | Pointer to a character array where the search will be. |
c | This character will be searched in the string. |
number | Which occurrence of c is searched. |
ignoreString | If this flag is set to true, the searched character within a string will be ignored. For example if the searched character is 'A' and the input str is 'dog "Apple" Arduino' 12 will be returned, which is the start of the Arduino word. |
Definition at line 527 of file Commander-API.cpp.
bool Commander::init | ( | ) |
Initializer.
This function initializes the object and its internal parts. Firstly it makes the API-tree alphabetically ordered, then creates a balanced binary tree from it. It is necessary to speed up the search process.
Definition at line 62 of file Commander-API.cpp.
|
static |
Detect if character is in the string.
You can detect if the index of a character is in a 'string' in a character array. For example if the source looks like this:
And the index is 3 it will return true, because the h character is within two " characters. If the index us 11 it will return false, because the b character is not in a string.
source | Pointer to a source string. It has to be terminated! |
index | The index of the investigated character. |
Definition at line 666 of file Commander-API.cpp.
|
static |
Print 'Argument error!' to a specified Serial channel.
This is a relatively common message, when arguments are used in a command. To avoid duplication and save som precious memory, this function is dedicated to print this message.
channel_p | Pointer to the Stream object where the message will be printed. |
Definition at line 562 of file Commander-API.cpp.
|
private |
Definition at line 829 of file Commander-API.cpp.
void Commander::printHelp | ( | Stream * | out, |
bool | description, | ||
bool | style = false |
||
) |
Print the help text to a specified Stream.
It prints all the available commands in alphabetical order. If the description argument is set to true, it also prints the description data for all commands.
out | Pointer to a Stream object. The output data will be printed to this Stream. |
description | If this flag is set to true, the description data will be printed as well. |
style | If this flag is set to true, the output text will be colorized. It will use VT100 escape sequences, and because of it, the output data only will be pretty on VT100 compatible frontends. |
Definition at line 442 of file Commander-API.cpp.
|
static |
Print the value of a system variable.
This function will print the value of a system variable to a specified Stream. The variable is accessed by its name.
channel_p | Pointer to a Stream object. The data will be printed to this Stream. |
name | The name of the system variable. |
decimalPlaces | Optionally, you can specify how many how many decimal places will be needed in case of float data. |
Definition at line 589 of file Commander-API.cpp.
|
static |
Print all the available system variables.
This function will print all the available system variables to a specified Stream. The variable name and its value will be printed.
channel_p | Pointer to a Stream object. The data will be printed to this Stream. |
Definition at line 622 of file Commander-API.cpp.
|
static |
Definition at line 438 of file Commander-API.cpp.
void Commander::update | ( | char * | buffer, |
int | bufferSize, | ||
Stream * | channel_p | ||
) |
Create a very very simple command lime.
With this function you can create a very basic command line. A buffer is required to store the incoming characters somewhere. This function has to be called periodically to make it work.
buffer | Pointer to a buffer. |
bufferSize | The size of the buffer. I recommend at least 20 character long buffer. |
channel_p | Pointer to a Stream object like Serial. |
Definition at line 704 of file Commander-API.cpp.
|
friend |
For unit testing.
Definition at line 468 of file Commander-API.hpp.
|
private |
Definition at line 471 of file Commander-API.hpp.
|
private |
Pointer to response class.
By default it points to the default debug response handler.
Definition at line 418 of file Commander-API.hpp.
|
staticprivate |
Flag to enable or disable debug messages.
Definition at line 414 of file Commander-API.hpp.
|
private |
Definition at line 470 of file Commander-API.hpp.
|
staticprivate |
Definition at line 465 of file Commander-API.hpp.
|
private |
Definition at line 411 of file Commander-API.hpp.
const char* Commander::lastCommandHint = NULL |
Definition at line 383 of file Commander-API.hpp.
int Commander::lastCommandHintOffset = 0 |
Definition at line 384 of file Commander-API.hpp.
AutoComplete* Commander::lastHint = NULL |
Definition at line 382 of file Commander-API.hpp.
|
private |
This variable will store the address of the input data.
It is required to track back the original command in case of an error.
Definition at line 409 of file Commander-API.hpp.
|
private |
If piping happens the output of the first command will be copied to this buffer.
This way it can be passed to the second command and so on.
Definition at line 456 of file Commander-API.hpp.
|
private |
Channel for the internal piping.
Definition at line 451 of file Commander-API.hpp.
|
private |
This function tracks the current pipe in case of a piped command.
It can be used to backtrack a broken pipe position.
Definition at line 460 of file Commander-API.hpp.
|
private |
Starting address of the API-tree.
CommanderDatabase<API_t>::dataRecord_t* API_tree = NULL; Number of elements in the API-tree. uint32_t API_tree_size = 0;
Definition at line 395 of file Commander-API.hpp.
|
staticprivate |
Definition at line 398 of file Commander-API.hpp.
|
private |
Internal command buffer.
The command data has to be copied to this buffer. It is necessary because the execute function has to modify the content of the command.
Definition at line 404 of file Commander-API.hpp.
|
private |
This variable tracks the next free elements index in the update functions buffer.
Definition at line 463 of file Commander-API.hpp.
|
static |
Library version string.
Definition at line 182 of file Commander-API.hpp.