diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-11-13 17:59:29 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2013-11-13 17:59:29 +0000 |
commit | 3893d490abaa275fcadf180ff4ffc88e159f5c04 (patch) | |
tree | 82dc18af36134dd65de33ede1ce14f40af792a26 /src/cfg_utils.c | |
parent | 3fba18a9c6ffa24e7f31afd7bd6bb4ede464e51d (diff) | |
download | rspamd-3893d490abaa275fcadf180ff4ffc88e159f5c04.tar.gz rspamd-3893d490abaa275fcadf180ff4ffc88e159f5c04.zip |
Add support of path variables.
Support expanding of path variables in rspamd:
- $CONFDIR: configuration directory
- $LOCALSTATESDIR: local states directory
- $INSTALLPREFIX: installation prefix
- $VERSION: rspamd version
Diffstat (limited to 'src/cfg_utils.c')
-rw-r--r-- | src/cfg_utils.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/src/cfg_utils.c b/src/cfg_utils.c index dd6b00dcd..6a9477e00 100644 --- a/src/cfg_utils.c +++ b/src/cfg_utils.c @@ -1151,5 +1151,138 @@ check_classifier_statfiles (struct classifier_config *cf) } /* + * Variables: + * $CONFDIR - configuration directory + * $LOCALSTATESDIR - local states directory + * $INSTALLPREFIX - installation prefix + * $VERSION - rspamd version + */ + +#define RSPAMD_CONFDIR_MACRO "CONFDIR" +#define RSPAMD_LOCALSTATESDIR_MACRO "LOCALSTATESDIR" +#define RSPAMD_INSTALLPREFIX_MACRO "INSTALLPREFIX" +#define RSPAMD_VERSION_MACRO "VERSION" + +static const gchar * +rspamd_check_path_variable (const gchar *in, gsize *len) +{ + switch (*in) { + case 'C': + if (strncmp (in, RSPAMD_CONFDIR_MACRO, sizeof (RSPAMD_CONFDIR_MACRO) - 1) == 0) { + *len += sizeof (ETC_PREFIX) - 1; + in += sizeof (RSPAMD_CONFDIR_MACRO) - 1; + } + break; + case 'L': + if (strncmp (in, RSPAMD_LOCALSTATESDIR_MACRO, sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1) == 0) { + *len += sizeof (LOCALSTATES_PREFIX) - 1; + in += sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1; + } + break; + case 'I': + if (strncmp (in, RSPAMD_INSTALLPREFIX_MACRO, sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1) == 0) { + *len += sizeof (CMAKE_PREFIX) - 1; + in += sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1; + } + break; + case 'V': + if (strncmp (in, RSPAMD_VERSION_MACRO, sizeof (RSPAMD_VERSION_MACRO) - 1) == 0) { + *len += sizeof (RVERSION) - 1; + in += sizeof (RSPAMD_VERSION_MACRO) - 1; + } + break; + } + + return in; +} + +static const gchar * +rspamd_expand_path_variable (const gchar *in, gchar **dest) +{ + gchar *d = *dest; + const gchar *v = in + 1; + + *d = *v; + in ++; + + switch (*v) { + case 'C': + if (strncmp (v, RSPAMD_CONFDIR_MACRO, sizeof (RSPAMD_CONFDIR_MACRO) - 1) == 0) { + memcpy (d, ETC_PREFIX, sizeof (ETC_PREFIX) - 1); + d += sizeof (ETC_PREFIX) - 1; + in += sizeof (RSPAMD_CONFDIR_MACRO) - 1; + } + break; + case 'L': + if (strncmp (v, RSPAMD_LOCALSTATESDIR_MACRO, sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1) == 0) { + memcpy (d, LOCALSTATES_PREFIX, sizeof (LOCALSTATES_PREFIX) - 1); + d += sizeof (LOCALSTATES_PREFIX) - 1; + in += sizeof (RSPAMD_LOCALSTATESDIR_MACRO) - 1; + } + break; + case 'I': + if (strncmp (v, RSPAMD_INSTALLPREFIX_MACRO, sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1) == 0) { + memcpy (d, CMAKE_PREFIX, sizeof (CMAKE_PREFIX) - 1); + d += sizeof (CMAKE_PREFIX) - 1; + in += sizeof (RSPAMD_INSTALLPREFIX_MACRO) - 1; + } + break; + case 'V': + if (strncmp (v, RSPAMD_VERSION_MACRO, sizeof (RSPAMD_VERSION_MACRO) - 1) == 0) { + memcpy (d, RVERSION, sizeof (RVERSION) - 1); + d += sizeof (RVERSION) - 1; + in += sizeof (RSPAMD_VERSION_MACRO) - 1; + } + break; + } + + *dest = d; + return in; +} + +const gchar* +rspamd_expand_path (memory_pool_t *pool, const gchar *path) +{ + const gchar *p = path; + gchar *dest, *d; + gsize len = 0, orig_len = 0; + + while (*p != '\0') { + if (*p == '$') { + len ++; + p = rspamd_check_path_variable (p + 1, &len); + } + else { + len ++; + } + orig_len ++; + p ++; + } + + if (len == orig_len) { + return path; + } + + dest = memory_pool_alloc (pool, len + 1); + p = path; + d = dest; + + while (*p != '\0') { + if (*p == '$') { + p = rspamd_expand_path_variable (p, &d); + } + else { + *d++ = *p++; + } + } + + *d = '\0'; + + msg_debug ("expanded %s to %s", path, dest); + + return dest; +} + +/* * vi:ts=4 */ |