From: Michael Tremer Date: Mon, 1 Mar 2021 12:23:39 +0000 (+0000) Subject: parser: Handle lines continued with backslash X-Git-Tag: 0.9.28~1285^2~679 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49935e02f3cceb9dce24e6c914c3e1024edb7803;p=pakfire.git parser: Handle lines continued with backslash Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/parser/scanner.l b/src/libpakfire/parser/scanner.l index 73fc2ca42..4f964d39f 100644 --- a/src/libpakfire/parser/scanner.l +++ b/src/libpakfire/parser/scanner.l @@ -73,6 +73,56 @@ COPY: #define unput_string(s) for (int i = strlen(s) - 1; i >= 0; i--) unput(s[i]) +static char* copy_string(const char* s) { + if (!s) + return NULL; + + // Remove any leading whitespace + while (*s && isspace(*s)) + s++; + + // Remove any trailing whitespace + const char* e = s + strlen(s); + while (*e && isspace(*s)) + e--; + + // Determine the length of the string + const size_t l = strlen(s); + + // Allocate a working buffer + char* buffer = malloc(l + 1); + + // Copy input string to buffer + memcpy(buffer, s, l + 1); + + // Pointer to the start of the string + char* p = buffer; + + char* linebreak = strstr(p, "\\\n"); + while (linebreak) { + // Move p to the beginning of the linebreak + p = linebreak; + + // Skip \\\n + linebreak += strlen("\\\n"); + + // Find any whitespace after the linebreak + while (*linebreak && isspace(*linebreak)) + linebreak++; + + if (!linebreak) + break; + + // Splice together + memmove(p, linebreak, buffer + l - linebreak + 1); + + // Find another linebreak + linebreak = strstr(p, "\\\n"); + } + + return buffer; +} + %} %x INDENT @@ -206,12 +256,17 @@ scriptlet script(let)? yy_push_state(NOKEYWORD); } +.*\\\n { + // Continue if a line ends with a "\" + yymore(); + } + .*$ { // Return to caller yy_pop_state(); // Copy the entire string - yylval.string = pakfire_lstrip(yytext); + yylval.string = copy_string(yytext); return T_STRING; } diff --git a/tests/data/parser/test-declarations.txt b/tests/data/parser/test-declarations.txt index 75b12dc6f..115f7dbae 100644 --- a/tests/data/parser/test-declarations.txt +++ b/tests/data/parser/test-declarations.txt @@ -11,3 +11,8 @@ lines line 1 line 2 end + +# Continued line +e = A \ + B \ + C