From 7d6d9b33254066fbfa2d7a0ecfbefe98d5d9ae47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Guelfucci?= Date: Sun, 24 Oct 2010 22:59:36 +0200 Subject: [PATCH] Expand ~ in the whole command, not only at the start. --- libxfce4util/xfce-miscutils.c | 82 +++++++++++++++++++++++++--------------- 1 files changed, 51 insertions(+), 31 deletions(-) diff --git a/libxfce4util/xfce-miscutils.c b/libxfce4util/xfce-miscutils.c index 56db56e..40aa78d 100644 --- a/libxfce4util/xfce-miscutils.c +++ b/libxfce4util/xfce-miscutils.c @@ -504,7 +504,7 @@ xfce_unsetenv (const gchar *name) * environment. * * Expands shell like environment variables and tilde (~/ and ~user/ are both supported) - * in @command. + * in @command. * * Return value: %NULL on error, else the string, which should be freed using * g_free() when no longer needed. @@ -519,6 +519,10 @@ xfce_expand_variables (const gchar *command, struct passwd *pw; #endif const gchar *value; + gboolean space_before; + gboolean equal_before; + gboolean in_single_quote; + gboolean in_double_quote; gchar variable[256]; gchar buffer[2048]; gchar *bend = buffer + 2047; @@ -529,55 +533,71 @@ xfce_expand_variables (const gchar *command, guint len; g_return_val_if_fail (command != NULL, NULL); - - if (*command == '~') + + space_before = TRUE; + equal_before = FALSE; + in_single_quote = FALSE; + in_double_quote = FALSE; + + while (*command != '\0' && bp < bend) { - if (*++command == '/' || *command == '\0') + if (*command == '~' + && (space_before || equal_before || in_single_quote || in_double_quote)) { - /* ~/ syntax */ - g_strlcpy (buffer, xfce_get_homedir (), 2048); - bp = buffer + strlen (buffer); - } + if (*++command == '/' || *command == '\0') + { + /* ~/ syntax */ + g_strlcat (buffer, xfce_get_homedir (), 2048); + bp = buffer + strlen (buffer); + } #ifdef HAVE_GETPWNAM - else - { - /* ~user/ syntax */ - for (vp = variable; g_ascii_isalnum (*command) && vp < vend; ) - *vp++ = *command++; - - if (vp != variable) + else { - *vp = '\0'; + /* ~user/ syntax */ + for (vp = variable; g_ascii_isalnum (*command) && vp < vend; ) + *vp++ = *command++; - pw = getpwnam (variable); - if (pw != NULL && pw->pw_dir != NULL) + if (vp != variable) { - g_strlcpy (buffer, pw->pw_dir, 2048); - bp = buffer + strlen (buffer); + *vp = '\0'; + + pw = getpwnam (variable); + if (pw != NULL && pw->pw_dir != NULL) + { + g_strlcat (buffer, pw->pw_dir, 2048); + bp = buffer + strlen (buffer); + } } } - } #endif - } - - while (*command != '\0' && bp < bend) - { - if (*command != '$') - *bp++ = *command++; + } + else if (*command != '$') + { + if (*command == ' ') + space_before = TRUE; + if (*command == '=') + equal_before = TRUE; + if (*command == '\'' && !in_double_quote) + in_single_quote = !in_single_quote; + if (*command == '"' && !in_single_quote) + in_double_quote = !in_double_quote; + + *bp++ = *command++; + } else { ++command; - + for (vp = variable; g_ascii_isalnum (*command) && vp < vend; ) *vp++ = *command++; - + if (vp == variable) continue; *vp = '\0'; len = vp - variable; value = NULL; - + if (envp != NULL) { for (ep = envp; *ep != NULL; ++ep) @@ -587,7 +607,7 @@ xfce_expand_variables (const gchar *command, break; } } - + if (value == NULL) value = g_getenv (variable); -- 1.7.1