Przeglądaj źródła

determine the length of the input command abd compare the length with the length of the known commands

prozessorkern 5 lat temu
rodzic
commit
21212d05b8
1 zmienionych plików z 17 dodań i 5 usunięć
  1. 17 5
      src/shellmatta.c

+ 17 - 5
src/shellmatta.c

@@ -193,6 +193,7 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
 {
     shellmatta_cmd_t        *cmd;
     uint8_t                 cmdExecuted = 0u;
+    uint32_t                cmdLen;
     shellmatta_retCode_t    ret = SHELLMATTA_OK;
     shellmatta_instance_t   *inst = (shellmatta_instance_t*)handle;
 
@@ -225,16 +226,27 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
                 history_storeCmd(inst);
                 history_reset(inst);
 
+                /** -# determine the cmd len (chars until first space or \0 is found */
+                cmdLen = 0u;
+                while(      (cmdLen <   inst->inputCount)
+                        &&  (' '    !=  inst->buffer[cmdLen])
+                        &&  ('\0'   !=  inst->buffer[cmdLen]))
+                {
+                    cmdLen ++;
+                }
+
                 /** -# search for a matching command */
                 while (NULL != cmd)
                 {
-                    /* only compare the length of the command -1 (to avoid comparing the \0 */
-                    if (    (0 == strncmp(  inst->buffer,
+                    /** -# compare command string and length */
+                    if (    ((0 == strncmp( inst->buffer,
                                             cmd->cmd,
-                                            strlen(cmd->cmd)))
-                        ||  (0 == strncmp(  inst->buffer,
+                                            cmdLen))
+                            && (cmdLen == strlen(cmd->cmd)))
+                        ||  ((0 == strncmp( inst->buffer,
                                             cmd->cmdAlias,
-                                            strlen(cmd->cmdAlias))))
+                                            cmdLen))
+                            && (cmdLen == strlen(cmd->cmdAlias))))
                     {
                         inst->write("\r\n", 2u);