From 70f7776c6e8c549e13adf858c57fe339583c7cec Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Thu, 4 Aug 2016 12:00:00 -0400 Subject: [PATCH] basic: add new merge_env_file function merge_env_file is a new function, that's like load_env_file, but takes a pre-existing environment as an input argument. New environment entries are merged. Variable expansion is performed. Falling back to the process environment is supported (when a flag is set). Alternatively this could be implemented as passing an additional fallback environment array, but later on we're adding another flag to allow braceless expansion, and the two flags can be combined in one arg, so there's less stuff to pass around. --- src/basic/fileio.c | 28 ++++++++++++++++++++++++++++ src/basic/fileio.h | 2 ++ 2 files changed, 30 insertions(+) diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 4ad2acf3b..55dda26a6 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -767,6 +767,34 @@ int load_env_file_pairs(FILE *f, const char *fname, const char *newline, char ** return 0; } +static int merge_env_file_push( + const char *filename, unsigned line, + const char *key, char *value, + void *userdata, + int *n_pushed) { + + char ***env = userdata; + char *expanded_value; + + assert(env); + + expanded_value = replace_env(value, *env, REPLACE_ENV_USE_ENVIRONMENT); + if (!expanded_value) + return -ENOMEM; + + free_and_replace(value, expanded_value); + + return load_env_file_push(filename, line, key, value, env, n_pushed); +} + +int merge_env_file( + char ***env, + FILE *f, + const char *fname) { + + return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL); +} + static void write_env_var(FILE *f, const char *v) { const char *p; diff --git a/src/basic/fileio.h b/src/basic/fileio.h index fe9506c3d..6a628086b 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -49,6 +49,8 @@ int parse_env_file(const char *fname, const char *separator, ...) _sentinel_; int load_env_file(FILE *f, const char *fname, const char *separator, char ***l); int load_env_file_pairs(FILE *f, const char *fname, const char *separator, char ***l); +int merge_env_file(char ***env, FILE *f, const char *fname); + int write_env_file(const char *fname, char **l); int executable_is_script(const char *path, char **interpreter); -- 2.30.2