Jelajahi Sumber

improved style and fixed some cppcheck findings

prozessorkern 4 tahun lalu
induk
melakukan
eb524436ce

+ 14 - 19
src/shellmatta.c

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

+ 3 - 3
src/shellmatta_autocomplete.c

@@ -34,16 +34,16 @@ void autocomplete_run(shellmatta_instance_t *inst)
     shellmatta_cmd_t   *cmd         = inst->cmdList;
     char               *tempCmd     = NULL;
     uint32_t            minLen      = 0u;
-    uint32_t            sizeDiff    = 0u;
     bool                exactMatch  = true;
     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 */
     inst->tabCounter++;
 
     /** -# on douple tab show all matching commands */
-    if (1u < inst->tabCounter)
+    if (inst->tabCounter > 1u)
     {
         inst->tabCounter        = 0u;
 

+ 3 - 3
src/shellmatta_history.c

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

+ 2 - 2
src/shellmatta_opt.c

@@ -51,7 +51,7 @@ static shellmatta_retCode_t findNextHunk(shellmatta_instance_t *inst)
         && (((' ' != inst->buffer[newOffset]) && ('\0' != inst->buffer[newOffset])) || '\0' != 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];
             exeptionOffset ++;
@@ -211,7 +211,7 @@ static shellmatta_retCode_t parseLongOpt(   shellmatta_instance_t       *inst,
         }
     }
     /** -# 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 */
         for(i = 0u; ('\0' != longOptions[i].paramShort) && ('\0' == *option); i ++)

+ 9 - 9
src/shellmatta_utils.c

@@ -54,14 +54,14 @@ uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base)
     int8_t digitValue;
 
     /** -# check the base for plausibility */
-    if((2 <= base) && (16 >= base))
+    if((base >= 2) && (base <= 16))
     {
         /** -# check for sign */
-        if(0 > value)
+        if(value < 0)
         {
-            value = value * (-1);
-            buffer[0u] = '-';
-            bufferIdx += 1u;
+            value       = value * (-1);
+            buffer[0u]  = '-';
+            bufferIdx   += 1u;
         }
 
         /** -# 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                      maxCmdAliasLen  = 0u;
     size_t                      maxCmdHelpLen   = 0u;
-    size_t                      cmdLen       = 0u;
-    size_t                      cmdAliasLen  = 0u;
-    size_t                      cmdHelpLen   = 0u;
-    uint32_t                    tabCnt       = 0u;
+    size_t                      cmdLen;
+    size_t                      cmdAliasLen;
+    size_t                      cmdHelpLen;
+    uint32_t                    tabCnt;
     static const char           tabBuffer[] = { ' ', ' ', ' ', ' ',
                                                 ' ', ' ', ' ', ' ',
                                                 ' ', ' ', ' ', ' ',