diff --git a/include/common/cfgparse.h b/include/common/cfgparse.h index 9e75a6576..fd04b1447 100644 --- a/include/common/cfgparse.h +++ b/include/common/cfgparse.h @@ -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 does not have at least one of the diff --git a/src/cfgparse.c b/src/cfgparse.c index ec8f6a1f0..771dbe952 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -328,6 +328,44 @@ int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf, return 0; } +/* + * Report an error in 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). + * and 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