-/* 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, MAX (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);
-}
-
-/* Searches for a configuration file with name NAME in the path
- given by PATH, which is environment-interpolated.
- Directories in PATH are delimited by ':'. Returns the
- malloc'd full name of the first file found, or NULL if none is
- found. */
-char *
-fn_search_path (const char *base_name, const char *path_)
-{
- struct string path;
- struct substring dir;
- struct string file = DS_EMPTY_INITIALIZER;
- size_t save_idx = 0;
-
- if (fn_is_absolute (base_name))
- return xstrdup (base_name);
-
- /* Interpolate environment variables. */
- ds_init_cstr (&path, path_);
- fn_interp_vars (ds_ss (&path), fn_getenv, &path);
-
- verbose_msg (2, _("searching for \"%s\" in path \"%s\""),
- base_name, ds_cstr (&path));
- while (ds_separate (&path, ss_cstr (":"), &save_idx, &dir))
- {
- /* Construct file name. */
- ds_clear (&file);
- ds_put_substring (&file, dir);
- if (!ds_is_empty (&file) && !ISSLASH (ds_last (&file)))
- ds_put_char (&file, '/');
- ds_put_cstr (&file, base_name);
-
- /* Check whether file exists. */
- if (fn_exists (ds_cstr (&file)))
- {
- verbose_msg (2, _("...found \"%s\""), ds_cstr (&file));
- ds_destroy (&path);
- return ds_cstr (&file);
- }
- }
-
- /* Failure. */
- verbose_msg (2, _("...not found"));
- ds_destroy (&path);
- ds_destroy (&file);
- return NULL;
-}
-
-/* Returns the directory part of FILE_NAME, as a malloc()'d
- string. */
-char *
-fn_dir_name (const char *file_name)
-{
- return dir_name (file_name);
-}
-