Parcourir la source

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

prozessorkern il y a 4 ans
Parent
commit
d2617a4f86

+ 21 - 0
.vscode/launch.json

@@ -24,6 +24,27 @@
             ],
             "preLaunchTask": "make test",
             "miDebuggerPath": "/usr/bin/gdb"
+        },
+        {
+            "name": "debug integrationtest",
+            "type": "cppdbg",
+            "request": "launch",
+            "program": "${workspaceFolder}/output/test/integrationtest/integrationtest",
+            "args": [],
+            "stopAtEntry": false,
+            "cwd": "${workspaceFolder}",
+            "environment": [],
+            "externalConsole": false,
+            "MIMode": "gdb",
+            "setupCommands": [
+                {
+                    "description": "Enable pretty-printing for gdb",
+                    "text": "-enable-pretty-printing",
+                    "ignoreFailures": true
+                }
+            ],
+            "preLaunchTask": "make integrationtest",
+            "miDebuggerPath": "/usr/bin/gdb"
         }
     ]
 }

+ 16 - 0
.vscode/tasks.json

@@ -28,6 +28,22 @@
             "label": "make unittest",
             "type": "shell",
             "command": "make unittest",
+            "problemMatcher": [
+                "$gcc"
+            ]
+        },
+        {
+            "label": "make integrationtest",
+            "type": "shell",
+            "command": "make integrationtest",
+            "problemMatcher": [
+                "$gcc"
+            ]
+        },
+        {
+            "label": "make test",
+            "type": "shell",
+            "command": "make test",
             "problemMatcher": [
                 "$gcc"
             ],

+ 11 - 7
api/shellmatta.h

@@ -125,19 +125,23 @@ shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t   *inst,
                                         const char              *prompt,
                                         const shellmatta_cmd_t  *cmdList,
                                         shellmatta_write_t      writeFct);
-shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
-                                        shellmatta_cmd_t    *cmd);
+
+shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t     handle,
+                                        shellmatta_cmd_t        *cmd);
+
 shellmatta_retCode_t shellmatta_removeCmd(  shellmatta_handle_t handle,
                                             shellmatta_cmd_t    *cmd);
+
 shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
                                             char                *data,
                                             uint32_t            size);
-shellmatta_retCode_t shellmatta_write(  shellmatta_handle_t handle,
-                                        char                *data,
-                                        uint32_t            length);
+
+shellmatta_retCode_t shellmatta_write(  shellmatta_handle_t     handle,
+                                        char                    *data,
+                                        uint32_t                length);
 #ifndef SHELLMATTA_STRIP_PRINTF
-shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
-                                        const char          *fmt,
+shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t     handle,
+                                        const char              *fmt,
                                         ...);
 #endif
 

+ 12 - 0
example/main.c

@@ -85,6 +85,17 @@ static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *argumen
 }
 shellmatta_cmd_t quitCommand = {"quit", "q", "Function quits the shell", "", quit, NULL};
 
+static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
+{
+    (void)arguments;
+    (void)length;
+
+    shellmatta_printf(handle, "empty function called\r\n");
+
+    return SHELLMATTA_OK;
+}
+shellmatta_cmd_t emptyCommand = {"empty", NULL, NULL, NULL, empty, NULL};
+
 
 shellmatta_retCode_t writeFct(const char* data, uint32_t length)
 {
@@ -128,6 +139,7 @@ int main(int argc, char **argv)
     shellmatta_addCmd(handle, &doSomeCmd);
     shellmatta_addCmd(handle, &quitCommand);
     shellmatta_addCmd(handle, &removeCommand);
+    shellmatta_addCmd(handle, &emptyCommand);
 
     while(exitRequest == false)
     {

+ 36 - 12
src/shellmatta.c

@@ -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);
 

+ 4 - 2
src/shellmatta_autocomplete.c

@@ -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 */

+ 22 - 7
src/shellmatta_utils.c

@@ -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;

+ 27 - 19
test/integrationtest/test_integration.cpp

@@ -33,6 +33,12 @@ static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *
 }
 shellmatta_cmd_t doSomethingCmd = {(char*)"doSomething", (char*)"do", (char*)"Function does something", (char*)"use me, please", doSomething, NULL};
 
