summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorRay Strode <rstrode@redhat.com>2016-08-09 10:20:22 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-02-20 23:32:53 -0500
commitb82f58bfe396b395bce3452bc0ba2f972fb01ab8 (patch)
tree02767a48aced8b08db4aba14890a224a97c05111 /src/basic/fileio.c
parent4bed076c5f79ce26451ea3d73950d895f630f9a7 (diff)
basic: support default and alternate values for env expansion
Sometimes it's useful to provide a default value during an environment expansion, if the environment variable isn't already set. For instance $XDG_DATA_DIRS is suppose to default to: /usr/local/share/:/usr/share/ if it's not yet set. That means callers wishing to augment XDG_DATA_DIRS need to manually add those two values. This commit changes replace_env to support the following shell compatible default value syntax: XDG_DATA_DIRS=/foo:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share} Likewise, it's useful to provide an alternate value during an environment expansion, if the environment variable isn't already set. For instance, $LD_LIBRARY_PATH will inadvertently search the current working directory if it starts or ends with a colon, so the following is usually wrong: LD_LIBRARY_PATH=/foo/lib:${LD_LIBRARY_PATH} To address that, this changes replace_env to support the following shell compatible alternate value syntax: LD_LIBRARY_PATH=/foo/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} [zj: gate the new syntax under REPLACE_ENV_ALLOW_EXTENDED switch, so existing callers are not modified.]
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 8185f67e00..b9a9f74892 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -784,7 +784,9 @@ static int merge_env_file_push(
}
expanded_value = replace_env(value, *env,
- REPLACE_ENV_USE_ENVIRONMENT|REPLACE_ENV_ALLOW_BRACELESS);
+ REPLACE_ENV_USE_ENVIRONMENT|
+ REPLACE_ENV_ALLOW_BRACELESS|
+ REPLACE_ENV_ALLOW_EXTENDED);
if (!expanded_value)
return -ENOMEM;
@@ -799,7 +801,7 @@ int merge_env_file(
const char *fname) {
/* NOTE: this function supports braceful and braceless variable expansions,
- * unlike other exported parsing functions.
+ * plus "extended" substitutions, unlike other exported parsing functions.
*/
return parse_env_file_internal(f, fname, NEWLINE, merge_env_file_push, env, NULL);