Pārlūkot izejas kodu

bugfix - now stdin returns 0 if the heredoc body is empty

prozessorkern 4 gadi atpakaļ
vecāks
revīzija
c2e4324236
2 mainītis faili ar 42 papildinājumiem un 3 dzēšanām
  1. 10 3
      src/shellmatta.c
  2. 32 0
      test/integrationtest/test_integration.cpp

+ 10 - 3
src/shellmatta.c

@@ -486,9 +486,16 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
                             inst->stdinIdx ++;
                         }
                         /** -# calculate length and terminate stdin string */
-                        inst->stdinLength = inst->lastNewlineIdx - inst->stdinIdx;
-                        inst->buffer[inst->lastNewlineIdx] = '\0';
-                        
+                        if(inst->stdinIdx < inst->lastNewlineIdx)
+                        {
+                            inst->stdinLength = inst->lastNewlineIdx - inst->stdinIdx;
+                            inst->buffer[inst->lastNewlineIdx] = '\0';
+                        }
+                        else
+                        {
+                            inst->stdinLength = 0u;
+                        }
+
                         /** -# calculate length and terminate argument string */
                         inst->inputCount = inst->hereStartIdx;
                         inst->buffer[inst->hereStartIdx] = '\0';

+ 32 - 0
test/integrationtest/test_integration.cpp

@@ -175,6 +175,38 @@ TEST_CASE( "shellmatta heredoc test" ) {
     REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
 }
 
+TEST_CASE( "shellmatta heredoc test empty" ) {
+
+    shellmatta_instance_t inst;
+    shellmatta_handle_t handle;
+    char buffer[1024];
+    char historyBuffer[1024];
+    char *dummyData =   (char*)"do this ";
+
+    shellmatta_doInit(  &inst,
+                        &handle,
+                        buffer,
+                        sizeof(buffer),
+                        historyBuffer,
+                        sizeof(historyBuffer),
+                        "shellmatta->",
+                        NULL,
+                        writeFct);
+    shellmatta_addCmd(handle, &doSomethingCmd);
+
+    doSomethingArguments = NULL;
+    doSomethingLength = 0u;
+
+    shellmatta_processData(handle, (char*)"do this << EOF\r\n"
+                                    "EOF\r\n"
+                                , 21);
+
+    CHECK( doSomethingStdinLength == 0u);
+    CHECK( NULL == doSomethingStdin );
+    CHECK( doSomethingLength == 8u);
+    REQUIRE( strcmp(dummyData, doSomethingArguments) == 0);
+}
+
 TEST_CASE( "shellmatta remove function" ) {
 
     shellmatta_instance_t inst;