+static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
+{
+    shellmatta_printf(handle, "empty - %s - length: %u", arguments, length);
+    return SHELLMATTA_OK;
+}
+shellmatta_cmd_t emptyCmd = {(char*)"empty", NULL, NULL, NULL, empty, NULL};
 
 TEST_CASE( "shellmatta empty function" ) {
 
@@ -69,9 +75,9 @@ TEST_CASE( "shellmatta help function" ) {
     shellmatta_handle_t handle;
     char buffer[1024];
     char historyBuffer[1024];
-    char *dummyData =   (char*)"h\r\n"
+    char *dummyData =   (char*)"?\r\n"
                         "doSomething  do  Function does something  use me, please\r\n"
-                        "help         h   Print this help text     help\r\n"
+                        "help         ?   Print this help text     help\r\n"
                         "\r\nshellmatta->";
 
     shellmatta_doInit(  &inst,
@@ -89,24 +95,26 @@ TEST_CASE( "shellmatta help function" ) {
     memset(write_data, 0, sizeof(write_data));
     write_length = 0u;
 
-    shellmatta_processData(handle, (char*)"h\r", 2);
+    shellmatta_processData(handle, (char*)"?\r", 2);
 
     CHECK( write_length == 123u);
     CHECK( strcmp(dummyData, write_data) == 0);
 
+    shellmatta_addCmd(handle, &emptyCmd);
 
     write_callCnt = 0u;
     memset(write_data, 0, sizeof(write_data));
     write_length = 0u;
 
-    dummyData =     (char*)"h 564 321 56 465 46\r\n"
+    dummyData =     (char*)"? 564 321 56 465 46\r\n"
                     "doSomething  do  Function does something  use me, please\r\n"
-                    "help         h   Print this help text     help\r\n"
+                    "empty                                     \r\n"
+                    "help         ?   Print this help text     help\r\n"
                     "\r\nshellmatta->";
 
-    shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
+    shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
 
-    CHECK( write_length == 141u);
+    CHECK( write_length == strlen(dummyData));
     CHECK( strcmp(dummyData, write_data) == 0);
 
 
@@ -114,11 +122,11 @@ TEST_CASE( "shellmatta help function" ) {
     memset(write_data, 0, sizeof(write_data));
     write_length = 0u;
 
-    dummyData = (char*)"hr\r\n"
-                "Command: hr not found"
+    dummyData = (char*)"?r\r\n"
+                "Command: ?r not found"
                 "\r\nshellmatta->";
 
-    shellmatta_processData(handle, (char*)"hr\r", 3);
+    shellmatta_processData(handle, (char*)"?r\r", 3);
 
     CHECK( write_length == 39u);
     REQUIRE( strcmp(dummyData, write_data) == 0);
@@ -169,9 +177,9 @@ TEST_CASE( "shellmatta remove function" ) {
     shellmatta_handle_t handle;
     char buffer[1024];
     char historyBuffer[1024];
-    char *dummyData =   (char*)"h\r\n"
+    char *dummyData =   (char*)"?\r\n"
                         "doSomething  do  Function does something  use me, please\r\n"
-                        "help         h   Print this help text     help\r\n"
+                        "help         ?   Print this help text     help\r\n"
                         "\r\nshellmatta->";
 
     shellmatta_doInit(  &inst,
@@ -189,7 +197,7 @@ TEST_CASE( "shellmatta remove function" ) {
     memset(write_data, 0, sizeof(write_data));
     write_length = 0u;
 
-    shellmatta_processData(handle, (char*)"h\r", 2);
+    shellmatta_processData(handle, (char*)"?\r", 2);
 
     CHECK( write_length == 123u);
     CHECK( strcmp(dummyData, write_data) == 0);
@@ -199,12 +207,12 @@ TEST_CASE( "shellmatta remove function" ) {
     memset(write_data, 0, sizeof(write_data));
     write_length = 0u;
 
-    dummyData =     (char*)"h 564 321 56 465 46\r\n"
+    dummyData =     (char*)"? 564 321 56 465 46\r\n"
                     "doSomething  do  Function does something  use me, please\r\n"
-                    "help         h   Print this help text     help\r\n"
+                    "help         ?   Print this help text     help\r\n"
                     "\r\nshellmatta->";
 
-    shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
+    shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
 
     CHECK( write_length == 141u);
     CHECK( strcmp(dummyData, write_data) == 0);
@@ -214,10 +222,10 @@ TEST_CASE( "shellmatta remove function" ) {
     write_length = 0u;
 
     shellmatta_removeCmd(handle, &doSomethingCmd);
-    shellmatta_processData(handle, (char*)"h 564 321 56 465 46\r", 20);
+    shellmatta_processData(handle, (char*)"? 564 321 56 465 46\r", 20);
 
-    dummyData =     (char*)"h 564 321 56 465 46\r\n"
-                    "help  h  Print this help text  help\r\n"
+    dummyData =     (char*)"? 564 321 56 465 46\r\n"
+                    "help  ?  Print this help text  help\r\n"
                     "\r\nshellmatta->";
 
     printf("sdfsd sdf sdf sdf sdf sd fds\n%s", write_data);