]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] pattern: add support for argument parsers for converters
authorWilly Tarreau <w@1wt.eu>
Tue, 26 Jan 2010 16:58:06 +0000 (17:58 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 26 Jan 2010 17:01:35 +0000 (18:01 +0100)
Some converters will need one or several arguments. It's not possible
to write a simple generic parser for that, so let's add the ability
for each converter to support its own argument parser, and call it
to get the arguments when it's specified. If unspecified, the arguments
are passed unmodified as string+len.

include/types/pattern.h
src/pattern.c

index 811625e79e702906baa21e8fc761d16c76d3ab3a..01fb69ca8dbf33fa5e41af2398c7d085563934cb 100644 (file)
@@ -59,6 +59,9 @@ struct pattern_conv {
                       union pattern_data *data); /* process function */
        unsigned int in_type;                     /* input needed pattern type */
        unsigned int out_type;                    /* output pattern type */
+       int (*parse_args)(const char *arg_str,
+                         void **arg_p,
+                         int *arg_i);            /* argument parser. Can be NULL. */
 };
 
 /* pattern conversion expression */
index a5cb31f3a6019d7c2b59ec7dcf04ac60055910f6..aef151a47e3428f8464bb6830074cb19574bceff 100644 (file)
@@ -432,9 +432,18 @@ struct pattern_expr *pattern_parse_expr(char **str, int *idx)
                conv_expr->conv = conv;
 
                if (end != endw) {
-                       conv_expr->arg_i = end - endw - 2;
-                       conv_expr->arg_p = calloc(1, conv_expr->arg_i + 1);
-                       memcpy(conv_expr->arg_p, endw + 1, conv_expr->arg_i);
+                       int i = end - endw - 2;
+                       char *p = my_strndup(endw + 1, i);
+
+                       if (conv->parse_args) {
+                               i = conv->parse_args(p, &conv_expr->arg_p, &conv_expr->arg_i);
+                               free(p);
+                               if (!i)
+                                       goto out_error;
+                       } else {
+                               conv_expr->arg_i = i;
+                               conv_expr->arg_p = p;
+                       }
                }
        }
        return expr;