]> git.webhop.me Git - lcd4linux.git/commitdiff
evaluator: escape sequences in strings (fix for ticket #145)
authormichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Mon, 26 Jan 2009 05:18:07 +0000 (05:18 +0000)
committermichael <michael@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Mon, 26 Jan 2009 05:18:07 +0000 (05:18 +0000)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@979 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

evaluator.c

index e70aaa59070cd3e394cd189b7545ec8b09e03160..52212b35f5401f56f1c6720e8a00f03ce3c13941 100644 (file)
@@ -591,13 +591,65 @@ static void Parse(void)
 
     /* strings */
     else if (*ExprPtr == '\'') {
-       char *start = ++ExprPtr;
-       while (*ExprPtr != '\0' && *ExprPtr != '\'')
-           ExprPtr++;
-       Word = strndup(start, ExprPtr - start);
+       size_t length = 0;
+       size_t size = CHUNK_SIZE;
+       Word = malloc(size);
+       ExprPtr++;
+       while (*ExprPtr != '\0' && *ExprPtr != '\'') {
+           if (*ExprPtr == '\\') {
+               switch (*(ExprPtr + 1)) {
+               case '\\':
+               case '\'':
+                   Word[length++] = *(ExprPtr + 1);
+                   ExprPtr += 2;
+                   break;
+               case 'a':
+                   Word[length++] = '\a';
+                   ExprPtr += 2;
+                   break;
+               case 'b':
+                   Word[length++] = '\b';
+                   ExprPtr += 2;
+                   break;
+               case 't':
+                   Word[length++] = '\t';
+                   ExprPtr += 2;
+                   break;
+               case 'n':
+                   Word[length++] = '\n';
+                   ExprPtr += 2;
+                   break;
+               case 'v':
+                   Word[length++] = '\v';
+                   ExprPtr += 2;
+                   break;
+               case 'f':
+                   Word[length++] = '\f';
+                   ExprPtr += 2;
+                   break;
+               case 'r':
+                   Word[length++] = '\r';
+                   ExprPtr += 2;
+                   break;
+               default:
+                   error("Evaluator: unknown escape sequence '\\%c' in <%s>", *(ExprPtr + 1), Expression);
+                   Word[length++] = *ExprPtr++;
+               }
+           } else {
+               Word[length++] = *ExprPtr++;
+           }
+           if (length >= size) {
+               size += CHUNK_SIZE;
+               Word = realloc(Word, size);
+           }
+       }
+       Word[length] = '\0';
        Token = T_STRING;
-       if (*ExprPtr == '\'')
+       if (*ExprPtr == '\'') {
            ExprPtr++;
+       } else {
+           error("Evaluator: unterminated string in <%s>", Expression);
+       }
     }
 
     /* non-alpha operators */