saait

Simple static page generator
git clone https://git.sinitax.com/codemadness/saait
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit 0007e983fbd257049d360f1347c4266ac9e83847
parent ee17d39756e30ba8fc6c6b46eecdcb08399f4d34
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Mon,  4 Dec 2017 23:20:49 +0100

writepage(): simplify key matching...

...for errors prefix the line with "filename:linenumber".

Diffstat:
Msaait.c | 38+++++++++++++++-----------------------
1 file changed, 15 insertions(+), 23 deletions(-)

diff --git a/saait.c b/saait.c @@ -247,10 +247,10 @@ readconfig(const char *file) void writepage(FILE *fp, const char *filename, struct variable *c, char *s) { - struct variable *var, *v; - char *key, *e, *value; + struct variable *v; + char *key; size_t keylen, line = 0; - int op; + int op, tmpc; for (; *s; s++) { op = *s; @@ -280,34 +280,26 @@ writepage(FILE *fp, const char *filename, struct variable *c, char *s) for (; keylen && isspace(key[keylen - 1]); ) keylen--; - /* page config variables */ - v = NULL; - for (var = c; var && !v; var = var->next) { - if (keylen == strlen(var->key) && !strncmp(var->key, key, keylen)) { - v = var; - break; - } - } - /* global config variables */ - for (var = global; var && !v; var = var->next) { - if (keylen == strlen(var->key) && !strncmp(var->key, key, keylen)) { - v = var; - break; - } - } + /* temporary NUL terminate */ + tmpc = key[keylen]; + key[keylen] = '\0'; + + if (!c || !(v = getvar(c, key))) + v = getvar(global, key); + key[keylen] = tmpc; /* restore NUL terminator to original */ + if (!v) { - fprintf(stderr, "%s: unknown variable: '%.*s' on line %zu\n", - filename, (int)keylen, key, line + 1); + fprintf(stderr, "%s:%zu: unknown variable: '%.*s'\n", + filename, line + 1, (int)keylen, key); exit(1); } - value = v->value; switch (op) { case '$': - xmlencode(value, fp); + xmlencode(v->value, fp); break; case '#': - fputs(value, fp); + fputs(v->value, fp); break; } }