summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2010-07-21 02:57:35 +0200
committerLennart Poettering <lennart@poettering.net>2010-07-21 02:57:35 +0200
commitc24eb49e6aecd6de2ad450083e826d4c9d9c75b6 (patch)
tree6833f35a658d6427eb981b320df3c6913e8fc455 /src/util.c
parent9d25f5ed7b80df732cf7842985aa9b243bcd2335 (diff)
exec: extend variable substitution to support splitting variable values into seperate arguments
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c51
1 files changed, 44 insertions, 7 deletions
diff --git a/src/util.c b/src/util.c
index 45de609c29..da8a6c3336 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2778,7 +2778,7 @@ void status_welcome(void) {
char *replace_env(const char *format, char **env) {
enum {
WORD,
- DOLLAR,
+ CURLY,
VARIABLE
} state = WORD;
@@ -2793,11 +2793,11 @@ char *replace_env(const char *format, char **env) {
case WORD:
if (*e == '$')
- state = DOLLAR;
+ state = CURLY;
break;
- case DOLLAR:
- if (*e == '(') {
+ case CURLY:
+ if (*e == '{') {
if (!(k = strnappend(r, word, e-word-1)))
goto fail;
@@ -2821,7 +2821,7 @@ char *replace_env(const char *format, char **env) {
break;
case VARIABLE:
- if (*e == ')') {
+ if (*e == '}') {
char *t;
if ((t = strv_env_get_with_length(env, word+2, e-word-2))) {
@@ -2853,12 +2853,49 @@ fail:
char **replace_env_argv(char **argv, char **env) {
char **r, **i;
- unsigned k = 0;
+ unsigned k = 0, l = 0;
+
+ l = strv_length(argv);
- if (!(r = new(char*, strv_length(argv)+1)))
+ if (!(r = new(char*, l+1)))
return NULL;
STRV_FOREACH(i, argv) {
+
+ /* If $FOO appears as single word, replace it by the split up variable */
+ if ((*i)[0] == '$') {
+ char *e = strv_env_get(env, *i+1);
+
+ if (e) {
+ char **w, **m;
+ unsigned q;
+
+ if (!(m = strv_split_quoted(e))) {
+ r[k] = NULL;
+ strv_free(r);
+ return NULL;
+ }
+
+ q = strv_length(m);
+ l = l + q - 1;
+
+ if (!(w = realloc(r, sizeof(char*) * (l+1)))) {
+ r[k] = NULL;
+ strv_free(r);
+ strv_free(m);
+ return NULL;
+ }
+
+ r = w;
+ memcpy(r + k, m, q * sizeof(char*));
+ free(m);
+
+ k += q;
+ continue;
+ }
+ }
+
+ /* If ${FOO} appears as part of a word, replace it by the variable as-is */
if (!(r[k++] = replace_env(*i, env))) {
strv_free(r);
return NULL;