aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-11-04 14:20:03 +0100
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-11-04 14:20:23 +0100
commit1ffc240b3f7668ff2fb4e72b6e15937824a6cf3e (patch)
treeff6c2580b0872e9fcaecbb9b79a6e40f6f710d29
parentacd09007a12e4901aa5d221af18de9c42044d970 (diff)
cleanup normalize_line state machine
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
-rw-r--r--lib/libcfg/rdline.c17
1 files 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;