improved style and fixed some cppcheck findings

This commit is contained in:
prozessorkern 2021-01-24 00:10:43 +01:00
parent c7f238c005
commit eb524436ce
5 changed files with 31 additions and 36 deletions

View File

@ -193,8 +193,8 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
shellmatta_cmd_t **prevCmd; shellmatta_cmd_t **prevCmd;
bool cmdPlaced = false; bool cmdPlaced = false;
shellmatta_retCode_t ret = SHELLMATTA_OK; shellmatta_retCode_t ret = SHELLMATTA_OK;
int cmdDiff = 0; int cmdDiff;
int aliasDiff = 0; int aliasDiff;
/** -# check parameters for plausibility */ /** -# check parameters for plausibility */
if( (NULL != inst) if( (NULL != inst)
@ -203,8 +203,8 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
&& (NULL != cmd) && (NULL != cmd)
&& (NULL != cmd->cmd)) && (NULL != cmd->cmd))
{ {
tempCmd = inst->cmdList; tempCmd = inst->cmdList;
prevCmd = &inst->cmdList; prevCmd = &inst->cmdList;
/** -# register first command as list entry */ /** -# register first command as list entry */
if (NULL == tempCmd) if (NULL == tempCmd)
{ {
@ -216,7 +216,7 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
{ {
while ((false == cmdPlaced) && (SHELLMATTA_OK == ret)) while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
{ {
cmdDiff = strcmp(tempCmd->cmd, cmd->cmd); cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
if( (NULL != cmd->cmdAlias) if( (NULL != cmd->cmdAlias)
&& (NULL != tempCmd->cmdAlias)) && (NULL != tempCmd->cmdAlias))
{ {
@ -232,7 +232,7 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
{ {
ret = SHELLMATTA_DUPLICATE; ret = SHELLMATTA_DUPLICATE;
} }
else if(0 < cmdDiff) else if(cmdDiff > 0)
{ {
cmd->next = tempCmd; cmd->next = tempCmd;
*prevCmd = cmd; *prevCmd = cmd;
@ -289,9 +289,8 @@ shellmatta_retCode_t shellmatta_removeCmd(shellmatta_handle_t handle, shellmatta
while(NULL != tempCmd) while(NULL != tempCmd)
{ {
/** -# compare command strings to find the command to delete */ /** -# compare command strings to find the command to delete */
if (0 == strcmp( tempCmd->cmd, if (0 == strcmp(tempCmd->cmd, cmd->cmd)
cmd->cmd) && (strlen(tempCmd->cmd) == strlen(cmd->cmd)))
&& (strlen(tempCmd->cmd) == strlen(cmd->cmd)))
{ {
/** -# first command removed */ /** -# first command removed */
if(NULL == prevCmd) if(NULL == prevCmd)
@ -344,7 +343,7 @@ shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
/** -# check parameters for plausibility */ /** -# check parameters for plausibility */
if( (NULL != inst) if( (NULL != inst)
&& (SHELLMATTA_MAGIC == inst->magic) && (SHELLMATTA_MAGIC == inst->magic)
&& ((mode == SHELLMATTA_MODE_INSERT) || (mode == SHELLMATTA_MODE_OVERWRITE))) && ((SHELLMATTA_MODE_INSERT == mode) || (SHELLMATTA_MODE_OVERWRITE == mode)))
{ {
inst->mode = mode; inst->mode = mode;
inst->echoEnabled = echoEnabled; inst->echoEnabled = echoEnabled;
@ -601,15 +600,11 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
while (NULL != cmd) while (NULL != cmd)
{ {
/** -# compare command and alias string and length */ /** -# compare command and alias string and length */
if ( ((cmdLen == strlen(cmd->cmd)) if ( ((cmdLen == strlen(cmd->cmd))
&& (0 == strncmp( inst->buffer, && (0 == strncmp(inst->buffer, cmd->cmd, cmdLen)))
cmd->cmd,
cmdLen)))
|| ((NULL != cmd->cmdAlias) || ((NULL != cmd->cmdAlias)
&& (cmdLen == strlen(cmd->cmdAlias)) && (cmdLen == strlen(cmd->cmdAlias))
&& (0 == strncmp( inst->buffer, && (0 == strncmp(inst->buffer, cmd->cmdAlias, cmdLen))))
cmd->cmdAlias,
cmdLen))))
{ {
utils_writeEcho(inst, "\r\n", 2u); utils_writeEcho(inst, "\r\n", 2u);
shellmatta_opt_init(inst, cmdLen + 1u); shellmatta_opt_init(inst, cmdLen + 1u);
@ -643,7 +638,7 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
} }
} }
if ((cmdExecuted == 0u) && (inst->inputCount > 0)) if ((0u == cmdExecuted) && (inst->inputCount > 0))
{ {
inst->write("\r\nCommand: ", 11u); inst->write("\r\nCommand: ", 11u);
inst->write(inst->buffer, inst->inputCount); inst->write(inst->buffer, inst->inputCount);

View File

@ -34,16 +34,16 @@ void autocomplete_run(shellmatta_instance_t *inst)
shellmatta_cmd_t *cmd = inst->cmdList; shellmatta_cmd_t *cmd = inst->cmdList;
char *tempCmd = NULL; char *tempCmd = NULL;
uint32_t minLen = 0u; uint32_t minLen = 0u;
uint32_t sizeDiff = 0u;
bool exactMatch = true; bool exactMatch = true;
uint32_t printedLen = 0u; uint32_t printedLen = 0u;
uint32_t tempCursor = 0u; uint32_t sizeDiff;
uint32_t tempCursor;
/** -# increase the tab counter to print all matching commands next time */ /** -# increase the tab counter to print all matching commands next time */
inst->tabCounter++; inst->tabCounter++;
/** -# on douple tab show all matching commands */ /** -# on douple tab show all matching commands */
if (1u < inst->tabCounter) if (inst->tabCounter > 1u)
{ {
inst->tabCounter = 0u; inst->tabCounter = 0u;

View File

@ -141,8 +141,8 @@ static bool compareLastCommand(shellmatta_instance_t *inst)
*/ */
bool history_navigate(shellmatta_instance_t *inst, int32_t cnt) bool history_navigate(shellmatta_instance_t *inst, int32_t cnt)
{ {
bool ret = true; bool ret = true;
uint32_t tempReadIdx = 0u; uint32_t tempReadIdx;
while((cnt > 0) && (true == ret)) while((cnt > 0) && (true == ret))
{ {
@ -259,7 +259,7 @@ void history_restoreCmd(shellmatta_instance_t *inst)
utils_clearInput(inst); utils_clearInput(inst);
anythingToRestore = true; anythingToRestore = true;
} }
while((ret == true) && (byte != 0u)) while((true == ret) && (0u != byte))
{ {
inst->buffer[inst->inputCount] = byte; inst->buffer[inst->inputCount] = byte;
inst->inputCount ++; inst->inputCount ++;

View File

@ -51,7 +51,7 @@ static shellmatta_retCode_t findNextHunk(shellmatta_instance_t *inst)
&& (((' ' != inst->buffer[newOffset]) && ('\0' != inst->buffer[newOffset])) || '\0' != quotation)) && (((' ' != inst->buffer[newOffset]) && ('\0' != inst->buffer[newOffset])) || '\0' != quotation))
{ {
/** -# check for new quotation */ /** -# check for new quotation */
if((('\'' == inst->buffer[newOffset]) || ('"' == inst->buffer[newOffset])) && (quotation == '\0')) if((('\'' == inst->buffer[newOffset]) || ('"' == inst->buffer[newOffset])) && ('\0' == quotation))
{ {
quotation = inst->buffer[newOffset]; quotation = inst->buffer[newOffset];
exeptionOffset ++; exeptionOffset ++;
@ -211,7 +211,7 @@ static shellmatta_retCode_t parseLongOpt( shellmatta_instance_t *inst,
} }
} }
/** -# check for correct syntax for long options */ /** -# check for correct syntax for long options */
else if((3u <= inst->optionParser.len) && ('-' == buffer[0u]) && ('-' == buffer[1u])) else if((inst->optionParser.len >= 3u) && ('-' == buffer[0u]) && ('-' == buffer[1u]))
{ {
/** -# search for long option in option list */ /** -# search for long option in option list */
for(i = 0u; ('\0' != longOptions[i].paramShort) && ('\0' == *option); i ++) for(i = 0u; ('\0' != longOptions[i].paramShort) && ('\0' == *option); i ++)

View File

@ -54,14 +54,14 @@ uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base)
int8_t digitValue; int8_t digitValue;
/** -# check the base for plausibility */ /** -# check the base for plausibility */
if((2 <= base) && (16 >= base)) if((base >= 2) && (base <= 16))
{ {
/** -# check for sign */ /** -# check for sign */
if(0 > value) if(value < 0)
{ {
value = value * (-1); value = value * (-1);
buffer[0u] = '-'; buffer[0u] = '-';
bufferIdx += 1u; bufferIdx += 1u;
} }
/** -# loop through all digits in reverse order */ /** -# loop through all digits in reverse order */
@ -283,10 +283,10 @@ static shellmatta_retCode_t helpCmdFct(const shellmatta_handle_t handle, const c
size_t maxCmdLen = 0u; size_t maxCmdLen = 0u;
size_t maxCmdAliasLen = 0u; size_t maxCmdAliasLen = 0u;
size_t maxCmdHelpLen = 0u; size_t maxCmdHelpLen = 0u;
size_t cmdLen = 0u; size_t cmdLen;
size_t cmdAliasLen = 0u; size_t cmdAliasLen;
size_t cmdHelpLen = 0u; size_t cmdHelpLen;
uint32_t tabCnt = 0u; uint32_t tabCnt;
static const char tabBuffer[] = { ' ', ' ', ' ', ' ', static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',