mirror of
http://git.haproxy.org/git/haproxy.git/
synced 2025-03-21 10:27:36 +00:00
MINOR: cfgparse: add two new functions to check arguments count
We already had alertif_too_many_args{,_idx}(), but these ones are specifically designed for use in cfgparse. Outside of it we're trying to avoid calling Alert() all the time so we need an equivalent using a pointer to an error message. These new functions called too_many_args{,_idx)() do exactly this. They don't take the file name nor the line number which they have no use for but instead they take an optional pointer to an error message and the pointer to the error code is optional as well. With (NULL, NULL) they'll simply check the validity and return a verdict. They are quite convenient for use in isolated keyword parsers. These two new functions as well as the previous ones have all been exported.
This commit is contained in:
parent
bee9dde31f
commit
ece9b07c71
@ -80,6 +80,10 @@ void cfg_restore_sections(struct list *backup_sections);
|
||||
int warnif_misplaced_tcp_conn(struct proxy *proxy, const char *file, int line, const char *arg);
|
||||
int warnif_misplaced_tcp_sess(struct proxy *proxy, const char *file, int line, const char *arg);
|
||||
int warnif_misplaced_tcp_cont(struct proxy *proxy, const char *file, int line, const char *arg);
|
||||
int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code);
|
||||
int too_many_args(int maxarg, char **args, char **msg, int *err_code);
|
||||
int alertif_too_many_args_idx(int maxarg, int index, const char *file, int linenum, char **args, int *err_code);
|
||||
int alertif_too_many_args(int maxarg, const char *file, int linenum, char **args, int *err_code);
|
||||
|
||||
/*
|
||||
* Sends a warning if proxy <proxy> does not have at least one of the
|
||||
|
@ -328,6 +328,44 @@ int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Report an error in <msg> when there are too many arguments. This version is
|
||||
* intended to be used by keyword parsers so that the message will be included
|
||||
* into the general error message. The index is the current keyword in args.
|
||||
* Return 0 if the number of argument is correct, otherwise build a message and
|
||||
* return 1. Fill err_code with an ERR_ALERT and an ERR_FATAL if not null. The
|
||||
* message may also be null, it will simply not be produced (useful to check only).
|
||||
* <msg> and <err_code> are only affected on error.
|
||||
*/
|
||||
int too_many_args_idx(int maxarg, int index, char **args, char **msg, int *err_code)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!*args[index + maxarg + 1])
|
||||
return 0;
|
||||
|
||||
if (msg) {
|
||||
*msg = NULL;
|
||||
memprintf(msg, "%s", args[0]);
|
||||
for (i = 1; i <= index; i++)
|
||||
memprintf(msg, "%s %s", *msg, args[i]);
|
||||
|
||||
memprintf(msg, "'%s' cannot handle unexpected argument '%s'.", *msg, args[index + maxarg + 1]);
|
||||
}
|
||||
if (err_code)
|
||||
*err_code |= ERR_ALERT | ERR_FATAL;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* same as too_many_args_idx with a 0 index
|
||||
*/
|
||||
int too_many_args(int maxarg, char **args, char **msg, int *err_code)
|
||||
{
|
||||
return too_many_args_idx(maxarg, 0, args, msg, err_code);
|
||||
}
|
||||
|
||||
/*
|
||||
* Report a fatal Alert when there is too much arguments
|
||||
* The index is the current keyword in args
|
||||
|
Loading…
Reference in New Issue
Block a user