mirror of
git://git.gnupg.org/gnupg.git
synced 2025-01-21 14:47:03 +01:00
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.
This commit is contained in:
parent
baa8883215
commit
7b7fdf45e5
@ -1689,10 +1689,16 @@ format_text (const char *text_in, int target_cols, int max_cols)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Substitute environment variables in STRING and return a new string.
|
/* Substitute variables in STRING and return a new string. GETVAL is
|
||||||
* On error the function returns NULL. */
|
* 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 *
|
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;
|
char *line, *p, *pend;
|
||||||
const char *value;
|
const char *value;
|
||||||
@ -1743,19 +1749,22 @@ substitute_envvars (const char *string)
|
|||||||
{
|
{
|
||||||
int save = *pend;
|
int save = *pend;
|
||||||
*pend = 0;
|
*pend = 0;
|
||||||
value = getenv (p+2);
|
value = getval (cookie, p+2);
|
||||||
*pend++ = save;
|
*pend++ = save;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
int save = *pend;
|
int save = *pend;
|
||||||
*pend = 0;
|
*pend = 0;
|
||||||
value = getenv (p+1);
|
value = getval (cookie, p+1);
|
||||||
*pend = save;
|
*pend = save;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!value)
|
if (!value)
|
||||||
value = "";
|
{
|
||||||
|
xfree (result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
valuelen = strlen (value);
|
valuelen = strlen (value);
|
||||||
if (valuelen <= pend - p)
|
if (valuelen <= pend - p)
|
||||||
{
|
{
|
||||||
@ -1791,3 +1800,26 @@ substitute_envvars (const char *string)
|
|||||||
leave:
|
leave:
|
||||||
return result;
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -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. */
|
/* Format a string so that it fits within about TARGET_COLS columns. */
|
||||||
char *format_text (const char *text, int target_cols, int max_cols);
|
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);
|
char *substitute_envvars (const char *string);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user