From 33f6697cd5beb6bc21fcd5db5f61558005af4baa Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 2 Jul 2025 12:43:30 +0200 Subject: [PATCH] conf: new function conf_parse_env() Reduce code duplication. Signed-off-by: Joachim Wiberg --- src/conf.c | 36 +++++++++++++++++++++++++------- src/conf.h | 1 + src/service.c | 58 ++++++--------------------------------------------- 3 files changed, 35 insertions(+), 60 deletions(-) diff --git a/src/conf.c b/src/conf.c index bcaa8317..5041aa01 100644 --- a/src/conf.c +++ b/src/conf.c @@ -490,14 +490,16 @@ void conf_save_service(int type, char *cfg, char *file) fclose(fp); } -/* - * Sets, and makes a note of, all KEY=VALUE lines in a given .conf line - * from finit.conf, or other .conf file. Note, PATH is always reset in - * the conf_reset_env() function. +/** + * conf_parse_env - Parse a key=value line + * @line: Line buffer without newline + * @value: Whitespace trimmed value + * + * Returns: + * %NULL on error, otherwise a whitespace trimmed key. */ -static void parse_env(char *line) +char *conf_parse_env(char *line, char **value) { - struct env_entry *node; char *key, *val, *end; /* skip any leading whitespace */ @@ -519,7 +521,7 @@ static void parse_env(char *line) val = strchr(key, '='); if (!val) - return; + return NULL; *val++ = 0; /* strip leading whitespace from value */ @@ -528,6 +530,7 @@ static void parse_env(char *line) /* unquote value, if quoted */ unquote(&val, end); + *value = val; /* find end of key */ end = key; @@ -554,9 +557,26 @@ static void parse_env(char *line) end++; if (*end != 0) { warnx("'%s=%s': not a valid identifier", key, val); - return; /* invalid key */ + return NULL; /* invalid key */ } + return key; +} + +/* + * Sets, and makes a note of, all KEY=VALUE lines in a given .conf line + * from finit.conf, or other .conf file. Note, PATH is always reset in + * the conf_reset_env() function. + */ +static void parse_env(char *line) +{ + struct env_entry *node; + char *key, *val; + + key = conf_parse_env(line, &val); + if (!key) + return; + dbg("Global env '%s'='%s'", key, val); setenv(key, val, 1); diff --git a/src/conf.h b/src/conf.h index 0ac82e75..972197b8 100644 --- a/src/conf.h +++ b/src/conf.h @@ -47,6 +47,7 @@ void conf_saverc (void); void conf_save_exec_order (svc_t *svc, char *cmdline, int result); void conf_save_service (int type, char *cfg, char *file); void conf_parse_cmdline (int argc, char *argv[]); +char *conf_parse_env (char *line, char **val); int conf_parse_runlevels (char *runlevels); void conf_parse_cond (svc_t *svc, char *cond); diff --git a/src/service.c b/src/service.c index 3b3c0d77..83d9aa60 100644 --- a/src/service.c +++ b/src/service.c @@ -425,11 +425,13 @@ static void source_env(svc_t *svc) line = buf; while (fgets(line, LINE_SIZE, fp)) { - char *key = chomp(line); wordexp_t we = { 0 }; - char *value, *end; + char *key, *value; size_t i; + /* Trim newline */ + key = chomp(line); + /* skip any leading whitespace */ while (isspace(*key)) key++; @@ -438,57 +440,9 @@ static void source_env(svc_t *svc) if (*key == '#' || *key == ';') continue; - /* find end of line */ - end = key; - while (*end) - end++; - - /* strip trailing whitespace */ - if (end > key) { - end--; - while (isspace(*end)) - *end-- = 0; - } - - value = strchr(key, '='); - if (!value) + key = conf_parse_env(key, &value); + if (!key) continue; - *value++ = 0; - - /* strip leading whitespace from value */ - while (isspace(*value)) - value++; - - /* unquote value, if quoted */ - unquote(&value, end); - - /* find end of key */ - end = key; - while (*end) - end++; - - /* strip trailing whitespace */ - if (end > key) { - end--; - while (isspace(*end)) - *end-- = 0; - } - - /* strip any leading 'set ' */ - end = key; - if (!strncmp(key, "set", 3)) - end += 3; - - /* check key, no spaces allowed */ - while (*end && isspace(*end)) - end++; - key = end; - while (*end && !isspace(*end)) - end++; - if (*end != 0) { - warnx("'%s=%s': not a valid identifier", key, value); - continue; /* invalid key */ - } if (wordexp(value, &we, 0)) { setenv(key, value, 1);