aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2023-06-06 16:19:37 +0000
committerWerner Koch <[email protected]>2023-08-25 12:41:35 +0000
commiteeda4ef7d2233791a8dad2d0d56417e016662aa0 (patch)
tree5fd345affbc949303232837e7579a1ff4a5b0911
parentdirmngr: Fix LDAP time parser. (diff)
downloadgnupg-eeda4ef7d2233791a8dad2d0d56417e016662aa0.tar.gz
gnupg-eeda4ef7d2233791a8dad2d0d56417e016662aa0.zip
common: New function substitute_vars.
* common/stringhelp.c (substitute_envvars): Factor code out to (substitute_vars): new. (subst_getenv): New. -- This is a generalized version of substitute_envvars. (cherry picked from commit 7b7fdf45e5d8b3b066c5efbf6ec872e1249f3a24)
-rw-r--r--common/stringhelp.c44
-rw-r--r--common/stringhelp.h5
2 files changed, 42 insertions, 7 deletions
diff --git a/common/stringhelp.c b/common/stringhelp.c
index 5baaa1b2b..a14e2351e 100644
--- a/common/stringhelp.c
+++ b/common/stringhelp.c
@@ -1691,10 +1691,16 @@ format_text (const char *text_in, int target_cols, int max_cols)
}
-/* Substitute environment variables in STRING and return a new string.
- * On error the function returns NULL. */
+/* Substitute variables in STRING and return a new string. GETVAL is
+ * a function which maps NAME to its value; that value is a string
+ * which may not change during the execution time of this function.
+ * If GETVAL returns NULL substitute_vars returns NULL and the caller
+ * may inspect ERRNO for the reason. In all other error cases this
+ * function also returns NULL. Caller must free the returned string. */
char *
-substitute_envvars (const char *string)
+substitute_vars (const char *string,
+ const char *(*getval)(void *cookie, const char *name),
+ void *cookie)
{
char *line, *p, *pend;
const char *value;
@@ -1745,19 +1751,22 @@ substitute_envvars (const char *string)
{
int save = *pend;
*pend = 0;
- value = getenv (p+2);
+ value = getval (cookie, p+2);
*pend++ = save;
}
else
{
int save = *pend;
*pend = 0;
- value = getenv (p+1);
+ value = getval (cookie, p+1);
*pend = save;
}
if (!value)
- value = "";
+ {
+ xfree (result);
+ return NULL;
+ }
valuelen = strlen (value);
if (valuelen <= pend - p)
{
@@ -1793,3 +1802,26 @@ substitute_envvars (const char *string)
leave:
return result;
}
+
+
+/* Helper for substitute_envvars. */
+static const char *
+subst_getenv (void *cookie, const char *name)
+{
+ const char *s;
+
+ (void)cookie;
+
+ s = getenv (name);
+ return s? s : "";
+}
+
+
+/* Substitute environment variables in STRING and return a new string.
+ * On error the function returns NULL. */
+char *
+substitute_envvars (const char *string)
+{
+ return substitute_vars (string, subst_getenv, NULL);
+
+}
diff --git a/common/stringhelp.h b/common/stringhelp.h
index 84215c77c..7e5d593ec 100644
--- a/common/stringhelp.h
+++ b/common/stringhelp.h
@@ -169,7 +169,10 @@ int compare_version_strings (const char *my_version, const char *req_version);
/* Format a string so that it fits within about TARGET_COLS columns. */
char *format_text (const char *text, int target_cols, int max_cols);
-/* Substitute environmen variabales in STRING. */
+/* Substitute variables in STRING. */
+char *substitute_vars (const char *string,
+ const char *(*getval)(void *cookie, const char *name),
+ void *cookie);
char *substitute_envvars (const char *string);