From 1ffc240b3f7668ff2fb4e72b6e15937824a6cf3e Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 4 Nov 2018 14:20:03 +0100 Subject: [PATCH] cleanup normalize_line state machine Signed-off-by: David Oberhollenzer --- lib/libcfg/rdline.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/libcfg/rdline.c b/lib/libcfg/rdline.c index b532def..dbb3069 100644 --- a/lib/libcfg/rdline.c +++ b/lib/libcfg/rdline.c @@ -72,15 +72,17 @@ static int normalize_line(rdline_t *t) while (isspace(*src)) ++src; - while (*src != '\0' && (string || *src != '#')) { + do { c = *(src++); if (c == '"') { string = !string; + } else if (!string && c == '#') { + c = '\0'; } else if (!string && isspace(c)) { - c = ' '; - if (dst > t->line && dst[-1] == ' ') + if (*src == '#' || *src == '\0' || isspace(*src)) continue; + c = ' '; } else if (c == '%') { *(dst++) = c; c = *(src++); @@ -100,16 +102,12 @@ static int normalize_line(rdline_t *t) } *(dst++) = c; - } + } while (c != '\0'); if (string) { errstr = "missing \""; goto fail; } - - while (dst > t->line && dst[-1] == ' ') - --dst; - *dst = '\0'; return ret; fail: fprintf(stderr, "%s: %zu: %s\n", t->filename, t->lineno, errstr); @@ -123,9 +121,8 @@ static void substitute(rdline_t *t, char *dst, char *src) while (*src != '\0') { if (src[0] == '%' && isdigit(src[1])) { strcpy(dst, t->argv[src[1] - '0']); + dst += strlen(dst); src += 2; - while (*dst != '\0') - ++dst; } else if (src[0] == '%' && src[1] == '%') { *(dst++) = '%'; src += 2;