]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
common error messages
authorJan Engelhardt <jengelh@medozas.de>
Sun, 20 Jan 2008 13:19:40 +0000 (13:19 +0000)
committerPatrick McHardy <kaber@trash.net>
Sun, 20 Jan 2008 13:19:40 +0000 (13:19 +0000)
Error messages vary wildly among modules, and there is a lot of
reundance in it too. Introduce a helper function that does all of
the parameter checking boilerplate and gives unique messages.

Signed-off-by: Jan Engelhardt <jengelh@computergmbh.de>
include/xtables.h
xtables.c

index 728b1aa3f29f9a8385e140107a09315ffade9cbc..5e4b2c7b01f009ca2c4c9a0c57380b8199b782b9 100644 (file)
@@ -220,7 +220,11 @@ enum exittype {
        OTHER_PROBLEM = 1,
        PARAMETER_PROBLEM,
        VERSION_PROBLEM,
-       RESOURCE_PROBLEM
+       RESOURCE_PROBLEM,
+       P_ONLY_ONCE,
+       P_NO_INVERT,
+       P_BAD_VALUE,
+       P_ONE_ACTION,
 };
 
 /* this is a special 64bit data type that is 8-byte aligned */
@@ -229,6 +233,7 @@ enum exittype {
 int check_inverse(const char option[], int *invert, int *optind, int argc);
 void exit_error(enum exittype, const char *, ...)__attribute__((noreturn,
                                                          format(printf,2,3)));
+extern void param_act(unsigned int, const char *, ...);
 extern const char *program_name, *program_version;
 
 #ifdef NO_SHARED_LIBS
index 4313f5ba25eb6edf2721d70c4f25e5a4365dfd08..0a2fdb1df03f6f4be7754e4f2e33279fdab3534d 100644 (file)
--- a/xtables.c
+++ b/xtables.c
@@ -19,6 +19,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <netdb.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -639,3 +640,51 @@ void xtables_register_target(struct xtables_target *me)
        me->t = NULL;
        me->tflags = 0;
 }
+
+void param_act(unsigned int status, const char *p1, ...)
+{
+       const char *p2, *p3;
+       va_list args;
+       bool b;
+
+       va_start(args, p1);
+
+       switch (status) {
+       case P_ONLY_ONCE:
+               p2 = va_arg(args, const char *);
+               b  = va_arg(args, unsigned int);
+               if (!b)
+                       return;
+               exit_error(PARAMETER_PROBLEM,
+                          "%s: \"%s\" option may only be specified once",
+                          p1, p2);
+               break;
+       case P_NO_INVERT:
+               p2 = va_arg(args, const char *);
+               b  = va_arg(args, unsigned int);
+               if (!b)
+                       return;
+               exit_error(PARAMETER_PROBLEM,
+                          "%s: \"%s\" option cannot be inverted", p1, p2);
+               break;
+       case P_BAD_VALUE:
+               p2 = va_arg(args, const char *);
+               p3 = va_arg(args, const char *);
+               exit_error(PARAMETER_PROBLEM,
+                          "%s: Bad value for \"%s\" option: \"%s\"",
+                          p1, p2, p3);
+               break;
+       case P_ONE_ACTION:
+               b = va_arg(args, unsigned int);
+               if (!b)
+                       return;
+               exit_error(PARAMETER_PROBLEM,
+                          "%s: At most one action is possible", p1);
+               break;
+       default:
+               exit_error(status, p1, args);
+               break;
+       }
+
+       va_end(args);
+}