]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
config: config-parser - Add config_write_keyvariable
authorAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 20 Jan 2021 08:49:23 +0000 (10:49 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Wed, 3 Feb 2021 08:00:29 +0000 (08:00 +0000)
Move code handling key variable expansion to separate function.
Simplifies next commit.

src/config/config-parser.c

index 03fb4e35598422c911137f26efd58591f68df142..e7c33f95ee8b7eb213f58a011e2abed74c07e25d 100644 (file)
@@ -795,15 +795,55 @@ config_require_key(struct config_parser_context *ctx, const char *key)
        return FALSE;
 }
 
+static int config_write_keyvariable(struct config_parser_context *ctx,
+                                   const char *key, const char *value,
+                                   string_t *str)
+{
+       const void *var_name, *var_value, *p;
+       enum setting_type var_type;
+       bool dump, expand_parent;
+
+       /* expand_parent=TRUE for "key = $key stuff".
+          we'll always expand it so that doveconf -n can give
+          usable output */
+       p = strchr(value, ' ');
+       if (p == NULL)
+               var_name = value;
+       else
+               var_name = t_strdup_until(value, p);
+       expand_parent = strcmp(key, var_name) == 0;
+
+       if (!ctx->expand_values && !expand_parent) {
+               str_append_c(str, '$');
+               str_append(str, value);
+       } else {
+               var_value = config_get_value(ctx->cur_section, var_name,
+                                            expand_parent, &var_type);
+               if (var_value == NULL) {
+                       ctx->error = p_strconcat(ctx->pool,
+                                                "Unknown variable: $",
+                                                var_name, NULL);
+                       return -1;
+               }
+               if (!config_export_type(str, var_value, NULL,
+                                       var_type, TRUE, &dump)) {
+                       ctx->error = p_strconcat(ctx->pool,
+                                                "Invalid variable: $",
+                                                var_name, NULL);
+                       return -1;
+               }
+               if (p != NULL)
+                       str_append(str, p);
+       }
+       return 0;
+}
+
 static int config_write_value(struct config_parser_context *ctx,
                              enum config_line_type type,
                              const char *key, const char *value)
 {
        string_t *str = ctx->str;
-       const void *var_name, *var_value, *p;
-       enum setting_type var_type;
        const char *error, *path, *full_key;
-       bool dump, expand_parent;
 
        switch (type) {
        case CONFIG_LINE_TYPE_KEYVALUE:
@@ -828,38 +868,8 @@ static int config_write_value(struct config_parser_context *ctx,
                }
                break;
        case CONFIG_LINE_TYPE_KEYVARIABLE:
-               /* expand_parent=TRUE for "key = $key stuff".
-                  we'll always expand it so that doveconf -n can give
-                  usable output */
-               p = strchr(value, ' ');
-               if (p == NULL)
-                       var_name = value;
-               else
-                       var_name = t_strdup_until(value, p);
-               expand_parent = strcmp(key, var_name) == 0;
-
-               if (!ctx->expand_values && !expand_parent) {
-                       str_append_c(str, '$');
-                       str_append(str, value);
-               } else {
-                       var_value = config_get_value(ctx->cur_section, var_name,
-                                                    expand_parent, &var_type);
-                       if (var_value == NULL) {
-                               ctx->error = p_strconcat(ctx->pool,
-                                                        "Unknown variable: $",
-                                                        var_name, NULL);
-                               return -1;
-                       }
-                       if (!config_export_type(str, var_value, NULL,
-                                               var_type, TRUE, &dump)) {
-                               ctx->error = p_strconcat(ctx->pool,
-                                                        "Invalid variable: $",
-                                                        var_name, NULL);
-                               return -1;
-                       }
-                       if (p != NULL)
-                               str_append(str, p);
-               }
+               if (config_write_keyvariable(ctx, key, value, str) < 0)
+                       return -1;
                break;
        default:
                i_unreached();