[MINOR] pattern: make the converter more flexible by supporting void* and int args

The pattern type converters currently support a string arg and a length.
Sometimes we'll prefer to pass them a list or a structure. So let's convert
the string and length into a generic void* and int that each converter may
use as it likes.
This commit is contained in:
Willy Tarreau 2010-01-26 17:17:56 +01:00
parent a724d49b6e
commit 1a51b6342e
2 changed files with 10 additions and 11 deletions

View File

@ -54,8 +54,8 @@ struct pattern {
/* pattern conversion */ /* pattern conversion */
struct pattern_conv { struct pattern_conv {
const char *kw; /* configuration keyword */ const char *kw; /* configuration keyword */
int (*process)(const char *arg, int (*process)(const void *arg_p,
int arg_len, int arg_i,
union pattern_data *data); /* process function */ union pattern_data *data); /* process function */
unsigned int in_type; /* input needed pattern type */ unsigned int in_type; /* input needed pattern type */
unsigned int out_type; /* output pattern type */ unsigned int out_type; /* output pattern type */
@ -65,8 +65,8 @@ struct pattern_conv {
struct pattern_conv_expr { struct pattern_conv_expr {
struct list list; /* member of a pattern expression */ struct list list; /* member of a pattern expression */
struct pattern_conv *conv; /* pattern conversion */ struct pattern_conv *conv; /* pattern conversion */
char *arg; /* configured keyword argument */ void *arg_p; /* pointer arg, most often a string argument */
int arg_len; /* configured keyword argument length */ int arg_i; /* int arg, most often the argument's length */
}; };
/* pattern fetch */ /* pattern fetch */

View File

@ -432,10 +432,9 @@ struct pattern_expr *pattern_parse_expr(char **str, int *idx)
conv_expr->conv = conv; conv_expr->conv = conv;
if (end != endw) { if (end != endw) {
conv_expr->arg_len = end - endw - 2; conv_expr->arg_i = end - endw - 2;
conv_expr->arg = malloc(conv_expr->arg_len + 1); conv_expr->arg_p = calloc(1, conv_expr->arg_i + 1);
conv_expr->arg = memcpy(conv_expr->arg, endw + 1, conv_expr->arg_len); memcpy(conv_expr->arg_p, endw + 1, conv_expr->arg_i);
conv_expr->arg[expr->arg_len] = '\0';
} }
} }
return expr; return expr;
@ -471,7 +470,7 @@ struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7,
return NULL; return NULL;
p->type = conv_expr->conv->in_type; p->type = conv_expr->conv->in_type;
if (!conv_expr->conv->process(conv_expr->arg, conv_expr->arg_len, &p->data)) if (!conv_expr->conv->process(conv_expr->arg_p, conv_expr->arg_i, &p->data))
return NULL; return NULL;
p->type = conv_expr->conv->out_type; p->type = conv_expr->conv->out_type;
@ -532,7 +531,7 @@ int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type)
/* Pattern format convert functions */ /* Pattern format convert functions */
/*****************************************************************/ /*****************************************************************/
static int pattern_conv_str2lower(const char *arg, int arg_len, union pattern_data *data) static int pattern_conv_str2lower(const void *arg_p, int arg_i, union pattern_data *data)
{ {
int i; int i;
@ -543,7 +542,7 @@ static int pattern_conv_str2lower(const char *arg, int arg_len, union pattern_da
return 1; return 1;
} }
static int pattern_conv_str2upper(const char *arg, int arg_len, union pattern_data *data) static int pattern_conv_str2upper(const void *arg_p, int arg_i, union pattern_data *data)
{ {
int i; int i;