saait

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.inz.fi/saait
Log | Files | Refs | README | LICENSE

commit beb62344d803cff8f20c207f2face04fdb38d33f
parent 4e687c03f39d0bf6cc2f4ee4657691cc970b2240
Author: Santtu Lakkala <inz@inz.fi>
Date:   Thu, 17 Apr 2025 15:49:16 +0300

Add support for quoted values and conditional content

Diffstat:
Msaait.c | 26++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/saait.c b/saait.c @@ -275,6 +275,11 @@ parsevars(const char *file, const char *s) s = &s[strcspn(s, "\r\n")]; valueend = s; + if (valuestart != valueend && *valuestart == '"' && valueend[-1] == '"') { + valuestart++; + valueend--; + } + v = ecalloc(1, sizeof(*v)); v->key = ecalloc(1, keyend - keystart + 1); memcpy(v->key, keystart, keyend - keystart); @@ -331,6 +336,7 @@ writepage(FILE *fp, const char *name, const char *forname, case '#': /* insert value non-escaped */ case '$': /* insert value escaped */ case '%': /* insert contents of filename set in variable */ + case '?': /* text if variable matches */ if (*(s + 1) == '{') { s += 2; break; @@ -348,8 +354,8 @@ writepage(FILE *fp, const char *name, const char *forname, for (; *s && isspace((unsigned char)*s); s++) ; key = s; - for (keylen = 0; *s && *s != '}'; s++) - keylen++; + keylen = strcspn(s, "=}" + (op != '?')); + s += keylen; /* trim right whitespace */ for (; keylen && isspace((unsigned char)key[keylen - 1]); ) keylen--; @@ -385,6 +391,22 @@ writepage(FILE *fp, const char *name, const char *forname, catfile(fpin, v->value, fp, name); fclose(fpin); break; + case '?': + char *cmp = s + 1; + size_t len = strcspn(cmp, ":"); + tmpc = cmp[len]; + cmp[len] = '\0'; + s = cmp + len + 1 + strcspn(cmp + len + 1, "}"); + + if (strcmp(v->value, cmp)) { + cmp[len] = tmpc; + continue; + } + cmp[len] = tmpc; + tmpc = *s; + *s = '\0'; + fwrite(cmp + len + 1, 1, s - (cmp + len + 1), fp); + *s = tmpc; } } }