close #31 - made all command parameter except the command name optional + added and fixed tests

This commit is contained in:
prozessorkern
2020-03-01 18:45:30 +01:00
parent c807372bce
commit d2617a4f86
8 changed files with 149 additions and 47 deletions

View File

@@ -118,6 +118,9 @@ shellmatta_retCode_t shellmatta_doInit(
* @return errorcode #SHELLMATTA_OK
* #SHELLMATTA_USE_FAULT (param err)
* SHELLMATTA_DUPLICATE
*
* The cmd name is mandatory, the rest of the command parameters (alias, helpText and usageText) are optional
* and can be set to NULL if not used.
*/
shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
{
@@ -132,7 +135,9 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
/** -# check parameters for plausibility */
if( (NULL != inst)
&& (SHELLMATTA_MAGIC == inst->magic)
&& (false == inst->cmdListIsConst))
&& (false == inst->cmdListIsConst)
&& (NULL != cmd)
&& (NULL != cmd->cmd))
{
tempCmd = inst->cmdList;
prevCmd = &inst->cmdList;
@@ -147,8 +152,16 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
{
while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
{
cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
if(NULL != cmd->cmdAlias)
{
aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
}
else
{
aliasDiff = 1;
}
/** -# check for a duplicate command */
if((0u == cmdDiff) || (0u == aliasDiff))
{
@@ -200,7 +213,9 @@ shellmatta_retCode_t shellmatta_removeCmd(shellmatta_handle_t handle, shellmatta
/** -# check parameters for plausibility */
if( (NULL != inst)
&& (SHELLMATTA_MAGIC == inst->magic)
&& (false == inst->cmdListIsConst))
&& (false == inst->cmdListIsConst)
&& (NULL != cmd)
&& (NULL != cmd->cmd))
{
tempCmd = inst->cmdList;
prevCmd = NULL;
@@ -406,14 +421,23 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
while (NULL != cmd)
{
/** -# compare command string and length */
if ( ((0 == strncmp( argumentString,
cmd->cmd,
cmdLen))
&& (cmdLen == strlen(cmd->cmd)))
|| ((0 == strncmp( argumentString,
cmd->cmdAlias,
cmdLen))
&& (cmdLen == strlen(cmd->cmdAlias))))
if ((0 == strncmp( argumentString,
cmd->cmd,
cmdLen))
&& (cmdLen == strlen(cmd->cmd)))
{
inst->write("\r\n", 2u);
cmdExecuted = 1u;
cmd->cmdFct(inst, argumentString, argumentLength);
cmd = NULL;
}
/** -# compare command alias if any and length */
else if((NULL != cmd->cmdAlias)
&& ((0 == strncmp( argumentString,
cmd->cmdAlias,
cmdLen))
&& (cmdLen == strlen(cmd->cmdAlias))))
{
inst->write("\r\n", 2u);

View File

@@ -65,7 +65,8 @@ void autocomplete_run(shellmatta_instance_t *inst)
printedLen += 4u;
}
/** -# check if command alias matches the input */
if( (strlen(cmd->cmdAlias) >= inst->cursor)
if( (NULL != cmd->cmdAlias)
&& (strlen(cmd->cmdAlias) >= inst->cursor)
&& (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
{
/** -# add newline on first find */
@@ -123,7 +124,8 @@ void autocomplete_run(shellmatta_instance_t *inst)
}
/** -# check if command Alias matches the input */
if( (strlen(cmd->cmdAlias) >= inst->cursor)
if( (NULL != cmd->cmdAlias)
&& (strlen(cmd->cmdAlias) >= inst->cursor)
&& (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
{
/** -# store first match */

View File

@@ -290,8 +290,14 @@ static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *a
while(NULL != cmd)
{
maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText));
if(NULL != cmd->cmdAlias)
{
maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
}
if(NULL != cmd->helpText)
{
maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText));
}
cmd = cmd->next;
}
@@ -301,22 +307,31 @@ static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *a
{
/** -# determine the length of each field to add padding */
cmdLen = strlen(cmd->cmd);
cmdAliasLen = strlen(cmd->cmdAlias);
cmdHelpLen = strlen(cmd->helpText);
cmdAliasLen = (NULL != cmd->cmdAlias) ? strlen(cmd->cmdAlias) : 0u;
cmdHelpLen = (NULL != cmd->helpText) ? strlen(cmd->helpText) : 0u;
inst->write(cmd->cmd, strlen(cmd->cmd));
tabCnt = (maxCmdLen - cmdLen) + 2u;
SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias));
if(NULL != cmd->cmdAlias)
{
inst->write(cmd->cmdAlias, cmdAliasLen);
}
tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
inst->write(cmd->helpText, strlen(cmd->helpText));
if(NULL != cmd->helpText)
{
inst->write(cmd->helpText, cmdHelpLen);
}
tabCnt = (maxCmdHelpLen - cmdHelpLen) + 2u;
SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
inst->write(cmd->usageText, strlen(cmd->usageText));
if(NULL != cmd->usageText)
{
inst->write(cmd->usageText, strlen(cmd->usageText));
}
inst->write("\r\n", 2u);
cmd = cmd->next;