]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cfgparse: add two new functions to check arguments count
authorWilly Tarreau <w@1wt.eu>
Wed, 21 Dec 2016 21:41:44 +0000 (22:41 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 21 Dec 2016 22:39:26 +0000 (23:39 +0100)
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.

include/common/cfgparse.h
src/cfgparse.c

index 9e75a6576215d286c857e06deb334fcbe7a8fcca..fd04b14470713df1336181d1efb9e0123e44b4aa 100644 (file)
@@ -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
index ec8f6a1f08a0e4b7cc49e7cfa7378a629ceeca17..771dbe9529e9620484c3bcd070c941ee24710ace 100644 (file)
@@ -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