-
-/* Substitutes $variables in SRC, putting the result in DST,
- properly handling the case where SRC is a substring of DST.
- Variables are as defined by GETENV. Supports $var and ${var}
- syntaxes; $$ substitutes as $. */
-void
-fn_interp_vars (struct substring src, const char *(*getenv) (const char *),
- struct string *dst_)
-{
- struct string dst = DS_EMPTY_INITIALIZER;
- int c;
-
- while ((c = ss_get_char (&src)) != EOF)
- if (c != '$')
- ds_put_char (&dst, c);
- else
- {
- if (ss_match_char (&src, '$') || ss_is_empty (src))
- ds_put_char (&dst, '$');
- else
- {
- struct substring var_name;
- size_t start;
- const char *value;
-
- if (ss_match_char (&src, '('))
- ss_get_until (&src, ')', &var_name);
- else if (ss_match_char (&src, '{'))
- ss_get_until (&src, '}', &var_name);
- else
- ss_get_chars (&src, MIN (1, ss_span (src, ss_cstr (CC_ALNUM))),
- &var_name);
-
- start = ds_length (&dst);
- ds_put_substring (&dst, var_name);
- value = getenv (ds_cstr (&dst) + start);
- ds_truncate (&dst, start);
-
- ds_put_cstr (&dst, value);
- }
- }
-
- ds_swap (&dst, dst_);
- ds_destroy (&dst);
-}
-
-#ifdef unix
-/* Expands csh tilde notation from the path INPUT into a malloc()'d
- returned string. */
-char *
-fn_tilde_expand (const char *input)
-{
- struct string output = DS_EMPTY_INITIALIZER;
- if (input[0] == '~')
- {
- const char *home = NULL;
- const char *remainder = NULL;
- if (input[1] == '/' || input[1] == '\0')
- {
- home = fn_getenv ("HOME");
- remainder = input + 1;
- }
- else
- {
- struct string user_name = DS_EMPTY_INITIALIZER;
- struct passwd *pwd;
-
- ds_assign_substring (&user_name,
- ss_buffer (input + 1,
- strcspn (input + 1, "/")));
- pwd = getpwnam (ds_cstr (&user_name));
- if (pwd != NULL && pwd->pw_dir[0] != '\0')
- {
- home = xstrdup (pwd->pw_dir);
- remainder = input + 1 + ds_length (&user_name);
- }
- ds_destroy (&user_name);
- }
-
- if (home != NULL)
- {
- ds_put_cstr (&output, home);
- if (*remainder != '\0')
- ds_put_cstr (&output, remainder);
- }
- }
- if (ds_is_empty (&output))
- ds_put_cstr (&output, input);
- return ds_cstr (&output);
-}
-#else /* !unix */