/* Functions for performing operations on file names. */
-/* 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 $. */
+/* Copies from SRC to DST, calling INSERT_VARIABLE to handle each
+ instance of $var or ${var} in SRC. $$ is replaced by $. */
void
-fn_interp_vars (struct substring src, const char *(*getenv) (const char *),
- struct string *dst_)
+fn_interp_vars (struct substring src,
+ void (*insert_variable) (const char *var,
+ struct string *dst, void *aux),
+ void *aux, struct string *dst_)
{
struct string dst = DS_EMPTY_INITIALIZER;
int c;
else
{
struct substring var_name;
- size_t start;
- const char *value;
+ char *var;
if (ss_match_char (&src, '('))
ss_get_until (&src, ')', &var_name);
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);
+ var = ss_xstrdup (var_name);
+ insert_variable (var, &dst, aux);
+ free (var);
}
}
ds_destroy (&dst);
}
+static void
+insert_env_var (const char *var, struct string *dst, void *aux UNUSED)
+{
+ const char *value = fn_getenv (var);
+ if (value != NULL)
+ ds_put_cstr (dst, value);
+}
+
/* 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
/* Interpolate environment variables. */
ds_init_cstr (&path, path_);
- fn_interp_vars (ds_ss (&path), fn_getenv, &path);
+ fn_interp_vars (ds_ss (&path), insert_env_var, NULL, &path);
verbose_msg (2, _("searching for \"%s\" in path \"%s\""),
base_name, ds_cstr (&path));
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2009 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
void fn_init (void);
void fn_interp_vars (struct substring src,
- const char *(*getenv) (const char *),
- struct string *dst);
+ void (*insert_variable) (const char *var,
+ struct string *dst, void *aux),
+ void *aux, struct string *dst);
char *fn_search_path (const char *base_name, const char *path);
char *fn_dir_name (const char *fn);
char *fn_extension (const char *fn);
return getenv (key);
}
+static void
+insert_defn_value (const char *var, struct string *dst, void *aux UNUSED)
+{
+ const char *value = find_defn_value (var);
+ if (value != NULL)
+ ds_put_cstr (dst, value);
+}
+
/* Initializes global variables. */
void
outp_init (void)
ep++;
ds_init_cstr (&d->value, ep);
- fn_interp_vars (ds_ss (&d->value), find_defn_value, &d->value);
+ fn_interp_vars (ds_ss (&d->value), insert_defn_value, NULL, &d->value);
d->next = outp_macros;
d->prev = NULL;
if (outp_macros)
size_t save_idx;
size_t i;
- fn_interp_vars (line_, find_defn_value, &line);
+ fn_interp_vars (line_, insert_defn_value, NULL, &line);
save_idx = 0;
for (i = 0; i < 4; i++)