diff --git a/binding.c b/binding.c index e74f211..4f78c38 100644 --- a/binding.c +++ b/binding.c @@ -1,6 +1,42 @@ #include "binding.h" #include "inet.h" /* for sockets */ +char *sanitizeInput(char *input) +{ + unsigned int size = 16; + unsigned int cur = 0; + char *temp = malloc(sizeof(char) * size); + int pos = 0; + int c; + void *mem = NULL; + while (c = input[pos]) { + /* Reserve space to escape colon in input */ + if (c == ':') { + cur++; + } + if (cur > size - 1) { + /* Increase buffer size */ + size *= 2; + mem = realloc(temp, sizeof(char) * size); + if (!mem) { + perror("realloc"); + free(temp); + return NULL; + } + temp = mem; + } + /* Escape colon in input */ + if (c == ':') { + temp[cur - 1] = ':'; + } + temp[cur] = (char)c; + cur++; + pos++; + } + temp[cur] = '\0'; + return temp; +} + ValueObject *getArg(struct scopeobject *scope, char *name) { IdentifierNode *id = createIdentifierNode(IT_DIRECT, (void *)copyString(name), NULL, NULL, 0); @@ -106,7 +142,10 @@ ReturnObject *ireceiveWrapper(struct scopeobject *scope) int len = inet_receive(remote, local, data, amount, -1); data[len] = '\0'; - ValueObject *ret = createStringValueObject(data); + char *sanitized = sanitizeInput(data); + free(data); + + ValueObject *ret = createStringValueObject(sanitized); return createReturnObject(RT_RETURN, ret); } @@ -134,7 +173,10 @@ ReturnObject *freadWrapper(struct scopeobject *scope) int len = fread(buf, 1, length, file); buf[len] = '\0'; - ValueObject *ret = createStringValueObject(buf); + char *sanitized = sanitizeInput(buf); + free(buf); + + ValueObject *ret = createStringValueObject(sanitized); return createReturnObject(RT_RETURN, ret); } diff --git a/cmake/AddLolTest.cmake b/cmake/AddLolTest.cmake index 6152c2d..a14c97e 100644 --- a/cmake/AddLolTest.cmake +++ b/cmake/AddLolTest.cmake @@ -1,7 +1,7 @@ INCLUDE(ParseArguments) FUNCTION(ADD_LOL_TEST TEST_NAME) - PARSE_ARGUMENTS(ARG "LOLCODE;OUTPUT;INPUT" "ERROR" ${ARGN}) + PARSE_ARGUMENTS(ARG "LOLCODE;OUTPUT;INPUT" "ERROR;CWD" ${ARGN}) IF(NOT ARG_LOLCODE) SET(ARG_LOLCODE ${CMAKE_CURRENT_SOURCE_DIR}/test.lol) @@ -17,6 +17,10 @@ FUNCTION(ADD_LOL_TEST TEST_NAME) LIST(APPEND TEST_COMMAND -i=${CMAKE_CURRENT_SOURCE_DIR}/${ARG_INPUT}) ENDIF(ARG_INPUT) + IF(ARG_CWD) + LIST(APPEND TEST_COMMAND -w=${CMAKE_CURRENT_SOURCE_DIR}) + ENDIF(ARG_CWD) + IF(ARG_ERROR) LIST(APPEND TEST_COMMAND -e) ENDIF(ARG_ERROR) diff --git a/test/1.4-Tests/13-Bindings/1-stdio/2-fread/CMakeLists.txt b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/CMakeLists.txt new file mode 100644 index 0000000..20e286e --- /dev/null +++ b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/CMakeLists.txt @@ -0,0 +1,2 @@ +INCLUDE(AddLolTest) +ADD_LOL_TEST(2-fread OUTPUT test.out CWD) diff --git a/test/1.4-Tests/13-Bindings/1-stdio/2-fread/read.dat b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/read.dat new file mode 100644 index 0000000..8ff0c76 --- /dev/null +++ b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/read.dat @@ -0,0 +1,9 @@ +Now close the windows and hush all the fields: +If the trees must, let them silently toss; +No bird is singing now, and if there is, +Be it my loss. + +It will be long ere the marshes resume, +I will be long ere the earliest bird: +So close the windows and not hear the wind, +But see all wind-stirred. diff --git a/test/1.4-Tests/13-Bindings/1-stdio/2-fread/test.lol b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/test.lol new file mode 100644 index 0000000..a402e18 --- /dev/null +++ b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/test.lol @@ -0,0 +1,19 @@ +HAI 1.4 + CAN HAS STDIO? + I HAS A file + file R I IZ STDIO'Z OPEN YR "read.dat" AN YR "r" MKAY + + I HAS A var + var R I IZ STDIO'Z LUK YR file AN YR 45 MKAY + VISIBLE var + + I HAS A var2 + var2 R I IZ STDIO'Z LUK YR file AN YR 1 MKAY + VISIBLE var2 AN " - " AN "::" + BOTH SAEM var2 AN "::", O RLY? + YA RLY + VISIBLE "success" + NO WAI + VISIBLE "fail" + OIC +KTHXBYE diff --git a/test/1.4-Tests/13-Bindings/1-stdio/2-fread/test.out b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/test.out new file mode 100644 index 0000000..31d4062 --- /dev/null +++ b/test/1.4-Tests/13-Bindings/1-stdio/2-fread/test.out @@ -0,0 +1,3 @@ +Now close the windows and hush all the fields +: - : +success diff --git a/test/1.4-Tests/13-Bindings/1-stdio/CMakeLists.txt b/test/1.4-Tests/13-Bindings/1-stdio/CMakeLists.txt index 3c192e3..3a568bc 100644 --- a/test/1.4-Tests/13-Bindings/1-stdio/CMakeLists.txt +++ b/test/1.4-Tests/13-Bindings/1-stdio/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(1-fopen) +add_subdirectory(2-fread) diff --git a/test/testDriver.py b/test/testDriver.py index e372476..043bb3a 100644 --- a/test/testDriver.py +++ b/test/testDriver.py @@ -3,6 +3,7 @@ import subprocess import sys import tempfile import argparse +import os MEMERR = 127 @@ -13,9 +14,12 @@ parser.add_argument('-o', '--outputFile', type=argparse.FileType('r'), default=N parser.add_argument('-i', '--inputFile', type=argparse.FileType('r'), default=None, help="File to be used as input") parser.add_argument('-e', '--expectError', action="store_true", help="Specify that an error should occur") parser.add_argument('-m', '--memCheck', action='store_true', help="Do a memory check") +parser.add_argument('-w', '--workingDirectory', default=None, help="Set the working directory") args = parser.parse_args() +if args.workingDirectory: + os.chdir(args.workingDirectory) if args.inputFile == None: print("Not using an input file")